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

Annotation of src/usr.bin/size/size.c, Revision 1.4

1.4     ! deraadt     1: /*     $OpenBSD: size.c,v 1.3 1996/06/26 05:39:18 deraadt Exp $        */
1.2       deraadt     2: /*     $NetBSD: size.c,v 1.7 1996/01/14 23:07:12 pk Exp $      */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1988, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: static char copyright[] =
                     39: "@(#) Copyright (c) 1988, 1993\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
                     45: static char sccsid[] = "@(#)size.c     8.2 (Berkeley) 12/9/93";
                     46: #endif
1.4     ! deraadt    47: static char rcsid[] = "$OpenBSD: size.c,v 1.3 1996/06/26 05:39:18 deraadt Exp $";
1.1       deraadt    48: #endif /* not lint */
                     49:
                     50: #include <sys/param.h>
                     51: #include <sys/file.h>
                     52: #include <a.out.h>
1.2       deraadt    53: #include <ar.h>
                     54: #include <ranlib.h>
1.1       deraadt    55: #include <unistd.h>
                     56: #include <stdlib.h>
                     57: #include <stdio.h>
                     58: #include <err.h>
                     59:
1.2       deraadt    60: int ignore_bad_archive_entries = 1;
                     61:
                     62: int    process_file __P((int, char *));
                     63: int    show_archive __P((int, char *, FILE *));
                     64: int    show_object __P((int, char *, FILE *));
                     65: void   *emalloc __P((size_t));
                     66: void   *erealloc __P((void *, size_t));
1.1       deraadt    67: void   usage __P((void));
                     68:
                     69: int
                     70: main(argc, argv)
                     71:        int argc;
                     72:        char *argv[];
                     73: {
                     74:        int ch, eval;
                     75:
1.2       deraadt    76:        while ((ch = getopt(argc, argv, "w")) != EOF)
1.1       deraadt    77:                switch(ch) {
1.2       deraadt    78:                case 'w':
                     79:                        ignore_bad_archive_entries = 0;
                     80:                        break;
1.1       deraadt    81:                case '?':
                     82:                default:
                     83:                        usage();
                     84:                }
                     85:        argc -= optind;
                     86:        argv += optind;
                     87:
                     88:        eval = 0;
                     89:        if (*argv)
                     90:                do {
1.2       deraadt    91:                        eval |= process_file(argc, *argv);
1.1       deraadt    92:                } while (*++argv);
                     93:        else
1.2       deraadt    94:                eval |= process_file(1, "a.out");
1.1       deraadt    95:        exit(eval);
                     96: }
                     97:
1.2       deraadt    98: /*
                     99:  * process_file()
                    100:  *     show symbols in the file given as an argument.  Accepts archive and
                    101:  *     object files as input.
                    102:  */
                    103: int
                    104: process_file(count, fname)
                    105:        int count;
                    106:        char *fname;
                    107: {
                    108:        struct exec exec_head;
                    109:        FILE *fp;
                    110:        int retval;
                    111:        char magic[SARMAG];
                    112:
                    113:        if (!(fp = fopen(fname, "r"))) {
                    114:                warnx("cannot read %s", fname);
                    115:                return(1);
                    116:        }
                    117:
                    118:        /*
                    119:         * first check whether this is an object file - read a object
                    120:         * header, and skip back to the beginning
                    121:         */
                    122:        if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
                    123:                warnx("%s: bad format", fname);
                    124:                (void)fclose(fp);
                    125:                return(1);
                    126:        }
                    127:        rewind(fp);
                    128:
                    129:        /* this could be an archive */
                    130:        if (N_BADMAG(exec_head)) {
                    131:                if (fread(magic, sizeof(magic), (size_t)1, fp) != 1 ||
                    132:                    strncmp(magic, ARMAG, SARMAG)) {
                    133:                        warnx("%s: not object file or archive", fname);
                    134:                        (void)fclose(fp);
                    135:                        return(1);
                    136:                }
                    137:                retval = show_archive(count, fname, fp);
                    138:        } else
                    139:                retval = show_objfile(count, fname, fp);
                    140:        (void)fclose(fp);
                    141:        return(retval);
                    142: }
                    143:
                    144: /*
                    145:  * show_archive()
                    146:  *     show symbols in the given archive file
                    147:  */
1.1       deraadt   148: int
1.2       deraadt   149: show_archive(count, fname, fp)
                    150:        int count;
                    151:        char *fname;
                    152:        FILE *fp;
                    153: {
                    154:        struct ar_hdr ar_head;
                    155:        struct exec exec_head;
                    156:        int i, rval;
                    157:        long last_ar_off;
                    158:        char *p, *name;
                    159:        int baselen, namelen;
                    160:
                    161:        baselen = strlen(fname) + 3;
                    162:        namelen = sizeof(ar_head.ar_name);
                    163:        name = emalloc(baselen + namelen);
                    164:
                    165:        rval = 0;
                    166:
                    167:        /* while there are more entries in the archive */
                    168:        while (fread((char *)&ar_head, sizeof(ar_head), (size_t)1, fp) == 1) {
                    169:                /* bad archive entry - stop processing this archive */
                    170:                if (strncmp(ar_head.ar_fmag, ARFMAG, sizeof(ar_head.ar_fmag))) {
                    171:                        warnx("%s: bad format archive header", fname);
                    172:                        (void)free(name);
                    173:                        return(1);
                    174:                }
                    175:
                    176:                /* remember start position of current archive object */
                    177:                last_ar_off = ftell(fp);
                    178:
                    179:                /* skip ranlib entries */
                    180:                if (!strncmp(ar_head.ar_name, RANLIBMAG, sizeof(RANLIBMAG) - 1))
                    181:                        goto skip;
                    182:
                    183:                /*
                    184:                 * construct a name of the form "archive.a:obj.o:" for the
                    185:                 * current archive entry if the object name is to be printed
                    186:                 * on each output line
                    187:                 */
                    188:                p = name;
                    189:                if (count > 1)
                    190:                        p += sprintf(p, "%s:", fname);
                    191: #ifdef AR_EFMT1
                    192:                /*
                    193:                 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
                    194:                 * first <namelen> bytes of the file
                    195:                 */
                    196:                if (            (ar_head.ar_name[0] == '#') &&
                    197:                                (ar_head.ar_name[1] == '1') &&
                    198:                                (ar_head.ar_name[2] == '/') &&
                    199:                                (isdigit(ar_head.ar_name[3]))) {
                    200:
                    201:                        int len = atoi(&ar_head.ar_name[3]);
                    202:                        if (len > namelen) {
                    203:                                p -= (long)name;
                    204:                                name = (char *)erealloc(name, baselen+len);
                    205:                                namelen = len;
                    206:                                p += (long)name;
                    207:                        }
                    208:                        if (fread(p, len, 1, fp) != 1) {
                    209:                                (void)fprintf(stderr,
                    210:                                    "nm: %s: premature EOF.\n", name);
                    211:                                (void)free(name);
                    212:                                return 1;
                    213:                        }
                    214:                        p += len;
                    215:                } else
                    216: #endif
                    217:                for (i = 0; i < sizeof(ar_head.ar_name); ++i)
                    218:                        if (ar_head.ar_name[i] && ar_head.ar_name[i] != ' ')
                    219:                                *p++ = ar_head.ar_name[i];
                    220:                *p++ = '\0';
                    221:
                    222:                /* get and check current object's header */
                    223:                if (fread((char *)&exec_head, sizeof(exec_head),
                    224:                    (size_t)1, fp) != 1) {
                    225:                        warnx("%s: premature EOF", name);
                    226:                        (void)free(name);
                    227:                        return(1);
                    228:                }
                    229:
                    230:                if (N_BADMAG(exec_head)) {
                    231:                        if (!ignore_bad_archive_entries) {
                    232:                                warnx("%s: bad format", name);
                    233:                                rval = 1;
                    234:                        }
1.4     ! deraadt   235:                } else if (N_GETMID(exec_head) != MID_MACHINE) {
        !           236:                        if (!ignore_bad_archive_entries) {
        !           237:                                warnx("%s: wrong architecture", name);
        !           238:                                rval = 1;
        !           239:                        }
1.2       deraadt   240:                } else {
                    241:                        (void)fseek(fp, (long)-sizeof(exec_head),
                    242:                            SEEK_CUR);
                    243:                        rval |= show_objfile(2, name, fp);
                    244:                }
                    245:
                    246:                /*
                    247:                 * skip to next archive object - it starts at the next
                    248:                 * even byte boundary
                    249:                 */
                    250: #define even(x) (((x) + 1) & ~1)
                    251: skip:          if (fseek(fp, last_ar_off + even(atol(ar_head.ar_size)),
                    252:                    SEEK_SET)) {
                    253:                        warn("%s", fname);
                    254:                        (void)free(name);
                    255:                        return(1);
                    256:                }
                    257:        }
                    258:        (void)free(name);
                    259:        return(rval);
                    260: }
                    261:
                    262: int
                    263: show_objfile(count, name, fp)
1.1       deraadt   264:        int count;
                    265:        char *name;
1.2       deraadt   266:        FILE *fp;
1.1       deraadt   267: {
                    268:        static int first = 1;
                    269:        struct exec head;
                    270:        u_long total;
                    271:
1.2       deraadt   272:        if (fread((char *)&head, sizeof(head), (size_t)1, fp) != 1) {
                    273:                warnx("%s: cannot read header", name);
                    274:                return(1);
                    275:        }
                    276:
                    277:        if (N_BADMAG(head)) {
                    278:                warnx("%s: bad format", name);
1.4     ! deraadt   279:                return(1);
        !           280:        }
        !           281:
        !           282:        if (N_GETMID(head) != MID_MACHINE) {
        !           283:                warnx("%s: wrong architecture", name);
1.2       deraadt   284:                return(1);
1.1       deraadt   285:        }
                    286:
                    287:        if (first) {
                    288:                first = 0;
                    289:                (void)printf("text\tdata\tbss\tdec\thex\n");
                    290:        }
                    291:        total = head.a_text + head.a_data + head.a_bss;
                    292:        (void)printf("%lu\t%lu\t%lu\t%lu\t%lx", head.a_text, head.a_data,
                    293:            head.a_bss, total, total);
                    294:        if (count > 1)
                    295:                (void)printf("\t%s", name);
                    296:        (void)printf("\n");
                    297:        return (0);
                    298: }
                    299:
1.2       deraadt   300: void *
                    301: emalloc(size)
                    302:        size_t size;
                    303: {
                    304:        char *p;
                    305:
                    306:        /* NOSTRICT */
                    307:        if (p = malloc(size))
                    308:                return(p);
                    309:        err(1, NULL);
                    310: }
                    311:
                    312: void *
                    313: erealloc(p, size)
                    314:        void   *p;
                    315:        size_t size;
                    316: {
                    317:        /* NOSTRICT */
                    318:        if (p = realloc(p, size))
                    319:                return(p);
                    320:        err(1, NULL);
                    321: }
                    322:
1.1       deraadt   323: void
                    324: usage()
                    325: {
1.2       deraadt   326:        (void)fprintf(stderr, "usage: size [-w] [file ...]\n");
1.1       deraadt   327:        exit(1);
                    328: }