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
36 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)nm.c 8.1 (Berkeley) 6/6/93
38 * $FreeBSD: src/usr.bin/nm/nm.c,v 1.13.2.1 2001/03/04 08:54:10 kris Exp $
39 * $DragonFly: src/usr.bin/nm/Attic/nm.c,v 1.2 2003/06/17 04:29:30 dillon Exp $
42 #include <sys/types.h>
55 int ignore_bad_archive_entries = 1;
56 int print_only_external_symbols;
57 int print_only_undefined_symbols;
58 int print_all_symbols;
59 int print_file_each_line;
60 int print_weak_symbols;
64 int fname(), rname(), value();
65 int (*sfunc)() = fname;
67 /* some macros for symbol type (nlist.n_type) handling */
68 #define IS_DEBUGGER_SYMBOL(x) ((x) & N_STAB)
69 #define IS_EXTERNAL(x) ((x) & N_EXT)
70 #define SYMBOL_TYPE(x) ((x) & (N_TYPE | N_STAB))
71 #define SYMBOL_BIND(x) (((x) >> 4) & 0xf)
74 static void usage __P(( void ));
75 int process_file __P(( char * ));
76 int show_archive __P(( char *, FILE * ));
77 int show_objfile __P(( char *, FILE * ));
78 void print_symbol __P(( char *, struct nlist * ));
79 char typeletter __P((u_char));
83 * parse command line, execute process_file() for each file
84 * specified on the command line.
93 while ((ch = getopt(argc, argv, "agnoprtuwW")) != -1) {
96 print_all_symbols = 1;
99 print_only_external_symbols = 1;
105 print_file_each_line = 1;
117 print_only_undefined_symbols = 1;
120 ignore_bad_archive_entries = 0;
123 print_weak_symbols = 1;
130 fcount = argc - optind;
133 if (rev && sfunc == fname)
137 errors = process_file("a.out");
141 errors |= process_file(*argv);
149 * show symbols in the file given as an argument. Accepts archive and
150 * object files as input.
156 struct exec exec_head;
161 if (!(fp = fopen(fname, "r"))) {
162 warnx("cannot read %s", fname);
166 if (fcount > 1 && !table)
167 (void)printf("\n%s:\n", fname);
170 * first check whether this is an object file - read a object
171 * header, and skip back to the beginning
173 if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
174 warnx("%s: bad format", fname);
180 /* this could be an archive */
181 if (N_BADMAG(exec_head)) {
182 if (fread(magic, sizeof(magic), (size_t)1, fp) != 1 ||
183 strncmp(magic, ARMAG, SARMAG)) {
184 warnx("%s: not object file or archive", fname);
188 retval = show_archive(fname, fp);
190 retval = show_objfile(fname, fp);
195 /* scat: concatenate strings, returning new concatenation point
196 * and permitting overlap.
198 static char *scat(char *dest, char *src)
201 int l1 = strlen(dest), l2 = strlen(src);
203 memmove(dest + l1, src, l2);
204 end = dest + l1 + l2;
212 * show symbols in the given archive file
215 show_archive(fname, fp)
219 struct ar_hdr ar_head;
220 struct exec exec_head;
223 char *p, *name, *ar_name;
224 int extra = strlen(fname) + 3;
226 name = emalloc(MAXNAMLEN + extra);
227 ar_name = name + extra;
231 /* while there are more entries in the archive */
232 while (fread((char *)&ar_head, sizeof(ar_head), (size_t)1, fp) == 1) {
233 /* bad archive entry - stop processing this archive */
234 if (strncmp(ar_head.ar_fmag, ARFMAG, sizeof(ar_head.ar_fmag))) {
235 warnx("%s: bad format archive header", fname);
240 /* remember start position of current archive object */
241 last_ar_off = ftell(fp);
243 /* skip ranlib entries */
244 if (!bcmp(ar_head.ar_name, RANLIBMAG, sizeof(RANLIBMAG) - 1))
247 /* handle long names. If one is present, read it in at the
248 * end of the "name" buffer.
250 if (!bcmp(ar_head.ar_name, AR_EFMT1, sizeof(AR_EFMT1) - 1))
252 size_t len = atoi(ar_head.ar_name + sizeof(AR_EFMT1) - 1);
254 if (len <= 0 || len > MAXNAMLEN)
256 warnx("illegal length for format 1 long name");
259 if (fread(ar_name, 1, len, fp) != len)
261 warnx("EOF reading format 1 long name");
270 for (i = 0; i < sizeof(ar_head.ar_name) && p[i] && p[i] != ' '; i++)
276 * construct a name of the form "archive.a:obj.o:" for the
277 * current archive entry if the object name is to be printed
278 * on each output line
282 if (print_file_each_line && !table)
287 p = scat(p, ar_name);
289 /* get and check current object's header */
290 if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
291 warnx("%s: premature EOF", name);
296 if (N_BADMAG(exec_head)) {
297 if (!ignore_bad_archive_entries) {
298 warnx("%s: bad format", name);
302 (void)fseek(fp, -(long)sizeof(exec_head),
304 if (!print_file_each_line && !table)
305 (void)printf("\n%s:\n", name);
306 rval |= show_objfile(name, fp);
310 * skip to next archive object - it starts at the next
313 #define even(x) (((x) + 1) & ~1)
314 skip: if (fseek(fp, last_ar_off + even(atol(ar_head.ar_size)),
327 * show symbols from the object file pointed to by fp. The current
328 * file pointer for fp is expected to be at the beginning of an a.out
332 show_objfile(objname, fp)
336 register struct nlist *names, *np;
337 register int i, nnames, nrawnames;
342 /* read a.out header */
343 if (fread((char *)&head, sizeof(head), (size_t)1, fp) != 1) {
344 warnx("%s: cannot read header", objname);
349 * skip back to the header - the N_-macros return values relative
350 * to the beginning of the a.out header
352 if (fseek(fp, -(long)sizeof(head), SEEK_CUR)) {
357 /* stop if this is no valid object file */
358 if (N_BADMAG(head)) {
359 warnx("%s: bad format", objname);
363 /* stop if the object file contains no symbol table */
365 warnx("%s: no name list", objname);
369 if (fseek(fp, (long)N_SYMOFF(head), SEEK_CUR)) {
374 /* get memory for the symbol table */
375 names = emalloc((size_t)head.a_syms);
376 nrawnames = head.a_syms / sizeof(*names);
377 if (fread((char *)names, (size_t)head.a_syms, (size_t)1, fp) != 1) {
378 warnx("%s: cannot read symbol table", objname);
379 (void)free((char *)names);
384 * Following the symbol table comes the string table. The first
385 * 4-byte-integer gives the total size of the string table
386 * _including_ the size specification itself.
388 if (fread((char *)&stabsize, sizeof(stabsize), (size_t)1, fp) != 1) {
389 warnx("%s: cannot read stab size", objname);
390 (void)free((char *)names);
393 stab = emalloc((size_t)stabsize);
396 * read the string table offset by 4 - all indices into the string
397 * table include the size specification.
399 stabsize -= 4; /* we already have the size */
400 if (fread(stab + 4, (size_t)stabsize, (size_t)1, fp) != 1) {
401 warnx("%s: stab truncated..", objname);
402 (void)free((char *)names);
408 * fix up the symbol table and filter out unwanted entries
410 * common symbols are characterized by a n_type of N_UNDF and a
411 * non-zero n_value -- change n_type to N_COMM for all such
412 * symbols to make life easier later.
414 * filter out all entries which we don't want to print anyway
416 for (np = names, i = nnames = 0; i < nrawnames; np++, i++) {
417 if (SYMBOL_TYPE(np->n_type) == N_UNDF && np->n_value)
418 np->n_type = N_COMM | (np->n_type & N_EXT);
419 if (!print_all_symbols && IS_DEBUGGER_SYMBOL(np->n_type))
421 if (print_only_external_symbols && !IS_EXTERNAL(np->n_type))
423 if (print_only_undefined_symbols &&
424 SYMBOL_TYPE(np->n_type) != N_UNDF)
428 * make n_un.n_name a character pointer by adding the string
429 * table's base to n_un.n_strx
431 * don't mess with zero offsets
434 np->n_un.n_name = stab + np->n_un.n_strx;
436 np->n_un.n_name = "";
437 names[nnames++] = *np;
440 /* sort the symbol table if applicable */
442 qsort((char *)names, (size_t)nnames, sizeof(*names), sfunc);
444 /* print out symbols */
445 for (np = names, i = 0; i < nnames; np++, i++)
446 print_symbol(objname, np);
448 (void)free((char *)names);
458 print_symbol(objname, sym)
460 register struct nlist *sym;
465 printf("%s|", objname);
466 if (SYMBOL_TYPE(sym->n_type) != N_UNDF)
467 (void)printf("%08lx", (u_long)sym->n_value);
469 if (IS_DEBUGGER_SYMBOL(sym->n_type))
470 (void)printf("-|%02x %04x %5s|", sym->n_other,
471 sym->n_desc&0xffff, typestring(sym->n_type));
473 putchar(typeletter(sym->n_type));
474 if (print_weak_symbols && SYMBOL_BIND(sym->n_other)== 2)
479 /* print the symbol's name */
480 (void)printf("%s\n",sym->n_un.n_name);
483 if (print_file_each_line)
484 (void)printf("%s:", objname);
487 * handle undefined-only format separately (no space is
488 * left for symbol values, no type field is printed)
490 if (print_only_undefined_symbols) {
491 (void)puts(sym->n_un.n_name);
495 /* print symbol's value */
496 if (SYMBOL_TYPE(sym->n_type) == N_UNDF)
499 (void)printf("%08lx", (u_long)sym->n_value);
501 /* print type information */
502 if (IS_DEBUGGER_SYMBOL(sym->n_type))
503 (void)printf(" - %02x %04x %5s ", sym->n_other,
504 sym->n_desc&0xffff, typestring(sym->n_type));
507 putchar(typeletter(sym->n_type));
508 if (print_weak_symbols) {
509 if (SYMBOL_BIND(sym->n_other) == 2)
517 /* print the symbol's name */
518 (void)puts(sym->n_un.n_name);
523 * return the a description string for an STAB entry
527 register u_char type;
576 * return a description letter for the given basic type code of an
577 * symbol table entry. The return value will be upper case for
578 * external, lower case for internal symbols.
584 switch(SYMBOL_TYPE(type)) {
586 return(IS_EXTERNAL(type) ? 'A' : 'a');
588 return(IS_EXTERNAL(type) ? 'B' : 'b');
590 return(IS_EXTERNAL(type) ? 'C' : 'c');
592 return(IS_EXTERNAL(type) ? 'D' : 'd');
594 /* This one is overloaded. EXT = Filename, INT = warn refs */
595 return(IS_EXTERNAL(type) ? 'F' : 'w');
597 return(IS_EXTERNAL(type) ? 'I' : 'i');
599 return(IS_EXTERNAL(type) ? 'T' : 't');
601 return(IS_EXTERNAL(type) ? 'U' : 'u');
610 struct nlist *a = a0, *b = b0;
612 return(strcmp(a->n_un.n_name, b->n_un.n_name));
619 struct nlist *a = a0, *b = b0;
621 return(strcmp(b->n_un.n_name, a->n_un.n_name));
628 register struct nlist *a = a0, *b = b0;
630 if (SYMBOL_TYPE(a->n_type) == N_UNDF)
631 if (SYMBOL_TYPE(b->n_type) == N_UNDF)
635 else if (SYMBOL_TYPE(b->n_type) == N_UNDF)
638 if (a->n_value == b->n_value)
639 return(rname(a0, b0));
640 return(b->n_value > a->n_value ? 1 : -1);
642 if (a->n_value == b->n_value)
643 return(fname(a0, b0));
644 return(a->n_value > b->n_value ? 1 : -1);
655 if ( (p = malloc(size)) )
663 (void)fprintf(stderr, "usage: nm [-agnoprtuwW] [file ...]\n");