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