Home | History | Annotate | Line # | Download | only in binutils
objdump.c revision 1.8
      1  1.1  christos /* objdump.c -- dump information about an object file.
      2  1.8  christos    Copyright (C) 1990-2022 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.8  christos #include "demanguse.h"
     59  1.1  christos #include "dwarf.h"
     60  1.7  christos #include "ctf-api.h"
     61  1.1  christos #include "getopt.h"
     62  1.1  christos #include "safe-ctype.h"
     63  1.1  christos #include "dis-asm.h"
     64  1.1  christos #include "libiberty.h"
     65  1.1  christos #include "demangle.h"
     66  1.1  christos #include "filenames.h"
     67  1.1  christos #include "debug.h"
     68  1.1  christos #include "budbg.h"
     69  1.1  christos #include "objdump.h"
     70  1.1  christos 
     71  1.1  christos #ifdef HAVE_MMAP
     72  1.1  christos #include <sys/mman.h>
     73  1.1  christos #endif
     74  1.1  christos 
     75  1.1  christos /* Internal headers for the ELF .stab-dump code - sorry.  */
     76  1.1  christos #define	BYTES_IN_WORD	32
     77  1.1  christos #include "aout/aout64.h"
     78  1.1  christos 
     79  1.1  christos /* Exit status.  */
     80  1.1  christos static int exit_status = 0;
     81  1.1  christos 
     82  1.1  christos static char *default_target = NULL;	/* Default at runtime.  */
     83  1.1  christos 
     84  1.1  christos /* The following variables are set based on arguments passed on the
     85  1.1  christos    command line.  */
     86  1.1  christos static int show_version = 0;		/* Show the version number.  */
     87  1.1  christos static int dump_section_contents;	/* -s */
     88  1.1  christos static int dump_section_headers;	/* -h */
     89  1.8  christos static bool dump_file_header;		/* -f */
     90  1.1  christos static int dump_symtab;			/* -t */
     91  1.1  christos static int dump_dynamic_symtab;		/* -T */
     92  1.1  christos static int dump_reloc_info;		/* -r */
     93  1.1  christos static int dump_dynamic_reloc_info;	/* -R */
     94  1.1  christos static int dump_ar_hdrs;		/* -a */
     95  1.1  christos static int dump_private_headers;	/* -p */
     96  1.1  christos static char *dump_private_options;	/* -P */
     97  1.8  christos static int no_addresses;		/* --no-addresses */
     98  1.1  christos static int prefix_addresses;		/* --prefix-addresses */
     99  1.1  christos static int with_line_numbers;		/* -l */
    100  1.8  christos static bool with_source_code;		/* -S */
    101  1.1  christos static int show_raw_insn;		/* --show-raw-insn */
    102  1.1  christos static int dump_dwarf_section_info;	/* --dwarf */
    103  1.1  christos static int dump_stab_section_info;	/* --stabs */
    104  1.7  christos static int dump_ctf_section_info;       /* --ctf */
    105  1.7  christos static char *dump_ctf_section_name;
    106  1.7  christos static char *dump_ctf_parent_name;	/* --ctf-parent */
    107  1.1  christos static int do_demangle;			/* -C, --demangle */
    108  1.8  christos static bool disassemble;		/* -d */
    109  1.8  christos static bool disassemble_all;		/* -D */
    110  1.1  christos static int disassemble_zeroes;		/* --disassemble-zeroes */
    111  1.8  christos static bool formats_info;		/* -i */
    112  1.1  christos static int wide_output;			/* -w */
    113  1.1  christos static int insn_width;			/* --insn-width */
    114  1.1  christos static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
    115  1.1  christos static bfd_vma stop_address = (bfd_vma) -1;  /* --stop-address */
    116  1.1  christos static int dump_debugging;		/* --debugging */
    117  1.1  christos static int dump_debugging_tags;		/* --debugging-tags */
    118  1.1  christos static int suppress_bfd_header;
    119  1.1  christos static int dump_special_syms = 0;	/* --special-syms */
    120  1.1  christos static bfd_vma adjust_section_vma = 0;	/* --adjust-vma */
    121  1.1  christos static int file_start_context = 0;      /* --file-start-context */
    122  1.8  christos static bool display_file_offsets;	/* -F */
    123  1.1  christos static const char *prefix;		/* --prefix */
    124  1.1  christos static int prefix_strip;		/* --prefix-strip */
    125  1.1  christos static size_t prefix_length;
    126  1.8  christos static bool unwind_inlines;		/* --inlines.  */
    127  1.7  christos static const char * disasm_sym;		/* Disassembly start symbol.  */
    128  1.7  christos static const char * source_comment;     /* --source_comment.  */
    129  1.8  christos static bool visualize_jumps = false;	/* --visualize-jumps.  */
    130  1.8  christos static bool color_output = false;	/* --visualize-jumps=color.  */
    131  1.8  christos static bool extended_color_output = false; /* --visualize-jumps=extended-color.  */
    132  1.8  christos static int process_links = false;       /* --process-links.  */
    133  1.8  christos static bool disassembler_color = false; /* --disassembler-color=color.  */
    134  1.8  christos static bool disassembler_extended_color = false; /* --disassembler-color=extended-color.  */
    135  1.7  christos 
    136  1.8  christos static int dump_any_debugging;
    137  1.7  christos static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
    138  1.1  christos 
    139  1.8  christos /* This is reset to false each time we enter the disassembler, and set true
    140  1.8  christos    when the disassembler emits something in the dis_style_comment_start
    141  1.8  christos    style.  Once this is true, all further output on that line is done in
    142  1.8  christos    the comment style.  This only has an effect when disassembler coloring
    143  1.8  christos    is turned on.  */
    144  1.8  christos static bool disassembler_in_comment = false;
    145  1.8  christos 
    146  1.1  christos /* A structure to record the sections mentioned in -j switches.  */
    147  1.1  christos struct only
    148  1.1  christos {
    149  1.8  christos   const char *name; /* The name of the section.  */
    150  1.8  christos   bool seen; /* A flag to indicate that the section has been found in one or more input files.  */
    151  1.8  christos   struct only *next; /* Pointer to the next structure in the list.  */
    152  1.1  christos };
    153  1.1  christos /* Pointer to an array of 'only' structures.
    154  1.1  christos    This pointer is NULL if the -j switch has not been used.  */
    155  1.1  christos static struct only * only_list = NULL;
    156  1.1  christos 
    157  1.1  christos /* Variables for handling include file path table.  */
    158  1.1  christos static const char **include_paths;
    159  1.1  christos static int include_path_count;
    160  1.1  christos 
    161  1.1  christos /* Extra info to pass to the section disassembler and address printing
    162  1.1  christos    function.  */
    163  1.1  christos struct objdump_disasm_info
    164  1.1  christos {
    165  1.8  christos   bfd *abfd;
    166  1.8  christos   bool require_sec;
    167  1.1  christos   disassembler_ftype disassemble_fn;
    168  1.8  christos   arelent *reloc;
    169  1.8  christos   const char *symbol;
    170  1.1  christos };
    171  1.1  christos 
    172  1.1  christos /* Architecture to disassemble for, or default if NULL.  */
    173  1.1  christos static char *machine = NULL;
    174  1.1  christos 
    175  1.1  christos /* Target specific options to the disassembler.  */
    176  1.1  christos static char *disassembler_options = NULL;
    177  1.1  christos 
    178  1.1  christos /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN.  */
    179  1.1  christos static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
    180  1.1  christos 
    181  1.1  christos /* The symbol table.  */
    182  1.1  christos static asymbol **syms;
    183  1.1  christos 
    184  1.1  christos /* Number of symbols in `syms'.  */
    185  1.1  christos static long symcount = 0;
    186  1.1  christos 
    187  1.1  christos /* The sorted symbol table.  */
    188  1.1  christos static asymbol **sorted_syms;
    189  1.1  christos 
    190  1.1  christos /* Number of symbols in `sorted_syms'.  */
    191  1.1  christos static long sorted_symcount = 0;
    192  1.1  christos 
    193  1.1  christos /* The dynamic symbol table.  */
    194  1.1  christos static asymbol **dynsyms;
    195  1.1  christos 
    196  1.1  christos /* The synthetic symbol table.  */
    197  1.1  christos static asymbol *synthsyms;
    198  1.1  christos static long synthcount = 0;
    199  1.1  christos 
    200  1.1  christos /* Number of symbols in `dynsyms'.  */
    201  1.1  christos static long dynsymcount = 0;
    202  1.1  christos 
    203  1.1  christos static bfd_byte *stabs;
    204  1.1  christos static bfd_size_type stab_size;
    205  1.1  christos 
    206  1.6  christos static bfd_byte *strtab;
    207  1.1  christos static bfd_size_type stabstr_size;
    208  1.1  christos 
    209  1.1  christos /* Handlers for -P/--private.  */
    210  1.1  christos static const struct objdump_private_desc * const objdump_private_vectors[] =
    211  1.1  christos   {
    212  1.1  christos     OBJDUMP_PRIVATE_VECTORS
    213  1.1  christos     NULL
    214  1.1  christos   };
    215  1.7  christos 
    216  1.7  christos /* The list of detected jumps inside a function.  */
    217  1.7  christos static struct jump_info *detected_jumps = NULL;
    218  1.8  christos 
    219  1.8  christos typedef enum unicode_display_type
    220  1.8  christos {
    221  1.8  christos   unicode_default = 0,
    222  1.8  christos   unicode_locale,
    223  1.8  christos   unicode_escape,
    224  1.8  christos   unicode_hex,
    225  1.8  christos   unicode_highlight,
    226  1.8  christos   unicode_invalid
    227  1.8  christos } unicode_display_type;
    228  1.8  christos 
    229  1.8  christos static unicode_display_type unicode_display = unicode_default;
    230  1.1  christos 
    231  1.3  christos static void usage (FILE *, int) ATTRIBUTE_NORETURN;
    233  1.1  christos static void
    234  1.1  christos usage (FILE *stream, int status)
    235  1.1  christos {
    236  1.1  christos   fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
    237  1.1  christos   fprintf (stream, _(" Display information from object <file(s)>.\n"));
    238  1.1  christos   fprintf (stream, _(" At least one of the following switches must be given:\n"));
    239  1.8  christos   fprintf (stream, _("\
    240  1.8  christos   -a, --archive-headers    Display archive header information\n"));
    241  1.8  christos   fprintf (stream, _("\
    242  1.8  christos   -f, --file-headers       Display the contents of the overall file header\n"));
    243  1.8  christos   fprintf (stream, _("\
    244  1.8  christos   -p, --private-headers    Display object format specific file header contents\n"));
    245  1.8  christos   fprintf (stream, _("\
    246  1.8  christos   -P, --private=OPT,OPT... Display object format specific contents\n"));
    247  1.8  christos   fprintf (stream, _("\
    248  1.8  christos   -h, --[section-]headers  Display the contents of the section headers\n"));
    249  1.8  christos   fprintf (stream, _("\
    250  1.8  christos   -x, --all-headers        Display the contents of all headers\n"));
    251  1.8  christos   fprintf (stream, _("\
    252  1.8  christos   -d, --disassemble        Display assembler contents of executable sections\n"));
    253  1.8  christos   fprintf (stream, _("\
    254  1.8  christos   -D, --disassemble-all    Display assembler contents of all sections\n"));
    255  1.8  christos   fprintf (stream, _("\
    256  1.8  christos       --disassemble=<sym>  Display assembler contents from <sym>\n"));
    257  1.8  christos   fprintf (stream, _("\
    258  1.8  christos   -S, --source             Intermix source code with disassembly\n"));
    259  1.8  christos   fprintf (stream, _("\
    260  1.8  christos       --source-comment[=<txt>] Prefix lines of source code with <txt>\n"));
    261  1.8  christos   fprintf (stream, _("\
    262  1.8  christos   -s, --full-contents      Display the full contents of all sections requested\n"));
    263  1.8  christos   fprintf (stream, _("\
    264  1.8  christos   -g, --debugging          Display debug information in object file\n"));
    265  1.8  christos   fprintf (stream, _("\
    266  1.8  christos   -e, --debugging-tags     Display debug information using ctags style\n"));
    267  1.8  christos   fprintf (stream, _("\
    268  1.8  christos   -G, --stabs              Display (in raw form) any STABS info in the file\n"));
    269  1.8  christos   fprintf (stream, _("\
    270  1.8  christos   -W, --dwarf[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
    271  1.8  christos               f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
    272  1.8  christos               m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
    273  1.8  christos               s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
    274  1.8  christos               U/=trace_info]\n\
    275  1.8  christos                            Display the contents of DWARF debug sections\n"));
    276  1.8  christos   fprintf (stream, _("\
    277  1.8  christos   -Wk,--dwarf=links        Display the contents of sections that link to\n\
    278  1.8  christos                             separate debuginfo files\n"));
    279  1.8  christos #if DEFAULT_FOR_FOLLOW_LINKS
    280  1.8  christos   fprintf (stream, _("\
    281  1.8  christos   -WK,--dwarf=follow-links\n\
    282  1.8  christos                            Follow links to separate debug info files (default)\n"));
    283  1.8  christos   fprintf (stream, _("\
    284  1.8  christos   -WN,--dwarf=no-follow-links\n\
    285  1.8  christos                            Do not follow links to separate debug info files\n"));
    286  1.8  christos #else
    287  1.8  christos   fprintf (stream, _("\
    288  1.8  christos   -WK,--dwarf=follow-links\n\
    289  1.8  christos                            Follow links to separate debug info files\n"));
    290  1.8  christos   fprintf (stream, _("\
    291  1.8  christos   -WN,--dwarf=no-follow-links\n\
    292  1.8  christos                            Do not follow links to separate debug info files\n\
    293  1.8  christos                             (default)\n"));
    294  1.8  christos #endif
    295  1.8  christos #if HAVE_LIBDEBUGINFOD
    296  1.8  christos   fprintf (stream, _("\
    297  1.8  christos   -WD --dwarf=use-debuginfod\n\
    298  1.8  christos                            When following links, also query debuginfod servers (default)\n"));
    299  1.8  christos   fprintf (stream, _("\
    300  1.8  christos   -WE --dwarf=do-not-use-debuginfod\n\
    301  1.8  christos                            When following links, do not query debuginfod servers\n"));
    302  1.8  christos #endif
    303  1.8  christos   fprintf (stream, _("\
    304  1.8  christos   -L, --process-links      Display the contents of non-debug sections in\n\
    305  1.8  christos                             separate debuginfo files.  (Implies -WK)\n"));
    306  1.8  christos #ifdef ENABLE_LIBCTF
    307  1.8  christos   fprintf (stream, _("\
    308  1.8  christos       --ctf[=SECTION]      Display CTF info from SECTION, (default `.ctf')\n"));
    309  1.8  christos #endif
    310  1.8  christos   fprintf (stream, _("\
    311  1.8  christos   -t, --syms               Display the contents of the symbol table(s)\n"));
    312  1.8  christos   fprintf (stream, _("\
    313  1.8  christos   -T, --dynamic-syms       Display the contents of the dynamic symbol table\n"));
    314  1.8  christos   fprintf (stream, _("\
    315  1.8  christos   -r, --reloc              Display the relocation entries in the file\n"));
    316  1.8  christos   fprintf (stream, _("\
    317  1.8  christos   -R, --dynamic-reloc      Display the dynamic relocation entries in the file\n"));
    318  1.8  christos   fprintf (stream, _("\
    319  1.8  christos   @<file>                  Read options from <file>\n"));
    320  1.8  christos   fprintf (stream, _("\
    321  1.8  christos   -v, --version            Display this program's version number\n"));
    322  1.8  christos   fprintf (stream, _("\
    323  1.8  christos   -i, --info               List object formats and architectures supported\n"));
    324  1.8  christos   fprintf (stream, _("\
    325  1.8  christos   -H, --help               Display this information\n"));
    326  1.1  christos 
    327  1.1  christos   if (status != 2)
    328  1.1  christos     {
    329  1.1  christos       const struct objdump_private_desc * const *desc;
    330  1.1  christos 
    331  1.1  christos       fprintf (stream, _("\n The following switches are optional:\n"));
    332  1.8  christos       fprintf (stream, _("\
    333  1.8  christos   -b, --target=BFDNAME           Specify the target object format as BFDNAME\n"));
    334  1.8  christos       fprintf (stream, _("\
    335  1.8  christos   -m, --architecture=MACHINE     Specify the target architecture as MACHINE\n"));
    336  1.8  christos       fprintf (stream, _("\
    337  1.8  christos   -j, --section=NAME             Only display information for section NAME\n"));
    338  1.8  christos       fprintf (stream, _("\
    339  1.8  christos   -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n"));
    340  1.8  christos       fprintf (stream, _("\
    341  1.8  christos   -EB --endian=big               Assume big endian format when disassembling\n"));
    342  1.8  christos       fprintf (stream, _("\
    343  1.8  christos   -EL --endian=little            Assume little endian format when disassembling\n"));
    344  1.8  christos       fprintf (stream, _("\
    345  1.8  christos       --file-start-context       Include context from start of file (with -S)\n"));
    346  1.8  christos       fprintf (stream, _("\
    347  1.8  christos   -I, --include=DIR              Add DIR to search list for source files\n"));
    348  1.8  christos       fprintf (stream, _("\
    349  1.8  christos   -l, --line-numbers             Include line numbers and filenames in output\n"));
    350  1.8  christos       fprintf (stream, _("\
    351  1.8  christos   -F, --file-offsets             Include file offsets when displaying information\n"));
    352  1.8  christos       fprintf (stream, _("\
    353  1.8  christos   -C, --demangle[=STYLE]         Decode mangled/processed symbol names\n"));
    354  1.8  christos       display_demangler_styles (stream, _("\
    355  1.8  christos                                    STYLE can be "));
    356  1.8  christos       fprintf (stream, _("\
    357  1.8  christos       --recurse-limit            Enable a limit on recursion whilst demangling\n\
    358  1.8  christos                                   (default)\n"));
    359  1.8  christos       fprintf (stream, _("\
    360  1.8  christos       --no-recurse-limit         Disable a limit on recursion whilst demangling\n"));
    361  1.8  christos       fprintf (stream, _("\
    362  1.8  christos   -w, --wide                     Format output for more than 80 columns\n"));
    363  1.8  christos       fprintf (stream, _("\
    364  1.8  christos   -U[d|l|i|x|e|h]                Controls the display of UTF-8 unicode characters\n\
    365  1.8  christos   --unicode=[default|locale|invalid|hex|escape|highlight]\n"));
    366  1.8  christos       fprintf (stream, _("\
    367  1.8  christos   -z, --disassemble-zeroes       Do not skip blocks of zeroes when disassembling\n"));
    368  1.8  christos       fprintf (stream, _("\
    369  1.8  christos       --start-address=ADDR       Only process data whose address is >= ADDR\n"));
    370  1.8  christos       fprintf (stream, _("\
    371  1.8  christos       --stop-address=ADDR        Only process data whose address is < ADDR\n"));
    372  1.8  christos       fprintf (stream, _("\
    373  1.8  christos       --no-addresses             Do not print address alongside disassembly\n"));
    374  1.8  christos       fprintf (stream, _("\
    375  1.8  christos       --prefix-addresses         Print complete address alongside disassembly\n"));
    376  1.8  christos       fprintf (stream, _("\
    377  1.8  christos       --[no-]show-raw-insn       Display hex alongside symbolic disassembly\n"));
    378  1.8  christos       fprintf (stream, _("\
    379  1.8  christos       --insn-width=WIDTH         Display WIDTH bytes on a single line for -d\n"));
    380  1.8  christos       fprintf (stream, _("\
    381  1.8  christos       --adjust-vma=OFFSET        Add OFFSET to all displayed section addresses\n"));
    382  1.8  christos       fprintf (stream, _("\
    383  1.8  christos       --special-syms             Include special symbols in symbol dumps\n"));
    384  1.8  christos       fprintf (stream, _("\
    385  1.8  christos       --inlines                  Print all inlines for source line (with -l)\n"));
    386  1.8  christos       fprintf (stream, _("\
    387  1.8  christos       --prefix=PREFIX            Add PREFIX to absolute paths for -S\n"));
    388  1.1  christos       fprintf (stream, _("\
    389  1.1  christos       --prefix-strip=LEVEL       Strip initial directory names for -S\n"));
    390  1.8  christos       fprintf (stream, _("\
    391  1.8  christos       --dwarf-depth=N            Do not display DIEs at depth N or greater\n"));
    392  1.8  christos       fprintf (stream, _("\
    393  1.8  christos       --dwarf-start=N            Display DIEs starting at offset N\n"));
    394  1.8  christos       fprintf (stream, _("\
    395  1.8  christos       --dwarf-check              Make additional dwarf consistency checks.\n"));
    396  1.8  christos #ifdef ENABLE_LIBCTF
    397  1.8  christos       fprintf (stream, _("\
    398  1.8  christos       --ctf-parent=NAME          Use CTF archive member NAME as the CTF parent\n"));
    399  1.8  christos #endif
    400  1.8  christos       fprintf (stream, _("\
    401  1.8  christos       --visualize-jumps          Visualize jumps by drawing ASCII art lines\n"));
    402  1.8  christos       fprintf (stream, _("\
    403  1.8  christos       --visualize-jumps=color    Use colors in the ASCII art\n"));
    404  1.8  christos       fprintf (stream, _("\
    405  1.8  christos       --visualize-jumps=extended-color\n\
    406  1.8  christos                                  Use extended 8-bit color codes\n"));
    407  1.7  christos       fprintf (stream, _("\
    408  1.8  christos       --visualize-jumps=off      Disable jump visualization\n\n"));
    409  1.8  christos       fprintf (stream, _("\
    410  1.8  christos       --disassembler-color=off   Disable disassembler color output.\n\n"));
    411  1.8  christos       fprintf (stream, _("\
    412  1.7  christos       --disassembler-color=color Use basic colors in disassembler output.\n\n"));
    413  1.1  christos 
    414  1.1  christos       list_supported_targets (program_name, stream);
    415  1.1  christos       list_supported_architectures (program_name, stream);
    416  1.1  christos 
    417  1.1  christos       disassembler_usage (stream);
    418  1.1  christos 
    419  1.1  christos       if (objdump_private_vectors[0] != NULL)
    420  1.1  christos         {
    421  1.1  christos           fprintf (stream,
    422  1.1  christos                    _("\nOptions supported for -P/--private switch:\n"));
    423  1.1  christos           for (desc = objdump_private_vectors; *desc != NULL; desc++)
    424  1.1  christos             (*desc)->help (stream);
    425  1.1  christos         }
    426  1.1  christos     }
    427  1.1  christos   if (REPORT_BUGS_TO[0] && status == 0)
    428  1.1  christos     fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
    429  1.1  christos   exit (status);
    430  1.1  christos }
    431  1.1  christos 
    432  1.1  christos /* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
    433  1.1  christos enum option_values
    434  1.1  christos   {
    435  1.1  christos     OPTION_ENDIAN=150,
    436  1.1  christos     OPTION_START_ADDRESS,
    437  1.1  christos     OPTION_STOP_ADDRESS,
    438  1.1  christos     OPTION_DWARF,
    439  1.1  christos     OPTION_PREFIX,
    440  1.1  christos     OPTION_PREFIX_STRIP,
    441  1.1  christos     OPTION_INSN_WIDTH,
    442  1.1  christos     OPTION_ADJUST_VMA,
    443  1.1  christos     OPTION_DWARF_DEPTH,
    444  1.6  christos     OPTION_DWARF_CHECK,
    445  1.7  christos     OPTION_DWARF_START,
    446  1.7  christos     OPTION_RECURSE_LIMIT,
    447  1.7  christos     OPTION_NO_RECURSE_LIMIT,
    448  1.7  christos     OPTION_INLINES,
    449  1.8  christos     OPTION_SOURCE_COMMENT,
    450  1.7  christos #ifdef ENABLE_LIBCTF
    451  1.7  christos     OPTION_CTF,
    452  1.8  christos     OPTION_CTF_PARENT,
    453  1.8  christos #endif
    454  1.8  christos     OPTION_VISUALIZE_JUMPS,
    455  1.1  christos     OPTION_DISASSEMBLER_COLOR
    456  1.1  christos   };
    457  1.1  christos 
    458  1.1  christos static struct option long_options[]=
    459  1.1  christos {
    460  1.1  christos   {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
    461  1.1  christos   {"all-headers", no_argument, NULL, 'x'},
    462  1.1  christos   {"architecture", required_argument, NULL, 'm'},
    463  1.8  christos   {"archive-headers", no_argument, NULL, 'a'},
    464  1.8  christos #ifdef ENABLE_LIBCTF
    465  1.8  christos   {"ctf", optional_argument, NULL, OPTION_CTF},
    466  1.8  christos   {"ctf-parent", required_argument, NULL, OPTION_CTF_PARENT},
    467  1.1  christos #endif
    468  1.1  christos   {"debugging", no_argument, NULL, 'g'},
    469  1.1  christos   {"debugging-tags", no_argument, NULL, 'e'},
    470  1.7  christos   {"demangle", optional_argument, NULL, 'C'},
    471  1.1  christos   {"disassemble", optional_argument, NULL, 'd'},
    472  1.8  christos   {"disassemble-all", no_argument, NULL, 'D'},
    473  1.1  christos   {"disassemble-zeroes", no_argument, NULL, 'z'},
    474  1.8  christos   {"disassembler-options", required_argument, NULL, 'M'},
    475  1.8  christos   {"dwarf", optional_argument, NULL, OPTION_DWARF},
    476  1.8  christos   {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
    477  1.8  christos   {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
    478  1.1  christos   {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
    479  1.1  christos   {"dynamic-reloc", no_argument, NULL, 'R'},
    480  1.1  christos   {"dynamic-syms", no_argument, NULL, 'T'},
    481  1.1  christos   {"endian", required_argument, NULL, OPTION_ENDIAN},
    482  1.1  christos   {"file-headers", no_argument, NULL, 'f'},
    483  1.1  christos   {"file-offsets", no_argument, NULL, 'F'},
    484  1.1  christos   {"file-start-context", no_argument, &file_start_context, 1},
    485  1.1  christos   {"full-contents", no_argument, NULL, 's'},
    486  1.1  christos   {"headers", no_argument, NULL, 'h'},
    487  1.8  christos   {"help", no_argument, NULL, 'H'},
    488  1.1  christos   {"include", required_argument, NULL, 'I'},
    489  1.8  christos   {"info", no_argument, NULL, 'i'},
    490  1.8  christos   {"inlines", no_argument, 0, OPTION_INLINES},
    491  1.1  christos   {"insn-width", required_argument, NULL, OPTION_INSN_WIDTH},
    492  1.8  christos   {"line-numbers", no_argument, NULL, 'l'},
    493  1.8  christos   {"no-addresses", no_argument, &no_addresses, 1},
    494  1.8  christos   {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
    495  1.1  christos   {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
    496  1.8  christos   {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
    497  1.1  christos   {"prefix", required_argument, NULL, OPTION_PREFIX},
    498  1.8  christos   {"prefix-addresses", no_argument, &prefix_addresses, 1},
    499  1.8  christos   {"prefix-strip", required_argument, NULL, OPTION_PREFIX_STRIP},
    500  1.8  christos   {"private", required_argument, NULL, 'P'},
    501  1.8  christos   {"private-headers", no_argument, NULL, 'p'},
    502  1.7  christos   {"process-links", no_argument, &process_links, true},
    503  1.7  christos   {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
    504  1.1  christos   {"recursion-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
    505  1.1  christos   {"reloc", no_argument, NULL, 'r'},
    506  1.1  christos   {"section", required_argument, NULL, 'j'},
    507  1.1  christos   {"section-headers", no_argument, NULL, 'h'},
    508  1.1  christos   {"show-raw-insn", no_argument, &show_raw_insn, 1},
    509  1.7  christos   {"source", no_argument, NULL, 'S'},
    510  1.1  christos   {"source-comment", optional_argument, NULL, OPTION_SOURCE_COMMENT},
    511  1.1  christos   {"special-syms", no_argument, &dump_special_syms, 1},
    512  1.1  christos   {"stabs", no_argument, NULL, 'G'},
    513  1.1  christos   {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
    514  1.1  christos   {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
    515  1.1  christos   {"syms", no_argument, NULL, 't'},
    516  1.8  christos   {"target", required_argument, NULL, 'b'},
    517  1.1  christos   {"unicode", required_argument, NULL, 'U'},
    518  1.8  christos   {"version", no_argument, NULL, 'V'},
    519  1.1  christos   {"visualize-jumps", optional_argument, 0, OPTION_VISUALIZE_JUMPS},
    520  1.8  christos   {"wide", no_argument, NULL, 'w'},
    521  1.8  christos   {"disassembler-color", required_argument, NULL, OPTION_DISASSEMBLER_COLOR},
    522  1.1  christos   {NULL, no_argument, NULL, 0}
    523  1.1  christos };
    524  1.1  christos 
    525  1.1  christos static void
    527  1.1  christos nonfatal (const char *msg)
    528  1.1  christos {
    529  1.1  christos   bfd_nonfatal (msg);
    530  1.7  christos   exit_status = 1;
    531  1.8  christos }
    532  1.8  christos 
    533  1.8  christos /* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
    534  1.8  christos    The conversion format is controlled by the unicode_display variable.
    535  1.8  christos    Returns the number of characters added to OUT.
    536  1.8  christos    Returns the number of bytes consumed from IN in CONSUMED.
    537  1.8  christos    Always consumes at least one byte and displays at least one character.  */
    538  1.8  christos 
    539  1.8  christos static unsigned int
    540  1.8  christos display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
    541  1.8  christos {
    542  1.8  christos   char *        orig_out = out;
    543  1.8  christos   unsigned int  nchars = 0;
    544  1.8  christos   unsigned int j;
    545  1.8  christos 
    546  1.8  christos   if (unicode_display == unicode_default)
    547  1.8  christos     goto invalid;
    548  1.8  christos 
    549  1.8  christos   if (in[0] < 0xc0)
    550  1.8  christos     goto invalid;
    551  1.8  christos 
    552  1.8  christos   if ((in[1] & 0xc0) != 0x80)
    553  1.8  christos     goto invalid;
    554  1.8  christos 
    555  1.8  christos   if ((in[0] & 0x20) == 0)
    556  1.8  christos     {
    557  1.8  christos       nchars = 2;
    558  1.8  christos       goto valid;
    559  1.8  christos     }
    560  1.8  christos 
    561  1.8  christos   if ((in[2] & 0xc0) != 0x80)
    562  1.8  christos     goto invalid;
    563  1.8  christos 
    564  1.8  christos   if ((in[0] & 0x10) == 0)
    565  1.8  christos     {
    566  1.8  christos       nchars = 3;
    567  1.8  christos       goto valid;
    568  1.8  christos     }
    569  1.8  christos 
    570  1.8  christos   if ((in[3] & 0xc0) != 0x80)
    571  1.8  christos     goto invalid;
    572  1.8  christos 
    573  1.8  christos   nchars = 4;
    574  1.8  christos 
    575  1.8  christos  valid:
    576  1.8  christos   switch (unicode_display)
    577  1.8  christos     {
    578  1.8  christos     case unicode_locale:
    579  1.8  christos       /* Copy the bytes into the output buffer as is.  */
    580  1.8  christos       memcpy (out, in, nchars);
    581  1.8  christos       out += nchars;
    582  1.8  christos       break;
    583  1.8  christos 
    584  1.8  christos     case unicode_invalid:
    585  1.8  christos     case unicode_hex:
    586  1.8  christos       out += sprintf (out, "%c", unicode_display == unicode_hex ? '<' : '{');
    587  1.8  christos       out += sprintf (out, "0x");
    588  1.8  christos       for (j = 0; j < nchars; j++)
    589  1.8  christos 	out += sprintf (out, "%02x", in [j]);
    590  1.8  christos       out += sprintf (out, "%c", unicode_display == unicode_hex ? '>' : '}');
    591  1.8  christos       break;
    592  1.8  christos 
    593  1.8  christos     case unicode_highlight:
    594  1.8  christos       if (isatty (1))
    595  1.8  christos 	out += sprintf (out, "\x1B[31;47m"); /* Red.  */
    596  1.8  christos       /* Fall through.  */
    597  1.8  christos     case unicode_escape:
    598  1.8  christos       switch (nchars)
    599  1.8  christos 	{
    600  1.8  christos 	case 2:
    601  1.8  christos 	  out += sprintf (out, "\\u%02x%02x",
    602  1.8  christos 		  ((in[0] & 0x1c) >> 2),
    603  1.8  christos 		  ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
    604  1.8  christos 	  break;
    605  1.8  christos 
    606  1.8  christos 	case 3:
    607  1.8  christos 	  out += sprintf (out, "\\u%02x%02x",
    608  1.8  christos 		  ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
    609  1.8  christos 		  ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
    610  1.8  christos 	  break;
    611  1.8  christos 
    612  1.8  christos 	case 4:
    613  1.8  christos 	  out += sprintf (out, "\\u%02x%02x%02x",
    614  1.8  christos 		  ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
    615  1.8  christos 		  ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
    616  1.8  christos 		  ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
    617  1.8  christos 	  break;
    618  1.8  christos 	default:
    619  1.8  christos 	  /* URG.  */
    620  1.8  christos 	  break;
    621  1.8  christos 	}
    622  1.8  christos 
    623  1.8  christos       if (unicode_display == unicode_highlight && isatty (1))
    624  1.8  christos 	out += sprintf (out, "\033[0m"); /* Default colour.  */
    625  1.8  christos       break;
    626  1.8  christos 
    627  1.8  christos     default:
    628  1.8  christos       /* URG */
    629  1.8  christos       break;
    630  1.8  christos     }
    631  1.8  christos 
    632  1.8  christos   * consumed = nchars;
    633  1.8  christos   return out - orig_out;
    634  1.8  christos 
    635  1.8  christos  invalid:
    636  1.8  christos   /* Not a valid UTF-8 sequence.  */
    637  1.8  christos   *out = *in;
    638  1.8  christos   * consumed = 1;
    639  1.8  christos   return 1;
    640  1.7  christos }
    641  1.7  christos 
    642  1.8  christos /* Returns a version of IN with any control characters
    643  1.8  christos    replaced by escape sequences.  Uses a static buffer
    644  1.8  christos    if necessary.
    645  1.8  christos 
    646  1.7  christos    If unicode display is enabled, then also handles the
    647  1.7  christos    conversion of unicode characters.  */
    648  1.7  christos 
    649  1.7  christos static const char *
    650  1.7  christos sanitize_string (const char * in)
    651  1.7  christos {
    652  1.7  christos   static char *  buffer = NULL;
    653  1.7  christos   static size_t  buffer_len = 0;
    654  1.7  christos   const char *   original = in;
    655  1.7  christos   char *         out;
    656  1.7  christos 
    657  1.7  christos   /* Paranoia.  */
    658  1.7  christos   if (in == NULL)
    659  1.7  christos     return "";
    660  1.7  christos 
    661  1.7  christos   /* See if any conversion is necessary.  In the majority
    662  1.7  christos      of cases it will not be needed.  */
    663  1.8  christos   do
    664  1.7  christos     {
    665  1.7  christos       unsigned char c = *in++;
    666  1.7  christos 
    667  1.7  christos       if (c == 0)
    668  1.7  christos 	return original;
    669  1.7  christos 
    670  1.8  christos       if (ISCNTRL (c))
    671  1.8  christos 	break;
    672  1.8  christos 
    673  1.7  christos       if (unicode_display != unicode_default && c >= 0xc0)
    674  1.7  christos 	break;
    675  1.7  christos     }
    676  1.7  christos   while (1);
    677  1.7  christos 
    678  1.8  christos   /* Copy the input, translating as needed.  */
    679  1.7  christos   in = original;
    680  1.7  christos   if (buffer_len < (strlen (in) * 9))
    681  1.8  christos     {
    682  1.7  christos       free ((void *) buffer);
    683  1.7  christos       buffer_len = strlen (in) * 9;
    684  1.7  christos       buffer = xmalloc (buffer_len + 1);
    685  1.7  christos     }
    686  1.7  christos 
    687  1.7  christos   out = buffer;
    688  1.8  christos   do
    689  1.7  christos     {
    690  1.7  christos       unsigned char c = *in++;
    691  1.7  christos 
    692  1.7  christos       if (c == 0)
    693  1.8  christos 	break;
    694  1.7  christos 
    695  1.7  christos       if (ISCNTRL (c))
    696  1.7  christos 	{
    697  1.7  christos 	  *out++ = '^';
    698  1.8  christos 	  *out++ = c + 0x40;
    699  1.8  christos 	}
    700  1.8  christos       else if (unicode_display != unicode_default && c >= 0xc0)
    701  1.8  christos 	{
    702  1.8  christos 	  unsigned int num_consumed;
    703  1.8  christos 
    704  1.8  christos 	  out += display_utf8 ((const unsigned char *)(in - 1), out, & num_consumed);
    705  1.8  christos 	  in += num_consumed - 1;
    706  1.8  christos 	}
    707  1.7  christos       else
    708  1.7  christos 	*out++ = c;
    709  1.7  christos     }
    710  1.7  christos   while (1);
    711  1.7  christos 
    712  1.7  christos   *out = 0;
    713  1.7  christos   return buffer;
    714  1.1  christos }
    715  1.1  christos 
    716  1.1  christos 
    717  1.8  christos /* Returns TRUE if the specified section should be dumped.  */
    719  1.1  christos 
    720  1.1  christos static bool
    721  1.1  christos process_section_p (asection * section)
    722  1.1  christos {
    723  1.8  christos   struct only * only;
    724  1.1  christos 
    725  1.1  christos   if (only_list == NULL)
    726  1.1  christos     return true;
    727  1.1  christos 
    728  1.8  christos   for (only = only_list; only; only = only->next)
    729  1.8  christos     if (strcmp (only->name, section->name) == 0)
    730  1.1  christos       {
    731  1.1  christos 	only->seen = true;
    732  1.8  christos 	return true;
    733  1.1  christos       }
    734  1.1  christos 
    735  1.1  christos   return false;
    736  1.1  christos }
    737  1.1  christos 
    738  1.1  christos /* Add an entry to the 'only' list.  */
    739  1.1  christos 
    740  1.1  christos static void
    741  1.1  christos add_only (char * name)
    742  1.1  christos {
    743  1.1  christos   struct only * only;
    744  1.1  christos 
    745  1.1  christos   /* First check to make sure that we do not
    746  1.1  christos      already have an entry for this name.  */
    747  1.1  christos   for (only = only_list; only; only = only->next)
    748  1.1  christos     if (strcmp (only->name, name) == 0)
    749  1.1  christos       return;
    750  1.8  christos 
    751  1.1  christos   only = xmalloc (sizeof * only);
    752  1.1  christos   only->name = name;
    753  1.1  christos   only->seen = false;
    754  1.1  christos   only->next = only_list;
    755  1.1  christos   only_list = only;
    756  1.1  christos }
    757  1.1  christos 
    758  1.1  christos /* Release the memory used by the 'only' list.
    759  1.1  christos    PR 11225: Issue a warning message for unseen sections.
    760  1.1  christos    Only do this if none of the sections were seen.  This is mainly to support
    761  1.1  christos    tools like the GAS testsuite where an object file is dumped with a list of
    762  1.1  christos    generic section names known to be present in a range of different file
    763  1.1  christos    formats.  */
    764  1.1  christos 
    765  1.8  christos static void
    766  1.1  christos free_only_list (void)
    767  1.1  christos {
    768  1.1  christos   bool at_least_one_seen = false;
    769  1.1  christos   struct only * only;
    770  1.1  christos   struct only * next;
    771  1.1  christos 
    772  1.1  christos   if (only_list == NULL)
    773  1.1  christos     return;
    774  1.1  christos 
    775  1.8  christos   for (only = only_list; only; only = only->next)
    776  1.1  christos     if (only->seen)
    777  1.1  christos       {
    778  1.1  christos 	at_least_one_seen = true;
    779  1.1  christos 	break;
    780  1.1  christos       }
    781  1.1  christos 
    782  1.1  christos   for (only = only_list; only; only = next)
    783  1.1  christos     {
    784  1.1  christos       if (! at_least_one_seen)
    785  1.1  christos 	{
    786  1.1  christos 	  non_fatal (_("section '%s' mentioned in a -j option, "
    787  1.1  christos 		       "but not found in any input file"),
    788  1.1  christos 		     only->name);
    789  1.1  christos 	  exit_status = 1;
    790  1.1  christos 	}
    791  1.1  christos       next = only->next;
    792  1.1  christos       free (only);
    793  1.1  christos     }
    794  1.1  christos }
    795  1.6  christos 
    796  1.1  christos 
    797  1.1  christos static void
    799  1.6  christos dump_section_header (bfd *abfd, asection *section, void *data)
    800  1.1  christos {
    801  1.1  christos   char *comma = "";
    802  1.1  christos   unsigned int opb = bfd_octets_per_byte (abfd, section);
    803  1.1  christos   int longest_section_name = *((int *) data);
    804  1.1  christos 
    805  1.1  christos   /* Ignore linker created section.  See elfNN_ia64_object_p in
    806  1.1  christos      bfd/elfxx-ia64.c.  */
    807  1.1  christos   if (section->flags & SEC_LINKER_CREATED)
    808  1.1  christos     return;
    809  1.1  christos 
    810  1.6  christos   /* PR 10413: Skip sections that we are ignoring.  */
    811  1.7  christos   if (! process_section_p (section))
    812  1.7  christos     return;
    813  1.7  christos 
    814  1.1  christos   printf ("%3d %-*s %08lx  ", section->index, longest_section_name,
    815  1.1  christos 	  sanitize_string (bfd_section_name (section)),
    816  1.1  christos 	  (unsigned long) bfd_section_size (section) / opb);
    817  1.7  christos   bfd_printf_vma (abfd, bfd_section_vma (section));
    818  1.1  christos   printf ("  ");
    819  1.1  christos   bfd_printf_vma (abfd, section->lma);
    820  1.1  christos   printf ("  %08lx  2**%u", (unsigned long) section->filepos,
    821  1.1  christos 	  bfd_section_alignment (section));
    822  1.1  christos   if (! wide_output)
    823  1.1  christos     printf ("\n                ");
    824  1.1  christos   printf ("  ");
    825  1.1  christos 
    826  1.1  christos #define PF(x, y) \
    827  1.1  christos   if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
    828  1.1  christos 
    829  1.1  christos   PF (SEC_HAS_CONTENTS, "CONTENTS");
    830  1.1  christos   PF (SEC_ALLOC, "ALLOC");
    831  1.1  christos   PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
    832  1.1  christos   PF (SEC_LOAD, "LOAD");
    833  1.1  christos   PF (SEC_RELOC, "RELOC");
    834  1.1  christos   PF (SEC_READONLY, "READONLY");
    835  1.1  christos   PF (SEC_CODE, "CODE");
    836  1.1  christos   PF (SEC_DATA, "DATA");
    837  1.1  christos   PF (SEC_ROM, "ROM");
    838  1.1  christos   PF (SEC_DEBUGGING, "DEBUGGING");
    839  1.1  christos   PF (SEC_NEVER_LOAD, "NEVER_LOAD");
    840  1.1  christos   PF (SEC_EXCLUDE, "EXCLUDE");
    841  1.1  christos   PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
    842  1.1  christos   if (bfd_get_arch (abfd) == bfd_arch_tic54x)
    843  1.1  christos     {
    844  1.1  christos       PF (SEC_TIC54X_BLOCK, "BLOCK");
    845  1.5  christos       PF (SEC_TIC54X_CLINK, "CLINK");
    846  1.5  christos     }
    847  1.5  christos   PF (SEC_SMALL_DATA, "SMALL_DATA");
    848  1.5  christos   if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
    849  1.5  christos     {
    850  1.7  christos       PF (SEC_COFF_SHARED, "SHARED");
    851  1.7  christos       PF (SEC_COFF_NOREAD, "NOREAD");
    852  1.7  christos     }
    853  1.7  christos   else if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
    854  1.1  christos     {
    855  1.1  christos       PF (SEC_ELF_OCTETS, "OCTETS");
    856  1.5  christos       PF (SEC_ELF_PURECODE, "PURECODE");
    857  1.5  christos     }
    858  1.5  christos   PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
    859  1.5  christos   PF (SEC_GROUP, "GROUP");
    860  1.1  christos   if (bfd_get_arch (abfd) == bfd_arch_mep)
    861  1.1  christos     {
    862  1.1  christos       PF (SEC_MEP_VLIW, "VLIW");
    863  1.1  christos     }
    864  1.1  christos 
    865  1.1  christos   if ((section->flags & SEC_LINK_ONCE) != 0)
    866  1.1  christos     {
    867  1.1  christos       const char *ls;
    868  1.1  christos       struct coff_comdat_info *comdat;
    869  1.1  christos 
    870  1.1  christos       switch (section->flags & SEC_LINK_DUPLICATES)
    871  1.1  christos 	{
    872  1.1  christos 	default:
    873  1.1  christos 	  abort ();
    874  1.1  christos 	case SEC_LINK_DUPLICATES_DISCARD:
    875  1.1  christos 	  ls = "LINK_ONCE_DISCARD";
    876  1.1  christos 	  break;
    877  1.1  christos 	case SEC_LINK_DUPLICATES_ONE_ONLY:
    878  1.1  christos 	  ls = "LINK_ONCE_ONE_ONLY";
    879  1.1  christos 	  break;
    880  1.1  christos 	case SEC_LINK_DUPLICATES_SAME_SIZE:
    881  1.1  christos 	  ls = "LINK_ONCE_SAME_SIZE";
    882  1.1  christos 	  break;
    883  1.1  christos 	case SEC_LINK_DUPLICATES_SAME_CONTENTS:
    884  1.1  christos 	  ls = "LINK_ONCE_SAME_CONTENTS";
    885  1.1  christos 	  break;
    886  1.1  christos 	}
    887  1.1  christos       printf ("%s%s", comma, ls);
    888  1.1  christos 
    889  1.1  christos       comdat = bfd_coff_get_comdat_section (abfd, section);
    890  1.1  christos       if (comdat != NULL)
    891  1.1  christos 	printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
    892  1.1  christos 
    893  1.1  christos       comma = ", ";
    894  1.1  christos     }
    895  1.1  christos 
    896  1.6  christos   printf ("\n");
    897  1.6  christos #undef PF
    898  1.6  christos }
    899  1.6  christos 
    900  1.7  christos /* Called on each SECTION in ABFD, update the int variable pointed to by
    901  1.7  christos    DATA which contains the string length of the longest section name.  */
    902  1.6  christos 
    903  1.6  christos static void
    904  1.6  christos find_longest_section_name (bfd *abfd ATTRIBUTE_UNUSED,
    905  1.6  christos 			   asection *section, void *data)
    906  1.6  christos {
    907  1.6  christos   int *longest_so_far = (int *) data;
    908  1.6  christos   const char *name;
    909  1.6  christos   int len;
    910  1.6  christos 
    911  1.6  christos   /* Ignore linker created section.  */
    912  1.6  christos   if (section->flags & SEC_LINKER_CREATED)
    913  1.6  christos     return;
    914  1.6  christos 
    915  1.7  christos   /* Skip sections that we are ignoring.  */
    916  1.6  christos   if (! process_section_p (section))
    917  1.6  christos     return;
    918  1.6  christos 
    919  1.6  christos   name = bfd_section_name (section);
    920  1.6  christos   len = (int) strlen (name);
    921  1.1  christos   if (len > *longest_so_far)
    922  1.1  christos     *longest_so_far = len;
    923  1.1  christos }
    924  1.6  christos 
    925  1.6  christos static void
    926  1.6  christos dump_headers (bfd *abfd)
    927  1.1  christos {
    928  1.1  christos   /* The default width of 13 is just an arbitrary choice.  */
    929  1.6  christos   int max_section_name_length = 13;
    930  1.1  christos   int bfd_vma_width;
    931  1.1  christos 
    932  1.1  christos #ifndef BFD64
    933  1.6  christos   bfd_vma_width = 10;
    934  1.1  christos #else
    935  1.6  christos   /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses.  */
    936  1.1  christos   if (bfd_get_arch_size (abfd) == 32)
    937  1.1  christos     bfd_vma_width = 10;
    938  1.6  christos   else
    939  1.6  christos     bfd_vma_width = 18;
    940  1.6  christos #endif
    941  1.6  christos 
    942  1.8  christos   printf (_("Sections:\n"));
    943  1.6  christos 
    944  1.6  christos   if (wide_output)
    945  1.6  christos     bfd_map_over_sections (abfd, find_longest_section_name,
    946  1.6  christos 			   &max_section_name_length);
    947  1.6  christos 
    948  1.6  christos   printf (_("Idx %-*s Size      %-*s%-*sFile off  Algn"),
    949  1.1  christos 	  max_section_name_length, "Name",
    950  1.1  christos 	  bfd_vma_width, "VMA",
    951  1.1  christos 	  bfd_vma_width, "LMA");
    952  1.1  christos 
    953  1.6  christos   if (wide_output)
    954  1.8  christos     printf (_("  Flags"));
    955  1.1  christos   printf ("\n");
    956  1.1  christos 
    957  1.1  christos   bfd_map_over_sections (abfd, dump_section_header,
    958  1.1  christos 			 &max_section_name_length);
    959  1.1  christos }
    960  1.1  christos 
    961  1.1  christos static asymbol **
    963  1.1  christos slurp_symtab (bfd *abfd)
    964  1.1  christos {
    965  1.1  christos   asymbol **sy = NULL;
    966  1.1  christos   long storage;
    967  1.1  christos 
    968  1.1  christos   if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
    969  1.1  christos     {
    970  1.1  christos       symcount = 0;
    971  1.3  christos       return NULL;
    972  1.3  christos     }
    973  1.3  christos 
    974  1.3  christos   storage = bfd_get_symtab_upper_bound (abfd);
    975  1.8  christos   if (storage < 0)
    976  1.1  christos     {
    977  1.7  christos       non_fatal (_("failed to read symbol table from: %s"), bfd_get_filename (abfd));
    978  1.7  christos       bfd_fatal (_("error message was"));
    979  1.7  christos     }
    980  1.7  christos 
    981  1.7  christos   if (storage)
    982  1.7  christos     {
    983  1.7  christos       off_t filesize = bfd_get_file_size (abfd);
    984  1.7  christos 
    985  1.8  christos       /* qv PR 24707.  */
    986  1.7  christos       if (filesize > 0
    987  1.7  christos 	  && filesize < storage
    988  1.8  christos 	  /* The MMO file format supports its own special compression
    989  1.8  christos 	     technique, so its sections can be larger than the file size.  */
    990  1.8  christos 	  && bfd_get_flavour (abfd) != bfd_target_mmo_flavour)
    991  1.7  christos 	{
    992  1.7  christos 	  bfd_nonfatal_message (bfd_get_filename (abfd), abfd, NULL,
    993  1.7  christos 				_("error: symbol table size (%#lx) "
    994  1.7  christos 				  "is larger than filesize (%#lx)"),
    995  1.7  christos 				storage, (long) filesize);
    996  1.7  christos 	  exit_status = 1;
    997  1.7  christos 	  symcount = 0;
    998  1.1  christos 	  return NULL;
    999  1.1  christos 	}
   1000  1.1  christos 
   1001  1.1  christos       sy = (asymbol **) xmalloc (storage);
   1002  1.1  christos     }
   1003  1.1  christos 
   1004  1.1  christos   symcount = bfd_canonicalize_symtab (abfd, sy);
   1005  1.1  christos   if (symcount < 0)
   1006  1.1  christos     bfd_fatal (bfd_get_filename (abfd));
   1007  1.1  christos   return sy;
   1008  1.1  christos }
   1009  1.1  christos 
   1010  1.1  christos /* Read in the dynamic symbols.  */
   1011  1.1  christos 
   1012  1.1  christos static asymbol **
   1013  1.1  christos slurp_dynamic_symtab (bfd *abfd)
   1014  1.1  christos {
   1015  1.1  christos   asymbol **sy = NULL;
   1016  1.1  christos   long storage;
   1017  1.1  christos 
   1018  1.1  christos   storage = bfd_get_dynamic_symtab_upper_bound (abfd);
   1019  1.1  christos   if (storage < 0)
   1020  1.1  christos     {
   1021  1.1  christos       if (!(bfd_get_file_flags (abfd) & DYNAMIC))
   1022  1.1  christos 	{
   1023  1.1  christos 	  non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
   1024  1.1  christos 	  exit_status = 1;
   1025  1.1  christos 	  dynsymcount = 0;
   1026  1.8  christos 	  return NULL;
   1027  1.1  christos 	}
   1028  1.1  christos 
   1029  1.1  christos       bfd_fatal (bfd_get_filename (abfd));
   1030  1.1  christos     }
   1031  1.1  christos 
   1032  1.1  christos   if (storage)
   1033  1.1  christos     sy = (asymbol **) xmalloc (storage);
   1034  1.1  christos 
   1035  1.1  christos   dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
   1036  1.6  christos   if (dynsymcount < 0)
   1037  1.6  christos     bfd_fatal (bfd_get_filename (abfd));
   1038  1.6  christos   return sy;
   1039  1.6  christos }
   1040  1.8  christos 
   1041  1.6  christos /* Some symbol names are significant and should be kept in the
   1042  1.6  christos    table of sorted symbol names, even if they are marked as
   1043  1.8  christos    debugging/section symbols.  */
   1044  1.6  christos 
   1045  1.6  christos static bool
   1046  1.1  christos is_significant_symbol_name (const char * name)
   1047  1.1  christos {
   1048  1.1  christos   return startswith (name, ".plt") || startswith (name, ".got");
   1049  1.1  christos }
   1050  1.1  christos 
   1051  1.1  christos /* Filter out (in place) symbols that are useless for disassembly.
   1052  1.1  christos    COUNT is the number of elements in SYMBOLS.
   1053  1.1  christos    Return the number of useful symbols.  */
   1054  1.1  christos 
   1055  1.1  christos static long
   1056  1.1  christos remove_useless_symbols (asymbol **symbols, long count)
   1057  1.1  christos {
   1058  1.1  christos   asymbol **in_ptr = symbols, **out_ptr = symbols;
   1059  1.1  christos 
   1060  1.1  christos   while (--count >= 0)
   1061  1.6  christos     {
   1062  1.6  christos       asymbol *sym = *in_ptr++;
   1063  1.1  christos 
   1064  1.1  christos       if (sym->name == NULL || sym->name[0] == '\0')
   1065  1.1  christos 	continue;
   1066  1.1  christos       if ((sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
   1067  1.1  christos 	  && ! is_significant_symbol_name (sym->name))
   1068  1.1  christos 	continue;
   1069  1.1  christos       if (bfd_is_und_section (sym->section)
   1070  1.1  christos 	  || bfd_is_com_section (sym->section))
   1071  1.1  christos 	continue;
   1072  1.1  christos 
   1073  1.7  christos       *out_ptr++ = sym;
   1074  1.7  christos     }
   1075  1.1  christos   return out_ptr - symbols;
   1076  1.1  christos }
   1077  1.1  christos 
   1078  1.1  christos static const asection *compare_section;
   1079  1.1  christos 
   1080  1.1  christos /* Sort symbols into value order.  */
   1081  1.1  christos 
   1082  1.1  christos static int
   1083  1.1  christos compare_symbols (const void *ap, const void *bp)
   1084  1.1  christos {
   1085  1.1  christos   const asymbol *a = * (const asymbol **) ap;
   1086  1.8  christos   const asymbol *b = * (const asymbol **) bp;
   1087  1.1  christos   const char *an;
   1088  1.1  christos   const char *bn;
   1089  1.1  christos   size_t anl;
   1090  1.1  christos   size_t bnl;
   1091  1.1  christos   bool as, af, bs, bf;
   1092  1.1  christos   flagword aflags;
   1093  1.1  christos   flagword bflags;
   1094  1.1  christos 
   1095  1.7  christos   if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
   1096  1.7  christos     return 1;
   1097  1.7  christos   else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
   1098  1.7  christos     return -1;
   1099  1.7  christos 
   1100  1.7  christos   /* Prefer symbols from the section currently being disassembled.
   1101  1.7  christos      Don't sort symbols from other sections by section, since there
   1102  1.7  christos      isn't much reason to prefer one section over another otherwise.
   1103  1.7  christos      See sym_ok comment for why we compare by section name.  */
   1104  1.1  christos   as = strcmp (compare_section->name, a->section->name) == 0;
   1105  1.1  christos   bs = strcmp (compare_section->name, b->section->name) == 0;
   1106  1.1  christos   if (as && !bs)
   1107  1.1  christos     return -1;
   1108  1.1  christos   if (!as && bs)
   1109  1.1  christos     return 1;
   1110  1.1  christos 
   1111  1.1  christos   an = bfd_asymbol_name (a);
   1112  1.1  christos   bn = bfd_asymbol_name (b);
   1113  1.1  christos   anl = strlen (an);
   1114  1.1  christos   bnl = strlen (bn);
   1115  1.1  christos 
   1116  1.1  christos   /* The symbols gnu_compiled and gcc2_compiled convey no real
   1117  1.1  christos      information, so put them after other symbols with the same value.  */
   1118  1.1  christos   af = (strstr (an, "gnu_compiled") != NULL
   1119  1.1  christos 	|| strstr (an, "gcc2_compiled") != NULL);
   1120  1.1  christos   bf = (strstr (bn, "gnu_compiled") != NULL
   1121  1.1  christos 	|| strstr (bn, "gcc2_compiled") != NULL);
   1122  1.1  christos 
   1123  1.1  christos   if (af && ! bf)
   1124  1.1  christos     return 1;
   1125  1.1  christos   if (! af && bf)
   1126  1.1  christos     return -1;
   1127  1.1  christos 
   1128  1.1  christos   /* We use a heuristic for the file name, to try to sort it after
   1129  1.1  christos      more useful symbols.  It may not work on non Unix systems, but it
   1130  1.7  christos      doesn't really matter; the only difference is precisely which
   1131  1.7  christos      symbol names get printed.  */
   1132  1.1  christos 
   1133  1.1  christos #define file_symbol(s, sn, snl)			\
   1134  1.1  christos   (((s)->flags & BSF_FILE) != 0			\
   1135  1.1  christos    || ((snl) > 2				\
   1136  1.1  christos        && (sn)[(snl) - 2] == '.'		\
   1137  1.1  christos        && ((sn)[(snl) - 1] == 'o'		\
   1138  1.1  christos 	   || (sn)[(snl) - 1] == 'a')))
   1139  1.1  christos 
   1140  1.1  christos   af = file_symbol (a, an, anl);
   1141  1.1  christos   bf = file_symbol (b, bn, bnl);
   1142  1.1  christos 
   1143  1.7  christos   if (af && ! bf)
   1144  1.7  christos     return 1;
   1145  1.1  christos   if (! af && bf)
   1146  1.1  christos     return -1;
   1147  1.1  christos 
   1148  1.1  christos   /* Sort function and object symbols before global symbols before
   1149  1.1  christos      local symbols before section symbols before debugging symbols.  */
   1150  1.1  christos 
   1151  1.1  christos   aflags = a->flags;
   1152  1.1  christos   bflags = b->flags;
   1153  1.1  christos 
   1154  1.1  christos   if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
   1155  1.1  christos     {
   1156  1.7  christos       if ((aflags & BSF_DEBUGGING) != 0)
   1157  1.7  christos 	return 1;
   1158  1.7  christos       else
   1159  1.7  christos 	return -1;
   1160  1.7  christos     }
   1161  1.7  christos   if ((aflags & BSF_SECTION_SYM) != (bflags & BSF_SECTION_SYM))
   1162  1.7  christos     {
   1163  1.1  christos       if ((aflags & BSF_SECTION_SYM) != 0)
   1164  1.1  christos 	return 1;
   1165  1.1  christos       else
   1166  1.1  christos 	return -1;
   1167  1.1  christos     }
   1168  1.1  christos   if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
   1169  1.1  christos     {
   1170  1.7  christos       if ((aflags & BSF_FUNCTION) != 0)
   1171  1.7  christos 	return -1;
   1172  1.7  christos       else
   1173  1.7  christos 	return 1;
   1174  1.7  christos     }
   1175  1.7  christos   if ((aflags & BSF_OBJECT) != (bflags & BSF_OBJECT))
   1176  1.7  christos     {
   1177  1.1  christos       if ((aflags & BSF_OBJECT) != 0)
   1178  1.1  christos 	return -1;
   1179  1.1  christos       else
   1180  1.1  christos 	return 1;
   1181  1.1  christos     }
   1182  1.1  christos   if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
   1183  1.1  christos     {
   1184  1.1  christos       if ((aflags & BSF_LOCAL) != 0)
   1185  1.1  christos 	return 1;
   1186  1.1  christos       else
   1187  1.1  christos 	return -1;
   1188  1.1  christos     }
   1189  1.1  christos   if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
   1190  1.1  christos     {
   1191  1.1  christos       if ((aflags & BSF_GLOBAL) != 0)
   1192  1.6  christos 	return -1;
   1193  1.6  christos       else
   1194  1.6  christos 	return 1;
   1195  1.6  christos     }
   1196  1.6  christos 
   1197  1.6  christos   if (bfd_get_flavour (bfd_asymbol_bfd (a)) == bfd_target_elf_flavour
   1198  1.6  christos       && bfd_get_flavour (bfd_asymbol_bfd (b)) == bfd_target_elf_flavour)
   1199  1.6  christos     {
   1200  1.6  christos       bfd_vma asz, bsz;
   1201  1.6  christos 
   1202  1.6  christos       asz = 0;
   1203  1.6  christos       if ((a->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
   1204  1.6  christos 	asz = ((elf_symbol_type *) a)->internal_elf_sym.st_size;
   1205  1.6  christos       bsz = 0;
   1206  1.6  christos       if ((b->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
   1207  1.1  christos 	bsz = ((elf_symbol_type *) b)->internal_elf_sym.st_size;
   1208  1.1  christos       if (asz != bsz)
   1209  1.1  christos 	return asz > bsz ? -1 : 1;
   1210  1.1  christos     }
   1211  1.1  christos 
   1212  1.1  christos   /* Symbols that start with '.' might be section names, so sort them
   1213  1.1  christos      after symbols that don't start with '.'.  */
   1214  1.1  christos   if (an[0] == '.' && bn[0] != '.')
   1215  1.1  christos     return 1;
   1216  1.1  christos   if (an[0] != '.' && bn[0] == '.')
   1217  1.1  christos     return -1;
   1218  1.1  christos 
   1219  1.1  christos   /* Finally, if we can't distinguish them in any other way, try to
   1220  1.1  christos      get consistent results by sorting the symbols by name.  */
   1221  1.1  christos   return strcmp (an, bn);
   1222  1.1  christos }
   1223  1.1  christos 
   1224  1.1  christos /* Sort relocs into address order.  */
   1225  1.1  christos 
   1226  1.1  christos static int
   1227  1.1  christos compare_relocs (const void *ap, const void *bp)
   1228  1.1  christos {
   1229  1.1  christos   const arelent *a = * (const arelent **) ap;
   1230  1.1  christos   const arelent *b = * (const arelent **) bp;
   1231  1.1  christos 
   1232  1.1  christos   if (a->address > b->address)
   1233  1.1  christos     return 1;
   1234  1.1  christos   else if (a->address < b->address)
   1235  1.1  christos     return -1;
   1236  1.1  christos 
   1237  1.1  christos   /* So that associated relocations tied to the same address show up
   1238  1.1  christos      in the correct order, we don't do any further sorting.  */
   1239  1.1  christos   if (a > b)
   1240  1.1  christos     return 1;
   1241  1.1  christos   else if (a < b)
   1242  1.1  christos     return -1;
   1243  1.1  christos   else
   1244  1.1  christos     return 0;
   1245  1.1  christos }
   1246  1.1  christos 
   1247  1.8  christos /* Print an address (VMA) to the output stream in INFO.
   1248  1.1  christos    If SKIP_ZEROES is TRUE, omit leading zeroes.  */
   1249  1.1  christos 
   1250  1.1  christos static void
   1251  1.1  christos objdump_print_value (bfd_vma vma, struct disassemble_info *inf,
   1252  1.1  christos 		     bool skip_zeroes)
   1253  1.1  christos {
   1254  1.1  christos   char buf[30];
   1255  1.1  christos   char *p;
   1256  1.1  christos   struct objdump_disasm_info *aux;
   1257  1.1  christos 
   1258  1.1  christos   aux = (struct objdump_disasm_info *) inf->application_data;
   1259  1.1  christos   bfd_sprintf_vma (aux->abfd, buf, vma);
   1260  1.1  christos   if (! skip_zeroes)
   1261  1.1  christos     p = buf;
   1262  1.1  christos   else
   1263  1.1  christos     {
   1264  1.8  christos       for (p = buf; *p == '0'; ++p)
   1265  1.1  christos 	;
   1266  1.1  christos       if (*p == '\0')
   1267  1.1  christos 	--p;
   1268  1.1  christos     }
   1269  1.1  christos   (*inf->fprintf_styled_func) (inf->stream, dis_style_address, "%s", p);
   1270  1.1  christos }
   1271  1.1  christos 
   1272  1.1  christos /* Print the name of a symbol.  */
   1273  1.1  christos 
   1274  1.3  christos static void
   1275  1.8  christos objdump_print_symname (bfd *abfd, struct disassemble_info *inf,
   1276  1.1  christos 		       asymbol *sym)
   1277  1.1  christos {
   1278  1.1  christos   char *alloc;
   1279  1.1  christos   const char *name, *version_string = NULL;
   1280  1.1  christos   bool hidden = false;
   1281  1.1  christos 
   1282  1.7  christos   alloc = NULL;
   1283  1.1  christos   name = bfd_asymbol_name (sym);
   1284  1.1  christos   if (do_demangle && name[0] != '\0')
   1285  1.1  christos     {
   1286  1.1  christos       /* Demangle the name.  */
   1287  1.6  christos       alloc = bfd_demangle (abfd, name, demangle_flags);
   1288  1.8  christos       if (alloc != NULL)
   1289  1.8  christos 	name = alloc;
   1290  1.3  christos     }
   1291  1.7  christos 
   1292  1.8  christos   if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
   1293  1.3  christos     version_string = bfd_get_symbol_version_string (abfd, sym, true,
   1294  1.7  christos 						    &hidden);
   1295  1.7  christos 
   1296  1.1  christos   if (bfd_is_und_section (bfd_asymbol_section (sym)))
   1297  1.3  christos     hidden = true;
   1298  1.8  christos 
   1299  1.3  christos   name = sanitize_string (name);
   1300  1.8  christos 
   1301  1.8  christos   if (inf != NULL)
   1302  1.8  christos     {
   1303  1.3  christos       (*inf->fprintf_styled_func) (inf->stream, dis_style_symbol, "%s", name);
   1304  1.1  christos       if (version_string && *version_string != '\0')
   1305  1.3  christos 	(*inf->fprintf_styled_func) (inf->stream, dis_style_symbol,
   1306  1.3  christos 				     hidden ? "@%s" : "@@%s",
   1307  1.3  christos 				     version_string);
   1308  1.3  christos     }
   1309  1.3  christos   else
   1310  1.1  christos     {
   1311  1.1  christos       printf ("%s", name);
   1312  1.1  christos       if (version_string && *version_string != '\0')
   1313  1.1  christos 	printf (hidden ? "@%s" : "@@%s", version_string);
   1314  1.1  christos     }
   1315  1.8  christos 
   1316  1.8  christos   if (alloc != NULL)
   1317  1.8  christos     free (alloc);
   1318  1.8  christos }
   1319  1.8  christos 
   1320  1.8  christos static inline bool
   1321  1.7  christos sym_ok (bool want_section,
   1322  1.7  christos 	bfd *abfd ATTRIBUTE_UNUSED,
   1323  1.7  christos 	long place,
   1324  1.8  christos 	asection *sec,
   1325  1.8  christos 	struct disassemble_info *inf)
   1326  1.8  christos {
   1327  1.8  christos   if (want_section)
   1328  1.8  christos     {
   1329  1.8  christos       /* NB: An object file can have different sections with the same
   1330  1.8  christos 	 section name.  Compare compare section pointers if they have
   1331  1.7  christos 	 the same owner.  */
   1332  1.7  christos       if (sorted_syms[place]->section->owner == sec->owner
   1333  1.7  christos 	  && sorted_syms[place]->section != sec)
   1334  1.7  christos 	return false;
   1335  1.7  christos 
   1336  1.7  christos       /* Note - we cannot just compare section pointers because they could
   1337  1.7  christos 	 be different, but the same...  Ie the symbol that we are trying to
   1338  1.7  christos 	 find could have come from a separate debug info file.  Under such
   1339  1.7  christos 	 circumstances the symbol will be associated with a section in the
   1340  1.8  christos 	 debug info file, whilst the section we want is in a normal file.
   1341  1.7  christos 	 So the section pointers will be different, but the section names
   1342  1.7  christos 	 will be the same.  */
   1343  1.7  christos       if (strcmp (bfd_section_name (sorted_syms[place]->section),
   1344  1.7  christos 		  bfd_section_name (sec)) != 0)
   1345  1.7  christos 	return false;
   1346  1.1  christos     }
   1347  1.1  christos 
   1348  1.1  christos   return inf->symbol_is_valid (sorted_syms[place], inf);
   1349  1.1  christos }
   1350  1.1  christos 
   1351  1.1  christos /* Locate a symbol given a bfd and a section (from INFO->application_data),
   1352  1.1  christos    and a VMA.  If INFO->application_data->require_sec is TRUE, then always
   1353  1.1  christos    require the symbol to be in the section.  Returns NULL if there is no
   1354  1.1  christos    suitable symbol.  If PLACE is not NULL, then *PLACE is set to the index
   1355  1.1  christos    of the symbol in sorted_syms.  */
   1356  1.1  christos 
   1357  1.1  christos static asymbol *
   1358  1.1  christos find_symbol_for_address (bfd_vma vma,
   1359  1.1  christos 			 struct disassemble_info *inf,
   1360  1.1  christos 			 long *place)
   1361  1.1  christos {
   1362  1.1  christos   /* @@ Would it speed things up to cache the last two symbols returned,
   1363  1.1  christos      and maybe their address ranges?  For many processors, only one memory
   1364  1.1  christos      operand can be present at a time, so the 2-entry cache wouldn't be
   1365  1.1  christos      constantly churned by code doing heavy memory accesses.  */
   1366  1.1  christos 
   1367  1.1  christos   /* Indices in `sorted_syms'.  */
   1368  1.1  christos   long min = 0;
   1369  1.1  christos   long max_count = sorted_symcount;
   1370  1.8  christos   long thisplace;
   1371  1.6  christos   struct objdump_disasm_info *aux;
   1372  1.1  christos   bfd *abfd;
   1373  1.1  christos   asection *sec;
   1374  1.1  christos   unsigned int opb;
   1375  1.1  christos   bool want_section;
   1376  1.1  christos   long rel_count;
   1377  1.1  christos 
   1378  1.7  christos   if (sorted_symcount < 1)
   1379  1.1  christos     return NULL;
   1380  1.1  christos 
   1381  1.1  christos   aux = (struct objdump_disasm_info *) inf->application_data;
   1382  1.1  christos   abfd = aux->abfd;
   1383  1.1  christos   sec = inf->section;
   1384  1.1  christos   opb = inf->octets_per_byte;
   1385  1.1  christos 
   1386  1.1  christos   /* Perform a binary search looking for the closest symbol to the
   1387  1.1  christos      required value.  We are searching the range (min, max_count].  */
   1388  1.1  christos   while (min + 1 < max_count)
   1389  1.1  christos     {
   1390  1.1  christos       asymbol *sym;
   1391  1.1  christos 
   1392  1.1  christos       thisplace = (max_count + min) / 2;
   1393  1.1  christos       sym = sorted_syms[thisplace];
   1394  1.1  christos 
   1395  1.1  christos       if (bfd_asymbol_value (sym) > vma)
   1396  1.1  christos 	max_count = thisplace;
   1397  1.1  christos       else if (bfd_asymbol_value (sym) < vma)
   1398  1.1  christos 	min = thisplace;
   1399  1.1  christos       else
   1400  1.1  christos 	{
   1401  1.1  christos 	  min = thisplace;
   1402  1.1  christos 	  break;
   1403  1.7  christos 	}
   1404  1.1  christos     }
   1405  1.1  christos 
   1406  1.1  christos   /* The symbol we want is now in min, the low end of the range we
   1407  1.7  christos      were searching.  If there are several symbols with the same
   1408  1.1  christos      value, we want the first one.  */
   1409  1.1  christos   thisplace = min;
   1410  1.1  christos   while (thisplace > 0
   1411  1.1  christos 	 && (bfd_asymbol_value (sorted_syms[thisplace])
   1412  1.1  christos 	     == bfd_asymbol_value (sorted_syms[thisplace - 1])))
   1413  1.1  christos     --thisplace;
   1414  1.1  christos 
   1415  1.1  christos   /* Prefer a symbol in the current section if we have multple symbols
   1416  1.1  christos      with the same value, as can occur with overlays or zero size
   1417  1.1  christos      sections.  */
   1418  1.8  christos   min = thisplace;
   1419  1.1  christos   while (min < max_count
   1420  1.1  christos 	 && (bfd_asymbol_value (sorted_syms[min])
   1421  1.1  christos 	     == bfd_asymbol_value (sorted_syms[thisplace])))
   1422  1.1  christos     {
   1423  1.1  christos       if (sym_ok (true, abfd, min, sec, inf))
   1424  1.1  christos 	{
   1425  1.1  christos 	  thisplace = min;
   1426  1.1  christos 
   1427  1.1  christos 	  if (place != NULL)
   1428  1.1  christos 	    *place = thisplace;
   1429  1.1  christos 
   1430  1.1  christos 	  return sorted_syms[thisplace];
   1431  1.1  christos 	}
   1432  1.1  christos       ++min;
   1433  1.1  christos     }
   1434  1.1  christos 
   1435  1.1  christos   /* If the file is relocatable, and the symbol could be from this
   1436  1.1  christos      section, prefer a symbol from this section over symbols from
   1437  1.1  christos      others, even if the other symbol's value might be closer.
   1438  1.3  christos 
   1439  1.1  christos      Note that this may be wrong for some symbol references if the
   1440  1.1  christos      sections have overlapping memory ranges, but in that case there's
   1441  1.1  christos      no way to tell what's desired without looking at the relocation
   1442  1.7  christos      table.
   1443  1.7  christos 
   1444  1.7  christos      Also give the target a chance to reject symbols.  */
   1445  1.8  christos   want_section = (aux->require_sec
   1446  1.7  christos 		  || ((abfd->flags & HAS_RELOC) != 0
   1447  1.1  christos 		      && vma >= bfd_section_vma (sec)
   1448  1.1  christos 		      && vma < (bfd_section_vma (sec)
   1449  1.1  christos 				+ bfd_section_size (sec) / opb)));
   1450  1.1  christos 
   1451  1.1  christos   if (! sym_ok (want_section, abfd, thisplace, sec, inf))
   1452  1.1  christos     {
   1453  1.7  christos       long i;
   1454  1.1  christos       long newplace = sorted_symcount;
   1455  1.1  christos 
   1456  1.1  christos       for (i = min - 1; i >= 0; i--)
   1457  1.1  christos 	{
   1458  1.1  christos 	  if (sym_ok (want_section, abfd, i, sec, inf))
   1459  1.1  christos 	    {
   1460  1.1  christos 	      if (newplace == sorted_symcount)
   1461  1.1  christos 		newplace = i;
   1462  1.1  christos 
   1463  1.1  christos 	      if (bfd_asymbol_value (sorted_syms[i])
   1464  1.1  christos 		  != bfd_asymbol_value (sorted_syms[newplace]))
   1465  1.1  christos 		break;
   1466  1.1  christos 
   1467  1.1  christos 	      /* Remember this symbol and keep searching until we reach
   1468  1.1  christos 		 an earlier address.  */
   1469  1.1  christos 	      newplace = i;
   1470  1.1  christos 	    }
   1471  1.1  christos 	}
   1472  1.1  christos 
   1473  1.1  christos       if (newplace != sorted_symcount)
   1474  1.1  christos 	thisplace = newplace;
   1475  1.1  christos       else
   1476  1.7  christos 	{
   1477  1.1  christos 	  /* We didn't find a good symbol with a smaller value.
   1478  1.1  christos 	     Look for one with a larger value.  */
   1479  1.1  christos 	  for (i = thisplace + 1; i < sorted_symcount; i++)
   1480  1.1  christos 	    {
   1481  1.1  christos 	      if (sym_ok (want_section, abfd, i, sec, inf))
   1482  1.1  christos 		{
   1483  1.1  christos 		  thisplace = i;
   1484  1.7  christos 		  break;
   1485  1.1  christos 		}
   1486  1.1  christos 	    }
   1487  1.1  christos 	}
   1488  1.1  christos 
   1489  1.6  christos       if (! sym_ok (want_section, abfd, thisplace, sec, inf))
   1490  1.6  christos 	/* There is no suitable symbol.  */
   1491  1.6  christos 	return NULL;
   1492  1.6  christos     }
   1493  1.8  christos 
   1494  1.6  christos   /* If we have not found an exact match for the specified address
   1495  1.6  christos      and we have dynamic relocations available, then we can produce
   1496  1.6  christos      a better result by matching a relocation to the address and
   1497  1.8  christos      using the symbol associated with that relocation.  */
   1498  1.8  christos   rel_count = inf->dynrelcount;
   1499  1.8  christos   if (!want_section
   1500  1.6  christos       && sorted_syms[thisplace]->value != vma
   1501  1.6  christos       && rel_count > 0
   1502  1.6  christos       && inf->dynrelbuf != NULL
   1503  1.6  christos       && inf->dynrelbuf[0]->address <= vma
   1504  1.6  christos       && inf->dynrelbuf[rel_count - 1]->address >= vma
   1505  1.6  christos       /* If we have matched a synthetic symbol, then stick with that.  */
   1506  1.8  christos       && (sorted_syms[thisplace]->flags & BSF_SYNTHETIC) == 0)
   1507  1.6  christos     {
   1508  1.6  christos       arelent **  rel_low;
   1509  1.6  christos       arelent **  rel_high;
   1510  1.6  christos 
   1511  1.6  christos       rel_low = inf->dynrelbuf;
   1512  1.6  christos       rel_high = rel_low + rel_count - 1;
   1513  1.6  christos       while (rel_low <= rel_high)
   1514  1.6  christos 	{
   1515  1.6  christos 	  arelent **rel_mid = &rel_low[(rel_high - rel_low) / 2];
   1516  1.8  christos 	  arelent * rel = *rel_mid;
   1517  1.6  christos 
   1518  1.6  christos 	  if (rel->address == vma)
   1519  1.6  christos 	    {
   1520  1.6  christos 	      /* Absolute relocations do not provide a more helpful
   1521  1.6  christos 		 symbolic address.  Find a non-absolute relocation
   1522  1.6  christos 		 with the same address.  */
   1523  1.6  christos 	      arelent **rel_vma = rel_mid;
   1524  1.6  christos 	      for (rel_mid--;
   1525  1.6  christos 		   rel_mid >= rel_low && rel_mid[0]->address == vma;
   1526  1.6  christos 		   rel_mid--)
   1527  1.6  christos 		rel_vma = rel_mid;
   1528  1.6  christos 
   1529  1.6  christos 	      for (; rel_vma <= rel_high && rel_vma[0]->address == vma;
   1530  1.6  christos 		   rel_vma++)
   1531  1.6  christos 		{
   1532  1.6  christos 		  rel = *rel_vma;
   1533  1.6  christos 		  if (rel->sym_ptr_ptr != NULL
   1534  1.6  christos 		      && ! bfd_is_abs_section ((* rel->sym_ptr_ptr)->section))
   1535  1.6  christos 		    {
   1536  1.6  christos 		      if (place != NULL)
   1537  1.6  christos 			* place = thisplace;
   1538  1.6  christos 		      return * rel->sym_ptr_ptr;
   1539  1.6  christos 		    }
   1540  1.6  christos 		}
   1541  1.6  christos 	      break;
   1542  1.6  christos 	    }
   1543  1.6  christos 
   1544  1.6  christos 	  if (vma < rel->address)
   1545  1.6  christos 	    rel_high = rel_mid;
   1546  1.6  christos 	  else if (vma >= rel_mid[1]->address)
   1547  1.6  christos 	    rel_low = rel_mid + 1;
   1548  1.1  christos 	  else
   1549  1.1  christos 	    break;
   1550  1.1  christos 	}
   1551  1.1  christos     }
   1552  1.1  christos 
   1553  1.1  christos   if (place != NULL)
   1554  1.1  christos     *place = thisplace;
   1555  1.1  christos 
   1556  1.1  christos   return sorted_syms[thisplace];
   1557  1.1  christos }
   1558  1.1  christos 
   1559  1.8  christos /* Print an address and the offset to the nearest symbol.  */
   1560  1.1  christos 
   1561  1.8  christos static void
   1562  1.8  christos objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
   1563  1.8  christos 			     bfd_vma vma, struct disassemble_info *inf,
   1564  1.8  christos 			     bool skip_zeroes)
   1565  1.8  christos {
   1566  1.1  christos   if (!no_addresses)
   1567  1.1  christos     {
   1568  1.1  christos       objdump_print_value (vma, inf, skip_zeroes);
   1569  1.1  christos       (*inf->fprintf_styled_func) (inf->stream, dis_style_text, " ");
   1570  1.1  christos     }
   1571  1.8  christos 
   1572  1.8  christos   if (sym == NULL)
   1573  1.8  christos     {
   1574  1.7  christos       bfd_vma secaddr;
   1575  1.1  christos 
   1576  1.1  christos       (*inf->fprintf_styled_func) (inf->stream, dis_style_text,"<");
   1577  1.8  christos       (*inf->fprintf_styled_func) (inf->stream, dis_style_symbol, "%s",
   1578  1.8  christos 				   sanitize_string (bfd_section_name (sec)));
   1579  1.8  christos       secaddr = bfd_section_vma (sec);
   1580  1.1  christos       if (vma < secaddr)
   1581  1.1  christos 	{
   1582  1.1  christos 	  (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate,
   1583  1.8  christos 				       "-0x");
   1584  1.8  christos 	  objdump_print_value (secaddr - vma, inf, true);
   1585  1.1  christos 	}
   1586  1.8  christos       else if (vma > secaddr)
   1587  1.1  christos 	{
   1588  1.1  christos 	  (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate, "+0x");
   1589  1.1  christos 	  objdump_print_value (vma - secaddr, inf, true);
   1590  1.8  christos 	}
   1591  1.6  christos       (*inf->fprintf_styled_func) (inf->stream, dis_style_text, ">");
   1592  1.1  christos     }
   1593  1.6  christos   else
   1594  1.6  christos     {
   1595  1.6  christos       (*inf->fprintf_styled_func) (inf->stream, dis_style_text, "<");
   1596  1.6  christos 
   1597  1.6  christos       objdump_print_symname (abfd, inf, sym);
   1598  1.6  christos 
   1599  1.6  christos       if (bfd_asymbol_value (sym) == vma)
   1600  1.6  christos 	;
   1601  1.6  christos       /* Undefined symbols in an executables and dynamic objects do not have
   1602  1.6  christos 	 a value associated with them, so it does not make sense to display
   1603  1.6  christos 	 an offset relative to them.  Normally we would not be provided with
   1604  1.6  christos 	 this kind of symbol, but the target backend might choose to do so,
   1605  1.6  christos 	 and the code in find_symbol_for_address might return an as yet
   1606  1.1  christos 	 unresolved symbol associated with a dynamic reloc.  */
   1607  1.8  christos       else if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
   1608  1.8  christos 	       && bfd_is_und_section (sym->section))
   1609  1.1  christos 	;
   1610  1.1  christos       else if (bfd_asymbol_value (sym) > vma)
   1611  1.1  christos 	{
   1612  1.8  christos 	  (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate,"-0x");
   1613  1.8  christos 	  objdump_print_value (bfd_asymbol_value (sym) - vma, inf, true);
   1614  1.1  christos 	}
   1615  1.6  christos       else if (vma > bfd_asymbol_value (sym))
   1616  1.8  christos 	{
   1617  1.1  christos 	  (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate, "+0x");
   1618  1.1  christos 	  objdump_print_value (vma - bfd_asymbol_value (sym), inf, true);
   1619  1.1  christos 	}
   1620  1.8  christos 
   1621  1.8  christos       (*inf->fprintf_styled_func) (inf->stream, dis_style_text, ">");
   1622  1.8  christos     }
   1623  1.1  christos 
   1624  1.1  christos   if (display_file_offsets)
   1625  1.1  christos     inf->fprintf_styled_func (inf->stream, dis_style_text,
   1626  1.1  christos 			      _(" (File Offset: 0x%lx)"),
   1627  1.1  christos 			      (long int)(sec->filepos + (vma - sec->vma)));
   1628  1.1  christos }
   1629  1.1  christos 
   1630  1.1  christos /* Print an address (VMA), symbolically if possible.
   1631  1.8  christos    If SKIP_ZEROES is TRUE, don't output leading zeroes.  */
   1632  1.1  christos 
   1633  1.1  christos static void
   1634  1.1  christos objdump_print_addr (bfd_vma vma,
   1635  1.8  christos 		    struct disassemble_info *inf,
   1636  1.1  christos 		    bool skip_zeroes)
   1637  1.1  christos {
   1638  1.1  christos   struct objdump_disasm_info *aux;
   1639  1.1  christos   asymbol *sym = NULL;
   1640  1.1  christos   bool skip_find = false;
   1641  1.8  christos 
   1642  1.8  christos   aux = (struct objdump_disasm_info *) inf->application_data;
   1643  1.8  christos 
   1644  1.8  christos   if (sorted_symcount < 1)
   1645  1.8  christos     {
   1646  1.1  christos       if (!no_addresses)
   1647  1.1  christos 	{
   1648  1.8  christos 	  (*inf->fprintf_styled_func) (inf->stream, dis_style_address, "0x");
   1649  1.8  christos 	  objdump_print_value (vma, inf, skip_zeroes);
   1650  1.8  christos 	}
   1651  1.8  christos 
   1652  1.1  christos       if (display_file_offsets)
   1653  1.1  christos 	inf->fprintf_styled_func (inf->stream, dis_style_text,
   1654  1.1  christos 				  _(" (File Offset: 0x%lx)"),
   1655  1.1  christos 				  (long int) (inf->section->filepos
   1656  1.1  christos 					      + (vma - inf->section->vma)));
   1657  1.1  christos       return;
   1658  1.1  christos     }
   1659  1.1  christos 
   1660  1.1  christos   if (aux->reloc != NULL
   1661  1.1  christos       && aux->reloc->sym_ptr_ptr != NULL
   1662  1.1  christos       && * aux->reloc->sym_ptr_ptr != NULL)
   1663  1.1  christos     {
   1664  1.7  christos       sym = * aux->reloc->sym_ptr_ptr;
   1665  1.8  christos 
   1666  1.1  christos       /* Adjust the vma to the reloc.  */
   1667  1.1  christos       vma += bfd_asymbol_value (sym);
   1668  1.1  christos 
   1669  1.1  christos       if (bfd_is_und_section (bfd_asymbol_section (sym)))
   1670  1.1  christos 	skip_find = true;
   1671  1.7  christos     }
   1672  1.1  christos 
   1673  1.1  christos   if (!skip_find)
   1674  1.1  christos     sym = find_symbol_for_address (vma, inf, NULL);
   1675  1.1  christos 
   1676  1.1  christos   objdump_print_addr_with_sym (aux->abfd, inf->section, sym, vma, inf,
   1677  1.1  christos 			       skip_zeroes);
   1678  1.1  christos }
   1679  1.1  christos 
   1680  1.1  christos /* Print VMA to INFO.  This function is passed to the disassembler
   1681  1.1  christos    routine.  */
   1682  1.1  christos 
   1683  1.1  christos static void
   1684  1.1  christos objdump_print_address (bfd_vma vma, struct disassemble_info *inf)
   1685  1.1  christos {
   1686  1.8  christos   objdump_print_addr (vma, inf, ! prefix_addresses);
   1687  1.1  christos }
   1688  1.1  christos 
   1689  1.1  christos /* Determine if the given address has a symbol associated with it.  */
   1690  1.1  christos 
   1691  1.1  christos static asymbol *
   1692  1.8  christos objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * inf)
   1693  1.8  christos {
   1694  1.1  christos   asymbol * sym;
   1695  1.8  christos 
   1696  1.1  christos   sym = find_symbol_for_address (vma, inf, NULL);
   1697  1.1  christos   if (sym != NULL && bfd_asymbol_value (sym) == vma)
   1698  1.1  christos     return sym;
   1699  1.1  christos 
   1700  1.1  christos   return NULL;
   1701  1.1  christos }
   1702  1.1  christos 
   1703  1.1  christos /* Hold the last function name and the last line number we displayed
   1704  1.1  christos    in a disassembly.  */
   1705  1.1  christos 
   1706  1.1  christos static char *prev_functionname;
   1707  1.1  christos static unsigned int prev_line;
   1708  1.1  christos static unsigned int prev_discriminator;
   1709  1.1  christos 
   1710  1.1  christos /* We keep a list of all files that we have seen when doing a
   1711  1.1  christos    disassembly with source, so that we know how much of the file to
   1712  1.1  christos    display.  This can be important for inlined functions.  */
   1713  1.1  christos 
   1714  1.3  christos struct print_file_list
   1715  1.1  christos {
   1716  1.3  christos   struct print_file_list *next;
   1717  1.1  christos   const char *filename;
   1718  1.1  christos   const char *modname;
   1719  1.5  christos   const char *map;
   1720  1.1  christos   size_t mapsize;
   1721  1.1  christos   const char **linemap;
   1722  1.1  christos   unsigned maxline;
   1723  1.1  christos   unsigned last_line;
   1724  1.1  christos   unsigned max_printed;
   1725  1.1  christos   int first;
   1726  1.1  christos };
   1727  1.1  christos 
   1728  1.1  christos static struct print_file_list *print_files;
   1729  1.1  christos 
   1730  1.1  christos /* The number of preceding context lines to show when we start
   1731  1.1  christos    displaying a file for the first time.  */
   1732  1.1  christos 
   1733  1.6  christos #define SHOW_PRECEDING_CONTEXT_LINES (5)
   1734  1.1  christos 
   1735  1.1  christos /* Read a complete file into memory.  */
   1736  1.1  christos 
   1737  1.1  christos static const char *
   1738  1.1  christos slurp_file (const char *fn, size_t *size, struct stat *fst)
   1739  1.1  christos {
   1740  1.1  christos #ifdef HAVE_MMAP
   1741  1.1  christos   int ps = getpagesize ();
   1742  1.1  christos   size_t msize;
   1743  1.1  christos #endif
   1744  1.6  christos   const char *map;
   1745  1.1  christos   int fd = open (fn, O_RDONLY | O_BINARY);
   1746  1.1  christos 
   1747  1.1  christos   if (fd < 0)
   1748  1.1  christos     return NULL;
   1749  1.6  christos   if (fstat (fd, fst) < 0)
   1750  1.1  christos     {
   1751  1.1  christos       close (fd);
   1752  1.1  christos       return NULL;
   1753  1.1  christos     }
   1754  1.1  christos   *size = fst->st_size;
   1755  1.1  christos #ifdef HAVE_MMAP
   1756  1.1  christos   msize = (*size + ps - 1) & ~(ps - 1);
   1757  1.1  christos   map = mmap (NULL, msize, PROT_READ, MAP_SHARED, fd, 0);
   1758  1.1  christos   if (map != (char *) -1L)
   1759  1.1  christos     {
   1760  1.1  christos       close (fd);
   1761  1.1  christos       return map;
   1762  1.1  christos     }
   1763  1.1  christos #endif
   1764  1.1  christos   map = (const char *) malloc (*size);
   1765  1.1  christos   if (!map || (size_t) read (fd, (char *) map, *size) != *size)
   1766  1.1  christos     {
   1767  1.1  christos       free ((void *) map);
   1768  1.1  christos       map = NULL;
   1769  1.1  christos     }
   1770  1.1  christos   close (fd);
   1771  1.1  christos   return map;
   1772  1.1  christos }
   1773  1.3  christos 
   1774  1.3  christos #define line_map_decrease 5
   1775  1.1  christos 
   1776  1.1  christos /* Precompute array of lines for a mapped file. */
   1777  1.1  christos 
   1778  1.1  christos static const char **
   1779  1.3  christos index_file (const char *map, size_t size, unsigned int *maxline)
   1780  1.1  christos {
   1781  1.3  christos   const char *p, *lstart, *end;
   1782  1.1  christos   int chars_per_line = 45; /* First iteration will use 40.  */
   1783  1.1  christos   unsigned int lineno;
   1784  1.1  christos   const char **linemap = NULL;
   1785  1.1  christos   unsigned long line_map_size = 0;
   1786  1.3  christos 
   1787  1.3  christos   lineno = 0;
   1788  1.3  christos   lstart = map;
   1789  1.3  christos   end = map + size;
   1790  1.3  christos 
   1791  1.3  christos   for (p = map; p < end; p++)
   1792  1.3  christos     {
   1793  1.3  christos       if (*p == '\n')
   1794  1.3  christos 	{
   1795  1.1  christos 	  if (p + 1 < end && p[1] == '\r')
   1796  1.1  christos 	    p++;
   1797  1.1  christos 	}
   1798  1.1  christos       else if (*p == '\r')
   1799  1.1  christos 	{
   1800  1.3  christos 	  if (p + 1 < end && p[1] == '\n')
   1801  1.1  christos 	    p++;
   1802  1.1  christos 	}
   1803  1.3  christos       else
   1804  1.3  christos 	continue;
   1805  1.1  christos 
   1806  1.1  christos       /* End of line found.  */
   1807  1.1  christos 
   1808  1.1  christos       if (linemap == NULL || line_map_size < lineno + 1)
   1809  1.1  christos 	{
   1810  1.1  christos 	  unsigned long newsize;
   1811  1.1  christos 
   1812  1.1  christos 	  chars_per_line -= line_map_decrease;
   1813  1.1  christos 	  if (chars_per_line <= 1)
   1814  1.1  christos 	    chars_per_line = 1;
   1815  1.1  christos 	  line_map_size = size / chars_per_line + 1;
   1816  1.1  christos 	  if (line_map_size < lineno + 1)
   1817  1.3  christos 	    line_map_size = lineno + 1;
   1818  1.3  christos 	  newsize = line_map_size * sizeof (char *);
   1819  1.1  christos 	  linemap = (const char **) xrealloc (linemap, newsize);
   1820  1.3  christos 	}
   1821  1.3  christos 
   1822  1.1  christos       linemap[lineno++] = lstart;
   1823  1.1  christos       lstart = p + 1;
   1824  1.1  christos     }
   1825  1.1  christos 
   1826  1.1  christos   *maxline = lineno;
   1827  1.1  christos   return linemap;
   1828  1.1  christos }
   1829  1.6  christos 
   1830  1.1  christos /* Tries to open MODNAME, and if successful adds a node to print_files
   1831  1.1  christos    linked list and returns that node.  Returns NULL on failure.  */
   1832  1.1  christos 
   1833  1.1  christos static struct print_file_list *
   1834  1.1  christos try_print_file_open (const char *origname, const char *modname, struct stat *fst)
   1835  1.6  christos {
   1836  1.1  christos   struct print_file_list *p;
   1837  1.1  christos 
   1838  1.1  christos   p = (struct print_file_list *) xmalloc (sizeof (struct print_file_list));
   1839  1.1  christos 
   1840  1.1  christos   p->map = slurp_file (modname, &p->mapsize, fst);
   1841  1.3  christos   if (p->map == NULL)
   1842  1.1  christos     {
   1843  1.1  christos       free (p);
   1844  1.5  christos       return NULL;
   1845  1.1  christos     }
   1846  1.1  christos 
   1847  1.1  christos   p->linemap = index_file (p->map, p->mapsize, &p->maxline);
   1848  1.1  christos   p->last_line = 0;
   1849  1.1  christos   p->max_printed = 0;
   1850  1.1  christos   p->filename = origname;
   1851  1.1  christos   p->modname = modname;
   1852  1.1  christos   p->next = print_files;
   1853  1.3  christos   p->first = 1;
   1854  1.1  christos   print_files = p;
   1855  1.1  christos   return p;
   1856  1.1  christos }
   1857  1.1  christos 
   1858  1.6  christos /* If the source file, as described in the symtab, is not found
   1859  1.1  christos    try to locate it in one of the paths specified with -I
   1860  1.1  christos    If found, add location to print_files linked list.  */
   1861  1.1  christos 
   1862  1.6  christos static struct print_file_list *
   1863  1.1  christos update_source_path (const char *filename, bfd *abfd)
   1864  1.1  christos {
   1865  1.6  christos   struct print_file_list *p;
   1866  1.6  christos   const char *fname;
   1867  1.6  christos   struct stat fst;
   1868  1.6  christos   int i;
   1869  1.6  christos 
   1870  1.6  christos   p = try_print_file_open (filename, filename, &fst);
   1871  1.6  christos   if (p == NULL)
   1872  1.6  christos     {
   1873  1.1  christos       if (include_path_count == 0)
   1874  1.6  christos 	return NULL;
   1875  1.6  christos 
   1876  1.6  christos       /* Get the name of the file.  */
   1877  1.6  christos       fname = lbasename (filename);
   1878  1.6  christos 
   1879  1.6  christos       /* If file exists under a new path, we need to add it to the list
   1880  1.1  christos 	 so that show_line knows about it.  */
   1881  1.6  christos       for (i = 0; i < include_path_count; i++)
   1882  1.6  christos 	{
   1883  1.6  christos 	  char *modname = concat (include_paths[i], "/", fname,
   1884  1.1  christos 				  (const char *) 0);
   1885  1.6  christos 
   1886  1.6  christos 	  p = try_print_file_open (filename, modname, &fst);
   1887  1.6  christos 	  if (p)
   1888  1.6  christos 	    break;
   1889  1.6  christos 
   1890  1.1  christos 	  free (modname);
   1891  1.6  christos 	}
   1892  1.1  christos     }
   1893  1.6  christos 
   1894  1.6  christos   if (p != NULL)
   1895  1.6  christos     {
   1896  1.1  christos       long mtime = bfd_get_mtime (abfd);
   1897  1.1  christos 
   1898  1.6  christos       if (fst.st_mtime > mtime)
   1899  1.1  christos 	warn (_("source file %s is more recent than object file\n"),
   1900  1.1  christos 	      filename);
   1901  1.1  christos     }
   1902  1.1  christos 
   1903  1.3  christos   return p;
   1904  1.1  christos }
   1905  1.1  christos 
   1906  1.1  christos /* Print a source file line.  */
   1907  1.1  christos 
   1908  1.3  christos static void
   1909  1.3  christos print_line (struct print_file_list *p, unsigned int linenum)
   1910  1.1  christos {
   1911  1.1  christos   const char *l;
   1912  1.1  christos   size_t len;
   1913  1.7  christos 
   1914  1.7  christos   --linenum;
   1915  1.7  christos   if (linenum >= p->maxline)
   1916  1.1  christos     return;
   1917  1.1  christos   l = p->linemap [linenum];
   1918  1.1  christos   if (source_comment != NULL && strlen (l) > 0)
   1919  1.1  christos     printf ("%s", source_comment);
   1920  1.1  christos   len = strcspn (l, "\n\r");
   1921  1.1  christos   /* Test fwrite return value to quiet glibc warning.  */
   1922  1.1  christos   if (len == 0 || fwrite (l, len, 1, stdout) == 1)
   1923  1.1  christos     putchar ('\n');
   1924  1.1  christos }
   1925  1.1  christos 
   1926  1.1  christos /* Print a range of source code lines. */
   1927  1.1  christos 
   1928  1.3  christos static void
   1929  1.1  christos dump_lines (struct print_file_list *p, unsigned int start, unsigned int end)
   1930  1.1  christos {
   1931  1.1  christos   if (p->map == NULL)
   1932  1.1  christos     return;
   1933  1.1  christos   while (start <= end)
   1934  1.1  christos     {
   1935  1.1  christos       print_line (p, start);
   1936  1.1  christos       start++;
   1937  1.1  christos     }
   1938  1.1  christos }
   1939  1.1  christos 
   1940  1.1  christos /* Show the line number, or the source line, in a disassembly
   1941  1.1  christos    listing.  */
   1942  1.1  christos 
   1943  1.1  christos static void
   1944  1.1  christos show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
   1945  1.8  christos {
   1946  1.5  christos   const char *filename;
   1947  1.1  christos   const char *functionname;
   1948  1.1  christos   unsigned int linenumber;
   1949  1.1  christos   unsigned int discriminator;
   1950  1.1  christos   bool reloc;
   1951  1.1  christos   char *path = NULL;
   1952  1.6  christos 
   1953  1.6  christos   if (! with_line_numbers && ! with_source_code)
   1954  1.1  christos     return;
   1955  1.1  christos 
   1956  1.1  christos   if (! bfd_find_nearest_line_discriminator (abfd, section, syms, addr_offset,
   1957  1.1  christos 					     &filename, &functionname,
   1958  1.1  christos 					     &linenumber, &discriminator))
   1959  1.1  christos     return;
   1960  1.1  christos 
   1961  1.1  christos   if (filename != NULL && *filename == '\0')
   1962  1.1  christos     filename = NULL;
   1963  1.1  christos   if (functionname != NULL && *functionname == '\0')
   1964  1.1  christos     functionname = NULL;
   1965  1.1  christos 
   1966  1.1  christos   if (filename
   1967  1.5  christos       && IS_ABSOLUTE_PATH (filename)
   1968  1.8  christos       && prefix)
   1969  1.1  christos     {
   1970  1.1  christos       char *path_up;
   1971  1.1  christos       const char *fname = filename;
   1972  1.1  christos 
   1973  1.1  christos       path = xmalloc (prefix_length + 1 + strlen (filename));
   1974  1.1  christos 
   1975  1.5  christos       if (prefix_length)
   1976  1.1  christos 	memcpy (path, prefix, prefix_length);
   1977  1.1  christos       path_up = path + prefix_length;
   1978  1.1  christos 
   1979  1.1  christos       /* Build relocated filename, stripping off leading directories
   1980  1.1  christos 	 from the initial filename if requested.  */
   1981  1.5  christos       if (prefix_strip > 0)
   1982  1.1  christos 	{
   1983  1.7  christos 	  int level = 0;
   1984  1.1  christos 	  const char *s;
   1985  1.1  christos 
   1986  1.1  christos 	  /* Skip selected directory levels.  */
   1987  1.1  christos 	  for (s = fname + 1; *s != '\0' && level < prefix_strip; s++)
   1988  1.1  christos 	    if (IS_DIR_SEPARATOR (*s))
   1989  1.1  christos 	      {
   1990  1.5  christos 		fname = s;
   1991  1.8  christos 		level++;
   1992  1.1  christos 	      }
   1993  1.1  christos 	}
   1994  1.8  christos 
   1995  1.1  christos       /* Update complete filename.  */
   1996  1.1  christos       strcpy (path_up, fname);
   1997  1.8  christos 
   1998  1.1  christos       filename = path;
   1999  1.1  christos       reloc = true;
   2000  1.1  christos     }
   2001  1.1  christos   else
   2002  1.1  christos     reloc = false;
   2003  1.1  christos 
   2004  1.6  christos   if (with_line_numbers)
   2005  1.8  christos     {
   2006  1.8  christos       if (functionname != NULL
   2007  1.8  christos 	  && (prev_functionname == NULL
   2008  1.8  christos 	      || strcmp (functionname, prev_functionname) != 0))
   2009  1.8  christos 	{
   2010  1.8  christos 	  char *demangle_alloc = NULL;
   2011  1.8  christos 	  if (do_demangle && functionname[0] != '\0')
   2012  1.8  christos 	    {
   2013  1.8  christos 	      /* Demangle the name.  */
   2014  1.8  christos 	      demangle_alloc = bfd_demangle (abfd, functionname,
   2015  1.8  christos 					     demangle_flags);
   2016  1.8  christos 	    }
   2017  1.8  christos 
   2018  1.8  christos 	  /* Demangling adds trailing parens, so don't print those.  */
   2019  1.6  christos 	  if (demangle_alloc != NULL)
   2020  1.8  christos 	    printf ("%s:\n", sanitize_string (demangle_alloc));
   2021  1.6  christos 	  else
   2022  1.6  christos 	    printf ("%s():\n", sanitize_string (functionname));
   2023  1.6  christos 
   2024  1.6  christos 	  prev_line = -1;
   2025  1.6  christos 	  free (demangle_alloc);
   2026  1.6  christos 	}
   2027  1.6  christos       if (linenumber > 0
   2028  1.7  christos 	  && (linenumber != prev_line
   2029  1.6  christos 	      || discriminator != prev_discriminator))
   2030  1.6  christos 	{
   2031  1.7  christos 	  if (discriminator > 0)
   2032  1.7  christos 	    printf ("%s:%u (discriminator %u)\n",
   2033  1.6  christos 		    filename == NULL ? "???" : sanitize_string (filename),
   2034  1.6  christos 		    linenumber, discriminator);
   2035  1.6  christos 	  else
   2036  1.6  christos 	    printf ("%s:%u\n", filename == NULL
   2037  1.6  christos 		    ? "???" : sanitize_string (filename),
   2038  1.6  christos 		    linenumber);
   2039  1.6  christos 	}
   2040  1.6  christos       if (unwind_inlines)
   2041  1.6  christos 	{
   2042  1.6  christos 	  const char *filename2;
   2043  1.7  christos 	  const char *functionname2;
   2044  1.7  christos 	  unsigned line2;
   2045  1.7  christos 
   2046  1.7  christos 	  while (bfd_find_inliner_info (abfd, &filename2, &functionname2,
   2047  1.7  christos 					&line2))
   2048  1.6  christos 	    {
   2049  1.1  christos 	      printf ("inlined by %s:%u",
   2050  1.1  christos 		      sanitize_string (filename2), line2);
   2051  1.1  christos 	      printf (" (%s)\n", sanitize_string (functionname2));
   2052  1.1  christos 	    }
   2053  1.1  christos 	}
   2054  1.1  christos     }
   2055  1.1  christos 
   2056  1.1  christos   if (with_source_code
   2057  1.1  christos       && filename != NULL
   2058  1.1  christos       && linenumber > 0)
   2059  1.1  christos     {
   2060  1.1  christos       struct print_file_list **pp, *p;
   2061  1.1  christos       unsigned l;
   2062  1.1  christos 
   2063  1.1  christos       for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
   2064  1.1  christos 	if (filename_cmp ((*pp)->filename, filename) == 0)
   2065  1.1  christos 	  break;
   2066  1.1  christos       p = *pp;
   2067  1.6  christos 
   2068  1.1  christos       if (p == NULL)
   2069  1.1  christos 	{
   2070  1.1  christos 	  if (reloc)
   2071  1.1  christos 	    filename = xstrdup (filename);
   2072  1.3  christos 	  p = update_source_path (filename, abfd);
   2073  1.1  christos 	}
   2074  1.3  christos 
   2075  1.1  christos       if (p != NULL && linenumber != p->last_line)
   2076  1.1  christos 	{
   2077  1.3  christos 	  if (file_start_context && p->first)
   2078  1.1  christos 	    l = 1;
   2079  1.5  christos 	  else
   2080  1.5  christos 	    {
   2081  1.5  christos 	      l = linenumber - SHOW_PRECEDING_CONTEXT_LINES;
   2082  1.5  christos 	      if (l >= linenumber)
   2083  1.5  christos 		l = 1;
   2084  1.5  christos 	      if (p->max_printed >= l)
   2085  1.5  christos 		{
   2086  1.1  christos 		  if (p->max_printed < linenumber)
   2087  1.1  christos 		    l = p->max_printed + 1;
   2088  1.5  christos 		  else
   2089  1.5  christos 		    l = linenumber;
   2090  1.1  christos 		}
   2091  1.1  christos 	    }
   2092  1.1  christos 	  dump_lines (p, l, linenumber);
   2093  1.1  christos 	  if (p->max_printed < linenumber)
   2094  1.1  christos 	    p->max_printed = linenumber;
   2095  1.1  christos 	  p->last_line = linenumber;
   2096  1.1  christos 	  p->first = 0;
   2097  1.1  christos 	}
   2098  1.1  christos     }
   2099  1.1  christos 
   2100  1.1  christos   if (functionname != NULL
   2101  1.1  christos       && (prev_functionname == NULL
   2102  1.1  christos 	  || strcmp (functionname, prev_functionname) != 0))
   2103  1.1  christos     {
   2104  1.1  christos       if (prev_functionname != NULL)
   2105  1.1  christos 	free (prev_functionname);
   2106  1.1  christos       prev_functionname = (char *) xmalloc (strlen (functionname) + 1);
   2107  1.1  christos       strcpy (prev_functionname, functionname);
   2108  1.1  christos     }
   2109  1.1  christos 
   2110  1.5  christos   if (linenumber > 0 && linenumber != prev_line)
   2111  1.5  christos     prev_line = linenumber;
   2112  1.5  christos 
   2113  1.1  christos   if (discriminator != prev_discriminator)
   2114  1.1  christos     prev_discriminator = discriminator;
   2115  1.1  christos 
   2116  1.1  christos   if (path)
   2117  1.1  christos     free (path);
   2118  1.1  christos }
   2119  1.1  christos 
   2120  1.1  christos /* Pseudo FILE object for strings.  */
   2121  1.1  christos typedef struct
   2122  1.1  christos {
   2123  1.1  christos   char *buffer;
   2124  1.1  christos   size_t pos;
   2125  1.1  christos   size_t alloc;
   2126  1.1  christos } SFILE;
   2127  1.1  christos 
   2128  1.1  christos /* sprintf to a "stream".  */
   2129  1.1  christos 
   2130  1.1  christos static int ATTRIBUTE_PRINTF_2
   2131  1.1  christos objdump_sprintf (SFILE *f, const char *format, ...)
   2132  1.1  christos {
   2133  1.1  christos   size_t n;
   2134  1.3  christos   va_list args;
   2135  1.1  christos 
   2136  1.1  christos   while (1)
   2137  1.1  christos     {
   2138  1.1  christos       size_t space = f->alloc - f->pos;
   2139  1.1  christos 
   2140  1.7  christos       va_start (args, format);
   2141  1.7  christos       n = vsnprintf (f->buffer + f->pos, space, format, args);
   2142  1.7  christos       va_end (args);
   2143  1.7  christos 
   2144  1.7  christos       if (space > n)
   2145  1.7  christos 	break;
   2146  1.7  christos 
   2147  1.7  christos       f->alloc = (f->alloc + n) * 2;
   2148  1.7  christos       f->buffer = (char *) xrealloc (f->buffer, f->alloc);
   2149  1.7  christos     }
   2150  1.8  christos   f->pos += n;
   2151  1.8  christos 
   2152  1.8  christos   return n;
   2153  1.8  christos }
   2154  1.8  christos 
   2155  1.8  christos /* Return an integer greater than, or equal to zero, representing the color
   2156  1.8  christos    for STYLE, or -1 if no color should be used.  */
   2157  1.8  christos 
   2158  1.8  christos static int
   2159  1.8  christos objdump_color_for_disassembler_style (enum disassembler_style style)
   2160  1.8  christos {
   2161  1.8  christos   int color = -1;
   2162  1.8  christos 
   2163  1.8  christos   if (style == dis_style_comment_start)
   2164  1.8  christos     disassembler_in_comment = true;
   2165  1.8  christos 
   2166  1.8  christos   if (disassembler_color)
   2167  1.8  christos     {
   2168  1.8  christos       if (disassembler_in_comment)
   2169  1.8  christos 	return color;
   2170  1.8  christos 
   2171  1.8  christos       switch (style)
   2172  1.8  christos 	{
   2173  1.8  christos 	case dis_style_symbol: color = 32; break;
   2174  1.8  christos         case dis_style_assembler_directive:
   2175  1.8  christos 	case dis_style_mnemonic: color = 33; break;
   2176  1.8  christos 	case dis_style_register: color = 34; break;
   2177  1.8  christos 	case dis_style_address:
   2178  1.8  christos         case dis_style_address_offset:
   2179  1.8  christos 	case dis_style_immediate: color = 35; break;
   2180  1.8  christos 	default:
   2181  1.8  christos 	case dis_style_text: color = -1; break;
   2182  1.8  christos 	}
   2183  1.8  christos     }
   2184  1.8  christos   else if (disassembler_extended_color)
   2185  1.8  christos     {
   2186  1.8  christos       if (disassembler_in_comment)
   2187  1.8  christos 	return 250;
   2188  1.8  christos 
   2189  1.8  christos       switch (style)
   2190  1.8  christos 	{
   2191  1.8  christos 	case dis_style_symbol: color = 40; break;
   2192  1.8  christos         case dis_style_assembler_directive:
   2193  1.8  christos 	case dis_style_mnemonic: color = 142; break;
   2194  1.8  christos 	case dis_style_register: color = 27; break;
   2195  1.8  christos 	case dis_style_address:
   2196  1.8  christos         case dis_style_address_offset:
   2197  1.8  christos 	case dis_style_immediate: color = 134; break;
   2198  1.8  christos 	default:
   2199  1.8  christos 	case dis_style_text: color = -1; break;
   2200  1.8  christos 	}
   2201  1.8  christos     }
   2202  1.8  christos 
   2203  1.8  christos   return color;
   2204  1.8  christos }
   2205  1.8  christos 
   2206  1.8  christos /* Like objdump_sprintf, but add in escape sequences to highlight the
   2207  1.8  christos    content according to STYLE.  */
   2208  1.8  christos 
   2209  1.8  christos static int ATTRIBUTE_PRINTF_3
   2210  1.8  christos objdump_styled_sprintf (SFILE *f, enum disassembler_style style,
   2211  1.8  christos 			const char *format, ...)
   2212  1.8  christos {
   2213  1.8  christos   size_t n;
   2214  1.8  christos   va_list args;
   2215  1.8  christos   int color = objdump_color_for_disassembler_style (style);
   2216  1.8  christos 
   2217  1.8  christos   if (color >= 0)
   2218  1.8  christos     {
   2219  1.8  christos       while (1)
   2220  1.8  christos 	{
   2221  1.8  christos 	  size_t space = f->alloc - f->pos;
   2222  1.8  christos 
   2223  1.8  christos 	  if (disassembler_color)
   2224  1.8  christos 	    n = snprintf (f->buffer + f->pos, space, "\033[%dm", color);
   2225  1.8  christos 	  else
   2226  1.8  christos 	    n = snprintf (f->buffer + f->pos, space, "\033[38;5;%dm", color);
   2227  1.8  christos 	  if (space > n)
   2228  1.8  christos 	    break;
   2229  1.8  christos 
   2230  1.8  christos 	  f->alloc = (f->alloc + n) * 2;
   2231  1.8  christos 	  f->buffer = (char *) xrealloc (f->buffer, f->alloc);
   2232  1.8  christos 	}
   2233  1.8  christos       f->pos += n;
   2234  1.8  christos     }
   2235  1.8  christos 
   2236  1.8  christos   while (1)
   2237  1.8  christos     {
   2238  1.8  christos       size_t space = f->alloc - f->pos;
   2239  1.8  christos 
   2240  1.8  christos       va_start (args, format);
   2241  1.8  christos       n = vsnprintf (f->buffer + f->pos, space, format, args);
   2242  1.8  christos       va_end (args);
   2243  1.8  christos 
   2244  1.8  christos       if (space > n)
   2245  1.8  christos 	break;
   2246  1.8  christos 
   2247  1.8  christos       f->alloc = (f->alloc + n) * 2;
   2248  1.8  christos       f->buffer = (char *) xrealloc (f->buffer, f->alloc);
   2249  1.8  christos     }
   2250  1.8  christos   f->pos += n;
   2251  1.8  christos 
   2252  1.8  christos   if (color >= 0)
   2253  1.8  christos     {
   2254  1.8  christos       while (1)
   2255  1.8  christos 	{
   2256  1.8  christos 	  size_t space = f->alloc - f->pos;
   2257  1.8  christos 
   2258  1.8  christos 	  n = snprintf (f->buffer + f->pos, space, "\033[0m");
   2259  1.8  christos 
   2260  1.8  christos 	  if (space > n)
   2261  1.8  christos 	    break;
   2262  1.8  christos 
   2263  1.8  christos 	  f->alloc = (f->alloc + n) * 2;
   2264  1.8  christos 	  f->buffer = (char *) xrealloc (f->buffer, f->alloc);
   2265  1.8  christos 	}
   2266  1.8  christos       f->pos += n;
   2267  1.8  christos     }
   2268  1.8  christos 
   2269  1.8  christos   return n;
   2270  1.8  christos }
   2271  1.8  christos 
   2272  1.8  christos /* We discard the styling information here.  This function is only used
   2273  1.8  christos    when objdump is printing auxiliary information, the symbol headers, and
   2274  1.8  christos    disassembly address, or the bytes of the disassembled instruction.  We
   2275  1.8  christos    don't (currently) apply styling to any of this stuff, so, for now, just
   2276  1.8  christos    print the content with no additional style added.  */
   2277  1.8  christos 
   2278  1.8  christos static int ATTRIBUTE_PRINTF_3
   2279  1.8  christos fprintf_styled (FILE *f, enum disassembler_style style ATTRIBUTE_UNUSED,
   2280  1.8  christos 		const char *fmt, ...)
   2281  1.8  christos {
   2282  1.8  christos   int res;
   2283  1.8  christos   va_list ap;
   2284  1.8  christos 
   2285  1.8  christos   va_start (ap, fmt);
   2286  1.8  christos   res = vfprintf (f, fmt, ap);
   2287  1.7  christos   va_end (ap);
   2288  1.7  christos 
   2289  1.7  christos   return res;
   2290  1.7  christos }
   2291  1.7  christos 
   2292  1.7  christos /* Code for generating (colored) diagrams of control flow start and end
   2293  1.7  christos    points.  */
   2294  1.7  christos 
   2295  1.7  christos /* Structure used to store the properties of a jump.  */
   2296  1.7  christos 
   2297  1.7  christos struct jump_info
   2298  1.7  christos {
   2299  1.7  christos   /* The next jump, or NULL if this is the last object.  */
   2300  1.7  christos   struct jump_info *next;
   2301  1.7  christos   /* The previous jump, or NULL if this is the first object.  */
   2302  1.7  christos   struct jump_info *prev;
   2303  1.7  christos   /* The start addresses of the jump.  */
   2304  1.7  christos   struct
   2305  1.7  christos     {
   2306  1.7  christos       /* The list of start addresses.  */
   2307  1.7  christos       bfd_vma *addresses;
   2308  1.7  christos       /* The number of elements.  */
   2309  1.7  christos       size_t count;
   2310  1.7  christos       /* The maximum number of elements that fit into the array.  */
   2311  1.7  christos       size_t max_count;
   2312  1.7  christos     } start;
   2313  1.7  christos   /* The end address of the jump.  */
   2314  1.7  christos   bfd_vma end;
   2315  1.7  christos   /* The drawing level of the jump.  */
   2316  1.7  christos   int level;
   2317  1.7  christos };
   2318  1.7  christos 
   2319  1.7  christos /* Construct a jump object for a jump from start
   2320  1.7  christos    to end with the corresponding level.  */
   2321  1.7  christos 
   2322  1.7  christos static struct jump_info *
   2323  1.7  christos jump_info_new (bfd_vma start, bfd_vma end, int level)
   2324  1.7  christos {
   2325  1.7  christos   struct jump_info *result = xmalloc (sizeof (struct jump_info));
   2326  1.7  christos 
   2327  1.7  christos   result->next = NULL;
   2328  1.7  christos   result->prev = NULL;
   2329  1.7  christos   result->start.addresses = xmalloc (sizeof (bfd_vma *) * 2);
   2330  1.7  christos   result->start.addresses[0] = start;
   2331  1.7  christos   result->start.count = 1;
   2332  1.7  christos   result->start.max_count = 2;
   2333  1.7  christos   result->end = end;
   2334  1.7  christos   result->level = level;
   2335  1.7  christos 
   2336  1.7  christos   return result;
   2337  1.7  christos }
   2338  1.7  christos 
   2339  1.7  christos /* Free a jump object and return the next object
   2340  1.7  christos    or NULL if this was the last one.  */
   2341  1.7  christos 
   2342  1.7  christos static struct jump_info *
   2343  1.7  christos jump_info_free (struct jump_info *ji)
   2344  1.7  christos {
   2345  1.7  christos   struct jump_info *result = NULL;
   2346  1.7  christos 
   2347  1.7  christos   if (ji)
   2348  1.7  christos     {
   2349  1.7  christos       result = ji->next;
   2350  1.7  christos       if (ji->start.addresses)
   2351  1.7  christos 	free (ji->start.addresses);
   2352  1.7  christos       free (ji);
   2353  1.7  christos     }
   2354  1.7  christos 
   2355  1.7  christos   return result;
   2356  1.7  christos }
   2357  1.7  christos 
   2358  1.7  christos /* Get the smallest value of all start and end addresses.  */
   2359  1.7  christos 
   2360  1.7  christos static bfd_vma
   2361  1.7  christos jump_info_min_address (const struct jump_info *ji)
   2362  1.7  christos {
   2363  1.7  christos   bfd_vma min_address = ji->end;
   2364  1.7  christos   size_t i;
   2365  1.7  christos 
   2366  1.7  christos   for (i = ji->start.count; i-- > 0;)
   2367  1.7  christos     if (ji->start.addresses[i] < min_address)
   2368  1.7  christos       min_address = ji->start.addresses[i];
   2369  1.7  christos   return min_address;
   2370  1.7  christos }
   2371  1.7  christos 
   2372  1.7  christos /* Get the largest value of all start and end addresses.  */
   2373  1.7  christos 
   2374  1.7  christos static bfd_vma
   2375  1.7  christos jump_info_max_address (const struct jump_info *ji)
   2376  1.7  christos {
   2377  1.7  christos   bfd_vma max_address = ji->end;
   2378  1.7  christos   size_t i;
   2379  1.7  christos 
   2380  1.7  christos   for (i = ji->start.count; i-- > 0;)
   2381  1.7  christos     if (ji->start.addresses[i] > max_address)
   2382  1.7  christos       max_address = ji->start.addresses[i];
   2383  1.7  christos   return max_address;
   2384  1.7  christos }
   2385  1.7  christos 
   2386  1.7  christos /* Get the target address of a jump.  */
   2387  1.7  christos 
   2388  1.7  christos static bfd_vma
   2389  1.7  christos jump_info_end_address (const struct jump_info *ji)
   2390  1.7  christos {
   2391  1.8  christos   return ji->end;
   2392  1.7  christos }
   2393  1.7  christos 
   2394  1.8  christos /* Test if an address is one of the start addresses of a jump.  */
   2395  1.7  christos 
   2396  1.7  christos static bool
   2397  1.7  christos jump_info_is_start_address (const struct jump_info *ji, bfd_vma address)
   2398  1.7  christos {
   2399  1.7  christos   bool result = false;
   2400  1.8  christos   size_t i;
   2401  1.7  christos 
   2402  1.7  christos   for (i = ji->start.count; i-- > 0;)
   2403  1.7  christos     if (address == ji->start.addresses[i])
   2404  1.7  christos       {
   2405  1.7  christos 	result = true;
   2406  1.7  christos 	break;
   2407  1.7  christos       }
   2408  1.7  christos 
   2409  1.8  christos   return result;
   2410  1.7  christos }
   2411  1.7  christos 
   2412  1.7  christos /* Test if an address is the target address of a jump.  */
   2413  1.7  christos 
   2414  1.7  christos static bool
   2415  1.7  christos jump_info_is_end_address (const struct jump_info *ji, bfd_vma address)
   2416  1.7  christos {
   2417  1.7  christos   return (address == ji->end);
   2418  1.7  christos }
   2419  1.7  christos 
   2420  1.7  christos /* Get the difference between the smallest and largest address of a jump.  */
   2421  1.7  christos 
   2422  1.7  christos static bfd_vma
   2423  1.7  christos jump_info_size (const struct jump_info *ji)
   2424  1.7  christos {
   2425  1.7  christos   return jump_info_max_address (ji) - jump_info_min_address (ji);
   2426  1.7  christos }
   2427  1.7  christos 
   2428  1.7  christos /* Unlink a jump object from a list.  */
   2429  1.7  christos 
   2430  1.7  christos static void
   2431  1.7  christos jump_info_unlink (struct jump_info *node,
   2432  1.7  christos 		  struct jump_info **base)
   2433  1.7  christos {
   2434  1.7  christos   if (node->next)
   2435  1.7  christos     node->next->prev = node->prev;
   2436  1.7  christos   if (node->prev)
   2437  1.7  christos     node->prev->next = node->next;
   2438  1.7  christos   else
   2439  1.7  christos     *base = node->next;
   2440  1.7  christos   node->next = NULL;
   2441  1.7  christos   node->prev = NULL;
   2442  1.7  christos }
   2443  1.7  christos 
   2444  1.7  christos /* Insert unlinked jump info node into a list.  */
   2445  1.7  christos 
   2446  1.7  christos static void
   2447  1.7  christos jump_info_insert (struct jump_info *node,
   2448  1.7  christos 		  struct jump_info *target,
   2449  1.7  christos 		  struct jump_info **base)
   2450  1.7  christos {
   2451  1.7  christos   node->next = target;
   2452  1.7  christos   node->prev = target->prev;
   2453  1.7  christos   target->prev = node;
   2454  1.7  christos   if (node->prev)
   2455  1.7  christos     node->prev->next = node;
   2456  1.7  christos   else
   2457  1.7  christos     *base = node;
   2458  1.7  christos }
   2459  1.7  christos 
   2460  1.7  christos /* Add unlinked node to the front of a list.  */
   2461  1.7  christos 
   2462  1.7  christos static void
   2463  1.7  christos jump_info_add_front (struct jump_info *node,
   2464  1.7  christos 		     struct jump_info **base)
   2465  1.7  christos {
   2466  1.7  christos   node->next = *base;
   2467  1.7  christos   if (node->next)
   2468  1.7  christos     node->next->prev = node;
   2469  1.7  christos   node->prev = NULL;
   2470  1.7  christos   *base = node;
   2471  1.7  christos }
   2472  1.7  christos 
   2473  1.7  christos /* Move linked node to target position.  */
   2474  1.7  christos 
   2475  1.7  christos static void
   2476  1.7  christos jump_info_move_linked (struct jump_info *node,
   2477  1.7  christos 		       struct jump_info *target,
   2478  1.7  christos 		       struct jump_info **base)
   2479  1.7  christos {
   2480  1.7  christos   /* Unlink node.  */
   2481  1.7  christos   jump_info_unlink (node, base);
   2482  1.7  christos   /* Insert node at target position.  */
   2483  1.8  christos   jump_info_insert (node, target, base);
   2484  1.7  christos }
   2485  1.7  christos 
   2486  1.7  christos /* Test if two jumps intersect.  */
   2487  1.7  christos 
   2488  1.7  christos static bool
   2489  1.7  christos jump_info_intersect (const struct jump_info *a,
   2490  1.7  christos 		     const struct jump_info *b)
   2491  1.7  christos {
   2492  1.7  christos   return ((jump_info_max_address (a) >= jump_info_min_address (b))
   2493  1.7  christos 	  && (jump_info_min_address (a) <= jump_info_max_address (b)));
   2494  1.7  christos }
   2495  1.7  christos 
   2496  1.7  christos /* Merge two compatible jump info objects.  */
   2497  1.7  christos 
   2498  1.7  christos static void
   2499  1.7  christos jump_info_merge (struct jump_info **base)
   2500  1.7  christos {
   2501  1.7  christos   struct jump_info *a;
   2502  1.7  christos 
   2503  1.7  christos   for (a = *base; a; a = a->next)
   2504  1.7  christos     {
   2505  1.7  christos       struct jump_info *b;
   2506  1.7  christos 
   2507  1.7  christos       for (b = a->next; b; b = b->next)
   2508  1.7  christos 	{
   2509  1.7  christos 	  /* Merge both jumps into one.  */
   2510  1.7  christos 	  if (a->end == b->end)
   2511  1.7  christos 	    {
   2512  1.7  christos 	      /* Reallocate addresses.  */
   2513  1.7  christos 	      size_t needed_size = a->start.count + b->start.count;
   2514  1.7  christos 	      size_t i;
   2515  1.7  christos 
   2516  1.7  christos 	      if (needed_size > a->start.max_count)
   2517  1.7  christos 		{
   2518  1.7  christos 		  a->start.max_count += b->start.max_count;
   2519  1.7  christos 		  a->start.addresses =
   2520  1.7  christos 		    xrealloc (a->start.addresses,
   2521  1.7  christos 			      a->start.max_count * sizeof (bfd_vma *));
   2522  1.7  christos 		}
   2523  1.7  christos 
   2524  1.7  christos 	      /* Append start addresses.  */
   2525  1.7  christos 	      for (i = 0; i < b->start.count; ++i)
   2526  1.7  christos 		a->start.addresses[a->start.count++] =
   2527  1.7  christos 		  b->start.addresses[i];
   2528  1.7  christos 
   2529  1.7  christos 	      /* Remove and delete jump.  */
   2530  1.7  christos 	      struct jump_info *tmp = b->prev;
   2531  1.7  christos 	      jump_info_unlink (b, base);
   2532  1.7  christos 	      jump_info_free (b);
   2533  1.7  christos 	      b = tmp;
   2534  1.7  christos 	    }
   2535  1.7  christos 	}
   2536  1.7  christos     }
   2537  1.7  christos }
   2538  1.7  christos 
   2539  1.7  christos /* Sort jumps by their size and starting point using a stable
   2540  1.7  christos    minsort. This could be improved if sorting performance is
   2541  1.7  christos    an issue, for example by using mergesort.  */
   2542  1.7  christos 
   2543  1.7  christos static void
   2544  1.7  christos jump_info_sort (struct jump_info **base)
   2545  1.7  christos {
   2546  1.7  christos   struct jump_info *current_element = *base;
   2547  1.7  christos 
   2548  1.7  christos   while (current_element)
   2549  1.7  christos     {
   2550  1.7  christos       struct jump_info *best_match = current_element;
   2551  1.7  christos       struct jump_info *runner = current_element->next;
   2552  1.7  christos       bfd_vma best_size = jump_info_size (best_match);
   2553  1.7  christos 
   2554  1.7  christos       while (runner)
   2555  1.7  christos 	{
   2556  1.7  christos 	  bfd_vma runner_size = jump_info_size (runner);
   2557  1.7  christos 
   2558  1.7  christos 	  if ((runner_size < best_size)
   2559  1.7  christos 	      || ((runner_size == best_size)
   2560  1.7  christos 		  && (jump_info_min_address (runner)
   2561  1.7  christos 		      < jump_info_min_address (best_match))))
   2562  1.7  christos 	    {
   2563  1.7  christos 	      best_match = runner;
   2564  1.7  christos 	      best_size = runner_size;
   2565  1.7  christos 	    }
   2566  1.7  christos 
   2567  1.7  christos 	  runner = runner->next;
   2568  1.7  christos 	}
   2569  1.7  christos 
   2570  1.7  christos       if (best_match == current_element)
   2571  1.7  christos 	current_element = current_element->next;
   2572  1.7  christos       else
   2573  1.7  christos 	jump_info_move_linked (best_match, current_element, base);
   2574  1.7  christos     }
   2575  1.7  christos }
   2576  1.7  christos 
   2577  1.7  christos /* Visualize all jumps at a given address.  */
   2578  1.7  christos 
   2579  1.7  christos static void
   2580  1.7  christos jump_info_visualize_address (bfd_vma address,
   2581  1.7  christos 			     int max_level,
   2582  1.7  christos 			     char *line_buffer,
   2583  1.7  christos 			     uint8_t *color_buffer)
   2584  1.7  christos {
   2585  1.7  christos   struct jump_info *ji = detected_jumps;
   2586  1.7  christos   size_t len = (max_level + 1) * 3;
   2587  1.7  christos 
   2588  1.7  christos   /* Clear line buffer.  */
   2589  1.7  christos   memset (line_buffer, ' ', len);
   2590  1.7  christos   memset (color_buffer, 0, len);
   2591  1.7  christos 
   2592  1.7  christos   /* Iterate over jumps and add their ASCII art.  */
   2593  1.7  christos   while (ji)
   2594  1.7  christos     {
   2595  1.7  christos       /* Discard jumps that are never needed again.  */
   2596  1.7  christos       if (jump_info_max_address (ji) < address)
   2597  1.7  christos 	{
   2598  1.7  christos 	  struct jump_info *tmp = ji;
   2599  1.7  christos 
   2600  1.7  christos 	  ji = ji->next;
   2601  1.7  christos 	  jump_info_unlink (tmp, &detected_jumps);
   2602  1.7  christos 	  jump_info_free (tmp);
   2603  1.7  christos 	  continue;
   2604  1.7  christos 	}
   2605  1.7  christos 
   2606  1.7  christos       /* This jump intersects with the current address.  */
   2607  1.7  christos       if (jump_info_min_address (ji) <= address)
   2608  1.7  christos 	{
   2609  1.7  christos 	  /* Hash target address to get an even
   2610  1.7  christos 	     distribution between all values.  */
   2611  1.7  christos 	  bfd_vma hash_address = jump_info_end_address (ji);
   2612  1.7  christos 	  uint8_t color = iterative_hash_object (hash_address, 0);
   2613  1.7  christos 	  /* Fetch line offset.  */
   2614  1.7  christos 	  int offset = (max_level - ji->level) * 3;
   2615  1.7  christos 
   2616  1.7  christos 	  /* Draw start line.  */
   2617  1.7  christos 	  if (jump_info_is_start_address (ji, address))
   2618  1.7  christos 	    {
   2619  1.7  christos 	      size_t i = offset + 1;
   2620  1.7  christos 
   2621  1.7  christos 	      for (; i < len - 1; ++i)
   2622  1.7  christos 		if (line_buffer[i] == ' ')
   2623  1.7  christos 		  {
   2624  1.7  christos 		    line_buffer[i] = '-';
   2625  1.7  christos 		    color_buffer[i] = color;
   2626  1.7  christos 		  }
   2627  1.7  christos 
   2628  1.7  christos 	      if (line_buffer[i] == ' ')
   2629  1.7  christos 		{
   2630  1.7  christos 		  line_buffer[i] = '-';
   2631  1.7  christos 		  color_buffer[i] = color;
   2632  1.7  christos 		}
   2633  1.7  christos 	      else if (line_buffer[i] == '>')
   2634  1.7  christos 		{
   2635  1.7  christos 		  line_buffer[i] = 'X';
   2636  1.7  christos 		  color_buffer[i] = color;
   2637  1.7  christos 		}
   2638  1.7  christos 
   2639  1.7  christos 	      if (line_buffer[offset] == ' ')
   2640  1.7  christos 		{
   2641  1.7  christos 		  if (address <= ji->end)
   2642  1.7  christos 		    line_buffer[offset] =
   2643  1.7  christos 		      (jump_info_min_address (ji) == address) ? '/': '+';
   2644  1.7  christos 		  else
   2645  1.7  christos 		    line_buffer[offset] =
   2646  1.7  christos 		      (jump_info_max_address (ji) == address) ? '\\': '+';
   2647  1.7  christos 		  color_buffer[offset] = color;
   2648  1.7  christos 		}
   2649  1.7  christos 	    }
   2650  1.7  christos 	  /* Draw jump target.  */
   2651  1.7  christos 	  else if (jump_info_is_end_address (ji, address))
   2652  1.7  christos 	    {
   2653  1.7  christos 	      size_t i = offset + 1;
   2654  1.7  christos 
   2655  1.7  christos 	      for (; i < len - 1; ++i)
   2656  1.7  christos 		if (line_buffer[i] == ' ')
   2657  1.7  christos 		  {
   2658  1.7  christos 		    line_buffer[i] = '-';
   2659  1.7  christos 		    color_buffer[i] = color;
   2660  1.7  christos 		  }
   2661  1.7  christos 
   2662  1.7  christos 	      if (line_buffer[i] == ' ')
   2663  1.7  christos 		{
   2664  1.7  christos 		  line_buffer[i] = '>';
   2665  1.7  christos 		  color_buffer[i] = color;
   2666  1.7  christos 		}
   2667  1.7  christos 	      else if (line_buffer[i] == '-')
   2668  1.7  christos 		{
   2669  1.7  christos 		  line_buffer[i] = 'X';
   2670  1.7  christos 		  color_buffer[i] = color;
   2671  1.7  christos 		}
   2672  1.7  christos 
   2673  1.7  christos 	      if (line_buffer[offset] == ' ')
   2674  1.7  christos 		{
   2675  1.7  christos 		  if (jump_info_min_address (ji) < address)
   2676  1.7  christos 		    line_buffer[offset] =
   2677  1.7  christos 		      (jump_info_max_address (ji) > address) ? '>' : '\\';
   2678  1.7  christos 		  else
   2679  1.7  christos 		    line_buffer[offset] = '/';
   2680  1.7  christos 		  color_buffer[offset] = color;
   2681  1.7  christos 		}
   2682  1.7  christos 	    }
   2683  1.7  christos 	  /* Draw intermediate line segment.  */
   2684  1.7  christos 	  else if (line_buffer[offset] == ' ')
   2685  1.7  christos 	    {
   2686  1.7  christos 	      line_buffer[offset] = '|';
   2687  1.7  christos 	      color_buffer[offset] = color;
   2688  1.7  christos 	    }
   2689  1.7  christos 	}
   2690  1.7  christos 
   2691  1.7  christos       ji = ji->next;
   2692  1.7  christos     }
   2693  1.7  christos }
   2694  1.7  christos 
   2695  1.7  christos /* Clone of disassemble_bytes to detect jumps inside a function.  */
   2696  1.7  christos /* FIXME: is this correct? Can we strip it down even further?  */
   2697  1.7  christos 
   2698  1.7  christos static struct jump_info *
   2699  1.7  christos disassemble_jumps (struct disassemble_info * inf,
   2700  1.7  christos 		   disassembler_ftype        disassemble_fn,
   2701  1.7  christos 		   bfd_vma                   start_offset,
   2702  1.7  christos 		   bfd_vma                   stop_offset,
   2703  1.7  christos 		   bfd_vma		     rel_offset,
   2704  1.7  christos 		   arelent ***               relppp,
   2705  1.7  christos 		   arelent **                relppend)
   2706  1.7  christos {
   2707  1.7  christos   struct objdump_disasm_info *aux;
   2708  1.7  christos   struct jump_info *jumps = NULL;
   2709  1.7  christos   asection *section;
   2710  1.7  christos   bfd_vma addr_offset;
   2711  1.7  christos   unsigned int opb = inf->octets_per_byte;
   2712  1.7  christos   int octets = opb;
   2713  1.7  christos   SFILE sfile;
   2714  1.7  christos 
   2715  1.7  christos   aux = (struct objdump_disasm_info *) inf->application_data;
   2716  1.7  christos   section = inf->section;
   2717  1.7  christos 
   2718  1.8  christos   sfile.alloc = 120;
   2719  1.8  christos   sfile.buffer = (char *) xmalloc (sfile.alloc);
   2720  1.7  christos   sfile.pos = 0;
   2721  1.7  christos 
   2722  1.7  christos   inf->insn_info_valid = 0;
   2723  1.7  christos   disassemble_set_printf (inf, &sfile, (fprintf_ftype) objdump_sprintf,
   2724  1.7  christos 			  (fprintf_styled_ftype) objdump_styled_sprintf);
   2725  1.7  christos 
   2726  1.7  christos   addr_offset = start_offset;
   2727  1.7  christos   while (addr_offset < stop_offset)
   2728  1.7  christos     {
   2729  1.7  christos       int previous_octets;
   2730  1.7  christos 
   2731  1.7  christos       /* Remember the length of the previous instruction.  */
   2732  1.7  christos       previous_octets = octets;
   2733  1.7  christos       octets = 0;
   2734  1.8  christos 
   2735  1.7  christos       sfile.pos = 0;
   2736  1.7  christos       inf->bytes_per_line = 0;
   2737  1.7  christos       inf->bytes_per_chunk = 0;
   2738  1.7  christos       inf->flags = ((disassemble_all ? DISASSEMBLE_DATA : 0)
   2739  1.7  christos 		    | (wide_output ? WIDE_OUTPUT : 0));
   2740  1.7  christos       if (machine)
   2741  1.7  christos 	inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
   2742  1.7  christos 
   2743  1.7  christos       if (inf->disassembler_needs_relocs
   2744  1.7  christos 	  && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
   2745  1.7  christos 	  && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
   2746  1.7  christos 	  && *relppp < relppend)
   2747  1.7  christos 	{
   2748  1.7  christos 	  bfd_signed_vma distance_to_rel;
   2749  1.7  christos 
   2750  1.7  christos 	  distance_to_rel = (**relppp)->address - (rel_offset + addr_offset);
   2751  1.7  christos 
   2752  1.7  christos 	  /* Check to see if the current reloc is associated with
   2753  1.7  christos 	     the instruction that we are about to disassemble.  */
   2754  1.7  christos 	  if (distance_to_rel == 0
   2755  1.7  christos 	      /* FIXME: This is wrong.  We are trying to catch
   2756  1.7  christos 		 relocs that are addressed part way through the
   2757  1.7  christos 		 current instruction, as might happen with a packed
   2758  1.7  christos 		 VLIW instruction.  Unfortunately we do not know the
   2759  1.7  christos 		 length of the current instruction since we have not
   2760  1.7  christos 		 disassembled it yet.  Instead we take a guess based
   2761  1.7  christos 		 upon the length of the previous instruction.  The
   2762  1.7  christos 		 proper solution is to have a new target-specific
   2763  1.7  christos 		 disassembler function which just returns the length
   2764  1.7  christos 		 of an instruction at a given address without trying
   2765  1.7  christos 		 to display its disassembly. */
   2766  1.7  christos 	      || (distance_to_rel > 0
   2767  1.7  christos 		&& distance_to_rel < (bfd_signed_vma) (previous_octets/ opb)))
   2768  1.7  christos 	    {
   2769  1.7  christos 	      inf->flags |= INSN_HAS_RELOC;
   2770  1.7  christos 	    }
   2771  1.7  christos 	}
   2772  1.7  christos 
   2773  1.7  christos       if (! disassemble_all
   2774  1.7  christos 	  && (section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
   2775  1.7  christos 	  == (SEC_CODE | SEC_HAS_CONTENTS))
   2776  1.7  christos 	/* Set a stop_vma so that the disassembler will not read
   2777  1.7  christos 	   beyond the next symbol.  We assume that symbols appear on
   2778  1.7  christos 	   the boundaries between instructions.  We only do this when
   2779  1.7  christos 	   disassembling code of course, and when -D is in effect.  */
   2780  1.7  christos 	inf->stop_vma = section->vma + stop_offset;
   2781  1.8  christos 
   2782  1.7  christos       inf->stop_offset = stop_offset;
   2783  1.7  christos 
   2784  1.7  christos       /* Extract jump information.  */
   2785  1.7  christos       inf->insn_info_valid = 0;
   2786  1.7  christos       disassembler_in_comment = false;
   2787  1.7  christos       octets = (*disassemble_fn) (section->vma + addr_offset, inf);
   2788  1.7  christos       /* Test if a jump was detected.  */
   2789  1.7  christos       if (inf->insn_info_valid
   2790  1.7  christos 	  && ((inf->insn_type == dis_branch)
   2791  1.7  christos 	      || (inf->insn_type == dis_condbranch)
   2792  1.7  christos 	      || (inf->insn_type == dis_jsr)
   2793  1.7  christos 	      || (inf->insn_type == dis_condjsr))
   2794  1.7  christos 	  && (inf->target >= section->vma + start_offset)
   2795  1.7  christos 	  && (inf->target < section->vma + stop_offset))
   2796  1.7  christos 	{
   2797  1.7  christos 	  struct jump_info *ji =
   2798  1.7  christos 	    jump_info_new (section->vma + addr_offset, inf->target, -1);
   2799  1.7  christos 	  jump_info_add_front (ji, &jumps);
   2800  1.7  christos 	}
   2801  1.7  christos 
   2802  1.8  christos       inf->stop_vma = 0;
   2803  1.8  christos 
   2804  1.7  christos       addr_offset += octets / opb;
   2805  1.7  christos     }
   2806  1.7  christos 
   2807  1.7  christos   disassemble_set_printf (inf, (void *) stdout, (fprintf_ftype) fprintf,
   2808  1.7  christos 			  (fprintf_styled_ftype) fprintf_styled);
   2809  1.7  christos   free (sfile.buffer);
   2810  1.7  christos 
   2811  1.7  christos   /* Merge jumps.  */
   2812  1.7  christos   jump_info_merge (&jumps);
   2813  1.7  christos   /* Process jumps.  */
   2814  1.7  christos   jump_info_sort (&jumps);
   2815  1.7  christos 
   2816  1.7  christos   /* Group jumps by level.  */
   2817  1.7  christos   struct jump_info *last_jump = jumps;
   2818  1.7  christos   int max_level = -1;
   2819  1.7  christos 
   2820  1.7  christos   while (last_jump)
   2821  1.7  christos     {
   2822  1.7  christos       /* The last jump is part of the next group.  */
   2823  1.7  christos       struct jump_info *base = last_jump;
   2824  1.7  christos       /* Increment level.  */
   2825  1.7  christos       base->level = ++max_level;
   2826  1.7  christos 
   2827  1.7  christos       /* Find jumps that can be combined on the same
   2828  1.7  christos 	 level, with the largest jumps tested first.
   2829  1.7  christos 	 This has the advantage that large jumps are on
   2830  1.7  christos 	 lower levels and do not intersect with small
   2831  1.7  christos 	 jumps that get grouped on higher levels.  */
   2832  1.7  christos       struct jump_info *exchange_item = last_jump->next;
   2833  1.7  christos       struct jump_info *it = exchange_item;
   2834  1.8  christos 
   2835  1.7  christos       for (; it; it = it->next)
   2836  1.7  christos 	{
   2837  1.7  christos 	  /* Test if the jump intersects with any
   2838  1.7  christos 	     jump from current group.  */
   2839  1.7  christos 	  bool ok = true;
   2840  1.7  christos 	  struct jump_info *it_collision;
   2841  1.7  christos 
   2842  1.7  christos 	  for (it_collision = base;
   2843  1.7  christos 	       it_collision != exchange_item;
   2844  1.8  christos 	       it_collision = it_collision->next)
   2845  1.7  christos 	    {
   2846  1.7  christos 	      /* This jump intersects so we leave it out.  */
   2847  1.7  christos 	      if (jump_info_intersect (it_collision, it))
   2848  1.7  christos 		{
   2849  1.7  christos 		  ok = false;
   2850  1.7  christos 		  break;
   2851  1.7  christos 		}
   2852  1.7  christos 	    }
   2853  1.7  christos 
   2854  1.7  christos 	  /* Add jump to group.  */
   2855  1.7  christos 	  if (ok)
   2856  1.7  christos 	    {
   2857  1.7  christos 	      /* Move current element to the front.  */
   2858  1.7  christos 	      if (it != exchange_item)
   2859  1.7  christos 		{
   2860  1.7  christos 		  struct jump_info *save = it->prev;
   2861  1.7  christos 		  jump_info_move_linked (it, exchange_item, &jumps);
   2862  1.7  christos 		  last_jump = it;
   2863  1.7  christos 		  it = save;
   2864  1.7  christos 		}
   2865  1.7  christos 	      else
   2866  1.7  christos 		{
   2867  1.7  christos 		  last_jump = exchange_item;
   2868  1.3  christos 		  exchange_item = exchange_item->next;
   2869  1.7  christos 		}
   2870  1.7  christos 	      last_jump->level = max_level;
   2871  1.1  christos 	    }
   2872  1.3  christos 	}
   2873  1.7  christos 
   2874  1.1  christos       /* Move to next group.  */
   2875  1.1  christos       last_jump = exchange_item;
   2876  1.1  christos     }
   2877  1.1  christos 
   2878  1.1  christos   return jumps;
   2879  1.1  christos }
   2880  1.1  christos 
   2881  1.1  christos /* The number of zeroes we want to see before we start skipping them.
   2882  1.1  christos    The number is arbitrarily chosen.  */
   2883  1.1  christos 
   2884  1.1  christos #define DEFAULT_SKIP_ZEROES 8
   2885  1.1  christos 
   2886  1.1  christos /* The number of zeroes to skip at the end of a section.  If the
   2887  1.1  christos    number of zeroes at the end is between SKIP_ZEROES_AT_END and
   2888  1.1  christos    SKIP_ZEROES, they will be disassembled.  If there are fewer than
   2889  1.1  christos    SKIP_ZEROES_AT_END, they will be skipped.  This is a heuristic
   2890  1.7  christos    attempt to avoid disassembling zeroes inserted by section
   2891  1.7  christos    alignment.  */
   2892  1.7  christos 
   2893  1.7  christos #define DEFAULT_SKIP_ZEROES_AT_END 3
   2894  1.7  christos 
   2895  1.7  christos static int
   2896  1.8  christos null_print (const void * stream ATTRIBUTE_UNUSED, const char * format ATTRIBUTE_UNUSED, ...)
   2897  1.8  christos {
   2898  1.8  christos   return 1;
   2899  1.8  christos }
   2900  1.8  christos 
   2901  1.8  christos /* Like null_print, but takes the extra STYLE argument.  As this is not
   2902  1.8  christos    going to print anything, the extra argument is just ignored.  */
   2903  1.8  christos 
   2904  1.8  christos static int
   2905  1.8  christos null_styled_print (const void * stream ATTRIBUTE_UNUSED,
   2906  1.8  christos 		   enum disassembler_style style ATTRIBUTE_UNUSED,
   2907  1.8  christos 		   const char * format ATTRIBUTE_UNUSED, ...)
   2908  1.8  christos {
   2909  1.8  christos   return 1;
   2910  1.8  christos }
   2911  1.8  christos 
   2912  1.8  christos /* Print out jump visualization.  */
   2913  1.8  christos 
   2914  1.8  christos static void
   2915  1.8  christos print_jump_visualisation (bfd_vma addr, int max_level, char *line_buffer,
   2916  1.8  christos 			  uint8_t *color_buffer)
   2917  1.8  christos {
   2918  1.8  christos   if (!line_buffer)
   2919  1.8  christos     return;
   2920  1.8  christos 
   2921  1.8  christos   jump_info_visualize_address (addr, max_level, line_buffer, color_buffer);
   2922  1.8  christos 
   2923  1.8  christos   size_t line_buffer_size = strlen (line_buffer);
   2924  1.8  christos   char last_color = 0;
   2925  1.8  christos   size_t i;
   2926  1.8  christos 
   2927  1.8  christos   for (i = 0; i <= line_buffer_size; ++i)
   2928  1.8  christos     {
   2929  1.8  christos       if (color_output)
   2930  1.8  christos 	{
   2931  1.8  christos 	  uint8_t color = (i < line_buffer_size) ? color_buffer[i]: 0;
   2932  1.8  christos 
   2933  1.8  christos 	  if (color != last_color)
   2934  1.8  christos 	    {
   2935  1.8  christos 	      if (color)
   2936  1.8  christos 		if (extended_color_output)
   2937  1.8  christos 		  /* Use extended 8bit color, but
   2938  1.8  christos 		     do not choose dark colors.  */
   2939  1.8  christos 		  printf ("\033[38;5;%dm", 124 + (color % 108));
   2940  1.8  christos 		else
   2941  1.8  christos 		  /* Use simple terminal colors.  */
   2942  1.8  christos 		  printf ("\033[%dm", 31 + (color % 7));
   2943  1.8  christos 	      else
   2944  1.8  christos 		/* Clear color.  */
   2945  1.8  christos 		printf ("\033[0m");
   2946  1.8  christos 	      last_color = color;
   2947  1.8  christos 	    }
   2948  1.1  christos 	}
   2949  1.1  christos       putchar ((i < line_buffer_size) ? line_buffer[i]: ' ');
   2950  1.1  christos     }
   2951  1.8  christos }
   2952  1.8  christos 
   2953  1.8  christos /* Disassemble some data in memory between given values.  */
   2954  1.8  christos 
   2955  1.8  christos static void
   2956  1.8  christos disassemble_bytes (struct disassemble_info *inf,
   2957  1.8  christos 		   disassembler_ftype disassemble_fn,
   2958  1.8  christos 		   bool insns,
   2959  1.8  christos 		   bfd_byte *data,
   2960  1.1  christos 		   bfd_vma start_offset,
   2961  1.1  christos 		   bfd_vma stop_offset,
   2962  1.1  christos 		   bfd_vma rel_offset,
   2963  1.8  christos 		   arelent ***relppp,
   2964  1.8  christos 		   arelent **relppend)
   2965  1.1  christos {
   2966  1.1  christos   struct objdump_disasm_info *aux;
   2967  1.1  christos   asection *section;
   2968  1.1  christos   unsigned int octets_per_line;
   2969  1.8  christos   unsigned int skip_addr_chars;
   2970  1.1  christos   bfd_vma addr_offset;
   2971  1.1  christos   unsigned int opb = inf->octets_per_byte;
   2972  1.1  christos   unsigned int skip_zeroes = inf->skip_zeroes;
   2973  1.7  christos   unsigned int skip_zeroes_at_end = inf->skip_zeroes_at_end;
   2974  1.1  christos   size_t octets;
   2975  1.1  christos   SFILE sfile;
   2976  1.1  christos 
   2977  1.1  christos   aux = (struct objdump_disasm_info *) inf->application_data;
   2978  1.3  christos   section = inf->section;
   2979  1.1  christos 
   2980  1.1  christos   sfile.alloc = 120;
   2981  1.1  christos   sfile.buffer = (char *) xmalloc (sfile.alloc);
   2982  1.1  christos   sfile.pos = 0;
   2983  1.1  christos 
   2984  1.1  christos   if (insn_width)
   2985  1.1  christos     octets_per_line = insn_width;
   2986  1.1  christos   else if (insns)
   2987  1.1  christos     octets_per_line = 4;
   2988  1.1  christos   else
   2989  1.1  christos     octets_per_line = 16;
   2990  1.1  christos 
   2991  1.8  christos   /* Figure out how many characters to skip at the start of an
   2992  1.1  christos      address, to make the disassembly look nicer.  We discard leading
   2993  1.1  christos      zeroes in chunks of 4, ensuring that there is always a leading
   2994  1.1  christos      zero remaining.  */
   2995  1.1  christos   skip_addr_chars = 0;
   2996  1.1  christos   if (!no_addresses && !prefix_addresses)
   2997  1.1  christos     {
   2998  1.1  christos       char buf[30];
   2999  1.1  christos 
   3000  1.1  christos       bfd_sprintf_vma (aux->abfd, buf, section->vma + section->size / opb);
   3001  1.1  christos 
   3002  1.1  christos       while (buf[skip_addr_chars] == '0')
   3003  1.1  christos 	++skip_addr_chars;
   3004  1.1  christos 
   3005  1.1  christos       /* Don't discard zeros on overflow.  */
   3006  1.1  christos       if (buf[skip_addr_chars] == '\0' && section->vma != 0)
   3007  1.1  christos 	skip_addr_chars = 0;
   3008  1.1  christos 
   3009  1.1  christos       if (skip_addr_chars != 0)
   3010  1.7  christos 	skip_addr_chars = (skip_addr_chars - 1) & -4;
   3011  1.7  christos     }
   3012  1.7  christos 
   3013  1.7  christos   inf->insn_info_valid = 0;
   3014  1.7  christos 
   3015  1.7  christos   /* Determine maximum level. */
   3016  1.7  christos   uint8_t *color_buffer = NULL;
   3017  1.7  christos   char *line_buffer = NULL;
   3018  1.7  christos   int max_level = -1;
   3019  1.7  christos 
   3020  1.7  christos   /* Some jumps were detected.  */
   3021  1.7  christos   if (detected_jumps)
   3022  1.7  christos     {
   3023  1.7  christos       struct jump_info *ji;
   3024  1.7  christos 
   3025  1.7  christos       /* Find maximum jump level.  */
   3026  1.7  christos       for (ji = detected_jumps; ji; ji = ji->next)
   3027  1.7  christos 	{
   3028  1.7  christos 	  if (ji->level > max_level)
   3029  1.7  christos 	    max_level = ji->level;
   3030  1.7  christos 	}
   3031  1.7  christos 
   3032  1.7  christos       /* Allocate buffers.  */
   3033  1.7  christos       size_t len = (max_level + 1) * 3 + 1;
   3034  1.7  christos       line_buffer = xmalloc (len);
   3035  1.1  christos       line_buffer[len - 1] = 0;
   3036  1.1  christos       color_buffer = xmalloc (len);
   3037  1.1  christos       color_buffer[len - 1] = 0;
   3038  1.8  christos     }
   3039  1.1  christos 
   3040  1.1  christos   addr_offset = start_offset;
   3041  1.1  christos   while (addr_offset < stop_offset)
   3042  1.1  christos     {
   3043  1.1  christos       bool need_nl = false;
   3044  1.1  christos 
   3045  1.1  christos       octets = 0;
   3046  1.1  christos 
   3047  1.8  christos       /* Make sure we don't use relocs from previous instructions.  */
   3048  1.8  christos       aux->reloc = NULL;
   3049  1.8  christos 
   3050  1.8  christos       /* If we see more than SKIP_ZEROES octets of zeroes, we just
   3051  1.1  christos 	 print `...'.  */
   3052  1.1  christos       if (! disassemble_zeroes)
   3053  1.1  christos 	for (; addr_offset * opb + octets < stop_offset * opb; octets++)
   3054  1.8  christos 	  if (data[addr_offset * opb + octets] != 0)
   3055  1.8  christos 	    break;
   3056  1.8  christos       if (! disassemble_zeroes
   3057  1.1  christos 	  && (inf->insn_info_valid == 0
   3058  1.1  christos 	      || inf->branch_delay_insns == 0)
   3059  1.1  christos 	  && (octets >= skip_zeroes
   3060  1.1  christos 	      || (addr_offset * opb + octets == stop_offset * opb
   3061  1.1  christos 		  && octets < skip_zeroes_at_end)))
   3062  1.8  christos 	{
   3063  1.8  christos 	  /* If there are more nonzero octets to follow, we only skip
   3064  1.1  christos 	     zeroes in multiples of 4, to try to avoid running over
   3065  1.1  christos 	     the start of an instruction which happens to start with
   3066  1.1  christos 	     zero.  */
   3067  1.1  christos 	  if (addr_offset * opb + octets != stop_offset * opb)
   3068  1.8  christos 	    octets &= ~3;
   3069  1.8  christos 
   3070  1.8  christos 	  /* If we are going to display more data, and we are displaying
   3071  1.8  christos 	     file offsets, then tell the user how many zeroes we skip
   3072  1.8  christos 	     and the file offset from where we resume dumping.  */
   3073  1.1  christos 	  if (display_file_offsets
   3074  1.8  christos 	      && addr_offset + octets / opb < stop_offset)
   3075  1.1  christos 	    printf (_("\t... (skipping %lu zeroes, "
   3076  1.1  christos 		      "resuming at file offset: 0x%lx)\n"),
   3077  1.1  christos 		    (unsigned long) (octets / opb),
   3078  1.1  christos 		    (unsigned long) (section->filepos
   3079  1.1  christos 				     + addr_offset + octets / opb));
   3080  1.1  christos 	  else
   3081  1.8  christos 	    printf ("\t...\n");
   3082  1.8  christos 	}
   3083  1.1  christos       else
   3084  1.1  christos 	{
   3085  1.1  christos 	  char buf[50];
   3086  1.1  christos 	  unsigned int bpc = 0;
   3087  1.8  christos 	  unsigned int pb = 0;
   3088  1.8  christos 
   3089  1.8  christos 	  if (with_line_numbers || with_source_code)
   3090  1.1  christos 	    show_line (aux->abfd, section, addr_offset);
   3091  1.1  christos 
   3092  1.1  christos 	  if (no_addresses)
   3093  1.1  christos 	    printf ("\t");
   3094  1.1  christos 	  else if (!prefix_addresses)
   3095  1.1  christos 	    {
   3096  1.1  christos 	      char *s;
   3097  1.1  christos 
   3098  1.1  christos 	      bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
   3099  1.1  christos 	      for (s = buf + skip_addr_chars; *s == '0'; s++)
   3100  1.1  christos 		*s = ' ';
   3101  1.1  christos 	      if (*s == '\0')
   3102  1.8  christos 		*--s = '0';
   3103  1.1  christos 	      printf ("%s:\t", buf + skip_addr_chars);
   3104  1.8  christos 	    }
   3105  1.1  christos 	  else
   3106  1.1  christos 	    {
   3107  1.1  christos 	      aux->require_sec = true;
   3108  1.8  christos 	      objdump_print_address (section->vma + addr_offset, inf);
   3109  1.8  christos 	      aux->require_sec = false;
   3110  1.8  christos 	      putchar (' ');
   3111  1.7  christos 	    }
   3112  1.1  christos 
   3113  1.1  christos 	  print_jump_visualisation (section->vma + addr_offset,
   3114  1.8  christos 				    max_level, line_buffer,
   3115  1.8  christos 				    color_buffer);
   3116  1.1  christos 
   3117  1.8  christos 	  if (insns)
   3118  1.8  christos 	    {
   3119  1.8  christos 	      int insn_size;
   3120  1.1  christos 
   3121  1.1  christos 	      sfile.pos = 0;
   3122  1.7  christos 	      disassemble_set_printf
   3123  1.7  christos 		(inf, &sfile, (fprintf_ftype) objdump_sprintf,
   3124  1.1  christos 		 (fprintf_styled_ftype) objdump_styled_sprintf);
   3125  1.1  christos 	      inf->bytes_per_line = 0;
   3126  1.1  christos 	      inf->bytes_per_chunk = 0;
   3127  1.1  christos 	      inf->flags = ((disassemble_all ? DISASSEMBLE_DATA : 0)
   3128  1.1  christos 			    | (wide_output ? WIDE_OUTPUT : 0));
   3129  1.1  christos 	      if (machine)
   3130  1.1  christos 		inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
   3131  1.1  christos 
   3132  1.1  christos 	      if (inf->disassembler_needs_relocs
   3133  1.7  christos 		  && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
   3134  1.7  christos 		  && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
   3135  1.7  christos 		  && *relppp < relppend)
   3136  1.7  christos 		{
   3137  1.7  christos 		  bfd_signed_vma distance_to_rel;
   3138  1.7  christos 		  int max_reloc_offset
   3139  1.8  christos 		    = aux->abfd->arch_info->max_reloc_offset_into_insn;
   3140  1.7  christos 
   3141  1.7  christos 		  distance_to_rel = ((**relppp)->address - rel_offset
   3142  1.7  christos 				     - addr_offset);
   3143  1.7  christos 
   3144  1.7  christos 		  insn_size = 0;
   3145  1.7  christos 		  if (distance_to_rel > 0
   3146  1.7  christos 		      && (max_reloc_offset < 0
   3147  1.7  christos 			  || distance_to_rel <= max_reloc_offset))
   3148  1.7  christos 		    {
   3149  1.7  christos 		      /* This reloc *might* apply to the current insn,
   3150  1.7  christos 			 starting somewhere inside it.  Discover the length
   3151  1.7  christos 			 of the current insn so that the check below will
   3152  1.7  christos 			 work.  */
   3153  1.7  christos 		      if (insn_width)
   3154  1.7  christos 			insn_size = insn_width;
   3155  1.7  christos 		      else
   3156  1.7  christos 			{
   3157  1.7  christos 			  /* We find the length by calling the dissassembler
   3158  1.7  christos 			     function with a dummy print handler.  This should
   3159  1.7  christos 			     work unless the disassembler is not expecting to
   3160  1.7  christos 			     be called multiple times for the same address.
   3161  1.8  christos 
   3162  1.8  christos 			     This does mean disassembling the instruction
   3163  1.8  christos 			     twice, but we only do this when there is a high
   3164  1.7  christos 			     probability that there is a reloc that will
   3165  1.7  christos 			     affect the instruction.  */
   3166  1.8  christos 			  disassemble_set_printf
   3167  1.8  christos 			    (inf, inf->stream, (fprintf_ftype) null_print,
   3168  1.8  christos 			     (fprintf_styled_ftype) null_styled_print);
   3169  1.8  christos 			  insn_size = disassemble_fn (section->vma
   3170  1.7  christos 						      + addr_offset, inf);
   3171  1.7  christos 			  disassemble_set_printf
   3172  1.1  christos 			    (inf, inf->stream,
   3173  1.1  christos 			     (fprintf_ftype) objdump_sprintf,
   3174  1.1  christos 			     (fprintf_styled_ftype) objdump_styled_sprintf);
   3175  1.1  christos 			}
   3176  1.1  christos 		    }
   3177  1.7  christos 
   3178  1.1  christos 		  /* Check to see if the current reloc is associated with
   3179  1.1  christos 		     the instruction that we are about to disassemble.  */
   3180  1.1  christos 		  if (distance_to_rel == 0
   3181  1.1  christos 		      || (distance_to_rel > 0
   3182  1.1  christos 			  && distance_to_rel < insn_size / (int) opb))
   3183  1.1  christos 		    {
   3184  1.3  christos 		      inf->flags |= INSN_HAS_RELOC;
   3185  1.8  christos 		      aux->reloc = **relppp;
   3186  1.8  christos 		    }
   3187  1.3  christos 		}
   3188  1.3  christos 
   3189  1.3  christos 	      if (! disassemble_all
   3190  1.3  christos 		  && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
   3191  1.3  christos 		      == (SEC_CODE | SEC_HAS_CONTENTS)))
   3192  1.3  christos 		/* Set a stop_vma so that the disassembler will not read
   3193  1.7  christos 		   beyond the next symbol.  We assume that symbols appear on
   3194  1.8  christos 		   the boundaries between instructions.  We only do this when
   3195  1.8  christos 		   disassembling code of course, and when -D is in effect.  */
   3196  1.8  christos 		inf->stop_vma = section->vma + stop_offset;
   3197  1.3  christos 
   3198  1.3  christos 	      inf->stop_offset = stop_offset;
   3199  1.8  christos 	      disassembler_in_comment = false;
   3200  1.8  christos 	      insn_size = (*disassemble_fn) (section->vma + addr_offset, inf);
   3201  1.1  christos 	      octets = insn_size;
   3202  1.1  christos 
   3203  1.8  christos 	      inf->stop_vma = 0;
   3204  1.1  christos 	      disassemble_set_printf (inf, stdout, (fprintf_ftype) fprintf,
   3205  1.1  christos 				      (fprintf_styled_ftype) fprintf_styled);
   3206  1.1  christos 	      if (insn_width == 0 && inf->bytes_per_line != 0)
   3207  1.8  christos 		octets_per_line = inf->bytes_per_line;
   3208  1.1  christos 	      if (insn_size < (int) opb)
   3209  1.1  christos 		{
   3210  1.8  christos 		  if (sfile.pos)
   3211  1.1  christos 		    printf ("%s\n", sfile.buffer);
   3212  1.1  christos 		  if (insn_size >= 0)
   3213  1.1  christos 		    {
   3214  1.1  christos 		      non_fatal (_("disassemble_fn returned length %d"),
   3215  1.1  christos 				 insn_size);
   3216  1.1  christos 		      exit_status = 1;
   3217  1.1  christos 		    }
   3218  1.1  christos 		  break;
   3219  1.1  christos 		}
   3220  1.1  christos 	    }
   3221  1.1  christos 	  else
   3222  1.1  christos 	    {
   3223  1.1  christos 	      bfd_vma j;
   3224  1.1  christos 
   3225  1.1  christos 	      octets = octets_per_line;
   3226  1.1  christos 	      if (addr_offset + octets / opb > stop_offset)
   3227  1.1  christos 		octets = (stop_offset - addr_offset) * opb;
   3228  1.1  christos 
   3229  1.1  christos 	      for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
   3230  1.1  christos 		{
   3231  1.1  christos 		  if (ISPRINT (data[j]))
   3232  1.1  christos 		    buf[j - addr_offset * opb] = data[j];
   3233  1.1  christos 		  else
   3234  1.1  christos 		    buf[j - addr_offset * opb] = '.';
   3235  1.1  christos 		}
   3236  1.1  christos 	      buf[j - addr_offset * opb] = '\0';
   3237  1.1  christos 	    }
   3238  1.1  christos 
   3239  1.1  christos 	  if (prefix_addresses
   3240  1.1  christos 	      ? show_raw_insn > 0
   3241  1.1  christos 	      : show_raw_insn >= 0)
   3242  1.1  christos 	    {
   3243  1.1  christos 	      bfd_vma j;
   3244  1.1  christos 
   3245  1.1  christos 	      /* If ! prefix_addresses and ! wide_output, we print
   3246  1.1  christos 		 octets_per_line octets per line.  */
   3247  1.1  christos 	      pb = octets;
   3248  1.1  christos 	      if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
   3249  1.1  christos 		pb = octets_per_line;
   3250  1.1  christos 
   3251  1.1  christos 	      if (inf->bytes_per_chunk)
   3252  1.1  christos 		bpc = inf->bytes_per_chunk;
   3253  1.6  christos 	      else
   3254  1.6  christos 		bpc = 1;
   3255  1.6  christos 
   3256  1.8  christos 	      for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
   3257  1.1  christos 		{
   3258  1.6  christos 		  /* PR 21580: Check for a buffer ending early.  */
   3259  1.6  christos 		  if (j + bpc <= stop_offset * opb)
   3260  1.8  christos 		    {
   3261  1.6  christos 		      unsigned int k;
   3262  1.6  christos 
   3263  1.6  christos 		      if (inf->display_endian == BFD_ENDIAN_LITTLE)
   3264  1.6  christos 			{
   3265  1.6  christos 			  for (k = bpc; k-- != 0; )
   3266  1.6  christos 			    printf ("%02x", (unsigned) data[j + k]);
   3267  1.6  christos 			}
   3268  1.1  christos 		      else
   3269  1.6  christos 			{
   3270  1.1  christos 			  for (k = 0; k < bpc; k++)
   3271  1.1  christos 			    printf ("%02x", (unsigned) data[j + k]);
   3272  1.1  christos 			}
   3273  1.1  christos 		    }
   3274  1.8  christos 		  putchar (' ');
   3275  1.1  christos 		}
   3276  1.1  christos 
   3277  1.1  christos 	      for (; pb < octets_per_line; pb += bpc)
   3278  1.1  christos 		{
   3279  1.1  christos 		  unsigned int k;
   3280  1.1  christos 
   3281  1.1  christos 		  for (k = 0; k < bpc; k++)
   3282  1.1  christos 		    printf ("  ");
   3283  1.1  christos 		  putchar (' ');
   3284  1.1  christos 		}
   3285  1.1  christos 
   3286  1.1  christos 	      /* Separate raw data from instruction by extra space.  */
   3287  1.1  christos 	      if (insns)
   3288  1.1  christos 		putchar ('\t');
   3289  1.1  christos 	      else
   3290  1.1  christos 		printf ("    ");
   3291  1.1  christos 	    }
   3292  1.1  christos 
   3293  1.1  christos 	  if (! insns)
   3294  1.1  christos 	    printf ("%s", buf);
   3295  1.1  christos 	  else if (sfile.pos)
   3296  1.1  christos 	    printf ("%s", sfile.buffer);
   3297  1.1  christos 
   3298  1.1  christos 	  if (prefix_addresses
   3299  1.1  christos 	      ? show_raw_insn > 0
   3300  1.1  christos 	      : show_raw_insn >= 0)
   3301  1.1  christos 	    {
   3302  1.1  christos 	      while (pb < octets)
   3303  1.1  christos 		{
   3304  1.1  christos 		  bfd_vma j;
   3305  1.8  christos 		  char *s;
   3306  1.8  christos 
   3307  1.8  christos 		  putchar ('\n');
   3308  1.8  christos 		  j = addr_offset * opb + pb;
   3309  1.8  christos 
   3310  1.8  christos 		  if (no_addresses)
   3311  1.8  christos 		    printf ("\t");
   3312  1.8  christos 		  else
   3313  1.8  christos 		    {
   3314  1.8  christos 		      bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
   3315  1.8  christos 		      for (s = buf + skip_addr_chars; *s == '0'; s++)
   3316  1.8  christos 			*s = ' ';
   3317  1.8  christos 		      if (*s == '\0')
   3318  1.8  christos 			*--s = '0';
   3319  1.8  christos 		      printf ("%s:\t", buf + skip_addr_chars);
   3320  1.1  christos 		    }
   3321  1.1  christos 
   3322  1.1  christos 		  print_jump_visualisation (section->vma + j / opb,
   3323  1.1  christos 					    max_level, line_buffer,
   3324  1.1  christos 					    color_buffer);
   3325  1.1  christos 
   3326  1.6  christos 		  pb += octets_per_line;
   3327  1.6  christos 		  if (pb > octets)
   3328  1.6  christos 		    pb = octets;
   3329  1.8  christos 		  for (; j < addr_offset * opb + pb; j += bpc)
   3330  1.1  christos 		    {
   3331  1.6  christos 		      /* PR 21619: Check for a buffer ending early.  */
   3332  1.6  christos 		      if (j + bpc <= stop_offset * opb)
   3333  1.8  christos 			{
   3334  1.6  christos 			  unsigned int k;
   3335  1.6  christos 
   3336  1.6  christos 			  if (inf->display_endian == BFD_ENDIAN_LITTLE)
   3337  1.6  christos 			    {
   3338  1.6  christos 			      for (k = bpc; k-- != 0; )
   3339  1.6  christos 				printf ("%02x", (unsigned) data[j + k]);
   3340  1.6  christos 			    }
   3341  1.1  christos 			  else
   3342  1.6  christos 			    {
   3343  1.1  christos 			      for (k = 0; k < bpc; k++)
   3344  1.1  christos 				printf ("%02x", (unsigned) data[j + k]);
   3345  1.1  christos 			    }
   3346  1.1  christos 			}
   3347  1.1  christos 		      putchar (' ');
   3348  1.1  christos 		    }
   3349  1.1  christos 		}
   3350  1.8  christos 	    }
   3351  1.1  christos 
   3352  1.1  christos 	  if (!wide_output)
   3353  1.1  christos 	    putchar ('\n');
   3354  1.1  christos 	  else
   3355  1.1  christos 	    need_nl = true;
   3356  1.1  christos 	}
   3357  1.1  christos 
   3358  1.1  christos       while ((*relppp) < relppend
   3359  1.1  christos 	     && (**relppp)->address < rel_offset + addr_offset + octets / opb)
   3360  1.1  christos 	{
   3361  1.1  christos 	  if (dump_reloc_info || dump_dynamic_reloc_info)
   3362  1.1  christos 	    {
   3363  1.1  christos 	      arelent *q;
   3364  1.1  christos 
   3365  1.1  christos 	      q = **relppp;
   3366  1.1  christos 
   3367  1.8  christos 	      if (wide_output)
   3368  1.8  christos 		putchar ('\t');
   3369  1.8  christos 	      else
   3370  1.8  christos 		printf ("\t\t\t");
   3371  1.8  christos 
   3372  1.8  christos 	      if (!no_addresses)
   3373  1.1  christos 		{
   3374  1.1  christos 		  objdump_print_value (section->vma - rel_offset + q->address,
   3375  1.8  christos 				       inf, true);
   3376  1.1  christos 		  printf (": ");
   3377  1.8  christos 		}
   3378  1.1  christos 
   3379  1.8  christos 	      if (q->howto == NULL)
   3380  1.1  christos 		printf ("*unknown*\t");
   3381  1.1  christos 	      else if (q->howto->name)
   3382  1.1  christos 		printf ("%s\t", q->howto->name);
   3383  1.1  christos 	      else
   3384  1.1  christos 		printf ("%d\t", q->howto->type);
   3385  1.1  christos 
   3386  1.1  christos 	      if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
   3387  1.1  christos 		printf ("*unknown*");
   3388  1.1  christos 	      else
   3389  1.1  christos 		{
   3390  1.1  christos 		  const char *sym_name;
   3391  1.1  christos 
   3392  1.1  christos 		  sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
   3393  1.1  christos 		  if (sym_name != NULL && *sym_name != '\0')
   3394  1.7  christos 		    objdump_print_symname (aux->abfd, inf, *q->sym_ptr_ptr);
   3395  1.7  christos 		  else
   3396  1.1  christos 		    {
   3397  1.1  christos 		      asection *sym_sec;
   3398  1.7  christos 
   3399  1.1  christos 		      sym_sec = bfd_asymbol_section (*q->sym_ptr_ptr);
   3400  1.1  christos 		      sym_name = bfd_section_name (sym_sec);
   3401  1.1  christos 		      if (sym_name == NULL || *sym_name == '\0')
   3402  1.1  christos 			sym_name = "*unknown*";
   3403  1.1  christos 		      printf ("%s", sanitize_string (sym_name));
   3404  1.8  christos 		    }
   3405  1.8  christos 		}
   3406  1.1  christos 
   3407  1.1  christos 	      if (q->addend)
   3408  1.1  christos 		{
   3409  1.1  christos 		  bfd_vma addend = q->addend;
   3410  1.1  christos 		  if ((bfd_signed_vma) addend < 0)
   3411  1.1  christos 		    {
   3412  1.8  christos 		      printf ("-0x");
   3413  1.1  christos 		      addend = -addend;
   3414  1.1  christos 		    }
   3415  1.1  christos 		  else
   3416  1.8  christos 		    printf ("+0x");
   3417  1.1  christos 		  objdump_print_value (addend, inf, true);
   3418  1.1  christos 		}
   3419  1.1  christos 
   3420  1.1  christos 	      printf ("\n");
   3421  1.1  christos 	      need_nl = false;
   3422  1.1  christos 	    }
   3423  1.1  christos 	  ++(*relppp);
   3424  1.1  christos 	}
   3425  1.1  christos 
   3426  1.1  christos       if (need_nl)
   3427  1.1  christos 	printf ("\n");
   3428  1.7  christos 
   3429  1.7  christos       addr_offset += octets / opb;
   3430  1.1  christos     }
   3431  1.1  christos 
   3432  1.1  christos   free (sfile.buffer);
   3433  1.1  christos   free (line_buffer);
   3434  1.1  christos   free (color_buffer);
   3435  1.8  christos }
   3436  1.8  christos 
   3437  1.8  christos static void
   3438  1.8  christos disassemble_section (bfd *abfd, asection *section, void *inf)
   3439  1.8  christos {
   3440  1.8  christos   const struct elf_backend_data *bed;
   3441  1.8  christos   bfd_vma sign_adjust = 0;
   3442  1.8  christos   struct disassemble_info *pinfo = (struct disassemble_info *) inf;
   3443  1.8  christos   struct objdump_disasm_info *paux;
   3444  1.8  christos   unsigned int opb = pinfo->octets_per_byte;
   3445  1.8  christos   bfd_byte *data = NULL;
   3446  1.8  christos   bfd_size_type datasize = 0;
   3447  1.8  christos   arelent **rel_pp = NULL;
   3448  1.8  christos   arelent **rel_ppstart = NULL;
   3449  1.8  christos   arelent **rel_ppend;
   3450  1.8  christos   bfd_vma stop_offset;
   3451  1.8  christos   asymbol *sym = NULL;
   3452  1.7  christos   long place = 0;
   3453  1.7  christos   long rel_count;
   3454  1.7  christos   bfd_vma rel_offset;
   3455  1.7  christos   unsigned long addr_offset;
   3456  1.7  christos   bool do_print;
   3457  1.7  christos   enum loop_control
   3458  1.1  christos   {
   3459  1.1  christos    stop_offset_reached,
   3460  1.1  christos    function_sym,
   3461  1.1  christos    next_sym
   3462  1.1  christos   } loop_until;
   3463  1.1  christos 
   3464  1.1  christos   /* Sections that do not contain machine
   3465  1.1  christos      code are not normally disassembled.  */
   3466  1.1  christos   if (! disassemble_all
   3467  1.1  christos       && only_list == NULL
   3468  1.1  christos       && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
   3469  1.1  christos 	  != (SEC_CODE | SEC_HAS_CONTENTS)))
   3470  1.7  christos     return;
   3471  1.1  christos 
   3472  1.1  christos   if (! process_section_p (section))
   3473  1.1  christos     return;
   3474  1.3  christos 
   3475  1.3  christos   datasize = bfd_section_size (section);
   3476  1.3  christos   if (datasize == 0)
   3477  1.3  christos     return;
   3478  1.3  christos 
   3479  1.3  christos   if (start_address == (bfd_vma) -1
   3480  1.3  christos       || start_address < section->vma)
   3481  1.3  christos     addr_offset = 0;
   3482  1.3  christos   else
   3483  1.3  christos     addr_offset = start_address - section->vma;
   3484  1.3  christos 
   3485  1.3  christos   if (stop_address == (bfd_vma) -1)
   3486  1.3  christos     stop_offset = datasize / opb;
   3487  1.3  christos   else
   3488  1.3  christos     {
   3489  1.3  christos       if (stop_address < section->vma)
   3490  1.3  christos 	stop_offset = 0;
   3491  1.3  christos       else
   3492  1.3  christos 	stop_offset = stop_address - section->vma;
   3493  1.3  christos       if (stop_offset > datasize / opb)
   3494  1.3  christos 	stop_offset = datasize / opb;
   3495  1.1  christos     }
   3496  1.1  christos 
   3497  1.8  christos   if (addr_offset >= stop_offset)
   3498  1.1  christos     return;
   3499  1.8  christos 
   3500  1.8  christos   /* Decide which set of relocs to use.  Load them if necessary.  */
   3501  1.1  christos   paux = (struct objdump_disasm_info *) pinfo->application_data;
   3502  1.1  christos   if (pinfo->dynrelbuf && dump_dynamic_reloc_info)
   3503  1.1  christos     {
   3504  1.1  christos       rel_pp = pinfo->dynrelbuf;
   3505  1.1  christos       rel_count = pinfo->dynrelcount;
   3506  1.1  christos       /* Dynamic reloc addresses are absolute, non-dynamic are section
   3507  1.1  christos 	 relative.  REL_OFFSET specifies the reloc address corresponding
   3508  1.1  christos 	 to the start of this section.  */
   3509  1.1  christos       rel_offset = section->vma;
   3510  1.1  christos     }
   3511  1.1  christos   else
   3512  1.1  christos     {
   3513  1.1  christos       rel_count = 0;
   3514  1.1  christos       rel_pp = NULL;
   3515  1.1  christos       rel_offset = 0;
   3516  1.1  christos 
   3517  1.1  christos       if ((section->flags & SEC_RELOC) != 0
   3518  1.1  christos 	  && (dump_reloc_info || pinfo->disassembler_needs_relocs))
   3519  1.1  christos 	{
   3520  1.1  christos 	  long relsize;
   3521  1.1  christos 
   3522  1.1  christos 	  relsize = bfd_get_reloc_upper_bound (abfd, section);
   3523  1.1  christos 	  if (relsize < 0)
   3524  1.1  christos 	    bfd_fatal (bfd_get_filename (abfd));
   3525  1.1  christos 
   3526  1.1  christos 	  if (relsize > 0)
   3527  1.1  christos 	    {
   3528  1.1  christos 	      rel_ppstart = rel_pp = (arelent **) xmalloc (relsize);
   3529  1.1  christos 	      rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
   3530  1.1  christos 	      if (rel_count < 0)
   3531  1.1  christos 		bfd_fatal (bfd_get_filename (abfd));
   3532  1.1  christos 
   3533  1.8  christos 	      /* Sort the relocs by address.  */
   3534  1.1  christos 	      qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
   3535  1.6  christos 	    }
   3536  1.6  christos 	}
   3537  1.6  christos     }
   3538  1.6  christos   rel_ppend = PTR_ADD (rel_pp, rel_count);
   3539  1.6  christos 
   3540  1.6  christos   if (!bfd_malloc_and_get_section (abfd, section, &data))
   3541  1.1  christos     {
   3542  1.1  christos       non_fatal (_("Reading section %s failed because: %s"),
   3543  1.1  christos 		 section->name, bfd_errmsg (bfd_get_error ()));
   3544  1.1  christos       return;
   3545  1.1  christos     }
   3546  1.1  christos 
   3547  1.7  christos   pinfo->buffer = data;
   3548  1.7  christos   pinfo->buffer_vma = section->vma;
   3549  1.8  christos   pinfo->buffer_length = datasize;
   3550  1.8  christos   pinfo->section = section;
   3551  1.7  christos 
   3552  1.1  christos   /* Sort the symbols into value and section order.  */
   3553  1.1  christos   compare_section = section;
   3554  1.1  christos   if (sorted_symcount > 1)
   3555  1.1  christos     qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
   3556  1.1  christos 
   3557  1.1  christos   /* Skip over the relocs belonging to addresses below the
   3558  1.7  christos      start address.  */
   3559  1.1  christos   while (rel_pp < rel_ppend
   3560  1.1  christos 	 && (*rel_pp)->address < rel_offset + addr_offset)
   3561  1.8  christos     ++rel_pp;
   3562  1.1  christos 
   3563  1.8  christos   printf (_("\nDisassembly of section %s:\n"), sanitize_string (section->name));
   3564  1.8  christos 
   3565  1.8  christos   /* Find the nearest symbol forwards from our current position.  */
   3566  1.1  christos   paux->require_sec = true;
   3567  1.1  christos   sym = (asymbol *) find_symbol_for_address (section->vma + addr_offset,
   3568  1.1  christos 					     (struct disassemble_info *) inf,
   3569  1.1  christos 					     &place);
   3570  1.1  christos   paux->require_sec = false;
   3571  1.1  christos 
   3572  1.1  christos   /* PR 9774: If the target used signed addresses then we must make
   3573  1.1  christos      sure that we sign extend the value that we calculate for 'addr'
   3574  1.1  christos      in the loop below.  */
   3575  1.1  christos   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   3576  1.1  christos       && (bed = get_elf_backend_data (abfd)) != NULL
   3577  1.1  christos       && bed->sign_extend_vma)
   3578  1.1  christos     sign_adjust = (bfd_vma) 1 << (bed->s->arch_size - 1);
   3579  1.7  christos 
   3580  1.7  christos   /* Disassemble a block of instructions up to the address associated with
   3581  1.7  christos      the symbol we have just found.  Then print the symbol and find the
   3582  1.1  christos      next symbol on.  Repeat until we have disassembled the entire section
   3583  1.1  christos      or we have reached the end of the address range we are interested in.  */
   3584  1.1  christos   do_print = paux->symbol == NULL;
   3585  1.1  christos   loop_until = stop_offset_reached;
   3586  1.3  christos 
   3587  1.8  christos   while (addr_offset < stop_offset)
   3588  1.1  christos     {
   3589  1.1  christos       bfd_vma addr;
   3590  1.1  christos       asymbol *nextsym;
   3591  1.1  christos       bfd_vma nextstop_offset;
   3592  1.1  christos       bool insns;
   3593  1.1  christos 
   3594  1.1  christos       addr = section->vma + addr_offset;
   3595  1.1  christos       addr = ((addr & ((sign_adjust << 1) - 1)) ^ sign_adjust) - sign_adjust;
   3596  1.1  christos 
   3597  1.1  christos       if (sym != NULL && bfd_asymbol_value (sym) <= addr)
   3598  1.1  christos 	{
   3599  1.1  christos 	  int x;
   3600  1.1  christos 
   3601  1.1  christos 	  for (x = place;
   3602  1.1  christos 	       (x < sorted_symcount
   3603  1.1  christos 		&& (bfd_asymbol_value (sorted_syms[x]) <= addr));
   3604  1.1  christos 	       ++x)
   3605  1.1  christos 	    continue;
   3606  1.1  christos 
   3607  1.1  christos 	  pinfo->symbols = sorted_syms + place;
   3608  1.1  christos 	  pinfo->num_symbols = x - place;
   3609  1.1  christos 	  pinfo->symtab_pos = place;
   3610  1.1  christos 	}
   3611  1.1  christos       else
   3612  1.1  christos 	{
   3613  1.7  christos 	  pinfo->symbols = NULL;
   3614  1.7  christos 	  pinfo->num_symbols = 0;
   3615  1.7  christos 	  pinfo->symtab_pos = -1;
   3616  1.7  christos 	}
   3617  1.7  christos 
   3618  1.7  christos       /* If we are only disassembling from a specific symbol,
   3619  1.7  christos 	 check to see if we should start or stop displaying.  */
   3620  1.7  christos       if (sym && paux->symbol)
   3621  1.7  christos 	{
   3622  1.7  christos 	  if (do_print)
   3623  1.7  christos 	    {
   3624  1.8  christos 	      /* See if we should stop printing.  */
   3625  1.7  christos 	      switch (loop_until)
   3626  1.7  christos 		{
   3627  1.7  christos 		case function_sym:
   3628  1.7  christos 		  if (sym->flags & BSF_FUNCTION)
   3629  1.7  christos 		    do_print = false;
   3630  1.7  christos 		  break;
   3631  1.7  christos 
   3632  1.7  christos 		case stop_offset_reached:
   3633  1.7  christos 		  /* Handled by the while loop.  */
   3634  1.7  christos 		  break;
   3635  1.7  christos 
   3636  1.8  christos 		case next_sym:
   3637  1.7  christos 		  /* FIXME: There is an implicit assumption here
   3638  1.7  christos 		     that the name of sym is different from
   3639  1.7  christos 		     paux->symbol.  */
   3640  1.7  christos 		  if (! bfd_is_local_label (abfd, sym))
   3641  1.7  christos 		    do_print = false;
   3642  1.7  christos 		  break;
   3643  1.7  christos 		}
   3644  1.7  christos 	    }
   3645  1.7  christos 	  else
   3646  1.7  christos 	    {
   3647  1.7  christos 	      const char * name = bfd_asymbol_name (sym);
   3648  1.7  christos 	      char * alloc = NULL;
   3649  1.7  christos 
   3650  1.7  christos 	      if (do_demangle && name[0] != '\0')
   3651  1.7  christos 		{
   3652  1.7  christos 		  /* Demangle the name.  */
   3653  1.7  christos 		  alloc = bfd_demangle (abfd, name, demangle_flags);
   3654  1.7  christos 		  if (alloc != NULL)
   3655  1.7  christos 		    name = alloc;
   3656  1.7  christos 		}
   3657  1.8  christos 
   3658  1.7  christos 	      /* We are not currently printing.  Check to see
   3659  1.7  christos 		 if the current symbol matches the requested symbol.  */
   3660  1.7  christos 	      if (streq (name, paux->symbol))
   3661  1.7  christos 		{
   3662  1.7  christos 		  do_print = true;
   3663  1.7  christos 
   3664  1.7  christos 		  if (sym->flags & BSF_FUNCTION)
   3665  1.7  christos 		    {
   3666  1.7  christos 		      if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   3667  1.7  christos 			  && ((elf_symbol_type *) sym)->internal_elf_sym.st_size > 0)
   3668  1.7  christos 			{
   3669  1.7  christos 			  /* Sym is a function symbol with a size associated
   3670  1.7  christos 			     with it.  Turn on automatic disassembly for the
   3671  1.7  christos 			     next VALUE bytes.  */
   3672  1.7  christos 			  stop_offset = addr_offset
   3673  1.7  christos 			    + ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
   3674  1.7  christos 			  loop_until = stop_offset_reached;
   3675  1.7  christos 			}
   3676  1.7  christos 		      else
   3677  1.7  christos 			{
   3678  1.7  christos 			  /* Otherwise we need to tell the loop heuristic to
   3679  1.7  christos 			     loop until the next function symbol is encountered.  */
   3680  1.7  christos 			  loop_until = function_sym;
   3681  1.7  christos 			}
   3682  1.7  christos 		    }
   3683  1.7  christos 		  else
   3684  1.7  christos 		    {
   3685  1.7  christos 		      /* Otherwise loop until the next symbol is encountered.  */
   3686  1.7  christos 		      loop_until = next_sym;
   3687  1.7  christos 		    }
   3688  1.7  christos 		}
   3689  1.7  christos 
   3690  1.1  christos 	      free (alloc);
   3691  1.1  christos 	    }
   3692  1.1  christos 	}
   3693  1.8  christos 
   3694  1.1  christos       if (! prefix_addresses && do_print)
   3695  1.1  christos 	{
   3696  1.1  christos 	  pinfo->fprintf_func (pinfo->stream, "\n");
   3697  1.1  christos 	  objdump_print_addr_with_sym (abfd, section, sym, addr,
   3698  1.1  christos 				       pinfo, false);
   3699  1.1  christos 	  pinfo->fprintf_func (pinfo->stream, ":\n");
   3700  1.1  christos 	}
   3701  1.1  christos 
   3702  1.1  christos       if (sym != NULL && bfd_asymbol_value (sym) > addr)
   3703  1.1  christos 	nextsym = sym;
   3704  1.7  christos       else if (sym == NULL)
   3705  1.1  christos 	nextsym = NULL;
   3706  1.1  christos       else
   3707  1.3  christos 	{
   3708  1.1  christos #define is_valid_next_sym(SYM) \
   3709  1.1  christos   (strcmp (bfd_section_name ((SYM)->section), bfd_section_name (section)) == 0 \
   3710  1.1  christos    && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
   3711  1.1  christos    && pinfo->symbol_is_valid (SYM, pinfo))
   3712  1.1  christos 
   3713  1.1  christos 	  /* Search forward for the next appropriate symbol in
   3714  1.1  christos 	     SECTION.  Note that all the symbols are sorted
   3715  1.1  christos 	     together into one big array, and that some sections
   3716  1.1  christos 	     may have overlapping addresses.  */
   3717  1.1  christos 	  while (place < sorted_symcount
   3718  1.1  christos 		 && ! is_valid_next_sym (sorted_syms [place]))
   3719  1.1  christos 	    ++place;
   3720  1.1  christos 
   3721  1.1  christos 	  if (place >= sorted_symcount)
   3722  1.1  christos 	    nextsym = NULL;
   3723  1.1  christos 	  else
   3724  1.1  christos 	    nextsym = sorted_syms[place];
   3725  1.1  christos 	}
   3726  1.1  christos 
   3727  1.1  christos       if (sym != NULL && bfd_asymbol_value (sym) > addr)
   3728  1.1  christos 	nextstop_offset = bfd_asymbol_value (sym) - section->vma;
   3729  1.1  christos       else if (nextsym == NULL)
   3730  1.1  christos 	nextstop_offset = stop_offset;
   3731  1.1  christos       else
   3732  1.1  christos 	nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
   3733  1.1  christos 
   3734  1.1  christos       if (nextstop_offset > stop_offset
   3735  1.1  christos 	  || nextstop_offset <= addr_offset)
   3736  1.1  christos 	nextstop_offset = stop_offset;
   3737  1.1  christos 
   3738  1.1  christos       /* If a symbol is explicitly marked as being an object
   3739  1.1  christos 	 rather than a function, just dump the bytes without
   3740  1.1  christos 	 disassembling them.  */
   3741  1.1  christos       if (disassemble_all
   3742  1.1  christos 	  || sym == NULL
   3743  1.1  christos 	  || sym->section != section
   3744  1.1  christos 	  || bfd_asymbol_value (sym) > addr
   3745  1.1  christos 	  || ((sym->flags & BSF_OBJECT) == 0
   3746  1.8  christos 	      && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
   3747  1.1  christos 		  == NULL)
   3748  1.8  christos 	      && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
   3749  1.1  christos 		  == NULL))
   3750  1.7  christos 	  || (sym->flags & BSF_FUNCTION) != 0)
   3751  1.7  christos 	insns = true;
   3752  1.7  christos       else
   3753  1.7  christos 	insns = false;
   3754  1.7  christos 
   3755  1.7  christos       if (do_print)
   3756  1.7  christos 	{
   3757  1.7  christos 	  /* Resolve symbol name.  */
   3758  1.7  christos 	  if (visualize_jumps && abfd && sym && sym->name)
   3759  1.7  christos 	    {
   3760  1.7  christos 	      struct disassemble_info di;
   3761  1.8  christos 	      SFILE sf;
   3762  1.8  christos 
   3763  1.8  christos 	      sf.alloc = strlen (sym->name) + 40;
   3764  1.7  christos 	      sf.buffer = (char*) xmalloc (sf.alloc);
   3765  1.7  christos 	      sf.pos = 0;
   3766  1.7  christos 	      disassemble_set_printf
   3767  1.7  christos 		(&di, &sf, (fprintf_ftype) objdump_sprintf,
   3768  1.7  christos 		 (fprintf_styled_ftype) objdump_styled_sprintf);
   3769  1.7  christos 
   3770  1.7  christos 	      objdump_print_symname (abfd, &di, sym);
   3771  1.7  christos 
   3772  1.7  christos 	      /* Fetch jump information.  */
   3773  1.7  christos 	      detected_jumps = disassemble_jumps
   3774  1.7  christos 		(pinfo, paux->disassemble_fn,
   3775  1.7  christos 		 addr_offset, nextstop_offset,
   3776  1.7  christos 		 rel_offset, &rel_pp, rel_ppend);
   3777  1.7  christos 
   3778  1.7  christos 	      /* Free symbol name.  */
   3779  1.7  christos 	      free (sf.buffer);
   3780  1.7  christos 	    }
   3781  1.7  christos 
   3782  1.7  christos 	  /* Add jumps to output.  */
   3783  1.7  christos 	  disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
   3784  1.7  christos 			     addr_offset, nextstop_offset,
   3785  1.7  christos 			     rel_offset, &rel_pp, rel_ppend);
   3786  1.7  christos 
   3787  1.7  christos 	  /* Free jumps.  */
   3788  1.3  christos 	  while (detected_jumps)
   3789  1.1  christos 	    {
   3790  1.1  christos 	      detected_jumps = jump_info_free (detected_jumps);
   3791  1.1  christos 	    }
   3792  1.1  christos 	}
   3793  1.1  christos 
   3794  1.1  christos       addr_offset = nextstop_offset;
   3795  1.1  christos       sym = nextsym;
   3796  1.1  christos     }
   3797  1.1  christos 
   3798  1.1  christos   free (data);
   3799  1.1  christos 
   3800  1.1  christos   if (rel_ppstart != NULL)
   3801  1.1  christos     free (rel_ppstart);
   3802  1.1  christos }
   3803  1.1  christos 
   3804  1.1  christos /* Disassemble the contents of an object file.  */
   3805  1.1  christos 
   3806  1.1  christos static void
   3807  1.1  christos disassemble_data (bfd *abfd)
   3808  1.1  christos {
   3809  1.1  christos   struct disassemble_info disasm_info;
   3810  1.1  christos   struct objdump_disasm_info aux;
   3811  1.1  christos   long i;
   3812  1.1  christos 
   3813  1.1  christos   print_files = NULL;
   3814  1.1  christos   prev_functionname = NULL;
   3815  1.1  christos   prev_line = -1;
   3816  1.1  christos   prev_discriminator = 0;
   3817  1.8  christos 
   3818  1.8  christos   /* We make a copy of syms to sort.  We don't want to sort syms
   3819  1.8  christos      because that will screw up the relocs.  */
   3820  1.8  christos   sorted_symcount = symcount ? symcount : dynsymcount;
   3821  1.8  christos   sorted_syms = (asymbol **) xmalloc ((sorted_symcount + synthcount)
   3822  1.1  christos 				      * sizeof (asymbol *));
   3823  1.8  christos   if (sorted_symcount != 0)
   3824  1.8  christos     {
   3825  1.1  christos       memcpy (sorted_syms, symcount ? syms : dynsyms,
   3826  1.1  christos 	      sorted_symcount * sizeof (asymbol *));
   3827  1.1  christos 
   3828  1.1  christos       sorted_symcount = remove_useless_symbols (sorted_syms, sorted_symcount);
   3829  1.1  christos     }
   3830  1.1  christos 
   3831  1.1  christos   for (i = 0; i < synthcount; ++i)
   3832  1.8  christos     {
   3833  1.8  christos       sorted_syms[sorted_symcount] = synthsyms + i;
   3834  1.1  christos       ++sorted_symcount;
   3835  1.1  christos     }
   3836  1.8  christos 
   3837  1.8  christos   init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf,
   3838  1.8  christos 			 (fprintf_styled_ftype) fprintf_styled);
   3839  1.1  christos   disasm_info.application_data = (void *) &aux;
   3840  1.7  christos   aux.abfd = abfd;
   3841  1.1  christos   aux.require_sec = false;
   3842  1.1  christos   disasm_info.dynrelbuf = NULL;
   3843  1.1  christos   disasm_info.dynrelcount = 0;
   3844  1.1  christos   aux.reloc = NULL;
   3845  1.1  christos   aux.symbol = disasm_sym;
   3846  1.1  christos 
   3847  1.1  christos   disasm_info.print_address_func = objdump_print_address;
   3848  1.1  christos   disasm_info.symbol_at_address_func = objdump_symbol_at_address;
   3849  1.1  christos 
   3850  1.1  christos   if (machine != NULL)
   3851  1.1  christos     {
   3852  1.1  christos       const bfd_arch_info_type *inf = bfd_scan_arch (machine);
   3853  1.1  christos 
   3854  1.1  christos       if (inf == NULL)
   3855  1.1  christos 	fatal (_("can't use supplied machine %s"), machine);
   3856  1.1  christos 
   3857  1.1  christos       abfd->arch_info = inf;
   3858  1.1  christos     }
   3859  1.1  christos 
   3860  1.1  christos   if (endian != BFD_ENDIAN_UNKNOWN)
   3861  1.1  christos     {
   3862  1.1  christos       struct bfd_target *xvec;
   3863  1.1  christos 
   3864  1.1  christos       xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
   3865  1.1  christos       memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
   3866  1.6  christos       xvec->byteorder = endian;
   3867  1.6  christos       abfd->xvec = xvec;
   3868  1.6  christos     }
   3869  1.1  christos 
   3870  1.1  christos   /* Use libopcodes to locate a suitable disassembler.  */
   3871  1.1  christos   aux.disassemble_fn = disassembler (bfd_get_arch (abfd),
   3872  1.1  christos 				     bfd_big_endian (abfd),
   3873  1.1  christos 				     bfd_get_mach (abfd), abfd);
   3874  1.1  christos   if (!aux.disassemble_fn)
   3875  1.1  christos     {
   3876  1.1  christos       non_fatal (_("can't disassemble for architecture %s\n"),
   3877  1.1  christos 		 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
   3878  1.1  christos       exit_status = 1;
   3879  1.1  christos       return;
   3880  1.1  christos     }
   3881  1.7  christos 
   3882  1.1  christos   disasm_info.flavour = bfd_get_flavour (abfd);
   3883  1.1  christos   disasm_info.arch = bfd_get_arch (abfd);
   3884  1.8  christos   disasm_info.mach = bfd_get_mach (abfd);
   3885  1.1  christos   disasm_info.disassembler_options = disassembler_options;
   3886  1.1  christos   disasm_info.octets_per_byte = bfd_octets_per_byte (abfd, NULL);
   3887  1.1  christos   disasm_info.skip_zeroes = DEFAULT_SKIP_ZEROES;
   3888  1.1  christos   disasm_info.skip_zeroes_at_end = DEFAULT_SKIP_ZEROES_AT_END;
   3889  1.1  christos   disasm_info.disassembler_needs_relocs = false;
   3890  1.1  christos 
   3891  1.1  christos   if (bfd_big_endian (abfd))
   3892  1.1  christos     disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
   3893  1.1  christos   else if (bfd_little_endian (abfd))
   3894  1.1  christos     disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
   3895  1.8  christos   else
   3896  1.8  christos     /* ??? Aborting here seems too drastic.  We could default to big or little
   3897  1.1  christos        instead.  */
   3898  1.1  christos     disasm_info.endian = BFD_ENDIAN_UNKNOWN;
   3899  1.1  christos 
   3900  1.6  christos   disasm_info.endian_code = disasm_info.endian;
   3901  1.8  christos 
   3902  1.8  christos   /* Allow the target to customize the info structure.  */
   3903  1.8  christos   disassemble_init_for_target (& disasm_info);
   3904  1.8  christos 
   3905  1.8  christos   /* Pre-load the dynamic relocs as we may need them during the disassembly.  */
   3906  1.8  christos   long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
   3907  1.1  christos 
   3908  1.8  christos   if (relsize < 0 && dump_dynamic_reloc_info)
   3909  1.8  christos     bfd_fatal (bfd_get_filename (abfd));
   3910  1.8  christos 
   3911  1.8  christos   if (relsize > 0)
   3912  1.1  christos     {
   3913  1.1  christos       disasm_info.dynrelbuf = (arelent **) xmalloc (relsize);
   3914  1.8  christos       disasm_info.dynrelcount
   3915  1.8  christos 	= bfd_canonicalize_dynamic_reloc (abfd, disasm_info.dynrelbuf, dynsyms);
   3916  1.8  christos       if (disasm_info.dynrelcount < 0)
   3917  1.8  christos 	bfd_fatal (bfd_get_filename (abfd));
   3918  1.1  christos 
   3919  1.1  christos       /* Sort the relocs by address.  */
   3920  1.1  christos       qsort (disasm_info.dynrelbuf, disasm_info.dynrelcount, sizeof (arelent *),
   3921  1.1  christos 	     compare_relocs);
   3922  1.1  christos     }
   3923  1.1  christos 
   3924  1.8  christos   disasm_info.symtab = sorted_syms;
   3925  1.8  christos   disasm_info.symtab_size = sorted_symcount;
   3926  1.1  christos 
   3927  1.7  christos   bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
   3928  1.1  christos 
   3929  1.1  christos   free (disasm_info.dynrelbuf);
   3930  1.8  christos   disasm_info.dynrelbuf = NULL;
   3931  1.1  christos   free (sorted_syms);
   3932  1.1  christos   disassemble_free_target (&disasm_info);
   3933  1.1  christos }
   3934  1.1  christos 
   3935  1.1  christos static bool
   3937  1.6  christos load_specific_debug_section (enum dwarf_section_display_enum debug,
   3938  1.7  christos 			     asection *sec, void *file)
   3939  1.8  christos {
   3940  1.1  christos   struct dwarf_section *section = &debug_displays [debug].section;
   3941  1.1  christos   bfd *abfd = (bfd *) file;
   3942  1.6  christos   bfd_byte *contents;
   3943  1.6  christos   bfd_size_type amt;
   3944  1.6  christos   size_t alloced;
   3945  1.8  christos   bool ret;
   3946  1.6  christos 
   3947  1.6  christos   if (section->start != NULL)
   3948  1.1  christos     {
   3949  1.6  christos       /* If it is already loaded, do nothing.  */
   3950  1.3  christos       if (streq (section->filename, bfd_get_filename (abfd)))
   3951  1.3  christos 	return true;
   3952  1.7  christos       free (section->start);
   3953  1.7  christos     }
   3954  1.7  christos 
   3955  1.7  christos   section->filename = bfd_get_filename (abfd);
   3956  1.7  christos   section->reloc_info = NULL;
   3957  1.7  christos   section->num_relocs = 0;
   3958  1.7  christos   section->address = bfd_section_vma (sec);
   3959  1.7  christos   section->size = bfd_section_size (sec);
   3960  1.7  christos   /* PR 24360: On 32-bit hosts sizeof (size_t) < sizeof (bfd_size_type). */
   3961  1.7  christos   alloced = amt = section->size + 1;
   3962  1.7  christos   if (alloced != amt || alloced == 0)
   3963  1.8  christos     {
   3964  1.1  christos       section->start = NULL;
   3965  1.8  christos       free_debug_section (debug);
   3966  1.8  christos       printf (_("\nSection '%s' has an invalid size: %#llx.\n"),
   3967  1.6  christos 	      sanitize_string (section->name),
   3968  1.6  christos 	      (unsigned long long) section->size);
   3969  1.1  christos       return false;
   3970  1.7  christos     }
   3971  1.7  christos 
   3972  1.1  christos   section->start = contents = xmalloc (alloced);
   3973  1.1  christos   /* Ensure any string section has a terminating NUL.  */
   3974  1.1  christos   section->start[section->size] = 0;
   3975  1.1  christos 
   3976  1.1  christos   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
   3977  1.8  christos       && debug_displays [debug].relocate)
   3978  1.8  christos     {
   3979  1.8  christos       ret = bfd_simple_get_relocated_section_contents (abfd,
   3980  1.1  christos 						       sec,
   3981  1.8  christos 						       section->start,
   3982  1.8  christos 						       syms) != NULL;
   3983  1.8  christos       if (ret)
   3984  1.8  christos 	{
   3985  1.3  christos 	  long reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
   3986  1.8  christos 
   3987  1.3  christos 	  if (reloc_size > 0)
   3988  1.8  christos 	    {
   3989  1.8  christos 	      unsigned long reloc_count;
   3990  1.8  christos 	      arelent **relocs;
   3991  1.8  christos 
   3992  1.8  christos 	      relocs = (arelent **) xmalloc (reloc_size);
   3993  1.8  christos 
   3994  1.8  christos 	      reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, NULL);
   3995  1.8  christos 	      if (reloc_count == 0)
   3996  1.3  christos 		free (relocs);
   3997  1.3  christos 	      else
   3998  1.1  christos 		{
   3999  1.8  christos 		  section->reloc_info = relocs;
   4000  1.8  christos 		  section->num_relocs = reloc_count;
   4001  1.8  christos 		}
   4002  1.8  christos 	    }
   4003  1.8  christos 	}
   4004  1.8  christos     }
   4005  1.8  christos   else
   4006  1.8  christos     ret = bfd_get_full_section_contents (abfd, sec, &contents);
   4007  1.8  christos 
   4008  1.8  christos   if (!ret)
   4009  1.1  christos     {
   4010  1.8  christos       free_debug_section (debug);
   4011  1.1  christos       printf (_("\nCan't get contents for section '%s'.\n"),
   4012  1.1  christos 	      sanitize_string (section->name));
   4013  1.8  christos       return false;
   4014  1.3  christos     }
   4015  1.3  christos 
   4016  1.3  christos   return true;
   4017  1.3  christos }
   4018  1.3  christos 
   4019  1.3  christos bool
   4020  1.8  christos reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
   4021  1.3  christos {
   4022  1.3  christos   arelent ** relocs;
   4023  1.3  christos   arelent * rp;
   4024  1.3  christos 
   4025  1.3  christos   if (dsec == NULL || dsec->reloc_info == NULL)
   4026  1.8  christos     return false;
   4027  1.3  christos 
   4028  1.8  christos   relocs = (arelent **) dsec->reloc_info;
   4029  1.3  christos 
   4030  1.3  christos   for (; (rp = * relocs) != NULL; ++ relocs)
   4031  1.8  christos     if (rp->address == offset)
   4032  1.1  christos       return true;
   4033  1.1  christos 
   4034  1.1  christos   return false;
   4035  1.1  christos }
   4036  1.1  christos 
   4037  1.8  christos bool
   4038  1.8  christos load_debug_section (enum dwarf_section_display_enum debug, void *file)
   4039  1.8  christos {
   4040  1.8  christos   struct dwarf_section *section = &debug_displays [debug].section;
   4041  1.1  christos   bfd *abfd = (bfd *) file;
   4042  1.1  christos   asection *sec;
   4043  1.1  christos   const char *name;
   4044  1.6  christos 
   4045  1.6  christos   if (!dump_any_debugging)
   4046  1.8  christos     return false;
   4047  1.6  christos 
   4048  1.1  christos   /* If it is already loaded, do nothing.  */
   4049  1.8  christos   if (section->start != NULL)
   4050  1.8  christos     {
   4051  1.8  christos       if (streq (section->filename, bfd_get_filename (abfd)))
   4052  1.8  christos 	return true;
   4053  1.8  christos     }
   4054  1.8  christos   /* Locate the debug section.  */
   4055  1.8  christos   name = section->uncompressed_name;
   4056  1.8  christos   sec = bfd_get_section_by_name (abfd, name);
   4057  1.8  christos   if (sec == NULL)
   4058  1.1  christos     {
   4059  1.8  christos       name = section->compressed_name;
   4060  1.8  christos       if (*name)
   4061  1.8  christos 	sec = bfd_get_section_by_name (abfd, name);
   4062  1.1  christos     }
   4063  1.1  christos   if (sec == NULL)
   4064  1.8  christos     {
   4065  1.1  christos       name = section->xcoff_name;
   4066  1.8  christos       if (*name)
   4067  1.1  christos 	sec = bfd_get_section_by_name (abfd, name);
   4068  1.1  christos     }
   4069  1.1  christos   if (sec == NULL)
   4070  1.1  christos     return false;
   4071  1.1  christos 
   4072  1.1  christos   section->name = name;
   4073  1.1  christos   return load_specific_debug_section (debug, sec, file);
   4074  1.1  christos }
   4075  1.1  christos 
   4076  1.1  christos void
   4077  1.1  christos free_debug_section (enum dwarf_section_display_enum debug)
   4078  1.1  christos {
   4079  1.1  christos   struct dwarf_section *section = &debug_displays [debug].section;
   4080  1.1  christos 
   4081  1.6  christos   free ((char *) section->start);
   4082  1.6  christos   section->start = NULL;
   4083  1.6  christos   section->address = 0;
   4084  1.6  christos   section->size = 0;
   4085  1.6  christos }
   4086  1.6  christos 
   4087  1.6  christos void
   4088  1.6  christos close_debug_file (void * file)
   4089  1.6  christos {
   4090  1.6  christos   bfd * abfd = (bfd *) file;
   4091  1.6  christos 
   4092  1.6  christos   bfd_close (abfd);
   4093  1.6  christos }
   4094  1.6  christos 
   4095  1.6  christos void *
   4096  1.6  christos open_debug_file (const char * pathname)
   4097  1.6  christos {
   4098  1.6  christos   bfd * data;
   4099  1.6  christos 
   4100  1.8  christos   data = bfd_openr (pathname, NULL);
   4101  1.6  christos   if (data == NULL)
   4102  1.6  christos     return NULL;
   4103  1.6  christos 
   4104  1.7  christos   if (! bfd_check_format (data, bfd_object))
   4105  1.7  christos     return NULL;
   4106  1.7  christos 
   4107  1.7  christos   return data;
   4108  1.7  christos }
   4109  1.7  christos 
   4110  1.7  christos #if HAVE_LIBDEBUGINFOD
   4111  1.7  christos /* Return a hex string represention of the build-id.  */
   4112  1.7  christos 
   4113  1.7  christos unsigned char *
   4114  1.7  christos get_build_id (void * data)
   4115  1.7  christos {
   4116  1.7  christos   unsigned i;
   4117  1.7  christos   char * build_id_str;
   4118  1.7  christos   bfd * abfd = (bfd *) data;
   4119  1.7  christos   const struct bfd_build_id * build_id;
   4120  1.7  christos 
   4121  1.7  christos   build_id = abfd->build_id;
   4122  1.7  christos   if (build_id == NULL)
   4123  1.7  christos     return NULL;
   4124  1.7  christos 
   4125  1.7  christos   build_id_str = malloc (build_id->size * 2 + 1);
   4126  1.7  christos   if (build_id_str == NULL)
   4127  1.7  christos     return NULL;
   4128  1.7  christos 
   4129  1.7  christos   for (i = 0; i < build_id->size; i++)
   4130  1.7  christos     sprintf (build_id_str + (i * 2), "%02x", build_id->data[i]);
   4131  1.1  christos   build_id_str[build_id->size * 2] = '\0';
   4132  1.1  christos 
   4133  1.8  christos   return (unsigned char *)build_id_str;
   4134  1.1  christos }
   4135  1.7  christos #endif /* HAVE_LIBDEBUGINFOD */
   4136  1.1  christos 
   4137  1.1  christos static void
   4138  1.8  christos dump_dwarf_section (bfd *abfd, asection *section,
   4139  1.8  christos 		    void *arg)
   4140  1.8  christos {
   4141  1.8  christos   const char *name = bfd_section_name (section);
   4142  1.1  christos   const char *match;
   4143  1.8  christos   int i;
   4144  1.8  christos   bool is_mainfile = *(bool *) arg;
   4145  1.8  christos 
   4146  1.8  christos   if (*name == 0)
   4147  1.8  christos     return;
   4148  1.1  christos 
   4149  1.1  christos   if (!is_mainfile && !process_links
   4150  1.1  christos       && (section->flags & SEC_DEBUGGING) == 0)
   4151  1.1  christos     return;
   4152  1.1  christos 
   4153  1.1  christos   if (startswith (name, ".gnu.linkonce.wi."))
   4154  1.8  christos     match = ".debug_info";
   4155  1.8  christos   else
   4156  1.1  christos     match = name;
   4157  1.1  christos 
   4158  1.1  christos   for (i = 0; i < max; i++)
   4159  1.1  christos     if ((strcmp (debug_displays [i].section.uncompressed_name, match) == 0
   4160  1.1  christos 	 || strcmp (debug_displays [i].section.compressed_name, match) == 0
   4161  1.1  christos 	 || strcmp (debug_displays [i].section.xcoff_name, match) == 0)
   4162  1.1  christos 	&& debug_displays [i].enabled != NULL
   4163  1.8  christos 	&& *debug_displays [i].enabled)
   4164  1.8  christos       {
   4165  1.1  christos 	struct dwarf_section *sec = &debug_displays [i].section;
   4166  1.8  christos 
   4167  1.1  christos 	if (strcmp (sec->uncompressed_name, match) == 0)
   4168  1.8  christos 	  sec->name = sec->uncompressed_name;
   4169  1.1  christos 	else if (strcmp (sec->compressed_name, match) == 0)
   4170  1.1  christos 	  sec->name = sec->compressed_name;
   4171  1.3  christos 	else
   4172  1.1  christos 	  sec->name = sec->xcoff_name;
   4173  1.1  christos 	if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
   4174  1.1  christos 					 section, abfd))
   4175  1.1  christos 	  {
   4176  1.1  christos 	    debug_displays [i].display (sec, abfd);
   4177  1.1  christos 
   4178  1.1  christos 	    if (i != info && i != abbrev)
   4179  1.1  christos 	      free_debug_section ((enum dwarf_section_display_enum) i);
   4180  1.1  christos 	  }
   4181  1.1  christos 	break;
   4182  1.8  christos       }
   4183  1.1  christos }
   4184  1.7  christos 
   4185  1.7  christos /* Dump the dwarf debugging information.  */
   4186  1.3  christos 
   4187  1.3  christos static void
   4188  1.3  christos dump_dwarf (bfd *abfd, bool is_mainfile)
   4189  1.3  christos {
   4190  1.3  christos   /* The byte_get pointer should have been set at the start of dump_bfd().  */
   4191  1.1  christos   if (byte_get == NULL)
   4192  1.1  christos     {
   4193  1.1  christos       warn (_("File %s does not contain any dwarf debug information\n"),
   4194  1.7  christos 	    bfd_get_filename (abfd));
   4195  1.7  christos       return;
   4196  1.7  christos     }
   4197  1.7  christos 
   4198  1.6  christos   switch (bfd_get_arch (abfd))
   4199  1.6  christos     {
   4200  1.1  christos     case bfd_arch_s12z:
   4201  1.7  christos       /* S12Z has a 24 bit address space.  But the only known
   4202  1.1  christos 	 producer of dwarf_info encodes addresses into 32 bits.  */
   4203  1.1  christos       eh_addr_size = 4;
   4204  1.1  christos       break;
   4205  1.7  christos 
   4206  1.7  christos     default:
   4207  1.6  christos       eh_addr_size = bfd_arch_bits_per_address (abfd) / 8;
   4208  1.8  christos       break;
   4209  1.1  christos     }
   4210  1.1  christos 
   4211  1.1  christos   init_dwarf_regnames_by_bfd_arch_and_mach (bfd_get_arch (abfd),
   4212  1.1  christos 					    bfd_get_mach (abfd));
   4213  1.1  christos 
   4214  1.6  christos   bfd_map_over_sections (abfd, dump_dwarf_section, (void *) &is_mainfile);
   4215  1.7  christos }
   4216  1.7  christos 
   4217  1.1  christos /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
   4219  1.6  christos    it.  Return NULL on failure.   */
   4220  1.1  christos 
   4221  1.1  christos static bfd_byte *
   4222  1.1  christos read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr,
   4223  1.1  christos 		    bfd_size_type *entsize_ptr)
   4224  1.7  christos {
   4225  1.7  christos   asection *stabsect;
   4226  1.8  christos   bfd_byte *contents;
   4227  1.1  christos 
   4228  1.1  christos   stabsect = bfd_get_section_by_name (abfd, sect_name);
   4229  1.6  christos   if (stabsect == NULL)
   4230  1.1  christos     {
   4231  1.1  christos       printf (_("No %s section present\n\n"),
   4232  1.1  christos 	      sanitize_string (sect_name));
   4233  1.1  christos       return false;
   4234  1.1  christos     }
   4235  1.1  christos 
   4236  1.1  christos   if (!bfd_malloc_and_get_section (abfd, stabsect, &contents))
   4237  1.1  christos     {
   4238  1.1  christos       non_fatal (_("reading %s section of %s failed: %s"),
   4239  1.7  christos 		 sect_name, bfd_get_filename (abfd),
   4240  1.7  christos 		 bfd_errmsg (bfd_get_error ()));
   4241  1.7  christos       exit_status = 1;
   4242  1.1  christos       free (contents);
   4243  1.1  christos       return NULL;
   4244  1.1  christos     }
   4245  1.1  christos 
   4246  1.1  christos   *size_ptr = bfd_section_size (stabsect);
   4247  1.1  christos   if (entsize_ptr)
   4248  1.1  christos     *entsize_ptr = stabsect->entsize;
   4249  1.1  christos 
   4250  1.1  christos   return contents;
   4251  1.1  christos }
   4252  1.1  christos 
   4253  1.1  christos /* Stabs entries use a 12 byte format:
   4254  1.1  christos      4 byte string table index
   4255  1.1  christos      1 byte stab type
   4256  1.1  christos      1 byte stab other field
   4257  1.1  christos      2 byte stab desc field
   4258  1.1  christos      4 byte stab value
   4259  1.1  christos    FIXME: This will have to change for a 64 bit object format.  */
   4260  1.1  christos 
   4261  1.1  christos #define STRDXOFF  (0)
   4262  1.1  christos #define TYPEOFF   (4)
   4263  1.1  christos #define OTHEROFF  (5)
   4264  1.1  christos #define DESCOFF   (6)
   4265  1.1  christos #define VALOFF    (8)
   4266  1.1  christos #define STABSIZE (12)
   4267  1.1  christos 
   4268  1.1  christos /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
   4269  1.1  christos    using string table section STRSECT_NAME (in `strtab').  */
   4270  1.1  christos 
   4271  1.1  christos static void
   4272  1.1  christos print_section_stabs (bfd *abfd,
   4273  1.1  christos 		     const char *stabsect_name,
   4274  1.1  christos 		     unsigned *string_offset_ptr)
   4275  1.1  christos {
   4276  1.1  christos   int i;
   4277  1.7  christos   unsigned file_string_table_offset = 0;
   4278  1.1  christos   unsigned next_file_string_table_offset = *string_offset_ptr;
   4279  1.1  christos   bfd_byte *stabp, *stabs_end;
   4280  1.1  christos 
   4281  1.1  christos   stabp = stabs;
   4282  1.1  christos   stabs_end = stabp + stab_size;
   4283  1.1  christos 
   4284  1.3  christos   printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name));
   4285  1.1  christos   printf ("Symnum n_type n_othr n_desc n_value  n_strx String\n");
   4286  1.1  christos 
   4287  1.1  christos   /* Loop through all symbols and print them.
   4288  1.1  christos 
   4289  1.1  christos      We start the index at -1 because there is a dummy symbol on
   4290  1.1  christos      the front of stabs-in-{coff,elf} sections that supplies sizes.  */
   4291  1.1  christos   for (i = -1; stabp <= stabs_end - STABSIZE; stabp += STABSIZE, i++)
   4292  1.1  christos     {
   4293  1.1  christos       const char *name;
   4294  1.1  christos       unsigned long strx;
   4295  1.1  christos       unsigned char type, other;
   4296  1.1  christos       unsigned short desc;
   4297  1.1  christos       bfd_vma value;
   4298  1.1  christos 
   4299  1.1  christos       strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
   4300  1.1  christos       type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
   4301  1.1  christos       other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
   4302  1.1  christos       desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
   4303  1.7  christos       value = bfd_h_get_32 (abfd, stabp + VALOFF);
   4304  1.1  christos 
   4305  1.1  christos       printf ("\n%-6d ", i);
   4306  1.1  christos       /* Either print the stab name, or, if unnamed, print its number
   4307  1.1  christos 	 again (makes consistent formatting for tools like awk).  */
   4308  1.1  christos       name = bfd_get_stab_name (type);
   4309  1.1  christos       if (name != NULL)
   4310  1.1  christos 	printf ("%-6s", sanitize_string (name));
   4311  1.1  christos       else if (type == N_UNDF)
   4312  1.1  christos 	printf ("HdrSym");
   4313  1.1  christos       else
   4314  1.1  christos 	printf ("%-6d", type);
   4315  1.1  christos       printf (" %-6d %-6d ", other, desc);
   4316  1.1  christos       bfd_printf_vma (abfd, value);
   4317  1.1  christos       printf (" %-6lu", strx);
   4318  1.1  christos 
   4319  1.1  christos       /* Symbols with type == 0 (N_UNDF) specify the length of the
   4320  1.1  christos 	 string table associated with this file.  We use that info
   4321  1.1  christos 	 to know how to relocate the *next* file's string table indices.  */
   4322  1.3  christos       if (type == N_UNDF)
   4323  1.3  christos 	{
   4324  1.1  christos 	  file_string_table_offset = next_file_string_table_offset;
   4325  1.1  christos 	  next_file_string_table_offset += value;
   4326  1.3  christos 	}
   4327  1.7  christos       else
   4328  1.7  christos 	{
   4329  1.3  christos 	  bfd_size_type amt = strx + file_string_table_offset;
   4330  1.1  christos 
   4331  1.1  christos 	  /* Using the (possibly updated) string table offset, print the
   4332  1.1  christos 	     string (if any) associated with this symbol.  */
   4333  1.1  christos 	  if (amt < stabstr_size)
   4334  1.1  christos 	    /* PR 17512: file: 079-79389-0.001:0.1.
   4335  1.1  christos 	       FIXME: May need to sanitize this string before displaying.  */
   4336  1.1  christos 	    printf (" %.*s", (int)(stabstr_size - amt), strtab + amt);
   4337  1.1  christos 	  else
   4338  1.1  christos 	    printf (" *");
   4339  1.1  christos 	}
   4340  1.1  christos     }
   4341  1.1  christos   printf ("\n\n");
   4342  1.1  christos   *string_offset_ptr = next_file_string_table_offset;
   4343  1.1  christos }
   4344  1.1  christos 
   4345  1.1  christos typedef struct
   4346  1.1  christos {
   4347  1.1  christos   const char * section_name;
   4348  1.1  christos   const char * string_section_name;
   4349  1.1  christos   unsigned string_offset;
   4350  1.1  christos }
   4351  1.1  christos stab_section_names;
   4352  1.1  christos 
   4353  1.1  christos static void
   4354  1.1  christos find_stabs_section (bfd *abfd, asection *section, void *names)
   4355  1.1  christos {
   4356  1.1  christos   int len;
   4357  1.1  christos   stab_section_names * sought = (stab_section_names *) names;
   4358  1.1  christos 
   4359  1.1  christos   /* Check for section names for which stabsect_name is a prefix, to
   4360  1.1  christos      handle .stab.N, etc.  */
   4361  1.1  christos   len = strlen (sought->section_name);
   4362  1.1  christos 
   4363  1.1  christos   /* If the prefix matches, and the files section name ends with a
   4364  1.1  christos      nul or a digit, then we match.  I.e., we want either an exact
   4365  1.7  christos      match or a section followed by a number.  */
   4366  1.3  christos   if (strncmp (sought->section_name, section->name, len) == 0
   4367  1.1  christos       && (section->name[len] == 0
   4368  1.1  christos 	  || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
   4369  1.7  christos     {
   4370  1.1  christos       if (strtab == NULL)
   4371  1.1  christos 	strtab = read_section_stabs (abfd, sought->string_section_name,
   4372  1.1  christos 				     &stabstr_size, NULL);
   4373  1.1  christos 
   4374  1.1  christos       if (strtab)
   4375  1.1  christos 	{
   4376  1.1  christos 	  stabs = read_section_stabs (abfd, section->name, &stab_size, NULL);
   4377  1.1  christos 	  if (stabs)
   4378  1.1  christos 	    print_section_stabs (abfd, section->name, &sought->string_offset);
   4379  1.1  christos 	}
   4380  1.1  christos     }
   4381  1.1  christos }
   4382  1.1  christos 
   4383  1.1  christos static void
   4384  1.1  christos dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
   4385  1.1  christos {
   4386  1.1  christos   stab_section_names s;
   4387  1.1  christos 
   4388  1.1  christos   s.section_name = stabsect_name;
   4389  1.1  christos   s.string_section_name = strsect_name;
   4390  1.1  christos   s.string_offset = 0;
   4391  1.1  christos 
   4392  1.1  christos   bfd_map_over_sections (abfd, find_stabs_section, & s);
   4393  1.1  christos 
   4394  1.1  christos   free (strtab);
   4395  1.1  christos   strtab = NULL;
   4396  1.1  christos }
   4397  1.1  christos 
   4398  1.1  christos /* Dump the any sections containing stabs debugging information.  */
   4399  1.1  christos 
   4400  1.1  christos static void
   4401  1.1  christos dump_stabs (bfd *abfd)
   4402  1.1  christos {
   4403  1.1  christos   dump_stabs_section (abfd, ".stab", ".stabstr");
   4404  1.1  christos   dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
   4405  1.1  christos   dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
   4406  1.1  christos 
   4407  1.1  christos   /* For Darwin.  */
   4408  1.1  christos   dump_stabs_section (abfd, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
   4409  1.1  christos 
   4410  1.1  christos   dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
   4411  1.1  christos }
   4412  1.1  christos 
   4413  1.1  christos static void
   4415  1.1  christos dump_bfd_header (bfd *abfd)
   4416  1.7  christos {
   4417  1.1  christos   char *comma = "";
   4418  1.1  christos 
   4419  1.1  christos   printf (_("architecture: %s, "),
   4420  1.1  christos 	  bfd_printable_arch_mach (bfd_get_arch (abfd),
   4421  1.1  christos 				   bfd_get_mach (abfd)));
   4422  1.1  christos   printf (_("flags 0x%08x:\n"), abfd->flags & ~BFD_FLAGS_FOR_BFD_USE_MASK);
   4423  1.1  christos 
   4424  1.1  christos #define PF(x, y)    if (abfd->flags & x) {printf ("%s%s", comma, y); comma=", ";}
   4425  1.1  christos   PF (HAS_RELOC, "HAS_RELOC");
   4426  1.1  christos   PF (EXEC_P, "EXEC_P");
   4427  1.1  christos   PF (HAS_LINENO, "HAS_LINENO");
   4428  1.1  christos   PF (HAS_DEBUG, "HAS_DEBUG");
   4429  1.1  christos   PF (HAS_SYMS, "HAS_SYMS");
   4430  1.1  christos   PF (HAS_LOCALS, "HAS_LOCALS");
   4431  1.7  christos   PF (DYNAMIC, "DYNAMIC");
   4432  1.7  christos   PF (WP_TEXT, "WP_TEXT");
   4433  1.8  christos   PF (D_PAGED, "D_PAGED");
   4434  1.7  christos   PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
   4435  1.7  christos   printf (_("\nstart address 0x"));
   4436  1.7  christos   bfd_printf_vma (abfd, abfd->start_address);
   4437  1.7  christos   printf ("\n");
   4438  1.7  christos }
   4439  1.7  christos 
   4440  1.7  christos 
   4442  1.7  christos #ifdef ENABLE_LIBCTF
   4443  1.7  christos /* Formatting callback function passed to ctf_dump.  Returns either the pointer
   4444  1.7  christos    it is passed, or a pointer to newly-allocated storage, in which case
   4445  1.7  christos    dump_ctf() will free it when it no longer needs it.  */
   4446  1.7  christos 
   4447  1.7  christos static char *
   4448  1.7  christos dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
   4449  1.7  christos 		       char *s, void *arg)
   4450  1.7  christos {
   4451  1.7  christos   const char *blanks = arg;
   4452  1.7  christos   char *new_s;
   4453  1.7  christos 
   4454  1.7  christos   if (asprintf (&new_s, "%s%s", blanks, s) < 0)
   4455  1.7  christos     return s;
   4456  1.7  christos   return new_s;
   4457  1.7  christos }
   4458  1.7  christos 
   4459  1.7  christos /* Make a ctfsect suitable for ctf_bfdopen_ctfsect().  */
   4460  1.7  christos static ctf_sect_t
   4461  1.7  christos make_ctfsect (const char *name, bfd_byte *data,
   4462  1.7  christos 	      bfd_size_type size)
   4463  1.7  christos {
   4464  1.7  christos   ctf_sect_t ctfsect;
   4465  1.8  christos 
   4466  1.8  christos   ctfsect.cts_name = name;
   4467  1.8  christos   ctfsect.cts_entsize = 1;
   4468  1.8  christos   ctfsect.cts_size = size;
   4469  1.8  christos   ctfsect.cts_data = data;
   4470  1.8  christos 
   4471  1.8  christos   return ctfsect;
   4472  1.8  christos }
   4473  1.8  christos 
   4474  1.8  christos /* Dump CTF errors/warnings.  */
   4475  1.8  christos static void
   4476  1.8  christos dump_ctf_errs (ctf_dict_t *fp)
   4477  1.8  christos {
   4478  1.8  christos   ctf_next_t *it = NULL;
   4479  1.8  christos   char *errtext;
   4480  1.8  christos   int is_warning;
   4481  1.8  christos   int err;
   4482  1.8  christos 
   4483  1.8  christos   /* Dump accumulated errors and warnings.  */
   4484  1.8  christos   while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
   4485  1.8  christos     {
   4486  1.8  christos       non_fatal (_("%s: %s"), is_warning ? _("warning"): _("error"),
   4487  1.8  christos 		 errtext);
   4488  1.7  christos       free (errtext);
   4489  1.7  christos     }
   4490  1.8  christos   if (err != ECTF_NEXT_END)
   4491  1.8  christos     {
   4492  1.8  christos       non_fatal (_("CTF error: cannot get CTF errors: `%s'"),
   4493  1.7  christos 		 ctf_errmsg (err));
   4494  1.7  christos     }
   4495  1.7  christos }
   4496  1.7  christos 
   4497  1.7  christos /* Dump one CTF archive member.  */
   4498  1.7  christos 
   4499  1.7  christos static void
   4500  1.8  christos dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
   4501  1.8  christos 			 size_t member)
   4502  1.8  christos {
   4503  1.8  christos   const char *things[] = {"Header", "Labels", "Data objects",
   4504  1.8  christos 			  "Function objects", "Variables", "Types", "Strings",
   4505  1.8  christos 			  ""};
   4506  1.8  christos   const char **thing;
   4507  1.8  christos   size_t i;
   4508  1.7  christos 
   4509  1.8  christos   /* Don't print out the name of the default-named archive member if it appears
   4510  1.8  christos      first in the list.  The name .ctf appears everywhere, even for things that
   4511  1.7  christos      aren't really archives, so printing it out is liable to be confusing; also,
   4512  1.7  christos      the common case by far is for only one archive member to exist, and hiding
   4513  1.7  christos      it in that case seems worthwhile.  */
   4514  1.7  christos 
   4515  1.7  christos   if (strcmp (name, ".ctf") != 0 || member != 0)
   4516  1.7  christos     printf (_("\nCTF archive member: %s:\n"), sanitize_string (name));
   4517  1.7  christos 
   4518  1.7  christos   if (ctf_parent_name (ctf) != NULL)
   4519  1.7  christos     ctf_import (ctf, parent);
   4520  1.7  christos 
   4521  1.7  christos   for (i = 0, thing = things; *thing[0]; thing++, i++)
   4522  1.7  christos     {
   4523  1.7  christos       ctf_dump_state_t *s = NULL;
   4524  1.7  christos       char *item;
   4525  1.7  christos 
   4526  1.7  christos       printf ("\n  %s:\n", *thing);
   4527  1.8  christos       while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
   4528  1.7  christos 			       (void *) "    ")) != NULL)
   4529  1.7  christos 	{
   4530  1.7  christos 	  printf ("%s\n", item);
   4531  1.7  christos 	  free (item);
   4532  1.8  christos 	}
   4533  1.8  christos 
   4534  1.7  christos       if (ctf_errno (ctf))
   4535  1.7  christos 	{
   4536  1.7  christos 	  non_fatal (_("Iteration failed: %s, %s"), *thing,
   4537  1.7  christos 		   ctf_errmsg (ctf_errno (ctf)));
   4538  1.7  christos 	  break;
   4539  1.7  christos 	}
   4540  1.7  christos     }
   4541  1.8  christos 
   4542  1.8  christos   dump_ctf_errs (ctf);
   4543  1.8  christos }
   4544  1.7  christos 
   4545  1.8  christos /* Dump the CTF debugging information.  */
   4546  1.8  christos 
   4547  1.8  christos static void
   4548  1.8  christos dump_ctf (bfd *abfd, const char *sect_name, const char *parent_name)
   4549  1.8  christos {
   4550  1.7  christos   ctf_archive_t *ctfa = NULL;
   4551  1.7  christos   bfd_byte *ctfdata = NULL;
   4552  1.8  christos   bfd_size_type ctfsize;
   4553  1.8  christos   ctf_sect_t ctfsect;
   4554  1.8  christos   ctf_dict_t *parent;
   4555  1.7  christos   ctf_dict_t *fp;
   4556  1.7  christos   ctf_next_t *i = NULL;
   4557  1.7  christos   const char *name;
   4558  1.8  christos   size_t member = 0;
   4559  1.8  christos   int err;
   4560  1.7  christos 
   4561  1.7  christos   if (sect_name == NULL)
   4562  1.7  christos     sect_name = ".ctf";
   4563  1.7  christos 
   4564  1.8  christos   if ((ctfdata = read_section_stabs (abfd, sect_name, &ctfsize, NULL)) == NULL)
   4565  1.8  christos       bfd_fatal (bfd_get_filename (abfd));
   4566  1.7  christos 
   4567  1.7  christos   /* Load the CTF file and dump it.  Preload the parent dict, since it will
   4568  1.7  christos      need to be imported into every child in turn. */
   4569  1.8  christos 
   4570  1.7  christos   ctfsect = make_ctfsect (sect_name, ctfdata, ctfsize);
   4571  1.8  christos   if ((ctfa = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
   4572  1.8  christos     {
   4573  1.8  christos       dump_ctf_errs (NULL);
   4574  1.8  christos       non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
   4575  1.7  christos       bfd_fatal (bfd_get_filename (abfd));
   4576  1.8  christos     }
   4577  1.7  christos 
   4578  1.8  christos   if ((parent = ctf_dict_open (ctfa, parent_name, &err)) == NULL)
   4579  1.8  christos     {
   4580  1.8  christos       dump_ctf_errs (NULL);
   4581  1.7  christos       non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
   4582  1.8  christos       bfd_fatal (bfd_get_filename (abfd));
   4583  1.8  christos     }
   4584  1.7  christos 
   4585  1.7  christos   printf (_("Contents of CTF section %s:\n"), sanitize_string (sect_name));
   4586  1.8  christos 
   4587  1.7  christos   while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
   4588  1.7  christos     dump_ctf_archive_member (fp, name, parent, member++);
   4589  1.7  christos   if (err != ECTF_NEXT_END)
   4590  1.8  christos     {
   4591  1.8  christos       dump_ctf_errs (NULL);
   4592  1.8  christos       non_fatal (_("CTF archive member open failure: %s"), ctf_errmsg (err));
   4593  1.8  christos       bfd_fatal (bfd_get_filename (abfd));
   4594  1.8  christos     }
   4595  1.1  christos   ctf_dict_close (parent);
   4596  1.1  christos   ctf_close (ctfa);
   4597  1.1  christos   free (ctfdata);
   4598  1.1  christos }
   4599  1.1  christos #else
   4600  1.7  christos static void
   4601  1.7  christos dump_ctf (bfd *abfd ATTRIBUTE_UNUSED, const char *sect_name ATTRIBUTE_UNUSED,
   4602  1.7  christos 	  const char *parent_name ATTRIBUTE_UNUSED) {}
   4603  1.1  christos #endif
   4604  1.1  christos 
   4605  1.1  christos 
   4606  1.1  christos static void
   4608  1.1  christos dump_bfd_private_header (bfd *abfd)
   4609  1.1  christos {
   4610  1.1  christos   if (!bfd_print_private_bfd_data (abfd, stdout))
   4611  1.1  christos     non_fatal (_("warning: private headers incomplete: %s"),
   4612  1.1  christos 	       bfd_errmsg (bfd_get_error ()));
   4613  1.1  christos }
   4614  1.1  christos 
   4615  1.1  christos static void
   4616  1.1  christos dump_target_specific (bfd *abfd)
   4617  1.3  christos {
   4618  1.1  christos   const struct objdump_private_desc * const *desc;
   4619  1.1  christos   struct objdump_private_option *opt;
   4620  1.1  christos   char *e, *b;
   4621  1.1  christos 
   4622  1.1  christos   /* Find the desc.  */
   4623  1.1  christos   for (desc = objdump_private_vectors; *desc != NULL; desc++)
   4624  1.1  christos     if ((*desc)->filter (abfd))
   4625  1.8  christos       break;
   4626  1.1  christos 
   4627  1.1  christos   if (*desc == NULL)
   4628  1.1  christos     {
   4629  1.1  christos       non_fatal (_("option -P/--private not supported by this file"));
   4630  1.1  christos       return;
   4631  1.1  christos     }
   4632  1.1  christos 
   4633  1.1  christos   /* Clear all options.  */
   4634  1.8  christos   for (opt = (*desc)->options; opt->name; opt++)
   4635  1.1  christos     opt->selected = false;
   4636  1.1  christos 
   4637  1.8  christos   /* Decode options.  */
   4638  1.8  christos   b = dump_private_options;
   4639  1.8  christos   do
   4640  1.8  christos     {
   4641  1.8  christos       e = strchr (b, ',');
   4642  1.1  christos 
   4643  1.8  christos       if (e)
   4644  1.1  christos 	*e = 0;
   4645  1.1  christos 
   4646  1.8  christos       for (opt = (*desc)->options; opt->name; opt++)
   4647  1.8  christos 	if (strcmp (opt->name, b) == 0)
   4648  1.8  christos 	  {
   4649  1.8  christos 	    opt->selected = true;
   4650  1.1  christos 	    break;
   4651  1.1  christos 	  }
   4652  1.1  christos       if (opt->name == NULL)
   4653  1.1  christos 	non_fatal (_("target specific dump '%s' not supported"), b);
   4654  1.1  christos 
   4655  1.1  christos       if (e)
   4656  1.1  christos 	{
   4657  1.1  christos 	  *e = ',';
   4658  1.1  christos 	  b = e + 1;
   4659  1.1  christos 	}
   4660  1.1  christos     }
   4661  1.1  christos   while (e != NULL);
   4662  1.1  christos 
   4663  1.6  christos   /* Dump.  */
   4664  1.1  christos   (*desc)->dump (abfd);
   4665  1.3  christos }
   4666  1.3  christos 
   4667  1.3  christos /* Display a section in hexadecimal format with associated characters.
   4669  1.1  christos    Each line prefixed by the zero padded address.  */
   4670  1.1  christos 
   4671  1.1  christos static void
   4672  1.1  christos dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
   4673  1.1  christos {
   4674  1.1  christos   bfd_byte *data = NULL;
   4675  1.8  christos   bfd_size_type datasize;
   4676  1.1  christos   bfd_vma addr_offset;
   4677  1.1  christos   bfd_vma start_offset;
   4678  1.8  christos   bfd_vma stop_offset;
   4679  1.1  christos   unsigned int opb = bfd_octets_per_byte (abfd, section);
   4680  1.3  christos   /* Bytes per line.  */
   4681  1.7  christos   const int onaline = 16;
   4682  1.1  christos   char buf[64];
   4683  1.1  christos   int count;
   4684  1.1  christos   int width;
   4685  1.1  christos 
   4686  1.1  christos   if (! process_section_p (section))
   4687  1.1  christos     return;
   4688  1.1  christos 
   4689  1.1  christos   if ((section->flags & SEC_HAS_CONTENTS) == 0)
   4690  1.1  christos     return;
   4691  1.1  christos 
   4692  1.1  christos   if ((datasize = bfd_section_size (section)) == 0)
   4693  1.1  christos     return;
   4694  1.1  christos 
   4695  1.1  christos   /* Compute the address range to display.  */
   4696  1.1  christos   if (start_address == (bfd_vma) -1
   4697  1.1  christos       || start_address < section->vma)
   4698  1.1  christos     start_offset = 0;
   4699  1.1  christos   else
   4700  1.1  christos     start_offset = start_address - section->vma;
   4701  1.1  christos 
   4702  1.1  christos   if (stop_address == (bfd_vma) -1)
   4703  1.1  christos     stop_offset = datasize / opb;
   4704  1.1  christos   else
   4705  1.1  christos     {
   4706  1.3  christos       if (stop_address < section->vma)
   4707  1.7  christos 	stop_offset = 0;
   4708  1.1  christos       else
   4709  1.1  christos 	stop_offset = stop_address - section->vma;
   4710  1.1  christos 
   4711  1.1  christos       if (stop_offset > datasize / opb)
   4712  1.1  christos 	stop_offset = datasize / opb;
   4713  1.1  christos     }
   4714  1.1  christos 
   4715  1.3  christos   if (start_offset >= stop_offset)
   4716  1.3  christos     return;
   4717  1.1  christos 
   4718  1.1  christos   printf (_("Contents of section %s:"), sanitize_string (section->name));
   4719  1.1  christos   if (display_file_offsets)
   4720  1.1  christos     printf (_("  (Starting at file offset: 0x%lx)"),
   4721  1.1  christos 	    (unsigned long) (section->filepos + start_offset));
   4722  1.1  christos   printf ("\n");
   4723  1.1  christos 
   4724  1.1  christos   if (!bfd_get_full_section_contents (abfd, section, &data))
   4725  1.1  christos     {
   4726  1.1  christos       non_fatal (_("Reading section %s failed because: %s"),
   4727  1.1  christos 		 section->name, bfd_errmsg (bfd_get_error ()));
   4728  1.1  christos       return;
   4729  1.1  christos     }
   4730  1.1  christos 
   4731  1.1  christos   width = 4;
   4732  1.1  christos 
   4733  1.1  christos   bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
   4734  1.1  christos   if (strlen (buf) >= sizeof (buf))
   4735  1.1  christos     abort ();
   4736  1.1  christos 
   4737  1.1  christos   count = 0;
   4738  1.1  christos   while (buf[count] == '0' && buf[count+1] != '\0')
   4739  1.1  christos     count++;
   4740  1.1  christos   count = strlen (buf) - count;
   4741  1.1  christos   if (count > width)
   4742  1.1  christos     width = count;
   4743  1.1  christos 
   4744  1.1  christos   bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
   4745  1.1  christos   if (strlen (buf) >= sizeof (buf))
   4746  1.1  christos     abort ();
   4747  1.1  christos 
   4748  1.1  christos   count = 0;
   4749  1.1  christos   while (buf[count] == '0' && buf[count+1] != '\0')
   4750  1.1  christos     count++;
   4751  1.1  christos   count = strlen (buf) - count;
   4752  1.1  christos   if (count > width)
   4753  1.1  christos     width = count;
   4754  1.1  christos 
   4755  1.1  christos   for (addr_offset = start_offset;
   4756  1.1  christos        addr_offset < stop_offset; addr_offset += onaline / opb)
   4757  1.1  christos     {
   4758  1.1  christos       bfd_size_type j;
   4759  1.1  christos 
   4760  1.1  christos       bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
   4761  1.1  christos       count = strlen (buf);
   4762  1.1  christos       if ((size_t) count >= sizeof (buf))
   4763  1.1  christos 	abort ();
   4764  1.1  christos 
   4765  1.1  christos       putchar (' ');
   4766  1.1  christos       while (count < width)
   4767  1.1  christos 	{
   4768  1.1  christos 	  putchar ('0');
   4769  1.1  christos 	  count++;
   4770  1.1  christos 	}
   4771  1.1  christos       fputs (buf + count - width, stdout);
   4772  1.1  christos       putchar (' ');
   4773  1.1  christos 
   4774  1.1  christos       for (j = addr_offset * opb;
   4775  1.1  christos 	   j < addr_offset * opb + onaline; j++)
   4776  1.1  christos 	{
   4777  1.1  christos 	  if (j < stop_offset * opb)
   4778  1.1  christos 	    printf ("%02x", (unsigned) (data[j]));
   4779  1.1  christos 	  else
   4780  1.1  christos 	    printf ("  ");
   4781  1.1  christos 	  if ((j & 3) == 3)
   4782  1.1  christos 	    printf (" ");
   4783  1.1  christos 	}
   4784  1.1  christos 
   4785  1.1  christos       printf (" ");
   4786  1.1  christos       for (j = addr_offset * opb;
   4787  1.1  christos 	   j < addr_offset * opb + onaline; j++)
   4788  1.1  christos 	{
   4789  1.1  christos 	  if (j >= stop_offset * opb)
   4790  1.1  christos 	    printf (" ");
   4791  1.1  christos 	  else
   4792  1.1  christos 	    printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
   4793  1.1  christos 	}
   4794  1.1  christos       putchar ('\n');
   4795  1.1  christos     }
   4796  1.1  christos   free (data);
   4797  1.1  christos }
   4798  1.1  christos 
   4799  1.8  christos /* Actually display the various requested regions.  */
   4800  1.1  christos 
   4801  1.1  christos static void
   4802  1.1  christos dump_data (bfd *abfd)
   4803  1.1  christos {
   4804  1.1  christos   bfd_map_over_sections (abfd, dump_section, NULL);
   4805  1.1  christos }
   4806  1.1  christos 
   4807  1.1  christos /* Should perhaps share code and display with nm?  */
   4808  1.1  christos 
   4809  1.1  christos static void
   4810  1.1  christos dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bool dynamic)
   4811  1.1  christos {
   4812  1.1  christos   asymbol **current;
   4813  1.1  christos   long max_count;
   4814  1.1  christos   long count;
   4815  1.1  christos 
   4816  1.1  christos   if (dynamic)
   4817  1.1  christos     {
   4818  1.1  christos       current = dynsyms;
   4819  1.1  christos       max_count = dynsymcount;
   4820  1.1  christos       printf ("DYNAMIC SYMBOL TABLE:\n");
   4821  1.1  christos     }
   4822  1.1  christos   else
   4823  1.1  christos     {
   4824  1.1  christos       current = syms;
   4825  1.1  christos       max_count = symcount;
   4826  1.1  christos       printf ("SYMBOL TABLE:\n");
   4827  1.1  christos     }
   4828  1.1  christos 
   4829  1.1  christos   if (max_count == 0)
   4830  1.1  christos     printf (_("no symbols\n"));
   4831  1.1  christos 
   4832  1.1  christos   for (count = 0; count < max_count; count++)
   4833  1.1  christos     {
   4834  1.1  christos       bfd *cur_bfd;
   4835  1.1  christos 
   4836  1.1  christos       if (*current == NULL)
   4837  1.1  christos 	printf (_("no information for symbol number %ld\n"), count);
   4838  1.1  christos 
   4839  1.1  christos       else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
   4840  1.1  christos 	printf (_("could not determine the type of symbol number %ld\n"),
   4841  1.1  christos 		count);
   4842  1.1  christos 
   4843  1.1  christos       else if (process_section_p ((* current)->section)
   4844  1.1  christos 	       && (dump_special_syms
   4845  1.7  christos 		   || !bfd_is_target_special_symbol (cur_bfd, *current)))
   4846  1.1  christos 	{
   4847  1.1  christos 	  const char *name = (*current)->name;
   4848  1.1  christos 
   4849  1.1  christos 	  if (do_demangle && name != NULL && *name != '\0')
   4850  1.1  christos 	    {
   4851  1.1  christos 	      char *alloc;
   4852  1.1  christos 
   4853  1.1  christos 	      /* If we want to demangle the name, we demangle it
   4854  1.1  christos 		 here, and temporarily clobber it while calling
   4855  1.1  christos 		 bfd_print_symbol.  FIXME: This is a gross hack.  */
   4856  1.8  christos 	      alloc = bfd_demangle (cur_bfd, name, demangle_flags);
   4857  1.8  christos 	      if (alloc != NULL)
   4858  1.8  christos 		(*current)->name = alloc;
   4859  1.8  christos 	      bfd_print_symbol (cur_bfd, stdout, *current,
   4860  1.8  christos 				bfd_print_symbol_all);
   4861  1.8  christos 	      if (alloc != NULL)
   4862  1.8  christos 		{
   4863  1.8  christos 		  (*current)->name = name;
   4864  1.8  christos 		  free (alloc);
   4865  1.8  christos 		}
   4866  1.8  christos 	    }
   4867  1.8  christos 	  else if (unicode_display != unicode_default
   4868  1.8  christos 		   && name != NULL && *name != '\0')
   4869  1.8  christos 	    {
   4870  1.8  christos 	      const char * sanitized_name;
   4871  1.8  christos 
   4872  1.8  christos 	      /* If we want to sanitize the name, we do it here, and
   4873  1.8  christos 		 temporarily clobber it while calling bfd_print_symbol.
   4874  1.1  christos 		 FIXME: This is a gross hack.  */
   4875  1.1  christos 	      sanitized_name = sanitize_string (name);
   4876  1.1  christos 	      if (sanitized_name != name)
   4877  1.1  christos 		(*current)->name = sanitized_name;
   4878  1.1  christos 	      else
   4879  1.1  christos 		sanitized_name = NULL;
   4880  1.1  christos 	      bfd_print_symbol (cur_bfd, stdout, *current,
   4881  1.1  christos 				bfd_print_symbol_all);
   4882  1.1  christos 	      if (sanitized_name != NULL)
   4883  1.1  christos 		(*current)->name = name;
   4884  1.1  christos 	    }
   4885  1.1  christos 	  else
   4886  1.1  christos 	    bfd_print_symbol (cur_bfd, stdout, *current,
   4887  1.1  christos 			      bfd_print_symbol_all);
   4888  1.1  christos 	  printf ("\n");
   4889  1.1  christos 	}
   4890  1.1  christos 
   4891  1.1  christos       current++;
   4892  1.1  christos     }
   4893  1.1  christos   printf ("\n\n");
   4894  1.1  christos }
   4895  1.1  christos 
   4896  1.1  christos static void
   4898  1.1  christos dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
   4899  1.1  christos {
   4900  1.1  christos   arelent **p;
   4901  1.1  christos   char *last_filename, *last_functionname;
   4902  1.1  christos   unsigned int last_line;
   4903  1.1  christos   unsigned int last_discriminator;
   4904  1.8  christos 
   4905  1.1  christos   /* Get column headers lined up reasonably.  */
   4906  1.1  christos   {
   4907  1.1  christos     static int width;
   4908  1.1  christos 
   4909  1.1  christos     if (width == 0)
   4910  1.1  christos       {
   4911  1.1  christos 	char buf[30];
   4912  1.1  christos 
   4913  1.1  christos 	bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
   4914  1.1  christos 	width = strlen (buf) - 7;
   4915  1.1  christos       }
   4916  1.1  christos     printf ("OFFSET %*s TYPE %*s VALUE\n", width, "", 12, "");
   4917  1.1  christos   }
   4918  1.1  christos 
   4919  1.1  christos   last_filename = NULL;
   4920  1.1  christos   last_functionname = NULL;
   4921  1.1  christos   last_line = 0;
   4922  1.1  christos   last_discriminator = 0;
   4923  1.1  christos 
   4924  1.1  christos   for (p = relpp; relcount && *p != NULL; p++, relcount--)
   4925  1.1  christos     {
   4926  1.1  christos       arelent *q = *p;
   4927  1.1  christos       const char *filename, *functionname;
   4928  1.1  christos       unsigned int linenumber;
   4929  1.1  christos       unsigned int discriminator;
   4930  1.1  christos       const char *sym_name;
   4931  1.1  christos       const char *section_name;
   4932  1.8  christos       bfd_vma addend2 = 0;
   4933  1.8  christos 
   4934  1.1  christos       if (start_address != (bfd_vma) -1
   4935  1.1  christos 	  && q->address < start_address)
   4936  1.1  christos 	continue;
   4937  1.1  christos       if (stop_address != (bfd_vma) -1
   4938  1.1  christos 	  && q->address > stop_address)
   4939  1.7  christos 	continue;
   4940  1.1  christos 
   4941  1.1  christos       if (with_line_numbers
   4942  1.1  christos 	  && sec != NULL
   4943  1.1  christos 	  && bfd_find_nearest_line_discriminator (abfd, sec, syms, q->address,
   4944  1.1  christos 						  &filename, &functionname,
   4945  1.1  christos 						  &linenumber, &discriminator))
   4946  1.1  christos 	{
   4947  1.1  christos 	  if (functionname != NULL
   4948  1.1  christos 	      && (last_functionname == NULL
   4949  1.1  christos 		  || strcmp (functionname, last_functionname) != 0))
   4950  1.8  christos 	    {
   4951  1.1  christos 	      printf ("%s():\n", sanitize_string (functionname));
   4952  1.8  christos 	      if (last_functionname != NULL)
   4953  1.8  christos 		free (last_functionname);
   4954  1.7  christos 	      last_functionname = xstrdup (functionname);
   4955  1.8  christos 	    }
   4956  1.8  christos 
   4957  1.7  christos 	  if (linenumber > 0
   4958  1.8  christos 	      && (linenumber != last_line
   4959  1.1  christos 		  || (filename != NULL
   4960  1.1  christos 		      && last_filename != NULL
   4961  1.1  christos 		      && filename_cmp (filename, last_filename) != 0)
   4962  1.1  christos 		  || (discriminator != last_discriminator)))
   4963  1.1  christos 	    {
   4964  1.1  christos 	      if (discriminator > 0)
   4965  1.1  christos 		printf ("%s:%u\n", filename == NULL ? "???" :
   4966  1.1  christos 			sanitize_string (filename), linenumber);
   4967  1.1  christos 	      else
   4968  1.1  christos 		printf ("%s:%u (discriminator %u)\n",
   4969  1.1  christos 			filename == NULL ? "???" : sanitize_string (filename),
   4970  1.1  christos 			linenumber, discriminator);
   4971  1.1  christos 	      last_line = linenumber;
   4972  1.1  christos 	      last_discriminator = discriminator;
   4973  1.1  christos 	      if (last_filename != NULL)
   4974  1.1  christos 		free (last_filename);
   4975  1.1  christos 	      if (filename == NULL)
   4976  1.1  christos 		last_filename = NULL;
   4977  1.1  christos 	      else
   4978  1.1  christos 		last_filename = xstrdup (filename);
   4979  1.1  christos 	    }
   4980  1.1  christos 	}
   4981  1.1  christos 
   4982  1.1  christos       if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
   4983  1.1  christos 	{
   4984  1.1  christos 	  sym_name = (*(q->sym_ptr_ptr))->name;
   4985  1.1  christos 	  section_name = (*(q->sym_ptr_ptr))->section->name;
   4986  1.1  christos 	}
   4987  1.1  christos       else
   4988  1.1  christos 	{
   4989  1.1  christos 	  sym_name = NULL;
   4990  1.1  christos 	  section_name = NULL;
   4991  1.1  christos 	}
   4992  1.1  christos 
   4993  1.1  christos       bfd_printf_vma (abfd, q->address);
   4994  1.1  christos       if (q->howto == NULL)
   4995  1.1  christos 	printf (" *unknown*         ");
   4996  1.1  christos       else if (q->howto->name)
   4997  1.1  christos 	{
   4998  1.1  christos 	  const char *name = q->howto->name;
   4999  1.7  christos 
   5000  1.1  christos 	  /* R_SPARC_OLO10 relocations contain two addends.
   5001  1.1  christos 	     But because 'arelent' lacks enough storage to
   5002  1.1  christos 	     store them both, the 64-bit ELF Sparc backend
   5003  1.1  christos 	     records this as two relocations.  One R_SPARC_LO10
   5004  1.1  christos 	     and one R_SPARC_13, both pointing to the same
   5005  1.1  christos 	     address.  This is merely so that we have some
   5006  1.1  christos 	     place to store both addend fields.
   5007  1.1  christos 
   5008  1.1  christos 	     Undo this transformation, otherwise the output
   5009  1.1  christos 	     will be confusing.  */
   5010  1.1  christos 	  if (abfd->xvec->flavour == bfd_target_elf_flavour
   5011  1.1  christos 	      && elf_tdata (abfd)->elf_header->e_machine == EM_SPARCV9
   5012  1.1  christos 	      && relcount > 1
   5013  1.1  christos 	      && !strcmp (q->howto->name, "R_SPARC_LO10"))
   5014  1.1  christos 	    {
   5015  1.1  christos 	      arelent *q2 = *(p + 1);
   5016  1.1  christos 	      if (q2 != NULL
   5017  1.1  christos 		  && q2->howto
   5018  1.1  christos 		  && q->address == q2->address
   5019  1.1  christos 		  && !strcmp (q2->howto->name, "R_SPARC_13"))
   5020  1.1  christos 		{
   5021  1.1  christos 		  name = "R_SPARC_OLO10";
   5022  1.1  christos 		  addend2 = q2->addend;
   5023  1.1  christos 		  p++;
   5024  1.1  christos 		}
   5025  1.1  christos 	    }
   5026  1.1  christos 	  printf (" %-16s  ", name);
   5027  1.7  christos 	}
   5028  1.1  christos       else
   5029  1.1  christos 	printf (" %-16d  ", q->howto->type);
   5030  1.1  christos 
   5031  1.1  christos       if (sym_name)
   5032  1.1  christos 	{
   5033  1.1  christos 	  objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
   5034  1.1  christos 	}
   5035  1.1  christos       else
   5036  1.1  christos 	{
   5037  1.1  christos 	  if (section_name == NULL)
   5038  1.1  christos 	    section_name = "*unknown*";
   5039  1.1  christos 	  printf ("[%s]", sanitize_string (section_name));
   5040  1.1  christos 	}
   5041  1.1  christos 
   5042  1.1  christos       if (q->addend)
   5043  1.1  christos 	{
   5044  1.1  christos 	  bfd_signed_vma addend = q->addend;
   5045  1.1  christos 	  if (addend < 0)
   5046  1.1  christos 	    {
   5047  1.1  christos 	      printf ("-0x");
   5048  1.1  christos 	      addend = -addend;
   5049  1.1  christos 	    }
   5050  1.1  christos 	  else
   5051  1.1  christos 	    printf ("+0x");
   5052  1.1  christos 	  bfd_printf_vma (abfd, addend);
   5053  1.1  christos 	}
   5054  1.1  christos       if (addend2)
   5055  1.1  christos 	{
   5056  1.1  christos 	  printf ("+0x");
   5057  1.1  christos 	  bfd_printf_vma (abfd, addend2);
   5058  1.1  christos 	}
   5059  1.1  christos 
   5060  1.1  christos       printf ("\n");
   5061  1.1  christos     }
   5062  1.7  christos 
   5063  1.1  christos   if (last_filename != NULL)
   5064  1.1  christos     free (last_filename);
   5065  1.1  christos   if (last_functionname != NULL)
   5066  1.1  christos     free (last_functionname);
   5067  1.1  christos }
   5068  1.1  christos 
   5069  1.1  christos static void
   5070  1.1  christos dump_relocs_in_section (bfd *abfd,
   5071  1.1  christos 			asection *section,
   5072  1.1  christos 			void *dummy ATTRIBUTE_UNUSED)
   5073  1.7  christos {
   5074  1.7  christos   arelent **relpp = NULL;
   5075  1.1  christos   long relcount;
   5076  1.1  christos   long relsize;
   5077  1.1  christos 
   5078  1.1  christos   if (   bfd_is_abs_section (section)
   5079  1.1  christos       || bfd_is_und_section (section)
   5080  1.1  christos       || bfd_is_com_section (section)
   5081  1.1  christos       || (! process_section_p (section))
   5082  1.7  christos       || ((section->flags & SEC_RELOC) == 0))
   5083  1.7  christos     return;
   5084  1.7  christos 
   5085  1.6  christos   printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section->name));
   5086  1.7  christos 
   5087  1.7  christos   relsize = bfd_get_reloc_upper_bound (abfd, section);
   5088  1.6  christos   if (relsize == 0)
   5089  1.6  christos     {
   5090  1.1  christos       printf (" (none)\n\n");
   5091  1.3  christos       return;
   5092  1.3  christos     }
   5093  1.7  christos 
   5094  1.7  christos   if (relsize < 0)
   5095  1.3  christos     relcount = relsize;
   5096  1.3  christos   else
   5097  1.1  christos     {
   5098  1.1  christos       relpp = (arelent **) xmalloc (relsize);
   5099  1.1  christos       relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
   5100  1.1  christos     }
   5101  1.1  christos 
   5102  1.1  christos   if (relcount < 0)
   5103  1.1  christos     {
   5104  1.1  christos       printf ("\n");
   5105  1.1  christos       non_fatal (_("failed to read relocs in: %s"),
   5106  1.1  christos 		 sanitize_string (bfd_get_filename (abfd)));
   5107  1.1  christos       bfd_fatal (_("error message was"));
   5108  1.1  christos     }
   5109  1.1  christos   else if (relcount == 0)
   5110  1.1  christos     printf (" (none)\n\n");
   5111  1.1  christos   else
   5112  1.1  christos     {
   5113  1.1  christos       printf ("\n");
   5114  1.1  christos       dump_reloc_set (abfd, section, relpp, relcount);
   5115  1.1  christos       printf ("\n\n");
   5116  1.1  christos     }
   5117  1.1  christos   free (relpp);
   5118  1.1  christos }
   5119  1.1  christos 
   5120  1.1  christos static void
   5121  1.1  christos dump_relocs (bfd *abfd)
   5122  1.1  christos {
   5123  1.1  christos   bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
   5124  1.1  christos }
   5125  1.1  christos 
   5126  1.1  christos static void
   5127  1.1  christos dump_dynamic_relocs (bfd *abfd)
   5128  1.1  christos {
   5129  1.1  christos   long relsize;
   5130  1.1  christos   arelent **relpp;
   5131  1.1  christos   long relcount;
   5132  1.1  christos 
   5133  1.1  christos   relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
   5134  1.1  christos   if (relsize < 0)
   5135  1.1  christos     bfd_fatal (bfd_get_filename (abfd));
   5136  1.1  christos 
   5137  1.1  christos   printf ("DYNAMIC RELOCATION RECORDS");
   5138  1.1  christos 
   5139  1.1  christos   if (relsize == 0)
   5140  1.1  christos     printf (" (none)\n\n");
   5141  1.1  christos   else
   5142  1.1  christos     {
   5143  1.1  christos       relpp = (arelent **) xmalloc (relsize);
   5144  1.1  christos       relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
   5145  1.1  christos 
   5146  1.1  christos       if (relcount < 0)
   5147  1.1  christos 	bfd_fatal (bfd_get_filename (abfd));
   5148  1.1  christos       else if (relcount == 0)
   5149  1.1  christos 	printf (" (none)\n\n");
   5150  1.1  christos       else
   5151  1.1  christos 	{
   5152  1.1  christos 	  printf ("\n");
   5153  1.1  christos 	  dump_reloc_set (abfd, NULL, relpp, relcount);
   5154  1.1  christos 	  printf ("\n\n");
   5155  1.1  christos 	}
   5156  1.1  christos       free (relpp);
   5157  1.1  christos     }
   5158  1.1  christos }
   5159  1.1  christos 
   5160  1.1  christos /* Creates a table of paths, to search for source files.  */
   5161  1.1  christos 
   5162  1.1  christos static void
   5163  1.1  christos add_include_path (const char *path)
   5164  1.1  christos {
   5165  1.1  christos   if (path[0] == 0)
   5166  1.1  christos     return;
   5167  1.1  christos   include_path_count++;
   5168  1.1  christos   include_paths = (const char **)
   5169  1.1  christos       xrealloc (include_paths, include_path_count * sizeof (*include_paths));
   5170  1.1  christos #ifdef HAVE_DOS_BASED_FILE_SYSTEM
   5171  1.1  christos   if (path[1] == ':' && path[2] == 0)
   5172  1.8  christos     path = concat (path, ".", (const char *) 0);
   5173  1.1  christos #endif
   5174  1.1  christos   include_paths[include_path_count - 1] = path;
   5175  1.1  christos }
   5176  1.1  christos 
   5177  1.1  christos static void
   5178  1.1  christos adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
   5179  1.7  christos 		  asection *section,
   5180  1.7  christos 		  void *arg)
   5181  1.7  christos {
   5182  1.7  christos   if ((section->flags & SEC_DEBUGGING) == 0)
   5183  1.7  christos     {
   5184  1.7  christos       bool *has_reloc_p = (bool *) arg;
   5185  1.7  christos       section->vma += adjust_section_vma;
   5186  1.7  christos       if (*has_reloc_p)
   5187  1.7  christos 	section->lma += adjust_section_vma;
   5188  1.7  christos     }
   5189  1.7  christos }
   5190  1.7  christos 
   5191  1.8  christos /* Return the sign-extended form of an ARCH_SIZE sized VMA.  */
   5192  1.8  christos 
   5193  1.8  christos static bfd_vma
   5194  1.8  christos sign_extend_address (bfd *abfd ATTRIBUTE_UNUSED,
   5195  1.8  christos 		     bfd_vma vma,
   5196  1.8  christos 		     unsigned arch_size)
   5197  1.8  christos {
   5198  1.8  christos   bfd_vma mask;
   5199  1.8  christos   mask = (bfd_vma) 1 << (arch_size - 1);
   5200  1.8  christos   return (((vma & ((mask << 1) - 1)) ^ mask) - mask);
   5201  1.8  christos }
   5202  1.8  christos 
   5203  1.8  christos static bool
   5204  1.8  christos might_need_separate_debug_info (bool is_mainfile)
   5205  1.8  christos {
   5206  1.8  christos   /* We do not follow links from debug info files.  */
   5207  1.8  christos   if (! is_mainfile)
   5208  1.8  christos     return false;
   5209  1.8  christos 
   5210  1.8  christos   /* Since do_follow_links might be enabled by default, only treat it as an
   5211  1.1  christos      indication that separate files should be loaded if setting it was a
   5212  1.1  christos      deliberate user action.  */
   5213  1.1  christos   if (DEFAULT_FOR_FOLLOW_LINKS == 0 && do_follow_links)
   5214  1.8  christos     return true;
   5215  1.1  christos 
   5216  1.7  christos   if (process_links || dump_symtab || dump_debugging
   5217  1.7  christos       || dump_dwarf_section_info)
   5218  1.7  christos     return true;
   5219  1.7  christos 
   5220  1.7  christos   return false;
   5221  1.7  christos }
   5222  1.7  christos 
   5223  1.7  christos /* Dump selected contents of ABFD.  */
   5224  1.7  christos 
   5225  1.8  christos static void
   5226  1.8  christos dump_bfd (bfd *abfd, bool is_mainfile)
   5227  1.7  christos {
   5228  1.7  christos   const struct elf_backend_data * bed;
   5229  1.7  christos 
   5230  1.7  christos   if (bfd_big_endian (abfd))
   5231  1.7  christos     byte_get = byte_get_big_endian;
   5232  1.7  christos   else if (bfd_little_endian (abfd))
   5233  1.7  christos     byte_get = byte_get_little_endian;
   5234  1.7  christos   else
   5235  1.7  christos     byte_get = NULL;
   5236  1.8  christos 
   5237  1.7  christos   /* Load any separate debug information files.  */
   5238  1.7  christos   if (byte_get != NULL && might_need_separate_debug_info (is_mainfile))
   5239  1.7  christos     {
   5240  1.7  christos       load_separate_debug_files (abfd, bfd_get_filename (abfd));
   5241  1.7  christos 
   5242  1.7  christos       /* If asked to do so, recursively dump the separate files.  */
   5243  1.7  christos       if (do_follow_links)
   5244  1.7  christos 	{
   5245  1.7  christos 	  separate_info * i;
   5246  1.7  christos 
   5247  1.7  christos 	  for (i = first_separate_info; i != NULL; i = i->next)
   5248  1.7  christos 	    dump_bfd (i->handle, false);
   5249  1.7  christos 	}
   5250  1.7  christos     }
   5251  1.7  christos 
   5252  1.1  christos   /* Adjust user-specified start and stop limits for targets that use
   5253  1.1  christos      signed addresses.  */
   5254  1.1  christos   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   5255  1.1  christos       && (bed = get_elf_backend_data (abfd)) != NULL
   5256  1.1  christos       && bed->sign_extend_vma)
   5257  1.8  christos     {
   5258  1.1  christos       start_address = sign_extend_address (abfd, start_address,
   5259  1.1  christos 					   bed->s->arch_size);
   5260  1.1  christos       stop_address = sign_extend_address (abfd, stop_address,
   5261  1.8  christos 					  bed->s->arch_size);
   5262  1.8  christos     }
   5263  1.8  christos 
   5264  1.8  christos   /* If we are adjusting section VMA's, change them all now.  Changing
   5265  1.8  christos      the BFD information is a hack.  However, we must do it, or
   5266  1.8  christos      bfd_find_nearest_line will not do the right thing.  */
   5267  1.8  christos   if (adjust_section_vma != 0)
   5268  1.8  christos     {
   5269  1.8  christos       bool has_reloc = (abfd->flags & HAS_RELOC);
   5270  1.8  christos       bfd_map_over_sections (abfd, adjust_addresses, &has_reloc);
   5271  1.8  christos     }
   5272  1.8  christos 
   5273  1.8  christos   if (is_mainfile || process_links)
   5274  1.8  christos     {
   5275  1.8  christos       if (! dump_debugging_tags && ! suppress_bfd_header)
   5276  1.8  christos 	printf (_("\n%s:     file format %s\n"),
   5277  1.8  christos 		sanitize_string (bfd_get_filename (abfd)),
   5278  1.1  christos 		abfd->xvec->name);
   5279  1.1  christos       if (dump_ar_hdrs)
   5280  1.1  christos 	print_arelt_descr (stdout, abfd, true, false);
   5281  1.1  christos       if (dump_file_header)
   5282  1.1  christos 	dump_bfd_header (abfd);
   5283  1.1  christos       if (dump_private_headers)
   5284  1.7  christos 	dump_bfd_private_header (abfd);
   5285  1.7  christos       if (dump_private_options != NULL)
   5286  1.7  christos 	dump_target_specific (abfd);
   5287  1.7  christos       if (! dump_debugging_tags && ! suppress_bfd_header)
   5288  1.7  christos 	putchar ('\n');
   5289  1.7  christos     }
   5290  1.7  christos 
   5291  1.7  christos   if (dump_symtab
   5292  1.7  christos       || dump_reloc_info
   5293  1.7  christos       || disassemble
   5294  1.7  christos       || dump_debugging
   5295  1.7  christos       || dump_dwarf_section_info)
   5296  1.8  christos     {
   5297  1.7  christos       syms = slurp_symtab (abfd);
   5298  1.7  christos 
   5299  1.7  christos       /* If following links, load any symbol tables from the linked files as well.  */
   5300  1.7  christos       if (do_follow_links && is_mainfile)
   5301  1.7  christos 	{
   5302  1.7  christos 	  separate_info * i;
   5303  1.7  christos 
   5304  1.7  christos 	  for (i = first_separate_info; i != NULL; i = i->next)
   5305  1.7  christos 	    {
   5306  1.7  christos 	      asymbol **  extra_syms;
   5307  1.8  christos 	      long        old_symcount = symcount;
   5308  1.8  christos 
   5309  1.7  christos 	      extra_syms = slurp_symtab (i->handle);
   5310  1.7  christos 
   5311  1.8  christos 	      if (extra_syms)
   5312  1.7  christos 		{
   5313  1.7  christos 		  if (old_symcount == 0)
   5314  1.7  christos 		    {
   5315  1.7  christos 		      syms = extra_syms;
   5316  1.7  christos 		    }
   5317  1.7  christos 		  else
   5318  1.7  christos 		    {
   5319  1.3  christos 		      syms = xrealloc (syms, ((symcount + old_symcount + 1)
   5320  1.8  christos 					      * sizeof (asymbol *)));
   5321  1.8  christos 		      memcpy (syms + old_symcount,
   5322  1.8  christos 			      extra_syms,
   5323  1.8  christos 			      (symcount + 1) * sizeof (asymbol *));
   5324  1.8  christos 		    }
   5325  1.8  christos 		}
   5326  1.8  christos 
   5327  1.8  christos 	      symcount += old_symcount;
   5328  1.3  christos 	    }
   5329  1.8  christos 	}
   5330  1.8  christos     }
   5331  1.8  christos 
   5332  1.8  christos   if (is_mainfile || process_links)
   5333  1.8  christos     {
   5334  1.8  christos       if (dump_section_headers)
   5335  1.8  christos 	dump_headers (abfd);
   5336  1.8  christos 
   5337  1.7  christos       if (dump_dynamic_symtab || dump_dynamic_reloc_info
   5338  1.8  christos 	  || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
   5339  1.8  christos 	dynsyms = slurp_dynamic_symtab (abfd);
   5340  1.8  christos 
   5341  1.8  christos       if (disassemble)
   5342  1.8  christos 	{
   5343  1.8  christos 	  synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
   5344  1.8  christos 						 dynsymcount, dynsyms,
   5345  1.8  christos 						 &synthsyms);
   5346  1.1  christos 	  if (synthcount < 0)
   5347  1.8  christos 	    synthcount = 0;
   5348  1.8  christos 	}
   5349  1.8  christos 
   5350  1.8  christos       if (dump_symtab)
   5351  1.8  christos 	dump_symbols (abfd, false);
   5352  1.8  christos       if (dump_dynamic_symtab)
   5353  1.8  christos 	dump_symbols (abfd, true);
   5354  1.8  christos     }
   5355  1.8  christos   if (dump_dwarf_section_info)
   5356  1.8  christos     dump_dwarf (abfd, is_mainfile);
   5357  1.8  christos   if (is_mainfile || process_links)
   5358  1.8  christos     {
   5359  1.1  christos       if (dump_ctf_section_info)
   5360  1.1  christos 	dump_ctf (abfd, dump_ctf_section_name, dump_ctf_parent_name);
   5361  1.1  christos       if (dump_stab_section_info)
   5362  1.1  christos 	dump_stabs (abfd);
   5363  1.1  christos       if (dump_reloc_info && ! disassemble)
   5364  1.1  christos 	dump_relocs (abfd);
   5365  1.8  christos       if (dump_dynamic_reloc_info && ! disassemble)
   5366  1.1  christos 	dump_dynamic_relocs (abfd);
   5367  1.1  christos       if (dump_section_contents)
   5368  1.1  christos 	dump_data (abfd);
   5369  1.1  christos       if (disassemble)
   5370  1.8  christos 	disassemble_data (abfd);
   5371  1.1  christos     }
   5372  1.1  christos 
   5373  1.1  christos   if (dump_debugging)
   5374  1.1  christos     {
   5375  1.1  christos       void *dhandle;
   5376  1.7  christos 
   5377  1.7  christos       dhandle = read_debugging_info (abfd, syms, symcount, true);
   5378  1.1  christos       if (dhandle != NULL)
   5379  1.6  christos 	{
   5380  1.6  christos 	  if (!print_debugging_info (stdout, dhandle, abfd, syms,
   5381  1.1  christos 				     bfd_demangle,
   5382  1.1  christos 				     dump_debugging_tags != 0))
   5383  1.3  christos 	    {
   5384  1.8  christos 	      non_fatal (_("%s: printing debugging information failed"),
   5385  1.1  christos 			 bfd_get_filename (abfd));
   5386  1.1  christos 	      exit_status = 1;
   5387  1.1  christos 	    }
   5388  1.1  christos 
   5389  1.1  christos 	  free (dhandle);
   5390  1.1  christos 	}
   5391  1.1  christos       /* PR 6483: If there was no STABS debug info in the file, try
   5392  1.1  christos 	 DWARF instead.  */
   5393  1.1  christos       else if (! dump_dwarf_section_info)
   5394  1.1  christos 	{
   5395  1.1  christos 	  dwarf_select_sections_all ();
   5396  1.1  christos 	  dump_dwarf (abfd, is_mainfile);
   5397  1.1  christos 	}
   5398  1.1  christos     }
   5399  1.1  christos 
   5400  1.1  christos   if (syms)
   5401  1.1  christos     {
   5402  1.1  christos       free (syms);
   5403  1.1  christos       syms = NULL;
   5404  1.1  christos     }
   5405  1.1  christos 
   5406  1.1  christos   if (dynsyms)
   5407  1.1  christos     {
   5408  1.1  christos       free (dynsyms);
   5409  1.7  christos       dynsyms = NULL;
   5410  1.7  christos     }
   5411  1.7  christos 
   5412  1.1  christos   if (synthsyms)
   5413  1.1  christos     {
   5414  1.1  christos       free (synthsyms);
   5415  1.1  christos       synthsyms = NULL;
   5416  1.1  christos     }
   5417  1.1  christos 
   5418  1.1  christos   symcount = 0;
   5419  1.1  christos   dynsymcount = 0;
   5420  1.1  christos   synthcount = 0;
   5421  1.8  christos 
   5422  1.1  christos   if (is_mainfile)
   5423  1.1  christos     free_debug_memory ();
   5424  1.1  christos }
   5425  1.1  christos 
   5426  1.1  christos static void
   5427  1.1  christos display_object_bfd (bfd *abfd)
   5428  1.1  christos {
   5429  1.1  christos   char **matching;
   5430  1.1  christos 
   5431  1.1  christos   if (bfd_check_format_matches (abfd, bfd_object, &matching))
   5432  1.1  christos     {
   5433  1.1  christos       dump_bfd (abfd, true);
   5434  1.1  christos       return;
   5435  1.1  christos     }
   5436  1.1  christos 
   5437  1.1  christos   if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
   5438  1.1  christos     {
   5439  1.1  christos       nonfatal (bfd_get_filename (abfd));
   5440  1.8  christos       list_matching_formats (matching);
   5441  1.1  christos       return;
   5442  1.1  christos     }
   5443  1.1  christos 
   5444  1.1  christos   if (bfd_get_error () != bfd_error_file_not_recognized)
   5445  1.1  christos     {
   5446  1.1  christos       nonfatal (bfd_get_filename (abfd));
   5447  1.8  christos       return;
   5448  1.1  christos     }
   5449  1.1  christos 
   5450  1.1  christos   if (bfd_check_format_matches (abfd, bfd_core, &matching))
   5451  1.1  christos     {
   5452  1.1  christos       dump_bfd (abfd, true);
   5453  1.1  christos       return;
   5454  1.1  christos     }
   5455  1.1  christos 
   5456  1.1  christos   nonfatal (bfd_get_filename (abfd));
   5457  1.1  christos 
   5458  1.1  christos   if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
   5459  1.1  christos     list_matching_formats (matching);
   5460  1.1  christos }
   5461  1.1  christos 
   5462  1.1  christos static void
   5463  1.1  christos display_any_bfd (bfd *file, int level)
   5464  1.8  christos {
   5465  1.3  christos   /* Decompress sections unless dumping the section contents.  */
   5466  1.3  christos   if (!dump_section_contents)
   5467  1.3  christos     file->flags |= BFD_DECOMPRESS;
   5468  1.3  christos 
   5469  1.3  christos   /* If the file is an archive, process all of its elements.  */
   5470  1.3  christos   if (bfd_check_format (file, bfd_archive))
   5471  1.3  christos     {
   5472  1.1  christos       bfd *arfile = NULL;
   5473  1.8  christos       bfd *last_arfile = NULL;
   5474  1.7  christos 
   5475  1.1  christos       if (level == 0)
   5476  1.1  christos 	printf (_("In archive %s:\n"), sanitize_string (bfd_get_filename (file)));
   5477  1.1  christos       else if (level > 100)
   5478  1.1  christos 	{
   5479  1.1  christos 	  /* Prevent corrupted files from spinning us into an
   5480  1.1  christos 	     infinite loop.  100 is an arbitrary heuristic.  */
   5481  1.1  christos 	  fatal (_("Archive nesting is too deep"));
   5482  1.1  christos 	  return;
   5483  1.1  christos 	}
   5484  1.1  christos       else
   5485  1.1  christos 	printf (_("In nested archive %s:\n"),
   5486  1.1  christos 		sanitize_string (bfd_get_filename (file)));
   5487  1.1  christos 
   5488  1.1  christos       for (;;)
   5489  1.1  christos 	{
   5490  1.1  christos 	  bfd_set_error (bfd_error_no_error);
   5491  1.3  christos 
   5492  1.3  christos 	  arfile = bfd_openr_next_archived_file (file, arfile);
   5493  1.3  christos 	  if (arfile == NULL)
   5494  1.3  christos 	    {
   5495  1.3  christos 	      if (bfd_get_error () != bfd_error_no_more_archived_files)
   5496  1.3  christos 		nonfatal (bfd_get_filename (file));
   5497  1.3  christos 	      break;
   5498  1.3  christos 	    }
   5499  1.3  christos 
   5500  1.1  christos 	  display_any_bfd (arfile, level + 1);
   5501  1.1  christos 
   5502  1.1  christos 	  if (last_arfile != NULL)
   5503  1.1  christos 	    {
   5504  1.1  christos 	      bfd_close (last_arfile);
   5505  1.1  christos 	      /* PR 17512: file: ac585d01.  */
   5506  1.1  christos 	      if (arfile == last_arfile)
   5507  1.1  christos 		{
   5508  1.1  christos 		  last_arfile = NULL;
   5509  1.1  christos 		  break;
   5510  1.1  christos 		}
   5511  1.8  christos 	    }
   5512  1.1  christos 	  last_arfile = arfile;
   5513  1.1  christos 	}
   5514  1.1  christos 
   5515  1.1  christos       if (last_arfile != NULL)
   5516  1.1  christos 	bfd_close (last_arfile);
   5517  1.1  christos     }
   5518  1.1  christos   else
   5519  1.1  christos     display_object_bfd (file);
   5520  1.1  christos }
   5521  1.1  christos 
   5522  1.1  christos static void
   5523  1.1  christos display_file (char *filename, char *target, bool last_file)
   5524  1.1  christos {
   5525  1.1  christos   bfd *file;
   5526  1.1  christos 
   5527  1.1  christos   if (get_file_size (filename) < 1)
   5528  1.1  christos     {
   5529  1.1  christos       exit_status = 1;
   5530  1.6  christos       return;
   5531  1.6  christos     }
   5532  1.6  christos 
   5533  1.6  christos   file = bfd_openr (filename, target);
   5534  1.6  christos   if (file == NULL)
   5535  1.6  christos     {
   5536  1.6  christos       nonfatal (filename);
   5537  1.6  christos       return;
   5538  1.6  christos     }
   5539  1.6  christos 
   5540  1.6  christos   display_any_bfd (file, 0);
   5541  1.6  christos 
   5542  1.1  christos   /* This is an optimization to improve the speed of objdump, especially when
   5543  1.1  christos      dumping a file with lots of associated debug informatiom.  Calling
   5544  1.1  christos      bfd_close on such a file can take a non-trivial amount of time as there
   5545  1.1  christos      are lots of lists to walk and buffers to free.  This is only really
   5546  1.1  christos      necessary however if we are about to load another file and we need the
   5547  1.1  christos      memory back.  Otherwise, if we are about to exit, then we can save (a lot
   5548  1.1  christos      of) time by only doing a quick close, and allowing the OS to reclaim the
   5549  1.8  christos      memory for us.  */
   5550  1.1  christos   if (! last_file)
   5551  1.8  christos     bfd_close (file);
   5552  1.1  christos   else
   5553  1.1  christos     bfd_close_all_done (file);
   5554  1.1  christos }
   5555  1.1  christos 
   5556  1.1  christos int
   5558  1.1  christos main (int argc, char **argv)
   5559  1.1  christos {
   5560  1.1  christos   int c;
   5561  1.3  christos   char *target = default_target;
   5562  1.1  christos   bool seenflag = false;
   5563  1.1  christos 
   5564  1.1  christos #ifdef HAVE_LC_MESSAGES
   5565  1.1  christos   setlocale (LC_MESSAGES, "");
   5566  1.1  christos #endif
   5567  1.7  christos   setlocale (LC_CTYPE, "");
   5568  1.7  christos 
   5569  1.1  christos   bindtextdomain (PACKAGE, LOCALEDIR);
   5570  1.1  christos   textdomain (PACKAGE);
   5571  1.1  christos 
   5572  1.8  christos   program_name = *argv;
   5573  1.1  christos   xmalloc_set_program_name (program_name);
   5574  1.1  christos   bfd_set_error_program_name (program_name);
   5575  1.1  christos 
   5576  1.1  christos   START_PROGRESS (program_name, 0);
   5577  1.1  christos 
   5578  1.1  christos   expandargv (&argc, &argv);
   5579  1.1  christos 
   5580  1.1  christos   if (bfd_init () != BFD_INIT_MAGIC)
   5581  1.1  christos     fatal (_("fatal error: libbfd ABI mismatch"));
   5582  1.1  christos   set_default_bfd_target ();
   5583  1.1  christos 
   5584  1.6  christos   while ((c = getopt_long (argc, argv,
   5585  1.6  christos 			   "CDE:FGHI:LM:P:RSTU:VW::ab:defghij:lm:prstvwxz",
   5586  1.6  christos 			   long_options, (int *) 0))
   5587  1.6  christos 	 != EOF)
   5588  1.6  christos     {
   5589  1.6  christos       switch (c)
   5590  1.6  christos 	{
   5591  1.6  christos 	case 0:
   5592  1.6  christos 	  break;		/* We've been given a long option.  */
   5593  1.6  christos 	case 'm':
   5594  1.1  christos 	  machine = optarg;
   5595  1.1  christos 	  break;
   5596  1.1  christos 	case 'M':
   5597  1.1  christos 	  {
   5598  1.1  christos 	    char *options;
   5599  1.8  christos 	    if (disassembler_options)
   5600  1.1  christos 	      /* Ignore potential memory leak for now.  */
   5601  1.1  christos 	      options = concat (disassembler_options, ",",
   5602  1.8  christos 				optarg, (const char *) NULL);
   5603  1.1  christos 	    else
   5604  1.1  christos 	      options = optarg;
   5605  1.1  christos 	    disassembler_options = remove_whitespace_and_extra_commas (options);
   5606  1.1  christos 	  }
   5607  1.1  christos 	  break;
   5608  1.8  christos 	case 'j':
   5609  1.1  christos 	  add_only (optarg);
   5610  1.1  christos 	  break;
   5611  1.1  christos 	case 'F':
   5612  1.1  christos 	  display_file_offsets = true;
   5613  1.1  christos 	  break;
   5614  1.1  christos 	case 'l':
   5615  1.1  christos 	  with_line_numbers = true;
   5616  1.1  christos 	  break;
   5617  1.1  christos 	case 'b':
   5618  1.1  christos 	  target = optarg;
   5619  1.1  christos 	  break;
   5620  1.1  christos 	case 'C':
   5621  1.7  christos 	  do_demangle = true;
   5622  1.7  christos 	  if (optarg != NULL)
   5623  1.7  christos 	    {
   5624  1.7  christos 	      enum demangling_styles style;
   5625  1.7  christos 
   5626  1.7  christos 	      style = cplus_demangle_name_to_style (optarg);
   5627  1.1  christos 	      if (style == unknown_demangling)
   5628  1.8  christos 		fatal (_("unknown demangling style `%s'"),
   5629  1.1  christos 		       optarg);
   5630  1.1  christos 
   5631  1.1  christos 	      cplus_demangle_set_style (style);
   5632  1.1  christos 	    }
   5633  1.1  christos 	  break;
   5634  1.1  christos 	case OPTION_RECURSE_LIMIT:
   5635  1.1  christos 	  demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
   5636  1.1  christos 	  break;
   5637  1.1  christos 	case OPTION_NO_RECURSE_LIMIT:
   5638  1.1  christos 	  demangle_flags |= DMGL_NO_RECURSE_LIMIT;
   5639  1.1  christos 	  break;
   5640  1.1  christos 	case 'w':
   5641  1.1  christos 	  do_wide = wide_output = true;
   5642  1.1  christos 	  break;
   5643  1.1  christos 	case OPTION_ADJUST_VMA:
   5644  1.1  christos 	  adjust_section_vma = parse_vma (optarg, "--adjust-vma");
   5645  1.1  christos 	  break;
   5646  1.1  christos 	case OPTION_START_ADDRESS:
   5647  1.1  christos 	  start_address = parse_vma (optarg, "--start-address");
   5648  1.1  christos 	  if ((stop_address != (bfd_vma) -1) && stop_address <= start_address)
   5649  1.1  christos 	    fatal (_("error: the start address should be before the end address"));
   5650  1.1  christos 	  break;
   5651  1.1  christos 	case OPTION_STOP_ADDRESS:
   5652  1.1  christos 	  stop_address = parse_vma (optarg, "--stop-address");
   5653  1.1  christos 	  if ((start_address != (bfd_vma) -1) && stop_address <= start_address)
   5654  1.1  christos 	    fatal (_("error: the stop address should be after the start address"));
   5655  1.1  christos 	  break;
   5656  1.1  christos 	case OPTION_PREFIX:
   5657  1.1  christos 	  prefix = optarg;
   5658  1.1  christos 	  prefix_length = strlen (prefix);
   5659  1.1  christos 	  /* Remove an unnecessary trailing '/' */
   5660  1.6  christos 	  while (IS_DIR_SEPARATOR (prefix[prefix_length - 1]))
   5661  1.8  christos 	    prefix_length--;
   5662  1.6  christos 	  break;
   5663  1.7  christos 	case OPTION_PREFIX_STRIP:
   5664  1.8  christos 	  prefix_strip = atoi (optarg);
   5665  1.8  christos 	  if (prefix_strip < 0)
   5666  1.8  christos 	    fatal (_("error: prefix strip must be non-negative"));
   5667  1.7  christos 	  break;
   5668  1.7  christos 	case OPTION_INSN_WIDTH:
   5669  1.7  christos 	  insn_width = strtoul (optarg, NULL, 0);
   5670  1.8  christos 	  if (insn_width <= 0)
   5671  1.7  christos 	    fatal (_("error: instruction width must be positive"));
   5672  1.7  christos 	  break;
   5673  1.8  christos 	case OPTION_INLINES:
   5674  1.8  christos 	  unwind_inlines = true;
   5675  1.7  christos 	  break;
   5676  1.7  christos 	case OPTION_VISUALIZE_JUMPS:
   5677  1.8  christos 	  visualize_jumps = true;
   5678  1.7  christos 	  color_output = false;
   5679  1.7  christos 	  extended_color_output = false;
   5680  1.7  christos 	  if (optarg != NULL)
   5681  1.7  christos 	    {
   5682  1.8  christos 	      if (streq (optarg, "color"))
   5683  1.8  christos 		color_output = true;
   5684  1.8  christos 	      else if (streq (optarg, "extended-color"))
   5685  1.8  christos 		{
   5686  1.8  christos 		  color_output = true;
   5687  1.8  christos 		  extended_color_output = true;
   5688  1.8  christos 		}
   5689  1.8  christos 	      else if (streq (optarg, "off"))
   5690  1.8  christos 		visualize_jumps = false;
   5691  1.8  christos 	      else
   5692  1.1  christos 		nonfatal (_("unrecognized argument to --visualize-option"));
   5693  1.1  christos 	    }
   5694  1.1  christos 	  break;
   5695  1.1  christos 	case OPTION_DISASSEMBLER_COLOR:
   5696  1.1  christos 	  if (streq (optarg, "off"))
   5697  1.1  christos 	    disassembler_color = false;
   5698  1.1  christos 	  else if (streq (optarg, "color"))
   5699  1.1  christos 	    disassembler_color = true;
   5700  1.1  christos 	  else if (streq (optarg, "extended-color"))
   5701  1.1  christos 	    disassembler_extended_color = true;
   5702  1.1  christos 	  else
   5703  1.1  christos 	    nonfatal (_("unrecognized argument to --disassembler-color"));
   5704  1.1  christos 	  break;
   5705  1.1  christos 	case 'E':
   5706  1.1  christos 	  if (strcmp (optarg, "B") == 0)
   5707  1.1  christos 	    endian = BFD_ENDIAN_BIG;
   5708  1.1  christos 	  else if (strcmp (optarg, "L") == 0)
   5709  1.1  christos 	    endian = BFD_ENDIAN_LITTLE;
   5710  1.1  christos 	  else
   5711  1.1  christos 	    {
   5712  1.1  christos 	      nonfatal (_("unrecognized -E option"));
   5713  1.1  christos 	      usage (stderr, 1);
   5714  1.1  christos 	    }
   5715  1.1  christos 	  break;
   5716  1.1  christos 	case OPTION_ENDIAN:
   5717  1.8  christos 	  if (strncmp (optarg, "big", strlen (optarg)) == 0)
   5718  1.8  christos 	    endian = BFD_ENDIAN_BIG;
   5719  1.1  christos 	  else if (strncmp (optarg, "little", strlen (optarg)) == 0)
   5720  1.1  christos 	    endian = BFD_ENDIAN_LITTLE;
   5721  1.8  christos 	  else
   5722  1.8  christos 	    {
   5723  1.1  christos 	      non_fatal (_("unrecognized --endian type `%s'"), optarg);
   5724  1.1  christos 	      exit_status = 1;
   5725  1.1  christos 	      usage (stderr, 1);
   5726  1.1  christos 	    }
   5727  1.1  christos 	  break;
   5728  1.8  christos 
   5729  1.8  christos 	case 'f':
   5730  1.1  christos 	  dump_file_header = true;
   5731  1.1  christos 	  seenflag = true;
   5732  1.1  christos 	  break;
   5733  1.8  christos 	case 'i':
   5734  1.1  christos 	  formats_info = true;
   5735  1.1  christos 	  seenflag = true;
   5736  1.8  christos 	  break;
   5737  1.8  christos 	case 'I':
   5738  1.8  christos 	  add_include_path (optarg);
   5739  1.8  christos 	  break;
   5740  1.8  christos 	case 'p':
   5741  1.8  christos 	  dump_private_headers = true;
   5742  1.8  christos 	  seenflag = true;
   5743  1.1  christos 	  break;
   5744  1.1  christos 	case 'P':
   5745  1.8  christos 	  dump_private_options = optarg;
   5746  1.8  christos 	  seenflag = true;
   5747  1.1  christos 	  break;
   5748  1.1  christos 	case 'x':
   5749  1.8  christos 	  dump_private_headers = true;
   5750  1.8  christos 	  dump_symtab = true;
   5751  1.1  christos 	  dump_reloc_info = true;
   5752  1.1  christos 	  dump_file_header = true;
   5753  1.8  christos 	  dump_ar_hdrs = true;
   5754  1.8  christos 	  dump_section_headers = true;
   5755  1.7  christos 	  seenflag = true;
   5756  1.1  christos 	  break;
   5757  1.1  christos 	case 't':
   5758  1.8  christos 	  dump_symtab = true;
   5759  1.1  christos 	  seenflag = true;
   5760  1.1  christos 	  break;
   5761  1.8  christos 	case 'T':
   5762  1.8  christos 	  dump_dynamic_symtab = true;
   5763  1.8  christos 	  seenflag = true;
   5764  1.1  christos 	  break;
   5765  1.1  christos 	case 'd':
   5766  1.8  christos 	  disassemble = true;
   5767  1.8  christos 	  seenflag = true;
   5768  1.8  christos 	  disasm_sym = optarg;
   5769  1.1  christos 	  break;
   5770  1.7  christos 	case 'z':
   5771  1.8  christos 	  disassemble_zeroes = true;
   5772  1.8  christos 	  break;
   5773  1.8  christos 	case 'D':
   5774  1.7  christos 	  disassemble = true;
   5775  1.7  christos 	  disassemble_all = true;
   5776  1.7  christos 	  seenflag = true;
   5777  1.7  christos 	  break;
   5778  1.7  christos 	case 'S':
   5779  1.1  christos 	  disassemble = true;
   5780  1.1  christos 	  with_source_code = true;
   5781  1.8  christos 	  seenflag = true;
   5782  1.1  christos 	  break;
   5783  1.1  christos 	case OPTION_SOURCE_COMMENT:
   5784  1.1  christos 	  disassemble = true;
   5785  1.1  christos 	  with_source_code = true;
   5786  1.8  christos 	  seenflag = true;
   5787  1.8  christos 	  if (optarg)
   5788  1.8  christos 	    source_comment = xstrdup (sanitize_string (optarg));
   5789  1.8  christos 	  else
   5790  1.8  christos 	    source_comment = xstrdup ("# ");
   5791  1.8  christos 	  break;
   5792  1.1  christos 	case 'g':
   5793  1.1  christos 	  dump_debugging = 1;
   5794  1.8  christos 	  seenflag = true;
   5795  1.1  christos 	  break;
   5796  1.8  christos 	case 'e':
   5797  1.8  christos 	  dump_debugging = 1;
   5798  1.8  christos 	  dump_debugging_tags = 1;
   5799  1.8  christos 	  do_demangle = true;
   5800  1.1  christos 	  seenflag = true;
   5801  1.8  christos 	  break;
   5802  1.8  christos 	case 'L':
   5803  1.8  christos 	  process_links = true;
   5804  1.8  christos 	  do_follow_links = true;
   5805  1.1  christos 	  break;
   5806  1.1  christos 	case 'W':
   5807  1.8  christos 	  seenflag = true;
   5808  1.1  christos 	  if (optarg)
   5809  1.8  christos 	    {
   5810  1.8  christos 	      if (dwarf_select_sections_by_letters (optarg))
   5811  1.8  christos 		dump_dwarf_section_info = true;
   5812  1.8  christos 	    }
   5813  1.1  christos 	  else
   5814  1.8  christos 	    {
   5815  1.8  christos 	      dump_dwarf_section_info = true;
   5816  1.8  christos 	      dwarf_select_sections_all ();
   5817  1.8  christos 	    }
   5818  1.1  christos 	  break;
   5819  1.1  christos 	case OPTION_DWARF:
   5820  1.1  christos 	  seenflag = true;
   5821  1.1  christos 	  if (optarg)
   5822  1.1  christos 	    {
   5823  1.1  christos 	      if (dwarf_select_sections_by_names (optarg))
   5824  1.1  christos 		dump_dwarf_section_info = true;
   5825  1.1  christos 	    }
   5826  1.1  christos 	  else
   5827  1.1  christos 	    {
   5828  1.1  christos 	      dwarf_select_sections_all ();
   5829  1.1  christos 	      dump_dwarf_section_info = true;
   5830  1.1  christos 	    }
   5831  1.1  christos 	  break;
   5832  1.1  christos 	case OPTION_DWARF_DEPTH:
   5833  1.8  christos 	  {
   5834  1.1  christos 	    char *cp;
   5835  1.8  christos 	    dwarf_cutoff_level = strtoul (optarg, & cp, 0);
   5836  1.7  christos 	  }
   5837  1.8  christos 	  break;
   5838  1.8  christos 	case OPTION_DWARF_START:
   5839  1.8  christos 	  {
   5840  1.8  christos 	    char *cp;
   5841  1.7  christos 	    dwarf_start_die = strtoul (optarg, & cp, 0);
   5842  1.7  christos 	    suppress_bfd_header = 1;
   5843  1.7  christos 	  }
   5844  1.7  christos 	  break;
   5845  1.8  christos 	case OPTION_DWARF_CHECK:
   5846  1.1  christos 	  dwarf_check = true;
   5847  1.8  christos 	  break;
   5848  1.8  christos #ifdef ENABLE_LIBCTF
   5849  1.1  christos 	case OPTION_CTF:
   5850  1.1  christos 	  dump_ctf_section_info = true;
   5851  1.8  christos 	  if (optarg)
   5852  1.8  christos 	    dump_ctf_section_name = xstrdup (optarg);
   5853  1.1  christos 	  seenflag = true;
   5854  1.1  christos 	  break;
   5855  1.8  christos 	case OPTION_CTF_PARENT:
   5856  1.8  christos 	  dump_ctf_parent_name = xstrdup (optarg);
   5857  1.1  christos 	  break;
   5858  1.1  christos #endif
   5859  1.8  christos 	case 'G':
   5860  1.8  christos 	  dump_stab_section_info = true;
   5861  1.1  christos 	  seenflag = true;
   5862  1.1  christos 	  break;
   5863  1.8  christos 	case 's':
   5864  1.8  christos 	  dump_section_contents = true;
   5865  1.1  christos 	  seenflag = true;
   5866  1.1  christos 	  break;
   5867  1.8  christos 	case 'r':
   5868  1.8  christos 	  dump_reloc_info = true;
   5869  1.1  christos 	  seenflag = true;
   5870  1.1  christos 	  break;
   5871  1.1  christos 	case 'R':
   5872  1.8  christos 	  dump_dynamic_reloc_info = true;
   5873  1.8  christos 	  seenflag = true;
   5874  1.8  christos 	  break;
   5875  1.8  christos 	case 'a':
   5876  1.8  christos 	  dump_ar_hdrs = true;
   5877  1.8  christos 	  seenflag = true;
   5878  1.8  christos 	  break;
   5879  1.8  christos 	case 'h':
   5880  1.8  christos 	  dump_section_headers = true;
   5881  1.8  christos 	  seenflag = true;
   5882  1.8  christos 	  break;
   5883  1.8  christos 	case 'v':
   5884  1.8  christos 	case 'V':
   5885  1.8  christos 	  show_version = true;
   5886  1.8  christos 	  seenflag = true;
   5887  1.8  christos 	  break;
   5888  1.8  christos 
   5889  1.8  christos 	case 'U':
   5890  1.8  christos 	  if (streq (optarg, "default") || streq (optarg, "d"))
   5891  1.1  christos 	    unicode_display = unicode_default;
   5892  1.1  christos 	  else if (streq (optarg, "locale") || streq (optarg, "l"))
   5893  1.3  christos 	    unicode_display = unicode_locale;
   5894  1.3  christos 	  else if (streq (optarg, "escape") || streq (optarg, "e"))
   5895  1.3  christos 	    unicode_display = unicode_escape;
   5896  1.1  christos 	  else if (streq (optarg, "invalid") || streq (optarg, "i"))
   5897  1.1  christos 	    unicode_display = unicode_invalid;
   5898  1.1  christos 	  else if (streq (optarg, "hex") || streq (optarg, "x"))
   5899  1.1  christos 	    unicode_display = unicode_hex;
   5900  1.1  christos 	  else if (streq (optarg, "highlight") || streq (optarg, "h"))
   5901  1.1  christos 	    unicode_display = unicode_highlight;
   5902  1.1  christos 	  else
   5903  1.1  christos 	    fatal (_("invalid argument to -U/--unicode: %s"), optarg);
   5904  1.1  christos 	  break;
   5905  1.1  christos 
   5906  1.1  christos 	case 'H':
   5907  1.8  christos 	  usage (stdout, 0);
   5908  1.8  christos 	  /* No need to set seenflag or to break - usage() does not return.  */
   5909  1.8  christos 	default:
   5910  1.8  christos 	  usage (stderr, 1);
   5911  1.1  christos 	}
   5912  1.1  christos     }
   5913  1.1  christos 
   5914  1.1  christos   if (show_version)
   5915  1.1  christos     print_version ("objdump");
   5916  1.8  christos 
   5917  1.1  christos   if (!seenflag)
   5918  1.1  christos     usage (stderr, 2);
   5919  1.6  christos 
   5920  1.6  christos   dump_any_debugging = (dump_debugging
   5921  1.6  christos 			|| dump_dwarf_section_info
   5922  1.6  christos 			|| process_links);
   5923  1.1  christos 
   5924  1.1  christos   if (formats_info)
   5925  1.1  christos     exit_status = display_info ();
   5926  1.7  christos   else
   5927  1.7  christos     {
   5928  1.7  christos       if (optind == argc)
   5929  1.1  christos 	display_file ("a.out", target, true);
   5930  1.1  christos       else
   5931  1.1  christos 	for (; optind < argc;)
   5932  1.1  christos 	  {
   5933  1.1  christos 	    display_file (argv[optind], target, optind == argc - 1);
   5934                	    optind++;
   5935                	  }
   5936                    }
   5937                
   5938                  free_only_list ();
   5939                  free (dump_ctf_section_name);
   5940                  free (dump_ctf_parent_name);
   5941                  free ((void *) source_comment);
   5942                
   5943                  END_PROGRESS (program_name);
   5944                
   5945                  return exit_status;
   5946                }
   5947