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