1 /* nm.c -- Describe symbol table of a rel file.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "aout/stab_gnu.h"
28 #include "aout/ranlib.h"
30 #include "libiberty.h"
33 /* When sorting by size, we use this structure to hold the size and a
34 pointer to the minisymbol. */
42 /* When fetching relocs, we use this structure to pass information to
45 struct get_relocs_info
53 struct extended_symbol_info
57 /* FIXME: We should add more fields for Type, Line, Section. */
59 #define SYM_NAME(sym) (sym->sinfo->name)
60 #define SYM_VALUE(sym) (sym->sinfo->value)
61 #define SYM_TYPE(sym) (sym->sinfo->type)
62 #define SYM_STAB_NAME(sym) (sym->sinfo->stab_name)
63 #define SYM_STAB_DESC(sym) (sym->sinfo->stab_desc)
64 #define SYM_STAB_OTHER(sym) (sym->sinfo->stab_other)
65 #define SYM_SIZE(sym) (sym->ssize)
67 static void usage PARAMS ((FILE *, int));
68 static void set_print_radix PARAMS ((char *));
69 static void set_output_format PARAMS ((char *));
70 static void display_archive PARAMS ((bfd *));
71 static boolean display_file PARAMS ((char *));
72 static void display_rel_file PARAMS ((bfd *, bfd *));
73 static long filter_symbols PARAMS ((bfd *, boolean, PTR, long, unsigned int));
74 static long sort_symbols_by_size PARAMS ((bfd *, boolean, PTR, long, unsigned int, struct size_sym **));
75 static void print_symbols PARAMS ((bfd *, boolean, PTR, long, unsigned int, bfd *));
76 static void print_size_symbols PARAMS ((bfd *, boolean, struct size_sym *, long, bfd *));
77 static void print_symname PARAMS ((const char *, const char *, bfd *));
78 static void print_symbol PARAMS ((bfd *, asymbol *, bfd_vma ssize, bfd *));
79 static void print_symdef_entry PARAMS ((bfd *));
81 /* The sorting functions. */
82 static int numeric_forward PARAMS ((const PTR, const PTR));
83 static int numeric_reverse PARAMS ((const PTR, const PTR));
84 static int non_numeric_forward PARAMS ((const PTR, const PTR));
85 static int non_numeric_reverse PARAMS ((const PTR, const PTR));
86 static int size_forward1 PARAMS ((const PTR, const PTR));
87 static int size_forward2 PARAMS ((const PTR, const PTR));
89 /* The output formatting functions. */
90 static void print_object_filename_bsd PARAMS ((char *));
91 static void print_object_filename_sysv PARAMS ((char *));
92 static void print_object_filename_posix PARAMS ((char *));
93 static void print_archive_filename_bsd PARAMS ((char *));
94 static void print_archive_filename_sysv PARAMS ((char *));
95 static void print_archive_filename_posix PARAMS ((char *));
96 static void print_archive_member_bsd PARAMS ((char *, const char *));
97 static void print_archive_member_sysv PARAMS ((char *, const char *));
98 static void print_archive_member_posix PARAMS ((char *, const char *));
99 static void print_symbol_filename_bsd PARAMS ((bfd *, bfd *));
100 static void print_symbol_filename_sysv PARAMS ((bfd *, bfd *));
101 static void print_symbol_filename_posix PARAMS ((bfd *, bfd *));
102 static void print_value PARAMS ((bfd *, bfd_vma));
103 static void print_symbol_info_bsd PARAMS ((struct extended_symbol_info *, bfd *));
104 static void print_symbol_info_sysv PARAMS ((struct extended_symbol_info *, bfd *));
105 static void print_symbol_info_posix PARAMS ((struct extended_symbol_info *, bfd *));
106 static void get_relocs PARAMS ((bfd *, asection *, PTR));
108 /* Support for different output formats. */
111 /* Print the name of an object file given on the command line. */
112 void (*print_object_filename) PARAMS ((char *));
114 /* Print the name of an archive file given on the command line. */
115 void (*print_archive_filename) PARAMS ((char *));
117 /* Print the name of an archive member file. */
118 void (*print_archive_member) PARAMS ((char *, const char *));
120 /* Print the name of the file (and archive, if there is one)
121 containing a symbol. */
122 void (*print_symbol_filename) PARAMS ((bfd *, bfd *));
124 /* Print a line of information about a symbol. */
125 void (*print_symbol_info) PARAMS ((struct extended_symbol_info *, bfd *));
128 static struct output_fns formats[] =
130 {print_object_filename_bsd,
131 print_archive_filename_bsd,
132 print_archive_member_bsd,
133 print_symbol_filename_bsd,
134 print_symbol_info_bsd},
135 {print_object_filename_sysv,
136 print_archive_filename_sysv,
137 print_archive_member_sysv,
138 print_symbol_filename_sysv,
139 print_symbol_info_sysv},
140 {print_object_filename_posix,
141 print_archive_filename_posix,
142 print_archive_member_posix,
143 print_symbol_filename_posix,
144 print_symbol_info_posix}
147 /* Indices in `formats'. */
149 #define FORMAT_SYSV 1
150 #define FORMAT_POSIX 2
151 #define FORMAT_DEFAULT FORMAT_BSD
153 /* The output format to use. */
154 static struct output_fns *format = &formats[FORMAT_DEFAULT];
156 /* Command options. */
158 static int do_demangle = 0; /* Pretty print C++ symbol names. */
159 static int external_only = 0; /* Print external symbols only. */
160 static int defined_only = 0; /* Print defined symbols only. */
161 static int no_sort = 0; /* Don't sort; print syms in order found. */
162 static int print_debug_syms = 0;/* Print debugger-only symbols too. */
163 static int print_armap = 0; /* Describe __.SYMDEF data in archive files. */
164 static int reverse_sort = 0; /* Sort in downward(alpha or numeric) order. */
165 static int sort_numerically = 0;/* Sort in numeric rather than alpha order. */
166 static int sort_by_size = 0; /* Sort by size of symbol. */
167 static int undefined_only = 0; /* Print undefined symbols only. */
168 static int dynamic = 0; /* Print dynamic symbols. */
169 static int show_version = 0; /* Show the version number. */
170 static int show_stats = 0; /* Show statistics. */
171 static int line_numbers = 0; /* Print line numbers for symbols. */
173 /* When to print the names of files. Not mutually exclusive in SYSV format. */
174 static int filename_per_file = 0; /* Once per file, on its own line. */
175 static int filename_per_symbol = 0; /* Once per symbol, at start of line. */
177 /* Print formats for printing a symbol value. */
179 static char value_format[] = "%08lx";
181 #if BFD_HOST_64BIT_LONG
182 static char value_format[] = "%016lx";
184 /* We don't use value_format for this case. */
188 static int print_width = 16;
190 static int print_width = 8;
192 static int print_radix = 16;
193 /* Print formats for printing stab info. */
194 static char other_format[] = "%02x";
195 static char desc_format[] = "%04x";
197 static char *target = NULL;
199 /* Used to cache the line numbers for a BFD. */
200 static bfd *lineno_cache_bfd;
201 static bfd *lineno_cache_rel_bfd;
203 #define OPTION_TARGET 200
205 static struct option long_options[] =
207 {"debug-syms", no_argument, &print_debug_syms, 1},
208 {"demangle", optional_argument, 0, 'C'},
209 {"dynamic", no_argument, &dynamic, 1},
210 {"extern-only", no_argument, &external_only, 1},
211 {"format", required_argument, 0, 'f'},
212 {"help", no_argument, 0, 'h'},
213 {"line-numbers", no_argument, 0, 'l'},
214 {"no-cplus", no_argument, &do_demangle, 0}, /* Linux compatibility. */
215 {"no-demangle", no_argument, &do_demangle, 0},
216 {"no-sort", no_argument, &no_sort, 1},
217 {"numeric-sort", no_argument, &sort_numerically, 1},
218 {"portability", no_argument, 0, 'P'},
219 {"print-armap", no_argument, &print_armap, 1},
220 {"print-file-name", no_argument, 0, 'o'},
221 {"radix", required_argument, 0, 't'},
222 {"reverse-sort", no_argument, &reverse_sort, 1},
223 {"size-sort", no_argument, &sort_by_size, 1},
224 {"stats", no_argument, &show_stats, 1},
225 {"target", required_argument, 0, OPTION_TARGET},
226 {"defined-only", no_argument, &defined_only, 1},
227 {"undefined-only", no_argument, &undefined_only, 1},
228 {"version", no_argument, &show_version, 1},
229 {0, no_argument, 0, 0}
232 /* Some error-reporting functions. */
235 usage (stream, status)
239 fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
240 fprintf (stream, _(" List symbols in [file(s)] (a.out by default).\n"));
241 fprintf (stream, _(" The options are:\n\
242 -a, --debug-syms Display debugger-only symbols\n\
243 -A, --print-file-name Print name of the input file before every symbol\n\
244 -B Same as --format=bsd\n\
245 -C, --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
246 The STYLE, if specified, can be `auto' (the default),\n\
247 `gnu', 'lucid', 'arm', 'hp', 'edg' or 'gnu-new-abi'\n\
248 --no-demangle Do not demangle low-level symbol names\n\
249 -D, --dynamic Display dynamic symbols instead of normal symbols\n\
250 --defined-only Display only defined symbols\n\
252 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
253 `sysv' or `posix'. The default is `bsd'\n\
254 -g, --extern-only Display only external symbols\n\
255 -l, --line-numbers Use debugging information to find a filename and\n\
256 line number for each symbol\n\
257 -n, --numeric-sort Sort symbols numerically by address\n\
259 -p, --no-sort Do not sort the symbols\n\
260 -P, --portability Same as --format=posix\n\
261 -r, --reverse-sort Reverse the sense of the sort\n\
262 -s, --print-armap Include index for symbols from archive members\n\
263 --size-sort Sort symbols by size\n\
264 -t, --radix=RADIX Use RADIX for printing symbol values\n\
265 --target=BFDNAME Specify the target object format as BFDNAME\n\
266 -u, --undefined-only Display only undefined symbols\n\
267 -X 32_64 (ignored)\n\
268 -h, --help Display this information\n\
269 -V, --version Display this program's version number\n\
271 list_supported_targets (program_name, stream);
273 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
277 /* Set the radix for the symbol value and size according to RADIX. */
280 set_print_radix (radix)
294 value_format[4] = *radix;
296 #if BFD_HOST_64BIT_LONG
297 value_format[5] = *radix;
299 /* This case requires special handling for octal and decimal
303 other_format[3] = desc_format[3] = *radix;
306 fatal (_("%s: invalid radix"), radix);
311 set_output_format (f)
331 fatal (_("%s: invalid output format"), f);
333 format = &formats[i];
336 int main PARAMS ((int, char **));
346 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
347 setlocale (LC_MESSAGES, "");
349 #if defined (HAVE_SETLOCALE)
350 setlocale (LC_CTYPE, "");
352 bindtextdomain (PACKAGE, LOCALEDIR);
353 textdomain (PACKAGE);
355 program_name = *argv;
356 xmalloc_set_program_name (program_name);
358 START_PROGRESS (program_name, 0);
361 set_default_bfd_target ();
363 while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrst:uvVvX:",
364 long_options, (int *) 0)) != EOF)
369 print_debug_syms = 1;
373 filename_per_symbol = 1;
375 case 'B': /* For MIPS compatibility. */
376 set_output_format ("bsd");
382 enum demangling_styles style;
384 style = cplus_demangle_name_to_style (optarg);
385 if (style == unknown_demangling)
386 fatal (_("unknown demangling style `%s'"),
389 cplus_demangle_set_style (style);
396 /* Ignored for HP/UX compatibility. */
399 set_output_format (optarg);
412 sort_numerically = 1;
418 set_output_format ("posix");
427 set_print_radix (optarg);
436 /* Ignored for (partial) AIX compatibility. On AIX, the
437 argument has values 32, 64, or 32_64, and specfies that
438 only 32-bit, only 64-bit, or both kinds of objects should
439 be examined. The default is 32. So plain AIX nm on a
440 library archive with both kinds of objects will ignore
441 the 64-bit ones. For GNU nm, the default is and always
442 has been -X 32_64, and other options are not supported. */
443 if (strcmp (optarg, "32_64") != 0)
444 fatal (_("Only -X 32_64 is supported"));
447 case OPTION_TARGET: /* --target */
451 case 0: /* A long option that just sets a flag. */
460 print_version ("nm");
462 /* OK, all options now parsed. If no filename specified, do a.out. */
464 return !display_file ("a.out");
468 if (argc - optind > 1)
469 filename_per_file = 1;
471 /* We were given several filenames to do. */
472 while (optind < argc)
475 if (!display_file (argv[optind++]))
479 END_PROGRESS (program_name);
484 char *lim = (char *) sbrk (0);
486 non_fatal (_("data size %ld"), (long) (lim - (char *) &environ));
495 display_archive (file)
499 bfd *last_arfile = NULL;
502 (*format->print_archive_filename) (bfd_get_filename (file));
505 print_symdef_entry (file);
511 arfile = bfd_openr_next_archived_file (file, arfile);
515 if (bfd_get_error () != bfd_error_no_more_archived_files)
516 bfd_fatal (bfd_get_filename (file));
520 if (bfd_check_format_matches (arfile, bfd_object, &matching))
522 (*format->print_archive_member) (bfd_get_filename (file),
523 bfd_get_filename (arfile));
524 display_rel_file (arfile, file);
528 bfd_nonfatal (bfd_get_filename (arfile));
529 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
531 list_matching_formats (matching);
536 if (last_arfile != NULL)
538 bfd_close (last_arfile);
539 lineno_cache_bfd = NULL;
540 lineno_cache_rel_bfd = NULL;
542 last_arfile = arfile;
545 if (last_arfile != NULL)
547 bfd_close (last_arfile);
548 lineno_cache_bfd = NULL;
549 lineno_cache_rel_bfd = NULL;
554 display_file (filename)
557 boolean retval = true;
561 file = bfd_openr (filename, target);
564 bfd_nonfatal (filename);
568 if (bfd_check_format (file, bfd_archive))
570 display_archive (file);
572 else if (bfd_check_format_matches (file, bfd_object, &matching))
574 (*format->print_object_filename) (filename);
575 display_rel_file (file, NULL);
579 bfd_nonfatal (filename);
580 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
582 list_matching_formats (matching);
588 if (bfd_close (file) == false)
589 bfd_fatal (filename);
591 lineno_cache_bfd = NULL;
592 lineno_cache_rel_bfd = NULL;
597 /* These globals are used to pass information into the sorting
599 static bfd *sort_bfd;
600 static boolean sort_dynamic;
601 static asymbol *sort_x;
602 static asymbol *sort_y;
604 /* Symbol-sorting predicates */
605 #define valueof(x) ((x)->section->vma + (x)->value)
607 /* Numeric sorts. Undefined symbols are always considered "less than"
608 defined symbols with zero values. Common symbols are not treated
609 specially -- i.e., their sizes are used as their "values". */
612 numeric_forward (P_x, P_y)
619 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
620 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
621 if (x == NULL || y == NULL)
622 bfd_fatal (bfd_get_filename (sort_bfd));
624 xs = bfd_get_section (x);
625 ys = bfd_get_section (y);
627 if (bfd_is_und_section (xs))
629 if (! bfd_is_und_section (ys))
632 else if (bfd_is_und_section (ys))
634 else if (valueof (x) != valueof (y))
635 return valueof (x) < valueof (y) ? -1 : 1;
637 return non_numeric_forward (P_x, P_y);
641 numeric_reverse (x, y)
645 return - numeric_forward (x, y);
649 non_numeric_forward (P_x, P_y)
656 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
657 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
658 if (x == NULL || y == NULL)
659 bfd_fatal (bfd_get_filename (sort_bfd));
661 xn = bfd_asymbol_name (x);
662 yn = bfd_asymbol_name (y);
664 return ((xn == NULL) ? ((yn == NULL) ? 0 : -1) :
665 ((yn == NULL) ? 1 : strcmp (xn, yn)));
669 non_numeric_reverse (x, y)
673 return - non_numeric_forward (x, y);
676 static int (*(sorters[2][2])) PARAMS ((const PTR, const PTR)) =
678 { non_numeric_forward, non_numeric_reverse },
679 { numeric_forward, numeric_reverse }
682 /* This sort routine is used by sort_symbols_by_size. It is similar
683 to numeric_forward, but when symbols have the same value it sorts
684 by section VMA. This simplifies the sort_symbols_by_size code
685 which handles symbols at the end of sections. Also, this routine
686 tries to sort file names before other symbols with the same value.
687 That will make the file name have a zero size, which will make
688 sort_symbols_by_size choose the non file name symbol, leading to
689 more meaningful output. For similar reasons, this code sorts
690 gnu_compiled_* and gcc2_compiled before other symbols with the same
694 size_forward1 (P_x, P_y)
704 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
705 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
706 if (x == NULL || y == NULL)
707 bfd_fatal (bfd_get_filename (sort_bfd));
709 xs = bfd_get_section (x);
710 ys = bfd_get_section (y);
712 if (bfd_is_und_section (xs))
714 if (bfd_is_und_section (ys))
717 if (valueof (x) != valueof (y))
718 return valueof (x) < valueof (y) ? -1 : 1;
720 if (xs->vma != ys->vma)
721 return xs->vma < ys->vma ? -1 : 1;
723 xn = bfd_asymbol_name (x);
724 yn = bfd_asymbol_name (y);
728 /* The symbols gnu_compiled and gcc2_compiled convey even less
729 information than the file name, so sort them out first. */
731 xf = (strstr (xn, "gnu_compiled") != NULL
732 || strstr (xn, "gcc2_compiled") != NULL);
733 yf = (strstr (yn, "gnu_compiled") != NULL
734 || strstr (yn, "gcc2_compiled") != NULL);
741 /* We use a heuristic for the file name. It may not work on non
742 Unix systems, but it doesn't really matter; the only difference
743 is precisely which symbol names get printed. */
745 #define file_symbol(s, sn, snl) \
746 (((s)->flags & BSF_FILE) != 0 \
747 || ((sn)[(snl) - 2] == '.' \
748 && ((sn)[(snl) - 1] == 'o' \
749 || (sn)[(snl) - 1] == 'a')))
751 xf = file_symbol (x, xn, xnl);
752 yf = file_symbol (y, yn, ynl);
759 return non_numeric_forward (P_x, P_y);
762 /* This sort routine is used by sort_symbols_by_size. It is sorting
763 an array of size_sym structures into size order. */
766 size_forward2 (P_x, P_y)
770 const struct size_sym *x = (const struct size_sym *) P_x;
771 const struct size_sym *y = (const struct size_sym *) P_y;
773 if (x->size < y->size)
774 return reverse_sort ? 1 : -1;
775 else if (x->size > y->size)
776 return reverse_sort ? -1 : 1;
778 return sorters[0][reverse_sort] (x->minisym, y->minisym);
781 /* Sort the symbols by size. ELF provides a size but for other formats
782 we have to make a guess by assuming that the difference between the
783 address of a symbol and the address of the next higher symbol is the
787 sort_symbols_by_size (abfd, dynamic, minisyms, symcount, size, symsizesp)
793 struct size_sym **symsizesp;
795 struct size_sym *symsizes;
796 bfd_byte *from, *fromend;
798 asymbol *store_sym, *store_next;
800 qsort (minisyms, symcount, size, size_forward1);
802 /* We are going to return a special set of symbols and sizes to
804 symsizes = (struct size_sym *) xmalloc (symcount * sizeof (struct size_sym));
805 *symsizesp = symsizes;
807 /* Note that filter_symbols has already removed all absolute and
808 undefined symbols. Here we remove all symbols whose size winds
810 from = (bfd_byte *) minisyms;
811 fromend = from + symcount * size;
818 sym = bfd_minisymbol_to_symbol (abfd, dynamic, (const PTR) from,
821 bfd_fatal (bfd_get_filename (abfd));
824 for (; from < fromend; from += size)
831 if (from + size < fromend)
833 next = bfd_minisymbol_to_symbol (abfd,
835 (const PTR) (from + size),
838 bfd_fatal (bfd_get_filename (abfd));
843 sec = bfd_get_section (sym);
845 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
846 sz = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
847 else if (bfd_is_com_section (sec))
851 if (from + size < fromend
852 && sec == bfd_get_section (next))
853 sz = valueof (next) - valueof (sym);
855 sz = (bfd_get_section_vma (abfd, sec)
856 + bfd_section_size (abfd, sec)
862 symsizes->minisym = (const PTR) from;
870 store_sym = store_next;
874 symcount = symsizes - *symsizesp;
876 /* We must now sort again by size. */
877 qsort ((PTR) *symsizesp, symcount, sizeof (struct size_sym), size_forward2);
882 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
885 display_rel_file (abfd, archive_bfd)
892 struct size_sym *symsizes;
897 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
899 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
904 symcount = bfd_read_minisymbols (abfd, dynamic, &minisyms, &size);
906 bfd_fatal (bfd_get_filename (abfd));
910 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
914 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
915 print_width = strlen (buf);
917 /* Discard the symbols we don't want to print.
918 It's OK to do this in place; we'll free the storage anyway
921 symcount = filter_symbols (abfd, dynamic, minisyms, symcount, size);
927 sort_dynamic = dynamic;
928 sort_x = bfd_make_empty_symbol (abfd);
929 sort_y = bfd_make_empty_symbol (abfd);
930 if (sort_x == NULL || sort_y == NULL)
931 bfd_fatal (bfd_get_filename (abfd));
934 qsort (minisyms, symcount, size,
935 sorters[sort_numerically][reverse_sort]);
937 symcount = sort_symbols_by_size (abfd, dynamic, minisyms, symcount,
942 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd);
944 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd);
949 /* Choose which symbol entries to print;
950 compact them downward to get rid of the rest.
951 Return the number of symbols to be printed. */
954 filter_symbols (abfd, dynamic, minisyms, symcount, size)
961 bfd_byte *from, *fromend, *to;
964 store = bfd_make_empty_symbol (abfd);
966 bfd_fatal (bfd_get_filename (abfd));
968 from = (bfd_byte *) minisyms;
969 fromend = from + symcount * size;
970 to = (bfd_byte *) minisyms;
972 for (; from < fromend; from += size)
979 sym = bfd_minisymbol_to_symbol (abfd, dynamic, (const PTR) from, store);
981 bfd_fatal (bfd_get_filename (abfd));
984 keep = bfd_is_und_section (sym->section);
985 else if (external_only)
986 keep = ((sym->flags & BSF_GLOBAL) != 0
987 || (sym->flags & BSF_WEAK) != 0
988 || bfd_is_und_section (sym->section)
989 || bfd_is_com_section (sym->section));
994 && ! print_debug_syms
995 && (sym->flags & BSF_DEBUGGING) != 0)
1000 && (bfd_is_abs_section (sym->section)
1001 || bfd_is_und_section (sym->section)))
1007 if (bfd_is_und_section (sym->section))
1013 memcpy (to, from, size);
1018 return (to - (bfd_byte *) minisyms) / size;
1021 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
1022 demangling it if requested. */
1025 print_symname (format, name, abfd)
1030 if (do_demangle && *name)
1034 /* In this mode, give a user-level view of the symbol name
1035 even if it's not mangled; strip off any leading
1037 if (bfd_get_symbol_leading_char (abfd) == name[0])
1040 res = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
1043 printf (format, res);
1049 printf (format, name);
1052 /* Print the symbols. If ARCHIVE_BFD is non-NULL, it is the archive
1056 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd)
1065 bfd_byte *from, *fromend;
1067 store = bfd_make_empty_symbol (abfd);
1069 bfd_fatal (bfd_get_filename (abfd));
1071 from = (bfd_byte *) minisyms;
1072 fromend = from + symcount * size;
1073 for (; from < fromend; from += size)
1078 sym = bfd_minisymbol_to_symbol (abfd, dynamic, from, store);
1080 bfd_fatal (bfd_get_filename (abfd));
1082 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1083 ssize = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
1087 print_symbol (abfd, sym, ssize, archive_bfd);
1091 /* Print the symbols when sorting by size. */
1094 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd)
1097 struct size_sym *symsizes;
1102 struct size_sym *from, *fromend;
1104 store = bfd_make_empty_symbol (abfd);
1106 bfd_fatal (bfd_get_filename (abfd));
1109 fromend = from + symcount;
1110 for (; from < fromend; from++)
1115 sym = bfd_minisymbol_to_symbol (abfd, dynamic, from->minisym, store);
1117 bfd_fatal (bfd_get_filename (abfd));
1119 /* Set the symbol value so that we actually display the symbol size. */
1120 sym->value = from->size - bfd_section_vma (abfd, bfd_get_section (sym));
1122 /* For elf we have already computed the correct symbol size. */
1123 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1126 ssize = from->size - bfd_section_vma (abfd, bfd_get_section (sym));
1128 print_symbol (abfd, sym, ssize, archive_bfd);
1132 /* Print a single symbol. */
1135 print_symbol (abfd, sym, ssize, archive_bfd)
1143 (*format->print_symbol_filename) (archive_bfd, abfd);
1147 if (bfd_is_und_section (bfd_get_section (sym)))
1148 print_symname ("%s", bfd_asymbol_name (sym), abfd);
1152 symbol_info syminfo;
1153 struct extended_symbol_info info;
1155 bfd_get_symbol_info (abfd, sym, &syminfo);
1156 info.sinfo = &syminfo;
1158 (*format->print_symbol_info) (&info, abfd);
1163 static asymbol **syms;
1164 static long symcount;
1165 const char *filename, *functionname;
1166 unsigned int lineno;
1168 /* We need to get the canonical symbols in order to call
1169 bfd_find_nearest_line. This is inefficient, but, then, you
1170 don't have to use --line-numbers. */
1171 if (abfd != lineno_cache_bfd && syms != NULL)
1180 symsize = bfd_get_symtab_upper_bound (abfd);
1182 bfd_fatal (bfd_get_filename (abfd));
1183 syms = (asymbol **) xmalloc (symsize);
1184 symcount = bfd_canonicalize_symtab (abfd, syms);
1186 bfd_fatal (bfd_get_filename (abfd));
1187 lineno_cache_bfd = abfd;
1190 if (bfd_is_und_section (bfd_get_section (sym)))
1192 static asection **secs;
1193 static arelent ***relocs;
1194 static long *relcount;
1195 static unsigned int seccount;
1197 const char *symname;
1199 /* For an undefined symbol, we try to find a reloc for the
1200 symbol, and print the line number of the reloc. */
1201 if (abfd != lineno_cache_rel_bfd && relocs != NULL)
1203 for (i = 0; i < seccount; i++)
1204 if (relocs[i] != NULL)
1216 struct get_relocs_info info;
1218 seccount = bfd_count_sections (abfd);
1220 secs = (asection **) xmalloc (seccount * sizeof *secs);
1221 relocs = (arelent ***) xmalloc (seccount * sizeof *relocs);
1222 relcount = (long *) xmalloc (seccount * sizeof *relcount);
1225 info.relocs = relocs;
1226 info.relcount = relcount;
1228 bfd_map_over_sections (abfd, get_relocs, (PTR) &info);
1229 lineno_cache_rel_bfd = abfd;
1232 symname = bfd_asymbol_name (sym);
1233 for (i = 0; i < seccount; i++)
1237 for (j = 0; j < relcount[i]; j++)
1242 if (r->sym_ptr_ptr != NULL
1243 && (*r->sym_ptr_ptr)->section == sym->section
1244 && (*r->sym_ptr_ptr)->value == sym->value
1246 bfd_asymbol_name (*r->sym_ptr_ptr)) == 0
1247 && bfd_find_nearest_line (abfd, secs[i], syms,
1248 r->address, &filename,
1249 &functionname, &lineno)
1250 && filename != NULL)
1252 /* We only print the first one we find. */
1253 printf ("\t%s:%u", filename, lineno);
1260 else if (bfd_get_section (sym)->owner == abfd)
1262 if (bfd_find_nearest_line (abfd, bfd_get_section (sym), syms,
1263 sym->value, &filename, &functionname,
1268 printf ("\t%s:%u", filename, lineno);
1276 /* The following 3 groups of functions are called unconditionally,
1277 once at the start of processing each file of the appropriate type.
1278 They should check `filename_per_file' and `filename_per_symbol',
1279 as appropriate for their output format, to determine whether to
1282 /* Print the name of an object file given on the command line. */
1285 print_object_filename_bsd (filename)
1288 if (filename_per_file && !filename_per_symbol)
1289 printf ("\n%s:\n", filename);
1293 print_object_filename_sysv (filename)
1297 printf (_("\n\nUndefined symbols from %s:\n\n"), filename);
1299 printf (_("\n\nSymbols from %s:\n\n"), filename);
1301 Name Value Class Type Size Line Section\n\n"));
1305 print_object_filename_posix (filename)
1308 if (filename_per_file && !filename_per_symbol)
1309 printf ("%s:\n", filename);
1312 /* Print the name of an archive file given on the command line. */
1315 print_archive_filename_bsd (filename)
1318 if (filename_per_file)
1319 printf ("\n%s:\n", filename);
1323 print_archive_filename_sysv (filename)
1324 char *filename ATTRIBUTE_UNUSED;
1329 print_archive_filename_posix (filename)
1330 char *filename ATTRIBUTE_UNUSED;
1334 /* Print the name of an archive member file. */
1337 print_archive_member_bsd (archive, filename)
1338 char *archive ATTRIBUTE_UNUSED;
1339 const char *filename;
1341 if (!filename_per_symbol)
1342 printf ("\n%s:\n", filename);
1346 print_archive_member_sysv (archive, filename)
1348 const char *filename;
1351 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename);
1353 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename);
1355 Name Value Class Type Size Line Section\n\n"));
1359 print_archive_member_posix (archive, filename)
1361 const char *filename;
1363 if (!filename_per_symbol)
1364 printf ("%s[%s]:\n", archive, filename);
1367 /* Print the name of the file (and archive, if there is one)
1368 containing a symbol. */
1371 print_symbol_filename_bsd (archive_bfd, abfd)
1372 bfd *archive_bfd, *abfd;
1374 if (filename_per_symbol)
1377 printf ("%s:", bfd_get_filename (archive_bfd));
1378 printf ("%s:", bfd_get_filename (abfd));
1383 print_symbol_filename_sysv (archive_bfd, abfd)
1384 bfd *archive_bfd, *abfd;
1386 if (filename_per_symbol)
1389 printf ("%s:", bfd_get_filename (archive_bfd));
1390 printf ("%s:", bfd_get_filename (abfd));
1395 print_symbol_filename_posix (archive_bfd, abfd)
1396 bfd *archive_bfd, *abfd;
1398 if (filename_per_symbol)
1401 printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
1402 bfd_get_filename (abfd));
1404 printf ("%s: ", bfd_get_filename (abfd));
1408 /* Print a symbol value. */
1411 print_value (abfd, val)
1412 bfd *abfd ATTRIBUTE_UNUSED;
1415 #if ! defined (BFD64) || BFD_HOST_64BIT_LONG
1416 printf (value_format, val);
1418 /* We have a 64 bit value to print, but the host is only 32 bit. */
1419 if (print_radix == 16)
1420 bfd_fprintf_vma (abfd, stdout, val);
1426 s = buf + sizeof buf;
1430 *--s = (val % print_radix) + '0';
1433 while ((buf + sizeof buf - 1) - s < 16)
1440 /* Print a line of information about a symbol. */
1443 print_symbol_info_bsd (info, abfd)
1444 struct extended_symbol_info *info;
1447 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1449 if (print_width == 16)
1455 print_value (abfd, SYM_VALUE (info));
1457 if (SYM_SIZE (info))
1460 print_value (abfd, SYM_SIZE (info));
1464 printf (" %c", SYM_TYPE (info));
1466 if (SYM_TYPE (info) == '-')
1470 printf (other_format, SYM_STAB_OTHER (info));
1472 printf (desc_format, SYM_STAB_DESC (info));
1473 printf (" %5s", SYM_STAB_NAME (info));
1475 print_symname (" %s", SYM_NAME (info), abfd);
1479 print_symbol_info_sysv (info, abfd)
1480 struct extended_symbol_info *info;
1483 print_symname ("%-20s|", SYM_NAME (info), abfd);
1485 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1488 print_value (abfd, SYM_VALUE (info));
1490 printf ("| %c |", SYM_TYPE (info));
1492 if (SYM_TYPE (info) == '-')
1495 printf ("%18s| ", SYM_STAB_NAME (info)); /* (C) Type */
1496 printf (desc_format, SYM_STAB_DESC (info)); /* Size */
1497 printf ("| |"); /* Line, Section */
1501 /* Type, Size, Line, Section */
1504 if (SYM_SIZE (info))
1505 print_value (abfd, SYM_SIZE (info));
1514 print_symbol_info_posix (info, abfd)
1515 struct extended_symbol_info *info;
1518 print_symname ("%s ", SYM_NAME (info), abfd);
1519 printf ("%c ", SYM_TYPE (info));
1521 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1525 print_value (abfd, SYM_VALUE (info));
1527 if (SYM_SIZE (info))
1528 print_value (abfd, SYM_SIZE (info));
1533 print_symdef_entry (abfd)
1536 symindex idx = BFD_NO_MORE_SYMBOLS;
1538 boolean everprinted = false;
1540 for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
1541 idx != BFD_NO_MORE_SYMBOLS;
1542 idx = bfd_get_next_mapent (abfd, idx, &thesym))
1547 printf (_("\nArchive index:\n"));
1550 elt = bfd_get_elt_at_index (abfd, idx);
1552 bfd_fatal ("bfd_get_elt_at_index");
1553 if (thesym->name != (char *) NULL)
1555 print_symname ("%s", thesym->name, abfd);
1556 printf (" in %s\n", bfd_get_filename (elt));
1561 /* This function is used to get the relocs for a particular section.
1562 It is called via bfd_map_over_sections. */
1565 get_relocs (abfd, sec, dataarg)
1570 struct get_relocs_info *data = (struct get_relocs_info *) dataarg;
1574 if ((sec->flags & SEC_RELOC) == 0)
1576 *data->relocs = NULL;
1577 *data->relcount = 0;
1583 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1585 bfd_fatal (bfd_get_filename (abfd));
1587 *data->relocs = (arelent **) xmalloc (relsize);
1588 *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs,
1590 if (*data->relcount < 0)
1591 bfd_fatal (bfd_get_filename (abfd));