Automatic date update in version.in
[binutils-gdb.git] / binutils / nm.c
blob4e86057910ceea68975a955d9ccd4c17679e9ca1
1 /* nm.c -- Describe symbol table of a rel file.
2 Copyright (C) 1991-2025 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;
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 bufp = xasprintf (_("<processor specific>: %d"), type);
451 else if (type >= STT_LOOS && type <= STT_HIOS)
452 bufp = xasprintf (_("<OS specific>: %d"), type);
453 else
454 bufp = xasprintf (_("<unknown>: %d"), type);
455 return bufp;
458 static const char *
459 get_coff_symbol_type (const struct internal_syment *sym)
461 static char *bufp;
463 switch (sym->n_sclass)
465 case C_BLOCK: return "Block";
466 case C_FILE: return "File";
467 case C_LINE: return "Line";
470 if (!sym->n_type)
471 return "None";
473 switch (DTYPE(sym->n_type))
475 case DT_FCN: return "Function";
476 case DT_PTR: return "Pointer";
477 case DT_ARY: return "Array";
480 free (bufp);
481 bufp = xasprintf (_("<unknown>: %d/%d"), sym->n_sclass, sym->n_type);
482 return bufp;
485 /* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
486 The conversion format is controlled by the unicode_display variable.
487 Returns the number of characters added to OUT.
488 Returns the number of bytes consumed from IN in CONSUMED.
489 Always consumes at least one byte and displays at least one character. */
491 static unsigned int
492 display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
494 char * orig_out = out;
495 unsigned int nchars = 0;
496 unsigned int j;
498 if (unicode_display == unicode_default)
499 goto invalid;
501 if (in[0] < 0xc0)
502 goto invalid;
504 if ((in[1] & 0xc0) != 0x80)
505 goto invalid;
507 if ((in[0] & 0x20) == 0)
509 nchars = 2;
510 goto valid;
513 if ((in[2] & 0xc0) != 0x80)
514 goto invalid;
516 if ((in[0] & 0x10) == 0)
518 nchars = 3;
519 goto valid;
522 if ((in[3] & 0xc0) != 0x80)
523 goto invalid;
525 nchars = 4;
527 valid:
528 switch (unicode_display)
530 case unicode_locale:
531 /* Copy the bytes into the output buffer as is. */
532 memcpy (out, in, nchars);
533 out += nchars;
534 break;
536 case unicode_invalid:
537 case unicode_hex:
538 *out++ = unicode_display == unicode_hex ? '<' : '{';
539 *out++ = '0';
540 *out++ = 'x';
541 for (j = 0; j < nchars; j++)
542 out += sprintf (out, "%02x", in [j]);
543 *out++ = unicode_display == unicode_hex ? '>' : '}';
544 break;
546 case unicode_highlight:
547 if (isatty (1))
548 out += sprintf (out, "\x1B[31;47m"); /* Red. */
549 /* Fall through. */
550 case unicode_escape:
551 switch (nchars)
553 case 2:
554 out += sprintf (out, "\\u%02x%02x",
555 ((in[0] & 0x1c) >> 2),
556 ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
557 break;
559 case 3:
560 out += sprintf (out, "\\u%02x%02x",
561 ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
562 ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
563 break;
565 case 4:
566 out += sprintf (out, "\\u%02x%02x%02x",
567 ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
568 ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
569 ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
570 break;
571 default:
572 /* URG. */
573 break;
576 if (unicode_display == unicode_highlight && isatty (1))
577 out += sprintf (out, "\x1B[0m"); /* Default colour. */
578 break;
580 default:
581 /* URG */
582 break;
585 * consumed = nchars;
586 return out - orig_out;
588 invalid:
589 /* Not a valid UTF-8 sequence. */
590 *out = *in;
591 * consumed = 1;
592 return 1;
595 /* Convert any UTF-8 encoded characters in NAME into the form specified by
596 unicode_display. Also converts control characters. Returns a static
597 buffer if conversion was necessary.
598 Code stolen from objdump.c:sanitize_string(). */
600 static const char *
601 convert_utf8 (const char * in)
603 static char * buffer = NULL;
604 static size_t buffer_len = 0;
605 const char * original = in;
606 char * out;
608 /* Paranoia. */
609 if (in == NULL)
610 return "";
612 /* See if any conversion is necessary.
613 In the majority of cases it will not be needed. */
616 unsigned char c = *in++;
618 if (c == 0)
619 return original;
621 if (ISCNTRL (c))
622 break;
624 if (unicode_display != unicode_default && c >= 0xc0)
625 break;
627 while (1);
629 /* Copy the input, translating as needed. */
630 in = original;
631 /* For 2 char unicode, max out is 12 (colour escapes) + 6, ie. 9 per in
632 For hex, max out is 8 for 2 char unicode, ie. 4 per in.
633 3 and 4 char unicode produce less output for input. */
634 size_t max_needed = strlen (in) * 9 + 1;
635 if (buffer_len < max_needed)
637 buffer_len = max_needed;
638 free (buffer);
639 buffer = xmalloc (buffer_len);
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, out, &num_consumed);
660 in += num_consumed;
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 *atname = 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 char *atver = strchr (name, '@');
689 if (atver)
691 /* PR 32467 - Corrupt binaries might include an @ character in a
692 symbol name. Since non-versioned symbol names can be in
693 read-only memory (via memory mapping of a file's contents) we
694 cannot just replace the @ character with a NUL. Instead we
695 create a truncated copy of the name. */
696 atname = xstrdup (name);
697 atname [atver - name] = 0;
698 name = atname;
702 if (do_demangle && *name)
704 alloc = bfd_demangle (abfd, name, demangle_flags);
705 if (alloc != NULL)
706 name = alloc;
709 if (unicode_display != unicode_default)
710 name = convert_utf8 (name);
712 if (info != NULL && info->elfinfo && with_symbol_versions)
714 const char *version_string;
715 bool hidden;
717 version_string
718 = bfd_get_symbol_version_string (abfd, &info->elfinfo->symbol,
719 false, &hidden);
720 if (version_string && version_string[0])
722 const char *at = "@@";
723 if (hidden || bfd_is_und_section (info->elfinfo->symbol.section))
724 at = "@";
725 alloc = reconcat (alloc, name, at, version_string, NULL);
726 if (alloc != NULL)
727 name = alloc;
730 printf (form, name);
732 free (atname);
733 free (alloc);
736 static void
737 print_symdef_entry (bfd *abfd)
739 symindex idx = BFD_NO_MORE_SYMBOLS;
740 carsym *thesym;
741 bool everprinted = false;
743 for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
744 idx != BFD_NO_MORE_SYMBOLS;
745 idx = bfd_get_next_mapent (abfd, idx, &thesym))
747 if (!everprinted)
749 printf (_("\nArchive index:\n"));
750 everprinted = true;
752 if (thesym->name != NULL)
754 print_symname ("%s", NULL, thesym->name, abfd);
755 bfd *elt = bfd_get_elt_at_index (abfd, idx);
756 if (elt)
757 printf (" in %s\n", bfd_get_filename (elt));
758 else
759 printf ("\n");
765 /* True when we can report missing plugin error. */
766 bool report_plugin_err = true;
768 /* Choose which symbol entries to print;
769 compact them downward to get rid of the rest.
770 Return the number of symbols to be printed. */
772 static long
773 filter_symbols (bfd *abfd, bool is_dynamic, void *minisyms,
774 long symcount, unsigned int size)
776 bfd_byte *from, *fromend, *to;
777 asymbol *store;
779 store = bfd_make_empty_symbol (abfd);
780 if (store == NULL)
781 bfd_fatal (bfd_get_filename (abfd));
783 from = (bfd_byte *) minisyms;
784 fromend = from + symcount * size;
785 to = (bfd_byte *) minisyms;
787 for (; from < fromend; from += size)
789 int keep = 0;
790 asymbol *sym;
792 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from, store);
793 if (sym == NULL)
794 continue;
796 if (sym->name != NULL
797 && sym->name[0] == '_'
798 && sym->name[1] == '_'
799 && strcmp (sym->name + (sym->name[2] == '_'), "__gnu_lto_slim") == 0
800 && report_plugin_err)
802 report_plugin_err = false;
803 non_fatal (_("%s: plugin needed to handle lto object"),
804 bfd_get_filename (abfd));
807 if (undefined_only)
808 keep = bfd_is_und_section (sym->section);
809 else if (external_only)
810 /* PR binutls/12753: Unique symbols are global too. */
811 keep = ((sym->flags & (BSF_GLOBAL
812 | BSF_WEAK
813 | BSF_GNU_UNIQUE)) != 0
814 || bfd_is_und_section (sym->section)
815 || bfd_is_com_section (sym->section));
816 else if (non_weak)
817 keep = ((sym->flags & BSF_WEAK) == 0);
818 else
819 keep = 1;
821 if (keep
822 && ! print_debug_syms
823 && (sym->flags & BSF_DEBUGGING) != 0)
824 keep = 0;
826 if (keep
827 && sort_by_size
828 && (bfd_is_abs_section (sym->section)
829 || bfd_is_und_section (sym->section)))
830 keep = 0;
832 if (keep
833 && defined_only)
835 if (bfd_is_und_section (sym->section))
836 keep = 0;
839 if (keep
840 && bfd_is_target_special_symbol (abfd, sym)
841 && ! allow_special_symbols)
842 keep = 0;
844 if (keep)
846 if (to != from)
847 memcpy (to, from, size);
848 to += size;
852 return (to - (bfd_byte *) minisyms) / size;
855 /* These globals are used to pass information into the sorting
856 routines. */
857 static bfd *sort_bfd;
858 static bool sort_dynamic;
859 static asymbol *sort_x;
860 static asymbol *sort_y;
862 /* Symbol-sorting predicates */
863 #define valueof(x) ((x)->section->vma + (x)->value)
865 /* Numeric sorts. Undefined symbols are always considered "less than"
866 defined symbols with zero values. Common symbols are not treated
867 specially -- i.e., their sizes are used as their "values". */
869 static int
870 non_numeric_forward (const void *P_x, const void *P_y)
872 asymbol *x, *y;
873 const char *xn, *yn;
875 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
876 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
877 if (x == NULL || y == NULL)
878 bfd_fatal (bfd_get_filename (sort_bfd));
880 xn = bfd_asymbol_name (x);
881 yn = bfd_asymbol_name (y);
883 if (yn == NULL)
884 return xn != NULL;
885 if (xn == NULL)
886 return -1;
888 /* Solaris 2.5 has a bug in strcoll.
889 strcoll returns invalid values when confronted with empty strings. */
890 if (*yn == '\0')
891 return *xn != '\0';
892 if (*xn == '\0')
893 return -1;
895 return strcoll (xn, yn);
898 static int
899 non_numeric_reverse (const void *x, const void *y)
901 return - non_numeric_forward (x, y);
904 static int
905 numeric_forward (const void *P_x, const void *P_y)
907 asymbol *x, *y;
908 asection *xs, *ys;
910 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
911 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
912 if (x == NULL || y == NULL)
913 bfd_fatal (bfd_get_filename (sort_bfd));
915 xs = bfd_asymbol_section (x);
916 ys = bfd_asymbol_section (y);
918 if (bfd_is_und_section (xs))
920 if (! bfd_is_und_section (ys))
921 return -1;
923 else if (bfd_is_und_section (ys))
924 return 1;
925 else if (valueof (x) != valueof (y))
926 return valueof (x) < valueof (y) ? -1 : 1;
928 return non_numeric_forward (P_x, P_y);
931 static int
932 numeric_reverse (const void *x, const void *y)
934 return - numeric_forward (x, y);
937 static int (*(sorters[2][2])) (const void *, const void *) =
939 { non_numeric_forward, non_numeric_reverse },
940 { numeric_forward, numeric_reverse }
943 /* This sort routine is used by sort_symbols_by_size. It is similar
944 to numeric_forward, but when symbols have the same value it sorts
945 by section VMA. This simplifies the sort_symbols_by_size code
946 which handles symbols at the end of sections. Also, this routine
947 tries to sort file names before other symbols with the same value.
948 That will make the file name have a zero size, which will make
949 sort_symbols_by_size choose the non file name symbol, leading to
950 more meaningful output. For similar reasons, this code sorts
951 gnu_compiled_* and gcc2_compiled before other symbols with the same
952 value. */
954 static int
955 size_forward1 (const void *P_x, const void *P_y)
957 asymbol *x, *y;
958 asection *xs, *ys;
959 const char *xn, *yn;
960 size_t xnl, ynl;
961 int xf, yf;
963 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
964 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
965 if (x == NULL || y == NULL)
966 bfd_fatal (bfd_get_filename (sort_bfd));
968 xs = bfd_asymbol_section (x);
969 ys = bfd_asymbol_section (y);
971 if (bfd_is_und_section (xs))
972 abort ();
973 if (bfd_is_und_section (ys))
974 abort ();
976 if (valueof (x) != valueof (y))
977 return valueof (x) < valueof (y) ? -1 : 1;
979 if (xs->vma != ys->vma)
980 return xs->vma < ys->vma ? -1 : 1;
982 xn = bfd_asymbol_name (x);
983 yn = bfd_asymbol_name (y);
984 xnl = strlen (xn);
985 ynl = strlen (yn);
987 /* The symbols gnu_compiled and gcc2_compiled convey even less
988 information than the file name, so sort them out first. */
990 xf = (strstr (xn, "gnu_compiled") != NULL
991 || strstr (xn, "gcc2_compiled") != NULL);
992 yf = (strstr (yn, "gnu_compiled") != NULL
993 || strstr (yn, "gcc2_compiled") != NULL);
995 if (xf && ! yf)
996 return -1;
997 if (! xf && yf)
998 return 1;
1000 /* We use a heuristic for the file name. It may not work on non
1001 Unix systems, but it doesn't really matter; the only difference
1002 is precisely which symbol names get printed. */
1004 #define file_symbol(s, sn, snl) \
1005 (((s)->flags & BSF_FILE) != 0 \
1006 || ((snl) > 2 \
1007 && (sn)[(snl) - 2] == '.' \
1008 && ((sn)[(snl) - 1] == 'o' \
1009 || (sn)[(snl) - 1] == 'a')))
1011 xf = file_symbol (x, xn, xnl);
1012 yf = file_symbol (y, yn, ynl);
1014 if (xf && ! yf)
1015 return -1;
1016 if (! xf && yf)
1017 return 1;
1019 return non_numeric_forward (P_x, P_y);
1022 /* This sort routine is used by sort_symbols_by_size. It is sorting
1023 an array of size_sym structures into size order. */
1025 static int
1026 size_forward2 (const void *P_x, const void *P_y)
1028 const struct size_sym *x = (const struct size_sym *) P_x;
1029 const struct size_sym *y = (const struct size_sym *) P_y;
1031 if (x->size < y->size)
1032 return reverse_sort ? 1 : -1;
1033 else if (x->size > y->size)
1034 return reverse_sort ? -1 : 1;
1035 else
1036 return sorters[0][reverse_sort] (x->minisym, y->minisym);
1039 /* Sort the symbols by size. ELF provides a size but for other formats
1040 we have to make a guess by assuming that the difference between the
1041 address of a symbol and the address of the next higher symbol is the
1042 size. */
1044 static long
1045 sort_symbols_by_size (bfd *abfd, bool is_dynamic, void *minisyms,
1046 long symcount, unsigned int size,
1047 struct size_sym **symsizesp)
1049 struct size_sym *symsizes;
1050 bfd_byte *from, *fromend;
1051 asymbol *sym = NULL;
1052 asymbol *store_sym, *store_next;
1054 qsort (minisyms, symcount, size, size_forward1);
1056 /* We are going to return a special set of symbols and sizes to
1057 print. */
1058 symsizes = (struct size_sym *) xmalloc (symcount * sizeof (struct size_sym));
1059 *symsizesp = symsizes;
1061 /* Note that filter_symbols has already removed all absolute and
1062 undefined symbols. Here we remove all symbols whose size winds
1063 up as zero. */
1064 from = (bfd_byte *) minisyms;
1065 fromend = from + symcount * size;
1067 store_sym = sort_x;
1068 store_next = sort_y;
1070 if (from < fromend)
1072 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, (const void *) from,
1073 store_sym);
1074 if (sym == NULL)
1075 bfd_fatal (bfd_get_filename (abfd));
1078 for (; from < fromend; from += size)
1080 asymbol *next;
1081 asection *sec;
1082 bfd_vma sz;
1083 asymbol *temp;
1085 if (from + size < fromend)
1087 next = bfd_minisymbol_to_symbol (abfd,
1088 is_dynamic,
1089 (const void *) (from + size),
1090 store_next);
1091 if (next == NULL)
1092 bfd_fatal (bfd_get_filename (abfd));
1094 else
1095 next = NULL;
1097 sec = bfd_asymbol_section (sym);
1099 /* Synthetic symbols don't have a full type set of data available, thus
1100 we can't rely on that information for the symbol size. Ditto for
1101 bfd/section.c:global_syms like *ABS*. */
1102 if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0
1103 && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1104 sz = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
1105 else if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0
1106 && bfd_is_com_section (sec))
1107 sz = sym->value;
1108 else
1110 if (from + size < fromend
1111 && sec == bfd_asymbol_section (next))
1112 sz = valueof (next) - valueof (sym);
1113 else
1114 sz = (bfd_section_vma (sec)
1115 + bfd_section_size (sec)
1116 - valueof (sym));
1119 if (sz != 0)
1121 symsizes->minisym = (const void *) from;
1122 symsizes->size = sz;
1123 ++symsizes;
1126 sym = next;
1128 temp = store_sym;
1129 store_sym = store_next;
1130 store_next = temp;
1133 symcount = symsizes - *symsizesp;
1135 /* We must now sort again by size. */
1136 qsort ((void *) *symsizesp, symcount, sizeof (struct size_sym), size_forward2);
1138 return symcount;
1141 /* This function is used to get the relocs for a particular section.
1142 It is called via bfd_map_over_sections. */
1144 static void
1145 get_relocs (bfd *abfd, asection *sec, void *dataarg)
1147 struct lineno_cache *data = (struct lineno_cache *) dataarg;
1149 *data->secs = sec;
1150 *data->relocs = NULL;
1151 *data->relcount = 0;
1153 if ((sec->flags & SEC_RELOC) != 0)
1155 long relsize = bfd_get_reloc_upper_bound (abfd, sec);
1156 if (relsize > 0)
1158 *data->relocs = (arelent **) xmalloc (relsize);
1159 *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs,
1160 data->syms);
1164 ++data->secs;
1165 ++data->relocs;
1166 ++data->relcount;
1169 static void
1170 free_lineno_cache (bfd *abfd)
1172 struct lineno_cache *lc = bfd_usrdata (abfd);
1174 if (lc)
1176 if (lc->relocs)
1177 for (unsigned int i = 0; i < lc->seccount; i++)
1178 free (lc->relocs[i]);
1179 free (lc->relcount);
1180 free (lc->relocs);
1181 free (lc->secs);
1182 free (lc->syms);
1183 free (lc);
1184 bfd_set_usrdata (abfd, NULL);
1188 /* Print a single symbol. */
1190 static void
1191 print_symbol (bfd * abfd,
1192 asymbol * sym,
1193 bfd_vma ssize,
1194 bfd * archive_bfd)
1196 symbol_info syminfo;
1197 struct extended_symbol_info info;
1199 format->print_symbol_filename (archive_bfd, abfd);
1201 bfd_get_symbol_info (abfd, sym, &syminfo);
1203 /* PR 22967 - Distinguish between local and global ifunc symbols. */
1204 if (syminfo.type == 'i'
1205 && sym->flags & BSF_GNU_INDIRECT_FUNCTION)
1207 if (ifunc_type_chars == NULL || ifunc_type_chars[0] == 0)
1208 ; /* Change nothing. */
1209 else if (sym->flags & BSF_GLOBAL)
1210 syminfo.type = ifunc_type_chars[0];
1211 else if (ifunc_type_chars[1] != 0)
1212 syminfo.type = ifunc_type_chars[1];
1215 info.sinfo = &syminfo;
1216 info.ssize = ssize;
1217 /* Synthetic symbols do not have a full symbol type set of data available.
1218 Nor do bfd/section.c:global_syms like *ABS*. */
1219 if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) != 0)
1221 info.elfinfo = NULL;
1222 info.coffinfo = NULL;
1224 else
1226 info.elfinfo = elf_symbol_from (sym);
1227 info.coffinfo = coff_symbol_from (sym);
1230 format->print_symbol_info (&info, abfd);
1232 const char *symname = bfd_asymbol_name (sym);
1233 if (line_numbers && symname != NULL && symname[0] != 0)
1235 struct lineno_cache *lc = bfd_usrdata (abfd);
1236 const char *filename, *functionname;
1237 unsigned int lineno;
1239 /* We need to get the canonical symbols in order to call
1240 bfd_find_nearest_line. This is inefficient, but, then, you
1241 don't have to use --line-numbers. */
1242 if (lc == NULL)
1244 lc = xcalloc (1, sizeof (*lc));
1245 bfd_set_usrdata (abfd, lc);
1247 if (lc->syms == NULL && lc->symcount == 0)
1249 long symsize = bfd_get_symtab_upper_bound (abfd);
1250 if (symsize <= 0)
1251 lc->symcount = -1;
1252 else
1254 lc->syms = xmalloc (symsize);
1255 lc->symcount = bfd_canonicalize_symtab (abfd, lc->syms);
1259 if (lc->symcount <= 0)
1261 else if (bfd_is_und_section (bfd_asymbol_section (sym)))
1263 unsigned int i;
1265 /* For an undefined symbol, we try to find a reloc for the
1266 symbol, and print the line number of the reloc. */
1267 if (lc->relocs == NULL)
1269 unsigned int seccount = bfd_count_sections (abfd);
1270 lc->seccount = seccount;
1271 lc->secs = xmalloc (seccount * sizeof (*lc->secs));
1272 lc->relocs = xmalloc (seccount * sizeof (*lc->relocs));
1273 lc->relcount = xmalloc (seccount * sizeof (*lc->relcount));
1275 struct lineno_cache rinfo = *lc;
1276 bfd_map_over_sections (abfd, get_relocs, &rinfo);
1279 for (i = 0; i < lc->seccount; i++)
1281 long j;
1283 for (j = 0; j < lc->relcount[i]; j++)
1285 arelent *r;
1287 r = lc->relocs[i][j];
1288 if (r->sym_ptr_ptr != NULL
1289 && (*r->sym_ptr_ptr)->section == sym->section
1290 && (*r->sym_ptr_ptr)->value == sym->value
1291 && bfd_asymbol_name (*r->sym_ptr_ptr) != NULL
1292 && strcmp (symname,
1293 bfd_asymbol_name (*r->sym_ptr_ptr)) == 0
1294 && bfd_find_nearest_line (abfd, lc->secs[i], lc->syms,
1295 r->address, &filename,
1296 &functionname, &lineno)
1297 && filename != NULL)
1299 /* We only print the first one we find. */
1300 printf ("\t%s:%u", filename, lineno);
1301 i = lc->seccount;
1302 break;
1307 else if (bfd_asymbol_section (sym)->owner == abfd)
1309 if ((bfd_find_line (abfd, lc->syms, sym, &filename, &lineno)
1310 || bfd_find_nearest_line (abfd, bfd_asymbol_section (sym),
1311 lc->syms, sym->value, &filename,
1312 &functionname, &lineno))
1313 && filename != NULL
1314 && lineno != 0)
1315 printf ("\t%s:%u", filename, lineno);
1319 putchar ('\n');
1322 /* Print the symbols when sorting by size. */
1324 static void
1325 print_size_symbols (bfd *abfd,
1326 bool is_dynamic,
1327 struct size_sym *symsizes,
1328 long symcount,
1329 bfd *archive_bfd)
1331 asymbol *store;
1332 struct size_sym *from;
1333 struct size_sym *fromend;
1335 store = bfd_make_empty_symbol (abfd);
1336 if (store == NULL)
1337 bfd_fatal (bfd_get_filename (abfd));
1339 from = symsizes;
1340 fromend = from + symcount;
1342 for (; from < fromend; from++)
1344 asymbol *sym;
1346 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from->minisym, store);
1347 if (sym == NULL)
1348 bfd_fatal (bfd_get_filename (abfd));
1350 print_symbol (abfd, sym, from->size, archive_bfd);
1355 /* Print the symbols of ABFD that are held in MINISYMS.
1357 If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD.
1359 SYMCOUNT is the number of symbols in MINISYMS.
1361 SIZE is the size of a symbol in MINISYMS. */
1363 static void
1364 print_symbols (bfd *abfd,
1365 bool is_dynamic,
1366 void *minisyms,
1367 long symcount,
1368 unsigned int size,
1369 bfd *archive_bfd)
1371 asymbol *store;
1372 bfd_byte *from;
1373 bfd_byte *fromend;
1375 store = bfd_make_empty_symbol (abfd);
1376 if (store == NULL)
1377 bfd_fatal (bfd_get_filename (abfd));
1379 from = (bfd_byte *) minisyms;
1380 fromend = from + symcount * size;
1382 for (; from < fromend; from += size)
1384 asymbol *sym;
1386 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from, store);
1387 if (sym == NULL)
1388 bfd_fatal (bfd_get_filename (abfd));
1390 print_symbol (abfd, sym, (bfd_vma) 0, archive_bfd);
1394 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
1396 static void
1397 display_rel_file (bfd *abfd, bfd *archive_bfd)
1399 long symcount;
1400 void *minisyms;
1401 unsigned int size;
1402 struct size_sym *symsizes;
1403 asymbol *synthsyms = NULL;
1405 if (! dynamic)
1407 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
1409 if (!quiet)
1410 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
1411 return;
1415 symcount = bfd_read_minisymbols (abfd, dynamic, &minisyms, &size);
1416 if (symcount <= 0)
1418 if (!quiet)
1419 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
1420 return;
1423 if (show_synthetic && size == sizeof (asymbol *))
1425 asymbol **static_syms = NULL;
1426 asymbol **dyn_syms = NULL;
1427 long static_count = 0;
1428 long dyn_count = 0;
1429 long synth_count;
1431 if (dynamic)
1433 dyn_count = symcount;
1434 dyn_syms = (asymbol **) minisyms;
1436 else
1438 long storage = bfd_get_dynamic_symtab_upper_bound (abfd);
1440 static_count = symcount;
1441 static_syms = (asymbol **) minisyms;
1443 if (storage > 0)
1445 dyn_syms = (asymbol **) xmalloc (storage);
1446 dyn_count = bfd_canonicalize_dynamic_symtab (abfd, dyn_syms);
1447 if (dyn_count < 0)
1448 dyn_count = 0;
1452 synth_count = bfd_get_synthetic_symtab (abfd, static_count, static_syms,
1453 dyn_count, dyn_syms, &synthsyms);
1454 if (synth_count > 0)
1456 asymbol **symp;
1457 long i;
1459 minisyms = xrealloc (minisyms,
1460 (symcount + synth_count + 1) * sizeof (*symp));
1461 symp = (asymbol **) minisyms + symcount;
1462 for (i = 0; i < synth_count; i++)
1463 *symp++ = synthsyms + i;
1464 *symp = 0;
1465 symcount += synth_count;
1467 if (!dynamic && dyn_syms != NULL)
1468 free (dyn_syms);
1471 /* lto_type is set to lto_non_ir_object when a bfd is loaded with a
1472 compiler LTO plugin. */
1473 if (bfd_get_lto_type (abfd) == lto_slim_ir_object)
1475 report_plugin_err = false;
1476 non_fatal (_("%s: plugin needed to handle lto object"),
1477 bfd_get_filename (abfd));
1480 /* Discard the symbols we don't want to print.
1481 It's OK to do this in place; we'll free the storage anyway
1482 (after printing). */
1484 symcount = filter_symbols (abfd, dynamic, minisyms, symcount, size);
1486 symsizes = NULL;
1487 if (! no_sort)
1489 sort_bfd = abfd;
1490 sort_dynamic = dynamic;
1491 sort_x = bfd_make_empty_symbol (abfd);
1492 sort_y = bfd_make_empty_symbol (abfd);
1493 if (sort_x == NULL || sort_y == NULL)
1494 bfd_fatal (bfd_get_filename (abfd));
1496 if (! sort_by_size)
1497 qsort (minisyms, symcount, size,
1498 sorters[sort_numerically][reverse_sort]);
1499 else
1500 symcount = sort_symbols_by_size (abfd, dynamic, minisyms, symcount,
1501 size, &symsizes);
1504 if (! sort_by_size)
1505 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd);
1506 else
1507 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd);
1509 free_lineno_cache (abfd);
1510 free (synthsyms);
1511 free (minisyms);
1512 free (symsizes);
1515 /* Construct a formatting string for printing symbol values. */
1517 static void
1518 set_print_format (bfd *file)
1520 print_width = bfd_get_arch_size (file);
1522 if (print_width == -1)
1524 /* PR binutils/4292
1525 Guess the target's bitsize based on its name.
1526 We assume here than any 64-bit format will include
1527 "64" somewhere in its name. The only known exception
1528 is the MMO object file format. */
1529 if (strstr (bfd_get_target (file), "64") != NULL
1530 || strcmp (bfd_get_target (file), "mmo") == 0)
1531 print_width = 64;
1532 else
1533 print_width = 32;
1536 char *p = print_format_string;
1537 *p++ = '%';
1538 if (print_format == FORMAT_POSIX || print_format == FORMAT_JUST_SYMBOLS)
1540 /* POSIX compatible output does not have any padding. */
1542 else if (print_width == 32)
1544 *p++ = '0';
1545 *p++ = '8';
1547 else /* print_width == 64. */
1549 *p++ = '0';
1550 *p++ = '1';
1551 *p++ = '6';
1554 if (print_width == 32)
1556 switch (print_radix)
1558 case 8: strcpy (p, PRIo32); break;
1559 case 10: strcpy (p, PRId32); break;
1560 case 16: strcpy (p, PRIx32); break;
1563 else
1565 switch (print_radix)
1567 case 8: strcpy (p, PRIo64); break;
1568 case 10: strcpy (p, PRId64); break;
1569 case 16: strcpy (p, PRIx64); break;
1574 static void
1575 display_archive (bfd *file)
1577 format->print_archive_filename (bfd_get_filename (file));
1579 if (print_armap)
1580 print_symdef_entry (file);
1582 bfd *last_arfile = NULL;
1583 for (;;)
1585 bfd *arfile = bfd_openr_next_archived_file (file, last_arfile);
1586 if (arfile == NULL
1587 || arfile == last_arfile)
1589 if (arfile != NULL)
1590 bfd_set_error (bfd_error_malformed_archive);
1591 if (bfd_get_error () != bfd_error_no_more_archived_files)
1592 bfd_nonfatal (bfd_get_filename (file));
1593 break;
1596 if (last_arfile != NULL)
1597 bfd_close (last_arfile);
1599 char **matching;
1600 if (bfd_check_format_matches (arfile, bfd_object, &matching))
1602 set_print_format (arfile);
1603 format->print_archive_member (bfd_get_filename (file),
1604 bfd_get_filename (arfile));
1605 display_rel_file (arfile, file);
1607 else
1609 bfd_nonfatal (bfd_get_filename (arfile));
1610 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1611 list_matching_formats (matching);
1614 last_arfile = arfile;
1617 if (last_arfile != NULL)
1618 bfd_close (last_arfile);
1621 static bool
1622 display_file (char *filename)
1624 bool retval = true;
1625 bfd *file;
1626 char **matching;
1628 if (get_file_size (filename) < 1)
1629 return false;
1631 file = bfd_openr (filename, target ? target : plugin_target);
1632 if (file == NULL)
1634 bfd_nonfatal (filename);
1635 return false;
1638 /* If printing line numbers, decompress the debug sections. */
1639 if (line_numbers)
1640 file->flags |= BFD_DECOMPRESS;
1642 if (bfd_check_format (file, bfd_archive))
1644 display_archive (file);
1646 else if (bfd_check_format_matches (file, bfd_object, &matching))
1648 set_print_format (file);
1649 format->print_object_filename (filename);
1650 display_rel_file (file, NULL);
1652 else
1654 bfd_nonfatal (filename);
1655 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1656 list_matching_formats (matching);
1657 retval = false;
1660 if (!bfd_close (file))
1661 retval = false;
1663 return retval;
1666 /* The following 3 groups of functions are called unconditionally,
1667 once at the start of processing each file of the appropriate type.
1668 They should check `filename_per_file' and `filename_per_symbol',
1669 as appropriate for their output format, to determine whether to
1670 print anything. */
1672 /* Print the name of an object file given on the command line. */
1674 static void
1675 print_object_filename_bsd (const char *filename)
1677 if (filename_per_file && !filename_per_symbol)
1678 printf ("\n%s:\n", filename);
1681 static void
1682 print_object_filename_sysv (const char *filename)
1684 if (undefined_only)
1685 printf (_("\n\nUndefined symbols from %s:\n\n"), filename);
1686 else
1687 printf (_("\n\nSymbols from %s:\n\n"), filename);
1688 if (print_width == 32)
1689 printf (_("\
1690 Name Value Class Type Size Line Section\n\n"));
1691 else
1692 printf (_("\
1693 Name Value Class Type Size Line Section\n\n"));
1696 static void
1697 print_object_filename_posix (const char *filename)
1699 if (filename_per_file && !filename_per_symbol)
1700 printf ("%s:\n", filename);
1703 static void
1704 do_not_print_object_filename (const char *filename ATTRIBUTE_UNUSED)
1708 /* Print the name of an archive file given on the command line. */
1710 static void
1711 print_archive_filename_bsd (const char *filename)
1713 if (filename_per_file)
1714 printf ("\n%s:\n", filename);
1717 static void
1718 print_archive_filename_sysv (const char *filename ATTRIBUTE_UNUSED)
1722 static void
1723 print_archive_filename_posix (const char *filename ATTRIBUTE_UNUSED)
1727 static void
1728 do_not_print_archive_filename (const char *filename ATTRIBUTE_UNUSED)
1732 /* Print the name of an archive member file. */
1734 static void
1735 print_archive_member_bsd (const char *archive ATTRIBUTE_UNUSED,
1736 const char *filename)
1738 if (!filename_per_symbol)
1739 printf ("\n%s:\n", filename);
1742 static void
1743 print_archive_member_sysv (const char *archive, const char *filename)
1745 if (undefined_only)
1746 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename);
1747 else
1748 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename);
1749 if (print_width == 32)
1750 printf (_("\
1751 Name Value Class Type Size Line Section\n\n"));
1752 else
1753 printf (_("\
1754 Name Value Class Type Size Line Section\n\n"));
1757 static void
1758 print_archive_member_posix (const char *archive, const char *filename)
1760 if (!filename_per_symbol)
1761 printf ("%s[%s]:\n", archive, filename);
1764 static void
1765 do_not_print_archive_member (const char *archive ATTRIBUTE_UNUSED,
1766 const char *filename ATTRIBUTE_UNUSED)
1771 /* Print the name of the file (and archive, if there is one)
1772 containing a symbol. */
1774 static void
1775 print_symbol_filename_bsd (bfd *archive_bfd, bfd *abfd)
1777 if (filename_per_symbol)
1779 if (archive_bfd)
1780 printf ("%s:", bfd_get_filename (archive_bfd));
1781 printf ("%s:", bfd_get_filename (abfd));
1785 static void
1786 print_symbol_filename_sysv (bfd *archive_bfd, bfd *abfd)
1788 if (filename_per_symbol)
1790 if (archive_bfd)
1791 printf ("%s:", bfd_get_filename (archive_bfd));
1792 printf ("%s:", bfd_get_filename (abfd));
1796 static void
1797 print_symbol_filename_posix (bfd *archive_bfd, bfd *abfd)
1799 if (filename_per_symbol)
1801 if (archive_bfd)
1802 printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
1803 bfd_get_filename (abfd));
1804 else
1805 printf ("%s: ", bfd_get_filename (abfd));
1809 static void
1810 do_not_print_symbol_filename (bfd *archive_bfd ATTRIBUTE_UNUSED,
1811 bfd *abfd ATTRIBUTE_UNUSED)
1816 /* Print a symbol value. */
1818 static void
1819 print_value (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma val)
1821 switch (print_width)
1823 case 32:
1824 printf (print_format_string, (uint32_t) val);
1825 break;
1827 case 64:
1828 printf (print_format_string, (uint64_t) val);
1829 break;
1831 default:
1832 fatal (_("Print width has not been initialized (%d)"), print_width);
1833 break;
1837 /* Print a line of information about a symbol. */
1839 static void
1840 print_symbol_info_bsd (struct extended_symbol_info *info, bfd *abfd)
1842 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1844 if (print_width == 64)
1845 printf (" ");
1846 printf (" ");
1848 else
1850 /* Normally we print the value of the symbol. If we are printing the
1851 size or sorting by size then we print its size, except for the
1852 (weird) special case where both flags are defined, in which case we
1853 print both values. This conforms to documented behaviour. */
1854 if (sort_by_size && !print_size)
1855 print_value (abfd, SYM_SIZE (info));
1856 else
1857 print_value (abfd, SYM_VALUE (info));
1858 if (print_size && SYM_SIZE (info))
1860 printf (" ");
1861 print_value (abfd, SYM_SIZE (info));
1865 printf (" %c", SYM_TYPE (info));
1867 if (SYM_TYPE (info) == '-')
1869 /* A stab. */
1870 printf (" ");
1871 printf (other_format, SYM_STAB_OTHER (info));
1872 printf (" ");
1873 printf (desc_format, SYM_STAB_DESC (info));
1874 printf (" %5s", SYM_STAB_NAME (info));
1876 print_symname (" %s", info, NULL, abfd);
1879 static void
1880 print_symbol_info_sysv (struct extended_symbol_info *info, bfd *abfd)
1882 print_symname ("%-20s|", info, NULL, abfd);
1884 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1886 if (print_width == 32)
1887 printf (" ");
1888 else
1889 printf (" ");
1891 else
1892 print_value (abfd, SYM_VALUE (info));
1894 printf ("| %c |", SYM_TYPE (info));
1896 if (SYM_TYPE (info) == '-')
1898 /* A stab. */
1899 printf ("%18s| ", SYM_STAB_NAME (info)); /* (C) Type. */
1900 printf (desc_format, SYM_STAB_DESC (info)); /* Size. */
1901 printf ("| |"); /* Line, Section. */
1903 else
1905 /* Type, Size, Line, Section */
1906 if (info->elfinfo)
1907 printf ("%18s|",
1908 get_elf_symbol_type (ELF_ST_TYPE (info->elfinfo->internal_elf_sym.st_info)));
1909 else if (info->coffinfo)
1910 printf ("%18s|",
1911 get_coff_symbol_type (&info->coffinfo->native->u.syment));
1912 else
1913 printf (" |");
1915 if (SYM_SIZE (info))
1916 print_value (abfd, SYM_SIZE (info));
1917 else
1919 if (print_width == 32)
1920 printf (" ");
1921 else
1922 printf (" ");
1925 if (info->elfinfo)
1926 printf("| |%s", info->elfinfo->symbol.section->name);
1927 else if (info->coffinfo)
1928 printf("| |%s", info->coffinfo->symbol.section->name);
1929 else
1930 printf("| |");
1934 static void
1935 print_symbol_info_posix (struct extended_symbol_info *info, bfd *abfd)
1937 print_symname ("%s ", info, NULL, abfd);
1938 printf ("%c ", SYM_TYPE (info));
1940 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1941 printf (" ");
1942 else
1944 print_value (abfd, SYM_VALUE (info));
1945 printf (" ");
1946 if (SYM_SIZE (info))
1947 print_value (abfd, SYM_SIZE (info));
1951 static void
1952 just_print_symbol_name (struct extended_symbol_info *info, bfd *abfd)
1954 print_symname ("%s", info, NULL, abfd);
1958 main (int argc, char **argv)
1960 int c;
1961 int retval;
1963 #ifdef HAVE_LC_MESSAGES
1964 setlocale (LC_MESSAGES, "");
1965 #endif
1966 setlocale (LC_CTYPE, "");
1967 setlocale (LC_COLLATE, "");
1968 bindtextdomain (PACKAGE, LOCALEDIR);
1969 textdomain (PACKAGE);
1971 program_name = *argv;
1972 xmalloc_set_program_name (program_name);
1973 bfd_set_error_program_name (program_name);
1974 #if BFD_SUPPORTS_PLUGINS
1975 bfd_plugin_set_program_name (program_name);
1976 #endif
1978 expandargv (&argc, &argv);
1980 if (bfd_init () != BFD_INIT_MAGIC)
1981 fatal (_("fatal error: libbfd ABI mismatch"));
1982 set_default_bfd_target ();
1984 while ((c = getopt_long (argc, argv, "aABCDef:gHhjJlnopPrSst:uUvVvWX:",
1985 long_options, (int *) 0)) != EOF)
1987 switch (c)
1989 case 'a':
1990 print_debug_syms = 1;
1991 break;
1992 case 'A':
1993 case 'o':
1994 filename_per_symbol = 1;
1995 break;
1996 case 'B': /* For MIPS compatibility. */
1997 set_output_format ("bsd");
1998 break;
1999 case 'C':
2000 do_demangle = 1;
2001 if (optarg != NULL)
2003 enum demangling_styles style;
2005 style = cplus_demangle_name_to_style (optarg);
2006 if (style == unknown_demangling)
2007 fatal (_("unknown demangling style `%s'"),
2008 optarg);
2010 cplus_demangle_set_style (style);
2012 break;
2013 case OPTION_RECURSE_LIMIT:
2014 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
2015 break;
2016 case OPTION_NO_RECURSE_LIMIT:
2017 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
2018 break;
2019 case OPTION_QUIET:
2020 quiet = 1;
2021 break;
2022 case 'D':
2023 dynamic = 1;
2024 break;
2025 case 'e':
2026 /* Ignored for HP/UX compatibility. */
2027 break;
2028 case 'f':
2029 set_output_format (optarg);
2030 break;
2031 case 'g':
2032 external_only = 1;
2033 break;
2034 case 'H':
2035 case 'h':
2036 usage (stdout, 0);
2037 case 'l':
2038 line_numbers = 1;
2039 break;
2040 case 'n':
2041 case 'v':
2042 no_sort = 0;
2043 sort_numerically = 1;
2044 sort_by_size = 0;
2045 break;
2046 case 'p':
2047 no_sort = 1;
2048 sort_numerically = 0;
2049 sort_by_size = 0;
2050 break;
2051 case OPTION_SIZE_SORT:
2052 no_sort = 0;
2053 sort_numerically = 0;
2054 sort_by_size = 1;
2055 break;
2056 case 'P':
2057 set_output_format ("posix");
2058 break;
2059 case 'j':
2060 set_output_format ("just-symbols");
2061 break;
2062 case 'r':
2063 reverse_sort = 1;
2064 break;
2065 case 's':
2066 print_armap = 1;
2067 break;
2068 case 'S':
2069 print_size = 1;
2070 break;
2071 case 't':
2072 set_print_radix (optarg);
2073 break;
2074 case 'u':
2075 undefined_only = 1;
2076 defined_only = 0;
2077 break;
2078 case 'U':
2079 defined_only = 1;
2080 undefined_only = 0;
2081 break;
2083 case OPTION_UNICODE:
2084 if (streq (optarg, "default") || streq (optarg, "d"))
2085 unicode_display = unicode_default;
2086 else if (streq (optarg, "locale") || streq (optarg, "l"))
2087 unicode_display = unicode_locale;
2088 else if (streq (optarg, "escape") || streq (optarg, "e"))
2089 unicode_display = unicode_escape;
2090 else if (streq (optarg, "invalid") || streq (optarg, "i"))
2091 unicode_display = unicode_invalid;
2092 else if (streq (optarg, "hex") || streq (optarg, "x"))
2093 unicode_display = unicode_hex;
2094 else if (streq (optarg, "highlight") || streq (optarg, "h"))
2095 unicode_display = unicode_highlight;
2096 else
2097 fatal (_("invalid argument to -U/--unicode: %s"), optarg);
2098 break;
2100 case 'V':
2101 show_version = 1;
2102 break;
2103 case 'W':
2104 non_weak = 1;
2105 break;
2106 case 'X':
2107 /* Ignored for (partial) AIX compatibility. On AIX, the
2108 argument has values 32, 64, or 32_64, and specifies that
2109 only 32-bit, only 64-bit, or both kinds of objects should
2110 be examined. The default is 32. So plain AIX nm on a
2111 library archive with both kinds of objects will ignore
2112 the 64-bit ones. For GNU nm, the default is and always
2113 has been -X 32_64, and other options are not supported. */
2114 if (strcmp (optarg, "32_64") != 0)
2115 fatal (_("Only -X 32_64 is supported"));
2116 break;
2118 case OPTION_TARGET: /* --target */
2119 target = optarg;
2120 break;
2122 case OPTION_PLUGIN: /* --plugin */
2123 #if BFD_SUPPORTS_PLUGINS
2124 bfd_plugin_set_plugin (optarg);
2125 #else
2126 fatal (_("sorry - this program has been built without plugin support\n"));
2127 #endif
2128 break;
2130 case OPTION_IFUNC_CHARS:
2131 ifunc_type_chars = optarg;
2132 break;
2134 case 0: /* A long option that just sets a flag. */
2135 break;
2137 default:
2138 usage (stderr, 1);
2142 if (show_version)
2143 print_version ("nm");
2145 if (sort_by_size && undefined_only)
2147 non_fatal (_("Using the --size-sort and --undefined-only options together"));
2148 non_fatal (_("will produce no output, since undefined symbols have no size."));
2149 return 0;
2152 /* OK, all options now parsed. If no filename specified, do a.out. */
2153 if (optind == argc)
2154 return !display_file ("a.out");
2156 retval = 0;
2158 if (argc - optind > 1)
2159 filename_per_file = 1;
2161 /* We were given several filenames to do. */
2162 while (optind < argc)
2164 if (!display_file (argv[optind++]))
2165 retval++;
2168 exit (retval);
2169 return retval;