1 /* $NetBSD: crunchide.c,v 1.8 1997/11/01 06:51:45 lukem Exp $ */
3 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
4 * Copyright (c) 1994 University of Maryland
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.
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.
24 * Author: James da Silva, Systems Design and Analysis Group
25 * Computer Science Department
26 * University of Maryland at College Park
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).
35 * The point of all this is to allow multiple programs to be linked
36 * together without getting multiple-defined errors.
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); }
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.
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.
62 #include <sys/cdefs.h>
64 __RCSID("$NetBSD: crunchide.c,v 1.8 1997/11/01 06:51:45 lukem Exp $");
66 __FBSDID("$FreeBSD$");
68 #include <sys/types.h>
70 #include <sys/errno.h>
79 char *pname = "crunchide";
83 void add_to_keep_list(char *symbol);
84 void add_file_to_keep_list(char *filename);
86 int hide_syms(const char *filename);
90 int main(int, char *[]);
93 main(int argc, char **argv)
97 if(argc > 0) pname = argv[0];
99 while ((ch = getopt(argc, argv, "k:f:v")) != -1)
102 add_to_keep_list(optarg);
105 add_file_to_keep_list(optarg);
117 if(argc == 0) usage();
121 if (hide_syms(*argv))
133 "usage: %s [-k <symbol-name>] [-f <keep-list-file>] <files> ...\n",
138 /* ---------------------------- */
146 add_to_keep_list(char *symbol)
148 struct keep *newp, *prevp, *curp;
153 for(curp = keep_list, prevp = NULL; curp; prevp = curp, curp = curp->next)
154 if((cmp = strcmp(symbol, curp->sym)) <= 0) break;
157 return; /* already in table */
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);
167 if(prevp) prevp->next = newp;
168 else keep_list = newp;
172 in_keep_list(const char *symbol)
179 for(curp = keep_list; curp; curp = curp->next)
180 if((cmp = strcmp(symbol, curp->sym)) <= 0) break;
182 return curp && cmp == 0;
186 add_file_to_keep_list(char *filename)
192 if((keepf = fopen(filename, "r")) == NULL) {
197 while(fgets(symbol, sizeof(symbol), keepf)) {
198 len = strlen(symbol);
199 if(len && symbol[len-1] == '\n')
200 symbol[len-1] = '\0';
202 add_to_keep_list(symbol);
207 /* ---------------------------- */
211 int (*check)(int, const char *); /* 1 if match, zero if not */
212 int (*hide)(int, const char *); /* non-zero if error */
215 { "ELF32", check_elf32, hide_elf32, },
218 { "ELF64", check_elf64, hide_elf64, },
223 hide_syms(const char *filename)
227 fd = open(filename, O_RDWR, 0);
235 n = sizeof exec_formats / sizeof exec_formats[0];
236 for (i = 0; i < n; i++) {
237 if (lseek(fd, 0, SEEK_SET) != 0) {
241 if ((*exec_formats[i].check)(fd, filename) != 0)
245 fprintf(stderr, "%s: unknown executable format\n", filename);
250 fprintf(stderr, "%s is an %s binary\n", filename,
251 exec_formats[i].name);
253 if (lseek(fd, 0, SEEK_SET) != 0) {
257 rv = (*exec_formats[i].hide)(fd, filename);