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