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