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

1.30    ! deraadt     1: /*     $OpenBSD: strip.c,v 1.29 2010/06/01 21:44:39 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.
1.19      millert    15:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
1.4       deraadt    32: #include <sys/param.h>
1.1       deraadt    33: #include <sys/types.h>
                     34: #include <sys/stat.h>
                     35: #include <sys/mman.h>
                     36: #include <fcntl.h>
                     37: #include <errno.h>
                     38: #include <a.out.h>
                     39: #include <unistd.h>
                     40: #include <stdio.h>
                     41: #include <stdlib.h>
                     42: #include <string.h>
1.7       mickey     43: #include <err.h>
1.11      espie      44: #include <ranlib.h>
                     45: #include "byte.c"
1.10      niklas     46:
                     47: #ifdef MID_MACHINE_OVERRIDE
                     48: #undef MID_MACHINE
                     49: #define MID_MACHINE MID_MACHINE_OVERRIDE
1.21      mickey     50: #if MID_MACHINE_OVERRIDE == MID_M68K
                     51: #undef __LDPGSZ
                     52: #undef ELF_TARG_DATA
                     53: #undef ELF_TARG_MACH
                     54: #include "m68k/exec.h"
1.22      miod       55: #elif MID_MACHINE_OVERRIDE == MID_M88K
                     56: #undef __LDPGSZ
                     57: #undef ELF_TARG_DATA
                     58: #undef ELF_TARG_MACH
                     59: #include "m88k/exec.h"
1.21      mickey     60: #endif
1.10      niklas     61: #endif
1.1       deraadt    62:
                     63: typedef struct exec EXEC;
                     64: typedef struct nlist NLIST;
                     65:
                     66: #define        strx    n_un.n_strx
                     67:
1.17      espie      68: int s_stab(const char *, int, EXEC *, struct stat *, off_t *);
                     69: int s_sym(const char *, int, EXEC *, struct stat *, off_t *);
1.16      millert    70: void usage(void);
1.1       deraadt    71:
                     72: int xflag = 0;
1.27      deraadt    73:
1.9       deraadt    74: int
1.20      deraadt    75: main(int argc, char *argv[])
1.1       deraadt    76: {
1.15      mpech      77:        int fd;
1.1       deraadt    78:        EXEC *ep;
                     79:        struct stat sb;
1.17      espie      80:        int (*sfcn)(const char *, int, EXEC *, struct stat *, off_t *);
1.1       deraadt    81:        int ch, errors;
1.27      deraadt    82:        char *fn, *ofile = NULL;
1.17      espie      83:        off_t newsize;
1.1       deraadt    84:
                     85:        sfcn = s_sym;
1.29      deraadt    86:        while ((ch = getopt(argc, argv, "dgsxo:")) != -1)
1.1       deraadt    87:                switch(ch) {
1.27      deraadt    88:                case 'x':
                     89:                        xflag = 1;
                     90:                        /*FALLTHROUGH*/
1.29      deraadt    91:                case 'g':
1.1       deraadt    92:                case 'd':
                     93:                        sfcn = s_stab;
                     94:                        break;
1.29      deraadt    95:                case 's':
                     96:                        /* reset back to the defaults */
                     97:                        xflag = 0;
                     98:                        sfcn = s_sym;
                     99:                        break;
1.27      deraadt   100:                case 'o':
                    101:                        ofile = optarg;
                    102:                        break;
1.1       deraadt   103:                case '?':
                    104:                default:
                    105:                        usage();
                    106:                }
                    107:        argc -= optind;
                    108:        argv += optind;
                    109:
1.27      deraadt   110:        if (ofile != NULL && argc > 1)
                    111:                usage();
1.1       deraadt   112:        errors = 0;
1.8       mickey    113: #define        ERROR(x) errors |= 1; warnx("%s: %s", fn, strerror(x)); continue;
1.9       deraadt   114:        while ((fn = *argv++)) {
1.27      deraadt   115:                if (ofile) {
                    116:                        char buf[8192];
                    117:                        ssize_t wn;
                    118:                        size_t rn;
                    119:                        off_t off;
                    120:                        int wfd;
                    121:
                    122:                        if ((fd = open(fn, O_RDONLY)) < 0) {
                    123:                                ERROR(errno);
                    124:                                break;
                    125:                        }
                    126:                        if ((wfd = open(ofile, O_RDWR|O_CREAT)) < 0) {
                    127:                                ERROR(errno);
                    128:                                break;
                    129:                        }
                    130:                        do {
                    131:                                rn = read(fd, buf, sizeof buf);
                    132:                                if (rn == (ssize_t)-1) {
                    133:                                        int save_errno = errno;
                    134:
                    135:                                        unlink(ofile);
                    136:                                        ERROR(save_errno);
                    137:                                        exit(errors);
                    138:                                }
                    139:                                if (rn == 0)
                    140:                                        break;
                    141:
                    142:                                off = 0;
                    143:                                while (rn - off > 0) {
                    144:                                        wn = write(wfd, buf + off, rn - off);
                    145:                                        if (wn == (ssize_t)-1) {
                    146:                                                int save_errno = errno;
                    147:
                    148:                                                unlink(ofile);
                    149:                                                ERROR(save_errno);
                    150:                                                exit(errors);
                    151:                                        }
                    152:                                        off += wn;
                    153:                                }
                    154:                        } while (rn > 0);
                    155:
                    156:                        fn = ofile;
                    157:                        close(fd);
                    158:                        fd = wfd;
                    159:                } else if ((fd = open(fn, O_RDWR)) < 0) {
1.1       deraadt   160:                        ERROR(errno);
                    161:                }
1.27      deraadt   162:                if (fstat(fd, &sb)) {
1.1       deraadt   163:                        (void)close(fd);
                    164:                        ERROR(errno);
                    165:                }
                    166:                if (sb.st_size < sizeof(EXEC)) {
                    167:                        (void)close(fd);
                    168:                        ERROR(EFTYPE);
                    169:                }
                    170:                if ((ep = (EXEC *)mmap(NULL, sb.st_size, PROT_READ|PROT_WRITE,
1.13      art       171:                    MAP_SHARED, fd, (off_t)0)) == MAP_FAILED) {
1.1       deraadt   172:                        (void)close(fd);
                    173:                        ERROR(errno);
                    174:                }
1.11      espie     175:                if (BAD_OBJECT(*ep)) {
1.1       deraadt   176:                        munmap((caddr_t)ep, sb.st_size);
                    177:                        (void)close(fd);
                    178:                        ERROR(EFTYPE);
                    179:                }
1.25      sobrado   180:                /*
                    181:                 * Since we're dealing with an mmap there, we have to convert
                    182:                 * once for dealing with data in memory, and a second time
                    183:                 * for out.
1.11      espie     184:                 */
                    185:                fix_header_order(ep);
1.17      espie     186:                newsize = 0;
                    187:                errors |= sfcn(fn, fd, ep, &sb, &newsize);
1.11      espie     188:                fix_header_order(ep);
1.1       deraadt   189:                munmap((caddr_t)ep, sb.st_size);
1.17      espie     190:                if (newsize  && ftruncate(fd, newsize)) {
                    191:                        warn("%s", fn);
                    192:                        errors = 1;
                    193:                }
1.1       deraadt   194:                if (close(fd)) {
                    195:                        ERROR(errno);
                    196:                }
                    197:        }
                    198: #undef ERROR
                    199:        exit(errors);
                    200: }
                    201:
                    202: int
1.20      deraadt   203: s_sym(const char *fn, int fd, EXEC *ep, struct stat *sp, off_t *sz)
1.1       deraadt   204: {
1.15      mpech     205:        char *neweof;
1.14      pvalchev  206: #if    0
1.15      mpech     207:        char *mineof;
1.14      pvalchev  208: #endif
1.1       deraadt   209:        int zmagic;
                    210:
                    211:        zmagic = ep->a_data &&
                    212:                 (N_GETMAGIC(*ep) == ZMAGIC || N_GETMAGIC(*ep) == QMAGIC);
                    213:
                    214:        /*
                    215:         * If no symbols or data/text relocation info and
                    216:         * the file data segment size is already minimized, quit.
                    217:         */
                    218:        if (!ep->a_syms && !ep->a_trsize && !ep->a_drsize) {
                    219: #if 0
                    220:                if (!zmagic)
                    221:                        return 0;
                    222:                if (sp->st_size < N_TRELOFF(*ep))
                    223: #endif
                    224:                        return 0;
                    225:        }
                    226:
                    227:        /*
                    228:         * New file size is the header plus text and data segments; OMAGIC
                    229:         * and NMAGIC formats have the text/data immediately following the
                    230:         * header.  ZMAGIC format wastes the rest of of header page.
                    231:         */
                    232:        neweof = (char *)ep + N_TRELOFF(*ep);
                    233:
                    234: #if 0
                    235:        /*
                    236:         * Unfortunately, this can't work correctly without changing the way
                    237:         * the loader works.  We could cap it at one page, or even fiddle with
1.30    ! deraadt   238:         * a_data and a_bss, but this only works for CLBYTES == PAGE_SIZE.  If
        !           239:         * we are on a system where, e.g., CLBYTES is 8k and PAGE_SIZE is 4k,
        !           240:         * and we happen to remove 4.5k, we will lose.  And we really don't want
        !           241:         * to fiddle with pages, because that breaks binary compatibility.  Lose.
1.1       deraadt   242:         */
                    243:
                    244:        if (zmagic) {
                    245:                /*
                    246:                 * Get rid of unneeded zeroes at the end of the data segment
                    247:                 * to reduce the file size even more.
                    248:                 */
                    249:                mineof = (char *)ep + N_DATOFF(*ep);
                    250:                while (neweof > mineof && neweof[-1] == '\0')
                    251:                        neweof--;
                    252:        }
                    253: #endif
                    254:
                    255:        /* Set symbol size and relocation info values to 0. */
                    256:        ep->a_syms = ep->a_trsize = ep->a_drsize = 0;
                    257:
                    258:        /* Truncate the file. */
1.17      espie     259:        *sz = neweof - (char *)ep;
1.1       deraadt   260:
                    261:        return 0;
                    262: }
                    263:
                    264: int
1.20      deraadt   265: s_stab(const char *fn, int fd, EXEC *ep, struct stat *sp, off_t *sz)
1.1       deraadt   266: {
1.15      mpech     267:        int cnt, len;
1.18      espie     268:        char *nstr, *nstrbase=0, *used=0, *p, *strbase;
1.15      mpech     269:        NLIST *sym, *nsym;
1.11      espie     270:        u_long allocsize;
                    271:        int mid;
1.1       deraadt   272:        NLIST *symbase;
1.18      espie     273:        unsigned int *mapping=0;
                    274:        int error=1;
                    275:        unsigned int nsyms;
                    276:        struct relocation_info *reloc_base;
                    277:        unsigned int i, j;
1.1       deraadt   278:
                    279:        /* Quit if no symbols. */
                    280:        if (ep->a_syms == 0)
                    281:                return 0;
                    282:
                    283:        if (N_SYMOFF(*ep) >= sp->st_size) {
1.7       mickey    284:                warnx("%s: bad symbol table", fn);
1.1       deraadt   285:                return 1;
                    286:        }
                    287:
1.11      espie     288:        mid = N_GETMID(*ep);
                    289:
1.1       deraadt   290:        /*
                    291:         * Initialize old and new symbol pointers.  They both point to the
                    292:         * beginning of the symbol table in memory, since we're deleting
                    293:         * entries.
                    294:         */
                    295:        sym = nsym = symbase = (NLIST *)((char *)ep + N_SYMOFF(*ep));
                    296:
                    297:        /*
                    298:         * Allocate space for the new string table, initialize old and
                    299:         * new string pointers.  Handle the extra long at the beginning
                    300:         * of the string table.
                    301:         */
                    302:        strbase = (char *)ep + N_STROFF(*ep);
1.23      mickey    303:        allocsize = fix_32_order(*(u_long *)strbase, mid);
1.11      espie     304:        if ((nstrbase = malloc((u_int) allocsize)) == NULL) {
1.7       mickey    305:                warnx("%s", strerror(ENOMEM));
1.18      espie     306:                goto end;
1.1       deraadt   307:        }
                    308:        nstr = nstrbase + sizeof(u_long);
                    309:
1.18      espie     310:        /* okay, so we also need to keep symbol numbers for relocations. */
                    311:        nsyms = ep->a_syms/ sizeof(NLIST);
                    312:        used = calloc(nsyms, 1);
                    313:        if (!used) {
                    314:                warnx("%s", strerror(ENOMEM));
                    315:                goto end;
                    316:        }
1.24      deraadt   317:        mapping = calloc(nsyms, sizeof(unsigned int));
1.18      espie     318:        if (!mapping) {
                    319:                warnx("%s", strerror(ENOMEM));
                    320:                goto end;
                    321:        }
                    322:
                    323:        if ((ep->a_trsize || ep->a_drsize) && byte_sex(mid) != BYTE_ORDER) {
                    324:                warnx("%s: cross-stripping not supported", fn);
                    325:                goto end;
                    326:        }
                    327:
                    328:        /* first check the relocations for used symbols, and mark them */
                    329:        /* text */
                    330:        reloc_base = (struct relocation_info *) ((char *)ep + N_TRELOFF(*ep));
                    331:        if (N_TRELOFF(*ep) + ep->a_trsize > sp->st_size) {
                    332:                warnx("%s: bad text relocation", fn);
                    333:                goto end;
                    334:        }
                    335:        for (i = 0; i < ep->a_trsize / sizeof(struct relocation_info); i++) {
                    336:                if (!reloc_base[i].r_extern)
                    337:                        continue;
                    338:                if (reloc_base[i].r_symbolnum > nsyms) {
                    339:                        warnx("%s: bad symbol number in text relocation", fn);
                    340:                        goto end;
                    341:                }
                    342:                used[reloc_base[i].r_symbolnum] = 1;
                    343:        }
                    344:        /* data */
                    345:        reloc_base = (struct relocation_info *) ((char *)ep + N_DRELOFF(*ep));
                    346:        if (N_DRELOFF(*ep) + ep->a_drsize > sp->st_size) {
                    347:                warnx("%s: bad data relocation", fn);
                    348:                goto end;
                    349:        }
                    350:        for (i = 0; i < ep->a_drsize / sizeof(struct relocation_info); i++) {
                    351:                if (!reloc_base[i].r_extern)
                    352:                        continue;
                    353:                if (reloc_base[i].r_symbolnum > nsyms) {
                    354:                        warnx("%s: bad symbol number in data relocation", fn);
                    355:                        goto end;
                    356:                }
                    357:                used[reloc_base[i].r_symbolnum] = 1;
                    358:        }
                    359:
1.1       deraadt   360:        /*
                    361:         * Read through the symbol table.  For each non-debugging symbol,
                    362:         * copy it and save its string in the new string table.  Keep
                    363:         * track of the number of symbols.
                    364:         */
1.18      espie     365:        for (cnt = nsyms, i = 0, j = 0; cnt--; ++sym, ++i) {
1.11      espie     366:                fix_nlist_order(sym, mid);
1.1       deraadt   367:                if (!(sym->n_type & N_STAB) && sym->strx) {
                    368:                        *nsym = *sym;
                    369:                        nsym->strx = nstr - nstrbase;
                    370:                        p = strbase + sym->strx;
1.27      deraadt   371:                        if (xflag && !used[i] &&
                    372:                            (!(sym->n_type & N_EXT) ||
                    373:                             (sym->n_type & ~N_EXT) == N_FN ||
                    374:                             strcmp(p, "gcc_compiled.") == 0 ||
                    375:                             strcmp(p, "gcc2_compiled.") == 0 ||
                    376:                             strncmp(p, "___gnu_compiled_", 16) == 0)) {
                    377:                                continue;
                    378:                        }
1.1       deraadt   379:                        len = strlen(p) + 1;
1.18      espie     380:                        mapping[i] = j++;
1.17      espie     381:                        if (N_STROFF(*ep) + sym->strx + len > sp->st_size) {
                    382:                                warnx("%s: bad symbol table", fn);
1.18      espie     383:                                goto end;
1.17      espie     384:                        }
1.1       deraadt   385:                        bcopy(p, nstr, len);
                    386:                        nstr += len;
1.11      espie     387:                        fix_nlist_order(nsym++, mid);
1.1       deraadt   388:                }
1.11      espie     389:        }
1.1       deraadt   390:
1.18      espie     391:        /* renumber symbol relocations */
                    392:        /* text */
                    393:        reloc_base = (struct relocation_info *) ((char *)ep + N_TRELOFF(*ep));
                    394:        for (i = 0; i < ep->a_trsize / sizeof(struct relocation_info); i++) {
                    395:                if (!reloc_base[i].r_extern)
                    396:                        continue;
                    397:                reloc_base[i].r_symbolnum = mapping[reloc_base[i].r_symbolnum];
                    398:        }
                    399:        /* data */
                    400:        reloc_base = (struct relocation_info *) ((char *)ep + N_DRELOFF(*ep));
                    401:        for (i = 0; i < ep->a_drsize / sizeof(struct relocation_info); i++) {
                    402:                if (!reloc_base[i].r_extern)
                    403:                        continue;
                    404:                reloc_base[i].r_symbolnum = mapping[reloc_base[i].r_symbolnum];
                    405:        }
                    406:
1.1       deraadt   407:        /* Fill in new symbol table size. */
                    408:        ep->a_syms = (nsym - symbase) * sizeof(NLIST);
                    409:
                    410:        /* Fill in the new size of the string table. */
1.11      espie     411:        len = nstr - nstrbase;
1.23      mickey    412:        *(u_long *)nstrbase = fix_32_order(len, mid);
1.1       deraadt   413:
                    414:        /*
                    415:         * Copy the new string table into place.  Nsym should be pointing
                    416:         * at the address past the last symbol entry.
                    417:         */
                    418:        bcopy(nstrbase, (void *)nsym, len);
1.18      espie     419:        error = 0;
                    420: end:
1.1       deraadt   421:        free(nstrbase);
1.18      espie     422:        free(used);
                    423:        free(mapping);
1.1       deraadt   424:
                    425:        /* Truncate to the current length. */
1.17      espie     426:        *sz = (char *)nsym + len - (char *)ep;
1.1       deraadt   427:
1.18      espie     428:        return error;
1.1       deraadt   429: }
                    430:
                    431: void
1.20      deraadt   432: usage(void)
1.1       deraadt   433: {
1.21      mickey    434:        extern char *__progname;
                    435:
1.29      deraadt   436:        fprintf(stderr, "usage: %s [-dgsx] [-o outfile] file ...\n",
                    437:            __progname);
1.1       deraadt   438:        exit(1);
                    439: }
                    440: