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