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