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