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