1 /* nm.c -- Describe symbol table of a rel file.
2 Copyright 1991, 92, 93, 94 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 display_file PARAMS ((char *filename));
32 display_rel_file PARAMS ((bfd * file, bfd * archive));
35 filter_symbols PARAMS ((bfd * file, asymbol ** syms, unsigned long symcount));
38 print_symbols PARAMS ((bfd * file, asymbol ** syms, unsigned long symcount,
42 print_symdef_entry PARAMS ((bfd * abfd));
45 /* The output formatting functions. */
48 print_object_filename_bsd PARAMS ((char *filename));
51 print_object_filename_sysv PARAMS ((char *filename));
54 print_object_filename_posix PARAMS ((char *filename));
58 print_archive_filename_bsd PARAMS ((char *filename));
61 print_archive_filename_sysv PARAMS ((char *filename));
64 print_archive_filename_posix PARAMS ((char *filename));
68 print_archive_member_bsd PARAMS ((char *archive, CONST char *filename));
71 print_archive_member_sysv PARAMS ((char *archive, CONST char *filename));
74 print_archive_member_posix PARAMS ((char *archive, CONST char *filename));
78 print_symbol_filename_bsd PARAMS ((bfd * archive_bfd, bfd * abfd));
81 print_symbol_filename_sysv PARAMS ((bfd * archive_bfd, bfd * abfd));
84 print_symbol_filename_posix PARAMS ((bfd * archive_bfd, bfd * abfd));
88 print_symbol_info_bsd PARAMS ((symbol_info * info, bfd * abfd));
91 print_symbol_info_sysv PARAMS ((symbol_info * info, bfd * abfd));
94 print_symbol_info_posix PARAMS ((symbol_info * info, bfd * abfd));
97 /* Support for different output formats. */
100 /* Print the name of an object file given on the command line. */
101 void (*print_object_filename) PARAMS ((char *filename));
103 /* Print the name of an archive file given on the command line. */
104 void (*print_archive_filename) PARAMS ((char *filename));
106 /* Print the name of an archive member file. */
107 void (*print_archive_member) PARAMS ((char *archive, CONST char *filename));
109 /* Print the name of the file (and archive, if there is one)
110 containing a symbol. */
111 void (*print_symbol_filename) PARAMS ((bfd * archive_bfd, bfd * abfd));
113 /* Print a line of information about a symbol. */
114 void (*print_symbol_info) PARAMS ((symbol_info * info, bfd * abfd));
116 static struct output_fns formats[] =
118 {print_object_filename_bsd,
119 print_archive_filename_bsd,
120 print_archive_member_bsd,
121 print_symbol_filename_bsd,
122 print_symbol_info_bsd},
123 {print_object_filename_sysv,
124 print_archive_filename_sysv,
125 print_archive_member_sysv,
126 print_symbol_filename_sysv,
127 print_symbol_info_sysv},
128 {print_object_filename_posix,
129 print_archive_filename_posix,
130 print_archive_member_posix,
131 print_symbol_filename_posix,
132 print_symbol_info_posix}
135 /* Indices in `formats'. */
137 #define FORMAT_SYSV 1
138 #define FORMAT_POSIX 2
139 #define FORMAT_DEFAULT FORMAT_BSD
141 /* The output format to use. */
142 static struct output_fns *format = &formats[FORMAT_DEFAULT];
145 /* Command options. */
147 static int do_demangle = 0; /* Pretty print C++ symbol names. */
148 static int external_only = 0; /* print external symbols only */
149 static int no_sort = 0; /* don't sort; print syms in order found */
150 static int print_debug_syms = 0; /* print debugger-only symbols too */
151 static int print_armap = 0; /* describe __.SYMDEF data in archive files. */
152 static int reverse_sort = 0; /* sort in downward(alpha or numeric) order */
153 static int sort_numerically = 0; /* sort in numeric rather than alpha order */
154 static int undefined_only = 0; /* print undefined symbols only */
155 static int dynamic = 0; /* print dynamic symbols. */
156 static int show_version = 0; /* show the version number */
158 /* When to print the names of files. Not mutually exclusive in SYSV format. */
159 static int filename_per_file = 0; /* Once per file, on its own line. */
160 static int filename_per_symbol = 0; /* Once per symbol, at start of line. */
162 /* Print formats for printing a symbol value. */
163 #ifdef BFD_HOST_64_BIT
164 static char value_format[] = "%08x%08x";
166 static char value_format[] = "%08lx";
168 /* Print formats for printing stab info. */
169 static char other_format[] = "%02x";
170 static char desc_format[] = "%04x";
173 extern char *program_name;
174 extern char *program_version;
176 extern int print_version;
178 static struct option long_options[] =
180 {"debug-syms", no_argument, &print_debug_syms, 1},
181 {"demangle", no_argument, &do_demangle, 1},
182 {"dynamic", no_argument, &dynamic, 1},
183 {"extern-only", no_argument, &external_only, 1},
184 {"format", required_argument, 0, 'f'},
185 {"help", no_argument, 0, 'h'},
186 {"no-cplus", no_argument, &do_demangle, 0}, /* Linux compatibility. */
187 {"no-demangle", no_argument, &do_demangle, 0},
188 {"no-sort", no_argument, &no_sort, 1},
189 {"numeric-sort", no_argument, &sort_numerically, 1},
190 {"portability", no_argument, 0, 'P'},
191 {"print-armap", no_argument, &print_armap, 1},
192 {"print-file-name", no_argument, 0, 'o'},
193 {"radix", required_argument, 0, 't'},
194 {"reverse-sort", no_argument, &reverse_sort, 1},
195 {"target", required_argument, 0, 200},
196 {"undefined-only", no_argument, &undefined_only, 1},
197 {"version", no_argument, &show_version, 1},
198 {0, no_argument, 0, 0}
201 /* Some error-reporting functions */
204 usage (stream, status)
209 Usage: %s [-aABCDgnopPrsuvV] [-t radix] [--radix=radix] [--target=bfdname]\n\
210 [--debug-syms] [--extern-only] [--print-armap] [--print-file-name]\n\
211 [--numeric-sort] [--no-sort] [--reverse-sort] [--undefined-only]\n\
212 [--portability] [-f {bsd,sysv,posix}] [--format={bsd,sysv,posix}]\n\
213 [--demangle] [--no-demangle] [--dynamic] [--version] [--help]\n\
219 /* Set the radix for the symbol value and size according to RADIX. */
222 set_print_radix (radix)
230 #ifdef BFD_HOST_64_BIT
231 value_format[3] = value_format[7] = *radix;
233 value_format[4] = *radix;
235 other_format[3] = desc_format[3] = *radix;
238 fprintf (stderr, "%s: %s: invalid radix\n", program_name, radix);
244 set_output_format (f)
264 fprintf (stderr, "%s: %s: invalid output format\n", program_name, f);
267 format = &formats[i];
278 program_name = *argv;
279 xmalloc_set_program_name (program_name);
283 while ((c = getopt_long (argc, argv, "aABCDf:gnopPrst:uvV", long_options, (int *) 0)) != EOF)
288 print_debug_syms = 1;
292 filename_per_symbol = 1;
294 case 'B': /* For MIPS compatibility. */
295 set_output_format ("bsd");
304 set_output_format (optarg);
313 sort_numerically = 1;
319 set_output_format ("posix");
328 set_print_radix (optarg);
337 case 200: /* --target */
341 case 0: /* A long option that just sets a flag. */
351 printf ("GNU %s version %s\n", program_name, program_version);
355 /* OK, all options now parsed. If no filename specified, do a.out. */
357 return !display_file ("a.out");
361 if (argc - optind > 1)
362 filename_per_file = 1;
364 /* We were given several filenames to do. */
365 while (optind < argc)
367 if (!display_file (argv[optind++]))
376 display_archive (file)
380 bfd *last_arfile = NULL;
383 (*format->print_archive_filename) (bfd_get_filename (file));
386 print_symdef_entry (file);
390 arfile = bfd_openr_next_archived_file (file, arfile);
394 if (bfd_get_error () != bfd_error_no_more_archived_files)
395 bfd_fatal (bfd_get_filename (file));
399 if (bfd_check_format_matches (arfile, bfd_object, &matching))
401 (*format->print_archive_member) (bfd_get_filename (file),
402 bfd_get_filename (arfile));
403 display_rel_file (arfile, file);
407 bfd_nonfatal (bfd_get_filename (arfile));
408 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
410 list_matching_formats (matching);
415 if (last_arfile != NULL)
416 bfd_close (last_arfile);
417 last_arfile = arfile;
420 if (last_arfile != NULL)
421 bfd_close (last_arfile);
425 display_file (filename)
428 boolean retval = true;
432 file = bfd_openr (filename, target);
435 bfd_nonfatal (filename);
439 if (bfd_check_format (file, bfd_archive))
441 display_archive (file);
443 else if (bfd_check_format_matches (file, bfd_object, &matching))
445 (*format->print_object_filename) (filename);
446 display_rel_file (file, NULL);
450 bfd_nonfatal (filename);
451 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
453 list_matching_formats (matching);
459 if (bfd_close (file) == false)
460 bfd_fatal (filename);
465 /* Symbol-sorting predicates */
466 #define valueof(x) ((x)->section->vma + (x)->value)
468 numeric_forward (x, y)
472 return (valueof (*(asymbol **) x) - valueof (*(asymbol **) y));
476 numeric_reverse (x, y)
480 return (valueof (*(asymbol **) y) - valueof (*(asymbol **) x));
484 non_numeric_forward (x, y)
488 CONST char *xn = (*(asymbol **) x)->name;
489 CONST char *yn = (*(asymbol **) y)->name;
491 return ((xn == NULL) ? ((yn == NULL) ? 0 : -1) :
492 ((yn == NULL) ? 1 : strcmp (xn, yn)));
496 non_numeric_reverse (x, y)
500 return -(non_numeric_forward (x, y));
503 static int (*(sorters[2][2])) PARAMS ((CONST void *, CONST void *)) =
505 { non_numeric_forward, non_numeric_reverse },
506 { numeric_forward, numeric_reverse }
509 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
512 display_rel_file (abfd, archive_bfd)
522 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
524 printf ("\"%s\" is not a dynamic object.\n",
525 bfd_get_filename (abfd));
531 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
533 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
539 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
541 storage = bfd_get_symtab_upper_bound (abfd);
543 bfd_fatal (bfd_get_filename (abfd));
548 fprintf (stderr, "%s: no symbols\n", bfd_get_filename (abfd));
550 fprintf (stderr, "%s: Symflags set but there are none?\n",
551 bfd_get_filename (abfd));
555 syms = (asymbol **) xmalloc (storage);
558 symcount = bfd_canonicalize_dynamic_symtab (abfd, syms);
560 symcount = bfd_canonicalize_symtab (abfd, syms);
562 bfd_fatal (bfd_get_filename (abfd));
569 /* Discard the symbols we don't want to print.
570 It's OK to do this in place; we'll free the storage anyway
573 symcount = filter_symbols (abfd, syms, symcount);
576 qsort ((char *) syms, symcount, sizeof (asymbol *),
577 sorters[sort_numerically][reverse_sort]);
579 print_symbols (abfd, syms, symcount, archive_bfd);
583 /* Choose which symbol entries to print;
584 compact them downward to get rid of the rest.
585 Return the number of symbols to be printed. */
588 filter_symbols (abfd, syms, symcount)
589 bfd *abfd; /* Unused. */
591 unsigned long symcount;
593 asymbol **from, **to;
594 unsigned int src_count;
595 unsigned int dst_count = 0;
598 for (from = to = syms, src_count = 0; src_count < symcount; src_count++)
601 flagword flags = (from[src_count])->flags;
603 sym = from[src_count];
605 keep = sym->section == &bfd_und_section;
606 else if (external_only)
607 keep = ((flags & BSF_GLOBAL)
608 || (sym->section == &bfd_und_section)
609 || (bfd_is_com_section (sym->section)));
613 if (!print_debug_syms && ((flags & BSF_DEBUGGING) != 0))
617 to[dst_count++] = from[src_count];
623 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
624 demangling it if requested. */
627 print_symname (format, name, abfd)
635 /* In this mode, give a user-level view of the symbol name
636 even if it's not mangled; strip off any leading
638 if (bfd_get_symbol_leading_char (abfd) == name[0])
641 res = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
644 printf (format, res);
650 printf (format, name);
653 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
656 print_symbols (abfd, syms, symcount, archive_bfd)
659 unsigned long symcount;
662 asymbol **sym = syms, **end = syms + symcount;
665 for (; sym < end; ++sym)
667 (*format->print_symbol_filename) (archive_bfd, abfd);
671 if ((*sym)->section == &bfd_und_section)
673 print_symname ("%s\n", (*sym)->name, abfd);
681 bfd_get_symbol_info (abfd, p, &syminfo);
682 (*format->print_symbol_info) (&syminfo, abfd);
689 /* The following 3 groups of functions are called unconditionally,
690 once at the start of processing each file of the appropriate type.
691 They should check `filename_per_file' and `filename_per_symbol',
692 as appropriate for their output format, to determine whether to
695 /* Print the name of an object file given on the command line. */
698 print_object_filename_bsd (filename)
701 if (filename_per_file && !filename_per_symbol)
702 printf ("\n%s:\n", filename);
706 print_object_filename_sysv (filename)
710 printf ("\n\nUndefined symbols from %s:\n\n", filename);
712 printf ("\n\nSymbols from %s:\n\n", filename);
714 Name Value Class Type Size Line Section\n\n");
718 print_object_filename_posix (filename)
721 if (filename_per_file && !filename_per_symbol)
722 printf ("%s:\n", filename);
725 /* Print the name of an archive file given on the command line. */
728 print_archive_filename_bsd (filename)
731 if (filename_per_file)
732 printf ("\n%s:\n", filename);
736 print_archive_filename_sysv (filename)
742 print_archive_filename_posix (filename)
747 /* Print the name of an archive member file. */
750 print_archive_member_bsd (archive, filename)
752 CONST char *filename;
754 if (!filename_per_symbol)
755 printf ("\n%s:\n", filename);
759 print_archive_member_sysv (archive, filename)
761 CONST char *filename;
764 printf ("\n\nUndefined symbols from %s[%s]:\n\n", archive, filename);
766 printf ("\n\nSymbols from %s[%s]:\n\n", archive, filename);
768 Name Value Class Type Size Line Section\n\n");
772 print_archive_member_posix (archive, filename)
774 CONST char *filename;
776 if (!filename_per_symbol)
777 printf ("%s[%s]:\n", archive, filename);
780 /* Print the name of the file (and archive, if there is one)
781 containing a symbol. */
784 print_symbol_filename_bsd (archive_bfd, abfd)
785 bfd *archive_bfd, *abfd;
787 if (filename_per_symbol)
790 printf ("%s:", bfd_get_filename (archive_bfd));
791 printf ("%s:", bfd_get_filename (abfd));
796 print_symbol_filename_sysv (archive_bfd, abfd)
797 bfd *archive_bfd, *abfd;
799 if (filename_per_symbol)
802 printf ("%s:", bfd_get_filename (archive_bfd));
803 printf ("%s:", bfd_get_filename (abfd));
808 print_symbol_filename_posix (archive_bfd, abfd)
809 bfd *archive_bfd, *abfd;
811 if (filename_per_symbol)
814 printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
815 bfd_get_filename (abfd));
817 printf ("%s: ", bfd_get_filename (abfd));
821 /* Print a line of information about a symbol. */
824 print_symbol_info_bsd (info, abfd)
828 if (info->type == 'U')
832 #ifdef BFD_HOST_64_BIT
833 printf (value_format, uint64_typeHIGH (info->value),
834 uint64_typeLOW (info->value));
836 printf (value_format, info->value);
839 printf (" %c", info->type);
840 if (info->type == '-')
844 printf (other_format, info->stab_other);
846 printf (desc_format, info->stab_desc);
847 printf (" %5s", info->stab_name);
849 print_symname (" %s", info->name, abfd);
853 print_symbol_info_sysv (info, abfd)
857 print_symname ("%-20s|", info->name, abfd); /* Name */
858 if (info->type == 'U')
859 printf (" "); /* Value */
862 #ifdef BFD_HOST_64_BIT
863 printf (value_format, uint64_typeHIGH (info->value),
864 uint64_typeLOW (info->value));
866 printf (value_format, info->value);
869 printf ("| %c |", info->type); /* Class */
870 if (info->type == '-')
873 printf ("%18s| ", info->stab_name); /* (C) Type */
874 printf (desc_format, info->stab_desc); /* Size */
875 printf ("| |"); /* Line, Section */
878 printf (" | | |"); /* Type, Size, Line, Section */
882 print_symbol_info_posix (info, abfd)
886 print_symname ("%s ", info->name, abfd);
887 printf ("%c ", info->type);
888 if (info->type == 'U')
892 #ifdef BFD_HOST_64_BIT
893 printf (value_format, uint64_typeHIGH (info->value),
894 uint64_typeLOW (info->value));
896 printf (value_format, info->value);
899 /* POSIX.2 wants the symbol size printed here, when applicable;
900 BFD currently doesn't provide it, so we take the easy way out by
901 considering it to never be applicable. */
905 print_symdef_entry (abfd)
908 symindex idx = BFD_NO_MORE_SYMBOLS;
910 boolean everprinted = false;
912 for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
913 idx != BFD_NO_MORE_SYMBOLS;
914 idx = bfd_get_next_mapent (abfd, idx, &thesym))
919 printf ("\nArchive index:\n");
922 elt = bfd_get_elt_at_index (abfd, idx);
923 if (thesym->name != (char *) NULL)
925 printf ("%s in %s\n", thesym->name, bfd_get_filename (elt));