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. */
25 #include "aout/stab_gnu.h"
26 #include "aout/ranlib.h"
28 #include "libiberty.h"
31 display_file PARAMS ((char *filename));
34 display_rel_file PARAMS ((bfd * file, bfd * archive));
37 filter_symbols PARAMS ((bfd * file, asymbol ** syms, unsigned long symcount));
40 sort_symbols_by_size PARAMS ((bfd *, asymbol **, unsigned long));
43 print_symbols PARAMS ((bfd * file, asymbol ** syms, unsigned long symcount,
47 print_symdef_entry PARAMS ((bfd * abfd));
49 /* The sorting functions. */
52 numeric_forward PARAMS ((const PTR, const PTR));
55 numeric_reverse PARAMS ((const PTR, const PTR));
58 non_numeric_forward PARAMS ((const PTR, const PTR));
61 non_numeric_reverse PARAMS ((const PTR, const PTR));
64 size_forward PARAMS ((const PTR, const PTR));
66 /* The output formatting functions. */
69 print_object_filename_bsd PARAMS ((char *filename));
72 print_object_filename_sysv PARAMS ((char *filename));
75 print_object_filename_posix PARAMS ((char *filename));
79 print_archive_filename_bsd PARAMS ((char *filename));
82 print_archive_filename_sysv PARAMS ((char *filename));
85 print_archive_filename_posix PARAMS ((char *filename));
89 print_archive_member_bsd PARAMS ((char *archive, CONST char *filename));
92 print_archive_member_sysv PARAMS ((char *archive, CONST char *filename));
95 print_archive_member_posix PARAMS ((char *archive, CONST char *filename));
99 print_symbol_filename_bsd PARAMS ((bfd * archive_bfd, bfd * abfd));
102 print_symbol_filename_sysv PARAMS ((bfd * archive_bfd, bfd * abfd));
105 print_symbol_filename_posix PARAMS ((bfd * archive_bfd, bfd * abfd));
109 print_symbol_info_bsd PARAMS ((symbol_info * info, bfd * abfd));
112 print_symbol_info_sysv PARAMS ((symbol_info * info, bfd * abfd));
115 print_symbol_info_posix PARAMS ((symbol_info * info, bfd * abfd));
118 /* Support for different output formats. */
121 /* Print the name of an object file given on the command line. */
122 void (*print_object_filename) PARAMS ((char *filename));
124 /* Print the name of an archive file given on the command line. */
125 void (*print_archive_filename) PARAMS ((char *filename));
127 /* Print the name of an archive member file. */
128 void (*print_archive_member) PARAMS ((char *archive, CONST char *filename));
130 /* Print the name of the file (and archive, if there is one)
131 containing a symbol. */
132 void (*print_symbol_filename) PARAMS ((bfd * archive_bfd, bfd * abfd));
134 /* Print a line of information about a symbol. */
135 void (*print_symbol_info) PARAMS ((symbol_info * info, bfd * abfd));
137 static struct output_fns formats[] =
139 {print_object_filename_bsd,
140 print_archive_filename_bsd,
141 print_archive_member_bsd,
142 print_symbol_filename_bsd,
143 print_symbol_info_bsd},
144 {print_object_filename_sysv,
145 print_archive_filename_sysv,
146 print_archive_member_sysv,
147 print_symbol_filename_sysv,
148 print_symbol_info_sysv},
149 {print_object_filename_posix,
150 print_archive_filename_posix,
151 print_archive_member_posix,
152 print_symbol_filename_posix,
153 print_symbol_info_posix}
156 /* Indices in `formats'. */
158 #define FORMAT_SYSV 1
159 #define FORMAT_POSIX 2
160 #define FORMAT_DEFAULT FORMAT_BSD
162 /* The output format to use. */
163 static struct output_fns *format = &formats[FORMAT_DEFAULT];
166 /* Command options. */
168 static int do_demangle = 0; /* Pretty print C++ symbol names. */
169 static int external_only = 0; /* print external symbols only */
170 static int no_sort = 0; /* don't sort; print syms in order found */
171 static int print_debug_syms = 0; /* print debugger-only symbols too */
172 static int print_armap = 0; /* describe __.SYMDEF data in archive files. */
173 static int reverse_sort = 0; /* sort in downward(alpha or numeric) order */
174 static int sort_numerically = 0; /* sort in numeric rather than alpha order */
175 static int sort_by_size = 0; /* sort by size of symbol */
176 static int undefined_only = 0; /* print undefined symbols only */
177 static int dynamic = 0; /* print dynamic symbols. */
178 static int show_version = 0; /* show the version number */
180 /* When to print the names of files. Not mutually exclusive in SYSV format. */
181 static int filename_per_file = 0; /* Once per file, on its own line. */
182 static int filename_per_symbol = 0; /* Once per symbol, at start of line. */
184 /* Print formats for printing a symbol value. */
185 #ifdef BFD_HOST_64_BIT
186 static char value_format[] = "%08x%08x";
188 static char value_format[] = "%08lx";
190 /* Print formats for printing stab info. */
191 static char other_format[] = "%02x";
192 static char desc_format[] = "%04x";
195 extern char *program_name;
196 extern char *program_version;
198 extern int print_version;
200 static struct option long_options[] =
202 {"debug-syms", no_argument, &print_debug_syms, 1},
203 {"demangle", no_argument, &do_demangle, 1},
204 {"dynamic", no_argument, &dynamic, 1},
205 {"extern-only", no_argument, &external_only, 1},
206 {"format", required_argument, 0, 'f'},
207 {"help", no_argument, 0, 'h'},
208 {"no-cplus", no_argument, &do_demangle, 0}, /* Linux compatibility. */
209 {"no-demangle", no_argument, &do_demangle, 0},
210 {"no-sort", no_argument, &no_sort, 1},
211 {"numeric-sort", no_argument, &sort_numerically, 1},
212 {"portability", no_argument, 0, 'P'},
213 {"print-armap", no_argument, &print_armap, 1},
214 {"print-file-name", no_argument, 0, 'o'},
215 {"radix", required_argument, 0, 't'},
216 {"reverse-sort", no_argument, &reverse_sort, 1},
217 {"size-sort", no_argument, &sort_by_size, 1},
218 {"target", required_argument, 0, 200},
219 {"undefined-only", no_argument, &undefined_only, 1},
220 {"version", no_argument, &show_version, 1},
221 {0, no_argument, 0, 0}
224 /* Some error-reporting functions */
227 usage (stream, status)
232 Usage: %s [-aABCDgnopPrsuvV] [-t radix] [--radix=radix] [--target=bfdname]\n\
233 [--debug-syms] [--extern-only] [--print-armap] [--print-file-name]\n\
234 [--numeric-sort] [--no-sort] [--reverse-sort] [--size-sort]\n\
235 [--undefined-only] [--portability] [-f {bsd,sysv,posix}]\n\
236 [--format={bsd,sysv,posix}] [--demangle] [--no-demangle] [--dynamic]\n\
237 [--version] [--help]\n\
240 list_supported_targets (program_name, stream);
244 /* Set the radix for the symbol value and size according to RADIX. */
247 set_print_radix (radix)
255 #ifdef BFD_HOST_64_BIT
256 value_format[3] = value_format[7] = *radix;
258 value_format[4] = *radix;
260 other_format[3] = desc_format[3] = *radix;
263 fprintf (stderr, "%s: %s: invalid radix\n", program_name, radix);
269 set_output_format (f)
289 fprintf (stderr, "%s: %s: invalid output format\n", program_name, f);
292 format = &formats[i];
303 program_name = *argv;
304 xmalloc_set_program_name (program_name);
306 START_PROGRESS (program_name, 0);
310 while ((c = getopt_long (argc, argv, "aABCDf:gnopPrst:uvV", long_options, (int *) 0)) != EOF)
315 print_debug_syms = 1;
319 filename_per_symbol = 1;
321 case 'B': /* For MIPS compatibility. */
322 set_output_format ("bsd");
331 set_output_format (optarg);
340 sort_numerically = 1;
346 set_output_format ("posix");
355 set_print_radix (optarg);
364 case 200: /* --target */
368 case 0: /* A long option that just sets a flag. */
378 printf ("GNU %s version %s\n", program_name, program_version);
382 /* OK, all options now parsed. If no filename specified, do a.out. */
384 return !display_file ("a.out");
388 if (argc - optind > 1)
389 filename_per_file = 1;
391 /* We were given several filenames to do. */
392 while (optind < argc)
395 if (!display_file (argv[optind++]))
399 END_PROGRESS (program_name);
406 display_archive (file)
410 bfd *last_arfile = NULL;
413 (*format->print_archive_filename) (bfd_get_filename (file));
416 print_symdef_entry (file);
422 arfile = bfd_openr_next_archived_file (file, arfile);
426 if (bfd_get_error () != bfd_error_no_more_archived_files)
427 bfd_fatal (bfd_get_filename (file));
431 if (bfd_check_format_matches (arfile, bfd_object, &matching))
433 (*format->print_archive_member) (bfd_get_filename (file),
434 bfd_get_filename (arfile));
435 display_rel_file (arfile, file);
439 bfd_nonfatal (bfd_get_filename (arfile));
440 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
442 list_matching_formats (matching);
447 if (last_arfile != NULL)
448 bfd_close (last_arfile);
449 last_arfile = arfile;
452 if (last_arfile != NULL)
453 bfd_close (last_arfile);
457 display_file (filename)
460 boolean retval = true;
464 file = bfd_openr (filename, target);
467 bfd_nonfatal (filename);
471 if (bfd_check_format (file, bfd_archive))
473 display_archive (file);
475 else if (bfd_check_format_matches (file, bfd_object, &matching))
477 (*format->print_object_filename) (filename);
478 display_rel_file (file, NULL);
482 bfd_nonfatal (filename);
483 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
485 list_matching_formats (matching);
491 if (bfd_close (file) == false)
492 bfd_fatal (filename);
497 /* Symbol-sorting predicates */
498 #define valueof(x) ((x)->section->vma + (x)->value)
500 /* Numeric sorts. Undefined symbols are always considered "less than"
501 defined symbols with zero values. Common symbols are not treated
502 specially -- i.e., their sizes are used as their "values". */
504 numeric_forward (P_x, P_y)
508 asymbol *x = *(asymbol **) P_x;
509 asymbol *y = *(asymbol **) P_y;
510 asection *xs = bfd_get_section (x);
511 asection *ys = bfd_get_section (y);
512 if (bfd_is_und_section (xs))
514 if (bfd_is_und_section (ys))
518 else if (bfd_is_und_section (ys))
520 /* Don't just return the difference -- in cross configurations,
521 after truncation to `int' it might not have the sign we want. */
522 if (valueof (x) != valueof (y))
523 return valueof (x) < valueof (y) ? -1 : 1;
525 return non_numeric_forward (P_x, P_y);
529 numeric_reverse (x, y)
533 return -numeric_forward (x, y);
537 non_numeric_forward (x, y)
541 CONST char *xn = (*(asymbol **) x)->name;
542 CONST char *yn = (*(asymbol **) y)->name;
544 return ((xn == NULL) ? ((yn == NULL) ? 0 : -1) :
545 ((yn == NULL) ? 1 : strcmp (xn, yn)));
549 non_numeric_reverse (x, y)
553 return -non_numeric_forward (x, y);
556 static int (*(sorters[2][2])) PARAMS ((const PTR, const PTR)) =
558 { non_numeric_forward, non_numeric_reverse },
559 { numeric_forward, numeric_reverse }
562 /* This sort routine is used by sort_symbols_by_size. It is similar
563 to numeric_forward, but when symbols have the same value it sorts
564 by section VMA. This simplifies the sort_symbols_by_size code
565 which handles symbols at the end of sections. Also, this routine
566 tries to sort file names before other symbols with the same value.
567 That will make the file name have a zero size, which will make
568 sort_symbols_by_size choose the non file name symbol, leading to
569 more meaningful output. For similar reasons, this code sorts
570 gnu_compiled_* and gcc2_compiled before other symbols with the same
574 size_forward (P_x, P_y)
578 asymbol *x = *(asymbol **) P_x;
579 asymbol *y = *(asymbol **) P_y;
580 asection *xs = bfd_get_section (x);
581 asection *ys = bfd_get_section (y);
589 if (bfd_is_und_section (xs))
591 if (bfd_is_und_section (ys))
594 if (valueof (x) != valueof (y))
595 return valueof (x) < valueof (y) ? -1 : 1;
597 if (xs->vma != ys->vma)
598 return xs->vma < ys->vma ? -1 : 1;
600 xn = bfd_asymbol_name (x);
601 yn = bfd_asymbol_name (y);
605 /* The symbols gnu_compiled and gcc2_compiled convey even less
606 information than the file name, so sort them out first. */
608 xf = (strstr (xn, "gnu_compiled") != NULL
609 || strstr (xn, "gcc2_compiled") != NULL);
610 yf = (strstr (yn, "gnu_compiled") != NULL
611 || strstr (yn, "gcc2_compiled") != NULL);
618 /* We use a heuristic for the file name. It may not work on non
619 Unix systems, but it doesn't really matter; the only difference
620 is precisely which symbol names get printed. */
622 #define file_symbol(s, sn, snl) \
623 ((s->flags & BSF_FILE) != 0 \
624 || (sn[snl - 2] == '.' \
625 && (sn[snl - 1] == 'o' \
626 || sn[snl - 1] == 'a')))
628 xf = file_symbol (x, xn, xnl);
629 yf = file_symbol (y, yn, ynl);
636 return non_numeric_forward (P_x, P_y);
639 /* Sort the symbols by size. We guess the size by assuming that the
640 difference between the address of a symbol and the address of the
641 next higher symbol is the size. FIXME: ELF actually stores a size
642 with each symbol. We should use it. */
645 sort_symbols_by_size (abfd, syms, symcount)
648 unsigned long symcount;
650 asymbol **from, **to;
651 unsigned int src_count;
652 unsigned int dst_count = 0;
656 qsort ((PTR) syms, symcount, sizeof (asymbol *), size_forward);
658 /* Note that filter_symbols has already removed all absolute and
659 undefined symbols. Here we remove all symbols whose size winds
662 for (from = to = syms, src_count = 0; src_count < symcount; src_count++)
666 sym = from[src_count];
667 sec = bfd_get_section (sym);
669 if (bfd_is_com_section (sec))
673 if (src_count + 1 < symcount
674 && sec == bfd_get_section (from[src_count + 1]))
675 size = valueof (from[src_count + 1]) - valueof (sym);
677 size = (bfd_get_section_vma (abfd, sec)
678 + bfd_section_size (abfd, sec)
684 /* We adjust the value of the symbol so that when it is
685 printed out, it will actually be the size. */
686 sym->value = size - bfd_get_section_vma (abfd, sec);
688 to[dst_count++] = sym;
692 /* We must now sort again by size. */
693 qsort ((PTR) syms, dst_count, sizeof (asymbol *), sorters[1][reverse_sort]);
698 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
701 display_rel_file (abfd, archive_bfd)
711 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
713 printf ("\"%s\" is not a dynamic object.\n",
714 bfd_get_filename (abfd));
720 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
722 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
728 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
730 storage = bfd_get_symtab_upper_bound (abfd);
732 bfd_fatal (bfd_get_filename (abfd));
737 fprintf (stderr, "%s: no symbols\n", bfd_get_filename (abfd));
739 fprintf (stderr, "%s: Symflags set but there are none?\n",
740 bfd_get_filename (abfd));
744 syms = (asymbol **) xmalloc (storage);
747 symcount = bfd_canonicalize_dynamic_symtab (abfd, syms);
749 symcount = bfd_canonicalize_symtab (abfd, syms);
751 bfd_fatal (bfd_get_filename (abfd));
758 /* Discard the symbols we don't want to print.
759 It's OK to do this in place; we'll free the storage anyway
762 symcount = filter_symbols (abfd, syms, symcount);
767 qsort ((char *) syms, symcount, sizeof (asymbol *),
768 sorters[sort_numerically][reverse_sort]);
770 symcount = sort_symbols_by_size (abfd, syms, symcount);
773 print_symbols (abfd, syms, symcount, archive_bfd);
777 /* Choose which symbol entries to print;
778 compact them downward to get rid of the rest.
779 Return the number of symbols to be printed. */
782 filter_symbols (abfd, syms, symcount)
783 bfd *abfd; /* Unused. */
785 unsigned long symcount;
787 asymbol **from, **to;
788 unsigned int src_count;
789 unsigned int dst_count = 0;
792 for (from = to = syms, src_count = 0; src_count < symcount; src_count++)
795 flagword flags = (from[src_count])->flags;
799 sym = from[src_count];
801 keep = bfd_is_und_section (sym->section);
802 else if (external_only)
803 keep = ((flags & BSF_GLOBAL)
804 || bfd_is_und_section (sym->section)
805 || bfd_is_com_section (sym->section));
809 if (!print_debug_syms && ((flags & BSF_DEBUGGING) != 0))
813 && (bfd_is_abs_section (sym->section)
814 || bfd_is_und_section (sym->section)))
818 to[dst_count++] = from[src_count];
824 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
825 demangling it if requested. */
828 print_symname (format, name, abfd)
836 /* In this mode, give a user-level view of the symbol name
837 even if it's not mangled; strip off any leading
839 if (bfd_get_symbol_leading_char (abfd) == name[0])
842 res = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
845 printf (format, res);
851 printf (format, name);
854 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
857 print_symbols (abfd, syms, symcount, archive_bfd)
860 unsigned long symcount;
863 asymbol **sym = syms, **end = syms + symcount;
866 for (; sym < end; ++sym)
870 (*format->print_symbol_filename) (archive_bfd, abfd);
874 if (bfd_is_und_section ((*sym)->section))
876 print_symname ("%s\n", (*sym)->name, abfd);
884 bfd_get_symbol_info (abfd, p, &syminfo);
885 (*format->print_symbol_info) (&syminfo, abfd);
892 /* The following 3 groups of functions are called unconditionally,
893 once at the start of processing each file of the appropriate type.
894 They should check `filename_per_file' and `filename_per_symbol',
895 as appropriate for their output format, to determine whether to
898 /* Print the name of an object file given on the command line. */
901 print_object_filename_bsd (filename)
904 if (filename_per_file && !filename_per_symbol)
905 printf ("\n%s:\n", filename);
909 print_object_filename_sysv (filename)
913 printf ("\n\nUndefined symbols from %s:\n\n", filename);
915 printf ("\n\nSymbols from %s:\n\n", filename);
917 Name Value Class Type Size Line Section\n\n");
921 print_object_filename_posix (filename)
924 if (filename_per_file && !filename_per_symbol)
925 printf ("%s:\n", filename);
928 /* Print the name of an archive file given on the command line. */
931 print_archive_filename_bsd (filename)
934 if (filename_per_file)
935 printf ("\n%s:\n", filename);
939 print_archive_filename_sysv (filename)
945 print_archive_filename_posix (filename)
950 /* Print the name of an archive member file. */
953 print_archive_member_bsd (archive, filename)
955 CONST char *filename;
957 if (!filename_per_symbol)
958 printf ("\n%s:\n", filename);
962 print_archive_member_sysv (archive, filename)
964 CONST char *filename;
967 printf ("\n\nUndefined symbols from %s[%s]:\n\n", archive, filename);
969 printf ("\n\nSymbols from %s[%s]:\n\n", archive, filename);
971 Name Value Class Type Size Line Section\n\n");
975 print_archive_member_posix (archive, filename)
977 CONST char *filename;
979 if (!filename_per_symbol)
980 printf ("%s[%s]:\n", archive, filename);
983 /* Print the name of the file (and archive, if there is one)
984 containing a symbol. */
987 print_symbol_filename_bsd (archive_bfd, abfd)
988 bfd *archive_bfd, *abfd;
990 if (filename_per_symbol)
993 printf ("%s:", bfd_get_filename (archive_bfd));
994 printf ("%s:", bfd_get_filename (abfd));
999 print_symbol_filename_sysv (archive_bfd, abfd)
1000 bfd *archive_bfd, *abfd;
1002 if (filename_per_symbol)
1005 printf ("%s:", bfd_get_filename (archive_bfd));
1006 printf ("%s:", bfd_get_filename (abfd));
1011 print_symbol_filename_posix (archive_bfd, abfd)
1012 bfd *archive_bfd, *abfd;
1014 if (filename_per_symbol)
1017 printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
1018 bfd_get_filename (abfd));
1020 printf ("%s: ", bfd_get_filename (abfd));
1024 /* Print a line of information about a symbol. */
1027 print_symbol_info_bsd (info, abfd)
1031 if (info->type == 'U')
1034 #ifdef BFD_HOST_64_BIT
1043 #ifdef BFD_HOST_64_BIT
1044 printf (value_format, uint64_typeHIGH (info->value),
1045 uint64_typeLOW (info->value));
1047 printf (value_format, info->value);
1050 printf (" %c", info->type);
1051 if (info->type == '-')
1055 printf (other_format, info->stab_other);
1057 printf (desc_format, info->stab_desc);
1058 printf (" %5s", info->stab_name);
1060 print_symname (" %s", info->name, abfd);
1064 print_symbol_info_sysv (info, abfd)
1068 print_symname ("%-20s|", info->name, abfd); /* Name */
1069 if (info->type == 'U')
1070 printf (" "); /* Value */
1073 #ifdef BFD_HOST_64_BIT
1074 printf (value_format, uint64_typeHIGH (info->value),
1075 uint64_typeLOW (info->value));
1077 printf (value_format, info->value);
1080 printf ("| %c |", info->type); /* Class */
1081 if (info->type == '-')
1084 printf ("%18s| ", info->stab_name); /* (C) Type */
1085 printf (desc_format, info->stab_desc); /* Size */
1086 printf ("| |"); /* Line, Section */
1089 printf (" | | |"); /* Type, Size, Line, Section */
1093 print_symbol_info_posix (info, abfd)
1097 print_symname ("%s ", info->name, abfd);
1098 printf ("%c ", info->type);
1099 if (info->type == 'U')
1103 #ifdef BFD_HOST_64_BIT
1104 printf (value_format, uint64_typeHIGH (info->value),
1105 uint64_typeLOW (info->value));
1107 printf (value_format, info->value);
1110 /* POSIX.2 wants the symbol size printed here, when applicable;
1111 BFD currently doesn't provide it, so we take the easy way out by
1112 considering it to never be applicable. */
1116 print_symdef_entry (abfd)
1119 symindex idx = BFD_NO_MORE_SYMBOLS;
1121 boolean everprinted = false;
1123 for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
1124 idx != BFD_NO_MORE_SYMBOLS;
1125 idx = bfd_get_next_mapent (abfd, idx, &thesym))
1130 printf ("\nArchive index:\n");
1133 elt = bfd_get_elt_at_index (abfd, idx);
1134 if (thesym->name != (char *) NULL)
1136 print_symname ("%s", thesym->name, abfd);
1137 printf (" in %s\n", bfd_get_filename (elt));