1 /* nm.c -- Describe symbol table of a rel file.
2 Copyright 1991, 92, 93, 94, 95, 96, 97, 1998 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "aout/stab_gnu.h"
25 #include "aout/ranlib.h"
27 #include "libiberty.h"
29 /* When sorting by size, we use this structure to hold the size and a
30 pointer to the minisymbol. */
38 /* When fetching relocs, we use this structure to pass information to
41 struct get_relocs_info
50 usage PARAMS ((FILE *, int));
53 set_print_radix PARAMS ((char *));
56 set_output_format PARAMS ((char *));
59 display_archive PARAMS ((bfd *));
62 display_file PARAMS ((char *filename));
65 display_rel_file PARAMS ((bfd * file, bfd * archive));
68 filter_symbols PARAMS ((bfd *, boolean, PTR, long, unsigned int));
71 sort_symbols_by_size PARAMS ((bfd *, boolean, PTR, long, unsigned int,
75 print_symbols PARAMS ((bfd *, boolean, PTR, long, unsigned int, bfd *));
78 print_size_symbols PARAMS ((bfd *, boolean, struct size_sym *, long, bfd *));
81 print_symname PARAMS ((const char *, const char *, bfd *));
84 print_symbol PARAMS ((bfd *, asymbol *, bfd *));
87 print_symdef_entry PARAMS ((bfd * abfd));
89 /* The sorting functions. */
92 numeric_forward PARAMS ((const PTR, const PTR));
95 numeric_reverse PARAMS ((const PTR, const PTR));
98 non_numeric_forward PARAMS ((const PTR, const PTR));
101 non_numeric_reverse PARAMS ((const PTR, const PTR));
104 size_forward1 PARAMS ((const PTR, const PTR));
107 size_forward2 PARAMS ((const PTR, const PTR));
109 /* The output formatting functions. */
112 print_object_filename_bsd PARAMS ((char *filename));
115 print_object_filename_sysv PARAMS ((char *filename));
118 print_object_filename_posix PARAMS ((char *filename));
122 print_archive_filename_bsd PARAMS ((char *filename));
125 print_archive_filename_sysv PARAMS ((char *filename));
128 print_archive_filename_posix PARAMS ((char *filename));
132 print_archive_member_bsd PARAMS ((char *archive, CONST char *filename));
135 print_archive_member_sysv PARAMS ((char *archive, CONST char *filename));
138 print_archive_member_posix PARAMS ((char *archive, CONST char *filename));
142 print_symbol_filename_bsd PARAMS ((bfd * archive_bfd, bfd * abfd));
145 print_symbol_filename_sysv PARAMS ((bfd * archive_bfd, bfd * abfd));
148 print_symbol_filename_posix PARAMS ((bfd * archive_bfd, bfd * abfd));
152 print_value PARAMS ((bfd_vma));
155 print_symbol_info_bsd PARAMS ((symbol_info * info, bfd * abfd));
158 print_symbol_info_sysv PARAMS ((symbol_info * info, bfd * abfd));
161 print_symbol_info_posix PARAMS ((symbol_info * info, bfd * abfd));
164 get_relocs PARAMS ((bfd *, asection *, PTR));
166 /* Support for different output formats. */
169 /* Print the name of an object file given on the command line. */
170 void (*print_object_filename) PARAMS ((char *filename));
172 /* Print the name of an archive file given on the command line. */
173 void (*print_archive_filename) PARAMS ((char *filename));
175 /* Print the name of an archive member file. */
176 void (*print_archive_member) PARAMS ((char *archive, CONST char *filename));
178 /* Print the name of the file (and archive, if there is one)
179 containing a symbol. */
180 void (*print_symbol_filename) PARAMS ((bfd * archive_bfd, bfd * abfd));
182 /* Print a line of information about a symbol. */
183 void (*print_symbol_info) PARAMS ((symbol_info * info, bfd * abfd));
185 static struct output_fns formats[] =
187 {print_object_filename_bsd,
188 print_archive_filename_bsd,
189 print_archive_member_bsd,
190 print_symbol_filename_bsd,
191 print_symbol_info_bsd},
192 {print_object_filename_sysv,
193 print_archive_filename_sysv,
194 print_archive_member_sysv,
195 print_symbol_filename_sysv,
196 print_symbol_info_sysv},
197 {print_object_filename_posix,
198 print_archive_filename_posix,
199 print_archive_member_posix,
200 print_symbol_filename_posix,
201 print_symbol_info_posix}
204 /* Indices in `formats'. */
206 #define FORMAT_SYSV 1
207 #define FORMAT_POSIX 2
208 #define FORMAT_DEFAULT FORMAT_BSD
210 /* The output format to use. */
211 static struct output_fns *format = &formats[FORMAT_DEFAULT];
214 /* Command options. */
216 static int do_demangle = 0; /* Pretty print C++ symbol names. */
217 static int external_only = 0; /* print external symbols only */
218 static int defined_only = 0; /* Print defined symbols only */
219 static int no_sort = 0; /* don't sort; print syms in order found */
220 static int print_debug_syms = 0; /* print debugger-only symbols too */
221 static int print_armap = 0; /* describe __.SYMDEF data in archive files. */
222 static int reverse_sort = 0; /* sort in downward(alpha or numeric) order */
223 static int sort_numerically = 0; /* sort in numeric rather than alpha order */
224 static int sort_by_size = 0; /* sort by size of symbol */
225 static int undefined_only = 0; /* print undefined symbols only */
226 static int dynamic = 0; /* print dynamic symbols. */
227 static int show_version = 0; /* show the version number */
228 static int show_stats = 0; /* show statistics */
229 static int line_numbers = 0; /* print line numbers for symbols */
231 /* When to print the names of files. Not mutually exclusive in SYSV format. */
232 static int filename_per_file = 0; /* Once per file, on its own line. */
233 static int filename_per_symbol = 0; /* Once per symbol, at start of line. */
235 /* Print formats for printing a symbol value. */
237 static char value_format[] = "%08lx";
239 #if BFD_HOST_64BIT_LONG
240 static char value_format[] = "%016lx";
242 /* We don't use value_format for this case. */
245 static int print_radix = 16;
246 /* Print formats for printing stab info. */
247 static char other_format[] = "%02x";
248 static char desc_format[] = "%04x";
250 static char *target = NULL;
252 /* Used to cache the line numbers for a BFD. */
253 static bfd *lineno_cache_bfd;
254 static bfd *lineno_cache_rel_bfd;
256 static struct option long_options[] =
258 {"debug-syms", no_argument, &print_debug_syms, 1},
259 {"demangle", no_argument, &do_demangle, 1},
260 {"dynamic", no_argument, &dynamic, 1},
261 {"extern-only", no_argument, &external_only, 1},
262 {"format", required_argument, 0, 'f'},
263 {"help", no_argument, 0, 'h'},
264 {"line-numbers", no_argument, 0, 'l'},
265 {"no-cplus", no_argument, &do_demangle, 0}, /* Linux compatibility. */
266 {"no-demangle", no_argument, &do_demangle, 0},
267 {"no-sort", no_argument, &no_sort, 1},
268 {"numeric-sort", no_argument, &sort_numerically, 1},
269 {"portability", no_argument, 0, 'P'},
270 {"print-armap", no_argument, &print_armap, 1},
271 {"print-file-name", no_argument, 0, 'o'},
272 {"radix", required_argument, 0, 't'},
273 {"reverse-sort", no_argument, &reverse_sort, 1},
274 {"size-sort", no_argument, &sort_by_size, 1},
275 {"stats", no_argument, &show_stats, 1},
276 {"target", required_argument, 0, 200},
277 {"defined-only", no_argument, &defined_only, 1},
278 {"undefined-only", no_argument, &undefined_only, 1},
279 {"version", no_argument, &show_version, 1},
280 {0, no_argument, 0, 0}
283 /* Some error-reporting functions */
286 usage (stream, status)
290 fprintf (stream, _("\
291 Usage: %s [-aABCDglnopPrsuvV] [-t radix] [--radix=radix] [--target=bfdname]\n\
292 [--debug-syms] [--extern-only] [--print-armap] [--print-file-name]\n\
293 [--numeric-sort] [--no-sort] [--reverse-sort] [--size-sort]\n\
294 [--undefined-only] [--portability] [-f {bsd,sysv,posix}]\n\
295 [--format={bsd,sysv,posix}] [--demangle] [--no-demangle] [--dynamic]\n\
296 [--defined-only] [--line-numbers]\n\
297 [--version] [--help]\n\
300 list_supported_targets (program_name, stream);
306 /* Set the radix for the symbol value and size according to RADIX. */
309 set_print_radix (radix)
323 value_format[4] = *radix;
325 #if BFD_HOST_64BIT_LONG
326 value_format[5] = *radix;
328 /* This case requires special handling for octal and decimal
332 other_format[3] = desc_format[3] = *radix;
335 fprintf (stderr, _("%s: %s: invalid radix\n"), program_name, radix);
341 set_output_format (f)
361 fprintf (stderr, _("%s: %s: invalid output format\n"), program_name, f);
364 format = &formats[i];
375 setlocale (LC_MESSAGES, "");
376 bindtextdomain (PACKAGE, LOCALEDIR);
377 textdomain (PACKAGE);
379 program_name = *argv;
380 xmalloc_set_program_name (program_name);
382 START_PROGRESS (program_name, 0);
385 set_default_bfd_target ();
387 while ((c = getopt_long (argc, argv, "aABCDef:glnopPrst:uvV", long_options, (int *) 0)) != EOF)
392 print_debug_syms = 1;
396 filename_per_symbol = 1;
398 case 'B': /* For MIPS compatibility. */
399 set_output_format ("bsd");
408 /* Ignored for HP/UX compatibility. */
411 set_output_format (optarg);
423 sort_numerically = 1;
429 set_output_format ("posix");
438 set_print_radix (optarg);
447 case 200: /* --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 extern char **environ;
485 char *lim = (char *) sbrk (0);
487 fprintf (stderr, _("%s: data size %ld\n"), program_name,
488 (long) (lim - (char *) &environ));
497 display_archive (file)
501 bfd *last_arfile = NULL;
504 (*format->print_archive_filename) (bfd_get_filename (file));
507 print_symdef_entry (file);
513 arfile = bfd_openr_next_archived_file (file, arfile);
517 if (bfd_get_error () != bfd_error_no_more_archived_files)
518 bfd_fatal (bfd_get_filename (file));
522 if (bfd_check_format_matches (arfile, bfd_object, &matching))
524 (*format->print_archive_member) (bfd_get_filename (file),
525 bfd_get_filename (arfile));
526 display_rel_file (arfile, file);
530 bfd_nonfatal (bfd_get_filename (arfile));
531 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
533 list_matching_formats (matching);
538 if (last_arfile != NULL)
540 bfd_close (last_arfile);
541 lineno_cache_bfd = NULL;
542 lineno_cache_rel_bfd = NULL;
544 last_arfile = arfile;
547 if (last_arfile != NULL)
549 bfd_close (last_arfile);
550 lineno_cache_bfd = NULL;
551 lineno_cache_rel_bfd = NULL;
556 display_file (filename)
559 boolean retval = true;
563 file = bfd_openr (filename, target);
566 bfd_nonfatal (filename);
570 if (bfd_check_format (file, bfd_archive))
572 display_archive (file);
574 else if (bfd_check_format_matches (file, bfd_object, &matching))
576 (*format->print_object_filename) (filename);
577 display_rel_file (file, NULL);
581 bfd_nonfatal (filename);
582 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
584 list_matching_formats (matching);
590 if (bfd_close (file) == false)
591 bfd_fatal (filename);
593 lineno_cache_bfd = NULL;
594 lineno_cache_rel_bfd = NULL;
599 /* These globals are used to pass information into the sorting
601 static bfd *sort_bfd;
602 static boolean sort_dynamic;
603 static asymbol *sort_x;
604 static asymbol *sort_y;
606 /* Symbol-sorting predicates */
607 #define valueof(x) ((x)->section->vma + (x)->value)
609 /* Numeric sorts. Undefined symbols are always considered "less than"
610 defined symbols with zero values. Common symbols are not treated
611 specially -- i.e., their sizes are used as their "values". */
614 numeric_forward (P_x, P_y)
621 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
622 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
623 if (x == NULL || y == NULL)
624 bfd_fatal (bfd_get_filename (sort_bfd));
626 xs = bfd_get_section (x);
627 ys = bfd_get_section (y);
629 if (bfd_is_und_section (xs))
631 if (! bfd_is_und_section (ys))
634 else if (bfd_is_und_section (ys))
636 else if (valueof (x) != valueof (y))
637 return valueof (x) < valueof (y) ? -1 : 1;
639 return non_numeric_forward (P_x, P_y);
643 numeric_reverse (x, y)
647 return - numeric_forward (x, y);
651 non_numeric_forward (P_x, P_y)
658 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
659 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
660 if (x == NULL || y == NULL)
661 bfd_fatal (bfd_get_filename (sort_bfd));
663 xn = bfd_asymbol_name (x);
664 yn = bfd_asymbol_name (y);
666 return ((xn == NULL) ? ((yn == NULL) ? 0 : -1) :
667 ((yn == NULL) ? 1 : strcmp (xn, yn)));
671 non_numeric_reverse (x, y)
675 return - non_numeric_forward (x, y);
678 static int (*(sorters[2][2])) PARAMS ((const PTR, const PTR)) =
680 { non_numeric_forward, non_numeric_reverse },
681 { numeric_forward, numeric_reverse }
684 /* This sort routine is used by sort_symbols_by_size. It is similar
685 to numeric_forward, but when symbols have the same value it sorts
686 by section VMA. This simplifies the sort_symbols_by_size code
687 which handles symbols at the end of sections. Also, this routine
688 tries to sort file names before other symbols with the same value.
689 That will make the file name have a zero size, which will make
690 sort_symbols_by_size choose the non file name symbol, leading to
691 more meaningful output. For similar reasons, this code sorts
692 gnu_compiled_* and gcc2_compiled before other symbols with the same
696 size_forward1 (P_x, P_y)
706 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
707 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
708 if (x == NULL || y == NULL)
709 bfd_fatal (bfd_get_filename (sort_bfd));
711 xs = bfd_get_section (x);
712 ys = bfd_get_section (y);
714 if (bfd_is_und_section (xs))
716 if (bfd_is_und_section (ys))
719 if (valueof (x) != valueof (y))
720 return valueof (x) < valueof (y) ? -1 : 1;
722 if (xs->vma != ys->vma)
723 return xs->vma < ys->vma ? -1 : 1;
725 xn = bfd_asymbol_name (x);
726 yn = bfd_asymbol_name (y);
730 /* The symbols gnu_compiled and gcc2_compiled convey even less
731 information than the file name, so sort them out first. */
733 xf = (strstr (xn, "gnu_compiled") != NULL
734 || strstr (xn, "gcc2_compiled") != NULL);
735 yf = (strstr (yn, "gnu_compiled") != NULL
736 || strstr (yn, "gcc2_compiled") != NULL);
743 /* We use a heuristic for the file name. It may not work on non
744 Unix systems, but it doesn't really matter; the only difference
745 is precisely which symbol names get printed. */
747 #define file_symbol(s, sn, snl) \
748 (((s)->flags & BSF_FILE) != 0 \
749 || ((sn)[(snl) - 2] == '.' \
750 && ((sn)[(snl) - 1] == 'o' \
751 || (sn)[(snl) - 1] == 'a')))
753 xf = file_symbol (x, xn, xnl);
754 yf = file_symbol (y, yn, ynl);
761 return non_numeric_forward (P_x, P_y);
764 /* This sort routine is used by sort_symbols_by_size. It is sorting
765 an array of size_sym structures into size order. */
768 size_forward2 (P_x, P_y)
772 const struct size_sym *x = (const struct size_sym *) P_x;
773 const struct size_sym *y = (const struct size_sym *) P_y;
775 if (x->size < y->size)
776 return reverse_sort ? 1 : -1;
777 else if (x->size > y->size)
778 return reverse_sort ? -1 : 1;
780 return sorters[0][reverse_sort] (x->minisym, y->minisym);
783 /* Sort the symbols by size. We guess the size by assuming that the
784 difference between the address of a symbol and the address of the
785 next higher symbol is the size. FIXME: ELF actually stores a size
786 with each symbol. We should use it. */
789 sort_symbols_by_size (abfd, dynamic, minisyms, symcount, size, symsizesp)
795 struct size_sym **symsizesp;
797 struct size_sym *symsizes;
798 bfd_byte *from, *fromend;
800 asymbol *store_sym, *store_next;
802 qsort (minisyms, symcount, size, size_forward1);
804 /* We are going to return a special set of symbols and sizes to
806 symsizes = (struct size_sym *) xmalloc (symcount * sizeof (struct size_sym));
807 *symsizesp = symsizes;
809 /* Note that filter_symbols has already removed all absolute and
810 undefined symbols. Here we remove all symbols whose size winds
813 from = (bfd_byte *) minisyms;
814 fromend = from + symcount * size;
821 sym = bfd_minisymbol_to_symbol (abfd, dynamic, (const PTR) from,
824 bfd_fatal (bfd_get_filename (abfd));
827 for (; from < fromend; from += size)
834 if (from + size < fromend)
836 next = bfd_minisymbol_to_symbol (abfd,
838 (const PTR) (from + size),
841 bfd_fatal (bfd_get_filename (abfd));
846 sec = bfd_get_section (sym);
848 if (bfd_is_com_section (sec))
852 if (from + size < fromend
853 && sec == bfd_get_section (next))
854 sz = valueof (next) - valueof (sym);
856 sz = (bfd_get_section_vma (abfd, sec)
857 + bfd_section_size (abfd, sec)
863 symsizes->minisym = (const PTR) from;
871 store_sym = store_next;
875 symcount = symsizes - *symsizesp;
877 /* We must now sort again by size. */
878 qsort ((PTR) *symsizesp, symcount, sizeof (struct size_sym), size_forward2);
883 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
886 display_rel_file (abfd, archive_bfd)
893 struct size_sym *symsizes;
897 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
899 printf (_("No symbols in \"%s\".\n"), bfd_get_filename (abfd));
904 symcount = bfd_read_minisymbols (abfd, dynamic, &minisyms, &size);
906 bfd_fatal (bfd_get_filename (abfd));
910 fprintf (stderr, _("%s: no symbols\n"), bfd_get_filename (abfd));
914 /* Discard the symbols we don't want to print.
915 It's OK to do this in place; we'll free the storage anyway
918 symcount = filter_symbols (abfd, dynamic, minisyms, symcount, size);
924 sort_dynamic = dynamic;
925 sort_x = bfd_make_empty_symbol (abfd);
926 sort_y = bfd_make_empty_symbol (abfd);
927 if (sort_x == NULL || sort_y == NULL)
928 bfd_fatal (bfd_get_filename (abfd));
931 qsort (minisyms, symcount, size,
932 sorters[sort_numerically][reverse_sort]);
934 symcount = sort_symbols_by_size (abfd, dynamic, minisyms, symcount,
939 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd);
941 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd);
946 /* Choose which symbol entries to print;
947 compact them downward to get rid of the rest.
948 Return the number of symbols to be printed. */
951 filter_symbols (abfd, dynamic, minisyms, symcount, size)
958 bfd_byte *from, *fromend, *to;
961 store = bfd_make_empty_symbol (abfd);
963 bfd_fatal (bfd_get_filename (abfd));
965 from = (bfd_byte *) minisyms;
966 fromend = from + symcount * size;
967 to = (bfd_byte *) minisyms;
969 for (; from < fromend; from += size)
976 sym = bfd_minisymbol_to_symbol (abfd, dynamic, (const PTR) from, store);
978 bfd_fatal (bfd_get_filename (abfd));
981 keep = bfd_is_und_section (sym->section);
982 else if (external_only)
983 keep = ((sym->flags & BSF_GLOBAL) != 0
984 || (sym->flags & BSF_WEAK) != 0
985 || bfd_is_und_section (sym->section)
986 || bfd_is_com_section (sym->section));
991 && ! print_debug_syms
992 && (sym->flags & BSF_DEBUGGING) != 0)
997 && (bfd_is_abs_section (sym->section)
998 || bfd_is_und_section (sym->section)))
1004 if (bfd_is_und_section (sym->section))
1010 memcpy (to, from, size);
1015 return (to - (bfd_byte *) minisyms) / size;
1018 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
1019 demangling it if requested. */
1022 print_symname (format, name, abfd)
1027 if (do_demangle && *name)
1031 /* In this mode, give a user-level view of the symbol name
1032 even if it's not mangled; strip off any leading
1034 if (bfd_get_symbol_leading_char (abfd) == name[0])
1037 res = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
1040 printf (format, res);
1046 printf (format, name);
1049 /* Print the symbols. If ARCHIVE_BFD is non-NULL, it is the archive
1053 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd)
1062 bfd_byte *from, *fromend;
1064 store = bfd_make_empty_symbol (abfd);
1066 bfd_fatal (bfd_get_filename (abfd));
1068 from = (bfd_byte *) minisyms;
1069 fromend = from + symcount * size;
1070 for (; from < fromend; from += size)
1074 sym = bfd_minisymbol_to_symbol (abfd, dynamic, from, store);
1076 bfd_fatal (bfd_get_filename (abfd));
1078 print_symbol (abfd, sym, archive_bfd);
1082 /* Print the symbols when sorting by size. */
1085 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd)
1088 struct size_sym *symsizes;
1093 struct size_sym *from, *fromend;
1095 store = bfd_make_empty_symbol (abfd);
1097 bfd_fatal (bfd_get_filename (abfd));
1100 fromend = from + symcount;
1101 for (; from < fromend; from++)
1105 sym = bfd_minisymbol_to_symbol (abfd, dynamic, from->minisym, store);
1107 bfd_fatal (bfd_get_filename (abfd));
1109 /* Set the symbol value so that we actually display the symbol
1111 sym->value = from->size - bfd_section_vma (abfd, bfd_get_section (sym));
1113 print_symbol (abfd, sym, archive_bfd);
1117 /* Print a single symbol. */
1120 print_symbol (abfd, sym, archive_bfd)
1127 (*format->print_symbol_filename) (archive_bfd, abfd);
1131 if (bfd_is_und_section (bfd_get_section (sym)))
1132 print_symname ("%s", bfd_asymbol_name (sym), abfd);
1136 symbol_info syminfo;
1138 bfd_get_symbol_info (abfd, sym, &syminfo);
1139 (*format->print_symbol_info) (&syminfo, abfd);
1144 static asymbol **syms;
1145 static long symcount;
1146 const char *filename, *functionname;
1147 unsigned int lineno;
1149 /* We need to get the canonical symbols in order to call
1150 bfd_find_nearest_line. This is inefficient, but, then, you
1151 don't have to use --line-numbers. */
1152 if (abfd != lineno_cache_bfd && syms != NULL)
1161 symsize = bfd_get_symtab_upper_bound (abfd);
1163 bfd_fatal (bfd_get_filename (abfd));
1164 syms = (asymbol **) xmalloc (symsize);
1165 symcount = bfd_canonicalize_symtab (abfd, syms);
1167 bfd_fatal (bfd_get_filename (abfd));
1168 lineno_cache_bfd = abfd;
1171 if (bfd_is_und_section (bfd_get_section (sym)))
1173 static asection **secs;
1174 static arelent ***relocs;
1175 static long *relcount;
1176 static unsigned int seccount;
1178 const char *symname;
1180 /* For an undefined symbol, we try to find a reloc for the
1181 symbol, and print the line number of the reloc. */
1183 if (abfd != lineno_cache_rel_bfd && relocs != NULL)
1185 for (i = 0; i < seccount; i++)
1186 if (relocs[i] != NULL)
1198 struct get_relocs_info info;
1200 seccount = bfd_count_sections (abfd);
1202 secs = (asection **) xmalloc (seccount * sizeof *secs);
1203 relocs = (arelent ***) xmalloc (seccount * sizeof *relocs);
1204 relcount = (long *) xmalloc (seccount * sizeof *relcount);
1207 info.relocs = relocs;
1208 info.relcount = relcount;
1210 bfd_map_over_sections (abfd, get_relocs, (PTR) &info);
1211 lineno_cache_rel_bfd = abfd;
1214 symname = bfd_asymbol_name (sym);
1215 for (i = 0; i < seccount; i++)
1219 for (j = 0; j < relcount[i]; j++)
1224 if (r->sym_ptr_ptr != NULL
1225 && (*r->sym_ptr_ptr)->section == sym->section
1226 && (*r->sym_ptr_ptr)->value == sym->value
1228 bfd_asymbol_name (*r->sym_ptr_ptr)) == 0
1229 && bfd_find_nearest_line (abfd, secs[i], syms,
1230 r->address, &filename,
1231 &functionname, &lineno))
1233 /* We only print the first one we find. */
1234 printf ("\t%s:%u", filename, lineno);
1241 else if (bfd_get_section (sym)->owner == abfd)
1243 if (bfd_find_nearest_line (abfd, bfd_get_section (sym), syms,
1244 sym->value, &filename, &functionname,
1249 printf ("\t%s:%u", filename, lineno);
1257 /* The following 3 groups of functions are called unconditionally,
1258 once at the start of processing each file of the appropriate type.
1259 They should check `filename_per_file' and `filename_per_symbol',
1260 as appropriate for their output format, to determine whether to
1263 /* Print the name of an object file given on the command line. */
1266 print_object_filename_bsd (filename)
1269 if (filename_per_file && !filename_per_symbol)
1270 printf ("\n%s:\n", filename);
1274 print_object_filename_sysv (filename)
1278 printf (_("\n\nUndefined symbols from %s:\n\n"), filename);
1280 printf (_("\n\nSymbols from %s:\n\n"), filename);
1282 Name Value Class Type Size Line Section\n\n"));
1286 print_object_filename_posix (filename)
1289 if (filename_per_file && !filename_per_symbol)
1290 printf ("%s:\n", filename);
1293 /* Print the name of an archive file given on the command line. */
1296 print_archive_filename_bsd (filename)
1299 if (filename_per_file)
1300 printf ("\n%s:\n", filename);
1304 print_archive_filename_sysv (filename)
1310 print_archive_filename_posix (filename)
1315 /* Print the name of an archive member file. */
1318 print_archive_member_bsd (archive, filename)
1320 CONST char *filename;
1322 if (!filename_per_symbol)
1323 printf ("\n%s:\n", filename);
1327 print_archive_member_sysv (archive, filename)
1329 CONST char *filename;
1332 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename);
1334 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename);
1336 Name Value Class Type Size Line Section\n\n"));
1340 print_archive_member_posix (archive, filename)
1342 CONST char *filename;
1344 if (!filename_per_symbol)
1345 printf ("%s[%s]:\n", archive, filename);
1348 /* Print the name of the file (and archive, if there is one)
1349 containing a symbol. */
1352 print_symbol_filename_bsd (archive_bfd, abfd)
1353 bfd *archive_bfd, *abfd;
1355 if (filename_per_symbol)
1358 printf ("%s:", bfd_get_filename (archive_bfd));
1359 printf ("%s:", bfd_get_filename (abfd));
1364 print_symbol_filename_sysv (archive_bfd, abfd)
1365 bfd *archive_bfd, *abfd;
1367 if (filename_per_symbol)
1370 printf ("%s:", bfd_get_filename (archive_bfd));
1371 printf ("%s:", bfd_get_filename (abfd));
1376 print_symbol_filename_posix (archive_bfd, abfd)
1377 bfd *archive_bfd, *abfd;
1379 if (filename_per_symbol)
1382 printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
1383 bfd_get_filename (abfd));
1385 printf ("%s: ", bfd_get_filename (abfd));
1389 /* Print a symbol value. */
1395 #if ! defined (BFD64) || BFD_HOST_64BIT_LONG
1396 printf (value_format, val);
1398 /* We have a 64 bit value to print, but the host is only 32 bit. */
1399 if (print_radix == 16)
1400 fprintf_vma (stdout, val);
1406 s = buf + sizeof buf;
1410 *--s = (val % print_radix) + '0';
1413 while ((buf + sizeof buf - 1) - s < 16)
1420 /* Print a line of information about a symbol. */
1423 print_symbol_info_bsd (info, abfd)
1427 if (info->type == 'U')
1438 print_value (info->value);
1439 printf (" %c", info->type);
1440 if (info->type == '-')
1444 printf (other_format, info->stab_other);
1446 printf (desc_format, info->stab_desc);
1447 printf (" %5s", info->stab_name);
1449 print_symname (" %s", info->name, abfd);
1453 print_symbol_info_sysv (info, abfd)
1457 print_symname ("%-20s|", info->name, abfd); /* Name */
1458 if (info->type == 'U')
1459 printf (" "); /* Value */
1461 print_value (info->value);
1462 printf ("| %c |", info->type); /* Class */
1463 if (info->type == '-')
1466 printf ("%18s| ", info->stab_name); /* (C) Type */
1467 printf (desc_format, info->stab_desc); /* Size */
1468 printf ("| |"); /* Line, Section */
1471 printf (" | | |"); /* Type, Size, Line, Section */
1475 print_symbol_info_posix (info, abfd)
1479 print_symname ("%s ", info->name, abfd);
1480 printf ("%c ", info->type);
1481 if (info->type == 'U')
1484 print_value (info->value);
1485 /* POSIX.2 wants the symbol size printed here, when applicable;
1486 BFD currently doesn't provide it, so we take the easy way out by
1487 considering it to never be applicable. */
1491 print_symdef_entry (abfd)
1494 symindex idx = BFD_NO_MORE_SYMBOLS;
1496 boolean everprinted = false;
1498 for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
1499 idx != BFD_NO_MORE_SYMBOLS;
1500 idx = bfd_get_next_mapent (abfd, idx, &thesym))
1505 printf (_("\nArchive index:\n"));
1508 elt = bfd_get_elt_at_index (abfd, idx);
1510 bfd_fatal ("bfd_get_elt_at_index");
1511 if (thesym->name != (char *) NULL)
1513 print_symname ("%s", thesym->name, abfd);
1514 printf (" in %s\n", bfd_get_filename (elt));
1519 /* This function is used to get the relocs for a particular section.
1520 It is called via bfd_map_over_sections. */
1523 get_relocs (abfd, sec, dataarg)
1528 struct get_relocs_info *data = (struct get_relocs_info *) dataarg;
1532 if ((sec->flags & SEC_RELOC) == 0)
1534 *data->relocs = NULL;
1535 *data->relcount = 0;
1541 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1543 bfd_fatal (bfd_get_filename (abfd));
1545 *data->relocs = (arelent **) xmalloc (relsize);
1546 *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs,
1548 if (*data->relcount < 0)
1549 bfd_fatal (bfd_get_filename (abfd));