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