DIGEST: Documentation
[binutils-gdb.git] / binutils / nm.c
blob8b6b249a95124300b6810a226cd679d631d37e22
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;
67 struct extended_symbol_info
69 symbol_info *sinfo;
70 bfd_vma ssize;
71 elf_symbol_type *elfinfo;
72 coff_symbol_type *coffinfo;
73 /* FIXME: We should add more fields for Type, Line, Section. */
75 #define SYM_VALUE(sym) (sym->sinfo->value)
76 #define SYM_TYPE(sym) (sym->sinfo->type)
77 #define SYM_STAB_NAME(sym) (sym->sinfo->stab_name)
78 #define SYM_STAB_DESC(sym) (sym->sinfo->stab_desc)
79 #define SYM_STAB_OTHER(sym) (sym->sinfo->stab_other)
80 #define SYM_SIZE(sym) \
81 (sym->elfinfo ? sym->elfinfo->internal_elf_sym.st_size: sym->ssize)
83 /* The output formatting functions. */
84 static void print_object_filename_bsd (const char *);
85 static void print_object_filename_sysv (const char *);
86 static void print_object_filename_posix (const char *);
87 static void do_not_print_object_filename (const char *);
89 static void print_archive_filename_bsd (const char *);
90 static void print_archive_filename_sysv (const char *);
91 static void print_archive_filename_posix (const char *);
92 static void do_not_print_archive_filename (const char *);
94 static void print_archive_member_bsd (const char *, const char *);
95 static void print_archive_member_sysv (const char *, const char *);
96 static void print_archive_member_posix (const char *, const char *);
97 static void do_not_print_archive_member (const char *, const char *);
99 static void print_symbol_filename_bsd (bfd *, bfd *);
100 static void print_symbol_filename_sysv (bfd *, bfd *);
101 static void print_symbol_filename_posix (bfd *, bfd *);
102 static void do_not_print_symbol_filename (bfd *, bfd *);
104 static void print_symbol_info_bsd (struct extended_symbol_info *, bfd *);
105 static void print_symbol_info_sysv (struct extended_symbol_info *, bfd *);
106 static void print_symbol_info_posix (struct extended_symbol_info *, bfd *);
107 static void just_print_symbol_name (struct extended_symbol_info *, bfd *);
109 static void print_value (bfd *, bfd_vma);
111 /* Support for different output formats. */
112 struct output_fns
114 /* Print the name of an object file given on the command line. */
115 void (*print_object_filename) (const char *);
117 /* Print the name of an archive file given on the command line. */
118 void (*print_archive_filename) (const char *);
120 /* Print the name of an archive member file. */
121 void (*print_archive_member) (const char *, const char *);
123 /* Print the name of the file (and archive, if there is one)
124 containing a symbol. */
125 void (*print_symbol_filename) (bfd *, bfd *);
127 /* Print a line of information about a symbol. */
128 void (*print_symbol_info) (struct extended_symbol_info *, bfd *);
131 /* Indices in `formats'. */
132 enum formats
134 FORMAT_BSD = 0,
135 FORMAT_SYSV,
136 FORMAT_POSIX,
137 FORMAT_JUST_SYMBOLS,
138 FORMAT_MAX
141 #define FORMAT_DEFAULT FORMAT_BSD
143 static const struct output_fns formats[FORMAT_MAX] =
145 {print_object_filename_bsd,
146 print_archive_filename_bsd,
147 print_archive_member_bsd,
148 print_symbol_filename_bsd,
149 print_symbol_info_bsd},
150 {print_object_filename_sysv,
151 print_archive_filename_sysv,
152 print_archive_member_sysv,
153 print_symbol_filename_sysv,
154 print_symbol_info_sysv},
155 {print_object_filename_posix,
156 print_archive_filename_posix,
157 print_archive_member_posix,
158 print_symbol_filename_posix,
159 print_symbol_info_posix},
160 {do_not_print_object_filename,
161 do_not_print_archive_filename,
162 do_not_print_archive_member,
163 do_not_print_symbol_filename,
164 just_print_symbol_name}
168 /* The output format to use. */
169 static const struct output_fns *format = &formats[FORMAT_DEFAULT];
170 static unsigned int print_format = FORMAT_DEFAULT;
171 static const char *print_format_string = NULL;
173 /* Command options. */
175 static int do_demangle = 0; /* Pretty print C++ symbol names. */
176 static int external_only = 0; /* Print external symbols only. */
177 static int defined_only = 0; /* Print defined symbols only. */
178 static int non_weak = 0; /* Ignore weak symbols. */
179 static int no_sort = 0; /* Don't sort; print syms in order found. */
180 static int print_debug_syms = 0;/* Print debugger-only symbols too. */
181 static int print_armap = 0; /* Describe __.SYMDEF data in archive files. */
182 static int print_size = 0; /* Print size of defined symbols. */
183 static int reverse_sort = 0; /* Sort in downward(alpha or numeric) order. */
184 static int sort_numerically = 0;/* Sort in numeric rather than alpha order. */
185 static int sort_by_size = 0; /* Sort by size of symbol. */
186 static int undefined_only = 0; /* Print undefined symbols only. */
187 static int dynamic = 0; /* Print dynamic symbols. */
188 static int show_version = 0; /* Show the version number. */
189 static int show_synthetic = 0; /* Display synthesized symbols too. */
190 static int line_numbers = 0; /* Print line numbers for symbols. */
191 static int allow_special_symbols = 0; /* Allow special symbols. */
192 static int with_symbol_versions = -1; /* Output symbol version information. */
193 static int quiet = 0; /* Suppress "no symbols" diagnostic. */
195 /* The characters to use for global and local ifunc symbols. */
196 #if DEFAULT_F_FOR_IFUNC_SYMBOLS
197 static const char * ifunc_type_chars = "Ff";
198 #else
199 static const char * ifunc_type_chars = NULL;
200 #endif
202 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
204 /* When to print the names of files. Not mutually exclusive in SYSV format. */
205 static int filename_per_file = 0; /* Once per file, on its own line. */
206 static int filename_per_symbol = 0; /* Once per symbol, at start of line. */
208 static int print_width = 0;
209 static int print_radix = 16;
210 /* Print formats for printing stab info. */
211 static char other_format[] = "%02x";
212 static char desc_format[] = "%04x";
214 static char *target = NULL;
215 #if BFD_SUPPORTS_PLUGINS
216 static const char *plugin_target = "plugin";
217 #else
218 static const char *plugin_target = NULL;
219 #endif
221 typedef enum unicode_display_type
223 unicode_default = 0,
224 unicode_locale,
225 unicode_escape,
226 unicode_hex,
227 unicode_highlight,
228 unicode_invalid
229 } unicode_display_type;
231 static unicode_display_type unicode_display = unicode_default;
233 enum long_option_values
235 OPTION_TARGET = 200,
236 OPTION_PLUGIN,
237 OPTION_SIZE_SORT,
238 OPTION_RECURSE_LIMIT,
239 OPTION_NO_RECURSE_LIMIT,
240 OPTION_IFUNC_CHARS,
241 OPTION_UNICODE,
242 OPTION_QUIET
245 static struct option long_options[] =
247 {"debug-syms", no_argument, &print_debug_syms, 1},
248 {"demangle", optional_argument, 0, 'C'},
249 {"dynamic", no_argument, &dynamic, 1},
250 {"extern-only", no_argument, &external_only, 1},
251 {"format", required_argument, 0, 'f'},
252 {"help", no_argument, 0, 'h'},
253 {"ifunc-chars", required_argument, 0, OPTION_IFUNC_CHARS},
254 {"just-symbols", no_argument, 0, 'j'},
255 {"line-numbers", no_argument, 0, 'l'},
256 {"no-cplus", no_argument, &do_demangle, 0}, /* Linux compatibility. */
257 {"no-demangle", no_argument, &do_demangle, 0},
258 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
259 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
260 {"no-sort", no_argument, 0, 'p'},
261 {"numeric-sort", no_argument, 0, 'n'},
262 {"plugin", required_argument, 0, OPTION_PLUGIN},
263 {"portability", no_argument, 0, 'P'},
264 {"print-armap", no_argument, &print_armap, 1},
265 {"print-file-name", no_argument, 0, 'o'},
266 {"print-size", no_argument, 0, 'S'},
267 {"quiet", no_argument, 0, OPTION_QUIET},
268 {"radix", required_argument, 0, 't'},
269 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
270 {"recursion-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
271 {"reverse-sort", no_argument, &reverse_sort, 1},
272 {"size-sort", no_argument, 0, OPTION_SIZE_SORT},
273 {"special-syms", no_argument, &allow_special_symbols, 1},
274 {"synthetic", no_argument, &show_synthetic, 1},
275 {"target", required_argument, 0, OPTION_TARGET},
276 {"defined-only", no_argument, 0, 'U'},
277 {"undefined-only", no_argument, 0, 'u'},
278 {"unicode", required_argument, NULL, OPTION_UNICODE},
279 {"version", no_argument, &show_version, 1},
280 {"no-weak", no_argument, 0, 'W'},
281 {"with-symbol-versions", no_argument, &with_symbol_versions, 1},
282 {"without-symbol-versions", no_argument, &with_symbol_versions, 0},
283 {0, no_argument, 0, 0}
286 /* Some error-reporting functions. */
288 ATTRIBUTE_NORETURN static void
289 usage (FILE *stream, int status)
291 fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
292 fprintf (stream, _(" List symbols in [file(s)] (a.out by default).\n"));
293 fprintf (stream, _(" The options are:\n"));
294 fprintf (stream, _("\
295 -a, --debug-syms Display debugger-only symbols\n"));
296 fprintf (stream, _("\
297 -A, --print-file-name Print name of the input file before every symbol\n"));
298 fprintf (stream, _("\
299 -B Same as --format=bsd\n"));
300 fprintf (stream, _("\
301 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n"));
302 display_demangler_styles (stream, _("\
303 STYLE can be "));
304 fprintf (stream, _("\
305 --no-demangle Do not demangle low-level symbol names\n"));
306 fprintf (stream, _("\
307 --recurse-limit Enable a demangling recursion limit. (default)\n"));
308 fprintf (stream, _("\
309 --no-recurse-limit Disable a demangling recursion limit.\n"));
310 fprintf (stream, _("\
311 -D, --dynamic Display dynamic symbols instead of normal symbols\n"));
312 fprintf (stream, _("\
313 -e (ignored)\n"));
314 fprintf (stream, _("\
315 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
316 `sysv', `posix' or 'just-symbols'.\n\
317 The default is `bsd'\n"));
318 fprintf (stream, _("\
319 -g, --extern-only Display only external symbols\n"));
320 fprintf (stream, _("\
321 --ifunc-chars=CHARS Characters to use when displaying ifunc symbols\n"));
322 fprintf (stream, _("\
323 -j, --just-symbols Same as --format=just-symbols\n"));
324 fprintf (stream, _("\
325 -l, --line-numbers Use debugging information to find a filename and\n\
326 line number for each symbol\n"));
327 fprintf (stream, _("\
328 -n, --numeric-sort Sort symbols numerically by address\n"));
329 fprintf (stream, _("\
330 -o Same as -A\n"));
331 fprintf (stream, _("\
332 -p, --no-sort Do not sort the symbols\n"));
333 fprintf (stream, _("\
334 -P, --portability Same as --format=posix\n"));
335 fprintf (stream, _("\
336 -r, --reverse-sort Reverse the sense of the sort\n"));
337 #if BFD_SUPPORTS_PLUGINS
338 fprintf (stream, _("\
339 --plugin NAME Load the specified plugin\n"));
340 #endif
341 fprintf (stream, _("\
342 -S, --print-size Print size of defined symbols\n"));
343 fprintf (stream, _("\
344 -s, --print-armap Include index for symbols from archive members\n"));
345 fprintf (stream, _("\
346 --quiet Suppress \"no symbols\" diagnostic\n"));
347 fprintf (stream, _("\
348 --size-sort Sort symbols by size\n"));
349 fprintf (stream, _("\
350 --special-syms Include special symbols in the output\n"));
351 fprintf (stream, _("\
352 --synthetic Display synthetic symbols as well\n"));
353 fprintf (stream, _("\
354 -t, --radix=RADIX Use RADIX for printing symbol values\n"));
355 fprintf (stream, _("\
356 --target=BFDNAME Specify the target object format as BFDNAME\n"));
357 fprintf (stream, _("\
358 -u, --undefined-only Display only undefined symbols\n"));
359 fprintf (stream, _("\
360 -U, --defined-only Display only defined symbols\n"));
361 fprintf (stream, _("\
362 --unicode={default|show|invalid|hex|escape|highlight}\n\
363 Specify how to treat UTF-8 encoded unicode characters\n"));
364 fprintf (stream, _("\
365 -W, --no-weak Ignore weak symbols\n"));
366 fprintf (stream, _("\
367 --with-symbol-versions Display version strings after symbol names\n"));
368 fprintf (stream, _("\
369 -X 32_64 (ignored)\n"));
370 fprintf (stream, _("\
371 @FILE Read options from FILE\n"));
372 fprintf (stream, _("\
373 -h, --help Display this information\n"));
374 fprintf (stream, _("\
375 -V, --version Display this program's version number\n"));
377 list_supported_targets (program_name, stream);
378 if (REPORT_BUGS_TO[0] && status == 0)
379 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
380 exit (status);
383 /* Set the radix for the symbol value and size according to RADIX. */
385 static void
386 set_print_radix (char *radix)
388 switch (*radix)
390 case 'x': print_radix = 16; break;
391 case 'd': print_radix = 10; break;
392 case 'o': print_radix = 8; break;
394 default:
395 fatal (_("%s: invalid radix"), radix);
398 other_format[3] = desc_format[3] = *radix;
401 static void
402 set_output_format (char *f)
404 int i;
406 switch (*f)
408 case 'b':
409 case 'B':
410 i = FORMAT_BSD;
411 break;
412 case 'p':
413 case 'P':
414 i = FORMAT_POSIX;
415 break;
416 case 's':
417 case 'S':
418 i = FORMAT_SYSV;
419 break;
420 case 'j':
421 case 'J':
422 i = FORMAT_JUST_SYMBOLS;
423 break;
424 default:
425 fatal (_("%s: invalid output format"), f);
427 format = &formats[i];
428 print_format = i;
431 static const char *
432 get_elf_symbol_type (unsigned int type)
434 static char *bufp;
435 int n;
437 switch (type)
439 case STT_NOTYPE: return "NOTYPE";
440 case STT_OBJECT: return "OBJECT";
441 case STT_FUNC: return "FUNC";
442 case STT_SECTION: return "SECTION";
443 case STT_FILE: return "FILE";
444 case STT_COMMON: return "COMMON";
445 case STT_TLS: return "TLS";
448 free (bufp);
449 if (type >= STT_LOPROC && type <= STT_HIPROC)
450 n = asprintf (&bufp, _("<processor specific>: %d"), type);
451 else if (type >= STT_LOOS && type <= STT_HIOS)
452 n = asprintf (&bufp, _("<OS specific>: %d"), type);
453 else
454 n = asprintf (&bufp, _("<unknown>: %d"), type);
455 if (n < 0)
456 fatal ("%s", xstrerror (errno));
457 return bufp;
460 static const char *
461 get_coff_symbol_type (const struct internal_syment *sym)
463 static char *bufp;
464 int n;
466 switch (sym->n_sclass)
468 case C_BLOCK: return "Block";
469 case C_FILE: return "File";
470 case C_LINE: return "Line";
473 if (!sym->n_type)
474 return "None";
476 switch (DTYPE(sym->n_type))
478 case DT_FCN: return "Function";
479 case DT_PTR: return "Pointer";
480 case DT_ARY: return "Array";
483 free (bufp);
484 n = asprintf (&bufp, _("<unknown>: %d/%d"), sym->n_sclass, sym->n_type);
485 if (n < 0)
486 fatal ("%s", xstrerror (errno));
487 return bufp;
490 /* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
491 The conversion format is controlled by the unicode_display variable.
492 Returns the number of characters added to OUT.
493 Returns the number of bytes consumed from IN in CONSUMED.
494 Always consumes at least one byte and displays at least one character. */
496 static unsigned int
497 display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
499 char * orig_out = out;
500 unsigned int nchars = 0;
501 unsigned int j;
503 if (unicode_display == unicode_default)
504 goto invalid;
506 if (in[0] < 0xc0)
507 goto invalid;
509 if ((in[1] & 0xc0) != 0x80)
510 goto invalid;
512 if ((in[0] & 0x20) == 0)
514 nchars = 2;
515 goto valid;
518 if ((in[2] & 0xc0) != 0x80)
519 goto invalid;
521 if ((in[0] & 0x10) == 0)
523 nchars = 3;
524 goto valid;
527 if ((in[3] & 0xc0) != 0x80)
528 goto invalid;
530 nchars = 4;
532 valid:
533 switch (unicode_display)
535 case unicode_locale:
536 /* Copy the bytes into the output buffer as is. */
537 memcpy (out, in, nchars);
538 out += nchars;
539 break;
541 case unicode_invalid:
542 case unicode_hex:
543 out += sprintf (out, "%c", unicode_display == unicode_hex ? '<' : '{');
544 out += sprintf (out, "0x");
545 for (j = 0; j < nchars; j++)
546 out += sprintf (out, "%02x", in [j]);
547 out += sprintf (out, "%c", unicode_display == unicode_hex ? '>' : '}');
548 break;
550 case unicode_highlight:
551 if (isatty (1))
552 out += sprintf (out, "\x1B[31;47m"); /* Red. */
553 /* Fall through. */
554 case unicode_escape:
555 switch (nchars)
557 case 2:
558 out += sprintf (out, "\\u%02x%02x",
559 ((in[0] & 0x1c) >> 2),
560 ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
561 break;
563 case 3:
564 out += sprintf (out, "\\u%02x%02x",
565 ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
566 ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
567 break;
569 case 4:
570 out += sprintf (out, "\\u%02x%02x%02x",
571 ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
572 ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
573 ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
574 break;
575 default:
576 /* URG. */
577 break;
580 if (unicode_display == unicode_highlight && isatty (1))
581 out += sprintf (out, "\033[0m"); /* Default colour. */
582 break;
584 default:
585 /* URG */
586 break;
589 * consumed = nchars;
590 return out - orig_out;
592 invalid:
593 /* Not a valid UTF-8 sequence. */
594 *out = *in;
595 * consumed = 1;
596 return 1;
599 /* Convert any UTF-8 encoded characters in NAME into the form specified by
600 unicode_display. Also converts control characters. Returns a static
601 buffer if conversion was necessary.
602 Code stolen from objdump.c:sanitize_string(). */
604 static const char *
605 convert_utf8 (const char * in)
607 static char * buffer = NULL;
608 static size_t buffer_len = 0;
609 const char * original = in;
610 char * out;
612 /* Paranoia. */
613 if (in == NULL)
614 return "";
616 /* See if any conversion is necessary.
617 In the majority of cases it will not be needed. */
620 unsigned char c = *in++;
622 if (c == 0)
623 return original;
625 if (ISCNTRL (c))
626 break;
628 if (unicode_display != unicode_default && c >= 0xc0)
629 break;
631 while (1);
633 /* Copy the input, translating as needed. */
634 in = original;
635 if (buffer_len < (strlen (in) * 9))
637 free ((void *) buffer);
638 buffer_len = strlen (in) * 9;
639 buffer = xmalloc (buffer_len + 1);
642 out = buffer;
645 unsigned char c = *in++;
647 if (c == 0)
648 break;
650 if (ISCNTRL (c))
652 *out++ = '^';
653 *out++ = c + 0x40;
655 else if (unicode_display != unicode_default && c >= 0xc0)
657 unsigned int num_consumed;
659 out += display_utf8 ((const unsigned char *)(in - 1), out, & num_consumed);
660 in += num_consumed - 1;
662 else
663 *out++ = c;
665 while (1);
667 *out = 0;
668 return buffer;
671 /* Print symbol name NAME, read from ABFD, with printf format FORM,
672 demangling it if requested. */
674 static void
675 print_symname (const char *form, struct extended_symbol_info *info,
676 const char *name, bfd *abfd)
678 char *alloc = NULL;
679 char *atver = NULL;
681 if (name == NULL)
682 name = info->sinfo->name;
684 if (!with_symbol_versions
685 && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
687 atver = strchr (name, '@');
688 if (atver)
689 *atver = 0;
692 if (do_demangle && *name)
694 alloc = bfd_demangle (abfd, name, demangle_flags);
695 if (alloc != NULL)
696 name = alloc;
699 if (unicode_display != unicode_default)
701 name = convert_utf8 (name);
704 if (info != NULL && info->elfinfo && with_symbol_versions)
706 const char *version_string;
707 bool hidden;
709 version_string
710 = bfd_get_symbol_version_string (abfd, &info->elfinfo->symbol,
711 false, &hidden);
712 if (version_string && version_string[0])
714 const char *at = "@@";
715 if (hidden || bfd_is_und_section (info->elfinfo->symbol.section))
716 at = "@";
717 alloc = reconcat (alloc, name, at, version_string, NULL);
718 if (alloc != NULL)
719 name = alloc;
722 printf (form, name);
723 if (atver)
724 *atver = '@';
725 free (alloc);
728 static void
729 print_symdef_entry (bfd *abfd)
731 symindex idx = BFD_NO_MORE_SYMBOLS;
732 carsym *thesym;
733 bool everprinted = false;
735 for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
736 idx != BFD_NO_MORE_SYMBOLS;
737 idx = bfd_get_next_mapent (abfd, idx, &thesym))
739 if (!everprinted)
741 printf (_("\nArchive index:\n"));
742 everprinted = true;
744 if (thesym->name != NULL)
746 print_symname ("%s", NULL, thesym->name, abfd);
747 bfd *elt = bfd_get_elt_at_index (abfd, idx);
748 if (elt)
749 printf (" in %s\n", bfd_get_filename (elt));
750 else
751 printf ("\n");
757 /* True when we can report missing plugin error. */
758 bool report_plugin_err = true;
760 /* Choose which symbol entries to print;
761 compact them downward to get rid of the rest.
762 Return the number of symbols to be printed. */
764 static long
765 filter_symbols (bfd *abfd, bool is_dynamic, void *minisyms,
766 long symcount, unsigned int size)
768 bfd_byte *from, *fromend, *to;
769 asymbol *store;
771 store = bfd_make_empty_symbol (abfd);
772 if (store == NULL)
773 bfd_fatal (bfd_get_filename (abfd));
775 from = (bfd_byte *) minisyms;
776 fromend = from + symcount * size;
777 to = (bfd_byte *) minisyms;
779 for (; from < fromend; from += size)
781 int keep = 0;
782 asymbol *sym;
784 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from, store);
785 if (sym == NULL)
786 continue;
788 if (sym->name != NULL
789 && sym->name[0] == '_'
790 && sym->name[1] == '_'
791 && strcmp (sym->name + (sym->name[2] == '_'), "__gnu_lto_slim") == 0
792 && report_plugin_err)
794 report_plugin_err = false;
795 non_fatal (_("%s: plugin needed to handle lto object"),
796 bfd_get_filename (abfd));
799 if (undefined_only)
800 keep = bfd_is_und_section (sym->section);
801 else if (external_only)
802 /* PR binutls/12753: Unique symbols are global too. */
803 keep = ((sym->flags & (BSF_GLOBAL
804 | BSF_WEAK
805 | BSF_GNU_UNIQUE)) != 0
806 || bfd_is_und_section (sym->section)
807 || bfd_is_com_section (sym->section));
808 else if (non_weak)
809 keep = ((sym->flags & BSF_WEAK) == 0);
810 else
811 keep = 1;
813 if (keep
814 && ! print_debug_syms
815 && (sym->flags & BSF_DEBUGGING) != 0)
816 keep = 0;
818 if (keep
819 && sort_by_size
820 && (bfd_is_abs_section (sym->section)
821 || bfd_is_und_section (sym->section)))
822 keep = 0;
824 if (keep
825 && defined_only)
827 if (bfd_is_und_section (sym->section))
828 keep = 0;
831 if (keep
832 && bfd_is_target_special_symbol (abfd, sym)
833 && ! allow_special_symbols)
834 keep = 0;
836 if (keep)
838 if (to != from)
839 memcpy (to, from, size);
840 to += size;
844 return (to - (bfd_byte *) minisyms) / size;
847 /* These globals are used to pass information into the sorting
848 routines. */
849 static bfd *sort_bfd;
850 static bool sort_dynamic;
851 static asymbol *sort_x;
852 static asymbol *sort_y;
854 /* Symbol-sorting predicates */
855 #define valueof(x) ((x)->section->vma + (x)->value)
857 /* Numeric sorts. Undefined symbols are always considered "less than"
858 defined symbols with zero values. Common symbols are not treated
859 specially -- i.e., their sizes are used as their "values". */
861 static int
862 non_numeric_forward (const void *P_x, const void *P_y)
864 asymbol *x, *y;
865 const char *xn, *yn;
867 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
868 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
869 if (x == NULL || y == NULL)
870 bfd_fatal (bfd_get_filename (sort_bfd));
872 xn = bfd_asymbol_name (x);
873 yn = bfd_asymbol_name (y);
875 if (yn == NULL)
876 return xn != NULL;
877 if (xn == NULL)
878 return -1;
880 /* Solaris 2.5 has a bug in strcoll.
881 strcoll returns invalid values when confronted with empty strings. */
882 if (*yn == '\0')
883 return *xn != '\0';
884 if (*xn == '\0')
885 return -1;
887 return strcoll (xn, yn);
890 static int
891 non_numeric_reverse (const void *x, const void *y)
893 return - non_numeric_forward (x, y);
896 static int
897 numeric_forward (const void *P_x, const void *P_y)
899 asymbol *x, *y;
900 asection *xs, *ys;
902 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
903 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
904 if (x == NULL || y == NULL)
905 bfd_fatal (bfd_get_filename (sort_bfd));
907 xs = bfd_asymbol_section (x);
908 ys = bfd_asymbol_section (y);
910 if (bfd_is_und_section (xs))
912 if (! bfd_is_und_section (ys))
913 return -1;
915 else if (bfd_is_und_section (ys))
916 return 1;
917 else if (valueof (x) != valueof (y))
918 return valueof (x) < valueof (y) ? -1 : 1;
920 return non_numeric_forward (P_x, P_y);
923 static int
924 numeric_reverse (const void *x, const void *y)
926 return - numeric_forward (x, y);
929 static int (*(sorters[2][2])) (const void *, const void *) =
931 { non_numeric_forward, non_numeric_reverse },
932 { numeric_forward, numeric_reverse }
935 /* This sort routine is used by sort_symbols_by_size. It is similar
936 to numeric_forward, but when symbols have the same value it sorts
937 by section VMA. This simplifies the sort_symbols_by_size code
938 which handles symbols at the end of sections. Also, this routine
939 tries to sort file names before other symbols with the same value.
940 That will make the file name have a zero size, which will make
941 sort_symbols_by_size choose the non file name symbol, leading to
942 more meaningful output. For similar reasons, this code sorts
943 gnu_compiled_* and gcc2_compiled before other symbols with the same
944 value. */
946 static int
947 size_forward1 (const void *P_x, const void *P_y)
949 asymbol *x, *y;
950 asection *xs, *ys;
951 const char *xn, *yn;
952 size_t xnl, ynl;
953 int xf, yf;
955 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
956 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
957 if (x == NULL || y == NULL)
958 bfd_fatal (bfd_get_filename (sort_bfd));
960 xs = bfd_asymbol_section (x);
961 ys = bfd_asymbol_section (y);
963 if (bfd_is_und_section (xs))
964 abort ();
965 if (bfd_is_und_section (ys))
966 abort ();
968 if (valueof (x) != valueof (y))
969 return valueof (x) < valueof (y) ? -1 : 1;
971 if (xs->vma != ys->vma)
972 return xs->vma < ys->vma ? -1 : 1;
974 xn = bfd_asymbol_name (x);
975 yn = bfd_asymbol_name (y);
976 xnl = strlen (xn);
977 ynl = strlen (yn);
979 /* The symbols gnu_compiled and gcc2_compiled convey even less
980 information than the file name, so sort them out first. */
982 xf = (strstr (xn, "gnu_compiled") != NULL
983 || strstr (xn, "gcc2_compiled") != NULL);
984 yf = (strstr (yn, "gnu_compiled") != NULL
985 || strstr (yn, "gcc2_compiled") != NULL);
987 if (xf && ! yf)
988 return -1;
989 if (! xf && yf)
990 return 1;
992 /* We use a heuristic for the file name. It may not work on non
993 Unix systems, but it doesn't really matter; the only difference
994 is precisely which symbol names get printed. */
996 #define file_symbol(s, sn, snl) \
997 (((s)->flags & BSF_FILE) != 0 \
998 || ((snl) > 2 \
999 && (sn)[(snl) - 2] == '.' \
1000 && ((sn)[(snl) - 1] == 'o' \
1001 || (sn)[(snl) - 1] == 'a')))
1003 xf = file_symbol (x, xn, xnl);
1004 yf = file_symbol (y, yn, ynl);
1006 if (xf && ! yf)
1007 return -1;
1008 if (! xf && yf)
1009 return 1;
1011 return non_numeric_forward (P_x, P_y);
1014 /* This sort routine is used by sort_symbols_by_size. It is sorting
1015 an array of size_sym structures into size order. */
1017 static int
1018 size_forward2 (const void *P_x, const void *P_y)
1020 const struct size_sym *x = (const struct size_sym *) P_x;
1021 const struct size_sym *y = (const struct size_sym *) P_y;
1023 if (x->size < y->size)
1024 return reverse_sort ? 1 : -1;
1025 else if (x->size > y->size)
1026 return reverse_sort ? -1 : 1;
1027 else
1028 return sorters[0][reverse_sort] (x->minisym, y->minisym);
1031 /* Sort the symbols by size. ELF provides a size but for other formats
1032 we have to make a guess by assuming that the difference between the
1033 address of a symbol and the address of the next higher symbol is the
1034 size. */
1036 static long
1037 sort_symbols_by_size (bfd *abfd, bool is_dynamic, void *minisyms,
1038 long symcount, unsigned int size,
1039 struct size_sym **symsizesp)
1041 struct size_sym *symsizes;
1042 bfd_byte *from, *fromend;
1043 asymbol *sym = NULL;
1044 asymbol *store_sym, *store_next;
1046 qsort (minisyms, symcount, size, size_forward1);
1048 /* We are going to return a special set of symbols and sizes to
1049 print. */
1050 symsizes = (struct size_sym *) xmalloc (symcount * sizeof (struct size_sym));
1051 *symsizesp = symsizes;
1053 /* Note that filter_symbols has already removed all absolute and
1054 undefined symbols. Here we remove all symbols whose size winds
1055 up as zero. */
1056 from = (bfd_byte *) minisyms;
1057 fromend = from + symcount * size;
1059 store_sym = sort_x;
1060 store_next = sort_y;
1062 if (from < fromend)
1064 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, (const void *) from,
1065 store_sym);
1066 if (sym == NULL)
1067 bfd_fatal (bfd_get_filename (abfd));
1070 for (; from < fromend; from += size)
1072 asymbol *next;
1073 asection *sec;
1074 bfd_vma sz;
1075 asymbol *temp;
1077 if (from + size < fromend)
1079 next = bfd_minisymbol_to_symbol (abfd,
1080 is_dynamic,
1081 (const void *) (from + size),
1082 store_next);
1083 if (next == NULL)
1084 bfd_fatal (bfd_get_filename (abfd));
1086 else
1087 next = NULL;
1089 sec = bfd_asymbol_section (sym);
1091 /* Synthetic symbols don't have a full type set of data available, thus
1092 we can't rely on that information for the symbol size. Ditto for
1093 bfd/section.c:global_syms like *ABS*. */
1094 if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0
1095 && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1096 sz = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
1097 else if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0
1098 && bfd_is_com_section (sec))
1099 sz = sym->value;
1100 else
1102 if (from + size < fromend
1103 && sec == bfd_asymbol_section (next))
1104 sz = valueof (next) - valueof (sym);
1105 else
1106 sz = (bfd_section_vma (sec)
1107 + bfd_section_size (sec)
1108 - valueof (sym));
1111 if (sz != 0)
1113 symsizes->minisym = (const void *) from;
1114 symsizes->size = sz;
1115 ++symsizes;
1118 sym = next;
1120 temp = store_sym;
1121 store_sym = store_next;
1122 store_next = temp;
1125 symcount = symsizes - *symsizesp;
1127 /* We must now sort again by size. */
1128 qsort ((void *) *symsizesp, symcount, sizeof (struct size_sym), size_forward2);
1130 return symcount;
1133 /* This function is used to get the relocs for a particular section.
1134 It is called via bfd_map_over_sections. */
1136 static void
1137 get_relocs (bfd *abfd, asection *sec, void *dataarg)
1139 struct lineno_cache *data = (struct lineno_cache *) dataarg;
1141 *data->secs = sec;
1142 *data->relocs = NULL;
1143 *data->relcount = 0;
1145 if ((sec->flags & SEC_RELOC) != 0)
1147 long relsize = bfd_get_reloc_upper_bound (abfd, sec);
1148 if (relsize > 0)
1150 *data->relocs = (arelent **) xmalloc (relsize);
1151 *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs,
1152 data->syms);
1156 ++data->secs;
1157 ++data->relocs;
1158 ++data->relcount;
1161 static void
1162 free_lineno_cache (bfd *abfd)
1164 struct lineno_cache *lc = bfd_usrdata (abfd);
1166 if (lc)
1168 unsigned int seccount = bfd_count_sections (abfd);
1169 for (unsigned int i = 0; i < seccount; i++)
1170 if (lc->relocs[i] != NULL)
1171 free (lc->relocs[i]);
1172 free (lc->relcount);
1173 free (lc->relocs);
1174 free (lc->secs);
1175 free (lc->syms);
1176 free (lc);
1177 bfd_set_usrdata (abfd, NULL);
1181 /* Print a single symbol. */
1183 static void
1184 print_symbol (bfd * abfd,
1185 asymbol * sym,
1186 bfd_vma ssize,
1187 bfd * archive_bfd)
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;
1209 info.ssize = ssize;
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;
1217 else
1219 info.elfinfo = elf_symbol_from (sym);
1220 info.coffinfo = coff_symbol_from (sym);
1223 format->print_symbol_info (&info, abfd);
1225 if (line_numbers)
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. */
1234 if (lc == NULL)
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);
1242 if (symsize <= 0)
1243 lc->symcount = -1;
1244 else
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)))
1255 unsigned int i;
1256 const char *symname;
1257 unsigned int seccount = bfd_count_sections (abfd);
1259 /* For an undefined symbol, we try to find a reloc for the
1260 symbol, and print the line number of the reloc. */
1261 if (lc->relocs == NULL)
1263 lc->secs = xmalloc (seccount * sizeof (*lc->secs));
1264 lc->relocs = xmalloc (seccount * sizeof (*lc->relocs));
1265 lc->relcount = xmalloc (seccount * sizeof (*lc->relcount));
1267 struct lineno_cache rinfo = *lc;
1268 bfd_map_over_sections (abfd, get_relocs, &rinfo);
1271 symname = bfd_asymbol_name (sym);
1272 for (i = 0; i < seccount; i++)
1274 long j;
1276 for (j = 0; j < lc->relcount[i]; j++)
1278 arelent *r;
1280 r = lc->relocs[i][j];
1281 if (r->sym_ptr_ptr != NULL
1282 && (*r->sym_ptr_ptr)->section == sym->section
1283 && (*r->sym_ptr_ptr)->value == sym->value
1284 && strcmp (symname,
1285 bfd_asymbol_name (*r->sym_ptr_ptr)) == 0
1286 && bfd_find_nearest_line (abfd, lc->secs[i], lc->syms,
1287 r->address, &filename,
1288 &functionname, &lineno)
1289 && filename != NULL)
1291 /* We only print the first one we find. */
1292 printf ("\t%s:%u", filename, lineno);
1293 i = seccount;
1294 break;
1299 else if (bfd_asymbol_section (sym)->owner == abfd)
1301 if ((bfd_find_line (abfd, lc->syms, sym, &filename, &lineno)
1302 || bfd_find_nearest_line (abfd, bfd_asymbol_section (sym),
1303 lc->syms, sym->value, &filename,
1304 &functionname, &lineno))
1305 && filename != NULL
1306 && lineno != 0)
1307 printf ("\t%s:%u", filename, lineno);
1311 putchar ('\n');
1314 /* Print the symbols when sorting by size. */
1316 static void
1317 print_size_symbols (bfd *abfd,
1318 bool is_dynamic,
1319 struct size_sym *symsizes,
1320 long symcount,
1321 bfd *archive_bfd)
1323 asymbol *store;
1324 struct size_sym *from;
1325 struct size_sym *fromend;
1327 store = bfd_make_empty_symbol (abfd);
1328 if (store == NULL)
1329 bfd_fatal (bfd_get_filename (abfd));
1331 from = symsizes;
1332 fromend = from + symcount;
1334 for (; from < fromend; from++)
1336 asymbol *sym;
1338 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from->minisym, store);
1339 if (sym == NULL)
1340 bfd_fatal (bfd_get_filename (abfd));
1342 print_symbol (abfd, sym, from->size, archive_bfd);
1347 /* Print the symbols of ABFD that are held in MINISYMS.
1349 If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD.
1351 SYMCOUNT is the number of symbols in MINISYMS.
1353 SIZE is the size of a symbol in MINISYMS. */
1355 static void
1356 print_symbols (bfd *abfd,
1357 bool is_dynamic,
1358 void *minisyms,
1359 long symcount,
1360 unsigned int size,
1361 bfd *archive_bfd)
1363 asymbol *store;
1364 bfd_byte *from;
1365 bfd_byte *fromend;
1367 store = bfd_make_empty_symbol (abfd);
1368 if (store == NULL)
1369 bfd_fatal (bfd_get_filename (abfd));
1371 from = (bfd_byte *) minisyms;
1372 fromend = from + symcount * size;
1374 for (; from < fromend; from += size)
1376 asymbol *sym;
1378 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from, store);
1379 if (sym == NULL)
1380 bfd_fatal (bfd_get_filename (abfd));
1382 print_symbol (abfd, sym, (bfd_vma) 0, archive_bfd);
1386 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
1388 static void
1389 display_rel_file (bfd *abfd, bfd *archive_bfd)
1391 long symcount;
1392 void *minisyms;
1393 unsigned int size;
1394 struct size_sym *symsizes;
1395 asymbol *synthsyms = NULL;
1397 if (! dynamic)
1399 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
1401 if (!quiet)
1402 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
1403 return;
1407 symcount = bfd_read_minisymbols (abfd, dynamic, &minisyms, &size);
1408 if (symcount <= 0)
1410 if (!quiet)
1411 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
1412 return;
1415 if (show_synthetic && size == sizeof (asymbol *))
1417 asymbol **static_syms = NULL;
1418 asymbol **dyn_syms = NULL;
1419 long static_count = 0;
1420 long dyn_count = 0;
1421 long synth_count;
1423 if (dynamic)
1425 dyn_count = symcount;
1426 dyn_syms = (asymbol **) minisyms;
1428 else
1430 long storage = bfd_get_dynamic_symtab_upper_bound (abfd);
1432 static_count = symcount;
1433 static_syms = (asymbol **) minisyms;
1435 if (storage > 0)
1437 dyn_syms = (asymbol **) xmalloc (storage);
1438 dyn_count = bfd_canonicalize_dynamic_symtab (abfd, dyn_syms);
1439 if (dyn_count < 0)
1440 dyn_count = 0;
1444 synth_count = bfd_get_synthetic_symtab (abfd, static_count, static_syms,
1445 dyn_count, dyn_syms, &synthsyms);
1446 if (synth_count > 0)
1448 asymbol **symp;
1449 long i;
1451 minisyms = xrealloc (minisyms,
1452 (symcount + synth_count + 1) * sizeof (*symp));
1453 symp = (asymbol **) minisyms + symcount;
1454 for (i = 0; i < synth_count; i++)
1455 *symp++ = synthsyms + i;
1456 *symp = 0;
1457 symcount += synth_count;
1459 if (!dynamic && dyn_syms != NULL)
1460 free (dyn_syms);
1463 /* lto_slim_object is set to false when a bfd is loaded with a compiler
1464 LTO plugin. */
1465 if (abfd->lto_slim_object)
1467 report_plugin_err = false;
1468 non_fatal (_("%s: plugin needed to handle lto object"),
1469 bfd_get_filename (abfd));
1472 /* Discard the symbols we don't want to print.
1473 It's OK to do this in place; we'll free the storage anyway
1474 (after printing). */
1476 symcount = filter_symbols (abfd, dynamic, minisyms, symcount, size);
1478 symsizes = NULL;
1479 if (! no_sort)
1481 sort_bfd = abfd;
1482 sort_dynamic = dynamic;
1483 sort_x = bfd_make_empty_symbol (abfd);
1484 sort_y = bfd_make_empty_symbol (abfd);
1485 if (sort_x == NULL || sort_y == NULL)
1486 bfd_fatal (bfd_get_filename (abfd));
1488 if (! sort_by_size)
1489 qsort (minisyms, symcount, size,
1490 sorters[sort_numerically][reverse_sort]);
1491 else
1492 symcount = sort_symbols_by_size (abfd, dynamic, minisyms, symcount,
1493 size, &symsizes);
1496 if (! sort_by_size)
1497 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd);
1498 else
1499 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd);
1501 if (synthsyms)
1502 free (synthsyms);
1503 free (minisyms);
1504 free (symsizes);
1507 /* Construct a formatting string for printing symbol values. */
1509 static const char *
1510 get_print_format (void)
1512 const char * padding;
1513 if (print_format == FORMAT_POSIX || print_format == FORMAT_JUST_SYMBOLS)
1515 /* POSIX compatible output does not have any padding. */
1516 padding = "";
1518 else if (print_width == 32)
1520 padding ="08";
1522 else /* print_width == 64 */
1524 padding = "016";
1527 const char * radix = NULL;
1528 switch (print_radix)
1530 case 8: radix = PRIo64; break;
1531 case 10: radix = PRId64; break;
1532 case 16: radix = PRIx64; break;
1535 return concat ("%", padding, radix, NULL);
1538 static void
1539 set_print_width (bfd *file)
1541 print_width = bfd_get_arch_size (file);
1543 if (print_width == -1)
1545 /* PR binutils/4292
1546 Guess the target's bitsize based on its name.
1547 We assume here than any 64-bit format will include
1548 "64" somewhere in its name. The only known exception
1549 is the MMO object file format. */
1550 if (strstr (bfd_get_target (file), "64") != NULL
1551 || strcmp (bfd_get_target (file), "mmo") == 0)
1552 print_width = 64;
1553 else
1554 print_width = 32;
1556 free ((char *) print_format_string);
1557 print_format_string = get_print_format ();
1560 static void
1561 display_archive (bfd *file)
1563 bfd *arfile = NULL;
1564 bfd *last_arfile = NULL;
1565 char **matching;
1567 format->print_archive_filename (bfd_get_filename (file));
1569 if (print_armap)
1570 print_symdef_entry (file);
1572 for (;;)
1574 arfile = bfd_openr_next_archived_file (file, arfile);
1576 if (arfile == NULL)
1578 if (bfd_get_error () != bfd_error_no_more_archived_files)
1579 bfd_nonfatal (bfd_get_filename (file));
1580 break;
1583 if (bfd_check_format_matches (arfile, bfd_object, &matching))
1585 set_print_width (arfile);
1586 format->print_archive_member (bfd_get_filename (file),
1587 bfd_get_filename (arfile));
1588 display_rel_file (arfile, file);
1590 else
1592 bfd_nonfatal (bfd_get_filename (arfile));
1593 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1594 list_matching_formats (matching);
1597 if (last_arfile != NULL)
1599 free_lineno_cache (last_arfile);
1600 bfd_close (last_arfile);
1601 if (arfile == last_arfile)
1602 return;
1604 last_arfile = arfile;
1607 if (last_arfile != NULL)
1609 free_lineno_cache (last_arfile);
1610 bfd_close (last_arfile);
1614 static bool
1615 display_file (char *filename)
1617 bool retval = true;
1618 bfd *file;
1619 char **matching;
1621 if (get_file_size (filename) < 1)
1622 return false;
1624 file = bfd_openr (filename, target ? target : plugin_target);
1625 if (file == NULL)
1627 bfd_nonfatal (filename);
1628 return false;
1631 /* If printing line numbers, decompress the debug sections. */
1632 if (line_numbers)
1633 file->flags |= BFD_DECOMPRESS;
1635 if (bfd_check_format (file, bfd_archive))
1637 display_archive (file);
1639 else if (bfd_check_format_matches (file, bfd_object, &matching))
1641 set_print_width (file);
1642 format->print_object_filename (filename);
1643 display_rel_file (file, NULL);
1645 else
1647 bfd_nonfatal (filename);
1648 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1649 list_matching_formats (matching);
1650 retval = false;
1653 free_lineno_cache (file);
1654 if (!bfd_close (file))
1655 retval = false;
1657 return retval;
1660 /* The following 3 groups of functions are called unconditionally,
1661 once at the start of processing each file of the appropriate type.
1662 They should check `filename_per_file' and `filename_per_symbol',
1663 as appropriate for their output format, to determine whether to
1664 print anything. */
1666 /* Print the name of an object file given on the command line. */
1668 static void
1669 print_object_filename_bsd (const char *filename)
1671 if (filename_per_file && !filename_per_symbol)
1672 printf ("\n%s:\n", filename);
1675 static void
1676 print_object_filename_sysv (const char *filename)
1678 if (undefined_only)
1679 printf (_("\n\nUndefined symbols from %s:\n\n"), filename);
1680 else
1681 printf (_("\n\nSymbols from %s:\n\n"), filename);
1682 if (print_width == 32)
1683 printf (_("\
1684 Name Value Class Type Size Line Section\n\n"));
1685 else
1686 printf (_("\
1687 Name Value Class Type Size Line Section\n\n"));
1690 static void
1691 print_object_filename_posix (const char *filename)
1693 if (filename_per_file && !filename_per_symbol)
1694 printf ("%s:\n", filename);
1697 static void
1698 do_not_print_object_filename (const char *filename ATTRIBUTE_UNUSED)
1702 /* Print the name of an archive file given on the command line. */
1704 static void
1705 print_archive_filename_bsd (const char *filename)
1707 if (filename_per_file)
1708 printf ("\n%s:\n", filename);
1711 static void
1712 print_archive_filename_sysv (const char *filename ATTRIBUTE_UNUSED)
1716 static void
1717 print_archive_filename_posix (const char *filename ATTRIBUTE_UNUSED)
1721 static void
1722 do_not_print_archive_filename (const char *filename ATTRIBUTE_UNUSED)
1726 /* Print the name of an archive member file. */
1728 static void
1729 print_archive_member_bsd (const char *archive ATTRIBUTE_UNUSED,
1730 const char *filename)
1732 if (!filename_per_symbol)
1733 printf ("\n%s:\n", filename);
1736 static void
1737 print_archive_member_sysv (const char *archive, const char *filename)
1739 if (undefined_only)
1740 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename);
1741 else
1742 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename);
1743 if (print_width == 32)
1744 printf (_("\
1745 Name Value Class Type Size Line Section\n\n"));
1746 else
1747 printf (_("\
1748 Name Value Class Type Size Line Section\n\n"));
1751 static void
1752 print_archive_member_posix (const char *archive, const char *filename)
1754 if (!filename_per_symbol)
1755 printf ("%s[%s]:\n", archive, filename);
1758 static void
1759 do_not_print_archive_member (const char *archive ATTRIBUTE_UNUSED,
1760 const char *filename ATTRIBUTE_UNUSED)
1765 /* Print the name of the file (and archive, if there is one)
1766 containing a symbol. */
1768 static void
1769 print_symbol_filename_bsd (bfd *archive_bfd, bfd *abfd)
1771 if (filename_per_symbol)
1773 if (archive_bfd)
1774 printf ("%s:", bfd_get_filename (archive_bfd));
1775 printf ("%s:", bfd_get_filename (abfd));
1779 static void
1780 print_symbol_filename_sysv (bfd *archive_bfd, bfd *abfd)
1782 if (filename_per_symbol)
1784 if (archive_bfd)
1785 printf ("%s:", bfd_get_filename (archive_bfd));
1786 printf ("%s:", bfd_get_filename (abfd));
1790 static void
1791 print_symbol_filename_posix (bfd *archive_bfd, bfd *abfd)
1793 if (filename_per_symbol)
1795 if (archive_bfd)
1796 printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
1797 bfd_get_filename (abfd));
1798 else
1799 printf ("%s: ", bfd_get_filename (abfd));
1803 static void
1804 do_not_print_symbol_filename (bfd *archive_bfd ATTRIBUTE_UNUSED,
1805 bfd *abfd ATTRIBUTE_UNUSED)
1810 /* Print a symbol value. */
1812 static void
1813 print_value (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma val)
1815 switch (print_width)
1817 case 32:
1818 case 64:
1819 printf (print_format_string, (uint64_t) val);
1820 break;
1822 default:
1823 fatal (_("Print width has not been initialized (%d)"), print_width);
1824 break;
1828 /* Print a line of information about a symbol. */
1830 static void
1831 print_symbol_info_bsd (struct extended_symbol_info *info, bfd *abfd)
1833 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1835 if (print_width == 64)
1836 printf (" ");
1837 printf (" ");
1839 else
1841 /* Normally we print the value of the symbol. If we are printing the
1842 size or sorting by size then we print its size, except for the
1843 (weird) special case where both flags are defined, in which case we
1844 print both values. This conforms to documented behaviour. */
1845 if (sort_by_size && !print_size)
1846 print_value (abfd, SYM_SIZE (info));
1847 else
1848 print_value (abfd, SYM_VALUE (info));
1849 if (print_size && SYM_SIZE (info))
1851 printf (" ");
1852 print_value (abfd, SYM_SIZE (info));
1856 printf (" %c", SYM_TYPE (info));
1858 if (SYM_TYPE (info) == '-')
1860 /* A stab. */
1861 printf (" ");
1862 printf (other_format, SYM_STAB_OTHER (info));
1863 printf (" ");
1864 printf (desc_format, SYM_STAB_DESC (info));
1865 printf (" %5s", SYM_STAB_NAME (info));
1867 print_symname (" %s", info, NULL, abfd);
1870 static void
1871 print_symbol_info_sysv (struct extended_symbol_info *info, bfd *abfd)
1873 print_symname ("%-20s|", info, NULL, abfd);
1875 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1877 if (print_width == 32)
1878 printf (" ");
1879 else
1880 printf (" ");
1882 else
1883 print_value (abfd, SYM_VALUE (info));
1885 printf ("| %c |", SYM_TYPE (info));
1887 if (SYM_TYPE (info) == '-')
1889 /* A stab. */
1890 printf ("%18s| ", SYM_STAB_NAME (info)); /* (C) Type. */
1891 printf (desc_format, SYM_STAB_DESC (info)); /* Size. */
1892 printf ("| |"); /* Line, Section. */
1894 else
1896 /* Type, Size, Line, Section */
1897 if (info->elfinfo)
1898 printf ("%18s|",
1899 get_elf_symbol_type (ELF_ST_TYPE (info->elfinfo->internal_elf_sym.st_info)));
1900 else if (info->coffinfo)
1901 printf ("%18s|",
1902 get_coff_symbol_type (&info->coffinfo->native->u.syment));
1903 else
1904 printf (" |");
1906 if (SYM_SIZE (info))
1907 print_value (abfd, SYM_SIZE (info));
1908 else
1910 if (print_width == 32)
1911 printf (" ");
1912 else
1913 printf (" ");
1916 if (info->elfinfo)
1917 printf("| |%s", info->elfinfo->symbol.section->name);
1918 else if (info->coffinfo)
1919 printf("| |%s", info->coffinfo->symbol.section->name);
1920 else
1921 printf("| |");
1925 static void
1926 print_symbol_info_posix (struct extended_symbol_info *info, bfd *abfd)
1928 print_symname ("%s ", info, NULL, abfd);
1929 printf ("%c ", SYM_TYPE (info));
1931 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1932 printf (" ");
1933 else
1935 print_value (abfd, SYM_VALUE (info));
1936 printf (" ");
1937 if (SYM_SIZE (info))
1938 print_value (abfd, SYM_SIZE (info));
1942 static void
1943 just_print_symbol_name (struct extended_symbol_info *info, bfd *abfd)
1945 print_symname ("%s", info, NULL, abfd);
1949 main (int argc, char **argv)
1951 int c;
1952 int retval;
1954 #ifdef HAVE_LC_MESSAGES
1955 setlocale (LC_MESSAGES, "");
1956 #endif
1957 setlocale (LC_CTYPE, "");
1958 setlocale (LC_COLLATE, "");
1959 bindtextdomain (PACKAGE, LOCALEDIR);
1960 textdomain (PACKAGE);
1962 program_name = *argv;
1963 xmalloc_set_program_name (program_name);
1964 bfd_set_error_program_name (program_name);
1965 #if BFD_SUPPORTS_PLUGINS
1966 bfd_plugin_set_program_name (program_name);
1967 #endif
1969 expandargv (&argc, &argv);
1971 if (bfd_init () != BFD_INIT_MAGIC)
1972 fatal (_("fatal error: libbfd ABI mismatch"));
1973 set_default_bfd_target ();
1975 while ((c = getopt_long (argc, argv, "aABCDef:gHhjJlnopPrSst:uU:vVvWX:",
1976 long_options, (int *) 0)) != EOF)
1978 switch (c)
1980 case 'a':
1981 print_debug_syms = 1;
1982 break;
1983 case 'A':
1984 case 'o':
1985 filename_per_symbol = 1;
1986 break;
1987 case 'B': /* For MIPS compatibility. */
1988 set_output_format ("bsd");
1989 break;
1990 case 'C':
1991 do_demangle = 1;
1992 if (optarg != NULL)
1994 enum demangling_styles style;
1996 style = cplus_demangle_name_to_style (optarg);
1997 if (style == unknown_demangling)
1998 fatal (_("unknown demangling style `%s'"),
1999 optarg);
2001 cplus_demangle_set_style (style);
2003 break;
2004 case OPTION_RECURSE_LIMIT:
2005 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
2006 break;
2007 case OPTION_NO_RECURSE_LIMIT:
2008 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
2009 break;
2010 case OPTION_QUIET:
2011 quiet = 1;
2012 break;
2013 case 'D':
2014 dynamic = 1;
2015 break;
2016 case 'e':
2017 /* Ignored for HP/UX compatibility. */
2018 break;
2019 case 'f':
2020 set_output_format (optarg);
2021 break;
2022 case 'g':
2023 external_only = 1;
2024 break;
2025 case 'H':
2026 case 'h':
2027 usage (stdout, 0);
2028 case 'l':
2029 line_numbers = 1;
2030 break;
2031 case 'n':
2032 case 'v':
2033 no_sort = 0;
2034 sort_numerically = 1;
2035 sort_by_size = 0;
2036 break;
2037 case 'p':
2038 no_sort = 1;
2039 sort_numerically = 0;
2040 sort_by_size = 0;
2041 break;
2042 case OPTION_SIZE_SORT:
2043 no_sort = 0;
2044 sort_numerically = 0;
2045 sort_by_size = 1;
2046 break;
2047 case 'P':
2048 set_output_format ("posix");
2049 break;
2050 case 'j':
2051 set_output_format ("just-symbols");
2052 break;
2053 case 'r':
2054 reverse_sort = 1;
2055 break;
2056 case 's':
2057 print_armap = 1;
2058 break;
2059 case 'S':
2060 print_size = 1;
2061 break;
2062 case 't':
2063 set_print_radix (optarg);
2064 break;
2065 case 'u':
2066 undefined_only = 1;
2067 defined_only = 0;
2068 break;
2069 case 'U':
2070 defined_only = 1;
2071 undefined_only = 0;
2072 break;
2074 case OPTION_UNICODE:
2075 if (streq (optarg, "default") || streq (optarg, "d"))
2076 unicode_display = unicode_default;
2077 else if (streq (optarg, "locale") || streq (optarg, "l"))
2078 unicode_display = unicode_locale;
2079 else if (streq (optarg, "escape") || streq (optarg, "e"))
2080 unicode_display = unicode_escape;
2081 else if (streq (optarg, "invalid") || streq (optarg, "i"))
2082 unicode_display = unicode_invalid;
2083 else if (streq (optarg, "hex") || streq (optarg, "x"))
2084 unicode_display = unicode_hex;
2085 else if (streq (optarg, "highlight") || streq (optarg, "h"))
2086 unicode_display = unicode_highlight;
2087 else
2088 fatal (_("invalid argument to -U/--unicode: %s"), optarg);
2089 break;
2091 case 'V':
2092 show_version = 1;
2093 break;
2094 case 'W':
2095 non_weak = 1;
2096 break;
2097 case 'X':
2098 /* Ignored for (partial) AIX compatibility. On AIX, the
2099 argument has values 32, 64, or 32_64, and specifies that
2100 only 32-bit, only 64-bit, or both kinds of objects should
2101 be examined. The default is 32. So plain AIX nm on a
2102 library archive with both kinds of objects will ignore
2103 the 64-bit ones. For GNU nm, the default is and always
2104 has been -X 32_64, and other options are not supported. */
2105 if (strcmp (optarg, "32_64") != 0)
2106 fatal (_("Only -X 32_64 is supported"));
2107 break;
2109 case OPTION_TARGET: /* --target */
2110 target = optarg;
2111 break;
2113 case OPTION_PLUGIN: /* --plugin */
2114 #if BFD_SUPPORTS_PLUGINS
2115 bfd_plugin_set_plugin (optarg);
2116 #else
2117 fatal (_("sorry - this program has been built without plugin support\n"));
2118 #endif
2119 break;
2121 case OPTION_IFUNC_CHARS:
2122 ifunc_type_chars = optarg;
2123 break;
2125 case 0: /* A long option that just sets a flag. */
2126 break;
2128 default:
2129 usage (stderr, 1);
2133 if (show_version)
2134 print_version ("nm");
2136 if (sort_by_size && undefined_only)
2138 non_fatal (_("Using the --size-sort and --undefined-only options together"));
2139 non_fatal (_("will produce no output, since undefined symbols have no size."));
2140 return 0;
2143 /* OK, all options now parsed. If no filename specified, do a.out. */
2144 if (optind == argc)
2145 return !display_file ("a.out");
2147 retval = 0;
2149 if (argc - optind > 1)
2150 filename_per_file = 1;
2152 /* We were given several filenames to do. */
2153 while (optind < argc)
2155 if (!display_file (argv[optind++]))
2156 retval++;
2159 exit (retval);
2160 return retval;