Annotation of src/usr.bin/strip/strip.c, Revision 1.21
1.21 ! mickey 1: /* $OpenBSD: strip.c,v 1.20 2003/06/10 22:20:52 deraadt Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*
4: * Copyright (c) 1988 Regents of the University of California.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
1.19 millert 15: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: */
31:
32: #ifndef lint
33: char copyright[] =
34: "@(#) Copyright (c) 1988 Regents of the University of California.\n\
35: All rights reserved.\n";
36: #endif /* not lint */
37:
38: #ifndef lint
39: /*static char sccsid[] = "from: @(#)strip.c 5.8 (Berkeley) 11/6/91";*/
1.21 ! mickey 40: static char rcsid[] = "$OpenBSD: strip.c,v 1.20 2003/06/10 22:20:52 deraadt Exp $";
1.1 deraadt 41: #endif /* not lint */
42:
1.4 deraadt 43: #include <sys/param.h>
1.1 deraadt 44: #include <sys/types.h>
45: #include <sys/stat.h>
46: #include <sys/mman.h>
47: #include <fcntl.h>
48: #include <errno.h>
49: #include <a.out.h>
50: #include <unistd.h>
51: #include <stdio.h>
52: #include <stdlib.h>
53: #include <string.h>
1.7 mickey 54: #include <err.h>
1.11 espie 55: #include <ranlib.h>
56: #include "byte.c"
1.10 niklas 57:
58: #ifdef MID_MACHINE_OVERRIDE
59: #undef MID_MACHINE
60: #define MID_MACHINE MID_MACHINE_OVERRIDE
1.21 ! mickey 61: #if MID_MACHINE_OVERRIDE == MID_M68K
! 62: #undef __LDPGSZ
! 63: #undef ELF_TARG_DATA
! 64: #undef ELF_TARG_MACH
! 65: #include "m68k/exec.h"
! 66: #endif
1.10 niklas 67: #endif
1.1 deraadt 68:
69: typedef struct exec EXEC;
70: typedef struct nlist NLIST;
71:
72: #define strx n_un.n_strx
73:
1.17 espie 74: int s_stab(const char *, int, EXEC *, struct stat *, off_t *);
75: int s_sym(const char *, int, EXEC *, struct stat *, off_t *);
1.16 millert 76: void usage(void);
1.1 deraadt 77:
78: int xflag = 0;
79:
1.9 deraadt 80: int
1.20 deraadt 81: main(int argc, char *argv[])
1.1 deraadt 82: {
1.15 mpech 83: int fd;
1.1 deraadt 84: EXEC *ep;
85: struct stat sb;
1.17 espie 86: int (*sfcn)(const char *, int, EXEC *, struct stat *, off_t *);
1.1 deraadt 87: int ch, errors;
88: char *fn;
1.17 espie 89: off_t newsize;
1.1 deraadt 90:
91: sfcn = s_sym;
1.6 millert 92: while ((ch = getopt(argc, argv, "dx")) != -1)
1.1 deraadt 93: switch(ch) {
94: case 'x':
95: xflag = 1;
96: /*FALLTHROUGH*/
97: case 'd':
98: sfcn = s_stab;
99: break;
100: case '?':
101: default:
102: usage();
103: }
104: argc -= optind;
105: argv += optind;
106:
107: errors = 0;
1.8 mickey 108: #define ERROR(x) errors |= 1; warnx("%s: %s", fn, strerror(x)); continue;
1.9 deraadt 109: while ((fn = *argv++)) {
1.1 deraadt 110: if ((fd = open(fn, O_RDWR)) < 0) {
111: ERROR(errno);
112: }
113: if (fstat(fd, &sb)) {
114: (void)close(fd);
115: ERROR(errno);
116: }
117: if (sb.st_size < sizeof(EXEC)) {
118: (void)close(fd);
119: ERROR(EFTYPE);
120: }
121: if ((ep = (EXEC *)mmap(NULL, sb.st_size, PROT_READ|PROT_WRITE,
1.13 art 122: MAP_SHARED, fd, (off_t)0)) == MAP_FAILED) {
1.1 deraadt 123: (void)close(fd);
124: ERROR(errno);
125: }
1.11 espie 126: if (BAD_OBJECT(*ep)) {
1.1 deraadt 127: munmap((caddr_t)ep, sb.st_size);
128: (void)close(fd);
129: ERROR(EFTYPE);
130: }
1.11 espie 131: /* since we're dealing with an mmap there, we have to convert once
132: for dealing with data in memory, and a second time for out
133: */
134: fix_header_order(ep);
1.17 espie 135: newsize = 0;
136: errors |= sfcn(fn, fd, ep, &sb, &newsize);
1.11 espie 137: fix_header_order(ep);
1.1 deraadt 138: munmap((caddr_t)ep, sb.st_size);
1.17 espie 139: if (newsize && ftruncate(fd, newsize)) {
140: warn("%s", fn);
141: errors = 1;
142: }
1.1 deraadt 143: if (close(fd)) {
144: ERROR(errno);
145: }
146: }
147: #undef ERROR
148: exit(errors);
149: }
150:
151: int
1.20 deraadt 152: s_sym(const char *fn, int fd, EXEC *ep, struct stat *sp, off_t *sz)
1.1 deraadt 153: {
1.15 mpech 154: char *neweof;
1.14 pvalchev 155: #if 0
1.15 mpech 156: char *mineof;
1.14 pvalchev 157: #endif
1.1 deraadt 158: int zmagic;
159:
160: zmagic = ep->a_data &&
161: (N_GETMAGIC(*ep) == ZMAGIC || N_GETMAGIC(*ep) == QMAGIC);
162:
163: /*
164: * If no symbols or data/text relocation info and
165: * the file data segment size is already minimized, quit.
166: */
167: if (!ep->a_syms && !ep->a_trsize && !ep->a_drsize) {
168: #if 0
169: if (!zmagic)
170: return 0;
171: if (sp->st_size < N_TRELOFF(*ep))
172: #endif
173: return 0;
174: }
175:
176: /*
177: * New file size is the header plus text and data segments; OMAGIC
178: * and NMAGIC formats have the text/data immediately following the
179: * header. ZMAGIC format wastes the rest of of header page.
180: */
181: neweof = (char *)ep + N_TRELOFF(*ep);
182:
183: #if 0
184: /*
185: * Unfortunately, this can't work correctly without changing the way
186: * the loader works. We could cap it at one page, or even fiddle with
187: * a_data and a_bss, but this only works for CLBYTES == NBPG. If we
188: * are on a system where, e.g., CLBYTES is 8k and NBPG is 4k, and we
189: * happen to remove 4.5k, we will lose. And we really don't want to
190: * fiddle with pages, because that breaks binary compatibility. Lose.
191: */
192:
193: if (zmagic) {
194: /*
195: * Get rid of unneeded zeroes at the end of the data segment
196: * to reduce the file size even more.
197: */
198: mineof = (char *)ep + N_DATOFF(*ep);
199: while (neweof > mineof && neweof[-1] == '\0')
200: neweof--;
201: }
202: #endif
203:
204: /* Set symbol size and relocation info values to 0. */
205: ep->a_syms = ep->a_trsize = ep->a_drsize = 0;
206:
207: /* Truncate the file. */
1.17 espie 208: *sz = neweof - (char *)ep;
1.1 deraadt 209:
210: return 0;
211: }
212:
213: int
1.20 deraadt 214: s_stab(const char *fn, int fd, EXEC *ep, struct stat *sp, off_t *sz)
1.1 deraadt 215: {
1.15 mpech 216: int cnt, len;
1.18 espie 217: char *nstr, *nstrbase=0, *used=0, *p, *strbase;
1.15 mpech 218: NLIST *sym, *nsym;
1.11 espie 219: u_long allocsize;
220: int mid;
1.1 deraadt 221: NLIST *symbase;
1.18 espie 222: unsigned int *mapping=0;
223: int error=1;
224: unsigned int nsyms;
225: struct relocation_info *reloc_base;
226: unsigned int i, j;
1.1 deraadt 227:
228: /* Quit if no symbols. */
229: if (ep->a_syms == 0)
230: return 0;
231:
232: if (N_SYMOFF(*ep) >= sp->st_size) {
1.7 mickey 233: warnx("%s: bad symbol table", fn);
1.1 deraadt 234: return 1;
235: }
236:
1.11 espie 237: mid = N_GETMID(*ep);
238:
1.1 deraadt 239: /*
240: * Initialize old and new symbol pointers. They both point to the
241: * beginning of the symbol table in memory, since we're deleting
242: * entries.
243: */
244: sym = nsym = symbase = (NLIST *)((char *)ep + N_SYMOFF(*ep));
245:
246: /*
247: * Allocate space for the new string table, initialize old and
248: * new string pointers. Handle the extra long at the beginning
249: * of the string table.
250: */
251: strbase = (char *)ep + N_STROFF(*ep);
1.11 espie 252: allocsize = fix_long_order(*(u_long *)strbase, mid);
253: if ((nstrbase = malloc((u_int) allocsize)) == NULL) {
1.7 mickey 254: warnx("%s", strerror(ENOMEM));
1.18 espie 255: goto end;
1.1 deraadt 256: }
257: nstr = nstrbase + sizeof(u_long);
258:
1.18 espie 259: /* okay, so we also need to keep symbol numbers for relocations. */
260: nsyms = ep->a_syms/ sizeof(NLIST);
261: used = calloc(nsyms, 1);
262: if (!used) {
263: warnx("%s", strerror(ENOMEM));
264: goto end;
265: }
266: mapping = malloc(nsyms * sizeof(unsigned int));
267: if (!mapping) {
268: warnx("%s", strerror(ENOMEM));
269: goto end;
270: }
271:
272: if ((ep->a_trsize || ep->a_drsize) && byte_sex(mid) != BYTE_ORDER) {
273: warnx("%s: cross-stripping not supported", fn);
274: goto end;
275: }
276:
277: /* first check the relocations for used symbols, and mark them */
278: /* text */
279: reloc_base = (struct relocation_info *) ((char *)ep + N_TRELOFF(*ep));
280: if (N_TRELOFF(*ep) + ep->a_trsize > sp->st_size) {
281: warnx("%s: bad text relocation", fn);
282: goto end;
283: }
284: for (i = 0; i < ep->a_trsize / sizeof(struct relocation_info); i++) {
285: if (!reloc_base[i].r_extern)
286: continue;
287: if (reloc_base[i].r_symbolnum > nsyms) {
288: warnx("%s: bad symbol number in text relocation", fn);
289: goto end;
290: }
291: used[reloc_base[i].r_symbolnum] = 1;
292: }
293: /* data */
294: reloc_base = (struct relocation_info *) ((char *)ep + N_DRELOFF(*ep));
295: if (N_DRELOFF(*ep) + ep->a_drsize > sp->st_size) {
296: warnx("%s: bad data relocation", fn);
297: goto end;
298: }
299: for (i = 0; i < ep->a_drsize / sizeof(struct relocation_info); i++) {
300: if (!reloc_base[i].r_extern)
301: continue;
302: if (reloc_base[i].r_symbolnum > nsyms) {
303: warnx("%s: bad symbol number in data relocation", fn);
304: goto end;
305: }
306: used[reloc_base[i].r_symbolnum] = 1;
307: }
308:
1.1 deraadt 309: /*
310: * Read through the symbol table. For each non-debugging symbol,
311: * copy it and save its string in the new string table. Keep
312: * track of the number of symbols.
313: */
1.18 espie 314: for (cnt = nsyms, i = 0, j = 0; cnt--; ++sym, ++i) {
1.11 espie 315: fix_nlist_order(sym, mid);
1.1 deraadt 316: if (!(sym->n_type & N_STAB) && sym->strx) {
317: *nsym = *sym;
318: nsym->strx = nstr - nstrbase;
319: p = strbase + sym->strx;
1.18 espie 320: if (xflag && !used[i] &&
1.1 deraadt 321: (!(sym->n_type & N_EXT) ||
322: (sym->n_type & ~N_EXT) == N_FN ||
323: strcmp(p, "gcc_compiled.") == 0 ||
324: strcmp(p, "gcc2_compiled.") == 0 ||
325: strncmp(p, "___gnu_compiled_", 16) == 0)) {
326: continue;
327: }
328: len = strlen(p) + 1;
1.18 espie 329: mapping[i] = j++;
1.17 espie 330: if (N_STROFF(*ep) + sym->strx + len > sp->st_size) {
331: warnx("%s: bad symbol table", fn);
1.18 espie 332: goto end;
1.17 espie 333: }
1.1 deraadt 334: bcopy(p, nstr, len);
335: nstr += len;
1.11 espie 336: fix_nlist_order(nsym++, mid);
1.1 deraadt 337: }
1.11 espie 338: }
1.1 deraadt 339:
1.18 espie 340: /* renumber symbol relocations */
341: /* text */
342: reloc_base = (struct relocation_info *) ((char *)ep + N_TRELOFF(*ep));
343: for (i = 0; i < ep->a_trsize / sizeof(struct relocation_info); i++) {
344: if (!reloc_base[i].r_extern)
345: continue;
346: reloc_base[i].r_symbolnum = mapping[reloc_base[i].r_symbolnum];
347: }
348: /* data */
349: reloc_base = (struct relocation_info *) ((char *)ep + N_DRELOFF(*ep));
350: for (i = 0; i < ep->a_drsize / sizeof(struct relocation_info); i++) {
351: if (!reloc_base[i].r_extern)
352: continue;
353: reloc_base[i].r_symbolnum = mapping[reloc_base[i].r_symbolnum];
354: }
355:
1.1 deraadt 356: /* Fill in new symbol table size. */
357: ep->a_syms = (nsym - symbase) * sizeof(NLIST);
358:
359: /* Fill in the new size of the string table. */
1.11 espie 360: len = nstr - nstrbase;
361: *(u_long *)nstrbase = fix_long_order(len, mid);
1.1 deraadt 362:
363: /*
364: * Copy the new string table into place. Nsym should be pointing
365: * at the address past the last symbol entry.
366: */
367: bcopy(nstrbase, (void *)nsym, len);
1.18 espie 368: error = 0;
369: end:
1.1 deraadt 370: free(nstrbase);
1.18 espie 371: free(used);
372: free(mapping);
1.1 deraadt 373:
374: /* Truncate to the current length. */
1.17 espie 375: *sz = (char *)nsym + len - (char *)ep;
1.1 deraadt 376:
1.18 espie 377: return error;
1.1 deraadt 378: }
379:
380: void
1.20 deraadt 381: usage(void)
1.1 deraadt 382: {
1.21 ! mickey 383: extern char *__progname;
! 384:
! 385: fprintf(stderr, "usage: %s [-dx] file ...\n", __progname);
1.1 deraadt 386: exit(1);
387: }
388: