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. 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
62 #include <sys/types.h>
63 #include <sys/stat.h>
64 #include <sys/errno.h>
65 #include <unistd.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <fcntl.h>
70
71 #include "extern.h"
72
73 static char *pname = "crunchide";
74
75 static void usage(void) __dead2;
76
77 static void add_to_keep_list(char *symbol);
78 static void add_file_to_keep_list(char *filename);
79
80 static int hide_syms(const char *filename);
81
82 static int verbose;
83
84 int
main(int argc,char ** argv)85 main(int argc, char **argv)
86 {
87 int ch, errors;
88
89 if(argc > 0) pname = argv[0];
90
91 while ((ch = getopt(argc, argv, "k:f:v")) != -1)
92 switch(ch) {
93 case 'k':
94 add_to_keep_list(optarg);
95 break;
96 case 'f':
97 add_file_to_keep_list(optarg);
98 break;
99 case 'v':
100 verbose = 1;
101 break;
102 default:
103 usage();
104 }
105
106 argc -= optind;
107 argv += optind;
108
109 if(argc == 0) usage();
110
111 errors = 0;
112 while(argc) {
113 if (hide_syms(*argv))
114 errors = 1;
115 argc--, argv++;
116 }
117
118 return errors;
119 }
120
121 static void
usage(void)122 usage(void)
123 {
124 fprintf(stderr,
125 "usage: %s [-k <symbol-name>] [-f <keep-list-file>] <files> ...\n",
126 pname);
127 exit(1);
128 }
129
130 /* ---------------------------- */
131
132 static struct keep {
133 struct keep *next;
134 char *sym;
135 } *keep_list;
136
137 static void
add_to_keep_list(char * symbol)138 add_to_keep_list(char *symbol)
139 {
140 struct keep *newp, *prevp, *curp;
141 int cmp;
142
143 cmp = 0;
144
145 for(curp = keep_list, prevp = NULL; curp; prevp = curp, curp = curp->next)
146 if((cmp = strcmp(symbol, curp->sym)) <= 0) break;
147
148 if(curp && cmp == 0)
149 return; /* already in table */
150
151 newp = (struct keep *) malloc(sizeof(struct keep));
152 if(newp) newp->sym = strdup(symbol);
153 if(newp == NULL || newp->sym == NULL) {
154 fprintf(stderr, "%s: out of memory for keep list\n", pname);
155 exit(1);
156 }
157
158 newp->next = curp;
159 if(prevp) prevp->next = newp;
160 else keep_list = newp;
161 }
162
163 int
in_keep_list(const char * symbol)164 in_keep_list(const char *symbol)
165 {
166 struct keep *curp;
167 int cmp;
168
169 cmp = 0;
170
171 for(curp = keep_list; curp; curp = curp->next)
172 if((cmp = strcmp(symbol, curp->sym)) <= 0) break;
173
174 return curp && cmp == 0;
175 }
176
177 static void
add_file_to_keep_list(char * filename)178 add_file_to_keep_list(char *filename)
179 {
180 FILE *keepf;
181 char symbol[1024];
182 int len;
183
184 if((keepf = fopen(filename, "r")) == NULL) {
185 perror(filename);
186 usage();
187 }
188
189 while(fgets(symbol, sizeof(symbol), keepf)) {
190 len = strlen(symbol);
191 if(len && symbol[len-1] == '\n')
192 symbol[len-1] = '\0';
193
194 add_to_keep_list(symbol);
195 }
196 fclose(keepf);
197 }
198
199 /* ---------------------------- */
200
201 static struct {
202 const char *name;
203 int (*check)(int, const char *); /* 1 if match, zero if not */
204 int (*hide)(int, const char *); /* non-zero if error */
205 } exec_formats[] = {
206 #ifdef NLIST_ELF64
207 { "ELF64", check_elf64, hide_elf64, },
208 #endif
209 };
210
211 static int
hide_syms(const char * filename)212 hide_syms(const char *filename)
213 {
214 int fd, i, n, rv;
215
216 fd = open(filename, O_RDWR, 0);
217 if (fd == -1) {
218 perror(filename);
219 return 1;
220 }
221
222 rv = 0;
223
224 n = sizeof exec_formats / sizeof exec_formats[0];
225 for (i = 0; i < n; i++) {
226 if (lseek(fd, 0, SEEK_SET) != 0) {
227 perror(filename);
228 goto err;
229 }
230 if ((*exec_formats[i].check)(fd, filename) != 0)
231 break;
232 }
233 if (i == n) {
234 fprintf(stderr, "%s: unknown executable format\n", filename);
235 goto err;
236 }
237
238 if (verbose)
239 fprintf(stderr, "%s is an %s binary\n", filename,
240 exec_formats[i].name);
241
242 if (lseek(fd, 0, SEEK_SET) != 0) {
243 perror(filename);
244 goto err;
245 }
246 rv = (*exec_formats[i].hide)(fd, filename);
247
248 out:
249 close (fd);
250 return (rv);
251
252 err:
253 rv = 1;
254 goto out;
255 }
256