1 /* nm.c -- Describe symbol table of a rel file.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "aout/stab_gnu.h"
28 #include "aout/ranlib.h"
30 #include "libiberty.h"
33 /* When sorting by size, we use this structure to hold the size and a
34 pointer to the minisymbol. */
42 /* When fetching relocs, we use this structure to pass information to
45 struct get_relocs_info
53 struct extended_symbol_info
57 /* FIXME: We should add more fields for Type, Line, Section. */
59 #define SYM_NAME(sym) (sym->sinfo->name)
60 #define SYM_VALUE(sym) (sym->sinfo->value)
61 #define SYM_TYPE(sym) (sym->sinfo->type)
62 #define SYM_STAB_NAME(sym) (sym->sinfo->stab_name)
63 #define SYM_STAB_DESC(sym) (sym->sinfo->stab_desc)
64 #define SYM_STAB_OTHER(sym) (sym->sinfo->stab_other)
65 #define SYM_SIZE(sym) (sym->ssize)
67 static void usage
PARAMS ((FILE *, int));
68 static void set_print_radix
PARAMS ((char *));
69 static void set_output_format
PARAMS ((char *));
70 static void display_archive
PARAMS ((bfd
*));
71 static boolean display_file
PARAMS ((char *));
72 static void display_rel_file
PARAMS ((bfd
*, bfd
*));
73 static long filter_symbols
PARAMS ((bfd
*, boolean
, PTR
, long, unsigned int));
74 static long sort_symbols_by_size
PARAMS ((bfd
*, boolean
, PTR
, long, unsigned int, struct size_sym
**));
75 static void print_symbols
PARAMS ((bfd
*, boolean
, PTR
, long, unsigned int, bfd
*));
76 static void print_size_symbols
PARAMS ((bfd
*, boolean
, struct size_sym
*, long, bfd
*));
77 static void print_symname
PARAMS ((const char *, const char *, bfd
*));
78 static void print_symbol
PARAMS ((bfd
*, asymbol
*, bfd_vma ssize
, bfd
*));
79 static void print_symdef_entry
PARAMS ((bfd
*));
81 /* The sorting functions. */
82 static int numeric_forward
PARAMS ((const PTR
, const PTR
));
83 static int numeric_reverse
PARAMS ((const PTR
, const PTR
));
84 static int non_numeric_forward
PARAMS ((const PTR
, const PTR
));
85 static int non_numeric_reverse
PARAMS ((const PTR
, const PTR
));
86 static int size_forward1
PARAMS ((const PTR
, const PTR
));
87 static int size_forward2
PARAMS ((const PTR
, const PTR
));
89 /* The output formatting functions. */
90 static void print_object_filename_bsd
PARAMS ((char *));
91 static void print_object_filename_sysv
PARAMS ((char *));
92 static void print_object_filename_posix
PARAMS ((char *));
93 static void print_archive_filename_bsd
PARAMS ((char *));
94 static void print_archive_filename_sysv
PARAMS ((char *));
95 static void print_archive_filename_posix
PARAMS ((char *));
96 static void print_archive_member_bsd
PARAMS ((char *, const char *));
97 static void print_archive_member_sysv
PARAMS ((char *, const char *));
98 static void print_archive_member_posix
PARAMS ((char *, const char *));
99 static void print_symbol_filename_bsd
PARAMS ((bfd
*, bfd
*));
100 static void print_symbol_filename_sysv
PARAMS ((bfd
*, bfd
*));
101 static void print_symbol_filename_posix
PARAMS ((bfd
*, bfd
*));
102 static void print_value
PARAMS ((bfd
*, bfd_vma
));
103 static void print_symbol_info_bsd
PARAMS ((struct extended_symbol_info
*, bfd
*));
104 static void print_symbol_info_sysv
PARAMS ((struct extended_symbol_info
*, bfd
*));
105 static void print_symbol_info_posix
PARAMS ((struct extended_symbol_info
*, bfd
*));
106 static void get_relocs
PARAMS ((bfd
*, asection
*, PTR
));
108 /* Support for different output formats. */
111 /* Print the name of an object file given on the command line. */
112 void (*print_object_filename
) PARAMS ((char *));
114 /* Print the name of an archive file given on the command line. */
115 void (*print_archive_filename
) PARAMS ((char *));
117 /* Print the name of an archive member file. */
118 void (*print_archive_member
) PARAMS ((char *, const char *));
120 /* Print the name of the file (and archive, if there is one)
121 containing a symbol. */
122 void (*print_symbol_filename
) PARAMS ((bfd
*, bfd
*));
124 /* Print a line of information about a symbol. */
125 void (*print_symbol_info
) PARAMS ((struct extended_symbol_info
*, bfd
*));
128 static struct output_fns formats
[] =
130 {print_object_filename_bsd
,
131 print_archive_filename_bsd
,
132 print_archive_member_bsd
,
133 print_symbol_filename_bsd
,
134 print_symbol_info_bsd
},
135 {print_object_filename_sysv
,
136 print_archive_filename_sysv
,
137 print_archive_member_sysv
,
138 print_symbol_filename_sysv
,
139 print_symbol_info_sysv
},
140 {print_object_filename_posix
,
141 print_archive_filename_posix
,
142 print_archive_member_posix
,
143 print_symbol_filename_posix
,
144 print_symbol_info_posix
}
147 /* Indices in `formats'. */
149 #define FORMAT_SYSV 1
150 #define FORMAT_POSIX 2
151 #define FORMAT_DEFAULT FORMAT_BSD
153 /* The output format to use. */
154 static struct output_fns
*format
= &formats
[FORMAT_DEFAULT
];
156 /* Command options. */
158 static int do_demangle
= 0; /* Pretty print C++ symbol names. */
159 static int external_only
= 0; /* Print external symbols only. */
160 static int defined_only
= 0; /* Print defined symbols only. */
161 static int no_sort
= 0; /* Don't sort; print syms in order found. */
162 static int print_debug_syms
= 0;/* Print debugger-only symbols too. */
163 static int print_armap
= 0; /* Describe __.SYMDEF data in archive files. */
164 static int print_size
= 0; /* Print size of defined symbols. */
165 static int reverse_sort
= 0; /* Sort in downward(alpha or numeric) order. */
166 static int sort_numerically
= 0;/* Sort in numeric rather than alpha order. */
167 static int sort_by_size
= 0; /* Sort by size of symbol. */
168 static int undefined_only
= 0; /* Print undefined symbols only. */
169 static int dynamic
= 0; /* Print dynamic symbols. */
170 static int show_version
= 0; /* Show the version number. */
171 static int show_stats
= 0; /* Show statistics. */
172 static int line_numbers
= 0; /* Print line numbers for symbols. */
174 /* When to print the names of files. Not mutually exclusive in SYSV format. */
175 static int filename_per_file
= 0; /* Once per file, on its own line. */
176 static int filename_per_symbol
= 0; /* Once per symbol, at start of line. */
178 /* Print formats for printing a symbol value. */
180 static char value_format
[] = "%08lx";
182 #if BFD_HOST_64BIT_LONG
183 static char value_format
[] = "%016lx";
185 /* We don't use value_format for this case. */
189 static int print_width
= 16;
191 static int print_width
= 8;
193 static int print_radix
= 16;
194 /* Print formats for printing stab info. */
195 static char other_format
[] = "%02x";
196 static char desc_format
[] = "%04x";
198 static char *target
= NULL
;
200 /* Used to cache the line numbers for a BFD. */
201 static bfd
*lineno_cache_bfd
;
202 static bfd
*lineno_cache_rel_bfd
;
204 #define OPTION_TARGET 200
206 static struct option long_options
[] =
208 {"debug-syms", no_argument
, &print_debug_syms
, 1},
209 {"demangle", optional_argument
, 0, 'C'},
210 {"dynamic", no_argument
, &dynamic
, 1},
211 {"extern-only", no_argument
, &external_only
, 1},
212 {"format", required_argument
, 0, 'f'},
213 {"help", no_argument
, 0, 'h'},
214 {"line-numbers", no_argument
, 0, 'l'},
215 {"no-cplus", no_argument
, &do_demangle
, 0}, /* Linux compatibility. */
216 {"no-demangle", no_argument
, &do_demangle
, 0},
217 {"no-sort", no_argument
, &no_sort
, 1},
218 {"numeric-sort", no_argument
, &sort_numerically
, 1},
219 {"portability", no_argument
, 0, 'P'},
220 {"print-armap", no_argument
, &print_armap
, 1},
221 {"print-file-name", no_argument
, 0, 'o'},
222 {"print-size", no_argument
, 0, 'S'},
223 {"radix", required_argument
, 0, 't'},
224 {"reverse-sort", no_argument
, &reverse_sort
, 1},
225 {"size-sort", no_argument
, &sort_by_size
, 1},
226 {"stats", no_argument
, &show_stats
, 1},
227 {"target", required_argument
, 0, OPTION_TARGET
},
228 {"defined-only", no_argument
, &defined_only
, 1},
229 {"undefined-only", no_argument
, &undefined_only
, 1},
230 {"version", no_argument
, &show_version
, 1},
231 {0, no_argument
, 0, 0}
234 /* Some error-reporting functions. */
237 usage (stream
, status
)
241 fprintf (stream
, _("Usage: %s [option(s)] [file(s)]\n"), program_name
);
242 fprintf (stream
, _(" List symbols in [file(s)] (a.out by default).\n"));
243 fprintf (stream
, _(" The options are:\n\
244 -a, --debug-syms Display debugger-only symbols\n\
245 -A, --print-file-name Print name of the input file before every symbol\n\
246 -B Same as --format=bsd\n\
247 -C, --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
248 The STYLE, if specified, can be `auto' (the default),\n\
249 `gnu', 'lucid', 'arm', 'hp', 'edg' or 'gnu-new-abi'\n\
250 --no-demangle Do not demangle low-level symbol names\n\
251 -D, --dynamic Display dynamic symbols instead of normal symbols\n\
252 --defined-only Display only defined symbols\n\
254 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
255 `sysv' or `posix'. The default is `bsd'\n\
256 -g, --extern-only Display only external symbols\n\
257 -l, --line-numbers Use debugging information to find a filename and\n\
258 line number for each symbol\n\
259 -n, --numeric-sort Sort symbols numerically by address\n\
261 -p, --no-sort Do not sort the symbols\n\
262 -P, --portability Same as --format=posix\n\
263 -r, --reverse-sort Reverse the sense of the sort\n\
264 -S, --print-size Print size of defined symbols\n\
265 -s, --print-armap Include index for symbols from archive members\n\
266 --size-sort Sort symbols by size\n\
267 -t, --radix=RADIX Use RADIX for printing symbol values\n\
268 --target=BFDNAME Specify the target object format as BFDNAME\n\
269 -u, --undefined-only Display only undefined symbols\n\
270 -X 32_64 (ignored)\n\
271 -h, --help Display this information\n\
272 -V, --version Display this program's version number\n\
274 list_supported_targets (program_name
, stream
);
276 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
280 /* Set the radix for the symbol value and size according to RADIX. */
283 set_print_radix (radix
)
297 value_format
[4] = *radix
;
299 #if BFD_HOST_64BIT_LONG
300 value_format
[5] = *radix
;
302 /* This case requires special handling for octal and decimal
306 other_format
[3] = desc_format
[3] = *radix
;
309 fatal (_("%s: invalid radix"), radix
);
314 set_output_format (f
)
334 fatal (_("%s: invalid output format"), f
);
336 format
= &formats
[i
];
339 int main
PARAMS ((int, char **));
349 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
350 setlocale (LC_MESSAGES
, "");
352 #if defined (HAVE_SETLOCALE)
353 setlocale (LC_CTYPE
, "");
355 bindtextdomain (PACKAGE
, LOCALEDIR
);
356 textdomain (PACKAGE
);
358 program_name
= *argv
;
359 xmalloc_set_program_name (program_name
);
361 START_PROGRESS (program_name
, 0);
364 set_default_bfd_target ();
366 while ((c
= getopt_long (argc
, argv
, "aABCDef:gHhlnopPrSst:uvVvX:",
367 long_options
, (int *) 0)) != EOF
)
372 print_debug_syms
= 1;
376 filename_per_symbol
= 1;
378 case 'B': /* For MIPS compatibility. */
379 set_output_format ("bsd");
385 enum demangling_styles style
;
387 style
= cplus_demangle_name_to_style (optarg
);
388 if (style
== unknown_demangling
)
389 fatal (_("unknown demangling style `%s'"),
392 cplus_demangle_set_style (style
);
399 /* Ignored for HP/UX compatibility. */
402 set_output_format (optarg
);
415 sort_numerically
= 1;
421 set_output_format ("posix");
433 set_print_radix (optarg
);
442 /* Ignored for (partial) AIX compatibility. On AIX, the
443 argument has values 32, 64, or 32_64, and specfies that
444 only 32-bit, only 64-bit, or both kinds of objects should
445 be examined. The default is 32. So plain AIX nm on a
446 library archive with both kinds of objects will ignore
447 the 64-bit ones. For GNU nm, the default is and always
448 has been -X 32_64, and other options are not supported. */
449 if (strcmp (optarg
, "32_64") != 0)
450 fatal (_("Only -X 32_64 is supported"));
453 case OPTION_TARGET
: /* --target */
457 case 0: /* A long option that just sets a flag. */
466 print_version ("nm");
468 /* OK, all options now parsed. If no filename specified, do a.out. */
470 return !display_file ("a.out");
474 if (argc
- optind
> 1)
475 filename_per_file
= 1;
477 /* We were given several filenames to do. */
478 while (optind
< argc
)
481 if (!display_file (argv
[optind
++]))
485 END_PROGRESS (program_name
);
490 char *lim
= (char *) sbrk (0);
492 non_fatal (_("data size %ld"), (long) (lim
- (char *) &environ
));
501 display_archive (file
)
505 bfd
*last_arfile
= NULL
;
508 (*format
->print_archive_filename
) (bfd_get_filename (file
));
511 print_symdef_entry (file
);
517 arfile
= bfd_openr_next_archived_file (file
, arfile
);
521 if (bfd_get_error () != bfd_error_no_more_archived_files
)
522 bfd_fatal (bfd_get_filename (file
));
526 if (bfd_check_format_matches (arfile
, bfd_object
, &matching
))
528 (*format
->print_archive_member
) (bfd_get_filename (file
),
529 bfd_get_filename (arfile
));
530 display_rel_file (arfile
, file
);
534 bfd_nonfatal (bfd_get_filename (arfile
));
535 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
537 list_matching_formats (matching
);
542 if (last_arfile
!= NULL
)
544 bfd_close (last_arfile
);
545 lineno_cache_bfd
= NULL
;
546 lineno_cache_rel_bfd
= NULL
;
548 last_arfile
= arfile
;
551 if (last_arfile
!= NULL
)
553 bfd_close (last_arfile
);
554 lineno_cache_bfd
= NULL
;
555 lineno_cache_rel_bfd
= NULL
;
560 display_file (filename
)
563 boolean retval
= true;
567 file
= bfd_openr (filename
, target
);
570 bfd_nonfatal (filename
);
574 if (bfd_check_format (file
, bfd_archive
))
576 display_archive (file
);
578 else if (bfd_check_format_matches (file
, bfd_object
, &matching
))
580 (*format
->print_object_filename
) (filename
);
581 display_rel_file (file
, NULL
);
585 bfd_nonfatal (filename
);
586 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
588 list_matching_formats (matching
);
594 if (bfd_close (file
) == false)
595 bfd_fatal (filename
);
597 lineno_cache_bfd
= NULL
;
598 lineno_cache_rel_bfd
= NULL
;
603 /* These globals are used to pass information into the sorting
605 static bfd
*sort_bfd
;
606 static boolean sort_dynamic
;
607 static asymbol
*sort_x
;
608 static asymbol
*sort_y
;
610 /* Symbol-sorting predicates */
611 #define valueof(x) ((x)->section->vma + (x)->value)
613 /* Numeric sorts. Undefined symbols are always considered "less than"
614 defined symbols with zero values. Common symbols are not treated
615 specially -- i.e., their sizes are used as their "values". */
618 numeric_forward (P_x
, P_y
)
625 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
626 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
627 if (x
== NULL
|| y
== NULL
)
628 bfd_fatal (bfd_get_filename (sort_bfd
));
630 xs
= bfd_get_section (x
);
631 ys
= bfd_get_section (y
);
633 if (bfd_is_und_section (xs
))
635 if (! bfd_is_und_section (ys
))
638 else if (bfd_is_und_section (ys
))
640 else if (valueof (x
) != valueof (y
))
641 return valueof (x
) < valueof (y
) ? -1 : 1;
643 return non_numeric_forward (P_x
, P_y
);
647 numeric_reverse (x
, y
)
651 return - numeric_forward (x
, y
);
655 non_numeric_forward (P_x
, P_y
)
662 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
663 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
664 if (x
== NULL
|| y
== NULL
)
665 bfd_fatal (bfd_get_filename (sort_bfd
));
667 xn
= bfd_asymbol_name (x
);
668 yn
= bfd_asymbol_name (y
);
670 return ((xn
== NULL
) ? ((yn
== NULL
) ? 0 : -1) :
671 ((yn
== NULL
) ? 1 : strcmp (xn
, yn
)));
675 non_numeric_reverse (x
, y
)
679 return - non_numeric_forward (x
, y
);
682 static int (*(sorters
[2][2])) PARAMS ((const PTR
, const PTR
)) =
684 { non_numeric_forward
, non_numeric_reverse
},
685 { numeric_forward
, numeric_reverse
}
688 /* This sort routine is used by sort_symbols_by_size. It is similar
689 to numeric_forward, but when symbols have the same value it sorts
690 by section VMA. This simplifies the sort_symbols_by_size code
691 which handles symbols at the end of sections. Also, this routine
692 tries to sort file names before other symbols with the same value.
693 That will make the file name have a zero size, which will make
694 sort_symbols_by_size choose the non file name symbol, leading to
695 more meaningful output. For similar reasons, this code sorts
696 gnu_compiled_* and gcc2_compiled before other symbols with the same
700 size_forward1 (P_x
, P_y
)
710 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
711 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
712 if (x
== NULL
|| y
== NULL
)
713 bfd_fatal (bfd_get_filename (sort_bfd
));
715 xs
= bfd_get_section (x
);
716 ys
= bfd_get_section (y
);
718 if (bfd_is_und_section (xs
))
720 if (bfd_is_und_section (ys
))
723 if (valueof (x
) != valueof (y
))
724 return valueof (x
) < valueof (y
) ? -1 : 1;
726 if (xs
->vma
!= ys
->vma
)
727 return xs
->vma
< ys
->vma
? -1 : 1;
729 xn
= bfd_asymbol_name (x
);
730 yn
= bfd_asymbol_name (y
);
734 /* The symbols gnu_compiled and gcc2_compiled convey even less
735 information than the file name, so sort them out first. */
737 xf
= (strstr (xn
, "gnu_compiled") != NULL
738 || strstr (xn
, "gcc2_compiled") != NULL
);
739 yf
= (strstr (yn
, "gnu_compiled") != NULL
740 || strstr (yn
, "gcc2_compiled") != NULL
);
747 /* We use a heuristic for the file name. It may not work on non
748 Unix systems, but it doesn't really matter; the only difference
749 is precisely which symbol names get printed. */
751 #define file_symbol(s, sn, snl) \
752 (((s)->flags & BSF_FILE) != 0 \
753 || ((sn)[(snl) - 2] == '.' \
754 && ((sn)[(snl) - 1] == 'o' \
755 || (sn)[(snl) - 1] == 'a')))
757 xf
= file_symbol (x
, xn
, xnl
);
758 yf
= file_symbol (y
, yn
, ynl
);
765 return non_numeric_forward (P_x
, P_y
);
768 /* This sort routine is used by sort_symbols_by_size. It is sorting
769 an array of size_sym structures into size order. */
772 size_forward2 (P_x
, P_y
)
776 const struct size_sym
*x
= (const struct size_sym
*) P_x
;
777 const struct size_sym
*y
= (const struct size_sym
*) P_y
;
779 if (x
->size
< y
->size
)
780 return reverse_sort
? 1 : -1;
781 else if (x
->size
> y
->size
)
782 return reverse_sort
? -1 : 1;
784 return sorters
[0][reverse_sort
] (x
->minisym
, y
->minisym
);
787 /* Sort the symbols by size. ELF provides a size but for other formats
788 we have to make a guess by assuming that the difference between the
789 address of a symbol and the address of the next higher symbol is the
793 sort_symbols_by_size (abfd
, dynamic
, minisyms
, symcount
, size
, symsizesp
)
799 struct size_sym
**symsizesp
;
801 struct size_sym
*symsizes
;
802 bfd_byte
*from
, *fromend
;
804 asymbol
*store_sym
, *store_next
;
806 qsort (minisyms
, symcount
, size
, size_forward1
);
808 /* We are going to return a special set of symbols and sizes to
810 symsizes
= (struct size_sym
*) xmalloc (symcount
* sizeof (struct size_sym
));
811 *symsizesp
= symsizes
;
813 /* Note that filter_symbols has already removed all absolute and
814 undefined symbols. Here we remove all symbols whose size winds
816 from
= (bfd_byte
*) minisyms
;
817 fromend
= from
+ symcount
* size
;
824 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, (const PTR
) from
,
827 bfd_fatal (bfd_get_filename (abfd
));
830 for (; from
< fromend
; from
+= size
)
837 if (from
+ size
< fromend
)
839 next
= bfd_minisymbol_to_symbol (abfd
,
841 (const PTR
) (from
+ size
),
844 bfd_fatal (bfd_get_filename (abfd
));
849 sec
= bfd_get_section (sym
);
851 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
852 sz
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
853 else if (bfd_is_com_section (sec
))
857 if (from
+ size
< fromend
858 && sec
== bfd_get_section (next
))
859 sz
= valueof (next
) - valueof (sym
);
861 sz
= (bfd_get_section_vma (abfd
, sec
)
862 + bfd_section_size (abfd
, sec
)
868 symsizes
->minisym
= (const PTR
) from
;
876 store_sym
= store_next
;
880 symcount
= symsizes
- *symsizesp
;
882 /* We must now sort again by size. */
883 qsort ((PTR
) *symsizesp
, symcount
, sizeof (struct size_sym
), size_forward2
);
888 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
891 display_rel_file (abfd
, archive_bfd
)
898 struct size_sym
*symsizes
;
903 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
905 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
910 symcount
= bfd_read_minisymbols (abfd
, dynamic
, &minisyms
, &size
);
912 bfd_fatal (bfd_get_filename (abfd
));
916 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
920 bfd_sprintf_vma (abfd
, buf
, (bfd_vma
) -1);
921 print_width
= strlen (buf
);
923 /* Discard the symbols we don't want to print.
924 It's OK to do this in place; we'll free the storage anyway
927 symcount
= filter_symbols (abfd
, dynamic
, minisyms
, symcount
, size
);
933 sort_dynamic
= dynamic
;
934 sort_x
= bfd_make_empty_symbol (abfd
);
935 sort_y
= bfd_make_empty_symbol (abfd
);
936 if (sort_x
== NULL
|| sort_y
== NULL
)
937 bfd_fatal (bfd_get_filename (abfd
));
940 qsort (minisyms
, symcount
, size
,
941 sorters
[sort_numerically
][reverse_sort
]);
943 symcount
= sort_symbols_by_size (abfd
, dynamic
, minisyms
, symcount
,
948 print_symbols (abfd
, dynamic
, minisyms
, symcount
, size
, archive_bfd
);
950 print_size_symbols (abfd
, dynamic
, symsizes
, symcount
, archive_bfd
);
955 /* Choose which symbol entries to print;
956 compact them downward to get rid of the rest.
957 Return the number of symbols to be printed. */
960 filter_symbols (abfd
, dynamic
, minisyms
, symcount
, size
)
967 bfd_byte
*from
, *fromend
, *to
;
970 store
= bfd_make_empty_symbol (abfd
);
972 bfd_fatal (bfd_get_filename (abfd
));
974 from
= (bfd_byte
*) minisyms
;
975 fromend
= from
+ symcount
* size
;
976 to
= (bfd_byte
*) minisyms
;
978 for (; from
< fromend
; from
+= size
)
985 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, (const PTR
) from
, store
);
987 bfd_fatal (bfd_get_filename (abfd
));
990 keep
= bfd_is_und_section (sym
->section
);
991 else if (external_only
)
992 keep
= ((sym
->flags
& BSF_GLOBAL
) != 0
993 || (sym
->flags
& BSF_WEAK
) != 0
994 || bfd_is_und_section (sym
->section
)
995 || bfd_is_com_section (sym
->section
));
1000 && ! print_debug_syms
1001 && (sym
->flags
& BSF_DEBUGGING
) != 0)
1006 && (bfd_is_abs_section (sym
->section
)
1007 || bfd_is_und_section (sym
->section
)))
1013 if (bfd_is_und_section (sym
->section
))
1019 memcpy (to
, from
, size
);
1024 return (to
- (bfd_byte
*) minisyms
) / size
;
1027 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
1028 demangling it if requested. */
1031 print_symname (format
, name
, abfd
)
1036 if (do_demangle
&& *name
)
1040 /* In this mode, give a user-level view of the symbol name
1041 even if it's not mangled; strip off any leading
1043 if (bfd_get_symbol_leading_char (abfd
) == name
[0])
1046 res
= cplus_demangle (name
, DMGL_ANSI
| DMGL_PARAMS
);
1049 printf (format
, res
);
1055 printf (format
, name
);
1058 /* Print the symbols. If ARCHIVE_BFD is non-NULL, it is the archive
1062 print_symbols (abfd
, dynamic
, minisyms
, symcount
, size
, archive_bfd
)
1071 bfd_byte
*from
, *fromend
;
1073 store
= bfd_make_empty_symbol (abfd
);
1075 bfd_fatal (bfd_get_filename (abfd
));
1077 from
= (bfd_byte
*) minisyms
;
1078 fromend
= from
+ symcount
* size
;
1079 for (; from
< fromend
; from
+= size
)
1084 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, from
, store
);
1086 bfd_fatal (bfd_get_filename (abfd
));
1088 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1089 ssize
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
1093 print_symbol (abfd
, sym
, ssize
, archive_bfd
);
1097 /* Print the symbols when sorting by size. */
1100 print_size_symbols (abfd
, dynamic
, symsizes
, symcount
, archive_bfd
)
1103 struct size_sym
*symsizes
;
1108 struct size_sym
*from
, *fromend
;
1110 store
= bfd_make_empty_symbol (abfd
);
1112 bfd_fatal (bfd_get_filename (abfd
));
1115 fromend
= from
+ symcount
;
1116 for (; from
< fromend
; from
++)
1121 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, from
->minisym
, store
);
1123 bfd_fatal (bfd_get_filename (abfd
));
1125 /* Set the symbol value so that we actually display the symbol size. */
1126 sym
->value
= from
->size
- bfd_section_vma (abfd
, bfd_get_section (sym
));
1128 /* For elf we have already computed the correct symbol size. */
1129 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1132 ssize
= from
->size
- bfd_section_vma (abfd
, bfd_get_section (sym
));
1134 print_symbol (abfd
, sym
, ssize
, archive_bfd
);
1138 /* Print a single symbol. */
1141 print_symbol (abfd
, sym
, ssize
, archive_bfd
)
1149 (*format
->print_symbol_filename
) (archive_bfd
, abfd
);
1153 if (bfd_is_und_section (bfd_get_section (sym
)))
1154 print_symname ("%s", bfd_asymbol_name (sym
), abfd
);
1158 symbol_info syminfo
;
1159 struct extended_symbol_info info
;
1161 bfd_get_symbol_info (abfd
, sym
, &syminfo
);
1162 info
.sinfo
= &syminfo
;
1164 (*format
->print_symbol_info
) (&info
, abfd
);
1169 static asymbol
**syms
;
1170 static long symcount
;
1171 const char *filename
, *functionname
;
1172 unsigned int lineno
;
1174 /* We need to get the canonical symbols in order to call
1175 bfd_find_nearest_line. This is inefficient, but, then, you
1176 don't have to use --line-numbers. */
1177 if (abfd
!= lineno_cache_bfd
&& syms
!= NULL
)
1186 symsize
= bfd_get_symtab_upper_bound (abfd
);
1188 bfd_fatal (bfd_get_filename (abfd
));
1189 syms
= (asymbol
**) xmalloc (symsize
);
1190 symcount
= bfd_canonicalize_symtab (abfd
, syms
);
1192 bfd_fatal (bfd_get_filename (abfd
));
1193 lineno_cache_bfd
= abfd
;
1196 if (bfd_is_und_section (bfd_get_section (sym
)))
1198 static asection
**secs
;
1199 static arelent
***relocs
;
1200 static long *relcount
;
1201 static unsigned int seccount
;
1203 const char *symname
;
1205 /* For an undefined symbol, we try to find a reloc for the
1206 symbol, and print the line number of the reloc. */
1207 if (abfd
!= lineno_cache_rel_bfd
&& relocs
!= NULL
)
1209 for (i
= 0; i
< seccount
; i
++)
1210 if (relocs
[i
] != NULL
)
1222 struct get_relocs_info info
;
1224 seccount
= bfd_count_sections (abfd
);
1226 secs
= (asection
**) xmalloc (seccount
* sizeof *secs
);
1227 relocs
= (arelent
***) xmalloc (seccount
* sizeof *relocs
);
1228 relcount
= (long *) xmalloc (seccount
* sizeof *relcount
);
1231 info
.relocs
= relocs
;
1232 info
.relcount
= relcount
;
1234 bfd_map_over_sections (abfd
, get_relocs
, (PTR
) &info
);
1235 lineno_cache_rel_bfd
= abfd
;
1238 symname
= bfd_asymbol_name (sym
);
1239 for (i
= 0; i
< seccount
; i
++)
1243 for (j
= 0; j
< relcount
[i
]; j
++)
1248 if (r
->sym_ptr_ptr
!= NULL
1249 && (*r
->sym_ptr_ptr
)->section
== sym
->section
1250 && (*r
->sym_ptr_ptr
)->value
== sym
->value
1252 bfd_asymbol_name (*r
->sym_ptr_ptr
)) == 0
1253 && bfd_find_nearest_line (abfd
, secs
[i
], syms
,
1254 r
->address
, &filename
,
1255 &functionname
, &lineno
)
1256 && filename
!= NULL
)
1258 /* We only print the first one we find. */
1259 printf ("\t%s:%u", filename
, lineno
);
1266 else if (bfd_get_section (sym
)->owner
== abfd
)
1268 if (bfd_find_nearest_line (abfd
, bfd_get_section (sym
), syms
,
1269 sym
->value
, &filename
, &functionname
,
1274 printf ("\t%s:%u", filename
, lineno
);
1282 /* The following 3 groups of functions are called unconditionally,
1283 once at the start of processing each file of the appropriate type.
1284 They should check `filename_per_file' and `filename_per_symbol',
1285 as appropriate for their output format, to determine whether to
1288 /* Print the name of an object file given on the command line. */
1291 print_object_filename_bsd (filename
)
1294 if (filename_per_file
&& !filename_per_symbol
)
1295 printf ("\n%s:\n", filename
);
1299 print_object_filename_sysv (filename
)
1303 printf (_("\n\nUndefined symbols from %s:\n\n"), filename
);
1305 printf (_("\n\nSymbols from %s:\n\n"), filename
);
1307 Name Value Class Type Size Line Section\n\n"));
1311 print_object_filename_posix (filename
)
1314 if (filename_per_file
&& !filename_per_symbol
)
1315 printf ("%s:\n", filename
);
1318 /* Print the name of an archive file given on the command line. */
1321 print_archive_filename_bsd (filename
)
1324 if (filename_per_file
)
1325 printf ("\n%s:\n", filename
);
1329 print_archive_filename_sysv (filename
)
1330 char *filename ATTRIBUTE_UNUSED
;
1335 print_archive_filename_posix (filename
)
1336 char *filename ATTRIBUTE_UNUSED
;
1340 /* Print the name of an archive member file. */
1343 print_archive_member_bsd (archive
, filename
)
1344 char *archive ATTRIBUTE_UNUSED
;
1345 const char *filename
;
1347 if (!filename_per_symbol
)
1348 printf ("\n%s:\n", filename
);
1352 print_archive_member_sysv (archive
, filename
)
1354 const char *filename
;
1357 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive
, filename
);
1359 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive
, filename
);
1361 Name Value Class Type Size Line Section\n\n"));
1365 print_archive_member_posix (archive
, filename
)
1367 const char *filename
;
1369 if (!filename_per_symbol
)
1370 printf ("%s[%s]:\n", archive
, filename
);
1373 /* Print the name of the file (and archive, if there is one)
1374 containing a symbol. */
1377 print_symbol_filename_bsd (archive_bfd
, abfd
)
1378 bfd
*archive_bfd
, *abfd
;
1380 if (filename_per_symbol
)
1383 printf ("%s:", bfd_get_filename (archive_bfd
));
1384 printf ("%s:", bfd_get_filename (abfd
));
1389 print_symbol_filename_sysv (archive_bfd
, abfd
)
1390 bfd
*archive_bfd
, *abfd
;
1392 if (filename_per_symbol
)
1395 printf ("%s:", bfd_get_filename (archive_bfd
));
1396 printf ("%s:", bfd_get_filename (abfd
));
1401 print_symbol_filename_posix (archive_bfd
, abfd
)
1402 bfd
*archive_bfd
, *abfd
;
1404 if (filename_per_symbol
)
1407 printf ("%s[%s]: ", bfd_get_filename (archive_bfd
),
1408 bfd_get_filename (abfd
));
1410 printf ("%s: ", bfd_get_filename (abfd
));
1414 /* Print a symbol value. */
1417 print_value (abfd
, val
)
1418 bfd
*abfd ATTRIBUTE_UNUSED
;
1421 #if ! defined (BFD64) || BFD_HOST_64BIT_LONG
1422 printf (value_format
, val
);
1424 /* We have a 64 bit value to print, but the host is only 32 bit. */
1425 if (print_radix
== 16)
1426 bfd_fprintf_vma (abfd
, stdout
, val
);
1432 s
= buf
+ sizeof buf
;
1436 *--s
= (val
% print_radix
) + '0';
1439 while ((buf
+ sizeof buf
- 1) - s
< 16)
1446 /* Print a line of information about a symbol. */
1449 print_symbol_info_bsd (info
, abfd
)
1450 struct extended_symbol_info
*info
;
1453 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1455 if (print_width
== 16)
1461 print_value (abfd
, SYM_VALUE (info
));
1463 if (print_size
&& SYM_SIZE (info
))
1466 print_value (abfd
, SYM_SIZE (info
));
1470 printf (" %c", SYM_TYPE (info
));
1472 if (SYM_TYPE (info
) == '-')
1476 printf (other_format
, SYM_STAB_OTHER (info
));
1478 printf (desc_format
, SYM_STAB_DESC (info
));
1479 printf (" %5s", SYM_STAB_NAME (info
));
1481 print_symname (" %s", SYM_NAME (info
), abfd
);
1485 print_symbol_info_sysv (info
, abfd
)
1486 struct extended_symbol_info
*info
;
1489 print_symname ("%-20s|", SYM_NAME (info
), abfd
);
1491 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1494 print_value (abfd
, SYM_VALUE (info
));
1496 printf ("| %c |", SYM_TYPE (info
));
1498 if (SYM_TYPE (info
) == '-')
1501 printf ("%18s| ", SYM_STAB_NAME (info
)); /* (C) Type */
1502 printf (desc_format
, SYM_STAB_DESC (info
)); /* Size */
1503 printf ("| |"); /* Line, Section */
1507 /* Type, Size, Line, Section */
1510 if (SYM_SIZE (info
))
1511 print_value (abfd
, SYM_SIZE (info
));
1520 print_symbol_info_posix (info
, abfd
)
1521 struct extended_symbol_info
*info
;
1524 print_symname ("%s ", SYM_NAME (info
), abfd
);
1525 printf ("%c ", SYM_TYPE (info
));
1527 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1531 print_value (abfd
, SYM_VALUE (info
));
1533 if (SYM_SIZE (info
))
1534 print_value (abfd
, SYM_SIZE (info
));
1539 print_symdef_entry (abfd
)
1542 symindex idx
= BFD_NO_MORE_SYMBOLS
;
1544 boolean everprinted
= false;
1546 for (idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
);
1547 idx
!= BFD_NO_MORE_SYMBOLS
;
1548 idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
))
1553 printf (_("\nArchive index:\n"));
1556 elt
= bfd_get_elt_at_index (abfd
, idx
);
1558 bfd_fatal ("bfd_get_elt_at_index");
1559 if (thesym
->name
!= (char *) NULL
)
1561 print_symname ("%s", thesym
->name
, abfd
);
1562 printf (" in %s\n", bfd_get_filename (elt
));
1567 /* This function is used to get the relocs for a particular section.
1568 It is called via bfd_map_over_sections. */
1571 get_relocs (abfd
, sec
, dataarg
)
1576 struct get_relocs_info
*data
= (struct get_relocs_info
*) dataarg
;
1580 if ((sec
->flags
& SEC_RELOC
) == 0)
1582 *data
->relocs
= NULL
;
1583 *data
->relcount
= 0;
1589 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1591 bfd_fatal (bfd_get_filename (abfd
));
1593 *data
->relocs
= (arelent
**) xmalloc (relsize
);
1594 *data
->relcount
= bfd_canonicalize_reloc (abfd
, sec
, *data
->relocs
,
1596 if (*data
->relcount
< 0)
1597 bfd_fatal (bfd_get_filename (abfd
));