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