1 /* nm.c -- Describe symbol table of a rel file.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007
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., 51 Franklin Street - Fifth Floor, 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 /* The output formatting functions. */
71 static void print_object_filename_bsd (char *);
72 static void print_object_filename_sysv (char *);
73 static void print_object_filename_posix (char *);
74 static void print_archive_filename_bsd (char *);
75 static void print_archive_filename_sysv (char *);
76 static void print_archive_filename_posix (char *);
77 static void print_archive_member_bsd (char *, const char *);
78 static void print_archive_member_sysv (char *, const char *);
79 static void print_archive_member_posix (char *, const char *);
80 static void print_symbol_filename_bsd (bfd
*, bfd
*);
81 static void print_symbol_filename_sysv (bfd
*, bfd
*);
82 static void print_symbol_filename_posix (bfd
*, bfd
*);
83 static void print_value (bfd
*, bfd_vma
);
84 static void print_symbol_info_bsd (struct extended_symbol_info
*, bfd
*);
85 static void print_symbol_info_sysv (struct extended_symbol_info
*, bfd
*);
86 static void print_symbol_info_posix (struct extended_symbol_info
*, bfd
*);
88 /* Support for different output formats. */
91 /* Print the name of an object file given on the command line. */
92 void (*print_object_filename
) (char *);
94 /* Print the name of an archive file given on the command line. */
95 void (*print_archive_filename
) (char *);
97 /* Print the name of an archive member file. */
98 void (*print_archive_member
) (char *, const char *);
100 /* Print the name of the file (and archive, if there is one)
101 containing a symbol. */
102 void (*print_symbol_filename
) (bfd
*, bfd
*);
104 /* Print a line of information about a symbol. */
105 void (*print_symbol_info
) (struct extended_symbol_info
*, bfd
*);
108 static struct output_fns formats
[] =
110 {print_object_filename_bsd
,
111 print_archive_filename_bsd
,
112 print_archive_member_bsd
,
113 print_symbol_filename_bsd
,
114 print_symbol_info_bsd
},
115 {print_object_filename_sysv
,
116 print_archive_filename_sysv
,
117 print_archive_member_sysv
,
118 print_symbol_filename_sysv
,
119 print_symbol_info_sysv
},
120 {print_object_filename_posix
,
121 print_archive_filename_posix
,
122 print_archive_member_posix
,
123 print_symbol_filename_posix
,
124 print_symbol_info_posix
}
127 /* Indices in `formats'. */
129 #define FORMAT_SYSV 1
130 #define FORMAT_POSIX 2
131 #define FORMAT_DEFAULT FORMAT_BSD
133 /* The output format to use. */
134 static struct output_fns
*format
= &formats
[FORMAT_DEFAULT
];
136 /* Command options. */
138 static int do_demangle
= 0; /* Pretty print C++ symbol names. */
139 static int external_only
= 0; /* Print external symbols only. */
140 static int defined_only
= 0; /* Print defined symbols only. */
141 static int no_sort
= 0; /* Don't sort; print syms in order found. */
142 static int print_debug_syms
= 0;/* Print debugger-only symbols too. */
143 static int print_armap
= 0; /* Describe __.SYMDEF data in archive files. */
144 static int print_size
= 0; /* Print size of defined symbols. */
145 static int reverse_sort
= 0; /* Sort in downward(alpha or numeric) order. */
146 static int sort_numerically
= 0;/* Sort in numeric rather than alpha order. */
147 static int sort_by_size
= 0; /* Sort by size of symbol. */
148 static int undefined_only
= 0; /* Print undefined symbols only. */
149 static int dynamic
= 0; /* Print dynamic symbols. */
150 static int show_version
= 0; /* Show the version number. */
151 static int show_stats
= 0; /* Show statistics. */
152 static int show_synthetic
= 0; /* Display synthesized symbols too. */
153 static int line_numbers
= 0; /* Print line numbers for symbols. */
154 static int allow_special_symbols
= 0; /* Allow special symbols. */
156 /* When to print the names of files. Not mutually exclusive in SYSV format. */
157 static int filename_per_file
= 0; /* Once per file, on its own line. */
158 static int filename_per_symbol
= 0; /* Once per symbol, at start of line. */
160 /* Print formats for printing a symbol value. */
161 static char value_format_32bit
[] = "%08lx";
162 static char value_format_64bit
[] = "%016lx";
163 static int print_width
= 0;
164 static int print_radix
= 16;
165 /* Print formats for printing stab info. */
166 static char other_format
[] = "%02x";
167 static char desc_format
[] = "%04x";
169 static char *target
= NULL
;
171 /* Used to cache the line numbers for a BFD. */
172 static bfd
*lineno_cache_bfd
;
173 static bfd
*lineno_cache_rel_bfd
;
175 #define OPTION_TARGET 200
177 static struct option long_options
[] =
179 {"debug-syms", no_argument
, &print_debug_syms
, 1},
180 {"demangle", optional_argument
, 0, 'C'},
181 {"dynamic", no_argument
, &dynamic
, 1},
182 {"extern-only", no_argument
, &external_only
, 1},
183 {"format", required_argument
, 0, 'f'},
184 {"help", no_argument
, 0, 'h'},
185 {"line-numbers", no_argument
, 0, 'l'},
186 {"no-cplus", no_argument
, &do_demangle
, 0}, /* Linux compatibility. */
187 {"no-demangle", no_argument
, &do_demangle
, 0},
188 {"no-sort", no_argument
, &no_sort
, 1},
189 {"numeric-sort", no_argument
, &sort_numerically
, 1},
190 {"portability", no_argument
, 0, 'P'},
191 {"print-armap", no_argument
, &print_armap
, 1},
192 {"print-file-name", no_argument
, 0, 'o'},
193 {"print-size", no_argument
, 0, 'S'},
194 {"radix", required_argument
, 0, 't'},
195 {"reverse-sort", no_argument
, &reverse_sort
, 1},
196 {"size-sort", no_argument
, &sort_by_size
, 1},
197 {"special-syms", no_argument
, &allow_special_symbols
, 1},
198 {"stats", no_argument
, &show_stats
, 1},
199 {"synthetic", no_argument
, &show_synthetic
, 1},
200 {"target", required_argument
, 0, OPTION_TARGET
},
201 {"defined-only", no_argument
, &defined_only
, 1},
202 {"undefined-only", no_argument
, &undefined_only
, 1},
203 {"version", no_argument
, &show_version
, 1},
204 {0, no_argument
, 0, 0}
207 /* Some error-reporting functions. */
210 usage (FILE *stream
, int status
)
212 fprintf (stream
, _("Usage: %s [option(s)] [file(s)]\n"), program_name
);
213 fprintf (stream
, _(" List symbols in [file(s)] (a.out by default).\n"));
214 fprintf (stream
, _(" The options are:\n\
215 -a, --debug-syms Display debugger-only symbols\n\
216 -A, --print-file-name Print name of the input file before every symbol\n\
217 -B Same as --format=bsd\n\
218 -C, --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
219 The STYLE, if specified, can be `auto' (the default),\n\
220 `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
222 --no-demangle Do not demangle low-level symbol names\n\
223 -D, --dynamic Display dynamic symbols instead of normal symbols\n\
224 --defined-only Display only defined symbols\n\
226 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
227 `sysv' or `posix'. The default is `bsd'\n\
228 -g, --extern-only Display only external symbols\n\
229 -l, --line-numbers Use debugging information to find a filename and\n\
230 line number for each symbol\n\
231 -n, --numeric-sort Sort symbols numerically by address\n\
233 -p, --no-sort Do not sort the symbols\n\
234 -P, --portability Same as --format=posix\n\
235 -r, --reverse-sort Reverse the sense of the sort\n\
236 -S, --print-size Print size of defined symbols\n\
237 -s, --print-armap Include index for symbols from archive members\n\
238 --size-sort Sort symbols by size\n\
239 --special-syms Include special symbols in the output\n\
240 --synthetic Display synthetic symbols as well\n\
241 -t, --radix=RADIX Use RADIX for printing symbol values\n\
242 --target=BFDNAME Specify the target object format as BFDNAME\n\
243 -u, --undefined-only Display only undefined symbols\n\
244 -X 32_64 (ignored)\n\
245 @FILE Read options from FILE\n\
246 -h, --help Display this information\n\
247 -V, --version Display this program's version number\n\
249 list_supported_targets (program_name
, stream
);
250 if (REPORT_BUGS_TO
[0] && status
== 0)
251 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
255 /* Set the radix for the symbol value and size according to RADIX. */
258 set_print_radix (char *radix
)
270 value_format_32bit
[4] = *radix
;
271 value_format_64bit
[5] = *radix
;
272 other_format
[3] = desc_format
[3] = *radix
;
275 fatal (_("%s: invalid radix"), radix
);
280 set_output_format (char *f
)
299 fatal (_("%s: invalid output format"), f
);
301 format
= &formats
[i
];
305 get_symbol_type (unsigned int type
)
307 static char buff
[32];
311 case STT_NOTYPE
: return "NOTYPE";
312 case STT_OBJECT
: return "OBJECT";
313 case STT_FUNC
: return "FUNC";
314 case STT_SECTION
: return "SECTION";
315 case STT_FILE
: return "FILE";
316 case STT_COMMON
: return "COMMON";
317 case STT_TLS
: return "TLS";
319 if (type
>= STT_LOPROC
&& type
<= STT_HIPROC
)
320 sprintf (buff
, _("<processor specific>: %d"), type
);
321 else if (type
>= STT_LOOS
&& type
<= STT_HIOS
)
322 sprintf (buff
, _("<OS specific>: %d"), type
);
324 sprintf (buff
, _("<unknown>: %d"), type
);
329 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
330 demangling it if requested. */
333 print_symname (const char *format
, const char *name
, bfd
*abfd
)
335 if (do_demangle
&& *name
)
337 char *res
= bfd_demangle (abfd
, name
, DMGL_ANSI
| DMGL_PARAMS
);
341 printf (format
, res
);
347 printf (format
, name
);
351 print_symdef_entry (bfd
*abfd
)
353 symindex idx
= BFD_NO_MORE_SYMBOLS
;
355 bfd_boolean everprinted
= FALSE
;
357 for (idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
);
358 idx
!= BFD_NO_MORE_SYMBOLS
;
359 idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
))
364 printf (_("\nArchive index:\n"));
367 elt
= bfd_get_elt_at_index (abfd
, idx
);
369 bfd_fatal ("bfd_get_elt_at_index");
370 if (thesym
->name
!= (char *) NULL
)
372 print_symname ("%s", thesym
->name
, abfd
);
373 printf (" in %s\n", bfd_get_filename (elt
));
378 /* Choose which symbol entries to print;
379 compact them downward to get rid of the rest.
380 Return the number of symbols to be printed. */
383 filter_symbols (bfd
*abfd
, bfd_boolean dynamic
, void *minisyms
,
384 long symcount
, unsigned int size
)
386 bfd_byte
*from
, *fromend
, *to
;
389 store
= bfd_make_empty_symbol (abfd
);
391 bfd_fatal (bfd_get_filename (abfd
));
393 from
= (bfd_byte
*) minisyms
;
394 fromend
= from
+ symcount
* size
;
395 to
= (bfd_byte
*) minisyms
;
397 for (; from
< fromend
; from
+= size
)
404 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, (const void *) from
, store
);
406 bfd_fatal (bfd_get_filename (abfd
));
409 keep
= bfd_is_und_section (sym
->section
);
410 else if (external_only
)
411 keep
= ((sym
->flags
& BSF_GLOBAL
) != 0
412 || (sym
->flags
& BSF_WEAK
) != 0
413 || bfd_is_und_section (sym
->section
)
414 || bfd_is_com_section (sym
->section
));
419 && ! print_debug_syms
420 && (sym
->flags
& BSF_DEBUGGING
) != 0)
425 && (bfd_is_abs_section (sym
->section
)
426 || bfd_is_und_section (sym
->section
)))
432 if (bfd_is_und_section (sym
->section
))
437 && bfd_is_target_special_symbol (abfd
, sym
)
438 && ! allow_special_symbols
)
443 memcpy (to
, from
, size
);
448 return (to
- (bfd_byte
*) minisyms
) / size
;
451 /* These globals are used to pass information into the sorting
453 static bfd
*sort_bfd
;
454 static bfd_boolean sort_dynamic
;
455 static asymbol
*sort_x
;
456 static asymbol
*sort_y
;
458 /* Symbol-sorting predicates */
459 #define valueof(x) ((x)->section->vma + (x)->value)
461 /* Numeric sorts. Undefined symbols are always considered "less than"
462 defined symbols with zero values. Common symbols are not treated
463 specially -- i.e., their sizes are used as their "values". */
466 non_numeric_forward (const void *P_x
, const void *P_y
)
471 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
472 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
473 if (x
== NULL
|| y
== NULL
)
474 bfd_fatal (bfd_get_filename (sort_bfd
));
476 xn
= bfd_asymbol_name (x
);
477 yn
= bfd_asymbol_name (y
);
485 /* Solaris 2.5 has a bug in strcoll.
486 strcoll returns invalid values when confronted with empty strings. */
492 return strcoll (xn
, yn
);
494 return strcmp (xn
, yn
);
499 non_numeric_reverse (const void *x
, const void *y
)
501 return - non_numeric_forward (x
, y
);
505 numeric_forward (const void *P_x
, const void *P_y
)
510 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
511 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
512 if (x
== NULL
|| y
== NULL
)
513 bfd_fatal (bfd_get_filename (sort_bfd
));
515 xs
= bfd_get_section (x
);
516 ys
= bfd_get_section (y
);
518 if (bfd_is_und_section (xs
))
520 if (! bfd_is_und_section (ys
))
523 else if (bfd_is_und_section (ys
))
525 else if (valueof (x
) != valueof (y
))
526 return valueof (x
) < valueof (y
) ? -1 : 1;
528 return non_numeric_forward (P_x
, P_y
);
532 numeric_reverse (const void *x
, const void *y
)
534 return - numeric_forward (x
, y
);
537 static int (*(sorters
[2][2])) (const void *, const void *) =
539 { non_numeric_forward
, non_numeric_reverse
},
540 { numeric_forward
, numeric_reverse
}
543 /* This sort routine is used by sort_symbols_by_size. It is similar
544 to numeric_forward, but when symbols have the same value it sorts
545 by section VMA. This simplifies the sort_symbols_by_size code
546 which handles symbols at the end of sections. Also, this routine
547 tries to sort file names before other symbols with the same value.
548 That will make the file name have a zero size, which will make
549 sort_symbols_by_size choose the non file name symbol, leading to
550 more meaningful output. For similar reasons, this code sorts
551 gnu_compiled_* and gcc2_compiled before other symbols with the same
555 size_forward1 (const void *P_x
, const void *P_y
)
563 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
564 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
565 if (x
== NULL
|| y
== NULL
)
566 bfd_fatal (bfd_get_filename (sort_bfd
));
568 xs
= bfd_get_section (x
);
569 ys
= bfd_get_section (y
);
571 if (bfd_is_und_section (xs
))
573 if (bfd_is_und_section (ys
))
576 if (valueof (x
) != valueof (y
))
577 return valueof (x
) < valueof (y
) ? -1 : 1;
579 if (xs
->vma
!= ys
->vma
)
580 return xs
->vma
< ys
->vma
? -1 : 1;
582 xn
= bfd_asymbol_name (x
);
583 yn
= bfd_asymbol_name (y
);
587 /* The symbols gnu_compiled and gcc2_compiled convey even less
588 information than the file name, so sort them out first. */
590 xf
= (strstr (xn
, "gnu_compiled") != NULL
591 || strstr (xn
, "gcc2_compiled") != NULL
);
592 yf
= (strstr (yn
, "gnu_compiled") != NULL
593 || strstr (yn
, "gcc2_compiled") != NULL
);
600 /* We use a heuristic for the file name. It may not work on non
601 Unix systems, but it doesn't really matter; the only difference
602 is precisely which symbol names get printed. */
604 #define file_symbol(s, sn, snl) \
605 (((s)->flags & BSF_FILE) != 0 \
606 || ((sn)[(snl) - 2] == '.' \
607 && ((sn)[(snl) - 1] == 'o' \
608 || (sn)[(snl) - 1] == 'a')))
610 xf
= file_symbol (x
, xn
, xnl
);
611 yf
= file_symbol (y
, yn
, ynl
);
618 return non_numeric_forward (P_x
, P_y
);
621 /* This sort routine is used by sort_symbols_by_size. It is sorting
622 an array of size_sym structures into size order. */
625 size_forward2 (const void *P_x
, const void *P_y
)
627 const struct size_sym
*x
= (const struct size_sym
*) P_x
;
628 const struct size_sym
*y
= (const struct size_sym
*) P_y
;
630 if (x
->size
< y
->size
)
631 return reverse_sort
? 1 : -1;
632 else if (x
->size
> y
->size
)
633 return reverse_sort
? -1 : 1;
635 return sorters
[0][reverse_sort
] (x
->minisym
, y
->minisym
);
638 /* Sort the symbols by size. ELF provides a size but for other formats
639 we have to make a guess by assuming that the difference between the
640 address of a symbol and the address of the next higher symbol is the
644 sort_symbols_by_size (bfd
*abfd
, bfd_boolean dynamic
, void *minisyms
,
645 long symcount
, unsigned int size
,
646 struct size_sym
**symsizesp
)
648 struct size_sym
*symsizes
;
649 bfd_byte
*from
, *fromend
;
651 asymbol
*store_sym
, *store_next
;
653 qsort (minisyms
, symcount
, size
, size_forward1
);
655 /* We are going to return a special set of symbols and sizes to
657 symsizes
= xmalloc (symcount
* sizeof (struct size_sym
));
658 *symsizesp
= symsizes
;
660 /* Note that filter_symbols has already removed all absolute and
661 undefined symbols. Here we remove all symbols whose size winds
663 from
= (bfd_byte
*) minisyms
;
664 fromend
= from
+ symcount
* size
;
671 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, (const void *) from
,
674 bfd_fatal (bfd_get_filename (abfd
));
677 for (; from
< fromend
; from
+= size
)
684 if (from
+ size
< fromend
)
686 next
= bfd_minisymbol_to_symbol (abfd
,
688 (const void *) (from
+ size
),
691 bfd_fatal (bfd_get_filename (abfd
));
696 sec
= bfd_get_section (sym
);
698 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
699 sz
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
700 else if (bfd_is_com_section (sec
))
704 if (from
+ size
< fromend
705 && sec
== bfd_get_section (next
))
706 sz
= valueof (next
) - valueof (sym
);
708 sz
= (bfd_get_section_vma (abfd
, sec
)
709 + bfd_section_size (abfd
, sec
)
715 symsizes
->minisym
= (const void *) from
;
723 store_sym
= store_next
;
727 symcount
= symsizes
- *symsizesp
;
729 /* We must now sort again by size. */
730 qsort ((void *) *symsizesp
, symcount
, sizeof (struct size_sym
), size_forward2
);
735 /* This function is used to get the relocs for a particular section.
736 It is called via bfd_map_over_sections. */
739 get_relocs (bfd
*abfd
, asection
*sec
, void *dataarg
)
741 struct get_relocs_info
*data
= (struct get_relocs_info
*) dataarg
;
745 if ((sec
->flags
& SEC_RELOC
) == 0)
747 *data
->relocs
= NULL
;
754 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
756 bfd_fatal (bfd_get_filename (abfd
));
758 *data
->relocs
= xmalloc (relsize
);
759 *data
->relcount
= bfd_canonicalize_reloc (abfd
, sec
, *data
->relocs
,
761 if (*data
->relcount
< 0)
762 bfd_fatal (bfd_get_filename (abfd
));
770 /* Print a single symbol. */
773 print_symbol (bfd
*abfd
, asymbol
*sym
, bfd_vma ssize
, bfd
*archive_bfd
)
776 struct extended_symbol_info info
;
780 format
->print_symbol_filename (archive_bfd
, abfd
);
782 bfd_get_symbol_info (abfd
, sym
, &syminfo
);
783 info
.sinfo
= &syminfo
;
785 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
786 info
.elfinfo
= (elf_symbol_type
*) sym
;
789 format
->print_symbol_info (&info
, abfd
);
793 static asymbol
**syms
;
794 static long symcount
;
795 const char *filename
, *functionname
;
798 /* We need to get the canonical symbols in order to call
799 bfd_find_nearest_line. This is inefficient, but, then, you
800 don't have to use --line-numbers. */
801 if (abfd
!= lineno_cache_bfd
&& syms
!= NULL
)
810 symsize
= bfd_get_symtab_upper_bound (abfd
);
812 bfd_fatal (bfd_get_filename (abfd
));
813 syms
= xmalloc (symsize
);
814 symcount
= bfd_canonicalize_symtab (abfd
, syms
);
816 bfd_fatal (bfd_get_filename (abfd
));
817 lineno_cache_bfd
= abfd
;
820 if (bfd_is_und_section (bfd_get_section (sym
)))
822 static asection
**secs
;
823 static arelent
***relocs
;
824 static long *relcount
;
825 static unsigned int seccount
;
829 /* For an undefined symbol, we try to find a reloc for the
830 symbol, and print the line number of the reloc. */
831 if (abfd
!= lineno_cache_rel_bfd
&& relocs
!= NULL
)
833 for (i
= 0; i
< seccount
; i
++)
834 if (relocs
[i
] != NULL
)
846 struct get_relocs_info info
;
848 seccount
= bfd_count_sections (abfd
);
850 secs
= xmalloc (seccount
* sizeof *secs
);
851 relocs
= xmalloc (seccount
* sizeof *relocs
);
852 relcount
= xmalloc (seccount
* sizeof *relcount
);
855 info
.relocs
= relocs
;
856 info
.relcount
= relcount
;
858 bfd_map_over_sections (abfd
, get_relocs
, (void *) &info
);
859 lineno_cache_rel_bfd
= abfd
;
862 symname
= bfd_asymbol_name (sym
);
863 for (i
= 0; i
< seccount
; i
++)
867 for (j
= 0; j
< relcount
[i
]; j
++)
872 if (r
->sym_ptr_ptr
!= NULL
873 && (*r
->sym_ptr_ptr
)->section
== sym
->section
874 && (*r
->sym_ptr_ptr
)->value
== sym
->value
876 bfd_asymbol_name (*r
->sym_ptr_ptr
)) == 0
877 && bfd_find_nearest_line (abfd
, secs
[i
], syms
,
878 r
->address
, &filename
,
879 &functionname
, &lineno
)
882 /* We only print the first one we find. */
883 printf ("\t%s:%u", filename
, lineno
);
890 else if (bfd_get_section (sym
)->owner
== abfd
)
892 if ((bfd_find_line (abfd
, syms
, sym
, &filename
, &lineno
)
893 || bfd_find_nearest_line (abfd
, bfd_get_section (sym
),
894 syms
, sym
->value
, &filename
,
895 &functionname
, &lineno
))
898 printf ("\t%s:%u", filename
, lineno
);
905 /* Print the symbols when sorting by size. */
908 print_size_symbols (bfd
*abfd
, bfd_boolean dynamic
,
909 struct size_sym
*symsizes
, long symcount
,
913 struct size_sym
*from
, *fromend
;
915 store
= bfd_make_empty_symbol (abfd
);
917 bfd_fatal (bfd_get_filename (abfd
));
920 fromend
= from
+ symcount
;
921 for (; from
< fromend
; from
++)
926 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, from
->minisym
, store
);
928 bfd_fatal (bfd_get_filename (abfd
));
930 /* For elf we have already computed the correct symbol size. */
931 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
934 ssize
= from
->size
- bfd_section_vma (abfd
, bfd_get_section (sym
));
936 print_symbol (abfd
, sym
, ssize
, archive_bfd
);
941 /* Print the symbols. If ARCHIVE_BFD is non-NULL, it is the archive
945 print_symbols (bfd
*abfd
, bfd_boolean dynamic
, void *minisyms
, long symcount
,
946 unsigned int size
, bfd
*archive_bfd
)
949 bfd_byte
*from
, *fromend
;
951 store
= bfd_make_empty_symbol (abfd
);
953 bfd_fatal (bfd_get_filename (abfd
));
955 from
= (bfd_byte
*) minisyms
;
956 fromend
= from
+ symcount
* size
;
957 for (; from
< fromend
; from
+= size
)
961 sym
= bfd_minisymbol_to_symbol (abfd
, dynamic
, from
, store
);
963 bfd_fatal (bfd_get_filename (abfd
));
965 print_symbol (abfd
, sym
, (bfd_vma
) 0, archive_bfd
);
969 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
972 display_rel_file (bfd
*abfd
, bfd
*archive_bfd
)
977 struct size_sym
*symsizes
;
981 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
983 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
988 symcount
= bfd_read_minisymbols (abfd
, dynamic
, &minisyms
, &size
);
990 bfd_fatal (bfd_get_filename (abfd
));
994 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
998 if (show_synthetic
&& size
== sizeof (asymbol
*))
1002 asymbol
**static_syms
= NULL
;
1003 asymbol
**dyn_syms
= NULL
;
1004 long static_count
= 0;
1009 dyn_count
= symcount
;
1010 dyn_syms
= minisyms
;
1014 long storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
1016 static_count
= symcount
;
1017 static_syms
= minisyms
;
1021 dyn_syms
= xmalloc (storage
);
1022 dyn_count
= bfd_canonicalize_dynamic_symtab (abfd
, dyn_syms
);
1024 bfd_fatal (bfd_get_filename (abfd
));
1027 synth_count
= bfd_get_synthetic_symtab (abfd
, static_count
, static_syms
,
1028 dyn_count
, dyn_syms
, &synthsyms
);
1029 if (synth_count
> 0)
1035 new_mini
= xmalloc ((symcount
+ synth_count
+ 1) * sizeof (*symp
));
1037 memcpy (symp
, minisyms
, symcount
* sizeof (*symp
));
1039 for (i
= 0; i
< synth_count
; i
++)
1040 *symp
++ = synthsyms
+ i
;
1042 minisyms
= new_mini
;
1043 symcount
+= synth_count
;
1047 /* Discard the symbols we don't want to print.
1048 It's OK to do this in place; we'll free the storage anyway
1049 (after printing). */
1051 symcount
= filter_symbols (abfd
, dynamic
, minisyms
, symcount
, size
);
1057 sort_dynamic
= dynamic
;
1058 sort_x
= bfd_make_empty_symbol (abfd
);
1059 sort_y
= bfd_make_empty_symbol (abfd
);
1060 if (sort_x
== NULL
|| sort_y
== NULL
)
1061 bfd_fatal (bfd_get_filename (abfd
));
1064 qsort (minisyms
, symcount
, size
,
1065 sorters
[sort_numerically
][reverse_sort
]);
1067 symcount
= sort_symbols_by_size (abfd
, dynamic
, minisyms
, symcount
,
1072 print_symbols (abfd
, dynamic
, minisyms
, symcount
, size
, archive_bfd
);
1074 print_size_symbols (abfd
, dynamic
, symsizes
, symcount
, archive_bfd
);
1080 set_print_width (bfd
*file
)
1082 print_width
= bfd_get_arch_size (file
);
1084 if (print_width
== -1)
1087 Guess the target's bitsize based on its name.
1088 We assume here than any 64-bit format will include
1089 "64" somewhere in its name. The only known exception
1090 is the MMO object file format. */
1091 if (strstr (bfd_get_target (file
), "64") != NULL
1092 || strcmp (bfd_get_target (file
), "mmo") == 0)
1100 display_archive (bfd
*file
)
1103 bfd
*last_arfile
= NULL
;
1106 format
->print_archive_filename (bfd_get_filename (file
));
1109 print_symdef_entry (file
);
1115 arfile
= bfd_openr_next_archived_file (file
, arfile
);
1119 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1120 bfd_fatal (bfd_get_filename (file
));
1124 if (bfd_check_format_matches (arfile
, bfd_object
, &matching
))
1126 set_print_width (arfile
);
1127 format
->print_archive_member (bfd_get_filename (file
),
1128 bfd_get_filename (arfile
));
1129 display_rel_file (arfile
, file
);
1133 bfd_nonfatal (bfd_get_filename (arfile
));
1134 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1136 list_matching_formats (matching
);
1141 if (last_arfile
!= NULL
)
1143 bfd_close (last_arfile
);
1144 lineno_cache_bfd
= NULL
;
1145 lineno_cache_rel_bfd
= NULL
;
1147 last_arfile
= arfile
;
1150 if (last_arfile
!= NULL
)
1152 bfd_close (last_arfile
);
1153 lineno_cache_bfd
= NULL
;
1154 lineno_cache_rel_bfd
= NULL
;
1159 display_file (char *filename
)
1161 bfd_boolean retval
= TRUE
;
1165 if (get_file_size (filename
) < 1)
1168 file
= bfd_openr (filename
, target
);
1171 bfd_nonfatal (filename
);
1175 if (bfd_check_format (file
, bfd_archive
))
1177 display_archive (file
);
1179 else if (bfd_check_format_matches (file
, bfd_object
, &matching
))
1181 set_print_width (file
);
1182 format
->print_object_filename (filename
);
1183 display_rel_file (file
, NULL
);
1187 bfd_nonfatal (filename
);
1188 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1190 list_matching_formats (matching
);
1196 if (!bfd_close (file
))
1197 bfd_fatal (filename
);
1199 lineno_cache_bfd
= NULL
;
1200 lineno_cache_rel_bfd
= NULL
;
1205 /* The following 3 groups of functions are called unconditionally,
1206 once at the start of processing each file of the appropriate type.
1207 They should check `filename_per_file' and `filename_per_symbol',
1208 as appropriate for their output format, to determine whether to
1211 /* Print the name of an object file given on the command line. */
1214 print_object_filename_bsd (char *filename
)
1216 if (filename_per_file
&& !filename_per_symbol
)
1217 printf ("\n%s:\n", filename
);
1221 print_object_filename_sysv (char *filename
)
1224 printf (_("\n\nUndefined symbols from %s:\n\n"), filename
);
1226 printf (_("\n\nSymbols from %s:\n\n"), filename
);
1227 if (print_width
== 32)
1229 Name Value Class Type Size Line Section\n\n"));
1232 Name Value Class Type Size Line Section\n\n"));
1236 print_object_filename_posix (char *filename
)
1238 if (filename_per_file
&& !filename_per_symbol
)
1239 printf ("%s:\n", filename
);
1242 /* Print the name of an archive file given on the command line. */
1245 print_archive_filename_bsd (char *filename
)
1247 if (filename_per_file
)
1248 printf ("\n%s:\n", filename
);
1252 print_archive_filename_sysv (char *filename ATTRIBUTE_UNUSED
)
1257 print_archive_filename_posix (char *filename ATTRIBUTE_UNUSED
)
1261 /* Print the name of an archive member file. */
1264 print_archive_member_bsd (char *archive ATTRIBUTE_UNUSED
,
1265 const char *filename
)
1267 if (!filename_per_symbol
)
1268 printf ("\n%s:\n", filename
);
1272 print_archive_member_sysv (char *archive
, const char *filename
)
1275 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive
, filename
);
1277 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive
, filename
);
1278 if (print_width
== 32)
1280 Name Value Class Type Size Line Section\n\n"));
1283 Name Value Class Type Size Line Section\n\n"));
1287 print_archive_member_posix (char *archive
, const char *filename
)
1289 if (!filename_per_symbol
)
1290 printf ("%s[%s]:\n", archive
, filename
);
1293 /* Print the name of the file (and archive, if there is one)
1294 containing a symbol. */
1297 print_symbol_filename_bsd (bfd
*archive_bfd
, bfd
*abfd
)
1299 if (filename_per_symbol
)
1302 printf ("%s:", bfd_get_filename (archive_bfd
));
1303 printf ("%s:", bfd_get_filename (abfd
));
1308 print_symbol_filename_sysv (bfd
*archive_bfd
, bfd
*abfd
)
1310 if (filename_per_symbol
)
1313 printf ("%s:", bfd_get_filename (archive_bfd
));
1314 printf ("%s:", bfd_get_filename (abfd
));
1319 print_symbol_filename_posix (bfd
*archive_bfd
, bfd
*abfd
)
1321 if (filename_per_symbol
)
1324 printf ("%s[%s]: ", bfd_get_filename (archive_bfd
),
1325 bfd_get_filename (abfd
));
1327 printf ("%s: ", bfd_get_filename (abfd
));
1331 /* Print a symbol value. */
1334 print_value (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_vma val
)
1336 switch (print_width
)
1339 printf (value_format_32bit
, (unsigned long) val
);
1343 #if BFD_HOST_64BIT_LONG
1344 printf (value_format_64bit
, val
);
1346 /* We have a 64 bit value to print, but the host is only 32 bit. */
1347 if (print_radix
== 16)
1348 bfd_fprintf_vma (abfd
, stdout
, val
);
1354 s
= buf
+ sizeof buf
;
1358 *--s
= (val
% print_radix
) + '0';
1361 while ((buf
+ sizeof buf
- 1) - s
< 16)
1369 fatal (_("Print width has not been initialized (%d)"), print_width
);
1374 /* Print a line of information about a symbol. */
1377 print_symbol_info_bsd (struct extended_symbol_info
*info
, bfd
*abfd
)
1379 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1381 if (print_width
== 64)
1387 /* Normally we print the value of the symbol. If we are printing the
1388 size or sorting by size then we print its size, except for the
1389 (weird) special case where both flags are defined, in which case we
1390 print both values. This conforms to documented behaviour. */
1391 if (sort_by_size
&& !print_size
)
1392 print_value (abfd
, SYM_SIZE (info
));
1394 print_value (abfd
, SYM_VALUE (info
));
1396 if (print_size
&& SYM_SIZE (info
))
1399 print_value (abfd
, SYM_SIZE (info
));
1403 printf (" %c", SYM_TYPE (info
));
1405 if (SYM_TYPE (info
) == '-')
1409 printf (other_format
, SYM_STAB_OTHER (info
));
1411 printf (desc_format
, SYM_STAB_DESC (info
));
1412 printf (" %5s", SYM_STAB_NAME (info
));
1414 print_symname (" %s", SYM_NAME (info
), abfd
);
1418 print_symbol_info_sysv (struct extended_symbol_info
*info
, bfd
*abfd
)
1420 print_symname ("%-20s|", SYM_NAME (info
), abfd
);
1422 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1424 if (print_width
== 32)
1430 print_value (abfd
, SYM_VALUE (info
));
1432 printf ("| %c |", SYM_TYPE (info
));
1434 if (SYM_TYPE (info
) == '-')
1437 printf ("%18s| ", SYM_STAB_NAME (info
)); /* (C) Type. */
1438 printf (desc_format
, SYM_STAB_DESC (info
)); /* Size. */
1439 printf ("| |"); /* Line, Section. */
1443 /* Type, Size, Line, Section */
1446 get_symbol_type (ELF_ST_TYPE (info
->elfinfo
->internal_elf_sym
.st_info
)));
1450 if (SYM_SIZE (info
))
1451 print_value (abfd
, SYM_SIZE (info
));
1454 if (print_width
== 32)
1461 printf("| |%s", info
->elfinfo
->symbol
.section
->name
);
1468 print_symbol_info_posix (struct extended_symbol_info
*info
, bfd
*abfd
)
1470 print_symname ("%s ", SYM_NAME (info
), abfd
);
1471 printf ("%c ", SYM_TYPE (info
));
1473 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1477 print_value (abfd
, SYM_VALUE (info
));
1479 if (SYM_SIZE (info
))
1480 print_value (abfd
, SYM_SIZE (info
));
1485 main (int argc
, char **argv
)
1490 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1491 setlocale (LC_MESSAGES
, "");
1493 #if defined (HAVE_SETLOCALE)
1494 setlocale (LC_CTYPE
, "");
1495 setlocale (LC_COLLATE
, "");
1497 bindtextdomain (PACKAGE
, LOCALEDIR
);
1498 textdomain (PACKAGE
);
1500 program_name
= *argv
;
1501 xmalloc_set_program_name (program_name
);
1503 START_PROGRESS (program_name
, 0);
1505 expandargv (&argc
, &argv
);
1508 set_default_bfd_target ();
1510 while ((c
= getopt_long (argc
, argv
, "aABCDef:gHhlnopPrSst:uvVvX:",
1511 long_options
, (int *) 0)) != EOF
)
1516 print_debug_syms
= 1;
1520 filename_per_symbol
= 1;
1522 case 'B': /* For MIPS compatibility. */
1523 set_output_format ("bsd");
1529 enum demangling_styles style
;
1531 style
= cplus_demangle_name_to_style (optarg
);
1532 if (style
== unknown_demangling
)
1533 fatal (_("unknown demangling style `%s'"),
1536 cplus_demangle_set_style (style
);
1543 /* Ignored for HP/UX compatibility. */
1546 set_output_format (optarg
);
1559 sort_numerically
= 1;
1565 set_output_format ("posix");
1577 set_print_radix (optarg
);
1586 /* Ignored for (partial) AIX compatibility. On AIX, the
1587 argument has values 32, 64, or 32_64, and specifies that
1588 only 32-bit, only 64-bit, or both kinds of objects should
1589 be examined. The default is 32. So plain AIX nm on a
1590 library archive with both kinds of objects will ignore
1591 the 64-bit ones. For GNU nm, the default is and always
1592 has been -X 32_64, and other options are not supported. */
1593 if (strcmp (optarg
, "32_64") != 0)
1594 fatal (_("Only -X 32_64 is supported"));
1597 case OPTION_TARGET
: /* --target */
1601 case 0: /* A long option that just sets a flag. */
1610 print_version ("nm");
1612 if (sort_by_size
&& undefined_only
)
1614 non_fatal (_("Using the --size-sort and --undefined-only options together"));
1615 non_fatal (_("will produce no output, since undefined symbols have no size."));
1619 /* OK, all options now parsed. If no filename specified, do a.out. */
1621 return !display_file ("a.out");
1625 if (argc
- optind
> 1)
1626 filename_per_file
= 1;
1628 /* We were given several filenames to do. */
1629 while (optind
< argc
)
1632 if (!display_file (argv
[optind
++]))
1636 END_PROGRESS (program_name
);
1641 char *lim
= (char *) sbrk (0);
1643 non_fatal (_("data size %ld"), (long) (lim
- (char *) &environ
));