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