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