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