Home | History | Annotate | Line # | Download | only in libsa
loadfile_aout.c revision 1.5
      1 /* $NetBSD: loadfile_aout.c,v 1.5 2003/08/07 16:32:28 agc Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center and by Christos Zoulas.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1992, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * This code is derived from software contributed to Berkeley by
     45  * Ralph Campbell.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
     72  */
     73 
     74 #ifdef _STANDALONE
     75 #include <lib/libsa/stand.h>
     76 #include <lib/libkern/libkern.h>
     77 #else
     78 #include <stdio.h>
     79 #include <string.h>
     80 #include <errno.h>
     81 #include <stdlib.h>
     82 #include <unistd.h>
     83 #include <fcntl.h>
     84 #include <err.h>
     85 #endif
     86 
     87 #include <sys/param.h>
     88 #include <sys/exec.h>
     89 
     90 #include "loadfile.h"
     91 
     92 #ifdef BOOT_AOUT
     93 
     94 int
     95 loadfile_aout(fd, x, marks, flags)
     96 	int fd;
     97 	struct exec *x;
     98 	u_long *marks;
     99 	int flags;
    100 {
    101 	u_long entry = x->a_entry;
    102 	paddr_t aoutp = 0;
    103 	paddr_t minp, maxp;
    104 	int cc;
    105 	paddr_t offset = marks[MARK_START];
    106 	u_long magic = N_GETMAGIC(*x);
    107 	int sub;
    108 
    109 	/* some ports dont use the offset */
    110 	offset = offset;
    111 
    112 	/* In OMAGIC and NMAGIC, exec header isn't part of text segment */
    113 	if (magic == OMAGIC || magic == NMAGIC)
    114 		sub = 0;
    115 	else
    116 		sub = sizeof(*x);
    117 
    118 	minp = maxp = ALIGNENTRY(entry);
    119 
    120 	if (lseek(fd, sizeof(*x), SEEK_SET) == -1)  {
    121 		WARN(("lseek text"));
    122 		return 1;
    123 	}
    124 
    125 	/*
    126 	 * Leave a copy of the exec header before the text.
    127 	 * The kernel may use this to verify that the
    128 	 * symbols were loaded by this boot program.
    129 	 */
    130 	if (magic == OMAGIC || magic == NMAGIC) {
    131 		if (flags & LOAD_HDR && maxp >= sizeof(*x))
    132 			BCOPY(x, maxp - sizeof(*x), sizeof(*x));
    133 	}
    134 	else {
    135 		if (flags & LOAD_HDR)
    136 			BCOPY(x, maxp, sizeof(*x));
    137 		if (flags & (LOAD_HDR|COUNT_HDR))
    138 			maxp += sizeof(*x);
    139 	}
    140 
    141 	/*
    142 	 * Read in the text segment.
    143 	 */
    144 	if (flags & LOAD_TEXT) {
    145 		PROGRESS(("%ld", x->a_text));
    146 
    147 		if (READ(fd, maxp, x->a_text - sub) != x->a_text - sub) {
    148 			WARN(("read text"));
    149 			return 1;
    150 		}
    151 	} else {
    152 		if (lseek(fd, x->a_text - sub, SEEK_CUR) == -1) {
    153 			WARN(("seek text"));
    154 			return 1;
    155 		}
    156 	}
    157 	if (flags & (LOAD_TEXT|COUNT_TEXT))
    158 		maxp += x->a_text - sub;
    159 
    160 	/*
    161 	 * Provide alignment if required
    162 	 */
    163 	if (magic == ZMAGIC || magic == NMAGIC) {
    164 		int size = -(unsigned int)maxp & (AOUT_LDPGSZ - 1);
    165 
    166 		if (flags & LOAD_TEXTA) {
    167 			PROGRESS(("/%d", size));
    168 			BZERO(maxp, size);
    169 		}
    170 
    171 		if (flags & (LOAD_TEXTA|COUNT_TEXTA))
    172 			maxp += size;
    173 	}
    174 
    175 	/*
    176 	 * Read in the data segment.
    177 	 */
    178 	if (flags & LOAD_DATA) {
    179 		PROGRESS(("+%ld", x->a_data));
    180 
    181 		if (READ(fd, maxp, x->a_data) != x->a_data) {
    182 			WARN(("read data"));
    183 			return 1;
    184 		}
    185 	}
    186 	else {
    187 		if (lseek(fd, x->a_data, SEEK_CUR) == -1) {
    188 			WARN(("seek data"));
    189 			return 1;
    190 		}
    191 	}
    192 	if (flags & (LOAD_DATA|COUNT_DATA))
    193 		maxp += x->a_data;
    194 
    195 	/*
    196 	 * Zero out the BSS section.
    197 	 * (Kernel doesn't care, but do it anyway.)
    198 	 */
    199 	if (flags & LOAD_BSS) {
    200 		PROGRESS(("+%ld", x->a_bss));
    201 
    202 		BZERO(maxp, x->a_bss);
    203 	}
    204 
    205 	if (flags & (LOAD_BSS|COUNT_BSS))
    206 		maxp += x->a_bss;
    207 
    208 	/*
    209 	 * Read in the symbol table and strings.
    210 	 * (Always set the symtab size word.)
    211 	 */
    212 	if (flags & LOAD_SYM)
    213 		BCOPY(&x->a_syms, maxp, sizeof(x->a_syms));
    214 
    215 	if (flags & (LOAD_SYM|COUNT_SYM)) {
    216 		maxp += sizeof(x->a_syms);
    217 		aoutp = maxp;
    218 	}
    219 
    220 	if (x->a_syms > 0) {
    221 		/* Symbol table and string table length word. */
    222 
    223 		if (flags & LOAD_SYM) {
    224 			PROGRESS(("+[%ld", x->a_syms));
    225 
    226 			if (READ(fd, maxp, x->a_syms) != x->a_syms) {
    227 				WARN(("read symbols"));
    228 				return 1;
    229 			}
    230 		} else  {
    231 			if (lseek(fd, x->a_syms, SEEK_CUR) == -1) {
    232 				WARN(("seek symbols"));
    233 				return 1;
    234 			}
    235 		}
    236 		if (flags & (LOAD_SYM|COUNT_SYM))
    237 			maxp += x->a_syms;
    238 
    239 		if (read(fd, &cc, sizeof(cc)) != sizeof(cc)) {
    240 			WARN(("read string table"));
    241 			return 1;
    242 		}
    243 
    244 		if (flags & LOAD_SYM) {
    245 			BCOPY(&cc, maxp, sizeof(cc));
    246 
    247 			/* String table. Length word includes itself. */
    248 
    249 			PROGRESS(("+%d]", cc));
    250 		}
    251 		if (flags & (LOAD_SYM|COUNT_SYM))
    252 			maxp += sizeof(cc);
    253 
    254 		cc -= sizeof(int);
    255 		if (cc <= 0) {
    256 			WARN(("symbol table too short"));
    257 			return 1;
    258 		}
    259 
    260 		if (flags & LOAD_SYM) {
    261 			if (READ(fd, maxp, cc) != cc) {
    262 				WARN(("read strings"));
    263 				return 1;
    264 			}
    265 		} else {
    266 			if (lseek(fd, cc, SEEK_CUR) == -1) {
    267 				WARN(("seek strings"));
    268 				return 1;
    269 			}
    270 		}
    271 		if (flags & (LOAD_SYM|COUNT_SYM))
    272 			maxp += cc;
    273 	}
    274 
    275 	marks[MARK_START] = LOADADDR(minp);
    276 	marks[MARK_ENTRY] = LOADADDR(entry);
    277 	marks[MARK_NSYM] = x->a_syms;
    278 	marks[MARK_SYM] = LOADADDR(aoutp);
    279 	marks[MARK_END] = LOADADDR(maxp);
    280 	return 0;
    281 }
    282 
    283 #endif /* BOOT_AOUT */
    284