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