Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1988 Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
35 | char copyright[] = | |
36 | "@(#) Copyright (c) 1988 Regents of the University of California.\n\ | |
37 | All rights reserved.\n"; | |
38 | #endif /* not lint */ | |
39 | ||
40 | #ifndef lint | |
41 | static char sccsid[] = "@(#)strip.c 5.7 (Berkeley) 5/26/91"; | |
42 | #endif /* not lint */ | |
43 | ||
44 | #include <sys/types.h> | |
45 | #include <sys/stat.h> | |
46 | #include <fcntl.h> | |
47 | #include <errno.h> | |
48 | #include <a.out.h> | |
49 | #include <unistd.h> | |
50 | #include <stdio.h> | |
51 | #include <stdlib.h> | |
52 | #include <string.h> | |
53 | ||
54 | typedef struct exec EXEC; | |
55 | typedef struct nlist NLIST; | |
56 | ||
57 | void err __P((const char *fmt, ...)); | |
58 | void s_stab __P((const char *, int, EXEC *)); | |
59 | void s_sym __P((const char *, int, EXEC *)); | |
60 | void usage __P((void)); | |
61 | ||
62 | int eval; | |
63 | ||
64 | main(argc, argv) | |
65 | int argc; | |
66 | char *argv[]; | |
67 | { | |
68 | register int fd, nb; | |
69 | EXEC head; | |
70 | void (*sfcn)__P((const char *, int, EXEC *)); | |
71 | int ch; | |
72 | char *fn; | |
73 | ||
74 | sfcn = s_sym; | |
75 | while ((ch = getopt(argc, argv, "d")) != EOF) | |
76 | switch(ch) { | |
77 | case 'd': | |
78 | sfcn = s_stab; | |
79 | break; | |
80 | case '?': | |
81 | default: | |
82 | usage(); | |
83 | } | |
84 | argc -= optind; | |
85 | argv += optind; | |
86 | ||
87 | while (fn = *argv++) { | |
88 | if ((fd = open(fn, O_RDWR)) < 0 || | |
89 | (nb = read(fd, &head, sizeof(EXEC))) == -1) { | |
90 | err("%s: %s", fn, strerror(errno)); | |
91 | continue; | |
92 | } | |
93 | if (nb != sizeof(EXEC) || N_BADMAG(head)) { | |
94 | err("%s: %s", fn, strerror(EFTYPE)); | |
95 | continue; | |
96 | } | |
97 | sfcn(fn, fd, &head); | |
98 | if (close(fd)) | |
99 | err("%s: %s", fn, strerror(errno)); | |
100 | } | |
101 | exit(eval); | |
102 | } | |
103 | ||
104 | void | |
105 | s_sym(fn, fd, ep) | |
106 | const char *fn; | |
107 | int fd; | |
108 | register EXEC *ep; | |
109 | { | |
110 | static int pagesize = -1; | |
111 | register off_t fsize; | |
112 | ||
113 | /* If no symbols or data/text relocation info, quit. */ | |
114 | if (!ep->a_syms && !ep->a_trsize && !ep->a_drsize) | |
115 | return; | |
116 | ||
117 | /* | |
118 | * New file size is the header plus text and data segments; OMAGIC | |
119 | * and NMAGIC formats have the text/data immediately following the | |
120 | * header. ZMAGIC format wastes the rest of of header page. | |
121 | */ | |
122 | if (ep->a_magic == ZMAGIC) | |
123 | fsize = pagesize == -1 ? (pagesize = getpagesize()) : pagesize; | |
124 | else | |
125 | fsize = sizeof(EXEC); | |
126 | fsize += ep->a_text + ep->a_data; | |
127 | ||
128 | /* Set symbol size and relocation info values to 0. */ | |
129 | ep->a_syms = ep->a_trsize = ep->a_drsize = 0; | |
130 | ||
131 | /* Rewrite the header and truncate the file. */ | |
132 | if (lseek(fd, 0L, SEEK_SET) == -1 || | |
133 | write(fd, ep, sizeof(EXEC)) != sizeof(EXEC) || | |
134 | ftruncate(fd, fsize)) | |
135 | err("%s: %s", fn, strerror(errno)); | |
136 | } | |
137 | ||
138 | void | |
139 | s_stab(fn, fd, ep) | |
140 | const char *fn; | |
141 | int fd; | |
142 | EXEC *ep; | |
143 | { | |
144 | struct stat sb; | |
145 | register NLIST *bsym2, *sym1, *sym2; | |
146 | register u_long nsym1, nsym2; | |
147 | register char *p, *bstr2, *str1, *str2; | |
148 | register int len, symlen; | |
149 | off_t fsize; | |
150 | int nb; | |
151 | char *bp; | |
152 | ||
153 | /* Quit if no symbols. */ | |
154 | if (ep->a_syms == 0) | |
155 | return; | |
156 | ||
157 | bsym2 = NULL; | |
158 | bp = bstr2 = NULL; | |
159 | ||
160 | /* Read the file into memory. XXX mmap */ | |
161 | if (fstat(fd, &sb)) | |
162 | goto syserr; | |
163 | if ((bp = malloc(sb.st_size)) == NULL) | |
164 | goto syserr; | |
165 | if (lseek(fd, 0L, SEEK_SET) == -1) | |
166 | goto syserr; | |
167 | if ((nb = read(fd, bp, (int)sb.st_size)) == -1) | |
168 | goto syserr; | |
169 | if (nb != sb.st_size) { | |
170 | errno = EIO; | |
171 | goto syserr; | |
172 | } | |
173 | ||
174 | /* | |
175 | * Allocate space for new symbol and string tables. Allocate before | |
176 | * reading the symbol tables so we can do it all in a single pass. | |
177 | * This loses if there weren't any symbols to strip, but that's life. | |
178 | */ | |
179 | sym1 = (NLIST *)(bp + N_SYMOFF(*ep)); | |
180 | if ((bsym2 = sym2 = malloc((u_int)ep->a_syms)) == NULL) { | |
181 | err("%s", strerror(errno)); | |
182 | goto mem; | |
183 | } | |
184 | str1 = bp + N_STROFF(*ep); | |
185 | if ((bstr2 = malloc((u_int)*(u_long *)str1)) == NULL) { | |
186 | err("%s", strerror(errno)); | |
187 | goto mem; | |
188 | } | |
189 | str2 = bstr2 + sizeof(u_long); | |
190 | symlen = sizeof(u_long); | |
191 | ||
192 | /* | |
193 | * Read through the symbol table. For each non-debugging symbol, | |
194 | * copy it into the new symbol and string tables. Keep track of | |
195 | * how many symbols are copied and the length of the new string | |
196 | * table. | |
197 | */ | |
198 | #define strx n_un.n_strx | |
199 | nsym2 = 0; | |
200 | for (nsym1 = ep->a_syms / sizeof(NLIST); nsym1--; ++sym1) | |
201 | if (!(sym1->n_type & N_STAB) && sym1->strx) { | |
202 | *sym2 = *sym1; | |
203 | sym2->strx = str2 - bstr2; | |
204 | p = str1 + sym1->strx; | |
205 | len = strlen(p) + 1; | |
206 | bcopy(p, str2, len); | |
207 | symlen += len; | |
208 | str2 += len; | |
209 | ++sym2; | |
210 | ++nsym2; | |
211 | } | |
212 | ||
213 | /* If no debugging symbols, quit. */ | |
214 | if (!nsym2) | |
215 | goto mem; | |
216 | ||
217 | /* Fill in new symbol table size. */ | |
218 | ep->a_syms = nsym2 * sizeof(NLIST); | |
219 | ||
220 | /* Write out the header. */ | |
221 | if (lseek(fd, 0L, SEEK_SET) == -1 || | |
222 | write(fd, ep, sizeof(EXEC)) != sizeof(EXEC)) | |
223 | goto syserr; | |
224 | ||
225 | /* Write out the symbol table. */ | |
226 | if (lseek(fd, N_SYMOFF(*ep), SEEK_SET) == -1 || | |
227 | write(fd, bsym2, ep->a_syms) != ep->a_syms) | |
228 | goto syserr; | |
229 | ||
230 | /* Fill in the new size of the string table. */ | |
231 | *(u_long *)bstr2 = symlen; | |
232 | ||
233 | /* Write out the string table. */ | |
234 | if (write(fd, bstr2, symlen) != symlen) | |
235 | goto syserr; | |
236 | ||
237 | /* Truncate to the current length. */ | |
238 | if ((fsize = lseek(fd, 0L, SEEK_CUR)) == -1) | |
239 | goto syserr; | |
240 | if (ftruncate(fd, fsize)) | |
241 | syserr: err("%s: %s", fn, strerror(errno)); | |
242 | ||
243 | mem: if (bp) | |
244 | free(bp); | |
245 | if (bstr2) | |
246 | free(bstr2); | |
247 | if (bsym2) | |
248 | free(bsym2); | |
249 | } | |
250 | ||
251 | void | |
252 | usage() | |
253 | { | |
254 | ||
255 | (void)fprintf(stderr, "usage: strip [-d] file ...\n"); | |
256 | exit(1); | |
257 | } | |
258 | ||
259 | #if __STDC__ | |
260 | #include <stdarg.h> | |
261 | #else | |
262 | #include <varargs.h> | |
263 | #endif | |
264 | ||
265 | void | |
266 | #if __STDC__ | |
267 | err(const char *fmt, ...) | |
268 | #else | |
269 | err(fmt, va_alist) | |
270 | char *fmt; | |
271 | va_dcl | |
272 | #endif | |
273 | { | |
274 | va_list ap; | |
275 | #if __STDC__ | |
276 | va_start(ap, fmt); | |
277 | #else | |
278 | va_start(ap); | |
279 | #endif | |
280 | (void)fprintf(stderr, "strip: "); | |
281 | (void)vfprintf(stderr, fmt, ap); | |
282 | va_end(ap); | |
283 | (void)fprintf(stderr, "\n"); | |
284 | eval = 1; | |
285 | } |