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