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