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