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
28 #include "aout/stab_gnu.h"
29 #include "aout/ranlib.h"
31 #include "libiberty.h"
33 #include "elf/common.h"
35 /* When sorting by size, we use this structure to hold the size and a
36 pointer to the minisymbol. */
44 /* When fetching relocs, we use this structure to pass information to
47 struct get_relocs_info
55 struct extended_symbol_info
59 elf_symbol_type *elfinfo;
60 /* FIXME: We should add more fields for Type, Line, Section. */
62 #define SYM_NAME(sym) (sym->sinfo->name)
63 #define SYM_VALUE(sym) (sym->sinfo->value)
64 #define SYM_TYPE(sym) (sym->sinfo->type)
65 #define SYM_STAB_NAME(sym) (sym->sinfo->stab_name)
66 #define SYM_STAB_DESC(sym) (sym->sinfo->stab_desc)
67 #define SYM_STAB_OTHER(sym) (sym->sinfo->stab_other)
68 #define SYM_SIZE(sym) \
69 (sym->elfinfo ? sym->elfinfo->internal_elf_sym.st_size: sym->ssize)
71 static void usage PARAMS ((FILE *, int));
72 static void set_print_radix PARAMS ((char *));
73 static void set_output_format PARAMS ((char *));
74 static void display_archive PARAMS ((bfd *));
75 static boolean display_file PARAMS ((char *));
76 static void display_rel_file PARAMS ((bfd *, bfd *));
77 static long filter_symbols PARAMS ((bfd *, boolean, PTR, long, unsigned int));
78 static long sort_symbols_by_size PARAMS ((bfd *, boolean, PTR, long, unsigned int, struct size_sym **));
79 static void print_symbols PARAMS ((bfd *, boolean, PTR, long, unsigned int, bfd *));
80 static void print_size_symbols PARAMS ((bfd *, boolean, struct size_sym *, long, bfd *));
81 static void print_symname PARAMS ((const char *, const char *, bfd *));
82 static void print_symbol PARAMS ((bfd *, asymbol *, bfd_vma ssize, bfd *));
83 static void print_symdef_entry PARAMS ((bfd *));
85 /* The sorting functions. */
86 static int numeric_forward PARAMS ((const PTR, const PTR));
87 static int numeric_reverse PARAMS ((const PTR, const PTR));
88 static int non_numeric_forward PARAMS ((const PTR, const PTR));
89 static int non_numeric_reverse PARAMS ((const PTR, const PTR));
90 static int size_forward1 PARAMS ((const PTR, const PTR));
91 static int size_forward2 PARAMS ((const PTR, const PTR));
93 /* The output formatting functions. */
94 static void print_object_filename_bsd PARAMS ((char *));
95 static void print_object_filename_sysv PARAMS ((char *));
96 static void print_object_filename_posix PARAMS ((char *));
97 static void print_archive_filename_bsd PARAMS ((char *));
98 static void print_archive_filename_sysv PARAMS ((char *));
99 static void print_archive_filename_posix PARAMS ((char *));
100 static void print_archive_member_bsd PARAMS ((char *, const char *));
101 static void print_archive_member_sysv PARAMS ((char *, const char *));
102 static void print_archive_member_posix PARAMS ((char *, const char *));
103 static void print_symbol_filename_bsd PARAMS ((bfd *, bfd *));
104 static void print_symbol_filename_sysv PARAMS ((bfd *, bfd *));
105 static void print_symbol_filename_posix PARAMS ((bfd *, bfd *));
106 static void print_value PARAMS ((bfd *, bfd_vma));
107 static void print_symbol_info_bsd PARAMS ((struct extended_symbol_info *, bfd *));
108 static void print_symbol_info_sysv PARAMS ((struct extended_symbol_info *, bfd *));
109 static void print_symbol_info_posix PARAMS ((struct extended_symbol_info *, bfd *));
110 static void get_relocs PARAMS ((bfd *, asection *, PTR));
111 static const char * get_symbol_type PARAMS ((unsigned int));
113 /* Support for different output formats. */
116 /* Print the name of an object file given on the command line. */
117 void (*print_object_filename) PARAMS ((char *));
119 /* Print the name of an archive file given on the command line. */
120 void (*print_archive_filename) PARAMS ((char *));
122 /* Print the name of an archive member file. */
123 void (*print_archive_member) PARAMS ((char *, const char *));
125 /* Print the name of the file (and archive, if there is one)
126 containing a symbol. */
127 void (*print_symbol_filename) PARAMS ((bfd *, bfd *));
129 /* Print a line of information about a symbol. */
130 void (*print_symbol_info) PARAMS ((struct extended_symbol_info *, bfd *));
133 static struct output_fns formats[] =
135 {print_object_filename_bsd,
136 print_archive_filename_bsd,
137 print_archive_member_bsd,
138 print_symbol_filename_bsd,
139 print_symbol_info_bsd},
140 {print_object_filename_sysv,
141 print_archive_filename_sysv,
142 print_archive_member_sysv,
143 print_symbol_filename_sysv,
144 print_symbol_info_sysv},
145 {print_object_filename_posix,
146 print_archive_filename_posix,
147 print_archive_member_posix,
148 print_symbol_filename_posix,
149 print_symbol_info_posix}
152 /* Indices in `formats'. */
154 #define FORMAT_SYSV 1
155 #define FORMAT_POSIX 2
156 #define FORMAT_DEFAULT FORMAT_BSD
158 /* The output format to use. */
159 static struct output_fns *format = &formats[FORMAT_DEFAULT];
161 /* Command options. */
163 static int do_demangle = 0; /* Pretty print C++ symbol names. */
164 static int external_only = 0; /* Print external symbols only. */
165 static int defined_only = 0; /* Print defined symbols only. */
166 static int no_sort = 0; /* Don't sort; print syms in order found. */
167 static int print_debug_syms = 0;/* Print debugger-only symbols too. */
168 static int print_armap = 0; /* Describe __.SYMDEF data in archive files. */
169 static int print_size = 0; /* Print size of defined symbols. */
170 static int reverse_sort = 0; /* Sort in downward(alpha or numeric) order. */
171 static int sort_numerically = 0;/* Sort in numeric rather than alpha order. */
172 static int sort_by_size = 0; /* Sort by size of symbol. */
173 static int undefined_only = 0; /* Print undefined symbols only. */
174 static int dynamic = 0; /* Print dynamic symbols. */
175 static int show_version = 0; /* Show the version number. */
176 static int show_stats = 0; /* Show statistics. */
177 static int line_numbers = 0; /* Print line numbers for symbols. */
179 /* When to print the names of files. Not mutually exclusive in SYSV format. */
180 static int filename_per_file = 0; /* Once per file, on its own line. */
181 static int filename_per_symbol = 0; /* Once per symbol, at start of line. */
183 /* Print formats for printing a symbol value. */
185 static char value_format[] = "%08lx";
187 #if BFD_HOST_64BIT_LONG
188 static char value_format[] = "%016lx";
190 /* We don't use value_format for this case. */
194 static int print_width = 16;
196 static int print_width = 8;
198 static int print_radix = 16;
199 /* Print formats for printing stab info. */
200 static char other_format[] = "%02x";
201 static char desc_format[] = "%04x";
203 static char *target = NULL;
205 /* Used to cache the line numbers for a BFD. */
206 static bfd *lineno_cache_bfd;
207 static bfd *lineno_cache_rel_bfd;
209 #define OPTION_TARGET 200
211 static struct option long_options[] =
213 {"debug-syms", no_argument, &print_debug_syms, 1},
214 {"demangle", optional_argument, 0, 'C'},
215 {"dynamic", no_argument, &dynamic, 1},
216 {"extern-only", no_argument, &external_only, 1},
217 {"format", required_argument, 0, 'f'},
218 {"help", no_argument, 0, 'h'},
219 {"line-numbers", no_argument, 0, 'l'},
220 {"no-cplus", no_argument, &do_demangle, 0}, /* Linux compatibility. */
221 {"no-demangle", no_argument, &do_demangle, 0},
222 {"no-sort", no_argument, &no_sort, 1},
223 {"numeric-sort", no_argument, &sort_numerically, 1},
224 {"portability", no_argument, 0, 'P'},
225 {"print-armap", no_argument, &print_armap, 1},
226 {"print-file-name", no_argument, 0, 'o'},
227 {"print-size", no_argument, 0, 'S'},
228 {"radix", required_argument, 0, 't'},
229 {"reverse-sort", no_argument, &reverse_sort, 1},
230 {"size-sort", no_argument, &sort_by_size, 1},
231 {"stats", no_argument, &show_stats, 1},
232 {"target", required_argument, 0, OPTION_TARGET},
233 {"defined-only", no_argument, &defined_only, 1},
234 {"undefined-only", no_argument, &undefined_only, 1},
235 {"version", no_argument, &show_version, 1},
236 {0, no_argument, 0, 0}
239 /* Some error-reporting functions. */
242 usage (stream, status)
246 fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
247 fprintf (stream, _(" List symbols in [file(s)] (a.out by default).\n"));
248 fprintf (stream, _(" The options are:\n\
249 -a, --debug-syms Display debugger-only symbols\n\
250 -A, --print-file-name Print name of the input file before every symbol\n\
251 -B Same as --format=bsd\n\
252 -C, --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
253 The STYLE, if specified, can be `auto' (the default),\n\
254 `gnu', 'lucid', 'arm', 'hp', 'edg' or 'gnu-v3'\n\
255 --no-demangle Do not demangle low-level symbol names\n\
256 -D, --dynamic Display dynamic symbols instead of normal symbols\n\
257 --defined-only Display only defined symbols\n\
259 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
260 `sysv' or `posix'. The default is `bsd'\n\
261 -g, --extern-only Display only external symbols\n\
262 -l, --line-numbers Use debugging information to find a filename and\n\
263 line number for each symbol\n\
264 -n, --numeric-sort Sort symbols numerically by address\n\
266 -p, --no-sort Do not sort the symbols\n\
267 -P, --portability Same as --format=posix\n\
268 -r, --reverse-sort Reverse the sense of the sort\n\
269 -S, --print-size Print size of defined symbols\n\
270 -s, --print-armap Include index for symbols from archive members\n\
271 --size-sort Sort symbols by size\n\
272 -t, --radix=RADIX Use RADIX for printing symbol values\n\
273 --target=BFDNAME Specify the target object format as BFDNAME\n\
274 -u, --undefined-only Display only undefined symbols\n\
275 -X 32_64 (ignored)\n\
276 -h, --help Display this information\n\
277 -V, --version Display this program's version number\n\
279 list_supported_targets (program_name, stream);
281 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
285 /* Set the radix for the symbol value and size according to RADIX. */
288 set_print_radix (radix)
302 value_format[4] = *radix;
304 #if BFD_HOST_64BIT_LONG
305 value_format[5] = *radix;
307 /* This case requires special handling for octal and decimal
311 other_format[3] = desc_format[3] = *radix;
314 fatal (_("%s: invalid radix"), radix);
319 set_output_format (f)
339 fatal (_("%s: invalid output format"), f);
341 format = &formats[i];
344 int main PARAMS ((int, char **));
354 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
355 setlocale (LC_MESSAGES, "");
357 #if defined (HAVE_SETLOCALE)
358 setlocale (LC_CTYPE, "");
359 setlocale (LC_COLLATE, "");
361 bindtextdomain (PACKAGE, LOCALEDIR);
362 textdomain (PACKAGE);
364 program_name = *argv;
365 xmalloc_set_program_name (program_name);
367 START_PROGRESS (program_name, 0);
370 set_default_bfd_target ();
372 while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrSst:uvVvX:",
373 long_options, (int *) 0)) != EOF)
378 print_debug_syms = 1;
382 filename_per_symbol = 1;
384 case 'B': /* For MIPS compatibility. */
385 set_output_format ("bsd");
391 enum demangling_styles style;
393 style = cplus_demangle_name_to_style (optarg);
394 if (style == unknown_demangling)
395 fatal (_("unknown demangling style `%s'"),
398 cplus_demangle_set_style (style);
405 /* Ignored for HP/UX compatibility. */
408 set_output_format (optarg);
421 sort_numerically = 1;
427 set_output_format ("posix");
439 set_print_radix (optarg);
448 /* Ignored for (partial) AIX compatibility. On AIX, the
449 argument has values 32, 64, or 32_64, and specfies that
450 only 32-bit, only 64-bit, or both kinds of objects should
451 be examined. The default is 32. So plain AIX nm on a
452 library archive with both kinds of objects will ignore
453 the 64-bit ones. For GNU nm, the default is and always
454 has been -X 32_64, and other options are not supported. */
455 if (strcmp (optarg, "32_64") != 0)
456 fatal (_("Only -X 32_64 is supported"));
459 case OPTION_TARGET: /* --target */
463 case 0: /* A long option that just sets a flag. */
472 print_version ("nm");
474 /* OK, all options now parsed. If no filename specified, do a.out. */
476 return !display_file ("a.out");
480 if (argc - optind > 1)
481 filename_per_file = 1;
483 /* We were given several filenames to do. */
484 while (optind < argc)
487 if (!display_file (argv[optind++]))
491 END_PROGRESS (program_name);
496 char *lim = (char *) sbrk (0);
498 non_fatal (_("data size %ld"), (long) (lim - (char *) &environ));
507 get_symbol_type (type)
510 static char buff [32];
514 case STT_NOTYPE: return "NOTYPE";
515 case STT_OBJECT: return "OBJECT";
516 case STT_FUNC: return "FUNC";
517 case STT_SECTION: return "SECTION";
518 case STT_FILE: return "FILE";
519 case STT_COMMON: return "COMMON";
520 case STT_TLS: return "TLS";
522 if (type >= STT_LOPROC && type <= STT_HIPROC)
523 sprintf (buff, _("<processor specific>: %d"), type);
524 else if (type >= STT_LOOS && type <= STT_HIOS)
525 sprintf (buff, _("<OS specific>: %d"), type);
527 sprintf (buff, _("<unknown>: %d"), type);
533 display_archive (file)
537 bfd *last_arfile = NULL;
540 (*format->print_archive_filename) (bfd_get_filename (file));
543 print_symdef_entry (file);
549 arfile = bfd_openr_next_archived_file (file, arfile);
553 if (bfd_get_error () != bfd_error_no_more_archived_files)
554 bfd_fatal (bfd_get_filename (file));
558 if (bfd_check_format_matches (arfile, bfd_object, &matching))
562 bfd_sprintf_vma (arfile, buf, (bfd_vma) -1);
563 print_width = strlen (buf);
564 (*format->print_archive_member) (bfd_get_filename (file),
565 bfd_get_filename (arfile));
566 display_rel_file (arfile, file);
570 bfd_nonfatal (bfd_get_filename (arfile));
571 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
573 list_matching_formats (matching);
578 if (last_arfile != NULL)
580 bfd_close (last_arfile);
581 lineno_cache_bfd = NULL;
582 lineno_cache_rel_bfd = NULL;
584 last_arfile = arfile;
587 if (last_arfile != NULL)
589 bfd_close (last_arfile);
590 lineno_cache_bfd = NULL;
591 lineno_cache_rel_bfd = NULL;
596 display_file (filename)
599 boolean retval = true;
603 file = bfd_openr (filename, target);
606 bfd_nonfatal (filename);
610 if (bfd_check_format (file, bfd_archive))
612 display_archive (file);
614 else if (bfd_check_format_matches (file, bfd_object, &matching))
618 bfd_sprintf_vma (file, buf, (bfd_vma) -1);
619 print_width = strlen (buf);
620 (*format->print_object_filename) (filename);
621 display_rel_file (file, NULL);
625 bfd_nonfatal (filename);
626 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
628 list_matching_formats (matching);
634 if (bfd_close (file) == false)
635 bfd_fatal (filename);
637 lineno_cache_bfd = NULL;
638 lineno_cache_rel_bfd = NULL;
643 /* These globals are used to pass information into the sorting
645 static bfd *sort_bfd;
646 static boolean sort_dynamic;
647 static asymbol *sort_x;
648 static asymbol *sort_y;
650 /* Symbol-sorting predicates */
651 #define valueof(x) ((x)->section->vma + (x)->value)
653 /* Numeric sorts. Undefined symbols are always considered "less than"
654 defined symbols with zero values. Common symbols are not treated
655 specially -- i.e., their sizes are used as their "values". */
658 numeric_forward (P_x, P_y)
665 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
666 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
667 if (x == NULL || y == NULL)
668 bfd_fatal (bfd_get_filename (sort_bfd));
670 xs = bfd_get_section (x);
671 ys = bfd_get_section (y);
673 if (bfd_is_und_section (xs))
675 if (! bfd_is_und_section (ys))
678 else if (bfd_is_und_section (ys))
680 else if (valueof (x) != valueof (y))
681 return valueof (x) < valueof (y) ? -1 : 1;
683 return non_numeric_forward (P_x, P_y);
687 numeric_reverse (x, y)
691 return - numeric_forward (x, y);
695 non_numeric_forward (P_x, P_y)
702 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
703 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
704 if (x == NULL || y == NULL)
705 bfd_fatal (bfd_get_filename (sort_bfd));
707 xn = bfd_asymbol_name (x);
708 yn = bfd_asymbol_name (y);
716 /* Solaris 2.5 has a bug in strcoll.
717 strcoll returns invalid values when confronted with empty strings. */
723 return strcoll (xn, yn);
725 return strcmp (xn, yn);
730 non_numeric_reverse (x, y)
734 return - non_numeric_forward (x, y);
737 static int (*(sorters[2][2])) PARAMS ((const PTR, const PTR)) =
739 { non_numeric_forward, non_numeric_reverse },
740 { numeric_forward, numeric_reverse }
743 /* This sort routine is used by sort_symbols_by_size. It is similar
744 to numeric_forward, but when symbols have the same value it sorts
745 by section VMA. This simplifies the sort_symbols_by_size code
746 which handles symbols at the end of sections. Also, this routine
747 tries to sort file names before other symbols with the same value.
748 That will make the file name have a zero size, which will make
749 sort_symbols_by_size choose the non file name symbol, leading to
750 more meaningful output. For similar reasons, this code sorts
751 gnu_compiled_* and gcc2_compiled before other symbols with the same
755 size_forward1 (P_x, P_y)
765 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
766 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
767 if (x == NULL || y == NULL)
768 bfd_fatal (bfd_get_filename (sort_bfd));
770 xs = bfd_get_section (x);
771 ys = bfd_get_section (y);
773 if (bfd_is_und_section (xs))
775 if (bfd_is_und_section (ys))
778 if (valueof (x) != valueof (y))
779 return valueof (x) < valueof (y) ? -1 : 1;
781 if (xs->vma != ys->vma)
782 return xs->vma < ys->vma ? -1 : 1;
784 xn = bfd_asymbol_name (x);
785 yn = bfd_asymbol_name (y);
789 /* The symbols gnu_compiled and gcc2_compiled convey even less
790 information than the file name, so sort them out first. */
792 xf = (strstr (xn, "gnu_compiled") != NULL
793 || strstr (xn, "gcc2_compiled") != NULL);
794 yf = (strstr (yn, "gnu_compiled") != NULL
795 || strstr (yn, "gcc2_compiled") != NULL);
802 /* We use a heuristic for the file name. It may not work on non
803 Unix systems, but it doesn't really matter; the only difference
804 is precisely which symbol names get printed. */
806 #define file_symbol(s, sn, snl) \
807 (((s)->flags & BSF_FILE) != 0 \
808 || ((sn)[(snl) - 2] == '.' \
809 && ((sn)[(snl) - 1] == 'o' \
810 || (sn)[(snl) - 1] == 'a')))
812 xf = file_symbol (x, xn, xnl);
813 yf = file_symbol (y, yn, ynl);
820 return non_numeric_forward (P_x, P_y);
823 /* This sort routine is used by sort_symbols_by_size. It is sorting
824 an array of size_sym structures into size order. */
827 size_forward2 (P_x, P_y)
831 const struct size_sym *x = (const struct size_sym *) P_x;
832 const struct size_sym *y = (const struct size_sym *) P_y;
834 if (x->size < y->size)
835 return reverse_sort ? 1 : -1;
836 else if (x->size > y->size)
837 return reverse_sort ? -1 : 1;
839 return sorters[0][reverse_sort] (x->minisym, y->minisym);
842 /* Sort the symbols by size. ELF provides a size but for other formats
843 we have to make a guess by assuming that the difference between the
844 address of a symbol and the address of the next higher symbol is the
848 sort_symbols_by_size (abfd, dynamic, minisyms, symcount, size, symsizesp)
854 struct size_sym **symsizesp;
856 struct size_sym *symsizes;
857 bfd_byte *from, *fromend;
859 asymbol *store_sym, *store_next;
861 qsort (minisyms, symcount, size, size_forward1);
863 /* We are going to return a special set of symbols and sizes to
865 symsizes = (struct size_sym *) xmalloc (symcount * sizeof (struct size_sym));
866 *symsizesp = symsizes;
868 /* Note that filter_symbols has already removed all absolute and
869 undefined symbols. Here we remove all symbols whose size winds
871 from = (bfd_byte *) minisyms;
872 fromend = from + symcount * size;
879 sym = bfd_minisymbol_to_symbol (abfd, dynamic, (const PTR) from,
882 bfd_fatal (bfd_get_filename (abfd));
885 for (; from < fromend; from += size)
892 if (from + size < fromend)
894 next = bfd_minisymbol_to_symbol (abfd,
896 (const PTR) (from + size),
899 bfd_fatal (bfd_get_filename (abfd));
904 sec = bfd_get_section (sym);
906 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
907 sz = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
908 else if (bfd_is_com_section (sec))
912 if (from + size < fromend
913 && sec == bfd_get_section (next))
914 sz = valueof (next) - valueof (sym);
916 sz = (bfd_get_section_vma (abfd, sec)
917 + bfd_section_size (abfd, sec)
923 symsizes->minisym = (const PTR) from;
931 store_sym = store_next;
935 symcount = symsizes - *symsizesp;
937 /* We must now sort again by size. */
938 qsort ((PTR) *symsizesp, symcount, sizeof (struct size_sym), size_forward2);
943 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
946 display_rel_file (abfd, archive_bfd)
953 struct size_sym *symsizes;
957 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
961 symcount = bfd_read_minisymbols (abfd, dynamic, &minisyms, &size);
963 bfd_fatal (bfd_get_filename (abfd));
968 /* Discard the symbols we don't want to print.
969 It's OK to do this in place; we'll free the storage anyway
972 symcount = filter_symbols (abfd, dynamic, minisyms, symcount, size);
978 sort_dynamic = dynamic;
979 sort_x = bfd_make_empty_symbol (abfd);
980 sort_y = bfd_make_empty_symbol (abfd);
981 if (sort_x == NULL || sort_y == NULL)
982 bfd_fatal (bfd_get_filename (abfd));
985 qsort (minisyms, symcount, size,
986 sorters[sort_numerically][reverse_sort]);
988 symcount = sort_symbols_by_size (abfd, dynamic, minisyms, symcount,
993 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd);
995 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd);
1000 /* Choose which symbol entries to print;
1001 compact them downward to get rid of the rest.
1002 Return the number of symbols to be printed. */
1005 filter_symbols (abfd, dynamic, minisyms, symcount, size)
1012 bfd_byte *from, *fromend, *to;
1015 store = bfd_make_empty_symbol (abfd);
1017 bfd_fatal (bfd_get_filename (abfd));
1019 from = (bfd_byte *) minisyms;
1020 fromend = from + symcount * size;
1021 to = (bfd_byte *) minisyms;
1023 for (; from < fromend; from += size)
1030 sym = bfd_minisymbol_to_symbol (abfd, dynamic, (const PTR) from, store);
1032 bfd_fatal (bfd_get_filename (abfd));
1035 keep = bfd_is_und_section (sym->section);
1036 else if (external_only)
1037 keep = ((sym->flags & BSF_GLOBAL) != 0
1038 || (sym->flags & BSF_WEAK) != 0
1039 || bfd_is_und_section (sym->section)
1040 || bfd_is_com_section (sym->section));
1045 && ! print_debug_syms
1046 && (sym->flags & BSF_DEBUGGING) != 0)
1051 && (bfd_is_abs_section (sym->section)
1052 || bfd_is_und_section (sym->section)))
1058 if (bfd_is_und_section (sym->section))
1064 memcpy (to, from, size);
1069 return (to - (bfd_byte *) minisyms) / size;
1072 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
1073 demangling it if requested. */
1076 print_symname (format, name, abfd)
1081 if (do_demangle && *name)
1083 char *res = demangle (abfd, name);
1085 printf (format, res);
1090 printf (format, name);
1093 /* Print the symbols. If ARCHIVE_BFD is non-NULL, it is the archive
1097 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd)
1106 bfd_byte *from, *fromend;
1108 store = bfd_make_empty_symbol (abfd);
1110 bfd_fatal (bfd_get_filename (abfd));
1112 from = (bfd_byte *) minisyms;
1113 fromend = from + symcount * size;
1114 for (; from < fromend; from += size)
1118 sym = bfd_minisymbol_to_symbol (abfd, dynamic, from, store);
1120 bfd_fatal (bfd_get_filename (abfd));
1122 print_symbol (abfd, sym, (bfd_vma) 0, archive_bfd);
1126 /* Print the symbols when sorting by size. */
1129 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd)
1132 struct size_sym *symsizes;
1137 struct size_sym *from, *fromend;
1139 store = bfd_make_empty_symbol (abfd);
1141 bfd_fatal (bfd_get_filename (abfd));
1144 fromend = from + symcount;
1145 for (; from < fromend; from++)
1150 sym = bfd_minisymbol_to_symbol (abfd, dynamic, from->minisym, store);
1152 bfd_fatal (bfd_get_filename (abfd));
1154 /* For elf we have already computed the correct symbol size. */
1155 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1158 ssize = from->size - bfd_section_vma (abfd, bfd_get_section (sym));
1160 print_symbol (abfd, sym, ssize, archive_bfd);
1164 /* Print a single symbol. */
1167 print_symbol (abfd, sym, ssize, archive_bfd)
1175 (*format->print_symbol_filename) (archive_bfd, abfd);
1179 if (bfd_is_und_section (bfd_get_section (sym)))
1180 print_symname ("%s", bfd_asymbol_name (sym), abfd);
1184 symbol_info syminfo;
1185 struct extended_symbol_info info;
1187 bfd_get_symbol_info (abfd, sym, &syminfo);
1188 info.sinfo = &syminfo;
1190 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1191 info.elfinfo = (elf_symbol_type *) sym;
1193 info.elfinfo = NULL;
1194 (*format->print_symbol_info) (&info, abfd);
1199 static asymbol **syms;
1200 static long symcount;
1201 const char *filename, *functionname;
1202 unsigned int lineno;
1204 /* We need to get the canonical symbols in order to call
1205 bfd_find_nearest_line. This is inefficient, but, then, you
1206 don't have to use --line-numbers. */
1207 if (abfd != lineno_cache_bfd && syms != NULL)
1216 symsize = bfd_get_symtab_upper_bound (abfd);
1218 bfd_fatal (bfd_get_filename (abfd));
1219 syms = (asymbol **) xmalloc (symsize);
1220 symcount = bfd_canonicalize_symtab (abfd, syms);
1222 bfd_fatal (bfd_get_filename (abfd));
1223 lineno_cache_bfd = abfd;
1226 if (bfd_is_und_section (bfd_get_section (sym)))
1228 static asection **secs;
1229 static arelent ***relocs;
1230 static long *relcount;
1231 static unsigned int seccount;
1233 const char *symname;
1235 /* For an undefined symbol, we try to find a reloc for the
1236 symbol, and print the line number of the reloc. */
1237 if (abfd != lineno_cache_rel_bfd && relocs != NULL)
1239 for (i = 0; i < seccount; i++)
1240 if (relocs[i] != NULL)
1252 struct get_relocs_info info;
1254 seccount = bfd_count_sections (abfd);
1256 secs = (asection **) xmalloc (seccount * sizeof *secs);
1257 relocs = (arelent ***) xmalloc (seccount * sizeof *relocs);
1258 relcount = (long *) xmalloc (seccount * sizeof *relcount);
1261 info.relocs = relocs;
1262 info.relcount = relcount;
1264 bfd_map_over_sections (abfd, get_relocs, (PTR) &info);
1265 lineno_cache_rel_bfd = abfd;
1268 symname = bfd_asymbol_name (sym);
1269 for (i = 0; i < seccount; i++)
1273 for (j = 0; j < relcount[i]; j++)
1278 if (r->sym_ptr_ptr != NULL
1279 && (*r->sym_ptr_ptr)->section == sym->section
1280 && (*r->sym_ptr_ptr)->value == sym->value
1282 bfd_asymbol_name (*r->sym_ptr_ptr)) == 0
1283 && bfd_find_nearest_line (abfd, secs[i], syms,
1284 r->address, &filename,
1285 &functionname, &lineno)
1286 && filename != NULL)
1288 /* We only print the first one we find. */
1289 printf ("\t%s:%u", filename, lineno);
1296 else if (bfd_get_section (sym)->owner == abfd)
1298 if (bfd_find_nearest_line (abfd, bfd_get_section (sym), syms,
1299 sym->value, &filename, &functionname,
1304 printf ("\t%s:%u", filename, lineno);
1312 /* The following 3 groups of functions are called unconditionally,
1313 once at the start of processing each file of the appropriate type.
1314 They should check `filename_per_file' and `filename_per_symbol',
1315 as appropriate for their output format, to determine whether to
1318 /* Print the name of an object file given on the command line. */
1321 print_object_filename_bsd (filename)
1324 if (filename_per_file && !filename_per_symbol)
1325 printf ("\n%s:\n", filename);
1329 print_object_filename_sysv (filename)
1333 printf (_("\n\nUndefined symbols from %s:\n\n"), filename);
1335 printf (_("\n\nSymbols from %s:\n\n"), filename);
1336 if (print_width == 8)
1338 Name Value Class Type Size Line Section\n\n"));
1341 Name Value Class Type Size Line Section\n\n"));
1345 print_object_filename_posix (filename)
1348 if (filename_per_file && !filename_per_symbol)
1349 printf ("%s:\n", filename);
1352 /* Print the name of an archive file given on the command line. */
1355 print_archive_filename_bsd (filename)
1358 if (filename_per_file)
1359 printf ("\n%s:\n", filename);
1363 print_archive_filename_sysv (filename)
1364 char *filename ATTRIBUTE_UNUSED;
1369 print_archive_filename_posix (filename)
1370 char *filename ATTRIBUTE_UNUSED;
1374 /* Print the name of an archive member file. */
1377 print_archive_member_bsd (archive, filename)
1378 char *archive ATTRIBUTE_UNUSED;
1379 const char *filename;
1381 if (!filename_per_symbol)
1382 printf ("\n%s:\n", filename);
1386 print_archive_member_sysv (archive, filename)
1388 const char *filename;
1391 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename);
1393 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename);
1394 if (print_width == 8)
1396 Name Value Class Type Size Line Section\n\n"));
1399 Name Value Class Type Size Line Section\n\n"));
1403 print_archive_member_posix (archive, filename)
1405 const char *filename;
1407 if (!filename_per_symbol)
1408 printf ("%s[%s]:\n", archive, filename);
1411 /* Print the name of the file (and archive, if there is one)
1412 containing a symbol. */
1415 print_symbol_filename_bsd (archive_bfd, abfd)
1416 bfd *archive_bfd, *abfd;
1418 if (filename_per_symbol)
1421 printf ("%s:", bfd_get_filename (archive_bfd));
1422 printf ("%s:", bfd_get_filename (abfd));
1427 print_symbol_filename_sysv (archive_bfd, abfd)
1428 bfd *archive_bfd, *abfd;
1430 if (filename_per_symbol)
1433 printf ("%s:", bfd_get_filename (archive_bfd));
1434 printf ("%s:", bfd_get_filename (abfd));
1439 print_symbol_filename_posix (archive_bfd, abfd)
1440 bfd *archive_bfd, *abfd;
1442 if (filename_per_symbol)
1445 printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
1446 bfd_get_filename (abfd));
1448 printf ("%s: ", bfd_get_filename (abfd));
1452 /* Print a symbol value. */
1455 print_value (abfd, val)
1456 bfd *abfd ATTRIBUTE_UNUSED;
1459 #if ! defined (BFD64) || BFD_HOST_64BIT_LONG
1460 printf (value_format, val);
1462 /* We have a 64 bit value to print, but the host is only 32 bit. */
1463 if (print_radix == 16)
1464 bfd_fprintf_vma (abfd, stdout, val);
1470 s = buf + sizeof buf;
1474 *--s = (val % print_radix) + '0';
1477 while ((buf + sizeof buf - 1) - s < 16)
1484 /* Print a line of information about a symbol. */
1487 print_symbol_info_bsd (info, abfd)
1488 struct extended_symbol_info *info;
1491 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1493 if (print_width == 16)
1499 print_value (abfd, SYM_VALUE (info));
1501 if (print_size && SYM_SIZE (info))
1504 print_value (abfd, SYM_SIZE (info));
1508 printf (" %c", SYM_TYPE (info));
1510 if (SYM_TYPE (info) == '-')
1514 printf (other_format, SYM_STAB_OTHER (info));
1516 printf (desc_format, SYM_STAB_DESC (info));
1517 printf (" %5s", SYM_STAB_NAME (info));
1519 print_symname (" %s", SYM_NAME (info), abfd);
1523 print_symbol_info_sysv (info, abfd)
1524 struct extended_symbol_info *info;
1527 print_symname ("%-20s|", SYM_NAME (info), abfd);
1529 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1531 if (print_width == 8)
1537 print_value (abfd, SYM_VALUE (info));
1539 printf ("| %c |", SYM_TYPE (info));
1541 if (SYM_TYPE (info) == '-')
1544 printf ("%18s| ", SYM_STAB_NAME (info)); /* (C) Type */
1545 printf (desc_format, SYM_STAB_DESC (info)); /* Size */
1546 printf ("| |"); /* Line, Section */
1550 /* Type, Size, Line, Section */
1553 get_symbol_type (ELF_ST_TYPE (info->elfinfo->internal_elf_sym.st_info)));
1557 if (SYM_SIZE (info))
1558 print_value (abfd, SYM_SIZE (info));
1561 if (print_width == 8)
1568 printf("| |%s", info->elfinfo->symbol.section->name);
1575 print_symbol_info_posix (info, abfd)
1576 struct extended_symbol_info *info;
1579 print_symname ("%s ", SYM_NAME (info), abfd);
1580 printf ("%c ", SYM_TYPE (info));
1582 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1586 print_value (abfd, SYM_VALUE (info));
1588 if (SYM_SIZE (info))
1589 print_value (abfd, SYM_SIZE (info));
1594 print_symdef_entry (abfd)
1597 symindex idx = BFD_NO_MORE_SYMBOLS;
1599 boolean everprinted = false;
1601 for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
1602 idx != BFD_NO_MORE_SYMBOLS;
1603 idx = bfd_get_next_mapent (abfd, idx, &thesym))
1608 printf (_("\nArchive index:\n"));
1611 elt = bfd_get_elt_at_index (abfd, idx);
1613 bfd_fatal ("bfd_get_elt_at_index");
1614 if (thesym->name != (char *) NULL)
1616 print_symname ("%s", thesym->name, abfd);
1617 printf (" in %s\n", bfd_get_filename (elt));
1622 /* This function is used to get the relocs for a particular section.
1623 It is called via bfd_map_over_sections. */
1626 get_relocs (abfd, sec, dataarg)
1631 struct get_relocs_info *data = (struct get_relocs_info *) dataarg;
1635 if ((sec->flags & SEC_RELOC) == 0)
1637 *data->relocs = NULL;
1638 *data->relcount = 0;
1644 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1646 bfd_fatal (bfd_get_filename (abfd));
1648 *data->relocs = (arelent **) xmalloc (relsize);
1649 *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs,
1651 if (*data->relcount < 0)
1652 bfd_fatal (bfd_get_filename (abfd));