Annotation of src/usr.bin/nm/nm.c, Revision 1.10
1.10 ! espie 1: /* $OpenBSD: nm.c,v 1.9 2000/11/10 15:33:12 provos 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.10 ! espie 50: static char rcsid[] = "$OpenBSD: nm.c,v 1.9 2000/11/10 15:33:12 provos Exp $";
1.1 deraadt 51: #endif /* not lint */
52:
1.5 deraadt 53: #include <sys/param.h>
1.1 deraadt 54: #include <sys/types.h>
55: #include <a.out.h>
56: #include <stab.h>
57: #include <ar.h>
58: #include <ranlib.h>
59: #include <unistd.h>
1.2 deraadt 60: #include <err.h>
1.1 deraadt 61: #include <ctype.h>
62: #include <stdio.h>
63: #include <stdlib.h>
64: #include <string.h>
1.8 espie 65: /* XXX get shared code to handle byte-order swaps */
66: #include "byte.c"
1.7 niklas 67:
1.1 deraadt 68:
1.10 ! espie 69: int demangle = 0;
1.1 deraadt 70: int ignore_bad_archive_entries = 1;
71: int print_only_external_symbols;
72: int print_only_undefined_symbols;
73: int print_all_symbols;
74: int print_file_each_line;
75: int fcount;
76:
77: int rev;
78: int fname(), rname(), value();
79: int (*sfunc)() = fname;
80:
1.8 espie 81:
1.1 deraadt 82: /* some macros for symbol type (nlist.n_type) handling */
83: #define IS_DEBUGGER_SYMBOL(x) ((x) & N_STAB)
84: #define IS_EXTERNAL(x) ((x) & N_EXT)
85: #define SYMBOL_TYPE(x) ((x) & (N_TYPE | N_STAB))
86:
87: void *emalloc(), *erealloc();
88:
1.10 ! espie 89: void pipe2cppfilt();
! 90:
1.1 deraadt 91: /*
92: * main()
93: * parse command line, execute process_file() for each file
94: * specified on the command line.
95: */
96: main(argc, argv)
97: int argc;
98: char **argv;
99: {
100: extern int optind;
101: int ch, errors;
102:
1.10 ! espie 103: while ((ch = getopt(argc, argv, "aBCgnopruw")) != -1) {
1.1 deraadt 104: switch (ch) {
105: case 'a':
106: print_all_symbols = 1;
107: break;
1.10 ! espie 108: case 'B':
! 109: /* no-op, compat with gnu-nm */
! 110: break;
! 111: case 'C':
! 112: demangle = 1;
! 113: break;
1.1 deraadt 114: case 'g':
115: print_only_external_symbols = 1;
116: break;
117: case 'n':
118: sfunc = value;
119: break;
120: case 'o':
121: print_file_each_line = 1;
122: break;
123: case 'p':
124: sfunc = NULL;
125: break;
126: case 'r':
127: rev = 1;
128: break;
129: case 'u':
130: print_only_undefined_symbols = 1;
131: break;
132: case 'w':
133: ignore_bad_archive_entries = 0;
134: break;
135: case '?':
136: default:
137: usage();
138: }
139: }
1.10 ! espie 140:
! 141: if (demangle)
! 142: pipe2cppfilt();
1.1 deraadt 143: fcount = argc - optind;
144: argv += optind;
145:
146: if (rev && sfunc == fname)
147: sfunc = rname;
148:
149: if (!fcount)
150: errors = process_file("a.out");
151: else {
152: errors = 0;
153: do {
154: errors |= process_file(*argv);
155: } while (*++argv);
156: }
157: exit(errors);
158: }
159:
160: /*
161: * process_file()
162: * show symbols in the file given as an argument. Accepts archive and
163: * object files as input.
164: */
165: process_file(fname)
166: char *fname;
167: {
168: struct exec exec_head;
169: FILE *fp;
170: int retval;
171: char magic[SARMAG];
172:
173: if (!(fp = fopen(fname, "r"))) {
1.6 deraadt 174: warn("cannot read %s", fname);
1.1 deraadt 175: return(1);
176: }
177:
178: if (fcount > 1)
179: (void)printf("\n%s:\n", fname);
180:
181: /*
182: * first check whether this is an object file - read a object
183: * header, and skip back to the beginning
184: */
185: if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
1.2 deraadt 186: warnx("%s: bad format", fname);
1.1 deraadt 187: (void)fclose(fp);
188: return(1);
189: }
190: rewind(fp);
191:
1.8 espie 192: if (BAD_OBJECT(exec_head)) {
1.1 deraadt 193: /* this could be an archive */
194: if (fread(magic, sizeof(magic), (size_t)1, fp) != 1 ||
195: strncmp(magic, ARMAG, SARMAG)) {
1.2 deraadt 196: warnx("%s: not object file or archive", fname);
1.1 deraadt 197: (void)fclose(fp);
198: return(1);
199: }
200: retval = show_archive(fname, fp);
201: } else
202: retval = show_objfile(fname, fp);
203: (void)fclose(fp);
204: return(retval);
205: }
206:
207: /*
208: * show_archive()
209: * show symbols in the given archive file
210: */
211: show_archive(fname, fp)
212: char *fname;
213: FILE *fp;
214: {
215: struct ar_hdr ar_head;
216: struct exec exec_head;
217: int i, rval;
218: long last_ar_off;
219: char *p, *name;
220: int baselen, namelen;
221:
222: baselen = strlen(fname) + 3;
223: namelen = sizeof(ar_head.ar_name);
224: name = emalloc(baselen + namelen);
225:
226: rval = 0;
227:
228: /* while there are more entries in the archive */
229: while (fread((char *)&ar_head, sizeof(ar_head), (size_t)1, fp) == 1) {
230: /* bad archive entry - stop processing this archive */
231: if (strncmp(ar_head.ar_fmag, ARFMAG, sizeof(ar_head.ar_fmag))) {
1.2 deraadt 232: warnx("%s: bad format archive header", fname);
1.1 deraadt 233: (void)free(name);
234: return(1);
235: }
236:
237: /* remember start position of current archive object */
238: last_ar_off = ftell(fp);
239:
240: /* skip ranlib entries */
241: if (!strncmp(ar_head.ar_name, RANLIBMAG, sizeof(RANLIBMAG) - 1))
242: goto skip;
243:
244: /*
245: * construct a name of the form "archive.a:obj.o:" for the
246: * current archive entry if the object name is to be printed
247: * on each output line
248: */
249: p = name;
250: if (print_file_each_line)
251: p += sprintf(p, "%s:", fname);
252: #ifdef AR_EFMT1
253: /*
254: * BSD 4.4 extended AR format: #1/<namelen>, with name as the
255: * first <namelen> bytes of the file
256: */
257: if ( (ar_head.ar_name[0] == '#') &&
258: (ar_head.ar_name[1] == '1') &&
259: (ar_head.ar_name[2] == '/') &&
260: (isdigit(ar_head.ar_name[3]))) {
261:
262: int len = atoi(&ar_head.ar_name[3]);
263: if (len > namelen) {
264: p -= (long)name;
265: name = (char *)erealloc(name, baselen+len);
266: namelen = len;
267: p += (long)name;
268: }
269: if (fread(p, len, 1, fp) != 1) {
1.2 deraadt 270: warnx("%s: premature EOF", name);
1.1 deraadt 271: (void)free(name);
272: return 1;
273: }
274: p += len;
275: } else
276: #endif
277: for (i = 0; i < sizeof(ar_head.ar_name); ++i)
278: if (ar_head.ar_name[i] && ar_head.ar_name[i] != ' ')
279: *p++ = ar_head.ar_name[i];
280: *p++ = '\0';
281:
282: /* get and check current object's header */
283: if (fread((char *)&exec_head, sizeof(exec_head),
284: (size_t)1, fp) != 1) {
1.2 deraadt 285: warnx("%s: premature EOF", name);
1.1 deraadt 286: (void)free(name);
287: return(1);
288: }
289:
1.8 espie 290: if (BAD_OBJECT(exec_head)) {
1.1 deraadt 291: if (!ignore_bad_archive_entries) {
1.2 deraadt 292: warnx("%s: bad format", name);
1.1 deraadt 293: rval = 1;
294: }
295: } else {
1.2 deraadt 296: (void)fseek(fp, (long)-sizeof(exec_head), SEEK_CUR);
1.1 deraadt 297: if (!print_file_each_line)
298: (void)printf("\n%s:\n", name);
299: rval |= show_objfile(name, fp);
300: }
301:
302: /*
303: * skip to next archive object - it starts at the next
304: * even byte boundary
305: */
306: #define even(x) (((x) + 1) & ~1)
307: skip: if (fseek(fp, last_ar_off + even(atol(ar_head.ar_size)),
308: SEEK_SET)) {
1.2 deraadt 309: warn("%s", fname);
1.1 deraadt 310: (void)free(name);
311: return(1);
312: }
313: }
314: (void)free(name);
315: return(rval);
316: }
317:
318: /*
319: * show_objfile()
320: * show symbols from the object file pointed to by fp. The current
321: * file pointer for fp is expected to be at the beginning of an a.out
322: * header.
323: */
324: show_objfile(objname, fp)
325: char *objname;
326: FILE *fp;
327: {
328: register struct nlist *names, *np;
329: register int i, nnames, nrawnames;
330: struct exec head;
331: long stabsize;
332: char *stab;
333:
334: /* read a.out header */
335: if (fread((char *)&head, sizeof(head), (size_t)1, fp) != 1) {
1.2 deraadt 336: warnx("%s: cannot read header", objname);
1.1 deraadt 337: return(1);
338: }
339:
340: /*
341: * skip back to the header - the N_-macros return values relative
342: * to the beginning of the a.out header
343: */
344: if (fseek(fp, (long)-sizeof(head), SEEK_CUR)) {
1.2 deraadt 345: warn("%s", objname);
1.1 deraadt 346: return(1);
347: }
348:
1.8 espie 349: /* stop if this is no valid object file, or a format we don't dare
350: * playing with
351: */
352: if (BAD_OBJECT(head)) {
1.2 deraadt 353: warnx("%s: bad format", objname);
1.1 deraadt 354: return(1);
355: }
356:
1.8 espie 357: fix_header_order(&head);
358:
1.1 deraadt 359: /* stop if the object file contains no symbol table */
360: if (!head.a_syms) {
1.2 deraadt 361: warnx("%s: no name list", objname);
1.1 deraadt 362: return(1);
363: }
364:
365: if (fseek(fp, (long)N_SYMOFF(head), SEEK_CUR)) {
1.2 deraadt 366: warn("%s", objname);
1.1 deraadt 367: return(1);
368: }
369:
370: /* get memory for the symbol table */
371: names = emalloc((size_t)head.a_syms);
372: nrawnames = head.a_syms / sizeof(*names);
373: if (fread((char *)names, (size_t)head.a_syms, (size_t)1, fp) != 1) {
1.2 deraadt 374: warnx("%s: cannot read symbol table", objname);
1.1 deraadt 375: (void)free((char *)names);
376: return(1);
377: }
1.8 espie 378: fix_nlists_order(names, nrawnames, N_GETMID(head));
1.1 deraadt 379:
380: /*
381: * Following the symbol table comes the string table. The first
382: * 4-byte-integer gives the total size of the string table
383: * _including_ the size specification itself.
384: */
385: if (fread((char *)&stabsize, sizeof(stabsize), (size_t)1, fp) != 1) {
1.2 deraadt 386: warnx("%s: cannot read stab size", objname);
1.1 deraadt 387: (void)free((char *)names);
388: return(1);
389: }
1.8 espie 390: stabsize = fix_long_order(stabsize, N_GETMID(head));
1.1 deraadt 391: stab = emalloc((size_t)stabsize);
392:
393: /*
394: * read the string table offset by 4 - all indices into the string
395: * table include the size specification.
396: */
397: stabsize -= 4; /* we already have the size */
398: if (fread(stab + 4, (size_t)stabsize, (size_t)1, fp) != 1) {
1.2 deraadt 399: warnx("%s: stab truncated..", objname);
1.1 deraadt 400: (void)free((char *)names);
401: (void)free(stab);
402: return(1);
403: }
404:
405: /*
406: * fix up the symbol table and filter out unwanted entries
407: *
408: * common symbols are characterized by a n_type of N_UNDF and a
409: * non-zero n_value -- change n_type to N_COMM for all such
410: * symbols to make life easier later.
411: *
412: * filter out all entries which we don't want to print anyway
413: */
414: for (np = names, i = nnames = 0; i < nrawnames; np++, i++) {
415: if (SYMBOL_TYPE(np->n_type) == N_UNDF && np->n_value)
416: np->n_type = N_COMM | (np->n_type & N_EXT);
417: if (!print_all_symbols && IS_DEBUGGER_SYMBOL(np->n_type))
418: continue;
419: if (print_only_external_symbols && !IS_EXTERNAL(np->n_type))
420: continue;
421: if (print_only_undefined_symbols &&
422: SYMBOL_TYPE(np->n_type) != N_UNDF)
423: continue;
424:
425: /*
426: * make n_un.n_name a character pointer by adding the string
427: * table's base to n_un.n_strx
428: *
429: * don't mess with zero offsets
430: */
431: if (np->n_un.n_strx)
432: np->n_un.n_name = stab + np->n_un.n_strx;
433: else
434: np->n_un.n_name = "";
435: names[nnames++] = *np;
436: }
437:
438: /* sort the symbol table if applicable */
439: if (sfunc)
440: qsort((char *)names, (size_t)nnames, sizeof(*names), sfunc);
441:
442: /* print out symbols */
443: for (np = names, i = 0; i < nnames; np++, i++)
444: print_symbol(objname, np);
445:
446: (void)free((char *)names);
447: (void)free(stab);
448: return(0);
449: }
450:
451: /*
452: * print_symbol()
453: * show one symbol
454: */
455: print_symbol(objname, sym)
456: char *objname;
457: register struct nlist *sym;
458: {
459: char *typestring(), typeletter();
460:
461: if (print_file_each_line)
462: (void)printf("%s:", objname);
463:
464: /*
1.10 ! espie 465: * handle undefined-only format especially (no space is
1.1 deraadt 466: * left for symbol values, no type field is printed)
467: */
1.10 ! espie 468: if (!print_only_undefined_symbols) {
! 469: /* print symbol's value */
! 470: if (SYMBOL_TYPE(sym->n_type) == N_UNDF)
! 471: (void)printf(" ");
! 472: else
! 473: (void)printf("%08lx", sym->n_value);
! 474:
! 475: /* print type information */
! 476: if (IS_DEBUGGER_SYMBOL(sym->n_type))
! 477: (void)printf(" - %02x %04x %5s ", sym->n_other,
! 478: sym->n_desc&0xffff, typestring(sym->n_type));
! 479: else
! 480: (void)printf(" %c ", typeletter(sym->n_type));
1.1 deraadt 481: }
482:
1.10 ! espie 483: /* print the symbol's name */
! 484: if (demangle && sym->n_un.n_name[0] == '_')
! 485: (void)puts(sym->n_un.n_name + 1);
1.1 deraadt 486: else
1.10 ! espie 487: (void)puts(sym->n_un.n_name);
1.1 deraadt 488: }
489:
490: /*
491: * typestring()
492: * return the a description string for an STAB entry
493: */
494: char *
495: typestring(type)
496: register u_char type;
497: {
498: switch(type) {
499: case N_BCOMM:
500: return("BCOMM");
501: case N_ECOML:
502: return("ECOML");
503: case N_ECOMM:
504: return("ECOMM");
505: case N_ENTRY:
506: return("ENTRY");
507: case N_FNAME:
508: return("FNAME");
509: case N_FUN:
510: return("FUN");
511: case N_GSYM:
512: return("GSYM");
513: case N_LBRAC:
514: return("LBRAC");
515: case N_LCSYM:
516: return("LCSYM");
517: case N_LENG:
518: return("LENG");
519: case N_LSYM:
520: return("LSYM");
521: case N_PC:
522: return("PC");
523: case N_PSYM:
524: return("PSYM");
525: case N_RBRAC:
526: return("RBRAC");
527: case N_RSYM:
528: return("RSYM");
529: case N_SLINE:
530: return("SLINE");
531: case N_SO:
532: return("SO");
533: case N_SOL:
534: return("SOL");
535: case N_SSYM:
536: return("SSYM");
537: case N_STSYM:
538: return("STSYM");
539: }
540: return("???");
541: }
542:
543: /*
544: * typeletter()
545: * return a description letter for the given basic type code of an
546: * symbol table entry. The return value will be upper case for
547: * external, lower case for internal symbols.
548: */
549: char
550: typeletter(type)
551: u_char type;
552: {
553: switch(SYMBOL_TYPE(type)) {
554: case N_ABS:
555: return(IS_EXTERNAL(type) ? 'A' : 'a');
556: case N_BSS:
557: return(IS_EXTERNAL(type) ? 'B' : 'b');
558: case N_COMM:
559: return(IS_EXTERNAL(type) ? 'C' : 'c');
560: case N_DATA:
561: return(IS_EXTERNAL(type) ? 'D' : 'd');
562: case N_FN:
563: /* NOTE: N_FN == N_WARNING,
564: * in this case, the N_EXT bit is to considered as
565: * part of the symbol's type itself.
566: */
567: return(IS_EXTERNAL(type) ? 'F' : 'W');
568: case N_TEXT:
569: return(IS_EXTERNAL(type) ? 'T' : 't');
570: case N_INDR:
571: return(IS_EXTERNAL(type) ? 'I' : 'i');
572: case N_SIZE:
573: return(IS_EXTERNAL(type) ? 'S' : 's');
574: case N_UNDF:
575: return(IS_EXTERNAL(type) ? 'U' : 'u');
576: }
577: return('?');
578: }
579:
580: fname(a0, b0)
581: void *a0, *b0;
582: {
583: struct nlist *a = a0, *b = b0;
584:
585: return(strcmp(a->n_un.n_name, b->n_un.n_name));
586: }
587:
588: rname(a0, b0)
589: void *a0, *b0;
590: {
591: struct nlist *a = a0, *b = b0;
592:
593: return(strcmp(b->n_un.n_name, a->n_un.n_name));
594: }
595:
596: value(a0, b0)
597: void *a0, *b0;
598: {
599: register struct nlist *a = a0, *b = b0;
600:
601: if (SYMBOL_TYPE(a->n_type) == N_UNDF)
602: if (SYMBOL_TYPE(b->n_type) == N_UNDF)
603: return(0);
604: else
605: return(-1);
606: else if (SYMBOL_TYPE(b->n_type) == N_UNDF)
607: return(1);
608: if (rev) {
609: if (a->n_value == b->n_value)
610: return(rname(a0, b0));
611: return(b->n_value > a->n_value ? 1 : -1);
612: } else {
613: if (a->n_value == b->n_value)
614: return(fname(a0, b0));
615: return(a->n_value > b->n_value ? 1 : -1);
616: }
617: }
618:
619: void *
620: emalloc(size)
621: size_t size;
622: {
623: char *p;
624:
625: /* NOSTRICT */
626: if (p = malloc(size))
627: return(p);
1.2 deraadt 628: err(1, NULL);
1.1 deraadt 629: exit(1);
630: }
631:
632: void *
633: erealloc(p, size)
634: void *p;
635: size_t size;
636: {
637: /* NOSTRICT */
638: if (p = realloc(p, size))
639: return(p);
1.2 deraadt 640: err(1, NULL);
1.1 deraadt 641: exit(1);
642: }
643:
1.10 ! espie 644: #define CPPFILT "/usr/bin/c++filt"
! 645:
! 646: void
! 647: pipe2cppfilt()
! 648: {
! 649: int pip[2];
! 650: char *argv[2];
! 651:
! 652: argv[0] = "c++filt";
! 653: argv[1] = NULL;
! 654:
! 655: if (pipe(pip) == -1)
! 656: err(1, "pipe");
! 657: switch(fork()) {
! 658: case -1:
! 659: err(1, "fork");
! 660: default:
! 661: dup2(pip[0], 0);
! 662: close(pip[0]);
! 663: close(pip[1]);
! 664: execve(CPPFILT, argv, NULL);
! 665: err(1, "execve");
! 666: case 0:
! 667: dup2(pip[1], 1);
! 668: close(pip[1]);
! 669: close(pip[0]);
! 670: }
! 671: }
! 672:
1.1 deraadt 673: usage()
674: {
1.10 ! espie 675: (void)fprintf(stderr, "usage: nm [-aCgnopruw] [file ...]\n");
1.1 deraadt 676: exit(1);
677: }