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

1.9     ! niklas      1: /*     $OpenBSD: size.c,v 1.8 1997/09/11 11:21:52 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.9     ! niklas     47: static char rcsid[] = "$OpenBSD: size.c,v 1.8 1997/09/11 11:21:52 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>
1.8       deraadt    58: #include <string.h>
                     59: #include <ctype.h>
1.1       deraadt    60: #include <err.h>
1.9     ! niklas     61:
        !            62: #ifdef MID_MACHINE_OVERRIDE
        !            63: #undef MID_MACHINE
        !            64: #define MID_MACHINE MID_MACHINE_OVERRIDE
        !            65: #endif
1.1       deraadt    66:
1.6       deraadt    67: unsigned long total_text, total_data, total_bss, total_total;
1.2       deraadt    68: int ignore_bad_archive_entries = 1;
1.6       deraadt    69: int print_totals = 0;
1.2       deraadt    70:
                     71: int    process_file __P((int, char *));
                     72: int    show_archive __P((int, char *, FILE *));
1.8       deraadt    73: int    show_objfile __P((int, char *, FILE *));
1.2       deraadt    74: int    show_object __P((int, char *, FILE *));
                     75: void   *emalloc __P((size_t));
                     76: void   *erealloc __P((void *, size_t));
1.1       deraadt    77: void   usage __P((void));
                     78:
                     79: int
                     80: main(argc, argv)
                     81:        int argc;
                     82:        char *argv[];
                     83: {
                     84:        int ch, eval;
                     85:
1.6       deraadt    86:        while ((ch = getopt(argc, argv, "wt")) != -1)
1.1       deraadt    87:                switch(ch) {
1.2       deraadt    88:                case 'w':
                     89:                        ignore_bad_archive_entries = 0;
                     90:                        break;
1.6       deraadt    91:                case 't':
                     92:                        print_totals = 1;
                     93:                        break;
1.1       deraadt    94:                case '?':
                     95:                default:
                     96:                        usage();
                     97:                }
                     98:        argc -= optind;
                     99:        argv += optind;
                    100:
                    101:        eval = 0;
                    102:        if (*argv)
                    103:                do {
1.2       deraadt   104:                        eval |= process_file(argc, *argv);
1.1       deraadt   105:                } while (*++argv);
                    106:        else
1.2       deraadt   107:                eval |= process_file(1, "a.out");
1.6       deraadt   108:
                    109:        if (print_totals)
                    110:                (void)printf("\n%lu\t%lu\t%lu\t%lu\t%lx\tTOTAL\n",
                    111:                    total_text, total_data, total_bss,
                    112:                    total_total, total_total);
1.1       deraadt   113:        exit(eval);
                    114: }
                    115:
1.2       deraadt   116: /*
                    117:  * process_file()
                    118:  *     show symbols in the file given as an argument.  Accepts archive and
                    119:  *     object files as input.
                    120:  */
                    121: int
                    122: process_file(count, fname)
                    123:        int count;
                    124:        char *fname;
                    125: {
                    126:        struct exec exec_head;
                    127:        FILE *fp;
                    128:        int retval;
                    129:        char magic[SARMAG];
1.6       deraadt   130:
1.2       deraadt   131:        if (!(fp = fopen(fname, "r"))) {
                    132:                warnx("cannot read %s", fname);
                    133:                return(1);
                    134:        }
                    135:
                    136:        /*
                    137:         * first check whether this is an object file - read a object
                    138:         * header, and skip back to the beginning
                    139:         */
                    140:        if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
                    141:                warnx("%s: bad format", fname);
                    142:                (void)fclose(fp);
                    143:                return(1);
                    144:        }
                    145:        rewind(fp);
                    146:
                    147:        /* this could be an archive */
                    148:        if (N_BADMAG(exec_head)) {
                    149:                if (fread(magic, sizeof(magic), (size_t)1, fp) != 1 ||
                    150:                    strncmp(magic, ARMAG, SARMAG)) {
                    151:                        warnx("%s: not object file or archive", fname);
                    152:                        (void)fclose(fp);
                    153:                        return(1);
                    154:                }
                    155:                retval = show_archive(count, fname, fp);
                    156:        } else
                    157:                retval = show_objfile(count, fname, fp);
                    158:        (void)fclose(fp);
                    159:        return(retval);
                    160: }
                    161:
                    162: /*
                    163:  * show_archive()
                    164:  *     show symbols in the given archive file
                    165:  */
1.1       deraadt   166: int
1.2       deraadt   167: show_archive(count, fname, fp)
                    168:        int count;
                    169:        char *fname;
                    170:        FILE *fp;
                    171: {
                    172:        struct ar_hdr ar_head;
                    173:        struct exec exec_head;
                    174:        int i, rval;
                    175:        long last_ar_off;
                    176:        char *p, *name;
                    177:        int baselen, namelen;
                    178:
                    179:        baselen = strlen(fname) + 3;
                    180:        namelen = sizeof(ar_head.ar_name);
                    181:        name = emalloc(baselen + namelen);
                    182:
                    183:        rval = 0;
                    184:
                    185:        /* while there are more entries in the archive */
                    186:        while (fread((char *)&ar_head, sizeof(ar_head), (size_t)1, fp) == 1) {
                    187:                /* bad archive entry - stop processing this archive */
                    188:                if (strncmp(ar_head.ar_fmag, ARFMAG, sizeof(ar_head.ar_fmag))) {
                    189:                        warnx("%s: bad format archive header", fname);
                    190:                        (void)free(name);
                    191:                        return(1);
                    192:                }
                    193:
                    194:                /* remember start position of current archive object */
                    195:                last_ar_off = ftell(fp);
                    196:
                    197:                /* skip ranlib entries */
                    198:                if (!strncmp(ar_head.ar_name, RANLIBMAG, sizeof(RANLIBMAG) - 1))
                    199:                        goto skip;
                    200:
                    201:                /*
                    202:                 * construct a name of the form "archive.a:obj.o:" for the
                    203:                 * current archive entry if the object name is to be printed
                    204:                 * on each output line
                    205:                 */
                    206:                p = name;
                    207:                if (count > 1)
                    208:                        p += sprintf(p, "%s:", fname);
                    209: #ifdef AR_EFMT1
                    210:                /*
                    211:                 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
                    212:                 * first <namelen> bytes of the file
                    213:                 */
                    214:                if (            (ar_head.ar_name[0] == '#') &&
                    215:                                (ar_head.ar_name[1] == '1') &&
                    216:                                (ar_head.ar_name[2] == '/') &&
                    217:                                (isdigit(ar_head.ar_name[3]))) {
                    218:
                    219:                        int len = atoi(&ar_head.ar_name[3]);
                    220:                        if (len > namelen) {
                    221:                                p -= (long)name;
                    222:                                name = (char *)erealloc(name, baselen+len);
                    223:                                namelen = len;
                    224:                                p += (long)name;
                    225:                        }
                    226:                        if (fread(p, len, 1, fp) != 1) {
                    227:                                (void)fprintf(stderr,
                    228:                                    "nm: %s: premature EOF.\n", name);
                    229:                                (void)free(name);
                    230:                                return 1;
                    231:                        }
                    232:                        p += len;
                    233:                } else
                    234: #endif
                    235:                for (i = 0; i < sizeof(ar_head.ar_name); ++i)
                    236:                        if (ar_head.ar_name[i] && ar_head.ar_name[i] != ' ')
                    237:                                *p++ = ar_head.ar_name[i];
                    238:                *p++ = '\0';
                    239:
                    240:                /* get and check current object's header */
                    241:                if (fread((char *)&exec_head, sizeof(exec_head),
                    242:                    (size_t)1, fp) != 1) {
                    243:                        warnx("%s: premature EOF", name);
                    244:                        (void)free(name);
                    245:                        return(1);
                    246:                }
                    247:
                    248:                if (N_BADMAG(exec_head)) {
                    249:                        if (!ignore_bad_archive_entries) {
                    250:                                warnx("%s: bad format", name);
                    251:                                rval = 1;
                    252:                        }
1.4       deraadt   253:                } else if (N_GETMID(exec_head) != MID_MACHINE) {
                    254:                        if (!ignore_bad_archive_entries) {
                    255:                                warnx("%s: wrong architecture", name);
                    256:                                rval = 1;
                    257:                        }
1.2       deraadt   258:                } else {
                    259:                        (void)fseek(fp, (long)-sizeof(exec_head),
                    260:                            SEEK_CUR);
                    261:                        rval |= show_objfile(2, name, fp);
                    262:                }
                    263:
                    264:                /*
                    265:                 * skip to next archive object - it starts at the next
                    266:                 * even byte boundary
                    267:                 */
                    268: #define even(x) (((x) + 1) & ~1)
                    269: skip:          if (fseek(fp, last_ar_off + even(atol(ar_head.ar_size)),
                    270:                    SEEK_SET)) {
                    271:                        warn("%s", fname);
                    272:                        (void)free(name);
                    273:                        return(1);
                    274:                }
                    275:        }
                    276:        (void)free(name);
                    277:        return(rval);
                    278: }
                    279:
                    280: int
                    281: show_objfile(count, name, fp)
1.1       deraadt   282:        int count;
                    283:        char *name;
1.2       deraadt   284:        FILE *fp;
1.1       deraadt   285: {
                    286:        static int first = 1;
                    287:        struct exec head;
                    288:        u_long total;
                    289:
1.2       deraadt   290:        if (fread((char *)&head, sizeof(head), (size_t)1, fp) != 1) {
                    291:                warnx("%s: cannot read header", name);
                    292:                return(1);
                    293:        }
                    294:
                    295:        if (N_BADMAG(head)) {
                    296:                warnx("%s: bad format", name);
1.4       deraadt   297:                return(1);
                    298:        }
                    299:
                    300:        if (N_GETMID(head) != MID_MACHINE) {
                    301:                warnx("%s: wrong architecture", name);
1.2       deraadt   302:                return(1);
1.1       deraadt   303:        }
                    304:
                    305:        if (first) {
                    306:                first = 0;
                    307:                (void)printf("text\tdata\tbss\tdec\thex\n");
                    308:        }
                    309:        total = head.a_text + head.a_data + head.a_bss;
                    310:        (void)printf("%lu\t%lu\t%lu\t%lu\t%lx", head.a_text, head.a_data,
                    311:            head.a_bss, total, total);
                    312:        if (count > 1)
                    313:                (void)printf("\t%s", name);
1.6       deraadt   314:
                    315:        total_text += head.a_text;
                    316:        total_data += head.a_data;
                    317:        total_bss += head.a_bss;
                    318:        total_total += total;
                    319:
1.1       deraadt   320:        (void)printf("\n");
                    321:        return (0);
                    322: }
                    323:
1.2       deraadt   324: void *
                    325: emalloc(size)
                    326:        size_t size;
                    327: {
                    328:        char *p;
                    329:
                    330:        /* NOSTRICT */
1.8       deraadt   331:        if ((p = malloc(size)))
1.2       deraadt   332:                return(p);
                    333:        err(1, NULL);
                    334: }
                    335:
                    336: void *
                    337: erealloc(p, size)
                    338:        void   *p;
                    339:        size_t size;
                    340: {
                    341:        /* NOSTRICT */
1.8       deraadt   342:        if ((p = realloc(p, size)))
1.2       deraadt   343:                return(p);
                    344:        err(1, NULL);
                    345: }
                    346:
1.1       deraadt   347: void
                    348: usage()
                    349: {
1.7       mickey    350:        (void)fprintf(stderr, "usage: size [-tw] [file ...]\n");
1.1       deraadt   351:        exit(1);
                    352: }