Home | History | Annotate | Line # | Download | only in crunchide
crunchide.c revision 1.5
      1 /*
      2  * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
      3  * Copyright (c) 1994 University of Maryland
      4  * All Rights Reserved.
      5  *
      6  * Permission to use, copy, modify, distribute, and sell this software and its
      7  * documentation for any purpose is hereby granted without fee, provided that
      8  * the above copyright notice appear in all copies and that both that
      9  * copyright notice and this permission notice appear in supporting
     10  * documentation, and that the name of U.M. not be used in advertising or
     11  * publicity pertaining to distribution of the software without specific,
     12  * written prior permission.  U.M. makes no representations about the
     13  * suitability of this software for any purpose.  It is provided "as is"
     14  * without express or implied warranty.
     15  *
     16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
     18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
     20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
     21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     22  *
     23  * Author: James da Silva, Systems Design and Analysis Group
     24  *			   Computer Science Department
     25  *			   University of Maryland at College Park
     26  */
     27 /*
     28  * crunchide.c - tiptoes through an a.out symbol table, hiding all defined
     29  *	global symbols.  Allows the user to supply a "keep list" of symbols
     30  *	that are not to be hidden.  This program relies on the use of the
     31  * 	linker's -dc flag to actually put global bss data into the file's
     32  * 	bss segment (rather than leaving it as undefined "common" data).
     33  *
     34  * 	The point of all this is to allow multiple programs to be linked
     35  *	together without getting multiple-defined errors.
     36  *
     37  *	For example, consider a program "foo.c".  It can be linked with a
     38  *	small stub routine, called "foostub.c", eg:
     39  *	    int foo_main(int argc, char **argv){ return main(argc, argv); }
     40  *      like so:
     41  *	    cc -c foo.c foostub.c
     42  *	    ld -dc -r foo.o foostub.o -o foo.combined.o
     43  *	    crunchide -k _foo_main foo.combined.o
     44  *	at this point, foo.combined.o can be linked with another program
     45  * 	and invoked with "foo_main(argc, argv)".  foo's main() and any
     46  * 	other globals are hidden and will not conflict with other symbols.
     47  *
     48  * TODO:
     49  *	- resolve the theoretical hanging reloc problem (see check_reloc()
     50  *	  below). I have yet to see this problem actually occur in any real
     51  *	  program. In what cases will gcc/gas generate code that needs a
     52  *	  relative reloc from a global symbol, other than PIC?  The
     53  *	  solution is to not hide the symbol from the linker in this case,
     54  *	  but to generate some random name for it so that it doesn't link
     55  *	  with anything but holds the place for the reloc.
     56  *      - arrange that all the BSS segments start at the same address, so
     57  *	  that the final crunched binary BSS size is the max of all the
     58  *	  component programs' BSS sizes, rather than their sum.
     59  */
     60 #include <unistd.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #include <fcntl.h>
     65 #include <a.out.h>
     66 #include <sys/types.h>
     67 #include <sys/stat.h>
     68 #include <sys/errno.h>
     69 
     70 #include "extern.h"
     71 
     72 char *pname = "crunchide";
     73 
     74 void usage(void);
     75 
     76 void add_to_keep_list(char *symbol);
     77 void add_file_to_keep_list(char *filename);
     78 
     79 int hide_syms(const char *filename);
     80 
     81 int verbose;
     82 
     83 int main(argc, argv)
     84 int argc;
     85 char **argv;
     86 {
     87     int ch, errors;
     88 
     89     if(argc > 0) pname = argv[0];
     90 
     91     while ((ch = getopt(argc, argv, "k:f:v")) != EOF)
     92 	switch(ch) {
     93 	case 'k':
     94 	    add_to_keep_list(optarg);
     95 	    break;
     96 	case 'f':
     97 	    add_file_to_keep_list(optarg);
     98 	    break;
     99 	case 'v':
    100 	    verbose = 1;
    101 	    break;
    102 	default:
    103 	    usage();
    104 	}
    105 
    106     argc -= optind;
    107     argv += optind;
    108 
    109     if(argc == 0) usage();
    110 
    111     errors = 0;
    112     while(argc) {
    113 	if (hide_syms(*argv))
    114 		errors = 1;
    115 	argc--, argv++;
    116     }
    117 
    118     return errors;
    119 }
    120 
    121 void usage(void)
    122 {
    123     fprintf(stderr,
    124 	    "Usage: %s [-k <symbol-name>] [-f <keep-list-file>] <files> ...\n",
    125 	    pname);
    126     exit(1);
    127 }
    128 
    129 /* ---------------------------- */
    130 
    131 struct keep {
    132     struct keep *next;
    133     char *sym;
    134 } *keep_list;
    135 
    136 void add_to_keep_list(char *symbol)
    137 {
    138     struct keep *newp, *prevp, *curp;
    139     int cmp;
    140 
    141     for(curp = keep_list, prevp = NULL; curp; prevp = curp, curp = curp->next)
    142 	if((cmp = strcmp(symbol, curp->sym)) <= 0) break;
    143 
    144     if(curp && cmp == 0)
    145 	return;	/* already in table */
    146 
    147     newp = (struct keep *) malloc(sizeof(struct keep));
    148     if(newp) newp->sym = strdup(symbol);
    149     if(newp == NULL || newp->sym == NULL) {
    150 	fprintf(stderr, "%s: out of memory for keep list\n", pname);
    151 	exit(1);
    152     }
    153 
    154     newp->next = curp;
    155     if(prevp) prevp->next = newp;
    156     else keep_list = newp;
    157 }
    158 
    159 int in_keep_list(const char *symbol)
    160 {
    161     struct keep *curp;
    162     int cmp;
    163 
    164     for(curp = keep_list; curp; curp = curp->next)
    165 	if((cmp = strcmp(symbol, curp->sym)) <= 0) break;
    166 
    167     return curp && cmp == 0;
    168 }
    169 
    170 void add_file_to_keep_list(char *filename)
    171 {
    172     FILE *keepf;
    173     char symbol[1024];
    174     int len;
    175 
    176     if((keepf = fopen(filename, "r")) == NULL) {
    177 	perror(filename);
    178 	usage();
    179     }
    180 
    181     while(fgets(symbol, 1024, keepf)) {
    182 	len = strlen(symbol);
    183 	if(len && symbol[len-1] == '\n')
    184 	    symbol[len-1] = '\0';
    185 
    186 	add_to_keep_list(symbol);
    187     }
    188     fclose(keepf);
    189 }
    190 
    191 /* ---------------------------- */
    192 
    193 struct {
    194 	const char *name;
    195 	int	(*check)(int, const char *);	/* 1 if match, zero if not */
    196 	int	(*hide)(int, const char *);	/* non-zero if error */
    197 } exec_formats[] = {
    198 #ifdef NLIST_AOUT
    199 	{	"a.out",	check_aout,	hide_aout,	},
    200 #endif
    201 #ifdef NLIST_ECOFF
    202 	{	"ECOFF",	check_ecoff,	hide_ecoff,	},
    203 #endif
    204 #ifdef NLIST_ELF32
    205 	{	"ELF32",	check_elf32,	hide_elf32,	},
    206 #endif
    207 #ifdef NLIST_ELF64
    208 	{	"ELF64",	check_elf64,	hide_elf64,	},
    209 #endif
    210 };
    211 
    212 int hide_syms(const char *filename)
    213 {
    214 	int fd, i, n, rv;
    215 
    216 	fd = open(filename, O_RDWR, 0);
    217 	if (fd == -1) {
    218 		perror(filename);
    219 		return 1;
    220 	}
    221 
    222 	rv = 0;
    223 
    224         n = sizeof exec_formats / sizeof exec_formats[0];
    225         for (i = 0; i < n; i++) {
    226 		if (lseek(fd, 0, SEEK_SET) != 0) {
    227 			perror(filename);
    228 			goto err;
    229 		}
    230                 if ((*exec_formats[i].check)(fd, filename) != 0)
    231                         break;
    232 	}
    233 	if (i == n) {
    234 		fprintf(stderr, "%s: unknown executable format\n", filename);
    235 		goto err;
    236 	}
    237 
    238 	if (verbose)
    239 		fprintf(stderr, "%s is an %s binary\n", filename,
    240 		    exec_formats[i].name);
    241 
    242 	if (lseek(fd, 0, SEEK_SET) != 0) {
    243 		perror(filename);
    244 		goto err;
    245 	}
    246 	rv = (*exec_formats[i].hide)(fd, filename);
    247 
    248 out:
    249 	close (fd);
    250 	return (rv);
    251 
    252 err:
    253 	rv = 1;
    254 	goto out;
    255 }
    256