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