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