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

1.26    ! deraadt     1: /*     $OpenBSD: strip.c,v 1.25 2009/06/19 09:40:43 sobrado 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;
                     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;
                     82:        char *fn;
1.17      espie      83:        off_t newsize;
1.1       deraadt    84:
                     85:        sfcn = s_sym;
1.6       millert    86:        while ((ch = getopt(argc, argv, "dx")) != -1)
1.1       deraadt    87:                switch(ch) {
                     88:                 case 'x':
                     89:                         xflag = 1;
                     90:                         /*FALLTHROUGH*/
                     91:                case 'd':
                     92:                        sfcn = s_stab;
                     93:                        break;
                     94:                case '?':
                     95:                default:
                     96:                        usage();
                     97:                }
                     98:        argc -= optind;
                     99:        argv += optind;
                    100:
                    101:        errors = 0;
1.8       mickey    102: #define        ERROR(x) errors |= 1; warnx("%s: %s", fn, strerror(x)); continue;
1.9       deraadt   103:        while ((fn = *argv++)) {
1.1       deraadt   104:                if ((fd = open(fn, O_RDWR)) < 0) {
                    105:                        ERROR(errno);
                    106:                }
                    107:                if (fstat(fd, &sb)) {
                    108:                        (void)close(fd);
                    109:                        ERROR(errno);
                    110:                }
                    111:                if (sb.st_size < sizeof(EXEC)) {
                    112:                        (void)close(fd);
                    113:                        ERROR(EFTYPE);
                    114:                }
                    115:                if ((ep = (EXEC *)mmap(NULL, sb.st_size, PROT_READ|PROT_WRITE,
1.13      art       116:                    MAP_SHARED, fd, (off_t)0)) == MAP_FAILED) {
1.1       deraadt   117:                        (void)close(fd);
                    118:                        ERROR(errno);
                    119:                }
1.11      espie     120:                if (BAD_OBJECT(*ep)) {
1.1       deraadt   121:                        munmap((caddr_t)ep, sb.st_size);
                    122:                        (void)close(fd);
                    123:                        ERROR(EFTYPE);
                    124:                }
1.25      sobrado   125:                /*
                    126:                 * Since we're dealing with an mmap there, we have to convert
                    127:                 * once for dealing with data in memory, and a second time
                    128:                 * for out.
1.11      espie     129:                 */
                    130:                fix_header_order(ep);
1.17      espie     131:                newsize = 0;
                    132:                errors |= sfcn(fn, fd, ep, &sb, &newsize);
1.11      espie     133:                fix_header_order(ep);
1.1       deraadt   134:                munmap((caddr_t)ep, sb.st_size);
1.17      espie     135:                if (newsize  && ftruncate(fd, newsize)) {
                    136:                        warn("%s", fn);
                    137:                        errors = 1;
                    138:                }
1.1       deraadt   139:                if (close(fd)) {
                    140:                        ERROR(errno);
                    141:                }
                    142:        }
                    143: #undef ERROR
                    144:        exit(errors);
                    145: }
                    146:
                    147: int
1.20      deraadt   148: s_sym(const char *fn, int fd, EXEC *ep, struct stat *sp, off_t *sz)
1.1       deraadt   149: {
1.15      mpech     150:        char *neweof;
1.14      pvalchev  151: #if    0
1.15      mpech     152:        char *mineof;
1.14      pvalchev  153: #endif
1.1       deraadt   154:        int zmagic;
                    155:
                    156:        zmagic = ep->a_data &&
                    157:                 (N_GETMAGIC(*ep) == ZMAGIC || N_GETMAGIC(*ep) == QMAGIC);
                    158:
                    159:        /*
                    160:         * If no symbols or data/text relocation info and
                    161:         * the file data segment size is already minimized, quit.
                    162:         */
                    163:        if (!ep->a_syms && !ep->a_trsize && !ep->a_drsize) {
                    164: #if 0
                    165:                if (!zmagic)
                    166:                        return 0;
                    167:                if (sp->st_size < N_TRELOFF(*ep))
                    168: #endif
                    169:                        return 0;
                    170:        }
                    171:
                    172:        /*
                    173:         * New file size is the header plus text and data segments; OMAGIC
                    174:         * and NMAGIC formats have the text/data immediately following the
                    175:         * header.  ZMAGIC format wastes the rest of of header page.
                    176:         */
                    177:        neweof = (char *)ep + N_TRELOFF(*ep);
                    178:
                    179: #if 0
                    180:        /*
                    181:         * Unfortunately, this can't work correctly without changing the way
                    182:         * the loader works.  We could cap it at one page, or even fiddle with
                    183:         * a_data and a_bss, but this only works for CLBYTES == NBPG.  If we
                    184:         * are on a system where, e.g., CLBYTES is 8k and NBPG is 4k, and we
                    185:         * happen to remove 4.5k, we will lose.  And we really don't want to
                    186:         * fiddle with pages, because that breaks binary compatibility.  Lose.
                    187:         */
                    188:
                    189:        if (zmagic) {
                    190:                /*
                    191:                 * Get rid of unneeded zeroes at the end of the data segment
                    192:                 * to reduce the file size even more.
                    193:                 */
                    194:                mineof = (char *)ep + N_DATOFF(*ep);
                    195:                while (neweof > mineof && neweof[-1] == '\0')
                    196:                        neweof--;
                    197:        }
                    198: #endif
                    199:
                    200:        /* Set symbol size and relocation info values to 0. */
                    201:        ep->a_syms = ep->a_trsize = ep->a_drsize = 0;
                    202:
                    203:        /* Truncate the file. */
1.17      espie     204:        *sz = neweof - (char *)ep;
1.1       deraadt   205:
                    206:        return 0;
                    207: }
                    208:
                    209: int
1.20      deraadt   210: s_stab(const char *fn, int fd, EXEC *ep, struct stat *sp, off_t *sz)
1.1       deraadt   211: {
1.15      mpech     212:        int cnt, len;
1.18      espie     213:        char *nstr, *nstrbase=0, *used=0, *p, *strbase;
1.15      mpech     214:        NLIST *sym, *nsym;
1.11      espie     215:        u_long allocsize;
                    216:        int mid;
1.1       deraadt   217:        NLIST *symbase;
1.18      espie     218:        unsigned int *mapping=0;
                    219:        int error=1;
                    220:        unsigned int nsyms;
                    221:        struct relocation_info *reloc_base;
                    222:        unsigned int i, j;
1.1       deraadt   223:
                    224:        /* Quit if no symbols. */
                    225:        if (ep->a_syms == 0)
                    226:                return 0;
                    227:
                    228:        if (N_SYMOFF(*ep) >= sp->st_size) {
1.7       mickey    229:                warnx("%s: bad symbol table", fn);
1.1       deraadt   230:                return 1;
                    231:        }
                    232:
1.11      espie     233:        mid = N_GETMID(*ep);
                    234:
1.1       deraadt   235:        /*
                    236:         * Initialize old and new symbol pointers.  They both point to the
                    237:         * beginning of the symbol table in memory, since we're deleting
                    238:         * entries.
                    239:         */
                    240:        sym = nsym = symbase = (NLIST *)((char *)ep + N_SYMOFF(*ep));
                    241:
                    242:        /*
                    243:         * Allocate space for the new string table, initialize old and
                    244:         * new string pointers.  Handle the extra long at the beginning
                    245:         * of the string table.
                    246:         */
                    247:        strbase = (char *)ep + N_STROFF(*ep);
1.23      mickey    248:        allocsize = fix_32_order(*(u_long *)strbase, mid);
1.11      espie     249:        if ((nstrbase = malloc((u_int) allocsize)) == NULL) {
1.7       mickey    250:                warnx("%s", strerror(ENOMEM));
1.18      espie     251:                goto end;
1.1       deraadt   252:        }
                    253:        nstr = nstrbase + sizeof(u_long);
                    254:
1.18      espie     255:        /* okay, so we also need to keep symbol numbers for relocations. */
                    256:        nsyms = ep->a_syms/ sizeof(NLIST);
                    257:        used = calloc(nsyms, 1);
                    258:        if (!used) {
                    259:                warnx("%s", strerror(ENOMEM));
                    260:                goto end;
                    261:        }
1.24      deraadt   262:        mapping = calloc(nsyms, sizeof(unsigned int));
1.18      espie     263:        if (!mapping) {
                    264:                warnx("%s", strerror(ENOMEM));
                    265:                goto end;
                    266:        }
                    267:
                    268:        if ((ep->a_trsize || ep->a_drsize) && byte_sex(mid) != BYTE_ORDER) {
                    269:                warnx("%s: cross-stripping not supported", fn);
                    270:                goto end;
                    271:        }
                    272:
                    273:        /* first check the relocations for used symbols, and mark them */
                    274:        /* text */
                    275:        reloc_base = (struct relocation_info *) ((char *)ep + N_TRELOFF(*ep));
                    276:        if (N_TRELOFF(*ep) + ep->a_trsize > sp->st_size) {
                    277:                warnx("%s: bad text relocation", fn);
                    278:                goto end;
                    279:        }
                    280:        for (i = 0; i < ep->a_trsize / sizeof(struct relocation_info); i++) {
                    281:                if (!reloc_base[i].r_extern)
                    282:                        continue;
                    283:                if (reloc_base[i].r_symbolnum > nsyms) {
                    284:                        warnx("%s: bad symbol number in text relocation", fn);
                    285:                        goto end;
                    286:                }
                    287:                used[reloc_base[i].r_symbolnum] = 1;
                    288:        }
                    289:        /* data */
                    290:        reloc_base = (struct relocation_info *) ((char *)ep + N_DRELOFF(*ep));
                    291:        if (N_DRELOFF(*ep) + ep->a_drsize > sp->st_size) {
                    292:                warnx("%s: bad data relocation", fn);
                    293:                goto end;
                    294:        }
                    295:        for (i = 0; i < ep->a_drsize / sizeof(struct relocation_info); i++) {
                    296:                if (!reloc_base[i].r_extern)
                    297:                        continue;
                    298:                if (reloc_base[i].r_symbolnum > nsyms) {
                    299:                        warnx("%s: bad symbol number in data relocation", fn);
                    300:                        goto end;
                    301:                }
                    302:                used[reloc_base[i].r_symbolnum] = 1;
                    303:        }
                    304:
1.1       deraadt   305:        /*
                    306:         * Read through the symbol table.  For each non-debugging symbol,
                    307:         * copy it and save its string in the new string table.  Keep
                    308:         * track of the number of symbols.
                    309:         */
1.18      espie     310:        for (cnt = nsyms, i = 0, j = 0; cnt--; ++sym, ++i) {
1.11      espie     311:                fix_nlist_order(sym, mid);
1.1       deraadt   312:                if (!(sym->n_type & N_STAB) && sym->strx) {
                    313:                        *nsym = *sym;
                    314:                        nsym->strx = nstr - nstrbase;
                    315:                        p = strbase + sym->strx;
1.18      espie     316:                         if (xflag && !used[i] &&
1.1       deraadt   317:                             (!(sym->n_type & N_EXT) ||
                    318:                              (sym->n_type & ~N_EXT) == N_FN ||
                    319:                              strcmp(p, "gcc_compiled.") == 0 ||
                    320:                              strcmp(p, "gcc2_compiled.") == 0 ||
                    321:                              strncmp(p, "___gnu_compiled_", 16) == 0)) {
                    322:                                 continue;
                    323:                         }
                    324:                        len = strlen(p) + 1;
1.18      espie     325:                        mapping[i] = j++;
1.17      espie     326:                        if (N_STROFF(*ep) + sym->strx + len > sp->st_size) {
                    327:                                warnx("%s: bad symbol table", fn);
1.18      espie     328:                                goto end;
1.17      espie     329:                        }
1.1       deraadt   330:                        bcopy(p, nstr, len);
                    331:                        nstr += len;
1.11      espie     332:                        fix_nlist_order(nsym++, mid);
1.1       deraadt   333:                }
1.11      espie     334:        }
1.1       deraadt   335:
1.18      espie     336:        /* renumber symbol relocations */
                    337:        /* text */
                    338:        reloc_base = (struct relocation_info *) ((char *)ep + N_TRELOFF(*ep));
                    339:        for (i = 0; i < ep->a_trsize / sizeof(struct relocation_info); i++) {
                    340:                if (!reloc_base[i].r_extern)
                    341:                        continue;
                    342:                reloc_base[i].r_symbolnum = mapping[reloc_base[i].r_symbolnum];
                    343:        }
                    344:        /* data */
                    345:        reloc_base = (struct relocation_info *) ((char *)ep + N_DRELOFF(*ep));
                    346:        for (i = 0; i < ep->a_drsize / sizeof(struct relocation_info); i++) {
                    347:                if (!reloc_base[i].r_extern)
                    348:                        continue;
                    349:                reloc_base[i].r_symbolnum = mapping[reloc_base[i].r_symbolnum];
                    350:        }
                    351:
1.1       deraadt   352:        /* Fill in new symbol table size. */
                    353:        ep->a_syms = (nsym - symbase) * sizeof(NLIST);
                    354:
                    355:        /* Fill in the new size of the string table. */
1.11      espie     356:        len = nstr - nstrbase;
1.23      mickey    357:        *(u_long *)nstrbase = fix_32_order(len, mid);
1.1       deraadt   358:
                    359:        /*
                    360:         * Copy the new string table into place.  Nsym should be pointing
                    361:         * at the address past the last symbol entry.
                    362:         */
                    363:        bcopy(nstrbase, (void *)nsym, len);
1.18      espie     364:        error = 0;
                    365: end:
1.1       deraadt   366:        free(nstrbase);
1.18      espie     367:        free(used);
                    368:        free(mapping);
1.1       deraadt   369:
                    370:        /* Truncate to the current length. */
1.17      espie     371:        *sz = (char *)nsym + len - (char *)ep;
1.1       deraadt   372:
1.18      espie     373:        return error;
1.1       deraadt   374: }
                    375:
                    376: void
1.20      deraadt   377: usage(void)
1.1       deraadt   378: {
1.21      mickey    379:        extern char *__progname;
                    380:
                    381:        fprintf(stderr, "usage: %s [-dx] file ...\n", __progname);
1.1       deraadt   382:        exit(1);
                    383: }
                    384: