Home | History | Annotate | Line # | Download | only in libsa
loadfile_ecoff.c revision 1.7
      1  1.7   martin /* $NetBSD: loadfile_ecoff.c,v 1.7 2007/06/05 08:48:50 martin Exp $ */
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.1  thorpej  * NASA Ames Research Center and by Christos Zoulas.
     10  1.1  thorpej  *
     11  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.1  thorpej  * modification, are permitted provided that the following conditions
     13  1.1  thorpej  * are met:
     14  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.1  thorpej  *    must display the following acknowledgement:
     21  1.1  thorpej  *	This product includes software developed by the NetBSD
     22  1.1  thorpej  *	Foundation, Inc. and its contributors.
     23  1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1  thorpej  *    contributors may be used to endorse or promote products derived
     25  1.1  thorpej  *    from this software without specific prior written permission.
     26  1.1  thorpej  *
     27  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1  thorpej  */
     39  1.1  thorpej 
     40  1.2  thorpej #ifdef _STANDALONE
     41  1.2  thorpej #include <lib/libsa/stand.h>
     42  1.2  thorpej #include <lib/libkern/libkern.h>
     43  1.5    perry #else
     44  1.2  thorpej #include <stdio.h>
     45  1.2  thorpej #include <string.h>
     46  1.2  thorpej #include <errno.h>
     47  1.2  thorpej #include <stdlib.h>
     48  1.5    perry #include <unistd.h>
     49  1.2  thorpej #include <fcntl.h>
     50  1.2  thorpej #include <err.h>
     51  1.5    perry #endif
     52  1.2  thorpej 
     53  1.5    perry #include <sys/param.h>
     54  1.2  thorpej #include <sys/exec.h>
     55  1.2  thorpej 
     56  1.2  thorpej #include "loadfile.h"
     57  1.2  thorpej 
     58  1.2  thorpej #ifdef BOOT_ECOFF
     59  1.2  thorpej 
     60  1.2  thorpej int
     61  1.2  thorpej loadfile_coff(fd, coff, marks, flags)
     62  1.1  thorpej 	int fd;
     63  1.1  thorpej 	struct ecoff_exechdr *coff;
     64  1.1  thorpej 	u_long *marks;
     65  1.1  thorpej 	int flags;
     66  1.1  thorpej {
     67  1.1  thorpej 	paddr_t offset = marks[MARK_START];
     68  1.1  thorpej 	paddr_t minp = ~0, maxp = 0, pos;
     69  1.4  reinoud 
     70  1.4  reinoud 	/* some ports dont use the offset */
     71  1.4  reinoud 	offset = offset;
     72  1.1  thorpej 
     73  1.1  thorpej 	/* Read in text. */
     74  1.1  thorpej 	if (lseek(fd, ECOFF_TXTOFF(coff), SEEK_SET) == -1)  {
     75  1.1  thorpej 		WARN(("lseek text"));
     76  1.1  thorpej 		return 1;
     77  1.1  thorpej 	}
     78  1.1  thorpej 
     79  1.1  thorpej 	if (coff->a.tsize != 0) {
     80  1.1  thorpej 		if (flags & LOAD_TEXT) {
     81  1.1  thorpej 			PROGRESS(("%lu", coff->a.tsize));
     82  1.1  thorpej 			if (READ(fd, coff->a.text_start, coff->a.tsize) !=
     83  1.1  thorpej 			    coff->a.tsize) {
     84  1.1  thorpej 				return 1;
     85  1.1  thorpej 			}
     86  1.1  thorpej 		}
     87  1.1  thorpej 		else {
     88  1.1  thorpej 			if (lseek(fd, coff->a.tsize, SEEK_CUR) == -1) {
     89  1.1  thorpej 				WARN(("read text"));
     90  1.1  thorpej 				return 1;
     91  1.1  thorpej 			}
     92  1.1  thorpej 		}
     93  1.1  thorpej 		if (flags & (COUNT_TEXT|LOAD_TEXT)) {
     94  1.1  thorpej 			pos = coff->a.text_start;
     95  1.1  thorpej 			if (minp > pos)
     96  1.1  thorpej 				minp = pos;
     97  1.1  thorpej 			pos += coff->a.tsize;
     98  1.1  thorpej 			if (maxp < pos)
     99  1.1  thorpej 				maxp = pos;
    100  1.1  thorpej 		}
    101  1.1  thorpej 	}
    102  1.1  thorpej 
    103  1.1  thorpej 	/* Read in data. */
    104  1.1  thorpej 	if (coff->a.dsize != 0) {
    105  1.1  thorpej 		if (flags & LOAD_DATA) {
    106  1.1  thorpej 			PROGRESS(("+%lu", coff->a.dsize));
    107  1.1  thorpej 			if (READ(fd, coff->a.data_start, coff->a.dsize) !=
    108  1.1  thorpej 			    coff->a.dsize) {
    109  1.1  thorpej 				WARN(("read data"));
    110  1.1  thorpej 				return 1;
    111  1.1  thorpej 			}
    112  1.1  thorpej 		}
    113  1.1  thorpej 		if (flags & (COUNT_DATA|LOAD_DATA)) {
    114  1.1  thorpej 			pos = coff->a.data_start;
    115  1.1  thorpej 			if (minp > pos)
    116  1.1  thorpej 				minp = pos;
    117  1.1  thorpej 			pos += coff->a.dsize;
    118  1.1  thorpej 			if (maxp < pos)
    119  1.1  thorpej 				maxp = pos;
    120  1.1  thorpej 		}
    121  1.1  thorpej 	}
    122  1.1  thorpej 
    123  1.1  thorpej 	/* Zero out bss. */
    124  1.1  thorpej 	if (coff->a.bsize != 0) {
    125  1.1  thorpej 		if (flags & LOAD_BSS) {
    126  1.1  thorpej 			PROGRESS(("+%lu", coff->a.bsize));
    127  1.1  thorpej 			BZERO(coff->a.bss_start, coff->a.bsize);
    128  1.1  thorpej 		}
    129  1.1  thorpej 		if (flags & (COUNT_BSS|LOAD_BSS)) {
    130  1.1  thorpej 			pos = coff->a.bss_start;
    131  1.1  thorpej 			if (minp > pos)
    132  1.1  thorpej 				minp = pos;
    133  1.1  thorpej 			pos = coff->a.bsize;
    134  1.1  thorpej 			if (maxp < pos)
    135  1.1  thorpej 				maxp = pos;
    136  1.1  thorpej 		}
    137  1.1  thorpej 	}
    138  1.1  thorpej 
    139  1.1  thorpej 	marks[MARK_START] = LOADADDR(minp);
    140  1.1  thorpej 	marks[MARK_ENTRY] = LOADADDR(coff->a.entry);
    141  1.1  thorpej 	marks[MARK_NSYM] = 1;	/* XXX: Kernel needs >= 0 */
    142  1.1  thorpej 	marks[MARK_SYM] = LOADADDR(maxp);
    143  1.1  thorpej 	marks[MARK_END] = LOADADDR(maxp);
    144  1.7   martin 	marks[MARK_DATA] = LOADADDR(coff->a.data);
    145  1.1  thorpej 	return 0;
    146  1.1  thorpej }
    147  1.2  thorpej 
    148  1.2  thorpej #endif /* BOOT_ECOFF */
    149