]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/nm.c
readelf sprintf optimisation
[thirdparty/binutils-gdb.git] / binutils / nm.c
CommitLineData
252b5132 1/* nm.c -- Describe symbol table of a rel file.
d87bef3a 2 Copyright (C) 1991-2023 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;
352f6bc3 172static const char *print_format_string = NULL;
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
AM
367 fprintf (stream, _("\
368 --with-symbol-versions Display version strings after symbol names\n"));
369 fprintf (stream, _("\
370 -X 32_64 (ignored)\n"));
371 fprintf (stream, _("\
372 @FILE Read options from FILE\n"));
373 fprintf (stream, _("\
374 -h, --help Display this information\n"));
375 fprintf (stream, _("\
376 -V, --version Display this program's version number\n"));
377
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
cc5277b1
ML
1469 /* lto_slim_object is set to false when a bfd is loaded with a compiler
1470 LTO plugin. */
1471 if (abfd->lto_slim_object)
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
1515static const char *
1516get_print_format (void)
1517{
1518 const char * padding;
1996d0f1 1519 if (print_format == FORMAT_POSIX || print_format == FORMAT_JUST_SYMBOLS)
352f6bc3
AM
1520 {
1521 /* POSIX compatible output does not have any padding. */
1522 padding = "";
1523 }
1524 else if (print_width == 32)
1525 {
1526 padding ="08";
1527 }
1528 else /* print_width == 64 */
1529 {
1530 padding = "016";
1531 }
1532
352f6bc3
AM
1533 const char * radix = NULL;
1534 switch (print_radix)
1535 {
0e3c1eeb
AM
1536 case 8: radix = PRIo64; break;
1537 case 10: radix = PRId64; break;
1538 case 16: radix = PRIx64; break;
352f6bc3
AM
1539 }
1540
0e3c1eeb 1541 return concat ("%", padding, radix, NULL);
352f6bc3
AM
1542}
1543
970ccc77
NC
1544static void
1545set_print_width (bfd *file)
1546{
1547 print_width = bfd_get_arch_size (file);
1548
1549 if (print_width == -1)
1550 {
1551 /* PR binutils/4292
1552 Guess the target's bitsize based on its name.
1553 We assume here than any 64-bit format will include
1554 "64" somewhere in its name. The only known exception
1555 is the MMO object file format. */
1556 if (strstr (bfd_get_target (file), "64") != NULL
1557 || strcmp (bfd_get_target (file), "mmo") == 0)
1558 print_width = 64;
1559 else
1560 print_width = 32;
1561 }
352f6bc3
AM
1562 free ((char *) print_format_string);
1563 print_format_string = get_print_format ();
970ccc77
NC
1564}
1565
382c1116
NC
1566static void
1567display_archive (bfd *file)
1568{
1569 bfd *arfile = NULL;
1570 bfd *last_arfile = NULL;
1571 char **matching;
1572
1573 format->print_archive_filename (bfd_get_filename (file));
1574
1575 if (print_armap)
1576 print_symdef_entry (file);
1577
1578 for (;;)
252b5132 1579 {
382c1116
NC
1580 arfile = bfd_openr_next_archived_file (file, arfile);
1581
1582 if (arfile == NULL)
252b5132 1583 {
382c1116 1584 if (bfd_get_error () != bfd_error_no_more_archived_files)
86a2562b 1585 bfd_nonfatal (bfd_get_filename (file));
382c1116 1586 break;
252b5132 1587 }
382c1116
NC
1588
1589 if (bfd_check_format_matches (arfile, bfd_object, &matching))
252b5132 1590 {
970ccc77 1591 set_print_width (arfile);
382c1116
NC
1592 format->print_archive_member (bfd_get_filename (file),
1593 bfd_get_filename (arfile));
1594 display_rel_file (arfile, file);
252b5132 1595 }
382c1116 1596 else
252b5132 1597 {
382c1116
NC
1598 bfd_nonfatal (bfd_get_filename (arfile));
1599 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
370426d0 1600 list_matching_formats (matching);
382c1116 1601 }
252b5132 1602
382c1116
NC
1603 if (last_arfile != NULL)
1604 {
e3f450f3 1605 free_lineno_cache (last_arfile);
382c1116 1606 bfd_close (last_arfile);
896ca098
NC
1607 if (arfile == last_arfile)
1608 return;
382c1116
NC
1609 }
1610 last_arfile = arfile;
1611 }
252b5132 1612
382c1116
NC
1613 if (last_arfile != NULL)
1614 {
e3f450f3 1615 free_lineno_cache (last_arfile);
382c1116 1616 bfd_close (last_arfile);
382c1116
NC
1617 }
1618}
252b5132 1619
015dc7e1 1620static bool
382c1116
NC
1621display_file (char *filename)
1622{
015dc7e1 1623 bool retval = true;
382c1116
NC
1624 bfd *file;
1625 char **matching;
252b5132 1626
382c1116 1627 if (get_file_size (filename) < 1)
015dc7e1 1628 return false;
252b5132 1629
a4b8af35 1630 file = bfd_openr (filename, target ? target : plugin_target);
382c1116
NC
1631 if (file == NULL)
1632 {
1633 bfd_nonfatal (filename);
015dc7e1 1634 return false;
382c1116 1635 }
252b5132 1636
b76e66d3
CC
1637 /* If printing line numbers, decompress the debug sections. */
1638 if (line_numbers)
1639 file->flags |= BFD_DECOMPRESS;
1640
382c1116
NC
1641 if (bfd_check_format (file, bfd_archive))
1642 {
1643 display_archive (file);
1644 }
1645 else if (bfd_check_format_matches (file, bfd_object, &matching))
1646 {
970ccc77 1647 set_print_width (file);
382c1116
NC
1648 format->print_object_filename (filename);
1649 display_rel_file (file, NULL);
1650 }
1651 else
1652 {
1653 bfd_nonfatal (filename);
1654 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
370426d0 1655 list_matching_formats (matching);
015dc7e1 1656 retval = false;
252b5132
RH
1657 }
1658
e3f450f3 1659 free_lineno_cache (file);
382c1116 1660 if (!bfd_close (file))
86a2562b 1661 retval = false;
382c1116 1662
382c1116 1663 return retval;
252b5132
RH
1664}
1665\f
1666/* The following 3 groups of functions are called unconditionally,
1667 once at the start of processing each file of the appropriate type.
1668 They should check `filename_per_file' and `filename_per_symbol',
1669 as appropriate for their output format, to determine whether to
1670 print anything. */
1671\f
1672/* Print the name of an object file given on the command line. */
1673
1674static void
b16c44de 1675print_object_filename_bsd (const char *filename)
252b5132
RH
1676{
1677 if (filename_per_file && !filename_per_symbol)
1678 printf ("\n%s:\n", filename);
1679}
1680
1681static void
b16c44de 1682print_object_filename_sysv (const char *filename)
252b5132
RH
1683{
1684 if (undefined_only)
1685 printf (_("\n\nUndefined symbols from %s:\n\n"), filename);
1686 else
1687 printf (_("\n\nSymbols from %s:\n\n"), filename);
970ccc77 1688 if (print_width == 32)
33f5f537
L
1689 printf (_("\
1690Name Value Class Type Size Line Section\n\n"));
1691 else
1692 printf (_("\
1693Name Value Class Type Size Line Section\n\n"));
252b5132
RH
1694}
1695
1696static void
b16c44de 1697print_object_filename_posix (const char *filename)
252b5132
RH
1698{
1699 if (filename_per_file && !filename_per_symbol)
1700 printf ("%s:\n", filename);
1701}
1996d0f1
NC
1702
1703static void
1704do_not_print_object_filename (const char *filename ATTRIBUTE_UNUSED)
1705{
1706}
252b5132
RH
1707\f
1708/* Print the name of an archive file given on the command line. */
1709
1710static void
b16c44de 1711print_archive_filename_bsd (const char *filename)
252b5132
RH
1712{
1713 if (filename_per_file)
1714 printf ("\n%s:\n", filename);
1715}
1716
1717static void
b16c44de 1718print_archive_filename_sysv (const char *filename ATTRIBUTE_UNUSED)
252b5132
RH
1719{
1720}
1721
1722static void
b16c44de 1723print_archive_filename_posix (const char *filename ATTRIBUTE_UNUSED)
252b5132
RH
1724{
1725}
1996d0f1
NC
1726
1727static void
1728do_not_print_archive_filename (const char *filename ATTRIBUTE_UNUSED)
1729{
1730}
252b5132
RH
1731\f
1732/* Print the name of an archive member file. */
1733
1734static void
b16c44de 1735print_archive_member_bsd (const char *archive ATTRIBUTE_UNUSED,
2da42df6 1736 const char *filename)
252b5132
RH
1737{
1738 if (!filename_per_symbol)
1739 printf ("\n%s:\n", filename);
1740}
1741
1742static void
b16c44de 1743print_archive_member_sysv (const char *archive, const char *filename)
252b5132
RH
1744{
1745 if (undefined_only)
1746 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename);
1747 else
1748 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename);
970ccc77 1749 if (print_width == 32)
33f5f537
L
1750 printf (_("\
1751Name Value Class Type Size Line Section\n\n"));
1752 else
1753 printf (_("\
1754Name Value Class Type Size Line Section\n\n"));
252b5132
RH
1755}
1756
1757static void
b16c44de 1758print_archive_member_posix (const char *archive, const char *filename)
252b5132
RH
1759{
1760 if (!filename_per_symbol)
1761 printf ("%s[%s]:\n", archive, filename);
1762}
1996d0f1
NC
1763
1764static void
1765do_not_print_archive_member (const char *archive ATTRIBUTE_UNUSED,
1766 const char *filename ATTRIBUTE_UNUSED)
1767{
1768}
1769
252b5132
RH
1770\f
1771/* Print the name of the file (and archive, if there is one)
1772 containing a symbol. */
1773
1774static void
2da42df6 1775print_symbol_filename_bsd (bfd *archive_bfd, bfd *abfd)
252b5132
RH
1776{
1777 if (filename_per_symbol)
1778 {
1779 if (archive_bfd)
1780 printf ("%s:", bfd_get_filename (archive_bfd));
1781 printf ("%s:", bfd_get_filename (abfd));
1782 }
1783}
1784
1785static void
2da42df6 1786print_symbol_filename_sysv (bfd *archive_bfd, bfd *abfd)
252b5132
RH
1787{
1788 if (filename_per_symbol)
1789 {
1790 if (archive_bfd)
1791 printf ("%s:", bfd_get_filename (archive_bfd));
1792 printf ("%s:", bfd_get_filename (abfd));
1793 }
1794}
1795
1796static void
2da42df6 1797print_symbol_filename_posix (bfd *archive_bfd, bfd *abfd)
252b5132
RH
1798{
1799 if (filename_per_symbol)
1800 {
1801 if (archive_bfd)
1802 printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
1803 bfd_get_filename (abfd));
1804 else
1805 printf ("%s: ", bfd_get_filename (abfd));
1806 }
1807}
1996d0f1
NC
1808
1809static void
1810do_not_print_symbol_filename (bfd *archive_bfd ATTRIBUTE_UNUSED,
1811 bfd *abfd ATTRIBUTE_UNUSED)
1812{
1813}
1814
252b5132
RH
1815\f
1816/* Print a symbol value. */
1817
1818static void
2da42df6 1819print_value (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma val)
252b5132 1820{
970ccc77 1821 switch (print_width)
252b5132 1822 {
970ccc77 1823 case 32:
970ccc77 1824 case 64:
0e3c1eeb 1825 printf (print_format_string, (uint64_t) val);
970ccc77
NC
1826 break;
1827
1828 default:
1829 fatal (_("Print width has not been initialized (%d)"), print_width);
1830 break;
1831 }
252b5132
RH
1832}
1833
1834/* Print a line of information about a symbol. */
1835
1836static void
2da42df6 1837print_symbol_info_bsd (struct extended_symbol_info *info, bfd *abfd)
252b5132 1838{
977f7911 1839 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
252b5132 1840 {
970ccc77 1841 if (print_width == 64)
62a5a82d 1842 printf (" ");
21211521 1843 printf (" ");
252b5132
RH
1844 }
1845 else
977f7911 1846 {
06a30c77 1847 /* Normally we print the value of the symbol. If we are printing the
50c2245b 1848 size or sorting by size then we print its size, except for the
06a30c77
NC
1849 (weird) special case where both flags are defined, in which case we
1850 print both values. This conforms to documented behaviour. */
1851 if (sort_by_size && !print_size)
1852 print_value (abfd, SYM_SIZE (info));
1853 else
1854 print_value (abfd, SYM_VALUE (info));
72797995 1855 if (print_size && SYM_SIZE (info))
977f7911 1856 {
06a30c77 1857 printf (" ");
977f7911
NC
1858 print_value (abfd, SYM_SIZE (info));
1859 }
1860 }
1861
1862 printf (" %c", SYM_TYPE (info));
1863
1864 if (SYM_TYPE (info) == '-')
252b5132
RH
1865 {
1866 /* A stab. */
1867 printf (" ");
977f7911 1868 printf (other_format, SYM_STAB_OTHER (info));
252b5132 1869 printf (" ");
977f7911
NC
1870 printf (desc_format, SYM_STAB_DESC (info));
1871 printf (" %5s", SYM_STAB_NAME (info));
252b5132 1872 }
7e6e972f 1873 print_symname (" %s", info, NULL, abfd);
252b5132
RH
1874}
1875
1876static void
2da42df6 1877print_symbol_info_sysv (struct extended_symbol_info *info, bfd *abfd)
252b5132 1878{
7e6e972f 1879 print_symname ("%-20s|", info, NULL, abfd);
977f7911
NC
1880
1881 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
33f5f537 1882 {
970ccc77 1883 if (print_width == 32)
33f5f537
L
1884 printf (" ");
1885 else
1886 printf (" ");
1887 }
252b5132 1888 else
977f7911
NC
1889 print_value (abfd, SYM_VALUE (info));
1890
1891 printf ("| %c |", SYM_TYPE (info));
1892
1893 if (SYM_TYPE (info) == '-')
252b5132
RH
1894 {
1895 /* A stab. */
e3b83c8f
NC
1896 printf ("%18s| ", SYM_STAB_NAME (info)); /* (C) Type. */
1897 printf (desc_format, SYM_STAB_DESC (info)); /* Size. */
1898 printf ("| |"); /* Line, Section. */
252b5132
RH
1899 }
1900 else
9710509e 1901 {
977f7911 1902 /* Type, Size, Line, Section */
33f5f537
L
1903 if (info->elfinfo)
1904 printf ("%18s|",
552e55ed
JB
1905 get_elf_symbol_type (ELF_ST_TYPE (info->elfinfo->internal_elf_sym.st_info)));
1906 else if (info->coffinfo)
1907 printf ("%18s|",
1908 get_coff_symbol_type (&info->coffinfo->native->u.syment));
33f5f537
L
1909 else
1910 printf (" |");
977f7911
NC
1911
1912 if (SYM_SIZE (info))
1913 print_value (abfd, SYM_SIZE (info));
1914 else
33f5f537 1915 {
970ccc77 1916 if (print_width == 32)
33f5f537
L
1917 printf (" ");
1918 else
1919 printf (" ");
1920 }
977f7911 1921
33f5f537
L
1922 if (info->elfinfo)
1923 printf("| |%s", info->elfinfo->symbol.section->name);
552e55ed
JB
1924 else if (info->coffinfo)
1925 printf("| |%s", info->coffinfo->symbol.section->name);
33f5f537
L
1926 else
1927 printf("| |");
977f7911 1928 }
252b5132
RH
1929}
1930
1931static void
2da42df6 1932print_symbol_info_posix (struct extended_symbol_info *info, bfd *abfd)
252b5132 1933{
7e6e972f 1934 print_symname ("%s ", info, NULL, abfd);
977f7911
NC
1935 printf ("%c ", SYM_TYPE (info));
1936
1937 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
252b5132
RH
1938 printf (" ");
1939 else
977f7911
NC
1940 {
1941 print_value (abfd, SYM_VALUE (info));
1942 printf (" ");
1943 if (SYM_SIZE (info))
1944 print_value (abfd, SYM_SIZE (info));
1945 }
252b5132 1946}
1996d0f1
NC
1947
1948static void
1949just_print_symbol_name (struct extended_symbol_info *info, bfd *abfd)
1950{
1951 print_symname ("%s", info, NULL, abfd);
1952}
252b5132 1953\f
382c1116
NC
1954int
1955main (int argc, char **argv)
252b5132 1956{
382c1116
NC
1957 int c;
1958 int retval;
252b5132 1959
87b9f255 1960#ifdef HAVE_LC_MESSAGES
382c1116
NC
1961 setlocale (LC_MESSAGES, "");
1962#endif
382c1116
NC
1963 setlocale (LC_CTYPE, "");
1964 setlocale (LC_COLLATE, "");
382c1116
NC
1965 bindtextdomain (PACKAGE, LOCALEDIR);
1966 textdomain (PACKAGE);
1967
1968 program_name = *argv;
1969 xmalloc_set_program_name (program_name);
86eafac0 1970 bfd_set_error_program_name (program_name);
fc579192 1971#if BFD_SUPPORTS_PLUGINS
3d98c460 1972 bfd_plugin_set_program_name (program_name);
fc579192 1973#endif
382c1116 1974
869b9d07
MM
1975 expandargv (&argc, &argv);
1976
bf2dd8d7
AM
1977 if (bfd_init () != BFD_INIT_MAGIC)
1978 fatal (_("fatal error: libbfd ABI mismatch"));
382c1116
NC
1979 set_default_bfd_target ();
1980
2c3cc81e 1981 while ((c = getopt_long (argc, argv, "aABCDef:gHhjJlnopPrSst:uU:vVvWX:",
382c1116 1982 long_options, (int *) 0)) != EOF)
252b5132 1983 {
382c1116 1984 switch (c)
252b5132 1985 {
382c1116
NC
1986 case 'a':
1987 print_debug_syms = 1;
1988 break;
1989 case 'A':
1990 case 'o':
1991 filename_per_symbol = 1;
1992 break;
1993 case 'B': /* For MIPS compatibility. */
1994 set_output_format ("bsd");
1995 break;
1996 case 'C':
1997 do_demangle = 1;
1998 if (optarg != NULL)
1999 {
2000 enum demangling_styles style;
2001
2002 style = cplus_demangle_name_to_style (optarg);
2003 if (style == unknown_demangling)
2004 fatal (_("unknown demangling style `%s'"),
2005 optarg);
2006
2007 cplus_demangle_set_style (style);
2008 }
2009 break;
af03af8f
NC
2010 case OPTION_RECURSE_LIMIT:
2011 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
2012 break;
2013 case OPTION_NO_RECURSE_LIMIT:
2014 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
2015 break;
7fe1b138
FS
2016 case OPTION_QUIET:
2017 quiet = 1;
2018 break;
382c1116
NC
2019 case 'D':
2020 dynamic = 1;
2021 break;
2022 case 'e':
2023 /* Ignored for HP/UX compatibility. */
2024 break;
2025 case 'f':
2026 set_output_format (optarg);
2027 break;
2028 case 'g':
2029 external_only = 1;
2030 break;
2031 case 'H':
2032 case 'h':
2033 usage (stdout, 0);
2034 case 'l':
2035 line_numbers = 1;
2036 break;
2037 case 'n':
2038 case 'v':
ddb1377c 2039 no_sort = 0;
382c1116 2040 sort_numerically = 1;
ddb1377c 2041 sort_by_size = 0;
382c1116
NC
2042 break;
2043 case 'p':
2044 no_sort = 1;
ddb1377c
AM
2045 sort_numerically = 0;
2046 sort_by_size = 0;
2047 break;
2048 case OPTION_SIZE_SORT:
2049 no_sort = 0;
2050 sort_numerically = 0;
2051 sort_by_size = 1;
382c1116
NC
2052 break;
2053 case 'P':
2054 set_output_format ("posix");
2055 break;
1996d0f1
NC
2056 case 'j':
2057 set_output_format ("just-symbols");
2058 break;
382c1116
NC
2059 case 'r':
2060 reverse_sort = 1;
2061 break;
2062 case 's':
2063 print_armap = 1;
2064 break;
2065 case 'S':
2066 print_size = 1;
2067 break;
2068 case 't':
2069 set_print_radix (optarg);
2070 break;
2071 case 'u':
2072 undefined_only = 1;
aa02037b 2073 defined_only = 0;
382c1116 2074 break;
b3aa80b4 2075 case 'U':
aa02037b
NC
2076 defined_only = 1;
2077 undefined_only = 0;
2078 break;
2079
2080 case OPTION_UNICODE:
b3aa80b4
NC
2081 if (streq (optarg, "default") || streq (optarg, "d"))
2082 unicode_display = unicode_default;
2083 else if (streq (optarg, "locale") || streq (optarg, "l"))
2084 unicode_display = unicode_locale;
2085 else if (streq (optarg, "escape") || streq (optarg, "e"))
2086 unicode_display = unicode_escape;
2087 else if (streq (optarg, "invalid") || streq (optarg, "i"))
2088 unicode_display = unicode_invalid;
2089 else if (streq (optarg, "hex") || streq (optarg, "x"))
2090 unicode_display = unicode_hex;
2091 else if (streq (optarg, "highlight") || streq (optarg, "h"))
2092 unicode_display = unicode_highlight;
2093 else
2094 fatal (_("invalid argument to -U/--unicode: %s"), optarg);
2095 break;
2096
382c1116
NC
2097 case 'V':
2098 show_version = 1;
2099 break;
2c3cc81e
NC
2100 case 'W':
2101 non_weak = 1;
2102 break;
382c1116
NC
2103 case 'X':
2104 /* Ignored for (partial) AIX compatibility. On AIX, the
2105 argument has values 32, 64, or 32_64, and specifies that
2106 only 32-bit, only 64-bit, or both kinds of objects should
2107 be examined. The default is 32. So plain AIX nm on a
2108 library archive with both kinds of objects will ignore
2109 the 64-bit ones. For GNU nm, the default is and always
2110 has been -X 32_64, and other options are not supported. */
2111 if (strcmp (optarg, "32_64") != 0)
2112 fatal (_("Only -X 32_64 is supported"));
2113 break;
2114
2115 case OPTION_TARGET: /* --target */
2116 target = optarg;
2117 break;
2118
ce3c775b
NC
2119 case OPTION_PLUGIN: /* --plugin */
2120#if BFD_SUPPORTS_PLUGINS
2121 bfd_plugin_set_plugin (optarg);
2122#else
2123 fatal (_("sorry - this program has been built without plugin support\n"));
2124#endif
2125 break;
2126
e6f6aa8d
NC
2127 case OPTION_IFUNC_CHARS:
2128 ifunc_type_chars = optarg;
2129 break;
2130
382c1116
NC
2131 case 0: /* A long option that just sets a flag. */
2132 break;
2133
2134 default:
2135 usage (stderr, 1);
252b5132
RH
2136 }
2137 }
252b5132 2138
382c1116
NC
2139 if (show_version)
2140 print_version ("nm");
252b5132 2141
382c1116 2142 if (sort_by_size && undefined_only)
252b5132 2143 {
382c1116
NC
2144 non_fatal (_("Using the --size-sort and --undefined-only options together"));
2145 non_fatal (_("will produce no output, since undefined symbols have no size."));
2146 return 0;
252b5132 2147 }
382c1116
NC
2148
2149 /* OK, all options now parsed. If no filename specified, do a.out. */
2150 if (optind == argc)
2151 return !display_file ("a.out");
2152
2153 retval = 0;
2154
2155 if (argc - optind > 1)
2156 filename_per_file = 1;
2157
2158 /* We were given several filenames to do. */
2159 while (optind < argc)
252b5132 2160 {
382c1116
NC
2161 if (!display_file (argv[optind++]))
2162 retval++;
2163 }
252b5132 2164
382c1116
NC
2165 exit (retval);
2166 return retval;
252b5132 2167}