Home | History | Annotate | Line # | Download | only in crunchide
crunchide.c revision 1.10
      1  1.10  msaitoh /*	$NetBSD: crunchide.c,v 1.10 1999/11/26 13:47:52 msaitoh Exp $	*/
      2   1.1      cgd /*
      3   1.3      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.1      cgd /*
     29   1.1      cgd  * crunchide.c - tiptoes through an a.out symbol table, hiding all defined
     30   1.1      cgd  *	global symbols.  Allows the user to supply a "keep list" of symbols
     31   1.1      cgd  *	that are not to be hidden.  This program relies on the use of the
     32   1.1      cgd  * 	linker's -dc flag to actually put global bss data into the file's
     33   1.1      cgd  * 	bss segment (rather than leaving it as undefined "common" data).
     34   1.1      cgd  *
     35   1.1      cgd  * 	The point of all this is to allow multiple programs to be linked
     36   1.1      cgd  *	together without getting multiple-defined errors.
     37   1.1      cgd  *
     38   1.1      cgd  *	For example, consider a program "foo.c".  It can be linked with a
     39   1.1      cgd  *	small stub routine, called "foostub.c", eg:
     40   1.1      cgd  *	    int foo_main(int argc, char **argv){ return main(argc, argv); }
     41   1.1      cgd  *      like so:
     42   1.1      cgd  *	    cc -c foo.c foostub.c
     43   1.1      cgd  *	    ld -dc -r foo.o foostub.o -o foo.combined.o
     44   1.1      cgd  *	    crunchide -k _foo_main foo.combined.o
     45   1.1      cgd  *	at this point, foo.combined.o can be linked with another program
     46   1.1      cgd  * 	and invoked with "foo_main(argc, argv)".  foo's main() and any
     47   1.1      cgd  * 	other globals are hidden and will not conflict with other symbols.
     48   1.1      cgd  *
     49   1.1      cgd  * TODO:
     50   1.1      cgd  *	- resolve the theoretical hanging reloc problem (see check_reloc()
     51   1.1      cgd  *	  below). I have yet to see this problem actually occur in any real
     52   1.1      cgd  *	  program. In what cases will gcc/gas generate code that needs a
     53   1.1      cgd  *	  relative reloc from a global symbol, other than PIC?  The
     54   1.1      cgd  *	  solution is to not hide the symbol from the linker in this case,
     55   1.1      cgd  *	  but to generate some random name for it so that it doesn't link
     56   1.1      cgd  *	  with anything but holds the place for the reloc.
     57   1.1      cgd  *      - arrange that all the BSS segments start at the same address, so
     58   1.1      cgd  *	  that the final crunched binary BSS size is the max of all the
     59   1.1      cgd  *	  component programs' BSS sizes, rather than their sum.
     60   1.6    perry  */
     61   1.6    perry #include <sys/cdefs.h>
     62   1.6    perry #ifndef lint
     63  1.10  msaitoh __RCSID("$NetBSD: crunchide.c,v 1.10 1999/11/26 13:47:52 msaitoh Exp $");
     64   1.6    perry #endif
     65   1.6    perry 
     66   1.1      cgd #include <unistd.h>
     67   1.1      cgd #include <stdio.h>
     68   1.1      cgd #include <stdlib.h>
     69   1.1      cgd #include <string.h>
     70   1.1      cgd #include <fcntl.h>
     71   1.9   kleink #include <errno.h>
     72   1.1      cgd #include <a.out.h>
     73   1.1      cgd #include <sys/types.h>
     74   1.1      cgd #include <sys/stat.h>
     75   1.1      cgd 
     76   1.4      cgd #include "extern.h"
     77   1.4      cgd 
     78   1.1      cgd char *pname = "crunchide";
     79   1.1      cgd 
     80   1.1      cgd void usage(void);
     81   1.1      cgd 
     82   1.1      cgd void add_to_keep_list(char *symbol);
     83   1.1      cgd void add_file_to_keep_list(char *filename);
     84   1.1      cgd 
     85   1.3      cgd int hide_syms(const char *filename);
     86   1.1      cgd 
     87   1.3      cgd int verbose;
     88   1.1      cgd 
     89   1.6    perry int main __P((int, char *[]));
     90   1.6    perry 
     91   1.1      cgd int main(argc, argv)
     92   1.1      cgd int argc;
     93   1.1      cgd char **argv;
     94   1.1      cgd {
     95   1.3      cgd     int ch, errors;
     96   1.1      cgd 
     97   1.1      cgd     if(argc > 0) pname = argv[0];
     98   1.1      cgd 
     99   1.8    lukem     while ((ch = getopt(argc, argv, "k:f:v")) != -1)
    100   1.1      cgd 	switch(ch) {
    101   1.1      cgd 	case 'k':
    102   1.1      cgd 	    add_to_keep_list(optarg);
    103   1.1      cgd 	    break;
    104   1.1      cgd 	case 'f':
    105   1.1      cgd 	    add_file_to_keep_list(optarg);
    106   1.1      cgd 	    break;
    107   1.3      cgd 	case 'v':
    108   1.3      cgd 	    verbose = 1;
    109   1.3      cgd 	    break;
    110   1.1      cgd 	default:
    111   1.1      cgd 	    usage();
    112   1.1      cgd 	}
    113   1.1      cgd 
    114   1.1      cgd     argc -= optind;
    115   1.1      cgd     argv += optind;
    116   1.1      cgd 
    117   1.1      cgd     if(argc == 0) usage();
    118   1.1      cgd 
    119   1.3      cgd     errors = 0;
    120   1.1      cgd     while(argc) {
    121   1.3      cgd 	if (hide_syms(*argv))
    122   1.3      cgd 		errors = 1;
    123   1.1      cgd 	argc--, argv++;
    124   1.1      cgd     }
    125   1.1      cgd 
    126   1.3      cgd     return errors;
    127   1.1      cgd }
    128   1.1      cgd 
    129   1.1      cgd void usage(void)
    130   1.1      cgd {
    131   1.1      cgd     fprintf(stderr,
    132   1.1      cgd 	    "Usage: %s [-k <symbol-name>] [-f <keep-list-file>] <files> ...\n",
    133   1.1      cgd 	    pname);
    134   1.1      cgd     exit(1);
    135   1.1      cgd }
    136   1.1      cgd 
    137   1.1      cgd /* ---------------------------- */
    138   1.1      cgd 
    139   1.1      cgd struct keep {
    140   1.1      cgd     struct keep *next;
    141   1.1      cgd     char *sym;
    142   1.1      cgd } *keep_list;
    143   1.1      cgd 
    144   1.1      cgd void add_to_keep_list(char *symbol)
    145   1.1      cgd {
    146   1.1      cgd     struct keep *newp, *prevp, *curp;
    147   1.1      cgd     int cmp;
    148   1.7  hannken 
    149   1.7  hannken     cmp = 0;
    150   1.1      cgd 
    151   1.1      cgd     for(curp = keep_list, prevp = NULL; curp; prevp = curp, curp = curp->next)
    152   1.1      cgd 	if((cmp = strcmp(symbol, curp->sym)) <= 0) break;
    153   1.1      cgd 
    154   1.1      cgd     if(curp && cmp == 0)
    155   1.1      cgd 	return;	/* already in table */
    156   1.1      cgd 
    157   1.1      cgd     newp = (struct keep *) malloc(sizeof(struct keep));
    158   1.1      cgd     if(newp) newp->sym = strdup(symbol);
    159   1.1      cgd     if(newp == NULL || newp->sym == NULL) {
    160   1.1      cgd 	fprintf(stderr, "%s: out of memory for keep list\n", pname);
    161   1.1      cgd 	exit(1);
    162   1.1      cgd     }
    163   1.1      cgd 
    164   1.1      cgd     newp->next = curp;
    165   1.1      cgd     if(prevp) prevp->next = newp;
    166   1.1      cgd     else keep_list = newp;
    167   1.1      cgd }
    168   1.1      cgd 
    169   1.3      cgd int in_keep_list(const char *symbol)
    170   1.1      cgd {
    171   1.1      cgd     struct keep *curp;
    172   1.1      cgd     int cmp;
    173   1.6    perry 
    174   1.6    perry     cmp = 0;
    175   1.1      cgd 
    176   1.1      cgd     for(curp = keep_list; curp; curp = curp->next)
    177   1.1      cgd 	if((cmp = strcmp(symbol, curp->sym)) <= 0) break;
    178   1.1      cgd 
    179   1.1      cgd     return curp && cmp == 0;
    180   1.1      cgd }
    181   1.1      cgd 
    182   1.1      cgd void add_file_to_keep_list(char *filename)
    183   1.1      cgd {
    184   1.1      cgd     FILE *keepf;
    185   1.1      cgd     char symbol[1024];
    186   1.1      cgd     int len;
    187   1.1      cgd 
    188   1.1      cgd     if((keepf = fopen(filename, "r")) == NULL) {
    189   1.1      cgd 	perror(filename);
    190   1.1      cgd 	usage();
    191   1.1      cgd     }
    192   1.1      cgd 
    193   1.1      cgd     while(fgets(symbol, 1024, keepf)) {
    194   1.1      cgd 	len = strlen(symbol);
    195   1.1      cgd 	if(len && symbol[len-1] == '\n')
    196   1.1      cgd 	    symbol[len-1] = '\0';
    197   1.1      cgd 
    198   1.1      cgd 	add_to_keep_list(symbol);
    199   1.1      cgd     }
    200   1.1      cgd     fclose(keepf);
    201   1.1      cgd }
    202   1.1      cgd 
    203   1.3      cgd /* ---------------------------- */
    204   1.1      cgd 
    205   1.3      cgd struct {
    206   1.3      cgd 	const char *name;
    207   1.3      cgd 	int	(*check)(int, const char *);	/* 1 if match, zero if not */
    208   1.3      cgd 	int	(*hide)(int, const char *);	/* non-zero if error */
    209   1.3      cgd } exec_formats[] = {
    210   1.3      cgd #ifdef NLIST_AOUT
    211   1.3      cgd 	{	"a.out",	check_aout,	hide_aout,	},
    212  1.10  msaitoh #endif
    213  1.10  msaitoh #ifdef NLIST_COFF
    214  1.10  msaitoh 	{	"COFF",		check_coff,	hide_coff,	},
    215   1.3      cgd #endif
    216   1.5      cgd #ifdef NLIST_ECOFF
    217   1.5      cgd 	{	"ECOFF",	check_ecoff,	hide_ecoff,	},
    218   1.5      cgd #endif
    219   1.3      cgd #ifdef NLIST_ELF32
    220   1.3      cgd 	{	"ELF32",	check_elf32,	hide_elf32,	},
    221   1.3      cgd #endif
    222   1.3      cgd #ifdef NLIST_ELF64
    223   1.3      cgd 	{	"ELF64",	check_elf64,	hide_elf64,	},
    224   1.2       pk #endif
    225   1.3      cgd };
    226   1.1      cgd 
    227   1.3      cgd int hide_syms(const char *filename)
    228   1.1      cgd {
    229   1.3      cgd 	int fd, i, n, rv;
    230   1.1      cgd 
    231   1.3      cgd 	fd = open(filename, O_RDWR, 0);
    232   1.3      cgd 	if (fd == -1) {
    233   1.3      cgd 		perror(filename);
    234   1.3      cgd 		return 1;
    235   1.3      cgd 	}
    236   1.1      cgd 
    237   1.3      cgd 	rv = 0;
    238   1.1      cgd 
    239   1.3      cgd         n = sizeof exec_formats / sizeof exec_formats[0];
    240   1.3      cgd         for (i = 0; i < n; i++) {
    241   1.3      cgd 		if (lseek(fd, 0, SEEK_SET) != 0) {
    242   1.3      cgd 			perror(filename);
    243   1.3      cgd 			goto err;
    244   1.3      cgd 		}
    245   1.3      cgd                 if ((*exec_formats[i].check)(fd, filename) != 0)
    246   1.3      cgd                         break;
    247   1.3      cgd 	}
    248   1.3      cgd 	if (i == n) {
    249   1.4      cgd 		fprintf(stderr, "%s: unknown executable format\n", filename);
    250   1.3      cgd 		goto err;
    251   1.3      cgd 	}
    252   1.1      cgd 
    253   1.3      cgd 	if (verbose)
    254   1.3      cgd 		fprintf(stderr, "%s is an %s binary\n", filename,
    255   1.3      cgd 		    exec_formats[i].name);
    256   1.3      cgd 
    257   1.3      cgd 	if (lseek(fd, 0, SEEK_SET) != 0) {
    258   1.3      cgd 		perror(filename);
    259   1.3      cgd 		goto err;
    260   1.3      cgd 	}
    261   1.3      cgd 	rv = (*exec_formats[i].hide)(fd, filename);
    262   1.1      cgd 
    263   1.3      cgd out:
    264   1.3      cgd 	close (fd);
    265   1.3      cgd 	return (rv);
    266   1.3      cgd 
    267   1.3      cgd err:
    268   1.3      cgd 	rv = 1;
    269   1.3      cgd 	goto out;
    270   1.1      cgd }
    271