Annotation of src/usr.bin/size/size.c, Revision 1.11
1.11 ! espie 1: /* $OpenBSD: size.c,v 1.10 1998/05/11 20:20:55 niklas 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.11 ! espie 47: static char rcsid[] = "$OpenBSD: size.c,v 1.10 1998/05/11 20:20:55 niklas 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.2 deraadt 75: int show_object __P((int, char *, FILE *));
76: void *emalloc __P((size_t));
77: void *erealloc __P((void *, size_t));
1.1 deraadt 78: void usage __P((void));
79:
80: int
81: main(argc, argv)
82: int argc;
83: char *argv[];
84: {
85: int ch, eval;
86:
1.6 deraadt 87: while ((ch = getopt(argc, argv, "wt")) != -1)
1.1 deraadt 88: switch(ch) {
1.2 deraadt 89: case 'w':
90: ignore_bad_archive_entries = 0;
91: break;
1.6 deraadt 92: case 't':
93: print_totals = 1;
94: break;
1.1 deraadt 95: case '?':
96: default:
97: usage();
98: }
99: argc -= optind;
100: argv += optind;
101:
102: eval = 0;
103: if (*argv)
104: do {
1.2 deraadt 105: eval |= process_file(argc, *argv);
1.1 deraadt 106: } while (*++argv);
107: else
1.2 deraadt 108: eval |= process_file(1, "a.out");
1.6 deraadt 109:
110: if (print_totals)
111: (void)printf("\n%lu\t%lu\t%lu\t%lu\t%lx\tTOTAL\n",
112: total_text, total_data, total_bss,
113: total_total, total_total);
1.1 deraadt 114: exit(eval);
115: }
116:
1.2 deraadt 117: /*
118: * process_file()
119: * show symbols in the file given as an argument. Accepts archive and
120: * object files as input.
121: */
122: int
123: process_file(count, fname)
124: int count;
125: char *fname;
126: {
127: struct exec exec_head;
128: FILE *fp;
129: int retval;
130: char magic[SARMAG];
1.6 deraadt 131:
1.2 deraadt 132: if (!(fp = fopen(fname, "r"))) {
133: warnx("cannot read %s", fname);
134: return(1);
135: }
136:
137: /*
138: * first check whether this is an object file - read a object
139: * header, and skip back to the beginning
140: */
141: if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
142: warnx("%s: bad format", fname);
143: (void)fclose(fp);
144: return(1);
145: }
146: rewind(fp);
147:
148: /* this could be an archive */
149: if (N_BADMAG(exec_head)) {
150: if (fread(magic, sizeof(magic), (size_t)1, fp) != 1 ||
151: strncmp(magic, ARMAG, SARMAG)) {
152: warnx("%s: not object file or archive", fname);
153: (void)fclose(fp);
154: return(1);
155: }
156: retval = show_archive(count, fname, fp);
157: } else
158: retval = show_objfile(count, fname, fp);
159: (void)fclose(fp);
160: return(retval);
161: }
162:
163: /*
164: * show_archive()
165: * show symbols in the given archive file
166: */
1.1 deraadt 167: int
1.2 deraadt 168: show_archive(count, fname, fp)
169: int count;
170: char *fname;
171: FILE *fp;
172: {
173: struct ar_hdr ar_head;
174: struct exec exec_head;
175: int i, rval;
176: long last_ar_off;
177: char *p, *name;
178: int baselen, namelen;
179:
180: baselen = strlen(fname) + 3;
181: namelen = sizeof(ar_head.ar_name);
182: name = emalloc(baselen + namelen);
183:
184: rval = 0;
185:
186: /* while there are more entries in the archive */
187: while (fread((char *)&ar_head, sizeof(ar_head), (size_t)1, fp) == 1) {
188: /* bad archive entry - stop processing this archive */
189: if (strncmp(ar_head.ar_fmag, ARFMAG, sizeof(ar_head.ar_fmag))) {
190: warnx("%s: bad format archive header", fname);
191: (void)free(name);
192: return(1);
193: }
194:
195: /* remember start position of current archive object */
196: last_ar_off = ftell(fp);
197:
198: /* skip ranlib entries */
199: if (!strncmp(ar_head.ar_name, RANLIBMAG, sizeof(RANLIBMAG) - 1))
200: goto skip;
201:
202: /*
203: * construct a name of the form "archive.a:obj.o:" for the
204: * current archive entry if the object name is to be printed
205: * on each output line
206: */
207: p = name;
208: if (count > 1)
209: p += sprintf(p, "%s:", fname);
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: */
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:
220: int len = atoi(&ar_head.ar_name[3]);
221: if (len > namelen) {
222: p -= (long)name;
223: name = (char *)erealloc(name, baselen+len);
224: namelen = len;
225: p += (long)name;
226: }
227: if (fread(p, len, 1, fp) != 1) {
228: (void)fprintf(stderr,
229: "nm: %s: premature EOF.\n", name);
230: (void)free(name);
231: return 1;
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;
303: (void)printf("%lu\t%lu\t%lu\t%lu\t%lx", head.a_text, head.a_data,
304: head.a_bss, total, total);
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);
315: }
316:
1.2 deraadt 317: void *
318: emalloc(size)
319: size_t size;
320: {
321: char *p;
322:
323: /* NOSTRICT */
1.8 deraadt 324: if ((p = malloc(size)))
1.2 deraadt 325: return(p);
326: err(1, NULL);
327: }
328:
329: void *
330: erealloc(p, size)
331: void *p;
332: size_t size;
333: {
334: /* NOSTRICT */
1.8 deraadt 335: if ((p = realloc(p, size)))
1.2 deraadt 336: return(p);
337: err(1, NULL);
338: }
339:
1.1 deraadt 340: void
341: usage()
342: {
1.7 mickey 343: (void)fprintf(stderr, "usage: size [-tw] [file ...]\n");
1.1 deraadt 344: exit(1);
345: }