Home | History | Annotate | Line # | Download | only in libtos
aout.c revision 1.8
      1  1.8  junyoung /*	$NetBSD: aout.c,v 1.8 2005/06/28 21:03:02 junyoung Exp $	*/
      2  1.1       leo 
      3  1.1       leo /*-
      4  1.1       leo  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1       leo  * All rights reserved.
      6  1.1       leo  *
      7  1.1       leo  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1       leo  * by Leo Weppelman.
      9  1.1       leo  *
     10  1.1       leo  * Redistribution and use in source and binary forms, with or without
     11  1.1       leo  * modification, are permitted provided that the following conditions
     12  1.1       leo  * are met:
     13  1.1       leo  * 1. Redistributions of source code must retain the above copyright
     14  1.1       leo  *    notice, this list of conditions and the following disclaimer.
     15  1.1       leo  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1       leo  *    notice, this list of conditions and the following disclaimer in the
     17  1.1       leo  *    documentation and/or other materials provided with the distribution.
     18  1.1       leo  * 3. All advertising materials mentioning features or use of this software
     19  1.1       leo  *    must display the following acknowledgement:
     20  1.1       leo  *        This product includes software developed by the NetBSD
     21  1.1       leo  *        Foundation, Inc. and its contributors.
     22  1.1       leo  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1       leo  *    contributors may be used to endorse or promote products derived
     24  1.1       leo  *    from this software without specific prior written permission.
     25  1.1       leo  *
     26  1.1       leo  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1       leo  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1       leo  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1       leo  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1       leo  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1       leo  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1       leo  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1       leo  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1       leo  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1       leo  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1       leo  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1       leo  */
     38  1.2       leo 
     39  1.3       leo 
     40  1.3       leo #ifdef TOSTOOLS
     41  1.1       leo #include <stdio.h>
     42  1.1       leo #include <stdlib.h>
     43  1.4       leo #include <unistd.h>
     44  1.1       leo #include <string.h>
     45  1.1       leo #include <sys/types.h>
     46  1.3       leo #include <a_out.h>
     47  1.3       leo 
     48  1.3       leo #define	MALLOC(x)	malloc(x)
     49  1.1       leo 
     50  1.1       leo #else
     51  1.3       leo 
     52  1.8  junyoung #include <lib/libsa/stand.h>
     53  1.3       leo #include <atari_stand.h>
     54  1.3       leo #include <string.h>
     55  1.3       leo #include <libkern.h>
     56  1.1       leo #include <sys/exec_aout.h>
     57  1.3       leo 
     58  1.3       leo #define	MALLOC(x)	alloc(x)
     59  1.1       leo #endif
     60  1.3       leo 
     61  1.5       leo #include "libtos.h"
     62  1.5       leo #include "kparamb.h"
     63  1.1       leo #include "tosdefs.h"
     64  1.2       leo #include "cread.h"
     65  1.2       leo 
     66  1.1       leo 
     67  1.1       leo #ifdef TOSTOOLS
     68  1.1       leo /*
     69  1.1       leo  * Assume compiling under TOS or MINT. The page-size will always
     70  1.1       leo  * be incorrect then (if it is defined anyway).
     71  1.1       leo  */
     72  1.6   thorpej #ifdef AOUT_LDPGSZ
     73  1.6   thorpej #undef AOUT_LDPGSZ
     74  1.1       leo #endif
     75  1.1       leo 
     76  1.6   thorpej #define AOUT_LDPGSZ	(8*1024)	/* Page size for NetBSD		*/
     77  1.1       leo 
     78  1.1       leo #endif /* TOSTOOLS */
     79  1.1       leo 
     80  1.1       leo /*
     81  1.1       leo  * Load an a.out image.
     82  1.1       leo  * Exit codes:
     83  1.1       leo  *	-1      : Not an a.outfile
     84  1.1       leo  *	 0      : OK
     85  1.1       leo  *	 error# : Error during load (*errp might contain error string).
     86  1.1       leo  */
     87  1.1       leo int
     88  1.7  junyoung aout_load(int fd, osdsc_t *od, char **errp, int loadsyms)
     89  1.1       leo {
     90  1.1       leo 	long		textsz, stringsz;
     91  1.1       leo 	struct exec	ehdr;
     92  1.1       leo 	int		err;
     93  1.1       leo 
     94  1.1       leo 	*errp = NULL;
     95  1.1       leo 
     96  1.1       leo 	lseek(fd, (off_t)0, SEEK_SET);
     97  1.1       leo 	if (read(fd, (char *)&ehdr, sizeof(ehdr)) != sizeof(ehdr))
     98  1.1       leo 		return -1;
     99  1.1       leo 
    100  1.3       leo #ifdef TOSTOOLS
    101  1.3       leo 	if ((ehdr.a_magic & 0xffff) != NMAGIC)
    102  1.3       leo 		return -1;
    103  1.3       leo #else
    104  1.3       leo 	if ((N_GETMAGIC(ehdr) != NMAGIC) && (N_GETMAGIC(ehdr) != OMAGIC))
    105  1.1       leo 		return -1;
    106  1.3       leo #endif
    107  1.1       leo 
    108  1.1       leo 	/*
    109  1.1       leo 	 * Extract various sizes from the kernel executable
    110  1.1       leo 	 */
    111  1.6   thorpej 	textsz     = (ehdr.a_text + AOUT_LDPGSZ - 1) & ~(AOUT_LDPGSZ - 1);
    112  1.1       leo 	od->k_esym = 0;
    113  1.1       leo 	od->ksize  = textsz + ehdr.a_data + ehdr.a_bss;
    114  1.1       leo 	od->kentry = ehdr.a_entry;
    115  1.1       leo 
    116  1.1       leo 	if (loadsyms && ehdr.a_syms) {
    117  1.7  junyoung 		err = 1;
    118  1.7  junyoung 		if (lseek(fd, ehdr.a_text+ehdr.a_data+ehdr.a_syms+sizeof(ehdr),
    119  1.7  junyoung 			  0) <= 0)
    120  1.7  junyoung 			goto error;
    121  1.7  junyoung 		err = 2;
    122  1.7  junyoung 		if (read(fd, (char *)&stringsz, sizeof(long)) != sizeof(long))
    123  1.7  junyoung 			goto error;
    124  1.7  junyoung 		err = 3;
    125  1.7  junyoung 		if (lseek(fd, sizeof(ehdr), 0) <= 0)
    126  1.7  junyoung 			goto error;
    127  1.7  junyoung 		od->ksize += ehdr.a_syms + sizeof(long) + stringsz;
    128  1.1       leo 	}
    129  1.1       leo 
    130  1.1       leo 	err = 4;
    131  1.3       leo 	if ((od->kstart = (u_char *)MALLOC(od->ksize)) == NULL)
    132  1.1       leo 		goto error;
    133  1.1       leo 
    134  1.1       leo 	/*
    135  1.1       leo 	 * Read text & data, clear bss
    136  1.1       leo 	 */
    137  1.1       leo 	err = 5;
    138  1.1       leo 	if ((read(fd, (char *)(od->kstart), ehdr.a_text) != ehdr.a_text)
    139  1.1       leo 	    ||(read(fd,(char *)(od->kstart+textsz),ehdr.a_data) != ehdr.a_data))
    140  1.1       leo 		goto error;
    141  1.1       leo 	bzero(od->kstart + textsz + ehdr.a_data, ehdr.a_bss);
    142  1.1       leo 
    143  1.1       leo 	/*
    144  1.1       leo 	 * Read symbol and string table
    145  1.1       leo 	 */
    146  1.1       leo 	if (loadsyms && ehdr.a_syms) {
    147  1.7  junyoung 		long	*p;
    148  1.1       leo 
    149  1.7  junyoung 		p = (long *)((od->kstart) + textsz + ehdr.a_data + ehdr.a_bss);
    150  1.7  junyoung 		*p++ = ehdr.a_syms;
    151  1.7  junyoung 		err = 6;
    152  1.7  junyoung 		if (read(fd, (char *)p, ehdr.a_syms) != ehdr.a_syms)
    153  1.7  junyoung 			goto error;
    154  1.7  junyoung 		p = (long *)((char *)p + ehdr.a_syms);
    155  1.7  junyoung 		err = 7;
    156  1.7  junyoung 		if (read(fd, (char *)p, stringsz) != stringsz)
    157  1.7  junyoung 			goto error;
    158  1.7  junyoung 		od->k_esym = (long)((char *)p-(char *)od->kstart +stringsz);
    159  1.1       leo 	}
    160  1.1       leo 	return 0;
    161  1.1       leo 
    162  1.1       leo error:
    163  1.1       leo #ifdef TOSTOOLS
    164  1.1       leo 	{
    165  1.1       leo 		static char *errs[] = {
    166  1.1       leo 			/* 1 */ "Cannot seek to string table",
    167  1.1       leo 			/* 2 */ "Cannot read string-table size",
    168  1.1       leo 			/* 3 */ "Cannot seek back to text start",
    169  1.1       leo 			/* 4 */ "Cannot malloc kernel image space",
    170  1.1       leo 			/* 5 */ "Unable to read kernel image",
    171  1.1       leo 			/* 6 */ "Cannot read symbol table",
    172  1.1       leo 			/* 7 */ "Cannot read string table"
    173  1.1       leo 		};
    174  1.1       leo 		*errp = errs[err];
    175  1.1       leo 	}
    176  1.1       leo #endif /* TOSTOOLS */
    177  1.1       leo 
    178  1.1       leo 	return err;
    179  1.1       leo }
    180