1 /* nm.c -- Describe symbol table of a rel file.
2 Copyright (C) 1991-2023 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 const 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 const 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 non_weak
= 0; /* Ignore weak symbols. */
180 static int no_sort
= 0; /* Don't sort; print syms in order found. */
181 static int print_debug_syms
= 0;/* Print debugger-only symbols too. */
182 static int print_armap
= 0; /* Describe __.SYMDEF data in archive files. */
183 static int print_size
= 0; /* Print size of defined symbols. */
184 static int reverse_sort
= 0; /* Sort in downward(alpha or numeric) order. */
185 static int sort_numerically
= 0;/* Sort in numeric rather than alpha order. */
186 static int sort_by_size
= 0; /* Sort by size of symbol. */
187 static int undefined_only
= 0; /* Print undefined symbols only. */
188 static int dynamic
= 0; /* Print dynamic symbols. */
189 static int show_version
= 0; /* Show the version number. */
190 static int show_synthetic
= 0; /* Display synthesized symbols too. */
191 static int line_numbers
= 0; /* Print line numbers for symbols. */
192 static int allow_special_symbols
= 0; /* Allow special symbols. */
193 static int with_symbol_versions
= -1; /* Output symbol version information. */
194 static int quiet
= 0; /* Suppress "no symbols" diagnostic. */
196 /* The characters to use for global and local ifunc symbols. */
197 #if DEFAULT_F_FOR_IFUNC_SYMBOLS
198 static const char * ifunc_type_chars
= "Ff";
200 static const char * ifunc_type_chars
= NULL
;
203 static int demangle_flags
= DMGL_ANSI
| DMGL_PARAMS
;
205 /* When to print the names of files. Not mutually exclusive in SYSV format. */
206 static int filename_per_file
= 0; /* Once per file, on its own line. */
207 static int filename_per_symbol
= 0; /* Once per symbol, at start of line. */
209 static int print_width
= 0;
210 static int print_radix
= 16;
211 /* Print formats for printing stab info. */
212 static char other_format
[] = "%02x";
213 static char desc_format
[] = "%04x";
215 static char *target
= NULL
;
216 #if BFD_SUPPORTS_PLUGINS
217 static const char *plugin_target
= "plugin";
219 static const char *plugin_target
= NULL
;
222 /* Used to cache the line numbers for a BFD. */
223 static bfd
*lineno_cache_bfd
;
224 static bfd
*lineno_cache_rel_bfd
;
226 typedef enum unicode_display_type
234 } unicode_display_type
;
236 static unicode_display_type unicode_display
= unicode_default
;
238 enum long_option_values
243 OPTION_RECURSE_LIMIT
,
244 OPTION_NO_RECURSE_LIMIT
,
250 static struct option long_options
[] =
252 {"debug-syms", no_argument
, &print_debug_syms
, 1},
253 {"demangle", optional_argument
, 0, 'C'},
254 {"dynamic", no_argument
, &dynamic
, 1},
255 {"extern-only", no_argument
, &external_only
, 1},
256 {"format", required_argument
, 0, 'f'},
257 {"help", no_argument
, 0, 'h'},
258 {"ifunc-chars", required_argument
, 0, OPTION_IFUNC_CHARS
},
259 {"just-symbols", no_argument
, 0, 'j'},
260 {"line-numbers", no_argument
, 0, 'l'},
261 {"no-cplus", no_argument
, &do_demangle
, 0}, /* Linux compatibility. */
262 {"no-demangle", no_argument
, &do_demangle
, 0},
263 {"no-recurse-limit", no_argument
, NULL
, OPTION_NO_RECURSE_LIMIT
},
264 {"no-recursion-limit", no_argument
, NULL
, OPTION_NO_RECURSE_LIMIT
},
265 {"no-sort", no_argument
, 0, 'p'},
266 {"numeric-sort", no_argument
, 0, 'n'},
267 {"plugin", required_argument
, 0, OPTION_PLUGIN
},
268 {"portability", no_argument
, 0, 'P'},
269 {"print-armap", no_argument
, &print_armap
, 1},
270 {"print-file-name", no_argument
, 0, 'o'},
271 {"print-size", no_argument
, 0, 'S'},
272 {"quiet", no_argument
, 0, OPTION_QUIET
},
273 {"radix", required_argument
, 0, 't'},
274 {"recurse-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
275 {"recursion-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
276 {"reverse-sort", no_argument
, &reverse_sort
, 1},
277 {"size-sort", no_argument
, 0, OPTION_SIZE_SORT
},
278 {"special-syms", no_argument
, &allow_special_symbols
, 1},
279 {"synthetic", no_argument
, &show_synthetic
, 1},
280 {"target", required_argument
, 0, OPTION_TARGET
},
281 {"defined-only", no_argument
, 0, 'U'},
282 {"undefined-only", no_argument
, 0, 'u'},
283 {"unicode", required_argument
, NULL
, OPTION_UNICODE
},
284 {"version", no_argument
, &show_version
, 1},
285 {"no-weak", no_argument
, 0, 'W'},
286 {"with-symbol-versions", no_argument
, &with_symbol_versions
, 1},
287 {"without-symbol-versions", no_argument
, &with_symbol_versions
, 0},
288 {0, no_argument
, 0, 0}
291 /* Some error-reporting functions. */
293 ATTRIBUTE_NORETURN
static void
294 usage (FILE *stream
, int status
)
296 fprintf (stream
, _("Usage: %s [option(s)] [file(s)]\n"), program_name
);
297 fprintf (stream
, _(" List symbols in [file(s)] (a.out by default).\n"));
298 fprintf (stream
, _(" The options are:\n"));
299 fprintf (stream
, _("\
300 -a, --debug-syms Display debugger-only symbols\n"));
301 fprintf (stream
, _("\
302 -A, --print-file-name Print name of the input file before every symbol\n"));
303 fprintf (stream
, _("\
304 -B Same as --format=bsd\n"));
305 fprintf (stream
, _("\
306 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n"));
307 display_demangler_styles (stream
, _("\
309 fprintf (stream
, _("\
310 --no-demangle Do not demangle low-level symbol names\n"));
311 fprintf (stream
, _("\
312 --recurse-limit Enable a demangling recursion limit. (default)\n"));
313 fprintf (stream
, _("\
314 --no-recurse-limit Disable a demangling recursion limit.\n"));
315 fprintf (stream
, _("\
316 -D, --dynamic Display dynamic symbols instead of normal symbols\n"));
317 fprintf (stream
, _("\
319 fprintf (stream
, _("\
320 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
321 `sysv', `posix' or 'just-symbols'.\n\
322 The default is `bsd'\n"));
323 fprintf (stream
, _("\
324 -g, --extern-only Display only external symbols\n"));
325 fprintf (stream
, _("\
326 --ifunc-chars=CHARS Characters to use when displaying ifunc symbols\n"));
327 fprintf (stream
, _("\
328 -j, --just-symbols Same as --format=just-symbols\n"));
329 fprintf (stream
, _("\
330 -l, --line-numbers Use debugging information to find a filename and\n\
331 line number for each symbol\n"));
332 fprintf (stream
, _("\
333 -n, --numeric-sort Sort symbols numerically by address\n"));
334 fprintf (stream
, _("\
336 fprintf (stream
, _("\
337 -p, --no-sort Do not sort the symbols\n"));
338 fprintf (stream
, _("\
339 -P, --portability Same as --format=posix\n"));
340 fprintf (stream
, _("\
341 -r, --reverse-sort Reverse the sense of the sort\n"));
342 #if BFD_SUPPORTS_PLUGINS
343 fprintf (stream
, _("\
344 --plugin NAME Load the specified plugin\n"));
346 fprintf (stream
, _("\
347 -S, --print-size Print size of defined symbols\n"));
348 fprintf (stream
, _("\
349 -s, --print-armap Include index for symbols from archive members\n"));
350 fprintf (stream
, _("\
351 --quiet Suppress \"no symbols\" diagnostic\n"));
352 fprintf (stream
, _("\
353 --size-sort Sort symbols by size\n"));
354 fprintf (stream
, _("\
355 --special-syms Include special symbols in the output\n"));
356 fprintf (stream
, _("\
357 --synthetic Display synthetic symbols as well\n"));
358 fprintf (stream
, _("\
359 -t, --radix=RADIX Use RADIX for printing symbol values\n"));
360 fprintf (stream
, _("\
361 --target=BFDNAME Specify the target object format as BFDNAME\n"));
362 fprintf (stream
, _("\
363 -u, --undefined-only Display only undefined symbols\n"));
364 fprintf (stream
, _("\
365 -U, --defined-only Display only defined symbols\n"));
366 fprintf (stream
, _("\
367 --unicode={default|show|invalid|hex|escape|highlight}\n\
368 Specify how to treat UTF-8 encoded unicode characters\n"));
369 fprintf (stream
, _("\
370 -W, --no-weak Ignore weak symbols\n"));
371 fprintf (stream
, _("\
372 --with-symbol-versions Display version strings after symbol names\n"));
373 fprintf (stream
, _("\
374 -X 32_64 (ignored)\n"));
375 fprintf (stream
, _("\
376 @FILE Read options from FILE\n"));
377 fprintf (stream
, _("\
378 -h, --help Display this information\n"));
379 fprintf (stream
, _("\
380 -V, --version Display this program's version number\n"));
382 list_supported_targets (program_name
, stream
);
383 if (REPORT_BUGS_TO
[0] && status
== 0)
384 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
388 /* Set the radix for the symbol value and size according to RADIX. */
391 set_print_radix (char *radix
)
395 case 'x': print_radix
= 16; break;
396 case 'd': print_radix
= 10; break;
397 case 'o': print_radix
= 8; break;
400 fatal (_("%s: invalid radix"), radix
);
403 other_format
[3] = desc_format
[3] = *radix
;
407 set_output_format (char *f
)
427 i
= FORMAT_JUST_SYMBOLS
;
430 fatal (_("%s: invalid output format"), f
);
432 format
= &formats
[i
];
437 get_elf_symbol_type (unsigned int type
)
444 case STT_NOTYPE
: return "NOTYPE";
445 case STT_OBJECT
: return "OBJECT";
446 case STT_FUNC
: return "FUNC";
447 case STT_SECTION
: return "SECTION";
448 case STT_FILE
: return "FILE";
449 case STT_COMMON
: return "COMMON";
450 case STT_TLS
: return "TLS";
454 if (type
>= STT_LOPROC
&& type
<= STT_HIPROC
)
455 n
= asprintf (&bufp
, _("<processor specific>: %d"), type
);
456 else if (type
>= STT_LOOS
&& type
<= STT_HIOS
)
457 n
= asprintf (&bufp
, _("<OS specific>: %d"), type
);
459 n
= asprintf (&bufp
, _("<unknown>: %d"), type
);
461 fatal ("%s", xstrerror (errno
));
466 get_coff_symbol_type (const struct internal_syment
*sym
)
471 switch (sym
->n_sclass
)
473 case C_BLOCK
: return "Block";
474 case C_FILE
: return "File";
475 case C_LINE
: return "Line";
481 switch (DTYPE(sym
->n_type
))
483 case DT_FCN
: return "Function";
484 case DT_PTR
: return "Pointer";
485 case DT_ARY
: return "Array";
489 n
= asprintf (&bufp
, _("<unknown>: %d/%d"), sym
->n_sclass
, sym
->n_type
);
491 fatal ("%s", xstrerror (errno
));
495 /* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
496 The conversion format is controlled by the unicode_display variable.
497 Returns the number of characters added to OUT.
498 Returns the number of bytes consumed from IN in CONSUMED.
499 Always consumes at least one byte and displays at least one character. */
502 display_utf8 (const unsigned char * in
, char * out
, unsigned int * consumed
)
504 char * orig_out
= out
;
505 unsigned int nchars
= 0;
508 if (unicode_display
== unicode_default
)
514 if ((in
[1] & 0xc0) != 0x80)
517 if ((in
[0] & 0x20) == 0)
523 if ((in
[2] & 0xc0) != 0x80)
526 if ((in
[0] & 0x10) == 0)
532 if ((in
[3] & 0xc0) != 0x80)
538 switch (unicode_display
)
541 /* Copy the bytes into the output buffer as is. */
542 memcpy (out
, in
, nchars
);
546 case unicode_invalid
:
548 out
+= sprintf (out
, "%c", unicode_display
== unicode_hex
? '<' : '{');
549 out
+= sprintf (out
, "0x");
550 for (j
= 0; j
< nchars
; j
++)
551 out
+= sprintf (out
, "%02x", in
[j
]);
552 out
+= sprintf (out
, "%c", unicode_display
== unicode_hex
? '>' : '}');
555 case unicode_highlight
:
557 out
+= sprintf (out
, "\x1B[31;47m"); /* Red. */
563 out
+= sprintf (out
, "\\u%02x%02x",
564 ((in
[0] & 0x1c) >> 2),
565 ((in
[0] & 0x03) << 6) | (in
[1] & 0x3f));
569 out
+= sprintf (out
, "\\u%02x%02x",
570 ((in
[0] & 0x0f) << 4) | ((in
[1] & 0x3c) >> 2),
571 ((in
[1] & 0x03) << 6) | ((in
[2] & 0x3f)));
575 out
+= sprintf (out
, "\\u%02x%02x%02x",
576 ((in
[0] & 0x07) << 6) | ((in
[1] & 0x3c) >> 2),
577 ((in
[1] & 0x03) << 6) | ((in
[2] & 0x3c) >> 2),
578 ((in
[2] & 0x03) << 6) | ((in
[3] & 0x3f)));
585 if (unicode_display
== unicode_highlight
&& isatty (1))
586 out
+= sprintf (out
, "\033[0m"); /* Default colour. */
595 return out
- orig_out
;
598 /* Not a valid UTF-8 sequence. */
604 /* Convert any UTF-8 encoded characters in NAME into the form specified by
605 unicode_display. Also converts control characters. Returns a static
606 buffer if conversion was necessary.
607 Code stolen from objdump.c:sanitize_string(). */
610 convert_utf8 (const char * in
)
612 static char * buffer
= NULL
;
613 static size_t buffer_len
= 0;
614 const char * original
= in
;
621 /* See if any conversion is necessary.
622 In the majority of cases it will not be needed. */
625 unsigned char c
= *in
++;
633 if (unicode_display
!= unicode_default
&& c
>= 0xc0)
638 /* Copy the input, translating as needed. */
640 if (buffer_len
< (strlen (in
) * 9))
642 free ((void *) buffer
);
643 buffer_len
= strlen (in
) * 9;
644 buffer
= xmalloc (buffer_len
+ 1);
650 unsigned char c
= *in
++;
660 else if (unicode_display
!= unicode_default
&& c
>= 0xc0)
662 unsigned int num_consumed
;
664 out
+= display_utf8 ((const unsigned char *)(in
- 1), out
, & num_consumed
);
665 in
+= num_consumed
- 1;
676 /* Print symbol name NAME, read from ABFD, with printf format FORM,
677 demangling it if requested. */
680 print_symname (const char *form
, struct extended_symbol_info
*info
,
681 const char *name
, bfd
*abfd
)
687 name
= info
->sinfo
->name
;
689 if (!with_symbol_versions
690 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
692 atver
= strchr (name
, '@');
697 if (do_demangle
&& *name
)
699 alloc
= bfd_demangle (abfd
, name
, demangle_flags
);
704 if (unicode_display
!= unicode_default
)
706 name
= convert_utf8 (name
);
709 if (info
!= NULL
&& info
->elfinfo
&& with_symbol_versions
)
711 const char *version_string
;
715 = bfd_get_symbol_version_string (abfd
, &info
->elfinfo
->symbol
,
717 if (version_string
&& version_string
[0])
719 const char *at
= "@@";
720 if (hidden
|| bfd_is_und_section (info
->elfinfo
->symbol
.section
))
722 alloc
= reconcat (alloc
, name
, at
, version_string
, NULL
);
734 print_symdef_entry (bfd
*abfd
)
736 symindex idx
= BFD_NO_MORE_SYMBOLS
;
738 bool everprinted
= false;
740 for (idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
);
741 idx
!= BFD_NO_MORE_SYMBOLS
;
742 idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
))
747 printf (_("\nArchive index:\n"));
750 elt
= bfd_get_elt_at_index (abfd
, idx
);
752 bfd_fatal ("bfd_get_elt_at_index");
753 if (thesym
->name
!= (char *) NULL
)
755 print_symname ("%s", NULL
, thesym
->name
, abfd
);
756 printf (" in %s\n", bfd_get_filename (elt
));
762 /* True when we can report missing plugin error. */
763 bool report_plugin_err
= true;
765 /* Choose which symbol entries to print;
766 compact them downward to get rid of the rest.
767 Return the number of symbols to be printed. */
770 filter_symbols (bfd
*abfd
, bool is_dynamic
, void *minisyms
,
771 long symcount
, unsigned int size
)
773 bfd_byte
*from
, *fromend
, *to
;
776 store
= bfd_make_empty_symbol (abfd
);
778 bfd_fatal (bfd_get_filename (abfd
));
780 from
= (bfd_byte
*) minisyms
;
781 fromend
= from
+ symcount
* size
;
782 to
= (bfd_byte
*) minisyms
;
784 for (; from
< fromend
; from
+= size
)
791 sym
= bfd_minisymbol_to_symbol (abfd
, is_dynamic
, (const void *) from
, store
);
793 bfd_fatal (bfd_get_filename (abfd
));
795 if (sym
->name
!= NULL
796 && sym
->name
[0] == '_'
797 && sym
->name
[1] == '_'
798 && strcmp (sym
->name
+ (sym
->name
[2] == '_'), "__gnu_lto_slim") == 0
799 && report_plugin_err
)
801 report_plugin_err
= false;
802 non_fatal (_("%s: plugin needed to handle lto object"),
803 bfd_get_filename (abfd
));
807 keep
= bfd_is_und_section (sym
->section
);
808 else if (external_only
)
809 /* PR binutls/12753: Unique symbols are global too. */
810 keep
= ((sym
->flags
& (BSF_GLOBAL
812 | BSF_GNU_UNIQUE
)) != 0
813 || bfd_is_und_section (sym
->section
)
814 || bfd_is_com_section (sym
->section
));
816 keep
= ((sym
->flags
& BSF_WEAK
) == 0);
821 && ! print_debug_syms
822 && (sym
->flags
& BSF_DEBUGGING
) != 0)
827 && (bfd_is_abs_section (sym
->section
)
828 || bfd_is_und_section (sym
->section
)))
834 if (bfd_is_und_section (sym
->section
))
839 && bfd_is_target_special_symbol (abfd
, sym
)
840 && ! allow_special_symbols
)
846 memcpy (to
, from
, size
);
851 return (to
- (bfd_byte
*) minisyms
) / size
;
854 /* These globals are used to pass information into the sorting
856 static bfd
*sort_bfd
;
857 static bool sort_dynamic
;
858 static asymbol
*sort_x
;
859 static asymbol
*sort_y
;
861 /* Symbol-sorting predicates */
862 #define valueof(x) ((x)->section->vma + (x)->value)
864 /* Numeric sorts. Undefined symbols are always considered "less than"
865 defined symbols with zero values. Common symbols are not treated
866 specially -- i.e., their sizes are used as their "values". */
869 non_numeric_forward (const void *P_x
, const void *P_y
)
874 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
875 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
876 if (x
== NULL
|| y
== NULL
)
877 bfd_fatal (bfd_get_filename (sort_bfd
));
879 xn
= bfd_asymbol_name (x
);
880 yn
= bfd_asymbol_name (y
);
887 /* Solaris 2.5 has a bug in strcoll.
888 strcoll returns invalid values when confronted with empty strings. */
894 return strcoll (xn
, yn
);
898 non_numeric_reverse (const void *x
, const void *y
)
900 return - non_numeric_forward (x
, y
);
904 numeric_forward (const void *P_x
, const void *P_y
)
909 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
910 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
911 if (x
== NULL
|| y
== NULL
)
912 bfd_fatal (bfd_get_filename (sort_bfd
));
914 xs
= bfd_asymbol_section (x
);
915 ys
= bfd_asymbol_section (y
);
917 if (bfd_is_und_section (xs
))
919 if (! bfd_is_und_section (ys
))
922 else if (bfd_is_und_section (ys
))
924 else if (valueof (x
) != valueof (y
))
925 return valueof (x
) < valueof (y
) ? -1 : 1;
927 return non_numeric_forward (P_x
, P_y
);
931 numeric_reverse (const void *x
, const void *y
)
933 return - numeric_forward (x
, y
);
936 static int (*(sorters
[2][2])) (const void *, const void *) =
938 { non_numeric_forward
, non_numeric_reverse
},
939 { numeric_forward
, numeric_reverse
}
942 /* This sort routine is used by sort_symbols_by_size. It is similar
943 to numeric_forward, but when symbols have the same value it sorts
944 by section VMA. This simplifies the sort_symbols_by_size code
945 which handles symbols at the end of sections. Also, this routine
946 tries to sort file names before other symbols with the same value.
947 That will make the file name have a zero size, which will make
948 sort_symbols_by_size choose the non file name symbol, leading to
949 more meaningful output. For similar reasons, this code sorts
950 gnu_compiled_* and gcc2_compiled before other symbols with the same
954 size_forward1 (const void *P_x
, const void *P_y
)
962 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
963 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
964 if (x
== NULL
|| y
== NULL
)
965 bfd_fatal (bfd_get_filename (sort_bfd
));
967 xs
= bfd_asymbol_section (x
);
968 ys
= bfd_asymbol_section (y
);
970 if (bfd_is_und_section (xs
))
972 if (bfd_is_und_section (ys
))
975 if (valueof (x
) != valueof (y
))
976 return valueof (x
) < valueof (y
) ? -1 : 1;
978 if (xs
->vma
!= ys
->vma
)
979 return xs
->vma
< ys
->vma
? -1 : 1;
981 xn
= bfd_asymbol_name (x
);
982 yn
= bfd_asymbol_name (y
);
986 /* The symbols gnu_compiled and gcc2_compiled convey even less
987 information than the file name, so sort them out first. */
989 xf
= (strstr (xn
, "gnu_compiled") != NULL
990 || strstr (xn
, "gcc2_compiled") != NULL
);
991 yf
= (strstr (yn
, "gnu_compiled") != NULL
992 || strstr (yn
, "gcc2_compiled") != NULL
);
999 /* We use a heuristic for the file name. It may not work on non
1000 Unix systems, but it doesn't really matter; the only difference
1001 is precisely which symbol names get printed. */
1003 #define file_symbol(s, sn, snl) \
1004 (((s)->flags & BSF_FILE) != 0 \
1006 && (sn)[(snl) - 2] == '.' \
1007 && ((sn)[(snl) - 1] == 'o' \
1008 || (sn)[(snl) - 1] == 'a')))
1010 xf
= file_symbol (x
, xn
, xnl
);
1011 yf
= file_symbol (y
, yn
, ynl
);
1018 return non_numeric_forward (P_x
, P_y
);
1021 /* This sort routine is used by sort_symbols_by_size. It is sorting
1022 an array of size_sym structures into size order. */
1025 size_forward2 (const void *P_x
, const void *P_y
)
1027 const struct size_sym
*x
= (const struct size_sym
*) P_x
;
1028 const struct size_sym
*y
= (const struct size_sym
*) P_y
;
1030 if (x
->size
< y
->size
)
1031 return reverse_sort
? 1 : -1;
1032 else if (x
->size
> y
->size
)
1033 return reverse_sort
? -1 : 1;
1035 return sorters
[0][reverse_sort
] (x
->minisym
, y
->minisym
);
1038 /* Sort the symbols by size. ELF provides a size but for other formats
1039 we have to make a guess by assuming that the difference between the
1040 address of a symbol and the address of the next higher symbol is the
1044 sort_symbols_by_size (bfd
*abfd
, bool is_dynamic
, void *minisyms
,
1045 long symcount
, unsigned int size
,
1046 struct size_sym
**symsizesp
)
1048 struct size_sym
*symsizes
;
1049 bfd_byte
*from
, *fromend
;
1050 asymbol
*sym
= NULL
;
1051 asymbol
*store_sym
, *store_next
;
1053 qsort (minisyms
, symcount
, size
, size_forward1
);
1055 /* We are going to return a special set of symbols and sizes to
1057 symsizes
= (struct size_sym
*) xmalloc (symcount
* sizeof (struct size_sym
));
1058 *symsizesp
= symsizes
;
1060 /* Note that filter_symbols has already removed all absolute and
1061 undefined symbols. Here we remove all symbols whose size winds
1063 from
= (bfd_byte
*) minisyms
;
1064 fromend
= from
+ symcount
* size
;
1067 store_next
= sort_y
;
1071 sym
= bfd_minisymbol_to_symbol (abfd
, is_dynamic
, (const void *) from
,
1074 bfd_fatal (bfd_get_filename (abfd
));
1077 for (; from
< fromend
; from
+= size
)
1084 if (from
+ size
< fromend
)
1086 next
= bfd_minisymbol_to_symbol (abfd
,
1088 (const void *) (from
+ size
),
1091 bfd_fatal (bfd_get_filename (abfd
));
1096 sec
= bfd_asymbol_section (sym
);
1098 /* Synthetic symbols don't have a full type set of data available, thus
1099 we can't rely on that information for the symbol size. Ditto for
1100 bfd/section.c:global_syms like *ABS*. */
1101 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0
1102 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1103 sz
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
1104 else if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0
1105 && bfd_is_com_section (sec
))
1109 if (from
+ size
< fromend
1110 && sec
== bfd_asymbol_section (next
))
1111 sz
= valueof (next
) - valueof (sym
);
1113 sz
= (bfd_section_vma (sec
)
1114 + bfd_section_size (sec
)
1120 symsizes
->minisym
= (const void *) from
;
1121 symsizes
->size
= sz
;
1128 store_sym
= store_next
;
1132 symcount
= symsizes
- *symsizesp
;
1134 /* We must now sort again by size. */
1135 qsort ((void *) *symsizesp
, symcount
, sizeof (struct size_sym
), size_forward2
);
1140 /* This function is used to get the relocs for a particular section.
1141 It is called via bfd_map_over_sections. */
1144 get_relocs (bfd
*abfd
, asection
*sec
, void *dataarg
)
1146 struct get_relocs_info
*data
= (struct get_relocs_info
*) dataarg
;
1150 if ((sec
->flags
& SEC_RELOC
) == 0)
1152 *data
->relocs
= NULL
;
1153 *data
->relcount
= 0;
1159 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1161 bfd_fatal (bfd_get_filename (abfd
));
1163 *data
->relocs
= (arelent
**) xmalloc (relsize
);
1164 *data
->relcount
= bfd_canonicalize_reloc (abfd
, sec
, *data
->relocs
,
1166 if (*data
->relcount
< 0)
1167 bfd_fatal (bfd_get_filename (abfd
));
1175 /* Print a single symbol. */
1178 print_symbol (bfd
* abfd
,
1183 symbol_info syminfo
;
1184 struct extended_symbol_info info
;
1188 format
->print_symbol_filename (archive_bfd
, abfd
);
1190 bfd_get_symbol_info (abfd
, sym
, &syminfo
);
1192 /* PR 22967 - Distinguish between local and global ifunc symbols. */
1193 if (syminfo
.type
== 'i'
1194 && sym
->flags
& BSF_GNU_INDIRECT_FUNCTION
)
1196 if (ifunc_type_chars
== NULL
|| ifunc_type_chars
[0] == 0)
1197 ; /* Change nothing. */
1198 else if (sym
->flags
& BSF_GLOBAL
)
1199 syminfo
.type
= ifunc_type_chars
[0];
1200 else if (ifunc_type_chars
[1] != 0)
1201 syminfo
.type
= ifunc_type_chars
[1];
1204 info
.sinfo
= &syminfo
;
1206 /* Synthetic symbols do not have a full symbol type set of data available.
1207 Nor do bfd/section.c:global_syms like *ABS*. */
1208 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) != 0)
1210 info
.elfinfo
= NULL
;
1211 info
.coffinfo
= NULL
;
1215 info
.elfinfo
= elf_symbol_from (sym
);
1216 info
.coffinfo
= coff_symbol_from (sym
);
1219 format
->print_symbol_info (&info
, abfd
);
1223 static asymbol
**syms
;
1224 static long symcount
;
1225 const char *filename
, *functionname
;
1226 unsigned int lineno
;
1228 /* We need to get the canonical symbols in order to call
1229 bfd_find_nearest_line. This is inefficient, but, then, you
1230 don't have to use --line-numbers. */
1231 if (abfd
!= lineno_cache_bfd
&& syms
!= NULL
)
1240 symsize
= bfd_get_symtab_upper_bound (abfd
);
1242 bfd_fatal (bfd_get_filename (abfd
));
1243 syms
= (asymbol
**) xmalloc (symsize
);
1244 symcount
= bfd_canonicalize_symtab (abfd
, syms
);
1246 bfd_fatal (bfd_get_filename (abfd
));
1247 lineno_cache_bfd
= abfd
;
1250 if (bfd_is_und_section (bfd_asymbol_section (sym
)))
1252 static asection
**secs
;
1253 static arelent
***relocs
;
1254 static long *relcount
;
1255 static unsigned int seccount
;
1257 const char *symname
;
1259 /* For an undefined symbol, we try to find a reloc for the
1260 symbol, and print the line number of the reloc. */
1261 if (abfd
!= lineno_cache_rel_bfd
&& relocs
!= NULL
)
1263 for (i
= 0; i
< seccount
; i
++)
1264 if (relocs
[i
] != NULL
)
1276 struct get_relocs_info rinfo
;
1278 seccount
= bfd_count_sections (abfd
);
1280 secs
= (asection
**) xmalloc (seccount
* sizeof *secs
);
1281 relocs
= (arelent
***) xmalloc (seccount
* sizeof *relocs
);
1282 relcount
= (long *) xmalloc (seccount
* sizeof *relcount
);
1285 rinfo
.relocs
= relocs
;
1286 rinfo
.relcount
= relcount
;
1288 bfd_map_over_sections (abfd
, get_relocs
, (void *) &rinfo
);
1289 lineno_cache_rel_bfd
= abfd
;
1292 symname
= bfd_asymbol_name (sym
);
1293 for (i
= 0; i
< seccount
; i
++)
1297 for (j
= 0; j
< relcount
[i
]; j
++)
1302 if (r
->sym_ptr_ptr
!= NULL
1303 && (*r
->sym_ptr_ptr
)->section
== sym
->section
1304 && (*r
->sym_ptr_ptr
)->value
== sym
->value
1306 bfd_asymbol_name (*r
->sym_ptr_ptr
)) == 0
1307 && bfd_find_nearest_line (abfd
, secs
[i
], syms
,
1308 r
->address
, &filename
,
1309 &functionname
, &lineno
)
1310 && filename
!= NULL
)
1312 /* We only print the first one we find. */
1313 printf ("\t%s:%u", filename
, lineno
);
1320 else if (bfd_asymbol_section (sym
)->owner
== abfd
)
1322 if ((bfd_find_line (abfd
, syms
, sym
, &filename
, &lineno
)
1323 || bfd_find_nearest_line (abfd
, bfd_asymbol_section (sym
),
1324 syms
, sym
->value
, &filename
,
1325 &functionname
, &lineno
))
1328 printf ("\t%s:%u", filename
, lineno
);
1335 /* Print the symbols when sorting by size. */
1338 print_size_symbols (bfd
*abfd
,
1340 struct size_sym
*symsizes
,
1345 struct size_sym
*from
;
1346 struct size_sym
*fromend
;
1348 store
= bfd_make_empty_symbol (abfd
);
1350 bfd_fatal (bfd_get_filename (abfd
));
1353 fromend
= from
+ symcount
;
1355 for (; from
< fromend
; from
++)
1359 sym
= bfd_minisymbol_to_symbol (abfd
, is_dynamic
, from
->minisym
, store
);
1361 bfd_fatal (bfd_get_filename (abfd
));
1363 print_symbol (abfd
, sym
, from
->size
, archive_bfd
);
1368 /* Print the symbols of ABFD that are held in MINISYMS.
1370 If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD.
1372 SYMCOUNT is the number of symbols in MINISYMS.
1374 SIZE is the size of a symbol in MINISYMS. */
1377 print_symbols (bfd
*abfd
,
1388 store
= bfd_make_empty_symbol (abfd
);
1390 bfd_fatal (bfd_get_filename (abfd
));
1392 from
= (bfd_byte
*) minisyms
;
1393 fromend
= from
+ symcount
* size
;
1395 for (; from
< fromend
; from
+= size
)
1399 sym
= bfd_minisymbol_to_symbol (abfd
, is_dynamic
, from
, store
);
1401 bfd_fatal (bfd_get_filename (abfd
));
1403 print_symbol (abfd
, sym
, (bfd_vma
) 0, archive_bfd
);
1407 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
1410 display_rel_file (bfd
*abfd
, bfd
*archive_bfd
)
1415 struct size_sym
*symsizes
;
1416 asymbol
*synthsyms
= NULL
;
1420 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
1423 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
1428 symcount
= bfd_read_minisymbols (abfd
, dynamic
, &minisyms
, &size
);
1431 if (dynamic
&& bfd_get_error () == bfd_error_no_symbols
)
1434 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
1438 bfd_fatal (bfd_get_filename (abfd
));
1444 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
1448 if (show_synthetic
&& size
== sizeof (asymbol
*))
1450 asymbol
**static_syms
= NULL
;
1451 asymbol
**dyn_syms
= NULL
;
1452 long static_count
= 0;
1458 dyn_count
= symcount
;
1459 dyn_syms
= (asymbol
**) minisyms
;
1463 long storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
1465 static_count
= symcount
;
1466 static_syms
= (asymbol
**) minisyms
;
1470 dyn_syms
= (asymbol
**) xmalloc (storage
);
1471 dyn_count
= bfd_canonicalize_dynamic_symtab (abfd
, dyn_syms
);
1473 bfd_fatal (bfd_get_filename (abfd
));
1477 synth_count
= bfd_get_synthetic_symtab (abfd
, static_count
, static_syms
,
1478 dyn_count
, dyn_syms
, &synthsyms
);
1479 if (synth_count
> 0)
1484 minisyms
= xrealloc (minisyms
,
1485 (symcount
+ synth_count
+ 1) * sizeof (*symp
));
1486 symp
= (asymbol
**) minisyms
+ symcount
;
1487 for (i
= 0; i
< synth_count
; i
++)
1488 *symp
++ = synthsyms
+ i
;
1490 symcount
+= synth_count
;
1492 if (!dynamic
&& dyn_syms
!= NULL
)
1496 /* lto_slim_object is set to false when a bfd is loaded with a compiler
1498 if (abfd
->lto_slim_object
)
1500 report_plugin_err
= false;
1501 non_fatal (_("%s: plugin needed to handle lto object"),
1502 bfd_get_filename (abfd
));
1505 /* Discard the symbols we don't want to print.
1506 It's OK to do this in place; we'll free the storage anyway
1507 (after printing). */
1509 symcount
= filter_symbols (abfd
, dynamic
, minisyms
, symcount
, size
);
1515 sort_dynamic
= dynamic
;
1516 sort_x
= bfd_make_empty_symbol (abfd
);
1517 sort_y
= bfd_make_empty_symbol (abfd
);
1518 if (sort_x
== NULL
|| sort_y
== NULL
)
1519 bfd_fatal (bfd_get_filename (abfd
));
1522 qsort (minisyms
, symcount
, size
,
1523 sorters
[sort_numerically
][reverse_sort
]);
1525 symcount
= sort_symbols_by_size (abfd
, dynamic
, minisyms
, symcount
,
1530 print_symbols (abfd
, dynamic
, minisyms
, symcount
, size
, archive_bfd
);
1532 print_size_symbols (abfd
, dynamic
, symsizes
, symcount
, archive_bfd
);
1540 /* Construct a formatting string for printing symbol values. */
1543 get_print_format (void)
1545 const char * padding
;
1546 if (print_format
== FORMAT_POSIX
|| print_format
== FORMAT_JUST_SYMBOLS
)
1548 /* POSIX compatible output does not have any padding. */
1551 else if (print_width
== 32)
1555 else /* print_width == 64 */
1560 const char * radix
= NULL
;
1561 switch (print_radix
)
1563 case 8: radix
= PRIo64
; break;
1564 case 10: radix
= PRId64
; break;
1565 case 16: radix
= PRIx64
; break;
1568 return concat ("%", padding
, radix
, NULL
);
1572 set_print_width (bfd
*file
)
1574 print_width
= bfd_get_arch_size (file
);
1576 if (print_width
== -1)
1579 Guess the target's bitsize based on its name.
1580 We assume here than any 64-bit format will include
1581 "64" somewhere in its name. The only known exception
1582 is the MMO object file format. */
1583 if (strstr (bfd_get_target (file
), "64") != NULL
1584 || strcmp (bfd_get_target (file
), "mmo") == 0)
1589 free ((char *) print_format_string
);
1590 print_format_string
= get_print_format ();
1594 display_archive (bfd
*file
)
1597 bfd
*last_arfile
= NULL
;
1600 format
->print_archive_filename (bfd_get_filename (file
));
1603 print_symdef_entry (file
);
1609 arfile
= bfd_openr_next_archived_file (file
, arfile
);
1613 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1614 bfd_fatal (bfd_get_filename (file
));
1618 if (bfd_check_format_matches (arfile
, bfd_object
, &matching
))
1620 set_print_width (arfile
);
1621 format
->print_archive_member (bfd_get_filename (file
),
1622 bfd_get_filename (arfile
));
1623 display_rel_file (arfile
, file
);
1627 bfd_nonfatal (bfd_get_filename (arfile
));
1628 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1629 list_matching_formats (matching
);
1632 if (last_arfile
!= NULL
)
1634 bfd_close (last_arfile
);
1635 lineno_cache_bfd
= NULL
;
1636 lineno_cache_rel_bfd
= NULL
;
1637 if (arfile
== last_arfile
)
1640 last_arfile
= arfile
;
1643 if (last_arfile
!= NULL
)
1645 bfd_close (last_arfile
);
1646 lineno_cache_bfd
= NULL
;
1647 lineno_cache_rel_bfd
= NULL
;
1652 display_file (char *filename
)
1658 if (get_file_size (filename
) < 1)
1661 file
= bfd_openr (filename
, target
? target
: plugin_target
);
1664 bfd_nonfatal (filename
);
1668 /* If printing line numbers, decompress the debug sections. */
1670 file
->flags
|= BFD_DECOMPRESS
;
1672 if (bfd_check_format (file
, bfd_archive
))
1674 display_archive (file
);
1676 else if (bfd_check_format_matches (file
, bfd_object
, &matching
))
1678 set_print_width (file
);
1679 format
->print_object_filename (filename
);
1680 display_rel_file (file
, NULL
);
1684 bfd_nonfatal (filename
);
1685 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1686 list_matching_formats (matching
);
1690 if (!bfd_close (file
))
1691 bfd_fatal (filename
);
1693 lineno_cache_bfd
= NULL
;
1694 lineno_cache_rel_bfd
= NULL
;
1699 /* The following 3 groups of functions are called unconditionally,
1700 once at the start of processing each file of the appropriate type.
1701 They should check `filename_per_file' and `filename_per_symbol',
1702 as appropriate for their output format, to determine whether to
1705 /* Print the name of an object file given on the command line. */
1708 print_object_filename_bsd (const char *filename
)
1710 if (filename_per_file
&& !filename_per_symbol
)
1711 printf ("\n%s:\n", filename
);
1715 print_object_filename_sysv (const char *filename
)
1718 printf (_("\n\nUndefined symbols from %s:\n\n"), filename
);
1720 printf (_("\n\nSymbols from %s:\n\n"), filename
);
1721 if (print_width
== 32)
1723 Name Value Class Type Size Line Section\n\n"));
1726 Name Value Class Type Size Line Section\n\n"));
1730 print_object_filename_posix (const char *filename
)
1732 if (filename_per_file
&& !filename_per_symbol
)
1733 printf ("%s:\n", filename
);
1737 do_not_print_object_filename (const char *filename ATTRIBUTE_UNUSED
)
1741 /* Print the name of an archive file given on the command line. */
1744 print_archive_filename_bsd (const char *filename
)
1746 if (filename_per_file
)
1747 printf ("\n%s:\n", filename
);
1751 print_archive_filename_sysv (const char *filename ATTRIBUTE_UNUSED
)
1756 print_archive_filename_posix (const char *filename ATTRIBUTE_UNUSED
)
1761 do_not_print_archive_filename (const char *filename ATTRIBUTE_UNUSED
)
1765 /* Print the name of an archive member file. */
1768 print_archive_member_bsd (const char *archive ATTRIBUTE_UNUSED
,
1769 const char *filename
)
1771 if (!filename_per_symbol
)
1772 printf ("\n%s:\n", filename
);
1776 print_archive_member_sysv (const char *archive
, const char *filename
)
1779 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive
, filename
);
1781 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive
, filename
);
1782 if (print_width
== 32)
1784 Name Value Class Type Size Line Section\n\n"));
1787 Name Value Class Type Size Line Section\n\n"));
1791 print_archive_member_posix (const char *archive
, const char *filename
)
1793 if (!filename_per_symbol
)
1794 printf ("%s[%s]:\n", archive
, filename
);
1798 do_not_print_archive_member (const char *archive ATTRIBUTE_UNUSED
,
1799 const char *filename ATTRIBUTE_UNUSED
)
1804 /* Print the name of the file (and archive, if there is one)
1805 containing a symbol. */
1808 print_symbol_filename_bsd (bfd
*archive_bfd
, bfd
*abfd
)
1810 if (filename_per_symbol
)
1813 printf ("%s:", bfd_get_filename (archive_bfd
));
1814 printf ("%s:", bfd_get_filename (abfd
));
1819 print_symbol_filename_sysv (bfd
*archive_bfd
, bfd
*abfd
)
1821 if (filename_per_symbol
)
1824 printf ("%s:", bfd_get_filename (archive_bfd
));
1825 printf ("%s:", bfd_get_filename (abfd
));
1830 print_symbol_filename_posix (bfd
*archive_bfd
, bfd
*abfd
)
1832 if (filename_per_symbol
)
1835 printf ("%s[%s]: ", bfd_get_filename (archive_bfd
),
1836 bfd_get_filename (abfd
));
1838 printf ("%s: ", bfd_get_filename (abfd
));
1843 do_not_print_symbol_filename (bfd
*archive_bfd ATTRIBUTE_UNUSED
,
1844 bfd
*abfd ATTRIBUTE_UNUSED
)
1849 /* Print a symbol value. */
1852 print_value (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_vma val
)
1854 switch (print_width
)
1858 printf (print_format_string
, (uint64_t) val
);
1862 fatal (_("Print width has not been initialized (%d)"), print_width
);
1867 /* Print a line of information about a symbol. */
1870 print_symbol_info_bsd (struct extended_symbol_info
*info
, bfd
*abfd
)
1872 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1874 if (print_width
== 64)
1880 /* Normally we print the value of the symbol. If we are printing the
1881 size or sorting by size then we print its size, except for the
1882 (weird) special case where both flags are defined, in which case we
1883 print both values. This conforms to documented behaviour. */
1884 if (sort_by_size
&& !print_size
)
1885 print_value (abfd
, SYM_SIZE (info
));
1887 print_value (abfd
, SYM_VALUE (info
));
1888 if (print_size
&& SYM_SIZE (info
))
1891 print_value (abfd
, SYM_SIZE (info
));
1895 printf (" %c", SYM_TYPE (info
));
1897 if (SYM_TYPE (info
) == '-')
1901 printf (other_format
, SYM_STAB_OTHER (info
));
1903 printf (desc_format
, SYM_STAB_DESC (info
));
1904 printf (" %5s", SYM_STAB_NAME (info
));
1906 print_symname (" %s", info
, NULL
, abfd
);
1910 print_symbol_info_sysv (struct extended_symbol_info
*info
, bfd
*abfd
)
1912 print_symname ("%-20s|", info
, NULL
, abfd
);
1914 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1916 if (print_width
== 32)
1922 print_value (abfd
, SYM_VALUE (info
));
1924 printf ("| %c |", SYM_TYPE (info
));
1926 if (SYM_TYPE (info
) == '-')
1929 printf ("%18s| ", SYM_STAB_NAME (info
)); /* (C) Type. */
1930 printf (desc_format
, SYM_STAB_DESC (info
)); /* Size. */
1931 printf ("| |"); /* Line, Section. */
1935 /* Type, Size, Line, Section */
1938 get_elf_symbol_type (ELF_ST_TYPE (info
->elfinfo
->internal_elf_sym
.st_info
)));
1939 else if (info
->coffinfo
)
1941 get_coff_symbol_type (&info
->coffinfo
->native
->u
.syment
));
1945 if (SYM_SIZE (info
))
1946 print_value (abfd
, SYM_SIZE (info
));
1949 if (print_width
== 32)
1956 printf("| |%s", info
->elfinfo
->symbol
.section
->name
);
1957 else if (info
->coffinfo
)
1958 printf("| |%s", info
->coffinfo
->symbol
.section
->name
);
1965 print_symbol_info_posix (struct extended_symbol_info
*info
, bfd
*abfd
)
1967 print_symname ("%s ", info
, NULL
, abfd
);
1968 printf ("%c ", SYM_TYPE (info
));
1970 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1974 print_value (abfd
, SYM_VALUE (info
));
1976 if (SYM_SIZE (info
))
1977 print_value (abfd
, SYM_SIZE (info
));
1982 just_print_symbol_name (struct extended_symbol_info
*info
, bfd
*abfd
)
1984 print_symname ("%s", info
, NULL
, abfd
);
1988 main (int argc
, char **argv
)
1993 #ifdef HAVE_LC_MESSAGES
1994 setlocale (LC_MESSAGES
, "");
1996 setlocale (LC_CTYPE
, "");
1997 setlocale (LC_COLLATE
, "");
1998 bindtextdomain (PACKAGE
, LOCALEDIR
);
1999 textdomain (PACKAGE
);
2001 program_name
= *argv
;
2002 xmalloc_set_program_name (program_name
);
2003 bfd_set_error_program_name (program_name
);
2004 #if BFD_SUPPORTS_PLUGINS
2005 bfd_plugin_set_program_name (program_name
);
2008 START_PROGRESS (program_name
, 0);
2010 expandargv (&argc
, &argv
);
2012 if (bfd_init () != BFD_INIT_MAGIC
)
2013 fatal (_("fatal error: libbfd ABI mismatch"));
2014 set_default_bfd_target ();
2016 while ((c
= getopt_long (argc
, argv
, "aABCDef:gHhjJlnopPrSst:uU:vVvWX:",
2017 long_options
, (int *) 0)) != EOF
)
2022 print_debug_syms
= 1;
2026 filename_per_symbol
= 1;
2028 case 'B': /* For MIPS compatibility. */
2029 set_output_format ("bsd");
2035 enum demangling_styles style
;
2037 style
= cplus_demangle_name_to_style (optarg
);
2038 if (style
== unknown_demangling
)
2039 fatal (_("unknown demangling style `%s'"),
2042 cplus_demangle_set_style (style
);
2045 case OPTION_RECURSE_LIMIT
:
2046 demangle_flags
&= ~ DMGL_NO_RECURSE_LIMIT
;
2048 case OPTION_NO_RECURSE_LIMIT
:
2049 demangle_flags
|= DMGL_NO_RECURSE_LIMIT
;
2058 /* Ignored for HP/UX compatibility. */
2061 set_output_format (optarg
);
2075 sort_numerically
= 1;
2080 sort_numerically
= 0;
2083 case OPTION_SIZE_SORT
:
2085 sort_numerically
= 0;
2089 set_output_format ("posix");
2092 set_output_format ("just-symbols");
2104 set_print_radix (optarg
);
2115 case OPTION_UNICODE
:
2116 if (streq (optarg
, "default") || streq (optarg
, "d"))
2117 unicode_display
= unicode_default
;
2118 else if (streq (optarg
, "locale") || streq (optarg
, "l"))
2119 unicode_display
= unicode_locale
;
2120 else if (streq (optarg
, "escape") || streq (optarg
, "e"))
2121 unicode_display
= unicode_escape
;
2122 else if (streq (optarg
, "invalid") || streq (optarg
, "i"))
2123 unicode_display
= unicode_invalid
;
2124 else if (streq (optarg
, "hex") || streq (optarg
, "x"))
2125 unicode_display
= unicode_hex
;
2126 else if (streq (optarg
, "highlight") || streq (optarg
, "h"))
2127 unicode_display
= unicode_highlight
;
2129 fatal (_("invalid argument to -U/--unicode: %s"), optarg
);
2139 /* Ignored for (partial) AIX compatibility. On AIX, the
2140 argument has values 32, 64, or 32_64, and specifies that
2141 only 32-bit, only 64-bit, or both kinds of objects should
2142 be examined. The default is 32. So plain AIX nm on a
2143 library archive with both kinds of objects will ignore
2144 the 64-bit ones. For GNU nm, the default is and always
2145 has been -X 32_64, and other options are not supported. */
2146 if (strcmp (optarg
, "32_64") != 0)
2147 fatal (_("Only -X 32_64 is supported"));
2150 case OPTION_TARGET
: /* --target */
2154 case OPTION_PLUGIN
: /* --plugin */
2155 #if BFD_SUPPORTS_PLUGINS
2156 bfd_plugin_set_plugin (optarg
);
2158 fatal (_("sorry - this program has been built without plugin support\n"));
2162 case OPTION_IFUNC_CHARS
:
2163 ifunc_type_chars
= optarg
;
2166 case 0: /* A long option that just sets a flag. */
2175 print_version ("nm");
2177 if (sort_by_size
&& undefined_only
)
2179 non_fatal (_("Using the --size-sort and --undefined-only options together"));
2180 non_fatal (_("will produce no output, since undefined symbols have no size."));
2184 /* OK, all options now parsed. If no filename specified, do a.out. */
2186 return !display_file ("a.out");
2190 if (argc
- optind
> 1)
2191 filename_per_file
= 1;
2193 /* We were given several filenames to do. */
2194 while (optind
< argc
)
2197 if (!display_file (argv
[optind
++]))
2201 END_PROGRESS (program_name
);