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