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
24 #include "aout/stab_gnu.h"
25 #include "aout/ranlib.h"
27 #include "libiberty.h"
29 #include "elf/common.h"
30 #define DO_NOT_DEFINE_AOUTHDR
31 #define DO_NOT_DEFINE_FILHDR
32 #define DO_NOT_DEFINE_LINENO
33 #define DO_NOT_DEFINE_SCNHDR
34 #include "coff/external.h"
35 #include "coff/internal.h"
38 #include "demanguse.h"
39 #include "plugin-api.h"
41 #include "safe-ctype.h"
44 #define streq(a,b) (strcmp ((a),(b)) == 0)
47 /* When sorting by size, we use this structure to hold the size and a
48 pointer to the minisymbol. */
56 /* line number related info cached in bfd usrdata. */
65 unsigned int seccount
;
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 typedef enum unicode_display_type
230 } unicode_display_type
;
232 static unicode_display_type unicode_display
= unicode_default
;
234 enum long_option_values
239 OPTION_RECURSE_LIMIT
,
240 OPTION_NO_RECURSE_LIMIT
,
246 static struct option long_options
[] =
248 {"debug-syms", no_argument
, &print_debug_syms
, 1},
249 {"demangle", optional_argument
, 0, 'C'},
250 {"dynamic", no_argument
, &dynamic
, 1},
251 {"extern-only", no_argument
, &external_only
, 1},
252 {"format", required_argument
, 0, 'f'},
253 {"help", no_argument
, 0, 'h'},
254 {"ifunc-chars", required_argument
, 0, OPTION_IFUNC_CHARS
},
255 {"just-symbols", no_argument
, 0, 'j'},
256 {"line-numbers", no_argument
, 0, 'l'},
257 {"no-cplus", no_argument
, &do_demangle
, 0}, /* Linux compatibility. */
258 {"no-demangle", no_argument
, &do_demangle
, 0},
259 {"no-recurse-limit", no_argument
, NULL
, OPTION_NO_RECURSE_LIMIT
},
260 {"no-recursion-limit", no_argument
, NULL
, OPTION_NO_RECURSE_LIMIT
},
261 {"no-sort", no_argument
, 0, 'p'},
262 {"numeric-sort", no_argument
, 0, 'n'},
263 {"plugin", required_argument
, 0, OPTION_PLUGIN
},
264 {"portability", no_argument
, 0, 'P'},
265 {"print-armap", no_argument
, &print_armap
, 1},
266 {"print-file-name", no_argument
, 0, 'o'},
267 {"print-size", no_argument
, 0, 'S'},
268 {"quiet", no_argument
, 0, OPTION_QUIET
},
269 {"radix", required_argument
, 0, 't'},
270 {"recurse-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
271 {"recursion-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
272 {"reverse-sort", no_argument
, &reverse_sort
, 1},
273 {"size-sort", no_argument
, 0, OPTION_SIZE_SORT
},
274 {"special-syms", no_argument
, &allow_special_symbols
, 1},
275 {"synthetic", no_argument
, &show_synthetic
, 1},
276 {"target", required_argument
, 0, OPTION_TARGET
},
277 {"defined-only", no_argument
, 0, 'U'},
278 {"undefined-only", no_argument
, 0, 'u'},
279 {"unicode", required_argument
, NULL
, OPTION_UNICODE
},
280 {"version", no_argument
, &show_version
, 1},
281 {"no-weak", no_argument
, 0, 'W'},
282 {"with-symbol-versions", no_argument
, &with_symbol_versions
, 1},
283 {"without-symbol-versions", no_argument
, &with_symbol_versions
, 0},
284 {0, no_argument
, 0, 0}
287 /* Some error-reporting functions. */
289 ATTRIBUTE_NORETURN
static void
290 usage (FILE *stream
, int status
)
292 fprintf (stream
, _("Usage: %s [option(s)] [file(s)]\n"), program_name
);
293 fprintf (stream
, _(" List symbols in [file(s)] (a.out by default).\n"));
294 fprintf (stream
, _(" The options are:\n"));
295 fprintf (stream
, _("\
296 -a, --debug-syms Display debugger-only symbols\n"));
297 fprintf (stream
, _("\
298 -A, --print-file-name Print name of the input file before every symbol\n"));
299 fprintf (stream
, _("\
300 -B Same as --format=bsd\n"));
301 fprintf (stream
, _("\
302 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n"));
303 display_demangler_styles (stream
, _("\
305 fprintf (stream
, _("\
306 --no-demangle Do not demangle low-level symbol names\n"));
307 fprintf (stream
, _("\
308 --recurse-limit Enable a demangling recursion limit. (default)\n"));
309 fprintf (stream
, _("\
310 --no-recurse-limit Disable a demangling recursion limit.\n"));
311 fprintf (stream
, _("\
312 -D, --dynamic Display dynamic symbols instead of normal symbols\n"));
313 fprintf (stream
, _("\
315 fprintf (stream
, _("\
316 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
317 `sysv', `posix' or 'just-symbols'.\n\
318 The default is `bsd'\n"));
319 fprintf (stream
, _("\
320 -g, --extern-only Display only external symbols\n"));
321 fprintf (stream
, _("\
322 --ifunc-chars=CHARS Characters to use when displaying ifunc symbols\n"));
323 fprintf (stream
, _("\
324 -j, --just-symbols Same as --format=just-symbols\n"));
325 fprintf (stream
, _("\
326 -l, --line-numbers Use debugging information to find a filename and\n\
327 line number for each symbol\n"));
328 fprintf (stream
, _("\
329 -n, --numeric-sort Sort symbols numerically by address\n"));
330 fprintf (stream
, _("\
332 fprintf (stream
, _("\
333 -p, --no-sort Do not sort the symbols\n"));
334 fprintf (stream
, _("\
335 -P, --portability Same as --format=posix\n"));
336 fprintf (stream
, _("\
337 -r, --reverse-sort Reverse the sense of the sort\n"));
338 #if BFD_SUPPORTS_PLUGINS
339 fprintf (stream
, _("\
340 --plugin NAME Load the specified plugin\n"));
342 fprintf (stream
, _("\
343 -S, --print-size Print size of defined symbols\n"));
344 fprintf (stream
, _("\
345 -s, --print-armap Include index for symbols from archive members\n"));
346 fprintf (stream
, _("\
347 --quiet Suppress \"no symbols\" diagnostic\n"));
348 fprintf (stream
, _("\
349 --size-sort Sort symbols by size\n"));
350 fprintf (stream
, _("\
351 --special-syms Include special symbols in the output\n"));
352 fprintf (stream
, _("\
353 --synthetic Display synthetic symbols as well\n"));
354 fprintf (stream
, _("\
355 -t, --radix=RADIX Use RADIX for printing symbol values\n"));
356 fprintf (stream
, _("\
357 --target=BFDNAME Specify the target object format as BFDNAME\n"));
358 fprintf (stream
, _("\
359 -u, --undefined-only Display only undefined symbols\n"));
360 fprintf (stream
, _("\
361 -U, --defined-only Display only defined symbols\n"));
362 fprintf (stream
, _("\
363 --unicode={default|show|invalid|hex|escape|highlight}\n\
364 Specify how to treat UTF-8 encoded unicode characters\n"));
365 fprintf (stream
, _("\
366 -W, --no-weak Ignore weak symbols\n"));
367 fprintf (stream
, _("\
368 --with-symbol-versions Display version strings after symbol names\n"));
369 fprintf (stream
, _("\
370 -X 32_64 (ignored)\n"));
371 fprintf (stream
, _("\
372 @FILE Read options from FILE\n"));
373 fprintf (stream
, _("\
374 -h, --help Display this information\n"));
375 fprintf (stream
, _("\
376 -V, --version Display this program's version number\n"));
378 list_supported_targets (program_name
, stream
);
379 if (REPORT_BUGS_TO
[0] && status
== 0)
380 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
384 /* Set the radix for the symbol value and size according to RADIX. */
387 set_print_radix (char *radix
)
391 case 'x': print_radix
= 16; break;
392 case 'd': print_radix
= 10; break;
393 case 'o': print_radix
= 8; break;
396 fatal (_("%s: invalid radix"), radix
);
399 other_format
[3] = desc_format
[3] = *radix
;
403 set_output_format (char *f
)
423 i
= FORMAT_JUST_SYMBOLS
;
426 fatal (_("%s: invalid output format"), f
);
428 format
= &formats
[i
];
433 get_elf_symbol_type (unsigned int type
)
440 case STT_NOTYPE
: return "NOTYPE";
441 case STT_OBJECT
: return "OBJECT";
442 case STT_FUNC
: return "FUNC";
443 case STT_SECTION
: return "SECTION";
444 case STT_FILE
: return "FILE";
445 case STT_COMMON
: return "COMMON";
446 case STT_TLS
: return "TLS";
450 if (type
>= STT_LOPROC
&& type
<= STT_HIPROC
)
451 n
= asprintf (&bufp
, _("<processor specific>: %d"), type
);
452 else if (type
>= STT_LOOS
&& type
<= STT_HIOS
)
453 n
= asprintf (&bufp
, _("<OS specific>: %d"), type
);
455 n
= asprintf (&bufp
, _("<unknown>: %d"), type
);
457 fatal ("%s", xstrerror (errno
));
462 get_coff_symbol_type (const struct internal_syment
*sym
)
467 switch (sym
->n_sclass
)
469 case C_BLOCK
: return "Block";
470 case C_FILE
: return "File";
471 case C_LINE
: return "Line";
477 switch (DTYPE(sym
->n_type
))
479 case DT_FCN
: return "Function";
480 case DT_PTR
: return "Pointer";
481 case DT_ARY
: return "Array";
485 n
= asprintf (&bufp
, _("<unknown>: %d/%d"), sym
->n_sclass
, sym
->n_type
);
487 fatal ("%s", xstrerror (errno
));
491 /* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
492 The conversion format is controlled by the unicode_display variable.
493 Returns the number of characters added to OUT.
494 Returns the number of bytes consumed from IN in CONSUMED.
495 Always consumes at least one byte and displays at least one character. */
498 display_utf8 (const unsigned char * in
, char * out
, unsigned int * consumed
)
500 char * orig_out
= out
;
501 unsigned int nchars
= 0;
504 if (unicode_display
== unicode_default
)
510 if ((in
[1] & 0xc0) != 0x80)
513 if ((in
[0] & 0x20) == 0)
519 if ((in
[2] & 0xc0) != 0x80)
522 if ((in
[0] & 0x10) == 0)
528 if ((in
[3] & 0xc0) != 0x80)
534 switch (unicode_display
)
537 /* Copy the bytes into the output buffer as is. */
538 memcpy (out
, in
, nchars
);
542 case unicode_invalid
:
544 out
+= sprintf (out
, "%c", unicode_display
== unicode_hex
? '<' : '{');
545 out
+= sprintf (out
, "0x");
546 for (j
= 0; j
< nchars
; j
++)
547 out
+= sprintf (out
, "%02x", in
[j
]);
548 out
+= sprintf (out
, "%c", unicode_display
== unicode_hex
? '>' : '}');
551 case unicode_highlight
:
553 out
+= sprintf (out
, "\x1B[31;47m"); /* Red. */
559 out
+= sprintf (out
, "\\u%02x%02x",
560 ((in
[0] & 0x1c) >> 2),
561 ((in
[0] & 0x03) << 6) | (in
[1] & 0x3f));
565 out
+= sprintf (out
, "\\u%02x%02x",
566 ((in
[0] & 0x0f) << 4) | ((in
[1] & 0x3c) >> 2),
567 ((in
[1] & 0x03) << 6) | ((in
[2] & 0x3f)));
571 out
+= sprintf (out
, "\\u%02x%02x%02x",
572 ((in
[0] & 0x07) << 6) | ((in
[1] & 0x3c) >> 2),
573 ((in
[1] & 0x03) << 6) | ((in
[2] & 0x3c) >> 2),
574 ((in
[2] & 0x03) << 6) | ((in
[3] & 0x3f)));
581 if (unicode_display
== unicode_highlight
&& isatty (1))
582 out
+= sprintf (out
, "\033[0m"); /* Default colour. */
591 return out
- orig_out
;
594 /* Not a valid UTF-8 sequence. */
600 /* Convert any UTF-8 encoded characters in NAME into the form specified by
601 unicode_display. Also converts control characters. Returns a static
602 buffer if conversion was necessary.
603 Code stolen from objdump.c:sanitize_string(). */
606 convert_utf8 (const char * in
)
608 static char * buffer
= NULL
;
609 static size_t buffer_len
= 0;
610 const char * original
= in
;
617 /* See if any conversion is necessary.
618 In the majority of cases it will not be needed. */
621 unsigned char c
= *in
++;
629 if (unicode_display
!= unicode_default
&& c
>= 0xc0)
634 /* Copy the input, translating as needed. */
636 if (buffer_len
< (strlen (in
) * 9))
638 free ((void *) buffer
);
639 buffer_len
= strlen (in
) * 9;
640 buffer
= xmalloc (buffer_len
+ 1);
646 unsigned char c
= *in
++;
656 else if (unicode_display
!= unicode_default
&& c
>= 0xc0)
658 unsigned int num_consumed
;
660 out
+= display_utf8 ((const unsigned char *)(in
- 1), out
, & num_consumed
);
661 in
+= num_consumed
- 1;
672 /* Print symbol name NAME, read from ABFD, with printf format FORM,
673 demangling it if requested. */
676 print_symname (const char *form
, struct extended_symbol_info
*info
,
677 const char *name
, bfd
*abfd
)
683 name
= info
->sinfo
->name
;
685 if (!with_symbol_versions
686 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
688 atver
= strchr (name
, '@');
693 if (do_demangle
&& *name
)
695 alloc
= bfd_demangle (abfd
, name
, demangle_flags
);
700 if (unicode_display
!= unicode_default
)
702 name
= convert_utf8 (name
);
705 if (info
!= NULL
&& info
->elfinfo
&& with_symbol_versions
)
707 const char *version_string
;
711 = bfd_get_symbol_version_string (abfd
, &info
->elfinfo
->symbol
,
713 if (version_string
&& version_string
[0])
715 const char *at
= "@@";
716 if (hidden
|| bfd_is_und_section (info
->elfinfo
->symbol
.section
))
718 alloc
= reconcat (alloc
, name
, at
, version_string
, NULL
);
730 print_symdef_entry (bfd
*abfd
)
732 symindex idx
= BFD_NO_MORE_SYMBOLS
;
734 bool everprinted
= false;
736 for (idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
);
737 idx
!= BFD_NO_MORE_SYMBOLS
;
738 idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
))
742 printf (_("\nArchive index:\n"));
745 if (thesym
->name
!= NULL
)
747 print_symname ("%s", NULL
, thesym
->name
, abfd
);
748 bfd
*elt
= bfd_get_elt_at_index (abfd
, idx
);
750 printf (" in %s\n", bfd_get_filename (elt
));
758 /* True when we can report missing plugin error. */
759 bool report_plugin_err
= true;
761 /* Choose which symbol entries to print;
762 compact them downward to get rid of the rest.
763 Return the number of symbols to be printed. */
766 filter_symbols (bfd
*abfd
, bool is_dynamic
, void *minisyms
,
767 long symcount
, unsigned int size
)
769 bfd_byte
*from
, *fromend
, *to
;
772 store
= bfd_make_empty_symbol (abfd
);
774 bfd_fatal (bfd_get_filename (abfd
));
776 from
= (bfd_byte
*) minisyms
;
777 fromend
= from
+ symcount
* size
;
778 to
= (bfd_byte
*) minisyms
;
780 for (; from
< fromend
; from
+= size
)
785 sym
= bfd_minisymbol_to_symbol (abfd
, is_dynamic
, from
, store
);
789 if (sym
->name
!= NULL
790 && sym
->name
[0] == '_'
791 && sym
->name
[1] == '_'
792 && strcmp (sym
->name
+ (sym
->name
[2] == '_'), "__gnu_lto_slim") == 0
793 && report_plugin_err
)
795 report_plugin_err
= false;
796 non_fatal (_("%s: plugin needed to handle lto object"),
797 bfd_get_filename (abfd
));
801 keep
= bfd_is_und_section (sym
->section
);
802 else if (external_only
)
803 /* PR binutls/12753: Unique symbols are global too. */
804 keep
= ((sym
->flags
& (BSF_GLOBAL
806 | BSF_GNU_UNIQUE
)) != 0
807 || bfd_is_und_section (sym
->section
)
808 || bfd_is_com_section (sym
->section
));
810 keep
= ((sym
->flags
& BSF_WEAK
) == 0);
815 && ! print_debug_syms
816 && (sym
->flags
& BSF_DEBUGGING
) != 0)
821 && (bfd_is_abs_section (sym
->section
)
822 || bfd_is_und_section (sym
->section
)))
828 if (bfd_is_und_section (sym
->section
))
833 && bfd_is_target_special_symbol (abfd
, sym
)
834 && ! allow_special_symbols
)
840 memcpy (to
, from
, size
);
845 return (to
- (bfd_byte
*) minisyms
) / size
;
848 /* These globals are used to pass information into the sorting
850 static bfd
*sort_bfd
;
851 static bool sort_dynamic
;
852 static asymbol
*sort_x
;
853 static asymbol
*sort_y
;
855 /* Symbol-sorting predicates */
856 #define valueof(x) ((x)->section->vma + (x)->value)
858 /* Numeric sorts. Undefined symbols are always considered "less than"
859 defined symbols with zero values. Common symbols are not treated
860 specially -- i.e., their sizes are used as their "values". */
863 non_numeric_forward (const void *P_x
, const void *P_y
)
868 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
869 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
870 if (x
== NULL
|| y
== NULL
)
871 bfd_fatal (bfd_get_filename (sort_bfd
));
873 xn
= bfd_asymbol_name (x
);
874 yn
= bfd_asymbol_name (y
);
881 /* Solaris 2.5 has a bug in strcoll.
882 strcoll returns invalid values when confronted with empty strings. */
888 return strcoll (xn
, yn
);
892 non_numeric_reverse (const void *x
, const void *y
)
894 return - non_numeric_forward (x
, y
);
898 numeric_forward (const void *P_x
, const void *P_y
)
903 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
904 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
905 if (x
== NULL
|| y
== NULL
)
906 bfd_fatal (bfd_get_filename (sort_bfd
));
908 xs
= bfd_asymbol_section (x
);
909 ys
= bfd_asymbol_section (y
);
911 if (bfd_is_und_section (xs
))
913 if (! bfd_is_und_section (ys
))
916 else if (bfd_is_und_section (ys
))
918 else if (valueof (x
) != valueof (y
))
919 return valueof (x
) < valueof (y
) ? -1 : 1;
921 return non_numeric_forward (P_x
, P_y
);
925 numeric_reverse (const void *x
, const void *y
)
927 return - numeric_forward (x
, y
);
930 static int (*(sorters
[2][2])) (const void *, const void *) =
932 { non_numeric_forward
, non_numeric_reverse
},
933 { numeric_forward
, numeric_reverse
}
936 /* This sort routine is used by sort_symbols_by_size. It is similar
937 to numeric_forward, but when symbols have the same value it sorts
938 by section VMA. This simplifies the sort_symbols_by_size code
939 which handles symbols at the end of sections. Also, this routine
940 tries to sort file names before other symbols with the same value.
941 That will make the file name have a zero size, which will make
942 sort_symbols_by_size choose the non file name symbol, leading to
943 more meaningful output. For similar reasons, this code sorts
944 gnu_compiled_* and gcc2_compiled before other symbols with the same
948 size_forward1 (const void *P_x
, const void *P_y
)
956 x
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_x
, sort_x
);
957 y
= bfd_minisymbol_to_symbol (sort_bfd
, sort_dynamic
, P_y
, sort_y
);
958 if (x
== NULL
|| y
== NULL
)
959 bfd_fatal (bfd_get_filename (sort_bfd
));
961 xs
= bfd_asymbol_section (x
);
962 ys
= bfd_asymbol_section (y
);
964 if (bfd_is_und_section (xs
))
966 if (bfd_is_und_section (ys
))
969 if (valueof (x
) != valueof (y
))
970 return valueof (x
) < valueof (y
) ? -1 : 1;
972 if (xs
->vma
!= ys
->vma
)
973 return xs
->vma
< ys
->vma
? -1 : 1;
975 xn
= bfd_asymbol_name (x
);
976 yn
= bfd_asymbol_name (y
);
980 /* The symbols gnu_compiled and gcc2_compiled convey even less
981 information than the file name, so sort them out first. */
983 xf
= (strstr (xn
, "gnu_compiled") != NULL
984 || strstr (xn
, "gcc2_compiled") != NULL
);
985 yf
= (strstr (yn
, "gnu_compiled") != NULL
986 || strstr (yn
, "gcc2_compiled") != NULL
);
993 /* We use a heuristic for the file name. It may not work on non
994 Unix systems, but it doesn't really matter; the only difference
995 is precisely which symbol names get printed. */
997 #define file_symbol(s, sn, snl) \
998 (((s)->flags & BSF_FILE) != 0 \
1000 && (sn)[(snl) - 2] == '.' \
1001 && ((sn)[(snl) - 1] == 'o' \
1002 || (sn)[(snl) - 1] == 'a')))
1004 xf
= file_symbol (x
, xn
, xnl
);
1005 yf
= file_symbol (y
, yn
, ynl
);
1012 return non_numeric_forward (P_x
, P_y
);
1015 /* This sort routine is used by sort_symbols_by_size. It is sorting
1016 an array of size_sym structures into size order. */
1019 size_forward2 (const void *P_x
, const void *P_y
)
1021 const struct size_sym
*x
= (const struct size_sym
*) P_x
;
1022 const struct size_sym
*y
= (const struct size_sym
*) P_y
;
1024 if (x
->size
< y
->size
)
1025 return reverse_sort
? 1 : -1;
1026 else if (x
->size
> y
->size
)
1027 return reverse_sort
? -1 : 1;
1029 return sorters
[0][reverse_sort
] (x
->minisym
, y
->minisym
);
1032 /* Sort the symbols by size. ELF provides a size but for other formats
1033 we have to make a guess by assuming that the difference between the
1034 address of a symbol and the address of the next higher symbol is the
1038 sort_symbols_by_size (bfd
*abfd
, bool is_dynamic
, void *minisyms
,
1039 long symcount
, unsigned int size
,
1040 struct size_sym
**symsizesp
)
1042 struct size_sym
*symsizes
;
1043 bfd_byte
*from
, *fromend
;
1044 asymbol
*sym
= NULL
;
1045 asymbol
*store_sym
, *store_next
;
1047 qsort (minisyms
, symcount
, size
, size_forward1
);
1049 /* We are going to return a special set of symbols and sizes to
1051 symsizes
= (struct size_sym
*) xmalloc (symcount
* sizeof (struct size_sym
));
1052 *symsizesp
= symsizes
;
1054 /* Note that filter_symbols has already removed all absolute and
1055 undefined symbols. Here we remove all symbols whose size winds
1057 from
= (bfd_byte
*) minisyms
;
1058 fromend
= from
+ symcount
* size
;
1061 store_next
= sort_y
;
1065 sym
= bfd_minisymbol_to_symbol (abfd
, is_dynamic
, (const void *) from
,
1068 bfd_fatal (bfd_get_filename (abfd
));
1071 for (; from
< fromend
; from
+= size
)
1078 if (from
+ size
< fromend
)
1080 next
= bfd_minisymbol_to_symbol (abfd
,
1082 (const void *) (from
+ size
),
1085 bfd_fatal (bfd_get_filename (abfd
));
1090 sec
= bfd_asymbol_section (sym
);
1092 /* Synthetic symbols don't have a full type set of data available, thus
1093 we can't rely on that information for the symbol size. Ditto for
1094 bfd/section.c:global_syms like *ABS*. */
1095 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0
1096 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1097 sz
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
1098 else if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0
1099 && bfd_is_com_section (sec
))
1103 if (from
+ size
< fromend
1104 && sec
== bfd_asymbol_section (next
))
1105 sz
= valueof (next
) - valueof (sym
);
1107 sz
= (bfd_section_vma (sec
)
1108 + bfd_section_size (sec
)
1114 symsizes
->minisym
= (const void *) from
;
1115 symsizes
->size
= sz
;
1122 store_sym
= store_next
;
1126 symcount
= symsizes
- *symsizesp
;
1128 /* We must now sort again by size. */
1129 qsort ((void *) *symsizesp
, symcount
, sizeof (struct size_sym
), size_forward2
);
1134 /* This function is used to get the relocs for a particular section.
1135 It is called via bfd_map_over_sections. */
1138 get_relocs (bfd
*abfd
, asection
*sec
, void *dataarg
)
1140 struct lineno_cache
*data
= (struct lineno_cache
*) dataarg
;
1143 *data
->relocs
= NULL
;
1144 *data
->relcount
= 0;
1146 if ((sec
->flags
& SEC_RELOC
) != 0)
1148 long relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
1151 *data
->relocs
= (arelent
**) xmalloc (relsize
);
1152 *data
->relcount
= bfd_canonicalize_reloc (abfd
, sec
, *data
->relocs
,
1163 free_lineno_cache (bfd
*abfd
)
1165 struct lineno_cache
*lc
= bfd_usrdata (abfd
);
1170 for (unsigned int i
= 0; i
< lc
->seccount
; i
++)
1171 free (lc
->relocs
[i
]);
1172 free (lc
->relcount
);
1177 bfd_set_usrdata (abfd
, NULL
);
1181 /* Print a single symbol. */
1184 print_symbol (bfd
* abfd
,
1189 symbol_info syminfo
;
1190 struct extended_symbol_info info
;
1192 format
->print_symbol_filename (archive_bfd
, abfd
);
1194 bfd_get_symbol_info (abfd
, sym
, &syminfo
);
1196 /* PR 22967 - Distinguish between local and global ifunc symbols. */
1197 if (syminfo
.type
== 'i'
1198 && sym
->flags
& BSF_GNU_INDIRECT_FUNCTION
)
1200 if (ifunc_type_chars
== NULL
|| ifunc_type_chars
[0] == 0)
1201 ; /* Change nothing. */
1202 else if (sym
->flags
& BSF_GLOBAL
)
1203 syminfo
.type
= ifunc_type_chars
[0];
1204 else if (ifunc_type_chars
[1] != 0)
1205 syminfo
.type
= ifunc_type_chars
[1];
1208 info
.sinfo
= &syminfo
;
1210 /* Synthetic symbols do not have a full symbol type set of data available.
1211 Nor do bfd/section.c:global_syms like *ABS*. */
1212 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) != 0)
1214 info
.elfinfo
= NULL
;
1215 info
.coffinfo
= NULL
;
1219 info
.elfinfo
= elf_symbol_from (sym
);
1220 info
.coffinfo
= coff_symbol_from (sym
);
1223 format
->print_symbol_info (&info
, abfd
);
1227 struct lineno_cache
*lc
= bfd_usrdata (abfd
);
1228 const char *filename
, *functionname
;
1229 unsigned int lineno
;
1231 /* We need to get the canonical symbols in order to call
1232 bfd_find_nearest_line. This is inefficient, but, then, you
1233 don't have to use --line-numbers. */
1236 lc
= xcalloc (1, sizeof (*lc
));
1237 bfd_set_usrdata (abfd
, lc
);
1239 if (lc
->syms
== NULL
&& lc
->symcount
== 0)
1241 long symsize
= bfd_get_symtab_upper_bound (abfd
);
1246 lc
->syms
= xmalloc (symsize
);
1247 lc
->symcount
= bfd_canonicalize_symtab (abfd
, lc
->syms
);
1251 if (lc
->symcount
<= 0)
1253 else if (bfd_is_und_section (bfd_asymbol_section (sym
)))
1256 const char *symname
;
1258 /* For an undefined symbol, we try to find a reloc for the
1259 symbol, and print the line number of the reloc. */
1260 if (lc
->relocs
== NULL
)
1262 unsigned int seccount
= bfd_count_sections (abfd
);
1263 lc
->seccount
= seccount
;
1264 lc
->secs
= xmalloc (seccount
* sizeof (*lc
->secs
));
1265 lc
->relocs
= xmalloc (seccount
* sizeof (*lc
->relocs
));
1266 lc
->relcount
= xmalloc (seccount
* sizeof (*lc
->relcount
));
1268 struct lineno_cache rinfo
= *lc
;
1269 bfd_map_over_sections (abfd
, get_relocs
, &rinfo
);
1272 symname
= bfd_asymbol_name (sym
);
1273 for (i
= 0; i
< lc
->seccount
; i
++)
1277 for (j
= 0; j
< lc
->relcount
[i
]; j
++)
1281 r
= lc
->relocs
[i
][j
];
1282 if (r
->sym_ptr_ptr
!= NULL
1283 && (*r
->sym_ptr_ptr
)->section
== sym
->section
1284 && (*r
->sym_ptr_ptr
)->value
== sym
->value
1286 bfd_asymbol_name (*r
->sym_ptr_ptr
)) == 0
1287 && bfd_find_nearest_line (abfd
, lc
->secs
[i
], lc
->syms
,
1288 r
->address
, &filename
,
1289 &functionname
, &lineno
)
1290 && filename
!= NULL
)
1292 /* We only print the first one we find. */
1293 printf ("\t%s:%u", filename
, lineno
);
1300 else if (bfd_asymbol_section (sym
)->owner
== abfd
)
1302 if ((bfd_find_line (abfd
, lc
->syms
, sym
, &filename
, &lineno
)
1303 || bfd_find_nearest_line (abfd
, bfd_asymbol_section (sym
),
1304 lc
->syms
, sym
->value
, &filename
,
1305 &functionname
, &lineno
))
1308 printf ("\t%s:%u", filename
, lineno
);
1315 /* Print the symbols when sorting by size. */
1318 print_size_symbols (bfd
*abfd
,
1320 struct size_sym
*symsizes
,
1325 struct size_sym
*from
;
1326 struct size_sym
*fromend
;
1328 store
= bfd_make_empty_symbol (abfd
);
1330 bfd_fatal (bfd_get_filename (abfd
));
1333 fromend
= from
+ symcount
;
1335 for (; from
< fromend
; from
++)
1339 sym
= bfd_minisymbol_to_symbol (abfd
, is_dynamic
, from
->minisym
, store
);
1341 bfd_fatal (bfd_get_filename (abfd
));
1343 print_symbol (abfd
, sym
, from
->size
, archive_bfd
);
1348 /* Print the symbols of ABFD that are held in MINISYMS.
1350 If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD.
1352 SYMCOUNT is the number of symbols in MINISYMS.
1354 SIZE is the size of a symbol in MINISYMS. */
1357 print_symbols (bfd
*abfd
,
1368 store
= bfd_make_empty_symbol (abfd
);
1370 bfd_fatal (bfd_get_filename (abfd
));
1372 from
= (bfd_byte
*) minisyms
;
1373 fromend
= from
+ symcount
* size
;
1375 for (; from
< fromend
; from
+= size
)
1379 sym
= bfd_minisymbol_to_symbol (abfd
, is_dynamic
, from
, store
);
1381 bfd_fatal (bfd_get_filename (abfd
));
1383 print_symbol (abfd
, sym
, (bfd_vma
) 0, archive_bfd
);
1387 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
1390 display_rel_file (bfd
*abfd
, bfd
*archive_bfd
)
1395 struct size_sym
*symsizes
;
1396 asymbol
*synthsyms
= NULL
;
1400 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
1403 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
1408 symcount
= bfd_read_minisymbols (abfd
, dynamic
, &minisyms
, &size
);
1412 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd
));
1416 if (show_synthetic
&& size
== sizeof (asymbol
*))
1418 asymbol
**static_syms
= NULL
;
1419 asymbol
**dyn_syms
= NULL
;
1420 long static_count
= 0;
1426 dyn_count
= symcount
;
1427 dyn_syms
= (asymbol
**) minisyms
;
1431 long storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
1433 static_count
= symcount
;
1434 static_syms
= (asymbol
**) minisyms
;
1438 dyn_syms
= (asymbol
**) xmalloc (storage
);
1439 dyn_count
= bfd_canonicalize_dynamic_symtab (abfd
, dyn_syms
);
1445 synth_count
= bfd_get_synthetic_symtab (abfd
, static_count
, static_syms
,
1446 dyn_count
, dyn_syms
, &synthsyms
);
1447 if (synth_count
> 0)
1452 minisyms
= xrealloc (minisyms
,
1453 (symcount
+ synth_count
+ 1) * sizeof (*symp
));
1454 symp
= (asymbol
**) minisyms
+ symcount
;
1455 for (i
= 0; i
< synth_count
; i
++)
1456 *symp
++ = synthsyms
+ i
;
1458 symcount
+= synth_count
;
1460 if (!dynamic
&& dyn_syms
!= NULL
)
1464 /* lto_slim_object is set to false when a bfd is loaded with a compiler
1466 if (abfd
->lto_slim_object
)
1468 report_plugin_err
= false;
1469 non_fatal (_("%s: plugin needed to handle lto object"),
1470 bfd_get_filename (abfd
));
1473 /* Discard the symbols we don't want to print.
1474 It's OK to do this in place; we'll free the storage anyway
1475 (after printing). */
1477 symcount
= filter_symbols (abfd
, dynamic
, minisyms
, symcount
, size
);
1483 sort_dynamic
= dynamic
;
1484 sort_x
= bfd_make_empty_symbol (abfd
);
1485 sort_y
= bfd_make_empty_symbol (abfd
);
1486 if (sort_x
== NULL
|| sort_y
== NULL
)
1487 bfd_fatal (bfd_get_filename (abfd
));
1490 qsort (minisyms
, symcount
, size
,
1491 sorters
[sort_numerically
][reverse_sort
]);
1493 symcount
= sort_symbols_by_size (abfd
, dynamic
, minisyms
, symcount
,
1498 print_symbols (abfd
, dynamic
, minisyms
, symcount
, size
, archive_bfd
);
1500 print_size_symbols (abfd
, dynamic
, symsizes
, symcount
, archive_bfd
);
1508 /* Construct a formatting string for printing symbol values. */
1511 get_print_format (void)
1513 const char * padding
;
1514 if (print_format
== FORMAT_POSIX
|| print_format
== FORMAT_JUST_SYMBOLS
)
1516 /* POSIX compatible output does not have any padding. */
1519 else if (print_width
== 32)
1523 else /* print_width == 64 */
1528 const char * radix
= NULL
;
1529 switch (print_radix
)
1531 case 8: radix
= PRIo64
; break;
1532 case 10: radix
= PRId64
; break;
1533 case 16: radix
= PRIx64
; break;
1536 return concat ("%", padding
, radix
, NULL
);
1540 set_print_width (bfd
*file
)
1542 print_width
= bfd_get_arch_size (file
);
1544 if (print_width
== -1)
1547 Guess the target's bitsize based on its name.
1548 We assume here than any 64-bit format will include
1549 "64" somewhere in its name. The only known exception
1550 is the MMO object file format. */
1551 if (strstr (bfd_get_target (file
), "64") != NULL
1552 || strcmp (bfd_get_target (file
), "mmo") == 0)
1557 free ((char *) print_format_string
);
1558 print_format_string
= get_print_format ();
1562 display_archive (bfd
*file
)
1565 bfd
*last_arfile
= NULL
;
1568 format
->print_archive_filename (bfd_get_filename (file
));
1571 print_symdef_entry (file
);
1575 arfile
= bfd_openr_next_archived_file (file
, arfile
);
1579 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1580 bfd_nonfatal (bfd_get_filename (file
));
1584 if (bfd_check_format_matches (arfile
, bfd_object
, &matching
))
1586 set_print_width (arfile
);
1587 format
->print_archive_member (bfd_get_filename (file
),
1588 bfd_get_filename (arfile
));
1589 display_rel_file (arfile
, file
);
1593 bfd_nonfatal (bfd_get_filename (arfile
));
1594 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1595 list_matching_formats (matching
);
1598 if (last_arfile
!= NULL
)
1600 free_lineno_cache (last_arfile
);
1601 bfd_close (last_arfile
);
1602 if (arfile
== last_arfile
)
1605 last_arfile
= arfile
;
1608 if (last_arfile
!= NULL
)
1610 free_lineno_cache (last_arfile
);
1611 bfd_close (last_arfile
);
1616 display_file (char *filename
)
1622 if (get_file_size (filename
) < 1)
1625 file
= bfd_openr (filename
, target
? target
: plugin_target
);
1628 bfd_nonfatal (filename
);
1632 /* If printing line numbers, decompress the debug sections. */
1634 file
->flags
|= BFD_DECOMPRESS
;
1636 if (bfd_check_format (file
, bfd_archive
))
1638 display_archive (file
);
1640 else if (bfd_check_format_matches (file
, bfd_object
, &matching
))
1642 set_print_width (file
);
1643 format
->print_object_filename (filename
);
1644 display_rel_file (file
, NULL
);
1648 bfd_nonfatal (filename
);
1649 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1650 list_matching_formats (matching
);
1654 free_lineno_cache (file
);
1655 if (!bfd_close (file
))
1661 /* The following 3 groups of functions are called unconditionally,
1662 once at the start of processing each file of the appropriate type.
1663 They should check `filename_per_file' and `filename_per_symbol',
1664 as appropriate for their output format, to determine whether to
1667 /* Print the name of an object file given on the command line. */
1670 print_object_filename_bsd (const char *filename
)
1672 if (filename_per_file
&& !filename_per_symbol
)
1673 printf ("\n%s:\n", filename
);
1677 print_object_filename_sysv (const char *filename
)
1680 printf (_("\n\nUndefined symbols from %s:\n\n"), filename
);
1682 printf (_("\n\nSymbols from %s:\n\n"), filename
);
1683 if (print_width
== 32)
1685 Name Value Class Type Size Line Section\n\n"));
1688 Name Value Class Type Size Line Section\n\n"));
1692 print_object_filename_posix (const char *filename
)
1694 if (filename_per_file
&& !filename_per_symbol
)
1695 printf ("%s:\n", filename
);
1699 do_not_print_object_filename (const char *filename ATTRIBUTE_UNUSED
)
1703 /* Print the name of an archive file given on the command line. */
1706 print_archive_filename_bsd (const char *filename
)
1708 if (filename_per_file
)
1709 printf ("\n%s:\n", filename
);
1713 print_archive_filename_sysv (const char *filename ATTRIBUTE_UNUSED
)
1718 print_archive_filename_posix (const char *filename ATTRIBUTE_UNUSED
)
1723 do_not_print_archive_filename (const char *filename ATTRIBUTE_UNUSED
)
1727 /* Print the name of an archive member file. */
1730 print_archive_member_bsd (const char *archive ATTRIBUTE_UNUSED
,
1731 const char *filename
)
1733 if (!filename_per_symbol
)
1734 printf ("\n%s:\n", filename
);
1738 print_archive_member_sysv (const char *archive
, const char *filename
)
1741 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive
, filename
);
1743 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive
, filename
);
1744 if (print_width
== 32)
1746 Name Value Class Type Size Line Section\n\n"));
1749 Name Value Class Type Size Line Section\n\n"));
1753 print_archive_member_posix (const char *archive
, const char *filename
)
1755 if (!filename_per_symbol
)
1756 printf ("%s[%s]:\n", archive
, filename
);
1760 do_not_print_archive_member (const char *archive ATTRIBUTE_UNUSED
,
1761 const char *filename ATTRIBUTE_UNUSED
)
1766 /* Print the name of the file (and archive, if there is one)
1767 containing a symbol. */
1770 print_symbol_filename_bsd (bfd
*archive_bfd
, bfd
*abfd
)
1772 if (filename_per_symbol
)
1775 printf ("%s:", bfd_get_filename (archive_bfd
));
1776 printf ("%s:", bfd_get_filename (abfd
));
1781 print_symbol_filename_sysv (bfd
*archive_bfd
, bfd
*abfd
)
1783 if (filename_per_symbol
)
1786 printf ("%s:", bfd_get_filename (archive_bfd
));
1787 printf ("%s:", bfd_get_filename (abfd
));
1792 print_symbol_filename_posix (bfd
*archive_bfd
, bfd
*abfd
)
1794 if (filename_per_symbol
)
1797 printf ("%s[%s]: ", bfd_get_filename (archive_bfd
),
1798 bfd_get_filename (abfd
));
1800 printf ("%s: ", bfd_get_filename (abfd
));
1805 do_not_print_symbol_filename (bfd
*archive_bfd ATTRIBUTE_UNUSED
,
1806 bfd
*abfd ATTRIBUTE_UNUSED
)
1811 /* Print a symbol value. */
1814 print_value (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_vma val
)
1816 switch (print_width
)
1820 printf (print_format_string
, (uint64_t) val
);
1824 fatal (_("Print width has not been initialized (%d)"), print_width
);
1829 /* Print a line of information about a symbol. */
1832 print_symbol_info_bsd (struct extended_symbol_info
*info
, bfd
*abfd
)
1834 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1836 if (print_width
== 64)
1842 /* Normally we print the value of the symbol. If we are printing the
1843 size or sorting by size then we print its size, except for the
1844 (weird) special case where both flags are defined, in which case we
1845 print both values. This conforms to documented behaviour. */
1846 if (sort_by_size
&& !print_size
)
1847 print_value (abfd
, SYM_SIZE (info
));
1849 print_value (abfd
, SYM_VALUE (info
));
1850 if (print_size
&& SYM_SIZE (info
))
1853 print_value (abfd
, SYM_SIZE (info
));
1857 printf (" %c", SYM_TYPE (info
));
1859 if (SYM_TYPE (info
) == '-')
1863 printf (other_format
, SYM_STAB_OTHER (info
));
1865 printf (desc_format
, SYM_STAB_DESC (info
));
1866 printf (" %5s", SYM_STAB_NAME (info
));
1868 print_symname (" %s", info
, NULL
, abfd
);
1872 print_symbol_info_sysv (struct extended_symbol_info
*info
, bfd
*abfd
)
1874 print_symname ("%-20s|", info
, NULL
, abfd
);
1876 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1878 if (print_width
== 32)
1884 print_value (abfd
, SYM_VALUE (info
));
1886 printf ("| %c |", SYM_TYPE (info
));
1888 if (SYM_TYPE (info
) == '-')
1891 printf ("%18s| ", SYM_STAB_NAME (info
)); /* (C) Type. */
1892 printf (desc_format
, SYM_STAB_DESC (info
)); /* Size. */
1893 printf ("| |"); /* Line, Section. */
1897 /* Type, Size, Line, Section */
1900 get_elf_symbol_type (ELF_ST_TYPE (info
->elfinfo
->internal_elf_sym
.st_info
)));
1901 else if (info
->coffinfo
)
1903 get_coff_symbol_type (&info
->coffinfo
->native
->u
.syment
));
1907 if (SYM_SIZE (info
))
1908 print_value (abfd
, SYM_SIZE (info
));
1911 if (print_width
== 32)
1918 printf("| |%s", info
->elfinfo
->symbol
.section
->name
);
1919 else if (info
->coffinfo
)
1920 printf("| |%s", info
->coffinfo
->symbol
.section
->name
);
1927 print_symbol_info_posix (struct extended_symbol_info
*info
, bfd
*abfd
)
1929 print_symname ("%s ", info
, NULL
, abfd
);
1930 printf ("%c ", SYM_TYPE (info
));
1932 if (bfd_is_undefined_symclass (SYM_TYPE (info
)))
1936 print_value (abfd
, SYM_VALUE (info
));
1938 if (SYM_SIZE (info
))
1939 print_value (abfd
, SYM_SIZE (info
));
1944 just_print_symbol_name (struct extended_symbol_info
*info
, bfd
*abfd
)
1946 print_symname ("%s", info
, NULL
, abfd
);
1950 main (int argc
, char **argv
)
1955 #ifdef HAVE_LC_MESSAGES
1956 setlocale (LC_MESSAGES
, "");
1958 setlocale (LC_CTYPE
, "");
1959 setlocale (LC_COLLATE
, "");
1960 bindtextdomain (PACKAGE
, LOCALEDIR
);
1961 textdomain (PACKAGE
);
1963 program_name
= *argv
;
1964 xmalloc_set_program_name (program_name
);
1965 bfd_set_error_program_name (program_name
);
1966 #if BFD_SUPPORTS_PLUGINS
1967 bfd_plugin_set_program_name (program_name
);
1970 expandargv (&argc
, &argv
);
1972 if (bfd_init () != BFD_INIT_MAGIC
)
1973 fatal (_("fatal error: libbfd ABI mismatch"));
1974 set_default_bfd_target ();
1976 while ((c
= getopt_long (argc
, argv
, "aABCDef:gHhjJlnopPrSst:uU:vVvWX:",
1977 long_options
, (int *) 0)) != EOF
)
1982 print_debug_syms
= 1;
1986 filename_per_symbol
= 1;
1988 case 'B': /* For MIPS compatibility. */
1989 set_output_format ("bsd");
1995 enum demangling_styles style
;
1997 style
= cplus_demangle_name_to_style (optarg
);
1998 if (style
== unknown_demangling
)
1999 fatal (_("unknown demangling style `%s'"),
2002 cplus_demangle_set_style (style
);
2005 case OPTION_RECURSE_LIMIT
:
2006 demangle_flags
&= ~ DMGL_NO_RECURSE_LIMIT
;
2008 case OPTION_NO_RECURSE_LIMIT
:
2009 demangle_flags
|= DMGL_NO_RECURSE_LIMIT
;
2018 /* Ignored for HP/UX compatibility. */
2021 set_output_format (optarg
);
2035 sort_numerically
= 1;
2040 sort_numerically
= 0;
2043 case OPTION_SIZE_SORT
:
2045 sort_numerically
= 0;
2049 set_output_format ("posix");
2052 set_output_format ("just-symbols");
2064 set_print_radix (optarg
);
2075 case OPTION_UNICODE
:
2076 if (streq (optarg
, "default") || streq (optarg
, "d"))
2077 unicode_display
= unicode_default
;
2078 else if (streq (optarg
, "locale") || streq (optarg
, "l"))
2079 unicode_display
= unicode_locale
;
2080 else if (streq (optarg
, "escape") || streq (optarg
, "e"))
2081 unicode_display
= unicode_escape
;
2082 else if (streq (optarg
, "invalid") || streq (optarg
, "i"))
2083 unicode_display
= unicode_invalid
;
2084 else if (streq (optarg
, "hex") || streq (optarg
, "x"))
2085 unicode_display
= unicode_hex
;
2086 else if (streq (optarg
, "highlight") || streq (optarg
, "h"))
2087 unicode_display
= unicode_highlight
;
2089 fatal (_("invalid argument to -U/--unicode: %s"), optarg
);
2099 /* Ignored for (partial) AIX compatibility. On AIX, the
2100 argument has values 32, 64, or 32_64, and specifies that
2101 only 32-bit, only 64-bit, or both kinds of objects should
2102 be examined. The default is 32. So plain AIX nm on a
2103 library archive with both kinds of objects will ignore
2104 the 64-bit ones. For GNU nm, the default is and always
2105 has been -X 32_64, and other options are not supported. */
2106 if (strcmp (optarg
, "32_64") != 0)
2107 fatal (_("Only -X 32_64 is supported"));
2110 case OPTION_TARGET
: /* --target */
2114 case OPTION_PLUGIN
: /* --plugin */
2115 #if BFD_SUPPORTS_PLUGINS
2116 bfd_plugin_set_plugin (optarg
);
2118 fatal (_("sorry - this program has been built without plugin support\n"));
2122 case OPTION_IFUNC_CHARS
:
2123 ifunc_type_chars
= optarg
;
2126 case 0: /* A long option that just sets a flag. */
2135 print_version ("nm");
2137 if (sort_by_size
&& undefined_only
)
2139 non_fatal (_("Using the --size-sort and --undefined-only options together"));
2140 non_fatal (_("will produce no output, since undefined symbols have no size."));
2144 /* OK, all options now parsed. If no filename specified, do a.out. */
2146 return !display_file ("a.out");
2150 if (argc
- optind
> 1)
2151 filename_per_file
= 1;
2153 /* We were given several filenames to do. */
2154 while (optind
< argc
)
2156 if (!display_file (argv
[optind
++]))