Home | History | Annotate | Line # | Download | only in libsa
loadfile_aout.c revision 1.10
      1 /* $NetBSD: loadfile_aout.c,v 1.10 2007/11/23 04:32:14 isaki 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 	ssize_t nr;
    109 
    110 	/* some ports dont use the offset */
    111 	offset = offset;
    112 
    113 	/* In OMAGIC and NMAGIC, exec header isn't part of text segment */
    114 	if (magic == OMAGIC || magic == NMAGIC)
    115 		sub = 0;
    116 	else
    117 		sub = sizeof(*x);
    118 
    119 	minp = maxp = ALIGNENTRY(entry);
    120 
    121 	if (lseek(fd, sizeof(*x), SEEK_SET) == -1)  {
    122 		WARN(("lseek text"));
    123 		return 1;
    124 	}
    125 
    126 	/*
    127 	 * Leave a copy of the exec header before the text.
    128 	 * The kernel may use this to verify that the
    129 	 * symbols were loaded by this boot program.
    130 	 */
    131 	if (magic == OMAGIC || magic == NMAGIC) {
    132 		if (flags & LOAD_HDR && maxp >= sizeof(*x))
    133 			BCOPY(x, maxp - sizeof(*x), sizeof(*x));
    134 	}
    135 	else {
    136 		if (flags & LOAD_HDR)
    137 			BCOPY(x, maxp, sizeof(*x));
    138 		if (flags & (LOAD_HDR|COUNT_HDR))
    139 			maxp += sizeof(*x);
    140 	}
    141 
    142 	/*
    143 	 * Read in the text segment.
    144 	 */
    145 	if (flags & LOAD_TEXT) {
    146 		PROGRESS(("%ld", x->a_text));
    147 
    148 		nr = READ(fd, maxp, x->a_text - sub);
    149 		if (nr == -1) {
    150 			WARN(("read text"));
    151 			return 1;
    152 		}
    153 		if (nr != (ssize_t)(x->a_text - sub)) {
    154 			errno = ESHORT;
    155 			WARN(("read text"));
    156 			return 1;
    157 		}
    158 	} else {
    159 		if (lseek(fd, x->a_text - sub, SEEK_CUR) == -1) {
    160 			WARN(("seek text"));
    161 			return 1;
    162 		}
    163 	}
    164 	if (flags & (LOAD_TEXT|COUNT_TEXT))
    165 		maxp += x->a_text - sub;
    166 
    167 	/*
    168 	 * Provide alignment if required
    169 	 */
    170 	if (magic == ZMAGIC || magic == NMAGIC) {
    171 		int size = -(unsigned int)maxp & (AOUT_LDPGSZ - 1);
    172 
    173 		if (flags & LOAD_TEXTA) {
    174 			PROGRESS(("/%d", size));
    175 			BZERO(maxp, size);
    176 		}
    177 
    178 		if (flags & (LOAD_TEXTA|COUNT_TEXTA))
    179 			maxp += size;
    180 	}
    181 
    182 	/*
    183 	 * Read in the data segment.
    184 	 */
    185 	if (flags & LOAD_DATA) {
    186 		PROGRESS(("+%ld", x->a_data));
    187 
    188 		marks[MARK_DATA] = LOADADDR(maxp);
    189 		nr = READ(fd, maxp, x->a_data);
    190 		if (nr == -1) {
    191 			WARN(("read data"));
    192 			return 1;
    193 		}
    194 		if (nr != (ssize_t)x->a_data) {
    195 			errno = ESHORT;
    196 			WARN(("read data"));
    197 			return 1;
    198 		}
    199 	}
    200 	else {
    201 		if (lseek(fd, x->a_data, SEEK_CUR) == -1) {
    202 			WARN(("seek data"));
    203 			return 1;
    204 		}
    205 	}
    206 	if (flags & (LOAD_DATA|COUNT_DATA))
    207 		maxp += x->a_data;
    208 
    209 	/*
    210 	 * Zero out the BSS section.
    211 	 * (Kernel doesn't care, but do it anyway.)
    212 	 */
    213 	if (flags & LOAD_BSS) {
    214 		PROGRESS(("+%ld", x->a_bss));
    215 
    216 		BZERO(maxp, x->a_bss);
    217 	}
    218 
    219 	if (flags & (LOAD_BSS|COUNT_BSS))
    220 		maxp += x->a_bss;
    221 
    222 	/*
    223 	 * Read in the symbol table and strings.
    224 	 * (Always set the symtab size word.)
    225 	 */
    226 	if (flags & LOAD_SYM)
    227 		BCOPY(&x->a_syms, maxp, sizeof(x->a_syms));
    228 
    229 	if (flags & (LOAD_SYM|COUNT_SYM)) {
    230 		maxp += sizeof(x->a_syms);
    231 		aoutp = maxp;
    232 	}
    233 
    234 	if (x->a_syms > 0) {
    235 		/* Symbol table and string table length word. */
    236 
    237 		if (flags & LOAD_SYM) {
    238 			PROGRESS(("+[%ld", x->a_syms));
    239 
    240 			nr = READ(fd, maxp, x->a_syms);
    241 			if (nr == -1) {
    242 				WARN(("read symbols"));
    243 				return 1;
    244 			}
    245 			if (nr != (ssize_t)x->a_syms) {
    246 				errno = ESHORT;
    247 				WARN(("read symbols"));
    248 				return 1;
    249 			}
    250 		} else  {
    251 			if (lseek(fd, x->a_syms, SEEK_CUR) == -1) {
    252 				WARN(("seek symbols"));
    253 				return 1;
    254 			}
    255 		}
    256 		if (flags & (LOAD_SYM|COUNT_SYM))
    257 			maxp += x->a_syms;
    258 
    259 		nr = read(fd, &cc, sizeof(cc));
    260 		if (nr == -1) {
    261 			WARN(("read string table"));
    262 			return 1;
    263 		}
    264 		if (nr != sizeof(cc)) {
    265 			errno = ESHORT;
    266 			WARN(("read string table"));
    267 			return 1;
    268 		}
    269 
    270 		if (flags & LOAD_SYM) {
    271 			BCOPY(&cc, maxp, sizeof(cc));
    272 
    273 			/* String table. Length word includes itself. */
    274 
    275 			PROGRESS(("+%d]", cc));
    276 		}
    277 		if (flags & (LOAD_SYM|COUNT_SYM))
    278 			maxp += sizeof(cc);
    279 
    280 		cc -= sizeof(int);
    281 		if (cc <= 0) {
    282 			WARN(("symbol table too short"));
    283 			return 1;
    284 		}
    285 
    286 		if (flags & LOAD_SYM) {
    287 			nr = READ(fd, maxp, cc);
    288 			if (nr == -1) {
    289 				WARN(("read strings"));
    290 				return 1;
    291 			}
    292 			if (nr != cc) {
    293 				errno = ESHORT;
    294 				WARN(("read strings"));
    295 				return 1;
    296 			}
    297 		} else {
    298 			if (lseek(fd, cc, SEEK_CUR) == -1) {
    299 				WARN(("seek strings"));
    300 				return 1;
    301 			}
    302 		}
    303 		if (flags & (LOAD_SYM|COUNT_SYM))
    304 			maxp += cc;
    305 	}
    306 
    307 	marks[MARK_START] = LOADADDR(minp);
    308 	marks[MARK_ENTRY] = LOADADDR(entry);
    309 	marks[MARK_NSYM] = x->a_syms;
    310 	marks[MARK_SYM] = LOADADDR(aoutp);
    311 	marks[MARK_END] = LOADADDR(maxp);
    312 	return 0;
    313 }
    314 
    315 #endif /* BOOT_AOUT */
    316