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