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