1 /* $NetBSD: crunchide.c,v 1.8 1997/11/01 06:51:45 lukem Exp $ */ 2 /* $FreeBSD$ */ 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 * crunchide.c - tiptoes through an a.out symbol table, hiding all defined 31 * global symbols. Allows the user to supply a "keep list" of symbols 32 * that are not to be hidden. This program relies on the use of the 33 * linker's -dc flag to actually put global bss data into the file's 34 * bss segment (rather than leaving it as undefined "common" data). 35 * 36 * The point of all this is to allow multiple programs to be linked 37 * together without getting multiple-defined errors. 38 * 39 * For example, consider a program "foo.c". It can be linked with a 40 * small stub routine, called "foostub.c", eg: 41 * int foo_main(int argc, char **argv){ return main(argc, argv); } 42 * like so: 43 * cc -c foo.c foostub.c 44 * ld -dc -r foo.o foostub.o -o foo.combined.o 45 * crunchide -k _foo_main foo.combined.o 46 * at this point, foo.combined.o can be linked with another program 47 * and invoked with "foo_main(argc, argv)". foo's main() and any 48 * other globals are hidden and will not conflict with other symbols. 49 * 50 * TODO: 51 * - resolve the theoretical hanging reloc problem (see check_reloc() 52 * below). I have yet to see this problem actually occur in any real 53 * program. In what cases will gcc/gas generate code that needs a 54 * relative reloc from a global symbol, other than PIC? The 55 * solution is to not hide the symbol from the linker in this case, 56 * but to generate some random name for it so that it doesn't link 57 * with anything but holds the place for the reloc. 58 * - arrange that all the BSS segments start at the same address, so 59 * that the final crunched binary BSS size is the max of all the 60 * component programs' BSS sizes, rather than their sum. 61 */ 62 #include <sys/cdefs.h> 63 #ifndef lint 64 __RCSID("$NetBSD: crunchide.c,v 1.8 1997/11/01 06:51:45 lukem Exp $"); 65 #endif 66 67 #include <unistd.h> 68 #include <stdio.h> 69 #include <stdlib.h> 70 #include <string.h> 71 #include <fcntl.h> 72 #include <a.out.h> 73 #include <sys/types.h> 74 #include <sys/stat.h> 75 #include <sys/errno.h> 76 77 #include "extern.h" 78 79 char *pname = "crunchide"; 80 81 void usage(void); 82 83 void add_to_keep_list(char *symbol); 84 void add_file_to_keep_list(char *filename); 85 86 int hide_syms(const char *filename); 87 88 int verbose; 89 90 int main __P((int, char *[])); 91 92 int main(argc, argv) 93 int argc; 94 char **argv; 95 { 96 int ch, errors; 97 98 if(argc > 0) pname = argv[0]; 99 100 while ((ch = getopt(argc, argv, "k:f:v")) != -1) 101 switch(ch) { 102 case 'k': 103 add_to_keep_list(optarg); 104 break; 105 case 'f': 106 add_file_to_keep_list(optarg); 107 break; 108 case 'v': 109 verbose = 1; 110 break; 111 default: 112 usage(); 113 } 114 115 argc -= optind; 116 argv += optind; 117 118 if(argc == 0) usage(); 119 120 errors = 0; 121 while(argc) { 122 if (hide_syms(*argv)) 123 errors = 1; 124 argc--, argv++; 125 } 126 127 return errors; 128 } 129 130 void usage(void) 131 { 132 fprintf(stderr, 133 "usage: %s [-k <symbol-name>] [-f <keep-list-file>] <files> ...\n", 134 pname); 135 exit(1); 136 } 137 138 /* ---------------------------- */ 139 140 struct keep { 141 struct keep *next; 142 char *sym; 143 } *keep_list; 144 145 void 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) break; 154 155 if(curp && cmp == 0) 156 return; /* already in table */ 157 158 newp = (struct keep *) malloc(sizeof(struct keep)); 159 if(newp) newp->sym = strdup(symbol); 160 if(newp == NULL || newp->sym == NULL) { 161 fprintf(stderr, "%s: out of memory for keep list\n", pname); 162 exit(1); 163 } 164 165 newp->next = curp; 166 if(prevp) prevp->next = newp; 167 else keep_list = newp; 168 } 169 170 int in_keep_list(const char *symbol) 171 { 172 struct keep *curp; 173 int cmp; 174 175 cmp = 0; 176 177 for(curp = keep_list; curp; curp = curp->next) 178 if((cmp = strcmp(symbol, curp->sym)) <= 0) break; 179 180 return curp && cmp == 0; 181 } 182 183 void add_file_to_keep_list(char *filename) 184 { 185 FILE *keepf; 186 char symbol[1024]; 187 int len; 188 189 if((keepf = fopen(filename, "r")) == NULL) { 190 perror(filename); 191 usage(); 192 } 193 194 while(fgets(symbol, 1024, keepf)) { 195 len = strlen(symbol); 196 if(len && symbol[len-1] == '\n') 197 symbol[len-1] = '\0'; 198 199 add_to_keep_list(symbol); 200 } 201 fclose(keepf); 202 } 203 204 /* ---------------------------- */ 205 206 struct { 207 const char *name; 208 int (*check)(int, const char *); /* 1 if match, zero if not */ 209 int (*hide)(int, const char *); /* non-zero if error */ 210 } exec_formats[] = { 211 #ifdef __i386__ 212 #ifdef NLIST_AOUT 213 { "a.out", check_aout, hide_aout, }, 214 #endif 215 #endif 216 #ifdef NLIST_ECOFF 217 { "ECOFF", check_elf64, hide_elf64, }, 218 #endif 219 #ifdef NLIST_ELF32 220 { "ELF32", check_elf32, hide_elf32, }, 221 #endif 222 #ifdef NLIST_ELF64 223 { "ELF64", check_elf64, hide_elf64, }, 224 #endif 225 }; 226 227 int hide_syms(const char *filename) 228 { 229 int fd, i, n, rv; 230 231 fd = open(filename, O_RDWR, 0); 232 if (fd == -1) { 233 perror(filename); 234 return 1; 235 } 236 237 rv = 0; 238 239 n = sizeof exec_formats / sizeof exec_formats[0]; 240 for (i = 0; i < n; i++) { 241 if (lseek(fd, 0, SEEK_SET) != 0) { 242 perror(filename); 243 goto err; 244 } 245 if ((*exec_formats[i].check)(fd, filename) != 0) 246 break; 247 } 248 if (i == n) { 249 fprintf(stderr, "%s: unknown executable format\n", filename); 250 goto err; 251 } 252 253 if (verbose) 254 fprintf(stderr, "%s is an %s binary\n", filename, 255 exec_formats[i].name); 256 257 if (lseek(fd, 0, SEEK_SET) != 0) { 258 perror(filename); 259 goto err; 260 } 261 rv = (*exec_formats[i].hide)(fd, filename); 262 263 out: 264 close (fd); 265 return (rv); 266 267 err: 268 rv = 1; 269 goto out; 270 } 271