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