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 #include "elf/common.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
54 struct extended_symbol_info
58 elf_symbol_type
*elfinfo
;
59 /* FIXME: We should add more fields for Type, Line, Section. */
61 #define SYM_NAME(sym) (sym->sinfo->name)
62 #define SYM_VALUE(sym) (sym->sinfo->value)
63 #define SYM_TYPE(sym) (sym->sinfo->type)
64 #define SYM_STAB_NAME(sym) (sym->sinfo->stab_name)
65 #define SYM_STAB_DESC(sym) (sym->sinfo->stab_desc)
66 #define SYM_STAB_OTHER(sym) (sym->sinfo->stab_other)
67 #define SYM_SIZE(sym) \
68 (sym->elfinfo ? sym->elfinfo->internal_elf_sym.st_size: sym->ssize)
70 static void usage
PARAMS ((FILE *, int));
71 static void set_print_radix
PARAMS ((char *));
72 static void set_output_format
PARAMS ((char *));
73 static void display_archive
PARAMS ((bfd
*));
74 static boolean display_file
PARAMS ((char *));
75 static void display_rel_file
PARAMS ((bfd
*, bfd
*));
76 static long filter_symbols
PARAMS ((bfd
*, boolean
, PTR
, long, unsigned int));
77 static long sort_symbols_by_size
PARAMS ((bfd
*, boolean
, PTR
, long, unsigned int, struct size_sym
**));
78 static void print_symbols
PARAMS ((bfd
*, boolean
, PTR
, long, unsigned int, bfd
*));
79 static void print_size_symbols
PARAMS ((bfd
*, boolean
, struct size_sym
*, long, bfd
*));
80 static void print_symname
PARAMS ((const char *, const char *, bfd
*));
81 static void print_symbol
PARAMS ((bfd
*, asymbol
*, bfd_vma ssize
, bfd
*));
82 static void print_symdef_entry
PARAMS ((bfd
*));
84 /* The sorting functions. */
85 static int numeric_forward
PARAMS ((const PTR
, const PTR
));
86 static int numeric_reverse
PARAMS ((const PTR
, const PTR
));
87 static int non_numeric_forward
PARAMS ((const PTR
, const PTR
));
88 static int non_numeric_reverse
PARAMS ((const PTR
, const PTR
));
89 static int size_forward1
PARAMS ((const PTR
, const PTR
));
90 static int size_forward2
PARAMS ((const PTR
, const PTR
));
92 /* The output formatting functions. */
93 static void print_object_filename_bsd
PARAMS ((char *));
94 static void print_object_filename_sysv
PARAMS ((char *));
95 static void print_object_filename_posix
PARAMS ((char *));
96 static void print_archive_filename_bsd
PARAMS ((char *));
97 static void print_archive_filename_sysv
PARAMS ((char *));
98 static void print_archive_filename_posix
PARAMS ((char *));
99 static void print_archive_member_bsd
PARAMS ((char *, const char *));
100 static void print_archive_member_sysv
PARAMS ((char *, const char *));
101 static void print_archive_member_posix
PARAMS ((char *, const char *));
102 static void print_symbol_filename_bsd
PARAMS ((bfd
*, bfd
*));
103 static void print_symbol_filename_sysv
PARAMS ((bfd
*, bfd
*));
104 static void print_symbol_filename_posix
PARAMS ((bfd
*, bfd
*));
105 static void print_value
PARAMS ((bfd
*, bfd_vma
));
106 static void print_symbol_info_bsd
PARAMS ((struct extended_symbol_info
*, bfd
*));
107 static void print_symbol_info_sysv
PARAMS ((struct extended_symbol_info
*, bfd
*));
108 static void print_symbol_info_posix
PARAMS ((struct extended_symbol_info
*, bfd
*));
109 static void get_relocs
PARAMS ((bfd
*, asection
*, PTR
));
110 static const char * get_symbol_type
PARAMS ((unsigned int));
112 /* Support for different output formats. */
115 /* Print the name of an object file given on the command line. */
116 void (*print_object_filename
) PARAMS ((char *));
118 /* Print the name of an archive file given on the command line. */
119 void (*print_archive_filename
) PARAMS ((char *));
121 /* Print the name of an archive member file. */
122 void (*print_archive_member
) PARAMS ((char *, const char *));
124 /* Print the name of the file (and archive, if there is one)
125 containing a symbol. */
126 void (*print_symbol_filename
) PARAMS ((bfd
*, bfd
*));
128 /* Print a line of information about a symbol. */
129 void (*print_symbol_info
) PARAMS ((struct extended_symbol_info
*, bfd
*));
132 static struct output_fns formats
[] =
134 {print_object_filename_bsd
,
135 print_archive_filename_bsd
,
136 print_archive_member_bsd
,
137 print_symbol_filename_bsd
,
138 print_symbol_info_bsd
},
139 {print_object_filename_sysv
,
140 print_archive_filename_sysv
,
141 print_archive_member_sysv
,
142 print_symbol_filename_sysv
,
143 print_symbol_info_sysv
},
144 {print_object_filename_posix
,
145 print_archive_filename_posix
,
146 print_archive_member_posix
,
147 print_symbol_filename_posix
,
148 print_symbol_info_posix
}
151 /* Indices in `formats'. */
153 #define FORMAT_SYSV 1
154 #define FORMAT_POSIX 2
155 #define FORMAT_DEFAULT FORMAT_BSD
157 /* The output format to use. */
158 static struct output_fns
*format
= &formats
[FORMAT_DEFAULT
];
160 /* Command options. */
162 static int do_demangle
= 0; /* Pretty print C++ symbol names. */
163 static int external_only
= 0; /* Print external symbols only. */
164 static int defined_only
= 0; /* Print defined symbols only. */
165 static int no_sort
= 0; /* Don't sort; print syms in order found. */
166 static int print_debug_syms
= 0;/* Print debugger-only symbols too. */
167 static int print_armap
= 0; /* Describe __.SYMDEF data in archive files. */
168 static int print_size
= 0; /* Print size of defined symbols. */
169 static int reverse_sort
= 0; /* Sort in downward(alpha or numeric) order. */
170 static int sort_numerically
= 0;/* Sort in numeric rather than alpha order. */
171 static int sort_by_size
= 0; /* Sort by size of symbol. */
172 static int undefined_only
= 0; /* Print undefined symbols only. */
173 static int dynamic
= 0; /* Print dynamic symbols. */
174 static int show_version
= 0; /* Show the version number. */
175 static int show_stats
= 0; /* Show statistics. */
176 static int line_numbers
= 0; /* Print line numbers for symbols. */
178 /* When to print the names of files. Not mutually exclusive in SYSV format. */
179 static int filename_per_file
= 0; /* Once per file, on its own line. */
180 static int filename_per_symbol
= 0; /* Once per symbol, at start of line. */
182 /* Print formats for printing a symbol value. */
184 static char value_format
[] = "%08lx";
186 #if BFD_HOST_64BIT_LONG
187 static char value_format
[] = "%016lx";
189 /* We don't use value_format for this case. */
193 static int print_width
= 16;
195 static int print_width
= 8;
197 static int print_radix
= 16;
198 /* Print formats for printing stab info. */
199 static char other_format
[] = "%02x";
200 static char desc_format
[] = "%04x";
202 static char *target
= NULL
;
204 /* Used to cache the line numbers for a BFD. */
205 static bfd
*lineno_cache_bfd
;
206 static bfd
*lineno_cache_rel_bfd
;
208 #define OPTION_TARGET 200
210 static struct option long_options
[] =
212 {"debug-syms", no_argument
, &print_debug_syms
, 1},
213 {"demangle", optional_argument
, 0, 'C'},
214 {"dynamic", no_argument
, &dynamic
, 1},
215 {"extern-only", no_argument
, &external_only
, 1},
216 {"format", required_argument
, 0, 'f'},
217 {"help", no_argument
, 0, 'h'},
218 {"line-numbers", no_argument
, 0, 'l'},
219 {"no-cplus", no_argument
, &do_demangle
, 0}, /* Linux compatibility. */
220 {"no-demangle", no_argument
, &do_demangle
, 0},
221 {"no-sort", no_argument
, &no_sort
, 1},
222 {"numeric-sort", no_argument
, &sort_numerically
, 1},
223 {"portability", no_argument
, 0, 'P'},
224 {"print-armap", no_argument
, &print_armap
, 1},
225 {"print-file-name", no_argument
, 0, 'o'},
226 {"print-size", no_argument
, 0, 'S'},
227 {"radix", required_argument
, 0, 't'},
228 {"reverse-sort", no_argument
, &reverse_sort
, 1},
229 {"size-sort", no_argument
, &sort_by_size
, 1},
230 {"stats", no_argument
, &show_stats
, 1},
231 {"target", required_argument
, 0, OPTION_TARGET
},
232 {"defined-only", no_argument
, &defined_only
, 1},
233 {"undefined-only", no_argument
, &undefined_only
, 1},
234 {"version", no_argument
, &show_version
, 1},
235 {0, no_argument
, 0, 0}
238 /* Some error-reporting functions. */
241 usage (stream
, status
)
245 fprintf (stream
, _("Usage: %s [option(s)] [file(s)]\n"), program_name
);
246 fprintf (stream
, _(" List symbols in [file(s)] (a.out by default).\n"));
247 fprintf (stream
, _(" The options are:\n\
248 -a, --debug-syms Display debugger-only symbols\n\
249 -A, --print-file-name Print name of the input file before every symbol\n\
250 -B Same as --format=bsd\n\
251 -C, --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
252 The STYLE, if specified, can be `auto' (the default),\n\
253 `gnu', 'lucid', 'arm', 'hp', 'edg' or 'gnu-new-abi'\n\
254 --no-demangle Do not demangle low-level symbol names\n\
255 -D, --dynamic Display dynamic symbols instead of normal symbols\n\
256 --defined-only Display only defined symbols\n\
258 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
259 `sysv' or `posix'. The default is `bsd'\n\
260 -g, --extern-only Display only external symbols\n\
261 -l, --line-numbers Use debugging information to find a filename and\n\
262 line number for each symbol\n\
263 -n, --numeric-sort Sort symbols numerically by address\n\
265 -p, --no-sort Do not sort the symbols\n\
266 -P, --portability Same as --format=posix\n\
267 -r, --reverse-sort Reverse the sense of the sort\n\
268 -S, --print-size Print size of defined symbols\n\
269 -s, --print-armap Include index for symbols from archive members\n\
270 --size-sort Sort symbols by size\n\
271 -t, --radix=RADIX Use RADIX for printing symbol values\n\
272 --target=BFDNAME Specify the target object format as BFDNAME\n\
273 -u, --undefined-only Display only undefined symbols\n\
274 -X 32_64 (ignored)\n\
275 -h, --help Display this information\n\
276 -V, --version Display this program's version number\n\
278 list_supported_targets (program_name
, stream
);
280 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
284 /* Set the radix for the symbol value and size according to RADIX. */
287 set_print_radix (radix
)
301 value_format
[4] = *radix
;
303 #if BFD_HOST_64BIT_LONG
304 value_format
[5] = *radix
;
306 /* This case requires special handling for octal and decimal
310 other_format
[3] = desc_format
[3] = *radix
;
313 fatal (_("%s: invalid radix"), radix
);
318 set_output_format (f
)
338 fatal (_("%s: invalid output format"), f
);
340 format
= &formats
[i
];
343 int main
PARAMS ((int, char **));
353 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
354 setlocale (LC_MESSAGES
, "");
356 #if defined (HAVE_SETLOCALE)
357 setlocale (LC_CTYPE
, "");
358 setlocale (LC_COLLATE
, "");
360 bindtextdomain (PACKAGE
, LOCALEDIR
);
361 textdomain (PACKAGE
);
363 program_name
= *argv
;
364 xmalloc_set_program_name (program_name
);
366 START_PROGRESS (program_name
, 0);
369 set_default_bfd_target ();
371 while ((c
= getopt_long (argc
, argv
, "aABCDef:gHhlnopPrSst:uvVvX:",
372 long_options
, (int *) 0)) != EOF
)
377 print_debug_syms
= 1;
381 filename_per_symbol
= 1;
383 case 'B': /* For MIPS compatibility. */
384 set_output_format ("bsd");
390 enum demangling_styles style
;
392 style
= cplus_demangle_name_to_style (optarg
);
393 if (style
== unknown_demangling
)
394 fatal (_("unknown demangling style `%s'"),
397 cplus_demangle_set_style (style
);
404 /* Ignored for HP/UX compatibility. */
407 set_output_format (optarg
);
420 sort_numerically
= 1;
426 set_output_format ("posix");
438 set_print_radix (optarg
);
447 /* Ignored for (partial) AIX compatibility. On AIX, the
448 argument has values 32, 64, or 32_64, and specfies that
449 only 32-bit, only 64-bit, or both kinds of objects should
450 be examined. The default is 32. So plain AIX nm on a
451 library archive with both kinds of objects will ignore
452 the 64-bit ones. For GNU nm, the default is and always
453 has been -X 32_64, and other options are not supported. */
454 if (strcmp (optarg
, "32_64") != 0)
455 fatal (_("Only -X 32_64 is supported"));
458 case OPTION_TARGET
: /* --target */
462 case 0: /* A long option that just sets a flag. */
471 print_version ("nm");
473 /* OK, all options now parsed. If no filename specified, do a.out. */
475 return !display_file ("a.out");
479 if (argc
- optind
> 1)
480 filename_per_file
= 1;
482 /* We were given several filenames to do. */
483 while (optind
< argc
)
486 if (!display_file (argv
[optind
++]))
490 END_PROGRESS (program_name
);
495 char *lim
= (char *) sbrk (0);
497 non_fatal (_("data size %ld"), (long) (lim
- (char *) &environ
));
506 get_symbol_type (type
)
509 static char buff
[32];
513 case STT_NOTYPE
: return "NOTYPE";
514 case STT_OBJECT
: return "OBJECT";
515 case STT_FUNC
: return "FUNC";
516 case STT_SECTION
: return "SECTION";
517 case STT_FILE
: return "FILE";
518 case STT_COMMON
: return "COMMON";
519 case STT_TLS
: return "TLS";
521 if (type
>= STT_LOPROC
&& type
<= STT_HIPROC
)
522 sprintf (buff
, _("<processor specific>: %d"), type
);
523 else if (type
>= STT_LOOS
&& type
<= STT_HIOS
)
524 sprintf (buff
, _("<OS specific>: %d"), type
);
526 sprintf (buff
, _("<unknown>: %d"), type
);
532 display_archive (file
)
536 bfd
*last_arfile
= NULL
;
539 (*format
->print_archive_filename
) (bfd_get_filename (file
));
542 print_symdef_entry (file
);
548 arfile
= bfd_openr_next_archived_file (file
, arfile
);
552 if (bfd_get_error () != bfd_error_no_more_archived_files
)
553 bfd_fatal (bfd_get_filename (file
));
557 if (bfd_check_format_matches (arfile
, bfd_object
, &matching
))
561 bfd_sprintf_vma (arfile
, buf
, (bfd_vma
) -1);
562 print_width
= strlen (buf
);
563 (*format
->print_archive_member
) (bfd_get_filename (file
),
564 bfd_get_filename (arfile
));
565 display_rel_file (arfile
, file
);
569 bfd_nonfatal (bfd_get_filename (arfile
));
570 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
572 list_matching_formats (matching
);
577 if (last_arfile
!= NULL
)
579 bfd_close (last_arfile
);
580 lineno_cache_bfd
= NULL
;
581 lineno_cache_rel_bfd
= NULL
;
583 last_arfile
= arfile
;
586 if (last_arfile
!= NULL
)
588 bfd_close (last_arfile
);
589 lineno_cache_bfd
= NULL
;
590 lineno_cache_rel_bfd
= NULL
;
595 display_file (filename
)
598 boolean retval
= true;
602 file
= bfd_openr (filename
, target
);
605 bfd_nonfatal (filename
);
609 if (bfd_check_format (file
, bfd_archive
))
611 display_archive (file
);
613 else if (bfd_check_format_matches (file
, bfd_object
, &matching
))
617 bfd_sprintf_vma (file
, buf
, (bfd_vma
) -1);
618 print_width
= strlen (buf
);
619 (*format
->print_object_filename
) (filename
);
620 display_rel_file (file
, NULL
);
624 bfd_nonfatal (filename
);
625 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
627 list_matching_formats (matching
);
633 if (bfd_close (file
) == false)
634 bfd_fatal (filename
);
636 lineno_cache_bfd
= NULL
;
637 lineno_cache_rel_bfd
= NULL
;
642 /* These globals are used to pass information into the sorting
644 static bfd
*sort_bfd
;
645 static boolean sort_dynamic
;
646 static asymbol
*sort_x
;
647 static asymbol
*sort_y
;
649 /* Symbol-sorting predicates */
650 #define valueof(x) ((x)->section->vma + (x)->value)
652 /* Numeric sorts. Undefined symbols are always considered "less than"
653 defined symbols with zero values. Common symbols are not treated
654 specially -- i.e., their sizes are used as their "values". */
657 numeric_forward (P_x
, P_y
)
664 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
665 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
666 if (x
== NULL
|| y
== NULL
)
667 bfd_fatal (bfd_get_filename (sort_bfd
));
669 xs
= bfd_get_section (x
);
670 ys
= bfd_get_section (y
);
672 if (bfd_is_und_section (xs
))
674 if (! bfd_is_und_section (ys
))
677 else if (bfd_is_und_section (ys
))
679 else if (valueof (x
) != valueof (y
))
680 return valueof (x
) < valueof (y
) ? -1 : 1;
682 return non_numeric_forward (P_x
, P_y
);
686 numeric_reverse (x
, y
)
690 return - numeric_forward (x
, y
);
694 non_numeric_forward (P_x
, P_y
)
701 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
702 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
703 if (x
== NULL
|| y
== NULL
)
704 bfd_fatal (bfd_get_filename (sort_bfd
));
706 xn
= bfd_asymbol_name (x
);
707 yn
= bfd_asymbol_name (y
);
715 /* Solaris 2.5 has a bug in strcoll.
716 strcoll returns invalid values when confronted with empty strings. */
722 return strcoll (xn
, yn
);
724 return strcmp (xn
, yn
);
729 non_numeric_reverse (x
, y
)
733 return - non_numeric_forward (x
, y
);
736 static int (*(sorters
[2][2])) PARAMS ((const PTR
, const PTR
)) =
738 { non_numeric_forward
, non_numeric_reverse
},
739 { numeric_forward
, numeric_reverse
}
742 /* This sort routine is used by sort_symbols_by_size. It is similar
743 to numeric_forward, but when symbols have the same value it sorts
744 by section VMA. This simplifies the sort_symbols_by_size code
745 which handles symbols at the end of sections. Also, this routine
746 tries to sort file names before other symbols with the same value.
747 That will make the file name have a zero size, which will make
748 sort_symbols_by_size choose the non file name symbol, leading to
749 more meaningful output. For similar reasons, this code sorts
750 gnu_compiled_* and gcc2_compiled before other symbols with the same
754 size_forward1 (P_x
, P_y
)
764 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
765 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
766 if (x
== NULL
|| y
== NULL
)
767 bfd_fatal (bfd_get_filename (sort_bfd
));
769 xs
= bfd_get_section (x
);
770 ys
= bfd_get_section (y
);
772 if (bfd_is_und_section (xs
))
774 if (bfd_is_und_section (ys
))
777 if (valueof (x
) != valueof (y
))
778 return valueof (x
) < valueof (y
) ? -1 : 1;
780 if (xs
->vma
!= ys
->vma
)
781 return xs
->vma
< ys
->vma
? -1 : 1;
783 xn
= bfd_asymbol_name (x
);
784 yn
= bfd_asymbol_name (y
);
788 /* The symbols gnu_compiled and gcc2_compiled convey even less
789 information than the file name, so sort them out first. */
791 xf
= (strstr (xn
, "gnu_compiled") != NULL
792 || strstr (xn
, "gcc2_compiled") != NULL
);
793 yf
= (strstr (yn
, "gnu_compiled") != NULL
794 || strstr (yn
, "gcc2_compiled") != NULL
);
801 /* We use a heuristic for the file name. It may not work on non
802 Unix systems, but it doesn't really matter; the only difference
803 is precisely which symbol names get printed. */
805 #define file_symbol(s, sn, snl) \
806 (((s)->flags & BSF_FILE) != 0 \
807 || ((sn)[(snl) - 2] == '.' \
808 && ((sn)[(snl) - 1] == 'o' \
809 || (sn)[(snl) - 1] == 'a')))
811 xf
= file_symbol (x
, xn
, xnl
);
812 yf
= file_symbol (y
, yn
, ynl
);
819 return non_numeric_forward (P_x
, P_y
);
822 /* This sort routine is used by sort_symbols_by_size. It is sorting
823 an array of size_sym structures into size order. */
826 size_forward2 (P_x
, P_y
)
830 const struct size_sym
*x
= (const struct size_sym
*) P_x
;
831 const struct size_sym
*y
= (const struct size_sym
*) P_y
;
833 if (x
->size
< y
->size
)
834 return reverse_sort
? 1 : -1;
835 else if (x
->size
> y
->size
)
836 return reverse_sort
? -1 : 1;
838 return sorters
[0][reverse_sort
] (x
->minisym
, y
->minisym
);
841 /* Sort the symbols by size. ELF provides a size but for other formats
842 we have to make a guess by assuming that the difference between the
843 address of a symbol and the address of the next higher symbol is the
847 sort_symbols_by_size (abfd
, dynamic
, minisyms
, symcount
, size
, symsizesp
)
853 struct size_sym
**symsizesp
;
855 struct size_sym
*symsizes
;
856 bfd_byte
*from
, *fromend
;
858 asymbol
*store_sym
, *store_next
;
860 qsort (minisyms
, symcount
, size
, size_forward1
);
862 /* We are going to return a special set of symbols and sizes to
864 symsizes
= (struct size_sym
*) xmalloc (symcount
* sizeof (struct size_sym
));
865 *symsizesp
= symsizes
;
867 /* Note that filter_symbols has already removed all absolute and
868 undefined symbols. Here we remove all symbols whose size winds
870 from
= (bfd_byte
*) minisyms
;
871 fromend
= from
+ symcount
* size
;
878 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, (const PTR
) from
,
881 bfd_fatal (bfd_get_filename (abfd
));
884 for (; from
< fromend
; from
+= size
)
891 if (from
+ size
< fromend
)
893 next
= bfd_minisymbol_to_symbol (abfd
,
895 (const PTR
) (from
+ size
),
898 bfd_fatal (bfd_get_filename (abfd
));
903 sec
= bfd_get_section (sym
);
905 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
906 sz
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
907 else if (bfd_is_com_section (sec
))
911 if (from
+ size
< fromend
912 && sec
== bfd_get_section (next
))
913 sz
= valueof (next
) - valueof (sym
);
915 sz
= (bfd_get_section_vma (abfd
, sec
)
916 + bfd_section_size (abfd
, sec
)
922 symsizes
->minisym
= (const PTR
) from
;
930 store_sym
= store_next
;
934 symcount
= symsizes
- *symsizesp
;
936 /* We must now sort again by size. */
937 qsort ((PTR
) *symsizesp
, symcount
, sizeof (struct size_sym
), size_forward2
);
942 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
945 display_rel_file (abfd
, archive_bfd
)
952 struct size_sym
*symsizes
;
956 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
958 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
963 symcount
= bfd_read_minisymbols (abfd
, dynamic
, &minisyms
, &size
);
965 bfd_fatal (bfd_get_filename (abfd
));
969 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
973 /* Discard the symbols we don't want to print.
974 It's OK to do this in place; we'll free the storage anyway
977 symcount
= filter_symbols (abfd
, dynamic
, minisyms
, symcount
, size
);
983 sort_dynamic
= dynamic
;
984 sort_x
= bfd_make_empty_symbol (abfd
);
985 sort_y
= bfd_make_empty_symbol (abfd
);
986 if (sort_x
== NULL
|| sort_y
== NULL
)
987 bfd_fatal (bfd_get_filename (abfd
));
990 qsort (minisyms
, symcount
, size
,
991 sorters
[sort_numerically
][reverse_sort
]);
993 symcount
= sort_symbols_by_size (abfd
, dynamic
, minisyms
, symcount
,
998 print_symbols (abfd
, dynamic
, minisyms
, symcount
, size
, archive_bfd
);
1000 print_size_symbols (abfd
, dynamic
, symsizes
, symcount
, archive_bfd
);
1005 /* Choose which symbol entries to print;
1006 compact them downward to get rid of the rest.
1007 Return the number of symbols to be printed. */
1010 filter_symbols (abfd
, dynamic
, minisyms
, symcount
, size
)
1017 bfd_byte
*from
, *fromend
, *to
;
1020 store
= bfd_make_empty_symbol (abfd
);
1022 bfd_fatal (bfd_get_filename (abfd
));
1024 from
= (bfd_byte
*) minisyms
;
1025 fromend
= from
+ symcount
* size
;
1026 to
= (bfd_byte
*) minisyms
;
1028 for (; from
< fromend
; from
+= size
)
1035 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, (const PTR
) from
, store
);
1037 bfd_fatal (bfd_get_filename (abfd
));
1040 keep
= bfd_is_und_section (sym
->section
);
1041 else if (external_only
)
1042 keep
= ((sym
->flags
& BSF_GLOBAL
) != 0
1043 || (sym
->flags
& BSF_WEAK
) != 0
1044 || bfd_is_und_section (sym
->section
)
1045 || bfd_is_com_section (sym
->section
));
1050 && ! print_debug_syms
1051 && (sym
->flags
& BSF_DEBUGGING
) != 0)
1056 && (bfd_is_abs_section (sym
->section
)
1057 || bfd_is_und_section (sym
->section
)))
1063 if (bfd_is_und_section (sym
->section
))
1069 memcpy (to
, from
, size
);
1074 return (to
- (bfd_byte
*) minisyms
) / size
;
1077 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
1078 demangling it if requested. */
1081 print_symname (format
, name
, abfd
)
1086 if (do_demangle
&& *name
)
1090 /* In this mode, give a user-level view of the symbol name
1091 even if it's not mangled; strip off any leading
1093 if (bfd_get_symbol_leading_char (abfd
) == name
[0])
1096 res
= cplus_demangle (name
, DMGL_ANSI
| DMGL_PARAMS
);
1099 printf (format
, res
);
1105 printf (format
, name
);
1108 /* Print the symbols. If ARCHIVE_BFD is non-NULL, it is the archive
1112 print_symbols (abfd
, dynamic
, minisyms
, symcount
, size
, archive_bfd
)
1121 bfd_byte
*from
, *fromend
;
1123 store
= bfd_make_empty_symbol (abfd
);
1125 bfd_fatal (bfd_get_filename (abfd
));
1127 from
= (bfd_byte
*) minisyms
;
1128 fromend
= from
+ symcount
* size
;
1129 for (; from
< fromend
; from
+= size
)
1133 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, from
, store
);
1135 bfd_fatal (bfd_get_filename (abfd
));
1137 print_symbol (abfd
, sym
, (bfd_vma
) 0, archive_bfd
);
1141 /* Print the symbols when sorting by size. */
1144 print_size_symbols (abfd
, dynamic
, symsizes
, symcount
, archive_bfd
)
1147 struct size_sym
*symsizes
;
1152 struct size_sym
*from
, *fromend
;
1154 store
= bfd_make_empty_symbol (abfd
);
1156 bfd_fatal (bfd_get_filename (abfd
));
1159 fromend
= from
+ symcount
;
1160 for (; from
< fromend
; from
++)
1165 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, from
->minisym
, store
);
1167 bfd_fatal (bfd_get_filename (abfd
));
1169 /* For elf we have already computed the correct symbol size. */
1170 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1173 ssize
= from
->size
- bfd_section_vma (abfd
, bfd_get_section (sym
));
1175 print_symbol (abfd
, sym
, ssize
, archive_bfd
);
1179 /* Print a single symbol. */
1182 print_symbol (abfd
, sym
, ssize
, archive_bfd
)
1190 (*format
->print_symbol_filename
) (archive_bfd
, abfd
);
1194 if (bfd_is_und_section (bfd_get_section (sym
)))
1195 print_symname ("%s", bfd_asymbol_name (sym
), abfd
);
1199 symbol_info syminfo
;
1200 struct extended_symbol_info info
;
1202 bfd_get_symbol_info (abfd
, sym
, &syminfo
);
1203 info
.sinfo
= &syminfo
;
1205 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1206 info
.elfinfo
= (elf_symbol_type
*) sym
;
1208 info
.elfinfo
= NULL
;
1209 (*format
->print_symbol_info
) (&info
, abfd
);
1214 static asymbol
**syms
;
1215 static long symcount
;
1216 const char *filename
, *functionname
;
1217 unsigned int lineno
;
1219 /* We need to get the canonical symbols in order to call
1220 bfd_find_nearest_line. This is inefficient, but, then, you
1221 don't have to use --line-numbers. */
1222 if (abfd
!= lineno_cache_bfd
&& syms
!= NULL
)
1231 symsize
= bfd_get_symtab_upper_bound (abfd
);
1233 bfd_fatal (bfd_get_filename (abfd
));
1234 syms
= (asymbol
**) xmalloc (symsize
);
1235 symcount
= bfd_canonicalize_symtab (abfd
, syms
);
1237 bfd_fatal (bfd_get_filename (abfd
));
1238 lineno_cache_bfd
= abfd
;
1241 if (bfd_is_und_section (bfd_get_section (sym
)))
1243 static asection
**secs
;
1244 static arelent
***relocs
;
1245 static long *relcount
;
1246 static unsigned int seccount
;
1248 const char *symname
;
1250 /* For an undefined symbol, we try to find a reloc for the
1251 symbol, and print the line number of the reloc. */
1252 if (abfd
!= lineno_cache_rel_bfd
&& relocs
!= NULL
)
1254 for (i
= 0; i
< seccount
; i
++)
1255 if (relocs
[i
] != NULL
)
1267 struct get_relocs_info info
;
1269 seccount
= bfd_count_sections (abfd
);
1271 secs
= (asection
**) xmalloc (seccount
* sizeof *secs
);
1272 relocs
= (arelent
***) xmalloc (seccount
* sizeof *relocs
);
1273 relcount
= (long *) xmalloc (seccount
* sizeof *relcount
);
1276 info
.relocs
= relocs
;
1277 info
.relcount
= relcount
;
1279 bfd_map_over_sections (abfd
, get_relocs
, (PTR
) &info
);
1280 lineno_cache_rel_bfd
= abfd
;
1283 symname
= bfd_asymbol_name (sym
);
1284 for (i
= 0; i
< seccount
; i
++)
1288 for (j
= 0; j
< relcount
[i
]; j
++)
1293 if (r
->sym_ptr_ptr
!= NULL
1294 && (*r
->sym_ptr_ptr
)->section
== sym
->section
1295 && (*r
->sym_ptr_ptr
)->value
== sym
->value
1297 bfd_asymbol_name (*r
->sym_ptr_ptr
)) == 0
1298 && bfd_find_nearest_line (abfd
, secs
[i
], syms
,
1299 r
->address
, &filename
,
1300 &functionname
, &lineno
)
1301 && filename
!= NULL
)
1303 /* We only print the first one we find. */
1304 printf ("\t%s:%u", filename
, lineno
);
1311 else if (bfd_get_section (sym
)->owner
== abfd
)
1313 if (bfd_find_nearest_line (abfd
, bfd_get_section (sym
), syms
,
1314 sym
->value
, &filename
, &functionname
,
1319 printf ("\t%s:%u", filename
, lineno
);
1327 /* The following 3 groups of functions are called unconditionally,
1328 once at the start of processing each file of the appropriate type.
1329 They should check `filename_per_file' and `filename_per_symbol',
1330 as appropriate for their output format, to determine whether to
1333 /* Print the name of an object file given on the command line. */
1336 print_object_filename_bsd (filename
)
1339 if (filename_per_file
&& !filename_per_symbol
)
1340 printf ("\n%s:\n", filename
);
1344 print_object_filename_sysv (filename
)
1348 printf (_("\n\nUndefined symbols from %s:\n\n"), filename
);
1350 printf (_("\n\nSymbols from %s:\n\n"), filename
);
1351 if (print_width
== 8)
1353 Name Value Class Type Size Line Section\n\n"));
1356 Name Value Class Type Size Line Section\n\n"));
1360 print_object_filename_posix (filename
)
1363 if (filename_per_file
&& !filename_per_symbol
)
1364 printf ("%s:\n", filename
);
1367 /* Print the name of an archive file given on the command line. */
1370 print_archive_filename_bsd (filename
)
1373 if (filename_per_file
)
1374 printf ("\n%s:\n", filename
);
1378 print_archive_filename_sysv (filename
)
1379 char *filename ATTRIBUTE_UNUSED
;
1384 print_archive_filename_posix (filename
)
1385 char *filename ATTRIBUTE_UNUSED
;
1389 /* Print the name of an archive member file. */
1392 print_archive_member_bsd (archive
, filename
)
1393 char *archive ATTRIBUTE_UNUSED
;
1394 const char *filename
;
1396 if (!filename_per_symbol
)
1397 printf ("\n%s:\n", filename
);
1401 print_archive_member_sysv (archive
, filename
)
1403 const char *filename
;
1406 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive
, filename
);
1408 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive
, filename
);
1409 if (print_width
== 8)
1411 Name Value Class Type Size Line Section\n\n"));
1414 Name Value Class Type Size Line Section\n\n"));
1418 print_archive_member_posix (archive
, filename
)
1420 const char *filename
;
1422 if (!filename_per_symbol
)
1423 printf ("%s[%s]:\n", archive
, filename
);
1426 /* Print the name of the file (and archive, if there is one)
1427 containing a symbol. */
1430 print_symbol_filename_bsd (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_sysv (archive_bfd
, abfd
)
1443 bfd
*archive_bfd
, *abfd
;
1445 if (filename_per_symbol
)
1448 printf ("%s:", bfd_get_filename (archive_bfd
));
1449 printf ("%s:", bfd_get_filename (abfd
));
1454 print_symbol_filename_posix (archive_bfd
, abfd
)
1455 bfd
*archive_bfd
, *abfd
;
1457 if (filename_per_symbol
)
1460 printf ("%s[%s]: ", bfd_get_filename (archive_bfd
),
1461 bfd_get_filename (abfd
));
1463 printf ("%s: ", bfd_get_filename (abfd
));
1467 /* Print a symbol value. */
1470 print_value (abfd
, val
)
1471 bfd
*abfd ATTRIBUTE_UNUSED
;
1474 #if ! defined (BFD64) || BFD_HOST_64BIT_LONG
1475 printf (value_format
, val
);
1477 /* We have a 64 bit value to print, but the host is only 32 bit. */
1478 if (print_radix
== 16)
1479 bfd_fprintf_vma (abfd
, stdout
, val
);
1485 s
= buf
+ sizeof buf
;
1489 *--s
= (val
% print_radix
) + '0';
1492 while ((buf
+ sizeof buf
- 1) - s
< 16)
1499 /* Print a line of information about a symbol. */
1502 print_symbol_info_bsd (info
, abfd
)
1503 struct extended_symbol_info
*info
;
1506 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1508 if (print_width
== 16)
1514 print_value (abfd
, SYM_VALUE (info
));
1516 if (print_size
&& SYM_SIZE (info
))
1519 print_value (abfd
, SYM_SIZE (info
));
1523 printf (" %c", SYM_TYPE (info
));
1525 if (SYM_TYPE (info
) == '-')
1529 printf (other_format
, SYM_STAB_OTHER (info
));
1531 printf (desc_format
, SYM_STAB_DESC (info
));
1532 printf (" %5s", SYM_STAB_NAME (info
));
1534 print_symname (" %s", SYM_NAME (info
), abfd
);
1538 print_symbol_info_sysv (info
, abfd
)
1539 struct extended_symbol_info
*info
;
1542 print_symname ("%-20s|", SYM_NAME (info
), abfd
);
1544 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1546 if (print_width
== 8)
1552 print_value (abfd
, SYM_VALUE (info
));
1554 printf ("| %c |", SYM_TYPE (info
));
1556 if (SYM_TYPE (info
) == '-')
1559 printf ("%18s| ", SYM_STAB_NAME (info
)); /* (C) Type */
1560 printf (desc_format
, SYM_STAB_DESC (info
)); /* Size */
1561 printf ("| |"); /* Line, Section */
1565 /* Type, Size, Line, Section */
1568 get_symbol_type (ELF_ST_TYPE (info
->elfinfo
->internal_elf_sym
.st_info
)));
1572 if (SYM_SIZE (info
))
1573 print_value (abfd
, SYM_SIZE (info
));
1576 if (print_width
== 8)
1583 printf("| |%s", info
->elfinfo
->symbol
.section
->name
);
1590 print_symbol_info_posix (info
, abfd
)
1591 struct extended_symbol_info
*info
;
1594 print_symname ("%s ", SYM_NAME (info
), abfd
);
1595 printf ("%c ", SYM_TYPE (info
));
1597 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1601 print_value (abfd
, SYM_VALUE (info
));
1603 if (SYM_SIZE (info
))
1604 print_value (abfd
, SYM_SIZE (info
));
1609 print_symdef_entry (abfd
)
1612 symindex idx
= BFD_NO_MORE_SYMBOLS
;
1614 boolean everprinted
= false;
1616 for (idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
);
1617 idx
!= BFD_NO_MORE_SYMBOLS
;
1618 idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
))
1623 printf (_("\nArchive index:\n"));
1626 elt
= bfd_get_elt_at_index (abfd
, idx
);
1628 bfd_fatal ("bfd_get_elt_at_index");
1629 if (thesym
->name
!= (char *) NULL
)
1631 print_symname ("%s", thesym
->name
, abfd
);
1632 printf (" in %s\n", bfd_get_filename (elt
));
1637 /* This function is used to get the relocs for a particular section.
1638 It is called via bfd_map_over_sections. */
1641 get_relocs (abfd
, sec
, dataarg
)
1646 struct get_relocs_info
*data
= (struct get_relocs_info
*) dataarg
;
1650 if ((sec
->flags
& SEC_RELOC
) == 0)
1652 *data
->relocs
= NULL
;
1653 *data
->relcount
= 0;
1659 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1661 bfd_fatal (bfd_get_filename (abfd
));
1663 *data
->relocs
= (arelent
**) xmalloc (relsize
);
1664 *data
->relcount
= bfd_canonicalize_reloc (abfd
, sec
, *data
->relocs
,
1666 if (*data
->relcount
< 0)
1667 bfd_fatal (bfd_get_filename (abfd
));