* configure.in: Check for strcoll.
[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
1090       /* In this mode, give a user-level view of the symbol name
1091          even if it's not mangled; strip off any leading
1092          underscore.  */
1093       if (bfd_get_symbol_leading_char (abfd) == name[0])
1094         name++;
1095
1096       res = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
1097       if (res)
1098         {
1099           printf (format, res);
1100           free (res);
1101           return;
1102         }
1103     }
1104
1105   printf (format, name);
1106 }
1107
1108 /* Print the symbols.  If ARCHIVE_BFD is non-NULL, it is the archive
1109    containing ABFD.  */
1110
1111 static void
1112 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd)
1113      bfd *abfd;
1114      boolean dynamic;
1115      PTR minisyms;
1116      long symcount;
1117      unsigned int size;
1118      bfd *archive_bfd;
1119 {
1120   asymbol *store;
1121   bfd_byte *from, *fromend;
1122
1123   store = bfd_make_empty_symbol (abfd);
1124   if (store == NULL)
1125     bfd_fatal (bfd_get_filename (abfd));
1126
1127   from = (bfd_byte *) minisyms;
1128   fromend = from + symcount * size;
1129   for (; from < fromend; from += size)
1130     {
1131       asymbol *sym;
1132
1133       sym = bfd_minisymbol_to_symbol (abfd, dynamic, from, store);
1134       if (sym == NULL)
1135         bfd_fatal (bfd_get_filename (abfd));
1136
1137       print_symbol (abfd, sym, (bfd_vma) 0, archive_bfd);
1138     }
1139 }
1140
1141 /* Print the symbols when sorting by size.  */
1142
1143 static void
1144 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd)
1145      bfd *abfd;
1146      boolean dynamic;
1147      struct size_sym *symsizes;
1148      long symcount;
1149      bfd *archive_bfd;
1150 {
1151   asymbol *store;
1152   struct size_sym *from, *fromend;
1153
1154   store = bfd_make_empty_symbol (abfd);
1155   if (store == NULL)
1156     bfd_fatal (bfd_get_filename (abfd));
1157
1158   from = symsizes;
1159   fromend = from + symcount;
1160   for (; from < fromend; from++)
1161     {
1162       asymbol *sym;
1163       bfd_vma ssize;
1164
1165       sym = bfd_minisymbol_to_symbol (abfd, dynamic, from->minisym, store);
1166       if (sym == NULL)
1167         bfd_fatal (bfd_get_filename (abfd));
1168
1169       /* For elf we have already computed the correct symbol size.  */
1170       if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1171         ssize = from->size;
1172       else
1173         ssize = from->size - bfd_section_vma (abfd, bfd_get_section (sym));
1174
1175       print_symbol (abfd, sym, ssize, archive_bfd);
1176     }
1177 }
1178
1179 /* Print a single symbol.  */
1180
1181 static void
1182 print_symbol (abfd, sym, ssize, archive_bfd)
1183      bfd *abfd;
1184      asymbol *sym;
1185      bfd_vma ssize;
1186      bfd *archive_bfd;
1187 {
1188   PROGRESS (1);
1189
1190   (*format->print_symbol_filename) (archive_bfd, abfd);
1191
1192   if (undefined_only)
1193     {
1194       if (bfd_is_und_section (bfd_get_section (sym)))
1195         print_symname ("%s", bfd_asymbol_name (sym), abfd);
1196     }
1197   else
1198     {
1199       symbol_info syminfo;
1200       struct extended_symbol_info info;
1201
1202       bfd_get_symbol_info (abfd, sym, &syminfo);
1203       info.sinfo = &syminfo;
1204       info.ssize = ssize;
1205       if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1206         info.elfinfo = (elf_symbol_type *) sym;
1207       else
1208         info.elfinfo = NULL;
1209       (*format->print_symbol_info) (&info, abfd);
1210     }
1211
1212   if (line_numbers)
1213     {
1214       static asymbol **syms;
1215       static long symcount;
1216       const char *filename, *functionname;
1217       unsigned int lineno;
1218
1219       /* We need to get the canonical symbols in order to call
1220          bfd_find_nearest_line.  This is inefficient, but, then, you
1221          don't have to use --line-numbers.  */
1222       if (abfd != lineno_cache_bfd && syms != NULL)
1223         {
1224           free (syms);
1225           syms = NULL;
1226         }
1227       if (syms == NULL)
1228         {
1229           long symsize;
1230
1231           symsize = bfd_get_symtab_upper_bound (abfd);
1232           if (symsize < 0)
1233             bfd_fatal (bfd_get_filename (abfd));
1234           syms = (asymbol **) xmalloc (symsize);
1235           symcount = bfd_canonicalize_symtab (abfd, syms);
1236           if (symcount < 0)
1237             bfd_fatal (bfd_get_filename (abfd));
1238           lineno_cache_bfd = abfd;
1239         }
1240
1241       if (bfd_is_und_section (bfd_get_section (sym)))
1242         {
1243           static asection **secs;
1244           static arelent ***relocs;
1245           static long *relcount;
1246           static unsigned int seccount;
1247           unsigned int i;
1248           const char *symname;
1249
1250           /* For an undefined symbol, we try to find a reloc for the
1251              symbol, and print the line number of the reloc.  */
1252           if (abfd != lineno_cache_rel_bfd && relocs != NULL)
1253             {
1254               for (i = 0; i < seccount; i++)
1255                 if (relocs[i] != NULL)
1256                   free (relocs[i]);
1257               free (secs);
1258               free (relocs);
1259               free (relcount);
1260               secs = NULL;
1261               relocs = NULL;
1262               relcount = NULL;
1263             }
1264
1265           if (relocs == NULL)
1266             {
1267               struct get_relocs_info info;
1268
1269               seccount = bfd_count_sections (abfd);
1270
1271               secs = (asection **) xmalloc (seccount * sizeof *secs);
1272               relocs = (arelent ***) xmalloc (seccount * sizeof *relocs);
1273               relcount = (long *) xmalloc (seccount * sizeof *relcount);
1274
1275               info.secs = secs;
1276               info.relocs = relocs;
1277               info.relcount = relcount;
1278               info.syms = syms;
1279               bfd_map_over_sections (abfd, get_relocs, (PTR) &info);
1280               lineno_cache_rel_bfd = abfd;
1281             }
1282
1283           symname = bfd_asymbol_name (sym);
1284           for (i = 0; i < seccount; i++)
1285             {
1286               long j;
1287
1288               for (j = 0; j < relcount[i]; j++)
1289                 {
1290                   arelent *r;
1291
1292                   r = relocs[i][j];
1293                   if (r->sym_ptr_ptr != NULL
1294                       && (*r->sym_ptr_ptr)->section == sym->section
1295                       && (*r->sym_ptr_ptr)->value == sym->value
1296                       && strcmp (symname,
1297                                  bfd_asymbol_name (*r->sym_ptr_ptr)) == 0
1298                       && bfd_find_nearest_line (abfd, secs[i], syms,
1299                                                 r->address, &filename,
1300                                                 &functionname, &lineno)
1301                       && filename != NULL)
1302                     {
1303                       /* We only print the first one we find.  */
1304                       printf ("\t%s:%u", filename, lineno);
1305                       i = seccount;
1306                       break;
1307                     }
1308                 }
1309             }
1310         }
1311       else if (bfd_get_section (sym)->owner == abfd)
1312         {
1313           if (bfd_find_nearest_line (abfd, bfd_get_section (sym), syms,
1314                                      sym->value, &filename, &functionname,
1315                                      &lineno)
1316               && filename != NULL
1317               && lineno != 0)
1318             {
1319               printf ("\t%s:%u", filename, lineno);
1320             }
1321         }
1322     }
1323
1324   putchar ('\n');
1325 }
1326 \f
1327 /* The following 3 groups of functions are called unconditionally,
1328    once at the start of processing each file of the appropriate type.
1329    They should check `filename_per_file' and `filename_per_symbol',
1330    as appropriate for their output format, to determine whether to
1331    print anything.  */
1332 \f
1333 /* Print the name of an object file given on the command line.  */
1334
1335 static void
1336 print_object_filename_bsd (filename)
1337      char *filename;
1338 {
1339   if (filename_per_file && !filename_per_symbol)
1340     printf ("\n%s:\n", filename);
1341 }
1342
1343 static void
1344 print_object_filename_sysv (filename)
1345      char *filename;
1346 {
1347   if (undefined_only)
1348     printf (_("\n\nUndefined symbols from %s:\n\n"), filename);
1349   else
1350     printf (_("\n\nSymbols from %s:\n\n"), filename);
1351   if (print_width == 8)
1352     printf (_("\
1353 Name                  Value   Class        Type         Size     Line  Section\n\n"));
1354   else
1355     printf (_("\
1356 Name                  Value           Class        Type         Size             Line  Section\n\n"));
1357 }
1358
1359 static void
1360 print_object_filename_posix (filename)
1361      char *filename;
1362 {
1363   if (filename_per_file && !filename_per_symbol)
1364     printf ("%s:\n", filename);
1365 }
1366 \f
1367 /* Print the name of an archive file given on the command line.  */
1368
1369 static void
1370 print_archive_filename_bsd (filename)
1371      char *filename;
1372 {
1373   if (filename_per_file)
1374     printf ("\n%s:\n", filename);
1375 }
1376
1377 static void
1378 print_archive_filename_sysv (filename)
1379      char *filename ATTRIBUTE_UNUSED;
1380 {
1381 }
1382
1383 static void
1384 print_archive_filename_posix (filename)
1385      char *filename ATTRIBUTE_UNUSED;
1386 {
1387 }
1388 \f
1389 /* Print the name of an archive member file.  */
1390
1391 static void
1392 print_archive_member_bsd (archive, filename)
1393      char *archive ATTRIBUTE_UNUSED;
1394      const char *filename;
1395 {
1396   if (!filename_per_symbol)
1397     printf ("\n%s:\n", filename);
1398 }
1399
1400 static void
1401 print_archive_member_sysv (archive, filename)
1402      char *archive;
1403      const char *filename;
1404 {
1405   if (undefined_only)
1406     printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename);
1407   else
1408     printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename);
1409   if (print_width == 8)
1410     printf (_("\
1411 Name                  Value   Class        Type         Size     Line  Section\n\n"));
1412   else
1413     printf (_("\
1414 Name                  Value           Class        Type         Size             Line  Section\n\n"));
1415 }
1416
1417 static void
1418 print_archive_member_posix (archive, filename)
1419      char *archive;
1420      const char *filename;
1421 {
1422   if (!filename_per_symbol)
1423     printf ("%s[%s]:\n", archive, filename);
1424 }
1425 \f
1426 /* Print the name of the file (and archive, if there is one)
1427    containing a symbol.  */
1428
1429 static void
1430 print_symbol_filename_bsd (archive_bfd, abfd)
1431      bfd *archive_bfd, *abfd;
1432 {
1433   if (filename_per_symbol)
1434     {
1435       if (archive_bfd)
1436         printf ("%s:", bfd_get_filename (archive_bfd));
1437       printf ("%s:", bfd_get_filename (abfd));
1438     }
1439 }
1440
1441 static void
1442 print_symbol_filename_sysv (archive_bfd, abfd)
1443      bfd *archive_bfd, *abfd;
1444 {
1445   if (filename_per_symbol)
1446     {
1447       if (archive_bfd)
1448         printf ("%s:", bfd_get_filename (archive_bfd));
1449       printf ("%s:", bfd_get_filename (abfd));
1450     }
1451 }
1452
1453 static void
1454 print_symbol_filename_posix (archive_bfd, abfd)
1455      bfd *archive_bfd, *abfd;
1456 {
1457   if (filename_per_symbol)
1458     {
1459       if (archive_bfd)
1460         printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
1461                 bfd_get_filename (abfd));
1462       else
1463         printf ("%s: ", bfd_get_filename (abfd));
1464     }
1465 }
1466 \f
1467 /* Print a symbol value.  */
1468
1469 static void
1470 print_value (abfd, val)
1471      bfd *abfd ATTRIBUTE_UNUSED;
1472      bfd_vma val;
1473 {
1474 #if ! defined (BFD64) || BFD_HOST_64BIT_LONG
1475   printf (value_format, val);
1476 #else
1477   /* We have a 64 bit value to print, but the host is only 32 bit.  */
1478   if (print_radix == 16)
1479     bfd_fprintf_vma (abfd, stdout, val);
1480   else
1481     {
1482       char buf[30];
1483       char *s;
1484
1485       s = buf + sizeof buf;
1486       *--s = '\0';
1487       while (val > 0)
1488         {
1489           *--s = (val % print_radix) + '0';
1490           val /= print_radix;
1491         }
1492       while ((buf + sizeof buf - 1) - s < 16)
1493         *--s = '0';
1494       printf ("%s", s);
1495     }
1496 #endif
1497 }
1498
1499 /* Print a line of information about a symbol.  */
1500
1501 static void
1502 print_symbol_info_bsd (info, abfd)
1503      struct extended_symbol_info *info;
1504      bfd *abfd;
1505 {
1506   if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1507     {
1508       if (print_width == 16)
1509         printf ("        ");
1510       printf ("        ");
1511     }
1512   else
1513     {
1514       print_value (abfd, SYM_VALUE (info));
1515
1516       if (print_size && SYM_SIZE (info))
1517         {
1518           printf(" ");
1519           print_value (abfd, SYM_SIZE (info));
1520         }
1521     }
1522
1523   printf (" %c", SYM_TYPE (info));
1524
1525   if (SYM_TYPE (info) == '-')
1526     {
1527       /* A stab.  */
1528       printf (" ");
1529       printf (other_format, SYM_STAB_OTHER (info));
1530       printf (" ");
1531       printf (desc_format, SYM_STAB_DESC (info));
1532       printf (" %5s", SYM_STAB_NAME (info));
1533     }
1534   print_symname (" %s", SYM_NAME (info), abfd);
1535 }
1536
1537 static void
1538 print_symbol_info_sysv (info, abfd)
1539      struct extended_symbol_info *info;
1540      bfd *abfd;
1541 {
1542   print_symname ("%-20s|", SYM_NAME (info), abfd);
1543
1544   if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1545     {
1546       if (print_width == 8)
1547         printf ("        ");
1548       else
1549         printf ("                ");
1550     }
1551   else
1552     print_value (abfd, SYM_VALUE (info));
1553
1554   printf ("|   %c  |", SYM_TYPE (info));
1555
1556   if (SYM_TYPE (info) == '-')
1557     {
1558       /* A stab.  */
1559       printf ("%18s|  ", SYM_STAB_NAME (info));         /* (C) Type */
1560       printf (desc_format, SYM_STAB_DESC (info));       /* Size */
1561       printf ("|     |");                               /* Line, Section */
1562     }
1563   else
1564     {
1565       /* Type, Size, Line, Section */
1566       if (info->elfinfo)
1567         printf ("%18s|",
1568                 get_symbol_type (ELF_ST_TYPE (info->elfinfo->internal_elf_sym.st_info)));
1569       else
1570         printf ("                  |");
1571
1572       if (SYM_SIZE (info))
1573         print_value (abfd, SYM_SIZE (info));
1574       else
1575         {
1576           if (print_width == 8)
1577             printf ("        ");
1578           else
1579             printf ("                ");
1580         }
1581
1582       if (info->elfinfo)
1583         printf("|     |%s", info->elfinfo->symbol.section->name);
1584       else
1585         printf("|     |");
1586     }
1587 }
1588
1589 static void
1590 print_symbol_info_posix (info, abfd)
1591      struct extended_symbol_info *info;
1592      bfd *abfd;
1593 {
1594   print_symname ("%s ", SYM_NAME (info), abfd);
1595   printf ("%c ", SYM_TYPE (info));
1596
1597   if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1598     printf ("        ");
1599   else
1600     {
1601       print_value (abfd, SYM_VALUE (info));
1602       printf (" ");
1603       if (SYM_SIZE (info))
1604         print_value (abfd, SYM_SIZE (info));
1605     }
1606 }
1607 \f
1608 static void
1609 print_symdef_entry (abfd)
1610      bfd *abfd;
1611 {
1612   symindex idx = BFD_NO_MORE_SYMBOLS;
1613   carsym *thesym;
1614   boolean everprinted = false;
1615
1616   for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
1617        idx != BFD_NO_MORE_SYMBOLS;
1618        idx = bfd_get_next_mapent (abfd, idx, &thesym))
1619     {
1620       bfd *elt;
1621       if (!everprinted)
1622         {
1623           printf (_("\nArchive index:\n"));
1624           everprinted = true;
1625         }
1626       elt = bfd_get_elt_at_index (abfd, idx);
1627       if (elt == NULL)
1628         bfd_fatal ("bfd_get_elt_at_index");
1629       if (thesym->name != (char *) NULL)
1630         {
1631           print_symname ("%s", thesym->name, abfd);
1632           printf (" in %s\n", bfd_get_filename (elt));
1633         }
1634     }
1635 }
1636 \f
1637 /* This function is used to get the relocs for a particular section.
1638    It is called via bfd_map_over_sections.  */
1639
1640 static void
1641 get_relocs (abfd, sec, dataarg)
1642      bfd *abfd;
1643      asection *sec;
1644      PTR dataarg;
1645 {
1646   struct get_relocs_info *data = (struct get_relocs_info *) dataarg;
1647
1648   *data->secs = sec;
1649
1650   if ((sec->flags & SEC_RELOC) == 0)
1651     {
1652       *data->relocs = NULL;
1653       *data->relcount = 0;
1654     }
1655   else
1656     {
1657       long relsize;
1658
1659       relsize = bfd_get_reloc_upper_bound (abfd, sec);
1660       if (relsize < 0)
1661         bfd_fatal (bfd_get_filename (abfd));
1662
1663       *data->relocs = (arelent **) xmalloc (relsize);
1664       *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs,
1665                                                 data->syms);
1666       if (*data->relcount < 0)
1667         bfd_fatal (bfd_get_filename (abfd));
1668     }
1669
1670   ++data->secs;
1671   ++data->relocs;
1672   ++data->relcount;
1673 }