Annotation of src/usr.bin/strip/strip.c, Revision 1.19
1.19 ! millert 1: /* $OpenBSD: strip.c,v 1.18 2002/08/21 15:53:12 espie 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.19 ! millert 40: static char rcsid[] = "$OpenBSD: strip.c,v 1.18 2002/08/21 15:53:12 espie 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
61: #endif
1.1 deraadt 62:
63: typedef struct exec EXEC;
64: typedef struct nlist NLIST;
65:
66: #define strx n_un.n_strx
67:
1.17 espie 68: int s_stab(const char *, int, EXEC *, struct stat *, off_t *);
69: int s_sym(const char *, int, EXEC *, struct stat *, off_t *);
1.16 millert 70: void usage(void);
1.1 deraadt 71:
72: int xflag = 0;
73:
1.9 deraadt 74: int
1.1 deraadt 75: main(argc, argv)
76: int argc;
77: char *argv[];
78: {
1.15 mpech 79: int fd;
1.1 deraadt 80: EXEC *ep;
81: struct stat sb;
1.17 espie 82: int (*sfcn)(const char *, int, EXEC *, struct stat *, off_t *);
1.1 deraadt 83: int ch, errors;
84: char *fn;
1.17 espie 85: off_t newsize;
1.1 deraadt 86:
87: sfcn = s_sym;
1.6 millert 88: while ((ch = getopt(argc, argv, "dx")) != -1)
1.1 deraadt 89: switch(ch) {
90: case 'x':
91: xflag = 1;
92: /*FALLTHROUGH*/
93: case 'd':
94: sfcn = s_stab;
95: break;
96: case '?':
97: default:
98: usage();
99: }
100: argc -= optind;
101: argv += optind;
102:
103: errors = 0;
1.8 mickey 104: #define ERROR(x) errors |= 1; warnx("%s: %s", fn, strerror(x)); continue;
1.9 deraadt 105: while ((fn = *argv++)) {
1.1 deraadt 106: if ((fd = open(fn, O_RDWR)) < 0) {
107: ERROR(errno);
108: }
109: if (fstat(fd, &sb)) {
110: (void)close(fd);
111: ERROR(errno);
112: }
113: if (sb.st_size < sizeof(EXEC)) {
114: (void)close(fd);
115: ERROR(EFTYPE);
116: }
117: if ((ep = (EXEC *)mmap(NULL, sb.st_size, PROT_READ|PROT_WRITE,
1.13 art 118: MAP_SHARED, fd, (off_t)0)) == MAP_FAILED) {
1.1 deraadt 119: (void)close(fd);
120: ERROR(errno);
121: }
1.11 espie 122: if (BAD_OBJECT(*ep)) {
1.1 deraadt 123: munmap((caddr_t)ep, sb.st_size);
124: (void)close(fd);
125: ERROR(EFTYPE);
126: }
1.11 espie 127: /* since we're dealing with an mmap there, we have to convert once
128: for dealing with data in memory, and a second time for out
129: */
130: fix_header_order(ep);
1.17 espie 131: newsize = 0;
132: errors |= sfcn(fn, fd, ep, &sb, &newsize);
1.11 espie 133: fix_header_order(ep);
1.1 deraadt 134: munmap((caddr_t)ep, sb.st_size);
1.17 espie 135: if (newsize && ftruncate(fd, newsize)) {
136: warn("%s", fn);
137: errors = 1;
138: }
1.1 deraadt 139: if (close(fd)) {
140: ERROR(errno);
141: }
142: }
143: #undef ERROR
144: exit(errors);
145: }
146:
147: int
1.17 espie 148: s_sym(fn, fd, ep, sp, sz)
1.1 deraadt 149: const char *fn;
150: int fd;
1.15 mpech 151: EXEC *ep;
1.1 deraadt 152: struct stat *sp;
1.17 espie 153: off_t *sz;
1.1 deraadt 154: {
1.15 mpech 155: char *neweof;
1.14 pvalchev 156: #if 0
1.15 mpech 157: char *mineof;
1.14 pvalchev 158: #endif
1.1 deraadt 159: int zmagic;
160:
161: zmagic = ep->a_data &&
162: (N_GETMAGIC(*ep) == ZMAGIC || N_GETMAGIC(*ep) == QMAGIC);
163:
164: /*
165: * If no symbols or data/text relocation info and
166: * the file data segment size is already minimized, quit.
167: */
168: if (!ep->a_syms && !ep->a_trsize && !ep->a_drsize) {
169: #if 0
170: if (!zmagic)
171: return 0;
172: if (sp->st_size < N_TRELOFF(*ep))
173: #endif
174: return 0;
175: }
176:
177: /*
178: * New file size is the header plus text and data segments; OMAGIC
179: * and NMAGIC formats have the text/data immediately following the
180: * header. ZMAGIC format wastes the rest of of header page.
181: */
182: neweof = (char *)ep + N_TRELOFF(*ep);
183:
184: #if 0
185: /*
186: * Unfortunately, this can't work correctly without changing the way
187: * the loader works. We could cap it at one page, or even fiddle with
188: * a_data and a_bss, but this only works for CLBYTES == NBPG. If we
189: * are on a system where, e.g., CLBYTES is 8k and NBPG is 4k, and we
190: * happen to remove 4.5k, we will lose. And we really don't want to
191: * fiddle with pages, because that breaks binary compatibility. Lose.
192: */
193:
194: if (zmagic) {
195: /*
196: * Get rid of unneeded zeroes at the end of the data segment
197: * to reduce the file size even more.
198: */
199: mineof = (char *)ep + N_DATOFF(*ep);
200: while (neweof > mineof && neweof[-1] == '\0')
201: neweof--;
202: }
203: #endif
204:
205: /* Set symbol size and relocation info values to 0. */
206: ep->a_syms = ep->a_trsize = ep->a_drsize = 0;
207:
208: /* Truncate the file. */
1.17 espie 209: *sz = neweof - (char *)ep;
1.1 deraadt 210:
211: return 0;
212: }
213:
214: int
1.17 espie 215: s_stab(fn, fd, ep, sp, sz)
1.1 deraadt 216: const char *fn;
217: int fd;
218: EXEC *ep;
219: struct stat *sp;
1.17 espie 220: off_t *sz;
1.1 deraadt 221: {
1.15 mpech 222: int cnt, len;
1.18 espie 223: char *nstr, *nstrbase=0, *used=0, *p, *strbase;
1.15 mpech 224: NLIST *sym, *nsym;
1.11 espie 225: u_long allocsize;
226: int mid;
1.1 deraadt 227: NLIST *symbase;
1.18 espie 228: unsigned int *mapping=0;
229: int error=1;
230: unsigned int nsyms;
231: struct relocation_info *reloc_base;
232: unsigned int i, j;
1.1 deraadt 233:
234: /* Quit if no symbols. */
235: if (ep->a_syms == 0)
236: return 0;
237:
238: if (N_SYMOFF(*ep) >= sp->st_size) {
1.7 mickey 239: warnx("%s: bad symbol table", fn);
1.1 deraadt 240: return 1;
241: }
242:
1.11 espie 243: mid = N_GETMID(*ep);
244:
1.1 deraadt 245: /*
246: * Initialize old and new symbol pointers. They both point to the
247: * beginning of the symbol table in memory, since we're deleting
248: * entries.
249: */
250: sym = nsym = symbase = (NLIST *)((char *)ep + N_SYMOFF(*ep));
251:
252: /*
253: * Allocate space for the new string table, initialize old and
254: * new string pointers. Handle the extra long at the beginning
255: * of the string table.
256: */
257: strbase = (char *)ep + N_STROFF(*ep);
1.11 espie 258: allocsize = fix_long_order(*(u_long *)strbase, mid);
259: if ((nstrbase = malloc((u_int) allocsize)) == NULL) {
1.7 mickey 260: warnx("%s", strerror(ENOMEM));
1.18 espie 261: goto end;
1.1 deraadt 262: }
263: nstr = nstrbase + sizeof(u_long);
264:
1.18 espie 265: /* okay, so we also need to keep symbol numbers for relocations. */
266: nsyms = ep->a_syms/ sizeof(NLIST);
267: used = calloc(nsyms, 1);
268: if (!used) {
269: warnx("%s", strerror(ENOMEM));
270: goto end;
271: }
272: mapping = malloc(nsyms * sizeof(unsigned int));
273: if (!mapping) {
274: warnx("%s", strerror(ENOMEM));
275: goto end;
276: }
277:
278: if ((ep->a_trsize || ep->a_drsize) && byte_sex(mid) != BYTE_ORDER) {
279: warnx("%s: cross-stripping not supported", fn);
280: goto end;
281: }
282:
283: /* first check the relocations for used symbols, and mark them */
284: /* text */
285: reloc_base = (struct relocation_info *) ((char *)ep + N_TRELOFF(*ep));
286: if (N_TRELOFF(*ep) + ep->a_trsize > sp->st_size) {
287: warnx("%s: bad text relocation", fn);
288: goto end;
289: }
290: for (i = 0; i < ep->a_trsize / sizeof(struct relocation_info); i++) {
291: if (!reloc_base[i].r_extern)
292: continue;
293: if (reloc_base[i].r_symbolnum > nsyms) {
294: warnx("%s: bad symbol number in text relocation", fn);
295: goto end;
296: }
297: used[reloc_base[i].r_symbolnum] = 1;
298: }
299: /* data */
300: reloc_base = (struct relocation_info *) ((char *)ep + N_DRELOFF(*ep));
301: if (N_DRELOFF(*ep) + ep->a_drsize > sp->st_size) {
302: warnx("%s: bad data relocation", fn);
303: goto end;
304: }
305: for (i = 0; i < ep->a_drsize / sizeof(struct relocation_info); i++) {
306: if (!reloc_base[i].r_extern)
307: continue;
308: if (reloc_base[i].r_symbolnum > nsyms) {
309: warnx("%s: bad symbol number in data relocation", fn);
310: goto end;
311: }
312: used[reloc_base[i].r_symbolnum] = 1;
313: }
314:
1.1 deraadt 315: /*
316: * Read through the symbol table. For each non-debugging symbol,
317: * copy it and save its string in the new string table. Keep
318: * track of the number of symbols.
319: */
1.18 espie 320: for (cnt = nsyms, i = 0, j = 0; cnt--; ++sym, ++i) {
1.11 espie 321: fix_nlist_order(sym, mid);
1.1 deraadt 322: if (!(sym->n_type & N_STAB) && sym->strx) {
323: *nsym = *sym;
324: nsym->strx = nstr - nstrbase;
325: p = strbase + sym->strx;
1.18 espie 326: if (xflag && !used[i] &&
1.1 deraadt 327: (!(sym->n_type & N_EXT) ||
328: (sym->n_type & ~N_EXT) == N_FN ||
329: strcmp(p, "gcc_compiled.") == 0 ||
330: strcmp(p, "gcc2_compiled.") == 0 ||
331: strncmp(p, "___gnu_compiled_", 16) == 0)) {
332: continue;
333: }
334: len = strlen(p) + 1;
1.18 espie 335: mapping[i] = j++;
1.17 espie 336: if (N_STROFF(*ep) + sym->strx + len > sp->st_size) {
337: warnx("%s: bad symbol table", fn);
1.18 espie 338: goto end;
1.17 espie 339: }
1.1 deraadt 340: bcopy(p, nstr, len);
341: nstr += len;
1.11 espie 342: fix_nlist_order(nsym++, mid);
1.1 deraadt 343: }
1.11 espie 344: }
1.1 deraadt 345:
1.18 espie 346: /* renumber symbol relocations */
347: /* text */
348: reloc_base = (struct relocation_info *) ((char *)ep + N_TRELOFF(*ep));
349: for (i = 0; i < ep->a_trsize / sizeof(struct relocation_info); i++) {
350: if (!reloc_base[i].r_extern)
351: continue;
352: reloc_base[i].r_symbolnum = mapping[reloc_base[i].r_symbolnum];
353: }
354: /* data */
355: reloc_base = (struct relocation_info *) ((char *)ep + N_DRELOFF(*ep));
356: for (i = 0; i < ep->a_drsize / sizeof(struct relocation_info); i++) {
357: if (!reloc_base[i].r_extern)
358: continue;
359: reloc_base[i].r_symbolnum = mapping[reloc_base[i].r_symbolnum];
360: }
361:
1.1 deraadt 362: /* Fill in new symbol table size. */
363: ep->a_syms = (nsym - symbase) * sizeof(NLIST);
364:
365: /* Fill in the new size of the string table. */
1.11 espie 366: len = nstr - nstrbase;
367: *(u_long *)nstrbase = fix_long_order(len, mid);
1.1 deraadt 368:
369: /*
370: * Copy the new string table into place. Nsym should be pointing
371: * at the address past the last symbol entry.
372: */
373: bcopy(nstrbase, (void *)nsym, len);
1.18 espie 374: error = 0;
375: end:
1.1 deraadt 376: free(nstrbase);
1.18 espie 377: free(used);
378: free(mapping);
1.1 deraadt 379:
380: /* Truncate to the current length. */
1.17 espie 381: *sz = (char *)nsym + len - (char *)ep;
1.1 deraadt 382:
1.18 espie 383: return error;
1.1 deraadt 384: }
385:
386: void
387: usage()
388: {
389: (void)fprintf(stderr, "usage: strip [-dx] file ...\n");
390: exit(1);
391: }
392: