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