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
23 /* $FreeBSD: src/contrib/binutils/binutils/nm.c,v 1.3.6.6 2002/09/01 23:43:47 obrien Exp $ */
29 #include "aout/stab_gnu.h"
30 #include "aout/ranlib.h"
32 #include "libiberty.h"
34 /* When sorting by size, we use this structure to hold the size and a
35 pointer to the minisymbol. */
43 /* When fetching relocs, we use this structure to pass information to
46 struct get_relocs_info
55 usage PARAMS ((FILE *, int));
58 set_print_radix PARAMS ((char *));
61 set_output_format PARAMS ((char *));
64 display_archive PARAMS ((bfd *));
67 display_file PARAMS ((char *filename));
70 display_rel_file PARAMS ((bfd * file, bfd * archive));
73 filter_symbols PARAMS ((bfd *, boolean, PTR, long, unsigned int));
76 sort_symbols_by_size PARAMS ((bfd *, boolean, PTR, long, unsigned int,
80 print_symbols PARAMS ((bfd *, boolean, PTR, long, unsigned int, bfd *));
83 print_size_symbols PARAMS ((bfd *, boolean, struct size_sym *, long, bfd *));
86 print_symname PARAMS ((const char *, const char *, bfd *));
89 print_symbol PARAMS ((bfd *, asymbol *, bfd *));
92 print_symdef_entry PARAMS ((bfd * abfd));
94 /* The sorting functions. */
97 numeric_forward PARAMS ((const PTR, const PTR));
100 numeric_reverse PARAMS ((const PTR, const PTR));
103 non_numeric_forward PARAMS ((const PTR, const PTR));
106 non_numeric_reverse PARAMS ((const PTR, const PTR));
109 size_forward1 PARAMS ((const PTR, const PTR));
112 size_forward2 PARAMS ((const PTR, const PTR));
114 /* The output formatting functions. */
117 print_object_filename_bsd PARAMS ((char *filename));
120 print_object_filename_sysv PARAMS ((char *filename));
123 print_object_filename_posix PARAMS ((char *filename));
127 print_archive_filename_bsd PARAMS ((char *filename));
130 print_archive_filename_sysv PARAMS ((char *filename));
133 print_archive_filename_posix PARAMS ((char *filename));
137 print_archive_member_bsd PARAMS ((char *archive, CONST char *filename));
140 print_archive_member_sysv PARAMS ((char *archive, CONST char *filename));
143 print_archive_member_posix PARAMS ((char *archive, CONST char *filename));
147 print_symbol_filename_bsd PARAMS ((bfd * archive_bfd, bfd * abfd));
150 print_symbol_filename_sysv PARAMS ((bfd * archive_bfd, bfd * abfd));
153 print_symbol_filename_posix PARAMS ((bfd * archive_bfd, bfd * abfd));
157 print_value PARAMS ((bfd *, bfd_vma));
160 print_symbol_info_bsd PARAMS ((symbol_info * info, bfd * abfd));
163 print_symbol_info_sysv PARAMS ((symbol_info * info, bfd * abfd));
166 print_symbol_info_posix PARAMS ((symbol_info * info, bfd * abfd));
169 get_relocs PARAMS ((bfd *, asection *, PTR));
171 /* Support for different output formats. */
174 /* Print the name of an object file given on the command line. */
175 void (*print_object_filename) PARAMS ((char *filename));
177 /* Print the name of an archive file given on the command line. */
178 void (*print_archive_filename) PARAMS ((char *filename));
180 /* Print the name of an archive member file. */
181 void (*print_archive_member) PARAMS ((char *archive, CONST char *filename));
183 /* Print the name of the file (and archive, if there is one)
184 containing a symbol. */
185 void (*print_symbol_filename) PARAMS ((bfd * archive_bfd, bfd * abfd));
187 /* Print a line of information about a symbol. */
188 void (*print_symbol_info) PARAMS ((symbol_info * info, bfd * abfd));
190 static struct output_fns formats[] =
192 {print_object_filename_bsd,
193 print_archive_filename_bsd,
194 print_archive_member_bsd,
195 print_symbol_filename_bsd,
196 print_symbol_info_bsd},
197 {print_object_filename_sysv,
198 print_archive_filename_sysv,
199 print_archive_member_sysv,
200 print_symbol_filename_sysv,
201 print_symbol_info_sysv},
202 {print_object_filename_posix,
203 print_archive_filename_posix,
204 print_archive_member_posix,
205 print_symbol_filename_posix,
206 print_symbol_info_posix}
209 /* Indices in `formats'. */
211 #define FORMAT_SYSV 1
212 #define FORMAT_POSIX 2
213 #define FORMAT_DEFAULT FORMAT_BSD
215 /* The output format to use. */
216 static struct output_fns *format = &formats[FORMAT_DEFAULT];
219 /* Command options. */
221 static int do_demangle = 0; /* Pretty print C++ symbol names. */
222 static int external_only = 0; /* print external symbols only */
223 static int defined_only = 0; /* Print defined symbols only */
224 static int no_sort = 0; /* don't sort; print syms in order found */
225 static int print_debug_syms = 0; /* print debugger-only symbols too */
226 static int print_armap = 0; /* describe __.SYMDEF data in archive files. */
227 static int reverse_sort = 0; /* sort in downward(alpha or numeric) order */
228 static int sort_numerically = 0; /* sort in numeric rather than alpha order */
229 static int sort_by_size = 0; /* sort by size of symbol */
230 static int undefined_only = 0; /* print undefined symbols only */
231 static int dynamic = 0; /* print dynamic symbols. */
232 static int show_version = 0; /* show the version number */
233 static int show_stats = 0; /* show statistics */
234 static int line_numbers = 0; /* print line numbers for symbols */
236 /* When to print the names of files. Not mutually exclusive in SYSV format. */
237 static int filename_per_file = 0; /* Once per file, on its own line. */
238 static int filename_per_symbol = 0; /* Once per symbol, at start of line. */
240 /* Print formats for printing a symbol value. */
242 static char value_format[] = "%08lx";
244 #if BFD_HOST_64BIT_LONG
245 static char value_format[] = "%016lx";
247 /* We don't use value_format for this case. */
251 static int print_width = 16;
253 static int print_width = 8;
255 static int print_radix = 16;
256 /* Print formats for printing stab info. */
257 static char other_format[] = "%02x";
258 static char desc_format[] = "%04x";
260 static char *target = NULL;
262 /* Used to cache the line numbers for a BFD. */
263 static bfd *lineno_cache_bfd;
264 static bfd *lineno_cache_rel_bfd;
266 #define OPTION_TARGET 200
268 static struct option long_options[] =
270 {"debug-syms", no_argument, &print_debug_syms, 1},
271 {"demangle", optional_argument, 0, 'C'},
272 {"dynamic", no_argument, &dynamic, 1},
273 {"extern-only", no_argument, &external_only, 1},
274 {"format", required_argument, 0, 'f'},
275 {"help", no_argument, 0, 'h'},
276 {"line-numbers", no_argument, 0, 'l'},
277 {"no-cplus", no_argument, &do_demangle, 0}, /* Linux compatibility. */
278 {"no-demangle", no_argument, &do_demangle, 0},
279 {"no-sort", no_argument, &no_sort, 1},
280 {"numeric-sort", no_argument, &sort_numerically, 1},
281 {"portability", no_argument, 0, 'P'},
282 {"print-armap", no_argument, &print_armap, 1},
283 {"print-file-name", no_argument, 0, 'o'},
284 {"radix", required_argument, 0, 't'},
285 {"reverse-sort", no_argument, &reverse_sort, 1},
286 {"size-sort", no_argument, &sort_by_size, 1},
287 {"stats", no_argument, &show_stats, 1},
288 {"target", required_argument, 0, OPTION_TARGET},
289 {"defined-only", no_argument, &defined_only, 1},
290 {"undefined-only", no_argument, &undefined_only, 1},
291 {"version", no_argument, &show_version, 1},
292 {0, no_argument, 0, 0}
295 /* Some error-reporting functions */
298 usage (stream, status)
302 fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
303 fprintf (stream, _(" List symbols in [file(s)] (a.out by default).\n"));
304 fprintf (stream, _(" The options are:\n\
305 -a, --debug-syms Display debugger-only symbols\n\
306 -A, --print-file-name Print name of the input file before every symbol\n\
307 -B Same as --format=bsd\n\
308 -C, --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
309 The STYLE, if specified, can be `auto' (the default),\n\
310 `gnu', 'lucid', 'arm', 'hp', 'edg' or 'gnu-new-abi'\n\
311 --no-demangle Do not demangle low-level symbol names\n\
312 -D, --dynamic Display dynamic symbols instead of normal symbols\n\
313 --defined-only Display only defined symbols\n\
315 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
316 `sysv' or `posix'. The default is `bsd'\n\
317 -g, --extern-only Display only external symbols\n\
318 -l, --line-numbers Use debugging information to find a filename and\n\
319 line number for each symbol\n\
320 -n, --numeric-sort Sort symbols numerically by address\n\
322 -p, --no-sort Do not sort the symbols\n\
323 -P, --portability Same as --format=posix\n\
324 -r, --reverse-sort Reverse the sense of the sort\n\
325 -s, --print-armap Include index for symbols from archive members\n\
326 --size-sort Sort symbols by size\n\
327 -t, --radix=RADIX Use RADIX for printing symbol values\n\
328 --target=BFDNAME Specify the target object format as BFDNAME\n\
329 -u, --undefined-only Display only undefined symbols\n\
330 -X 32_64 (ignored)\n\
331 -h, --help Display this information\n\
332 -V, --version Display this program's version number\n\
334 list_supported_targets (program_name, stream);
336 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
340 /* Set the radix for the symbol value and size according to RADIX. */
343 set_print_radix (radix)
357 value_format[4] = *radix;
359 #if BFD_HOST_64BIT_LONG
360 value_format[5] = *radix;
362 /* This case requires special handling for octal and decimal
366 other_format[3] = desc_format[3] = *radix;
369 fatal (_("%s: invalid radix"), radix);
374 set_output_format (f)
394 fatal (_("%s: invalid output format"), f);
396 format = &formats[i];
399 int main PARAMS ((int, char **));
409 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
410 setlocale (LC_MESSAGES, "");
412 #if defined (HAVE_SETLOCALE)
413 setlocale (LC_CTYPE, "");
415 bindtextdomain (PACKAGE, LOCALEDIR);
416 textdomain (PACKAGE);
418 program_name = *argv;
419 xmalloc_set_program_name (program_name);
421 START_PROGRESS (program_name, 0);
424 set_default_bfd_target ();
426 while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrst:uvVvX:",
427 long_options, (int *) 0)) != EOF)
432 print_debug_syms = 1;
436 filename_per_symbol = 1;
438 case 'B': /* For MIPS compatibility. */
439 set_output_format ("bsd");
445 enum demangling_styles style;
447 style = cplus_demangle_name_to_style (optarg);
448 if (style == unknown_demangling)
449 fatal (_("unknown demangling style `%s'"),
452 cplus_demangle_set_style (style);
459 /* Ignored for HP/UX compatibility. */
462 set_output_format (optarg);
475 sort_numerically = 1;
481 set_output_format ("posix");
490 set_print_radix (optarg);
499 /* Ignored for (partial) AIX compatibility. On AIX, the
500 argument has values 32, 64, or 32_64, and specfies that
501 only 32-bit, only 64-bit, or both kinds of objects should
502 be examined. The default is 32. So plain AIX nm on a
503 library archive with both kinds of objects will ignore
504 the 64-bit ones. For GNU nm, the default is and always
505 has been -X 32_64, and other options are not supported. */
506 if (strcmp (optarg, "32_64") != 0)
507 fatal (_("Only -X 32_64 is supported"));
510 case OPTION_TARGET: /* --target */
514 case 0: /* A long option that just sets a flag. */
523 print_version ("nm");
525 /* OK, all options now parsed. If no filename specified, do a.out. */
527 return !display_file ("a.out");
531 if (argc - optind > 1)
532 filename_per_file = 1;
534 /* We were given several filenames to do. */
535 while (optind < argc)
538 if (!display_file (argv[optind++]))
542 END_PROGRESS (program_name);
547 char *lim = (char *) sbrk (0);
549 non_fatal (_("data size %ld"), (long) (lim - (char *) &environ));
558 display_archive (file)
562 bfd *last_arfile = NULL;
565 (*format->print_archive_filename) (bfd_get_filename (file));
568 print_symdef_entry (file);
574 arfile = bfd_openr_next_archived_file (file, arfile);
578 if (bfd_get_error () != bfd_error_no_more_archived_files)
579 bfd_fatal (bfd_get_filename (file));
583 if (bfd_check_format_matches (arfile, bfd_object, &matching))
585 (*format->print_archive_member) (bfd_get_filename (file),
586 bfd_get_filename (arfile));
587 display_rel_file (arfile, file);
591 bfd_nonfatal (bfd_get_filename (arfile));
592 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
594 list_matching_formats (matching);
599 if (last_arfile != NULL)
601 bfd_close (last_arfile);
602 lineno_cache_bfd = NULL;
603 lineno_cache_rel_bfd = NULL;
605 last_arfile = arfile;
608 if (last_arfile != NULL)
610 bfd_close (last_arfile);
611 lineno_cache_bfd = NULL;
612 lineno_cache_rel_bfd = NULL;
617 display_file (filename)
620 boolean retval = true;
624 file = bfd_openr (filename, target);
627 bfd_nonfatal (filename);
631 if (bfd_check_format (file, bfd_archive))
633 display_archive (file);
635 else if (bfd_check_format_matches (file, bfd_object, &matching))
637 (*format->print_object_filename) (filename);
638 display_rel_file (file, NULL);
642 bfd_nonfatal (filename);
643 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
645 list_matching_formats (matching);
651 if (bfd_close (file) == false)
652 bfd_fatal (filename);
654 lineno_cache_bfd = NULL;
655 lineno_cache_rel_bfd = NULL;
660 /* These globals are used to pass information into the sorting
662 static bfd *sort_bfd;
663 static boolean sort_dynamic;
664 static asymbol *sort_x;
665 static asymbol *sort_y;
667 /* Symbol-sorting predicates */
668 #define valueof(x) ((x)->section->vma + (x)->value)
670 /* Numeric sorts. Undefined symbols are always considered "less than"
671 defined symbols with zero values. Common symbols are not treated
672 specially -- i.e., their sizes are used as their "values". */
675 numeric_forward (P_x, P_y)
682 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
683 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
684 if (x == NULL || y == NULL)
685 bfd_fatal (bfd_get_filename (sort_bfd));
687 xs = bfd_get_section (x);
688 ys = bfd_get_section (y);
690 if (bfd_is_und_section (xs))
692 if (! bfd_is_und_section (ys))
695 else if (bfd_is_und_section (ys))
697 else if (valueof (x) != valueof (y))
698 return valueof (x) < valueof (y) ? -1 : 1;
700 return non_numeric_forward (P_x, P_y);
704 numeric_reverse (x, y)
708 return - numeric_forward (x, y);
712 non_numeric_forward (P_x, P_y)
719 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
720 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
721 if (x == NULL || y == NULL)
722 bfd_fatal (bfd_get_filename (sort_bfd));
724 xn = bfd_asymbol_name (x);
725 yn = bfd_asymbol_name (y);
727 return ((xn == NULL) ? ((yn == NULL) ? 0 : -1) :
728 ((yn == NULL) ? 1 : strcmp (xn, yn)));
732 non_numeric_reverse (x, y)
736 return - non_numeric_forward (x, y);
739 static int (*(sorters[2][2])) PARAMS ((const PTR, const PTR)) =
741 { non_numeric_forward, non_numeric_reverse },
742 { numeric_forward, numeric_reverse }
745 /* This sort routine is used by sort_symbols_by_size. It is similar
746 to numeric_forward, but when symbols have the same value it sorts
747 by section VMA. This simplifies the sort_symbols_by_size code
748 which handles symbols at the end of sections. Also, this routine
749 tries to sort file names before other symbols with the same value.
750 That will make the file name have a zero size, which will make
751 sort_symbols_by_size choose the non file name symbol, leading to
752 more meaningful output. For similar reasons, this code sorts
753 gnu_compiled_* and gcc2_compiled before other symbols with the same
757 size_forward1 (P_x, P_y)
767 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
768 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
769 if (x == NULL || y == NULL)
770 bfd_fatal (bfd_get_filename (sort_bfd));
772 xs = bfd_get_section (x);
773 ys = bfd_get_section (y);
775 if (bfd_is_und_section (xs))
777 if (bfd_is_und_section (ys))
780 if (valueof (x) != valueof (y))
781 return valueof (x) < valueof (y) ? -1 : 1;
783 if (xs->vma != ys->vma)
784 return xs->vma < ys->vma ? -1 : 1;
786 xn = bfd_asymbol_name (x);
787 yn = bfd_asymbol_name (y);
791 /* The symbols gnu_compiled and gcc2_compiled convey even less
792 information than the file name, so sort them out first. */
794 xf = (strstr (xn, "gnu_compiled") != NULL
795 || strstr (xn, "gcc2_compiled") != NULL);
796 yf = (strstr (yn, "gnu_compiled") != NULL
797 || strstr (yn, "gcc2_compiled") != NULL);
804 /* We use a heuristic for the file name. It may not work on non
805 Unix systems, but it doesn't really matter; the only difference
806 is precisely which symbol names get printed. */
808 #define file_symbol(s, sn, snl) \
809 (((s)->flags & BSF_FILE) != 0 \
810 || ((sn)[(snl) - 2] == '.' \
811 && ((sn)[(snl) - 1] == 'o' \
812 || (sn)[(snl) - 1] == 'a')))
814 xf = file_symbol (x, xn, xnl);
815 yf = file_symbol (y, yn, ynl);
822 return non_numeric_forward (P_x, P_y);
825 /* This sort routine is used by sort_symbols_by_size. It is sorting
826 an array of size_sym structures into size order. */
829 size_forward2 (P_x, P_y)
833 const struct size_sym *x = (const struct size_sym *) P_x;
834 const struct size_sym *y = (const struct size_sym *) P_y;
836 if (x->size < y->size)
837 return reverse_sort ? 1 : -1;
838 else if (x->size > y->size)
839 return reverse_sort ? -1 : 1;
841 return sorters[0][reverse_sort] (x->minisym, y->minisym);
844 /* Sort the symbols by size. We guess the size by assuming that the
845 difference between the address of a symbol and the address of the
846 next higher symbol is the size. FIXME: ELF actually stores a size
847 with each symbol. We should use it. */
850 sort_symbols_by_size (abfd, dynamic, minisyms, symcount, size, symsizesp)
856 struct size_sym **symsizesp;
858 struct size_sym *symsizes;
859 bfd_byte *from, *fromend;
861 asymbol *store_sym, *store_next;
863 qsort (minisyms, symcount, size, size_forward1);
865 /* We are going to return a special set of symbols and sizes to
867 symsizes = (struct size_sym *) xmalloc (symcount * sizeof (struct size_sym));
868 *symsizesp = symsizes;
870 /* Note that filter_symbols has already removed all absolute and
871 undefined symbols. Here we remove all symbols whose size winds
874 from = (bfd_byte *) minisyms;
875 fromend = from + symcount * size;
882 sym = bfd_minisymbol_to_symbol (abfd, dynamic, (const PTR) from,
885 bfd_fatal (bfd_get_filename (abfd));
888 for (; from < fromend; from += size)
895 if (from + size < fromend)
897 next = bfd_minisymbol_to_symbol (abfd,
899 (const PTR) (from + size),
902 bfd_fatal (bfd_get_filename (abfd));
907 sec = bfd_get_section (sym);
909 if (bfd_is_com_section (sec))
913 if (from + size < fromend
914 && sec == bfd_get_section (next))
915 sz = valueof (next) - valueof (sym);
917 sz = (bfd_get_section_vma (abfd, sec)
918 + bfd_section_size (abfd, sec)
924 symsizes->minisym = (const PTR) from;
932 store_sym = store_next;
936 symcount = symsizes - *symsizesp;
938 /* We must now sort again by size. */
939 qsort ((PTR) *symsizesp, symcount, sizeof (struct size_sym), size_forward2);
944 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
947 display_rel_file (abfd, archive_bfd)
954 struct size_sym *symsizes;
959 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
961 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
966 symcount = bfd_read_minisymbols (abfd, dynamic, &minisyms, &size);
968 bfd_fatal (bfd_get_filename (abfd));
972 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
976 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
977 print_width = strlen (buf);
979 /* Discard the symbols we don't want to print.
980 It's OK to do this in place; we'll free the storage anyway
983 symcount = filter_symbols (abfd, dynamic, minisyms, symcount, size);
989 sort_dynamic = dynamic;
990 sort_x = bfd_make_empty_symbol (abfd);
991 sort_y = bfd_make_empty_symbol (abfd);
992 if (sort_x == NULL || sort_y == NULL)
993 bfd_fatal (bfd_get_filename (abfd));
996 qsort (minisyms, symcount, size,
997 sorters[sort_numerically][reverse_sort]);
999 symcount = sort_symbols_by_size (abfd, dynamic, minisyms, symcount,
1004 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd);
1006 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd);
1011 /* Choose which symbol entries to print;
1012 compact them downward to get rid of the rest.
1013 Return the number of symbols to be printed. */
1016 filter_symbols (abfd, dynamic, minisyms, symcount, size)
1023 bfd_byte *from, *fromend, *to;
1026 store = bfd_make_empty_symbol (abfd);
1028 bfd_fatal (bfd_get_filename (abfd));
1030 from = (bfd_byte *) minisyms;
1031 fromend = from + symcount * size;
1032 to = (bfd_byte *) minisyms;
1034 for (; from < fromend; from += size)
1041 sym = bfd_minisymbol_to_symbol (abfd, dynamic, (const PTR) from, store);
1043 bfd_fatal (bfd_get_filename (abfd));
1046 keep = bfd_is_und_section (sym->section);
1047 else if (external_only)
1048 keep = ((sym->flags & BSF_GLOBAL) != 0
1049 || (sym->flags & BSF_WEAK) != 0
1050 || bfd_is_und_section (sym->section)
1051 || bfd_is_com_section (sym->section));
1056 && ! print_debug_syms
1057 && (sym->flags & BSF_DEBUGGING) != 0)
1062 && (bfd_is_abs_section (sym->section)
1063 || bfd_is_und_section (sym->section)))
1069 if (bfd_is_und_section (sym->section))
1075 memcpy (to, from, size);
1080 return (to - (bfd_byte *) minisyms) / size;
1083 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
1084 demangling it if requested. */
1087 print_symname (format, name, abfd)
1092 if (do_demangle && *name)
1096 /* In this mode, give a user-level view of the symbol name
1097 even if it's not mangled; strip off any leading
1099 if (bfd_get_symbol_leading_char (abfd) == name[0])
1102 res = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
1105 printf (format, res);
1111 printf (format, name);
1114 /* Print the symbols. If ARCHIVE_BFD is non-NULL, it is the archive
1118 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd)
1127 bfd_byte *from, *fromend;
1129 store = bfd_make_empty_symbol (abfd);
1131 bfd_fatal (bfd_get_filename (abfd));
1133 from = (bfd_byte *) minisyms;
1134 fromend = from + symcount * size;
1135 for (; from < fromend; from += size)
1139 sym = bfd_minisymbol_to_symbol (abfd, dynamic, from, store);
1141 bfd_fatal (bfd_get_filename (abfd));
1143 print_symbol (abfd, sym, archive_bfd);
1147 /* Print the symbols when sorting by size. */
1150 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd)
1153 struct size_sym *symsizes;
1158 struct size_sym *from, *fromend;
1160 store = bfd_make_empty_symbol (abfd);
1162 bfd_fatal (bfd_get_filename (abfd));
1165 fromend = from + symcount;
1166 for (; from < fromend; from++)
1170 sym = bfd_minisymbol_to_symbol (abfd, dynamic, from->minisym, store);
1172 bfd_fatal (bfd_get_filename (abfd));
1174 /* Set the symbol value so that we actually display the symbol
1176 sym->value = from->size - bfd_section_vma (abfd, bfd_get_section (sym));
1178 print_symbol (abfd, sym, archive_bfd);
1182 /* Print a single symbol. */
1185 print_symbol (abfd, sym, archive_bfd)
1192 (*format->print_symbol_filename) (archive_bfd, abfd);
1196 if (bfd_is_und_section (bfd_get_section (sym)))
1197 print_symname ("%s", bfd_asymbol_name (sym), abfd);
1201 symbol_info syminfo;
1203 bfd_get_symbol_info (abfd, sym, &syminfo);
1204 (*format->print_symbol_info) (&syminfo, abfd);
1209 static asymbol **syms;
1210 static long symcount;
1211 const char *filename, *functionname;
1212 unsigned int lineno;
1214 /* We need to get the canonical symbols in order to call
1215 bfd_find_nearest_line. This is inefficient, but, then, you
1216 don't have to use --line-numbers. */
1217 if (abfd != lineno_cache_bfd && syms != NULL)
1226 symsize = bfd_get_symtab_upper_bound (abfd);
1228 bfd_fatal (bfd_get_filename (abfd));
1229 syms = (asymbol **) xmalloc (symsize);
1230 symcount = bfd_canonicalize_symtab (abfd, syms);
1232 bfd_fatal (bfd_get_filename (abfd));
1233 lineno_cache_bfd = abfd;
1236 if (bfd_is_und_section (bfd_get_section (sym)))
1238 static asection **secs;
1239 static arelent ***relocs;
1240 static long *relcount;
1241 static unsigned int seccount;
1243 const char *symname;
1245 /* For an undefined symbol, we try to find a reloc for the
1246 symbol, and print the line number of the reloc. */
1248 if (abfd != lineno_cache_rel_bfd && relocs != NULL)
1250 for (i = 0; i < seccount; i++)
1251 if (relocs[i] != NULL)
1263 struct get_relocs_info info;
1265 seccount = bfd_count_sections (abfd);
1267 secs = (asection **) xmalloc (seccount * sizeof *secs);
1268 relocs = (arelent ***) xmalloc (seccount * sizeof *relocs);
1269 relcount = (long *) xmalloc (seccount * sizeof *relcount);
1272 info.relocs = relocs;
1273 info.relcount = relcount;
1275 bfd_map_over_sections (abfd, get_relocs, (PTR) &info);
1276 lineno_cache_rel_bfd = abfd;
1279 symname = bfd_asymbol_name (sym);
1280 for (i = 0; i < seccount; i++)
1284 for (j = 0; j < relcount[i]; j++)
1289 if (r->sym_ptr_ptr != NULL
1290 && (*r->sym_ptr_ptr)->section == sym->section
1291 && (*r->sym_ptr_ptr)->value == sym->value
1293 bfd_asymbol_name (*r->sym_ptr_ptr)) == 0
1294 && bfd_find_nearest_line (abfd, secs[i], syms,
1295 r->address, &filename,
1296 &functionname, &lineno)
1297 && filename != NULL)
1299 /* We only print the first one we find. */
1300 printf ("\t%s:%u", filename, lineno);
1307 else if (bfd_get_section (sym)->owner == abfd)
1309 if (bfd_find_nearest_line (abfd, bfd_get_section (sym), syms,
1310 sym->value, &filename, &functionname,
1315 printf ("\t%s:%u", filename, lineno);
1323 /* The following 3 groups of functions are called unconditionally,
1324 once at the start of processing each file of the appropriate type.
1325 They should check `filename_per_file' and `filename_per_symbol',
1326 as appropriate for their output format, to determine whether to
1329 /* Print the name of an object file given on the command line. */
1332 print_object_filename_bsd (filename)
1335 if (filename_per_file && !filename_per_symbol)
1336 printf ("\n%s:\n", filename);
1340 print_object_filename_sysv (filename)
1344 printf (_("\n\nUndefined symbols from %s:\n\n"), filename);
1346 printf (_("\n\nSymbols from %s:\n\n"), filename);
1348 Name Value Class Type Size Line Section\n\n"));
1352 print_object_filename_posix (filename)
1355 if (filename_per_file && !filename_per_symbol)
1356 printf ("%s:\n", filename);
1359 /* Print the name of an archive file given on the command line. */
1362 print_archive_filename_bsd (filename)
1365 if (filename_per_file)
1366 printf ("\n%s:\n", filename);
1370 print_archive_filename_sysv (filename)
1371 char *filename ATTRIBUTE_UNUSED;
1376 print_archive_filename_posix (filename)
1377 char *filename ATTRIBUTE_UNUSED;
1381 /* Print the name of an archive member file. */
1384 print_archive_member_bsd (archive, filename)
1385 char *archive ATTRIBUTE_UNUSED;
1386 CONST char *filename;
1388 if (!filename_per_symbol)
1389 printf ("\n%s:\n", filename);
1393 print_archive_member_sysv (archive, filename)
1395 CONST char *filename;
1398 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename);
1400 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename);
1402 Name Value Class Type Size Line Section\n\n"));
1406 print_archive_member_posix (archive, filename)
1408 CONST char *filename;
1410 if (!filename_per_symbol)
1411 printf ("%s[%s]:\n", archive, filename);
1414 /* Print the name of the file (and archive, if there is one)
1415 containing a symbol. */
1418 print_symbol_filename_bsd (archive_bfd, abfd)
1419 bfd *archive_bfd, *abfd;
1421 if (filename_per_symbol)
1424 printf ("%s:", bfd_get_filename (archive_bfd));
1425 printf ("%s:", bfd_get_filename (abfd));
1430 print_symbol_filename_sysv (archive_bfd, abfd)
1431 bfd *archive_bfd, *abfd;
1433 if (filename_per_symbol)
1436 printf ("%s:", bfd_get_filename (archive_bfd));
1437 printf ("%s:", bfd_get_filename (abfd));
1442 print_symbol_filename_posix (archive_bfd, abfd)
1443 bfd *archive_bfd, *abfd;
1445 if (filename_per_symbol)
1448 printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
1449 bfd_get_filename (abfd));
1451 printf ("%s: ", bfd_get_filename (abfd));
1455 /* Print a symbol value. */
1458 print_value (abfd, val)
1459 bfd *abfd ATTRIBUTE_UNUSED;
1462 #if ! defined (BFD64) || BFD_HOST_64BIT_LONG
1463 printf (value_format, val);
1465 /* We have a 64 bit value to print, but the host is only 32 bit. */
1466 if (print_radix == 16)
1467 bfd_fprintf_vma (abfd, stdout, val);
1473 s = buf + sizeof buf;
1477 *--s = (val % print_radix) + '0';
1480 while ((buf + sizeof buf - 1) - s < 16)
1487 /* Print a line of information about a symbol. */
1490 print_symbol_info_bsd (info, abfd)
1494 if (bfd_is_undefined_symclass (info->type))
1496 if (print_width == 16)
1501 print_value (abfd, info->value);
1502 printf (" %c", info->type);
1503 if (info->type == '-')
1507 printf (other_format, info->stab_other);
1509 printf (desc_format, info->stab_desc);
1510 printf (" %5s", info->stab_name);
1512 print_symname (" %s", info->name, abfd);
1516 print_symbol_info_sysv (info, abfd)
1520 print_symname ("%-20s|", info->name, abfd); /* Name */
1521 if (bfd_is_undefined_symclass (info->type))
1522 printf (" "); /* Value */
1524 print_value (abfd, info->value);
1525 printf ("| %c |", info->type); /* Class */
1526 if (info->type == '-')
1529 printf ("%18s| ", info->stab_name); /* (C) Type */
1530 printf (desc_format, info->stab_desc); /* Size */
1531 printf ("| |"); /* Line, Section */
1534 printf (" | | |"); /* Type, Size, Line, Section */
1538 print_symbol_info_posix (info, abfd)
1542 print_symname ("%s ", info->name, abfd);
1543 printf ("%c ", info->type);
1544 if (bfd_is_undefined_symclass (info->type))
1547 print_value (abfd, info->value);
1548 /* POSIX.2 wants the symbol size printed here, when applicable;
1549 BFD currently doesn't provide it, so we take the easy way out by
1550 considering it to never be applicable. */
1554 print_symdef_entry (abfd)
1557 symindex idx = BFD_NO_MORE_SYMBOLS;
1559 boolean everprinted = false;
1561 for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
1562 idx != BFD_NO_MORE_SYMBOLS;
1563 idx = bfd_get_next_mapent (abfd, idx, &thesym))
1568 printf (_("\nArchive index:\n"));
1571 elt = bfd_get_elt_at_index (abfd, idx);
1573 bfd_fatal ("bfd_get_elt_at_index");
1574 if (thesym->name != (char *) NULL)
1576 print_symname ("%s", thesym->name, abfd);
1577 printf (" in %s\n", bfd_get_filename (elt));
1582 /* This function is used to get the relocs for a particular section.
1583 It is called via bfd_map_over_sections. */
1586 get_relocs (abfd, sec, dataarg)
1591 struct get_relocs_info *data = (struct get_relocs_info *) dataarg;
1595 if ((sec->flags & SEC_RELOC) == 0)
1597 *data->relocs = NULL;
1598 *data->relcount = 0;
1604 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1606 bfd_fatal (bfd_get_filename (abfd));
1608 *data->relocs = (arelent **) xmalloc (relsize);
1609 *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs,
1611 if (*data->relcount < 0)
1612 bfd_fatal (bfd_get_filename (abfd));