[BACK]Return to strip.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / strip

Annotation of src/usr.bin/strip/strip.c, Revision 1.6

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