Home | History | Annotate | Line # | Download | only in crunchide
exec_aout.c revision 1.6
      1  1.6  perry /*	$NetBSD: exec_aout.c,v 1.6 1997/08/02 21:30:17 perry Exp $	*/
      2  1.1    cgd /*
      3  1.1    cgd  * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
      4  1.1    cgd  * Copyright (c) 1994 University of Maryland
      5  1.1    cgd  * All Rights Reserved.
      6  1.1    cgd  *
      7  1.1    cgd  * Permission to use, copy, modify, distribute, and sell this software and its
      8  1.1    cgd  * documentation for any purpose is hereby granted without fee, provided that
      9  1.1    cgd  * the above copyright notice appear in all copies and that both that
     10  1.1    cgd  * copyright notice and this permission notice appear in supporting
     11  1.1    cgd  * documentation, and that the name of U.M. not be used in advertising or
     12  1.1    cgd  * publicity pertaining to distribution of the software without specific,
     13  1.1    cgd  * written prior permission.  U.M. makes no representations about the
     14  1.1    cgd  * suitability of this software for any purpose.  It is provided "as is"
     15  1.1    cgd  * without express or implied warranty.
     16  1.1    cgd  *
     17  1.1    cgd  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
     18  1.1    cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
     19  1.1    cgd  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     20  1.1    cgd  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
     21  1.1    cgd  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
     22  1.1    cgd  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     23  1.1    cgd  *
     24  1.1    cgd  * Author: James da Silva, Systems Design and Analysis Group
     25  1.1    cgd  *			   Computer Science Department
     26  1.1    cgd  *			   University of Maryland at College Park
     27  1.1    cgd  */
     28  1.6  perry #include <sys/cdefs.h>
     29  1.6  perry #ifndef lint
     30  1.6  perry __RCSID("$NetBSD: exec_aout.c,v 1.6 1997/08/02 21:30:17 perry Exp $");
     31  1.6  perry #endif
     32  1.6  perry 
     33  1.1    cgd #include <unistd.h>
     34  1.1    cgd #include <stdio.h>
     35  1.1    cgd #include <stdlib.h>
     36  1.1    cgd #include <string.h>
     37  1.1    cgd #include <a.out.h>
     38  1.1    cgd #include <sys/types.h>
     39  1.1    cgd #include <sys/stat.h>
     40  1.1    cgd #include <sys/errno.h>
     41  1.1    cgd 
     42  1.1    cgd #include "extern.h"
     43  1.1    cgd 
     44  1.2    cgd #if defined(NLIST_AOUT)
     45  1.2    cgd 
     46  1.1    cgd int nsyms, ntextrel, ndatarel;
     47  1.1    cgd struct exec *hdrp;
     48  1.1    cgd char *aoutdata, *strbase;
     49  1.1    cgd struct relocation_info *textrel, *datarel;
     50  1.1    cgd struct nlist *symbase;
     51  1.1    cgd 
     52  1.1    cgd 
     53  1.5    cgd #define SYMSTR(sp)	(&strbase[(sp)->n_un.n_strx])
     54  1.1    cgd 
     55  1.1    cgd /* is the symbol a global symbol defined in the current file? */
     56  1.1    cgd #define IS_GLOBAL_DEFINED(sp) \
     57  1.1    cgd                   (((sp)->n_type & N_EXT) && ((sp)->n_type & N_TYPE) != N_UNDF)
     58  1.1    cgd 
     59  1.1    cgd #ifdef __sparc
     60  1.1    cgd /* is the relocation entry dependent on a symbol? */
     61  1.1    cgd #define IS_SYMBOL_RELOC(rp)   \
     62  1.1    cgd 	((rp)->r_extern || \
     63  1.1    cgd 	((rp)->r_type >= RELOC_BASE10 && (rp)->r_type <= RELOC_BASE22) || \
     64  1.1    cgd 	(rp)->r_type == RELOC_JMP_TBL)
     65  1.1    cgd #else
     66  1.1    cgd /* is the relocation entry dependent on a symbol? */
     67  1.1    cgd #define IS_SYMBOL_RELOC(rp)   \
     68  1.1    cgd                   ((rp)->r_extern||(rp)->r_baserel||(rp)->r_jmptable)
     69  1.1    cgd #endif
     70  1.1    cgd 
     71  1.1    cgd static void check_reloc(const char *filename, struct relocation_info *relp);
     72  1.1    cgd 
     73  1.1    cgd int check_aout(int inf, const char *filename)
     74  1.1    cgd {
     75  1.1    cgd     struct stat infstat;
     76  1.1    cgd     struct exec eh;
     77  1.1    cgd 
     78  1.1    cgd     /*
     79  1.1    cgd      * check the header to make sure it's an a.out-format file.
     80  1.1    cgd      */
     81  1.1    cgd 
     82  1.1    cgd     if(fstat(inf, &infstat) == -1)
     83  1.1    cgd 	return 0;
     84  1.1    cgd     if(infstat.st_size < sizeof eh)
     85  1.1    cgd 	return 0;
     86  1.2    cgd     if(read(inf, &eh, sizeof eh) != sizeof eh)
     87  1.1    cgd 	return 0;
     88  1.1    cgd 
     89  1.3    cgd     if(N_BADMAG(eh))
     90  1.1    cgd 	return 0;
     91  1.1    cgd 
     92  1.1    cgd     return 1;
     93  1.1    cgd }
     94  1.1    cgd 
     95  1.2    cgd int hide_aout(int inf, const char *filename)
     96  1.1    cgd {
     97  1.1    cgd     struct stat infstat;
     98  1.1    cgd     struct relocation_info *relp;
     99  1.1    cgd     struct nlist *symp;
    100  1.1    cgd     int rc;
    101  1.1    cgd 
    102  1.1    cgd     /*
    103  1.1    cgd      * do some error checking.
    104  1.1    cgd      */
    105  1.1    cgd 
    106  1.1    cgd     if(fstat(inf, &infstat) == -1) {
    107  1.1    cgd 	perror(filename);
    108  1.2    cgd 	return 1;
    109  1.1    cgd     }
    110  1.1    cgd 
    111  1.1    cgd     /*
    112  1.1    cgd      * Read the entire file into memory.  XXX - Really, we only need to
    113  1.1    cgd      * read the header and from TRELOFF to the end of the file.
    114  1.1    cgd      */
    115  1.1    cgd 
    116  1.1    cgd     if((aoutdata = (char *) malloc(infstat.st_size)) == NULL) {
    117  1.1    cgd 	fprintf(stderr, "%s: too big to read into memory\n", filename);
    118  1.2    cgd 	return 1;
    119  1.1    cgd     }
    120  1.1    cgd 
    121  1.1    cgd     if((rc = read(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
    122  1.1    cgd 	fprintf(stderr, "%s: read error: %s\n", filename,
    123  1.1    cgd 		rc == -1? strerror(errno) : "short read");
    124  1.2    cgd 	return 1;
    125  1.1    cgd     }
    126  1.1    cgd 
    127  1.1    cgd     /*
    128  1.3    cgd      * Calculate offsets and sizes from the header.
    129  1.1    cgd      */
    130  1.1    cgd 
    131  1.1    cgd     hdrp = (struct exec *) aoutdata;
    132  1.1    cgd 
    133  1.1    cgd #ifdef __FreeBSD__
    134  1.1    cgd     textrel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp));
    135  1.1    cgd     datarel = (struct relocation_info *) (aoutdata + N_RELOFF(*hdrp) +
    136  1.1    cgd 					  hdrp->a_trsize);
    137  1.1    cgd #else
    138  1.1    cgd     textrel = (struct relocation_info *) (aoutdata + N_TRELOFF(*hdrp));
    139  1.1    cgd     datarel = (struct relocation_info *) (aoutdata + N_DRELOFF(*hdrp));
    140  1.1    cgd #endif
    141  1.1    cgd     symbase = (struct nlist *)		 (aoutdata + N_SYMOFF(*hdrp));
    142  1.1    cgd     strbase = (char *) 			 (aoutdata + N_STROFF(*hdrp));
    143  1.1    cgd 
    144  1.1    cgd     ntextrel = hdrp->a_trsize / sizeof(struct relocation_info);
    145  1.1    cgd     ndatarel = hdrp->a_drsize / sizeof(struct relocation_info);
    146  1.1    cgd     nsyms    = hdrp->a_syms   / sizeof(struct nlist);
    147  1.1    cgd 
    148  1.1    cgd     /*
    149  1.1    cgd      * Zap the type field of all globally-defined symbols.  The linker will
    150  1.1    cgd      * subsequently ignore these entries.  Don't zap any symbols in the
    151  1.1    cgd      * keep list.
    152  1.1    cgd      */
    153  1.1    cgd 
    154  1.4    cgd     for(symp = symbase; symp < symbase + nsyms; symp++) {
    155  1.4    cgd 	if(!IS_GLOBAL_DEFINED(symp))		/* keep undefined syms */
    156  1.4    cgd 	    continue;
    157  1.4    cgd 
    158  1.4    cgd 	/* keep (C) symbols which are on the keep list */
    159  1.4    cgd 	if(SYMSTR(symp)[0] == '_' && in_keep_list(SYMSTR(symp) + 1))
    160  1.4    cgd 	    continue;
    161  1.4    cgd 
    162  1.4    cgd 	symp->n_type = 0;
    163  1.4    cgd     }
    164  1.1    cgd 
    165  1.1    cgd     /*
    166  1.1    cgd      * Check whether the relocation entries reference any symbols that we
    167  1.1    cgd      * just zapped.  I don't know whether ld can handle this case, but I
    168  1.1    cgd      * haven't encountered it yet.  These checks are here so that the program
    169  1.1    cgd      * doesn't fail silently should such symbols be encountered.
    170  1.1    cgd      */
    171  1.1    cgd 
    172  1.1    cgd     for(relp = textrel; relp < textrel + ntextrel; relp++)
    173  1.1    cgd 	check_reloc(filename, relp);
    174  1.1    cgd     for(relp = datarel; relp < datarel + ndatarel; relp++)
    175  1.1    cgd 	check_reloc(filename, relp);
    176  1.1    cgd 
    177  1.1    cgd     /*
    178  1.1    cgd      * Write the .o file back out to disk.  XXX - Really, we only need to
    179  1.1    cgd      * write the symbol table entries back out.
    180  1.1    cgd      */
    181  1.1    cgd     lseek(inf, 0, SEEK_SET);
    182  1.1    cgd     if((rc = write(inf, aoutdata, infstat.st_size)) < infstat.st_size) {
    183  1.1    cgd 	fprintf(stderr, "%s: write error: %s\n", filename,
    184  1.1    cgd 		rc == -1? strerror(errno) : "short write");
    185  1.2    cgd 	return 1;
    186  1.1    cgd     }
    187  1.2    cgd 
    188  1.2    cgd     return 0;
    189  1.1    cgd }
    190  1.1    cgd 
    191  1.1    cgd 
    192  1.1    cgd static void check_reloc(const char *filename, struct relocation_info *relp)
    193  1.1    cgd {
    194  1.1    cgd     /* bail out if we zapped a symbol that is needed */
    195  1.1    cgd     if(IS_SYMBOL_RELOC(relp) && symbase[relp->r_symbolnum].n_type == 0) {
    196  1.1    cgd 	fprintf(stderr,
    197  1.1    cgd 		"%s: oops, have hanging relocation for %s: bailing out!\n",
    198  1.1    cgd 		filename, SYMSTR(&symbase[relp->r_symbolnum]));
    199  1.1    cgd 	exit(1);
    200  1.1    cgd     }
    201  1.1    cgd }
    202  1.1    cgd 
    203  1.1    cgd #endif /* defined(NLIST_AOUT) */
    204