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