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.
103 while ((ch = getopt(argc, argv, "agnoprtuwW")) != -1) {
106 print_all_symbols = 1;
109 print_only_external_symbols = 1;
115 print_file_each_line = 1;
127 print_only_undefined_symbols = 1;
130 ignore_bad_archive_entries = 0;
133 print_weak_symbols = 1;
140 fcount = argc - optind;
143 if (rev && sfunc == fname)
147 errors = process_file("a.out");
151 errors |= process_file(*argv);
159 * show symbols in the file given as an argument. Accepts archive and
160 * object files as input.
166 struct exec exec_head;
171 if (!(fp = fopen(fname, "r"))) {
172 warnx("cannot read %s", fname);
176 if (fcount > 1 && !table)
177 (void)printf("\n%s:\n", fname);
180 * first check whether this is an object file - read a object
181 * header, and skip back to the beginning
183 if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
184 warnx("%s: bad format", fname);
190 /* this could be an archive */
191 if (N_BADMAG(exec_head)) {
192 if (fread(magic, sizeof(magic), (size_t)1, fp) != 1 ||
193 strncmp(magic, ARMAG, SARMAG)) {
194 warnx("%s: not object file or archive", fname);
198 retval = show_archive(fname, fp);
200 retval = show_objfile(fname, fp);
205 /* scat: concatenate strings, returning new concatenation point
206 * and permitting overlap.
208 static char *scat(char *dest, char *src)
211 int l1 = strlen(dest), l2 = strlen(src);
213 memmove(dest + l1, src, l2);
214 end = dest + l1 + l2;
222 * show symbols in the given archive file
225 show_archive(fname, fp)
229 struct ar_hdr ar_head;
230 struct exec exec_head;
233 char *p, *name, *ar_name;
234 int extra = strlen(fname) + 3;
236 name = emalloc(MAXNAMLEN + extra);
237 ar_name = name + extra;
241 /* while there are more entries in the archive */
242 while (fread((char *)&ar_head, sizeof(ar_head), (size_t)1, fp) == 1) {
243 /* bad archive entry - stop processing this archive */
244 if (strncmp(ar_head.ar_fmag, ARFMAG, sizeof(ar_head.ar_fmag))) {
245 warnx("%s: bad format archive header", fname);
250 /* remember start position of current archive object */
251 last_ar_off = ftell(fp);
253 /* skip ranlib entries */
254 if (!bcmp(ar_head.ar_name, RANLIBMAG, sizeof(RANLIBMAG) - 1))
257 /* handle long names. If one is present, read it in at the
258 * end of the "name" buffer.
260 if (!bcmp(ar_head.ar_name, AR_EFMT1, sizeof(AR_EFMT1) - 1))
262 size_t len = atoi(ar_head.ar_name + sizeof(AR_EFMT1) - 1);
264 if (len <= 0 || len > MAXNAMLEN)
266 warnx("illegal length for format 1 long name");
269 if (fread(ar_name, 1, len, fp) != len)
271 warnx("EOF reading format 1 long name");
280 for (i = 0; i < sizeof(ar_head.ar_name) && p[i] && p[i] != ' '; i++)
286 * construct a name of the form "archive.a:obj.o:" for the
287 * current archive entry if the object name is to be printed
288 * on each output line
292 if (print_file_each_line && !table)
297 p = scat(p, ar_name);
299 /* get and check current object's header */
300 if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
301 warnx("%s: premature EOF", name);
306 if (N_BADMAG(exec_head)) {
307 if (!ignore_bad_archive_entries) {
308 warnx("%s: bad format", name);
312 (void)fseek(fp, -(long)sizeof(exec_head),
314 if (!print_file_each_line && !table)
315 (void)printf("\n%s:\n", name);
316 rval |= show_objfile(name, fp);
320 * skip to next archive object - it starts at the next
323 #define even(x) (((x) + 1) & ~1)
324 skip: if (fseek(fp, last_ar_off + even(atol(ar_head.ar_size)),
337 * show symbols from the object file pointed to by fp. The current
338 * file pointer for fp is expected to be at the beginning of an a.out
342 show_objfile(objname, fp)
346 register struct nlist *names, *np;
347 register int i, nnames, nrawnames;
352 /* read a.out header */
353 if (fread((char *)&head, sizeof(head), (size_t)1, fp) != 1) {
354 warnx("%s: cannot read header", objname);
359 * skip back to the header - the N_-macros return values relative
360 * to the beginning of the a.out header
362 if (fseek(fp, -(long)sizeof(head), SEEK_CUR)) {
367 /* stop if this is no valid object file */
368 if (N_BADMAG(head)) {
369 warnx("%s: bad format", objname);
373 /* stop if the object file contains no symbol table */
375 warnx("%s: no name list", objname);
379 if (fseek(fp, (long)N_SYMOFF(head), SEEK_CUR)) {
384 /* get memory for the symbol table */
385 names = emalloc((size_t)head.a_syms);
386 nrawnames = head.a_syms / sizeof(*names);
387 if (fread((char *)names, (size_t)head.a_syms, (size_t)1, fp) != 1) {
388 warnx("%s: cannot read symbol table", objname);
389 (void)free((char *)names);
394 * Following the symbol table comes the string table. The first
395 * 4-byte-integer gives the total size of the string table
396 * _including_ the size specification itself.
398 if (fread((char *)&stabsize, sizeof(stabsize), (size_t)1, fp) != 1) {
399 warnx("%s: cannot read stab size", objname);
400 (void)free((char *)names);
403 stab = emalloc((size_t)stabsize);
406 * read the string table offset by 4 - all indices into the string
407 * table include the size specification.
409 stabsize -= 4; /* we already have the size */
410 if (fread(stab + 4, (size_t)stabsize, (size_t)1, fp) != 1) {
411 warnx("%s: stab truncated..", objname);
412 (void)free((char *)names);
418 * fix up the symbol table and filter out unwanted entries
420 * common symbols are characterized by a n_type of N_UNDF and a
421 * non-zero n_value -- change n_type to N_COMM for all such
422 * symbols to make life easier later.
424 * filter out all entries which we don't want to print anyway
426 for (np = names, i = nnames = 0; i < nrawnames; np++, i++) {
427 if (SYMBOL_TYPE(np->n_type) == N_UNDF && np->n_value)
428 np->n_type = N_COMM | (np->n_type & N_EXT);
429 if (!print_all_symbols && IS_DEBUGGER_SYMBOL(np->n_type))
431 if (print_only_external_symbols && !IS_EXTERNAL(np->n_type))
433 if (print_only_undefined_symbols &&
434 SYMBOL_TYPE(np->n_type) != N_UNDF)
438 * make n_un.n_name a character pointer by adding the string
439 * table's base to n_un.n_strx
441 * don't mess with zero offsets
444 np->n_un.n_name = stab + np->n_un.n_strx;
446 np->n_un.n_name = "";
447 names[nnames++] = *np;
450 /* sort the symbol table if applicable */
452 qsort((char *)names, (size_t)nnames, sizeof(*names), sfunc);
454 /* print out symbols */
455 for (np = names, i = 0; i < nnames; np++, i++)
456 print_symbol(objname, np);
458 (void)free((char *)names);
468 print_symbol(objname, sym)
470 register struct nlist *sym;
475 printf("%s|", objname);
476 if (SYMBOL_TYPE(sym->n_type) != N_UNDF)
477 (void)printf("%08lx", (u_long)sym->n_value);
479 if (IS_DEBUGGER_SYMBOL(sym->n_type))
480 (void)printf("-|%02x %04x %5s|", sym->n_other,
481 sym->n_desc&0xffff, typestring(sym->n_type));
483 putchar(typeletter(sym->n_type));
484 if (print_weak_symbols && SYMBOL_BIND(sym->n_other)== 2)
489 /* print the symbol's name */
490 (void)printf("%s\n",sym->n_un.n_name);
493 if (print_file_each_line)
494 (void)printf("%s:", objname);
497 * handle undefined-only format seperately (no space is
498 * left for symbol values, no type field is printed)
500 if (print_only_undefined_symbols) {
501 (void)puts(sym->n_un.n_name);
505 /* print symbol's value */
506 if (SYMBOL_TYPE(sym->n_type) == N_UNDF)
509 (void)printf("%08lx", (u_long)sym->n_value);
511 /* print type information */
512 if (IS_DEBUGGER_SYMBOL(sym->n_type))
513 (void)printf(" - %02x %04x %5s ", sym->n_other,
514 sym->n_desc&0xffff, typestring(sym->n_type));
517 putchar(typeletter(sym->n_type));
518 if (print_weak_symbols) {
519 if (SYMBOL_BIND(sym->n_other) == 2)
527 /* print the symbol's name */
528 (void)puts(sym->n_un.n_name);
533 * return the a description string for an STAB entry
537 register u_char type;
586 * return a description letter for the given basic type code of an
587 * symbol table entry. The return value will be upper case for
588 * external, lower case for internal symbols.
594 switch(SYMBOL_TYPE(type)) {
596 return(IS_EXTERNAL(type) ? 'A' : 'a');
598 return(IS_EXTERNAL(type) ? 'B' : 'b');
600 return(IS_EXTERNAL(type) ? 'C' : 'c');
602 return(IS_EXTERNAL(type) ? 'D' : 'd');
604 /* This one is overloaded. EXT = Filename, INT = warn refs */
605 return(IS_EXTERNAL(type) ? 'F' : 'w');
607 return(IS_EXTERNAL(type) ? 'I' : 'i');
609 return(IS_EXTERNAL(type) ? 'T' : 't');
611 return(IS_EXTERNAL(type) ? 'U' : 'u');
620 struct nlist *a = a0, *b = b0;
622 return(strcmp(a->n_un.n_name, b->n_un.n_name));
629 struct nlist *a = a0, *b = b0;
631 return(strcmp(b->n_un.n_name, a->n_un.n_name));
638 register struct nlist *a = a0, *b = b0;
640 if (SYMBOL_TYPE(a->n_type) == N_UNDF)
641 if (SYMBOL_TYPE(b->n_type) == N_UNDF)
645 else if (SYMBOL_TYPE(b->n_type) == N_UNDF)
648 if (a->n_value == b->n_value)
649 return(rname(a0, b0));
650 return(b->n_value > a->n_value ? 1 : -1);
652 if (a->n_value == b->n_value)
653 return(fname(a0, b0));
654 return(a->n_value > b->n_value ? 1 : -1);
665 if ( (p = malloc(size)) )
673 (void)fprintf(stderr, "usage: nm [-agnoprtuwW] [file ...]\n");