Annotation of src/usr.bin/size/size.c, Revision 1.17
1.17 ! deraadt 1: /* $OpenBSD: size.c,v 1.16 2003/04/08 00:53:55 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.17 ! deraadt 47: static char rcsid[] = "$OpenBSD: size.c,v 1.16 2003/04/08 00:53:55 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.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:
1.14 millert 72: int process_file(int, char *);
73: int show_archive(int, char *, FILE *);
74: int show_objfile(int, char *, FILE *);
75: void usage(void);
1.1 deraadt 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;
1.17 ! deraadt 206: if (count > 1) {
! 207: snprintf(name, baselen - 1, "%s:", fname);
! 208: p += strlen(name);
! 209: }
1.2 deraadt 210: #ifdef AR_EFMT1
211: /*
212: * BSD 4.4 extended AR format: #1/<namelen>, with name as the
213: * first <namelen> bytes of the file
214: */
1.15 deraadt 215: if ((ar_head.ar_name[0] == '#') &&
216: (ar_head.ar_name[1] == '1') &&
217: (ar_head.ar_name[2] == '/') &&
218: (isdigit(ar_head.ar_name[3]))) {
219: int len = atoi(&ar_head.ar_name[3]);
1.2 deraadt 220:
221: if (len > namelen) {
222: p -= (long)name;
1.12 smart 223: if ((name = realloc(name, baselen+len)) == NULL)
224: err(1, NULL);
1.2 deraadt 225: namelen = len;
226: p += (long)name;
227: }
228: if (fread(p, len, 1, fp) != 1) {
1.12 smart 229: warnx("%s: premature EOF", name);
1.2 deraadt 230: (void)free(name);
1.12 smart 231: return(1);
1.2 deraadt 232: }
233: p += len;
234: } else
235: #endif
236: for (i = 0; i < sizeof(ar_head.ar_name); ++i)
237: if (ar_head.ar_name[i] && ar_head.ar_name[i] != ' ')
238: *p++ = ar_head.ar_name[i];
239: *p++ = '\0';
240:
241: /* get and check current object's header */
242: if (fread((char *)&exec_head, sizeof(exec_head),
243: (size_t)1, fp) != 1) {
244: warnx("%s: premature EOF", name);
245: (void)free(name);
246: return(1);
247: }
248:
1.11 espie 249: if (BAD_OBJECT(exec_head)) {
1.2 deraadt 250: if (!ignore_bad_archive_entries) {
251: warnx("%s: bad format", name);
252: rval = 1;
253: }
254: } else {
255: (void)fseek(fp, (long)-sizeof(exec_head),
256: SEEK_CUR);
257: rval |= show_objfile(2, name, fp);
258: }
259:
260: /*
261: * skip to next archive object - it starts at the next
262: * even byte boundary
263: */
264: #define even(x) (((x) + 1) & ~1)
265: skip: if (fseek(fp, last_ar_off + even(atol(ar_head.ar_size)),
266: SEEK_SET)) {
267: warn("%s", fname);
268: (void)free(name);
269: return(1);
270: }
271: }
272: (void)free(name);
273: return(rval);
274: }
275:
276: int
277: show_objfile(count, name, fp)
1.1 deraadt 278: int count;
279: char *name;
1.2 deraadt 280: FILE *fp;
1.1 deraadt 281: {
282: static int first = 1;
283: struct exec head;
284: u_long total;
285:
1.2 deraadt 286: if (fread((char *)&head, sizeof(head), (size_t)1, fp) != 1) {
287: warnx("%s: cannot read header", name);
288: return(1);
289: }
290:
1.11 espie 291: if (BAD_OBJECT(head)) {
1.2 deraadt 292: warnx("%s: bad format", name);
1.4 deraadt 293: return(1);
294: }
295:
1.11 espie 296: fix_header_order(&head);
1.1 deraadt 297:
298: if (first) {
299: first = 0;
300: (void)printf("text\tdata\tbss\tdec\thex\n");
301: }
302: total = head.a_text + head.a_data + head.a_bss;
1.13 pvalchev 303: (void)printf("%lu\t%lu\t%lu\t%lu\t%lx", (unsigned long)head.a_text,
304: (unsigned long)head.a_data, (unsigned long)head.a_bss, total, total);
1.1 deraadt 305: if (count > 1)
306: (void)printf("\t%s", name);
1.6 deraadt 307:
308: total_text += head.a_text;
309: total_data += head.a_data;
310: total_bss += head.a_bss;
311: total_total += total;
312:
1.1 deraadt 313: (void)printf("\n");
314: return (0);
1.2 deraadt 315: }
316:
1.1 deraadt 317: void
318: usage()
319: {
1.7 mickey 320: (void)fprintf(stderr, "usage: size [-tw] [file ...]\n");
1.1 deraadt 321: exit(1);
322: }