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