Re: Move nm.c cached line number info to bfd usrdata
[binutils-gdb.git] / binutils / nm.c
1 /* nm.c -- Describe symbol table of a rel file.
2 Copyright (C) 1991-2023 Free Software Foundation, Inc.
3
4 This file is part of GNU Binutils.
5
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.
10
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.
15
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. */
20
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"
42
43 #ifndef streq
44 #define streq(a,b) (strcmp ((a),(b)) == 0)
45 #endif
46
47 /* When sorting by size, we use this structure to hold the size and a
48 pointer to the minisymbol. */
49
50 struct size_sym
51 {
52 const void *minisym;
53 bfd_vma size;
54 };
55
56 /* line number related info cached in bfd usrdata. */
57
58 struct lineno_cache
59 {
60 asection **secs;
61 arelent ***relocs;
62 long *relcount;
63 asymbol **syms;
64 long symcount;
65 unsigned int seccount;
66 };
67
68 struct extended_symbol_info
69 {
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. */
75 };
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)
83
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 *);
89
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 *);
94
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 *);
99
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 *);
104
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 *);
109
110 static void print_value (bfd *, bfd_vma);
111
112 /* Support for different output formats. */
113 struct output_fns
114 {
115 /* Print the name of an object file given on the command line. */
116 void (*print_object_filename) (const char *);
117
118 /* Print the name of an archive file given on the command line. */
119 void (*print_archive_filename) (const char *);
120
121 /* Print the name of an archive member file. */
122 void (*print_archive_member) (const char *, const char *);
123
124 /* Print the name of the file (and archive, if there is one)
125 containing a symbol. */
126 void (*print_symbol_filename) (bfd *, bfd *);
127
128 /* Print a line of information about a symbol. */
129 void (*print_symbol_info) (struct extended_symbol_info *, bfd *);
130 };
131
132 /* Indices in `formats'. */
133 enum formats
134 {
135 FORMAT_BSD = 0,
136 FORMAT_SYSV,
137 FORMAT_POSIX,
138 FORMAT_JUST_SYMBOLS,
139 FORMAT_MAX
140 };
141
142 #define FORMAT_DEFAULT FORMAT_BSD
143
144 static const struct output_fns formats[FORMAT_MAX] =
145 {
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}
166 };
167
168
169 /* The output format to use. */
170 static const struct output_fns *format = &formats[FORMAT_DEFAULT];
171 static unsigned int print_format = FORMAT_DEFAULT;
172 static const char *print_format_string = NULL;
173
174 /* Command options. */
175
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. */
195
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
202
203 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
204
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. */
208
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";
214
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
221
222 typedef enum unicode_display_type
223 {
224 unicode_default = 0,
225 unicode_locale,
226 unicode_escape,
227 unicode_hex,
228 unicode_highlight,
229 unicode_invalid
230 } unicode_display_type;
231
232 static unicode_display_type unicode_display = unicode_default;
233
234 enum long_option_values
235 {
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
244 };
245
246 static struct option long_options[] =
247 {
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}
285 };
286 \f
287 /* Some error-reporting functions. */
288
289 ATTRIBUTE_NORETURN static void
290 usage (FILE *stream, int status)
291 {
292 fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
293 fprintf (stream, _(" List symbols in [file(s)] (a.out by default).\n"));
294 fprintf (stream, _(" The options are:\n"));
295 fprintf (stream, _("\
296 -a, --debug-syms Display debugger-only symbols\n"));
297 fprintf (stream, _("\
298 -A, --print-file-name Print name of the input file before every symbol\n"));
299 fprintf (stream, _("\
300 -B Same as --format=bsd\n"));
301 fprintf (stream, _("\
302 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n"));
303 display_demangler_styles (stream, _("\
304 STYLE can be "));
305 fprintf (stream, _("\
306 --no-demangle Do not demangle low-level symbol names\n"));
307 fprintf (stream, _("\
308 --recurse-limit Enable a demangling recursion limit. (default)\n"));
309 fprintf (stream, _("\
310 --no-recurse-limit Disable a demangling recursion limit.\n"));
311 fprintf (stream, _("\
312 -D, --dynamic Display dynamic symbols instead of normal symbols\n"));
313 fprintf (stream, _("\
314 -e (ignored)\n"));
315 fprintf (stream, _("\
316 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
317 `sysv', `posix' or 'just-symbols'.\n\
318 The default is `bsd'\n"));
319 fprintf (stream, _("\
320 -g, --extern-only Display only external symbols\n"));
321 fprintf (stream, _("\
322 --ifunc-chars=CHARS Characters to use when displaying ifunc symbols\n"));
323 fprintf (stream, _("\
324 -j, --just-symbols Same as --format=just-symbols\n"));
325 fprintf (stream, _("\
326 -l, --line-numbers Use debugging information to find a filename and\n\
327 line number for each symbol\n"));
328 fprintf (stream, _("\
329 -n, --numeric-sort Sort symbols numerically by address\n"));
330 fprintf (stream, _("\
331 -o Same as -A\n"));
332 fprintf (stream, _("\
333 -p, --no-sort Do not sort the symbols\n"));
334 fprintf (stream, _("\
335 -P, --portability Same as --format=posix\n"));
336 fprintf (stream, _("\
337 -r, --reverse-sort Reverse the sense of the sort\n"));
338 #if BFD_SUPPORTS_PLUGINS
339 fprintf (stream, _("\
340 --plugin NAME Load the specified plugin\n"));
341 #endif
342 fprintf (stream, _("\
343 -S, --print-size Print size of defined symbols\n"));
344 fprintf (stream, _("\
345 -s, --print-armap Include index for symbols from archive members\n"));
346 fprintf (stream, _("\
347 --quiet Suppress \"no symbols\" diagnostic\n"));
348 fprintf (stream, _("\
349 --size-sort Sort symbols by size\n"));
350 fprintf (stream, _("\
351 --special-syms Include special symbols in the output\n"));
352 fprintf (stream, _("\
353 --synthetic Display synthetic symbols as well\n"));
354 fprintf (stream, _("\
355 -t, --radix=RADIX Use RADIX for printing symbol values\n"));
356 fprintf (stream, _("\
357 --target=BFDNAME Specify the target object format as BFDNAME\n"));
358 fprintf (stream, _("\
359 -u, --undefined-only Display only undefined symbols\n"));
360 fprintf (stream, _("\
361 -U, --defined-only Display only defined symbols\n"));
362 fprintf (stream, _("\
363 --unicode={default|show|invalid|hex|escape|highlight}\n\
364 Specify how to treat UTF-8 encoded unicode characters\n"));
365 fprintf (stream, _("\
366 -W, --no-weak Ignore weak symbols\n"));
367 fprintf (stream, _("\
368 --with-symbol-versions Display version strings after symbol names\n"));
369 fprintf (stream, _("\
370 -X 32_64 (ignored)\n"));
371 fprintf (stream, _("\
372 @FILE Read options from FILE\n"));
373 fprintf (stream, _("\
374 -h, --help Display this information\n"));
375 fprintf (stream, _("\
376 -V, --version Display this program's version number\n"));
377
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);
382 }
383
384 /* Set the radix for the symbol value and size according to RADIX. */
385
386 static void
387 set_print_radix (char *radix)
388 {
389 switch (*radix)
390 {
391 case 'x': print_radix = 16; break;
392 case 'd': print_radix = 10; break;
393 case 'o': print_radix = 8; break;
394
395 default:
396 fatal (_("%s: invalid radix"), radix);
397 }
398
399 other_format[3] = desc_format[3] = *radix;
400 }
401
402 static void
403 set_output_format (char *f)
404 {
405 int i;
406
407 switch (*f)
408 {
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);
427 }
428 format = &formats[i];
429 print_format = i;
430 }
431 \f
432 static const char *
433 get_elf_symbol_type (unsigned int type)
434 {
435 static char *bufp;
436 int n;
437
438 switch (type)
439 {
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";
447 }
448
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;
459 }
460
461 static const char *
462 get_coff_symbol_type (const struct internal_syment *sym)
463 {
464 static char *bufp;
465 int n;
466
467 switch (sym->n_sclass)
468 {
469 case C_BLOCK: return "Block";
470 case C_FILE: return "File";
471 case C_LINE: return "Line";
472 }
473
474 if (!sym->n_type)
475 return "None";
476
477 switch (DTYPE(sym->n_type))
478 {
479 case DT_FCN: return "Function";
480 case DT_PTR: return "Pointer";
481 case DT_ARY: return "Array";
482 }
483
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;
489 }
490 \f
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. */
496
497 static unsigned int
498 display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
499 {
500 char * orig_out = out;
501 unsigned int nchars = 0;
502 unsigned int j;
503
504 if (unicode_display == unicode_default)
505 goto invalid;
506
507 if (in[0] < 0xc0)
508 goto invalid;
509
510 if ((in[1] & 0xc0) != 0x80)
511 goto invalid;
512
513 if ((in[0] & 0x20) == 0)
514 {
515 nchars = 2;
516 goto valid;
517 }
518
519 if ((in[2] & 0xc0) != 0x80)
520 goto invalid;
521
522 if ((in[0] & 0x10) == 0)
523 {
524 nchars = 3;
525 goto valid;
526 }
527
528 if ((in[3] & 0xc0) != 0x80)
529 goto invalid;
530
531 nchars = 4;
532
533 valid:
534 switch (unicode_display)
535 {
536 case unicode_locale:
537 /* Copy the bytes into the output buffer as is. */
538 memcpy (out, in, nchars);
539 out += nchars;
540 break;
541
542 case unicode_invalid:
543 case unicode_hex:
544 out += sprintf (out, "%c", unicode_display == unicode_hex ? '<' : '{');
545 out += sprintf (out, "0x");
546 for (j = 0; j < nchars; j++)
547 out += sprintf (out, "%02x", in [j]);
548 out += sprintf (out, "%c", unicode_display == unicode_hex ? '>' : '}');
549 break;
550
551 case unicode_highlight:
552 if (isatty (1))
553 out += sprintf (out, "\x1B[31;47m"); /* Red. */
554 /* Fall through. */
555 case unicode_escape:
556 switch (nchars)
557 {
558 case 2:
559 out += sprintf (out, "\\u%02x%02x",
560 ((in[0] & 0x1c) >> 2),
561 ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
562 break;
563
564 case 3:
565 out += sprintf (out, "\\u%02x%02x",
566 ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
567 ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
568 break;
569
570 case 4:
571 out += sprintf (out, "\\u%02x%02x%02x",
572 ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
573 ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
574 ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
575 break;
576 default:
577 /* URG. */
578 break;
579 }
580
581 if (unicode_display == unicode_highlight && isatty (1))
582 out += sprintf (out, "\033[0m"); /* Default colour. */
583 break;
584
585 default:
586 /* URG */
587 break;
588 }
589
590 * consumed = nchars;
591 return out - orig_out;
592
593 invalid:
594 /* Not a valid UTF-8 sequence. */
595 *out = *in;
596 * consumed = 1;
597 return 1;
598 }
599
600 /* Convert any UTF-8 encoded characters in NAME into the form specified by
601 unicode_display. Also converts control characters. Returns a static
602 buffer if conversion was necessary.
603 Code stolen from objdump.c:sanitize_string(). */
604
605 static const char *
606 convert_utf8 (const char * in)
607 {
608 static char * buffer = NULL;
609 static size_t buffer_len = 0;
610 const char * original = in;
611 char * out;
612
613 /* Paranoia. */
614 if (in == NULL)
615 return "";
616
617 /* See if any conversion is necessary.
618 In the majority of cases it will not be needed. */
619 do
620 {
621 unsigned char c = *in++;
622
623 if (c == 0)
624 return original;
625
626 if (ISCNTRL (c))
627 break;
628
629 if (unicode_display != unicode_default && c >= 0xc0)
630 break;
631 }
632 while (1);
633
634 /* Copy the input, translating as needed. */
635 in = original;
636 if (buffer_len < (strlen (in) * 9))
637 {
638 free ((void *) buffer);
639 buffer_len = strlen (in) * 9;
640 buffer = xmalloc (buffer_len + 1);
641 }
642
643 out = buffer;
644 do
645 {
646 unsigned char c = *in++;
647
648 if (c == 0)
649 break;
650
651 if (ISCNTRL (c))
652 {
653 *out++ = '^';
654 *out++ = c + 0x40;
655 }
656 else if (unicode_display != unicode_default && c >= 0xc0)
657 {
658 unsigned int num_consumed;
659
660 out += display_utf8 ((const unsigned char *)(in - 1), out, & num_consumed);
661 in += num_consumed - 1;
662 }
663 else
664 *out++ = c;
665 }
666 while (1);
667
668 *out = 0;
669 return buffer;
670 }
671
672 /* Print symbol name NAME, read from ABFD, with printf format FORM,
673 demangling it if requested. */
674
675 static void
676 print_symname (const char *form, struct extended_symbol_info *info,
677 const char *name, bfd *abfd)
678 {
679 char *alloc = NULL;
680 char *atver = NULL;
681
682 if (name == NULL)
683 name = info->sinfo->name;
684
685 if (!with_symbol_versions
686 && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
687 {
688 atver = strchr (name, '@');
689 if (atver)
690 *atver = 0;
691 }
692
693 if (do_demangle && *name)
694 {
695 alloc = bfd_demangle (abfd, name, demangle_flags);
696 if (alloc != NULL)
697 name = alloc;
698 }
699
700 if (unicode_display != unicode_default)
701 {
702 name = convert_utf8 (name);
703 }
704
705 if (info != NULL && info->elfinfo && with_symbol_versions)
706 {
707 const char *version_string;
708 bool hidden;
709
710 version_string
711 = bfd_get_symbol_version_string (abfd, &info->elfinfo->symbol,
712 false, &hidden);
713 if (version_string && version_string[0])
714 {
715 const char *at = "@@";
716 if (hidden || bfd_is_und_section (info->elfinfo->symbol.section))
717 at = "@";
718 alloc = reconcat (alloc, name, at, version_string, NULL);
719 if (alloc != NULL)
720 name = alloc;
721 }
722 }
723 printf (form, name);
724 if (atver)
725 *atver = '@';
726 free (alloc);
727 }
728
729 static void
730 print_symdef_entry (bfd *abfd)
731 {
732 symindex idx = BFD_NO_MORE_SYMBOLS;
733 carsym *thesym;
734 bool everprinted = false;
735
736 for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
737 idx != BFD_NO_MORE_SYMBOLS;
738 idx = bfd_get_next_mapent (abfd, idx, &thesym))
739 {
740 if (!everprinted)
741 {
742 printf (_("\nArchive index:\n"));
743 everprinted = true;
744 }
745 if (thesym->name != NULL)
746 {
747 print_symname ("%s", NULL, thesym->name, abfd);
748 bfd *elt = bfd_get_elt_at_index (abfd, idx);
749 if (elt)
750 printf (" in %s\n", bfd_get_filename (elt));
751 else
752 printf ("\n");
753 }
754 }
755 }
756 \f
757
758 /* True when we can report missing plugin error. */
759 bool report_plugin_err = true;
760
761 /* Choose which symbol entries to print;
762 compact them downward to get rid of the rest.
763 Return the number of symbols to be printed. */
764
765 static long
766 filter_symbols (bfd *abfd, bool is_dynamic, void *minisyms,
767 long symcount, unsigned int size)
768 {
769 bfd_byte *from, *fromend, *to;
770 asymbol *store;
771
772 store = bfd_make_empty_symbol (abfd);
773 if (store == NULL)
774 bfd_fatal (bfd_get_filename (abfd));
775
776 from = (bfd_byte *) minisyms;
777 fromend = from + symcount * size;
778 to = (bfd_byte *) minisyms;
779
780 for (; from < fromend; from += size)
781 {
782 int keep = 0;
783 asymbol *sym;
784
785 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from, store);
786 if (sym == NULL)
787 continue;
788
789 if (sym->name != NULL
790 && sym->name[0] == '_'
791 && sym->name[1] == '_'
792 && strcmp (sym->name + (sym->name[2] == '_'), "__gnu_lto_slim") == 0
793 && report_plugin_err)
794 {
795 report_plugin_err = false;
796 non_fatal (_("%s: plugin needed to handle lto object"),
797 bfd_get_filename (abfd));
798 }
799
800 if (undefined_only)
801 keep = bfd_is_und_section (sym->section);
802 else if (external_only)
803 /* PR binutls/12753: Unique symbols are global too. */
804 keep = ((sym->flags & (BSF_GLOBAL
805 | BSF_WEAK
806 | BSF_GNU_UNIQUE)) != 0
807 || bfd_is_und_section (sym->section)
808 || bfd_is_com_section (sym->section));
809 else if (non_weak)
810 keep = ((sym->flags & BSF_WEAK) == 0);
811 else
812 keep = 1;
813
814 if (keep
815 && ! print_debug_syms
816 && (sym->flags & BSF_DEBUGGING) != 0)
817 keep = 0;
818
819 if (keep
820 && sort_by_size
821 && (bfd_is_abs_section (sym->section)
822 || bfd_is_und_section (sym->section)))
823 keep = 0;
824
825 if (keep
826 && defined_only)
827 {
828 if (bfd_is_und_section (sym->section))
829 keep = 0;
830 }
831
832 if (keep
833 && bfd_is_target_special_symbol (abfd, sym)
834 && ! allow_special_symbols)
835 keep = 0;
836
837 if (keep)
838 {
839 if (to != from)
840 memcpy (to, from, size);
841 to += size;
842 }
843 }
844
845 return (to - (bfd_byte *) minisyms) / size;
846 }
847 \f
848 /* These globals are used to pass information into the sorting
849 routines. */
850 static bfd *sort_bfd;
851 static bool sort_dynamic;
852 static asymbol *sort_x;
853 static asymbol *sort_y;
854
855 /* Symbol-sorting predicates */
856 #define valueof(x) ((x)->section->vma + (x)->value)
857
858 /* Numeric sorts. Undefined symbols are always considered "less than"
859 defined symbols with zero values. Common symbols are not treated
860 specially -- i.e., their sizes are used as their "values". */
861
862 static int
863 non_numeric_forward (const void *P_x, const void *P_y)
864 {
865 asymbol *x, *y;
866 const char *xn, *yn;
867
868 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
869 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
870 if (x == NULL || y == NULL)
871 bfd_fatal (bfd_get_filename (sort_bfd));
872
873 xn = bfd_asymbol_name (x);
874 yn = bfd_asymbol_name (y);
875
876 if (yn == NULL)
877 return xn != NULL;
878 if (xn == NULL)
879 return -1;
880
881 /* Solaris 2.5 has a bug in strcoll.
882 strcoll returns invalid values when confronted with empty strings. */
883 if (*yn == '\0')
884 return *xn != '\0';
885 if (*xn == '\0')
886 return -1;
887
888 return strcoll (xn, yn);
889 }
890
891 static int
892 non_numeric_reverse (const void *x, const void *y)
893 {
894 return - non_numeric_forward (x, y);
895 }
896
897 static int
898 numeric_forward (const void *P_x, const void *P_y)
899 {
900 asymbol *x, *y;
901 asection *xs, *ys;
902
903 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
904 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
905 if (x == NULL || y == NULL)
906 bfd_fatal (bfd_get_filename (sort_bfd));
907
908 xs = bfd_asymbol_section (x);
909 ys = bfd_asymbol_section (y);
910
911 if (bfd_is_und_section (xs))
912 {
913 if (! bfd_is_und_section (ys))
914 return -1;
915 }
916 else if (bfd_is_und_section (ys))
917 return 1;
918 else if (valueof (x) != valueof (y))
919 return valueof (x) < valueof (y) ? -1 : 1;
920
921 return non_numeric_forward (P_x, P_y);
922 }
923
924 static int
925 numeric_reverse (const void *x, const void *y)
926 {
927 return - numeric_forward (x, y);
928 }
929
930 static int (*(sorters[2][2])) (const void *, const void *) =
931 {
932 { non_numeric_forward, non_numeric_reverse },
933 { numeric_forward, numeric_reverse }
934 };
935
936 /* This sort routine is used by sort_symbols_by_size. It is similar
937 to numeric_forward, but when symbols have the same value it sorts
938 by section VMA. This simplifies the sort_symbols_by_size code
939 which handles symbols at the end of sections. Also, this routine
940 tries to sort file names before other symbols with the same value.
941 That will make the file name have a zero size, which will make
942 sort_symbols_by_size choose the non file name symbol, leading to
943 more meaningful output. For similar reasons, this code sorts
944 gnu_compiled_* and gcc2_compiled before other symbols with the same
945 value. */
946
947 static int
948 size_forward1 (const void *P_x, const void *P_y)
949 {
950 asymbol *x, *y;
951 asection *xs, *ys;
952 const char *xn, *yn;
953 size_t xnl, ynl;
954 int xf, yf;
955
956 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
957 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
958 if (x == NULL || y == NULL)
959 bfd_fatal (bfd_get_filename (sort_bfd));
960
961 xs = bfd_asymbol_section (x);
962 ys = bfd_asymbol_section (y);
963
964 if (bfd_is_und_section (xs))
965 abort ();
966 if (bfd_is_und_section (ys))
967 abort ();
968
969 if (valueof (x) != valueof (y))
970 return valueof (x) < valueof (y) ? -1 : 1;
971
972 if (xs->vma != ys->vma)
973 return xs->vma < ys->vma ? -1 : 1;
974
975 xn = bfd_asymbol_name (x);
976 yn = bfd_asymbol_name (y);
977 xnl = strlen (xn);
978 ynl = strlen (yn);
979
980 /* The symbols gnu_compiled and gcc2_compiled convey even less
981 information than the file name, so sort them out first. */
982
983 xf = (strstr (xn, "gnu_compiled") != NULL
984 || strstr (xn, "gcc2_compiled") != NULL);
985 yf = (strstr (yn, "gnu_compiled") != NULL
986 || strstr (yn, "gcc2_compiled") != NULL);
987
988 if (xf && ! yf)
989 return -1;
990 if (! xf && yf)
991 return 1;
992
993 /* We use a heuristic for the file name. It may not work on non
994 Unix systems, but it doesn't really matter; the only difference
995 is precisely which symbol names get printed. */
996
997 #define file_symbol(s, sn, snl) \
998 (((s)->flags & BSF_FILE) != 0 \
999 || ((snl) > 2 \
1000 && (sn)[(snl) - 2] == '.' \
1001 && ((sn)[(snl) - 1] == 'o' \
1002 || (sn)[(snl) - 1] == 'a')))
1003
1004 xf = file_symbol (x, xn, xnl);
1005 yf = file_symbol (y, yn, ynl);
1006
1007 if (xf && ! yf)
1008 return -1;
1009 if (! xf && yf)
1010 return 1;
1011
1012 return non_numeric_forward (P_x, P_y);
1013 }
1014
1015 /* This sort routine is used by sort_symbols_by_size. It is sorting
1016 an array of size_sym structures into size order. */
1017
1018 static int
1019 size_forward2 (const void *P_x, const void *P_y)
1020 {
1021 const struct size_sym *x = (const struct size_sym *) P_x;
1022 const struct size_sym *y = (const struct size_sym *) P_y;
1023
1024 if (x->size < y->size)
1025 return reverse_sort ? 1 : -1;
1026 else if (x->size > y->size)
1027 return reverse_sort ? -1 : 1;
1028 else
1029 return sorters[0][reverse_sort] (x->minisym, y->minisym);
1030 }
1031
1032 /* Sort the symbols by size. ELF provides a size but for other formats
1033 we have to make a guess by assuming that the difference between the
1034 address of a symbol and the address of the next higher symbol is the
1035 size. */
1036
1037 static long
1038 sort_symbols_by_size (bfd *abfd, bool is_dynamic, void *minisyms,
1039 long symcount, unsigned int size,
1040 struct size_sym **symsizesp)
1041 {
1042 struct size_sym *symsizes;
1043 bfd_byte *from, *fromend;
1044 asymbol *sym = NULL;
1045 asymbol *store_sym, *store_next;
1046
1047 qsort (minisyms, symcount, size, size_forward1);
1048
1049 /* We are going to return a special set of symbols and sizes to
1050 print. */
1051 symsizes = (struct size_sym *) xmalloc (symcount * sizeof (struct size_sym));
1052 *symsizesp = symsizes;
1053
1054 /* Note that filter_symbols has already removed all absolute and
1055 undefined symbols. Here we remove all symbols whose size winds
1056 up as zero. */
1057 from = (bfd_byte *) minisyms;
1058 fromend = from + symcount * size;
1059
1060 store_sym = sort_x;
1061 store_next = sort_y;
1062
1063 if (from < fromend)
1064 {
1065 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, (const void *) from,
1066 store_sym);
1067 if (sym == NULL)
1068 bfd_fatal (bfd_get_filename (abfd));
1069 }
1070
1071 for (; from < fromend; from += size)
1072 {
1073 asymbol *next;
1074 asection *sec;
1075 bfd_vma sz;
1076 asymbol *temp;
1077
1078 if (from + size < fromend)
1079 {
1080 next = bfd_minisymbol_to_symbol (abfd,
1081 is_dynamic,
1082 (const void *) (from + size),
1083 store_next);
1084 if (next == NULL)
1085 bfd_fatal (bfd_get_filename (abfd));
1086 }
1087 else
1088 next = NULL;
1089
1090 sec = bfd_asymbol_section (sym);
1091
1092 /* Synthetic symbols don't have a full type set of data available, thus
1093 we can't rely on that information for the symbol size. Ditto for
1094 bfd/section.c:global_syms like *ABS*. */
1095 if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0
1096 && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1097 sz = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
1098 else if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0
1099 && bfd_is_com_section (sec))
1100 sz = sym->value;
1101 else
1102 {
1103 if (from + size < fromend
1104 && sec == bfd_asymbol_section (next))
1105 sz = valueof (next) - valueof (sym);
1106 else
1107 sz = (bfd_section_vma (sec)
1108 + bfd_section_size (sec)
1109 - valueof (sym));
1110 }
1111
1112 if (sz != 0)
1113 {
1114 symsizes->minisym = (const void *) from;
1115 symsizes->size = sz;
1116 ++symsizes;
1117 }
1118
1119 sym = next;
1120
1121 temp = store_sym;
1122 store_sym = store_next;
1123 store_next = temp;
1124 }
1125
1126 symcount = symsizes - *symsizesp;
1127
1128 /* We must now sort again by size. */
1129 qsort ((void *) *symsizesp, symcount, sizeof (struct size_sym), size_forward2);
1130
1131 return symcount;
1132 }
1133
1134 /* This function is used to get the relocs for a particular section.
1135 It is called via bfd_map_over_sections. */
1136
1137 static void
1138 get_relocs (bfd *abfd, asection *sec, void *dataarg)
1139 {
1140 struct lineno_cache *data = (struct lineno_cache *) dataarg;
1141
1142 *data->secs = sec;
1143 *data->relocs = NULL;
1144 *data->relcount = 0;
1145
1146 if ((sec->flags & SEC_RELOC) != 0)
1147 {
1148 long relsize = bfd_get_reloc_upper_bound (abfd, sec);
1149 if (relsize > 0)
1150 {
1151 *data->relocs = (arelent **) xmalloc (relsize);
1152 *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs,
1153 data->syms);
1154 }
1155 }
1156
1157 ++data->secs;
1158 ++data->relocs;
1159 ++data->relcount;
1160 }
1161
1162 static void
1163 free_lineno_cache (bfd *abfd)
1164 {
1165 struct lineno_cache *lc = bfd_usrdata (abfd);
1166
1167 if (lc)
1168 {
1169 if (lc->relocs)
1170 for (unsigned int i = 0; i < lc->seccount; i++)
1171 free (lc->relocs[i]);
1172 free (lc->relcount);
1173 free (lc->relocs);
1174 free (lc->secs);
1175 free (lc->syms);
1176 free (lc);
1177 bfd_set_usrdata (abfd, NULL);
1178 }
1179 }
1180
1181 /* Print a single symbol. */
1182
1183 static void
1184 print_symbol (bfd * abfd,
1185 asymbol * sym,
1186 bfd_vma ssize,
1187 bfd * archive_bfd)
1188 {
1189 symbol_info syminfo;
1190 struct extended_symbol_info info;
1191
1192 format->print_symbol_filename (archive_bfd, abfd);
1193
1194 bfd_get_symbol_info (abfd, sym, &syminfo);
1195
1196 /* PR 22967 - Distinguish between local and global ifunc symbols. */
1197 if (syminfo.type == 'i'
1198 && sym->flags & BSF_GNU_INDIRECT_FUNCTION)
1199 {
1200 if (ifunc_type_chars == NULL || ifunc_type_chars[0] == 0)
1201 ; /* Change nothing. */
1202 else if (sym->flags & BSF_GLOBAL)
1203 syminfo.type = ifunc_type_chars[0];
1204 else if (ifunc_type_chars[1] != 0)
1205 syminfo.type = ifunc_type_chars[1];
1206 }
1207
1208 info.sinfo = &syminfo;
1209 info.ssize = ssize;
1210 /* Synthetic symbols do not have a full symbol type set of data available.
1211 Nor do bfd/section.c:global_syms like *ABS*. */
1212 if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) != 0)
1213 {
1214 info.elfinfo = NULL;
1215 info.coffinfo = NULL;
1216 }
1217 else
1218 {
1219 info.elfinfo = elf_symbol_from (sym);
1220 info.coffinfo = coff_symbol_from (sym);
1221 }
1222
1223 format->print_symbol_info (&info, abfd);
1224
1225 if (line_numbers)
1226 {
1227 struct lineno_cache *lc = bfd_usrdata (abfd);
1228 const char *filename, *functionname;
1229 unsigned int lineno;
1230
1231 /* We need to get the canonical symbols in order to call
1232 bfd_find_nearest_line. This is inefficient, but, then, you
1233 don't have to use --line-numbers. */
1234 if (lc == NULL)
1235 {
1236 lc = xcalloc (1, sizeof (*lc));
1237 bfd_set_usrdata (abfd, lc);
1238 }
1239 if (lc->syms == NULL && lc->symcount == 0)
1240 {
1241 long symsize = bfd_get_symtab_upper_bound (abfd);
1242 if (symsize <= 0)
1243 lc->symcount = -1;
1244 else
1245 {
1246 lc->syms = xmalloc (symsize);
1247 lc->symcount = bfd_canonicalize_symtab (abfd, lc->syms);
1248 }
1249 }
1250
1251 if (lc->symcount <= 0)
1252 ;
1253 else if (bfd_is_und_section (bfd_asymbol_section (sym)))
1254 {
1255 unsigned int i;
1256 const char *symname;
1257
1258 /* For an undefined symbol, we try to find a reloc for the
1259 symbol, and print the line number of the reloc. */
1260 if (lc->relocs == NULL)
1261 {
1262 unsigned int seccount = bfd_count_sections (abfd);
1263 lc->seccount = seccount;
1264 lc->secs = xmalloc (seccount * sizeof (*lc->secs));
1265 lc->relocs = xmalloc (seccount * sizeof (*lc->relocs));
1266 lc->relcount = xmalloc (seccount * sizeof (*lc->relcount));
1267
1268 struct lineno_cache rinfo = *lc;
1269 bfd_map_over_sections (abfd, get_relocs, &rinfo);
1270 }
1271
1272 symname = bfd_asymbol_name (sym);
1273 for (i = 0; i < lc->seccount; i++)
1274 {
1275 long j;
1276
1277 for (j = 0; j < lc->relcount[i]; j++)
1278 {
1279 arelent *r;
1280
1281 r = lc->relocs[i][j];
1282 if (r->sym_ptr_ptr != NULL
1283 && (*r->sym_ptr_ptr)->section == sym->section
1284 && (*r->sym_ptr_ptr)->value == sym->value
1285 && strcmp (symname,
1286 bfd_asymbol_name (*r->sym_ptr_ptr)) == 0
1287 && bfd_find_nearest_line (abfd, lc->secs[i], lc->syms,
1288 r->address, &filename,
1289 &functionname, &lineno)
1290 && filename != NULL)
1291 {
1292 /* We only print the first one we find. */
1293 printf ("\t%s:%u", filename, lineno);
1294 i = lc->seccount;
1295 break;
1296 }
1297 }
1298 }
1299 }
1300 else if (bfd_asymbol_section (sym)->owner == abfd)
1301 {
1302 if ((bfd_find_line (abfd, lc->syms, sym, &filename, &lineno)
1303 || bfd_find_nearest_line (abfd, bfd_asymbol_section (sym),
1304 lc->syms, sym->value, &filename,
1305 &functionname, &lineno))
1306 && filename != NULL
1307 && lineno != 0)
1308 printf ("\t%s:%u", filename, lineno);
1309 }
1310 }
1311
1312 putchar ('\n');
1313 }
1314 \f
1315 /* Print the symbols when sorting by size. */
1316
1317 static void
1318 print_size_symbols (bfd *abfd,
1319 bool is_dynamic,
1320 struct size_sym *symsizes,
1321 long symcount,
1322 bfd *archive_bfd)
1323 {
1324 asymbol *store;
1325 struct size_sym *from;
1326 struct size_sym *fromend;
1327
1328 store = bfd_make_empty_symbol (abfd);
1329 if (store == NULL)
1330 bfd_fatal (bfd_get_filename (abfd));
1331
1332 from = symsizes;
1333 fromend = from + symcount;
1334
1335 for (; from < fromend; from++)
1336 {
1337 asymbol *sym;
1338
1339 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from->minisym, store);
1340 if (sym == NULL)
1341 bfd_fatal (bfd_get_filename (abfd));
1342
1343 print_symbol (abfd, sym, from->size, archive_bfd);
1344 }
1345 }
1346
1347 \f
1348 /* Print the symbols of ABFD that are held in MINISYMS.
1349
1350 If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD.
1351
1352 SYMCOUNT is the number of symbols in MINISYMS.
1353
1354 SIZE is the size of a symbol in MINISYMS. */
1355
1356 static void
1357 print_symbols (bfd *abfd,
1358 bool is_dynamic,
1359 void *minisyms,
1360 long symcount,
1361 unsigned int size,
1362 bfd *archive_bfd)
1363 {
1364 asymbol *store;
1365 bfd_byte *from;
1366 bfd_byte *fromend;
1367
1368 store = bfd_make_empty_symbol (abfd);
1369 if (store == NULL)
1370 bfd_fatal (bfd_get_filename (abfd));
1371
1372 from = (bfd_byte *) minisyms;
1373 fromend = from + symcount * size;
1374
1375 for (; from < fromend; from += size)
1376 {
1377 asymbol *sym;
1378
1379 sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from, store);
1380 if (sym == NULL)
1381 bfd_fatal (bfd_get_filename (abfd));
1382
1383 print_symbol (abfd, sym, (bfd_vma) 0, archive_bfd);
1384 }
1385 }
1386
1387 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
1388
1389 static void
1390 display_rel_file (bfd *abfd, bfd *archive_bfd)
1391 {
1392 long symcount;
1393 void *minisyms;
1394 unsigned int size;
1395 struct size_sym *symsizes;
1396 asymbol *synthsyms = NULL;
1397
1398 if (! dynamic)
1399 {
1400 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
1401 {
1402 if (!quiet)
1403 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
1404 return;
1405 }
1406 }
1407
1408 symcount = bfd_read_minisymbols (abfd, dynamic, &minisyms, &size);
1409 if (symcount <= 0)
1410 {
1411 if (!quiet)
1412 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
1413 return;
1414 }
1415
1416 if (show_synthetic && size == sizeof (asymbol *))
1417 {
1418 asymbol **static_syms = NULL;
1419 asymbol **dyn_syms = NULL;
1420 long static_count = 0;
1421 long dyn_count = 0;
1422 long synth_count;
1423
1424 if (dynamic)
1425 {
1426 dyn_count = symcount;
1427 dyn_syms = (asymbol **) minisyms;
1428 }
1429 else
1430 {
1431 long storage = bfd_get_dynamic_symtab_upper_bound (abfd);
1432
1433 static_count = symcount;
1434 static_syms = (asymbol **) minisyms;
1435
1436 if (storage > 0)
1437 {
1438 dyn_syms = (asymbol **) xmalloc (storage);
1439 dyn_count = bfd_canonicalize_dynamic_symtab (abfd, dyn_syms);
1440 if (dyn_count < 0)
1441 dyn_count = 0;
1442 }
1443 }
1444
1445 synth_count = bfd_get_synthetic_symtab (abfd, static_count, static_syms,
1446 dyn_count, dyn_syms, &synthsyms);
1447 if (synth_count > 0)
1448 {
1449 asymbol **symp;
1450 long i;
1451
1452 minisyms = xrealloc (minisyms,
1453 (symcount + synth_count + 1) * sizeof (*symp));
1454 symp = (asymbol **) minisyms + symcount;
1455 for (i = 0; i < synth_count; i++)
1456 *symp++ = synthsyms + i;
1457 *symp = 0;
1458 symcount += synth_count;
1459 }
1460 if (!dynamic && dyn_syms != NULL)
1461 free (dyn_syms);
1462 }
1463
1464 /* lto_slim_object is set to false when a bfd is loaded with a compiler
1465 LTO plugin. */
1466 if (abfd->lto_slim_object)
1467 {
1468 report_plugin_err = false;
1469 non_fatal (_("%s: plugin needed to handle lto object"),
1470 bfd_get_filename (abfd));
1471 }
1472
1473 /* Discard the symbols we don't want to print.
1474 It's OK to do this in place; we'll free the storage anyway
1475 (after printing). */
1476
1477 symcount = filter_symbols (abfd, dynamic, minisyms, symcount, size);
1478
1479 symsizes = NULL;
1480 if (! no_sort)
1481 {
1482 sort_bfd = abfd;
1483 sort_dynamic = dynamic;
1484 sort_x = bfd_make_empty_symbol (abfd);
1485 sort_y = bfd_make_empty_symbol (abfd);
1486 if (sort_x == NULL || sort_y == NULL)
1487 bfd_fatal (bfd_get_filename (abfd));
1488
1489 if (! sort_by_size)
1490 qsort (minisyms, symcount, size,
1491 sorters[sort_numerically][reverse_sort]);
1492 else
1493 symcount = sort_symbols_by_size (abfd, dynamic, minisyms, symcount,
1494 size, &symsizes);
1495 }
1496
1497 if (! sort_by_size)
1498 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd);
1499 else
1500 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd);
1501
1502 if (synthsyms)
1503 free (synthsyms);
1504 free (minisyms);
1505 free (symsizes);
1506 }
1507
1508 /* Construct a formatting string for printing symbol values. */
1509
1510 static const char *
1511 get_print_format (void)
1512 {
1513 const char * padding;
1514 if (print_format == FORMAT_POSIX || print_format == FORMAT_JUST_SYMBOLS)
1515 {
1516 /* POSIX compatible output does not have any padding. */
1517 padding = "";
1518 }
1519 else if (print_width == 32)
1520 {
1521 padding ="08";
1522 }
1523 else /* print_width == 64 */
1524 {
1525 padding = "016";
1526 }
1527
1528 const char * radix = NULL;
1529 switch (print_radix)
1530 {
1531 case 8: radix = PRIo64; break;
1532 case 10: radix = PRId64; break;
1533 case 16: radix = PRIx64; break;
1534 }
1535
1536 return concat ("%", padding, radix, NULL);
1537 }
1538
1539 static void
1540 set_print_width (bfd *file)
1541 {
1542 print_width = bfd_get_arch_size (file);
1543
1544 if (print_width == -1)
1545 {
1546 /* PR binutils/4292
1547 Guess the target's bitsize based on its name.
1548 We assume here than any 64-bit format will include
1549 "64" somewhere in its name. The only known exception
1550 is the MMO object file format. */
1551 if (strstr (bfd_get_target (file), "64") != NULL
1552 || strcmp (bfd_get_target (file), "mmo") == 0)
1553 print_width = 64;
1554 else
1555 print_width = 32;
1556 }
1557 free ((char *) print_format_string);
1558 print_format_string = get_print_format ();
1559 }
1560
1561 static void
1562 display_archive (bfd *file)
1563 {
1564 bfd *arfile = NULL;
1565 bfd *last_arfile = NULL;
1566 char **matching;
1567
1568 format->print_archive_filename (bfd_get_filename (file));
1569
1570 if (print_armap)
1571 print_symdef_entry (file);
1572
1573 for (;;)
1574 {
1575 arfile = bfd_openr_next_archived_file (file, arfile);
1576
1577 if (arfile == NULL)
1578 {
1579 if (bfd_get_error () != bfd_error_no_more_archived_files)
1580 bfd_nonfatal (bfd_get_filename (file));
1581 break;
1582 }
1583
1584 if (bfd_check_format_matches (arfile, bfd_object, &matching))
1585 {
1586 set_print_width (arfile);
1587 format->print_archive_member (bfd_get_filename (file),
1588 bfd_get_filename (arfile));
1589 display_rel_file (arfile, file);
1590 }
1591 else
1592 {
1593 bfd_nonfatal (bfd_get_filename (arfile));
1594 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1595 list_matching_formats (matching);
1596 }
1597
1598 if (last_arfile != NULL)
1599 {
1600 free_lineno_cache (last_arfile);
1601 bfd_close (last_arfile);
1602 if (arfile == last_arfile)
1603 return;
1604 }
1605 last_arfile = arfile;
1606 }
1607
1608 if (last_arfile != NULL)
1609 {
1610 free_lineno_cache (last_arfile);
1611 bfd_close (last_arfile);
1612 }
1613 }
1614
1615 static bool
1616 display_file (char *filename)
1617 {
1618 bool retval = true;
1619 bfd *file;
1620 char **matching;
1621
1622 if (get_file_size (filename) < 1)
1623 return false;
1624
1625 file = bfd_openr (filename, target ? target : plugin_target);
1626 if (file == NULL)
1627 {
1628 bfd_nonfatal (filename);
1629 return false;
1630 }
1631
1632 /* If printing line numbers, decompress the debug sections. */
1633 if (line_numbers)
1634 file->flags |= BFD_DECOMPRESS;
1635
1636 if (bfd_check_format (file, bfd_archive))
1637 {
1638 display_archive (file);
1639 }
1640 else if (bfd_check_format_matches (file, bfd_object, &matching))
1641 {
1642 set_print_width (file);
1643 format->print_object_filename (filename);
1644 display_rel_file (file, NULL);
1645 }
1646 else
1647 {
1648 bfd_nonfatal (filename);
1649 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1650 list_matching_formats (matching);
1651 retval = false;
1652 }
1653
1654 free_lineno_cache (file);
1655 if (!bfd_close (file))
1656 retval = false;
1657
1658 return retval;
1659 }
1660 \f
1661 /* The following 3 groups of functions are called unconditionally,
1662 once at the start of processing each file of the appropriate type.
1663 They should check `filename_per_file' and `filename_per_symbol',
1664 as appropriate for their output format, to determine whether to
1665 print anything. */
1666 \f
1667 /* Print the name of an object file given on the command line. */
1668
1669 static void
1670 print_object_filename_bsd (const char *filename)
1671 {
1672 if (filename_per_file && !filename_per_symbol)
1673 printf ("\n%s:\n", filename);
1674 }
1675
1676 static void
1677 print_object_filename_sysv (const char *filename)
1678 {
1679 if (undefined_only)
1680 printf (_("\n\nUndefined symbols from %s:\n\n"), filename);
1681 else
1682 printf (_("\n\nSymbols from %s:\n\n"), filename);
1683 if (print_width == 32)
1684 printf (_("\
1685 Name Value Class Type Size Line Section\n\n"));
1686 else
1687 printf (_("\
1688 Name Value Class Type Size Line Section\n\n"));
1689 }
1690
1691 static void
1692 print_object_filename_posix (const char *filename)
1693 {
1694 if (filename_per_file && !filename_per_symbol)
1695 printf ("%s:\n", filename);
1696 }
1697
1698 static void
1699 do_not_print_object_filename (const char *filename ATTRIBUTE_UNUSED)
1700 {
1701 }
1702 \f
1703 /* Print the name of an archive file given on the command line. */
1704
1705 static void
1706 print_archive_filename_bsd (const char *filename)
1707 {
1708 if (filename_per_file)
1709 printf ("\n%s:\n", filename);
1710 }
1711
1712 static void
1713 print_archive_filename_sysv (const char *filename ATTRIBUTE_UNUSED)
1714 {
1715 }
1716
1717 static void
1718 print_archive_filename_posix (const char *filename ATTRIBUTE_UNUSED)
1719 {
1720 }
1721
1722 static void
1723 do_not_print_archive_filename (const char *filename ATTRIBUTE_UNUSED)
1724 {
1725 }
1726 \f
1727 /* Print the name of an archive member file. */
1728
1729 static void
1730 print_archive_member_bsd (const char *archive ATTRIBUTE_UNUSED,
1731 const char *filename)
1732 {
1733 if (!filename_per_symbol)
1734 printf ("\n%s:\n", filename);
1735 }
1736
1737 static void
1738 print_archive_member_sysv (const char *archive, const char *filename)
1739 {
1740 if (undefined_only)
1741 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename);
1742 else
1743 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename);
1744 if (print_width == 32)
1745 printf (_("\
1746 Name Value Class Type Size Line Section\n\n"));
1747 else
1748 printf (_("\
1749 Name Value Class Type Size Line Section\n\n"));
1750 }
1751
1752 static void
1753 print_archive_member_posix (const char *archive, const char *filename)
1754 {
1755 if (!filename_per_symbol)
1756 printf ("%s[%s]:\n", archive, filename);
1757 }
1758
1759 static void
1760 do_not_print_archive_member (const char *archive ATTRIBUTE_UNUSED,
1761 const char *filename ATTRIBUTE_UNUSED)
1762 {
1763 }
1764
1765 \f
1766 /* Print the name of the file (and archive, if there is one)
1767 containing a symbol. */
1768
1769 static void
1770 print_symbol_filename_bsd (bfd *archive_bfd, bfd *abfd)
1771 {
1772 if (filename_per_symbol)
1773 {
1774 if (archive_bfd)
1775 printf ("%s:", bfd_get_filename (archive_bfd));
1776 printf ("%s:", bfd_get_filename (abfd));
1777 }
1778 }
1779
1780 static void
1781 print_symbol_filename_sysv (bfd *archive_bfd, bfd *abfd)
1782 {
1783 if (filename_per_symbol)
1784 {
1785 if (archive_bfd)
1786 printf ("%s:", bfd_get_filename (archive_bfd));
1787 printf ("%s:", bfd_get_filename (abfd));
1788 }
1789 }
1790
1791 static void
1792 print_symbol_filename_posix (bfd *archive_bfd, bfd *abfd)
1793 {
1794 if (filename_per_symbol)
1795 {
1796 if (archive_bfd)
1797 printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
1798 bfd_get_filename (abfd));
1799 else
1800 printf ("%s: ", bfd_get_filename (abfd));
1801 }
1802 }
1803
1804 static void
1805 do_not_print_symbol_filename (bfd *archive_bfd ATTRIBUTE_UNUSED,
1806 bfd *abfd ATTRIBUTE_UNUSED)
1807 {
1808 }
1809
1810 \f
1811 /* Print a symbol value. */
1812
1813 static void
1814 print_value (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma val)
1815 {
1816 switch (print_width)
1817 {
1818 case 32:
1819 case 64:
1820 printf (print_format_string, (uint64_t) val);
1821 break;
1822
1823 default:
1824 fatal (_("Print width has not been initialized (%d)"), print_width);
1825 break;
1826 }
1827 }
1828
1829 /* Print a line of information about a symbol. */
1830
1831 static void
1832 print_symbol_info_bsd (struct extended_symbol_info *info, bfd *abfd)
1833 {
1834 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1835 {
1836 if (print_width == 64)
1837 printf (" ");
1838 printf (" ");
1839 }
1840 else
1841 {
1842 /* Normally we print the value of the symbol. If we are printing the
1843 size or sorting by size then we print its size, except for the
1844 (weird) special case where both flags are defined, in which case we
1845 print both values. This conforms to documented behaviour. */
1846 if (sort_by_size && !print_size)
1847 print_value (abfd, SYM_SIZE (info));
1848 else
1849 print_value (abfd, SYM_VALUE (info));
1850 if (print_size && SYM_SIZE (info))
1851 {
1852 printf (" ");
1853 print_value (abfd, SYM_SIZE (info));
1854 }
1855 }
1856
1857 printf (" %c", SYM_TYPE (info));
1858
1859 if (SYM_TYPE (info) == '-')
1860 {
1861 /* A stab. */
1862 printf (" ");
1863 printf (other_format, SYM_STAB_OTHER (info));
1864 printf (" ");
1865 printf (desc_format, SYM_STAB_DESC (info));
1866 printf (" %5s", SYM_STAB_NAME (info));
1867 }
1868 print_symname (" %s", info, NULL, abfd);
1869 }
1870
1871 static void
1872 print_symbol_info_sysv (struct extended_symbol_info *info, bfd *abfd)
1873 {
1874 print_symname ("%-20s|", info, NULL, abfd);
1875
1876 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1877 {
1878 if (print_width == 32)
1879 printf (" ");
1880 else
1881 printf (" ");
1882 }
1883 else
1884 print_value (abfd, SYM_VALUE (info));
1885
1886 printf ("| %c |", SYM_TYPE (info));
1887
1888 if (SYM_TYPE (info) == '-')
1889 {
1890 /* A stab. */
1891 printf ("%18s| ", SYM_STAB_NAME (info)); /* (C) Type. */
1892 printf (desc_format, SYM_STAB_DESC (info)); /* Size. */
1893 printf ("| |"); /* Line, Section. */
1894 }
1895 else
1896 {
1897 /* Type, Size, Line, Section */
1898 if (info->elfinfo)
1899 printf ("%18s|",
1900 get_elf_symbol_type (ELF_ST_TYPE (info->elfinfo->internal_elf_sym.st_info)));
1901 else if (info->coffinfo)
1902 printf ("%18s|",
1903 get_coff_symbol_type (&info->coffinfo->native->u.syment));
1904 else
1905 printf (" |");
1906
1907 if (SYM_SIZE (info))
1908 print_value (abfd, SYM_SIZE (info));
1909 else
1910 {
1911 if (print_width == 32)
1912 printf (" ");
1913 else
1914 printf (" ");
1915 }
1916
1917 if (info->elfinfo)
1918 printf("| |%s", info->elfinfo->symbol.section->name);
1919 else if (info->coffinfo)
1920 printf("| |%s", info->coffinfo->symbol.section->name);
1921 else
1922 printf("| |");
1923 }
1924 }
1925
1926 static void
1927 print_symbol_info_posix (struct extended_symbol_info *info, bfd *abfd)
1928 {
1929 print_symname ("%s ", info, NULL, abfd);
1930 printf ("%c ", SYM_TYPE (info));
1931
1932 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1933 printf (" ");
1934 else
1935 {
1936 print_value (abfd, SYM_VALUE (info));
1937 printf (" ");
1938 if (SYM_SIZE (info))
1939 print_value (abfd, SYM_SIZE (info));
1940 }
1941 }
1942
1943 static void
1944 just_print_symbol_name (struct extended_symbol_info *info, bfd *abfd)
1945 {
1946 print_symname ("%s", info, NULL, abfd);
1947 }
1948 \f
1949 int
1950 main (int argc, char **argv)
1951 {
1952 int c;
1953 int retval;
1954
1955 #ifdef HAVE_LC_MESSAGES
1956 setlocale (LC_MESSAGES, "");
1957 #endif
1958 setlocale (LC_CTYPE, "");
1959 setlocale (LC_COLLATE, "");
1960 bindtextdomain (PACKAGE, LOCALEDIR);
1961 textdomain (PACKAGE);
1962
1963 program_name = *argv;
1964 xmalloc_set_program_name (program_name);
1965 bfd_set_error_program_name (program_name);
1966 #if BFD_SUPPORTS_PLUGINS
1967 bfd_plugin_set_program_name (program_name);
1968 #endif
1969
1970 expandargv (&argc, &argv);
1971
1972 if (bfd_init () != BFD_INIT_MAGIC)
1973 fatal (_("fatal error: libbfd ABI mismatch"));
1974 set_default_bfd_target ();
1975
1976 while ((c = getopt_long (argc, argv, "aABCDef:gHhjJlnopPrSst:uU:vVvWX:",
1977 long_options, (int *) 0)) != EOF)
1978 {
1979 switch (c)
1980 {
1981 case 'a':
1982 print_debug_syms = 1;
1983 break;
1984 case 'A':
1985 case 'o':
1986 filename_per_symbol = 1;
1987 break;
1988 case 'B': /* For MIPS compatibility. */
1989 set_output_format ("bsd");
1990 break;
1991 case 'C':
1992 do_demangle = 1;
1993 if (optarg != NULL)
1994 {
1995 enum demangling_styles style;
1996
1997 style = cplus_demangle_name_to_style (optarg);
1998 if (style == unknown_demangling)
1999 fatal (_("unknown demangling style `%s'"),
2000 optarg);
2001
2002 cplus_demangle_set_style (style);
2003 }
2004 break;
2005 case OPTION_RECURSE_LIMIT:
2006 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
2007 break;
2008 case OPTION_NO_RECURSE_LIMIT:
2009 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
2010 break;
2011 case OPTION_QUIET:
2012 quiet = 1;
2013 break;
2014 case 'D':
2015 dynamic = 1;
2016 break;
2017 case 'e':
2018 /* Ignored for HP/UX compatibility. */
2019 break;
2020 case 'f':
2021 set_output_format (optarg);
2022 break;
2023 case 'g':
2024 external_only = 1;
2025 break;
2026 case 'H':
2027 case 'h':
2028 usage (stdout, 0);
2029 case 'l':
2030 line_numbers = 1;
2031 break;
2032 case 'n':
2033 case 'v':
2034 no_sort = 0;
2035 sort_numerically = 1;
2036 sort_by_size = 0;
2037 break;
2038 case 'p':
2039 no_sort = 1;
2040 sort_numerically = 0;
2041 sort_by_size = 0;
2042 break;
2043 case OPTION_SIZE_SORT:
2044 no_sort = 0;
2045 sort_numerically = 0;
2046 sort_by_size = 1;
2047 break;
2048 case 'P':
2049 set_output_format ("posix");
2050 break;
2051 case 'j':
2052 set_output_format ("just-symbols");
2053 break;
2054 case 'r':
2055 reverse_sort = 1;
2056 break;
2057 case 's':
2058 print_armap = 1;
2059 break;
2060 case 'S':
2061 print_size = 1;
2062 break;
2063 case 't':
2064 set_print_radix (optarg);
2065 break;
2066 case 'u':
2067 undefined_only = 1;
2068 defined_only = 0;
2069 break;
2070 case 'U':
2071 defined_only = 1;
2072 undefined_only = 0;
2073 break;
2074
2075 case OPTION_UNICODE:
2076 if (streq (optarg, "default") || streq (optarg, "d"))
2077 unicode_display = unicode_default;
2078 else if (streq (optarg, "locale") || streq (optarg, "l"))
2079 unicode_display = unicode_locale;
2080 else if (streq (optarg, "escape") || streq (optarg, "e"))
2081 unicode_display = unicode_escape;
2082 else if (streq (optarg, "invalid") || streq (optarg, "i"))
2083 unicode_display = unicode_invalid;
2084 else if (streq (optarg, "hex") || streq (optarg, "x"))
2085 unicode_display = unicode_hex;
2086 else if (streq (optarg, "highlight") || streq (optarg, "h"))
2087 unicode_display = unicode_highlight;
2088 else
2089 fatal (_("invalid argument to -U/--unicode: %s"), optarg);
2090 break;
2091
2092 case 'V':
2093 show_version = 1;
2094 break;
2095 case 'W':
2096 non_weak = 1;
2097 break;
2098 case 'X':
2099 /* Ignored for (partial) AIX compatibility. On AIX, the
2100 argument has values 32, 64, or 32_64, and specifies that
2101 only 32-bit, only 64-bit, or both kinds of objects should
2102 be examined. The default is 32. So plain AIX nm on a
2103 library archive with both kinds of objects will ignore
2104 the 64-bit ones. For GNU nm, the default is and always
2105 has been -X 32_64, and other options are not supported. */
2106 if (strcmp (optarg, "32_64") != 0)
2107 fatal (_("Only -X 32_64 is supported"));
2108 break;
2109
2110 case OPTION_TARGET: /* --target */
2111 target = optarg;
2112 break;
2113
2114 case OPTION_PLUGIN: /* --plugin */
2115 #if BFD_SUPPORTS_PLUGINS
2116 bfd_plugin_set_plugin (optarg);
2117 #else
2118 fatal (_("sorry - this program has been built without plugin support\n"));
2119 #endif
2120 break;
2121
2122 case OPTION_IFUNC_CHARS:
2123 ifunc_type_chars = optarg;
2124 break;
2125
2126 case 0: /* A long option that just sets a flag. */
2127 break;
2128
2129 default:
2130 usage (stderr, 1);
2131 }
2132 }
2133
2134 if (show_version)
2135 print_version ("nm");
2136
2137 if (sort_by_size && undefined_only)
2138 {
2139 non_fatal (_("Using the --size-sort and --undefined-only options together"));
2140 non_fatal (_("will produce no output, since undefined symbols have no size."));
2141 return 0;
2142 }
2143
2144 /* OK, all options now parsed. If no filename specified, do a.out. */
2145 if (optind == argc)
2146 return !display_file ("a.out");
2147
2148 retval = 0;
2149
2150 if (argc - optind > 1)
2151 filename_per_file = 1;
2152
2153 /* We were given several filenames to do. */
2154 while (optind < argc)
2155 {
2156 if (!display_file (argv[optind++]))
2157 retval++;
2158 }
2159
2160 exit (retval);
2161 return retval;
2162 }