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