1 /* nm.c -- Describe symbol table of a rel file.
2 Copyright (C) 1991 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "aout/stab_gnu.h"
25 #include "aout/ranlib.h"
29 PROTO(static boolean, display_file, (char *filename));
30 PROTO(static void, do_one_rel_file, (bfd *file));
31 PROTO(static unsigned int, filter_symbols, (bfd *file, asymbol **syms,
32 unsigned long symcount));
34 PROTO(static void, print_symbols, (bfd *file, asymbol **syms,
35 unsigned long symcount));
36 extern PROTO(int, (*sorters[2][2]), (char *x, char *y));
37 PROTO(static void, print_symdef_entry, (bfd * abfd));
39 /* Command options. */
41 int external_only = 0; /* print external symbols only */
42 int file_on_each_line = 0; /* print file name on each line */
43 int no_sort = 0; /* don't sort; print syms in order found */
44 int print_debug_syms = 0; /* print debugger-only symbols too */
45 int print_armap = 0; /* describe __.SYMDEF data in archive files. */
46 int reverse_sort = 0; /* sort in downward(alpha or numeric) order */
47 int sort_numerically = 0; /* sort in numeric rather than alpha order */
48 int undefined_only = 0; /* print undefined symbols only */
50 boolean print_each_filename = false; /* Ick. Used in archives. */
53 extern char *program_name;
54 extern char *program_version;
57 struct option long_options[] = {
58 {"debug-syms", no_argument, &print_debug_syms, 1},
59 {"extern-only", no_argument, &external_only, 1},
60 {"no-sort", no_argument, &no_sort, 1},
61 {"numeric-sort", no_argument, &sort_numerically, 1},
62 {"print-armap", no_argument, &print_armap, 1},
63 {"print-file-name", no_argument, &file_on_each_line, 1},
64 {"reverse-sort", no_argument, &reverse_sort, 1},
65 {"target", optional_argument, (int *)NULL, 0},
66 {"undefined-only", no_argument, &undefined_only, 1},
67 {0, no_argument, 0, 0}
72 /* Some error-reporting functions */
77 fprintf(stderr, "nm %s\nUsage: %s [-agnoprsu] filename...\n",
78 program_version, program_name);
87 int c; /* sez which option char */
88 int option_index = 0; /* used by getopt and ignored by us */
94 while ((c = getopt_long(argc, argv, "agnoprsu", long_options, &option_index)) != EOF) {
96 case 'a': print_debug_syms = 1; break;
97 case 'g': external_only = 1; break;
98 case 'n': sort_numerically = 1; break;
99 case 'o': file_on_each_line = 1; break;
100 case 'p': no_sort = 1; break;
101 case 'r': reverse_sort = 1; break;
102 case 's': print_armap = 1; break;
103 case 'u': undefined_only = 1; break;
106 if (!strcmp("target",(long_options[option_index]).name)) {
110 break; /* we've been given a long option */
117 /* Strangely, for the shell you should return only a nonzero value
118 on sucess -- the inverse of the C sense. */
120 /* OK, all options now parsed. If no filename specified, do a.out. */
121 if (option_index == argc) return !display_file ("a.out");
124 show_names = (argc -optind)>1;
125 /* We were given several filenames to do: */
126 while (optind < argc) {
127 if (!display_file (argv[optind++])) {
135 /** Display a file's stats */
137 /* goto here is marginally cleaner than the nested if syntax */
140 display_file (filename)
143 boolean retval = true;
147 file = bfd_openr(filename, target);
149 fprintf (stderr, "\n%s: can't open '%s'.\n", program_name, filename);
156 if (bfd_check_format(file, bfd_object))
159 printf ("\n%s:\n",filename);
161 do_one_rel_file (file);
164 else if (bfd_check_format (file, bfd_archive)) {
165 if (!bfd_check_format (file, bfd_archive)) {
166 fprintf (stderr, "%s: %s: unknown format.\n", program_name, filename);
171 printf("\n%s:\n", filename);
172 if (print_armap) print_symdef_entry (file);
174 arfile = bfd_openr_next_archived_file (file, arfile);
176 if (arfile == NULL) {
177 if (bfd_error != no_more_archived_files)
178 bfd_fatal (filename);
182 if (!bfd_check_format(arfile, bfd_object))
183 printf("%s: not an object file\n", arfile->filename);
185 printf ("\n%s:\n", arfile->filename);
186 do_one_rel_file (arfile) ;
191 fprintf (stderr, "\n%s: %s: unknown format.\n", program_name, filename);
197 if (bfd_close(file) == false)
198 bfd_fatal (filename);
205 do_one_rel_file (abfd)
208 unsigned int storage;
210 unsigned int symcount = 0;
212 if (!(bfd_get_file_flags (abfd) & HAS_SYMS)) {
213 (void) printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
218 storage = get_symtab_upper_bound (abfd);
221 fprintf (stderr, "%s: Symflags set but there are none?\n",
222 bfd_get_filename (abfd));
226 syms = (asymbol **) xmalloc (storage);
228 symcount = bfd_canonicalize_symtab (abfd, syms);
229 if (symcount == 0) goto nosymz;
231 /* Discard the symbols we don't want to print.
232 It's OK to do this in place; we'll free the storage anyway
235 symcount = filter_symbols (abfd, syms, symcount);
238 qsort((char *) syms, symcount, sizeof (asymbol *),
239 sorters[sort_numerically][reverse_sort]);
241 if (print_each_filename && !file_on_each_line)
242 printf("\n%s:\n", bfd_get_filename(abfd));
244 print_symbols (abfd, syms, symcount);
249 /* Symbol-sorting predicates */
250 #define valueof(x) ((x)->section->vma + (x)->value)
252 numeric_forward (x, y)
257 return (valueof(*(asymbol **)x) - valueof(*(asymbol **) y));;
261 numeric_reverse (x, y)
265 return (valueof(*(asymbol **)y) - valueof(*(asymbol **) x));
270 non_numeric_forward (x, y)
274 CONST char *xn = (*(asymbol **) x)->name;
275 CONST char *yn = (*(asymbol **) y)->name;
277 return ((xn == NULL) ? ((yn == NULL) ? 0 : -1) :
278 ((yn == NULL) ? 1 : strcmp (xn, yn)));
282 non_numeric_reverse (x, y)
286 return -(non_numeric_forward (x, y));
289 int (*sorters[2][2])() = {
290 {non_numeric_forward, non_numeric_reverse},
291 {numeric_forward, numeric_reverse},
295 /* Choose which symbol entries to print;
296 compact them downward to get rid of the rest.
297 Return the number of symbols to be printed. */
299 filter_symbols (abfd, syms, symcount)
302 unsigned long symcount;
304 asymbol **from, **to;
305 unsigned int dst_count = 0;
308 unsigned int src_count;
309 for (from = to = syms, src_count = 0; src_count <symcount; src_count++) {
314 flagword flags = (from[src_count])->flags;
315 sym = from[src_count];
316 if (undefined_only) {
317 keep = sym->section == &bfd_und_section;
318 } else if (external_only) {
319 keep = ((flags & BSF_GLOBAL)
320 || (sym->section == &bfd_und_section)
321 || (sym->section == &bfd_com_section));
327 if (!print_debug_syms && ((flags & BSF_DEBUGGING) != 0)) {
332 to[dst_count++] = from[src_count];
340 print_symbols (abfd, syms, symcount)
343 unsigned long symcount;
345 asymbol **sym = syms, **end = syms + symcount;
347 for (; sym < end; ++sym) {
348 if (file_on_each_line) printf("%s:", bfd_get_filename(abfd));
350 if (undefined_only) {
351 if ((*sym)->section == &bfd_und_section)
357 bfd_print_symbol(abfd, stdout, p, bfd_print_symbol_nm);
365 print_symdef_entry (abfd)
368 symindex idx = BFD_NO_MORE_SYMBOLS;
370 boolean everprinted = false;
372 for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
373 idx != BFD_NO_MORE_SYMBOLS;
374 idx = bfd_get_next_mapent (abfd, idx, &thesym)) {
377 printf ("\nArchive index:\n");
380 elt = bfd_get_elt_at_index (abfd, idx);
381 if (thesym->name != (char *)NULL) {
382 printf ("%s in %s\n", thesym->name, bfd_get_filename (elt));