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