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