1 /* nm.c -- Describe symbol table of a rel file.
2 Copyright (C) 1991-2022 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
25 #include "aout/stab_gnu.h"
26 #include "aout/ranlib.h"
28 #include "libiberty.h"
30 #include "elf/common.h"
31 #define DO_NOT_DEFINE_AOUTHDR
32 #define DO_NOT_DEFINE_FILHDR
33 #define DO_NOT_DEFINE_LINENO
34 #define DO_NOT_DEFINE_SCNHDR
35 #include "coff/external.h"
36 #include "coff/internal.h"
39 #include "demanguse.h"
40 #include "plugin-api.h"
42 #include "safe-ctype.h"
45 #define streq(a,b) (strcmp ((a),(b)) == 0)
48 /* When sorting by size, we use this structure to hold the size and a
49 pointer to the minisymbol. */
57 /* When fetching relocs, we use this structure to pass information to
60 struct get_relocs_info
68 struct extended_symbol_info
72 elf_symbol_type
*elfinfo
;
73 coff_symbol_type
*coffinfo
;
74 /* FIXME: We should add more fields for Type, Line, Section. */
76 #define SYM_VALUE(sym) (sym->sinfo->value)
77 #define SYM_TYPE(sym) (sym->sinfo->type)
78 #define SYM_STAB_NAME(sym) (sym->sinfo->stab_name)
79 #define SYM_STAB_DESC(sym) (sym->sinfo->stab_desc)
80 #define SYM_STAB_OTHER(sym) (sym->sinfo->stab_other)
81 #define SYM_SIZE(sym) \
82 (sym->elfinfo ? sym->elfinfo->internal_elf_sym.st_size: sym->ssize)
84 /* The output formatting functions. */
85 static void print_object_filename_bsd (const char *);
86 static void print_object_filename_sysv (const char *);
87 static void print_object_filename_posix (const char *);
88 static void do_not_print_object_filename (const char *);
90 static void print_archive_filename_bsd (const char *);
91 static void print_archive_filename_sysv (const char *);
92 static void print_archive_filename_posix (const char *);
93 static void do_not_print_archive_filename (const char *);
95 static void print_archive_member_bsd (const char *, const char *);
96 static void print_archive_member_sysv (const char *, const char *);
97 static void print_archive_member_posix (const char *, const char *);
98 static void do_not_print_archive_member (const char *, const char *);
100 static void print_symbol_filename_bsd (bfd
*, bfd
*);
101 static void print_symbol_filename_sysv (bfd
*, bfd
*);
102 static void print_symbol_filename_posix (bfd
*, bfd
*);
103 static void do_not_print_symbol_filename (bfd
*, bfd
*);
105 static void print_symbol_info_bsd (struct extended_symbol_info
*, bfd
*);
106 static void print_symbol_info_sysv (struct extended_symbol_info
*, bfd
*);
107 static void print_symbol_info_posix (struct extended_symbol_info
*, bfd
*);
108 static void just_print_symbol_name (struct extended_symbol_info
*, bfd
*);
110 static void print_value (bfd
*, bfd_vma
);
112 /* Support for different output formats. */
115 /* Print the name of an object file given on the command line. */
116 void (*print_object_filename
) (const char *);
118 /* Print the name of an archive file given on the command line. */
119 void (*print_archive_filename
) (const char *);
121 /* Print the name of an archive member file. */
122 void (*print_archive_member
) (const 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
) (bfd
*, bfd
*);
128 /* Print a line of information about a symbol. */
129 void (*print_symbol_info
) (struct extended_symbol_info
*, bfd
*);
132 /* Indices in `formats'. */
142 #define FORMAT_DEFAULT FORMAT_BSD
144 static struct output_fns formats
[FORMAT_MAX
] =
146 {print_object_filename_bsd
,
147 print_archive_filename_bsd
,
148 print_archive_member_bsd
,
149 print_symbol_filename_bsd
,
150 print_symbol_info_bsd
},
151 {print_object_filename_sysv
,
152 print_archive_filename_sysv
,
153 print_archive_member_sysv
,
154 print_symbol_filename_sysv
,
155 print_symbol_info_sysv
},
156 {print_object_filename_posix
,
157 print_archive_filename_posix
,
158 print_archive_member_posix
,
159 print_symbol_filename_posix
,
160 print_symbol_info_posix
},
161 {do_not_print_object_filename
,
162 do_not_print_archive_filename
,
163 do_not_print_archive_member
,
164 do_not_print_symbol_filename
,
165 just_print_symbol_name
}
169 /* The output format to use. */
170 static struct output_fns
*format
= &formats
[FORMAT_DEFAULT
];
171 static unsigned int print_format
= FORMAT_DEFAULT
;
172 static const char *print_format_string
= NULL
;
174 /* Command options. */
176 static int do_demangle
= 0; /* Pretty print C++ symbol names. */
177 static int external_only
= 0; /* Print external symbols only. */
178 static int defined_only
= 0; /* Print defined symbols only. */
179 static int no_sort
= 0; /* Don't sort; print syms in order found. */
180 static int print_debug_syms
= 0;/* Print debugger-only symbols too. */
181 static int print_armap
= 0; /* Describe __.SYMDEF data in archive files. */
182 static int print_size
= 0; /* Print size of defined symbols. */
183 static int reverse_sort
= 0; /* Sort in downward(alpha or numeric) order. */
184 static int sort_numerically
= 0;/* Sort in numeric rather than alpha order. */
185 static int sort_by_size
= 0; /* Sort by size of symbol. */
186 static int undefined_only
= 0; /* Print undefined symbols only. */
187 static int dynamic
= 0; /* Print dynamic symbols. */
188 static int show_version
= 0; /* Show the version number. */
189 static int show_synthetic
= 0; /* Display synthesized symbols too. */
190 static int line_numbers
= 0; /* Print line numbers for symbols. */
191 static int allow_special_symbols
= 0; /* Allow special symbols. */
192 static int with_symbol_versions
= -1; /* Output symbol version information. */
193 static int quiet
= 0; /* Suppress "no symbols" diagnostic. */
195 /* The characters to use for global and local ifunc symbols. */
196 #if DEFAULT_F_FOR_IFUNC_SYMBOLS
197 static const char * ifunc_type_chars
= "Ff";
199 static const char * ifunc_type_chars
= NULL
;
202 static int demangle_flags
= DMGL_ANSI
| DMGL_PARAMS
;
204 /* When to print the names of files. Not mutually exclusive in SYSV format. */
205 static int filename_per_file
= 0; /* Once per file, on its own line. */
206 static int filename_per_symbol
= 0; /* Once per symbol, at start of line. */
208 static int print_width
= 0;
209 static int print_radix
= 16;
210 /* Print formats for printing stab info. */
211 static char other_format
[] = "%02x";
212 static char desc_format
[] = "%04x";
214 static char *target
= NULL
;
215 #if BFD_SUPPORTS_PLUGINS
216 static const char *plugin_target
= "plugin";
218 static const char *plugin_target
= NULL
;
221 /* Used to cache the line numbers for a BFD. */
222 static bfd
*lineno_cache_bfd
;
223 static bfd
*lineno_cache_rel_bfd
;
225 typedef enum unicode_display_type
233 } unicode_display_type
;
235 static unicode_display_type unicode_display
= unicode_default
;
237 enum long_option_values
242 OPTION_RECURSE_LIMIT
,
243 OPTION_NO_RECURSE_LIMIT
,
248 static struct option long_options
[] =
250 {"debug-syms", no_argument
, &print_debug_syms
, 1},
251 {"demangle", optional_argument
, 0, 'C'},
252 {"dynamic", no_argument
, &dynamic
, 1},
253 {"extern-only", no_argument
, &external_only
, 1},
254 {"format", required_argument
, 0, 'f'},
255 {"help", no_argument
, 0, 'h'},
256 {"ifunc-chars", required_argument
, 0, OPTION_IFUNC_CHARS
},
257 {"just-symbols", no_argument
, 0, 'j'},
258 {"line-numbers", no_argument
, 0, 'l'},
259 {"no-cplus", no_argument
, &do_demangle
, 0}, /* Linux compatibility. */
260 {"no-demangle", no_argument
, &do_demangle
, 0},
261 {"no-recurse-limit", no_argument
, NULL
, OPTION_NO_RECURSE_LIMIT
},
262 {"no-recursion-limit", no_argument
, NULL
, OPTION_NO_RECURSE_LIMIT
},
263 {"no-sort", no_argument
, 0, 'p'},
264 {"numeric-sort", no_argument
, 0, 'n'},
265 {"plugin", required_argument
, 0, OPTION_PLUGIN
},
266 {"portability", no_argument
, 0, 'P'},
267 {"print-armap", no_argument
, &print_armap
, 1},
268 {"print-file-name", no_argument
, 0, 'o'},
269 {"print-size", no_argument
, 0, 'S'},
270 {"quiet", no_argument
, 0, OPTION_QUIET
},
271 {"radix", required_argument
, 0, 't'},
272 {"recurse-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
273 {"recursion-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
274 {"reverse-sort", no_argument
, &reverse_sort
, 1},
275 {"size-sort", no_argument
, 0, OPTION_SIZE_SORT
},
276 {"special-syms", no_argument
, &allow_special_symbols
, 1},
277 {"synthetic", no_argument
, &show_synthetic
, 1},
278 {"target", required_argument
, 0, OPTION_TARGET
},
279 {"defined-only", no_argument
, &defined_only
, 1},
280 {"undefined-only", no_argument
, &undefined_only
, 1},
281 {"unicode", required_argument
, NULL
, 'U'},
282 {"version", no_argument
, &show_version
, 1},
283 {"with-symbol-versions", no_argument
, &with_symbol_versions
, 1},
284 {"without-symbol-versions", no_argument
, &with_symbol_versions
, 0},
285 {0, no_argument
, 0, 0}
288 /* Some error-reporting functions. */
290 ATTRIBUTE_NORETURN
static void
291 usage (FILE *stream
, int status
)
293 fprintf (stream
, _("Usage: %s [option(s)] [file(s)]\n"), program_name
);
294 fprintf (stream
, _(" List symbols in [file(s)] (a.out by default).\n"));
295 fprintf (stream
, _(" The options are:\n"));
296 fprintf (stream
, _("\
297 -a, --debug-syms Display debugger-only symbols\n"));
298 fprintf (stream
, _("\
299 -A, --print-file-name Print name of the input file before every symbol\n"));
300 fprintf (stream
, _("\
301 -B Same as --format=bsd\n"));
302 fprintf (stream
, _("\
303 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n"));
304 display_demangler_styles (stream
, _("\
306 fprintf (stream
, _("\
307 --no-demangle Do not demangle low-level symbol names\n"));
308 fprintf (stream
, _("\
309 --recurse-limit Enable a demangling recursion limit. (default)\n"));
310 fprintf (stream
, _("\
311 --no-recurse-limit Disable a demangling recursion limit.\n"));
312 fprintf (stream
, _("\
313 -D, --dynamic Display dynamic symbols instead of normal symbols\n"));
314 fprintf (stream
, _("\
315 --defined-only Display only defined symbols\n"));
316 fprintf (stream
, _("\
318 fprintf (stream
, _("\
319 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
320 `sysv', `posix' or 'just-symbols'.\n\
321 The default is `bsd'\n"));
322 fprintf (stream
, _("\
323 -g, --extern-only Display only external symbols\n"));
324 fprintf (stream
, _("\
325 --ifunc-chars=CHARS Characters to use when displaying ifunc symbols\n"));
326 fprintf (stream
, _("\
327 -j, --just-symbols Same as --format=just-symbols\n"));
328 fprintf (stream
, _("\
329 -l, --line-numbers Use debugging information to find a filename and\n\
330 line number for each symbol\n"));
331 fprintf (stream
, _("\
332 -n, --numeric-sort Sort symbols numerically by address\n"));
333 fprintf (stream
, _("\
335 fprintf (stream
, _("\
336 -p, --no-sort Do not sort the symbols\n"));
337 fprintf (stream
, _("\
338 -P, --portability Same as --format=posix\n"));
339 fprintf (stream
, _("\
340 -r, --reverse-sort Reverse the sense of the sort\n"));
341 #if BFD_SUPPORTS_PLUGINS
342 fprintf (stream
, _("\
343 --plugin NAME Load the specified plugin\n"));
345 fprintf (stream
, _("\
346 -S, --print-size Print size of defined symbols\n"));
347 fprintf (stream
, _("\
348 -s, --print-armap Include index for symbols from archive members\n"));
349 fprintf (stream
, _("\
350 --quiet Suppress \"no symbols\" diagnostic\n"));
351 fprintf (stream
, _("\
352 --size-sort Sort symbols by size\n"));
353 fprintf (stream
, _("\
354 --special-syms Include special symbols in the output\n"));
355 fprintf (stream
, _("\
356 --synthetic Display synthetic symbols as well\n"));
357 fprintf (stream
, _("\
358 -t, --radix=RADIX Use RADIX for printing symbol values\n"));
359 fprintf (stream
, _("\
360 --target=BFDNAME Specify the target object format as BFDNAME\n"));
361 fprintf (stream
, _("\
362 -u, --undefined-only Display only undefined symbols\n"));
363 fprintf (stream
, _("\
364 -U {d|s|i|x|e|h} Specify how to treat UTF-8 encoded unicode characters\n\
365 --unicode={default|show|invalid|hex|escape|highlight}\n"));
366 fprintf (stream
, _("\
367 --with-symbol-versions Display version strings after symbol names\n"));
368 fprintf (stream
, _("\
369 -X 32_64 (ignored)\n"));
370 fprintf (stream
, _("\
371 @FILE Read options from FILE\n"));
372 fprintf (stream
, _("\
373 -h, --help Display this information\n"));
374 fprintf (stream
, _("\
375 -V, --version Display this program's version number\n"));
377 list_supported_targets (program_name
, stream
);
378 if (REPORT_BUGS_TO
[0] && status
== 0)
379 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
383 /* Set the radix for the symbol value and size according to RADIX. */
386 set_print_radix (char *radix
)
390 case 'x': print_radix
= 16; break;
391 case 'd': print_radix
= 10; break;
392 case 'o': print_radix
= 8; break;
395 fatal (_("%s: invalid radix"), radix
);
398 other_format
[3] = desc_format
[3] = *radix
;
402 set_output_format (char *f
)
422 i
= FORMAT_JUST_SYMBOLS
;
425 fatal (_("%s: invalid output format"), f
);
427 format
= &formats
[i
];
432 get_elf_symbol_type (unsigned int type
)
439 case STT_NOTYPE
: return "NOTYPE";
440 case STT_OBJECT
: return "OBJECT";
441 case STT_FUNC
: return "FUNC";
442 case STT_SECTION
: return "SECTION";
443 case STT_FILE
: return "FILE";
444 case STT_COMMON
: return "COMMON";
445 case STT_TLS
: return "TLS";
449 if (type
>= STT_LOPROC
&& type
<= STT_HIPROC
)
450 n
= asprintf (&bufp
, _("<processor specific>: %d"), type
);
451 else if (type
>= STT_LOOS
&& type
<= STT_HIOS
)
452 n
= asprintf (&bufp
, _("<OS specific>: %d"), type
);
454 n
= asprintf (&bufp
, _("<unknown>: %d"), type
);
456 fatal ("%s", xstrerror (errno
));
461 get_coff_symbol_type (const struct internal_syment
*sym
)
466 switch (sym
->n_sclass
)
468 case C_BLOCK
: return "Block";
469 case C_FILE
: return "File";
470 case C_LINE
: return "Line";
476 switch (DTYPE(sym
->n_type
))
478 case DT_FCN
: return "Function";
479 case DT_PTR
: return "Pointer";
480 case DT_ARY
: return "Array";
484 n
= asprintf (&bufp
, _("<unknown>: %d/%d"), sym
->n_sclass
, sym
->n_type
);
486 fatal ("%s", xstrerror (errno
));
490 /* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
491 The conversion format is controlled by the unicode_display variable.
492 Returns the number of characters added to OUT.
493 Returns the number of bytes consumed from IN in CONSUMED.
494 Always consumes at least one byte and displays at least one character. */
497 display_utf8 (const unsigned char * in
, char * out
, unsigned int * consumed
)
499 char * orig_out
= out
;
500 unsigned int nchars
= 0;
503 if (unicode_display
== unicode_default
)
509 if ((in
[1] & 0xc0) != 0x80)
512 if ((in
[0] & 0x20) == 0)
518 if ((in
[2] & 0xc0) != 0x80)
521 if ((in
[0] & 0x10) == 0)
527 if ((in
[3] & 0xc0) != 0x80)
533 switch (unicode_display
)
536 /* Copy the bytes into the output buffer as is. */
537 memcpy (out
, in
, nchars
);
541 case unicode_invalid
:
543 out
+= sprintf (out
, "%c", unicode_display
== unicode_hex
? '<' : '{');
544 out
+= sprintf (out
, "0x");
545 for (j
= 0; j
< nchars
; j
++)
546 out
+= sprintf (out
, "%02x", in
[j
]);
547 out
+= sprintf (out
, "%c", unicode_display
== unicode_hex
? '>' : '}');
550 case unicode_highlight
:
552 out
+= sprintf (out
, "\x1B[31;47m"); /* Red. */
558 out
+= sprintf (out
, "\\u%02x%02x",
559 ((in
[0] & 0x1c) >> 2),
560 ((in
[0] & 0x03) << 6) | (in
[1] & 0x3f));
564 out
+= sprintf (out
, "\\u%02x%02x",
565 ((in
[0] & 0x0f) << 4) | ((in
[1] & 0x3c) >> 2),
566 ((in
[1] & 0x03) << 6) | ((in
[2] & 0x3f)));
570 out
+= sprintf (out
, "\\u%02x%02x%02x",
571 ((in
[0] & 0x07) << 6) | ((in
[1] & 0x3c) >> 2),
572 ((in
[1] & 0x03) << 6) | ((in
[2] & 0x3c) >> 2),
573 ((in
[2] & 0x03) << 6) | ((in
[3] & 0x3f)));
580 if (unicode_display
== unicode_highlight
&& isatty (1))
581 out
+= sprintf (out
, "\033[0m"); /* Default colour. */
590 return out
- orig_out
;
593 /* Not a valid UTF-8 sequence. */
599 /* Convert any UTF-8 encoded characters in NAME into the form specified by
600 unicode_display. Also converts control characters. Returns a static
601 buffer if conversion was necessary.
602 Code stolen from objdump.c:sanitize_string(). */
605 convert_utf8 (const char * in
)
607 static char * buffer
= NULL
;
608 static size_t buffer_len
= 0;
609 const char * original
= in
;
616 /* See if any conversion is necessary.
617 In the majority of cases it will not be needed. */
620 unsigned char c
= *in
++;
628 if (unicode_display
!= unicode_default
&& c
>= 0xc0)
633 /* Copy the input, translating as needed. */
635 if (buffer_len
< (strlen (in
) * 9))
637 free ((void *) buffer
);
638 buffer_len
= strlen (in
) * 9;
639 buffer
= xmalloc (buffer_len
+ 1);
645 unsigned char c
= *in
++;
655 else if (unicode_display
!= unicode_default
&& c
>= 0xc0)
657 unsigned int num_consumed
;
659 out
+= display_utf8 ((const unsigned char *)(in
- 1), out
, & num_consumed
);
660 in
+= num_consumed
- 1;
671 /* Print symbol name NAME, read from ABFD, with printf format FORM,
672 demangling it if requested. */
675 print_symname (const char *form
, struct extended_symbol_info
*info
,
676 const char *name
, bfd
*abfd
)
682 name
= info
->sinfo
->name
;
684 if (!with_symbol_versions
685 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
687 atver
= strchr (name
, '@');
692 if (do_demangle
&& *name
)
694 alloc
= bfd_demangle (abfd
, name
, demangle_flags
);
699 if (unicode_display
!= unicode_default
)
701 name
= convert_utf8 (name
);
704 if (info
!= NULL
&& info
->elfinfo
&& with_symbol_versions
)
706 const char *version_string
;
710 = bfd_get_symbol_version_string (abfd
, &info
->elfinfo
->symbol
,
712 if (version_string
&& version_string
[0])
714 const char *at
= "@@";
715 if (hidden
|| bfd_is_und_section (info
->elfinfo
->symbol
.section
))
717 alloc
= reconcat (alloc
, name
, at
, version_string
, NULL
);
729 print_symdef_entry (bfd
*abfd
)
731 symindex idx
= BFD_NO_MORE_SYMBOLS
;
733 bool everprinted
= false;
735 for (idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
);
736 idx
!= BFD_NO_MORE_SYMBOLS
;
737 idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
))
742 printf (_("\nArchive index:\n"));
745 elt
= bfd_get_elt_at_index (abfd
, idx
);
747 bfd_fatal ("bfd_get_elt_at_index");
748 if (thesym
->name
!= (char *) NULL
)
750 print_symname ("%s", NULL
, thesym
->name
, abfd
);
751 printf (" in %s\n", bfd_get_filename (elt
));
757 /* True when we can report missing plugin error. */
758 bool report_plugin_err
= true;
760 /* Choose which symbol entries to print;
761 compact them downward to get rid of the rest.
762 Return the number of symbols to be printed. */
765 filter_symbols (bfd
*abfd
, bool is_dynamic
, void *minisyms
,
766 long symcount
, unsigned int size
)
768 bfd_byte
*from
, *fromend
, *to
;
771 store
= bfd_make_empty_symbol (abfd
);
773 bfd_fatal (bfd_get_filename (abfd
));
775 from
= (bfd_byte
*) minisyms
;
776 fromend
= from
+ symcount
* size
;
777 to
= (bfd_byte
*) minisyms
;
779 for (; from
< fromend
; from
+= size
)
786 sym
= bfd_minisymbol_to_symbol (abfd
, is_dynamic
, (const void *) from
, store
);
788 bfd_fatal (bfd_get_filename (abfd
));
790 if (sym
->name
!= NULL
791 && sym
->name
[0] == '_'
792 && sym
->name
[1] == '_'
793 && strcmp (sym
->name
+ (sym
->name
[2] == '_'), "__gnu_lto_slim") == 0
794 && report_plugin_err
)
796 report_plugin_err
= false;
797 non_fatal (_("%s: plugin needed to handle lto object"),
798 bfd_get_filename (abfd
));
802 keep
= bfd_is_und_section (sym
->section
);
803 else if (external_only
)
804 /* PR binutls/12753: Unique symbols are global too. */
805 keep
= ((sym
->flags
& (BSF_GLOBAL
807 | BSF_GNU_UNIQUE
)) != 0
808 || bfd_is_und_section (sym
->section
)
809 || bfd_is_com_section (sym
->section
));
814 && ! print_debug_syms
815 && (sym
->flags
& BSF_DEBUGGING
) != 0)
820 && (bfd_is_abs_section (sym
->section
)
821 || bfd_is_und_section (sym
->section
)))
827 if (bfd_is_und_section (sym
->section
))
832 && bfd_is_target_special_symbol (abfd
, sym
)
833 && ! allow_special_symbols
)
839 memcpy (to
, from
, size
);
844 return (to
- (bfd_byte
*) minisyms
) / size
;
847 /* These globals are used to pass information into the sorting
849 static bfd
*sort_bfd
;
850 static bool sort_dynamic
;
851 static asymbol
*sort_x
;
852 static asymbol
*sort_y
;
854 /* Symbol-sorting predicates */
855 #define valueof(x) ((x)->section->vma + (x)->value)
857 /* Numeric sorts. Undefined symbols are always considered "less than"
858 defined symbols with zero values. Common symbols are not treated
859 specially -- i.e., their sizes are used as their "values". */
862 non_numeric_forward (const void *P_x
, const void *P_y
)
867 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
868 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
869 if (x
== NULL
|| y
== NULL
)
870 bfd_fatal (bfd_get_filename (sort_bfd
));
872 xn
= bfd_asymbol_name (x
);
873 yn
= bfd_asymbol_name (y
);
880 /* Solaris 2.5 has a bug in strcoll.
881 strcoll returns invalid values when confronted with empty strings. */
887 return strcoll (xn
, yn
);
891 non_numeric_reverse (const void *x
, const void *y
)
893 return - non_numeric_forward (x
, y
);
897 numeric_forward (const void *P_x
, const void *P_y
)
902 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
903 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
904 if (x
== NULL
|| y
== NULL
)
905 bfd_fatal (bfd_get_filename (sort_bfd
));
907 xs
= bfd_asymbol_section (x
);
908 ys
= bfd_asymbol_section (y
);
910 if (bfd_is_und_section (xs
))
912 if (! bfd_is_und_section (ys
))
915 else if (bfd_is_und_section (ys
))
917 else if (valueof (x
) != valueof (y
))
918 return valueof (x
) < valueof (y
) ? -1 : 1;
920 return non_numeric_forward (P_x
, P_y
);
924 numeric_reverse (const void *x
, const void *y
)
926 return - numeric_forward (x
, y
);
929 static int (*(sorters
[2][2])) (const void *, const void *) =
931 { non_numeric_forward
, non_numeric_reverse
},
932 { numeric_forward
, numeric_reverse
}
935 /* This sort routine is used by sort_symbols_by_size. It is similar
936 to numeric_forward, but when symbols have the same value it sorts
937 by section VMA. This simplifies the sort_symbols_by_size code
938 which handles symbols at the end of sections. Also, this routine
939 tries to sort file names before other symbols with the same value.
940 That will make the file name have a zero size, which will make
941 sort_symbols_by_size choose the non file name symbol, leading to
942 more meaningful output. For similar reasons, this code sorts
943 gnu_compiled_* and gcc2_compiled before other symbols with the same
947 size_forward1 (const void *P_x
, const void *P_y
)
955 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
956 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
957 if (x
== NULL
|| y
== NULL
)
958 bfd_fatal (bfd_get_filename (sort_bfd
));
960 xs
= bfd_asymbol_section (x
);
961 ys
= bfd_asymbol_section (y
);
963 if (bfd_is_und_section (xs
))
965 if (bfd_is_und_section (ys
))
968 if (valueof (x
) != valueof (y
))
969 return valueof (x
) < valueof (y
) ? -1 : 1;
971 if (xs
->vma
!= ys
->vma
)
972 return xs
->vma
< ys
->vma
? -1 : 1;
974 xn
= bfd_asymbol_name (x
);
975 yn
= bfd_asymbol_name (y
);
979 /* The symbols gnu_compiled and gcc2_compiled convey even less
980 information than the file name, so sort them out first. */
982 xf
= (strstr (xn
, "gnu_compiled") != NULL
983 || strstr (xn
, "gcc2_compiled") != NULL
);
984 yf
= (strstr (yn
, "gnu_compiled") != NULL
985 || strstr (yn
, "gcc2_compiled") != NULL
);
992 /* We use a heuristic for the file name. It may not work on non
993 Unix systems, but it doesn't really matter; the only difference
994 is precisely which symbol names get printed. */
996 #define file_symbol(s, sn, snl) \
997 (((s)->flags & BSF_FILE) != 0 \
999 && (sn)[(snl) - 2] == '.' \
1000 && ((sn)[(snl) - 1] == 'o' \
1001 || (sn)[(snl) - 1] == 'a')))
1003 xf
= file_symbol (x
, xn
, xnl
);
1004 yf
= file_symbol (y
, yn
, ynl
);
1011 return non_numeric_forward (P_x
, P_y
);
1014 /* This sort routine is used by sort_symbols_by_size. It is sorting
1015 an array of size_sym structures into size order. */
1018 size_forward2 (const void *P_x
, const void *P_y
)
1020 const struct size_sym
*x
= (const struct size_sym
*) P_x
;
1021 const struct size_sym
*y
= (const struct size_sym
*) P_y
;
1023 if (x
->size
< y
->size
)
1024 return reverse_sort
? 1 : -1;
1025 else if (x
->size
> y
->size
)
1026 return reverse_sort
? -1 : 1;
1028 return sorters
[0][reverse_sort
] (x
->minisym
, y
->minisym
);
1031 /* Sort the symbols by size. ELF provides a size but for other formats
1032 we have to make a guess by assuming that the difference between the
1033 address of a symbol and the address of the next higher symbol is the
1037 sort_symbols_by_size (bfd
*abfd
, bool is_dynamic
, void *minisyms
,
1038 long symcount
, unsigned int size
,
1039 struct size_sym
**symsizesp
)
1041 struct size_sym
*symsizes
;
1042 bfd_byte
*from
, *fromend
;
1043 asymbol
*sym
= NULL
;
1044 asymbol
*store_sym
, *store_next
;
1046 qsort (minisyms
, symcount
, size
, size_forward1
);
1048 /* We are going to return a special set of symbols and sizes to
1050 symsizes
= (struct size_sym
*) xmalloc (symcount
* sizeof (struct size_sym
));
1051 *symsizesp
= symsizes
;
1053 /* Note that filter_symbols has already removed all absolute and
1054 undefined symbols. Here we remove all symbols whose size winds
1056 from
= (bfd_byte
*) minisyms
;
1057 fromend
= from
+ symcount
* size
;
1060 store_next
= sort_y
;
1064 sym
= bfd_minisymbol_to_symbol (abfd
, is_dynamic
, (const void *) from
,
1067 bfd_fatal (bfd_get_filename (abfd
));
1070 for (; from
< fromend
; from
+= size
)
1077 if (from
+ size
< fromend
)
1079 next
= bfd_minisymbol_to_symbol (abfd
,
1081 (const void *) (from
+ size
),
1084 bfd_fatal (bfd_get_filename (abfd
));
1089 sec
= bfd_asymbol_section (sym
);
1091 /* Synthetic symbols don't have a full type set of data available, thus
1092 we can't rely on that information for the symbol size. Ditto for
1093 bfd/section.c:global_syms like *ABS*. */
1094 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0
1095 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1096 sz
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
1097 else if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0
1098 && bfd_is_com_section (sec
))
1102 if (from
+ size
< fromend
1103 && sec
== bfd_asymbol_section (next
))
1104 sz
= valueof (next
) - valueof (sym
);
1106 sz
= (bfd_section_vma (sec
)
1107 + bfd_section_size (sec
)
1113 symsizes
->minisym
= (const void *) from
;
1114 symsizes
->size
= sz
;
1121 store_sym
= store_next
;
1125 symcount
= symsizes
- *symsizesp
;
1127 /* We must now sort again by size. */
1128 qsort ((void *) *symsizesp
, symcount
, sizeof (struct size_sym
), size_forward2
);
1133 /* This function is used to get the relocs for a particular section.
1134 It is called via bfd_map_over_sections. */
1137 get_relocs (bfd
*abfd
, asection
*sec
, void *dataarg
)
1139 struct get_relocs_info
*data
= (struct get_relocs_info
*) dataarg
;
1143 if ((sec
->flags
& SEC_RELOC
) == 0)
1145 *data
->relocs
= NULL
;
1146 *data
->relcount
= 0;
1152 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1154 bfd_fatal (bfd_get_filename (abfd
));
1156 *data
->relocs
= (arelent
**) xmalloc (relsize
);
1157 *data
->relcount
= bfd_canonicalize_reloc (abfd
, sec
, *data
->relocs
,
1159 if (*data
->relcount
< 0)
1160 bfd_fatal (bfd_get_filename (abfd
));
1168 /* Print a single symbol. */
1171 print_symbol (bfd
* abfd
,
1176 symbol_info syminfo
;
1177 struct extended_symbol_info info
;
1181 format
->print_symbol_filename (archive_bfd
, abfd
);
1183 bfd_get_symbol_info (abfd
, sym
, &syminfo
);
1185 /* PR 22967 - Distinguish between local and global ifunc symbols. */
1186 if (syminfo
.type
== 'i'
1187 && sym
->flags
& BSF_GNU_INDIRECT_FUNCTION
)
1189 if (ifunc_type_chars
== NULL
|| ifunc_type_chars
[0] == 0)
1190 ; /* Change nothing. */
1191 else if (sym
->flags
& BSF_GLOBAL
)
1192 syminfo
.type
= ifunc_type_chars
[0];
1193 else if (ifunc_type_chars
[1] != 0)
1194 syminfo
.type
= ifunc_type_chars
[1];
1197 info
.sinfo
= &syminfo
;
1199 /* Synthetic symbols do not have a full symbol type set of data available.
1200 Nor do bfd/section.c:global_syms like *ABS*. */
1201 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) != 0)
1203 info
.elfinfo
= NULL
;
1204 info
.coffinfo
= NULL
;
1208 info
.elfinfo
= elf_symbol_from (sym
);
1209 info
.coffinfo
= coff_symbol_from (sym
);
1212 format
->print_symbol_info (&info
, abfd
);
1216 static asymbol
**syms
;
1217 static long symcount
;
1218 const char *filename
, *functionname
;
1219 unsigned int lineno
;
1221 /* We need to get the canonical symbols in order to call
1222 bfd_find_nearest_line. This is inefficient, but, then, you
1223 don't have to use --line-numbers. */
1224 if (abfd
!= lineno_cache_bfd
&& syms
!= NULL
)
1233 symsize
= bfd_get_symtab_upper_bound (abfd
);
1235 bfd_fatal (bfd_get_filename (abfd
));
1236 syms
= (asymbol
**) xmalloc (symsize
);
1237 symcount
= bfd_canonicalize_symtab (abfd
, syms
);
1239 bfd_fatal (bfd_get_filename (abfd
));
1240 lineno_cache_bfd
= abfd
;
1243 if (bfd_is_und_section (bfd_asymbol_section (sym
)))
1245 static asection
**secs
;
1246 static arelent
***relocs
;
1247 static long *relcount
;
1248 static unsigned int seccount
;
1250 const char *symname
;
1252 /* For an undefined symbol, we try to find a reloc for the
1253 symbol, and print the line number of the reloc. */
1254 if (abfd
!= lineno_cache_rel_bfd
&& relocs
!= NULL
)
1256 for (i
= 0; i
< seccount
; i
++)
1257 if (relocs
[i
] != NULL
)
1269 struct get_relocs_info rinfo
;
1271 seccount
= bfd_count_sections (abfd
);
1273 secs
= (asection
**) xmalloc (seccount
* sizeof *secs
);
1274 relocs
= (arelent
***) xmalloc (seccount
* sizeof *relocs
);
1275 relcount
= (long *) xmalloc (seccount
* sizeof *relcount
);
1278 rinfo
.relocs
= relocs
;
1279 rinfo
.relcount
= relcount
;
1281 bfd_map_over_sections (abfd
, get_relocs
, (void *) &rinfo
);
1282 lineno_cache_rel_bfd
= abfd
;
1285 symname
= bfd_asymbol_name (sym
);
1286 for (i
= 0; i
< seccount
; i
++)
1290 for (j
= 0; j
< relcount
[i
]; j
++)
1295 if (r
->sym_ptr_ptr
!= NULL
1296 && (*r
->sym_ptr_ptr
)->section
== sym
->section
1297 && (*r
->sym_ptr_ptr
)->value
== sym
->value
1299 bfd_asymbol_name (*r
->sym_ptr_ptr
)) == 0
1300 && bfd_find_nearest_line (abfd
, secs
[i
], syms
,
1301 r
->address
, &filename
,
1302 &functionname
, &lineno
)
1303 && filename
!= NULL
)
1305 /* We only print the first one we find. */
1306 printf ("\t%s:%u", filename
, lineno
);
1313 else if (bfd_asymbol_section (sym
)->owner
== abfd
)
1315 if ((bfd_find_line (abfd
, syms
, sym
, &filename
, &lineno
)
1316 || bfd_find_nearest_line (abfd
, bfd_asymbol_section (sym
),
1317 syms
, sym
->value
, &filename
,
1318 &functionname
, &lineno
))
1321 printf ("\t%s:%u", filename
, lineno
);
1328 /* Print the symbols when sorting by size. */
1331 print_size_symbols (bfd
*abfd
,
1333 struct size_sym
*symsizes
,
1338 struct size_sym
*from
;
1339 struct size_sym
*fromend
;
1341 store
= bfd_make_empty_symbol (abfd
);
1343 bfd_fatal (bfd_get_filename (abfd
));
1346 fromend
= from
+ symcount
;
1348 for (; from
< fromend
; from
++)
1352 sym
= bfd_minisymbol_to_symbol (abfd
, is_dynamic
, from
->minisym
, store
);
1354 bfd_fatal (bfd_get_filename (abfd
));
1356 print_symbol (abfd
, sym
, from
->size
, archive_bfd
);
1361 /* Print the symbols of ABFD that are held in MINISYMS.
1363 If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD.
1365 SYMCOUNT is the number of symbols in MINISYMS.
1367 SIZE is the size of a symbol in MINISYMS. */
1370 print_symbols (bfd
*abfd
,
1381 store
= bfd_make_empty_symbol (abfd
);
1383 bfd_fatal (bfd_get_filename (abfd
));
1385 from
= (bfd_byte
*) minisyms
;
1386 fromend
= from
+ symcount
* size
;
1388 for (; from
< fromend
; from
+= size
)
1392 sym
= bfd_minisymbol_to_symbol (abfd
, is_dynamic
, from
, store
);
1394 bfd_fatal (bfd_get_filename (abfd
));
1396 print_symbol (abfd
, sym
, (bfd_vma
) 0, archive_bfd
);
1400 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
1403 display_rel_file (bfd
*abfd
, bfd
*archive_bfd
)
1408 struct size_sym
*symsizes
;
1409 asymbol
*synthsyms
= NULL
;
1413 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
1416 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
1421 symcount
= bfd_read_minisymbols (abfd
, dynamic
, &minisyms
, &size
);
1424 if (dynamic
&& bfd_get_error () == bfd_error_no_symbols
)
1427 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
1431 bfd_fatal (bfd_get_filename (abfd
));
1437 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
1441 if (show_synthetic
&& size
== sizeof (asymbol
*))
1443 asymbol
**static_syms
= NULL
;
1444 asymbol
**dyn_syms
= NULL
;
1445 long static_count
= 0;
1451 dyn_count
= symcount
;
1452 dyn_syms
= (asymbol
**) minisyms
;
1456 long storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
1458 static_count
= symcount
;
1459 static_syms
= (asymbol
**) minisyms
;
1463 dyn_syms
= (asymbol
**) xmalloc (storage
);
1464 dyn_count
= bfd_canonicalize_dynamic_symtab (abfd
, dyn_syms
);
1466 bfd_fatal (bfd_get_filename (abfd
));
1470 synth_count
= bfd_get_synthetic_symtab (abfd
, static_count
, static_syms
,
1471 dyn_count
, dyn_syms
, &synthsyms
);
1472 if (synth_count
> 0)
1477 minisyms
= xrealloc (minisyms
,
1478 (symcount
+ synth_count
+ 1) * sizeof (*symp
));
1479 symp
= (asymbol
**) minisyms
+ symcount
;
1480 for (i
= 0; i
< synth_count
; i
++)
1481 *symp
++ = synthsyms
+ i
;
1483 symcount
+= synth_count
;
1485 if (!dynamic
&& dyn_syms
!= NULL
)
1489 /* lto_slim_object is set to false when a bfd is loaded with a compiler
1491 if (abfd
->lto_slim_object
)
1493 report_plugin_err
= false;
1494 non_fatal (_("%s: plugin needed to handle lto object"),
1495 bfd_get_filename (abfd
));
1498 /* Discard the symbols we don't want to print.
1499 It's OK to do this in place; we'll free the storage anyway
1500 (after printing). */
1502 symcount
= filter_symbols (abfd
, dynamic
, minisyms
, symcount
, size
);
1508 sort_dynamic
= dynamic
;
1509 sort_x
= bfd_make_empty_symbol (abfd
);
1510 sort_y
= bfd_make_empty_symbol (abfd
);
1511 if (sort_x
== NULL
|| sort_y
== NULL
)
1512 bfd_fatal (bfd_get_filename (abfd
));
1515 qsort (minisyms
, symcount
, size
,
1516 sorters
[sort_numerically
][reverse_sort
]);
1518 symcount
= sort_symbols_by_size (abfd
, dynamic
, minisyms
, symcount
,
1523 print_symbols (abfd
, dynamic
, minisyms
, symcount
, size
, archive_bfd
);
1525 print_size_symbols (abfd
, dynamic
, symsizes
, symcount
, archive_bfd
);
1533 /* Construct a formatting string for printing symbol values. */
1536 get_print_format (void)
1538 const char * padding
;
1539 if (print_format
== FORMAT_POSIX
|| print_format
== FORMAT_JUST_SYMBOLS
)
1541 /* POSIX compatible output does not have any padding. */
1544 else if (print_width
== 32)
1548 else /* print_width == 64 */
1553 const char * length
= "l";
1554 if (print_width
== 64)
1556 #if BFD_HOST_64BIT_LONG
1558 #elif BFD_HOST_64BIT_LONG_LONG
1567 const char * radix
= NULL
;
1568 switch (print_radix
)
1570 case 8: radix
= "o"; break;
1571 case 10: radix
= "d"; break;
1572 case 16: radix
= "x"; break;
1575 return concat ("%", padding
, length
, radix
, NULL
);
1579 set_print_width (bfd
*file
)
1581 print_width
= bfd_get_arch_size (file
);
1583 if (print_width
== -1)
1586 Guess the target's bitsize based on its name.
1587 We assume here than any 64-bit format will include
1588 "64" somewhere in its name. The only known exception
1589 is the MMO object file format. */
1590 if (strstr (bfd_get_target (file
), "64") != NULL
1591 || strcmp (bfd_get_target (file
), "mmo") == 0)
1596 free ((char *) print_format_string
);
1597 print_format_string
= get_print_format ();
1601 display_archive (bfd
*file
)
1604 bfd
*last_arfile
= NULL
;
1607 format
->print_archive_filename (bfd_get_filename (file
));
1610 print_symdef_entry (file
);
1616 arfile
= bfd_openr_next_archived_file (file
, arfile
);
1620 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1621 bfd_fatal (bfd_get_filename (file
));
1625 if (bfd_check_format_matches (arfile
, bfd_object
, &matching
))
1627 set_print_width (arfile
);
1628 format
->print_archive_member (bfd_get_filename (file
),
1629 bfd_get_filename (arfile
));
1630 display_rel_file (arfile
, file
);
1634 bfd_nonfatal (bfd_get_filename (arfile
));
1635 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1637 list_matching_formats (matching
);
1642 if (last_arfile
!= NULL
)
1644 bfd_close (last_arfile
);
1645 lineno_cache_bfd
= NULL
;
1646 lineno_cache_rel_bfd
= NULL
;
1647 if (arfile
== last_arfile
)
1650 last_arfile
= arfile
;
1653 if (last_arfile
!= NULL
)
1655 bfd_close (last_arfile
);
1656 lineno_cache_bfd
= NULL
;
1657 lineno_cache_rel_bfd
= NULL
;
1662 display_file (char *filename
)
1668 if (get_file_size (filename
) < 1)
1671 file
= bfd_openr (filename
, target
? target
: plugin_target
);
1674 bfd_nonfatal (filename
);
1678 /* If printing line numbers, decompress the debug sections. */
1680 file
->flags
|= BFD_DECOMPRESS
;
1682 if (bfd_check_format (file
, bfd_archive
))
1684 display_archive (file
);
1686 else if (bfd_check_format_matches (file
, bfd_object
, &matching
))
1688 set_print_width (file
);
1689 format
->print_object_filename (filename
);
1690 display_rel_file (file
, NULL
);
1694 bfd_nonfatal (filename
);
1695 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1697 list_matching_formats (matching
);
1703 if (!bfd_close (file
))
1704 bfd_fatal (filename
);
1706 lineno_cache_bfd
= NULL
;
1707 lineno_cache_rel_bfd
= NULL
;
1712 /* The following 3 groups of functions are called unconditionally,
1713 once at the start of processing each file of the appropriate type.
1714 They should check `filename_per_file' and `filename_per_symbol',
1715 as appropriate for their output format, to determine whether to
1718 /* Print the name of an object file given on the command line. */
1721 print_object_filename_bsd (const char *filename
)
1723 if (filename_per_file
&& !filename_per_symbol
)
1724 printf ("\n%s:\n", filename
);
1728 print_object_filename_sysv (const char *filename
)
1731 printf (_("\n\nUndefined symbols from %s:\n\n"), filename
);
1733 printf (_("\n\nSymbols from %s:\n\n"), filename
);
1734 if (print_width
== 32)
1736 Name Value Class Type Size Line Section\n\n"));
1739 Name Value Class Type Size Line Section\n\n"));
1743 print_object_filename_posix (const char *filename
)
1745 if (filename_per_file
&& !filename_per_symbol
)
1746 printf ("%s:\n", filename
);
1750 do_not_print_object_filename (const char *filename ATTRIBUTE_UNUSED
)
1754 /* Print the name of an archive file given on the command line. */
1757 print_archive_filename_bsd (const char *filename
)
1759 if (filename_per_file
)
1760 printf ("\n%s:\n", filename
);
1764 print_archive_filename_sysv (const char *filename ATTRIBUTE_UNUSED
)
1769 print_archive_filename_posix (const char *filename ATTRIBUTE_UNUSED
)
1774 do_not_print_archive_filename (const char *filename ATTRIBUTE_UNUSED
)
1778 /* Print the name of an archive member file. */
1781 print_archive_member_bsd (const char *archive ATTRIBUTE_UNUSED
,
1782 const char *filename
)
1784 if (!filename_per_symbol
)
1785 printf ("\n%s:\n", filename
);
1789 print_archive_member_sysv (const char *archive
, const char *filename
)
1792 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive
, filename
);
1794 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive
, filename
);
1795 if (print_width
== 32)
1797 Name Value Class Type Size Line Section\n\n"));
1800 Name Value Class Type Size Line Section\n\n"));
1804 print_archive_member_posix (const char *archive
, const char *filename
)
1806 if (!filename_per_symbol
)
1807 printf ("%s[%s]:\n", archive
, filename
);
1811 do_not_print_archive_member (const char *archive ATTRIBUTE_UNUSED
,
1812 const char *filename ATTRIBUTE_UNUSED
)
1817 /* Print the name of the file (and archive, if there is one)
1818 containing a symbol. */
1821 print_symbol_filename_bsd (bfd
*archive_bfd
, bfd
*abfd
)
1823 if (filename_per_symbol
)
1826 printf ("%s:", bfd_get_filename (archive_bfd
));
1827 printf ("%s:", bfd_get_filename (abfd
));
1832 print_symbol_filename_sysv (bfd
*archive_bfd
, bfd
*abfd
)
1834 if (filename_per_symbol
)
1837 printf ("%s:", bfd_get_filename (archive_bfd
));
1838 printf ("%s:", bfd_get_filename (abfd
));
1843 print_symbol_filename_posix (bfd
*archive_bfd
, bfd
*abfd
)
1845 if (filename_per_symbol
)
1848 printf ("%s[%s]: ", bfd_get_filename (archive_bfd
),
1849 bfd_get_filename (abfd
));
1851 printf ("%s: ", bfd_get_filename (abfd
));
1856 do_not_print_symbol_filename (bfd
*archive_bfd ATTRIBUTE_UNUSED
,
1857 bfd
*abfd ATTRIBUTE_UNUSED
)
1862 /* Print a symbol value. */
1865 print_value (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_vma val
)
1867 switch (print_width
)
1870 printf (print_format_string
, (unsigned long) val
);
1874 #if BFD_HOST_64BIT_LONG || BFD_HOST_64BIT_LONG_LONG
1875 printf (print_format_string
, val
);
1877 /* We have a 64 bit value to print, but the host is only 32 bit. */
1878 if (print_radix
== 16)
1879 bfd_fprintf_vma (abfd
, stdout
, val
);
1885 s
= buf
+ sizeof buf
;
1889 *--s
= (val
% print_radix
) + '0';
1892 while ((buf
+ sizeof buf
- 1) - s
< 16)
1900 fatal (_("Print width has not been initialized (%d)"), print_width
);
1905 /* Print a line of information about a symbol. */
1908 print_symbol_info_bsd (struct extended_symbol_info
*info
, bfd
*abfd
)
1910 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1912 if (print_width
== 64)
1918 /* Normally we print the value of the symbol. If we are printing the
1919 size or sorting by size then we print its size, except for the
1920 (weird) special case where both flags are defined, in which case we
1921 print both values. This conforms to documented behaviour. */
1922 if (sort_by_size
&& !print_size
)
1923 print_value (abfd
, SYM_SIZE (info
));
1925 print_value (abfd
, SYM_VALUE (info
));
1926 if (print_size
&& SYM_SIZE (info
))
1929 print_value (abfd
, SYM_SIZE (info
));
1933 printf (" %c", SYM_TYPE (info
));
1935 if (SYM_TYPE (info
) == '-')
1939 printf (other_format
, SYM_STAB_OTHER (info
));
1941 printf (desc_format
, SYM_STAB_DESC (info
));
1942 printf (" %5s", SYM_STAB_NAME (info
));
1944 print_symname (" %s", info
, NULL
, abfd
);
1948 print_symbol_info_sysv (struct extended_symbol_info
*info
, bfd
*abfd
)
1950 print_symname ("%-20s|", info
, NULL
, abfd
);
1952 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1954 if (print_width
== 32)
1960 print_value (abfd
, SYM_VALUE (info
));
1962 printf ("| %c |", SYM_TYPE (info
));
1964 if (SYM_TYPE (info
) == '-')
1967 printf ("%18s| ", SYM_STAB_NAME (info
)); /* (C) Type. */
1968 printf (desc_format
, SYM_STAB_DESC (info
)); /* Size. */
1969 printf ("| |"); /* Line, Section. */
1973 /* Type, Size, Line, Section */
1976 get_elf_symbol_type (ELF_ST_TYPE (info
->elfinfo
->internal_elf_sym
.st_info
)));
1977 else if (info
->coffinfo
)
1979 get_coff_symbol_type (&info
->coffinfo
->native
->u
.syment
));
1983 if (SYM_SIZE (info
))
1984 print_value (abfd
, SYM_SIZE (info
));
1987 if (print_width
== 32)
1994 printf("| |%s", info
->elfinfo
->symbol
.section
->name
);
1995 else if (info
->coffinfo
)
1996 printf("| |%s", info
->coffinfo
->symbol
.section
->name
);
2003 print_symbol_info_posix (struct extended_symbol_info
*info
, bfd
*abfd
)
2005 print_symname ("%s ", info
, NULL
, abfd
);
2006 printf ("%c ", SYM_TYPE (info
));
2008 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
2012 print_value (abfd
, SYM_VALUE (info
));
2014 if (SYM_SIZE (info
))
2015 print_value (abfd
, SYM_SIZE (info
));
2020 just_print_symbol_name (struct extended_symbol_info
*info
, bfd
*abfd
)
2022 print_symname ("%s", info
, NULL
, abfd
);
2026 main (int argc
, char **argv
)
2031 #ifdef HAVE_LC_MESSAGES
2032 setlocale (LC_MESSAGES
, "");
2034 setlocale (LC_CTYPE
, "");
2035 setlocale (LC_COLLATE
, "");
2036 bindtextdomain (PACKAGE
, LOCALEDIR
);
2037 textdomain (PACKAGE
);
2039 program_name
= *argv
;
2040 xmalloc_set_program_name (program_name
);
2041 bfd_set_error_program_name (program_name
);
2042 #if BFD_SUPPORTS_PLUGINS
2043 bfd_plugin_set_program_name (program_name
);
2046 START_PROGRESS (program_name
, 0);
2048 expandargv (&argc
, &argv
);
2050 if (bfd_init () != BFD_INIT_MAGIC
)
2051 fatal (_("fatal error: libbfd ABI mismatch"));
2052 set_default_bfd_target ();
2054 while ((c
= getopt_long (argc
, argv
, "aABCDef:gHhjJlnopPrSst:uU:vVvX:",
2055 long_options
, (int *) 0)) != EOF
)
2060 print_debug_syms
= 1;
2064 filename_per_symbol
= 1;
2066 case 'B': /* For MIPS compatibility. */
2067 set_output_format ("bsd");
2073 enum demangling_styles style
;
2075 style
= cplus_demangle_name_to_style (optarg
);
2076 if (style
== unknown_demangling
)
2077 fatal (_("unknown demangling style `%s'"),
2080 cplus_demangle_set_style (style
);
2083 case OPTION_RECURSE_LIMIT
:
2084 demangle_flags
&= ~ DMGL_NO_RECURSE_LIMIT
;
2086 case OPTION_NO_RECURSE_LIMIT
:
2087 demangle_flags
|= DMGL_NO_RECURSE_LIMIT
;
2096 /* Ignored for HP/UX compatibility. */
2099 set_output_format (optarg
);
2113 sort_numerically
= 1;
2118 sort_numerically
= 0;
2121 case OPTION_SIZE_SORT
:
2123 sort_numerically
= 0;
2127 set_output_format ("posix");
2130 set_output_format ("just-symbols");
2142 set_print_radix (optarg
);
2149 if (streq (optarg
, "default") || streq (optarg
, "d"))
2150 unicode_display
= unicode_default
;
2151 else if (streq (optarg
, "locale") || streq (optarg
, "l"))
2152 unicode_display
= unicode_locale
;
2153 else if (streq (optarg
, "escape") || streq (optarg
, "e"))
2154 unicode_display
= unicode_escape
;
2155 else if (streq (optarg
, "invalid") || streq (optarg
, "i"))
2156 unicode_display
= unicode_invalid
;
2157 else if (streq (optarg
, "hex") || streq (optarg
, "x"))
2158 unicode_display
= unicode_hex
;
2159 else if (streq (optarg
, "highlight") || streq (optarg
, "h"))
2160 unicode_display
= unicode_highlight
;
2162 fatal (_("invalid argument to -U/--unicode: %s"), optarg
);
2169 /* Ignored for (partial) AIX compatibility. On AIX, the
2170 argument has values 32, 64, or 32_64, and specifies that
2171 only 32-bit, only 64-bit, or both kinds of objects should
2172 be examined. The default is 32. So plain AIX nm on a
2173 library archive with both kinds of objects will ignore
2174 the 64-bit ones. For GNU nm, the default is and always
2175 has been -X 32_64, and other options are not supported. */
2176 if (strcmp (optarg
, "32_64") != 0)
2177 fatal (_("Only -X 32_64 is supported"));
2180 case OPTION_TARGET
: /* --target */
2184 case OPTION_PLUGIN
: /* --plugin */
2185 #if BFD_SUPPORTS_PLUGINS
2186 bfd_plugin_set_plugin (optarg
);
2188 fatal (_("sorry - this program has been built without plugin support\n"));
2192 case OPTION_IFUNC_CHARS
:
2193 ifunc_type_chars
= optarg
;
2196 case 0: /* A long option that just sets a flag. */
2205 print_version ("nm");
2207 if (sort_by_size
&& undefined_only
)
2209 non_fatal (_("Using the --size-sort and --undefined-only options together"));
2210 non_fatal (_("will produce no output, since undefined symbols have no size."));
2214 /* OK, all options now parsed. If no filename specified, do a.out. */
2216 return !display_file ("a.out");
2220 if (argc
- optind
> 1)
2221 filename_per_file
= 1;
2223 /* We were given several filenames to do. */
2224 while (optind
< argc
)
2227 if (!display_file (argv
[optind
++]))
2231 END_PROGRESS (program_name
);