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