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