Home | History | Annotate | Download | only in binutils
      1 /* objdump.c -- dump information about an object file.
      2    Copyright (C) 1990-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, or (at your option)
      9    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, 51 Franklin Street - Fifth Floor, Boston,
     19    MA 02110-1301, USA.  */
     20 
     21 
     22 /* Objdump overview.
     23 
     24    Objdump displays information about one or more object files, either on
     25    their own, or inside libraries.  It is commonly used as a disassembler,
     26    but it can also display information about file headers, symbol tables,
     27    relocations, debugging directives and more.
     28 
     29    The flow of execution is as follows:
     30 
     31    1. Command line arguments are checked for control switches and the
     32       information to be displayed is selected.
     33 
     34    2. Any remaining arguments are assumed to be object files, and they are
     35       processed in order by display_bfd().  If the file is an archive each
     36       of its elements is processed in turn.
     37 
     38    3. The file's target architecture and binary file format are determined
     39       by bfd_check_format().  If they are recognised, then dump_bfd() is
     40       called.
     41 
     42    4. dump_bfd() in turn calls separate functions to display the requested
     43       item(s) of information(s).  For example disassemble_data() is called if
     44       a disassembly has been requested.
     45 
     46    When disassembling the code loops through blocks of instructions bounded
     47    by symbols, calling disassemble_bytes() on each block.  The actual
     48    disassembling is done by the libopcodes library, via a function pointer
     49    supplied by the disassembler() function.  */
     50 
     51 #include "sysdep.h"
     52 #include "bfd.h"
     53 #include "elf-bfd.h"
     54 #include "progress.h"
     55 #include "bucomm.h"
     56 #include "elfcomm.h"
     57 #include "dwarf.h"
     58 #include "getopt.h"
     59 #include "safe-ctype.h"
     60 #include "dis-asm.h"
     61 #include "libiberty.h"
     62 #include "demangle.h"
     63 #include "filenames.h"
     64 #include "debug.h"
     65 #include "budbg.h"
     66 #include "objdump.h"
     67 
     68 #ifdef HAVE_MMAP
     69 #include <sys/mman.h>
     70 #endif
     71 
     72 /* Internal headers for the ELF .stab-dump code - sorry.  */
     73 #define	BYTES_IN_WORD	32
     74 #include "aout/aout64.h"
     75 
     76 /* Exit status.  */
     77 static int exit_status = 0;
     78 
     79 static char *default_target = NULL;	/* Default at runtime.  */
     80 
     81 /* The following variables are set based on arguments passed on the
     82    command line.  */
     83 static int show_version = 0;		/* Show the version number.  */
     84 static int dump_section_contents;	/* -s */
     85 static int dump_section_headers;	/* -h */
     86 static bfd_boolean dump_file_header;	/* -f */
     87 static int dump_symtab;			/* -t */
     88 static int dump_dynamic_symtab;		/* -T */
     89 static int dump_reloc_info;		/* -r */
     90 static int dump_dynamic_reloc_info;	/* -R */
     91 static int dump_ar_hdrs;		/* -a */
     92 static int dump_private_headers;	/* -p */
     93 static char *dump_private_options;	/* -P */
     94 static int prefix_addresses;		/* --prefix-addresses */
     95 static int with_line_numbers;		/* -l */
     96 static bfd_boolean with_source_code;	/* -S */
     97 static int show_raw_insn;		/* --show-raw-insn */
     98 static int dump_dwarf_section_info;	/* --dwarf */
     99 static int dump_stab_section_info;	/* --stabs */
    100 static int do_demangle;			/* -C, --demangle */
    101 static bfd_boolean disassemble;		/* -d */
    102 static bfd_boolean disassemble_all;	/* -D */
    103 static int disassemble_zeroes;		/* --disassemble-zeroes */
    104 static bfd_boolean formats_info;	/* -i */
    105 static int wide_output;			/* -w */
    106 static int insn_width;			/* --insn-width */
    107 static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
    108 static bfd_vma stop_address = (bfd_vma) -1;  /* --stop-address */
    109 static int dump_debugging;		/* --debugging */
    110 static int dump_debugging_tags;		/* --debugging-tags */
    111 static int suppress_bfd_header;
    112 static int dump_special_syms = 0;	/* --special-syms */
    113 static bfd_vma adjust_section_vma = 0;	/* --adjust-vma */
    114 static int file_start_context = 0;      /* --file-start-context */
    115 static bfd_boolean display_file_offsets;/* -F */
    116 static const char *prefix;		/* --prefix */
    117 static int prefix_strip;		/* --prefix-strip */
    118 static size_t prefix_length;
    119 
    120 /* A structure to record the sections mentioned in -j switches.  */
    121 struct only
    122 {
    123   const char * name; /* The name of the section.  */
    124   bfd_boolean  seen; /* A flag to indicate that the section has been found in one or more input files.  */
    125   struct only * next; /* Pointer to the next structure in the list.  */
    126 };
    127 /* Pointer to an array of 'only' structures.
    128    This pointer is NULL if the -j switch has not been used.  */
    129 static struct only * only_list = NULL;
    130 
    131 /* Variables for handling include file path table.  */
    132 static const char **include_paths;
    133 static int include_path_count;
    134 
    135 /* Extra info to pass to the section disassembler and address printing
    136    function.  */
    137 struct objdump_disasm_info
    138 {
    139   bfd *              abfd;
    140   asection *         sec;
    141   bfd_boolean        require_sec;
    142   arelent **         dynrelbuf;
    143   long               dynrelcount;
    144   disassembler_ftype disassemble_fn;
    145   arelent *          reloc;
    146 };
    147 
    148 /* Architecture to disassemble for, or default if NULL.  */
    149 static char *machine = NULL;
    150 
    151 /* Target specific options to the disassembler.  */
    152 static char *disassembler_options = NULL;
    153 
    154 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN.  */
    155 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
    156 
    157 /* The symbol table.  */
    158 static asymbol **syms;
    159 
    160 /* Number of symbols in `syms'.  */
    161 static long symcount = 0;
    162 
    163 /* The sorted symbol table.  */
    164 static asymbol **sorted_syms;
    165 
    166 /* Number of symbols in `sorted_syms'.  */
    167 static long sorted_symcount = 0;
    168 
    169 /* The dynamic symbol table.  */
    170 static asymbol **dynsyms;
    171 
    172 /* The synthetic symbol table.  */
    173 static asymbol *synthsyms;
    174 static long synthcount = 0;
    175 
    176 /* Number of symbols in `dynsyms'.  */
    177 static long dynsymcount = 0;
    178 
    179 static bfd_byte *stabs;
    180 static bfd_size_type stab_size;
    181 
    182 static char *strtab;
    183 static bfd_size_type stabstr_size;
    184 
    185 static bfd_boolean is_relocatable = FALSE;
    186 
    187 /* Handlers for -P/--private.  */
    188 static const struct objdump_private_desc * const objdump_private_vectors[] =
    189   {
    190     OBJDUMP_PRIVATE_VECTORS
    191     NULL
    192   };
    193 
    194 static void usage (FILE *, int) ATTRIBUTE_NORETURN;
    196 static void
    197 usage (FILE *stream, int status)
    198 {
    199   fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
    200   fprintf (stream, _(" Display information from object <file(s)>.\n"));
    201   fprintf (stream, _(" At least one of the following switches must be given:\n"));
    202   fprintf (stream, _("\
    203   -a, --archive-headers    Display archive header information\n\
    204   -f, --file-headers       Display the contents of the overall file header\n\
    205   -p, --private-headers    Display object format specific file header contents\n\
    206   -P, --private=OPT,OPT... Display object format specific contents\n\
    207   -h, --[section-]headers  Display the contents of the section headers\n\
    208   -x, --all-headers        Display the contents of all headers\n\
    209   -d, --disassemble        Display assembler contents of executable sections\n\
    210   -D, --disassemble-all    Display assembler contents of all sections\n\
    211   -S, --source             Intermix source code with disassembly\n\
    212   -s, --full-contents      Display the full contents of all sections requested\n\
    213   -g, --debugging          Display debug information in object file\n\
    214   -e, --debugging-tags     Display debug information using ctags style\n\
    215   -G, --stabs              Display (in raw form) any STABS info in the file\n\
    216   -W[lLiaprmfFsoRt] or\n\
    217   --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
    218           =frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
    219           =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,\n\
    220           =addr,=cu_index]\n\
    221                            Display DWARF info in the file\n\
    222   -t, --syms               Display the contents of the symbol table(s)\n\
    223   -T, --dynamic-syms       Display the contents of the dynamic symbol table\n\
    224   -r, --reloc              Display the relocation entries in the file\n\
    225   -R, --dynamic-reloc      Display the dynamic relocation entries in the file\n\
    226   @<file>                  Read options from <file>\n\
    227   -v, --version            Display this program's version number\n\
    228   -i, --info               List object formats and architectures supported\n\
    229   -H, --help               Display this information\n\
    230 "));
    231   if (status != 2)
    232     {
    233       const struct objdump_private_desc * const *desc;
    234 
    235       fprintf (stream, _("\n The following switches are optional:\n"));
    236       fprintf (stream, _("\
    237   -b, --target=BFDNAME           Specify the target object format as BFDNAME\n\
    238   -m, --architecture=MACHINE     Specify the target architecture as MACHINE\n\
    239   -j, --section=NAME             Only display information for section NAME\n\
    240   -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
    241   -EB --endian=big               Assume big endian format when disassembling\n\
    242   -EL --endian=little            Assume little endian format when disassembling\n\
    243       --file-start-context       Include context from start of file (with -S)\n\
    244   -I, --include=DIR              Add DIR to search list for source files\n\
    245   -l, --line-numbers             Include line numbers and filenames in output\n\
    246   -F, --file-offsets             Include file offsets when displaying information\n\
    247   -C, --demangle[=STYLE]         Decode mangled/processed symbol names\n\
    248                                   The STYLE, if specified, can be `auto', `gnu',\n\
    249                                   `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
    250                                   or `gnat'\n\
    251   -w, --wide                     Format output for more than 80 columns\n\
    252   -z, --disassemble-zeroes       Do not skip blocks of zeroes when disassembling\n\
    253       --start-address=ADDR       Only process data whose address is >= ADDR\n\
    254       --stop-address=ADDR        Only process data whose address is <= ADDR\n\
    255       --prefix-addresses         Print complete address alongside disassembly\n\
    256       --[no-]show-raw-insn       Display hex alongside symbolic disassembly\n\
    257       --insn-width=WIDTH         Display WIDTH bytes on a single line for -d\n\
    258       --adjust-vma=OFFSET        Add OFFSET to all displayed section addresses\n\
    259       --special-syms             Include special symbols in symbol dumps\n\
    260       --prefix=PREFIX            Add PREFIX to absolute paths for -S\n\
    261       --prefix-strip=LEVEL       Strip initial directory names for -S\n"));
    262       fprintf (stream, _("\
    263       --dwarf-depth=N        Do not display DIEs at depth N or greater\n\
    264       --dwarf-start=N        Display DIEs starting with N, at the same depth\n\
    265                              or deeper\n\
    266       --dwarf-check          Make additional dwarf internal consistency checks.\
    267       \n\n"));
    268       list_supported_targets (program_name, stream);
    269       list_supported_architectures (program_name, stream);
    270 
    271       disassembler_usage (stream);
    272 
    273       if (objdump_private_vectors[0] != NULL)
    274         {
    275           fprintf (stream,
    276                    _("\nOptions supported for -P/--private switch:\n"));
    277           for (desc = objdump_private_vectors; *desc != NULL; desc++)
    278             (*desc)->help (stream);
    279         }
    280     }
    281   if (REPORT_BUGS_TO[0] && status == 0)
    282     fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
    283   exit (status);
    284 }
    285 
    286 /* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
    287 enum option_values
    288   {
    289     OPTION_ENDIAN=150,
    290     OPTION_START_ADDRESS,
    291     OPTION_STOP_ADDRESS,
    292     OPTION_DWARF,
    293     OPTION_PREFIX,
    294     OPTION_PREFIX_STRIP,
    295     OPTION_INSN_WIDTH,
    296     OPTION_ADJUST_VMA,
    297     OPTION_DWARF_DEPTH,
    298     OPTION_DWARF_CHECK,
    299     OPTION_DWARF_START
    300   };
    301 
    302 static struct option long_options[]=
    303 {
    304   {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
    305   {"all-headers", no_argument, NULL, 'x'},
    306   {"private-headers", no_argument, NULL, 'p'},
    307   {"private", required_argument, NULL, 'P'},
    308   {"architecture", required_argument, NULL, 'm'},
    309   {"archive-headers", no_argument, NULL, 'a'},
    310   {"debugging", no_argument, NULL, 'g'},
    311   {"debugging-tags", no_argument, NULL, 'e'},
    312   {"demangle", optional_argument, NULL, 'C'},
    313   {"disassemble", no_argument, NULL, 'd'},
    314   {"disassemble-all", no_argument, NULL, 'D'},
    315   {"disassembler-options", required_argument, NULL, 'M'},
    316   {"disassemble-zeroes", no_argument, NULL, 'z'},
    317   {"dynamic-reloc", no_argument, NULL, 'R'},
    318   {"dynamic-syms", no_argument, NULL, 'T'},
    319   {"endian", required_argument, NULL, OPTION_ENDIAN},
    320   {"file-headers", no_argument, NULL, 'f'},
    321   {"file-offsets", no_argument, NULL, 'F'},
    322   {"file-start-context", no_argument, &file_start_context, 1},
    323   {"full-contents", no_argument, NULL, 's'},
    324   {"headers", no_argument, NULL, 'h'},
    325   {"help", no_argument, NULL, 'H'},
    326   {"info", no_argument, NULL, 'i'},
    327   {"line-numbers", no_argument, NULL, 'l'},
    328   {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
    329   {"prefix-addresses", no_argument, &prefix_addresses, 1},
    330   {"reloc", no_argument, NULL, 'r'},
    331   {"section", required_argument, NULL, 'j'},
    332   {"section-headers", no_argument, NULL, 'h'},
    333   {"show-raw-insn", no_argument, &show_raw_insn, 1},
    334   {"source", no_argument, NULL, 'S'},
    335   {"special-syms", no_argument, &dump_special_syms, 1},
    336   {"include", required_argument, NULL, 'I'},
    337   {"dwarf", optional_argument, NULL, OPTION_DWARF},
    338   {"stabs", no_argument, NULL, 'G'},
    339   {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
    340   {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
    341   {"syms", no_argument, NULL, 't'},
    342   {"target", required_argument, NULL, 'b'},
    343   {"version", no_argument, NULL, 'V'},
    344   {"wide", no_argument, NULL, 'w'},
    345   {"prefix", required_argument, NULL, OPTION_PREFIX},
    346   {"prefix-strip", required_argument, NULL, OPTION_PREFIX_STRIP},
    347   {"insn-width", required_argument, NULL, OPTION_INSN_WIDTH},
    348   {"dwarf-depth",      required_argument, 0, OPTION_DWARF_DEPTH},
    349   {"dwarf-start",      required_argument, 0, OPTION_DWARF_START},
    350   {"dwarf-check",      no_argument, 0, OPTION_DWARF_CHECK},
    351   {0, no_argument, 0, 0}
    352 };
    353 
    354 static void
    356 nonfatal (const char *msg)
    357 {
    358   bfd_nonfatal (msg);
    359   exit_status = 1;
    360 }
    361 
    362 /* Returns TRUE if the specified section should be dumped.  */
    364 
    365 static bfd_boolean
    366 process_section_p (asection * section)
    367 {
    368   struct only * only;
    369 
    370   if (only_list == NULL)
    371     return TRUE;
    372 
    373   for (only = only_list; only; only = only->next)
    374     if (strcmp (only->name, section->name) == 0)
    375       {
    376 	only->seen = TRUE;
    377 	return TRUE;
    378       }
    379 
    380   return FALSE;
    381 }
    382 
    383 /* Add an entry to the 'only' list.  */
    384 
    385 static void
    386 add_only (char * name)
    387 {
    388   struct only * only;
    389 
    390   /* First check to make sure that we do not
    391      already have an entry for this name.  */
    392   for (only = only_list; only; only = only->next)
    393     if (strcmp (only->name, name) == 0)
    394       return;
    395 
    396   only = xmalloc (sizeof * only);
    397   only->name = name;
    398   only->seen = FALSE;
    399   only->next = only_list;
    400   only_list = only;
    401 }
    402 
    403 /* Release the memory used by the 'only' list.
    404    PR 11225: Issue a warning message for unseen sections.
    405    Only do this if none of the sections were seen.  This is mainly to support
    406    tools like the GAS testsuite where an object file is dumped with a list of
    407    generic section names known to be present in a range of different file
    408    formats.  */
    409 
    410 static void
    411 free_only_list (void)
    412 {
    413   bfd_boolean at_least_one_seen = FALSE;
    414   struct only * only;
    415   struct only * next;
    416 
    417   if (only_list == NULL)
    418     return;
    419 
    420   for (only = only_list; only; only = only->next)
    421     if (only->seen)
    422       {
    423 	at_least_one_seen = TRUE;
    424 	break;
    425       }
    426 
    427   for (only = only_list; only; only = next)
    428     {
    429       if (! at_least_one_seen)
    430 	{
    431 	  non_fatal (_("section '%s' mentioned in a -j option, "
    432 		       "but not found in any input file"),
    433 		     only->name);
    434 	  exit_status = 1;
    435 	}
    436       next = only->next;
    437       free (only);
    438     }
    439 }
    440 
    441 
    442 static void
    444 dump_section_header (bfd *abfd, asection *section,
    445 		     void *ignored ATTRIBUTE_UNUSED)
    446 {
    447   char *comma = "";
    448   unsigned int opb = bfd_octets_per_byte (abfd);
    449 
    450   /* Ignore linker created section.  See elfNN_ia64_object_p in
    451      bfd/elfxx-ia64.c.  */
    452   if (section->flags & SEC_LINKER_CREATED)
    453     return;
    454 
    455   /* PR 10413: Skip sections that we are ignoring.  */
    456   if (! process_section_p (section))
    457     return;
    458 
    459   printf ("%3d %-13s %08lx  ", section->index,
    460 	  bfd_get_section_name (abfd, section),
    461 	  (unsigned long) bfd_section_size (abfd, section) / opb);
    462   bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
    463   printf ("  ");
    464   bfd_printf_vma (abfd, section->lma);
    465   printf ("  %08lx  2**%u", (unsigned long) section->filepos,
    466 	  bfd_get_section_alignment (abfd, section));
    467   if (! wide_output)
    468     printf ("\n                ");
    469   printf ("  ");
    470 
    471 #define PF(x, y) \
    472   if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
    473 
    474   PF (SEC_HAS_CONTENTS, "CONTENTS");
    475   PF (SEC_ALLOC, "ALLOC");
    476   PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
    477   PF (SEC_LOAD, "LOAD");
    478   PF (SEC_RELOC, "RELOC");
    479   PF (SEC_READONLY, "READONLY");
    480   PF (SEC_CODE, "CODE");
    481   PF (SEC_DATA, "DATA");
    482   PF (SEC_ROM, "ROM");
    483   PF (SEC_DEBUGGING, "DEBUGGING");
    484   PF (SEC_NEVER_LOAD, "NEVER_LOAD");
    485   PF (SEC_EXCLUDE, "EXCLUDE");
    486   PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
    487   if (bfd_get_arch (abfd) == bfd_arch_tic54x)
    488     {
    489       PF (SEC_TIC54X_BLOCK, "BLOCK");
    490       PF (SEC_TIC54X_CLINK, "CLINK");
    491     }
    492   PF (SEC_SMALL_DATA, "SMALL_DATA");
    493   if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
    494     PF (SEC_COFF_SHARED, "SHARED");
    495   PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
    496   PF (SEC_GROUP, "GROUP");
    497 
    498   if ((section->flags & SEC_LINK_ONCE) != 0)
    499     {
    500       const char *ls;
    501       struct coff_comdat_info *comdat;
    502 
    503       switch (section->flags & SEC_LINK_DUPLICATES)
    504 	{
    505 	default:
    506 	  abort ();
    507 	case SEC_LINK_DUPLICATES_DISCARD:
    508 	  ls = "LINK_ONCE_DISCARD";
    509 	  break;
    510 	case SEC_LINK_DUPLICATES_ONE_ONLY:
    511 	  ls = "LINK_ONCE_ONE_ONLY";
    512 	  break;
    513 	case SEC_LINK_DUPLICATES_SAME_SIZE:
    514 	  ls = "LINK_ONCE_SAME_SIZE";
    515 	  break;
    516 	case SEC_LINK_DUPLICATES_SAME_CONTENTS:
    517 	  ls = "LINK_ONCE_SAME_CONTENTS";
    518 	  break;
    519 	}
    520       printf ("%s%s", comma, ls);
    521 
    522       comdat = bfd_coff_get_comdat_section (abfd, section);
    523       if (comdat != NULL)
    524 	printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
    525 
    526       comma = ", ";
    527     }
    528 
    529   printf ("\n");
    530 #undef PF
    531 }
    532 
    533 static void
    534 dump_headers (bfd *abfd)
    535 {
    536   printf (_("Sections:\n"));
    537 
    538 #ifndef BFD64
    539   printf (_("Idx Name          Size      VMA       LMA       File off  Algn"));
    540 #else
    541   /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses.  */
    542   if (bfd_get_arch_size (abfd) == 32)
    543     printf (_("Idx Name          Size      VMA       LMA       File off  Algn"));
    544   else
    545     printf (_("Idx Name          Size      VMA               LMA               File off  Algn"));
    546 #endif
    547 
    548   if (wide_output)
    549     printf (_("  Flags"));
    550   printf ("\n");
    551 
    552   bfd_map_over_sections (abfd, dump_section_header, NULL);
    553 }
    554 
    555 static asymbol **
    557 slurp_symtab (bfd *abfd)
    558 {
    559   asymbol **sy = NULL;
    560   long storage;
    561 
    562   if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
    563     {
    564       symcount = 0;
    565       return NULL;
    566     }
    567 
    568   storage = bfd_get_symtab_upper_bound (abfd);
    569   if (storage < 0)
    570     {
    571       non_fatal (_("failed to read symbol table from: %s"), bfd_get_filename (abfd));
    572       bfd_fatal (_("error message was"));
    573     }
    574   if (storage)
    575     sy = (asymbol **) xmalloc (storage);
    576 
    577   symcount = bfd_canonicalize_symtab (abfd, sy);
    578   if (symcount < 0)
    579     bfd_fatal (bfd_get_filename (abfd));
    580   return sy;
    581 }
    582 
    583 /* Read in the dynamic symbols.  */
    584 
    585 static asymbol **
    586 slurp_dynamic_symtab (bfd *abfd)
    587 {
    588   asymbol **sy = NULL;
    589   long storage;
    590 
    591   storage = bfd_get_dynamic_symtab_upper_bound (abfd);
    592   if (storage < 0)
    593     {
    594       if (!(bfd_get_file_flags (abfd) & DYNAMIC))
    595 	{
    596 	  non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
    597 	  exit_status = 1;
    598 	  dynsymcount = 0;
    599 	  return NULL;
    600 	}
    601 
    602       bfd_fatal (bfd_get_filename (abfd));
    603     }
    604   if (storage)
    605     sy = (asymbol **) xmalloc (storage);
    606 
    607   dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
    608   if (dynsymcount < 0)
    609     bfd_fatal (bfd_get_filename (abfd));
    610   return sy;
    611 }
    612 
    613 /* Filter out (in place) symbols that are useless for disassembly.
    614    COUNT is the number of elements in SYMBOLS.
    615    Return the number of useful symbols.  */
    616 
    617 static long
    618 remove_useless_symbols (asymbol **symbols, long count)
    619 {
    620   asymbol **in_ptr = symbols, **out_ptr = symbols;
    621 
    622   while (--count >= 0)
    623     {
    624       asymbol *sym = *in_ptr++;
    625 
    626       if (sym->name == NULL || sym->name[0] == '\0')
    627 	continue;
    628       if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
    629 	continue;
    630       if (bfd_is_und_section (sym->section)
    631 	  || bfd_is_com_section (sym->section))
    632 	continue;
    633 
    634       *out_ptr++ = sym;
    635     }
    636   return out_ptr - symbols;
    637 }
    638 
    639 /* Sort symbols into value order.  */
    640 
    641 static int
    642 compare_symbols (const void *ap, const void *bp)
    643 {
    644   const asymbol *a = * (const asymbol **) ap;
    645   const asymbol *b = * (const asymbol **) bp;
    646   const char *an;
    647   const char *bn;
    648   size_t anl;
    649   size_t bnl;
    650   bfd_boolean af;
    651   bfd_boolean bf;
    652   flagword aflags;
    653   flagword bflags;
    654 
    655   if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
    656     return 1;
    657   else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
    658     return -1;
    659 
    660   if (a->section > b->section)
    661     return 1;
    662   else if (a->section < b->section)
    663     return -1;
    664 
    665   an = bfd_asymbol_name (a);
    666   bn = bfd_asymbol_name (b);
    667   anl = strlen (an);
    668   bnl = strlen (bn);
    669 
    670   /* The symbols gnu_compiled and gcc2_compiled convey no real
    671      information, so put them after other symbols with the same value.  */
    672   af = (strstr (an, "gnu_compiled") != NULL
    673 	|| strstr (an, "gcc2_compiled") != NULL);
    674   bf = (strstr (bn, "gnu_compiled") != NULL
    675 	|| strstr (bn, "gcc2_compiled") != NULL);
    676 
    677   if (af && ! bf)
    678     return 1;
    679   if (! af && bf)
    680     return -1;
    681 
    682   /* We use a heuristic for the file name, to try to sort it after
    683      more useful symbols.  It may not work on non Unix systems, but it
    684      doesn't really matter; the only difference is precisely which
    685      symbol names get printed.  */
    686 
    687 #define file_symbol(s, sn, snl)			\
    688   (((s)->flags & BSF_FILE) != 0			\
    689    || ((sn)[(snl) - 2] == '.'			\
    690        && ((sn)[(snl) - 1] == 'o'		\
    691 	   || (sn)[(snl) - 1] == 'a')))
    692 
    693   af = file_symbol (a, an, anl);
    694   bf = file_symbol (b, bn, bnl);
    695 
    696   if (af && ! bf)
    697     return 1;
    698   if (! af && bf)
    699     return -1;
    700 
    701   /* Try to sort global symbols before local symbols before function
    702      symbols before debugging symbols.  */
    703 
    704   aflags = a->flags;
    705   bflags = b->flags;
    706 
    707   if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
    708     {
    709       if ((aflags & BSF_DEBUGGING) != 0)
    710 	return 1;
    711       else
    712 	return -1;
    713     }
    714   if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
    715     {
    716       if ((aflags & BSF_FUNCTION) != 0)
    717 	return -1;
    718       else
    719 	return 1;
    720     }
    721   if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
    722     {
    723       if ((aflags & BSF_LOCAL) != 0)
    724 	return 1;
    725       else
    726 	return -1;
    727     }
    728   if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
    729     {
    730       if ((aflags & BSF_GLOBAL) != 0)
    731 	return -1;
    732       else
    733 	return 1;
    734     }
    735 
    736   /* Symbols that start with '.' might be section names, so sort them
    737      after symbols that don't start with '.'.  */
    738   if (an[0] == '.' && bn[0] != '.')
    739     return 1;
    740   if (an[0] != '.' && bn[0] == '.')
    741     return -1;
    742 
    743   /* Finally, if we can't distinguish them in any other way, try to
    744      get consistent results by sorting the symbols by name.  */
    745   return strcmp (an, bn);
    746 }
    747 
    748 /* Sort relocs into address order.  */
    749 
    750 static int
    751 compare_relocs (const void *ap, const void *bp)
    752 {
    753   const arelent *a = * (const arelent **) ap;
    754   const arelent *b = * (const arelent **) bp;
    755 
    756   if (a->address > b->address)
    757     return 1;
    758   else if (a->address < b->address)
    759     return -1;
    760 
    761   /* So that associated relocations tied to the same address show up
    762      in the correct order, we don't do any further sorting.  */
    763   if (a > b)
    764     return 1;
    765   else if (a < b)
    766     return -1;
    767   else
    768     return 0;
    769 }
    770 
    771 /* Print an address (VMA) to the output stream in INFO.
    772    If SKIP_ZEROES is TRUE, omit leading zeroes.  */
    773 
    774 static void
    775 objdump_print_value (bfd_vma vma, struct disassemble_info *inf,
    776 		     bfd_boolean skip_zeroes)
    777 {
    778   char buf[30];
    779   char *p;
    780   struct objdump_disasm_info *aux;
    781 
    782   aux = (struct objdump_disasm_info *) inf->application_data;
    783   bfd_sprintf_vma (aux->abfd, buf, vma);
    784   if (! skip_zeroes)
    785     p = buf;
    786   else
    787     {
    788       for (p = buf; *p == '0'; ++p)
    789 	;
    790       if (*p == '\0')
    791 	--p;
    792     }
    793   (*inf->fprintf_func) (inf->stream, "%s", p);
    794 }
    795 
    796 /* Print the name of a symbol.  */
    797 
    798 static void
    799 objdump_print_symname (bfd *abfd, struct disassemble_info *inf,
    800 		       asymbol *sym)
    801 {
    802   char *alloc;
    803   const char *name;
    804 
    805   alloc = NULL;
    806   name = bfd_asymbol_name (sym);
    807   if (do_demangle && name[0] != '\0')
    808     {
    809       /* Demangle the name.  */
    810       alloc = bfd_demangle (abfd, name, DMGL_ANSI | DMGL_PARAMS);
    811       if (alloc != NULL)
    812 	name = alloc;
    813     }
    814 
    815   if (inf != NULL)
    816     (*inf->fprintf_func) (inf->stream, "%s", name);
    817   else
    818     printf ("%s", name);
    819 
    820   if (alloc != NULL)
    821     free (alloc);
    822 }
    823 
    824 /* Locate a symbol given a bfd and a section (from INFO->application_data),
    825    and a VMA.  If INFO->application_data->require_sec is TRUE, then always
    826    require the symbol to be in the section.  Returns NULL if there is no
    827    suitable symbol.  If PLACE is not NULL, then *PLACE is set to the index
    828    of the symbol in sorted_syms.  */
    829 
    830 static asymbol *
    831 find_symbol_for_address (bfd_vma vma,
    832 			 struct disassemble_info *inf,
    833 			 long *place)
    834 {
    835   /* @@ Would it speed things up to cache the last two symbols returned,
    836      and maybe their address ranges?  For many processors, only one memory
    837      operand can be present at a time, so the 2-entry cache wouldn't be
    838      constantly churned by code doing heavy memory accesses.  */
    839 
    840   /* Indices in `sorted_syms'.  */
    841   long min = 0;
    842   long max_count = sorted_symcount;
    843   long thisplace;
    844   struct objdump_disasm_info *aux;
    845   bfd *abfd;
    846   asection *sec;
    847   unsigned int opb;
    848   bfd_boolean want_section;
    849 
    850   if (sorted_symcount < 1)
    851     return NULL;
    852 
    853   aux = (struct objdump_disasm_info *) inf->application_data;
    854   abfd = aux->abfd;
    855   sec = aux->sec;
    856   opb = inf->octets_per_byte;
    857 
    858   /* Perform a binary search looking for the closest symbol to the
    859      required value.  We are searching the range (min, max_count].  */
    860   while (min + 1 < max_count)
    861     {
    862       asymbol *sym;
    863 
    864       thisplace = (max_count + min) / 2;
    865       sym = sorted_syms[thisplace];
    866 
    867       if (bfd_asymbol_value (sym) > vma)
    868 	max_count = thisplace;
    869       else if (bfd_asymbol_value (sym) < vma)
    870 	min = thisplace;
    871       else
    872 	{
    873 	  min = thisplace;
    874 	  break;
    875 	}
    876     }
    877 
    878   /* The symbol we want is now in min, the low end of the range we
    879      were searching.  If there are several symbols with the same
    880      value, we want the first one.  */
    881   thisplace = min;
    882   while (thisplace > 0
    883 	 && (bfd_asymbol_value (sorted_syms[thisplace])
    884 	     == bfd_asymbol_value (sorted_syms[thisplace - 1])))
    885     --thisplace;
    886 
    887   /* Prefer a symbol in the current section if we have multple symbols
    888      with the same value, as can occur with overlays or zero size
    889      sections.  */
    890   min = thisplace;
    891   while (min < max_count
    892 	 && (bfd_asymbol_value (sorted_syms[min])
    893 	     == bfd_asymbol_value (sorted_syms[thisplace])))
    894     {
    895       if (sorted_syms[min]->section == sec
    896 	  && inf->symbol_is_valid (sorted_syms[min], inf))
    897 	{
    898 	  thisplace = min;
    899 
    900 	  if (place != NULL)
    901 	    *place = thisplace;
    902 
    903 	  return sorted_syms[thisplace];
    904 	}
    905       ++min;
    906     }
    907 
    908   /* If the file is relocatable, and the symbol could be from this
    909      section, prefer a symbol from this section over symbols from
    910      others, even if the other symbol's value might be closer.
    911 
    912      Note that this may be wrong for some symbol references if the
    913      sections have overlapping memory ranges, but in that case there's
    914      no way to tell what's desired without looking at the relocation
    915      table.
    916 
    917      Also give the target a chance to reject symbols.  */
    918   want_section = (aux->require_sec
    919 		  || ((abfd->flags & HAS_RELOC) != 0
    920 		      && vma >= bfd_get_section_vma (abfd, sec)
    921 		      && vma < (bfd_get_section_vma (abfd, sec)
    922 				+ bfd_section_size (abfd, sec) / opb)));
    923   if ((sorted_syms[thisplace]->section != sec && want_section)
    924       || ! inf->symbol_is_valid (sorted_syms[thisplace], inf))
    925     {
    926       long i;
    927       long newplace = sorted_symcount;
    928 
    929       for (i = min - 1; i >= 0; i--)
    930 	{
    931 	  if ((sorted_syms[i]->section == sec || !want_section)
    932 	      && inf->symbol_is_valid (sorted_syms[i], inf))
    933 	    {
    934 	      if (newplace == sorted_symcount)
    935 		newplace = i;
    936 
    937 	      if (bfd_asymbol_value (sorted_syms[i])
    938 		  != bfd_asymbol_value (sorted_syms[newplace]))
    939 		break;
    940 
    941 	      /* Remember this symbol and keep searching until we reach
    942 		 an earlier address.  */
    943 	      newplace = i;
    944 	    }
    945 	}
    946 
    947       if (newplace != sorted_symcount)
    948 	thisplace = newplace;
    949       else
    950 	{
    951 	  /* We didn't find a good symbol with a smaller value.
    952 	     Look for one with a larger value.  */
    953 	  for (i = thisplace + 1; i < sorted_symcount; i++)
    954 	    {
    955 	      if ((sorted_syms[i]->section == sec || !want_section)
    956 		  && inf->symbol_is_valid (sorted_syms[i], inf))
    957 		{
    958 		  thisplace = i;
    959 		  break;
    960 		}
    961 	    }
    962 	}
    963 
    964       if ((sorted_syms[thisplace]->section != sec && want_section)
    965 	  || ! inf->symbol_is_valid (sorted_syms[thisplace], inf))
    966 	/* There is no suitable symbol.  */
    967 	return NULL;
    968     }
    969 
    970   if (place != NULL)
    971     *place = thisplace;
    972 
    973   return sorted_syms[thisplace];
    974 }
    975 
    976 /* Print an address and the offset to the nearest symbol.  */
    977 
    978 static void
    979 objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
    980 			     bfd_vma vma, struct disassemble_info *inf,
    981 			     bfd_boolean skip_zeroes)
    982 {
    983   objdump_print_value (vma, inf, skip_zeroes);
    984 
    985   if (sym == NULL)
    986     {
    987       bfd_vma secaddr;
    988 
    989       (*inf->fprintf_func) (inf->stream, " <%s",
    990 			    bfd_get_section_name (abfd, sec));
    991       secaddr = bfd_get_section_vma (abfd, sec);
    992       if (vma < secaddr)
    993 	{
    994 	  (*inf->fprintf_func) (inf->stream, "-0x");
    995 	  objdump_print_value (secaddr - vma, inf, TRUE);
    996 	}
    997       else if (vma > secaddr)
    998 	{
    999 	  (*inf->fprintf_func) (inf->stream, "+0x");
   1000 	  objdump_print_value (vma - secaddr, inf, TRUE);
   1001 	}
   1002       (*inf->fprintf_func) (inf->stream, ">");
   1003     }
   1004   else
   1005     {
   1006       (*inf->fprintf_func) (inf->stream, " <");
   1007       objdump_print_symname (abfd, inf, sym);
   1008       if (bfd_asymbol_value (sym) > vma)
   1009 	{
   1010 	  (*inf->fprintf_func) (inf->stream, "-0x");
   1011 	  objdump_print_value (bfd_asymbol_value (sym) - vma, inf, TRUE);
   1012 	}
   1013       else if (vma > bfd_asymbol_value (sym))
   1014 	{
   1015 	  (*inf->fprintf_func) (inf->stream, "+0x");
   1016 	  objdump_print_value (vma - bfd_asymbol_value (sym), inf, TRUE);
   1017 	}
   1018       (*inf->fprintf_func) (inf->stream, ">");
   1019     }
   1020 
   1021   if (display_file_offsets)
   1022     inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
   1023 			(long int)(sec->filepos + (vma - sec->vma)));
   1024 }
   1025 
   1026 /* Print an address (VMA), symbolically if possible.
   1027    If SKIP_ZEROES is TRUE, don't output leading zeroes.  */
   1028 
   1029 static void
   1030 objdump_print_addr (bfd_vma vma,
   1031 		    struct disassemble_info *inf,
   1032 		    bfd_boolean skip_zeroes)
   1033 {
   1034   struct objdump_disasm_info *aux;
   1035   asymbol *sym = NULL;
   1036   bfd_boolean skip_find = FALSE;
   1037 
   1038   aux = (struct objdump_disasm_info *) inf->application_data;
   1039 
   1040   if (sorted_symcount < 1)
   1041     {
   1042       (*inf->fprintf_func) (inf->stream, "0x");
   1043       objdump_print_value (vma, inf, skip_zeroes);
   1044 
   1045       if (display_file_offsets)
   1046 	inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
   1047 			   (long int)(aux->sec->filepos + (vma - aux->sec->vma)));
   1048       return;
   1049     }
   1050 
   1051   if (aux->reloc != NULL
   1052       && aux->reloc->sym_ptr_ptr != NULL
   1053       && * aux->reloc->sym_ptr_ptr != NULL)
   1054     {
   1055       sym = * aux->reloc->sym_ptr_ptr;
   1056 
   1057       /* Adjust the vma to the reloc.  */
   1058       vma += bfd_asymbol_value (sym);
   1059 
   1060       if (bfd_is_und_section (bfd_get_section (sym)))
   1061 	skip_find = TRUE;
   1062     }
   1063 
   1064   if (!skip_find)
   1065     sym = find_symbol_for_address (vma, inf, NULL);
   1066 
   1067   objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, inf,
   1068 			       skip_zeroes);
   1069 }
   1070 
   1071 /* Print VMA to INFO.  This function is passed to the disassembler
   1072    routine.  */
   1073 
   1074 static void
   1075 objdump_print_address (bfd_vma vma, struct disassemble_info *inf)
   1076 {
   1077   objdump_print_addr (vma, inf, ! prefix_addresses);
   1078 }
   1079 
   1080 /* Determine if the given address has a symbol associated with it.  */
   1081 
   1082 static int
   1083 objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * inf)
   1084 {
   1085   asymbol * sym;
   1086 
   1087   sym = find_symbol_for_address (vma, inf, NULL);
   1088 
   1089   return (sym != NULL && (bfd_asymbol_value (sym) == vma));
   1090 }
   1091 
   1092 /* Hold the last function name and the last line number we displayed
   1093    in a disassembly.  */
   1094 
   1095 static char *prev_functionname;
   1096 static unsigned int prev_line;
   1097 static unsigned int prev_discriminator;
   1098 
   1099 /* We keep a list of all files that we have seen when doing a
   1100    disassembly with source, so that we know how much of the file to
   1101    display.  This can be important for inlined functions.  */
   1102 
   1103 struct print_file_list
   1104 {
   1105   struct print_file_list *next;
   1106   const char *filename;
   1107   const char *modname;
   1108   const char *map;
   1109   size_t mapsize;
   1110   const char **linemap;
   1111   unsigned maxline;
   1112   unsigned last_line;
   1113   int first;
   1114 };
   1115 
   1116 static struct print_file_list *print_files;
   1117 
   1118 /* The number of preceding context lines to show when we start
   1119    displaying a file for the first time.  */
   1120 
   1121 #define SHOW_PRECEDING_CONTEXT_LINES (5)
   1122 
   1123 /* Read a complete file into memory.  */
   1124 
   1125 static const char *
   1126 slurp_file (const char *fn, size_t *size)
   1127 {
   1128 #ifdef HAVE_MMAP
   1129   int ps = getpagesize ();
   1130   size_t msize;
   1131 #endif
   1132   const char *map;
   1133   struct stat st;
   1134   int fd = open (fn, O_RDONLY | O_BINARY);
   1135 
   1136   if (fd < 0)
   1137     return NULL;
   1138   if (fstat (fd, &st) < 0)
   1139     {
   1140       close (fd);
   1141       return NULL;
   1142     }
   1143   *size = st.st_size;
   1144 #ifdef HAVE_MMAP
   1145   msize = (*size + ps - 1) & ~(ps - 1);
   1146   map = mmap (NULL, msize, PROT_READ, MAP_SHARED, fd, 0);
   1147   if (map != (char *) -1L)
   1148     {
   1149       close (fd);
   1150       return map;
   1151     }
   1152 #endif
   1153   map = (const char *) malloc (*size);
   1154   if (!map || (size_t) read (fd, (char *) map, *size) != *size)
   1155     {
   1156       free ((void *) map);
   1157       map = NULL;
   1158     }
   1159   close (fd);
   1160   return map;
   1161 }
   1162 
   1163 #define line_map_decrease 5
   1164 
   1165 /* Precompute array of lines for a mapped file. */
   1166 
   1167 static const char **
   1168 index_file (const char *map, size_t size, unsigned int *maxline)
   1169 {
   1170   const char *p, *lstart, *end;
   1171   int chars_per_line = 45; /* First iteration will use 40.  */
   1172   unsigned int lineno;
   1173   const char **linemap = NULL;
   1174   unsigned long line_map_size = 0;
   1175 
   1176   lineno = 0;
   1177   lstart = map;
   1178   end = map + size;
   1179 
   1180   for (p = map; p < end; p++)
   1181     {
   1182       if (*p == '\n')
   1183 	{
   1184 	  if (p + 1 < end && p[1] == '\r')
   1185 	    p++;
   1186 	}
   1187       else if (*p == '\r')
   1188 	{
   1189 	  if (p + 1 < end && p[1] == '\n')
   1190 	    p++;
   1191 	}
   1192       else
   1193 	continue;
   1194 
   1195       /* End of line found.  */
   1196 
   1197       if (linemap == NULL || line_map_size < lineno + 1)
   1198 	{
   1199 	  unsigned long newsize;
   1200 
   1201 	  chars_per_line -= line_map_decrease;
   1202 	  if (chars_per_line <= 1)
   1203 	    chars_per_line = 1;
   1204 	  line_map_size = size / chars_per_line + 1;
   1205 	  if (line_map_size < lineno + 1)
   1206 	    line_map_size = lineno + 1;
   1207 	  newsize = line_map_size * sizeof (char *);
   1208 	  linemap = (const char **) xrealloc (linemap, newsize);
   1209 	}
   1210 
   1211       linemap[lineno++] = lstart;
   1212       lstart = p + 1;
   1213     }
   1214 
   1215   *maxline = lineno;
   1216   return linemap;
   1217 }
   1218 
   1219 /* Tries to open MODNAME, and if successful adds a node to print_files
   1220    linked list and returns that node.  Returns NULL on failure.  */
   1221 
   1222 static struct print_file_list *
   1223 try_print_file_open (const char *origname, const char *modname)
   1224 {
   1225   struct print_file_list *p;
   1226 
   1227   p = (struct print_file_list *) xmalloc (sizeof (struct print_file_list));
   1228 
   1229   p->map = slurp_file (modname, &p->mapsize);
   1230   if (p->map == NULL)
   1231     {
   1232       free (p);
   1233       return NULL;
   1234     }
   1235 
   1236   p->linemap = index_file (p->map, p->mapsize, &p->maxline);
   1237   p->last_line = 0;
   1238   p->filename = origname;
   1239   p->modname = modname;
   1240   p->next = print_files;
   1241   p->first = 1;
   1242   print_files = p;
   1243   return p;
   1244 }
   1245 
   1246 /* If the source file, as described in the symtab, is not found
   1247    try to locate it in one of the paths specified with -I
   1248    If found, add location to print_files linked list.  */
   1249 
   1250 static struct print_file_list *
   1251 update_source_path (const char *filename)
   1252 {
   1253   struct print_file_list *p;
   1254   const char *fname;
   1255   int i;
   1256 
   1257   p = try_print_file_open (filename, filename);
   1258   if (p != NULL)
   1259     return p;
   1260 
   1261   if (include_path_count == 0)
   1262     return NULL;
   1263 
   1264   /* Get the name of the file.  */
   1265   fname = lbasename (filename);
   1266 
   1267   /* If file exists under a new path, we need to add it to the list
   1268      so that show_line knows about it.  */
   1269   for (i = 0; i < include_path_count; i++)
   1270     {
   1271       char *modname = concat (include_paths[i], "/", fname, (const char *) 0);
   1272 
   1273       p = try_print_file_open (filename, modname);
   1274       if (p)
   1275 	return p;
   1276 
   1277       free (modname);
   1278     }
   1279 
   1280   return NULL;
   1281 }
   1282 
   1283 /* Print a source file line.  */
   1284 
   1285 static void
   1286 print_line (struct print_file_list *p, unsigned int linenum)
   1287 {
   1288   const char *l;
   1289   size_t len;
   1290 
   1291   --linenum;
   1292   if (linenum >= p->maxline)
   1293     return;
   1294   l = p->linemap [linenum];
   1295   /* Test fwrite return value to quiet glibc warning.  */
   1296   len = strcspn (l, "\n\r");
   1297   if (len == 0 || fwrite (l, len, 1, stdout) == 1)
   1298     putchar ('\n');
   1299 }
   1300 
   1301 /* Print a range of source code lines. */
   1302 
   1303 static void
   1304 dump_lines (struct print_file_list *p, unsigned int start, unsigned int end)
   1305 {
   1306   if (p->map == NULL)
   1307     return;
   1308   while (start <= end)
   1309     {
   1310       print_line (p, start);
   1311       start++;
   1312     }
   1313 }
   1314 
   1315 /* Show the line number, or the source line, in a disassembly
   1316    listing.  */
   1317 
   1318 static void
   1319 show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
   1320 {
   1321   const char *filename;
   1322   const char *functionname;
   1323   unsigned int linenumber;
   1324   unsigned int discriminator;
   1325   bfd_boolean reloc;
   1326 
   1327   if (! with_line_numbers && ! with_source_code)
   1328     return;
   1329 
   1330   if (! bfd_find_nearest_line_discriminator (abfd, section, syms, addr_offset,
   1331                                              &filename, &functionname,
   1332                                              &linenumber, &discriminator))
   1333     return;
   1334 
   1335   if (filename != NULL && *filename == '\0')
   1336     filename = NULL;
   1337   if (functionname != NULL && *functionname == '\0')
   1338     functionname = NULL;
   1339 
   1340   if (filename
   1341       && IS_ABSOLUTE_PATH (filename)
   1342       && prefix)
   1343     {
   1344       char *path_up;
   1345       const char *fname = filename;
   1346       char *path = (char *) alloca (prefix_length + PATH_MAX + 1);
   1347 
   1348       if (prefix_length)
   1349 	memcpy (path, prefix, prefix_length);
   1350       path_up = path + prefix_length;
   1351 
   1352       /* Build relocated filename, stripping off leading directories
   1353 	 from the initial filename if requested. */
   1354       if (prefix_strip > 0)
   1355 	{
   1356 	  int level = 0;
   1357 	  const char *s;
   1358 
   1359 	  /* Skip selected directory levels. */
   1360 	  for (s = fname + 1; *s != '\0' && level < prefix_strip; s++)
   1361 	    if (IS_DIR_SEPARATOR(*s))
   1362 	      {
   1363 		fname = s;
   1364 		level++;
   1365 	      }
   1366 	}
   1367 
   1368       /* Update complete filename. */
   1369       strncpy (path_up, fname, PATH_MAX);
   1370       path_up[PATH_MAX] = '\0';
   1371 
   1372       filename = path;
   1373       reloc = TRUE;
   1374     }
   1375   else
   1376     reloc = FALSE;
   1377 
   1378   if (with_line_numbers)
   1379     {
   1380       if (functionname != NULL
   1381 	  && (prev_functionname == NULL
   1382 	      || strcmp (functionname, prev_functionname) != 0))
   1383 	printf ("%s():\n", functionname);
   1384       if (linenumber > 0 && (linenumber != prev_line ||
   1385                              (discriminator != prev_discriminator)))
   1386         {
   1387           if (discriminator > 0)
   1388             printf ("%s:%u (discriminator %u)\n", filename == NULL ? "???" : filename,
   1389                     linenumber, discriminator);
   1390           else
   1391             printf ("%s:%u\n", filename == NULL ? "???" : filename, linenumber);
   1392         }
   1393     }
   1394 
   1395   if (with_source_code
   1396       && filename != NULL
   1397       && linenumber > 0)
   1398     {
   1399       struct print_file_list **pp, *p;
   1400       unsigned l;
   1401 
   1402       for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
   1403 	if (filename_cmp ((*pp)->filename, filename) == 0)
   1404 	  break;
   1405       p = *pp;
   1406 
   1407       if (p == NULL)
   1408 	{
   1409 	  if (reloc)
   1410 	    filename = xstrdup (filename);
   1411 	  p = update_source_path (filename);
   1412 	}
   1413 
   1414       if (p != NULL && linenumber != p->last_line)
   1415 	{
   1416 	  if (file_start_context && p->first)
   1417 	    l = 1;
   1418 	  else
   1419 	    {
   1420 	      l = linenumber - SHOW_PRECEDING_CONTEXT_LINES;
   1421 	      if (l >= linenumber)
   1422 		l = 1;
   1423 	      if (p->last_line >= l && p->last_line <= linenumber)
   1424 		l = p->last_line + 1;
   1425 	    }
   1426 	  dump_lines (p, l, linenumber);
   1427 	  p->last_line = linenumber;
   1428 	  p->first = 0;
   1429 	}
   1430     }
   1431 
   1432   if (functionname != NULL
   1433       && (prev_functionname == NULL
   1434 	  || strcmp (functionname, prev_functionname) != 0))
   1435     {
   1436       if (prev_functionname != NULL)
   1437 	free (prev_functionname);
   1438       prev_functionname = (char *) xmalloc (strlen (functionname) + 1);
   1439       strcpy (prev_functionname, functionname);
   1440     }
   1441 
   1442   if (linenumber > 0 && linenumber != prev_line)
   1443     prev_line = linenumber;
   1444 
   1445   if (discriminator != prev_discriminator)
   1446     prev_discriminator = discriminator;
   1447 }
   1448 
   1449 /* Pseudo FILE object for strings.  */
   1450 typedef struct
   1451 {
   1452   char *buffer;
   1453   size_t pos;
   1454   size_t alloc;
   1455 } SFILE;
   1456 
   1457 /* sprintf to a "stream".  */
   1458 
   1459 static int ATTRIBUTE_PRINTF_2
   1460 objdump_sprintf (SFILE *f, const char *format, ...)
   1461 {
   1462   size_t n;
   1463   va_list args;
   1464 
   1465   while (1)
   1466     {
   1467       size_t space = f->alloc - f->pos;
   1468 
   1469       va_start (args, format);
   1470       n = vsnprintf (f->buffer + f->pos, space, format, args);
   1471       va_end (args);
   1472 
   1473       if (space > n)
   1474 	break;
   1475 
   1476       f->alloc = (f->alloc + n) * 2;
   1477       f->buffer = (char *) xrealloc (f->buffer, f->alloc);
   1478     }
   1479   f->pos += n;
   1480 
   1481   return n;
   1482 }
   1483 
   1484 /* The number of zeroes we want to see before we start skipping them.
   1485    The number is arbitrarily chosen.  */
   1486 
   1487 #define DEFAULT_SKIP_ZEROES 8
   1488 
   1489 /* The number of zeroes to skip at the end of a section.  If the
   1490    number of zeroes at the end is between SKIP_ZEROES_AT_END and
   1491    SKIP_ZEROES, they will be disassembled.  If there are fewer than
   1492    SKIP_ZEROES_AT_END, they will be skipped.  This is a heuristic
   1493    attempt to avoid disassembling zeroes inserted by section
   1494    alignment.  */
   1495 
   1496 #define DEFAULT_SKIP_ZEROES_AT_END 3
   1497 
   1498 /* Disassemble some data in memory between given values.  */
   1499 
   1500 static void
   1501 disassemble_bytes (struct disassemble_info * inf,
   1502 		   disassembler_ftype        disassemble_fn,
   1503 		   bfd_boolean               insns,
   1504 		   bfd_byte *                data,
   1505 		   bfd_vma                   start_offset,
   1506 		   bfd_vma                   stop_offset,
   1507 		   bfd_vma		     rel_offset,
   1508 		   arelent ***               relppp,
   1509 		   arelent **                relppend)
   1510 {
   1511   struct objdump_disasm_info *aux;
   1512   asection *section;
   1513   int octets_per_line;
   1514   int skip_addr_chars;
   1515   bfd_vma addr_offset;
   1516   unsigned int opb = inf->octets_per_byte;
   1517   unsigned int skip_zeroes = inf->skip_zeroes;
   1518   unsigned int skip_zeroes_at_end = inf->skip_zeroes_at_end;
   1519   int octets = opb;
   1520   SFILE sfile;
   1521 
   1522   aux = (struct objdump_disasm_info *) inf->application_data;
   1523   section = aux->sec;
   1524 
   1525   sfile.alloc = 120;
   1526   sfile.buffer = (char *) xmalloc (sfile.alloc);
   1527   sfile.pos = 0;
   1528 
   1529   if (insn_width)
   1530     octets_per_line = insn_width;
   1531   else if (insns)
   1532     octets_per_line = 4;
   1533   else
   1534     octets_per_line = 16;
   1535 
   1536   /* Figure out how many characters to skip at the start of an
   1537      address, to make the disassembly look nicer.  We discard leading
   1538      zeroes in chunks of 4, ensuring that there is always a leading
   1539      zero remaining.  */
   1540   skip_addr_chars = 0;
   1541   if (! prefix_addresses)
   1542     {
   1543       char buf[30];
   1544 
   1545       bfd_sprintf_vma (aux->abfd, buf, section->vma + section->size / opb);
   1546 
   1547       while (buf[skip_addr_chars] == '0')
   1548 	++skip_addr_chars;
   1549 
   1550       /* Don't discard zeros on overflow.  */
   1551       if (buf[skip_addr_chars] == '\0' && section->vma != 0)
   1552 	skip_addr_chars = 0;
   1553 
   1554       if (skip_addr_chars != 0)
   1555 	skip_addr_chars = (skip_addr_chars - 1) & -4;
   1556     }
   1557 
   1558   inf->insn_info_valid = 0;
   1559 
   1560   addr_offset = start_offset;
   1561   while (addr_offset < stop_offset)
   1562     {
   1563       bfd_vma z;
   1564       bfd_boolean need_nl = FALSE;
   1565       int previous_octets;
   1566 
   1567       /* Remember the length of the previous instruction.  */
   1568       previous_octets = octets;
   1569       octets = 0;
   1570 
   1571       /* Make sure we don't use relocs from previous instructions.  */
   1572       aux->reloc = NULL;
   1573 
   1574       /* If we see more than SKIP_ZEROES octets of zeroes, we just
   1575 	 print `...'.  */
   1576       for (z = addr_offset * opb; z < stop_offset * opb; z++)
   1577 	if (data[z] != 0)
   1578 	  break;
   1579       if (! disassemble_zeroes
   1580 	  && (inf->insn_info_valid == 0
   1581 	      || inf->branch_delay_insns == 0)
   1582 	  && (z - addr_offset * opb >= skip_zeroes
   1583 	      || (z == stop_offset * opb &&
   1584 		  z - addr_offset * opb < skip_zeroes_at_end)))
   1585 	{
   1586 	  /* If there are more nonzero octets to follow, we only skip
   1587 	     zeroes in multiples of 4, to try to avoid running over
   1588 	     the start of an instruction which happens to start with
   1589 	     zero.  */
   1590 	  if (z != stop_offset * opb)
   1591 	    z = addr_offset * opb + ((z - addr_offset * opb) &~ 3);
   1592 
   1593 	  octets = z - addr_offset * opb;
   1594 
   1595 	  /* If we are going to display more data, and we are displaying
   1596 	     file offsets, then tell the user how many zeroes we skip
   1597 	     and the file offset from where we resume dumping.  */
   1598 	  if (display_file_offsets && ((addr_offset + (octets / opb)) < stop_offset))
   1599 	    printf ("\t... (skipping %d zeroes, resuming at file offset: 0x%lx)\n",
   1600 		    octets / opb,
   1601 		    (unsigned long) (section->filepos
   1602 				     + (addr_offset + (octets / opb))));
   1603 	  else
   1604 	    printf ("\t...\n");
   1605 	}
   1606       else
   1607 	{
   1608 	  char buf[50];
   1609 	  int bpc = 0;
   1610 	  int pb = 0;
   1611 
   1612 	  if (with_line_numbers || with_source_code)
   1613 	    show_line (aux->abfd, section, addr_offset);
   1614 
   1615 	  if (! prefix_addresses)
   1616 	    {
   1617 	      char *s;
   1618 
   1619 	      bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
   1620 	      for (s = buf + skip_addr_chars; *s == '0'; s++)
   1621 		*s = ' ';
   1622 	      if (*s == '\0')
   1623 		*--s = '0';
   1624 	      printf ("%s:\t", buf + skip_addr_chars);
   1625 	    }
   1626 	  else
   1627 	    {
   1628 	      aux->require_sec = TRUE;
   1629 	      objdump_print_address (section->vma + addr_offset, inf);
   1630 	      aux->require_sec = FALSE;
   1631 	      putchar (' ');
   1632 	    }
   1633 
   1634 	  if (insns)
   1635 	    {
   1636 	      sfile.pos = 0;
   1637 	      inf->fprintf_func = (fprintf_ftype) objdump_sprintf;
   1638 	      inf->stream = &sfile;
   1639 	      inf->bytes_per_line = 0;
   1640 	      inf->bytes_per_chunk = 0;
   1641 	      inf->flags = disassemble_all ? DISASSEMBLE_DATA : 0;
   1642 	      if (machine)
   1643 		inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
   1644 
   1645 	      if (inf->disassembler_needs_relocs
   1646 		  && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
   1647 		  && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
   1648 		  && *relppp < relppend)
   1649 		{
   1650 		  bfd_signed_vma distance_to_rel;
   1651 
   1652 		  distance_to_rel = (**relppp)->address
   1653 		    - (rel_offset + addr_offset);
   1654 
   1655 		  /* Check to see if the current reloc is associated with
   1656 		     the instruction that we are about to disassemble.  */
   1657 		  if (distance_to_rel == 0
   1658 		      /* FIXME: This is wrong.  We are trying to catch
   1659 			 relocs that are addressed part way through the
   1660 			 current instruction, as might happen with a packed
   1661 			 VLIW instruction.  Unfortunately we do not know the
   1662 			 length of the current instruction since we have not
   1663 			 disassembled it yet.  Instead we take a guess based
   1664 			 upon the length of the previous instruction.  The
   1665 			 proper solution is to have a new target-specific
   1666 			 disassembler function which just returns the length
   1667 			 of an instruction at a given address without trying
   1668 			 to display its disassembly. */
   1669 		      || (distance_to_rel > 0
   1670 			  && distance_to_rel < (bfd_signed_vma) (previous_octets/ opb)))
   1671 		    {
   1672 		      inf->flags |= INSN_HAS_RELOC;
   1673 		      aux->reloc = **relppp;
   1674 		    }
   1675 		}
   1676 
   1677 	      octets = (*disassemble_fn) (section->vma + addr_offset, inf);
   1678 	      inf->fprintf_func = (fprintf_ftype) fprintf;
   1679 	      inf->stream = stdout;
   1680 	      if (insn_width == 0 && inf->bytes_per_line != 0)
   1681 		octets_per_line = inf->bytes_per_line;
   1682 	      if (octets < (int) opb)
   1683 		{
   1684 		  if (sfile.pos)
   1685 		    printf ("%s\n", sfile.buffer);
   1686 		  if (octets >= 0)
   1687 		    {
   1688 		      non_fatal (_("disassemble_fn returned length %d"),
   1689 				 octets);
   1690 		      exit_status = 1;
   1691 		    }
   1692 		  break;
   1693 		}
   1694 	    }
   1695 	  else
   1696 	    {
   1697 	      bfd_vma j;
   1698 
   1699 	      octets = octets_per_line;
   1700 	      if (addr_offset + octets / opb > stop_offset)
   1701 		octets = (stop_offset - addr_offset) * opb;
   1702 
   1703 	      for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
   1704 		{
   1705 		  if (ISPRINT (data[j]))
   1706 		    buf[j - addr_offset * opb] = data[j];
   1707 		  else
   1708 		    buf[j - addr_offset * opb] = '.';
   1709 		}
   1710 	      buf[j - addr_offset * opb] = '\0';
   1711 	    }
   1712 
   1713 	  if (prefix_addresses
   1714 	      ? show_raw_insn > 0
   1715 	      : show_raw_insn >= 0)
   1716 	    {
   1717 	      bfd_vma j;
   1718 
   1719 	      /* If ! prefix_addresses and ! wide_output, we print
   1720 		 octets_per_line octets per line.  */
   1721 	      pb = octets;
   1722 	      if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
   1723 		pb = octets_per_line;
   1724 
   1725 	      if (inf->bytes_per_chunk)
   1726 		bpc = inf->bytes_per_chunk;
   1727 	      else
   1728 		bpc = 1;
   1729 
   1730 	      for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
   1731 		{
   1732 		  int k;
   1733 
   1734 		  if (bpc > 1 && inf->display_endian == BFD_ENDIAN_LITTLE)
   1735 		    {
   1736 		      for (k = bpc - 1; k >= 0; k--)
   1737 			printf ("%02x", (unsigned) data[j + k]);
   1738 		      putchar (' ');
   1739 		    }
   1740 		  else
   1741 		    {
   1742 		      for (k = 0; k < bpc; k++)
   1743 			printf ("%02x", (unsigned) data[j + k]);
   1744 		      putchar (' ');
   1745 		    }
   1746 		}
   1747 
   1748 	      for (; pb < octets_per_line; pb += bpc)
   1749 		{
   1750 		  int k;
   1751 
   1752 		  for (k = 0; k < bpc; k++)
   1753 		    printf ("  ");
   1754 		  putchar (' ');
   1755 		}
   1756 
   1757 	      /* Separate raw data from instruction by extra space.  */
   1758 	      if (insns)
   1759 		putchar ('\t');
   1760 	      else
   1761 		printf ("    ");
   1762 	    }
   1763 
   1764 	  if (! insns)
   1765 	    printf ("%s", buf);
   1766 	  else if (sfile.pos)
   1767 	    printf ("%s", sfile.buffer);
   1768 
   1769 	  if (prefix_addresses
   1770 	      ? show_raw_insn > 0
   1771 	      : show_raw_insn >= 0)
   1772 	    {
   1773 	      while (pb < octets)
   1774 		{
   1775 		  bfd_vma j;
   1776 		  char *s;
   1777 
   1778 		  putchar ('\n');
   1779 		  j = addr_offset * opb + pb;
   1780 
   1781 		  bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
   1782 		  for (s = buf + skip_addr_chars; *s == '0'; s++)
   1783 		    *s = ' ';
   1784 		  if (*s == '\0')
   1785 		    *--s = '0';
   1786 		  printf ("%s:\t", buf + skip_addr_chars);
   1787 
   1788 		  pb += octets_per_line;
   1789 		  if (pb > octets)
   1790 		    pb = octets;
   1791 		  for (; j < addr_offset * opb + pb; j += bpc)
   1792 		    {
   1793 		      int k;
   1794 
   1795 		      if (bpc > 1 && inf->display_endian == BFD_ENDIAN_LITTLE)
   1796 			{
   1797 			  for (k = bpc - 1; k >= 0; k--)
   1798 			    printf ("%02x", (unsigned) data[j + k]);
   1799 			  putchar (' ');
   1800 			}
   1801 		      else
   1802 			{
   1803 			  for (k = 0; k < bpc; k++)
   1804 			    printf ("%02x", (unsigned) data[j + k]);
   1805 			  putchar (' ');
   1806 			}
   1807 		    }
   1808 		}
   1809 	    }
   1810 
   1811 	  if (!wide_output)
   1812 	    putchar ('\n');
   1813 	  else
   1814 	    need_nl = TRUE;
   1815 	}
   1816 
   1817       while ((*relppp) < relppend
   1818 	     && (**relppp)->address < rel_offset + addr_offset + octets / opb)
   1819 	{
   1820 	  if (dump_reloc_info || dump_dynamic_reloc_info)
   1821 	    {
   1822 	      arelent *q;
   1823 
   1824 	      q = **relppp;
   1825 
   1826 	      if (wide_output)
   1827 		putchar ('\t');
   1828 	      else
   1829 		printf ("\t\t\t");
   1830 
   1831 	      objdump_print_value (section->vma - rel_offset + q->address,
   1832 				   inf, TRUE);
   1833 
   1834 	      if (q->howto == NULL)
   1835 		printf (": *unknown*\t");
   1836 	      else if (q->howto->name)
   1837 		printf (": %s\t", q->howto->name);
   1838 	      else
   1839 		printf (": %d\t", q->howto->type);
   1840 
   1841 	      if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
   1842 		printf ("*unknown*");
   1843 	      else
   1844 		{
   1845 		  const char *sym_name;
   1846 
   1847 		  sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
   1848 		  if (sym_name != NULL && *sym_name != '\0')
   1849 		    objdump_print_symname (aux->abfd, inf, *q->sym_ptr_ptr);
   1850 		  else
   1851 		    {
   1852 		      asection *sym_sec;
   1853 
   1854 		      sym_sec = bfd_get_section (*q->sym_ptr_ptr);
   1855 		      sym_name = bfd_get_section_name (aux->abfd, sym_sec);
   1856 		      if (sym_name == NULL || *sym_name == '\0')
   1857 			sym_name = "*unknown*";
   1858 		      printf ("%s", sym_name);
   1859 		    }
   1860 		}
   1861 
   1862 	      if (q->addend)
   1863 		{
   1864 		  bfd_signed_vma addend = q->addend;
   1865 		  if (addend < 0)
   1866 		    {
   1867 		      printf ("-0x");
   1868 		      addend = -addend;
   1869 		    }
   1870 		  else
   1871 		    printf ("+0x");
   1872 		  objdump_print_value (addend, inf, TRUE);
   1873 		}
   1874 
   1875 	      printf ("\n");
   1876 	      need_nl = FALSE;
   1877 	    }
   1878 	  ++(*relppp);
   1879 	}
   1880 
   1881       if (need_nl)
   1882 	printf ("\n");
   1883 
   1884       addr_offset += octets / opb;
   1885     }
   1886 
   1887   free (sfile.buffer);
   1888 }
   1889 
   1890 static void
   1891 disassemble_section (bfd *abfd, asection *section, void *inf)
   1892 {
   1893   const struct elf_backend_data * bed;
   1894   bfd_vma                      sign_adjust = 0;
   1895   struct disassemble_info *    pinfo = (struct disassemble_info *) inf;
   1896   struct objdump_disasm_info * paux;
   1897   unsigned int                 opb = pinfo->octets_per_byte;
   1898   bfd_byte *                   data = NULL;
   1899   bfd_size_type                datasize = 0;
   1900   arelent **                   rel_pp = NULL;
   1901   arelent **                   rel_ppstart = NULL;
   1902   arelent **                   rel_ppend;
   1903   unsigned long                stop_offset;
   1904   asymbol *                    sym = NULL;
   1905   long                         place = 0;
   1906   long                         rel_count;
   1907   bfd_vma                      rel_offset;
   1908   unsigned long                addr_offset;
   1909 
   1910   /* Sections that do not contain machine
   1911      code are not normally disassembled.  */
   1912   if (! disassemble_all
   1913       && only_list == NULL
   1914       && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
   1915 	  != (SEC_CODE | SEC_HAS_CONTENTS)))
   1916     return;
   1917 
   1918   if (! process_section_p (section))
   1919     return;
   1920 
   1921   datasize = bfd_get_section_size (section);
   1922   if (datasize == 0)
   1923     return;
   1924 
   1925   if (start_address == (bfd_vma) -1
   1926       || start_address < section->vma)
   1927     addr_offset = 0;
   1928   else
   1929     addr_offset = start_address - section->vma;
   1930 
   1931   if (stop_address == (bfd_vma) -1)
   1932     stop_offset = datasize / opb;
   1933   else
   1934     {
   1935       if (stop_address < section->vma)
   1936 	stop_offset = 0;
   1937       else
   1938 	stop_offset = stop_address - section->vma;
   1939       if (stop_offset > datasize / opb)
   1940 	stop_offset = datasize / opb;
   1941     }
   1942 
   1943   if (addr_offset >= stop_offset)
   1944     return;
   1945 
   1946   /* Decide which set of relocs to use.  Load them if necessary.  */
   1947   paux = (struct objdump_disasm_info *) pinfo->application_data;
   1948   if (paux->dynrelbuf)
   1949     {
   1950       rel_pp = paux->dynrelbuf;
   1951       rel_count = paux->dynrelcount;
   1952       /* Dynamic reloc addresses are absolute, non-dynamic are section
   1953 	 relative.  REL_OFFSET specifies the reloc address corresponding
   1954 	 to the start of this section.  */
   1955       rel_offset = section->vma;
   1956     }
   1957   else
   1958     {
   1959       rel_count = 0;
   1960       rel_pp = NULL;
   1961       rel_offset = 0;
   1962 
   1963       if ((section->flags & SEC_RELOC) != 0
   1964 	  && (dump_reloc_info || pinfo->disassembler_needs_relocs))
   1965 	{
   1966 	  long relsize;
   1967 
   1968 	  relsize = bfd_get_reloc_upper_bound (abfd, section);
   1969 	  if (relsize < 0)
   1970 	    bfd_fatal (bfd_get_filename (abfd));
   1971 
   1972 	  if (relsize > 0)
   1973 	    {
   1974 	      rel_ppstart = rel_pp = (arelent **) xmalloc (relsize);
   1975 	      rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
   1976 	      if (rel_count < 0)
   1977 		bfd_fatal (bfd_get_filename (abfd));
   1978 
   1979 	      /* Sort the relocs by address.  */
   1980 	      qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
   1981 	    }
   1982 	}
   1983     }
   1984   rel_ppend = rel_pp + rel_count;
   1985 
   1986   data = (bfd_byte *) xmalloc (datasize);
   1987 
   1988   bfd_get_section_contents (abfd, section, data, 0, datasize);
   1989 
   1990   paux->sec = section;
   1991   pinfo->buffer = data;
   1992   pinfo->buffer_vma = section->vma;
   1993   pinfo->buffer_length = datasize;
   1994   pinfo->section = section;
   1995 
   1996   /* Skip over the relocs belonging to addresses below the
   1997      start address.  */
   1998   while (rel_pp < rel_ppend
   1999 	 && (*rel_pp)->address < rel_offset + addr_offset)
   2000     ++rel_pp;
   2001 
   2002   printf (_("\nDisassembly of section %s:\n"), section->name);
   2003 
   2004   /* Find the nearest symbol forwards from our current position.  */
   2005   paux->require_sec = TRUE;
   2006   sym = (asymbol *) find_symbol_for_address (section->vma + addr_offset,
   2007                                              (struct disassemble_info *) inf,
   2008                                              &place);
   2009   paux->require_sec = FALSE;
   2010 
   2011   /* PR 9774: If the target used signed addresses then we must make
   2012      sure that we sign extend the value that we calculate for 'addr'
   2013      in the loop below.  */
   2014   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   2015       && (bed = get_elf_backend_data (abfd)) != NULL
   2016       && bed->sign_extend_vma)
   2017     sign_adjust = (bfd_vma) 1 << (bed->s->arch_size - 1);
   2018 
   2019   /* Disassemble a block of instructions up to the address associated with
   2020      the symbol we have just found.  Then print the symbol and find the
   2021      next symbol on.  Repeat until we have disassembled the entire section
   2022      or we have reached the end of the address range we are interested in.  */
   2023   while (addr_offset < stop_offset)
   2024     {
   2025       bfd_vma addr;
   2026       asymbol *nextsym;
   2027       unsigned long nextstop_offset;
   2028       bfd_boolean insns;
   2029 
   2030       addr = section->vma + addr_offset;
   2031       addr = ((addr & ((sign_adjust << 1) - 1)) ^ sign_adjust) - sign_adjust;
   2032 
   2033       if (sym != NULL && bfd_asymbol_value (sym) <= addr)
   2034 	{
   2035 	  int x;
   2036 
   2037 	  for (x = place;
   2038 	       (x < sorted_symcount
   2039 		&& (bfd_asymbol_value (sorted_syms[x]) <= addr));
   2040 	       ++x)
   2041 	    continue;
   2042 
   2043 	  pinfo->symbols = sorted_syms + place;
   2044 	  pinfo->num_symbols = x - place;
   2045 	  pinfo->symtab_pos = place;
   2046 	}
   2047       else
   2048 	{
   2049 	  pinfo->symbols = NULL;
   2050 	  pinfo->num_symbols = 0;
   2051 	  pinfo->symtab_pos = -1;
   2052 	}
   2053 
   2054       if (! prefix_addresses)
   2055 	{
   2056 	  pinfo->fprintf_func (pinfo->stream, "\n");
   2057 	  objdump_print_addr_with_sym (abfd, section, sym, addr,
   2058 				       pinfo, FALSE);
   2059 	  pinfo->fprintf_func (pinfo->stream, ":\n");
   2060 	}
   2061 
   2062       if (sym != NULL && bfd_asymbol_value (sym) > addr)
   2063 	nextsym = sym;
   2064       else if (sym == NULL)
   2065 	nextsym = NULL;
   2066       else
   2067 	{
   2068 #define is_valid_next_sym(SYM) \
   2069   ((SYM)->section == section \
   2070    && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
   2071    && pinfo->symbol_is_valid (SYM, pinfo))
   2072 
   2073 	  /* Search forward for the next appropriate symbol in
   2074 	     SECTION.  Note that all the symbols are sorted
   2075 	     together into one big array, and that some sections
   2076 	     may have overlapping addresses.  */
   2077 	  while (place < sorted_symcount
   2078 		 && ! is_valid_next_sym (sorted_syms [place]))
   2079 	    ++place;
   2080 
   2081 	  if (place >= sorted_symcount)
   2082 	    nextsym = NULL;
   2083 	  else
   2084 	    nextsym = sorted_syms[place];
   2085 	}
   2086 
   2087       if (sym != NULL && bfd_asymbol_value (sym) > addr)
   2088 	nextstop_offset = bfd_asymbol_value (sym) - section->vma;
   2089       else if (nextsym == NULL)
   2090 	nextstop_offset = stop_offset;
   2091       else
   2092 	nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
   2093 
   2094       if (nextstop_offset > stop_offset
   2095 	  || nextstop_offset <= addr_offset)
   2096 	nextstop_offset = stop_offset;
   2097 
   2098       /* If a symbol is explicitly marked as being an object
   2099 	 rather than a function, just dump the bytes without
   2100 	 disassembling them.  */
   2101       if (disassemble_all
   2102 	  || sym == NULL
   2103 	  || sym->section != section
   2104 	  || bfd_asymbol_value (sym) > addr
   2105 	  || ((sym->flags & BSF_OBJECT) == 0
   2106 	      && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
   2107 		  == NULL)
   2108 	      && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
   2109 		  == NULL))
   2110 	  || (sym->flags & BSF_FUNCTION) != 0)
   2111 	insns = TRUE;
   2112       else
   2113 	insns = FALSE;
   2114 
   2115       disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
   2116 			 addr_offset, nextstop_offset,
   2117 			 rel_offset, &rel_pp, rel_ppend);
   2118 
   2119       addr_offset = nextstop_offset;
   2120       sym = nextsym;
   2121     }
   2122 
   2123   free (data);
   2124 
   2125   if (rel_ppstart != NULL)
   2126     free (rel_ppstart);
   2127 }
   2128 
   2129 /* Disassemble the contents of an object file.  */
   2130 
   2131 static void
   2132 disassemble_data (bfd *abfd)
   2133 {
   2134   struct disassemble_info disasm_info;
   2135   struct objdump_disasm_info aux;
   2136   long i;
   2137 
   2138   print_files = NULL;
   2139   prev_functionname = NULL;
   2140   prev_line = -1;
   2141   prev_discriminator = 0;
   2142 
   2143   /* We make a copy of syms to sort.  We don't want to sort syms
   2144      because that will screw up the relocs.  */
   2145   sorted_symcount = symcount ? symcount : dynsymcount;
   2146   sorted_syms = (asymbol **) xmalloc ((sorted_symcount + synthcount)
   2147                                       * sizeof (asymbol *));
   2148   memcpy (sorted_syms, symcount ? syms : dynsyms,
   2149 	  sorted_symcount * sizeof (asymbol *));
   2150 
   2151   sorted_symcount = remove_useless_symbols (sorted_syms, sorted_symcount);
   2152 
   2153   for (i = 0; i < synthcount; ++i)
   2154     {
   2155       sorted_syms[sorted_symcount] = synthsyms + i;
   2156       ++sorted_symcount;
   2157     }
   2158 
   2159   /* Sort the symbols into section and symbol order.  */
   2160   qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
   2161 
   2162   init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
   2163 
   2164   disasm_info.application_data = (void *) &aux;
   2165   aux.abfd = abfd;
   2166   aux.require_sec = FALSE;
   2167   aux.dynrelbuf = NULL;
   2168   aux.dynrelcount = 0;
   2169   aux.reloc = NULL;
   2170 
   2171   disasm_info.print_address_func = objdump_print_address;
   2172   disasm_info.symbol_at_address_func = objdump_symbol_at_address;
   2173 
   2174   if (machine != NULL)
   2175     {
   2176       const bfd_arch_info_type *inf = bfd_scan_arch (machine);
   2177 
   2178       if (inf == NULL)
   2179 	fatal (_("can't use supplied machine %s"), machine);
   2180 
   2181       abfd->arch_info = inf;
   2182     }
   2183 
   2184   if (endian != BFD_ENDIAN_UNKNOWN)
   2185     {
   2186       struct bfd_target *xvec;
   2187 
   2188       xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
   2189       memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
   2190       xvec->byteorder = endian;
   2191       abfd->xvec = xvec;
   2192     }
   2193 
   2194   /* Use libopcodes to locate a suitable disassembler.  */
   2195   aux.disassemble_fn = disassembler (abfd);
   2196   if (!aux.disassemble_fn)
   2197     {
   2198       non_fatal (_("can't disassemble for architecture %s\n"),
   2199 		 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
   2200       exit_status = 1;
   2201       return;
   2202     }
   2203 
   2204   disasm_info.flavour = bfd_get_flavour (abfd);
   2205   disasm_info.arch = bfd_get_arch (abfd);
   2206   disasm_info.mach = bfd_get_mach (abfd);
   2207   disasm_info.disassembler_options = disassembler_options;
   2208   disasm_info.octets_per_byte = bfd_octets_per_byte (abfd);
   2209   disasm_info.skip_zeroes = DEFAULT_SKIP_ZEROES;
   2210   disasm_info.skip_zeroes_at_end = DEFAULT_SKIP_ZEROES_AT_END;
   2211   disasm_info.disassembler_needs_relocs = FALSE;
   2212 
   2213   if (bfd_big_endian (abfd))
   2214     disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
   2215   else if (bfd_little_endian (abfd))
   2216     disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
   2217   else
   2218     /* ??? Aborting here seems too drastic.  We could default to big or little
   2219        instead.  */
   2220     disasm_info.endian = BFD_ENDIAN_UNKNOWN;
   2221 
   2222   /* Allow the target to customize the info structure.  */
   2223   disassemble_init_for_target (& disasm_info);
   2224 
   2225   /* Pre-load the dynamic relocs if we are going
   2226      to be dumping them along with the disassembly.  */
   2227   if (dump_dynamic_reloc_info)
   2228     {
   2229       long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
   2230 
   2231       if (relsize < 0)
   2232 	bfd_fatal (bfd_get_filename (abfd));
   2233 
   2234       if (relsize > 0)
   2235 	{
   2236 	  aux.dynrelbuf = (arelent **) xmalloc (relsize);
   2237 	  aux.dynrelcount = bfd_canonicalize_dynamic_reloc (abfd,
   2238 							    aux.dynrelbuf,
   2239 							    dynsyms);
   2240 	  if (aux.dynrelcount < 0)
   2241 	    bfd_fatal (bfd_get_filename (abfd));
   2242 
   2243 	  /* Sort the relocs by address.  */
   2244 	  qsort (aux.dynrelbuf, aux.dynrelcount, sizeof (arelent *),
   2245 		 compare_relocs);
   2246 	}
   2247     }
   2248   disasm_info.symtab = sorted_syms;
   2249   disasm_info.symtab_size = sorted_symcount;
   2250 
   2251   bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
   2252 
   2253   if (aux.dynrelbuf != NULL)
   2254     free (aux.dynrelbuf);
   2255   free (sorted_syms);
   2256 }
   2257 
   2258 static int
   2260 load_specific_debug_section (enum dwarf_section_display_enum debug,
   2261 			     asection *sec, void *file)
   2262 {
   2263   struct dwarf_section *section = &debug_displays [debug].section;
   2264   bfd *abfd = (bfd *) file;
   2265   bfd_boolean ret;
   2266 
   2267   /* If it is already loaded, do nothing.  */
   2268   if (section->start != NULL)
   2269     return 1;
   2270 
   2271   section->address = bfd_get_section_vma (abfd, sec);
   2272   section->size = bfd_get_section_size (sec);
   2273   section->start = NULL;
   2274   ret = bfd_get_full_section_contents (abfd, sec, &section->start);
   2275 
   2276   if (! ret)
   2277     {
   2278       free_debug_section (debug);
   2279       printf (_("\nCan't get contents for section '%s'.\n"),
   2280 	      section->name);
   2281       return 0;
   2282     }
   2283 
   2284   if (is_relocatable && debug_displays [debug].relocate)
   2285     {
   2286       bfd_cache_section_contents (sec, section->start);
   2287 
   2288       ret = bfd_simple_get_relocated_section_contents (abfd,
   2289 						       sec,
   2290 						       section->start,
   2291 						       syms) != NULL;
   2292 
   2293       if (! ret)
   2294         {
   2295           free_debug_section (debug);
   2296           printf (_("\nCan't get contents for section '%s'.\n"),
   2297 	          section->name);
   2298           return 0;
   2299         }
   2300     }
   2301 
   2302   return 1;
   2303 }
   2304 
   2305 int
   2306 load_debug_section (enum dwarf_section_display_enum debug, void *file)
   2307 {
   2308   struct dwarf_section *section = &debug_displays [debug].section;
   2309   bfd *abfd = (bfd *) file;
   2310   asection *sec;
   2311 
   2312   /* If it is already loaded, do nothing.  */
   2313   if (section->start != NULL)
   2314     return 1;
   2315 
   2316   /* Locate the debug section.  */
   2317   sec = bfd_get_section_by_name (abfd, section->uncompressed_name);
   2318   if (sec != NULL)
   2319     section->name = section->uncompressed_name;
   2320   else
   2321     {
   2322       sec = bfd_get_section_by_name (abfd, section->compressed_name);
   2323       if (sec != NULL)
   2324         section->name = section->compressed_name;
   2325     }
   2326   if (sec == NULL)
   2327     return 0;
   2328 
   2329   return load_specific_debug_section (debug, sec, file);
   2330 }
   2331 
   2332 void
   2333 free_debug_section (enum dwarf_section_display_enum debug)
   2334 {
   2335   struct dwarf_section *section = &debug_displays [debug].section;
   2336 
   2337   if (section->start == NULL)
   2338     return;
   2339 
   2340   free ((char *) section->start);
   2341   section->start = NULL;
   2342   section->address = 0;
   2343   section->size = 0;
   2344 }
   2345 
   2346 static void
   2347 dump_dwarf_section (bfd *abfd, asection *section,
   2348 		    void *arg ATTRIBUTE_UNUSED)
   2349 {
   2350   const char *name = bfd_get_section_name (abfd, section);
   2351   const char *match;
   2352   int i;
   2353 
   2354   if (CONST_STRNEQ (name, ".gnu.linkonce.wi."))
   2355     match = ".debug_info";
   2356   else
   2357     match = name;
   2358 
   2359   for (i = 0; i < max; i++)
   2360     if ((strcmp (debug_displays [i].section.uncompressed_name, match) == 0
   2361 	 || strcmp (debug_displays [i].section.compressed_name, match) == 0)
   2362 	&& debug_displays [i].enabled != NULL
   2363 	&& *debug_displays [i].enabled)
   2364       {
   2365 	struct dwarf_section *sec = &debug_displays [i].section;
   2366 
   2367 	if (strcmp (sec->uncompressed_name, match) == 0)
   2368 	  sec->name = sec->uncompressed_name;
   2369 	else
   2370 	  sec->name = sec->compressed_name;
   2371 	if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
   2372                                          section, abfd))
   2373 	  {
   2374 	    debug_displays [i].display (sec, abfd);
   2375 
   2376 	    if (i != info && i != abbrev)
   2377 	      free_debug_section ((enum dwarf_section_display_enum) i);
   2378 	  }
   2379 	break;
   2380       }
   2381 }
   2382 
   2383 /* Dump the dwarf debugging information.  */
   2384 
   2385 static void
   2386 dump_dwarf (bfd *abfd)
   2387 {
   2388   is_relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
   2389 
   2390   eh_addr_size = bfd_arch_bits_per_address (abfd) / 8;
   2391 
   2392   if (bfd_big_endian (abfd))
   2393     byte_get = byte_get_big_endian;
   2394   else if (bfd_little_endian (abfd))
   2395     byte_get = byte_get_little_endian;
   2396   else
   2397     /* PR 17512: file: objdump-s-endless-loop.tekhex.  */
   2398     {
   2399       warn (_("File %s does not contain any dwarf debug information\n"),
   2400 	    bfd_get_filename (abfd));
   2401       return;
   2402     }
   2403 
   2404   switch (bfd_get_arch (abfd))
   2405     {
   2406     case bfd_arch_i386:
   2407       switch (bfd_get_mach (abfd))
   2408 	{
   2409 	case bfd_mach_x86_64:
   2410 	case bfd_mach_x86_64_intel_syntax:
   2411 	case bfd_mach_x86_64_nacl:
   2412 	case bfd_mach_x64_32:
   2413 	case bfd_mach_x64_32_intel_syntax:
   2414 	case bfd_mach_x64_32_nacl:
   2415 	  init_dwarf_regnames_x86_64 ();
   2416 	  break;
   2417 
   2418 	default:
   2419 	  init_dwarf_regnames_i386 ();
   2420 	  break;
   2421 	}
   2422       break;
   2423 
   2424     case bfd_arch_aarch64:
   2425       init_dwarf_regnames_aarch64();
   2426       break;
   2427 
   2428     default:
   2429       break;
   2430     }
   2431 
   2432   bfd_map_over_sections (abfd, dump_dwarf_section, NULL);
   2433 
   2434   free_debug_memory ();
   2435 }
   2436 
   2437 /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
   2439    it.  Return NULL on failure.   */
   2440 
   2441 static char *
   2442 read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr)
   2443 {
   2444   asection *stabsect;
   2445   bfd_size_type size;
   2446   char *contents;
   2447 
   2448   stabsect = bfd_get_section_by_name (abfd, sect_name);
   2449   if (stabsect == NULL)
   2450     {
   2451       printf (_("No %s section present\n\n"), sect_name);
   2452       return FALSE;
   2453     }
   2454 
   2455   size = bfd_section_size (abfd, stabsect);
   2456   contents  = (char *) xmalloc (size);
   2457 
   2458   if (! bfd_get_section_contents (abfd, stabsect, contents, 0, size))
   2459     {
   2460       non_fatal (_("reading %s section of %s failed: %s"),
   2461 		 sect_name, bfd_get_filename (abfd),
   2462 		 bfd_errmsg (bfd_get_error ()));
   2463       exit_status = 1;
   2464       free (contents);
   2465       return NULL;
   2466     }
   2467 
   2468   *size_ptr = size;
   2469 
   2470   return contents;
   2471 }
   2472 
   2473 /* Stabs entries use a 12 byte format:
   2474      4 byte string table index
   2475      1 byte stab type
   2476      1 byte stab other field
   2477      2 byte stab desc field
   2478      4 byte stab value
   2479    FIXME: This will have to change for a 64 bit object format.  */
   2480 
   2481 #define STRDXOFF  (0)
   2482 #define TYPEOFF   (4)
   2483 #define OTHEROFF  (5)
   2484 #define DESCOFF   (6)
   2485 #define VALOFF    (8)
   2486 #define STABSIZE (12)
   2487 
   2488 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
   2489    using string table section STRSECT_NAME (in `strtab').  */
   2490 
   2491 static void
   2492 print_section_stabs (bfd *abfd,
   2493 		     const char *stabsect_name,
   2494 		     unsigned *string_offset_ptr)
   2495 {
   2496   int i;
   2497   unsigned file_string_table_offset = 0;
   2498   unsigned next_file_string_table_offset = *string_offset_ptr;
   2499   bfd_byte *stabp, *stabs_end;
   2500 
   2501   stabp = stabs;
   2502   stabs_end = stabp + stab_size;
   2503 
   2504   printf (_("Contents of %s section:\n\n"), stabsect_name);
   2505   printf ("Symnum n_type n_othr n_desc n_value  n_strx String\n");
   2506 
   2507   /* Loop through all symbols and print them.
   2508 
   2509      We start the index at -1 because there is a dummy symbol on
   2510      the front of stabs-in-{coff,elf} sections that supplies sizes.  */
   2511   for (i = -1; stabp <= stabs_end - STABSIZE; stabp += STABSIZE, i++)
   2512     {
   2513       const char *name;
   2514       unsigned long strx;
   2515       unsigned char type, other;
   2516       unsigned short desc;
   2517       bfd_vma value;
   2518 
   2519       strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
   2520       type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
   2521       other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
   2522       desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
   2523       value = bfd_h_get_32 (abfd, stabp + VALOFF);
   2524 
   2525       printf ("\n%-6d ", i);
   2526       /* Either print the stab name, or, if unnamed, print its number
   2527 	 again (makes consistent formatting for tools like awk).  */
   2528       name = bfd_get_stab_name (type);
   2529       if (name != NULL)
   2530 	printf ("%-6s", name);
   2531       else if (type == N_UNDF)
   2532 	printf ("HdrSym");
   2533       else
   2534 	printf ("%-6d", type);
   2535       printf (" %-6d %-6d ", other, desc);
   2536       bfd_printf_vma (abfd, value);
   2537       printf (" %-6lu", strx);
   2538 
   2539       /* Symbols with type == 0 (N_UNDF) specify the length of the
   2540 	 string table associated with this file.  We use that info
   2541 	 to know how to relocate the *next* file's string table indices.  */
   2542       if (type == N_UNDF)
   2543 	{
   2544 	  file_string_table_offset = next_file_string_table_offset;
   2545 	  next_file_string_table_offset += value;
   2546 	}
   2547       else
   2548 	{
   2549 	  bfd_size_type amt = strx + file_string_table_offset;
   2550 
   2551 	  /* Using the (possibly updated) string table offset, print the
   2552 	     string (if any) associated with this symbol.  */
   2553 	  if (amt < stabstr_size)
   2554 	    /* PR 17512: file: 079-79389-0.001:0.1.  */
   2555 	    printf (" %.*s", (int)(stabstr_size - amt), strtab + amt);
   2556 	  else
   2557 	    printf (" *");
   2558 	}
   2559     }
   2560   printf ("\n\n");
   2561   *string_offset_ptr = next_file_string_table_offset;
   2562 }
   2563 
   2564 typedef struct
   2565 {
   2566   const char * section_name;
   2567   const char * string_section_name;
   2568   unsigned string_offset;
   2569 }
   2570 stab_section_names;
   2571 
   2572 static void
   2573 find_stabs_section (bfd *abfd, asection *section, void *names)
   2574 {
   2575   int len;
   2576   stab_section_names * sought = (stab_section_names *) names;
   2577 
   2578   /* Check for section names for which stabsect_name is a prefix, to
   2579      handle .stab.N, etc.  */
   2580   len = strlen (sought->section_name);
   2581 
   2582   /* If the prefix matches, and the files section name ends with a
   2583      nul or a digit, then we match.  I.e., we want either an exact
   2584      match or a section followed by a number.  */
   2585   if (strncmp (sought->section_name, section->name, len) == 0
   2586       && (section->name[len] == 0
   2587 	  || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
   2588     {
   2589       if (strtab == NULL)
   2590 	strtab = read_section_stabs (abfd, sought->string_section_name,
   2591 				     &stabstr_size);
   2592 
   2593       if (strtab)
   2594 	{
   2595 	  stabs = (bfd_byte *) read_section_stabs (abfd, section->name,
   2596 						   &stab_size);
   2597 	  if (stabs)
   2598 	    print_section_stabs (abfd, section->name, &sought->string_offset);
   2599 	}
   2600     }
   2601 }
   2602 
   2603 static void
   2604 dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
   2605 {
   2606   stab_section_names s;
   2607 
   2608   s.section_name = stabsect_name;
   2609   s.string_section_name = strsect_name;
   2610   s.string_offset = 0;
   2611 
   2612   bfd_map_over_sections (abfd, find_stabs_section, & s);
   2613 
   2614   free (strtab);
   2615   strtab = NULL;
   2616 }
   2617 
   2618 /* Dump the any sections containing stabs debugging information.  */
   2619 
   2620 static void
   2621 dump_stabs (bfd *abfd)
   2622 {
   2623   dump_stabs_section (abfd, ".stab", ".stabstr");
   2624   dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
   2625   dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
   2626 
   2627   /* For Darwin.  */
   2628   dump_stabs_section (abfd, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
   2629 
   2630   dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
   2631 }
   2632 
   2633 static void
   2635 dump_bfd_header (bfd *abfd)
   2636 {
   2637   char *comma = "";
   2638 
   2639   printf (_("architecture: %s, "),
   2640 	  bfd_printable_arch_mach (bfd_get_arch (abfd),
   2641 				   bfd_get_mach (abfd)));
   2642   printf (_("flags 0x%08x:\n"), abfd->flags & ~BFD_FLAGS_FOR_BFD_USE_MASK);
   2643 
   2644 #define PF(x, y)    if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
   2645   PF (HAS_RELOC, "HAS_RELOC");
   2646   PF (EXEC_P, "EXEC_P");
   2647   PF (HAS_LINENO, "HAS_LINENO");
   2648   PF (HAS_DEBUG, "HAS_DEBUG");
   2649   PF (HAS_SYMS, "HAS_SYMS");
   2650   PF (HAS_LOCALS, "HAS_LOCALS");
   2651   PF (DYNAMIC, "DYNAMIC");
   2652   PF (WP_TEXT, "WP_TEXT");
   2653   PF (D_PAGED, "D_PAGED");
   2654   PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
   2655   printf (_("\nstart address 0x"));
   2656   bfd_printf_vma (abfd, abfd->start_address);
   2657   printf ("\n");
   2658 }
   2659 
   2660 
   2661 static void
   2663 dump_bfd_private_header (bfd *abfd)
   2664 {
   2665   bfd_print_private_bfd_data (abfd, stdout);
   2666 }
   2667 
   2668 static void
   2669 dump_target_specific (bfd *abfd)
   2670 {
   2671   const struct objdump_private_desc * const *desc;
   2672   struct objdump_private_option *opt;
   2673   char *e, *b;
   2674 
   2675   /* Find the desc.  */
   2676   for (desc = objdump_private_vectors; *desc != NULL; desc++)
   2677     if ((*desc)->filter (abfd))
   2678       break;
   2679 
   2680   if (*desc == NULL)
   2681     {
   2682       non_fatal (_("option -P/--private not supported by this file"));
   2683       return;
   2684     }
   2685 
   2686   /* Clear all options.  */
   2687   for (opt = (*desc)->options; opt->name; opt++)
   2688     opt->selected = FALSE;
   2689 
   2690   /* Decode options.  */
   2691   b = dump_private_options;
   2692   do
   2693     {
   2694       e = strchr (b, ',');
   2695 
   2696       if (e)
   2697         *e = 0;
   2698 
   2699       for (opt = (*desc)->options; opt->name; opt++)
   2700         if (strcmp (opt->name, b) == 0)
   2701           {
   2702             opt->selected = TRUE;
   2703             break;
   2704           }
   2705       if (opt->name == NULL)
   2706         non_fatal (_("target specific dump '%s' not supported"), b);
   2707 
   2708       if (e)
   2709         {
   2710           *e = ',';
   2711           b = e + 1;
   2712         }
   2713     }
   2714   while (e != NULL);
   2715 
   2716   /* Dump.  */
   2717   (*desc)->dump (abfd);
   2718 }
   2719 
   2720 /* Display a section in hexadecimal format with associated characters.
   2722    Each line prefixed by the zero padded address.  */
   2723 
   2724 static void
   2725 dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
   2726 {
   2727   bfd_byte *data = 0;
   2728   bfd_size_type datasize;
   2729   bfd_size_type addr_offset;
   2730   bfd_size_type start_offset;
   2731   bfd_size_type stop_offset;
   2732   unsigned int opb = bfd_octets_per_byte (abfd);
   2733   /* Bytes per line.  */
   2734   const int onaline = 16;
   2735   char buf[64];
   2736   int count;
   2737   int width;
   2738 
   2739   if ((section->flags & SEC_HAS_CONTENTS) == 0)
   2740     return;
   2741 
   2742   if (! process_section_p (section))
   2743     return;
   2744 
   2745   if ((datasize = bfd_section_size (abfd, section)) == 0)
   2746     return;
   2747 
   2748   /* Compute the address range to display.  */
   2749   if (start_address == (bfd_vma) -1
   2750       || start_address < section->vma)
   2751     start_offset = 0;
   2752   else
   2753     start_offset = start_address - section->vma;
   2754 
   2755   if (stop_address == (bfd_vma) -1)
   2756     stop_offset = datasize / opb;
   2757   else
   2758     {
   2759       if (stop_address < section->vma)
   2760 	stop_offset = 0;
   2761       else
   2762 	stop_offset = stop_address - section->vma;
   2763 
   2764       if (stop_offset > datasize / opb)
   2765 	stop_offset = datasize / opb;
   2766     }
   2767 
   2768   if (start_offset >= stop_offset)
   2769     return;
   2770 
   2771   printf (_("Contents of section %s:"), section->name);
   2772   if (display_file_offsets)
   2773     printf (_("  (Starting at file offset: 0x%lx)"),
   2774 	    (unsigned long) (section->filepos + start_offset));
   2775   printf ("\n");
   2776 
   2777   if (!bfd_get_full_section_contents (abfd, section, &data))
   2778     {
   2779       non_fatal (_("Reading section failed"));
   2780       return;
   2781     }
   2782 
   2783   width = 4;
   2784 
   2785   bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
   2786   if (strlen (buf) >= sizeof (buf))
   2787     abort ();
   2788 
   2789   count = 0;
   2790   while (buf[count] == '0' && buf[count+1] != '\0')
   2791     count++;
   2792   count = strlen (buf) - count;
   2793   if (count > width)
   2794     width = count;
   2795 
   2796   bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
   2797   if (strlen (buf) >= sizeof (buf))
   2798     abort ();
   2799 
   2800   count = 0;
   2801   while (buf[count] == '0' && buf[count+1] != '\0')
   2802     count++;
   2803   count = strlen (buf) - count;
   2804   if (count > width)
   2805     width = count;
   2806 
   2807   for (addr_offset = start_offset;
   2808        addr_offset < stop_offset; addr_offset += onaline / opb)
   2809     {
   2810       bfd_size_type j;
   2811 
   2812       bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
   2813       count = strlen (buf);
   2814       if ((size_t) count >= sizeof (buf))
   2815 	abort ();
   2816 
   2817       putchar (' ');
   2818       while (count < width)
   2819 	{
   2820 	  putchar ('0');
   2821 	  count++;
   2822 	}
   2823       fputs (buf + count - width, stdout);
   2824       putchar (' ');
   2825 
   2826       for (j = addr_offset * opb;
   2827 	   j < addr_offset * opb + onaline; j++)
   2828 	{
   2829 	  if (j < stop_offset * opb)
   2830 	    printf ("%02x", (unsigned) (data[j]));
   2831 	  else
   2832 	    printf ("  ");
   2833 	  if ((j & 3) == 3)
   2834 	    printf (" ");
   2835 	}
   2836 
   2837       printf (" ");
   2838       for (j = addr_offset * opb;
   2839 	   j < addr_offset * opb + onaline; j++)
   2840 	{
   2841 	  if (j >= stop_offset * opb)
   2842 	    printf (" ");
   2843 	  else
   2844 	    printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
   2845 	}
   2846       putchar ('\n');
   2847     }
   2848   free (data);
   2849 }
   2850 
   2851 /* Actually display the various requested regions.  */
   2852 
   2853 static void
   2854 dump_data (bfd *abfd)
   2855 {
   2856   bfd_map_over_sections (abfd, dump_section, NULL);
   2857 }
   2858 
   2859 /* Should perhaps share code and display with nm?  */
   2860 
   2861 static void
   2862 dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean dynamic)
   2863 {
   2864   asymbol **current;
   2865   long max_count;
   2866   long count;
   2867 
   2868   if (dynamic)
   2869     {
   2870       current = dynsyms;
   2871       max_count = dynsymcount;
   2872       printf ("DYNAMIC SYMBOL TABLE:\n");
   2873     }
   2874   else
   2875     {
   2876       current = syms;
   2877       max_count = symcount;
   2878       printf ("SYMBOL TABLE:\n");
   2879     }
   2880 
   2881   if (max_count == 0)
   2882     printf (_("no symbols\n"));
   2883 
   2884   for (count = 0; count < max_count; count++)
   2885     {
   2886       bfd *cur_bfd;
   2887 
   2888       if (*current == NULL)
   2889 	printf (_("no information for symbol number %ld\n"), count);
   2890 
   2891       else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
   2892 	printf (_("could not determine the type of symbol number %ld\n"),
   2893 		count);
   2894 
   2895       else if (process_section_p ((* current)->section)
   2896 	       && (dump_special_syms
   2897 		   || !bfd_is_target_special_symbol (cur_bfd, *current)))
   2898 	{
   2899 	  const char *name = (*current)->name;
   2900 
   2901 	  if (do_demangle && name != NULL && *name != '\0')
   2902 	    {
   2903 	      char *alloc;
   2904 
   2905 	      /* If we want to demangle the name, we demangle it
   2906 		 here, and temporarily clobber it while calling
   2907 		 bfd_print_symbol.  FIXME: This is a gross hack.  */
   2908 	      alloc = bfd_demangle (cur_bfd, name, DMGL_ANSI | DMGL_PARAMS);
   2909 	      if (alloc != NULL)
   2910 		(*current)->name = alloc;
   2911 	      bfd_print_symbol (cur_bfd, stdout, *current,
   2912 				bfd_print_symbol_all);
   2913 	      if (alloc != NULL)
   2914 		{
   2915 		  (*current)->name = name;
   2916 		  free (alloc);
   2917 		}
   2918 	    }
   2919 	  else
   2920 	    bfd_print_symbol (cur_bfd, stdout, *current,
   2921 			      bfd_print_symbol_all);
   2922 	  printf ("\n");
   2923 	}
   2924 
   2925       current++;
   2926     }
   2927   printf ("\n\n");
   2928 }
   2929 
   2930 static void
   2932 dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
   2933 {
   2934   arelent **p;
   2935   char *last_filename, *last_functionname;
   2936   unsigned int last_line;
   2937   unsigned int last_discriminator;
   2938 
   2939   /* Get column headers lined up reasonably.  */
   2940   {
   2941     static int width;
   2942 
   2943     if (width == 0)
   2944       {
   2945 	char buf[30];
   2946 
   2947 	bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
   2948 	width = strlen (buf) - 7;
   2949       }
   2950     printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
   2951   }
   2952 
   2953   last_filename = NULL;
   2954   last_functionname = NULL;
   2955   last_line = 0;
   2956   last_discriminator = 0;
   2957 
   2958   for (p = relpp; relcount && *p != NULL; p++, relcount--)
   2959     {
   2960       arelent *q = *p;
   2961       const char *filename, *functionname;
   2962       unsigned int linenumber;
   2963       unsigned int discriminator;
   2964       const char *sym_name;
   2965       const char *section_name;
   2966       bfd_vma addend2 = 0;
   2967 
   2968       if (start_address != (bfd_vma) -1
   2969 	  && q->address < start_address)
   2970 	continue;
   2971       if (stop_address != (bfd_vma) -1
   2972 	  && q->address > stop_address)
   2973 	continue;
   2974 
   2975       if (with_line_numbers
   2976 	  && sec != NULL
   2977 	  && bfd_find_nearest_line_discriminator (abfd, sec, syms, q->address,
   2978                                                   &filename, &functionname,
   2979                                                   &linenumber, &discriminator))
   2980 	{
   2981 	  if (functionname != NULL
   2982 	      && (last_functionname == NULL
   2983 		  || strcmp (functionname, last_functionname) != 0))
   2984 	    {
   2985 	      printf ("%s():\n", functionname);
   2986 	      if (last_functionname != NULL)
   2987 		free (last_functionname);
   2988 	      last_functionname = xstrdup (functionname);
   2989 	    }
   2990 
   2991 	  if (linenumber > 0
   2992 	      && (linenumber != last_line
   2993 		  || (filename != NULL
   2994 		      && last_filename != NULL
   2995 		      && filename_cmp (filename, last_filename) != 0)
   2996                   || (discriminator != last_discriminator)))
   2997 	    {
   2998               if (discriminator > 0)
   2999                 printf ("%s:%u\n", filename == NULL ? "???" : filename, linenumber);
   3000               else
   3001                 printf ("%s:%u (discriminator %u)\n", filename == NULL ? "???" : filename,
   3002                         linenumber, discriminator);
   3003 	      last_line = linenumber;
   3004 	      last_discriminator = discriminator;
   3005 	      if (last_filename != NULL)
   3006 		free (last_filename);
   3007 	      if (filename == NULL)
   3008 		last_filename = NULL;
   3009 	      else
   3010 		last_filename = xstrdup (filename);
   3011 	    }
   3012 	}
   3013 
   3014       if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
   3015 	{
   3016 	  sym_name = (*(q->sym_ptr_ptr))->name;
   3017 	  section_name = (*(q->sym_ptr_ptr))->section->name;
   3018 	}
   3019       else
   3020 	{
   3021 	  sym_name = NULL;
   3022 	  section_name = NULL;
   3023 	}
   3024 
   3025       bfd_printf_vma (abfd, q->address);
   3026       if (q->howto == NULL)
   3027 	printf (" *unknown*         ");
   3028       else if (q->howto->name)
   3029 	{
   3030 	  const char *name = q->howto->name;
   3031 
   3032 	  /* R_SPARC_OLO10 relocations contain two addends.
   3033 	     But because 'arelent' lacks enough storage to
   3034 	     store them both, the 64-bit ELF Sparc backend
   3035 	     records this as two relocations.  One R_SPARC_LO10
   3036 	     and one R_SPARC_13, both pointing to the same
   3037 	     address.  This is merely so that we have some
   3038 	     place to store both addend fields.
   3039 
   3040 	     Undo this transformation, otherwise the output
   3041 	     will be confusing.  */
   3042 	  if (abfd->xvec->flavour == bfd_target_elf_flavour
   3043 	      && elf_tdata(abfd)->elf_header->e_machine == EM_SPARCV9
   3044 	      && relcount > 1
   3045 	      && !strcmp (q->howto->name, "R_SPARC_LO10"))
   3046 	    {
   3047 	      arelent *q2 = *(p + 1);
   3048 	      if (q2 != NULL
   3049 		  && q2->howto
   3050 		  && q->address == q2->address
   3051 		  && !strcmp (q2->howto->name, "R_SPARC_13"))
   3052 		{
   3053 		  name = "R_SPARC_OLO10";
   3054 		  addend2 = q2->addend;
   3055 		  p++;
   3056 		}
   3057 	    }
   3058 	  printf (" %-16s  ", name);
   3059 	}
   3060       else
   3061 	printf (" %-16d  ", q->howto->type);
   3062 
   3063       if (sym_name)
   3064 	{
   3065 	  objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
   3066 	}
   3067       else
   3068 	{
   3069 	  if (section_name == NULL)
   3070 	    section_name = "*unknown*";
   3071 	  printf ("[%s]", section_name);
   3072 	}
   3073 
   3074       if (q->addend)
   3075 	{
   3076 	  bfd_signed_vma addend = q->addend;
   3077 	  if (addend < 0)
   3078 	    {
   3079 	      printf ("-0x");
   3080 	      addend = -addend;
   3081 	    }
   3082 	  else
   3083 	    printf ("+0x");
   3084 	  bfd_printf_vma (abfd, addend);
   3085 	}
   3086       if (addend2)
   3087 	{
   3088 	  printf ("+0x");
   3089 	  bfd_printf_vma (abfd, addend2);
   3090 	}
   3091 
   3092       printf ("\n");
   3093     }
   3094 
   3095   if (last_filename != NULL)
   3096     free (last_filename);
   3097   if (last_functionname != NULL)
   3098     free (last_functionname);
   3099 }
   3100 
   3101 static void
   3102 dump_relocs_in_section (bfd *abfd,
   3103 			asection *section,
   3104 			void *dummy ATTRIBUTE_UNUSED)
   3105 {
   3106   arelent **relpp;
   3107   long relcount;
   3108   long relsize;
   3109 
   3110   if (   bfd_is_abs_section (section)
   3111       || bfd_is_und_section (section)
   3112       || bfd_is_com_section (section)
   3113       || (! process_section_p (section))
   3114       || ((section->flags & SEC_RELOC) == 0))
   3115     return;
   3116 
   3117   relsize = bfd_get_reloc_upper_bound (abfd, section);
   3118   if (relsize < 0)
   3119     bfd_fatal (bfd_get_filename (abfd));
   3120 
   3121   printf ("RELOCATION RECORDS FOR [%s]:", section->name);
   3122 
   3123   if (relsize == 0)
   3124     {
   3125       printf (" (none)\n\n");
   3126       return;
   3127     }
   3128 
   3129   relpp = (arelent **) xmalloc (relsize);
   3130   relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
   3131 
   3132   if (relcount < 0)
   3133     {
   3134       printf ("\n");
   3135       non_fatal (_("failed to read relocs in: %s"), bfd_get_filename (abfd));
   3136       bfd_fatal (_("error message was"));
   3137     }
   3138   else if (relcount == 0)
   3139     printf (" (none)\n\n");
   3140   else
   3141     {
   3142       printf ("\n");
   3143       dump_reloc_set (abfd, section, relpp, relcount);
   3144       printf ("\n\n");
   3145     }
   3146   free (relpp);
   3147 }
   3148 
   3149 static void
   3150 dump_relocs (bfd *abfd)
   3151 {
   3152   bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
   3153 }
   3154 
   3155 static void
   3156 dump_dynamic_relocs (bfd *abfd)
   3157 {
   3158   long relsize;
   3159   arelent **relpp;
   3160   long relcount;
   3161 
   3162   relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
   3163   if (relsize < 0)
   3164     bfd_fatal (bfd_get_filename (abfd));
   3165 
   3166   printf ("DYNAMIC RELOCATION RECORDS");
   3167 
   3168   if (relsize == 0)
   3169     printf (" (none)\n\n");
   3170   else
   3171     {
   3172       relpp = (arelent **) xmalloc (relsize);
   3173       relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
   3174 
   3175       if (relcount < 0)
   3176 	bfd_fatal (bfd_get_filename (abfd));
   3177       else if (relcount == 0)
   3178 	printf (" (none)\n\n");
   3179       else
   3180 	{
   3181 	  printf ("\n");
   3182 	  dump_reloc_set (abfd, NULL, relpp, relcount);
   3183 	  printf ("\n\n");
   3184 	}
   3185       free (relpp);
   3186     }
   3187 }
   3188 
   3189 /* Creates a table of paths, to search for source files.  */
   3190 
   3191 static void
   3192 add_include_path (const char *path)
   3193 {
   3194   if (path[0] == 0)
   3195     return;
   3196   include_path_count++;
   3197   include_paths = (const char **)
   3198       xrealloc (include_paths, include_path_count * sizeof (*include_paths));
   3199 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
   3200   if (path[1] == ':' && path[2] == 0)
   3201     path = concat (path, ".", (const char *) 0);
   3202 #endif
   3203   include_paths[include_path_count - 1] = path;
   3204 }
   3205 
   3206 static void
   3207 adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
   3208 		  asection *section,
   3209 		  void *arg)
   3210 {
   3211   if ((section->flags & SEC_DEBUGGING) == 0)
   3212     {
   3213       bfd_boolean *has_reloc_p = (bfd_boolean *) arg;
   3214       section->vma += adjust_section_vma;
   3215       if (*has_reloc_p)
   3216 	section->lma += adjust_section_vma;
   3217     }
   3218 }
   3219 
   3220 /* Dump selected contents of ABFD.  */
   3221 
   3222 static void
   3223 dump_bfd (bfd *abfd)
   3224 {
   3225   /* If we are adjusting section VMA's, change them all now.  Changing
   3226      the BFD information is a hack.  However, we must do it, or
   3227      bfd_find_nearest_line will not do the right thing.  */
   3228   if (adjust_section_vma != 0)
   3229     {
   3230       bfd_boolean has_reloc = (abfd->flags & HAS_RELOC);
   3231       bfd_map_over_sections (abfd, adjust_addresses, &has_reloc);
   3232     }
   3233 
   3234   if (! dump_debugging_tags && ! suppress_bfd_header)
   3235     printf (_("\n%s:     file format %s\n"), bfd_get_filename (abfd),
   3236 	    abfd->xvec->name);
   3237   if (dump_ar_hdrs)
   3238     print_arelt_descr (stdout, abfd, TRUE);
   3239   if (dump_file_header)
   3240     dump_bfd_header (abfd);
   3241   if (dump_private_headers)
   3242     dump_bfd_private_header (abfd);
   3243   if (dump_private_options != NULL)
   3244     dump_target_specific (abfd);
   3245   if (! dump_debugging_tags && ! suppress_bfd_header)
   3246     putchar ('\n');
   3247 
   3248   if (dump_symtab
   3249       || dump_reloc_info
   3250       || disassemble
   3251       || dump_debugging
   3252       || dump_dwarf_section_info)
   3253     syms = slurp_symtab (abfd);
   3254 
   3255   if (dump_section_headers)
   3256     dump_headers (abfd);
   3257 
   3258   if (dump_dynamic_symtab || dump_dynamic_reloc_info
   3259       || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
   3260     dynsyms = slurp_dynamic_symtab (abfd);
   3261   if (disassemble)
   3262     {
   3263       synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
   3264 					     dynsymcount, dynsyms, &synthsyms);
   3265       if (synthcount < 0)
   3266 	synthcount = 0;
   3267     }
   3268 
   3269   if (dump_symtab)
   3270     dump_symbols (abfd, FALSE);
   3271   if (dump_dynamic_symtab)
   3272     dump_symbols (abfd, TRUE);
   3273   if (dump_dwarf_section_info)
   3274     dump_dwarf (abfd);
   3275   if (dump_stab_section_info)
   3276     dump_stabs (abfd);
   3277   if (dump_reloc_info && ! disassemble)
   3278     dump_relocs (abfd);
   3279   if (dump_dynamic_reloc_info && ! disassemble)
   3280     dump_dynamic_relocs (abfd);
   3281   if (dump_section_contents)
   3282     dump_data (abfd);
   3283   if (disassemble)
   3284     disassemble_data (abfd);
   3285 
   3286   if (dump_debugging)
   3287     {
   3288       void *dhandle;
   3289 
   3290       dhandle = read_debugging_info (abfd, syms, symcount, TRUE);
   3291       if (dhandle != NULL)
   3292 	{
   3293 	  if (!print_debugging_info (stdout, dhandle, abfd, syms,
   3294 				     bfd_demangle,
   3295 				     dump_debugging_tags ? TRUE : FALSE))
   3296 	    {
   3297 	      non_fatal (_("%s: printing debugging information failed"),
   3298 			 bfd_get_filename (abfd));
   3299 	      exit_status = 1;
   3300 	    }
   3301 	}
   3302       /* PR 6483: If there was no STABS or IEEE debug
   3303 	 info in the file, try DWARF instead.  */
   3304       else if (! dump_dwarf_section_info)
   3305 	{
   3306 	  dwarf_select_sections_all ();
   3307 	  dump_dwarf (abfd);
   3308 	}
   3309     }
   3310 
   3311   if (syms)
   3312     {
   3313       free (syms);
   3314       syms = NULL;
   3315     }
   3316 
   3317   if (dynsyms)
   3318     {
   3319       free (dynsyms);
   3320       dynsyms = NULL;
   3321     }
   3322 
   3323   if (synthsyms)
   3324     {
   3325       free (synthsyms);
   3326       synthsyms = NULL;
   3327     }
   3328 
   3329   symcount = 0;
   3330   dynsymcount = 0;
   3331   synthcount = 0;
   3332 }
   3333 
   3334 static void
   3335 display_object_bfd (bfd *abfd)
   3336 {
   3337   char **matching;
   3338 
   3339   if (bfd_check_format_matches (abfd, bfd_object, &matching))
   3340     {
   3341       dump_bfd (abfd);
   3342       return;
   3343     }
   3344 
   3345   if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
   3346     {
   3347       nonfatal (bfd_get_filename (abfd));
   3348       list_matching_formats (matching);
   3349       free (matching);
   3350       return;
   3351     }
   3352 
   3353   if (bfd_get_error () != bfd_error_file_not_recognized)
   3354     {
   3355       nonfatal (bfd_get_filename (abfd));
   3356       return;
   3357     }
   3358 
   3359   if (bfd_check_format_matches (abfd, bfd_core, &matching))
   3360     {
   3361       dump_bfd (abfd);
   3362       return;
   3363     }
   3364 
   3365   nonfatal (bfd_get_filename (abfd));
   3366 
   3367   if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
   3368     {
   3369       list_matching_formats (matching);
   3370       free (matching);
   3371     }
   3372 }
   3373 
   3374 static void
   3375 display_any_bfd (bfd *file, int level)
   3376 {
   3377   /* Decompress sections unless dumping the section contents.  */
   3378   if (!dump_section_contents)
   3379     file->flags |= BFD_DECOMPRESS;
   3380 
   3381   /* If the file is an archive, process all of its elements.  */
   3382   if (bfd_check_format (file, bfd_archive))
   3383     {
   3384       bfd *arfile = NULL;
   3385       bfd *last_arfile = NULL;
   3386 
   3387       if (level == 0)
   3388         printf (_("In archive %s:\n"), bfd_get_filename (file));
   3389       else
   3390         printf (_("In nested archive %s:\n"), bfd_get_filename (file));
   3391 
   3392       for (;;)
   3393 	{
   3394 	  bfd_set_error (bfd_error_no_error);
   3395 
   3396 	  arfile = bfd_openr_next_archived_file (file, arfile);
   3397 	  if (arfile == NULL)
   3398 	    {
   3399 	      if (bfd_get_error () != bfd_error_no_more_archived_files)
   3400 		nonfatal (bfd_get_filename (file));
   3401 	      break;
   3402 	    }
   3403 
   3404 	  display_any_bfd (arfile, level + 1);
   3405 
   3406 	  if (last_arfile != NULL)
   3407 	    bfd_close (last_arfile);
   3408 	  last_arfile = arfile;
   3409 	}
   3410 
   3411       if (last_arfile != NULL)
   3412 	bfd_close (last_arfile);
   3413     }
   3414   else
   3415     display_object_bfd (file);
   3416 }
   3417 
   3418 static void
   3419 display_file (char *filename, char *target)
   3420 {
   3421   bfd *file;
   3422 
   3423   if (get_file_size (filename) < 1)
   3424     {
   3425       exit_status = 1;
   3426       return;
   3427     }
   3428 
   3429   file = bfd_openr (filename, target);
   3430   if (file == NULL)
   3431     {
   3432       nonfatal (filename);
   3433       return;
   3434     }
   3435 
   3436   display_any_bfd (file, 0);
   3437 
   3438   bfd_close (file);
   3439 }
   3440 
   3441 int
   3443 main (int argc, char **argv)
   3444 {
   3445   int c;
   3446   char *target = default_target;
   3447   bfd_boolean seenflag = FALSE;
   3448 
   3449 #if defined (HAVE_SETLOCALE)
   3450 #if defined (HAVE_LC_MESSAGES)
   3451   setlocale (LC_MESSAGES, "");
   3452 #endif
   3453   setlocale (LC_CTYPE, "");
   3454 #endif
   3455 
   3456   bindtextdomain (PACKAGE, LOCALEDIR);
   3457   textdomain (PACKAGE);
   3458 
   3459   program_name = *argv;
   3460   xmalloc_set_program_name (program_name);
   3461 
   3462   START_PROGRESS (program_name, 0);
   3463 
   3464   expandargv (&argc, &argv);
   3465 
   3466   bfd_init ();
   3467   set_default_bfd_target ();
   3468 
   3469   while ((c = getopt_long (argc, argv,
   3470 			   "pP:ib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::",
   3471 			   long_options, (int *) 0))
   3472 	 != EOF)
   3473     {
   3474       switch (c)
   3475 	{
   3476 	case 0:
   3477 	  break;		/* We've been given a long option.  */
   3478 	case 'm':
   3479 	  machine = optarg;
   3480 	  break;
   3481 	case 'M':
   3482 	  if (disassembler_options)
   3483 	    /* Ignore potential memory leak for now.  */
   3484 	    disassembler_options = concat (disassembler_options, ",",
   3485 					   optarg, (const char *) NULL);
   3486 	  else
   3487 	    disassembler_options = optarg;
   3488 	  break;
   3489 	case 'j':
   3490 	  add_only (optarg);
   3491 	  break;
   3492 	case 'F':
   3493 	  display_file_offsets = TRUE;
   3494 	  break;
   3495 	case 'l':
   3496 	  with_line_numbers = TRUE;
   3497 	  break;
   3498 	case 'b':
   3499 	  target = optarg;
   3500 	  break;
   3501 	case 'C':
   3502 	  do_demangle = TRUE;
   3503 	  if (optarg != NULL)
   3504 	    {
   3505 	      enum demangling_styles style;
   3506 
   3507 	      style = cplus_demangle_name_to_style (optarg);
   3508 	      if (style == unknown_demangling)
   3509 		fatal (_("unknown demangling style `%s'"),
   3510 		       optarg);
   3511 
   3512 	      cplus_demangle_set_style (style);
   3513 	    }
   3514 	  break;
   3515 	case 'w':
   3516 	  wide_output = TRUE;
   3517 	  break;
   3518 	case OPTION_ADJUST_VMA:
   3519 	  adjust_section_vma = parse_vma (optarg, "--adjust-vma");
   3520 	  break;
   3521 	case OPTION_START_ADDRESS:
   3522 	  start_address = parse_vma (optarg, "--start-address");
   3523 	  if ((stop_address != (bfd_vma) -1) && stop_address <= start_address)
   3524 	    fatal (_("error: the start address should be before the end address"));
   3525 	  break;
   3526 	case OPTION_STOP_ADDRESS:
   3527 	  stop_address = parse_vma (optarg, "--stop-address");
   3528 	  if ((start_address != (bfd_vma) -1) && stop_address <= start_address)
   3529 	    fatal (_("error: the stop address should be after the start address"));
   3530 	  break;
   3531 	case OPTION_PREFIX:
   3532 	  prefix = optarg;
   3533 	  prefix_length = strlen (prefix);
   3534 	  /* Remove an unnecessary trailing '/' */
   3535 	  while (IS_DIR_SEPARATOR (prefix[prefix_length - 1]))
   3536 	    prefix_length--;
   3537 	  break;
   3538 	case OPTION_PREFIX_STRIP:
   3539 	  prefix_strip = atoi (optarg);
   3540 	  if (prefix_strip < 0)
   3541 	    fatal (_("error: prefix strip must be non-negative"));
   3542 	  break;
   3543 	case OPTION_INSN_WIDTH:
   3544 	  insn_width = strtoul (optarg, NULL, 0);
   3545 	  if (insn_width <= 0)
   3546 	    fatal (_("error: instruction width must be positive"));
   3547 	  break;
   3548 	case 'E':
   3549 	  if (strcmp (optarg, "B") == 0)
   3550 	    endian = BFD_ENDIAN_BIG;
   3551 	  else if (strcmp (optarg, "L") == 0)
   3552 	    endian = BFD_ENDIAN_LITTLE;
   3553 	  else
   3554 	    {
   3555 	      nonfatal (_("unrecognized -E option"));
   3556 	      usage (stderr, 1);
   3557 	    }
   3558 	  break;
   3559 	case OPTION_ENDIAN:
   3560 	  if (strncmp (optarg, "big", strlen (optarg)) == 0)
   3561 	    endian = BFD_ENDIAN_BIG;
   3562 	  else if (strncmp (optarg, "little", strlen (optarg)) == 0)
   3563 	    endian = BFD_ENDIAN_LITTLE;
   3564 	  else
   3565 	    {
   3566 	      non_fatal (_("unrecognized --endian type `%s'"), optarg);
   3567 	      exit_status = 1;
   3568 	      usage (stderr, 1);
   3569 	    }
   3570 	  break;
   3571 
   3572 	case 'f':
   3573 	  dump_file_header = TRUE;
   3574 	  seenflag = TRUE;
   3575 	  break;
   3576 	case 'i':
   3577 	  formats_info = TRUE;
   3578 	  seenflag = TRUE;
   3579 	  break;
   3580 	case 'I':
   3581 	  add_include_path (optarg);
   3582 	  break;
   3583 	case 'p':
   3584 	  dump_private_headers = TRUE;
   3585 	  seenflag = TRUE;
   3586 	  break;
   3587 	case 'P':
   3588 	  dump_private_options = optarg;
   3589 	  seenflag = TRUE;
   3590 	  break;
   3591 	case 'x':
   3592 	  dump_private_headers = TRUE;
   3593 	  dump_symtab = TRUE;
   3594 	  dump_reloc_info = TRUE;
   3595 	  dump_file_header = TRUE;
   3596 	  dump_ar_hdrs = TRUE;
   3597 	  dump_section_headers = TRUE;
   3598 	  seenflag = TRUE;
   3599 	  break;
   3600 	case 't':
   3601 	  dump_symtab = TRUE;
   3602 	  seenflag = TRUE;
   3603 	  break;
   3604 	case 'T':
   3605 	  dump_dynamic_symtab = TRUE;
   3606 	  seenflag = TRUE;
   3607 	  break;
   3608 	case 'd':
   3609 	  disassemble = TRUE;
   3610 	  seenflag = TRUE;
   3611 	  break;
   3612 	case 'z':
   3613 	  disassemble_zeroes = TRUE;
   3614 	  break;
   3615 	case 'D':
   3616 	  disassemble = TRUE;
   3617 	  disassemble_all = TRUE;
   3618 	  seenflag = TRUE;
   3619 	  break;
   3620 	case 'S':
   3621 	  disassemble = TRUE;
   3622 	  with_source_code = TRUE;
   3623 	  seenflag = TRUE;
   3624 	  break;
   3625 	case 'g':
   3626 	  dump_debugging = 1;
   3627 	  seenflag = TRUE;
   3628 	  break;
   3629 	case 'e':
   3630 	  dump_debugging = 1;
   3631 	  dump_debugging_tags = 1;
   3632 	  do_demangle = TRUE;
   3633 	  seenflag = TRUE;
   3634 	  break;
   3635 	case 'W':
   3636 	  dump_dwarf_section_info = TRUE;
   3637 	  seenflag = TRUE;
   3638 	  if (optarg)
   3639 	    dwarf_select_sections_by_letters (optarg);
   3640 	  else
   3641 	    dwarf_select_sections_all ();
   3642 	  break;
   3643 	case OPTION_DWARF:
   3644 	  dump_dwarf_section_info = TRUE;
   3645 	  seenflag = TRUE;
   3646 	  if (optarg)
   3647 	    dwarf_select_sections_by_names (optarg);
   3648 	  else
   3649 	    dwarf_select_sections_all ();
   3650 	  break;
   3651 	case OPTION_DWARF_DEPTH:
   3652 	  {
   3653 	    char *cp;
   3654 	    dwarf_cutoff_level = strtoul (optarg, & cp, 0);
   3655 	  }
   3656 	  break;
   3657 	case OPTION_DWARF_START:
   3658 	  {
   3659 	    char *cp;
   3660 	    dwarf_start_die = strtoul (optarg, & cp, 0);
   3661 	    suppress_bfd_header = 1;
   3662 	  }
   3663 	  break;
   3664 	case OPTION_DWARF_CHECK:
   3665 	  dwarf_check = TRUE;
   3666 	  break;
   3667 	case 'G':
   3668 	  dump_stab_section_info = TRUE;
   3669 	  seenflag = TRUE;
   3670 	  break;
   3671 	case 's':
   3672 	  dump_section_contents = TRUE;
   3673 	  seenflag = TRUE;
   3674 	  break;
   3675 	case 'r':
   3676 	  dump_reloc_info = TRUE;
   3677 	  seenflag = TRUE;
   3678 	  break;
   3679 	case 'R':
   3680 	  dump_dynamic_reloc_info = TRUE;
   3681 	  seenflag = TRUE;
   3682 	  break;
   3683 	case 'a':
   3684 	  dump_ar_hdrs = TRUE;
   3685 	  seenflag = TRUE;
   3686 	  break;
   3687 	case 'h':
   3688 	  dump_section_headers = TRUE;
   3689 	  seenflag = TRUE;
   3690 	  break;
   3691 	case 'v':
   3692 	case 'V':
   3693 	  show_version = TRUE;
   3694 	  seenflag = TRUE;
   3695 	  break;
   3696 
   3697 	case 'H':
   3698 	  usage (stdout, 0);
   3699 	  /* No need to set seenflag or to break - usage() does not return.  */
   3700 	default:
   3701 	  usage (stderr, 1);
   3702 	}
   3703     }
   3704 
   3705   if (show_version)
   3706     print_version ("objdump");
   3707 
   3708   if (!seenflag)
   3709     usage (stderr, 2);
   3710 
   3711   if (formats_info)
   3712     exit_status = display_info ();
   3713   else
   3714     {
   3715       if (optind == argc)
   3716 	display_file ("a.out", target);
   3717       else
   3718 	for (; optind < argc;)
   3719 	  display_file (argv[optind++], target);
   3720     }
   3721 
   3722   free_only_list ();
   3723 
   3724   END_PROGRESS (program_name);
   3725 
   3726   return exit_status;
   3727 }
   3728