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

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