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