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