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