testsuite, threads: fix LD_LIBRARY_PATH in 'tls-sepdebug.exp'
[binutils-gdb.git] / binutils / nm.c
blobfaf27c59b4d31585d81379ea8b052ff26459543e
1 /* nm.c -- Describe symbol table of a rel file.
2 Copyright (C) 1991-2024 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 char print_format_string[10];
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 --without-symbol-versions Do not 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_type is set to lto_non_ir_object when a bfd is loaded with a
1470 compiler LTO plugin. */
1471 if (bfd_get_lto_type (abfd) == lto_slim_ir_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 void
1516 set_print_format (bfd *file)
1518 print_width = bfd_get_arch_size (file);
1520 if (print_width == -1)
1522 /* PR binutils/4292
1523 Guess the target's bitsize based on its name.
1524 We assume here than any 64-bit format will include
1525 "64" somewhere in its name. The only known exception
1526 is the MMO object file format. */
1527 if (strstr (bfd_get_target (file), "64") != NULL
1528 || strcmp (bfd_get_target (file), "mmo") == 0)
1529 print_width = 64;
1530 else
1531 print_width = 32;
1534 char *p = print_format_string;
1535 *p++ = '%';
1536 if (print_format == FORMAT_POSIX || print_format == FORMAT_JUST_SYMBOLS)
1538 /* POSIX compatible output does not have any padding. */
1540 else if (print_width == 32)
1542 *p++ = '0';
1543 *p++ = '8';
1545 else /* print_width == 64. */
1547 *p++ = '0';
1548 *p++ = '1';
1549 *p++ = '6';
1552 if (print_width == 32)
1554 switch (print_radix)
1556 case 8: strcpy (p, PRIo32); break;
1557 case 10: strcpy (p, PRId32); break;
1558 case 16: strcpy (p, PRIx32); break;
1561 else
1563 switch (print_radix)
1565 case 8: strcpy (p, PRIo64); break;
1566 case 10: strcpy (p, PRId64); break;
1567 case 16: strcpy (p, PRIx64); break;
1572 static void
1573 display_archive (bfd *file)
1575 bfd *arfile = NULL;
1576 bfd *last_arfile = NULL;
1577 char **matching;
1579 format->print_archive_filename (bfd_get_filename (file));
1581 if (print_armap)
1582 print_symdef_entry (file);
1584 for (;;)
1586 arfile = bfd_openr_next_archived_file (file, arfile);
1588 if (arfile == NULL)
1590 if (bfd_get_error () != bfd_error_no_more_archived_files)
1591 bfd_nonfatal (bfd_get_filename (file));
1592 break;
1595 if (bfd_check_format_matches (arfile, bfd_object, &matching))
1597 set_print_format (arfile);
1598 format->print_archive_member (bfd_get_filename (file),
1599 bfd_get_filename (arfile));
1600 display_rel_file (arfile, file);
1602 else
1604 bfd_nonfatal (bfd_get_filename (arfile));
1605 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1606 list_matching_formats (matching);
1609 if (last_arfile != NULL)
1611 free_lineno_cache (last_arfile);
1612 bfd_close (last_arfile);
1613 if (arfile == last_arfile)
1614 return;
1616 last_arfile = arfile;
1619 if (last_arfile != NULL)
1621 free_lineno_cache (last_arfile);
1622 bfd_close (last_arfile);
1626 static bool
1627 display_file (char *filename)
1629 bool retval = true;
1630 bfd *file;
1631 char **matching;
1633 if (get_file_size (filename) < 1)
1634 return false;
1636 file = bfd_openr (filename, target ? target : plugin_target);
1637 if (file == NULL)
1639 bfd_nonfatal (filename);
1640 return false;
1643 /* If printing line numbers, decompress the debug sections. */
1644 if (line_numbers)
1645 file->flags |= BFD_DECOMPRESS;
1647 if (bfd_check_format (file, bfd_archive))
1649 display_archive (file);
1651 else if (bfd_check_format_matches (file, bfd_object, &matching))
1653 set_print_format (file);
1654 format->print_object_filename (filename);
1655 display_rel_file (file, NULL);
1657 else
1659 bfd_nonfatal (filename);
1660 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1661 list_matching_formats (matching);
1662 retval = false;
1665 free_lineno_cache (file);
1666 if (!bfd_close (file))
1667 retval = false;
1669 return retval;
1672 /* The following 3 groups of functions are called unconditionally,
1673 once at the start of processing each file of the appropriate type.
1674 They should check `filename_per_file' and `filename_per_symbol',
1675 as appropriate for their output format, to determine whether to
1676 print anything. */
1678 /* Print the name of an object file given on the command line. */
1680 static void
1681 print_object_filename_bsd (const char *filename)
1683 if (filename_per_file && !filename_per_symbol)
1684 printf ("\n%s:\n", filename);
1687 static void
1688 print_object_filename_sysv (const char *filename)
1690 if (undefined_only)
1691 printf (_("\n\nUndefined symbols from %s:\n\n"), filename);
1692 else
1693 printf (_("\n\nSymbols from %s:\n\n"), filename);
1694 if (print_width == 32)
1695 printf (_("\
1696 Name Value Class Type Size Line Section\n\n"));
1697 else
1698 printf (_("\
1699 Name Value Class Type Size Line Section\n\n"));
1702 static void
1703 print_object_filename_posix (const char *filename)
1705 if (filename_per_file && !filename_per_symbol)
1706 printf ("%s:\n", filename);
1709 static void
1710 do_not_print_object_filename (const char *filename ATTRIBUTE_UNUSED)
1714 /* Print the name of an archive file given on the command line. */
1716 static void
1717 print_archive_filename_bsd (const char *filename)
1719 if (filename_per_file)
1720 printf ("\n%s:\n", filename);
1723 static void
1724 print_archive_filename_sysv (const char *filename ATTRIBUTE_UNUSED)
1728 static void
1729 print_archive_filename_posix (const char *filename ATTRIBUTE_UNUSED)
1733 static void
1734 do_not_print_archive_filename (const char *filename ATTRIBUTE_UNUSED)
1738 /* Print the name of an archive member file. */
1740 static void
1741 print_archive_member_bsd (const char *archive ATTRIBUTE_UNUSED,
1742 const char *filename)
1744 if (!filename_per_symbol)
1745 printf ("\n%s:\n", filename);
1748 static void
1749 print_archive_member_sysv (const char *archive, const char *filename)
1751 if (undefined_only)
1752 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename);
1753 else
1754 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename);
1755 if (print_width == 32)
1756 printf (_("\
1757 Name Value Class Type Size Line Section\n\n"));
1758 else
1759 printf (_("\
1760 Name Value Class Type Size Line Section\n\n"));
1763 static void
1764 print_archive_member_posix (const char *archive, const char *filename)
1766 if (!filename_per_symbol)
1767 printf ("%s[%s]:\n", archive, filename);
1770 static void
1771 do_not_print_archive_member (const char *archive ATTRIBUTE_UNUSED,
1772 const char *filename ATTRIBUTE_UNUSED)
1777 /* Print the name of the file (and archive, if there is one)
1778 containing a symbol. */
1780 static void
1781 print_symbol_filename_bsd (bfd *archive_bfd, bfd *abfd)
1783 if (filename_per_symbol)
1785 if (archive_bfd)
1786 printf ("%s:", bfd_get_filename (archive_bfd));
1787 printf ("%s:", bfd_get_filename (abfd));
1791 static void
1792 print_symbol_filename_sysv (bfd *archive_bfd, bfd *abfd)
1794 if (filename_per_symbol)
1796 if (archive_bfd)
1797 printf ("%s:", bfd_get_filename (archive_bfd));
1798 printf ("%s:", bfd_get_filename (abfd));
1802 static void
1803 print_symbol_filename_posix (bfd *archive_bfd, bfd *abfd)
1805 if (filename_per_symbol)
1807 if (archive_bfd)
1808 printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
1809 bfd_get_filename (abfd));
1810 else
1811 printf ("%s: ", bfd_get_filename (abfd));
1815 static void
1816 do_not_print_symbol_filename (bfd *archive_bfd ATTRIBUTE_UNUSED,
1817 bfd *abfd ATTRIBUTE_UNUSED)
1822 /* Print a symbol value. */
1824 static void
1825 print_value (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma val)
1827 switch (print_width)
1829 case 32:
1830 printf (print_format_string, (uint32_t) val);
1831 break;
1833 case 64:
1834 printf (print_format_string, (uint64_t) val);
1835 break;
1837 default:
1838 fatal (_("Print width has not been initialized (%d)"), print_width);
1839 break;
1843 /* Print a line of information about a symbol. */
1845 static void
1846 print_symbol_info_bsd (struct extended_symbol_info *info, bfd *abfd)
1848 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1850 if (print_width == 64)
1851 printf (" ");
1852 printf (" ");
1854 else
1856 /* Normally we print the value of the symbol. If we are printing the
1857 size or sorting by size then we print its size, except for the
1858 (weird) special case where both flags are defined, in which case we
1859 print both values. This conforms to documented behaviour. */
1860 if (sort_by_size && !print_size)
1861 print_value (abfd, SYM_SIZE (info));
1862 else
1863 print_value (abfd, SYM_VALUE (info));
1864 if (print_size && SYM_SIZE (info))
1866 printf (" ");
1867 print_value (abfd, SYM_SIZE (info));
1871 printf (" %c", SYM_TYPE (info));
1873 if (SYM_TYPE (info) == '-')
1875 /* A stab. */
1876 printf (" ");
1877 printf (other_format, SYM_STAB_OTHER (info));
1878 printf (" ");
1879 printf (desc_format, SYM_STAB_DESC (info));
1880 printf (" %5s", SYM_STAB_NAME (info));
1882 print_symname (" %s", info, NULL, abfd);
1885 static void
1886 print_symbol_info_sysv (struct extended_symbol_info *info, bfd *abfd)
1888 print_symname ("%-20s|", info, NULL, abfd);
1890 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1892 if (print_width == 32)
1893 printf (" ");
1894 else
1895 printf (" ");
1897 else
1898 print_value (abfd, SYM_VALUE (info));
1900 printf ("| %c |", SYM_TYPE (info));
1902 if (SYM_TYPE (info) == '-')
1904 /* A stab. */
1905 printf ("%18s| ", SYM_STAB_NAME (info)); /* (C) Type. */
1906 printf (desc_format, SYM_STAB_DESC (info)); /* Size. */
1907 printf ("| |"); /* Line, Section. */
1909 else
1911 /* Type, Size, Line, Section */
1912 if (info->elfinfo)
1913 printf ("%18s|",
1914 get_elf_symbol_type (ELF_ST_TYPE (info->elfinfo->internal_elf_sym.st_info)));
1915 else if (info->coffinfo)
1916 printf ("%18s|",
1917 get_coff_symbol_type (&info->coffinfo->native->u.syment));
1918 else
1919 printf (" |");
1921 if (SYM_SIZE (info))
1922 print_value (abfd, SYM_SIZE (info));
1923 else
1925 if (print_width == 32)
1926 printf (" ");
1927 else
1928 printf (" ");
1931 if (info->elfinfo)
1932 printf("| |%s", info->elfinfo->symbol.section->name);
1933 else if (info->coffinfo)
1934 printf("| |%s", info->coffinfo->symbol.section->name);
1935 else
1936 printf("| |");
1940 static void
1941 print_symbol_info_posix (struct extended_symbol_info *info, bfd *abfd)
1943 print_symname ("%s ", info, NULL, abfd);
1944 printf ("%c ", SYM_TYPE (info));
1946 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1947 printf (" ");
1948 else
1950 print_value (abfd, SYM_VALUE (info));
1951 printf (" ");
1952 if (SYM_SIZE (info))
1953 print_value (abfd, SYM_SIZE (info));
1957 static void
1958 just_print_symbol_name (struct extended_symbol_info *info, bfd *abfd)
1960 print_symname ("%s", info, NULL, abfd);
1964 main (int argc, char **argv)
1966 int c;
1967 int retval;
1969 #ifdef HAVE_LC_MESSAGES
1970 setlocale (LC_MESSAGES, "");
1971 #endif
1972 setlocale (LC_CTYPE, "");
1973 setlocale (LC_COLLATE, "");
1974 bindtextdomain (PACKAGE, LOCALEDIR);
1975 textdomain (PACKAGE);
1977 program_name = *argv;
1978 xmalloc_set_program_name (program_name);
1979 bfd_set_error_program_name (program_name);
1980 #if BFD_SUPPORTS_PLUGINS
1981 bfd_plugin_set_program_name (program_name);
1982 #endif
1984 expandargv (&argc, &argv);
1986 if (bfd_init () != BFD_INIT_MAGIC)
1987 fatal (_("fatal error: libbfd ABI mismatch"));
1988 set_default_bfd_target ();
1990 while ((c = getopt_long (argc, argv, "aABCDef:gHhjJlnopPrSst:uUvVvWX:",
1991 long_options, (int *) 0)) != EOF)
1993 switch (c)
1995 case 'a':
1996 print_debug_syms = 1;
1997 break;
1998 case 'A':
1999 case 'o':
2000 filename_per_symbol = 1;
2001 break;
2002 case 'B': /* For MIPS compatibility. */
2003 set_output_format ("bsd");
2004 break;
2005 case 'C':
2006 do_demangle = 1;
2007 if (optarg != NULL)
2009 enum demangling_styles style;
2011 style = cplus_demangle_name_to_style (optarg);
2012 if (style == unknown_demangling)
2013 fatal (_("unknown demangling style `%s'"),
2014 optarg);
2016 cplus_demangle_set_style (style);
2018 break;
2019 case OPTION_RECURSE_LIMIT:
2020 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
2021 break;
2022 case OPTION_NO_RECURSE_LIMIT:
2023 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
2024 break;
2025 case OPTION_QUIET:
2026 quiet = 1;
2027 break;
2028 case 'D':
2029 dynamic = 1;
2030 break;
2031 case 'e':
2032 /* Ignored for HP/UX compatibility. */
2033 break;
2034 case 'f':
2035 set_output_format (optarg);
2036 break;
2037 case 'g':
2038 external_only = 1;
2039 break;
2040 case 'H':
2041 case 'h':
2042 usage (stdout, 0);
2043 case 'l':
2044 line_numbers = 1;
2045 break;
2046 case 'n':
2047 case 'v':
2048 no_sort = 0;
2049 sort_numerically = 1;
2050 sort_by_size = 0;
2051 break;
2052 case 'p':
2053 no_sort = 1;
2054 sort_numerically = 0;
2055 sort_by_size = 0;
2056 break;
2057 case OPTION_SIZE_SORT:
2058 no_sort = 0;
2059 sort_numerically = 0;
2060 sort_by_size = 1;
2061 break;
2062 case 'P':
2063 set_output_format ("posix");
2064 break;
2065 case 'j':
2066 set_output_format ("just-symbols");
2067 break;
2068 case 'r':
2069 reverse_sort = 1;
2070 break;
2071 case 's':
2072 print_armap = 1;
2073 break;
2074 case 'S':
2075 print_size = 1;
2076 break;
2077 case 't':
2078 set_print_radix (optarg);
2079 break;
2080 case 'u':
2081 undefined_only = 1;
2082 defined_only = 0;
2083 break;
2084 case 'U':
2085 defined_only = 1;
2086 undefined_only = 0;
2087 break;
2089 case OPTION_UNICODE:
2090 if (streq (optarg, "default") || streq (optarg, "d"))
2091 unicode_display = unicode_default;
2092 else if (streq (optarg, "locale") || streq (optarg, "l"))
2093 unicode_display = unicode_locale;
2094 else if (streq (optarg, "escape") || streq (optarg, "e"))
2095 unicode_display = unicode_escape;
2096 else if (streq (optarg, "invalid") || streq (optarg, "i"))
2097 unicode_display = unicode_invalid;
2098 else if (streq (optarg, "hex") || streq (optarg, "x"))
2099 unicode_display = unicode_hex;
2100 else if (streq (optarg, "highlight") || streq (optarg, "h"))
2101 unicode_display = unicode_highlight;
2102 else
2103 fatal (_("invalid argument to -U/--unicode: %s"), optarg);
2104 break;
2106 case 'V':
2107 show_version = 1;
2108 break;
2109 case 'W':
2110 non_weak = 1;
2111 break;
2112 case 'X':
2113 /* Ignored for (partial) AIX compatibility. On AIX, the
2114 argument has values 32, 64, or 32_64, and specifies that
2115 only 32-bit, only 64-bit, or both kinds of objects should
2116 be examined. The default is 32. So plain AIX nm on a
2117 library archive with both kinds of objects will ignore
2118 the 64-bit ones. For GNU nm, the default is and always
2119 has been -X 32_64, and other options are not supported. */
2120 if (strcmp (optarg, "32_64") != 0)
2121 fatal (_("Only -X 32_64 is supported"));
2122 break;
2124 case OPTION_TARGET: /* --target */
2125 target = optarg;
2126 break;
2128 case OPTION_PLUGIN: /* --plugin */
2129 #if BFD_SUPPORTS_PLUGINS
2130 bfd_plugin_set_plugin (optarg);
2131 #else
2132 fatal (_("sorry - this program has been built without plugin support\n"));
2133 #endif
2134 break;
2136 case OPTION_IFUNC_CHARS:
2137 ifunc_type_chars = optarg;
2138 break;
2140 case 0: /* A long option that just sets a flag. */
2141 break;
2143 default:
2144 usage (stderr, 1);
2148 if (show_version)
2149 print_version ("nm");
2151 if (sort_by_size && undefined_only)
2153 non_fatal (_("Using the --size-sort and --undefined-only options together"));
2154 non_fatal (_("will produce no output, since undefined symbols have no size."));
2155 return 0;
2158 /* OK, all options now parsed. If no filename specified, do a.out. */
2159 if (optind == argc)
2160 return !display_file ("a.out");
2162 retval = 0;
2164 if (argc - optind > 1)
2165 filename_per_file = 1;
2167 /* We were given several filenames to do. */
2168 while (optind < argc)
2170 if (!display_file (argv[optind++]))
2171 retval++;
2174 exit (retval);
2175 return retval;