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