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
, "");
359 bindtextdomain (PACKAGE
, LOCALEDIR
);
360 textdomain (PACKAGE
);
362 program_name
= *argv
;
363 xmalloc_set_program_name (program_name
);
365 START_PROGRESS (program_name
, 0);
368 set_default_bfd_target ();
370 while ((c
= getopt_long (argc
, argv
, "aABCDef:gHhlnopPrSst:uvVvX:",
371 long_options
, (int *) 0)) != EOF
)
376 print_debug_syms
= 1;
380 filename_per_symbol
= 1;
382 case 'B': /* For MIPS compatibility. */
383 set_output_format ("bsd");
389 enum demangling_styles style
;
391 style
= cplus_demangle_name_to_style (optarg
);
392 if (style
== unknown_demangling
)
393 fatal (_("unknown demangling style `%s'"),
396 cplus_demangle_set_style (style
);
403 /* Ignored for HP/UX compatibility. */
406 set_output_format (optarg
);
419 sort_numerically
= 1;
425 set_output_format ("posix");
437 set_print_radix (optarg
);
446 /* Ignored for (partial) AIX compatibility. On AIX, the
447 argument has values 32, 64, or 32_64, and specfies that
448 only 32-bit, only 64-bit, or both kinds of objects should
449 be examined. The default is 32. So plain AIX nm on a
450 library archive with both kinds of objects will ignore
451 the 64-bit ones. For GNU nm, the default is and always
452 has been -X 32_64, and other options are not supported. */
453 if (strcmp (optarg
, "32_64") != 0)
454 fatal (_("Only -X 32_64 is supported"));
457 case OPTION_TARGET
: /* --target */
461 case 0: /* A long option that just sets a flag. */
470 print_version ("nm");
472 /* OK, all options now parsed. If no filename specified, do a.out. */
474 return !display_file ("a.out");
478 if (argc
- optind
> 1)
479 filename_per_file
= 1;
481 /* We were given several filenames to do. */
482 while (optind
< argc
)
485 if (!display_file (argv
[optind
++]))
489 END_PROGRESS (program_name
);
494 char *lim
= (char *) sbrk (0);
496 non_fatal (_("data size %ld"), (long) (lim
- (char *) &environ
));
505 get_symbol_type (type
)
508 static char buff
[32];
512 case STT_NOTYPE
: return "NOTYPE";
513 case STT_OBJECT
: return "OBJECT";
514 case STT_FUNC
: return "FUNC";
515 case STT_SECTION
: return "SECTION";
516 case STT_FILE
: return "FILE";
517 case STT_COMMON
: return "COMMON";
518 case STT_TLS
: return "TLS";
520 if (type
>= STT_LOPROC
&& type
<= STT_HIPROC
)
521 sprintf (buff
, _("<processor specific>: %d"), type
);
522 else if (type
>= STT_LOOS
&& type
<= STT_HIOS
)
523 sprintf (buff
, _("<OS specific>: %d"), type
);
525 sprintf (buff
, _("<unknown>: %d"), type
);
531 display_archive (file
)
535 bfd
*last_arfile
= NULL
;
538 (*format
->print_archive_filename
) (bfd_get_filename (file
));
541 print_symdef_entry (file
);
547 arfile
= bfd_openr_next_archived_file (file
, arfile
);
551 if (bfd_get_error () != bfd_error_no_more_archived_files
)
552 bfd_fatal (bfd_get_filename (file
));
556 if (bfd_check_format_matches (arfile
, bfd_object
, &matching
))
560 bfd_sprintf_vma (arfile
, buf
, (bfd_vma
) -1);
561 print_width
= strlen (buf
);
562 (*format
->print_archive_member
) (bfd_get_filename (file
),
563 bfd_get_filename (arfile
));
564 display_rel_file (arfile
, file
);
568 bfd_nonfatal (bfd_get_filename (arfile
));
569 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
571 list_matching_formats (matching
);
576 if (last_arfile
!= NULL
)
578 bfd_close (last_arfile
);
579 lineno_cache_bfd
= NULL
;
580 lineno_cache_rel_bfd
= NULL
;
582 last_arfile
= arfile
;
585 if (last_arfile
!= NULL
)
587 bfd_close (last_arfile
);
588 lineno_cache_bfd
= NULL
;
589 lineno_cache_rel_bfd
= NULL
;
594 display_file (filename
)
597 boolean retval
= true;
601 file
= bfd_openr (filename
, target
);
604 bfd_nonfatal (filename
);
608 if (bfd_check_format (file
, bfd_archive
))
610 display_archive (file
);
612 else if (bfd_check_format_matches (file
, bfd_object
, &matching
))
616 bfd_sprintf_vma (file
, buf
, (bfd_vma
) -1);
617 print_width
= strlen (buf
);
618 (*format
->print_object_filename
) (filename
);
619 display_rel_file (file
, NULL
);
623 bfd_nonfatal (filename
);
624 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
626 list_matching_formats (matching
);
632 if (bfd_close (file
) == false)
633 bfd_fatal (filename
);
635 lineno_cache_bfd
= NULL
;
636 lineno_cache_rel_bfd
= NULL
;
641 /* These globals are used to pass information into the sorting
643 static bfd
*sort_bfd
;
644 static boolean sort_dynamic
;
645 static asymbol
*sort_x
;
646 static asymbol
*sort_y
;
648 /* Symbol-sorting predicates */
649 #define valueof(x) ((x)->section->vma + (x)->value)
651 /* Numeric sorts. Undefined symbols are always considered "less than"
652 defined symbols with zero values. Common symbols are not treated
653 specially -- i.e., their sizes are used as their "values". */
656 numeric_forward (P_x
, P_y
)
663 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
664 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
665 if (x
== NULL
|| y
== NULL
)
666 bfd_fatal (bfd_get_filename (sort_bfd
));
668 xs
= bfd_get_section (x
);
669 ys
= bfd_get_section (y
);
671 if (bfd_is_und_section (xs
))
673 if (! bfd_is_und_section (ys
))
676 else if (bfd_is_und_section (ys
))
678 else if (valueof (x
) != valueof (y
))
679 return valueof (x
) < valueof (y
) ? -1 : 1;
681 return non_numeric_forward (P_x
, P_y
);
685 numeric_reverse (x
, y
)
689 return - numeric_forward (x
, y
);
693 non_numeric_forward (P_x
, P_y
)
700 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
701 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
702 if (x
== NULL
|| y
== NULL
)
703 bfd_fatal (bfd_get_filename (sort_bfd
));
705 xn
= bfd_asymbol_name (x
);
706 yn
= bfd_asymbol_name (y
);
708 return ((xn
== NULL
) ? ((yn
== NULL
) ? 0 : -1) :
709 ((yn
== NULL
) ? 1 : strcmp (xn
, yn
)));
713 non_numeric_reverse (x
, y
)
717 return - non_numeric_forward (x
, y
);
720 static int (*(sorters
[2][2])) PARAMS ((const PTR
, const PTR
)) =
722 { non_numeric_forward
, non_numeric_reverse
},
723 { numeric_forward
, numeric_reverse
}
726 /* This sort routine is used by sort_symbols_by_size. It is similar
727 to numeric_forward, but when symbols have the same value it sorts
728 by section VMA. This simplifies the sort_symbols_by_size code
729 which handles symbols at the end of sections. Also, this routine
730 tries to sort file names before other symbols with the same value.
731 That will make the file name have a zero size, which will make
732 sort_symbols_by_size choose the non file name symbol, leading to
733 more meaningful output. For similar reasons, this code sorts
734 gnu_compiled_* and gcc2_compiled before other symbols with the same
738 size_forward1 (P_x
, P_y
)
748 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
749 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
750 if (x
== NULL
|| y
== NULL
)
751 bfd_fatal (bfd_get_filename (sort_bfd
));
753 xs
= bfd_get_section (x
);
754 ys
= bfd_get_section (y
);
756 if (bfd_is_und_section (xs
))
758 if (bfd_is_und_section (ys
))
761 if (valueof (x
) != valueof (y
))
762 return valueof (x
) < valueof (y
) ? -1 : 1;
764 if (xs
->vma
!= ys
->vma
)
765 return xs
->vma
< ys
->vma
? -1 : 1;
767 xn
= bfd_asymbol_name (x
);
768 yn
= bfd_asymbol_name (y
);
772 /* The symbols gnu_compiled and gcc2_compiled convey even less
773 information than the file name, so sort them out first. */
775 xf
= (strstr (xn
, "gnu_compiled") != NULL
776 || strstr (xn
, "gcc2_compiled") != NULL
);
777 yf
= (strstr (yn
, "gnu_compiled") != NULL
778 || strstr (yn
, "gcc2_compiled") != NULL
);
785 /* We use a heuristic for the file name. It may not work on non
786 Unix systems, but it doesn't really matter; the only difference
787 is precisely which symbol names get printed. */
789 #define file_symbol(s, sn, snl) \
790 (((s)->flags & BSF_FILE) != 0 \
791 || ((sn)[(snl) - 2] == '.' \
792 && ((sn)[(snl) - 1] == 'o' \
793 || (sn)[(snl) - 1] == 'a')))
795 xf
= file_symbol (x
, xn
, xnl
);
796 yf
= file_symbol (y
, yn
, ynl
);
803 return non_numeric_forward (P_x
, P_y
);
806 /* This sort routine is used by sort_symbols_by_size. It is sorting
807 an array of size_sym structures into size order. */
810 size_forward2 (P_x
, P_y
)
814 const struct size_sym
*x
= (const struct size_sym
*) P_x
;
815 const struct size_sym
*y
= (const struct size_sym
*) P_y
;
817 if (x
->size
< y
->size
)
818 return reverse_sort
? 1 : -1;
819 else if (x
->size
> y
->size
)
820 return reverse_sort
? -1 : 1;
822 return sorters
[0][reverse_sort
] (x
->minisym
, y
->minisym
);
825 /* Sort the symbols by size. ELF provides a size but for other formats
826 we have to make a guess by assuming that the difference between the
827 address of a symbol and the address of the next higher symbol is the
831 sort_symbols_by_size (abfd
, dynamic
, minisyms
, symcount
, size
, symsizesp
)
837 struct size_sym
**symsizesp
;
839 struct size_sym
*symsizes
;
840 bfd_byte
*from
, *fromend
;
842 asymbol
*store_sym
, *store_next
;
844 qsort (minisyms
, symcount
, size
, size_forward1
);
846 /* We are going to return a special set of symbols and sizes to
848 symsizes
= (struct size_sym
*) xmalloc (symcount
* sizeof (struct size_sym
));
849 *symsizesp
= symsizes
;
851 /* Note that filter_symbols has already removed all absolute and
852 undefined symbols. Here we remove all symbols whose size winds
854 from
= (bfd_byte
*) minisyms
;
855 fromend
= from
+ symcount
* size
;
862 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, (const PTR
) from
,
865 bfd_fatal (bfd_get_filename (abfd
));
868 for (; from
< fromend
; from
+= size
)
875 if (from
+ size
< fromend
)
877 next
= bfd_minisymbol_to_symbol (abfd
,
879 (const PTR
) (from
+ size
),
882 bfd_fatal (bfd_get_filename (abfd
));
887 sec
= bfd_get_section (sym
);
889 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
890 sz
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
891 else if (bfd_is_com_section (sec
))
895 if (from
+ size
< fromend
896 && sec
== bfd_get_section (next
))
897 sz
= valueof (next
) - valueof (sym
);
899 sz
= (bfd_get_section_vma (abfd
, sec
)
900 + bfd_section_size (abfd
, sec
)
906 symsizes
->minisym
= (const PTR
) from
;
914 store_sym
= store_next
;
918 symcount
= symsizes
- *symsizesp
;
920 /* We must now sort again by size. */
921 qsort ((PTR
) *symsizesp
, symcount
, sizeof (struct size_sym
), size_forward2
);
926 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
929 display_rel_file (abfd
, archive_bfd
)
936 struct size_sym
*symsizes
;
940 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
942 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
947 symcount
= bfd_read_minisymbols (abfd
, dynamic
, &minisyms
, &size
);
949 bfd_fatal (bfd_get_filename (abfd
));
953 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
957 /* Discard the symbols we don't want to print.
958 It's OK to do this in place; we'll free the storage anyway
961 symcount
= filter_symbols (abfd
, dynamic
, minisyms
, symcount
, size
);
967 sort_dynamic
= dynamic
;
968 sort_x
= bfd_make_empty_symbol (abfd
);
969 sort_y
= bfd_make_empty_symbol (abfd
);
970 if (sort_x
== NULL
|| sort_y
== NULL
)
971 bfd_fatal (bfd_get_filename (abfd
));
974 qsort (minisyms
, symcount
, size
,
975 sorters
[sort_numerically
][reverse_sort
]);
977 symcount
= sort_symbols_by_size (abfd
, dynamic
, minisyms
, symcount
,
982 print_symbols (abfd
, dynamic
, minisyms
, symcount
, size
, archive_bfd
);
984 print_size_symbols (abfd
, dynamic
, symsizes
, symcount
, archive_bfd
);
989 /* Choose which symbol entries to print;
990 compact them downward to get rid of the rest.
991 Return the number of symbols to be printed. */
994 filter_symbols (abfd
, dynamic
, minisyms
, symcount
, size
)
1001 bfd_byte
*from
, *fromend
, *to
;
1004 store
= bfd_make_empty_symbol (abfd
);
1006 bfd_fatal (bfd_get_filename (abfd
));
1008 from
= (bfd_byte
*) minisyms
;
1009 fromend
= from
+ symcount
* size
;
1010 to
= (bfd_byte
*) minisyms
;
1012 for (; from
< fromend
; from
+= size
)
1019 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, (const PTR
) from
, store
);
1021 bfd_fatal (bfd_get_filename (abfd
));
1024 keep
= bfd_is_und_section (sym
->section
);
1025 else if (external_only
)
1026 keep
= ((sym
->flags
& BSF_GLOBAL
) != 0
1027 || (sym
->flags
& BSF_WEAK
) != 0
1028 || bfd_is_und_section (sym
->section
)
1029 || bfd_is_com_section (sym
->section
));
1034 && ! print_debug_syms
1035 && (sym
->flags
& BSF_DEBUGGING
) != 0)
1040 && (bfd_is_abs_section (sym
->section
)
1041 || bfd_is_und_section (sym
->section
)))
1047 if (bfd_is_und_section (sym
->section
))
1053 memcpy (to
, from
, size
);
1058 return (to
- (bfd_byte
*) minisyms
) / size
;
1061 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
1062 demangling it if requested. */
1065 print_symname (format
, name
, abfd
)
1070 if (do_demangle
&& *name
)
1074 /* In this mode, give a user-level view of the symbol name
1075 even if it's not mangled; strip off any leading
1077 if (bfd_get_symbol_leading_char (abfd
) == name
[0])
1080 res
= cplus_demangle (name
, DMGL_ANSI
| DMGL_PARAMS
);
1083 printf (format
, res
);
1089 printf (format
, name
);
1092 /* Print the symbols. If ARCHIVE_BFD is non-NULL, it is the archive
1096 print_symbols (abfd
, dynamic
, minisyms
, symcount
, size
, archive_bfd
)
1105 bfd_byte
*from
, *fromend
;
1107 store
= bfd_make_empty_symbol (abfd
);
1109 bfd_fatal (bfd_get_filename (abfd
));
1111 from
= (bfd_byte
*) minisyms
;
1112 fromend
= from
+ symcount
* size
;
1113 for (; from
< fromend
; from
+= size
)
1117 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, from
, store
);
1119 bfd_fatal (bfd_get_filename (abfd
));
1121 print_symbol (abfd
, sym
, (bfd_vma
) 0, archive_bfd
);
1125 /* Print the symbols when sorting by size. */
1128 print_size_symbols (abfd
, dynamic
, symsizes
, symcount
, archive_bfd
)
1131 struct size_sym
*symsizes
;
1136 struct size_sym
*from
, *fromend
;
1138 store
= bfd_make_empty_symbol (abfd
);
1140 bfd_fatal (bfd_get_filename (abfd
));
1143 fromend
= from
+ symcount
;
1144 for (; from
< fromend
; from
++)
1149 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, from
->minisym
, store
);
1151 bfd_fatal (bfd_get_filename (abfd
));
1153 /* For elf we have already computed the correct symbol size. */
1154 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1157 ssize
= from
->size
- bfd_section_vma (abfd
, bfd_get_section (sym
));
1159 print_symbol (abfd
, sym
, ssize
, archive_bfd
);
1163 /* Print a single symbol. */
1166 print_symbol (abfd
, sym
, ssize
, archive_bfd
)
1174 (*format
->print_symbol_filename
) (archive_bfd
, abfd
);
1178 if (bfd_is_und_section (bfd_get_section (sym
)))
1179 print_symname ("%s", bfd_asymbol_name (sym
), abfd
);
1183 symbol_info syminfo
;
1184 struct extended_symbol_info info
;
1186 bfd_get_symbol_info (abfd
, sym
, &syminfo
);
1187 info
.sinfo
= &syminfo
;
1189 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1190 info
.elfinfo
= (elf_symbol_type
*) sym
;
1192 info
.elfinfo
= NULL
;
1193 (*format
->print_symbol_info
) (&info
, abfd
);
1198 static asymbol
**syms
;
1199 static long symcount
;
1200 const char *filename
, *functionname
;
1201 unsigned int lineno
;
1203 /* We need to get the canonical symbols in order to call
1204 bfd_find_nearest_line. This is inefficient, but, then, you
1205 don't have to use --line-numbers. */
1206 if (abfd
!= lineno_cache_bfd
&& syms
!= NULL
)
1215 symsize
= bfd_get_symtab_upper_bound (abfd
);
1217 bfd_fatal (bfd_get_filename (abfd
));
1218 syms
= (asymbol
**) xmalloc (symsize
);
1219 symcount
= bfd_canonicalize_symtab (abfd
, syms
);
1221 bfd_fatal (bfd_get_filename (abfd
));
1222 lineno_cache_bfd
= abfd
;
1225 if (bfd_is_und_section (bfd_get_section (sym
)))
1227 static asection
**secs
;
1228 static arelent
***relocs
;
1229 static long *relcount
;
1230 static unsigned int seccount
;
1232 const char *symname
;
1234 /* For an undefined symbol, we try to find a reloc for the
1235 symbol, and print the line number of the reloc. */
1236 if (abfd
!= lineno_cache_rel_bfd
&& relocs
!= NULL
)
1238 for (i
= 0; i
< seccount
; i
++)
1239 if (relocs
[i
] != NULL
)
1251 struct get_relocs_info info
;
1253 seccount
= bfd_count_sections (abfd
);
1255 secs
= (asection
**) xmalloc (seccount
* sizeof *secs
);
1256 relocs
= (arelent
***) xmalloc (seccount
* sizeof *relocs
);
1257 relcount
= (long *) xmalloc (seccount
* sizeof *relcount
);
1260 info
.relocs
= relocs
;
1261 info
.relcount
= relcount
;
1263 bfd_map_over_sections (abfd
, get_relocs
, (PTR
) &info
);
1264 lineno_cache_rel_bfd
= abfd
;
1267 symname
= bfd_asymbol_name (sym
);
1268 for (i
= 0; i
< seccount
; i
++)
1272 for (j
= 0; j
< relcount
[i
]; j
++)
1277 if (r
->sym_ptr_ptr
!= NULL
1278 && (*r
->sym_ptr_ptr
)->section
== sym
->section
1279 && (*r
->sym_ptr_ptr
)->value
== sym
->value
1281 bfd_asymbol_name (*r
->sym_ptr_ptr
)) == 0
1282 && bfd_find_nearest_line (abfd
, secs
[i
], syms
,
1283 r
->address
, &filename
,
1284 &functionname
, &lineno
)
1285 && filename
!= NULL
)
1287 /* We only print the first one we find. */
1288 printf ("\t%s:%u", filename
, lineno
);
1295 else if (bfd_get_section (sym
)->owner
== abfd
)
1297 if (bfd_find_nearest_line (abfd
, bfd_get_section (sym
), syms
,
1298 sym
->value
, &filename
, &functionname
,
1303 printf ("\t%s:%u", filename
, lineno
);
1311 /* The following 3 groups of functions are called unconditionally,
1312 once at the start of processing each file of the appropriate type.
1313 They should check `filename_per_file' and `filename_per_symbol',
1314 as appropriate for their output format, to determine whether to
1317 /* Print the name of an object file given on the command line. */
1320 print_object_filename_bsd (filename
)
1323 if (filename_per_file
&& !filename_per_symbol
)
1324 printf ("\n%s:\n", filename
);
1328 print_object_filename_sysv (filename
)
1332 printf (_("\n\nUndefined symbols from %s:\n\n"), filename
);
1334 printf (_("\n\nSymbols from %s:\n\n"), filename
);
1335 if (print_width
== 8)
1337 Name Value Class Type Size Line Section\n\n"));
1340 Name Value Class Type Size Line Section\n\n"));
1344 print_object_filename_posix (filename
)
1347 if (filename_per_file
&& !filename_per_symbol
)
1348 printf ("%s:\n", filename
);
1351 /* Print the name of an archive file given on the command line. */
1354 print_archive_filename_bsd (filename
)
1357 if (filename_per_file
)
1358 printf ("\n%s:\n", filename
);
1362 print_archive_filename_sysv (filename
)
1363 char *filename ATTRIBUTE_UNUSED
;
1368 print_archive_filename_posix (filename
)
1369 char *filename ATTRIBUTE_UNUSED
;
1373 /* Print the name of an archive member file. */
1376 print_archive_member_bsd (archive
, filename
)
1377 char *archive ATTRIBUTE_UNUSED
;
1378 const char *filename
;
1380 if (!filename_per_symbol
)
1381 printf ("\n%s:\n", filename
);
1385 print_archive_member_sysv (archive
, filename
)
1387 const char *filename
;
1390 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive
, filename
);
1392 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive
, filename
);
1393 if (print_width
== 8)
1395 Name Value Class Type Size Line Section\n\n"));
1398 Name Value Class Type Size Line Section\n\n"));
1402 print_archive_member_posix (archive
, filename
)
1404 const char *filename
;
1406 if (!filename_per_symbol
)
1407 printf ("%s[%s]:\n", archive
, filename
);
1410 /* Print the name of the file (and archive, if there is one)
1411 containing a symbol. */
1414 print_symbol_filename_bsd (archive_bfd
, abfd
)
1415 bfd
*archive_bfd
, *abfd
;
1417 if (filename_per_symbol
)
1420 printf ("%s:", bfd_get_filename (archive_bfd
));
1421 printf ("%s:", bfd_get_filename (abfd
));
1426 print_symbol_filename_sysv (archive_bfd
, abfd
)
1427 bfd
*archive_bfd
, *abfd
;
1429 if (filename_per_symbol
)
1432 printf ("%s:", bfd_get_filename (archive_bfd
));
1433 printf ("%s:", bfd_get_filename (abfd
));
1438 print_symbol_filename_posix (archive_bfd
, abfd
)
1439 bfd
*archive_bfd
, *abfd
;
1441 if (filename_per_symbol
)
1444 printf ("%s[%s]: ", bfd_get_filename (archive_bfd
),
1445 bfd_get_filename (abfd
));
1447 printf ("%s: ", bfd_get_filename (abfd
));
1451 /* Print a symbol value. */
1454 print_value (abfd
, val
)
1455 bfd
*abfd ATTRIBUTE_UNUSED
;
1458 #if ! defined (BFD64) || BFD_HOST_64BIT_LONG
1459 printf (value_format
, val
);
1461 /* We have a 64 bit value to print, but the host is only 32 bit. */
1462 if (print_radix
== 16)
1463 bfd_fprintf_vma (abfd
, stdout
, val
);
1469 s
= buf
+ sizeof buf
;
1473 *--s
= (val
% print_radix
) + '0';
1476 while ((buf
+ sizeof buf
- 1) - s
< 16)
1483 /* Print a line of information about a symbol. */
1486 print_symbol_info_bsd (info
, abfd
)
1487 struct extended_symbol_info
*info
;
1490 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1492 if (print_width
== 16)
1498 print_value (abfd
, SYM_VALUE (info
));
1500 if (print_size
&& SYM_SIZE (info
))
1503 print_value (abfd
, SYM_SIZE (info
));
1507 printf (" %c", SYM_TYPE (info
));
1509 if (SYM_TYPE (info
) == '-')
1513 printf (other_format
, SYM_STAB_OTHER (info
));
1515 printf (desc_format
, SYM_STAB_DESC (info
));
1516 printf (" %5s", SYM_STAB_NAME (info
));
1518 print_symname (" %s", SYM_NAME (info
), abfd
);
1522 print_symbol_info_sysv (info
, abfd
)
1523 struct extended_symbol_info
*info
;
1526 print_symname ("%-20s|", SYM_NAME (info
), abfd
);
1528 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1530 if (print_width
== 8)
1536 print_value (abfd
, SYM_VALUE (info
));
1538 printf ("| %c |", SYM_TYPE (info
));
1540 if (SYM_TYPE (info
) == '-')
1543 printf ("%18s| ", SYM_STAB_NAME (info
)); /* (C) Type */
1544 printf (desc_format
, SYM_STAB_DESC (info
)); /* Size */
1545 printf ("| |"); /* Line, Section */
1549 /* Type, Size, Line, Section */
1552 get_symbol_type (ELF_ST_TYPE (info
->elfinfo
->internal_elf_sym
.st_info
)));
1556 if (SYM_SIZE (info
))
1557 print_value (abfd
, SYM_SIZE (info
));
1560 if (print_width
== 8)
1567 printf("| |%s", info
->elfinfo
->symbol
.section
->name
);
1574 print_symbol_info_posix (info
, abfd
)
1575 struct extended_symbol_info
*info
;
1578 print_symname ("%s ", SYM_NAME (info
), abfd
);
1579 printf ("%c ", SYM_TYPE (info
));
1581 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1585 print_value (abfd
, SYM_VALUE (info
));
1587 if (SYM_SIZE (info
))
1588 print_value (abfd
, SYM_SIZE (info
));
1593 print_symdef_entry (abfd
)
1596 symindex idx
= BFD_NO_MORE_SYMBOLS
;
1598 boolean everprinted
= false;
1600 for (idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
);
1601 idx
!= BFD_NO_MORE_SYMBOLS
;
1602 idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
))
1607 printf (_("\nArchive index:\n"));
1610 elt
= bfd_get_elt_at_index (abfd
, idx
);
1612 bfd_fatal ("bfd_get_elt_at_index");
1613 if (thesym
->name
!= (char *) NULL
)
1615 print_symname ("%s", thesym
->name
, abfd
);
1616 printf (" in %s\n", bfd_get_filename (elt
));
1621 /* This function is used to get the relocs for a particular section.
1622 It is called via bfd_map_over_sections. */
1625 get_relocs (abfd
, sec
, dataarg
)
1630 struct get_relocs_info
*data
= (struct get_relocs_info
*) dataarg
;
1634 if ((sec
->flags
& SEC_RELOC
) == 0)
1636 *data
->relocs
= NULL
;
1637 *data
->relcount
= 0;
1643 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1645 bfd_fatal (bfd_get_filename (abfd
));
1647 *data
->relocs
= (arelent
**) xmalloc (relsize
);
1648 *data
->relcount
= bfd_canonicalize_reloc (abfd
, sec
, *data
->relocs
,
1650 if (*data
->relcount
< 0)
1651 bfd_fatal (bfd_get_filename (abfd
));