353128dc88dec0c8d77cb76d865ef4508b440994
[dragonfly.git] / usr.bin / strip / strip.c
1 /*
2  * Copyright (c) 1988, 1993
3  *      The Regents of the University of California.  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  * @(#) 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.3 2003/10/04 20:36:51 hmp Exp $
37  */
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/mman.h>
42
43 #include <a.out.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <limits.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 typedef struct exec EXEC;
54 typedef struct nlist NLIST;
55
56 #define strx    n_un.n_strx
57
58 void s_stab(const char *, int, EXEC *);
59 void s_sym(const char *, int, EXEC *);
60 static void usage(void);
61
62 int xflag = 0;
63 int err_val = 0;
64
65 int
66 main(int argc, 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, "dx")) != -1)
76                 switch(ch) {
77                 case 'x':
78                         xflag = 1;
79                         /*FALLTHROUGH*/
80                 case 'd':
81                         sfcn = s_stab;
82                         break;
83                 case '?':
84                 default:
85                         usage();
86                 }
87         argc -= optind;
88         argv += optind;
89
90         while ((fn = *argv++) != NULL) {
91                 if ((fd = open(fn, O_RDWR)) < 0 ||
92                     (nb = read(fd, &head, sizeof(EXEC))) == -1) {
93                         warn("%s", fn);
94                         err_val = 1;
95                         if (fd >= 0 && close(fd)) {
96                                 warn("%s", fn);
97                                 err_val = 1;
98                         }
99                         continue;
100                 }
101                 if (nb != sizeof(EXEC) || N_BADMAG(head)) {
102                         warnx("%s: %s", fn, strerror(EFTYPE));
103                         err_val = 1;
104                         if (close(fd)) {
105                                 warn("%s", fn);
106                                 err_val = 1;
107                         }
108                         continue;
109                 }
110                 sfcn(fn, fd, &head);
111                 if (close(fd)) {
112                         warn("%s", fn);
113                         err_val = 1;
114                 }
115         }
116         exit(err_val);
117 }
118
119 void
120 s_sym(const char *fn, int fd, register EXEC *ep)
121 {
122         register off_t fsize;
123
124         /* If no symbols or data/text relocation info, quit. */
125         if (!ep->a_syms && !ep->a_trsize && !ep->a_drsize)
126                 return;
127
128         /*
129          * New file size is the header plus text and data segments.
130          */
131         fsize = N_DATOFF(*ep) + ep->a_data;
132
133         /* Set symbol size and relocation info values to 0. */
134         ep->a_syms = ep->a_trsize = ep->a_drsize = 0;
135
136         /* Rewrite the header and truncate the file. */
137         if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
138             write(fd, ep, sizeof(EXEC)) != sizeof(EXEC) ||
139             ftruncate(fd, fsize)) {
140                 warn("%s", fn);
141                 err_val = 1;
142         }
143 }
144
145 void
146 s_stab(const char *fn, int fd, EXEC *ep)
147 {
148         register int cnt, len;
149         register char *nstr, *nstrbase, *p, *strbase;
150         register NLIST *sym, *nsym;
151         struct stat sb;
152         NLIST *symbase;
153
154         /* Quit if no symbols. */
155         if (ep->a_syms == 0)
156                 return;
157
158         /* Stat the file. */
159         if (fstat(fd, &sb) < 0) {
160                 warn("%s", fn);
161                 err_val = 1;
162                 return;
163         }
164
165         /* Check size. */
166         if (sb.st_size > SIZE_T_MAX) {
167                 warnx("%s: %s", fn, strerror(EFBIG));
168                 err_val = 1;
169                 return;
170         }
171
172         /* Map the file. */
173         if ((ep = (EXEC *)mmap(NULL, (size_t)sb.st_size,
174             PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t)0)) == (EXEC *)MAP_FAILED) {
175                 warn("%s", fn);
176                 err_val = 1;
177                 return;
178         }
179
180         /*
181          * Initialize old and new symbol pointers.  They both point to the
182          * beginning of the symbol table in memory, since we're deleting
183          * entries.
184          */
185         sym = nsym = symbase = (NLIST *)((char *)ep + N_SYMOFF(*ep));
186
187         /*
188          * Allocate space for the new string table, initialize old and
189          * new string pointers.  Handle the extra long at the beginning
190          * of the string table.
191          */
192         strbase = (char *)ep + N_STROFF(*ep);
193         if ((nstrbase = malloc((size_t)*(u_long *)strbase)) == NULL) {
194                 warn(NULL);
195                 err_val = 1;
196                 munmap((caddr_t)ep, sb.st_size);
197                 return;
198         }
199         nstr = nstrbase + sizeof(u_long);
200
201         /*
202          * Read through the symbol table.  For each non-debugging symbol,
203          * copy it and save its string in the new string table.  Keep
204          * track of the number of symbols.
205          */
206         for (cnt = ep->a_syms / sizeof(NLIST); cnt--; ++sym)
207                 if (!(sym->n_type & N_STAB) && sym->strx) {
208                         *nsym = *sym;
209                         nsym->strx = nstr - nstrbase;
210                         p = strbase + sym->strx;
211                         if (xflag &&
212                             (!(sym->n_type & N_EXT) ||
213                              (sym->n_type & ~N_EXT) == N_FN ||
214                              strcmp(p, "gcc_compiled.") == 0 ||
215                              strcmp(p, "gcc2_compiled.") == 0 ||
216                              strcmp(p, "___gnu_compiled_c") == 0))
217                                 continue;
218                         len = strlen(p) + 1;
219                         bcopy(p, nstr, len);
220                         nstr += len;
221                         ++nsym;
222                 }
223
224         /* Fill in new symbol table size. */
225         ep->a_syms = (nsym - symbase) * sizeof(NLIST);
226
227         /* Fill in the new size of the string table. */
228         *(u_long *)nstrbase = len = nstr - nstrbase;
229
230         /*
231          * Copy the new string table into place.  Nsym should be pointing
232          * at the address past the last symbol entry.
233          */
234         bcopy(nstrbase, (void *)nsym, len);
235
236         /* Truncate to the current length. */
237         if (ftruncate(fd, (char *)nsym + len - (char *)ep)) {
238                 warn("%s", fn);
239                 err_val = 1;
240         }
241         munmap((caddr_t)ep, (size_t)sb.st_size);
242 }
243
244 static void
245 usage(void)
246 {
247         (void)fprintf(stderr, "usage: strip [-dx] file ...\n");
248         exit(1);
249 }