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