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
28 #include "aout/stab_gnu.h"
29 #include "aout/ranlib.h"
31 #include "libiberty.h"
33 #include "elf/common.h"
35 /* When sorting by size, we use this structure to hold the size and a
36 pointer to the minisymbol. */
44 /* When fetching relocs, we use this structure to pass information to
47 struct get_relocs_info
55 struct extended_symbol_info
59 elf_symbol_type
*elfinfo
;
60 /* FIXME: We should add more fields for Type, Line, Section. */
62 #define SYM_NAME(sym) (sym->sinfo->name)
63 #define SYM_VALUE(sym) (sym->sinfo->value)
64 #define SYM_TYPE(sym) (sym->sinfo->type)
65 #define SYM_STAB_NAME(sym) (sym->sinfo->stab_name)
66 #define SYM_STAB_DESC(sym) (sym->sinfo->stab_desc)
67 #define SYM_STAB_OTHER(sym) (sym->sinfo->stab_other)
68 #define SYM_SIZE(sym) \
69 (sym->elfinfo ? sym->elfinfo->internal_elf_sym.st_size: sym->ssize)
71 static void usage
PARAMS ((FILE *, int));
72 static void set_print_radix
PARAMS ((char *));
73 static void set_output_format
PARAMS ((char *));
74 static void display_archive
PARAMS ((bfd
*));
75 static boolean display_file
PARAMS ((char *));
76 static void display_rel_file
PARAMS ((bfd
*, bfd
*));
77 static long filter_symbols
PARAMS ((bfd
*, boolean
, PTR
, long, unsigned int));
78 static long sort_symbols_by_size
PARAMS ((bfd
*, boolean
, PTR
, long, unsigned int, struct size_sym
**));
79 static void print_symbols
PARAMS ((bfd
*, boolean
, PTR
, long, unsigned int, bfd
*));
80 static void print_size_symbols
PARAMS ((bfd
*, boolean
, struct size_sym
*, long, bfd
*));
81 static void print_symname
PARAMS ((const char *, const char *, bfd
*));
82 static void print_symbol
PARAMS ((bfd
*, asymbol
*, bfd_vma ssize
, bfd
*));
83 static void print_symdef_entry
PARAMS ((bfd
*));
85 /* The sorting functions. */
86 static int numeric_forward
PARAMS ((const PTR
, const PTR
));
87 static int numeric_reverse
PARAMS ((const PTR
, const PTR
));
88 static int non_numeric_forward
PARAMS ((const PTR
, const PTR
));
89 static int non_numeric_reverse
PARAMS ((const PTR
, const PTR
));
90 static int size_forward1
PARAMS ((const PTR
, const PTR
));
91 static int size_forward2
PARAMS ((const PTR
, const PTR
));
93 /* The output formatting functions. */
94 static void print_object_filename_bsd
PARAMS ((char *));
95 static void print_object_filename_sysv
PARAMS ((char *));
96 static void print_object_filename_posix
PARAMS ((char *));
97 static void print_archive_filename_bsd
PARAMS ((char *));
98 static void print_archive_filename_sysv
PARAMS ((char *));
99 static void print_archive_filename_posix
PARAMS ((char *));
100 static void print_archive_member_bsd
PARAMS ((char *, const char *));
101 static void print_archive_member_sysv
PARAMS ((char *, const char *));
102 static void print_archive_member_posix
PARAMS ((char *, const char *));
103 static void print_symbol_filename_bsd
PARAMS ((bfd
*, bfd
*));
104 static void print_symbol_filename_sysv
PARAMS ((bfd
*, bfd
*));
105 static void print_symbol_filename_posix
PARAMS ((bfd
*, bfd
*));
106 static void print_value
PARAMS ((bfd
*, bfd_vma
));
107 static void print_symbol_info_bsd
PARAMS ((struct extended_symbol_info
*, bfd
*));
108 static void print_symbol_info_sysv
PARAMS ((struct extended_symbol_info
*, bfd
*));
109 static void print_symbol_info_posix
PARAMS ((struct extended_symbol_info
*, bfd
*));
110 static void get_relocs
PARAMS ((bfd
*, asection
*, PTR
));
111 static const char * get_symbol_type
PARAMS ((unsigned int));
113 /* Support for different output formats. */
116 /* Print the name of an object file given on the command line. */
117 void (*print_object_filename
) PARAMS ((char *));
119 /* Print the name of an archive file given on the command line. */
120 void (*print_archive_filename
) PARAMS ((char *));
122 /* Print the name of an archive member file. */
123 void (*print_archive_member
) PARAMS ((char *, const char *));
125 /* Print the name of the file (and archive, if there is one)
126 containing a symbol. */
127 void (*print_symbol_filename
) PARAMS ((bfd
*, bfd
*));
129 /* Print a line of information about a symbol. */
130 void (*print_symbol_info
) PARAMS ((struct extended_symbol_info
*, bfd
*));
133 static struct output_fns formats
[] =
135 {print_object_filename_bsd
,
136 print_archive_filename_bsd
,
137 print_archive_member_bsd
,
138 print_symbol_filename_bsd
,
139 print_symbol_info_bsd
},
140 {print_object_filename_sysv
,
141 print_archive_filename_sysv
,
142 print_archive_member_sysv
,
143 print_symbol_filename_sysv
,
144 print_symbol_info_sysv
},
145 {print_object_filename_posix
,
146 print_archive_filename_posix
,
147 print_archive_member_posix
,
148 print_symbol_filename_posix
,
149 print_symbol_info_posix
}
152 /* Indices in `formats'. */
154 #define FORMAT_SYSV 1
155 #define FORMAT_POSIX 2
156 #define FORMAT_DEFAULT FORMAT_BSD
158 /* The output format to use. */
159 static struct output_fns
*format
= &formats
[FORMAT_DEFAULT
];
161 /* Command options. */
163 static int do_demangle
= 0; /* Pretty print C++ symbol names. */
164 static int external_only
= 0; /* Print external symbols only. */
165 static int defined_only
= 0; /* Print defined symbols only. */
166 static int no_sort
= 0; /* Don't sort; print syms in order found. */
167 static int print_debug_syms
= 0;/* Print debugger-only symbols too. */
168 static int print_armap
= 0; /* Describe __.SYMDEF data in archive files. */
169 static int print_size
= 0; /* Print size of defined symbols. */
170 static int reverse_sort
= 0; /* Sort in downward(alpha or numeric) order. */
171 static int sort_numerically
= 0;/* Sort in numeric rather than alpha order. */
172 static int sort_by_size
= 0; /* Sort by size of symbol. */
173 static int undefined_only
= 0; /* Print undefined symbols only. */
174 static int dynamic
= 0; /* Print dynamic symbols. */
175 static int show_version
= 0; /* Show the version number. */
176 static int show_stats
= 0; /* Show statistics. */
177 static int line_numbers
= 0; /* Print line numbers for symbols. */
179 /* When to print the names of files. Not mutually exclusive in SYSV format. */
180 static int filename_per_file
= 0; /* Once per file, on its own line. */
181 static int filename_per_symbol
= 0; /* Once per symbol, at start of line. */
183 /* Print formats for printing a symbol value. */
185 static char value_format
[] = "%08lx";
187 #if BFD_HOST_64BIT_LONG
188 static char value_format
[] = "%016lx";
190 /* We don't use value_format for this case. */
194 static int print_width
= 16;
196 static int print_width
= 8;
198 static int print_radix
= 16;
199 /* Print formats for printing stab info. */
200 static char other_format
[] = "%02x";
201 static char desc_format
[] = "%04x";
203 static char *target
= NULL
;
205 /* Used to cache the line numbers for a BFD. */
206 static bfd
*lineno_cache_bfd
;
207 static bfd
*lineno_cache_rel_bfd
;
209 #define OPTION_TARGET 200
211 static struct option long_options
[] =
213 {"debug-syms", no_argument
, &print_debug_syms
, 1},
214 {"demangle", optional_argument
, 0, 'C'},
215 {"dynamic", no_argument
, &dynamic
, 1},
216 {"extern-only", no_argument
, &external_only
, 1},
217 {"format", required_argument
, 0, 'f'},
218 {"help", no_argument
, 0, 'h'},
219 {"line-numbers", no_argument
, 0, 'l'},
220 {"no-cplus", no_argument
, &do_demangle
, 0}, /* Linux compatibility. */
221 {"no-demangle", no_argument
, &do_demangle
, 0},
222 {"no-sort", no_argument
, &no_sort
, 1},
223 {"numeric-sort", no_argument
, &sort_numerically
, 1},
224 {"portability", no_argument
, 0, 'P'},
225 {"print-armap", no_argument
, &print_armap
, 1},
226 {"print-file-name", no_argument
, 0, 'o'},
227 {"print-size", no_argument
, 0, 'S'},
228 {"radix", required_argument
, 0, 't'},
229 {"reverse-sort", no_argument
, &reverse_sort
, 1},
230 {"size-sort", no_argument
, &sort_by_size
, 1},
231 {"stats", no_argument
, &show_stats
, 1},
232 {"target", required_argument
, 0, OPTION_TARGET
},
233 {"defined-only", no_argument
, &defined_only
, 1},
234 {"undefined-only", no_argument
, &undefined_only
, 1},
235 {"version", no_argument
, &show_version
, 1},
236 {0, no_argument
, 0, 0}
239 /* Some error-reporting functions. */
242 usage (stream
, status
)
246 fprintf (stream
, _("Usage: %s [option(s)] [file(s)]\n"), program_name
);
247 fprintf (stream
, _(" List symbols in [file(s)] (a.out by default).\n"));
248 fprintf (stream
, _(" The options are:\n\
249 -a, --debug-syms Display debugger-only symbols\n\
250 -A, --print-file-name Print name of the input file before every symbol\n\
251 -B Same as --format=bsd\n\
252 -C, --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
253 The STYLE, if specified, can be `auto' (the default),\n\
254 `gnu', 'lucid', 'arm', 'hp', 'edg' or 'gnu-v3'\n\
255 --no-demangle Do not demangle low-level symbol names\n\
256 -D, --dynamic Display dynamic symbols instead of normal symbols\n\
257 --defined-only Display only defined symbols\n\
259 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
260 `sysv' or `posix'. The default is `bsd'\n\
261 -g, --extern-only Display only external symbols\n\
262 -l, --line-numbers Use debugging information to find a filename and\n\
263 line number for each symbol\n\
264 -n, --numeric-sort Sort symbols numerically by address\n\
266 -p, --no-sort Do not sort the symbols\n\
267 -P, --portability Same as --format=posix\n\
268 -r, --reverse-sort Reverse the sense of the sort\n\
269 -S, --print-size Print size of defined symbols\n\
270 -s, --print-armap Include index for symbols from archive members\n\
271 --size-sort Sort symbols by size\n\
272 -t, --radix=RADIX Use RADIX for printing symbol values\n\
273 --target=BFDNAME Specify the target object format as BFDNAME\n\
274 -u, --undefined-only Display only undefined symbols\n\
275 -X 32_64 (ignored)\n\
276 -h, --help Display this information\n\
277 -V, --version Display this program's version number\n\
279 list_supported_targets (program_name
, stream
);
281 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
285 /* Set the radix for the symbol value and size according to RADIX. */
288 set_print_radix (radix
)
302 value_format
[4] = *radix
;
304 #if BFD_HOST_64BIT_LONG
305 value_format
[5] = *radix
;
307 /* This case requires special handling for octal and decimal
311 other_format
[3] = desc_format
[3] = *radix
;
314 fatal (_("%s: invalid radix"), radix
);
319 set_output_format (f
)
339 fatal (_("%s: invalid output format"), f
);
341 format
= &formats
[i
];
344 int main
PARAMS ((int, char **));
354 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
355 setlocale (LC_MESSAGES
, "");
357 #if defined (HAVE_SETLOCALE)
358 setlocale (LC_CTYPE
, "");
359 setlocale (LC_COLLATE
, "");
361 bindtextdomain (PACKAGE
, LOCALEDIR
);
362 textdomain (PACKAGE
);
364 program_name
= *argv
;
365 xmalloc_set_program_name (program_name
);
367 START_PROGRESS (program_name
, 0);
370 set_default_bfd_target ();
372 while ((c
= getopt_long (argc
, argv
, "aABCDef:gHhlnopPrSst:uvVvX:",
373 long_options
, (int *) 0)) != EOF
)
378 print_debug_syms
= 1;
382 filename_per_symbol
= 1;
384 case 'B': /* For MIPS compatibility. */
385 set_output_format ("bsd");
391 enum demangling_styles style
;
393 style
= cplus_demangle_name_to_style (optarg
);
394 if (style
== unknown_demangling
)
395 fatal (_("unknown demangling style `%s'"),
398 cplus_demangle_set_style (style
);
405 /* Ignored for HP/UX compatibility. */
408 set_output_format (optarg
);
421 sort_numerically
= 1;
427 set_output_format ("posix");
439 set_print_radix (optarg
);
448 /* Ignored for (partial) AIX compatibility. On AIX, the
449 argument has values 32, 64, or 32_64, and specfies that
450 only 32-bit, only 64-bit, or both kinds of objects should
451 be examined. The default is 32. So plain AIX nm on a
452 library archive with both kinds of objects will ignore
453 the 64-bit ones. For GNU nm, the default is and always
454 has been -X 32_64, and other options are not supported. */
455 if (strcmp (optarg
, "32_64") != 0)
456 fatal (_("Only -X 32_64 is supported"));
459 case OPTION_TARGET
: /* --target */
463 case 0: /* A long option that just sets a flag. */
472 print_version ("nm");
474 /* OK, all options now parsed. If no filename specified, do a.out. */
476 return !display_file ("a.out");
480 if (argc
- optind
> 1)
481 filename_per_file
= 1;
483 /* We were given several filenames to do. */
484 while (optind
< argc
)
487 if (!display_file (argv
[optind
++]))
491 END_PROGRESS (program_name
);
496 char *lim
= (char *) sbrk (0);
498 non_fatal (_("data size %ld"), (long) (lim
- (char *) &environ
));
507 get_symbol_type (type
)
510 static char buff
[32];
514 case STT_NOTYPE
: return "NOTYPE";
515 case STT_OBJECT
: return "OBJECT";
516 case STT_FUNC
: return "FUNC";
517 case STT_SECTION
: return "SECTION";
518 case STT_FILE
: return "FILE";
519 case STT_COMMON
: return "COMMON";
520 case STT_TLS
: return "TLS";
522 if (type
>= STT_LOPROC
&& type
<= STT_HIPROC
)
523 sprintf (buff
, _("<processor specific>: %d"), type
);
524 else if (type
>= STT_LOOS
&& type
<= STT_HIOS
)
525 sprintf (buff
, _("<OS specific>: %d"), type
);
527 sprintf (buff
, _("<unknown>: %d"), type
);
533 display_archive (file
)
537 bfd
*last_arfile
= NULL
;
540 (*format
->print_archive_filename
) (bfd_get_filename (file
));
543 print_symdef_entry (file
);
549 arfile
= bfd_openr_next_archived_file (file
, arfile
);
553 if (bfd_get_error () != bfd_error_no_more_archived_files
)
554 bfd_fatal (bfd_get_filename (file
));
558 if (bfd_check_format_matches (arfile
, bfd_object
, &matching
))
562 bfd_sprintf_vma (arfile
, buf
, (bfd_vma
) -1);
563 print_width
= strlen (buf
);
564 (*format
->print_archive_member
) (bfd_get_filename (file
),
565 bfd_get_filename (arfile
));
566 display_rel_file (arfile
, file
);
570 bfd_nonfatal (bfd_get_filename (arfile
));
571 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
573 list_matching_formats (matching
);
578 if (last_arfile
!= NULL
)
580 bfd_close (last_arfile
);
581 lineno_cache_bfd
= NULL
;
582 lineno_cache_rel_bfd
= NULL
;
584 last_arfile
= arfile
;
587 if (last_arfile
!= NULL
)
589 bfd_close (last_arfile
);
590 lineno_cache_bfd
= NULL
;
591 lineno_cache_rel_bfd
= NULL
;
596 display_file (filename
)
599 boolean retval
= true;
603 file
= bfd_openr (filename
, target
);
606 bfd_nonfatal (filename
);
610 if (bfd_check_format (file
, bfd_archive
))
612 display_archive (file
);
614 else if (bfd_check_format_matches (file
, bfd_object
, &matching
))
618 bfd_sprintf_vma (file
, buf
, (bfd_vma
) -1);
619 print_width
= strlen (buf
);
620 (*format
->print_object_filename
) (filename
);
621 display_rel_file (file
, NULL
);
625 bfd_nonfatal (filename
);
626 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
628 list_matching_formats (matching
);
634 if (bfd_close (file
) == false)
635 bfd_fatal (filename
);
637 lineno_cache_bfd
= NULL
;
638 lineno_cache_rel_bfd
= NULL
;
643 /* These globals are used to pass information into the sorting
645 static bfd
*sort_bfd
;
646 static boolean sort_dynamic
;
647 static asymbol
*sort_x
;
648 static asymbol
*sort_y
;
650 /* Symbol-sorting predicates */
651 #define valueof(x) ((x)->section->vma + (x)->value)
653 /* Numeric sorts. Undefined symbols are always considered "less than"
654 defined symbols with zero values. Common symbols are not treated
655 specially -- i.e., their sizes are used as their "values". */
658 numeric_forward (P_x
, P_y
)
665 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
666 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
667 if (x
== NULL
|| y
== NULL
)
668 bfd_fatal (bfd_get_filename (sort_bfd
));
670 xs
= bfd_get_section (x
);
671 ys
= bfd_get_section (y
);
673 if (bfd_is_und_section (xs
))
675 if (! bfd_is_und_section (ys
))
678 else if (bfd_is_und_section (ys
))
680 else if (valueof (x
) != valueof (y
))
681 return valueof (x
) < valueof (y
) ? -1 : 1;
683 return non_numeric_forward (P_x
, P_y
);
687 numeric_reverse (x
, y
)
691 return - numeric_forward (x
, y
);
695 non_numeric_forward (P_x
, P_y
)
702 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
703 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
704 if (x
== NULL
|| y
== NULL
)
705 bfd_fatal (bfd_get_filename (sort_bfd
));
707 xn
= bfd_asymbol_name (x
);
708 yn
= bfd_asymbol_name (y
);
716 /* Solaris 2.5 has a bug in strcoll.
717 strcoll returns invalid values when confronted with empty strings. */
723 return strcoll (xn
, yn
);
725 return strcmp (xn
, yn
);
730 non_numeric_reverse (x
, y
)
734 return - non_numeric_forward (x
, y
);
737 static int (*(sorters
[2][2])) PARAMS ((const PTR
, const PTR
)) =
739 { non_numeric_forward
, non_numeric_reverse
},
740 { numeric_forward
, numeric_reverse
}
743 /* This sort routine is used by sort_symbols_by_size. It is similar
744 to numeric_forward, but when symbols have the same value it sorts
745 by section VMA. This simplifies the sort_symbols_by_size code
746 which handles symbols at the end of sections. Also, this routine
747 tries to sort file names before other symbols with the same value.
748 That will make the file name have a zero size, which will make
749 sort_symbols_by_size choose the non file name symbol, leading to
750 more meaningful output. For similar reasons, this code sorts
751 gnu_compiled_* and gcc2_compiled before other symbols with the same
755 size_forward1 (P_x
, P_y
)
765 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
766 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
767 if (x
== NULL
|| y
== NULL
)
768 bfd_fatal (bfd_get_filename (sort_bfd
));
770 xs
= bfd_get_section (x
);
771 ys
= bfd_get_section (y
);
773 if (bfd_is_und_section (xs
))
775 if (bfd_is_und_section (ys
))
778 if (valueof (x
) != valueof (y
))
779 return valueof (x
) < valueof (y
) ? -1 : 1;
781 if (xs
->vma
!= ys
->vma
)
782 return xs
->vma
< ys
->vma
? -1 : 1;
784 xn
= bfd_asymbol_name (x
);
785 yn
= bfd_asymbol_name (y
);
789 /* The symbols gnu_compiled and gcc2_compiled convey even less
790 information than the file name, so sort them out first. */
792 xf
= (strstr (xn
, "gnu_compiled") != NULL
793 || strstr (xn
, "gcc2_compiled") != NULL
);
794 yf
= (strstr (yn
, "gnu_compiled") != NULL
795 || strstr (yn
, "gcc2_compiled") != NULL
);
802 /* We use a heuristic for the file name. It may not work on non
803 Unix systems, but it doesn't really matter; the only difference
804 is precisely which symbol names get printed. */
806 #define file_symbol(s, sn, snl) \
807 (((s)->flags & BSF_FILE) != 0 \
808 || ((sn)[(snl) - 2] == '.' \
809 && ((sn)[(snl) - 1] == 'o' \
810 || (sn)[(snl) - 1] == 'a')))
812 xf
= file_symbol (x
, xn
, xnl
);
813 yf
= file_symbol (y
, yn
, ynl
);
820 return non_numeric_forward (P_x
, P_y
);
823 /* This sort routine is used by sort_symbols_by_size. It is sorting
824 an array of size_sym structures into size order. */
827 size_forward2 (P_x
, P_y
)
831 const struct size_sym
*x
= (const struct size_sym
*) P_x
;
832 const struct size_sym
*y
= (const struct size_sym
*) P_y
;
834 if (x
->size
< y
->size
)
835 return reverse_sort
? 1 : -1;
836 else if (x
->size
> y
->size
)
837 return reverse_sort
? -1 : 1;
839 return sorters
[0][reverse_sort
] (x
->minisym
, y
->minisym
);
842 /* Sort the symbols by size. ELF provides a size but for other formats
843 we have to make a guess by assuming that the difference between the
844 address of a symbol and the address of the next higher symbol is the
848 sort_symbols_by_size (abfd
, dynamic
, minisyms
, symcount
, size
, symsizesp
)
854 struct size_sym
**symsizesp
;
856 struct size_sym
*symsizes
;
857 bfd_byte
*from
, *fromend
;
859 asymbol
*store_sym
, *store_next
;
861 qsort (minisyms
, symcount
, size
, size_forward1
);
863 /* We are going to return a special set of symbols and sizes to
865 symsizes
= (struct size_sym
*) xmalloc (symcount
* sizeof (struct size_sym
));
866 *symsizesp
= symsizes
;
868 /* Note that filter_symbols has already removed all absolute and
869 undefined symbols. Here we remove all symbols whose size winds
871 from
= (bfd_byte
*) minisyms
;
872 fromend
= from
+ symcount
* size
;
879 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, (const PTR
) from
,
882 bfd_fatal (bfd_get_filename (abfd
));
885 for (; from
< fromend
; from
+= size
)
892 if (from
+ size
< fromend
)
894 next
= bfd_minisymbol_to_symbol (abfd
,
896 (const PTR
) (from
+ size
),
899 bfd_fatal (bfd_get_filename (abfd
));
904 sec
= bfd_get_section (sym
);
906 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
907 sz
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
908 else if (bfd_is_com_section (sec
))
912 if (from
+ size
< fromend
913 && sec
== bfd_get_section (next
))
914 sz
= valueof (next
) - valueof (sym
);
916 sz
= (bfd_get_section_vma (abfd
, sec
)
917 + bfd_section_size (abfd
, sec
)
923 symsizes
->minisym
= (const PTR
) from
;
931 store_sym
= store_next
;
935 symcount
= symsizes
- *symsizesp
;
937 /* We must now sort again by size. */
938 qsort ((PTR
) *symsizesp
, symcount
, sizeof (struct size_sym
), size_forward2
);
943 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
946 display_rel_file (abfd
, archive_bfd
)
953 struct size_sym
*symsizes
;
957 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
959 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
964 symcount
= bfd_read_minisymbols (abfd
, dynamic
, &minisyms
, &size
);
966 bfd_fatal (bfd_get_filename (abfd
));
970 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
974 /* Discard the symbols we don't want to print.
975 It's OK to do this in place; we'll free the storage anyway
978 symcount
= filter_symbols (abfd
, dynamic
, minisyms
, symcount
, size
);
984 sort_dynamic
= dynamic
;
985 sort_x
= bfd_make_empty_symbol (abfd
);
986 sort_y
= bfd_make_empty_symbol (abfd
);
987 if (sort_x
== NULL
|| sort_y
== NULL
)
988 bfd_fatal (bfd_get_filename (abfd
));
991 qsort (minisyms
, symcount
, size
,
992 sorters
[sort_numerically
][reverse_sort
]);
994 symcount
= sort_symbols_by_size (abfd
, dynamic
, minisyms
, symcount
,
999 print_symbols (abfd
, dynamic
, minisyms
, symcount
, size
, archive_bfd
);
1001 print_size_symbols (abfd
, dynamic
, symsizes
, symcount
, archive_bfd
);
1006 /* Choose which symbol entries to print;
1007 compact them downward to get rid of the rest.
1008 Return the number of symbols to be printed. */
1011 filter_symbols (abfd
, dynamic
, minisyms
, symcount
, size
)
1018 bfd_byte
*from
, *fromend
, *to
;
1021 store
= bfd_make_empty_symbol (abfd
);
1023 bfd_fatal (bfd_get_filename (abfd
));
1025 from
= (bfd_byte
*) minisyms
;
1026 fromend
= from
+ symcount
* size
;
1027 to
= (bfd_byte
*) minisyms
;
1029 for (; from
< fromend
; from
+= size
)
1036 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, (const PTR
) from
, store
);
1038 bfd_fatal (bfd_get_filename (abfd
));
1041 keep
= bfd_is_und_section (sym
->section
);
1042 else if (external_only
)
1043 keep
= ((sym
->flags
& BSF_GLOBAL
) != 0
1044 || (sym
->flags
& BSF_WEAK
) != 0
1045 || bfd_is_und_section (sym
->section
)
1046 || bfd_is_com_section (sym
->section
));
1051 && ! print_debug_syms
1052 && (sym
->flags
& BSF_DEBUGGING
) != 0)
1057 && (bfd_is_abs_section (sym
->section
)
1058 || bfd_is_und_section (sym
->section
)))
1064 if (bfd_is_und_section (sym
->section
))
1070 memcpy (to
, from
, size
);
1075 return (to
- (bfd_byte
*) minisyms
) / size
;
1078 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
1079 demangling it if requested. */
1082 print_symname (format
, name
, abfd
)
1087 if (do_demangle
&& *name
)
1089 char *res
= demangle (abfd
, name
);
1091 printf (format
, res
);
1096 printf (format
, name
);
1099 /* Print the symbols. If ARCHIVE_BFD is non-NULL, it is the archive
1103 print_symbols (abfd
, dynamic
, minisyms
, symcount
, size
, archive_bfd
)
1112 bfd_byte
*from
, *fromend
;
1114 store
= bfd_make_empty_symbol (abfd
);
1116 bfd_fatal (bfd_get_filename (abfd
));
1118 from
= (bfd_byte
*) minisyms
;
1119 fromend
= from
+ symcount
* size
;
1120 for (; from
< fromend
; from
+= size
)
1124 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, from
, store
);
1126 bfd_fatal (bfd_get_filename (abfd
));
1128 print_symbol (abfd
, sym
, (bfd_vma
) 0, archive_bfd
);
1132 /* Print the symbols when sorting by size. */
1135 print_size_symbols (abfd
, dynamic
, symsizes
, symcount
, archive_bfd
)
1138 struct size_sym
*symsizes
;
1143 struct size_sym
*from
, *fromend
;
1145 store
= bfd_make_empty_symbol (abfd
);
1147 bfd_fatal (bfd_get_filename (abfd
));
1150 fromend
= from
+ symcount
;
1151 for (; from
< fromend
; from
++)
1156 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, from
->minisym
, store
);
1158 bfd_fatal (bfd_get_filename (abfd
));
1160 /* For elf we have already computed the correct symbol size. */
1161 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1164 ssize
= from
->size
- bfd_section_vma (abfd
, bfd_get_section (sym
));
1166 print_symbol (abfd
, sym
, ssize
, archive_bfd
);
1170 /* Print a single symbol. */
1173 print_symbol (abfd
, sym
, ssize
, archive_bfd
)
1181 (*format
->print_symbol_filename
) (archive_bfd
, abfd
);
1185 if (bfd_is_und_section (bfd_get_section (sym
)))
1186 print_symname ("%s", bfd_asymbol_name (sym
), abfd
);
1190 symbol_info syminfo
;
1191 struct extended_symbol_info info
;
1193 bfd_get_symbol_info (abfd
, sym
, &syminfo
);
1194 info
.sinfo
= &syminfo
;
1196 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1197 info
.elfinfo
= (elf_symbol_type
*) sym
;
1199 info
.elfinfo
= NULL
;
1200 (*format
->print_symbol_info
) (&info
, abfd
);
1205 static asymbol
**syms
;
1206 static long symcount
;
1207 const char *filename
, *functionname
;
1208 unsigned int lineno
;
1210 /* We need to get the canonical symbols in order to call
1211 bfd_find_nearest_line. This is inefficient, but, then, you
1212 don't have to use --line-numbers. */
1213 if (abfd
!= lineno_cache_bfd
&& syms
!= NULL
)
1222 symsize
= bfd_get_symtab_upper_bound (abfd
);
1224 bfd_fatal (bfd_get_filename (abfd
));
1225 syms
= (asymbol
**) xmalloc (symsize
);
1226 symcount
= bfd_canonicalize_symtab (abfd
, syms
);
1228 bfd_fatal (bfd_get_filename (abfd
));
1229 lineno_cache_bfd
= abfd
;
1232 if (bfd_is_und_section (bfd_get_section (sym
)))
1234 static asection
**secs
;
1235 static arelent
***relocs
;
1236 static long *relcount
;
1237 static unsigned int seccount
;
1239 const char *symname
;
1241 /* For an undefined symbol, we try to find a reloc for the
1242 symbol, and print the line number of the reloc. */
1243 if (abfd
!= lineno_cache_rel_bfd
&& relocs
!= NULL
)
1245 for (i
= 0; i
< seccount
; i
++)
1246 if (relocs
[i
] != NULL
)
1258 struct get_relocs_info info
;
1260 seccount
= bfd_count_sections (abfd
);
1262 secs
= (asection
**) xmalloc (seccount
* sizeof *secs
);
1263 relocs
= (arelent
***) xmalloc (seccount
* sizeof *relocs
);
1264 relcount
= (long *) xmalloc (seccount
* sizeof *relcount
);
1267 info
.relocs
= relocs
;
1268 info
.relcount
= relcount
;
1270 bfd_map_over_sections (abfd
, get_relocs
, (PTR
) &info
);
1271 lineno_cache_rel_bfd
= abfd
;
1274 symname
= bfd_asymbol_name (sym
);
1275 for (i
= 0; i
< seccount
; i
++)
1279 for (j
= 0; j
< relcount
[i
]; j
++)
1284 if (r
->sym_ptr_ptr
!= NULL
1285 && (*r
->sym_ptr_ptr
)->section
== sym
->section
1286 && (*r
->sym_ptr_ptr
)->value
== sym
->value
1288 bfd_asymbol_name (*r
->sym_ptr_ptr
)) == 0
1289 && bfd_find_nearest_line (abfd
, secs
[i
], syms
,
1290 r
->address
, &filename
,
1291 &functionname
, &lineno
)
1292 && filename
!= NULL
)
1294 /* We only print the first one we find. */
1295 printf ("\t%s:%u", filename
, lineno
);
1302 else if (bfd_get_section (sym
)->owner
== abfd
)
1304 if (bfd_find_nearest_line (abfd
, bfd_get_section (sym
), syms
,
1305 sym
->value
, &filename
, &functionname
,
1310 printf ("\t%s:%u", filename
, lineno
);
1318 /* The following 3 groups of functions are called unconditionally,
1319 once at the start of processing each file of the appropriate type.
1320 They should check `filename_per_file' and `filename_per_symbol',
1321 as appropriate for their output format, to determine whether to
1324 /* Print the name of an object file given on the command line. */
1327 print_object_filename_bsd (filename
)
1330 if (filename_per_file
&& !filename_per_symbol
)
1331 printf ("\n%s:\n", filename
);
1335 print_object_filename_sysv (filename
)
1339 printf (_("\n\nUndefined symbols from %s:\n\n"), filename
);
1341 printf (_("\n\nSymbols from %s:\n\n"), filename
);
1342 if (print_width
== 8)
1344 Name Value Class Type Size Line Section\n\n"));
1347 Name Value Class Type Size Line Section\n\n"));
1351 print_object_filename_posix (filename
)
1354 if (filename_per_file
&& !filename_per_symbol
)
1355 printf ("%s:\n", filename
);
1358 /* Print the name of an archive file given on the command line. */
1361 print_archive_filename_bsd (filename
)
1364 if (filename_per_file
)
1365 printf ("\n%s:\n", filename
);
1369 print_archive_filename_sysv (filename
)
1370 char *filename ATTRIBUTE_UNUSED
;
1375 print_archive_filename_posix (filename
)
1376 char *filename ATTRIBUTE_UNUSED
;
1380 /* Print the name of an archive member file. */
1383 print_archive_member_bsd (archive
, filename
)
1384 char *archive ATTRIBUTE_UNUSED
;
1385 const char *filename
;
1387 if (!filename_per_symbol
)
1388 printf ("\n%s:\n", filename
);
1392 print_archive_member_sysv (archive
, filename
)
1394 const char *filename
;
1397 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive
, filename
);
1399 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive
, filename
);
1400 if (print_width
== 8)
1402 Name Value Class Type Size Line Section\n\n"));
1405 Name Value Class Type Size Line Section\n\n"));
1409 print_archive_member_posix (archive
, filename
)
1411 const char *filename
;
1413 if (!filename_per_symbol
)
1414 printf ("%s[%s]:\n", archive
, filename
);
1417 /* Print the name of the file (and archive, if there is one)
1418 containing a symbol. */
1421 print_symbol_filename_bsd (archive_bfd
, abfd
)
1422 bfd
*archive_bfd
, *abfd
;
1424 if (filename_per_symbol
)
1427 printf ("%s:", bfd_get_filename (archive_bfd
));
1428 printf ("%s:", bfd_get_filename (abfd
));
1433 print_symbol_filename_sysv (archive_bfd
, abfd
)
1434 bfd
*archive_bfd
, *abfd
;
1436 if (filename_per_symbol
)
1439 printf ("%s:", bfd_get_filename (archive_bfd
));
1440 printf ("%s:", bfd_get_filename (abfd
));
1445 print_symbol_filename_posix (archive_bfd
, abfd
)
1446 bfd
*archive_bfd
, *abfd
;
1448 if (filename_per_symbol
)
1451 printf ("%s[%s]: ", bfd_get_filename (archive_bfd
),
1452 bfd_get_filename (abfd
));
1454 printf ("%s: ", bfd_get_filename (abfd
));
1458 /* Print a symbol value. */
1461 print_value (abfd
, val
)
1462 bfd
*abfd ATTRIBUTE_UNUSED
;
1465 #if ! defined (BFD64) || BFD_HOST_64BIT_LONG
1466 printf (value_format
, val
);
1468 /* We have a 64 bit value to print, but the host is only 32 bit. */
1469 if (print_radix
== 16)
1470 bfd_fprintf_vma (abfd
, stdout
, val
);
1476 s
= buf
+ sizeof buf
;
1480 *--s
= (val
% print_radix
) + '0';
1483 while ((buf
+ sizeof buf
- 1) - s
< 16)
1490 /* Print a line of information about a symbol. */
1493 print_symbol_info_bsd (info
, abfd
)
1494 struct extended_symbol_info
*info
;
1497 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1499 if (print_width
== 16)
1505 print_value (abfd
, SYM_VALUE (info
));
1507 if (print_size
&& SYM_SIZE (info
))
1510 print_value (abfd
, SYM_SIZE (info
));
1514 printf (" %c", SYM_TYPE (info
));
1516 if (SYM_TYPE (info
) == '-')
1520 printf (other_format
, SYM_STAB_OTHER (info
));
1522 printf (desc_format
, SYM_STAB_DESC (info
));
1523 printf (" %5s", SYM_STAB_NAME (info
));
1525 print_symname (" %s", SYM_NAME (info
), abfd
);
1529 print_symbol_info_sysv (info
, abfd
)
1530 struct extended_symbol_info
*info
;
1533 print_symname ("%-20s|", SYM_NAME (info
), abfd
);
1535 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1537 if (print_width
== 8)
1543 print_value (abfd
, SYM_VALUE (info
));
1545 printf ("| %c |", SYM_TYPE (info
));
1547 if (SYM_TYPE (info
) == '-')
1550 printf ("%18s| ", SYM_STAB_NAME (info
)); /* (C) Type */
1551 printf (desc_format
, SYM_STAB_DESC (info
)); /* Size */
1552 printf ("| |"); /* Line, Section */
1556 /* Type, Size, Line, Section */
1559 get_symbol_type (ELF_ST_TYPE (info
->elfinfo
->internal_elf_sym
.st_info
)));
1563 if (SYM_SIZE (info
))
1564 print_value (abfd
, SYM_SIZE (info
));
1567 if (print_width
== 8)
1574 printf("| |%s", info
->elfinfo
->symbol
.section
->name
);
1581 print_symbol_info_posix (info
, abfd
)
1582 struct extended_symbol_info
*info
;
1585 print_symname ("%s ", SYM_NAME (info
), abfd
);
1586 printf ("%c ", SYM_TYPE (info
));
1588 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1592 print_value (abfd
, SYM_VALUE (info
));
1594 if (SYM_SIZE (info
))
1595 print_value (abfd
, SYM_SIZE (info
));
1600 print_symdef_entry (abfd
)
1603 symindex idx
= BFD_NO_MORE_SYMBOLS
;
1605 boolean everprinted
= false;
1607 for (idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
);
1608 idx
!= BFD_NO_MORE_SYMBOLS
;
1609 idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
))
1614 printf (_("\nArchive index:\n"));
1617 elt
= bfd_get_elt_at_index (abfd
, idx
);
1619 bfd_fatal ("bfd_get_elt_at_index");
1620 if (thesym
->name
!= (char *) NULL
)
1622 print_symname ("%s", thesym
->name
, abfd
);
1623 printf (" in %s\n", bfd_get_filename (elt
));
1628 /* This function is used to get the relocs for a particular section.
1629 It is called via bfd_map_over_sections. */
1632 get_relocs (abfd
, sec
, dataarg
)
1637 struct get_relocs_info
*data
= (struct get_relocs_info
*) dataarg
;
1641 if ((sec
->flags
& SEC_RELOC
) == 0)
1643 *data
->relocs
= NULL
;
1644 *data
->relcount
= 0;
1650 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1652 bfd_fatal (bfd_get_filename (abfd
));
1654 *data
->relocs
= (arelent
**) xmalloc (relsize
);
1655 *data
->relcount
= bfd_canonicalize_reloc (abfd
, sec
, *data
->relocs
,
1657 if (*data
->relcount
< 0)
1658 bfd_fatal (bfd_get_filename (abfd
));