Annotation of src/usr.bin/strip/strip.c, Revision 1.9
1.9 ! deraadt 1: /* $OpenBSD: strip.c,v 1.8 1997/04/03 19:33:50 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.
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.9 ! deraadt 44: static char rcsid[] = "$OpenBSD: strip.c,v 1.8 1997/04/03 19:33:50 mickey 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.1 deraadt 59:
60: typedef struct exec EXEC;
61: typedef struct nlist NLIST;
62:
63: #define strx n_un.n_strx
64:
65: int s_stab __P((const char *, int, EXEC *, struct stat *));
66: int s_sym __P((const char *, int, EXEC *, struct stat *));
67: void usage __P((void));
68:
69: int xflag = 0;
70:
1.9 ! deraadt 71: int
1.1 deraadt 72: main(argc, argv)
73: int argc;
74: char *argv[];
75: {
1.9 ! deraadt 76: register int fd;
1.1 deraadt 77: EXEC *ep;
78: struct stat sb;
79: int (*sfcn)__P((const char *, int, EXEC *, struct stat *));
80: int ch, errors;
81: char *fn;
82:
83: sfcn = s_sym;
1.6 millert 84: while ((ch = getopt(argc, argv, "dx")) != -1)
1.1 deraadt 85: switch(ch) {
86: case 'x':
87: xflag = 1;
88: /*FALLTHROUGH*/
89: case 'd':
90: sfcn = s_stab;
91: break;
92: case '?':
93: default:
94: usage();
95: }
96: argc -= optind;
97: argv += optind;
98:
99: errors = 0;
1.8 mickey 100: #define ERROR(x) errors |= 1; warnx("%s: %s", fn, strerror(x)); continue;
1.9 ! deraadt 101: while ((fn = *argv++)) {
1.1 deraadt 102: if ((fd = open(fn, O_RDWR)) < 0) {
103: ERROR(errno);
104: }
105: if (fstat(fd, &sb)) {
106: (void)close(fd);
107: ERROR(errno);
108: }
109: if (sb.st_size < sizeof(EXEC)) {
110: (void)close(fd);
111: ERROR(EFTYPE);
112: }
113: if ((ep = (EXEC *)mmap(NULL, sb.st_size, PROT_READ|PROT_WRITE,
114: MAP_SHARED, fd, (off_t)0)) == (EXEC *)-1) {
115: (void)close(fd);
116: ERROR(errno);
117: }
1.5 downsj 118: #if (MID_MACHINE == MID_M68K)
119: if (N_BADMAG(*ep) || ((N_GETMID(*ep) != MID_MACHINE) &&
120: (N_GETMID(*ep) != MID_M68K4K))) {
121: #else
1.3 deraadt 122: if (N_BADMAG(*ep) || N_GETMID(*ep) != MID_MACHINE) {
1.5 downsj 123: #endif
1.1 deraadt 124: munmap((caddr_t)ep, sb.st_size);
125: (void)close(fd);
126: ERROR(EFTYPE);
127: }
128: errors |= sfcn(fn, fd, ep, &sb);
129: munmap((caddr_t)ep, sb.st_size);
130: if (close(fd)) {
131: ERROR(errno);
132: }
133: }
134: #undef ERROR
135: exit(errors);
136: }
137:
138: int
139: s_sym(fn, fd, ep, sp)
140: const char *fn;
141: int fd;
142: register EXEC *ep;
143: struct stat *sp;
144: {
145: register char *neweof, *mineof;
146: int zmagic;
147:
148: zmagic = ep->a_data &&
149: (N_GETMAGIC(*ep) == ZMAGIC || N_GETMAGIC(*ep) == QMAGIC);
150:
151: /*
152: * If no symbols or data/text relocation info and
153: * the file data segment size is already minimized, quit.
154: */
155: if (!ep->a_syms && !ep->a_trsize && !ep->a_drsize) {
156: #if 0
157: if (!zmagic)
158: return 0;
159: if (sp->st_size < N_TRELOFF(*ep))
160: #endif
161: return 0;
162: }
163:
164: /*
165: * New file size is the header plus text and data segments; OMAGIC
166: * and NMAGIC formats have the text/data immediately following the
167: * header. ZMAGIC format wastes the rest of of header page.
168: */
169: neweof = (char *)ep + N_TRELOFF(*ep);
170:
171: #if 0
172: /*
173: * Unfortunately, this can't work correctly without changing the way
174: * the loader works. We could cap it at one page, or even fiddle with
175: * a_data and a_bss, but this only works for CLBYTES == NBPG. If we
176: * are on a system where, e.g., CLBYTES is 8k and NBPG is 4k, and we
177: * happen to remove 4.5k, we will lose. And we really don't want to
178: * fiddle with pages, because that breaks binary compatibility. Lose.
179: */
180:
181: if (zmagic) {
182: /*
183: * Get rid of unneeded zeroes at the end of the data segment
184: * to reduce the file size even more.
185: */
186: mineof = (char *)ep + N_DATOFF(*ep);
187: while (neweof > mineof && neweof[-1] == '\0')
188: neweof--;
189: }
190: #endif
191:
192: /* Set symbol size and relocation info values to 0. */
193: ep->a_syms = ep->a_trsize = ep->a_drsize = 0;
194:
195: /* Truncate the file. */
196: if (ftruncate(fd, neweof - (char *)ep)) {
1.7 mickey 197: warn(fn);
1.1 deraadt 198: return 1;
199: }
200:
201: return 0;
202: }
203:
204: int
205: s_stab(fn, fd, ep, sp)
206: const char *fn;
207: int fd;
208: EXEC *ep;
209: struct stat *sp;
210: {
211: register int cnt, len, nsymcnt;
212: register char *nstr, *nstrbase, *p, *strbase;
213: register NLIST *sym, *nsym;
214: NLIST *symbase;
215:
216: /* Quit if no symbols. */
217: if (ep->a_syms == 0)
218: return 0;
219:
220: if (N_SYMOFF(*ep) >= sp->st_size) {
1.7 mickey 221: warnx("%s: bad symbol table", fn);
1.1 deraadt 222: return 1;
223: }
224:
225: /*
226: * Initialize old and new symbol pointers. They both point to the
227: * beginning of the symbol table in memory, since we're deleting
228: * entries.
229: */
230: sym = nsym = symbase = (NLIST *)((char *)ep + N_SYMOFF(*ep));
231:
232: /*
233: * Allocate space for the new string table, initialize old and
234: * new string pointers. Handle the extra long at the beginning
235: * of the string table.
236: */
237: strbase = (char *)ep + N_STROFF(*ep);
238: if ((nstrbase = malloc((u_int)*(u_long *)strbase)) == NULL) {
1.7 mickey 239: warnx("%s", strerror(ENOMEM));
1.1 deraadt 240: return 1;
241: }
242: nstr = nstrbase + sizeof(u_long);
243:
244: /*
245: * Read through the symbol table. For each non-debugging symbol,
246: * copy it and save its string in the new string table. Keep
247: * track of the number of symbols.
248: */
249: for (cnt = ep->a_syms / sizeof(NLIST); cnt--; ++sym)
250: if (!(sym->n_type & N_STAB) && sym->strx) {
251: *nsym = *sym;
252: nsym->strx = nstr - nstrbase;
253: p = strbase + sym->strx;
254: if (xflag &&
255: (!(sym->n_type & N_EXT) ||
256: (sym->n_type & ~N_EXT) == N_FN ||
257: strcmp(p, "gcc_compiled.") == 0 ||
258: strcmp(p, "gcc2_compiled.") == 0 ||
259: strncmp(p, "___gnu_compiled_", 16) == 0)) {
260: continue;
261: }
262: len = strlen(p) + 1;
263: bcopy(p, nstr, len);
264: nstr += len;
265: ++nsym;
266: }
267:
268: /* Fill in new symbol table size. */
269: ep->a_syms = (nsym - symbase) * sizeof(NLIST);
270:
271: /* Fill in the new size of the string table. */
272: *(u_long *)nstrbase = len = nstr - nstrbase;
273:
274: /*
275: * Copy the new string table into place. Nsym should be pointing
276: * at the address past the last symbol entry.
277: */
278: bcopy(nstrbase, (void *)nsym, len);
279: free(nstrbase);
280:
281: /* Truncate to the current length. */
282: if (ftruncate(fd, (char *)nsym + len - (char *)ep)) {
1.7 mickey 283: warn(fn);
1.1 deraadt 284: return 1;
285: }
286:
287: return 0;
288: }
289:
290: void
291: usage()
292: {
293: (void)fprintf(stderr, "usage: strip [-dx] file ...\n");
294: exit(1);
295: }
296: