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