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