* Makefile.am (INCLUDES): Search intl dirs for headers; define
[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   setlocale (LC_MESSAGES, "");
376   bindtextdomain (PACKAGE, LOCALEDIR);
377   textdomain (PACKAGE);
378
379   program_name = *argv;
380   xmalloc_set_program_name (program_name);
381
382   START_PROGRESS (program_name, 0);
383
384   bfd_init ();
385   set_default_bfd_target ();
386
387   while ((c = getopt_long (argc, argv, "aABCDef:glnopPrst:uvV", long_options, (int *) 0)) != EOF)
388     {
389       switch (c)
390         {
391         case 'a':
392           print_debug_syms = 1;
393           break;
394         case 'A':
395         case 'o':
396           filename_per_symbol = 1;
397           break;
398         case 'B':               /* For MIPS compatibility.  */
399           set_output_format ("bsd");
400           break;
401         case 'C':
402           do_demangle = 1;
403           break;
404         case 'D':
405           dynamic = 1;
406           break;
407         case 'e':
408           /* Ignored for HP/UX compatibility.  */
409           break;
410         case 'f':
411           set_output_format (optarg);
412           break;
413         case 'g':
414           external_only = 1;
415           break;
416         case 'h':
417           usage (stdout, 0);
418         case 'l':
419           line_numbers = 1;
420           break;
421         case 'n':
422         case 'v':
423           sort_numerically = 1;
424           break;
425         case 'p':
426           no_sort = 1;
427           break;
428         case 'P':
429           set_output_format ("posix");
430           break;
431         case 'r':
432           reverse_sort = 1;
433           break;
434         case 's':
435           print_armap = 1;
436           break;
437         case 't':
438           set_print_radix (optarg);
439           break;
440         case 'u':
441           undefined_only = 1;
442           break;
443         case 'V':
444           show_version = 1;
445           break;
446
447         case 200:               /* --target */
448           target = optarg;
449           break;
450
451         case 0:         /* A long option that just sets a flag.  */
452           break;
453
454         default:
455           usage (stderr, 1);
456         }
457     }
458
459   if (show_version)
460     print_version ("nm");
461
462   /* OK, all options now parsed.  If no filename specified, do a.out.  */
463   if (optind == argc)
464     return !display_file ("a.out");
465
466   retval = 0;
467
468   if (argc - optind > 1)
469     filename_per_file = 1;
470
471   /* We were given several filenames to do.  */
472   while (optind < argc)
473     {
474       PROGRESS (1);
475       if (!display_file (argv[optind++]))
476         retval++;
477     }
478
479   END_PROGRESS (program_name);
480
481 #ifdef HAVE_SBRK
482   if (show_stats)
483     {
484       extern char **environ;
485       char *lim = (char *) sbrk (0);
486
487       fprintf (stderr, _("%s: data size %ld\n"), program_name,
488                (long) (lim - (char *) &environ));
489     }
490 #endif
491
492   exit (retval);
493   return retval;
494 }
495 \f
496 static void
497 display_archive (file)
498      bfd *file;
499 {
500   bfd *arfile = NULL;
501   bfd *last_arfile = NULL;
502   char **matching;
503
504   (*format->print_archive_filename) (bfd_get_filename (file));
505
506   if (print_armap)
507     print_symdef_entry (file);
508
509   for (;;)
510     {
511       PROGRESS (1);
512
513       arfile = bfd_openr_next_archived_file (file, arfile);
514
515       if (arfile == NULL)
516         {
517           if (bfd_get_error () != bfd_error_no_more_archived_files)
518             bfd_fatal (bfd_get_filename (file));
519           break;
520         }
521
522       if (bfd_check_format_matches (arfile, bfd_object, &matching))
523         {
524           (*format->print_archive_member) (bfd_get_filename (file),
525                                            bfd_get_filename (arfile));
526           display_rel_file (arfile, file);
527         }
528       else
529         {
530           bfd_nonfatal (bfd_get_filename (arfile));
531           if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
532             {
533               list_matching_formats (matching);
534               free (matching);
535             }
536         }
537
538       if (last_arfile != NULL)
539         {
540           bfd_close (last_arfile);
541           lineno_cache_bfd = NULL;
542           lineno_cache_rel_bfd = NULL;
543         }
544       last_arfile = arfile;
545     }
546
547   if (last_arfile != NULL)
548     {
549       bfd_close (last_arfile);
550       lineno_cache_bfd = NULL;
551       lineno_cache_rel_bfd = NULL;
552     }
553 }
554
555 static boolean
556 display_file (filename)
557      char *filename;
558 {
559   boolean retval = true;
560   bfd *file;
561   char **matching;
562
563   file = bfd_openr (filename, target);
564   if (file == NULL)
565     {
566       bfd_nonfatal (filename);
567       return false;
568     }
569
570   if (bfd_check_format (file, bfd_archive))
571     {
572       display_archive (file);
573     }
574   else if (bfd_check_format_matches (file, bfd_object, &matching))
575     {
576       (*format->print_object_filename) (filename);
577       display_rel_file (file, NULL);
578     }
579   else
580     {
581       bfd_nonfatal (filename);
582       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
583         {
584           list_matching_formats (matching);
585           free (matching);
586         }
587       retval = false;
588     }
589
590   if (bfd_close (file) == false)
591     bfd_fatal (filename);
592
593   lineno_cache_bfd = NULL;
594   lineno_cache_rel_bfd = NULL;
595
596   return retval;
597 }
598 \f
599 /* These globals are used to pass information into the sorting
600    routines.  */
601 static bfd *sort_bfd;
602 static boolean sort_dynamic;
603 static asymbol *sort_x;
604 static asymbol *sort_y;
605
606 /* Symbol-sorting predicates */
607 #define valueof(x) ((x)->section->vma + (x)->value)
608
609 /* Numeric sorts.  Undefined symbols are always considered "less than"
610    defined symbols with zero values.  Common symbols are not treated
611    specially -- i.e., their sizes are used as their "values".  */
612
613 static int
614 numeric_forward (P_x, P_y)
615      const PTR P_x;
616      const PTR P_y;
617 {
618   asymbol *x, *y;
619   asection *xs, *ys;
620
621   x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
622   y =  bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
623   if (x == NULL || y == NULL)
624     bfd_fatal (bfd_get_filename (sort_bfd));
625
626   xs = bfd_get_section (x);
627   ys = bfd_get_section (y);
628
629   if (bfd_is_und_section (xs))
630     {
631       if (! bfd_is_und_section (ys))
632         return -1;
633     }
634   else if (bfd_is_und_section (ys))
635     return 1;
636   else if (valueof (x) != valueof (y))
637     return valueof (x) < valueof (y) ? -1 : 1;
638
639   return non_numeric_forward (P_x, P_y);
640 }
641
642 static int
643 numeric_reverse (x, y)
644      const PTR x;
645      const PTR y;
646 {
647   return - numeric_forward (x, y);
648 }
649
650 static int
651 non_numeric_forward (P_x, P_y)
652      const PTR P_x;
653      const PTR P_y;
654 {
655   asymbol *x, *y;
656   const char *xn, *yn;
657
658   x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
659   y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
660   if (x == NULL || y == NULL)
661     bfd_fatal (bfd_get_filename (sort_bfd));
662
663   xn = bfd_asymbol_name (x);
664   yn = bfd_asymbol_name (y);
665
666   return ((xn == NULL) ? ((yn == NULL) ? 0 : -1) :
667           ((yn == NULL) ? 1 : strcmp (xn, yn)));
668 }
669
670 static int
671 non_numeric_reverse (x, y)
672      const PTR x;
673      const PTR y;
674 {
675   return - non_numeric_forward (x, y);
676 }
677
678 static int (*(sorters[2][2])) PARAMS ((const PTR, const PTR)) =
679 {
680   { non_numeric_forward, non_numeric_reverse },
681   { numeric_forward, numeric_reverse }
682 };
683
684 /* This sort routine is used by sort_symbols_by_size.  It is similar
685    to numeric_forward, but when symbols have the same value it sorts
686    by section VMA.  This simplifies the sort_symbols_by_size code
687    which handles symbols at the end of sections.  Also, this routine
688    tries to sort file names before other symbols with the same value.
689    That will make the file name have a zero size, which will make
690    sort_symbols_by_size choose the non file name symbol, leading to
691    more meaningful output.  For similar reasons, this code sorts
692    gnu_compiled_* and gcc2_compiled before other symbols with the same
693    value.  */
694
695 static int
696 size_forward1 (P_x, P_y)
697      const PTR P_x;
698      const PTR P_y;
699 {
700   asymbol *x, *y;
701   asection *xs, *ys;
702   const char *xn, *yn;
703   size_t xnl, ynl;
704   int xf, yf;
705
706   x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
707   y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
708   if (x == NULL || y == NULL)
709     bfd_fatal (bfd_get_filename (sort_bfd));
710
711   xs = bfd_get_section (x);
712   ys = bfd_get_section (y);
713
714   if (bfd_is_und_section (xs))
715     abort ();
716   if (bfd_is_und_section (ys))
717     abort ();
718
719   if (valueof (x) != valueof (y))
720     return valueof (x) < valueof (y) ? -1 : 1;
721
722   if (xs->vma != ys->vma)
723     return xs->vma < ys->vma ? -1 : 1;
724
725   xn = bfd_asymbol_name (x);
726   yn = bfd_asymbol_name (y);
727   xnl = strlen (xn);
728   ynl = strlen (yn);
729
730   /* The symbols gnu_compiled and gcc2_compiled convey even less
731      information than the file name, so sort them out first.  */
732
733   xf = (strstr (xn, "gnu_compiled") != NULL
734         || strstr (xn, "gcc2_compiled") != NULL);
735   yf = (strstr (yn, "gnu_compiled") != NULL
736         || strstr (yn, "gcc2_compiled") != NULL);
737
738   if (xf && ! yf)
739     return -1;
740   if (! xf && yf)
741     return 1;
742
743   /* We use a heuristic for the file name.  It may not work on non
744      Unix systems, but it doesn't really matter; the only difference
745      is precisely which symbol names get printed.  */
746
747 #define file_symbol(s, sn, snl)                 \
748   (((s)->flags & BSF_FILE) != 0                 \
749    || ((sn)[(snl) - 2] == '.'                   \
750        && ((sn)[(snl) - 1] == 'o'               \
751            || (sn)[(snl) - 1] == 'a')))
752
753   xf = file_symbol (x, xn, xnl);
754   yf = file_symbol (y, yn, ynl);
755
756   if (xf && ! yf)
757     return -1;
758   if (! xf && yf)
759     return 1;
760
761   return non_numeric_forward (P_x, P_y);
762 }
763
764 /* This sort routine is used by sort_symbols_by_size.  It is sorting
765    an array of size_sym structures into size order.  */
766
767 static int
768 size_forward2 (P_x, P_y)
769      const PTR P_x;
770      const PTR P_y;
771 {
772   const struct size_sym *x = (const struct size_sym *) P_x;
773   const struct size_sym *y = (const struct size_sym *) P_y;
774
775   if (x->size < y->size)
776     return reverse_sort ? 1 : -1;
777   else if (x->size > y->size)
778     return reverse_sort ? -1 : 1;
779   else
780     return sorters[0][reverse_sort] (x->minisym, y->minisym);
781 }
782
783 /* Sort the symbols by size.  We guess the size by assuming that the
784    difference between the address of a symbol and the address of the
785    next higher symbol is the size.  FIXME: ELF actually stores a size
786    with each symbol.  We should use it.  */
787
788 static long
789 sort_symbols_by_size (abfd, dynamic, minisyms, symcount, size, symsizesp)
790      bfd *abfd;
791      boolean dynamic;
792      PTR minisyms;
793      long symcount;
794      unsigned int size;
795      struct size_sym **symsizesp;
796 {
797   struct size_sym *symsizes;
798   bfd_byte *from, *fromend;
799   asymbol *sym = NULL;
800   asymbol *store_sym, *store_next;
801
802   qsort (minisyms, symcount, size, size_forward1);
803
804   /* We are going to return a special set of symbols and sizes to
805      print.  */
806   symsizes = (struct size_sym *) xmalloc (symcount * sizeof (struct size_sym));
807   *symsizesp = symsizes;
808
809   /* Note that filter_symbols has already removed all absolute and
810      undefined symbols.  Here we remove all symbols whose size winds
811      up as zero.  */
812
813   from = (bfd_byte *) minisyms;
814   fromend = from + symcount * size;
815
816   store_sym = sort_x;
817   store_next = sort_y;
818
819   if (from < fromend)
820     {
821       sym = bfd_minisymbol_to_symbol (abfd, dynamic, (const PTR) from,
822                                       store_sym);
823       if (sym == NULL)
824         bfd_fatal (bfd_get_filename (abfd));
825     }
826
827   for (; from < fromend; from += size)
828     {
829       asymbol *next;
830       asection *sec;
831       bfd_vma sz;
832       asymbol *temp;
833
834       if (from + size < fromend)
835         {
836           next = bfd_minisymbol_to_symbol (abfd,
837                                            dynamic,
838                                            (const PTR) (from + size),
839                                            store_next);
840           if (next == NULL)
841             bfd_fatal (bfd_get_filename (abfd));
842         }
843       else
844         next = NULL;
845
846       sec = bfd_get_section (sym);
847
848       if (bfd_is_com_section (sec))
849         sz = sym->value;
850       else
851         {
852           if (from + size < fromend
853               && sec == bfd_get_section (next))
854             sz = valueof (next) - valueof (sym);
855           else
856             sz = (bfd_get_section_vma (abfd, sec)
857                   + bfd_section_size (abfd, sec)
858                   - valueof (sym));
859         }
860
861       if (sz != 0)
862         {
863           symsizes->minisym = (const PTR) from;
864           symsizes->size = sz;
865           ++symsizes;
866         }
867
868       sym = next;
869
870       temp = store_sym;
871       store_sym = store_next;
872       store_next = temp;
873     }
874
875   symcount = symsizes - *symsizesp;
876
877   /* We must now sort again by size.  */
878   qsort ((PTR) *symsizesp, symcount, sizeof (struct size_sym), size_forward2);
879
880   return symcount;
881 }
882 \f
883 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD.  */
884
885 static void
886 display_rel_file (abfd, archive_bfd)
887      bfd *abfd;
888      bfd *archive_bfd;
889 {
890   long symcount;
891   PTR minisyms;
892   unsigned int size;
893   struct size_sym *symsizes;
894
895   if (! dynamic)
896     {
897       if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
898         {
899           printf (_("No symbols in \"%s\".\n"), bfd_get_filename (abfd));
900           return;
901         }
902     }
903
904   symcount = bfd_read_minisymbols (abfd, dynamic, &minisyms, &size);
905   if (symcount < 0)
906     bfd_fatal (bfd_get_filename (abfd));
907
908   if (symcount == 0)
909     {
910       fprintf (stderr, _("%s: no symbols\n"), bfd_get_filename (abfd));
911       return;
912     }
913
914   /* Discard the symbols we don't want to print.
915      It's OK to do this in place; we'll free the storage anyway
916      (after printing).  */
917
918   symcount = filter_symbols (abfd, dynamic, minisyms, symcount, size);
919
920   symsizes = NULL;
921   if (! no_sort)
922     {
923       sort_bfd = abfd;
924       sort_dynamic = dynamic;
925       sort_x = bfd_make_empty_symbol (abfd);
926       sort_y = bfd_make_empty_symbol (abfd);
927       if (sort_x == NULL || sort_y == NULL)
928         bfd_fatal (bfd_get_filename (abfd));
929
930       if (! sort_by_size)
931         qsort (minisyms, symcount, size,
932                sorters[sort_numerically][reverse_sort]);
933       else
934         symcount = sort_symbols_by_size (abfd, dynamic, minisyms, symcount,
935                                          size, &symsizes);
936     }
937
938   if (! sort_by_size)
939     print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd);
940   else
941     print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd);
942
943   free (minisyms);
944 }
945 \f
946 /* Choose which symbol entries to print;
947    compact them downward to get rid of the rest.
948    Return the number of symbols to be printed.  */
949
950 static long
951 filter_symbols (abfd, dynamic, minisyms, symcount, size)
952      bfd *abfd;
953      boolean dynamic;
954      PTR minisyms;
955      long symcount;
956      unsigned int size;
957 {
958   bfd_byte *from, *fromend, *to;
959   asymbol *store;
960
961   store = bfd_make_empty_symbol (abfd);
962   if (store == NULL)
963     bfd_fatal (bfd_get_filename (abfd));
964
965   from = (bfd_byte *) minisyms;
966   fromend = from + symcount * size;
967   to = (bfd_byte *) minisyms;
968
969   for (; from < fromend; from += size)
970     {
971       int keep = 0;
972       asymbol *sym;
973
974       PROGRESS (1);
975       
976       sym = bfd_minisymbol_to_symbol (abfd, dynamic, (const PTR) from, store);
977       if (sym == NULL)
978         bfd_fatal (bfd_get_filename (abfd));
979
980       if (undefined_only)
981         keep = bfd_is_und_section (sym->section);
982       else if (external_only)
983         keep = ((sym->flags & BSF_GLOBAL) != 0
984                 || (sym->flags & BSF_WEAK) != 0
985                 || bfd_is_und_section (sym->section)
986                 || bfd_is_com_section (sym->section));
987       else
988         keep = 1;
989
990       if (keep
991           && ! print_debug_syms
992           && (sym->flags & BSF_DEBUGGING) != 0)
993         keep = 0;
994
995       if (keep
996           && sort_by_size
997           && (bfd_is_abs_section (sym->section)
998               || bfd_is_und_section (sym->section)))
999         keep = 0;
1000
1001       if (keep
1002           && defined_only)
1003         {
1004           if (bfd_is_und_section (sym->section))
1005             keep = 0;
1006         }
1007
1008       if (keep)
1009         {
1010           memcpy (to, from, size);
1011           to += size;
1012         }
1013     }
1014
1015   return (to - (bfd_byte *) minisyms) / size;
1016 }
1017 \f
1018 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
1019    demangling it if requested.  */
1020
1021 static void
1022 print_symname (format, name, abfd)
1023      const char *format;
1024      const char *name;
1025      bfd *abfd;
1026 {
1027   if (do_demangle && *name)
1028     {
1029       char *res;
1030
1031       /* In this mode, give a user-level view of the symbol name
1032          even if it's not mangled; strip off any leading
1033          underscore.  */
1034       if (bfd_get_symbol_leading_char (abfd) == name[0])
1035         name++;
1036
1037       res = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
1038       if (res)
1039         {
1040           printf (format, res);
1041           free (res);
1042           return;
1043         }
1044     }
1045
1046   printf (format, name);
1047 }
1048
1049 /* Print the symbols.  If ARCHIVE_BFD is non-NULL, it is the archive
1050    containing ABFD.  */
1051
1052 static void
1053 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd)
1054      bfd *abfd;
1055      boolean dynamic;
1056      PTR minisyms;
1057      long symcount;
1058      unsigned int size;
1059      bfd *archive_bfd;
1060 {
1061   asymbol *store;
1062   bfd_byte *from, *fromend;
1063
1064   store = bfd_make_empty_symbol (abfd);
1065   if (store == NULL)
1066     bfd_fatal (bfd_get_filename (abfd));
1067
1068   from = (bfd_byte *) minisyms;
1069   fromend = from + symcount * size;
1070   for (; from < fromend; from += size)
1071     {
1072       asymbol *sym;
1073
1074       sym = bfd_minisymbol_to_symbol (abfd, dynamic, from, store);
1075       if (sym == NULL)
1076         bfd_fatal (bfd_get_filename (abfd));
1077
1078       print_symbol (abfd, sym, archive_bfd);
1079     }
1080 }
1081
1082 /* Print the symbols when sorting by size.  */
1083
1084 static void 
1085 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd)
1086      bfd *abfd;
1087      boolean dynamic;
1088      struct size_sym *symsizes;
1089      long symcount;
1090      bfd *archive_bfd;
1091 {
1092   asymbol *store;
1093   struct size_sym *from, *fromend;
1094
1095   store = bfd_make_empty_symbol (abfd);
1096   if (store == NULL)
1097     bfd_fatal (bfd_get_filename (abfd));
1098
1099   from = symsizes;
1100   fromend = from + symcount;
1101   for (; from < fromend; from++)
1102     {
1103       asymbol *sym;
1104
1105       sym = bfd_minisymbol_to_symbol (abfd, dynamic, from->minisym, store);
1106       if (sym == NULL)
1107         bfd_fatal (bfd_get_filename (abfd));
1108
1109       /* Set the symbol value so that we actually display the symbol
1110          size.  */
1111       sym->value = from->size - bfd_section_vma (abfd, bfd_get_section (sym));
1112
1113       print_symbol (abfd, sym, archive_bfd);
1114     }
1115 }
1116
1117 /* Print a single symbol.  */
1118
1119 static void
1120 print_symbol (abfd, sym, archive_bfd)
1121      bfd *abfd;
1122      asymbol *sym;
1123      bfd *archive_bfd;
1124 {
1125   PROGRESS (1);
1126
1127   (*format->print_symbol_filename) (archive_bfd, abfd);
1128
1129   if (undefined_only)
1130     {
1131       if (bfd_is_und_section (bfd_get_section (sym)))
1132         print_symname ("%s", bfd_asymbol_name (sym), abfd);
1133     }
1134   else
1135     {
1136       symbol_info syminfo;
1137
1138       bfd_get_symbol_info (abfd, sym, &syminfo);
1139       (*format->print_symbol_info) (&syminfo, abfd);
1140     }
1141
1142   if (line_numbers)
1143     {
1144       static asymbol **syms;
1145       static long symcount;
1146       const char *filename, *functionname;
1147       unsigned int lineno;
1148
1149       /* We need to get the canonical symbols in order to call
1150          bfd_find_nearest_line.  This is inefficient, but, then, you
1151          don't have to use --line-numbers.  */
1152       if (abfd != lineno_cache_bfd && syms != NULL)
1153         {
1154           free (syms);
1155           syms = NULL;
1156         }
1157       if (syms == NULL)
1158         {
1159           long symsize;
1160
1161           symsize = bfd_get_symtab_upper_bound (abfd);
1162           if (symsize < 0)
1163             bfd_fatal (bfd_get_filename (abfd));
1164           syms = (asymbol **) xmalloc (symsize);
1165           symcount = bfd_canonicalize_symtab (abfd, syms);
1166           if (symcount < 0)
1167             bfd_fatal (bfd_get_filename (abfd));
1168           lineno_cache_bfd = abfd;
1169         }
1170
1171       if (bfd_is_und_section (bfd_get_section (sym)))
1172         {
1173           static asection **secs;
1174           static arelent ***relocs;
1175           static long *relcount;
1176           static unsigned int seccount;
1177           unsigned int i;
1178           const char *symname;
1179
1180           /* For an undefined symbol, we try to find a reloc for the
1181              symbol, and print the line number of the reloc.  */
1182
1183           if (abfd != lineno_cache_rel_bfd && relocs != NULL)
1184             {
1185               for (i = 0; i < seccount; i++)
1186                 if (relocs[i] != NULL)
1187                   free (relocs[i]);
1188               free (secs);
1189               free (relocs);
1190               free (relcount);
1191               secs = NULL;
1192               relocs = NULL;
1193               relcount = NULL;
1194             }
1195
1196           if (relocs == NULL)
1197             {
1198               struct get_relocs_info info;
1199
1200               seccount = bfd_count_sections (abfd);
1201
1202               secs = (asection **) xmalloc (seccount * sizeof *secs);
1203               relocs = (arelent ***) xmalloc (seccount * sizeof *relocs);
1204               relcount = (long *) xmalloc (seccount * sizeof *relcount);
1205
1206               info.secs = secs;
1207               info.relocs = relocs;
1208               info.relcount = relcount;
1209               info.syms = syms;
1210               bfd_map_over_sections (abfd, get_relocs, (PTR) &info);
1211               lineno_cache_rel_bfd = abfd;
1212             }
1213
1214           symname = bfd_asymbol_name (sym);
1215           for (i = 0; i < seccount; i++)
1216             {
1217               long j;
1218
1219               for (j = 0; j < relcount[i]; j++)
1220                 {
1221                   arelent *r;
1222
1223                   r = relocs[i][j];
1224                   if (r->sym_ptr_ptr != NULL
1225                       && (*r->sym_ptr_ptr)->section == sym->section
1226                       && (*r->sym_ptr_ptr)->value == sym->value
1227                       && strcmp (symname,
1228                                  bfd_asymbol_name (*r->sym_ptr_ptr)) == 0
1229                       && bfd_find_nearest_line (abfd, secs[i], syms,
1230                                                 r->address, &filename,
1231                                                 &functionname, &lineno))
1232                     {
1233                       /* We only print the first one we find.  */
1234                       printf ("\t%s:%u", filename, lineno);
1235                       i = seccount;
1236                       break;
1237                     }
1238                 }
1239             }
1240         }
1241       else if (bfd_get_section (sym)->owner == abfd)
1242         {
1243           if (bfd_find_nearest_line (abfd, bfd_get_section (sym), syms,
1244                                      sym->value, &filename, &functionname,
1245                                      &lineno)
1246               && filename != NULL
1247               && lineno != 0)
1248             {
1249               printf ("\t%s:%u", filename, lineno);
1250             }
1251         }
1252     }
1253
1254   putchar ('\n');
1255 }
1256 \f
1257 /* The following 3 groups of functions are called unconditionally,
1258    once at the start of processing each file of the appropriate type.
1259    They should check `filename_per_file' and `filename_per_symbol',
1260    as appropriate for their output format, to determine whether to
1261    print anything.  */
1262 \f
1263 /* Print the name of an object file given on the command line.  */
1264
1265 static void
1266 print_object_filename_bsd (filename)
1267      char *filename;
1268 {
1269   if (filename_per_file && !filename_per_symbol)
1270     printf ("\n%s:\n", filename);
1271 }
1272
1273 static void
1274 print_object_filename_sysv (filename)
1275      char *filename;
1276 {
1277   if (undefined_only)
1278     printf (_("\n\nUndefined symbols from %s:\n\n"), filename);
1279   else
1280     printf (_("\n\nSymbols from %s:\n\n"), filename);
1281   printf (_("\
1282 Name                  Value   Class        Type         Size   Line  Section\n\n"));
1283 }
1284
1285 static void
1286 print_object_filename_posix (filename)
1287      char *filename;
1288 {
1289   if (filename_per_file && !filename_per_symbol)
1290     printf ("%s:\n", filename);
1291 }
1292 \f
1293 /* Print the name of an archive file given on the command line.  */
1294
1295 static void
1296 print_archive_filename_bsd (filename)
1297      char *filename;
1298 {
1299   if (filename_per_file)
1300     printf ("\n%s:\n", filename);
1301 }
1302
1303 static void
1304 print_archive_filename_sysv (filename)
1305      char *filename;
1306 {
1307 }
1308
1309 static void
1310 print_archive_filename_posix (filename)
1311      char *filename;
1312 {
1313 }
1314 \f
1315 /* Print the name of an archive member file.  */
1316
1317 static void
1318 print_archive_member_bsd (archive, filename)
1319      char *archive;
1320      CONST char *filename;
1321 {
1322   if (!filename_per_symbol)
1323     printf ("\n%s:\n", filename);
1324 }
1325
1326 static void
1327 print_archive_member_sysv (archive, filename)
1328      char *archive;
1329      CONST char *filename;
1330 {
1331   if (undefined_only)
1332     printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename);
1333   else
1334     printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename);
1335   printf (_("\
1336 Name                  Value   Class        Type         Size   Line  Section\n\n"));
1337 }
1338
1339 static void
1340 print_archive_member_posix (archive, filename)
1341      char *archive;
1342      CONST char *filename;
1343 {
1344   if (!filename_per_symbol)
1345     printf ("%s[%s]:\n", archive, filename);
1346 }
1347 \f
1348 /* Print the name of the file (and archive, if there is one)
1349    containing a symbol.  */
1350
1351 static void
1352 print_symbol_filename_bsd (archive_bfd, abfd)
1353      bfd *archive_bfd, *abfd;
1354 {
1355   if (filename_per_symbol)
1356     {
1357       if (archive_bfd)
1358         printf ("%s:", bfd_get_filename (archive_bfd));
1359       printf ("%s:", bfd_get_filename (abfd));
1360     }
1361 }
1362
1363 static void
1364 print_symbol_filename_sysv (archive_bfd, abfd)
1365      bfd *archive_bfd, *abfd;
1366 {
1367   if (filename_per_symbol)
1368     {
1369       if (archive_bfd)
1370         printf ("%s:", bfd_get_filename (archive_bfd));
1371       printf ("%s:", bfd_get_filename (abfd));
1372     }
1373 }
1374
1375 static void
1376 print_symbol_filename_posix (archive_bfd, abfd)
1377      bfd *archive_bfd, *abfd;
1378 {
1379   if (filename_per_symbol)
1380     {
1381       if (archive_bfd)
1382         printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
1383                 bfd_get_filename (abfd));
1384       else
1385         printf ("%s: ", bfd_get_filename (abfd));
1386     }
1387 }
1388 \f
1389 /* Print a symbol value.  */
1390
1391 static void
1392 print_value (val)
1393      bfd_vma val;
1394 {
1395 #if ! defined (BFD64) || BFD_HOST_64BIT_LONG
1396   printf (value_format, val);
1397 #else
1398   /* We have a 64 bit value to print, but the host is only 32 bit.  */
1399   if (print_radix == 16)
1400     fprintf_vma (stdout, val);
1401   else
1402     {
1403       char buf[30];
1404       char *s;
1405
1406       s = buf + sizeof buf;
1407       *--s = '\0';
1408       while (val > 0)
1409         {
1410           *--s = (val % print_radix) + '0';
1411           val /= print_radix;
1412         }
1413       while ((buf + sizeof buf - 1) - s < 16)
1414         *--s = '0';
1415       printf ("%s", s);
1416     }
1417 #endif
1418 }
1419
1420 /* Print a line of information about a symbol.  */
1421
1422 static void
1423 print_symbol_info_bsd (info, abfd)
1424      symbol_info *info;
1425      bfd *abfd;
1426 {
1427   if (info->type == 'U')
1428     {
1429       printf ("%*s",
1430 #ifdef BFD64
1431               16,
1432 #else
1433               8,
1434 #endif
1435               "");
1436     }
1437   else
1438     print_value (info->value);
1439   printf (" %c", info->type);
1440   if (info->type == '-')
1441     {
1442       /* A stab.  */
1443       printf (" ");
1444       printf (other_format, info->stab_other);
1445       printf (" ");
1446       printf (desc_format, info->stab_desc);
1447       printf (" %5s", info->stab_name);
1448     }
1449   print_symname (" %s", info->name, abfd);
1450 }
1451
1452 static void
1453 print_symbol_info_sysv (info, abfd)
1454      symbol_info *info;
1455      bfd *abfd;
1456 {
1457   print_symname ("%-20s|", info->name, abfd);   /* Name */
1458   if (info->type == 'U')
1459     printf ("        ");        /* Value */
1460   else
1461     print_value (info->value);
1462   printf ("|   %c  |", info->type);     /* Class */
1463   if (info->type == '-')
1464     {
1465       /* A stab.  */
1466       printf ("%18s|  ", info->stab_name);      /* (C) Type */
1467       printf (desc_format, info->stab_desc);    /* Size */
1468       printf ("|     |");       /* Line, Section */
1469     }
1470   else
1471     printf ("                  |      |     |");        /* Type, Size, Line, Section */
1472 }
1473
1474 static void
1475 print_symbol_info_posix (info, abfd)
1476      symbol_info *info;
1477      bfd *abfd;
1478 {
1479   print_symname ("%s ", info->name, abfd);
1480   printf ("%c ", info->type);
1481   if (info->type == 'U')
1482     printf ("        ");
1483   else
1484     print_value (info->value);
1485   /* POSIX.2 wants the symbol size printed here, when applicable;
1486      BFD currently doesn't provide it, so we take the easy way out by
1487      considering it to never be applicable.  */
1488 }
1489 \f
1490 static void
1491 print_symdef_entry (abfd)
1492      bfd *abfd;
1493 {
1494   symindex idx = BFD_NO_MORE_SYMBOLS;
1495   carsym *thesym;
1496   boolean everprinted = false;
1497
1498   for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
1499        idx != BFD_NO_MORE_SYMBOLS;
1500        idx = bfd_get_next_mapent (abfd, idx, &thesym))
1501     {
1502       bfd *elt;
1503       if (!everprinted)
1504         {
1505           printf (_("\nArchive index:\n"));
1506           everprinted = true;
1507         }
1508       elt = bfd_get_elt_at_index (abfd, idx);
1509       if (elt == NULL)
1510         bfd_fatal ("bfd_get_elt_at_index");
1511       if (thesym->name != (char *) NULL)
1512         {
1513           print_symname ("%s", thesym->name, abfd);
1514           printf (" in %s\n", bfd_get_filename (elt));
1515         }
1516     }
1517 }
1518 \f
1519 /* This function is used to get the relocs for a particular section.
1520    It is called via bfd_map_over_sections.  */
1521
1522 static void
1523 get_relocs (abfd, sec, dataarg)
1524      bfd *abfd;
1525      asection *sec;
1526      PTR dataarg;
1527 {
1528   struct get_relocs_info *data = (struct get_relocs_info *) dataarg;
1529
1530   *data->secs = sec;
1531
1532   if ((sec->flags & SEC_RELOC) == 0)
1533     {
1534       *data->relocs = NULL;
1535       *data->relcount = 0;
1536     }
1537   else
1538     {
1539       long relsize;
1540
1541       relsize = bfd_get_reloc_upper_bound (abfd, sec);
1542       if (relsize < 0)
1543         bfd_fatal (bfd_get_filename (abfd));
1544
1545       *data->relocs = (arelent **) xmalloc (relsize);
1546       *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs,
1547                                                 data->syms);
1548       if (*data->relcount < 0)
1549         bfd_fatal (bfd_get_filename (abfd));
1550     }
1551
1552   ++data->secs;
1553   ++data->relocs;
1554   ++data->relcount;
1555 }