Annotation of src/usr.bin/strip/strip.c, Revision 1.16
1.16 ! millert 1: /* $OpenBSD: strip.c,v 1.15 2001/11/19 19:02:16 mpech 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.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
37: char copyright[] =
38: "@(#) Copyright (c) 1988 Regents of the University of California.\n\
39: All rights reserved.\n";
40: #endif /* not lint */
41:
42: #ifndef lint
43: /*static char sccsid[] = "from: @(#)strip.c 5.8 (Berkeley) 11/6/91";*/
1.16 ! millert 44: static char rcsid[] = "$OpenBSD: strip.c,v 1.15 2001/11/19 19:02:16 mpech Exp $";
1.1 deraadt 45: #endif /* not lint */
46:
1.4 deraadt 47: #include <sys/param.h>
1.1 deraadt 48: #include <sys/types.h>
49: #include <sys/stat.h>
50: #include <sys/mman.h>
51: #include <fcntl.h>
52: #include <errno.h>
53: #include <a.out.h>
54: #include <unistd.h>
55: #include <stdio.h>
56: #include <stdlib.h>
57: #include <string.h>
1.7 mickey 58: #include <err.h>
1.11 espie 59: #include <ranlib.h>
60: #include "byte.c"
1.10 niklas 61:
62: #ifdef MID_MACHINE_OVERRIDE
63: #undef MID_MACHINE
64: #define MID_MACHINE MID_MACHINE_OVERRIDE
65: #endif
1.1 deraadt 66:
67: typedef struct exec EXEC;
68: typedef struct nlist NLIST;
69:
70: #define strx n_un.n_strx
71:
1.16 ! millert 72: int s_stab(const char *, int, EXEC *, struct stat *);
! 73: int s_sym(const char *, int, EXEC *, struct stat *);
! 74: void usage(void);
1.1 deraadt 75:
76: int xflag = 0;
77:
1.9 deraadt 78: int
1.1 deraadt 79: main(argc, argv)
80: int argc;
81: char *argv[];
82: {
1.15 mpech 83: int fd;
1.1 deraadt 84: EXEC *ep;
85: struct stat sb;
1.16 ! millert 86: int (*sfcn)(const char *, int, EXEC *, struct stat *);
1.1 deraadt 87: int ch, errors;
88: char *fn;
89:
90: sfcn = s_sym;
1.6 millert 91: while ((ch = getopt(argc, argv, "dx")) != -1)
1.1 deraadt 92: switch(ch) {
93: case 'x':
94: xflag = 1;
95: /*FALLTHROUGH*/
96: case 'd':
97: sfcn = s_stab;
98: break;
99: case '?':
100: default:
101: usage();
102: }
103: argc -= optind;
104: argv += optind;
105:
106: errors = 0;
1.8 mickey 107: #define ERROR(x) errors |= 1; warnx("%s: %s", fn, strerror(x)); continue;
1.9 deraadt 108: while ((fn = *argv++)) {
1.1 deraadt 109: if ((fd = open(fn, O_RDWR)) < 0) {
110: ERROR(errno);
111: }
112: if (fstat(fd, &sb)) {
113: (void)close(fd);
114: ERROR(errno);
115: }
116: if (sb.st_size < sizeof(EXEC)) {
117: (void)close(fd);
118: ERROR(EFTYPE);
119: }
120: if ((ep = (EXEC *)mmap(NULL, sb.st_size, PROT_READ|PROT_WRITE,
1.13 art 121: MAP_SHARED, fd, (off_t)0)) == MAP_FAILED) {
1.1 deraadt 122: (void)close(fd);
123: ERROR(errno);
124: }
1.11 espie 125: if (BAD_OBJECT(*ep)) {
1.1 deraadt 126: munmap((caddr_t)ep, sb.st_size);
127: (void)close(fd);
128: ERROR(EFTYPE);
129: }
1.11 espie 130: /* since we're dealing with an mmap there, we have to convert once
131: for dealing with data in memory, and a second time for out
132: */
133: fix_header_order(ep);
1.1 deraadt 134: errors |= sfcn(fn, fd, ep, &sb);
1.11 espie 135: fix_header_order(ep);
1.1 deraadt 136: munmap((caddr_t)ep, sb.st_size);
137: if (close(fd)) {
138: ERROR(errno);
139: }
140: }
141: #undef ERROR
142: exit(errors);
143: }
144:
145: int
146: s_sym(fn, fd, ep, sp)
147: const char *fn;
148: int fd;
1.15 mpech 149: EXEC *ep;
1.1 deraadt 150: struct stat *sp;
151: {
1.15 mpech 152: char *neweof;
1.14 pvalchev 153: #if 0
1.15 mpech 154: char *mineof;
1.14 pvalchev 155: #endif
1.1 deraadt 156: int zmagic;
157:
158: zmagic = ep->a_data &&
159: (N_GETMAGIC(*ep) == ZMAGIC || N_GETMAGIC(*ep) == QMAGIC);
160:
161: /*
162: * If no symbols or data/text relocation info and
163: * the file data segment size is already minimized, quit.
164: */
165: if (!ep->a_syms && !ep->a_trsize && !ep->a_drsize) {
166: #if 0
167: if (!zmagic)
168: return 0;
169: if (sp->st_size < N_TRELOFF(*ep))
170: #endif
171: return 0;
172: }
173:
174: /*
175: * New file size is the header plus text and data segments; OMAGIC
176: * and NMAGIC formats have the text/data immediately following the
177: * header. ZMAGIC format wastes the rest of of header page.
178: */
179: neweof = (char *)ep + N_TRELOFF(*ep);
180:
181: #if 0
182: /*
183: * Unfortunately, this can't work correctly without changing the way
184: * the loader works. We could cap it at one page, or even fiddle with
185: * a_data and a_bss, but this only works for CLBYTES == NBPG. If we
186: * are on a system where, e.g., CLBYTES is 8k and NBPG is 4k, and we
187: * happen to remove 4.5k, we will lose. And we really don't want to
188: * fiddle with pages, because that breaks binary compatibility. Lose.
189: */
190:
191: if (zmagic) {
192: /*
193: * Get rid of unneeded zeroes at the end of the data segment
194: * to reduce the file size even more.
195: */
196: mineof = (char *)ep + N_DATOFF(*ep);
197: while (neweof > mineof && neweof[-1] == '\0')
198: neweof--;
199: }
200: #endif
201:
202: /* Set symbol size and relocation info values to 0. */
203: ep->a_syms = ep->a_trsize = ep->a_drsize = 0;
204:
205: /* Truncate the file. */
206: if (ftruncate(fd, neweof - (char *)ep)) {
1.12 millert 207: warn("%s", fn);
1.1 deraadt 208: return 1;
209: }
210:
211: return 0;
212: }
213:
214: int
215: s_stab(fn, fd, ep, sp)
216: const char *fn;
217: int fd;
218: EXEC *ep;
219: struct stat *sp;
220: {
1.15 mpech 221: int cnt, len;
222: char *nstr, *nstrbase, *p, *strbase;
223: NLIST *sym, *nsym;
1.11 espie 224: u_long allocsize;
225: int mid;
1.1 deraadt 226: NLIST *symbase;
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.1 deraadt 255: return 1;
256: }
257: nstr = nstrbase + sizeof(u_long);
258:
259: /*
260: * Read through the symbol table. For each non-debugging symbol,
261: * copy it and save its string in the new string table. Keep
262: * track of the number of symbols.
263: */
1.11 espie 264: for (cnt = ep->a_syms / sizeof(NLIST); cnt--; ++sym) {
265: fix_nlist_order(sym, mid);
1.1 deraadt 266: if (!(sym->n_type & N_STAB) && sym->strx) {
267: *nsym = *sym;
268: nsym->strx = nstr - nstrbase;
269: p = strbase + sym->strx;
270: if (xflag &&
271: (!(sym->n_type & N_EXT) ||
272: (sym->n_type & ~N_EXT) == N_FN ||
273: strcmp(p, "gcc_compiled.") == 0 ||
274: strcmp(p, "gcc2_compiled.") == 0 ||
275: strncmp(p, "___gnu_compiled_", 16) == 0)) {
276: continue;
277: }
278: len = strlen(p) + 1;
279: bcopy(p, nstr, len);
280: nstr += len;
1.11 espie 281: fix_nlist_order(nsym++, mid);
1.1 deraadt 282: }
1.11 espie 283: }
1.1 deraadt 284:
285: /* Fill in new symbol table size. */
286: ep->a_syms = (nsym - symbase) * sizeof(NLIST);
287:
288: /* Fill in the new size of the string table. */
1.11 espie 289: len = nstr - nstrbase;
290: *(u_long *)nstrbase = fix_long_order(len, mid);
1.1 deraadt 291:
292: /*
293: * Copy the new string table into place. Nsym should be pointing
294: * at the address past the last symbol entry.
295: */
296: bcopy(nstrbase, (void *)nsym, len);
297: free(nstrbase);
298:
299: /* Truncate to the current length. */
300: if (ftruncate(fd, (char *)nsym + len - (char *)ep)) {
1.12 millert 301: warn("%s", fn);
1.1 deraadt 302: return 1;
303: }
304:
305: return 0;
306: }
307:
308: void
309: usage()
310: {
311: (void)fprintf(stderr, "usage: strip [-dx] file ...\n");
312: exit(1);
313: }
314: