2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
38 static const char copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)nm.c 8.1 (Berkeley) 6/6/93";
47 static const char rcsid[] =
51 #include <sys/types.h>
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 print_weak_symbols;
73 int fname(), rname(), value();
74 int (*sfunc)() = fname;
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 #define SYMBOL_BIND(x) (((x) >> 4) & 0xf)
83 static void usage __P(( void ));
84 int process_file __P(( char * ));
85 int show_archive __P(( char *, FILE * ));
86 int show_objfile __P(( char *, FILE * ));
87 void print_symbol __P(( char *, struct nlist * ));
88 char typeletter __P((u_char));
92 * parse command line, execute process_file() for each file
93 * specified on the command line.
102 while ((ch = getopt(argc, argv, "agnoprtuwW")) != -1) {
105 print_all_symbols = 1;
108 print_only_external_symbols = 1;
114 print_file_each_line = 1;
126 print_only_undefined_symbols = 1;
129 ignore_bad_archive_entries = 0;
132 print_weak_symbols = 1;
139 fcount = argc - optind;
142 if (rev && sfunc == fname)
146 errors = process_file("a.out");
150 errors |= process_file(*argv);
158 * show symbols in the file given as an argument. Accepts archive and
159 * object files as input.
165 struct exec exec_head;
170 if (!(fp = fopen(fname, "r"))) {
171 warnx("cannot read %s", fname);
175 if (fcount > 1 && !table)
176 (void)printf("\n%s:\n", fname);
179 * first check whether this is an object file - read a object
180 * header, and skip back to the beginning
182 if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
183 warnx("%s: bad format", fname);
189 /* this could be an archive */
190 if (N_BADMAG(exec_head)) {
191 if (fread(magic, sizeof(magic), (size_t)1, fp) != 1 ||
192 strncmp(magic, ARMAG, SARMAG)) {
193 warnx("%s: not object file or archive", fname);
197 retval = show_archive(fname, fp);
199 retval = show_objfile(fname, fp);
204 /* scat: concatenate strings, returning new concatenation point
205 * and permitting overlap.
207 static char *scat(char *dest, char *src)
210 int l1 = strlen(dest), l2 = strlen(src);
212 memmove(dest + l1, src, l2);
213 end = dest + l1 + l2;
221 * show symbols in the given archive file
224 show_archive(fname, fp)
228 struct ar_hdr ar_head;
229 struct exec exec_head;
232 char *p, *name, *ar_name;
233 int extra = strlen(fname) + 3;
235 name = emalloc(MAXNAMLEN + extra);
236 ar_name = name + extra;
240 /* while there are more entries in the archive */
241 while (fread((char *)&ar_head, sizeof(ar_head), (size_t)1, fp) == 1) {
242 /* bad archive entry - stop processing this archive */
243 if (strncmp(ar_head.ar_fmag, ARFMAG, sizeof(ar_head.ar_fmag))) {
244 warnx("%s: bad format archive header", fname);
249 /* remember start position of current archive object */
250 last_ar_off = ftell(fp);
252 /* skip ranlib entries */
253 if (!bcmp(ar_head.ar_name, RANLIBMAG, sizeof(RANLIBMAG) - 1))
256 /* handle long names. If one is present, read it in at the
257 * end of the "name" buffer.
259 if (!bcmp(ar_head.ar_name, AR_EFMT1, sizeof(AR_EFMT1) - 1))
261 size_t len = atoi(ar_head.ar_name + sizeof(AR_EFMT1) - 1);
263 if (len <= 0 || len > MAXNAMLEN)
265 warnx("illegal length for format 1 long name");
268 if (fread(ar_name, 1, len, fp) != len)
270 warnx("EOF reading format 1 long name");
279 for (i = 0; i < sizeof(ar_head.ar_name) && p[i] && p[i] != ' '; i++)
285 * construct a name of the form "archive.a:obj.o:" for the
286 * current archive entry if the object name is to be printed
287 * on each output line
291 if (print_file_each_line && !table)
296 p = scat(p, ar_name);
298 /* get and check current object's header */
299 if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
300 warnx("%s: premature EOF", name);
305 if (N_BADMAG(exec_head)) {
306 if (!ignore_bad_archive_entries) {
307 warnx("%s: bad format", name);
311 (void)fseek(fp, -(long)sizeof(exec_head),
313 if (!print_file_each_line && !table)
314 (void)printf("\n%s:\n", name);
315 rval |= show_objfile(name, fp);
319 * skip to next archive object - it starts at the next
322 #define even(x) (((x) + 1) & ~1)
323 skip: if (fseek(fp, last_ar_off + even(atol(ar_head.ar_size)),
336 * show symbols from the object file pointed to by fp. The current
337 * file pointer for fp is expected to be at the beginning of an a.out
341 show_objfile(objname, fp)
345 register struct nlist *names, *np;
346 register int i, nnames, nrawnames;
351 /* read a.out header */
352 if (fread((char *)&head, sizeof(head), (size_t)1, fp) != 1) {
353 warnx("%s: cannot read header", objname);
358 * skip back to the header - the N_-macros return values relative
359 * to the beginning of the a.out header
361 if (fseek(fp, -(long)sizeof(head), SEEK_CUR)) {
366 /* stop if this is no valid object file */
367 if (N_BADMAG(head)) {
368 warnx("%s: bad format", objname);
372 /* stop if the object file contains no symbol table */
374 warnx("%s: no name list", objname);
378 if (fseek(fp, (long)N_SYMOFF(head), SEEK_CUR)) {
383 /* get memory for the symbol table */
384 names = emalloc((size_t)head.a_syms);
385 nrawnames = head.a_syms / sizeof(*names);
386 if (fread((char *)names, (size_t)head.a_syms, (size_t)1, fp) != 1) {
387 warnx("%s: cannot read symbol table", objname);
388 (void)free((char *)names);
393 * Following the symbol table comes the string table. The first
394 * 4-byte-integer gives the total size of the string table
395 * _including_ the size specification itself.
397 if (fread((char *)&stabsize, sizeof(stabsize), (size_t)1, fp) != 1) {
398 warnx("%s: cannot read stab size", objname);
399 (void)free((char *)names);
402 stab = emalloc((size_t)stabsize);
405 * read the string table offset by 4 - all indices into the string
406 * table include the size specification.
408 stabsize -= 4; /* we already have the size */
409 if (fread(stab + 4, (size_t)stabsize, (size_t)1, fp) != 1) {
410 warnx("%s: stab truncated..", objname);
411 (void)free((char *)names);
417 * fix up the symbol table and filter out unwanted entries
419 * common symbols are characterized by a n_type of N_UNDF and a
420 * non-zero n_value -- change n_type to N_COMM for all such
421 * symbols to make life easier later.
423 * filter out all entries which we don't want to print anyway
425 for (np = names, i = nnames = 0; i < nrawnames; np++, i++) {
426 if (SYMBOL_TYPE(np->n_type) == N_UNDF && np->n_value)
427 np->n_type = N_COMM | (np->n_type & N_EXT);
428 if (!print_all_symbols && IS_DEBUGGER_SYMBOL(np->n_type))
430 if (print_only_external_symbols && !IS_EXTERNAL(np->n_type))
432 if (print_only_undefined_symbols &&
433 SYMBOL_TYPE(np->n_type) != N_UNDF)
437 * make n_un.n_name a character pointer by adding the string
438 * table's base to n_un.n_strx
440 * don't mess with zero offsets
443 np->n_un.n_name = stab + np->n_un.n_strx;
445 np->n_un.n_name = "";
446 names[nnames++] = *np;
449 /* sort the symbol table if applicable */
451 qsort((char *)names, (size_t)nnames, sizeof(*names), sfunc);
453 /* print out symbols */
454 for (np = names, i = 0; i < nnames; np++, i++)
455 print_symbol(objname, np);
457 (void)free((char *)names);
467 print_symbol(objname, sym)
469 register struct nlist *sym;
474 printf("%s|", objname);
475 if (SYMBOL_TYPE(sym->n_type) != N_UNDF)
476 (void)printf("%08lx", (u_long)sym->n_value);
478 if (IS_DEBUGGER_SYMBOL(sym->n_type))
479 (void)printf("-|%02x %04x %5s|", sym->n_other,
480 sym->n_desc&0xffff, typestring(sym->n_type));
482 putchar(typeletter(sym->n_type));
483 if (print_weak_symbols && SYMBOL_BIND(sym->n_other)== 2)
488 /* print the symbol's name */
489 (void)printf("%s\n",sym->n_un.n_name);
492 if (print_file_each_line)
493 (void)printf("%s:", objname);
496 * handle undefined-only format seperately (no space is
497 * left for symbol values, no type field is printed)
499 if (print_only_undefined_symbols) {
500 (void)puts(sym->n_un.n_name);
504 /* print symbol's value */
505 if (SYMBOL_TYPE(sym->n_type) == N_UNDF)
508 (void)printf("%08lx", (u_long)sym->n_value);
510 /* print type information */
511 if (IS_DEBUGGER_SYMBOL(sym->n_type))
512 (void)printf(" - %02x %04x %5s ", sym->n_other,
513 sym->n_desc&0xffff, typestring(sym->n_type));
516 putchar(typeletter(sym->n_type));
517 if (print_weak_symbols) {
518 if (SYMBOL_BIND(sym->n_other) == 2)
526 /* print the symbol's name */
527 (void)puts(sym->n_un.n_name);
532 * return the a description string for an STAB entry
536 register u_char type;
585 * return a description letter for the given basic type code of an
586 * symbol table entry. The return value will be upper case for
587 * external, lower case for internal symbols.
593 switch(SYMBOL_TYPE(type)) {
595 return(IS_EXTERNAL(type) ? 'A' : 'a');
597 return(IS_EXTERNAL(type) ? 'B' : 'b');
599 return(IS_EXTERNAL(type) ? 'C' : 'c');
601 return(IS_EXTERNAL(type) ? 'D' : 'd');
603 /* This one is overloaded. EXT = Filename, INT = warn refs */
604 return(IS_EXTERNAL(type) ? 'F' : 'w');
606 return(IS_EXTERNAL(type) ? 'I' : 'i');
608 return(IS_EXTERNAL(type) ? 'T' : 't');
610 return(IS_EXTERNAL(type) ? 'U' : 'u');
619 struct nlist *a = a0, *b = b0;
621 return(strcmp(a->n_un.n_name, b->n_un.n_name));
628 struct nlist *a = a0, *b = b0;
630 return(strcmp(b->n_un.n_name, a->n_un.n_name));
637 register struct nlist *a = a0, *b = b0;
639 if (SYMBOL_TYPE(a->n_type) == N_UNDF)
640 if (SYMBOL_TYPE(b->n_type) == N_UNDF)
644 else if (SYMBOL_TYPE(b->n_type) == N_UNDF)
647 if (a->n_value == b->n_value)
648 return(rname(a0, b0));
649 return(b->n_value > a->n_value ? 1 : -1);
651 if (a->n_value == b->n_value)
652 return(fname(a0, b0));
653 return(a->n_value > b->n_value ? 1 : -1);
664 if ( (p = malloc(size)) )
672 (void)fprintf(stderr, "usage: nm [-agnoprtuwW] [file ...]\n");