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