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