Annotation of src/usr.bin/size/size.c, Revision 1.20
1.20 ! mickey 1: /* $OpenBSD: size.c,v 1.19 2003/06/10 22:20:51 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.
1.18 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #ifndef lint
1.20 ! mickey 34: static const char copyright[] =
1.1 deraadt 35: "@(#) Copyright (c) 1988, 1993\n\
36: The Regents of the University of California. All rights reserved.\n";
37: #endif /* not lint */
38:
39: #ifndef lint
40: #if 0
41: static char sccsid[] = "@(#)size.c 8.2 (Berkeley) 12/9/93";
42: #endif
1.20 ! mickey 43: static const char rcsid[] = "$OpenBSD: size.c,v 1.19 2003/06/10 22:20:51 deraadt Exp $";
1.1 deraadt 44: #endif /* not lint */
45:
46: #include <sys/param.h>
47: #include <sys/file.h>
1.20 ! mickey 48: #include <elf_abi.h>
1.1 deraadt 49: #include <a.out.h>
1.2 deraadt 50: #include <ar.h>
51: #include <ranlib.h>
1.1 deraadt 52: #include <unistd.h>
53: #include <stdlib.h>
54: #include <stdio.h>
1.8 deraadt 55: #include <string.h>
56: #include <ctype.h>
1.1 deraadt 57: #include <err.h>
1.11 espie 58: #include "byte.c"
1.9 niklas 59:
60: #ifdef MID_MACHINE_OVERRIDE
61: #undef MID_MACHINE
62: #define MID_MACHINE MID_MACHINE_OVERRIDE
63: #endif
1.1 deraadt 64:
1.20 ! mickey 65: #define STRTABMAG "//"
! 66:
! 67: union hdr {
! 68: struct exec aout;
! 69: Elf_Ehdr elf;
! 70: };
! 71:
1.6 deraadt 72: unsigned long total_text, total_data, total_bss, total_total;
1.20 ! mickey 73: int non_object_warning, print_totals;
1.2 deraadt 74:
1.14 millert 75: int process_file(int, char *);
76: int show_archive(int, char *, FILE *);
1.20 ! mickey 77: int show_file(int, int, char *, FILE *, off_t, union hdr *);
1.14 millert 78: void usage(void);
1.1 deraadt 79:
80: int
1.19 deraadt 81: main(int argc, char *argv[])
1.1 deraadt 82: {
83: int ch, eval;
84:
1.6 deraadt 85: while ((ch = getopt(argc, argv, "wt")) != -1)
1.1 deraadt 86: switch(ch) {
1.2 deraadt 87: case 'w':
1.20 ! mickey 88: non_object_warning = 1;
1.2 deraadt 89: break;
1.6 deraadt 90: case 't':
91: print_totals = 1;
92: break;
1.1 deraadt 93: case '?':
94: default:
95: usage();
96: }
97: argc -= optind;
98: argv += optind;
99:
100: eval = 0;
101: if (*argv)
102: do {
1.2 deraadt 103: eval |= process_file(argc, *argv);
1.1 deraadt 104: } while (*++argv);
105: else
1.2 deraadt 106: eval |= process_file(1, "a.out");
1.6 deraadt 107:
108: if (print_totals)
109: (void)printf("\n%lu\t%lu\t%lu\t%lu\t%lx\tTOTAL\n",
110: total_text, total_data, total_bss,
111: total_total, total_total);
1.1 deraadt 112: exit(eval);
113: }
114:
1.2 deraadt 115: /*
116: * process_file()
117: * show symbols in the file given as an argument. Accepts archive and
118: * object files as input.
119: */
120: int
1.19 deraadt 121: process_file(int count, char *fname)
1.2 deraadt 122: {
1.20 ! mickey 123: union hdr exec_head;
1.2 deraadt 124: FILE *fp;
125: int retval;
126: char magic[SARMAG];
1.6 deraadt 127:
1.2 deraadt 128: if (!(fp = fopen(fname, "r"))) {
129: warnx("cannot read %s", fname);
130: return(1);
131: }
132:
133: /*
134: * first check whether this is an object file - read a object
135: * header, and skip back to the beginning
136: */
137: if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
138: warnx("%s: bad format", fname);
139: (void)fclose(fp);
140: return(1);
141: }
142: rewind(fp);
143:
144: /* this could be an archive */
1.20 ! mickey 145: if (!IS_ELF(exec_head.elf) && N_BADMAG(exec_head.aout)) {
1.2 deraadt 146: if (fread(magic, sizeof(magic), (size_t)1, fp) != 1 ||
147: strncmp(magic, ARMAG, SARMAG)) {
148: warnx("%s: not object file or archive", fname);
149: (void)fclose(fp);
150: return(1);
151: }
152: retval = show_archive(count, fname, fp);
153: } else
1.20 ! mickey 154: retval = show_file(count, 1, fname, fp, 0, &exec_head);
1.2 deraadt 155: (void)fclose(fp);
156: return(retval);
157: }
158:
159: /*
160: * show_archive()
161: * show symbols in the given archive file
162: */
1.1 deraadt 163: int
1.19 deraadt 164: show_archive(int count, char *fname, FILE *fp)
1.2 deraadt 165: {
166: struct ar_hdr ar_head;
1.20 ! mickey 167: union hdr exec_head;
1.2 deraadt 168: int i, rval;
1.20 ! mickey 169: off_t last_ar_off, foff;
! 170: char *p, *name, *strtab;
1.2 deraadt 171: int baselen, namelen;
172:
173: baselen = strlen(fname) + 3;
174: namelen = sizeof(ar_head.ar_name);
1.12 smart 175: if ((name = malloc(baselen + namelen)) == NULL)
176: err(1, NULL);
1.2 deraadt 177:
178: rval = 0;
1.20 ! mickey 179: strtab = NULL;
1.2 deraadt 180:
181: /* while there are more entries in the archive */
182: while (fread((char *)&ar_head, sizeof(ar_head), (size_t)1, fp) == 1) {
183: /* bad archive entry - stop processing this archive */
184: if (strncmp(ar_head.ar_fmag, ARFMAG, sizeof(ar_head.ar_fmag))) {
185: warnx("%s: bad format archive header", fname);
1.20 ! mickey 186: free(name);
! 187: free(strtab);
1.2 deraadt 188: return(1);
189: }
190:
191: /* remember start position of current archive object */
1.20 ! mickey 192: last_ar_off = ftello(fp);
1.2 deraadt 193:
194: /* skip ranlib entries */
195: if (!strncmp(ar_head.ar_name, RANLIBMAG, sizeof(RANLIBMAG) - 1))
196: goto skip;
197:
1.20 ! mickey 198: /* load the Sys5 long names table */
! 199: if (!strncmp(ar_head.ar_name, STRTABMAG,
! 200: sizeof(STRTABMAG) - 1)) {
! 201:
! 202: i = atol(ar_head.ar_size);
! 203: if ((strtab = malloc(i)) == NULL) {
! 204: warn("%s: strtab", name);
! 205: free(name);
! 206: return(1);
! 207: }
! 208:
! 209: if (fread(strtab, i, (size_t)1, fp) != 1) {
! 210: warnx("%s: premature EOF", name);
! 211: free(strtab);
! 212: free(name);
! 213: return(1);
! 214: }
! 215:
! 216: for (p = strtab; i--; p++)
! 217: if (*p == '\n')
! 218: *p = '\0';
! 219: goto skip;
! 220: }
! 221:
1.2 deraadt 222: /*
223: * construct a name of the form "archive.a:obj.o:" for the
224: * current archive entry if the object name is to be printed
225: * on each output line
226: */
227: p = name;
1.17 deraadt 228: if (count > 1) {
229: snprintf(name, baselen - 1, "%s:", fname);
230: p += strlen(name);
231: }
1.20 ! mickey 232:
! 233: if (strtab && ar_head.ar_name[0] == '/') {
! 234: int len;
! 235:
! 236: i = atol(&ar_head.ar_name[1]);
! 237: len = strlen(&strtab[i]);
! 238: if (len > namelen) {
! 239: p -= (long)name;
! 240: if ((name = realloc(name, baselen+len)) == NULL)
! 241: err(1, NULL);
! 242: namelen = len;
! 243: p += (long)name;
! 244: }
! 245: strlcpy(p, &strtab[i], len);
! 246: p += len;
! 247: } else
1.2 deraadt 248: #ifdef AR_EFMT1
249: /*
250: * BSD 4.4 extended AR format: #1/<namelen>, with name as the
251: * first <namelen> bytes of the file
252: */
1.15 deraadt 253: if ((ar_head.ar_name[0] == '#') &&
254: (ar_head.ar_name[1] == '1') &&
255: (ar_head.ar_name[2] == '/') &&
256: (isdigit(ar_head.ar_name[3]))) {
257: int len = atoi(&ar_head.ar_name[3]);
1.2 deraadt 258:
259: if (len > namelen) {
260: p -= (long)name;
1.12 smart 261: if ((name = realloc(name, baselen+len)) == NULL)
262: err(1, NULL);
1.2 deraadt 263: namelen = len;
264: p += (long)name;
265: }
266: if (fread(p, len, 1, fp) != 1) {
1.12 smart 267: warnx("%s: premature EOF", name);
1.20 ! mickey 268: free(name);
1.12 smart 269: return(1);
1.2 deraadt 270: }
271: p += len;
272: } else
273: #endif
274: for (i = 0; i < sizeof(ar_head.ar_name); ++i)
275: if (ar_head.ar_name[i] && ar_head.ar_name[i] != ' ')
276: *p++ = ar_head.ar_name[i];
1.20 ! mickey 277: *p = '\0';
! 278: if (p[-1] == '/')
! 279: *--p = '\0';
! 280:
! 281: foff = ftello(fp);
1.2 deraadt 282:
283: /* get and check current object's header */
284: if (fread((char *)&exec_head, sizeof(exec_head),
285: (size_t)1, fp) != 1) {
286: warnx("%s: premature EOF", name);
1.20 ! mickey 287: free(name);
1.2 deraadt 288: return(1);
289: }
290:
1.20 ! mickey 291: rval |= show_file(2, non_object_warning, name, fp, foff, &exec_head);
1.2 deraadt 292: /*
293: * skip to next archive object - it starts at the next
294: * even byte boundary
295: */
296: #define even(x) (((x) + 1) & ~1)
1.20 ! mickey 297: skip: if (fseeko(fp, last_ar_off + even(atol(ar_head.ar_size)),
1.2 deraadt 298: SEEK_SET)) {
299: warn("%s", fname);
1.20 ! mickey 300: free(name);
1.2 deraadt 301: return(1);
302: }
303: }
1.20 ! mickey 304: free(name);
1.2 deraadt 305: return(rval);
306: }
307:
308: int
1.20 ! mickey 309: show_file(int count, int warn_fmt, char *name, FILE *fp, off_t foff, union hdr *head)
1.1 deraadt 310: {
311: static int first = 1;
1.20 ! mickey 312: Elf_Shdr *shdr;
! 313: u_long text, data, bss, total;
! 314: int i;
! 315:
! 316: if (IS_ELF(head->elf) &&
! 317: head->elf.e_ident[EI_CLASS] == ELF_TARG_CLASS &&
! 318: head->elf.e_ident[EI_DATA] == ELF_TARG_DATA &&
! 319: head->elf.e_ident[EI_VERSION] == ELF_TARG_VER &&
! 320: head->elf.e_machine == ELF_TARG_MACH &&
! 321: head->elf.e_version == ELF_TARG_VER) {
! 322:
! 323: if ((shdr = malloc(head->elf.e_shentsize *
! 324: head->elf.e_shnum)) == NULL) {
! 325: warn("%s: malloc shdr", name);
! 326: return (1);
! 327: }
! 328:
! 329: if (fseeko(fp, foff + head->elf.e_shoff, SEEK_SET)) {
! 330: warn("%s: fseeko", name);
! 331: free(shdr);
! 332: return (1);
! 333: }
1.1 deraadt 334:
1.20 ! mickey 335: if (fread(shdr, head->elf.e_shentsize, head->elf.e_shnum,
! 336: fp) != head->elf.e_shnum) {
! 337: warnx("%s: premature EOF", name);
! 338: free(shdr);
! 339: return(1);
! 340: }
! 341:
! 342: text = data = bss = 0;
! 343: for (i = 0; i < head->elf.e_shnum; i++) {
! 344: if (!(shdr[i].sh_flags & SHF_ALLOC))
! 345: ;
! 346: else if (shdr[i].sh_flags & SHF_EXECINSTR ||
! 347: !(shdr[i].sh_flags & SHF_WRITE))
! 348: text += shdr[i].sh_size;
! 349: else if (shdr[i].sh_type == SHT_NOBITS)
! 350: bss += shdr[i].sh_size;
! 351: else
! 352: data += shdr[i].sh_size;
! 353: }
! 354: free(shdr);
1.2 deraadt 355:
1.20 ! mickey 356: } else if (BAD_OBJECT(head->aout)) {
! 357: if (warn_fmt)
! 358: warnx("%s: bad format", name);
! 359: return (1);
! 360: } else {
! 361: fix_header_order(&head->aout);
! 362:
! 363: text = head->aout.a_text;
! 364: data = head->aout.a_data;
! 365: bss = head->aout.a_bss;
1.4 deraadt 366: }
367:
1.1 deraadt 368: if (first) {
369: first = 0;
370: (void)printf("text\tdata\tbss\tdec\thex\n");
371: }
1.20 ! mickey 372:
! 373: total = text + data + bss;
! 374: (void)printf("%lu\t%lu\t%lu\t%lu\t%lx", text, data, bss, total, total);
1.1 deraadt 375: if (count > 1)
376: (void)printf("\t%s", name);
1.6 deraadt 377:
1.20 ! mickey 378: total_text += text;
! 379: total_data += data;
! 380: total_bss += bss;
1.6 deraadt 381: total_total += total;
382:
1.1 deraadt 383: (void)printf("\n");
384: return (0);
1.2 deraadt 385: }
386:
1.1 deraadt 387: void
1.19 deraadt 388: usage(void)
1.1 deraadt 389: {
1.7 mickey 390: (void)fprintf(stderr, "usage: size [-tw] [file ...]\n");
1.1 deraadt 391: exit(1);
392: }