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