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

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