Change output of `nm --help' to include a description of the purpose of the
[deliverable/binutils-gdb.git] / binutils / nm.c
1 /* nm.c -- Describe symbol table of a rel file.
2 Copyright 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Binutils.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 #include "bfd.h"
23 #include "progress.h"
24 #include "bucomm.h"
25 #include "getopt.h"
26 #include "aout/stab_gnu.h"
27 #include "aout/ranlib.h"
28 #include "demangle.h"
29 #include "libiberty.h"
30
31 /* When sorting by size, we use this structure to hold the size and a
32 pointer to the minisymbol. */
33
34 struct size_sym
35 {
36 const PTR minisym;
37 bfd_vma size;
38 };
39
40 /* When fetching relocs, we use this structure to pass information to
41 get_relocs. */
42
43 struct get_relocs_info
44 {
45 asection **secs;
46 arelent ***relocs;
47 long *relcount;
48 asymbol **syms;
49 };
50
51 static void
52 usage PARAMS ((FILE *, int));
53
54 static void
55 set_print_radix PARAMS ((char *));
56
57 static void
58 set_output_format PARAMS ((char *));
59
60 static void
61 display_archive PARAMS ((bfd *));
62
63 static boolean
64 display_file PARAMS ((char *filename));
65
66 static void
67 display_rel_file PARAMS ((bfd * file, bfd * archive));
68
69 static long
70 filter_symbols PARAMS ((bfd *, boolean, PTR, long, unsigned int));
71
72 static long
73 sort_symbols_by_size PARAMS ((bfd *, boolean, PTR, long, unsigned int,
74 struct size_sym **));
75
76 static void
77 print_symbols PARAMS ((bfd *, boolean, PTR, long, unsigned int, bfd *));
78
79 static void
80 print_size_symbols PARAMS ((bfd *, boolean, struct size_sym *, long, bfd *));
81
82 static void
83 print_symname PARAMS ((const char *, const char *, bfd *));
84
85 static void
86 print_symbol PARAMS ((bfd *, asymbol *, bfd *));
87
88 static void
89 print_symdef_entry PARAMS ((bfd * abfd));
90
91 /* The sorting functions. */
92
93 static int
94 numeric_forward PARAMS ((const PTR, const PTR));
95
96 static int
97 numeric_reverse PARAMS ((const PTR, const PTR));
98
99 static int
100 non_numeric_forward PARAMS ((const PTR, const PTR));
101
102 static int
103 non_numeric_reverse PARAMS ((const PTR, const PTR));
104
105 static int
106 size_forward1 PARAMS ((const PTR, const PTR));
107
108 static int
109 size_forward2 PARAMS ((const PTR, const PTR));
110
111 /* The output formatting functions. */
112
113 static void
114 print_object_filename_bsd PARAMS ((char *filename));
115
116 static void
117 print_object_filename_sysv PARAMS ((char *filename));
118
119 static void
120 print_object_filename_posix PARAMS ((char *filename));
121
122
123 static void
124 print_archive_filename_bsd PARAMS ((char *filename));
125
126 static void
127 print_archive_filename_sysv PARAMS ((char *filename));
128
129 static void
130 print_archive_filename_posix PARAMS ((char *filename));
131
132
133 static void
134 print_archive_member_bsd PARAMS ((char *archive, CONST char *filename));
135
136 static void
137 print_archive_member_sysv PARAMS ((char *archive, CONST char *filename));
138
139 static void
140 print_archive_member_posix PARAMS ((char *archive, CONST char *filename));
141
142
143 static void
144 print_symbol_filename_bsd PARAMS ((bfd * archive_bfd, bfd * abfd));
145
146 static void
147 print_symbol_filename_sysv PARAMS ((bfd * archive_bfd, bfd * abfd));
148
149 static void
150 print_symbol_filename_posix PARAMS ((bfd * archive_bfd, bfd * abfd));
151
152
153 static void
154 print_value PARAMS ((bfd_vma));
155
156 static void
157 print_symbol_info_bsd PARAMS ((symbol_info * info, bfd * abfd));
158
159 static void
160 print_symbol_info_sysv PARAMS ((symbol_info * info, bfd * abfd));
161
162 static void
163 print_symbol_info_posix PARAMS ((symbol_info * info, bfd * abfd));
164
165 static void
166 get_relocs PARAMS ((bfd *, asection *, PTR));
167
168 /* Support for different output formats. */
169 struct output_fns
170 {
171 /* Print the name of an object file given on the command line. */
172 void (*print_object_filename) PARAMS ((char *filename));
173
174 /* Print the name of an archive file given on the command line. */
175 void (*print_archive_filename) PARAMS ((char *filename));
176
177 /* Print the name of an archive member file. */
178 void (*print_archive_member) PARAMS ((char *archive, CONST char *filename));
179
180 /* Print the name of the file (and archive, if there is one)
181 containing a symbol. */
182 void (*print_symbol_filename) PARAMS ((bfd * archive_bfd, bfd * abfd));
183
184 /* Print a line of information about a symbol. */
185 void (*print_symbol_info) PARAMS ((symbol_info * info, bfd * abfd));
186 };
187 static struct output_fns formats[] =
188 {
189 {print_object_filename_bsd,
190 print_archive_filename_bsd,
191 print_archive_member_bsd,
192 print_symbol_filename_bsd,
193 print_symbol_info_bsd},
194 {print_object_filename_sysv,
195 print_archive_filename_sysv,
196 print_archive_member_sysv,
197 print_symbol_filename_sysv,
198 print_symbol_info_sysv},
199 {print_object_filename_posix,
200 print_archive_filename_posix,
201 print_archive_member_posix,
202 print_symbol_filename_posix,
203 print_symbol_info_posix}
204 };
205
206 /* Indices in `formats'. */
207 #define FORMAT_BSD 0
208 #define FORMAT_SYSV 1
209 #define FORMAT_POSIX 2
210 #define FORMAT_DEFAULT FORMAT_BSD
211
212 /* The output format to use. */
213 static struct output_fns *format = &formats[FORMAT_DEFAULT];
214
215
216 /* Command options. */
217
218 static int do_demangle = 0; /* Pretty print C++ symbol names. */
219 static int external_only = 0; /* print external symbols only */
220 static int defined_only = 0; /* Print defined symbols only */
221 static int no_sort = 0; /* don't sort; print syms in order found */
222 static int print_debug_syms = 0; /* print debugger-only symbols too */
223 static int print_armap = 0; /* describe __.SYMDEF data in archive files. */
224 static int reverse_sort = 0; /* sort in downward(alpha or numeric) order */
225 static int sort_numerically = 0; /* sort in numeric rather than alpha order */
226 static int sort_by_size = 0; /* sort by size of symbol */
227 static int undefined_only = 0; /* print undefined symbols only */
228 static int dynamic = 0; /* print dynamic symbols. */
229 static int show_version = 0; /* show the version number */
230 static int show_stats = 0; /* show statistics */
231 static int line_numbers = 0; /* print line numbers for symbols */
232
233 /* When to print the names of files. Not mutually exclusive in SYSV format. */
234 static int filename_per_file = 0; /* Once per file, on its own line. */
235 static int filename_per_symbol = 0; /* Once per symbol, at start of line. */
236
237 /* Print formats for printing a symbol value. */
238 #ifndef BFD64
239 static char value_format[] = "%08lx";
240 #else
241 #if BFD_HOST_64BIT_LONG
242 static char value_format[] = "%016lx";
243 #else
244 /* We don't use value_format for this case. */
245 #endif
246 #endif
247 static int print_radix = 16;
248 /* Print formats for printing stab info. */
249 static char other_format[] = "%02x";
250 static char desc_format[] = "%04x";
251
252 static char *target = NULL;
253
254 /* Used to cache the line numbers for a BFD. */
255 static bfd *lineno_cache_bfd;
256 static bfd *lineno_cache_rel_bfd;
257
258 #define OPTION_TARGET 200
259
260 static struct option long_options[] =
261 {
262 {"debug-syms", no_argument, &print_debug_syms, 1},
263 {"demangle", no_argument, &do_demangle, 1},
264 {"dynamic", no_argument, &dynamic, 1},
265 {"extern-only", no_argument, &external_only, 1},
266 {"format", required_argument, 0, 'f'},
267 {"help", no_argument, 0, 'h'},
268 {"line-numbers", no_argument, 0, 'l'},
269 {"no-cplus", no_argument, &do_demangle, 0}, /* Linux compatibility. */
270 {"no-demangle", no_argument, &do_demangle, 0},
271 {"no-sort", no_argument, &no_sort, 1},
272 {"numeric-sort", no_argument, &sort_numerically, 1},
273 {"portability", no_argument, 0, 'P'},
274 {"print-armap", no_argument, &print_armap, 1},
275 {"print-file-name", no_argument, 0, 'o'},
276 {"radix", required_argument, 0, 't'},
277 {"reverse-sort", no_argument, &reverse_sort, 1},
278 {"size-sort", no_argument, &sort_by_size, 1},
279 {"stats", no_argument, &show_stats, 1},
280 {"target", required_argument, 0, OPTION_TARGET},
281 {"defined-only", no_argument, &defined_only, 1},
282 {"undefined-only", no_argument, &undefined_only, 1},
283 {"version", no_argument, &show_version, 1},
284 {0, no_argument, 0, 0}
285 };
286 \f
287 /* Some error-reporting functions */
288
289 static void
290 usage (stream, status)
291 FILE *stream;
292 int status;
293 {
294 fprintf (stream, _("Usage: %s [OPTION]... [FILE]...\n"), program_name);
295 fprintf (stream, _("List symbols from FILEs (a.out by default).\n"));
296 fprintf (stream, _("\n\
297 -a, --debug-syms Display debugger-only symbols\n\
298 -A, --print-file-name Print name of the input file before every symbol\n\
299 -B Same as --format=bsd\n\
300 -C, --demangle Decode low-level symbol names into user-level names\n\
301 --no-demangle Do not demangle low-level symbol names\n\
302 -D, --dynamic Display dynamic symbols instead of normal symbols\n\
303 --defined-only Display only defined symbols\n\
304 -e (ignored)\n\
305 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
306 `sysv' or `posix'. The default is `bsd'\n\
307 -g, --extern-only Display only external symbols\n\
308 -h, --help Display this information\n\
309 -l, --line-numbers Use debugging information to find a filename and\n\
310 line number for each symbol\n\
311 -n, --numeric-sort Sort symbols numerically by address\n\
312 -o Same as -A\n\
313 -p, --no-sort Do not sort the symbols\n\
314 -P, --portability Same as --format=posix\n\
315 -r, --reverse-sort Reverse the sense of the sort\n\
316 -s, --print-armap Include index for symbols from archive members\n\
317 --size-sort Sort symbols by size\n\
318 -t, --radix=RADIX Use RADIX for printing symbol values\n\
319 --target=BFDNAME Specify the target object format as BFDNAME\n\
320 -u, --undefined-only Display only undefined symbols\n\
321 -V, --version Display this program's version number\n\
322 \n"));
323 list_supported_targets (program_name, stream);
324 if (status == 0)
325 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
326 exit (status);
327 }
328
329 /* Set the radix for the symbol value and size according to RADIX. */
330
331 static void
332 set_print_radix (radix)
333 char *radix;
334 {
335 switch (*radix)
336 {
337 case 'x':
338 break;
339 case 'd':
340 case 'o':
341 if (*radix == 'd')
342 print_radix = 10;
343 else
344 print_radix = 8;
345 #ifndef BFD64
346 value_format[4] = *radix;
347 #else
348 #if BFD_HOST_64BIT_LONG
349 value_format[5] = *radix;
350 #else
351 /* This case requires special handling for octal and decimal
352 printing. */
353 #endif
354 #endif
355 other_format[3] = desc_format[3] = *radix;
356 break;
357 default:
358 fatal (_("%s: invalid radix"), radix);
359 }
360 }
361
362 static void
363 set_output_format (f)
364 char *f;
365 {
366 int i;
367
368 switch (*f)
369 {
370 case 'b':
371 case 'B':
372 i = FORMAT_BSD;
373 break;
374 case 'p':
375 case 'P':
376 i = FORMAT_POSIX;
377 break;
378 case 's':
379 case 'S':
380 i = FORMAT_SYSV;
381 break;
382 default:
383 fatal (_("%s: invalid output format"), f);
384 }
385 format = &formats[i];
386 }
387 \f
388 int
389 main (argc, argv)
390 int argc;
391 char **argv;
392 {
393 int c;
394 int retval;
395
396 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
397 setlocale (LC_MESSAGES, "");
398 #endif
399 bindtextdomain (PACKAGE, LOCALEDIR);
400 textdomain (PACKAGE);
401
402 program_name = *argv;
403 xmalloc_set_program_name (program_name);
404
405 START_PROGRESS (program_name, 0);
406
407 bfd_init ();
408 set_default_bfd_target ();
409
410 while ((c = getopt_long (argc, argv, "aABCDef:glnopPrst:uvV", long_options, (int *) 0)) != EOF)
411 {
412 switch (c)
413 {
414 case 'a':
415 print_debug_syms = 1;
416 break;
417 case 'A':
418 case 'o':
419 filename_per_symbol = 1;
420 break;
421 case 'B': /* For MIPS compatibility. */
422 set_output_format ("bsd");
423 break;
424 case 'C':
425 do_demangle = 1;
426 break;
427 case 'D':
428 dynamic = 1;
429 break;
430 case 'e':
431 /* Ignored for HP/UX compatibility. */
432 break;
433 case 'f':
434 set_output_format (optarg);
435 break;
436 case 'g':
437 external_only = 1;
438 break;
439 case 'h':
440 usage (stdout, 0);
441 case 'l':
442 line_numbers = 1;
443 break;
444 case 'n':
445 case 'v':
446 sort_numerically = 1;
447 break;
448 case 'p':
449 no_sort = 1;
450 break;
451 case 'P':
452 set_output_format ("posix");
453 break;
454 case 'r':
455 reverse_sort = 1;
456 break;
457 case 's':
458 print_armap = 1;
459 break;
460 case 't':
461 set_print_radix (optarg);
462 break;
463 case 'u':
464 undefined_only = 1;
465 break;
466 case 'V':
467 show_version = 1;
468 break;
469
470 case OPTION_TARGET: /* --target */
471 target = optarg;
472 break;
473
474 case 0: /* A long option that just sets a flag. */
475 break;
476
477 default:
478 usage (stderr, 1);
479 }
480 }
481
482 if (show_version)
483 print_version ("nm");
484
485 /* OK, all options now parsed. If no filename specified, do a.out. */
486 if (optind == argc)
487 return !display_file ("a.out");
488
489 retval = 0;
490
491 if (argc - optind > 1)
492 filename_per_file = 1;
493
494 /* We were given several filenames to do. */
495 while (optind < argc)
496 {
497 PROGRESS (1);
498 if (!display_file (argv[optind++]))
499 retval++;
500 }
501
502 END_PROGRESS (program_name);
503
504 #ifdef HAVE_SBRK
505 if (show_stats)
506 {
507 char *lim = (char *) sbrk (0);
508
509 non_fatal (_("data size %ld"), (long) (lim - (char *) &environ));
510 }
511 #endif
512
513 exit (retval);
514 return retval;
515 }
516 \f
517 static void
518 display_archive (file)
519 bfd *file;
520 {
521 bfd *arfile = NULL;
522 bfd *last_arfile = NULL;
523 char **matching;
524
525 (*format->print_archive_filename) (bfd_get_filename (file));
526
527 if (print_armap)
528 print_symdef_entry (file);
529
530 for (;;)
531 {
532 PROGRESS (1);
533
534 arfile = bfd_openr_next_archived_file (file, arfile);
535
536 if (arfile == NULL)
537 {
538 if (bfd_get_error () != bfd_error_no_more_archived_files)
539 bfd_fatal (bfd_get_filename (file));
540 break;
541 }
542
543 if (bfd_check_format_matches (arfile, bfd_object, &matching))
544 {
545 (*format->print_archive_member) (bfd_get_filename (file),
546 bfd_get_filename (arfile));
547 display_rel_file (arfile, file);
548 }
549 else
550 {
551 bfd_nonfatal (bfd_get_filename (arfile));
552 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
553 {
554 list_matching_formats (matching);
555 free (matching);
556 }
557 }
558
559 if (last_arfile != NULL)
560 {
561 bfd_close (last_arfile);
562 lineno_cache_bfd = NULL;
563 lineno_cache_rel_bfd = NULL;
564 }
565 last_arfile = arfile;
566 }
567
568 if (last_arfile != NULL)
569 {
570 bfd_close (last_arfile);
571 lineno_cache_bfd = NULL;
572 lineno_cache_rel_bfd = NULL;
573 }
574 }
575
576 static boolean
577 display_file (filename)
578 char *filename;
579 {
580 boolean retval = true;
581 bfd *file;
582 char **matching;
583
584 file = bfd_openr (filename, target);
585 if (file == NULL)
586 {
587 bfd_nonfatal (filename);
588 return false;
589 }
590
591 if (bfd_check_format (file, bfd_archive))
592 {
593 display_archive (file);
594 }
595 else if (bfd_check_format_matches (file, bfd_object, &matching))
596 {
597 (*format->print_object_filename) (filename);
598 display_rel_file (file, NULL);
599 }
600 else
601 {
602 bfd_nonfatal (filename);
603 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
604 {
605 list_matching_formats (matching);
606 free (matching);
607 }
608 retval = false;
609 }
610
611 if (bfd_close (file) == false)
612 bfd_fatal (filename);
613
614 lineno_cache_bfd = NULL;
615 lineno_cache_rel_bfd = NULL;
616
617 return retval;
618 }
619 \f
620 /* These globals are used to pass information into the sorting
621 routines. */
622 static bfd *sort_bfd;
623 static boolean sort_dynamic;
624 static asymbol *sort_x;
625 static asymbol *sort_y;
626
627 /* Symbol-sorting predicates */
628 #define valueof(x) ((x)->section->vma + (x)->value)
629
630 /* Numeric sorts. Undefined symbols are always considered "less than"
631 defined symbols with zero values. Common symbols are not treated
632 specially -- i.e., their sizes are used as their "values". */
633
634 static int
635 numeric_forward (P_x, P_y)
636 const PTR P_x;
637 const PTR P_y;
638 {
639 asymbol *x, *y;
640 asection *xs, *ys;
641
642 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
643 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
644 if (x == NULL || y == NULL)
645 bfd_fatal (bfd_get_filename (sort_bfd));
646
647 xs = bfd_get_section (x);
648 ys = bfd_get_section (y);
649
650 if (bfd_is_und_section (xs))
651 {
652 if (! bfd_is_und_section (ys))
653 return -1;
654 }
655 else if (bfd_is_und_section (ys))
656 return 1;
657 else if (valueof (x) != valueof (y))
658 return valueof (x) < valueof (y) ? -1 : 1;
659
660 return non_numeric_forward (P_x, P_y);
661 }
662
663 static int
664 numeric_reverse (x, y)
665 const PTR x;
666 const PTR y;
667 {
668 return - numeric_forward (x, y);
669 }
670
671 static int
672 non_numeric_forward (P_x, P_y)
673 const PTR P_x;
674 const PTR P_y;
675 {
676 asymbol *x, *y;
677 const char *xn, *yn;
678
679 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
680 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
681 if (x == NULL || y == NULL)
682 bfd_fatal (bfd_get_filename (sort_bfd));
683
684 xn = bfd_asymbol_name (x);
685 yn = bfd_asymbol_name (y);
686
687 return ((xn == NULL) ? ((yn == NULL) ? 0 : -1) :
688 ((yn == NULL) ? 1 : strcmp (xn, yn)));
689 }
690
691 static int
692 non_numeric_reverse (x, y)
693 const PTR x;
694 const PTR y;
695 {
696 return - non_numeric_forward (x, y);
697 }
698
699 static int (*(sorters[2][2])) PARAMS ((const PTR, const PTR)) =
700 {
701 { non_numeric_forward, non_numeric_reverse },
702 { numeric_forward, numeric_reverse }
703 };
704
705 /* This sort routine is used by sort_symbols_by_size. It is similar
706 to numeric_forward, but when symbols have the same value it sorts
707 by section VMA. This simplifies the sort_symbols_by_size code
708 which handles symbols at the end of sections. Also, this routine
709 tries to sort file names before other symbols with the same value.
710 That will make the file name have a zero size, which will make
711 sort_symbols_by_size choose the non file name symbol, leading to
712 more meaningful output. For similar reasons, this code sorts
713 gnu_compiled_* and gcc2_compiled before other symbols with the same
714 value. */
715
716 static int
717 size_forward1 (P_x, P_y)
718 const PTR P_x;
719 const PTR P_y;
720 {
721 asymbol *x, *y;
722 asection *xs, *ys;
723 const char *xn, *yn;
724 size_t xnl, ynl;
725 int xf, yf;
726
727 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
728 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
729 if (x == NULL || y == NULL)
730 bfd_fatal (bfd_get_filename (sort_bfd));
731
732 xs = bfd_get_section (x);
733 ys = bfd_get_section (y);
734
735 if (bfd_is_und_section (xs))
736 abort ();
737 if (bfd_is_und_section (ys))
738 abort ();
739
740 if (valueof (x) != valueof (y))
741 return valueof (x) < valueof (y) ? -1 : 1;
742
743 if (xs->vma != ys->vma)
744 return xs->vma < ys->vma ? -1 : 1;
745
746 xn = bfd_asymbol_name (x);
747 yn = bfd_asymbol_name (y);
748 xnl = strlen (xn);
749 ynl = strlen (yn);
750
751 /* The symbols gnu_compiled and gcc2_compiled convey even less
752 information than the file name, so sort them out first. */
753
754 xf = (strstr (xn, "gnu_compiled") != NULL
755 || strstr (xn, "gcc2_compiled") != NULL);
756 yf = (strstr (yn, "gnu_compiled") != NULL
757 || strstr (yn, "gcc2_compiled") != NULL);
758
759 if (xf && ! yf)
760 return -1;
761 if (! xf && yf)
762 return 1;
763
764 /* We use a heuristic for the file name. It may not work on non
765 Unix systems, but it doesn't really matter; the only difference
766 is precisely which symbol names get printed. */
767
768 #define file_symbol(s, sn, snl) \
769 (((s)->flags & BSF_FILE) != 0 \
770 || ((sn)[(snl) - 2] == '.' \
771 && ((sn)[(snl) - 1] == 'o' \
772 || (sn)[(snl) - 1] == 'a')))
773
774 xf = file_symbol (x, xn, xnl);
775 yf = file_symbol (y, yn, ynl);
776
777 if (xf && ! yf)
778 return -1;
779 if (! xf && yf)
780 return 1;
781
782 return non_numeric_forward (P_x, P_y);
783 }
784
785 /* This sort routine is used by sort_symbols_by_size. It is sorting
786 an array of size_sym structures into size order. */
787
788 static int
789 size_forward2 (P_x, P_y)
790 const PTR P_x;
791 const PTR P_y;
792 {
793 const struct size_sym *x = (const struct size_sym *) P_x;
794 const struct size_sym *y = (const struct size_sym *) P_y;
795
796 if (x->size < y->size)
797 return reverse_sort ? 1 : -1;
798 else if (x->size > y->size)
799 return reverse_sort ? -1 : 1;
800 else
801 return sorters[0][reverse_sort] (x->minisym, y->minisym);
802 }
803
804 /* Sort the symbols by size. We guess the size by assuming that the
805 difference between the address of a symbol and the address of the
806 next higher symbol is the size. FIXME: ELF actually stores a size
807 with each symbol. We should use it. */
808
809 static long
810 sort_symbols_by_size (abfd, dynamic, minisyms, symcount, size, symsizesp)
811 bfd *abfd;
812 boolean dynamic;
813 PTR minisyms;
814 long symcount;
815 unsigned int size;
816 struct size_sym **symsizesp;
817 {
818 struct size_sym *symsizes;
819 bfd_byte *from, *fromend;
820 asymbol *sym = NULL;
821 asymbol *store_sym, *store_next;
822
823 qsort (minisyms, symcount, size, size_forward1);
824
825 /* We are going to return a special set of symbols and sizes to
826 print. */
827 symsizes = (struct size_sym *) xmalloc (symcount * sizeof (struct size_sym));
828 *symsizesp = symsizes;
829
830 /* Note that filter_symbols has already removed all absolute and
831 undefined symbols. Here we remove all symbols whose size winds
832 up as zero. */
833
834 from = (bfd_byte *) minisyms;
835 fromend = from + symcount * size;
836
837 store_sym = sort_x;
838 store_next = sort_y;
839
840 if (from < fromend)
841 {
842 sym = bfd_minisymbol_to_symbol (abfd, dynamic, (const PTR) from,
843 store_sym);
844 if (sym == NULL)
845 bfd_fatal (bfd_get_filename (abfd));
846 }
847
848 for (; from < fromend; from += size)
849 {
850 asymbol *next;
851 asection *sec;
852 bfd_vma sz;
853 asymbol *temp;
854
855 if (from + size < fromend)
856 {
857 next = bfd_minisymbol_to_symbol (abfd,
858 dynamic,
859 (const PTR) (from + size),
860 store_next);
861 if (next == NULL)
862 bfd_fatal (bfd_get_filename (abfd));
863 }
864 else
865 next = NULL;
866
867 sec = bfd_get_section (sym);
868
869 if (bfd_is_com_section (sec))
870 sz = sym->value;
871 else
872 {
873 if (from + size < fromend
874 && sec == bfd_get_section (next))
875 sz = valueof (next) - valueof (sym);
876 else
877 sz = (bfd_get_section_vma (abfd, sec)
878 + bfd_section_size (abfd, sec)
879 - valueof (sym));
880 }
881
882 if (sz != 0)
883 {
884 symsizes->minisym = (const PTR) from;
885 symsizes->size = sz;
886 ++symsizes;
887 }
888
889 sym = next;
890
891 temp = store_sym;
892 store_sym = store_next;
893 store_next = temp;
894 }
895
896 symcount = symsizes - *symsizesp;
897
898 /* We must now sort again by size. */
899 qsort ((PTR) *symsizesp, symcount, sizeof (struct size_sym), size_forward2);
900
901 return symcount;
902 }
903 \f
904 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
905
906 static void
907 display_rel_file (abfd, archive_bfd)
908 bfd *abfd;
909 bfd *archive_bfd;
910 {
911 long symcount;
912 PTR minisyms;
913 unsigned int size;
914 struct size_sym *symsizes;
915
916 if (! dynamic)
917 {
918 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
919 {
920 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
921 return;
922 }
923 }
924
925 symcount = bfd_read_minisymbols (abfd, dynamic, &minisyms, &size);
926 if (symcount < 0)
927 bfd_fatal (bfd_get_filename (abfd));
928
929 if (symcount == 0)
930 {
931 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
932 return;
933 }
934
935 /* Discard the symbols we don't want to print.
936 It's OK to do this in place; we'll free the storage anyway
937 (after printing). */
938
939 symcount = filter_symbols (abfd, dynamic, minisyms, symcount, size);
940
941 symsizes = NULL;
942 if (! no_sort)
943 {
944 sort_bfd = abfd;
945 sort_dynamic = dynamic;
946 sort_x = bfd_make_empty_symbol (abfd);
947 sort_y = bfd_make_empty_symbol (abfd);
948 if (sort_x == NULL || sort_y == NULL)
949 bfd_fatal (bfd_get_filename (abfd));
950
951 if (! sort_by_size)
952 qsort (minisyms, symcount, size,
953 sorters[sort_numerically][reverse_sort]);
954 else
955 symcount = sort_symbols_by_size (abfd, dynamic, minisyms, symcount,
956 size, &symsizes);
957 }
958
959 if (! sort_by_size)
960 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd);
961 else
962 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd);
963
964 free (minisyms);
965 }
966 \f
967 /* Choose which symbol entries to print;
968 compact them downward to get rid of the rest.
969 Return the number of symbols to be printed. */
970
971 static long
972 filter_symbols (abfd, dynamic, minisyms, symcount, size)
973 bfd *abfd;
974 boolean dynamic;
975 PTR minisyms;
976 long symcount;
977 unsigned int size;
978 {
979 bfd_byte *from, *fromend, *to;
980 asymbol *store;
981
982 store = bfd_make_empty_symbol (abfd);
983 if (store == NULL)
984 bfd_fatal (bfd_get_filename (abfd));
985
986 from = (bfd_byte *) minisyms;
987 fromend = from + symcount * size;
988 to = (bfd_byte *) minisyms;
989
990 for (; from < fromend; from += size)
991 {
992 int keep = 0;
993 asymbol *sym;
994
995 PROGRESS (1);
996
997 sym = bfd_minisymbol_to_symbol (abfd, dynamic, (const PTR) from, store);
998 if (sym == NULL)
999 bfd_fatal (bfd_get_filename (abfd));
1000
1001 if (undefined_only)
1002 keep = bfd_is_und_section (sym->section);
1003 else if (external_only)
1004 keep = ((sym->flags & BSF_GLOBAL) != 0
1005 || (sym->flags & BSF_WEAK) != 0
1006 || bfd_is_und_section (sym->section)
1007 || bfd_is_com_section (sym->section));
1008 else
1009 keep = 1;
1010
1011 if (keep
1012 && ! print_debug_syms
1013 && (sym->flags & BSF_DEBUGGING) != 0)
1014 keep = 0;
1015
1016 if (keep
1017 && sort_by_size
1018 && (bfd_is_abs_section (sym->section)
1019 || bfd_is_und_section (sym->section)))
1020 keep = 0;
1021
1022 if (keep
1023 && defined_only)
1024 {
1025 if (bfd_is_und_section (sym->section))
1026 keep = 0;
1027 }
1028
1029 if (keep)
1030 {
1031 memcpy (to, from, size);
1032 to += size;
1033 }
1034 }
1035
1036 return (to - (bfd_byte *) minisyms) / size;
1037 }
1038 \f
1039 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
1040 demangling it if requested. */
1041
1042 static void
1043 print_symname (format, name, abfd)
1044 const char *format;
1045 const char *name;
1046 bfd *abfd;
1047 {
1048 if (do_demangle && *name)
1049 {
1050 char *res;
1051
1052 /* In this mode, give a user-level view of the symbol name
1053 even if it's not mangled; strip off any leading
1054 underscore. */
1055 if (bfd_get_symbol_leading_char (abfd) == name[0])
1056 name++;
1057
1058 res = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
1059 if (res)
1060 {
1061 printf (format, res);
1062 free (res);
1063 return;
1064 }
1065 }
1066
1067 printf (format, name);
1068 }
1069
1070 /* Print the symbols. If ARCHIVE_BFD is non-NULL, it is the archive
1071 containing ABFD. */
1072
1073 static void
1074 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd)
1075 bfd *abfd;
1076 boolean dynamic;
1077 PTR minisyms;
1078 long symcount;
1079 unsigned int size;
1080 bfd *archive_bfd;
1081 {
1082 asymbol *store;
1083 bfd_byte *from, *fromend;
1084
1085 store = bfd_make_empty_symbol (abfd);
1086 if (store == NULL)
1087 bfd_fatal (bfd_get_filename (abfd));
1088
1089 from = (bfd_byte *) minisyms;
1090 fromend = from + symcount * size;
1091 for (; from < fromend; from += size)
1092 {
1093 asymbol *sym;
1094
1095 sym = bfd_minisymbol_to_symbol (abfd, dynamic, from, store);
1096 if (sym == NULL)
1097 bfd_fatal (bfd_get_filename (abfd));
1098
1099 print_symbol (abfd, sym, archive_bfd);
1100 }
1101 }
1102
1103 /* Print the symbols when sorting by size. */
1104
1105 static void
1106 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd)
1107 bfd *abfd;
1108 boolean dynamic;
1109 struct size_sym *symsizes;
1110 long symcount;
1111 bfd *archive_bfd;
1112 {
1113 asymbol *store;
1114 struct size_sym *from, *fromend;
1115
1116 store = bfd_make_empty_symbol (abfd);
1117 if (store == NULL)
1118 bfd_fatal (bfd_get_filename (abfd));
1119
1120 from = symsizes;
1121 fromend = from + symcount;
1122 for (; from < fromend; from++)
1123 {
1124 asymbol *sym;
1125
1126 sym = bfd_minisymbol_to_symbol (abfd, dynamic, from->minisym, store);
1127 if (sym == NULL)
1128 bfd_fatal (bfd_get_filename (abfd));
1129
1130 /* Set the symbol value so that we actually display the symbol
1131 size. */
1132 sym->value = from->size - bfd_section_vma (abfd, bfd_get_section (sym));
1133
1134 print_symbol (abfd, sym, archive_bfd);
1135 }
1136 }
1137
1138 /* Print a single symbol. */
1139
1140 static void
1141 print_symbol (abfd, sym, archive_bfd)
1142 bfd *abfd;
1143 asymbol *sym;
1144 bfd *archive_bfd;
1145 {
1146 PROGRESS (1);
1147
1148 (*format->print_symbol_filename) (archive_bfd, abfd);
1149
1150 if (undefined_only)
1151 {
1152 if (bfd_is_und_section (bfd_get_section (sym)))
1153 print_symname ("%s", bfd_asymbol_name (sym), abfd);
1154 }
1155 else
1156 {
1157 symbol_info syminfo;
1158
1159 bfd_get_symbol_info (abfd, sym, &syminfo);
1160 (*format->print_symbol_info) (&syminfo, abfd);
1161 }
1162
1163 if (line_numbers)
1164 {
1165 static asymbol **syms;
1166 static long symcount;
1167 const char *filename, *functionname;
1168 unsigned int lineno;
1169
1170 /* We need to get the canonical symbols in order to call
1171 bfd_find_nearest_line. This is inefficient, but, then, you
1172 don't have to use --line-numbers. */
1173 if (abfd != lineno_cache_bfd && syms != NULL)
1174 {
1175 free (syms);
1176 syms = NULL;
1177 }
1178 if (syms == NULL)
1179 {
1180 long symsize;
1181
1182 symsize = bfd_get_symtab_upper_bound (abfd);
1183 if (symsize < 0)
1184 bfd_fatal (bfd_get_filename (abfd));
1185 syms = (asymbol **) xmalloc (symsize);
1186 symcount = bfd_canonicalize_symtab (abfd, syms);
1187 if (symcount < 0)
1188 bfd_fatal (bfd_get_filename (abfd));
1189 lineno_cache_bfd = abfd;
1190 }
1191
1192 if (bfd_is_und_section (bfd_get_section (sym)))
1193 {
1194 static asection **secs;
1195 static arelent ***relocs;
1196 static long *relcount;
1197 static unsigned int seccount;
1198 unsigned int i;
1199 const char *symname;
1200
1201 /* For an undefined symbol, we try to find a reloc for the
1202 symbol, and print the line number of the reloc. */
1203
1204 if (abfd != lineno_cache_rel_bfd && relocs != NULL)
1205 {
1206 for (i = 0; i < seccount; i++)
1207 if (relocs[i] != NULL)
1208 free (relocs[i]);
1209 free (secs);
1210 free (relocs);
1211 free (relcount);
1212 secs = NULL;
1213 relocs = NULL;
1214 relcount = NULL;
1215 }
1216
1217 if (relocs == NULL)
1218 {
1219 struct get_relocs_info info;
1220
1221 seccount = bfd_count_sections (abfd);
1222
1223 secs = (asection **) xmalloc (seccount * sizeof *secs);
1224 relocs = (arelent ***) xmalloc (seccount * sizeof *relocs);
1225 relcount = (long *) xmalloc (seccount * sizeof *relcount);
1226
1227 info.secs = secs;
1228 info.relocs = relocs;
1229 info.relcount = relcount;
1230 info.syms = syms;
1231 bfd_map_over_sections (abfd, get_relocs, (PTR) &info);
1232 lineno_cache_rel_bfd = abfd;
1233 }
1234
1235 symname = bfd_asymbol_name (sym);
1236 for (i = 0; i < seccount; i++)
1237 {
1238 long j;
1239
1240 for (j = 0; j < relcount[i]; j++)
1241 {
1242 arelent *r;
1243
1244 r = relocs[i][j];
1245 if (r->sym_ptr_ptr != NULL
1246 && (*r->sym_ptr_ptr)->section == sym->section
1247 && (*r->sym_ptr_ptr)->value == sym->value
1248 && strcmp (symname,
1249 bfd_asymbol_name (*r->sym_ptr_ptr)) == 0
1250 && bfd_find_nearest_line (abfd, secs[i], syms,
1251 r->address, &filename,
1252 &functionname, &lineno))
1253 {
1254 /* We only print the first one we find. */
1255 printf ("\t%s:%u", filename, lineno);
1256 i = seccount;
1257 break;
1258 }
1259 }
1260 }
1261 }
1262 else if (bfd_get_section (sym)->owner == abfd)
1263 {
1264 if (bfd_find_nearest_line (abfd, bfd_get_section (sym), syms,
1265 sym->value, &filename, &functionname,
1266 &lineno)
1267 && filename != NULL
1268 && lineno != 0)
1269 {
1270 printf ("\t%s:%u", filename, lineno);
1271 }
1272 }
1273 }
1274
1275 putchar ('\n');
1276 }
1277 \f
1278 /* The following 3 groups of functions are called unconditionally,
1279 once at the start of processing each file of the appropriate type.
1280 They should check `filename_per_file' and `filename_per_symbol',
1281 as appropriate for their output format, to determine whether to
1282 print anything. */
1283 \f
1284 /* Print the name of an object file given on the command line. */
1285
1286 static void
1287 print_object_filename_bsd (filename)
1288 char *filename;
1289 {
1290 if (filename_per_file && !filename_per_symbol)
1291 printf ("\n%s:\n", filename);
1292 }
1293
1294 static void
1295 print_object_filename_sysv (filename)
1296 char *filename;
1297 {
1298 if (undefined_only)
1299 printf (_("\n\nUndefined symbols from %s:\n\n"), filename);
1300 else
1301 printf (_("\n\nSymbols from %s:\n\n"), filename);
1302 printf (_("\
1303 Name Value Class Type Size Line Section\n\n"));
1304 }
1305
1306 static void
1307 print_object_filename_posix (filename)
1308 char *filename;
1309 {
1310 if (filename_per_file && !filename_per_symbol)
1311 printf ("%s:\n", filename);
1312 }
1313 \f
1314 /* Print the name of an archive file given on the command line. */
1315
1316 static void
1317 print_archive_filename_bsd (filename)
1318 char *filename;
1319 {
1320 if (filename_per_file)
1321 printf ("\n%s:\n", filename);
1322 }
1323
1324 static void
1325 print_archive_filename_sysv (filename)
1326 char *filename ATTRIBUTE_UNUSED;
1327 {
1328 }
1329
1330 static void
1331 print_archive_filename_posix (filename)
1332 char *filename ATTRIBUTE_UNUSED;
1333 {
1334 }
1335 \f
1336 /* Print the name of an archive member file. */
1337
1338 static void
1339 print_archive_member_bsd (archive, filename)
1340 char *archive ATTRIBUTE_UNUSED;
1341 CONST char *filename;
1342 {
1343 if (!filename_per_symbol)
1344 printf ("\n%s:\n", filename);
1345 }
1346
1347 static void
1348 print_archive_member_sysv (archive, filename)
1349 char *archive;
1350 CONST char *filename;
1351 {
1352 if (undefined_only)
1353 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename);
1354 else
1355 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename);
1356 printf (_("\
1357 Name Value Class Type Size Line Section\n\n"));
1358 }
1359
1360 static void
1361 print_archive_member_posix (archive, filename)
1362 char *archive;
1363 CONST char *filename;
1364 {
1365 if (!filename_per_symbol)
1366 printf ("%s[%s]:\n", archive, filename);
1367 }
1368 \f
1369 /* Print the name of the file (and archive, if there is one)
1370 containing a symbol. */
1371
1372 static void
1373 print_symbol_filename_bsd (archive_bfd, abfd)
1374 bfd *archive_bfd, *abfd;
1375 {
1376 if (filename_per_symbol)
1377 {
1378 if (archive_bfd)
1379 printf ("%s:", bfd_get_filename (archive_bfd));
1380 printf ("%s:", bfd_get_filename (abfd));
1381 }
1382 }
1383
1384 static void
1385 print_symbol_filename_sysv (archive_bfd, abfd)
1386 bfd *archive_bfd, *abfd;
1387 {
1388 if (filename_per_symbol)
1389 {
1390 if (archive_bfd)
1391 printf ("%s:", bfd_get_filename (archive_bfd));
1392 printf ("%s:", bfd_get_filename (abfd));
1393 }
1394 }
1395
1396 static void
1397 print_symbol_filename_posix (archive_bfd, abfd)
1398 bfd *archive_bfd, *abfd;
1399 {
1400 if (filename_per_symbol)
1401 {
1402 if (archive_bfd)
1403 printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
1404 bfd_get_filename (abfd));
1405 else
1406 printf ("%s: ", bfd_get_filename (abfd));
1407 }
1408 }
1409 \f
1410 /* Print a symbol value. */
1411
1412 static void
1413 print_value (val)
1414 bfd_vma val;
1415 {
1416 #if ! defined (BFD64) || BFD_HOST_64BIT_LONG
1417 printf (value_format, val);
1418 #else
1419 /* We have a 64 bit value to print, but the host is only 32 bit. */
1420 if (print_radix == 16)
1421 fprintf_vma (stdout, val);
1422 else
1423 {
1424 char buf[30];
1425 char *s;
1426
1427 s = buf + sizeof buf;
1428 *--s = '\0';
1429 while (val > 0)
1430 {
1431 *--s = (val % print_radix) + '0';
1432 val /= print_radix;
1433 }
1434 while ((buf + sizeof buf - 1) - s < 16)
1435 *--s = '0';
1436 printf ("%s", s);
1437 }
1438 #endif
1439 }
1440
1441 /* Print a line of information about a symbol. */
1442
1443 static void
1444 print_symbol_info_bsd (info, abfd)
1445 symbol_info *info;
1446 bfd *abfd;
1447 {
1448 if (bfd_is_undefined_symclass (info->type))
1449 {
1450 printf ("%*s",
1451 #ifdef BFD64
1452 16,
1453 #else
1454 8,
1455 #endif
1456 "");
1457 }
1458 else
1459 print_value (info->value);
1460 printf (" %c", info->type);
1461 if (info->type == '-')
1462 {
1463 /* A stab. */
1464 printf (" ");
1465 printf (other_format, info->stab_other);
1466 printf (" ");
1467 printf (desc_format, info->stab_desc);
1468 printf (" %5s", info->stab_name);
1469 }
1470 print_symname (" %s", info->name, abfd);
1471 }
1472
1473 static void
1474 print_symbol_info_sysv (info, abfd)
1475 symbol_info *info;
1476 bfd *abfd;
1477 {
1478 print_symname ("%-20s|", info->name, abfd); /* Name */
1479 if (bfd_is_undefined_symclass (info->type))
1480 printf (" "); /* Value */
1481 else
1482 print_value (info->value);
1483 printf ("| %c |", info->type); /* Class */
1484 if (info->type == '-')
1485 {
1486 /* A stab. */
1487 printf ("%18s| ", info->stab_name); /* (C) Type */
1488 printf (desc_format, info->stab_desc); /* Size */
1489 printf ("| |"); /* Line, Section */
1490 }
1491 else
1492 printf (" | | |"); /* Type, Size, Line, Section */
1493 }
1494
1495 static void
1496 print_symbol_info_posix (info, abfd)
1497 symbol_info *info;
1498 bfd *abfd;
1499 {
1500 print_symname ("%s ", info->name, abfd);
1501 printf ("%c ", info->type);
1502 if (bfd_is_undefined_symclass (info->type))
1503 printf (" ");
1504 else
1505 print_value (info->value);
1506 /* POSIX.2 wants the symbol size printed here, when applicable;
1507 BFD currently doesn't provide it, so we take the easy way out by
1508 considering it to never be applicable. */
1509 }
1510 \f
1511 static void
1512 print_symdef_entry (abfd)
1513 bfd *abfd;
1514 {
1515 symindex idx = BFD_NO_MORE_SYMBOLS;
1516 carsym *thesym;
1517 boolean everprinted = false;
1518
1519 for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
1520 idx != BFD_NO_MORE_SYMBOLS;
1521 idx = bfd_get_next_mapent (abfd, idx, &thesym))
1522 {
1523 bfd *elt;
1524 if (!everprinted)
1525 {
1526 printf (_("\nArchive index:\n"));
1527 everprinted = true;
1528 }
1529 elt = bfd_get_elt_at_index (abfd, idx);
1530 if (elt == NULL)
1531 bfd_fatal ("bfd_get_elt_at_index");
1532 if (thesym->name != (char *) NULL)
1533 {
1534 print_symname ("%s", thesym->name, abfd);
1535 printf (" in %s\n", bfd_get_filename (elt));
1536 }
1537 }
1538 }
1539 \f
1540 /* This function is used to get the relocs for a particular section.
1541 It is called via bfd_map_over_sections. */
1542
1543 static void
1544 get_relocs (abfd, sec, dataarg)
1545 bfd *abfd;
1546 asection *sec;
1547 PTR dataarg;
1548 {
1549 struct get_relocs_info *data = (struct get_relocs_info *) dataarg;
1550
1551 *data->secs = sec;
1552
1553 if ((sec->flags & SEC_RELOC) == 0)
1554 {
1555 *data->relocs = NULL;
1556 *data->relcount = 0;
1557 }
1558 else
1559 {
1560 long relsize;
1561
1562 relsize = bfd_get_reloc_upper_bound (abfd, sec);
1563 if (relsize < 0)
1564 bfd_fatal (bfd_get_filename (abfd));
1565
1566 *data->relocs = (arelent **) xmalloc (relsize);
1567 *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs,
1568 data->syms);
1569 if (*data->relcount < 0)
1570 bfd_fatal (bfd_get_filename (abfd));
1571 }
1572
1573 ++data->secs;
1574 ++data->relocs;
1575 ++data->relcount;
1576 }
This page took 0.096768 seconds and 5 git commands to generate.