1 /* nm.c -- Describe symbol table of a rel file.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "aout/stab_gnu.h"
28 #include "aout/ranlib.h"
30 #include "libiberty.h"
32 /* When sorting by size, we use this structure to hold the size and a
33 pointer to the minisymbol. */
41 /* When fetching relocs, we use this structure to pass information to
44 struct get_relocs_info
53 usage
PARAMS ((FILE *, int));
56 set_print_radix
PARAMS ((char *));
59 set_output_format
PARAMS ((char *));
62 display_archive
PARAMS ((bfd
*));
65 display_file
PARAMS ((char *filename
));
68 display_rel_file
PARAMS ((bfd
* file
, bfd
* archive
));
71 filter_symbols
PARAMS ((bfd
*, boolean
, PTR
, long, unsigned int));
74 sort_symbols_by_size
PARAMS ((bfd
*, boolean
, PTR
, long, unsigned int,
78 print_symbols
PARAMS ((bfd
*, boolean
, PTR
, long, unsigned int, bfd
*));
81 print_size_symbols
PARAMS ((bfd
*, boolean
, struct size_sym
*, long, bfd
*));
84 print_symname
PARAMS ((const char *, const char *, bfd
*));
87 print_symbol
PARAMS ((bfd
*, asymbol
*, bfd
*));
90 print_symdef_entry
PARAMS ((bfd
* abfd
));
92 /* The sorting functions. */
95 numeric_forward
PARAMS ((const PTR
, const PTR
));
98 numeric_reverse
PARAMS ((const PTR
, const PTR
));
101 non_numeric_forward
PARAMS ((const PTR
, const PTR
));
104 non_numeric_reverse
PARAMS ((const PTR
, const PTR
));
107 size_forward1
PARAMS ((const PTR
, const PTR
));
110 size_forward2
PARAMS ((const PTR
, const PTR
));
112 /* The output formatting functions. */
115 print_object_filename_bsd
PARAMS ((char *filename
));
118 print_object_filename_sysv
PARAMS ((char *filename
));
121 print_object_filename_posix
PARAMS ((char *filename
));
125 print_archive_filename_bsd
PARAMS ((char *filename
));
128 print_archive_filename_sysv
PARAMS ((char *filename
));
131 print_archive_filename_posix
PARAMS ((char *filename
));
135 print_archive_member_bsd
PARAMS ((char *archive
, CONST
char *filename
));
138 print_archive_member_sysv
PARAMS ((char *archive
, CONST
char *filename
));
141 print_archive_member_posix
PARAMS ((char *archive
, CONST
char *filename
));
145 print_symbol_filename_bsd
PARAMS ((bfd
* archive_bfd
, bfd
* abfd
));
148 print_symbol_filename_sysv
PARAMS ((bfd
* archive_bfd
, bfd
* abfd
));
151 print_symbol_filename_posix
PARAMS ((bfd
* archive_bfd
, bfd
* abfd
));
155 print_value
PARAMS ((bfd
*, bfd_vma
));
158 print_symbol_info_bsd
PARAMS ((symbol_info
* info
, bfd
* abfd
));
161 print_symbol_info_sysv
PARAMS ((symbol_info
* info
, bfd
* abfd
));
164 print_symbol_info_posix
PARAMS ((symbol_info
* info
, bfd
* abfd
));
167 get_relocs
PARAMS ((bfd
*, asection
*, PTR
));
169 /* Support for different output formats. */
172 /* Print the name of an object file given on the command line. */
173 void (*print_object_filename
) PARAMS ((char *filename
));
175 /* Print the name of an archive file given on the command line. */
176 void (*print_archive_filename
) PARAMS ((char *filename
));
178 /* Print the name of an archive member file. */
179 void (*print_archive_member
) PARAMS ((char *archive
, CONST
char *filename
));
181 /* Print the name of the file (and archive, if there is one)
182 containing a symbol. */
183 void (*print_symbol_filename
) PARAMS ((bfd
* archive_bfd
, bfd
* abfd
));
185 /* Print a line of information about a symbol. */
186 void (*print_symbol_info
) PARAMS ((symbol_info
* info
, bfd
* abfd
));
188 static struct output_fns formats
[] =
190 {print_object_filename_bsd
,
191 print_archive_filename_bsd
,
192 print_archive_member_bsd
,
193 print_symbol_filename_bsd
,
194 print_symbol_info_bsd
},
195 {print_object_filename_sysv
,
196 print_archive_filename_sysv
,
197 print_archive_member_sysv
,
198 print_symbol_filename_sysv
,
199 print_symbol_info_sysv
},
200 {print_object_filename_posix
,
201 print_archive_filename_posix
,
202 print_archive_member_posix
,
203 print_symbol_filename_posix
,
204 print_symbol_info_posix
}
207 /* Indices in `formats'. */
209 #define FORMAT_SYSV 1
210 #define FORMAT_POSIX 2
211 #define FORMAT_DEFAULT FORMAT_BSD
213 /* The output format to use. */
214 static struct output_fns
*format
= &formats
[FORMAT_DEFAULT
];
217 /* Command options. */
219 static int do_demangle
= 0; /* Pretty print C++ symbol names. */
220 static int external_only
= 0; /* print external symbols only */
221 static int defined_only
= 0; /* Print defined symbols only */
222 static int no_sort
= 0; /* don't sort; print syms in order found */
223 static int print_debug_syms
= 0; /* print debugger-only symbols too */
224 static int print_armap
= 0; /* describe __.SYMDEF data in archive files. */
225 static int reverse_sort
= 0; /* sort in downward(alpha or numeric) order */
226 static int sort_numerically
= 0; /* sort in numeric rather than alpha order */
227 static int sort_by_size
= 0; /* sort by size of symbol */
228 static int undefined_only
= 0; /* print undefined symbols only */
229 static int dynamic
= 0; /* print dynamic symbols. */
230 static int show_version
= 0; /* show the version number */
231 static int show_stats
= 0; /* show statistics */
232 static int line_numbers
= 0; /* print line numbers for symbols */
234 /* When to print the names of files. Not mutually exclusive in SYSV format. */
235 static int filename_per_file
= 0; /* Once per file, on its own line. */
236 static int filename_per_symbol
= 0; /* Once per symbol, at start of line. */
238 /* Print formats for printing a symbol value. */
240 static char value_format
[] = "%08lx";
242 #if BFD_HOST_64BIT_LONG
243 static char value_format
[] = "%016lx";
245 /* We don't use value_format for this case. */
249 static int print_width
= 16;
251 static int print_width
= 8;
253 static int print_radix
= 16;
254 /* Print formats for printing stab info. */
255 static char other_format
[] = "%02x";
256 static char desc_format
[] = "%04x";
258 static char *target
= NULL
;
260 /* Used to cache the line numbers for a BFD. */
261 static bfd
*lineno_cache_bfd
;
262 static bfd
*lineno_cache_rel_bfd
;
264 #define OPTION_TARGET 200
266 static struct option long_options
[] =
268 {"debug-syms", no_argument
, &print_debug_syms
, 1},
269 {"demangle", optional_argument
, 0, 'C'},
270 {"dynamic", no_argument
, &dynamic
, 1},
271 {"extern-only", no_argument
, &external_only
, 1},
272 {"format", required_argument
, 0, 'f'},
273 {"help", no_argument
, 0, 'h'},
274 {"line-numbers", no_argument
, 0, 'l'},
275 {"no-cplus", no_argument
, &do_demangle
, 0}, /* Linux compatibility. */
276 {"no-demangle", no_argument
, &do_demangle
, 0},
277 {"no-sort", no_argument
, &no_sort
, 1},
278 {"numeric-sort", no_argument
, &sort_numerically
, 1},
279 {"portability", no_argument
, 0, 'P'},
280 {"print-armap", no_argument
, &print_armap
, 1},
281 {"print-file-name", no_argument
, 0, 'o'},
282 {"radix", required_argument
, 0, 't'},
283 {"reverse-sort", no_argument
, &reverse_sort
, 1},
284 {"size-sort", no_argument
, &sort_by_size
, 1},
285 {"stats", no_argument
, &show_stats
, 1},
286 {"target", required_argument
, 0, OPTION_TARGET
},
287 {"defined-only", no_argument
, &defined_only
, 1},
288 {"undefined-only", no_argument
, &undefined_only
, 1},
289 {"version", no_argument
, &show_version
, 1},
290 {0, no_argument
, 0, 0}
293 /* Some error-reporting functions */
296 usage (stream
, status
)
300 fprintf (stream
, _("Usage: %s [OPTION]... [FILE]...\n"), program_name
);
301 fprintf (stream
, _("List symbols from FILEs (a.out by default).\n"));
302 fprintf (stream
, _("\n\
303 -a, --debug-syms Display debugger-only symbols\n\
304 -A, --print-file-name Print name of the input file before every symbol\n\
305 -B Same as --format=bsd\n\
306 -C, --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
307 The STYLE, if specified, can be `auto' (the default),\n\
308 `gnu', 'lucid', 'arm', 'hp', 'edg' or 'gnu-new-abi'\n\
309 --no-demangle Do not demangle low-level symbol names\n\
310 -D, --dynamic Display dynamic symbols instead of normal symbols\n\
311 --defined-only Display only defined symbols\n\
313 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
314 `sysv' or `posix'. The default is `bsd'\n\
315 -g, --extern-only Display only external symbols\n\
316 -h, --help Display this information\n\
317 -l, --line-numbers Use debugging information to find a filename and\n\
318 line number for each symbol\n\
319 -n, --numeric-sort Sort symbols numerically by address\n\
321 -p, --no-sort Do not sort the symbols\n\
322 -P, --portability Same as --format=posix\n\
323 -r, --reverse-sort Reverse the sense of the sort\n\
324 -s, --print-armap Include index for symbols from archive members\n\
325 --size-sort Sort symbols by size\n\
326 -t, --radix=RADIX Use RADIX for printing symbol values\n\
327 --target=BFDNAME Specify the target object format as BFDNAME\n\
328 -u, --undefined-only Display only undefined symbols\n\
329 -V, --version Display this program's version number\n\
330 -X 32_64 (ignored)\n\
332 list_supported_targets (program_name
, stream
);
334 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
338 /* Set the radix for the symbol value and size according to RADIX. */
341 set_print_radix (radix
)
355 value_format
[4] = *radix
;
357 #if BFD_HOST_64BIT_LONG
358 value_format
[5] = *radix
;
360 /* This case requires special handling for octal and decimal
364 other_format
[3] = desc_format
[3] = *radix
;
367 fatal (_("%s: invalid radix"), radix
);
372 set_output_format (f
)
392 fatal (_("%s: invalid output format"), f
);
394 format
= &formats
[i
];
405 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
406 setlocale (LC_MESSAGES
, "");
408 bindtextdomain (PACKAGE
, LOCALEDIR
);
409 textdomain (PACKAGE
);
411 program_name
= *argv
;
412 xmalloc_set_program_name (program_name
);
414 START_PROGRESS (program_name
, 0);
417 set_default_bfd_target ();
419 while ((c
= getopt_long (argc
, argv
, "aABCDef:glnopPrst:uvVX:",
420 long_options
, (int *) 0)) != EOF
)
425 print_debug_syms
= 1;
429 filename_per_symbol
= 1;
431 case 'B': /* For MIPS compatibility. */
432 set_output_format ("bsd");
438 enum demangling_styles style
;
440 style
= cplus_demangle_name_to_style (optarg
);
441 if (style
== unknown_demangling
)
442 fatal (_("unknown demangling style `%s'"),
445 cplus_demangle_set_style (style
);
452 /* Ignored for HP/UX compatibility. */
455 set_output_format (optarg
);
467 sort_numerically
= 1;
473 set_output_format ("posix");
482 set_print_radix (optarg
);
491 /* Ignored for (partial) AIX compatibility. On AIX, the
492 argument has values 32, 64, or 32_64, and specfies that
493 only 32-bit, only 64-bit, or both kinds of objects should
494 be examined. The default is 32. So plain AIX nm on a
495 library archive with both kinds of objects will ignore
496 the 64-bit ones. For GNU nm, the default is and always
497 has been -X 32_64, and other options are not supported. */
498 if (strcmp (optarg
, "32_64") != 0)
499 fatal (_("Only -X 32_64 is supported"));
502 case OPTION_TARGET
: /* --target */
506 case 0: /* A long option that just sets a flag. */
515 print_version ("nm");
517 /* OK, all options now parsed. If no filename specified, do a.out. */
519 return !display_file ("a.out");
523 if (argc
- optind
> 1)
524 filename_per_file
= 1;
526 /* We were given several filenames to do. */
527 while (optind
< argc
)
530 if (!display_file (argv
[optind
++]))
534 END_PROGRESS (program_name
);
539 char *lim
= (char *) sbrk (0);
541 non_fatal (_("data size %ld"), (long) (lim
- (char *) &environ
));
550 display_archive (file
)
554 bfd
*last_arfile
= NULL
;
557 (*format
->print_archive_filename
) (bfd_get_filename (file
));
560 print_symdef_entry (file
);
566 arfile
= bfd_openr_next_archived_file (file
, arfile
);
570 if (bfd_get_error () != bfd_error_no_more_archived_files
)
571 bfd_fatal (bfd_get_filename (file
));
575 if (bfd_check_format_matches (arfile
, bfd_object
, &matching
))
577 (*format
->print_archive_member
) (bfd_get_filename (file
),
578 bfd_get_filename (arfile
));
579 display_rel_file (arfile
, file
);
583 bfd_nonfatal (bfd_get_filename (arfile
));
584 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
586 list_matching_formats (matching
);
591 if (last_arfile
!= NULL
)
593 bfd_close (last_arfile
);
594 lineno_cache_bfd
= NULL
;
595 lineno_cache_rel_bfd
= NULL
;
597 last_arfile
= arfile
;
600 if (last_arfile
!= NULL
)
602 bfd_close (last_arfile
);
603 lineno_cache_bfd
= NULL
;
604 lineno_cache_rel_bfd
= NULL
;
609 display_file (filename
)
612 boolean retval
= true;
616 file
= bfd_openr (filename
, target
);
619 bfd_nonfatal (filename
);
623 if (bfd_check_format (file
, bfd_archive
))
625 display_archive (file
);
627 else if (bfd_check_format_matches (file
, bfd_object
, &matching
))
629 (*format
->print_object_filename
) (filename
);
630 display_rel_file (file
, NULL
);
634 bfd_nonfatal (filename
);
635 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
637 list_matching_formats (matching
);
643 if (bfd_close (file
) == false)
644 bfd_fatal (filename
);
646 lineno_cache_bfd
= NULL
;
647 lineno_cache_rel_bfd
= NULL
;
652 /* These globals are used to pass information into the sorting
654 static bfd
*sort_bfd
;
655 static boolean sort_dynamic
;
656 static asymbol
*sort_x
;
657 static asymbol
*sort_y
;
659 /* Symbol-sorting predicates */
660 #define valueof(x) ((x)->section->vma + (x)->value)
662 /* Numeric sorts. Undefined symbols are always considered "less than"
663 defined symbols with zero values. Common symbols are not treated
664 specially -- i.e., their sizes are used as their "values". */
667 numeric_forward (P_x
, P_y
)
674 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
675 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
676 if (x
== NULL
|| y
== NULL
)
677 bfd_fatal (bfd_get_filename (sort_bfd
));
679 xs
= bfd_get_section (x
);
680 ys
= bfd_get_section (y
);
682 if (bfd_is_und_section (xs
))
684 if (! bfd_is_und_section (ys
))
687 else if (bfd_is_und_section (ys
))
689 else if (valueof (x
) != valueof (y
))
690 return valueof (x
) < valueof (y
) ? -1 : 1;
692 return non_numeric_forward (P_x
, P_y
);
696 numeric_reverse (x
, y
)
700 return - numeric_forward (x
, y
);
704 non_numeric_forward (P_x
, P_y
)
711 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
712 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
713 if (x
== NULL
|| y
== NULL
)
714 bfd_fatal (bfd_get_filename (sort_bfd
));
716 xn
= bfd_asymbol_name (x
);
717 yn
= bfd_asymbol_name (y
);
719 return ((xn
== NULL
) ? ((yn
== NULL
) ? 0 : -1) :
720 ((yn
== NULL
) ? 1 : strcmp (xn
, yn
)));
724 non_numeric_reverse (x
, y
)
728 return - non_numeric_forward (x
, y
);
731 static int (*(sorters
[2][2])) PARAMS ((const PTR
, const PTR
)) =
733 { non_numeric_forward
, non_numeric_reverse
},
734 { numeric_forward
, numeric_reverse
}
737 /* This sort routine is used by sort_symbols_by_size. It is similar
738 to numeric_forward, but when symbols have the same value it sorts
739 by section VMA. This simplifies the sort_symbols_by_size code
740 which handles symbols at the end of sections. Also, this routine
741 tries to sort file names before other symbols with the same value.
742 That will make the file name have a zero size, which will make
743 sort_symbols_by_size choose the non file name symbol, leading to
744 more meaningful output. For similar reasons, this code sorts
745 gnu_compiled_* and gcc2_compiled before other symbols with the same
749 size_forward1 (P_x
, P_y
)
759 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
760 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
761 if (x
== NULL
|| y
== NULL
)
762 bfd_fatal (bfd_get_filename (sort_bfd
));
764 xs
= bfd_get_section (x
);
765 ys
= bfd_get_section (y
);
767 if (bfd_is_und_section (xs
))
769 if (bfd_is_und_section (ys
))
772 if (valueof (x
) != valueof (y
))
773 return valueof (x
) < valueof (y
) ? -1 : 1;
775 if (xs
->vma
!= ys
->vma
)
776 return xs
->vma
< ys
->vma
? -1 : 1;
778 xn
= bfd_asymbol_name (x
);
779 yn
= bfd_asymbol_name (y
);
783 /* The symbols gnu_compiled and gcc2_compiled convey even less
784 information than the file name, so sort them out first. */
786 xf
= (strstr (xn
, "gnu_compiled") != NULL
787 || strstr (xn
, "gcc2_compiled") != NULL
);
788 yf
= (strstr (yn
, "gnu_compiled") != NULL
789 || strstr (yn
, "gcc2_compiled") != NULL
);
796 /* We use a heuristic for the file name. It may not work on non
797 Unix systems, but it doesn't really matter; the only difference
798 is precisely which symbol names get printed. */
800 #define file_symbol(s, sn, snl) \
801 (((s)->flags & BSF_FILE) != 0 \
802 || ((sn)[(snl) - 2] == '.' \
803 && ((sn)[(snl) - 1] == 'o' \
804 || (sn)[(snl) - 1] == 'a')))
806 xf
= file_symbol (x
, xn
, xnl
);
807 yf
= file_symbol (y
, yn
, ynl
);
814 return non_numeric_forward (P_x
, P_y
);
817 /* This sort routine is used by sort_symbols_by_size. It is sorting
818 an array of size_sym structures into size order. */
821 size_forward2 (P_x
, P_y
)
825 const struct size_sym
*x
= (const struct size_sym
*) P_x
;
826 const struct size_sym
*y
= (const struct size_sym
*) P_y
;
828 if (x
->size
< y
->size
)
829 return reverse_sort
? 1 : -1;
830 else if (x
->size
> y
->size
)
831 return reverse_sort
? -1 : 1;
833 return sorters
[0][reverse_sort
] (x
->minisym
, y
->minisym
);
836 /* Sort the symbols by size. We guess the size by assuming that the
837 difference between the address of a symbol and the address of the
838 next higher symbol is the size. FIXME: ELF actually stores a size
839 with each symbol. We should use it. */
842 sort_symbols_by_size (abfd
, dynamic
, minisyms
, symcount
, size
, symsizesp
)
848 struct size_sym
**symsizesp
;
850 struct size_sym
*symsizes
;
851 bfd_byte
*from
, *fromend
;
853 asymbol
*store_sym
, *store_next
;
855 qsort (minisyms
, symcount
, size
, size_forward1
);
857 /* We are going to return a special set of symbols and sizes to
859 symsizes
= (struct size_sym
*) xmalloc (symcount
* sizeof (struct size_sym
));
860 *symsizesp
= symsizes
;
862 /* Note that filter_symbols has already removed all absolute and
863 undefined symbols. Here we remove all symbols whose size winds
866 from
= (bfd_byte
*) minisyms
;
867 fromend
= from
+ symcount
* size
;
874 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, (const PTR
) from
,
877 bfd_fatal (bfd_get_filename (abfd
));
880 for (; from
< fromend
; from
+= size
)
887 if (from
+ size
< fromend
)
889 next
= bfd_minisymbol_to_symbol (abfd
,
891 (const PTR
) (from
+ size
),
894 bfd_fatal (bfd_get_filename (abfd
));
899 sec
= bfd_get_section (sym
);
901 if (bfd_is_com_section (sec
))
905 if (from
+ size
< fromend
906 && sec
== bfd_get_section (next
))
907 sz
= valueof (next
) - valueof (sym
);
909 sz
= (bfd_get_section_vma (abfd
, sec
)
910 + bfd_section_size (abfd
, sec
)
916 symsizes
->minisym
= (const PTR
) from
;
924 store_sym
= store_next
;
928 symcount
= symsizes
- *symsizesp
;
930 /* We must now sort again by size. */
931 qsort ((PTR
) *symsizesp
, symcount
, sizeof (struct size_sym
), size_forward2
);
936 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
939 display_rel_file (abfd
, archive_bfd
)
946 struct size_sym
*symsizes
;
951 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
953 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
958 symcount
= bfd_read_minisymbols (abfd
, dynamic
, &minisyms
, &size
);
960 bfd_fatal (bfd_get_filename (abfd
));
964 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
968 bfd_sprintf_vma (abfd
, buf
, (bfd_vma
) -1);
969 print_width
= strlen (buf
);
971 /* Discard the symbols we don't want to print.
972 It's OK to do this in place; we'll free the storage anyway
975 symcount
= filter_symbols (abfd
, dynamic
, minisyms
, symcount
, size
);
981 sort_dynamic
= dynamic
;
982 sort_x
= bfd_make_empty_symbol (abfd
);
983 sort_y
= bfd_make_empty_symbol (abfd
);
984 if (sort_x
== NULL
|| sort_y
== NULL
)
985 bfd_fatal (bfd_get_filename (abfd
));
988 qsort (minisyms
, symcount
, size
,
989 sorters
[sort_numerically
][reverse_sort
]);
991 symcount
= sort_symbols_by_size (abfd
, dynamic
, minisyms
, symcount
,
996 print_symbols (abfd
, dynamic
, minisyms
, symcount
, size
, archive_bfd
);
998 print_size_symbols (abfd
, dynamic
, symsizes
, symcount
, archive_bfd
);
1003 /* Choose which symbol entries to print;
1004 compact them downward to get rid of the rest.
1005 Return the number of symbols to be printed. */
1008 filter_symbols (abfd
, dynamic
, minisyms
, symcount
, size
)
1015 bfd_byte
*from
, *fromend
, *to
;
1018 store
= bfd_make_empty_symbol (abfd
);
1020 bfd_fatal (bfd_get_filename (abfd
));
1022 from
= (bfd_byte
*) minisyms
;
1023 fromend
= from
+ symcount
* size
;
1024 to
= (bfd_byte
*) minisyms
;
1026 for (; from
< fromend
; from
+= size
)
1033 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, (const PTR
) from
, store
);
1035 bfd_fatal (bfd_get_filename (abfd
));
1038 keep
= bfd_is_und_section (sym
->section
);
1039 else if (external_only
)
1040 keep
= ((sym
->flags
& BSF_GLOBAL
) != 0
1041 || (sym
->flags
& BSF_WEAK
) != 0
1042 || bfd_is_und_section (sym
->section
)
1043 || bfd_is_com_section (sym
->section
));
1048 && ! print_debug_syms
1049 && (sym
->flags
& BSF_DEBUGGING
) != 0)
1054 && (bfd_is_abs_section (sym
->section
)
1055 || bfd_is_und_section (sym
->section
)))
1061 if (bfd_is_und_section (sym
->section
))
1067 memcpy (to
, from
, size
);
1072 return (to
- (bfd_byte
*) minisyms
) / size
;
1075 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
1076 demangling it if requested. */
1079 print_symname (format
, name
, abfd
)
1084 if (do_demangle
&& *name
)
1088 /* In this mode, give a user-level view of the symbol name
1089 even if it's not mangled; strip off any leading
1091 if (bfd_get_symbol_leading_char (abfd
) == name
[0])
1094 res
= cplus_demangle (name
, DMGL_ANSI
| DMGL_PARAMS
);
1097 printf (format
, res
);
1103 printf (format
, name
);
1106 /* Print the symbols. If ARCHIVE_BFD is non-NULL, it is the archive
1110 print_symbols (abfd
, dynamic
, minisyms
, symcount
, size
, archive_bfd
)
1119 bfd_byte
*from
, *fromend
;
1121 store
= bfd_make_empty_symbol (abfd
);
1123 bfd_fatal (bfd_get_filename (abfd
));
1125 from
= (bfd_byte
*) minisyms
;
1126 fromend
= from
+ symcount
* size
;
1127 for (; from
< fromend
; from
+= size
)
1131 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, from
, store
);
1133 bfd_fatal (bfd_get_filename (abfd
));
1135 print_symbol (abfd
, sym
, archive_bfd
);
1139 /* Print the symbols when sorting by size. */
1142 print_size_symbols (abfd
, dynamic
, symsizes
, symcount
, archive_bfd
)
1145 struct size_sym
*symsizes
;
1150 struct size_sym
*from
, *fromend
;
1152 store
= bfd_make_empty_symbol (abfd
);
1154 bfd_fatal (bfd_get_filename (abfd
));
1157 fromend
= from
+ symcount
;
1158 for (; from
< fromend
; from
++)
1162 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, from
->minisym
, store
);
1164 bfd_fatal (bfd_get_filename (abfd
));
1166 /* Set the symbol value so that we actually display the symbol
1168 sym
->value
= from
->size
- bfd_section_vma (abfd
, bfd_get_section (sym
));
1170 print_symbol (abfd
, sym
, archive_bfd
);
1174 /* Print a single symbol. */
1177 print_symbol (abfd
, sym
, archive_bfd
)
1184 (*format
->print_symbol_filename
) (archive_bfd
, abfd
);
1188 if (bfd_is_und_section (bfd_get_section (sym
)))
1189 print_symname ("%s", bfd_asymbol_name (sym
), abfd
);
1193 symbol_info syminfo
;
1195 bfd_get_symbol_info (abfd
, sym
, &syminfo
);
1196 (*format
->print_symbol_info
) (&syminfo
, abfd
);
1201 static asymbol
**syms
;
1202 static long symcount
;
1203 const char *filename
, *functionname
;
1204 unsigned int lineno
;
1206 /* We need to get the canonical symbols in order to call
1207 bfd_find_nearest_line. This is inefficient, but, then, you
1208 don't have to use --line-numbers. */
1209 if (abfd
!= lineno_cache_bfd
&& syms
!= NULL
)
1218 symsize
= bfd_get_symtab_upper_bound (abfd
);
1220 bfd_fatal (bfd_get_filename (abfd
));
1221 syms
= (asymbol
**) xmalloc (symsize
);
1222 symcount
= bfd_canonicalize_symtab (abfd
, syms
);
1224 bfd_fatal (bfd_get_filename (abfd
));
1225 lineno_cache_bfd
= abfd
;
1228 if (bfd_is_und_section (bfd_get_section (sym
)))
1230 static asection
**secs
;
1231 static arelent
***relocs
;
1232 static long *relcount
;
1233 static unsigned int seccount
;
1235 const char *symname
;
1237 /* For an undefined symbol, we try to find a reloc for the
1238 symbol, and print the line number of the reloc. */
1240 if (abfd
!= lineno_cache_rel_bfd
&& relocs
!= NULL
)
1242 for (i
= 0; i
< seccount
; i
++)
1243 if (relocs
[i
] != NULL
)
1255 struct get_relocs_info info
;
1257 seccount
= bfd_count_sections (abfd
);
1259 secs
= (asection
**) xmalloc (seccount
* sizeof *secs
);
1260 relocs
= (arelent
***) xmalloc (seccount
* sizeof *relocs
);
1261 relcount
= (long *) xmalloc (seccount
* sizeof *relcount
);
1264 info
.relocs
= relocs
;
1265 info
.relcount
= relcount
;
1267 bfd_map_over_sections (abfd
, get_relocs
, (PTR
) &info
);
1268 lineno_cache_rel_bfd
= abfd
;
1271 symname
= bfd_asymbol_name (sym
);
1272 for (i
= 0; i
< seccount
; i
++)
1276 for (j
= 0; j
< relcount
[i
]; j
++)
1281 if (r
->sym_ptr_ptr
!= NULL
1282 && (*r
->sym_ptr_ptr
)->section
== sym
->section
1283 && (*r
->sym_ptr_ptr
)->value
== sym
->value
1285 bfd_asymbol_name (*r
->sym_ptr_ptr
)) == 0
1286 && bfd_find_nearest_line (abfd
, secs
[i
], syms
,
1287 r
->address
, &filename
,
1288 &functionname
, &lineno
))
1290 /* We only print the first one we find. */
1291 printf ("\t%s:%u", filename
, lineno
);
1298 else if (bfd_get_section (sym
)->owner
== abfd
)
1300 if (bfd_find_nearest_line (abfd
, bfd_get_section (sym
), syms
,
1301 sym
->value
, &filename
, &functionname
,
1306 printf ("\t%s:%u", filename
, lineno
);
1314 /* The following 3 groups of functions are called unconditionally,
1315 once at the start of processing each file of the appropriate type.
1316 They should check `filename_per_file' and `filename_per_symbol',
1317 as appropriate for their output format, to determine whether to
1320 /* Print the name of an object file given on the command line. */
1323 print_object_filename_bsd (filename
)
1326 if (filename_per_file
&& !filename_per_symbol
)
1327 printf ("\n%s:\n", filename
);
1331 print_object_filename_sysv (filename
)
1335 printf (_("\n\nUndefined symbols from %s:\n\n"), filename
);
1337 printf (_("\n\nSymbols from %s:\n\n"), filename
);
1339 Name Value Class Type Size Line Section\n\n"));
1343 print_object_filename_posix (filename
)
1346 if (filename_per_file
&& !filename_per_symbol
)
1347 printf ("%s:\n", filename
);
1350 /* Print the name of an archive file given on the command line. */
1353 print_archive_filename_bsd (filename
)
1356 if (filename_per_file
)
1357 printf ("\n%s:\n", filename
);
1361 print_archive_filename_sysv (filename
)
1362 char *filename ATTRIBUTE_UNUSED
;
1367 print_archive_filename_posix (filename
)
1368 char *filename ATTRIBUTE_UNUSED
;
1372 /* Print the name of an archive member file. */
1375 print_archive_member_bsd (archive
, filename
)
1376 char *archive ATTRIBUTE_UNUSED
;
1377 CONST
char *filename
;
1379 if (!filename_per_symbol
)
1380 printf ("\n%s:\n", filename
);
1384 print_archive_member_sysv (archive
, filename
)
1386 CONST
char *filename
;
1389 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive
, filename
);
1391 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive
, filename
);
1393 Name Value Class Type Size Line Section\n\n"));
1397 print_archive_member_posix (archive
, filename
)
1399 CONST
char *filename
;
1401 if (!filename_per_symbol
)
1402 printf ("%s[%s]:\n", archive
, filename
);
1405 /* Print the name of the file (and archive, if there is one)
1406 containing a symbol. */
1409 print_symbol_filename_bsd (archive_bfd
, abfd
)
1410 bfd
*archive_bfd
, *abfd
;
1412 if (filename_per_symbol
)
1415 printf ("%s:", bfd_get_filename (archive_bfd
));
1416 printf ("%s:", bfd_get_filename (abfd
));
1421 print_symbol_filename_sysv (archive_bfd
, abfd
)
1422 bfd
*archive_bfd
, *abfd
;
1424 if (filename_per_symbol
)
1427 printf ("%s:", bfd_get_filename (archive_bfd
));
1428 printf ("%s:", bfd_get_filename (abfd
));
1433 print_symbol_filename_posix (archive_bfd
, abfd
)
1434 bfd
*archive_bfd
, *abfd
;
1436 if (filename_per_symbol
)
1439 printf ("%s[%s]: ", bfd_get_filename (archive_bfd
),
1440 bfd_get_filename (abfd
));
1442 printf ("%s: ", bfd_get_filename (abfd
));
1446 /* Print a symbol value. */
1449 print_value (abfd
, val
)
1453 #if ! defined (BFD64) || BFD_HOST_64BIT_LONG
1454 printf (value_format
, val
);
1456 /* We have a 64 bit value to print, but the host is only 32 bit. */
1457 if (print_radix
== 16)
1458 bfd_fprintf_vma (abfd
, stdout
, val
);
1464 s
= buf
+ sizeof buf
;
1468 *--s
= (val
% print_radix
) + '0';
1471 while ((buf
+ sizeof buf
- 1) - s
< 16)
1478 /* Print a line of information about a symbol. */
1481 print_symbol_info_bsd (info
, abfd
)
1485 if (bfd_is_undefined_symclass (info
->type
))
1487 if (print_width
== 16)
1492 print_value (abfd
, info
->value
);
1493 printf (" %c", info
->type
);
1494 if (info
->type
== '-')
1498 printf (other_format
, info
->stab_other
);
1500 printf (desc_format
, info
->stab_desc
);
1501 printf (" %5s", info
->stab_name
);
1503 print_symname (" %s", info
->name
, abfd
);
1507 print_symbol_info_sysv (info
, abfd
)
1511 print_symname ("%-20s|", info
->name
, abfd
); /* Name */
1512 if (bfd_is_undefined_symclass (info
->type
))
1513 printf (" "); /* Value */
1515 print_value (abfd
, info
->value
);
1516 printf ("| %c |", info
->type
); /* Class */
1517 if (info
->type
== '-')
1520 printf ("%18s| ", info
->stab_name
); /* (C) Type */
1521 printf (desc_format
, info
->stab_desc
); /* Size */
1522 printf ("| |"); /* Line, Section */
1525 printf (" | | |"); /* Type, Size, Line, Section */
1529 print_symbol_info_posix (info
, abfd
)
1533 print_symname ("%s ", info
->name
, abfd
);
1534 printf ("%c ", info
->type
);
1535 if (bfd_is_undefined_symclass (info
->type
))
1538 print_value (abfd
, info
->value
);
1539 /* POSIX.2 wants the symbol size printed here, when applicable;
1540 BFD currently doesn't provide it, so we take the easy way out by
1541 considering it to never be applicable. */
1545 print_symdef_entry (abfd
)
1548 symindex idx
= BFD_NO_MORE_SYMBOLS
;
1550 boolean everprinted
= false;
1552 for (idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
);
1553 idx
!= BFD_NO_MORE_SYMBOLS
;
1554 idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
))
1559 printf (_("\nArchive index:\n"));
1562 elt
= bfd_get_elt_at_index (abfd
, idx
);
1564 bfd_fatal ("bfd_get_elt_at_index");
1565 if (thesym
->name
!= (char *) NULL
)
1567 print_symname ("%s", thesym
->name
, abfd
);
1568 printf (" in %s\n", bfd_get_filename (elt
));
1573 /* This function is used to get the relocs for a particular section.
1574 It is called via bfd_map_over_sections. */
1577 get_relocs (abfd
, sec
, dataarg
)
1582 struct get_relocs_info
*data
= (struct get_relocs_info
*) dataarg
;
1586 if ((sec
->flags
& SEC_RELOC
) == 0)
1588 *data
->relocs
= NULL
;
1589 *data
->relcount
= 0;
1595 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1597 bfd_fatal (bfd_get_filename (abfd
));
1599 *data
->relocs
= (arelent
**) xmalloc (relsize
);
1600 *data
->relcount
= bfd_canonicalize_reloc (abfd
, sec
, *data
->relocs
,
1602 if (*data
->relcount
< 0)
1603 bfd_fatal (bfd_get_filename (abfd
));