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