Merge from vendor branch BINUTILS:
[dragonfly.git] / contrib / binutils / 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
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 /* $FreeBSD: src/contrib/binutils/binutils/nm.c,v 1.3.6.6 2002/09/01 23:43:47 obrien Exp $ */
24 /* $DragonFly: src/contrib/binutils/binutils/Attic/nm.c,v 1.2 2003/06/17 04:23:58 dillon Exp $ */
25
26 #include "bfd.h"
27 #include "progress.h"
28 #include "bucomm.h"
29 #include "getopt.h"
30 #include "aout/stab_gnu.h"
31 #include "aout/ranlib.h"
32 #include "demangle.h"
33 #include "libiberty.h"
34
35 /* When sorting by size, we use this structure to hold the size and a
36    pointer to the minisymbol.  */
37
38 struct size_sym
39 {
40   const PTR minisym;
41   bfd_vma size;
42 };
43
44 /* When fetching relocs, we use this structure to pass information to
45    get_relocs.  */
46
47 struct get_relocs_info
48 {
49   asection **secs;
50   arelent ***relocs;
51   long *relcount;
52   asymbol **syms;
53 };
54
55 static void
56 usage PARAMS ((FILE *, int));
57
58 static void
59 set_print_radix PARAMS ((char *));
60
61 static void
62 set_output_format PARAMS ((char *));
63
64 static void
65 display_archive PARAMS ((bfd *));
66
67 static boolean
68 display_file PARAMS ((char *filename));
69
70 static void
71 display_rel_file PARAMS ((bfd * file, bfd * archive));
72
73 static long
74 filter_symbols PARAMS ((bfd *, boolean, PTR, long, unsigned int));
75
76 static long
77 sort_symbols_by_size PARAMS ((bfd *, boolean, PTR, long, unsigned int,
78                               struct size_sym **));
79
80 static void
81 print_symbols PARAMS ((bfd *, boolean, PTR, long, unsigned int, bfd *));
82
83 static void
84 print_size_symbols PARAMS ((bfd *, boolean, struct size_sym *, long, bfd *));
85
86 static void
87 print_symname PARAMS ((const char *, const char *, bfd *));
88
89 static void
90 print_symbol PARAMS ((bfd *, asymbol *, bfd *));
91
92 static void
93 print_symdef_entry PARAMS ((bfd * abfd));
94
95 /* The sorting functions.  */
96
97 static int
98 numeric_forward PARAMS ((const PTR, const PTR));
99
100 static int
101 numeric_reverse PARAMS ((const PTR, const PTR));
102
103 static int
104 non_numeric_forward PARAMS ((const PTR, const PTR));
105
106 static int
107 non_numeric_reverse PARAMS ((const PTR, const PTR));
108
109 static int
110 size_forward1 PARAMS ((const PTR, const PTR));
111
112 static int
113 size_forward2 PARAMS ((const PTR, const PTR));
114
115 /* The output formatting functions.  */
116
117 static void
118 print_object_filename_bsd PARAMS ((char *filename));
119
120 static void
121 print_object_filename_sysv PARAMS ((char *filename));
122
123 static void
124 print_object_filename_posix PARAMS ((char *filename));
125
126
127 static void
128 print_archive_filename_bsd PARAMS ((char *filename));
129
130 static void
131 print_archive_filename_sysv PARAMS ((char *filename));
132
133 static void
134 print_archive_filename_posix PARAMS ((char *filename));
135
136
137 static void
138 print_archive_member_bsd PARAMS ((char *archive, CONST char *filename));
139
140 static void
141 print_archive_member_sysv PARAMS ((char *archive, CONST char *filename));
142
143 static void
144 print_archive_member_posix PARAMS ((char *archive, CONST char *filename));
145
146
147 static void
148 print_symbol_filename_bsd PARAMS ((bfd * archive_bfd, bfd * abfd));
149
150 static void
151 print_symbol_filename_sysv PARAMS ((bfd * archive_bfd, bfd * abfd));
152
153 static void
154 print_symbol_filename_posix PARAMS ((bfd * archive_bfd, bfd * abfd));
155
156
157 static void
158 print_value PARAMS ((bfd *, bfd_vma));
159
160 static void
161 print_symbol_info_bsd PARAMS ((symbol_info * info, bfd * abfd));
162
163 static void
164 print_symbol_info_sysv PARAMS ((symbol_info * info, bfd * abfd));
165
166 static void
167 print_symbol_info_posix PARAMS ((symbol_info * info, bfd * abfd));
168
169 static void
170 get_relocs PARAMS ((bfd *, asection *, PTR));
171
172 /* Support for different output formats.  */
173 struct output_fns
174   {
175     /* Print the name of an object file given on the command line.  */
176     void (*print_object_filename) PARAMS ((char *filename));
177
178     /* Print the name of an archive file given on the command line.  */
179     void (*print_archive_filename) PARAMS ((char *filename));
180
181     /* Print the name of an archive member file.  */
182     void (*print_archive_member) PARAMS ((char *archive, CONST char *filename));
183
184     /* Print the name of the file (and archive, if there is one)
185        containing a symbol.  */
186     void (*print_symbol_filename) PARAMS ((bfd * archive_bfd, bfd * abfd));
187
188     /* Print a line of information about a symbol.  */
189     void (*print_symbol_info) PARAMS ((symbol_info * info, bfd * abfd));
190   };
191 static struct output_fns formats[] =
192 {
193   {print_object_filename_bsd,
194    print_archive_filename_bsd,
195    print_archive_member_bsd,
196    print_symbol_filename_bsd,
197    print_symbol_info_bsd},
198   {print_object_filename_sysv,
199    print_archive_filename_sysv,
200    print_archive_member_sysv,
201    print_symbol_filename_sysv,
202    print_symbol_info_sysv},
203   {print_object_filename_posix,
204    print_archive_filename_posix,
205    print_archive_member_posix,
206    print_symbol_filename_posix,
207    print_symbol_info_posix}
208 };
209
210 /* Indices in `formats'.  */
211 #define FORMAT_BSD 0
212 #define FORMAT_SYSV 1
213 #define FORMAT_POSIX 2
214 #define FORMAT_DEFAULT FORMAT_BSD
215
216 /* The output format to use.  */
217 static struct output_fns *format = &formats[FORMAT_DEFAULT];
218
219
220 /* Command options.  */
221
222 static int do_demangle = 0;     /* Pretty print C++ symbol names.  */
223 static int external_only = 0;   /* print external symbols only */
224 static int defined_only = 0;    /* Print defined symbols only */
225 static int no_sort = 0;         /* don't sort; print syms in order found */
226 static int print_debug_syms = 0;        /* print debugger-only symbols too */
227 static int print_armap = 0;     /* describe __.SYMDEF data in archive files.  */
228 static int reverse_sort = 0;    /* sort in downward(alpha or numeric) order */
229 static int sort_numerically = 0;        /* sort in numeric rather than alpha order */
230 static int sort_by_size = 0;    /* sort by size of symbol */
231 static int undefined_only = 0;  /* print undefined symbols only */
232 static int dynamic = 0;         /* print dynamic symbols.  */
233 static int show_version = 0;    /* show the version number */
234 static int show_stats = 0;      /* show statistics */
235 static int line_numbers = 0;    /* print line numbers for symbols */
236
237 /* When to print the names of files.  Not mutually exclusive in SYSV format.  */
238 static int filename_per_file = 0;       /* Once per file, on its own line.  */
239 static int filename_per_symbol = 0;     /* Once per symbol, at start of line.  */
240
241 /* Print formats for printing a symbol value.  */
242 #ifndef BFD64
243 static char value_format[] = "%08lx";
244 #else
245 #if BFD_HOST_64BIT_LONG
246 static char value_format[] = "%016lx";
247 #else
248 /* We don't use value_format for this case.  */
249 #endif
250 #endif
251 #ifdef BFD64
252 static int print_width = 16;
253 #else
254 static int print_width = 8;
255 #endif
256 static int print_radix = 16;
257 /* Print formats for printing stab info.  */
258 static char other_format[] = "%02x";
259 static char desc_format[] = "%04x";
260
261 static char *target = NULL;
262
263 /* Used to cache the line numbers for a BFD.  */
264 static bfd *lineno_cache_bfd;
265 static bfd *lineno_cache_rel_bfd;
266
267 #define OPTION_TARGET 200
268
269 static struct option long_options[] =
270 {
271   {"debug-syms", no_argument, &print_debug_syms, 1},
272   {"demangle", optional_argument, 0, 'C'},
273   {"dynamic", no_argument, &dynamic, 1},
274   {"extern-only", no_argument, &external_only, 1},
275   {"format", required_argument, 0, 'f'},
276   {"help", no_argument, 0, 'h'},
277   {"line-numbers", no_argument, 0, 'l'},
278   {"no-cplus", no_argument, &do_demangle, 0},  /* Linux compatibility.  */
279   {"no-demangle", no_argument, &do_demangle, 0},
280   {"no-sort", no_argument, &no_sort, 1},
281   {"numeric-sort", no_argument, &sort_numerically, 1},
282   {"portability", no_argument, 0, 'P'},
283   {"print-armap", no_argument, &print_armap, 1},
284   {"print-file-name", no_argument, 0, 'o'},
285   {"radix", required_argument, 0, 't'},
286   {"reverse-sort", no_argument, &reverse_sort, 1},
287   {"size-sort", no_argument, &sort_by_size, 1},
288   {"stats", no_argument, &show_stats, 1},
289   {"target", required_argument, 0, OPTION_TARGET},
290   {"defined-only", no_argument, &defined_only, 1},
291   {"undefined-only", no_argument, &undefined_only, 1},
292   {"version", no_argument, &show_version, 1},
293   {0, no_argument, 0, 0}
294 };
295 \f
296 /* Some error-reporting functions */
297
298 static void
299 usage (stream, status)
300      FILE *stream;
301      int status;
302 {
303   fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
304   fprintf (stream, _(" List symbols in [file(s)] (a.out by default).\n"));
305   fprintf (stream, _(" The options are:\n\
306   -a, --debug-syms       Display debugger-only symbols\n\
307   -A, --print-file-name  Print name of the input file before every symbol\n\
308   -B                     Same as --format=bsd\n\
309   -C, --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
310                           The STYLE, if specified, can be `auto' (the default),\n\
311                           `gnu', 'lucid', 'arm', 'hp', 'edg' or 'gnu-new-abi'\n\
312       --no-demangle      Do not demangle low-level symbol names\n\
313   -D, --dynamic          Display dynamic symbols instead of normal symbols\n\
314       --defined-only     Display only defined symbols\n\
315   -e                     (ignored)\n\
316   -f, --format=FORMAT    Use the output format FORMAT.  FORMAT can be `bsd',\n\
317                            `sysv' or `posix'.  The default is `bsd'\n\
318   -g, --extern-only      Display only external symbols\n\
319   -l, --line-numbers     Use debugging information to find a filename and\n\
320                            line number for each symbol\n\
321   -n, --numeric-sort     Sort symbols numerically by address\n\
322   -o                     Same as -A\n\
323   -p, --no-sort          Do not sort the symbols\n\
324   -P, --portability      Same as --format=posix\n\
325   -r, --reverse-sort     Reverse the sense of the sort\n\
326   -s, --print-armap      Include index for symbols from archive members\n\
327       --size-sort        Sort symbols by size\n\
328   -t, --radix=RADIX      Use RADIX for printing symbol values\n\
329       --target=BFDNAME   Specify the target object format as BFDNAME\n\
330   -u, --undefined-only   Display only undefined symbols\n\
331   -X 32_64               (ignored)\n\
332   -h, --help             Display this information\n\
333   -V, --version          Display this program's version number\n\
334 \n"));
335   list_supported_targets (program_name, stream);
336   if (status == 0)
337     fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
338   exit (status);
339 }
340
341 /* Set the radix for the symbol value and size according to RADIX.  */
342
343 static void
344 set_print_radix (radix)
345      char *radix;
346 {
347   switch (*radix)
348     {
349     case 'x':
350       break;
351     case 'd':
352     case 'o':
353       if (*radix == 'd')
354         print_radix = 10;
355       else
356         print_radix = 8;
357 #ifndef BFD64
358       value_format[4] = *radix;
359 #else
360 #if BFD_HOST_64BIT_LONG
361       value_format[5] = *radix;
362 #else
363       /* This case requires special handling for octal and decimal
364          printing.  */
365 #endif
366 #endif
367       other_format[3] = desc_format[3] = *radix;
368       break;
369     default:
370       fatal (_("%s: invalid radix"), radix);
371     }
372 }
373
374 static void
375 set_output_format (f)
376      char *f;
377 {
378   int i;
379
380   switch (*f)
381     {
382     case 'b':
383     case 'B':
384       i = FORMAT_BSD;
385       break;
386     case 'p':
387     case 'P':
388       i = FORMAT_POSIX;
389       break;
390     case 's':
391     case 'S':
392       i = FORMAT_SYSV;
393       break;
394     default:
395       fatal (_("%s: invalid output format"), f);
396     }
397   format = &formats[i];
398 }
399 \f
400 int main PARAMS ((int, char **));
401
402 int
403 main (argc, argv)
404      int argc;
405      char **argv;
406 {
407   int c;
408   int retval;
409
410 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
411   setlocale (LC_MESSAGES, "");
412 #endif
413 #if defined (HAVE_SETLOCALE)
414   setlocale (LC_CTYPE, "");
415 #endif
416   bindtextdomain (PACKAGE, LOCALEDIR);
417   textdomain (PACKAGE);
418
419   program_name = *argv;
420   xmalloc_set_program_name (program_name);
421
422   START_PROGRESS (program_name, 0);
423
424   bfd_init ();
425   set_default_bfd_target ();
426
427   while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrst:uvVvX:",
428                            long_options, (int *) 0)) != EOF)
429     {
430       switch (c)
431         {
432         case 'a':
433           print_debug_syms = 1;
434           break;
435         case 'A':
436         case 'o':
437           filename_per_symbol = 1;
438           break;
439         case 'B':               /* For MIPS compatibility.  */
440           set_output_format ("bsd");
441           break;
442         case 'C':
443           do_demangle = 1;
444           if (optarg != NULL)
445             {
446               enum demangling_styles style;
447               
448               style = cplus_demangle_name_to_style (optarg);
449               if (style == unknown_demangling) 
450                 fatal (_("unknown demangling style `%s'"),
451                        optarg);
452               
453               cplus_demangle_set_style (style);
454            }
455           break;
456         case 'D':
457           dynamic = 1;
458           break;
459         case 'e':
460           /* Ignored for HP/UX compatibility.  */
461           break;
462         case 'f':
463           set_output_format (optarg);
464           break;
465         case 'g':
466           external_only = 1;
467           break;
468         case 'H':
469         case 'h':
470           usage (stdout, 0);
471         case 'l':
472           line_numbers = 1;
473           break;
474         case 'n':
475         case 'v':
476           sort_numerically = 1;
477           break;
478         case 'p':
479           no_sort = 1;
480           break;
481         case 'P':
482           set_output_format ("posix");
483           break;
484         case 'r':
485           reverse_sort = 1;
486           break;
487         case 's':
488           print_armap = 1;
489           break;
490         case 't':
491           set_print_radix (optarg);
492           break;
493         case 'u':
494           undefined_only = 1;
495           break;
496         case 'V':
497           show_version = 1;
498           break;
499         case 'X':
500           /* Ignored for (partial) AIX compatibility.  On AIX, the
501              argument has values 32, 64, or 32_64, and specfies that
502              only 32-bit, only 64-bit, or both kinds of objects should
503              be examined.  The default is 32.  So plain AIX nm on a
504              library archive with both kinds of objects will ignore
505              the 64-bit ones.  For GNU nm, the default is and always
506              has been -X 32_64, and other options are not supported.  */
507           if (strcmp (optarg, "32_64") != 0)
508             fatal (_("Only -X 32_64 is supported"));
509           break;
510
511         case OPTION_TARGET:     /* --target */
512           target = optarg;
513           break;
514
515         case 0:         /* A long option that just sets a flag.  */
516           break;
517
518         default:
519           usage (stderr, 1);
520         }
521     }
522
523   if (show_version)
524     print_version ("nm");
525
526   /* OK, all options now parsed.  If no filename specified, do a.out.  */
527   if (optind == argc)
528     return !display_file ("a.out");
529
530   retval = 0;
531
532   if (argc - optind > 1)
533     filename_per_file = 1;
534
535   /* We were given several filenames to do.  */
536   while (optind < argc)
537     {
538       PROGRESS (1);
539       if (!display_file (argv[optind++]))
540         retval++;
541     }
542
543   END_PROGRESS (program_name);
544
545 #ifdef HAVE_SBRK
546   if (show_stats)
547     {
548       char *lim = (char *) sbrk (0);
549
550       non_fatal (_("data size %ld"), (long) (lim - (char *) &environ));
551     }
552 #endif
553
554   exit (retval);
555   return retval;
556 }
557 \f
558 static void
559 display_archive (file)
560      bfd *file;
561 {
562   bfd *arfile = NULL;
563   bfd *last_arfile = NULL;
564   char **matching;
565
566   (*format->print_archive_filename) (bfd_get_filename (file));
567
568   if (print_armap)
569     print_symdef_entry (file);
570
571   for (;;)
572     {
573       PROGRESS (1);
574
575       arfile = bfd_openr_next_archived_file (file, arfile);
576
577       if (arfile == NULL)
578         {
579           if (bfd_get_error () != bfd_error_no_more_archived_files)
580             bfd_fatal (bfd_get_filename (file));
581           break;
582         }
583
584       if (bfd_check_format_matches (arfile, bfd_object, &matching))
585         {
586           (*format->print_archive_member) (bfd_get_filename (file),
587                                            bfd_get_filename (arfile));
588           display_rel_file (arfile, file);
589         }
590       else
591         {
592           bfd_nonfatal (bfd_get_filename (arfile));
593           if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
594             {
595               list_matching_formats (matching);
596               free (matching);
597             }
598         }
599
600       if (last_arfile != NULL)
601         {
602           bfd_close (last_arfile);
603           lineno_cache_bfd = NULL;
604           lineno_cache_rel_bfd = NULL;
605         }
606       last_arfile = arfile;
607     }
608
609   if (last_arfile != NULL)
610     {
611       bfd_close (last_arfile);
612       lineno_cache_bfd = NULL;
613       lineno_cache_rel_bfd = NULL;
614     }
615 }
616
617 static boolean
618 display_file (filename)
619      char *filename;
620 {
621   boolean retval = true;
622   bfd *file;
623   char **matching;
624
625   file = bfd_openr (filename, target);
626   if (file == NULL)
627     {
628       bfd_nonfatal (filename);
629       return false;
630     }
631
632   if (bfd_check_format (file, bfd_archive))
633     {
634       display_archive (file);
635     }
636   else if (bfd_check_format_matches (file, bfd_object, &matching))
637     {
638       (*format->print_object_filename) (filename);
639       display_rel_file (file, NULL);
640     }
641   else
642     {
643       bfd_nonfatal (filename);
644       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
645         {
646           list_matching_formats (matching);
647           free (matching);
648         }
649       retval = false;
650     }
651
652   if (bfd_close (file) == false)
653     bfd_fatal (filename);
654
655   lineno_cache_bfd = NULL;
656   lineno_cache_rel_bfd = NULL;
657
658   return retval;
659 }
660 \f
661 /* These globals are used to pass information into the sorting
662    routines.  */
663 static bfd *sort_bfd;
664 static boolean sort_dynamic;
665 static asymbol *sort_x;
666 static asymbol *sort_y;
667
668 /* Symbol-sorting predicates */
669 #define valueof(x) ((x)->section->vma + (x)->value)
670
671 /* Numeric sorts.  Undefined symbols are always considered "less than"
672    defined symbols with zero values.  Common symbols are not treated
673    specially -- i.e., their sizes are used as their "values".  */
674
675 static int
676 numeric_forward (P_x, P_y)
677      const PTR P_x;
678      const PTR P_y;
679 {
680   asymbol *x, *y;
681   asection *xs, *ys;
682
683   x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
684   y =  bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
685   if (x == NULL || y == NULL)
686     bfd_fatal (bfd_get_filename (sort_bfd));
687
688   xs = bfd_get_section (x);
689   ys = bfd_get_section (y);
690
691   if (bfd_is_und_section (xs))
692     {
693       if (! bfd_is_und_section (ys))
694         return -1;
695     }
696   else if (bfd_is_und_section (ys))
697     return 1;
698   else if (valueof (x) != valueof (y))
699     return valueof (x) < valueof (y) ? -1 : 1;
700
701   return non_numeric_forward (P_x, P_y);
702 }
703
704 static int
705 numeric_reverse (x, y)
706      const PTR x;
707      const PTR y;
708 {
709   return - numeric_forward (x, y);
710 }
711
712 static int
713 non_numeric_forward (P_x, P_y)
714      const PTR P_x;
715      const PTR P_y;
716 {
717   asymbol *x, *y;
718   const char *xn, *yn;
719
720   x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
721   y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
722   if (x == NULL || y == NULL)
723     bfd_fatal (bfd_get_filename (sort_bfd));
724
725   xn = bfd_asymbol_name (x);
726   yn = bfd_asymbol_name (y);
727
728   return ((xn == NULL) ? ((yn == NULL) ? 0 : -1) :
729           ((yn == NULL) ? 1 : strcmp (xn, yn)));
730 }
731
732 static int
733 non_numeric_reverse (x, y)
734      const PTR x;
735      const PTR y;
736 {
737   return - non_numeric_forward (x, y);
738 }
739
740 static int (*(sorters[2][2])) PARAMS ((const PTR, const PTR)) =
741 {
742   { non_numeric_forward, non_numeric_reverse },
743   { numeric_forward, numeric_reverse }
744 };
745
746 /* This sort routine is used by sort_symbols_by_size.  It is similar
747    to numeric_forward, but when symbols have the same value it sorts
748    by section VMA.  This simplifies the sort_symbols_by_size code
749    which handles symbols at the end of sections.  Also, this routine
750    tries to sort file names before other symbols with the same value.
751    That will make the file name have a zero size, which will make
752    sort_symbols_by_size choose the non file name symbol, leading to
753    more meaningful output.  For similar reasons, this code sorts
754    gnu_compiled_* and gcc2_compiled before other symbols with the same
755    value.  */
756
757 static int
758 size_forward1 (P_x, P_y)
759      const PTR P_x;
760      const PTR P_y;
761 {
762   asymbol *x, *y;
763   asection *xs, *ys;
764   const char *xn, *yn;
765   size_t xnl, ynl;
766   int xf, yf;
767
768   x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
769   y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
770   if (x == NULL || y == NULL)
771     bfd_fatal (bfd_get_filename (sort_bfd));
772
773   xs = bfd_get_section (x);
774   ys = bfd_get_section (y);
775
776   if (bfd_is_und_section (xs))
777     abort ();
778   if (bfd_is_und_section (ys))
779     abort ();
780
781   if (valueof (x) != valueof (y))
782     return valueof (x) < valueof (y) ? -1 : 1;
783
784   if (xs->vma != ys->vma)
785     return xs->vma < ys->vma ? -1 : 1;
786
787   xn = bfd_asymbol_name (x);
788   yn = bfd_asymbol_name (y);
789   xnl = strlen (xn);
790   ynl = strlen (yn);
791
792   /* The symbols gnu_compiled and gcc2_compiled convey even less
793      information than the file name, so sort them out first.  */
794
795   xf = (strstr (xn, "gnu_compiled") != NULL
796         || strstr (xn, "gcc2_compiled") != NULL);
797   yf = (strstr (yn, "gnu_compiled") != NULL
798         || strstr (yn, "gcc2_compiled") != NULL);
799
800   if (xf && ! yf)
801     return -1;
802   if (! xf && yf)
803     return 1;
804
805   /* We use a heuristic for the file name.  It may not work on non
806      Unix systems, but it doesn't really matter; the only difference
807      is precisely which symbol names get printed.  */
808
809 #define file_symbol(s, sn, snl)                 \
810   (((s)->flags & BSF_FILE) != 0                 \
811    || ((sn)[(snl) - 2] == '.'                   \
812        && ((sn)[(snl) - 1] == 'o'               \
813            || (sn)[(snl) - 1] == 'a')))
814
815   xf = file_symbol (x, xn, xnl);
816   yf = file_symbol (y, yn, ynl);
817
818   if (xf && ! yf)
819     return -1;
820   if (! xf && yf)
821     return 1;
822
823   return non_numeric_forward (P_x, P_y);
824 }
825
826 /* This sort routine is used by sort_symbols_by_size.  It is sorting
827    an array of size_sym structures into size order.  */
828
829 static int
830 size_forward2 (P_x, P_y)
831      const PTR P_x;
832      const PTR P_y;
833 {
834   const struct size_sym *x = (const struct size_sym *) P_x;
835   const struct size_sym *y = (const struct size_sym *) P_y;
836
837   if (x->size < y->size)
838     return reverse_sort ? 1 : -1;
839   else if (x->size > y->size)
840     return reverse_sort ? -1 : 1;
841   else
842     return sorters[0][reverse_sort] (x->minisym, y->minisym);
843 }
844
845 /* Sort the symbols by size.  We guess the size by assuming that the
846    difference between the address of a symbol and the address of the
847    next higher symbol is the size.  FIXME: ELF actually stores a size
848    with each symbol.  We should use it.  */
849
850 static long
851 sort_symbols_by_size (abfd, dynamic, minisyms, symcount, size, symsizesp)
852      bfd *abfd;
853      boolean dynamic;
854      PTR minisyms;
855      long symcount;
856      unsigned int size;
857      struct size_sym **symsizesp;
858 {
859   struct size_sym *symsizes;
860   bfd_byte *from, *fromend;
861   asymbol *sym = NULL;
862   asymbol *store_sym, *store_next;
863
864   qsort (minisyms, symcount, size, size_forward1);
865
866   /* We are going to return a special set of symbols and sizes to
867      print.  */
868   symsizes = (struct size_sym *) xmalloc (symcount * sizeof (struct size_sym));
869   *symsizesp = symsizes;
870
871   /* Note that filter_symbols has already removed all absolute and
872      undefined symbols.  Here we remove all symbols whose size winds
873      up as zero.  */
874
875   from = (bfd_byte *) minisyms;
876   fromend = from + symcount * size;
877
878   store_sym = sort_x;
879   store_next = sort_y;
880
881   if (from < fromend)
882     {
883       sym = bfd_minisymbol_to_symbol (abfd, dynamic, (const PTR) from,
884                                       store_sym);
885       if (sym == NULL)
886         bfd_fatal (bfd_get_filename (abfd));
887     }
888
889   for (; from < fromend; from += size)
890     {
891       asymbol *next;
892       asection *sec;
893       bfd_vma sz;
894       asymbol *temp;
895
896       if (from + size < fromend)
897         {
898           next = bfd_minisymbol_to_symbol (abfd,
899                                            dynamic,
900                                            (const PTR) (from + size),
901                                            store_next);
902           if (next == NULL)
903             bfd_fatal (bfd_get_filename (abfd));
904         }
905       else
906         next = NULL;
907
908       sec = bfd_get_section (sym);
909
910       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 }