Fix typo.
[dragonfly.git] / usr.bin / strip / strip.c
blob90f685fa39f089976fd09bca5dbcd893cb5ea82b
1 /*
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
33 * @(#) Copyright (c) 1988, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)strip.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.bin/strip/strip.c,v 1.12 1999/08/28 01:05:53 peter Exp $
36 * $DragonFly: src/usr.bin/strip/Attic/strip.c,v 1.5 2005/01/01 00:22:33 cpressey Exp $
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/mman.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
46 #include <a.out.h>
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <limits.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
56 typedef struct exec EXEC;
57 typedef struct nlist NLIST;
59 #define strx n_un.n_strx
61 void s_stab(const char *, int, EXEC *);
62 void s_sym(const char *, int, EXEC *);
63 static void usage(void);
65 int xflag = 0;
66 int err_val = 0;
68 int
69 main(int argc, char **argv)
71 register int fd, nb;
72 EXEC head;
73 void (*sfcn)(const char *, int, EXEC *);
74 int ch;
75 char *fn;
77 sfcn = s_sym;
78 while ((ch = getopt(argc, argv, "dx")) != -1)
79 switch(ch) {
80 case 'x':
81 xflag = 1;
82 /*FALLTHROUGH*/
83 case 'd':
84 sfcn = s_stab;
85 break;
86 case '?':
87 default:
88 usage();
90 argc -= optind;
91 argv += optind;
93 while ((fn = *argv++) != NULL) {
94 if ((fd = open(fn, O_RDWR)) < 0 ||
95 (nb = read(fd, &head, sizeof(EXEC))) == -1) {
96 warn("%s", fn);
97 err_val = 1;
98 if (fd >= 0 && close(fd)) {
99 warn("%s", fn);
100 err_val = 1;
102 continue;
104 if (nb != sizeof(EXEC) || N_BADMAG(head)) {
105 warnx("%s: %s", fn, strerror(EFTYPE));
106 err_val = 1;
107 if (close(fd)) {
108 warn("%s", fn);
109 err_val = 1;
111 continue;
113 sfcn(fn, fd, &head);
114 if (close(fd)) {
115 warn("%s", fn);
116 err_val = 1;
119 exit(err_val);
122 void
123 s_sym(const char *fn, int fd, register EXEC *ep)
125 register off_t fsize;
127 /* If no symbols or data/text relocation info, quit. */
128 if (!ep->a_syms && !ep->a_trsize && !ep->a_drsize)
129 return;
132 * New file size is the header plus text and data segments.
134 fsize = N_DATOFF(*ep) + ep->a_data;
136 /* Set symbol size and relocation info values to 0. */
137 ep->a_syms = ep->a_trsize = ep->a_drsize = 0;
139 /* Rewrite the header and truncate the file. */
140 if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
141 write(fd, ep, sizeof(EXEC)) != sizeof(EXEC) ||
142 ftruncate(fd, fsize)) {
143 warn("%s", fn);
144 err_val = 1;
148 void
149 s_stab(const char *fn, int fd, EXEC *ep)
151 register int cnt, len;
152 register char *nstr, *nstrbase, *p, *strbase;
153 register NLIST *sym, *nsym;
154 struct stat sb;
155 NLIST *symbase;
157 /* Quit if no symbols. */
158 if (ep->a_syms == 0)
159 return;
161 /* Stat the file. */
162 if (fstat(fd, &sb) < 0) {
163 warn("%s", fn);
164 err_val = 1;
165 return;
168 /* Check size. */
169 if (sb.st_size > SIZE_T_MAX) {
170 warnx("%s: %s", fn, strerror(EFBIG));
171 err_val = 1;
172 return;
175 /* Map the file. */
176 if ((ep = (EXEC *)mmap(NULL, (size_t)sb.st_size,
177 PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t)0)) == (EXEC *)MAP_FAILED) {
178 warn("%s", fn);
179 err_val = 1;
180 return;
184 * Initialize old and new symbol pointers. They both point to the
185 * beginning of the symbol table in memory, since we're deleting
186 * entries.
188 sym = nsym = symbase = (NLIST *)((char *)ep + N_SYMOFF(*ep));
191 * Allocate space for the new string table, initialize old and
192 * new string pointers. Handle the extra long at the beginning
193 * of the string table.
195 strbase = (char *)ep + N_STROFF(*ep);
196 if ((nstrbase = malloc((size_t)*(u_long *)strbase)) == NULL) {
197 warn(NULL);
198 err_val = 1;
199 munmap((caddr_t)ep, sb.st_size);
200 return;
202 nstr = nstrbase + sizeof(u_long);
205 * Read through the symbol table. For each non-debugging symbol,
206 * copy it and save its string in the new string table. Keep
207 * track of the number of symbols.
209 for (cnt = ep->a_syms / sizeof(NLIST); cnt--; ++sym)
210 if (!(sym->n_type & N_STAB) && sym->strx) {
211 *nsym = *sym;
212 nsym->strx = nstr - nstrbase;
213 p = strbase + sym->strx;
214 if (xflag &&
215 (!(sym->n_type & N_EXT) ||
216 (sym->n_type & ~N_EXT) == N_FN ||
217 strcmp(p, "gcc_compiled.") == 0 ||
218 strcmp(p, "gcc2_compiled.") == 0 ||
219 strcmp(p, "___gnu_compiled_c") == 0))
220 continue;
221 len = strlen(p) + 1;
222 bcopy(p, nstr, len);
223 nstr += len;
224 ++nsym;
227 /* Fill in new symbol table size. */
228 ep->a_syms = (nsym - symbase) * sizeof(NLIST);
230 /* Fill in the new size of the string table. */
231 *(u_long *)nstrbase = len = nstr - nstrbase;
234 * Copy the new string table into place. Nsym should be pointing
235 * at the address past the last symbol entry.
237 bcopy(nstrbase, (void *)nsym, len);
239 /* Truncate to the current length. */
240 if (ftruncate(fd, (char *)nsym + len - (char *)ep)) {
241 warn("%s", fn);
242 err_val = 1;
244 munmap((caddr_t)ep, (size_t)sb.st_size);
247 static void
248 usage(void)
250 (void)fprintf(stderr, "usage: strip [-dx] file ...\n");
251 exit(1);