Annotation of src/usr.bin/nm/nm.c, Revision 1.3
1.3 ! deraadt 1: /* $OpenBSD: nm.c,v 1.7 1996/01/14 23:04:03 pk Exp $ */
1.2 deraadt 2: /* $NetBSD: nm.c,v 1.7 1996/01/14 23:04:03 pk Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1989, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Hans Huebner.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
19: * 3. All advertising materials mentioning features or use of this software
20: * must display the following acknowledgement:
21: * This product includes software developed by the University of
22: * California, Berkeley and its contributors.
23: * 4. Neither the name of the University nor the names of its contributors
24: * may be used to endorse or promote products derived from this software
25: * without specific prior written permission.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37: * SUCH DAMAGE.
38: */
39:
40: #ifndef lint
41: static char copyright[] =
42: "@(#) Copyright (c) 1989, 1993\n\
43: The Regents of the University of California. All rights reserved.\n";
44: #endif /* not lint */
45:
46: #ifndef lint
47: #if 0
48: static char sccsid[] = "@(#)nm.c 8.1 (Berkeley) 6/6/93";
49: #endif
1.3 ! deraadt 50: static char rcsid[] = "$OpenBSD: nm.c,v 1.7 1996/01/14 23:04:03 pk Exp $";
1.1 deraadt 51: #endif /* not lint */
52:
53: #include <sys/types.h>
54: #include <a.out.h>
55: #include <stab.h>
56: #include <ar.h>
57: #include <ranlib.h>
58: #include <unistd.h>
1.2 deraadt 59: #include <err.h>
1.1 deraadt 60: #include <ctype.h>
61: #include <stdio.h>
62: #include <stdlib.h>
63: #include <string.h>
64:
65: int ignore_bad_archive_entries = 1;
66: int print_only_external_symbols;
67: int print_only_undefined_symbols;
68: int print_all_symbols;
69: int print_file_each_line;
70: int fcount;
71:
72: int rev;
73: int fname(), rname(), value();
74: int (*sfunc)() = fname;
75:
76: /* some macros for symbol type (nlist.n_type) handling */
77: #define IS_DEBUGGER_SYMBOL(x) ((x) & N_STAB)
78: #define IS_EXTERNAL(x) ((x) & N_EXT)
79: #define SYMBOL_TYPE(x) ((x) & (N_TYPE | N_STAB))
80:
81: void *emalloc(), *erealloc();
82:
83: /*
84: * main()
85: * parse command line, execute process_file() for each file
86: * specified on the command line.
87: */
88: main(argc, argv)
89: int argc;
90: char **argv;
91: {
92: extern int optind;
93: int ch, errors;
94:
95: while ((ch = getopt(argc, argv, "agnopruw")) != EOF) {
96: switch (ch) {
97: case 'a':
98: print_all_symbols = 1;
99: break;
100: case 'g':
101: print_only_external_symbols = 1;
102: break;
103: case 'n':
104: sfunc = value;
105: break;
106: case 'o':
107: print_file_each_line = 1;
108: break;
109: case 'p':
110: sfunc = NULL;
111: break;
112: case 'r':
113: rev = 1;
114: break;
115: case 'u':
116: print_only_undefined_symbols = 1;
117: break;
118: case 'w':
119: ignore_bad_archive_entries = 0;
120: break;
121: case '?':
122: default:
123: usage();
124: }
125: }
126: fcount = argc - optind;
127: argv += optind;
128:
129: if (rev && sfunc == fname)
130: sfunc = rname;
131:
132: if (!fcount)
133: errors = process_file("a.out");
134: else {
135: errors = 0;
136: do {
137: errors |= process_file(*argv);
138: } while (*++argv);
139: }
140: exit(errors);
141: }
142:
143: /*
144: * process_file()
145: * show symbols in the file given as an argument. Accepts archive and
146: * object files as input.
147: */
148: process_file(fname)
149: char *fname;
150: {
151: struct exec exec_head;
152: FILE *fp;
153: int retval;
154: char magic[SARMAG];
155:
156: if (!(fp = fopen(fname, "r"))) {
1.2 deraadt 157: warnx("cannot read %s", fname);
1.1 deraadt 158: return(1);
159: }
160:
161: if (fcount > 1)
162: (void)printf("\n%s:\n", fname);
163:
164: /*
165: * first check whether this is an object file - read a object
166: * header, and skip back to the beginning
167: */
168: if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
1.2 deraadt 169: warnx("%s: bad format", fname);
1.1 deraadt 170: (void)fclose(fp);
171: return(1);
172: }
173: rewind(fp);
174:
175: /* this could be an archive */
176: if (N_BADMAG(exec_head)) {
177: if (fread(magic, sizeof(magic), (size_t)1, fp) != 1 ||
178: strncmp(magic, ARMAG, SARMAG)) {
1.2 deraadt 179: warnx("%s: not object file or archive", fname);
1.1 deraadt 180: (void)fclose(fp);
181: return(1);
182: }
183: retval = show_archive(fname, fp);
184: } else
185: retval = show_objfile(fname, fp);
186: (void)fclose(fp);
187: return(retval);
188: }
189:
190: /*
191: * show_archive()
192: * show symbols in the given archive file
193: */
194: show_archive(fname, fp)
195: char *fname;
196: FILE *fp;
197: {
198: struct ar_hdr ar_head;
199: struct exec exec_head;
200: int i, rval;
201: long last_ar_off;
202: char *p, *name;
203: int baselen, namelen;
204:
205: baselen = strlen(fname) + 3;
206: namelen = sizeof(ar_head.ar_name);
207: name = emalloc(baselen + namelen);
208:
209: rval = 0;
210:
211: /* while there are more entries in the archive */
212: while (fread((char *)&ar_head, sizeof(ar_head), (size_t)1, fp) == 1) {
213: /* bad archive entry - stop processing this archive */
214: if (strncmp(ar_head.ar_fmag, ARFMAG, sizeof(ar_head.ar_fmag))) {
1.2 deraadt 215: warnx("%s: bad format archive header", fname);
1.1 deraadt 216: (void)free(name);
217: return(1);
218: }
219:
220: /* remember start position of current archive object */
221: last_ar_off = ftell(fp);
222:
223: /* skip ranlib entries */
224: if (!strncmp(ar_head.ar_name, RANLIBMAG, sizeof(RANLIBMAG) - 1))
225: goto skip;
226:
227: /*
228: * construct a name of the form "archive.a:obj.o:" for the
229: * current archive entry if the object name is to be printed
230: * on each output line
231: */
232: p = name;
233: if (print_file_each_line)
234: p += sprintf(p, "%s:", fname);
235: #ifdef AR_EFMT1
236: /*
237: * BSD 4.4 extended AR format: #1/<namelen>, with name as the
238: * first <namelen> bytes of the file
239: */
240: if ( (ar_head.ar_name[0] == '#') &&
241: (ar_head.ar_name[1] == '1') &&
242: (ar_head.ar_name[2] == '/') &&
243: (isdigit(ar_head.ar_name[3]))) {
244:
245: int len = atoi(&ar_head.ar_name[3]);
246: if (len > namelen) {
247: p -= (long)name;
248: name = (char *)erealloc(name, baselen+len);
249: namelen = len;
250: p += (long)name;
251: }
252: if (fread(p, len, 1, fp) != 1) {
1.2 deraadt 253: warnx("%s: premature EOF", name);
1.1 deraadt 254: (void)free(name);
255: return 1;
256: }
257: p += len;
258: } else
259: #endif
260: for (i = 0; i < sizeof(ar_head.ar_name); ++i)
261: if (ar_head.ar_name[i] && ar_head.ar_name[i] != ' ')
262: *p++ = ar_head.ar_name[i];
263: *p++ = '\0';
264:
265: /* get and check current object's header */
266: if (fread((char *)&exec_head, sizeof(exec_head),
267: (size_t)1, fp) != 1) {
1.2 deraadt 268: warnx("%s: premature EOF", name);
1.1 deraadt 269: (void)free(name);
270: return(1);
271: }
272:
273: if (N_BADMAG(exec_head)) {
274: if (!ignore_bad_archive_entries) {
1.2 deraadt 275: warnx("%s: bad format", name);
1.1 deraadt 276: rval = 1;
277: }
278: } else {
1.2 deraadt 279: (void)fseek(fp, (long)-sizeof(exec_head), SEEK_CUR);
1.1 deraadt 280: if (!print_file_each_line)
281: (void)printf("\n%s:\n", name);
282: rval |= show_objfile(name, fp);
283: }
284:
285: /*
286: * skip to next archive object - it starts at the next
287: * even byte boundary
288: */
289: #define even(x) (((x) + 1) & ~1)
290: skip: if (fseek(fp, last_ar_off + even(atol(ar_head.ar_size)),
291: SEEK_SET)) {
1.2 deraadt 292: warn("%s", fname);
1.1 deraadt 293: (void)free(name);
294: return(1);
295: }
296: }
297: (void)free(name);
298: return(rval);
299: }
300:
301: /*
302: * show_objfile()
303: * show symbols from the object file pointed to by fp. The current
304: * file pointer for fp is expected to be at the beginning of an a.out
305: * header.
306: */
307: show_objfile(objname, fp)
308: char *objname;
309: FILE *fp;
310: {
311: register struct nlist *names, *np;
312: register int i, nnames, nrawnames;
313: struct exec head;
314: long stabsize;
315: char *stab;
316:
317: /* read a.out header */
318: if (fread((char *)&head, sizeof(head), (size_t)1, fp) != 1) {
1.2 deraadt 319: warnx("%s: cannot read header", objname);
1.1 deraadt 320: return(1);
321: }
322:
323: /*
324: * skip back to the header - the N_-macros return values relative
325: * to the beginning of the a.out header
326: */
327: if (fseek(fp, (long)-sizeof(head), SEEK_CUR)) {
1.2 deraadt 328: warn("%s", objname);
1.1 deraadt 329: return(1);
330: }
331:
332: /* stop if this is no valid object file */
333: if (N_BADMAG(head)) {
1.2 deraadt 334: warnx("%s: bad format", objname);
1.1 deraadt 335: return(1);
336: }
337:
338: /* stop if the object file contains no symbol table */
339: if (!head.a_syms) {
1.2 deraadt 340: warnx("%s: no name list", objname);
1.1 deraadt 341: return(1);
342: }
343:
344: if (fseek(fp, (long)N_SYMOFF(head), SEEK_CUR)) {
1.2 deraadt 345: warn("%s", objname);
1.1 deraadt 346: return(1);
347: }
348:
349: /* get memory for the symbol table */
350: names = emalloc((size_t)head.a_syms);
351: nrawnames = head.a_syms / sizeof(*names);
352: if (fread((char *)names, (size_t)head.a_syms, (size_t)1, fp) != 1) {
1.2 deraadt 353: warnx("%s: cannot read symbol table", objname);
1.1 deraadt 354: (void)free((char *)names);
355: return(1);
356: }
357:
358: /*
359: * Following the symbol table comes the string table. The first
360: * 4-byte-integer gives the total size of the string table
361: * _including_ the size specification itself.
362: */
363: if (fread((char *)&stabsize, sizeof(stabsize), (size_t)1, fp) != 1) {
1.2 deraadt 364: warnx("%s: cannot read stab size", objname);
1.1 deraadt 365: (void)free((char *)names);
366: return(1);
367: }
368: stab = emalloc((size_t)stabsize);
369:
370: /*
371: * read the string table offset by 4 - all indices into the string
372: * table include the size specification.
373: */
374: stabsize -= 4; /* we already have the size */
375: if (fread(stab + 4, (size_t)stabsize, (size_t)1, fp) != 1) {
1.2 deraadt 376: warnx("%s: stab truncated..", objname);
1.1 deraadt 377: (void)free((char *)names);
378: (void)free(stab);
379: return(1);
380: }
381:
382: /*
383: * fix up the symbol table and filter out unwanted entries
384: *
385: * common symbols are characterized by a n_type of N_UNDF and a
386: * non-zero n_value -- change n_type to N_COMM for all such
387: * symbols to make life easier later.
388: *
389: * filter out all entries which we don't want to print anyway
390: */
391: for (np = names, i = nnames = 0; i < nrawnames; np++, i++) {
392: if (SYMBOL_TYPE(np->n_type) == N_UNDF && np->n_value)
393: np->n_type = N_COMM | (np->n_type & N_EXT);
394: if (!print_all_symbols && IS_DEBUGGER_SYMBOL(np->n_type))
395: continue;
396: if (print_only_external_symbols && !IS_EXTERNAL(np->n_type))
397: continue;
398: if (print_only_undefined_symbols &&
399: SYMBOL_TYPE(np->n_type) != N_UNDF)
400: continue;
401:
402: /*
403: * make n_un.n_name a character pointer by adding the string
404: * table's base to n_un.n_strx
405: *
406: * don't mess with zero offsets
407: */
408: if (np->n_un.n_strx)
409: np->n_un.n_name = stab + np->n_un.n_strx;
410: else
411: np->n_un.n_name = "";
412: names[nnames++] = *np;
413: }
414:
415: /* sort the symbol table if applicable */
416: if (sfunc)
417: qsort((char *)names, (size_t)nnames, sizeof(*names), sfunc);
418:
419: /* print out symbols */
420: for (np = names, i = 0; i < nnames; np++, i++)
421: print_symbol(objname, np);
422:
423: (void)free((char *)names);
424: (void)free(stab);
425: return(0);
426: }
427:
428: /*
429: * print_symbol()
430: * show one symbol
431: */
432: print_symbol(objname, sym)
433: char *objname;
434: register struct nlist *sym;
435: {
436: char *typestring(), typeletter();
437:
438: if (print_file_each_line)
439: (void)printf("%s:", objname);
440:
441: /*
442: * handle undefined-only format seperately (no space is
443: * left for symbol values, no type field is printed)
444: */
445: if (print_only_undefined_symbols) {
446: (void)puts(sym->n_un.n_name);
447: return;
448: }
449:
450: /* print symbol's value */
451: if (SYMBOL_TYPE(sym->n_type) == N_UNDF)
452: (void)printf(" ");
453: else
454: (void)printf("%08lx", sym->n_value);
455:
456: /* print type information */
457: if (IS_DEBUGGER_SYMBOL(sym->n_type))
458: (void)printf(" - %02x %04x %5s ", sym->n_other,
459: sym->n_desc&0xffff, typestring(sym->n_type));
460: else
461: (void)printf(" %c ", typeletter(sym->n_type));
462:
463: /* print the symbol's name */
464: (void)puts(sym->n_un.n_name);
465: }
466:
467: /*
468: * typestring()
469: * return the a description string for an STAB entry
470: */
471: char *
472: typestring(type)
473: register u_char type;
474: {
475: switch(type) {
476: case N_BCOMM:
477: return("BCOMM");
478: case N_ECOML:
479: return("ECOML");
480: case N_ECOMM:
481: return("ECOMM");
482: case N_ENTRY:
483: return("ENTRY");
484: case N_FNAME:
485: return("FNAME");
486: case N_FUN:
487: return("FUN");
488: case N_GSYM:
489: return("GSYM");
490: case N_LBRAC:
491: return("LBRAC");
492: case N_LCSYM:
493: return("LCSYM");
494: case N_LENG:
495: return("LENG");
496: case N_LSYM:
497: return("LSYM");
498: case N_PC:
499: return("PC");
500: case N_PSYM:
501: return("PSYM");
502: case N_RBRAC:
503: return("RBRAC");
504: case N_RSYM:
505: return("RSYM");
506: case N_SLINE:
507: return("SLINE");
508: case N_SO:
509: return("SO");
510: case N_SOL:
511: return("SOL");
512: case N_SSYM:
513: return("SSYM");
514: case N_STSYM:
515: return("STSYM");
516: }
517: return("???");
518: }
519:
520: /*
521: * typeletter()
522: * return a description letter for the given basic type code of an
523: * symbol table entry. The return value will be upper case for
524: * external, lower case for internal symbols.
525: */
526: char
527: typeletter(type)
528: u_char type;
529: {
530: switch(SYMBOL_TYPE(type)) {
531: case N_ABS:
532: return(IS_EXTERNAL(type) ? 'A' : 'a');
533: case N_BSS:
534: return(IS_EXTERNAL(type) ? 'B' : 'b');
535: case N_COMM:
536: return(IS_EXTERNAL(type) ? 'C' : 'c');
537: case N_DATA:
538: return(IS_EXTERNAL(type) ? 'D' : 'd');
539: case N_FN:
540: /* NOTE: N_FN == N_WARNING,
541: * in this case, the N_EXT bit is to considered as
542: * part of the symbol's type itself.
543: */
544: return(IS_EXTERNAL(type) ? 'F' : 'W');
545: case N_TEXT:
546: return(IS_EXTERNAL(type) ? 'T' : 't');
547: case N_INDR:
548: return(IS_EXTERNAL(type) ? 'I' : 'i');
549: case N_SIZE:
550: return(IS_EXTERNAL(type) ? 'S' : 's');
551: case N_UNDF:
552: return(IS_EXTERNAL(type) ? 'U' : 'u');
553: }
554: return('?');
555: }
556:
557: fname(a0, b0)
558: void *a0, *b0;
559: {
560: struct nlist *a = a0, *b = b0;
561:
562: return(strcmp(a->n_un.n_name, b->n_un.n_name));
563: }
564:
565: rname(a0, b0)
566: void *a0, *b0;
567: {
568: struct nlist *a = a0, *b = b0;
569:
570: return(strcmp(b->n_un.n_name, a->n_un.n_name));
571: }
572:
573: value(a0, b0)
574: void *a0, *b0;
575: {
576: register struct nlist *a = a0, *b = b0;
577:
578: if (SYMBOL_TYPE(a->n_type) == N_UNDF)
579: if (SYMBOL_TYPE(b->n_type) == N_UNDF)
580: return(0);
581: else
582: return(-1);
583: else if (SYMBOL_TYPE(b->n_type) == N_UNDF)
584: return(1);
585: if (rev) {
586: if (a->n_value == b->n_value)
587: return(rname(a0, b0));
588: return(b->n_value > a->n_value ? 1 : -1);
589: } else {
590: if (a->n_value == b->n_value)
591: return(fname(a0, b0));
592: return(a->n_value > b->n_value ? 1 : -1);
593: }
594: }
595:
596: void *
597: emalloc(size)
598: size_t size;
599: {
600: char *p;
601:
602: /* NOSTRICT */
603: if (p = malloc(size))
604: return(p);
1.2 deraadt 605: err(1, NULL);
1.1 deraadt 606: exit(1);
607: }
608:
609: void *
610: erealloc(p, size)
611: void *p;
612: size_t size;
613: {
614: /* NOSTRICT */
615: if (p = realloc(p, size))
616: return(p);
1.2 deraadt 617: err(1, NULL);
1.1 deraadt 618: exit(1);
619: }
620:
621: usage()
622: {
623: (void)fprintf(stderr, "usage: nm [-agnopruw] [file ...]\n");
624: exit(1);
625: }