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