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