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