[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.10

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