readelf sprintf optimisation
[binutils-gdb.git] / binutils / nm.c
blobe4c8036df1bb8a4f4a948f99ca7ce461522a0b18
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
19 02110-1301, USA. */
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "getopt.h"
24 #include "aout/stab_gnu.h"
25 #include "aout/ranlib.h"
26 #include "demangle.h"
27 #include "libiberty.h"
28 #include "elf-bfd.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"
36 #include "libcoff.h"
37 #include "bucomm.h"
38 #include "demanguse.h"
39 #include "plugin-api.h"
40 #include "plugin.h"
41 #include "safe-ctype.h"
43 #ifndef streq
44 #define streq(a,b) (strcmp ((a),(b)) == 0)
45 #endif
47 /* When sorting by size, we use this structure to hold the size and a
48 pointer to the minisymbol. */
50 struct size_sym
52 const void *minisym;
53 bfd_vma size;
56 /* line number related info cached in bfd usrdata. */
58 struct lineno_cache
60 asection **secs;
61 arelent ***relocs;
62 long *relcount;
63 asymbol **syms;
64 long symcount;
65 unsigned int seccount;
68 struct extended_symbol_info
70 symbol_info *sinfo;
71 bfd_vma ssize;
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. */
113 struct output_fns
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'. */
133 enum formats
135 FORMAT_BSD = 0,
136 FORMAT_SYSV,
137 FORMAT_POSIX,
138 FORMAT_JUST_SYMBOLS,
139 FORMAT_MAX
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";
199 #else
200 static const char * ifunc_type_chars = NULL;
201 #endif
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";
218 #else
219 static const char *plugin_target = NULL;
220 #endif
222 typedef enum unicode_display_type
224 unicode_default = 0,
225 unicode_locale,
226 unicode_escape,
227 unicode_hex,
228 unicode_highlight,
229 unicode_invalid
230 } unicode_display_type;
232 static unicode_display_type unicode_display = unicode_default;
234 enum long_option_values
236 OPTION_TARGET = 200,
237 OPTION_PLUGIN,
238 OPTION_SIZE_SORT,
239 OPTION_RECURSE_LIMIT,
240 OPTION_NO_RECURSE_LIMIT,
241 OPTION_IFUNC_CHARS,
242 OPTION_UNICODE,
243 OPTION_QUIET
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, _("\
304 STYLE can be "));
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, _("\
314 -e (ignored)\n"));
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, _("\
331 -o Same as -A\n"));
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"));
341 #endif
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);
381 exit (status);
384 /* Set the radix for the symbol value and size according to RADIX. */
386 static void
387 set_print_radix (char *radix)
389 switch (*radix)
391 case 'x': print_radix = 16; break;
392 case 'd': print_radix = 10; break;
393 case 'o': print_radix = 8; break;
395 default:
396 fatal (_("%s: invalid radix"), radix);
399 other_format[3] = desc_format[3] = *radix;
402 static void
403 set_output_format (char *f)
405 int i;
407 switch (*f)
409 case 'b':
410 case 'B':
411 i = FORMAT_BSD;
412 break;
413 case 'p':
414 case 'P':
415 i = FORMAT_POSIX;
416 break;
417 case 's':
418 case 'S':
419 i = FORMAT_SYSV;
420 break;
421 case 'j':
422 case 'J':
423 i = FORMAT_JUST_SYMBOLS;
424 break;
425 default:
426 fatal (_("%s: invalid output format"), f);
428 format = &formats[i];
429 print_format = i;
432 static const char *
433 get_elf_symbol_type (unsigned int type)
435 static char *bufp;
436 int n;
438 switch (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";
449 free (bufp);
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);
454 else
455 n = asprintf (&bufp, _("<unknown>: %d"), type);
456 if (n < 0)
457 fatal ("%s", xstrerror (errno));
458 return bufp;
461 static const char *
462 get_coff_symbol_type (const struct internal_syment *sym)
464 static char *bufp;
465 int n;
467 switch (sym->n_sclass)
469 case C_BLOCK: return "Block";
470 case C_FILE: return "File";
471 case C_LINE: return "Line";
474 if (!sym->n_type)
475 return "None";
477 switch (DTYPE(sym->n_type))
479 case DT_FCN: return "Function";
480 case DT_PTR: return "Pointer";
481 case DT_ARY: return "Array";
484 free (bufp);
485 n = asprintf (&bufp, _("<unknown>: %d/%d"), sym->n_sclass, sym->n_type);
486 if (n < 0)
487 fatal ("%s", xstrerror (errno));
488 return bufp;
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. */
497 static unsigned int
498 display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
500 char * orig_out = out;
501 unsigned int nchars = 0;
502 unsigned int j;
504 if (unicode_display == unicode_default)
505 goto invalid;
507 if (in[0] < 0xc0)
508 goto invalid;
510 if ((in[1] & 0xc0) != 0x80)
511 goto invalid;
513 if ((in[0] & 0x20) == 0)
515 nchars = 2;
516 goto valid;
519 if ((in[2] & 0xc0) != 0x80)
520 goto invalid;
522 if ((in[0] & 0x10) == 0)
524 nchars = 3;
525 goto valid;
528 if ((in[3] & 0xc0) != 0x80)
529 goto invalid;
531 nchars = 4;
533 valid:
534 switch (unicode_display)
536 case unicode_locale:
537 /* Copy the bytes into the output buffer as is. */
538 memcpy (out, in, nchars);
539 out += nchars;
540 break;
542 case unicode_invalid:
543 case unicode_hex:
544 *out++ = unicode_display == unicode_hex ? '<' : '{';
545 *out++ = '0';
546 *out++ = 'x';
547 for (j = 0; j < nchars; j++)
548 out += sprintf (out, "%02x", in [j]);
549 *out++ = unicode_display == unicode_hex ? '>' : '}';
550 break;
552 case unicode_highlight:
553 if (isatty (1))
554 out += sprintf (out, "\x1B[31;47m"); /* Red. */
555 /* Fall through. */
556 case unicode_escape:
557 switch (nchars)
559 case 2:
560 out += sprintf (out, "\\u%02x%02x",
561 ((in[0] & 0x1c) >> 2),
562 ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
563 break;
565 case 3:
566 out += sprintf (out, "\\u%02x%02x",
567 ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
568 ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
569 break;
571 case 4:
572 out += sprintf (out, "\\u%02x%02x%02x",
573 ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
574 ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
575 ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
576 break;
577 default:
578 /* URG. */
579 break;
582 if (unicode_display == unicode_highlight && isatty (1))
583 out += sprintf (out, "\x1B[0m"); /* Default colour. */
584 break;
586 default:
587 /* URG */
588 break;
591 * consumed = nchars;
592 return out - orig_out;
594 invalid:
595 /* Not a valid UTF-8 sequence. */
596 *out = *in;
597 * consumed = 1;
598 return 1;
601 /* Convert any UTF-8 encoded characters in NAME into the form specified by
602 unicode_display. Also converts control characters. Returns a static
603 buffer if conversion was necessary.
604 Code stolen from objdump.c:sanitize_string(). */
606 static const char *
607 convert_utf8 (const char * in)
609 static char * buffer = NULL;
610 static size_t buffer_len = 0;
611 const char * original = in;
612 char * out;
614 /* Paranoia. */
615 if (in == NULL)
616 return "";
618 /* See if any conversion is necessary.
619 In the majority of cases it will not be needed. */
622 unsigned char c = *in++;
624 if (c == 0)
625 return original;
627 if (ISCNTRL (c))
628 break;
630 if (unicode_display != unicode_default && c >= 0xc0)
631 break;
633 while (1);
635 /* Copy the input, translating as needed. */
636 in = original;
637 /* For 2 char unicode, max out is 12 (colour escapes) + 6, ie. 9 per in
638 For hex, max out is 8 for 2 char unicode, ie. 4 per in.
639 3 and 4 char unicode produce less output for input. */
640 size_t max_needed = strlen (in) * 9 + 1;
641 if (buffer_len < max_needed)
643 buffer_len = max_needed;
644 free (buffer);
645 buffer = xmalloc (buffer_len);
648 out = buffer;
651 unsigned char c = *in++;
653 if (c == 0)
654 break;
656 if (ISCNTRL (c))
658 *out++ = '^';
659 *out++ = c + 0x40;
661 else if (unicode_display != unicode_default && c >= 0xc0)
663 unsigned int num_consumed;
665 out += display_utf8 ((const unsigned char *) --in, out, &num_consumed);
666 in += num_consumed;
668 else
669 *out++ = c;
671 while (1);
673 *out = 0;
674 return buffer;
677 /* Print symbol name NAME, read from ABFD, with printf format FORM,
678 demangling it if requested. */
680 static void
681 print_symname (const char *form, struct extended_symbol_info *info,
682 const char *name, bfd *abfd)
684 char *alloc = NULL;
685 char *atver = NULL;
687 if (name == NULL)
688 name = info->sinfo->name;
690 if (!with_symbol_versions
691 && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
693 atver = strchr (name, '@');
694 if (atver)
695 *atver = 0;
698 if (do_demangle && *name)
700 alloc = bfd_demangle (abfd, name, demangle_flags);
701 if (alloc != NULL)
702 name = alloc;
705 if (unicode_display != unicode_default)
707 name = convert_utf8 (name);
710 if (info != NULL && info->elfinfo && with_symbol_versions)
712 const char *version_string;
713 bool hidden;
715 version_string
716 = bfd_get_symbol_version_string (abfd, &info->elfinfo->symbol,
717 false, &hidden);
718 if (version_string && version_string[0])
720 const char *at = "@@";
721 if (hidden || bfd_is_und_section (info->elfinfo->symbol.section))
722 at = "@";
723 alloc = reconcat (alloc, name, at, version_string, NULL);
724 if (alloc != NULL)
725 name = alloc;
728 printf (form, name);
729 if (atver)
730 *atver = '@';
731 free (alloc);
734 static void
735 print_symdef_entry (bfd *abfd)
737 symindex idx = BFD_NO_MORE_SYMBOLS;
738 carsym *thesym;
739 bool everprinted = false;
741 for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
742 idx != BFD_NO_MORE_SYMBOLS;
743 idx = bfd_get_next_mapent (abfd, idx, &thesym))
745 if (!everprinted)
747 printf (_("\nArchive index:\n"));
748 everprinted = true;
750 if (thesym->name != NULL)
752 print_symname ("%s", NULL, thesym->name, abfd);
753 bfd *elt = bfd_get_elt_at_index (abfd, idx);
754 if (elt)
755 printf (" in %s\n", bfd_get_filename (elt));
756 else
757 printf ("\n");
763 /* True when we can report missing plugin error. */
764 bool report_plugin_err = true;
766 /* Choose which symbol entries to print;
767 compact them downward to get rid of the rest.
768 Return the number of symbols to be printed. */
770 static long
771 filter_symbols (bfd *abfd, bool is_dynamic, void *minisyms,
772 long symcount, unsigned int size)
774 bfd_byte *from, *fromend, *to;
775 asymbol *store;
777 store = bfd_make_empty_symbol (abfd);
778 if (store == NULL)
779 bfd_fatal (bfd_get_filename (abfd));
781 from = (bfd_byte *) minisyms;
782 fromend = from + symcount * size;
783 to = (bfd_byte *) minisyms;
785 for (; from < fromend; from += size)
787 int keep = 0;
788 asymbol *sym;
790 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from, store);
791 if (sym == NULL)
792 continue;
794 if (sym->name != NULL
795 && sym->name[0] == '_'
796 && sym->name[1] == '_'
797 && strcmp (sym->name + (sym->name[2] == '_'), "__gnu_lto_slim") == 0
798 && report_plugin_err)
800 report_plugin_err = false;
801 non_fatal (_("%s: plugin needed to handle lto object"),
802 bfd_get_filename (abfd));
805 if (undefined_only)
806 keep = bfd_is_und_section (sym->section);
807 else if (external_only)
808 /* PR binutls/12753: Unique symbols are global too. */
809 keep = ((sym->flags & (BSF_GLOBAL
810 | BSF_WEAK
811 | BSF_GNU_UNIQUE)) != 0
812 || bfd_is_und_section (sym->section)
813 || bfd_is_com_section (sym->section));
814 else if (non_weak)
815 keep = ((sym->flags & BSF_WEAK) == 0);
816 else
817 keep = 1;
819 if (keep
820 && ! print_debug_syms
821 && (sym->flags & BSF_DEBUGGING) != 0)
822 keep = 0;
824 if (keep
825 && sort_by_size
826 && (bfd_is_abs_section (sym->section)
827 || bfd_is_und_section (sym->section)))
828 keep = 0;
830 if (keep
831 && defined_only)
833 if (bfd_is_und_section (sym->section))
834 keep = 0;
837 if (keep
838 && bfd_is_target_special_symbol (abfd, sym)
839 && ! allow_special_symbols)
840 keep = 0;
842 if (keep)
844 if (to != from)
845 memcpy (to, from, size);
846 to += size;
850 return (to - (bfd_byte *) minisyms) / size;
853 /* These globals are used to pass information into the sorting
854 routines. */
855 static bfd *sort_bfd;
856 static bool sort_dynamic;
857 static asymbol *sort_x;
858 static asymbol *sort_y;
860 /* Symbol-sorting predicates */
861 #define valueof(x) ((x)->section->vma + (x)->value)
863 /* Numeric sorts. Undefined symbols are always considered "less than"
864 defined symbols with zero values. Common symbols are not treated
865 specially -- i.e., their sizes are used as their "values". */
867 static int
868 non_numeric_forward (const void *P_x, const void *P_y)
870 asymbol *x, *y;
871 const char *xn, *yn;
873 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
874 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
875 if (x == NULL || y == NULL)
876 bfd_fatal (bfd_get_filename (sort_bfd));
878 xn = bfd_asymbol_name (x);
879 yn = bfd_asymbol_name (y);
881 if (yn == NULL)
882 return xn != NULL;
883 if (xn == NULL)
884 return -1;
886 /* Solaris 2.5 has a bug in strcoll.
887 strcoll returns invalid values when confronted with empty strings. */
888 if (*yn == '\0')
889 return *xn != '\0';
890 if (*xn == '\0')
891 return -1;
893 return strcoll (xn, yn);
896 static int
897 non_numeric_reverse (const void *x, const void *y)
899 return - non_numeric_forward (x, y);
902 static int
903 numeric_forward (const void *P_x, const void *P_y)
905 asymbol *x, *y;
906 asection *xs, *ys;
908 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
909 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
910 if (x == NULL || y == NULL)
911 bfd_fatal (bfd_get_filename (sort_bfd));
913 xs = bfd_asymbol_section (x);
914 ys = bfd_asymbol_section (y);
916 if (bfd_is_und_section (xs))
918 if (! bfd_is_und_section (ys))
919 return -1;
921 else if (bfd_is_und_section (ys))
922 return 1;
923 else if (valueof (x) != valueof (y))
924 return valueof (x) < valueof (y) ? -1 : 1;
926 return non_numeric_forward (P_x, P_y);
929 static int
930 numeric_reverse (const void *x, const void *y)
932 return - numeric_forward (x, y);
935 static int (*(sorters[2][2])) (const void *, const void *) =
937 { non_numeric_forward, non_numeric_reverse },
938 { numeric_forward, numeric_reverse }
941 /* This sort routine is used by sort_symbols_by_size. It is similar
942 to numeric_forward, but when symbols have the same value it sorts
943 by section VMA. This simplifies the sort_symbols_by_size code
944 which handles symbols at the end of sections. Also, this routine
945 tries to sort file names before other symbols with the same value.
946 That will make the file name have a zero size, which will make
947 sort_symbols_by_size choose the non file name symbol, leading to
948 more meaningful output. For similar reasons, this code sorts
949 gnu_compiled_* and gcc2_compiled before other symbols with the same
950 value. */
952 static int
953 size_forward1 (const void *P_x, const void *P_y)
955 asymbol *x, *y;
956 asection *xs, *ys;
957 const char *xn, *yn;
958 size_t xnl, ynl;
959 int xf, yf;
961 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
962 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
963 if (x == NULL || y == NULL)
964 bfd_fatal (bfd_get_filename (sort_bfd));
966 xs = bfd_asymbol_section (x);
967 ys = bfd_asymbol_section (y);
969 if (bfd_is_und_section (xs))
970 abort ();
971 if (bfd_is_und_section (ys))
972 abort ();
974 if (valueof (x) != valueof (y))
975 return valueof (x) < valueof (y) ? -1 : 1;
977 if (xs->vma != ys->vma)
978 return xs->vma < ys->vma ? -1 : 1;
980 xn = bfd_asymbol_name (x);
981 yn = bfd_asymbol_name (y);
982 xnl = strlen (xn);
983 ynl = strlen (yn);
985 /* The symbols gnu_compiled and gcc2_compiled convey even less
986 information than the file name, so sort them out first. */
988 xf = (strstr (xn, "gnu_compiled") != NULL
989 || strstr (xn, "gcc2_compiled") != NULL);
990 yf = (strstr (yn, "gnu_compiled") != NULL
991 || strstr (yn, "gcc2_compiled") != NULL);
993 if (xf && ! yf)
994 return -1;
995 if (! xf && yf)
996 return 1;
998 /* We use a heuristic for the file name. It may not work on non
999 Unix systems, but it doesn't really matter; the only difference
1000 is precisely which symbol names get printed. */
1002 #define file_symbol(s, sn, snl) \
1003 (((s)->flags & BSF_FILE) != 0 \
1004 || ((snl) > 2 \
1005 && (sn)[(snl) - 2] == '.' \
1006 && ((sn)[(snl) - 1] == 'o' \
1007 || (sn)[(snl) - 1] == 'a')))
1009 xf = file_symbol (x, xn, xnl);
1010 yf = file_symbol (y, yn, ynl);
1012 if (xf && ! yf)
1013 return -1;
1014 if (! xf && yf)
1015 return 1;
1017 return non_numeric_forward (P_x, P_y);
1020 /* This sort routine is used by sort_symbols_by_size. It is sorting
1021 an array of size_sym structures into size order. */
1023 static int
1024 size_forward2 (const void *P_x, const void *P_y)
1026 const struct size_sym *x = (const struct size_sym *) P_x;
1027 const struct size_sym *y = (const struct size_sym *) P_y;
1029 if (x->size < y->size)
1030 return reverse_sort ? 1 : -1;
1031 else if (x->size > y->size)
1032 return reverse_sort ? -1 : 1;
1033 else
1034 return sorters[0][reverse_sort] (x->minisym, y->minisym);
1037 /* Sort the symbols by size. ELF provides a size but for other formats
1038 we have to make a guess by assuming that the difference between the
1039 address of a symbol and the address of the next higher symbol is the
1040 size. */
1042 static long
1043 sort_symbols_by_size (bfd *abfd, bool is_dynamic, void *minisyms,
1044 long symcount, unsigned int size,
1045 struct size_sym **symsizesp)
1047 struct size_sym *symsizes;
1048 bfd_byte *from, *fromend;
1049 asymbol *sym = NULL;
1050 asymbol *store_sym, *store_next;
1052 qsort (minisyms, symcount, size, size_forward1);
1054 /* We are going to return a special set of symbols and sizes to
1055 print. */
1056 symsizes = (struct size_sym *) xmalloc (symcount * sizeof (struct size_sym));
1057 *symsizesp = symsizes;
1059 /* Note that filter_symbols has already removed all absolute and
1060 undefined symbols. Here we remove all symbols whose size winds
1061 up as zero. */
1062 from = (bfd_byte *) minisyms;
1063 fromend = from + symcount * size;
1065 store_sym = sort_x;
1066 store_next = sort_y;
1068 if (from < fromend)
1070 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, (const void *) from,
1071 store_sym);
1072 if (sym == NULL)
1073 bfd_fatal (bfd_get_filename (abfd));
1076 for (; from < fromend; from += size)
1078 asymbol *next;
1079 asection *sec;
1080 bfd_vma sz;
1081 asymbol *temp;
1083 if (from + size < fromend)
1085 next = bfd_minisymbol_to_symbol (abfd,
1086 is_dynamic,
1087 (const void *) (from + size),
1088 store_next);
1089 if (next == NULL)
1090 bfd_fatal (bfd_get_filename (abfd));
1092 else
1093 next = NULL;
1095 sec = bfd_asymbol_section (sym);
1097 /* Synthetic symbols don't have a full type set of data available, thus
1098 we can't rely on that information for the symbol size. Ditto for
1099 bfd/section.c:global_syms like *ABS*. */
1100 if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0
1101 && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1102 sz = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
1103 else if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0
1104 && bfd_is_com_section (sec))
1105 sz = sym->value;
1106 else
1108 if (from + size < fromend
1109 && sec == bfd_asymbol_section (next))
1110 sz = valueof (next) - valueof (sym);
1111 else
1112 sz = (bfd_section_vma (sec)
1113 + bfd_section_size (sec)
1114 - valueof (sym));
1117 if (sz != 0)
1119 symsizes->minisym = (const void *) from;
1120 symsizes->size = sz;
1121 ++symsizes;
1124 sym = next;
1126 temp = store_sym;
1127 store_sym = store_next;
1128 store_next = temp;
1131 symcount = symsizes - *symsizesp;
1133 /* We must now sort again by size. */
1134 qsort ((void *) *symsizesp, symcount, sizeof (struct size_sym), size_forward2);
1136 return symcount;
1139 /* This function is used to get the relocs for a particular section.
1140 It is called via bfd_map_over_sections. */
1142 static void
1143 get_relocs (bfd *abfd, asection *sec, void *dataarg)
1145 struct lineno_cache *data = (struct lineno_cache *) dataarg;
1147 *data->secs = sec;
1148 *data->relocs = NULL;
1149 *data->relcount = 0;
1151 if ((sec->flags & SEC_RELOC) != 0)
1153 long relsize = bfd_get_reloc_upper_bound (abfd, sec);
1154 if (relsize > 0)
1156 *data->relocs = (arelent **) xmalloc (relsize);
1157 *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs,
1158 data->syms);
1162 ++data->secs;
1163 ++data->relocs;
1164 ++data->relcount;
1167 static void
1168 free_lineno_cache (bfd *abfd)
1170 struct lineno_cache *lc = bfd_usrdata (abfd);
1172 if (lc)
1174 if (lc->relocs)
1175 for (unsigned int i = 0; i < lc->seccount; i++)
1176 free (lc->relocs[i]);
1177 free (lc->relcount);
1178 free (lc->relocs);
1179 free (lc->secs);
1180 free (lc->syms);
1181 free (lc);
1182 bfd_set_usrdata (abfd, NULL);
1186 /* Print a single symbol. */
1188 static void
1189 print_symbol (bfd * abfd,
1190 asymbol * sym,
1191 bfd_vma ssize,
1192 bfd * archive_bfd)
1194 symbol_info syminfo;
1195 struct extended_symbol_info info;
1197 format->print_symbol_filename (archive_bfd, abfd);
1199 bfd_get_symbol_info (abfd, sym, &syminfo);
1201 /* PR 22967 - Distinguish between local and global ifunc symbols. */
1202 if (syminfo.type == 'i'
1203 && sym->flags & BSF_GNU_INDIRECT_FUNCTION)
1205 if (ifunc_type_chars == NULL || ifunc_type_chars[0] == 0)
1206 ; /* Change nothing. */
1207 else if (sym->flags & BSF_GLOBAL)
1208 syminfo.type = ifunc_type_chars[0];
1209 else if (ifunc_type_chars[1] != 0)
1210 syminfo.type = ifunc_type_chars[1];
1213 info.sinfo = &syminfo;
1214 info.ssize = ssize;
1215 /* Synthetic symbols do not have a full symbol type set of data available.
1216 Nor do bfd/section.c:global_syms like *ABS*. */
1217 if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) != 0)
1219 info.elfinfo = NULL;
1220 info.coffinfo = NULL;
1222 else
1224 info.elfinfo = elf_symbol_from (sym);
1225 info.coffinfo = coff_symbol_from (sym);
1228 format->print_symbol_info (&info, abfd);
1230 if (line_numbers)
1232 struct lineno_cache *lc = bfd_usrdata (abfd);
1233 const char *filename, *functionname;
1234 unsigned int lineno;
1236 /* We need to get the canonical symbols in order to call
1237 bfd_find_nearest_line. This is inefficient, but, then, you
1238 don't have to use --line-numbers. */
1239 if (lc == NULL)
1241 lc = xcalloc (1, sizeof (*lc));
1242 bfd_set_usrdata (abfd, lc);
1244 if (lc->syms == NULL && lc->symcount == 0)
1246 long symsize = bfd_get_symtab_upper_bound (abfd);
1247 if (symsize <= 0)
1248 lc->symcount = -1;
1249 else
1251 lc->syms = xmalloc (symsize);
1252 lc->symcount = bfd_canonicalize_symtab (abfd, lc->syms);
1256 if (lc->symcount <= 0)
1258 else if (bfd_is_und_section (bfd_asymbol_section (sym)))
1260 unsigned int i;
1261 const char *symname;
1263 /* For an undefined symbol, we try to find a reloc for the
1264 symbol, and print the line number of the reloc. */
1265 if (lc->relocs == NULL)
1267 unsigned int seccount = bfd_count_sections (abfd);
1268 lc->seccount = seccount;
1269 lc->secs = xmalloc (seccount * sizeof (*lc->secs));
1270 lc->relocs = xmalloc (seccount * sizeof (*lc->relocs));
1271 lc->relcount = xmalloc (seccount * sizeof (*lc->relcount));
1273 struct lineno_cache rinfo = *lc;
1274 bfd_map_over_sections (abfd, get_relocs, &rinfo);
1277 symname = bfd_asymbol_name (sym);
1278 for (i = 0; i < lc->seccount; i++)
1280 long j;
1282 for (j = 0; j < lc->relcount[i]; j++)
1284 arelent *r;
1286 r = lc->relocs[i][j];
1287 if (r->sym_ptr_ptr != NULL
1288 && (*r->sym_ptr_ptr)->section == sym->section
1289 && (*r->sym_ptr_ptr)->value == sym->value
1290 && strcmp (symname,
1291 bfd_asymbol_name (*r->sym_ptr_ptr)) == 0
1292 && bfd_find_nearest_line (abfd, lc->secs[i], lc->syms,
1293 r->address, &filename,
1294 &functionname, &lineno)
1295 && filename != NULL)
1297 /* We only print the first one we find. */
1298 printf ("\t%s:%u", filename, lineno);
1299 i = lc->seccount;
1300 break;
1305 else if (bfd_asymbol_section (sym)->owner == abfd)
1307 if ((bfd_find_line (abfd, lc->syms, sym, &filename, &lineno)
1308 || bfd_find_nearest_line (abfd, bfd_asymbol_section (sym),
1309 lc->syms, sym->value, &filename,
1310 &functionname, &lineno))
1311 && filename != NULL
1312 && lineno != 0)
1313 printf ("\t%s:%u", filename, lineno);
1317 putchar ('\n');
1320 /* Print the symbols when sorting by size. */
1322 static void
1323 print_size_symbols (bfd *abfd,
1324 bool is_dynamic,
1325 struct size_sym *symsizes,
1326 long symcount,
1327 bfd *archive_bfd)
1329 asymbol *store;
1330 struct size_sym *from;
1331 struct size_sym *fromend;
1333 store = bfd_make_empty_symbol (abfd);
1334 if (store == NULL)
1335 bfd_fatal (bfd_get_filename (abfd));
1337 from = symsizes;
1338 fromend = from + symcount;
1340 for (; from < fromend; from++)
1342 asymbol *sym;
1344 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from->minisym, store);
1345 if (sym == NULL)
1346 bfd_fatal (bfd_get_filename (abfd));
1348 print_symbol (abfd, sym, from->size, archive_bfd);
1353 /* Print the symbols of ABFD that are held in MINISYMS.
1355 If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD.
1357 SYMCOUNT is the number of symbols in MINISYMS.
1359 SIZE is the size of a symbol in MINISYMS. */
1361 static void
1362 print_symbols (bfd *abfd,
1363 bool is_dynamic,
1364 void *minisyms,
1365 long symcount,
1366 unsigned int size,
1367 bfd *archive_bfd)
1369 asymbol *store;
1370 bfd_byte *from;
1371 bfd_byte *fromend;
1373 store = bfd_make_empty_symbol (abfd);
1374 if (store == NULL)
1375 bfd_fatal (bfd_get_filename (abfd));
1377 from = (bfd_byte *) minisyms;
1378 fromend = from + symcount * size;
1380 for (; from < fromend; from += size)
1382 asymbol *sym;
1384 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from, store);
1385 if (sym == NULL)
1386 bfd_fatal (bfd_get_filename (abfd));
1388 print_symbol (abfd, sym, (bfd_vma) 0, archive_bfd);
1392 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
1394 static void
1395 display_rel_file (bfd *abfd, bfd *archive_bfd)
1397 long symcount;
1398 void *minisyms;
1399 unsigned int size;
1400 struct size_sym *symsizes;
1401 asymbol *synthsyms = NULL;
1403 if (! dynamic)
1405 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
1407 if (!quiet)
1408 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
1409 return;
1413 symcount = bfd_read_minisymbols (abfd, dynamic, &minisyms, &size);
1414 if (symcount <= 0)
1416 if (!quiet)
1417 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
1418 return;
1421 if (show_synthetic && size == sizeof (asymbol *))
1423 asymbol **static_syms = NULL;
1424 asymbol **dyn_syms = NULL;
1425 long static_count = 0;
1426 long dyn_count = 0;
1427 long synth_count;
1429 if (dynamic)
1431 dyn_count = symcount;
1432 dyn_syms = (asymbol **) minisyms;
1434 else
1436 long storage = bfd_get_dynamic_symtab_upper_bound (abfd);
1438 static_count = symcount;
1439 static_syms = (asymbol **) minisyms;
1441 if (storage > 0)
1443 dyn_syms = (asymbol **) xmalloc (storage);
1444 dyn_count = bfd_canonicalize_dynamic_symtab (abfd, dyn_syms);
1445 if (dyn_count < 0)
1446 dyn_count = 0;
1450 synth_count = bfd_get_synthetic_symtab (abfd, static_count, static_syms,
1451 dyn_count, dyn_syms, &synthsyms);
1452 if (synth_count > 0)
1454 asymbol **symp;
1455 long i;
1457 minisyms = xrealloc (minisyms,
1458 (symcount + synth_count + 1) * sizeof (*symp));
1459 symp = (asymbol **) minisyms + symcount;
1460 for (i = 0; i < synth_count; i++)
1461 *symp++ = synthsyms + i;
1462 *symp = 0;
1463 symcount += synth_count;
1465 if (!dynamic && dyn_syms != NULL)
1466 free (dyn_syms);
1469 /* lto_slim_object is set to false when a bfd is loaded with a compiler
1470 LTO plugin. */
1471 if (abfd->lto_slim_object)
1473 report_plugin_err = false;
1474 non_fatal (_("%s: plugin needed to handle lto object"),
1475 bfd_get_filename (abfd));
1478 /* Discard the symbols we don't want to print.
1479 It's OK to do this in place; we'll free the storage anyway
1480 (after printing). */
1482 symcount = filter_symbols (abfd, dynamic, minisyms, symcount, size);
1484 symsizes = NULL;
1485 if (! no_sort)
1487 sort_bfd = abfd;
1488 sort_dynamic = dynamic;
1489 sort_x = bfd_make_empty_symbol (abfd);
1490 sort_y = bfd_make_empty_symbol (abfd);
1491 if (sort_x == NULL || sort_y == NULL)
1492 bfd_fatal (bfd_get_filename (abfd));
1494 if (! sort_by_size)
1495 qsort (minisyms, symcount, size,
1496 sorters[sort_numerically][reverse_sort]);
1497 else
1498 symcount = sort_symbols_by_size (abfd, dynamic, minisyms, symcount,
1499 size, &symsizes);
1502 if (! sort_by_size)
1503 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd);
1504 else
1505 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd);
1507 if (synthsyms)
1508 free (synthsyms);
1509 free (minisyms);
1510 free (symsizes);
1513 /* Construct a formatting string for printing symbol values. */
1515 static const char *
1516 get_print_format (void)
1518 const char * padding;
1519 if (print_format == FORMAT_POSIX || print_format == FORMAT_JUST_SYMBOLS)
1521 /* POSIX compatible output does not have any padding. */
1522 padding = "";
1524 else if (print_width == 32)
1526 padding ="08";
1528 else /* print_width == 64 */
1530 padding = "016";
1533 const char * radix = NULL;
1534 switch (print_radix)
1536 case 8: radix = PRIo64; break;
1537 case 10: radix = PRId64; break;
1538 case 16: radix = PRIx64; break;
1541 return concat ("%", padding, radix, NULL);
1544 static void
1545 set_print_width (bfd *file)
1547 print_width = bfd_get_arch_size (file);
1549 if (print_width == -1)
1551 /* PR binutils/4292
1552 Guess the target's bitsize based on its name.
1553 We assume here than any 64-bit format will include
1554 "64" somewhere in its name. The only known exception
1555 is the MMO object file format. */
1556 if (strstr (bfd_get_target (file), "64") != NULL
1557 || strcmp (bfd_get_target (file), "mmo") == 0)
1558 print_width = 64;
1559 else
1560 print_width = 32;
1562 free ((char *) print_format_string);
1563 print_format_string = get_print_format ();
1566 static void
1567 display_archive (bfd *file)
1569 bfd *arfile = NULL;
1570 bfd *last_arfile = NULL;
1571 char **matching;
1573 format->print_archive_filename (bfd_get_filename (file));
1575 if (print_armap)
1576 print_symdef_entry (file);
1578 for (;;)
1580 arfile = bfd_openr_next_archived_file (file, arfile);
1582 if (arfile == NULL)
1584 if (bfd_get_error () != bfd_error_no_more_archived_files)
1585 bfd_nonfatal (bfd_get_filename (file));
1586 break;
1589 if (bfd_check_format_matches (arfile, bfd_object, &matching))
1591 set_print_width (arfile);
1592 format->print_archive_member (bfd_get_filename (file),
1593 bfd_get_filename (arfile));
1594 display_rel_file (arfile, file);
1596 else
1598 bfd_nonfatal (bfd_get_filename (arfile));
1599 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1600 list_matching_formats (matching);
1603 if (last_arfile != NULL)
1605 free_lineno_cache (last_arfile);
1606 bfd_close (last_arfile);
1607 if (arfile == last_arfile)
1608 return;
1610 last_arfile = arfile;
1613 if (last_arfile != NULL)
1615 free_lineno_cache (last_arfile);
1616 bfd_close (last_arfile);
1620 static bool
1621 display_file (char *filename)
1623 bool retval = true;
1624 bfd *file;
1625 char **matching;
1627 if (get_file_size (filename) < 1)
1628 return false;
1630 file = bfd_openr (filename, target ? target : plugin_target);
1631 if (file == NULL)
1633 bfd_nonfatal (filename);
1634 return false;
1637 /* If printing line numbers, decompress the debug sections. */
1638 if (line_numbers)
1639 file->flags |= BFD_DECOMPRESS;
1641 if (bfd_check_format (file, bfd_archive))
1643 display_archive (file);
1645 else if (bfd_check_format_matches (file, bfd_object, &matching))
1647 set_print_width (file);
1648 format->print_object_filename (filename);
1649 display_rel_file (file, NULL);
1651 else
1653 bfd_nonfatal (filename);
1654 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1655 list_matching_formats (matching);
1656 retval = false;
1659 free_lineno_cache (file);
1660 if (!bfd_close (file))
1661 retval = false;
1663 return retval;
1666 /* The following 3 groups of functions are called unconditionally,
1667 once at the start of processing each file of the appropriate type.
1668 They should check `filename_per_file' and `filename_per_symbol',
1669 as appropriate for their output format, to determine whether to
1670 print anything. */
1672 /* Print the name of an object file given on the command line. */
1674 static void
1675 print_object_filename_bsd (const char *filename)
1677 if (filename_per_file && !filename_per_symbol)
1678 printf ("\n%s:\n", filename);
1681 static void
1682 print_object_filename_sysv (const char *filename)
1684 if (undefined_only)
1685 printf (_("\n\nUndefined symbols from %s:\n\n"), filename);
1686 else
1687 printf (_("\n\nSymbols from %s:\n\n"), filename);
1688 if (print_width == 32)
1689 printf (_("\
1690 Name Value Class Type Size Line Section\n\n"));
1691 else
1692 printf (_("\
1693 Name Value Class Type Size Line Section\n\n"));
1696 static void
1697 print_object_filename_posix (const char *filename)
1699 if (filename_per_file && !filename_per_symbol)
1700 printf ("%s:\n", filename);
1703 static void
1704 do_not_print_object_filename (const char *filename ATTRIBUTE_UNUSED)
1708 /* Print the name of an archive file given on the command line. */
1710 static void
1711 print_archive_filename_bsd (const char *filename)
1713 if (filename_per_file)
1714 printf ("\n%s:\n", filename);
1717 static void
1718 print_archive_filename_sysv (const char *filename ATTRIBUTE_UNUSED)
1722 static void
1723 print_archive_filename_posix (const char *filename ATTRIBUTE_UNUSED)
1727 static void
1728 do_not_print_archive_filename (const char *filename ATTRIBUTE_UNUSED)
1732 /* Print the name of an archive member file. */
1734 static void
1735 print_archive_member_bsd (const char *archive ATTRIBUTE_UNUSED,
1736 const char *filename)
1738 if (!filename_per_symbol)
1739 printf ("\n%s:\n", filename);
1742 static void
1743 print_archive_member_sysv (const char *archive, const char *filename)
1745 if (undefined_only)
1746 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename);
1747 else
1748 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename);
1749 if (print_width == 32)
1750 printf (_("\
1751 Name Value Class Type Size Line Section\n\n"));
1752 else
1753 printf (_("\
1754 Name Value Class Type Size Line Section\n\n"));
1757 static void
1758 print_archive_member_posix (const char *archive, const char *filename)
1760 if (!filename_per_symbol)
1761 printf ("%s[%s]:\n", archive, filename);
1764 static void
1765 do_not_print_archive_member (const char *archive ATTRIBUTE_UNUSED,
1766 const char *filename ATTRIBUTE_UNUSED)
1771 /* Print the name of the file (and archive, if there is one)
1772 containing a symbol. */
1774 static void
1775 print_symbol_filename_bsd (bfd *archive_bfd, bfd *abfd)
1777 if (filename_per_symbol)
1779 if (archive_bfd)
1780 printf ("%s:", bfd_get_filename (archive_bfd));
1781 printf ("%s:", bfd_get_filename (abfd));
1785 static void
1786 print_symbol_filename_sysv (bfd *archive_bfd, bfd *abfd)
1788 if (filename_per_symbol)
1790 if (archive_bfd)
1791 printf ("%s:", bfd_get_filename (archive_bfd));
1792 printf ("%s:", bfd_get_filename (abfd));
1796 static void
1797 print_symbol_filename_posix (bfd *archive_bfd, bfd *abfd)
1799 if (filename_per_symbol)
1801 if (archive_bfd)
1802 printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
1803 bfd_get_filename (abfd));
1804 else
1805 printf ("%s: ", bfd_get_filename (abfd));
1809 static void
1810 do_not_print_symbol_filename (bfd *archive_bfd ATTRIBUTE_UNUSED,
1811 bfd *abfd ATTRIBUTE_UNUSED)
1816 /* Print a symbol value. */
1818 static void
1819 print_value (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma val)
1821 switch (print_width)
1823 case 32:
1824 case 64:
1825 printf (print_format_string, (uint64_t) val);
1826 break;
1828 default:
1829 fatal (_("Print width has not been initialized (%d)"), print_width);
1830 break;
1834 /* Print a line of information about a symbol. */
1836 static void
1837 print_symbol_info_bsd (struct extended_symbol_info *info, bfd *abfd)
1839 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1841 if (print_width == 64)
1842 printf (" ");
1843 printf (" ");
1845 else
1847 /* Normally we print the value of the symbol. If we are printing the
1848 size or sorting by size then we print its size, except for the
1849 (weird) special case where both flags are defined, in which case we
1850 print both values. This conforms to documented behaviour. */
1851 if (sort_by_size && !print_size)
1852 print_value (abfd, SYM_SIZE (info));
1853 else
1854 print_value (abfd, SYM_VALUE (info));
1855 if (print_size && SYM_SIZE (info))
1857 printf (" ");
1858 print_value (abfd, SYM_SIZE (info));
1862 printf (" %c", SYM_TYPE (info));
1864 if (SYM_TYPE (info) == '-')
1866 /* A stab. */
1867 printf (" ");
1868 printf (other_format, SYM_STAB_OTHER (info));
1869 printf (" ");
1870 printf (desc_format, SYM_STAB_DESC (info));
1871 printf (" %5s", SYM_STAB_NAME (info));
1873 print_symname (" %s", info, NULL, abfd);
1876 static void
1877 print_symbol_info_sysv (struct extended_symbol_info *info, bfd *abfd)
1879 print_symname ("%-20s|", info, NULL, abfd);
1881 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1883 if (print_width == 32)
1884 printf (" ");
1885 else
1886 printf (" ");
1888 else
1889 print_value (abfd, SYM_VALUE (info));
1891 printf ("| %c |", SYM_TYPE (info));
1893 if (SYM_TYPE (info) == '-')
1895 /* A stab. */
1896 printf ("%18s| ", SYM_STAB_NAME (info)); /* (C) Type. */
1897 printf (desc_format, SYM_STAB_DESC (info)); /* Size. */
1898 printf ("| |"); /* Line, Section. */
1900 else
1902 /* Type, Size, Line, Section */
1903 if (info->elfinfo)
1904 printf ("%18s|",
1905 get_elf_symbol_type (ELF_ST_TYPE (info->elfinfo->internal_elf_sym.st_info)));
1906 else if (info->coffinfo)
1907 printf ("%18s|",
1908 get_coff_symbol_type (&info->coffinfo->native->u.syment));
1909 else
1910 printf (" |");
1912 if (SYM_SIZE (info))
1913 print_value (abfd, SYM_SIZE (info));
1914 else
1916 if (print_width == 32)
1917 printf (" ");
1918 else
1919 printf (" ");
1922 if (info->elfinfo)
1923 printf("| |%s", info->elfinfo->symbol.section->name);
1924 else if (info->coffinfo)
1925 printf("| |%s", info->coffinfo->symbol.section->name);
1926 else
1927 printf("| |");
1931 static void
1932 print_symbol_info_posix (struct extended_symbol_info *info, bfd *abfd)
1934 print_symname ("%s ", info, NULL, abfd);
1935 printf ("%c ", SYM_TYPE (info));
1937 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1938 printf (" ");
1939 else
1941 print_value (abfd, SYM_VALUE (info));
1942 printf (" ");
1943 if (SYM_SIZE (info))
1944 print_value (abfd, SYM_SIZE (info));
1948 static void
1949 just_print_symbol_name (struct extended_symbol_info *info, bfd *abfd)
1951 print_symname ("%s", info, NULL, abfd);
1955 main (int argc, char **argv)
1957 int c;
1958 int retval;
1960 #ifdef HAVE_LC_MESSAGES
1961 setlocale (LC_MESSAGES, "");
1962 #endif
1963 setlocale (LC_CTYPE, "");
1964 setlocale (LC_COLLATE, "");
1965 bindtextdomain (PACKAGE, LOCALEDIR);
1966 textdomain (PACKAGE);
1968 program_name = *argv;
1969 xmalloc_set_program_name (program_name);
1970 bfd_set_error_program_name (program_name);
1971 #if BFD_SUPPORTS_PLUGINS
1972 bfd_plugin_set_program_name (program_name);
1973 #endif
1975 expandargv (&argc, &argv);
1977 if (bfd_init () != BFD_INIT_MAGIC)
1978 fatal (_("fatal error: libbfd ABI mismatch"));
1979 set_default_bfd_target ();
1981 while ((c = getopt_long (argc, argv, "aABCDef:gHhjJlnopPrSst:uU:vVvWX:",
1982 long_options, (int *) 0)) != EOF)
1984 switch (c)
1986 case 'a':
1987 print_debug_syms = 1;
1988 break;
1989 case 'A':
1990 case 'o':
1991 filename_per_symbol = 1;
1992 break;
1993 case 'B': /* For MIPS compatibility. */
1994 set_output_format ("bsd");
1995 break;
1996 case 'C':
1997 do_demangle = 1;
1998 if (optarg != NULL)
2000 enum demangling_styles style;
2002 style = cplus_demangle_name_to_style (optarg);
2003 if (style == unknown_demangling)
2004 fatal (_("unknown demangling style `%s'"),
2005 optarg);
2007 cplus_demangle_set_style (style);
2009 break;
2010 case OPTION_RECURSE_LIMIT:
2011 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
2012 break;
2013 case OPTION_NO_RECURSE_LIMIT:
2014 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
2015 break;
2016 case OPTION_QUIET:
2017 quiet = 1;
2018 break;
2019 case 'D':
2020 dynamic = 1;
2021 break;
2022 case 'e':
2023 /* Ignored for HP/UX compatibility. */
2024 break;
2025 case 'f':
2026 set_output_format (optarg);
2027 break;
2028 case 'g':
2029 external_only = 1;
2030 break;
2031 case 'H':
2032 case 'h':
2033 usage (stdout, 0);
2034 case 'l':
2035 line_numbers = 1;
2036 break;
2037 case 'n':
2038 case 'v':
2039 no_sort = 0;
2040 sort_numerically = 1;
2041 sort_by_size = 0;
2042 break;
2043 case 'p':
2044 no_sort = 1;
2045 sort_numerically = 0;
2046 sort_by_size = 0;
2047 break;
2048 case OPTION_SIZE_SORT:
2049 no_sort = 0;
2050 sort_numerically = 0;
2051 sort_by_size = 1;
2052 break;
2053 case 'P':
2054 set_output_format ("posix");
2055 break;
2056 case 'j':
2057 set_output_format ("just-symbols");
2058 break;
2059 case 'r':
2060 reverse_sort = 1;
2061 break;
2062 case 's':
2063 print_armap = 1;
2064 break;
2065 case 'S':
2066 print_size = 1;
2067 break;
2068 case 't':
2069 set_print_radix (optarg);
2070 break;
2071 case 'u':
2072 undefined_only = 1;
2073 defined_only = 0;
2074 break;
2075 case 'U':
2076 defined_only = 1;
2077 undefined_only = 0;
2078 break;
2080 case OPTION_UNICODE:
2081 if (streq (optarg, "default") || streq (optarg, "d"))
2082 unicode_display = unicode_default;
2083 else if (streq (optarg, "locale") || streq (optarg, "l"))
2084 unicode_display = unicode_locale;
2085 else if (streq (optarg, "escape") || streq (optarg, "e"))
2086 unicode_display = unicode_escape;
2087 else if (streq (optarg, "invalid") || streq (optarg, "i"))
2088 unicode_display = unicode_invalid;
2089 else if (streq (optarg, "hex") || streq (optarg, "x"))
2090 unicode_display = unicode_hex;
2091 else if (streq (optarg, "highlight") || streq (optarg, "h"))
2092 unicode_display = unicode_highlight;
2093 else
2094 fatal (_("invalid argument to -U/--unicode: %s"), optarg);
2095 break;
2097 case 'V':
2098 show_version = 1;
2099 break;
2100 case 'W':
2101 non_weak = 1;
2102 break;
2103 case 'X':
2104 /* Ignored for (partial) AIX compatibility. On AIX, the
2105 argument has values 32, 64, or 32_64, and specifies that
2106 only 32-bit, only 64-bit, or both kinds of objects should
2107 be examined. The default is 32. So plain AIX nm on a
2108 library archive with both kinds of objects will ignore
2109 the 64-bit ones. For GNU nm, the default is and always
2110 has been -X 32_64, and other options are not supported. */
2111 if (strcmp (optarg, "32_64") != 0)
2112 fatal (_("Only -X 32_64 is supported"));
2113 break;
2115 case OPTION_TARGET: /* --target */
2116 target = optarg;
2117 break;
2119 case OPTION_PLUGIN: /* --plugin */
2120 #if BFD_SUPPORTS_PLUGINS
2121 bfd_plugin_set_plugin (optarg);
2122 #else
2123 fatal (_("sorry - this program has been built without plugin support\n"));
2124 #endif
2125 break;
2127 case OPTION_IFUNC_CHARS:
2128 ifunc_type_chars = optarg;
2129 break;
2131 case 0: /* A long option that just sets a flag. */
2132 break;
2134 default:
2135 usage (stderr, 1);
2139 if (show_version)
2140 print_version ("nm");
2142 if (sort_by_size && undefined_only)
2144 non_fatal (_("Using the --size-sort and --undefined-only options together"));
2145 non_fatal (_("will produce no output, since undefined symbols have no size."));
2146 return 0;
2149 /* OK, all options now parsed. If no filename specified, do a.out. */
2150 if (optind == argc)
2151 return !display_file ("a.out");
2153 retval = 0;
2155 if (argc - optind > 1)
2156 filename_per_file = 1;
2158 /* We were given several filenames to do. */
2159 while (optind < argc)
2161 if (!display_file (argv[optind++]))
2162 retval++;
2165 exit (retval);
2166 return retval;