1 /* objdump.c -- dump information about an object file.
2 Copyright (C) 1990-2024 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
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.
29 The flow of execution is as follows:
31 1. Command line arguments are checked for control switches and the
32 information to be displayed is selected.
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.
38 3. The file's target architecture and binary file format are determined
39 by bfd_check_format(). If they are recognised, then dump_bfd() is
42 4. dump_bfd() in turn calls separate functions to display the requested
43 item(s) of information(s). For example disassemble_data() is called if
44 a disassembly has been requested.
46 When disassembling the code loops through blocks of instructions bounded
47 by symbols, calling disassemble_bytes() on each block. The actual
48 disassembling is done by the libopcodes library, via a function pointer
49 supplied by the disassembler() function. */
57 #include "demanguse.h"
60 #include "sframe-api.h"
62 #include "safe-ctype.h"
64 #include "libiberty.h"
66 #include "filenames.h"
75 #ifdef HAVE_LIBDEBUGINFOD
76 #include <elfutils/debuginfod.h>
79 /* Internal headers for the ELF .stab-dump code - sorry. */
80 #define BYTES_IN_WORD 32
81 #include "aout/aout64.h"
84 static int exit_status
= 0;
86 static char *default_target
= NULL
; /* Default at runtime. */
88 /* The following variables are set based on arguments passed on the
90 static int show_version
= 0; /* Show the version number. */
91 static int dump_section_contents
; /* -s */
92 static int dump_section_headers
; /* -h */
93 static bool dump_file_header
; /* -f */
94 static int dump_symtab
; /* -t */
95 static int dump_dynamic_symtab
; /* -T */
96 static int dump_reloc_info
; /* -r */
97 static int dump_dynamic_reloc_info
; /* -R */
98 static int dump_ar_hdrs
; /* -a */
99 static int dump_private_headers
; /* -p */
100 static char *dump_private_options
; /* -P */
101 static int no_addresses
; /* --no-addresses */
102 static int prefix_addresses
; /* --prefix-addresses */
103 static int with_line_numbers
; /* -l */
104 static bool with_source_code
; /* -S */
105 static int show_raw_insn
; /* --show-raw-insn */
106 static int dump_dwarf_section_info
; /* --dwarf */
107 static int dump_stab_section_info
; /* --stabs */
108 static int dump_ctf_section_info
; /* --ctf */
109 static char *dump_ctf_section_name
;
110 static char *dump_ctf_parent_name
; /* --ctf-parent */
111 static int dump_sframe_section_info
; /* --sframe */
112 static char *dump_sframe_section_name
;
113 static int do_demangle
; /* -C, --demangle */
114 static bool disassemble
; /* -d */
115 static bool disassemble_all
; /* -D */
116 static int disassemble_zeroes
; /* --disassemble-zeroes */
117 static bool formats_info
; /* -i */
118 int wide_output
; /* -w */
119 static int insn_width
; /* --insn-width */
120 static bfd_vma start_address
= (bfd_vma
) -1; /* --start-address */
121 static bfd_vma stop_address
= (bfd_vma
) -1; /* --stop-address */
122 static int dump_debugging
; /* --debugging */
123 static int dump_debugging_tags
; /* --debugging-tags */
124 static int suppress_bfd_header
;
125 static int dump_special_syms
= 0; /* --special-syms */
126 static bfd_vma adjust_section_vma
= 0; /* --adjust-vma */
127 static int file_start_context
= 0; /* --file-start-context */
128 static bool display_file_offsets
; /* -F */
129 static const char *prefix
; /* --prefix */
130 static int prefix_strip
; /* --prefix-strip */
131 static size_t prefix_length
;
132 static bool unwind_inlines
; /* --inlines. */
133 static const char * disasm_sym
; /* Disassembly start symbol. */
134 static const char * source_comment
; /* --source_comment. */
135 static bool visualize_jumps
= false; /* --visualize-jumps. */
136 static bool color_output
= false; /* --visualize-jumps=color. */
137 static bool extended_color_output
= false; /* --visualize-jumps=extended-color. */
138 static int process_links
= false; /* --process-links. */
139 static int show_all_symbols
; /* --show-all-symbols. */
140 static bool decompressed_dumps
= false; /* -Z, --decompress. */
142 static enum color_selection
144 on_if_terminal_output
,
145 on
, /* --disassembler-color=color. */
146 off
, /* --disassembler-color=off. */
147 extended
/* --disassembler-color=extended-color. */
148 } disassembler_color
=
149 #if DEFAULT_FOR_COLORED_DISASSEMBLY
150 on_if_terminal_output
;
155 static int dump_any_debugging
;
156 static int demangle_flags
= DMGL_ANSI
| DMGL_PARAMS
;
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
163 static bool disassembler_in_comment
= false;
165 /* A structure to record the sections mentioned in -j switches. */
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. */
172 /* Pointer to an array of 'only' structures.
173 This pointer is NULL if the -j switch has not been used. */
174 static struct only
* only_list
= NULL
;
176 /* Variables for handling include file path table. */
177 static const char **include_paths
;
178 static int include_path_count
;
180 /* Extra info to pass to the section disassembler and address printing
182 struct objdump_disasm_info
186 disassembler_ftype disassemble_fn
;
191 /* Architecture to disassemble for, or default if NULL. */
192 static char *machine
= NULL
;
194 /* Target specific options to the disassembler. */
195 static char *disassembler_options
= NULL
;
197 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
198 static enum bfd_endian endian
= BFD_ENDIAN_UNKNOWN
;
200 /* The symbol table. */
201 static asymbol
**syms
;
203 /* Number of symbols in `syms'. */
204 static long symcount
= 0;
206 /* The sorted symbol table. */
207 static asymbol
**sorted_syms
;
209 /* Number of symbols in `sorted_syms'. */
210 static long sorted_symcount
= 0;
212 /* The dynamic symbol table. */
213 static asymbol
**dynsyms
;
215 /* The synthetic symbol table. */
216 static asymbol
*synthsyms
;
217 static long synthcount
= 0;
219 /* Number of symbols in `dynsyms'. */
220 static long dynsymcount
= 0;
222 static bfd_byte
*stabs
;
223 static bfd_size_type stab_size
;
225 static bfd_byte
*strtab
;
226 static bfd_size_type stabstr_size
;
228 /* Handlers for -P/--private. */
229 static const struct objdump_private_desc
* const objdump_private_vectors
[] =
231 OBJDUMP_PRIVATE_VECTORS
235 /* The list of detected jumps inside a function. */
236 static struct jump_info
*detected_jumps
= NULL
;
238 typedef enum unicode_display_type
246 } unicode_display_type
;
248 static unicode_display_type unicode_display
= unicode_default
;
250 static void usage (FILE *, int) ATTRIBUTE_NORETURN
;
252 usage (FILE *stream
, int status
)
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"));
257 fprintf (stream
, _("\
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
, _("\
282 -Z, --decompress Decompress section(s) before displaying their contents\n"));
283 fprintf (stream
, _("\
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\
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"));
299 #if DEFAULT_FOR_FOLLOW_LINKS
300 fprintf (stream
, _("\
301 -WK,--dwarf=follow-links\n\
302 Follow links to separate debug info files (default)\n"));
303 fprintf (stream
, _("\
304 -WN,--dwarf=no-follow-links\n\
305 Do not follow links to separate debug info files\n"));
307 fprintf (stream
, _("\
308 -WK,--dwarf=follow-links\n\
309 Follow links to separate debug info files\n"));
310 fprintf (stream
, _("\
311 -WN,--dwarf=no-follow-links\n\
312 Do not follow links to separate debug info files\n\
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"));
323 fprintf (stream
, _("\
324 -L, --process-links Display the contents of non-debug sections in\n\
325 separate debuginfo files. (Implies -WK)\n"));
327 fprintf (stream
, _("\
328 --ctf[=SECTION] Display CTF info from SECTION, (default `.ctf')\n"));
330 fprintf (stream
, _("\
331 --sframe[=SECTION] Display SFrame info from SECTION, (default '.sframe')\n"));
332 fprintf (stream
, _("\
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"));
351 const struct objdump_private_desc
* const *desc
;
353 fprintf (stream
, _("\n The following switches are optional:\n"));
354 fprintf (stream
, _("\
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
, _("\
375 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n"));
376 display_demangler_styles (stream
, _("\
378 fprintf (stream
, _("\
379 --recurse-limit Enable a limit on recursion whilst demangling\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
, _("\
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
, _("\
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
, _("\
405 --show-all-symbols When disassembling, display all symbols at a given address\n"));
406 fprintf (stream
, _("\
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
, _("\
413 --prefix-strip=LEVEL Strip initial directory names for -S\n"));
414 fprintf (stream
, _("\
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"));
421 fprintf (stream
, _("\
422 --ctf-parent=NAME Use CTF archive member NAME as the CTF parent\n"));
424 fprintf (stream
, _("\
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
, _("\
432 --visualize-jumps=off Disable jump visualization\n"));
433 #if DEFAULT_FOR_COLORED_DISASSEMBLY
434 fprintf (stream
, _("\
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"));
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"));
444 fprintf (stream
, _("\
445 --disassembler-color=on Enable disassembler color output.\n"));
446 fprintf (stream
, _("\
447 --disassembler-color=extended Use 8-bit colors in disassembler output.\n\n"));
449 list_supported_targets (program_name
, stream
);
450 list_supported_architectures (program_name
, stream
);
452 disassembler_usage (stream
);
454 if (objdump_private_vectors
[0] != NULL
)
457 _("\nOptions supported for -P/--private switch:\n"));
458 for (desc
= objdump_private_vectors
; *desc
!= NULL
; desc
++)
459 (*desc
)->help (stream
);
462 if (REPORT_BUGS_TO
[0] && status
== 0)
463 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
467 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
471 OPTION_START_ADDRESS
,
481 OPTION_RECURSE_LIMIT
,
482 OPTION_NO_RECURSE_LIMIT
,
484 OPTION_SOURCE_COMMENT
,
490 OPTION_VISUALIZE_JUMPS
,
491 OPTION_DISASSEMBLER_COLOR
494 static struct option long_options
[]=
496 {"adjust-vma", required_argument
, NULL
, OPTION_ADJUST_VMA
},
497 {"all-headers", no_argument
, NULL
, 'x'},
498 {"architecture", required_argument
, NULL
, 'm'},
499 {"archive-headers", no_argument
, NULL
, 'a'},
501 {"ctf", optional_argument
, NULL
, OPTION_CTF
},
502 {"ctf-parent", required_argument
, NULL
, OPTION_CTF_PARENT
},
504 {"debugging", no_argument
, NULL
, 'g'},
505 {"debugging-tags", no_argument
, NULL
, 'e'},
506 {"decompress", no_argument
, NULL
, 'Z'},
507 {"demangle", optional_argument
, NULL
, 'C'},
508 {"disassemble", optional_argument
, NULL
, 'd'},
509 {"disassemble-all", no_argument
, NULL
, 'D'},
510 {"disassemble-zeroes", no_argument
, NULL
, 'z'},
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
},
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'},
520 {"file-offsets", no_argument
, NULL
, 'F'},
521 {"file-start-context", no_argument
, &file_start_context
, 1},
522 {"full-contents", no_argument
, NULL
, 's'},
523 {"headers", no_argument
, NULL
, 'h'},
524 {"help", no_argument
, NULL
, 'H'},
525 {"include", required_argument
, NULL
, 'I'},
526 {"info", no_argument
, NULL
, 'i'},
527 {"inlines", no_argument
, 0, OPTION_INLINES
},
528 {"insn-width", required_argument
, NULL
, OPTION_INSN_WIDTH
},
529 {"line-numbers", no_argument
, NULL
, 'l'},
530 {"no-addresses", no_argument
, &no_addresses
, 1},
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
},
535 {"prefix-addresses", no_argument
, &prefix_addresses
, 1},
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},
540 {"recurse-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
541 {"recursion-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
542 {"reloc", no_argument
, NULL
, 'r'},
543 {"section", required_argument
, NULL
, 'j'},
544 {"section-headers", no_argument
, NULL
, 'h'},
545 {"sframe", optional_argument
, NULL
, OPTION_SFRAME
},
546 {"show-all-symbols", no_argument
, &show_all_symbols
, 1},
547 {"show-raw-insn", no_argument
, &show_raw_insn
, 1},
548 {"source", no_argument
, NULL
, 'S'},
549 {"source-comment", optional_argument
, NULL
, OPTION_SOURCE_COMMENT
},
550 {"special-syms", no_argument
, &dump_special_syms
, 1},
551 {"stabs", no_argument
, NULL
, 'G'},
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'},
556 {"unicode", required_argument
, NULL
, 'U'},
557 {"version", no_argument
, NULL
, 'V'},
558 {"visualize-jumps", optional_argument
, 0, OPTION_VISUALIZE_JUMPS
},
559 {"wide", no_argument
, NULL
, 'w'},
560 {"disassembler-color", required_argument
, NULL
, OPTION_DISASSEMBLER_COLOR
},
561 {NULL
, no_argument
, NULL
, 0}
565 my_bfd_nonfatal (const char *msg
)
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. */
578 display_utf8 (const unsigned char * in
, char * out
, unsigned int * consumed
)
580 char * orig_out
= out
;
581 unsigned int nchars
= 0;
584 if (unicode_display
== unicode_default
)
590 if ((in
[1] & 0xc0) != 0x80)
593 if ((in
[0] & 0x20) == 0)
599 if ((in
[2] & 0xc0) != 0x80)
602 if ((in
[0] & 0x10) == 0)
608 if ((in
[3] & 0xc0) != 0x80)
614 switch (unicode_display
)
617 /* Copy the bytes into the output buffer as is. */
618 memcpy (out
, in
, nchars
);
622 case unicode_invalid
:
624 *out
++ = unicode_display
== unicode_hex
? '<' : '{';
627 for (j
= 0; j
< nchars
; j
++)
628 out
+= sprintf (out
, "%02x", in
[j
]);
629 *out
++ = unicode_display
== unicode_hex
? '>' : '}';
632 case unicode_highlight
:
634 out
+= sprintf (out
, "\x1B[31;47m"); /* Red. */
640 out
+= sprintf (out
, "\\u%02x%02x",
641 ((in
[0] & 0x1c) >> 2),
642 ((in
[0] & 0x03) << 6) | (in
[1] & 0x3f));
646 out
+= sprintf (out
, "\\u%02x%02x",
647 ((in
[0] & 0x0f) << 4) | ((in
[1] & 0x3c) >> 2),
648 ((in
[1] & 0x03) << 6) | ((in
[2] & 0x3f)));
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)));
662 if (unicode_display
== unicode_highlight
&& isatty (1))
663 out
+= sprintf (out
, "\x1B[0m"); /* Default colour. */
672 return out
- orig_out
;
675 /* Not a valid UTF-8 sequence. */
681 /* Returns a version of IN with any control characters
682 replaced by escape sequences. Uses a static buffer
685 If unicode display is enabled, then also handles the
686 conversion of unicode characters. */
689 sanitize_string (const char * in
)
691 static char * buffer
= NULL
;
692 static size_t buffer_len
= 0;
693 const char * original
= in
;
700 /* See if any conversion is necessary. In the majority
701 of cases it will not be needed. */
704 unsigned char c
= *in
++;
712 if (unicode_display
!= unicode_default
&& c
>= 0xc0)
717 /* Copy the input, translating as needed. */
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
)
725 buffer_len
= max_needed
;
727 buffer
= xmalloc (buffer_len
);
733 unsigned char c
= *in
++;
743 else if (unicode_display
!= unicode_default
&& c
>= 0xc0)
745 unsigned int num_consumed
;
747 out
+= display_utf8 ((const unsigned char *) --in
, out
, &num_consumed
);
760 /* Returns TRUE if the specified section should be dumped. */
763 process_section_p (asection
* section
)
767 if (only_list
== NULL
)
770 for (only
= only_list
; only
; only
= only
->next
)
771 if (strcmp (only
->name
, section
->name
) == 0)
780 /* Add an entry to the 'only' list. */
783 add_only (char * name
)
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)
793 only
= xmalloc (sizeof * only
);
796 only
->next
= only_list
;
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
808 free_only_list (void)
810 bool at_least_one_seen
= false;
814 if (only_list
== NULL
)
817 for (only
= only_list
; only
; only
= only
->next
)
820 at_least_one_seen
= true;
824 for (only
= only_list
; only
; only
= next
)
826 if (! at_least_one_seen
)
828 non_fatal (_("section '%s' mentioned in a -j option, "
829 "but not found in any input file"),
840 dump_section_header (bfd
*abfd
, asection
*section
, void *data
)
843 unsigned int opb
= bfd_octets_per_byte (abfd
, section
);
844 int longest_section_name
= *((int *) data
);
846 /* Ignore linker created section. See elfNN_ia64_object_p in
848 if (section
->flags
& SEC_LINKER_CREATED
)
851 /* PR 10413: Skip sections that we are ignoring. */
852 if (! process_section_p (section
))
855 printf ("%3d %-*s %08lx ", section
->index
, longest_section_name
,
856 sanitize_string (bfd_section_name (section
)),
857 (unsigned long) bfd_section_size (section
) / opb
);
858 bfd_printf_vma (abfd
, bfd_section_vma (section
));
860 bfd_printf_vma (abfd
, section
->lma
);
861 printf (" %08lx 2**%u", (unsigned long) section
->filepos
,
862 bfd_section_alignment (section
));
868 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
870 PF (SEC_HAS_CONTENTS
, "CONTENTS");
871 PF (SEC_ALLOC
, "ALLOC");
872 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
873 PF (SEC_LOAD
, "LOAD");
874 PF (SEC_RELOC
, "RELOC");
875 PF (SEC_READONLY
, "READONLY");
876 PF (SEC_CODE
, "CODE");
877 PF (SEC_DATA
, "DATA");
879 PF (SEC_DEBUGGING
, "DEBUGGING");
880 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
881 PF (SEC_EXCLUDE
, "EXCLUDE");
882 PF (SEC_SORT_ENTRIES
, "SORT_ENTRIES");
883 if (bfd_get_arch (abfd
) == bfd_arch_tic54x
)
885 PF (SEC_TIC54X_BLOCK
, "BLOCK");
886 PF (SEC_TIC54X_CLINK
, "CLINK");
888 PF (SEC_SMALL_DATA
, "SMALL_DATA");
889 if (bfd_get_flavour (abfd
) == bfd_target_coff_flavour
)
891 PF (SEC_COFF_SHARED
, "SHARED");
892 PF (SEC_COFF_NOREAD
, "NOREAD");
894 else if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
896 PF (SEC_ELF_OCTETS
, "OCTETS");
897 PF (SEC_ELF_PURECODE
, "PURECODE");
899 PF (SEC_THREAD_LOCAL
, "THREAD_LOCAL");
900 PF (SEC_GROUP
, "GROUP");
901 if (bfd_get_arch (abfd
) == bfd_arch_mep
)
903 PF (SEC_MEP_VLIW
, "VLIW");
906 if ((section
->flags
& SEC_LINK_ONCE
) != 0)
909 struct coff_comdat_info
*comdat
;
911 switch (section
->flags
& SEC_LINK_DUPLICATES
)
915 case SEC_LINK_DUPLICATES_DISCARD
:
916 ls
= "LINK_ONCE_DISCARD";
918 case SEC_LINK_DUPLICATES_ONE_ONLY
:
919 ls
= "LINK_ONCE_ONE_ONLY";
921 case SEC_LINK_DUPLICATES_SAME_SIZE
:
922 ls
= "LINK_ONCE_SAME_SIZE";
924 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
925 ls
= "LINK_ONCE_SAME_CONTENTS";
928 printf ("%s%s", comma
, ls
);
930 comdat
= bfd_coff_get_comdat_section (abfd
, section
);
932 printf (" (COMDAT %s %ld)", comdat
->name
, comdat
->symbol
);
937 if (bfd_is_section_compressed (abfd
, section
))
938 printf ("%sCOMPRESSED", comma
);
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. */
948 find_longest_section_name (bfd
*abfd ATTRIBUTE_UNUSED
,
949 asection
*section
, void *data
)
951 int *longest_so_far
= (int *) data
;
955 /* Ignore linker created section. */
956 if (section
->flags
& SEC_LINKER_CREATED
)
959 /* Skip sections that we are ignoring. */
960 if (! process_section_p (section
))
963 name
= bfd_section_name (section
);
964 len
= (int) strlen (name
);
965 if (len
> *longest_so_far
)
966 *longest_so_far
= len
;
970 dump_headers (bfd
*abfd
)
972 /* The default width of 13 is just an arbitrary choice. */
973 int max_section_name_length
= 13;
979 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
980 if (bfd_get_arch_size (abfd
) == 32)
986 printf (_("Sections:\n"));
989 bfd_map_over_sections (abfd
, find_longest_section_name
,
990 &max_section_name_length
);
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");
998 printf (_(" Flags"));
1001 bfd_map_over_sections (abfd
, dump_section_header
,
1002 &max_section_name_length
);
1006 slurp_symtab (bfd
*abfd
)
1009 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
1012 long storage
= bfd_get_symtab_upper_bound (abfd
);
1015 non_fatal (_("failed to read symbol table from: %s"),
1016 bfd_get_filename (abfd
));
1017 my_bfd_nonfatal (_("error message was"));
1023 asymbol
**sy
= (asymbol
**) xmalloc (storage
);
1024 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
1027 my_bfd_nonfatal (bfd_get_filename (abfd
));
1035 /* Read in the dynamic symbols. */
1038 slurp_dynamic_symtab (bfd
*abfd
)
1041 long storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
1044 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
1046 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd
));
1051 my_bfd_nonfatal (bfd_get_filename (abfd
));
1057 asymbol
**sy
= (asymbol
**) xmalloc (storage
);
1058 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
1059 if (dynsymcount
< 0)
1061 my_bfd_nonfatal (bfd_get_filename (abfd
));
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. */
1074 is_significant_symbol_name (const char * name
)
1076 return startswith (name
, ".plt") || startswith (name
, ".got");
1079 /* Filter out (in place) symbols that are useless for disassembly.
1080 COUNT is the number of elements in SYMBOLS.
1081 Return the number of useful symbols. */
1084 remove_useless_symbols (asymbol
**symbols
, long count
)
1086 asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
1088 while (--count
>= 0)
1090 asymbol
*sym
= *in_ptr
++;
1092 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
1094 if ((sym
->flags
& (BSF_DEBUGGING
| BSF_SECTION_SYM
))
1095 && ! is_significant_symbol_name (sym
->name
))
1097 if (bfd_is_und_section (sym
->section
)
1098 || bfd_is_com_section (sym
->section
))
1103 return out_ptr
- symbols
;
1106 static const asection
*compare_section
;
1108 /* Sort symbols into value order. */
1111 compare_symbols (const void *ap
, const void *bp
)
1113 const asymbol
*a
= * (const asymbol
**) ap
;
1114 const asymbol
*b
= * (const asymbol
**) bp
;
1119 bool as
, af
, bs
, bf
;
1123 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
1125 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
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;
1139 an
= bfd_asymbol_name (a
);
1140 bn
= bfd_asymbol_name (b
);
1144 /* The symbols gnu_compiled and gcc2_compiled convey no real
1145 information, so put them after other symbols with the same value. */
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
);
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. */
1161 #define file_symbol(s, sn, snl) \
1162 (((s)->flags & BSF_FILE) != 0 \
1164 && (sn)[(snl) - 2] == '.' \
1165 && ((sn)[(snl) - 1] == 'o' \
1166 || (sn)[(snl) - 1] == 'a')))
1168 af
= file_symbol (a
, an
, anl
);
1169 bf
= file_symbol (b
, bn
, bnl
);
1176 /* Sort function and object symbols before global symbols before
1177 local symbols before section symbols before debugging symbols. */
1182 if ((aflags
& BSF_DEBUGGING
) != (bflags
& BSF_DEBUGGING
))
1184 if ((aflags
& BSF_DEBUGGING
) != 0)
1189 if ((aflags
& BSF_SECTION_SYM
) != (bflags
& BSF_SECTION_SYM
))
1191 if ((aflags
& BSF_SECTION_SYM
) != 0)
1196 if ((aflags
& BSF_FUNCTION
) != (bflags
& BSF_FUNCTION
))
1198 if ((aflags
& BSF_FUNCTION
) != 0)
1203 if ((aflags
& BSF_OBJECT
) != (bflags
& BSF_OBJECT
))
1205 if ((aflags
& BSF_OBJECT
) != 0)
1210 if ((aflags
& BSF_LOCAL
) != (bflags
& BSF_LOCAL
))
1212 if ((aflags
& BSF_LOCAL
) != 0)
1217 if ((aflags
& BSF_GLOBAL
) != (bflags
& BSF_GLOBAL
))
1219 if ((aflags
& BSF_GLOBAL
) != 0)
1225 /* Sort larger size ELF symbols before smaller. See PR20337. */
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
;
1231 if ((b
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0
1232 && bfd_get_flavour (bfd_asymbol_bfd (b
)) == bfd_target_elf_flavour
)
1233 bsz
= ((elf_symbol_type
*) b
)->internal_elf_sym
.st_size
;
1235 return asz
> bsz
? -1 : 1;
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] != '.')
1241 if (an
[0] != '.' && bn
[0] == '.')
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
);
1249 /* Sort relocs into address order. */
1252 compare_relocs (const void *ap
, const void *bp
)
1254 const arelent
*a
= * (const arelent
**) ap
;
1255 const arelent
*b
= * (const arelent
**) bp
;
1257 if (a
->address
> b
->address
)
1259 else if (a
->address
< b
->address
)
1262 /* So that associated relocations tied to the same address show up
1263 in the correct order, we don't do any further sorting. */
1272 /* Print an address (VMA) to the output stream in INFO.
1273 If SKIP_ZEROES is TRUE, omit leading zeroes. */
1276 objdump_print_value (bfd_vma vma
, struct disassemble_info
*inf
,
1281 struct objdump_disasm_info
*aux
;
1283 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1284 bfd_sprintf_vma (aux
->abfd
, buf
, vma
);
1289 for (p
= buf
; *p
== '0'; ++p
)
1294 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_address
, "%s", p
);
1297 /* Print the name of a symbol. */
1300 objdump_print_symname (bfd
*abfd
, struct disassemble_info
*inf
,
1304 const char *name
, *version_string
= NULL
;
1305 bool hidden
= false;
1308 name
= bfd_asymbol_name (sym
);
1309 if (do_demangle
&& name
[0] != '\0')
1311 /* Demangle the name. */
1312 alloc
= bfd_demangle (abfd
, name
, demangle_flags
);
1317 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0)
1318 version_string
= bfd_get_symbol_version_string (abfd
, sym
, true,
1321 if (bfd_is_und_section (bfd_asymbol_section (sym
)))
1324 name
= sanitize_string (name
);
1328 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_symbol
, "%s", name
);
1329 if (version_string
&& *version_string
!= '\0')
1330 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_symbol
,
1331 hidden
? "@%s" : "@@%s",
1336 printf ("%s", name
);
1337 if (version_string
&& *version_string
!= '\0')
1338 printf (hidden
? "@%s" : "@@%s", version_string
);
1346 sym_ok (bool want_section
,
1347 bfd
*abfd ATTRIBUTE_UNUSED
,
1350 struct disassemble_info
*inf
)
1354 /* NB: An object file can have different sections with the same
1355 section name. Compare compare section pointers if they have
1357 if (sorted_syms
[place
]->section
->owner
== sec
->owner
1358 && sorted_syms
[place
]->section
!= sec
)
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. */
1368 if (strcmp (bfd_section_name (sorted_syms
[place
]->section
),
1369 bfd_section_name (sec
)) != 0)
1373 return inf
->symbol_is_valid (sorted_syms
[place
], inf
);
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. */
1383 find_symbol_for_address (bfd_vma vma
,
1384 struct disassemble_info
*inf
,
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. */
1392 /* Indices in `sorted_syms'. */
1394 long max_count
= sorted_symcount
;
1396 struct objdump_disasm_info
*aux
;
1403 if (sorted_symcount
< 1)
1406 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1409 opb
= inf
->octets_per_byte
;
1411 /* Perform a binary search looking for the closest symbol to the
1412 required value. We are searching the range (min, max_count]. */
1413 while (min
+ 1 < max_count
)
1417 thisplace
= (max_count
+ min
) / 2;
1418 sym
= sorted_syms
[thisplace
];
1420 if (bfd_asymbol_value (sym
) > vma
)
1421 max_count
= thisplace
;
1422 else if (bfd_asymbol_value (sym
) < vma
)
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
1433 value, we want the first one. */
1435 while (thisplace
> 0
1436 && (bfd_asymbol_value (sorted_syms
[thisplace
])
1437 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
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
1444 while (min
< max_count
1445 && (bfd_asymbol_value (sorted_syms
[min
])
1446 == bfd_asymbol_value (sorted_syms
[thisplace
])))
1448 if (sym_ok (true, abfd
, min
, sec
, inf
))
1455 return sorted_syms
[thisplace
];
1460 /* If the file is relocatable, and the symbol could be from this
1461 section, prefer a symbol from this section over symbols from
1462 others, even if the other symbol's value might be closer.
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
1469 Also give the target a chance to reject symbols. */
1470 want_section
= (aux
->require_sec
1471 || ((abfd
->flags
& HAS_RELOC
) != 0
1472 && vma
>= bfd_section_vma (sec
)
1473 && vma
< (bfd_section_vma (sec
)
1474 + bfd_section_size (sec
) / opb
)));
1476 if (! sym_ok (want_section
, abfd
, thisplace
, sec
, inf
))
1479 long newplace
= sorted_symcount
;
1481 for (i
= min
- 1; i
>= 0; i
--)
1483 if (sym_ok (want_section
, abfd
, i
, sec
, inf
))
1485 if (newplace
== sorted_symcount
)
1488 if (bfd_asymbol_value (sorted_syms
[i
])
1489 != bfd_asymbol_value (sorted_syms
[newplace
]))
1492 /* Remember this symbol and keep searching until we reach
1493 an earlier address. */
1498 if (newplace
!= sorted_symcount
)
1499 thisplace
= newplace
;
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
++)
1506 if (sym_ok (want_section
, abfd
, i
, sec
, inf
))
1514 if (! sym_ok (want_section
, abfd
, thisplace
, sec
, inf
))
1515 /* There is no suitable symbol. */
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. */
1523 rel_count
= inf
->dynrelcount
;
1525 && sorted_syms
[thisplace
]->value
!= vma
1527 && inf
->dynrelbuf
!= NULL
1528 && inf
->dynrelbuf
[0]->address
<= vma
1529 && inf
->dynrelbuf
[rel_count
- 1]->address
>= vma
1530 /* If we have matched a synthetic symbol, then stick with that. */
1531 && (sorted_syms
[thisplace
]->flags
& BSF_SYNTHETIC
) == 0)
1534 arelent
** rel_high
;
1536 rel_low
= inf
->dynrelbuf
;
1537 rel_high
= rel_low
+ rel_count
- 1;
1538 while (rel_low
<= rel_high
)
1540 arelent
**rel_mid
= &rel_low
[(rel_high
- rel_low
) / 2];
1541 arelent
* rel
= *rel_mid
;
1543 if (rel
->address
== vma
)
1545 /* Absolute relocations do not provide a more helpful
1546 symbolic address. Find a non-absolute relocation
1547 with the same address. */
1548 arelent
**rel_vma
= rel_mid
;
1550 rel_mid
>= rel_low
&& rel_mid
[0]->address
== vma
;
1554 for (; rel_vma
<= rel_high
&& rel_vma
[0]->address
== vma
;
1558 if (rel
->sym_ptr_ptr
!= NULL
1559 && ! bfd_is_abs_section ((* rel
->sym_ptr_ptr
)->section
))
1562 * place
= thisplace
;
1563 return * rel
->sym_ptr_ptr
;
1569 if (vma
< rel
->address
)
1571 else if (vma
>= rel_mid
[1]->address
)
1572 rel_low
= rel_mid
+ 1;
1581 return sorted_syms
[thisplace
];
1584 /* Print an address and the offset to the nearest symbol. */
1587 objdump_print_addr_with_sym (bfd
*abfd
, asection
*sec
, asymbol
*sym
,
1588 bfd_vma vma
, struct disassemble_info
*inf
,
1593 objdump_print_value (vma
, inf
, skip_zeroes
);
1594 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, " ");
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
)));
1604 secaddr
= bfd_section_vma (sec
);
1607 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
,
1609 objdump_print_value (secaddr
- vma
, inf
, true);
1611 else if (vma
> secaddr
)
1613 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
, "+0x");
1614 objdump_print_value (vma
- secaddr
, inf
, true);
1616 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, ">");
1620 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, "<");
1622 objdump_print_symname (abfd
, inf
, sym
);
1624 if (bfd_asymbol_value (sym
) == vma
)
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
))
1635 else if (bfd_asymbol_value (sym
) > vma
)
1637 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
,"-0x");
1638 objdump_print_value (bfd_asymbol_value (sym
) - vma
, inf
, true);
1640 else if (vma
> bfd_asymbol_value (sym
))
1642 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
, "+0x");
1643 objdump_print_value (vma
- bfd_asymbol_value (sym
), inf
, true);
1646 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, ">");
1649 if (display_file_offsets
)
1650 inf
->fprintf_styled_func (inf
->stream
, dis_style_text
,
1651 _(" (File Offset: 0x%lx)"),
1652 (long int)(sec
->filepos
+ (vma
- sec
->vma
)));
1655 /* Print an address (VMA), symbolically if possible.
1656 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
1659 objdump_print_addr (bfd_vma vma
,
1660 struct disassemble_info
*inf
,
1663 struct objdump_disasm_info
*aux
;
1664 asymbol
*sym
= NULL
;
1665 bool skip_find
= false;
1667 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1669 if (sorted_symcount
< 1)
1673 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_address
, "0x");
1674 objdump_print_value (vma
, inf
, skip_zeroes
);
1677 if (display_file_offsets
)
1678 inf
->fprintf_styled_func (inf
->stream
, dis_style_text
,
1679 _(" (File Offset: 0x%lx)"),
1680 (long int) (inf
->section
->filepos
1681 + (vma
- inf
->section
->vma
)));
1685 if (aux
->reloc
!= NULL
1686 && aux
->reloc
->sym_ptr_ptr
!= NULL
1687 && * aux
->reloc
->sym_ptr_ptr
!= NULL
)
1689 sym
= * aux
->reloc
->sym_ptr_ptr
;
1691 /* Adjust the vma to the reloc. */
1692 vma
+= bfd_asymbol_value (sym
);
1694 if (bfd_is_und_section (bfd_asymbol_section (sym
)))
1699 sym
= find_symbol_for_address (vma
, inf
, NULL
);
1701 objdump_print_addr_with_sym (aux
->abfd
, inf
->section
, sym
, vma
, inf
,
1705 /* Print VMA to INFO. This function is passed to the disassembler
1709 objdump_print_address (bfd_vma vma
, struct disassemble_info
*inf
)
1711 objdump_print_addr (vma
, inf
, ! prefix_addresses
);
1714 /* Determine if the given address has a symbol associated with it. */
1717 objdump_symbol_at_address (bfd_vma vma
, struct disassemble_info
* inf
)
1721 sym
= find_symbol_for_address (vma
, inf
, NULL
);
1722 if (sym
!= NULL
&& bfd_asymbol_value (sym
) == vma
)
1728 /* Hold the last function name and the last line number we displayed
1729 in a disassembly. */
1731 static char *prev_functionname
;
1732 static unsigned int prev_line
;
1733 static unsigned int prev_discriminator
;
1735 /* We keep a list of all files that we have seen when doing a
1736 disassembly with source, so that we know how much of the file to
1737 display. This can be important for inlined functions. */
1739 struct print_file_list
1741 struct print_file_list
*next
;
1742 const char *filename
;
1743 const char *modname
;
1746 const char **linemap
;
1749 unsigned max_printed
;
1753 static struct print_file_list
*print_files
;
1755 /* The number of preceding context lines to show when we start
1756 displaying a file for the first time. */
1758 #define SHOW_PRECEDING_CONTEXT_LINES (5)
1760 #if HAVE_LIBDEBUGINFOD
1761 /* Return a hex string represention of the build-id. */
1764 get_build_id (void * data
)
1767 char * build_id_str
;
1768 bfd
* abfd
= (bfd
*) data
;
1769 const struct bfd_build_id
* build_id
;
1771 build_id
= abfd
->build_id
;
1772 if (build_id
== NULL
)
1775 build_id_str
= malloc (build_id
->size
* 2 + 1);
1776 if (build_id_str
== NULL
)
1779 for (i
= 0; i
< build_id
->size
; i
++)
1780 sprintf (build_id_str
+ (i
* 2), "%02x", build_id
->data
[i
]);
1781 build_id_str
[build_id
->size
* 2] = '\0';
1783 return (unsigned char *) build_id_str
;
1786 /* Search for a separate debug file matching ABFD's build-id. */
1789 find_separate_debug (const bfd
* abfd
)
1791 const struct bfd_build_id
* build_id
= abfd
->build_id
;
1792 separate_info
* i
= first_separate_info
;
1794 if (build_id
== NULL
|| i
== NULL
)
1799 const bfd
* i_bfd
= (bfd
*) i
->handle
;
1801 if (abfd
!= NULL
&& i_bfd
->build_id
!= NULL
)
1803 const unsigned char * data
= i_bfd
->build_id
->data
;
1804 size_t size
= i_bfd
->build_id
->size
;
1806 if (size
== build_id
->size
1807 && memcmp (data
, build_id
->data
, size
) == 0)
1808 return (bfd
*) i
->handle
;
1817 /* Search for a separate debug file matching ABFD's .gnu_debugaltlink
1821 find_alt_debug (const bfd
* abfd
)
1826 struct dwarf_section
* section
;
1827 const struct bfd_build_id
* build_id
= abfd
->build_id
;
1828 separate_info
* i
= first_separate_info
;
1832 || !load_debug_section (gnu_debugaltlink
, (void *) abfd
))
1835 section
= &debug_displays
[gnu_debugaltlink
].section
;
1836 if (section
== NULL
)
1839 name
= (const char *) section
->start
;
1840 namelen
= strnlen (name
, section
->size
) + 1;
1843 if (namelen
>= section
->size
)
1846 id_len
= section
->size
- namelen
;
1850 /* Compare the .gnu_debugaltlink build-id with the build-ids of the
1851 known separate_info files. */
1854 const bfd
* i_bfd
= (bfd
*) i
->handle
;
1856 if (i_bfd
!= NULL
&& i_bfd
->build_id
!= NULL
)
1858 const unsigned char * data
= i_bfd
->build_id
->data
;
1859 size_t size
= i_bfd
->build_id
->size
;
1862 && memcmp (section
->start
+ namelen
, data
, size
) == 0)
1863 return (bfd
*) i
->handle
;
1872 #endif /* HAVE_LIBDEBUGINFOD */
1874 /* Reads the contents of file FN into memory. Returns a pointer to the buffer.
1875 Also returns the size of the buffer in SIZE_RETURN and a filled out
1876 stat structure in FST_RETURN. Returns NULL upon failure. */
1879 slurp_file (const char * fn
,
1880 size_t * size_return
,
1881 struct stat
* fst_return
,
1882 bfd
* abfd ATTRIBUTE_UNUSED
)
1892 if (fn
== NULL
|| * fn
== 0 || size_return
== NULL
|| fst_return
== NULL
)
1895 fd
= open (fn
, O_RDONLY
| O_BINARY
);
1897 #if HAVE_LIBDEBUGINFOD
1898 if (fd
< 0 && use_debuginfod
&& fn
[0] == '/' && abfd
!= NULL
)
1900 unsigned char *build_id
= get_build_id (abfd
);
1904 debuginfod_client
*client
= debuginfod_begin ();
1908 fd
= debuginfod_find_source (client
, build_id
, 0, fn
, NULL
);
1909 debuginfod_end (client
);
1919 if (fstat (fd
, fst_return
) < 0)
1925 *size_return
= fst_return
->st_size
;
1928 ps
= getpagesize ();
1929 msize
= (*size_return
+ ps
- 1) & ~(ps
- 1);
1930 map
= mmap (NULL
, msize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1931 if (map
!= (char *) -1L)
1938 map
= (const char *) malloc (*size_return
);
1939 if (!map
|| (size_t) read (fd
, (char *) map
, *size_return
) != *size_return
)
1941 free ((void *) map
);
1948 #define line_map_decrease 5
1950 /* Precompute array of lines for a mapped file. */
1952 static const char **
1953 index_file (const char *map
, size_t size
, unsigned int *maxline
)
1955 const char *p
, *lstart
, *end
;
1956 int chars_per_line
= 45; /* First iteration will use 40. */
1957 unsigned int lineno
;
1958 const char **linemap
= NULL
;
1959 unsigned long line_map_size
= 0;
1965 for (p
= map
; p
< end
; p
++)
1969 if (p
+ 1 < end
&& p
[1] == '\r')
1972 else if (*p
== '\r')
1974 if (p
+ 1 < end
&& p
[1] == '\n')
1980 /* End of line found. */
1982 if (linemap
== NULL
|| line_map_size
< lineno
+ 1)
1984 unsigned long newsize
;
1986 chars_per_line
-= line_map_decrease
;
1987 if (chars_per_line
<= 1)
1989 line_map_size
= size
/ chars_per_line
+ 1;
1990 if (line_map_size
< lineno
+ 1)
1991 line_map_size
= lineno
+ 1;
1992 newsize
= line_map_size
* sizeof (char *);
1993 linemap
= (const char **) xrealloc (linemap
, newsize
);
1996 linemap
[lineno
++] = lstart
;
2004 /* Tries to open MODNAME, and if successful adds a node to print_files
2005 linked list and returns that node. Also fills in the stat structure
2006 pointed to by FST_RETURN. Returns NULL on failure. */
2008 static struct print_file_list
*
2009 try_print_file_open (const char * origname
,
2010 const char * modname
,
2011 struct stat
* fst_return
,
2014 struct print_file_list
*p
;
2016 p
= (struct print_file_list
*) xmalloc (sizeof (struct print_file_list
));
2018 p
->map
= slurp_file (modname
, &p
->mapsize
, fst_return
, abfd
);
2025 p
->linemap
= index_file (p
->map
, p
->mapsize
, &p
->maxline
);
2028 p
->filename
= origname
;
2029 p
->modname
= modname
;
2030 p
->next
= print_files
;
2036 /* If the source file, as described in the symtab, is not found
2037 try to locate it in one of the paths specified with -I
2038 If found, add location to print_files linked list. */
2040 static struct print_file_list
*
2041 update_source_path (const char *filename
, bfd
*abfd
)
2043 struct print_file_list
*p
;
2048 p
= try_print_file_open (filename
, filename
, &fst
, abfd
);
2051 if (include_path_count
== 0)
2054 /* Get the name of the file. */
2055 fname
= lbasename (filename
);
2057 /* If file exists under a new path, we need to add it to the list
2058 so that show_line knows about it. */
2059 for (i
= 0; i
< include_path_count
; i
++)
2061 char *modname
= concat (include_paths
[i
], "/", fname
,
2064 p
= try_print_file_open (filename
, modname
, &fst
, abfd
);
2074 long mtime
= bfd_get_mtime (abfd
);
2076 if (fst
.st_mtime
> mtime
)
2077 warn (_("source file %s is more recent than object file\n"),
2084 /* Print a source file line. */
2087 print_line (struct print_file_list
*p
, unsigned int linenum
)
2092 if (linenum
>= p
->maxline
)
2094 l
= p
->linemap
[linenum
];
2095 if (source_comment
!= NULL
&& strlen (l
) > 0)
2096 printf ("%s", source_comment
);
2097 len
= strcspn (l
, "\n\r");
2098 /* Test fwrite return value to quiet glibc warning. */
2099 if (len
== 0 || fwrite (l
, len
, 1, stdout
) == 1)
2103 /* Print a range of source code lines. */
2106 dump_lines (struct print_file_list
*p
, unsigned int start
, unsigned int end
)
2114 print_line (p
, start
);
2119 /* Show the line number, or the source line, in a disassembly
2123 show_line (bfd
*abfd
, asection
*section
, bfd_vma addr_offset
)
2125 const char *filename
;
2126 const char *functionname
;
2127 unsigned int linenumber
;
2128 unsigned int discriminator
;
2132 if (! with_line_numbers
&& ! with_source_code
)
2135 #ifdef HAVE_LIBDEBUGINFOD
2138 const char *alt_filename
= NULL
;
2144 /* PR 29075: Check for separate debuginfo and .gnu_debugaltlink files.
2145 They need to be passed to bfd_find_nearest_line_with_alt in case they
2146 were downloaded from debuginfod. Otherwise libbfd will attempt to
2147 search for them and fail to locate them. */
2148 debug_bfd
= find_separate_debug (abfd
);
2149 if (debug_bfd
== NULL
)
2152 alt_bfd
= find_alt_debug (debug_bfd
);
2153 if (alt_bfd
!= NULL
)
2154 alt_filename
= bfd_get_filename (alt_bfd
);
2159 bfd_set_error (bfd_error_no_error
);
2160 if (! bfd_find_nearest_line_with_alt (debug_bfd
, alt_filename
,
2162 addr_offset
, &filename
,
2163 &functionname
, &linenumber
,
2166 if (bfd_get_error () == bfd_error_no_error
)
2168 if (! bfd_find_nearest_line_discriminator (abfd
, section
, syms
,
2169 addr_offset
, &filename
,
2170 &functionname
, &linenumber
,
2176 if (! bfd_find_nearest_line_discriminator (abfd
, section
, syms
, addr_offset
,
2177 &filename
, &functionname
,
2178 &linenumber
, &discriminator
))
2182 if (filename
!= NULL
&& *filename
== '\0')
2184 if (functionname
!= NULL
&& *functionname
== '\0')
2185 functionname
= NULL
;
2188 && IS_ABSOLUTE_PATH (filename
)
2192 const char *fname
= filename
;
2194 path
= xmalloc (prefix_length
+ 1 + strlen (filename
));
2197 memcpy (path
, prefix
, prefix_length
);
2198 path_up
= path
+ prefix_length
;
2200 /* Build relocated filename, stripping off leading directories
2201 from the initial filename if requested. */
2202 if (prefix_strip
> 0)
2207 /* Skip selected directory levels. */
2208 for (s
= fname
+ 1; *s
!= '\0' && level
< prefix_strip
; s
++)
2209 if (IS_DIR_SEPARATOR (*s
))
2216 /* Update complete filename. */
2217 strcpy (path_up
, fname
);
2225 if (with_line_numbers
)
2227 if (functionname
!= NULL
2228 && (prev_functionname
== NULL
2229 || strcmp (functionname
, prev_functionname
) != 0))
2231 char *demangle_alloc
= NULL
;
2232 if (do_demangle
&& functionname
[0] != '\0')
2234 /* Demangle the name. */
2235 demangle_alloc
= bfd_demangle (abfd
, functionname
,
2239 /* Demangling adds trailing parens, so don't print those. */
2240 if (demangle_alloc
!= NULL
)
2241 printf ("%s:\n", sanitize_string (demangle_alloc
));
2243 printf ("%s():\n", sanitize_string (functionname
));
2246 free (demangle_alloc
);
2249 && (linenumber
!= prev_line
2250 || discriminator
!= prev_discriminator
))
2252 if (discriminator
> 0)
2253 printf ("%s:%u (discriminator %u)\n",
2254 filename
== NULL
? "???" : sanitize_string (filename
),
2255 linenumber
, discriminator
);
2257 printf ("%s:%u\n", filename
== NULL
2258 ? "???" : sanitize_string (filename
),
2263 const char *filename2
;
2264 const char *functionname2
;
2267 while (bfd_find_inliner_info (abfd
, &filename2
, &functionname2
,
2270 printf ("inlined by %s:%u",
2271 sanitize_string (filename2
), line2
);
2272 printf (" (%s)\n", sanitize_string (functionname2
));
2277 if (with_source_code
2281 struct print_file_list
**pp
, *p
;
2284 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
2285 if (filename_cmp ((*pp
)->filename
, filename
) == 0)
2292 filename
= xstrdup (filename
);
2293 p
= update_source_path (filename
, abfd
);
2296 if (p
!= NULL
&& linenumber
!= p
->last_line
)
2298 if (file_start_context
&& p
->first
)
2302 l
= linenumber
- SHOW_PRECEDING_CONTEXT_LINES
;
2303 if (l
>= linenumber
)
2305 if (p
->max_printed
>= l
)
2307 if (p
->max_printed
< linenumber
)
2308 l
= p
->max_printed
+ 1;
2313 dump_lines (p
, l
, linenumber
);
2314 if (p
->max_printed
< linenumber
)
2315 p
->max_printed
= linenumber
;
2316 p
->last_line
= linenumber
;
2321 if (functionname
!= NULL
2322 && (prev_functionname
== NULL
2323 || strcmp (functionname
, prev_functionname
) != 0))
2325 if (prev_functionname
!= NULL
)
2326 free (prev_functionname
);
2327 prev_functionname
= (char *) xmalloc (strlen (functionname
) + 1);
2328 strcpy (prev_functionname
, functionname
);
2331 if (linenumber
> 0 && linenumber
!= prev_line
)
2332 prev_line
= linenumber
;
2334 if (discriminator
!= prev_discriminator
)
2335 prev_discriminator
= discriminator
;
2341 /* Pseudo FILE object for strings. */
2349 /* sprintf to a "stream". */
2351 static int ATTRIBUTE_PRINTF_2
2352 objdump_sprintf (SFILE
*f
, const char *format
, ...)
2359 size_t space
= f
->alloc
- f
->pos
;
2361 va_start (args
, format
);
2362 n
= vsnprintf (f
->buffer
+ f
->pos
, space
, format
, args
);
2368 f
->alloc
= (f
->alloc
+ n
) * 2;
2369 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2376 /* Return an integer greater than, or equal to zero, representing the color
2377 for STYLE, or -1 if no color should be used. */
2380 objdump_color_for_disassembler_style (enum disassembler_style style
)
2384 if (style
== dis_style_comment_start
)
2385 disassembler_in_comment
= true;
2387 if (disassembler_color
== on
)
2389 if (disassembler_in_comment
)
2394 case dis_style_symbol
:
2397 case dis_style_assembler_directive
:
2398 case dis_style_sub_mnemonic
:
2399 case dis_style_mnemonic
:
2402 case dis_style_register
:
2405 case dis_style_address
:
2406 case dis_style_address_offset
:
2407 case dis_style_immediate
:
2411 case dis_style_text
:
2416 else if (disassembler_color
== extended
)
2418 if (disassembler_in_comment
)
2423 case dis_style_symbol
:
2426 case dis_style_assembler_directive
:
2427 case dis_style_sub_mnemonic
:
2428 case dis_style_mnemonic
:
2431 case dis_style_register
:
2434 case dis_style_address
:
2435 case dis_style_address_offset
:
2436 case dis_style_immediate
:
2440 case dis_style_text
:
2445 else if (disassembler_color
!= off
)
2446 bfd_fatal (_("disassembly color not correctly selected"));
2451 /* Like objdump_sprintf, but add in escape sequences to highlight the
2452 content according to STYLE. */
2454 static int ATTRIBUTE_PRINTF_3
2455 objdump_styled_sprintf (SFILE
*f
, enum disassembler_style style
,
2456 const char *format
, ...)
2460 int color
= objdump_color_for_disassembler_style (style
);
2466 size_t space
= f
->alloc
- f
->pos
;
2468 if (disassembler_color
== on
)
2469 n
= snprintf (f
->buffer
+ f
->pos
, space
, "\033[%dm", color
);
2471 n
= snprintf (f
->buffer
+ f
->pos
, space
, "\033[38;5;%dm", color
);
2475 f
->alloc
= (f
->alloc
+ n
) * 2;
2476 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2483 size_t space
= f
->alloc
- f
->pos
;
2485 va_start (args
, format
);
2486 n
= vsnprintf (f
->buffer
+ f
->pos
, space
, format
, args
);
2492 f
->alloc
= (f
->alloc
+ n
) * 2;
2493 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2501 size_t space
= f
->alloc
- f
->pos
;
2503 n
= snprintf (f
->buffer
+ f
->pos
, space
, "\033[0m");
2508 f
->alloc
= (f
->alloc
+ n
) * 2;
2509 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2517 /* We discard the styling information here. This function is only used
2518 when objdump is printing auxiliary information, the symbol headers, and
2519 disassembly address, or the bytes of the disassembled instruction. We
2520 don't (currently) apply styling to any of this stuff, so, for now, just
2521 print the content with no additional style added. */
2523 static int ATTRIBUTE_PRINTF_3
2524 fprintf_styled (FILE *f
, enum disassembler_style style ATTRIBUTE_UNUSED
,
2525 const char *fmt
, ...)
2531 res
= vfprintf (f
, fmt
, ap
);
2537 /* Code for generating (colored) diagrams of control flow start and end
2540 /* Structure used to store the properties of a jump. */
2544 /* The next jump, or NULL if this is the last object. */
2545 struct jump_info
*next
;
2546 /* The previous jump, or NULL if this is the first object. */
2547 struct jump_info
*prev
;
2548 /* The start addresses of the jump. */
2551 /* The list of start addresses. */
2553 /* The number of elements. */
2555 /* The maximum number of elements that fit into the array. */
2558 /* The end address of the jump. */
2560 /* The drawing level of the jump. */
2564 /* Construct a jump object for a jump from start
2565 to end with the corresponding level. */
2567 static struct jump_info
*
2568 jump_info_new (bfd_vma start
, bfd_vma end
, int level
)
2570 struct jump_info
*result
= xmalloc (sizeof (struct jump_info
));
2572 result
->next
= NULL
;
2573 result
->prev
= NULL
;
2574 result
->start
.addresses
= xmalloc (sizeof (bfd_vma
*) * 2);
2575 result
->start
.addresses
[0] = start
;
2576 result
->start
.count
= 1;
2577 result
->start
.max_count
= 2;
2579 result
->level
= level
;
2584 /* Free a jump object and return the next object
2585 or NULL if this was the last one. */
2587 static struct jump_info
*
2588 jump_info_free (struct jump_info
*ji
)
2590 struct jump_info
*result
= NULL
;
2595 if (ji
->start
.addresses
)
2596 free (ji
->start
.addresses
);
2603 /* Get the smallest value of all start and end addresses. */
2606 jump_info_min_address (const struct jump_info
*ji
)
2608 bfd_vma min_address
= ji
->end
;
2611 for (i
= ji
->start
.count
; i
-- > 0;)
2612 if (ji
->start
.addresses
[i
] < min_address
)
2613 min_address
= ji
->start
.addresses
[i
];
2617 /* Get the largest value of all start and end addresses. */
2620 jump_info_max_address (const struct jump_info
*ji
)
2622 bfd_vma max_address
= ji
->end
;
2625 for (i
= ji
->start
.count
; i
-- > 0;)
2626 if (ji
->start
.addresses
[i
] > max_address
)
2627 max_address
= ji
->start
.addresses
[i
];
2631 /* Get the target address of a jump. */
2634 jump_info_end_address (const struct jump_info
*ji
)
2639 /* Test if an address is one of the start addresses of a jump. */
2642 jump_info_is_start_address (const struct jump_info
*ji
, bfd_vma address
)
2644 bool result
= false;
2647 for (i
= ji
->start
.count
; i
-- > 0;)
2648 if (address
== ji
->start
.addresses
[i
])
2657 /* Test if an address is the target address of a jump. */
2660 jump_info_is_end_address (const struct jump_info
*ji
, bfd_vma address
)
2662 return (address
== ji
->end
);
2665 /* Get the difference between the smallest and largest address of a jump. */
2668 jump_info_size (const struct jump_info
*ji
)
2670 return jump_info_max_address (ji
) - jump_info_min_address (ji
);
2673 /* Unlink a jump object from a list. */
2676 jump_info_unlink (struct jump_info
*node
,
2677 struct jump_info
**base
)
2680 node
->next
->prev
= node
->prev
;
2682 node
->prev
->next
= node
->next
;
2689 /* Insert unlinked jump info node into a list. */
2692 jump_info_insert (struct jump_info
*node
,
2693 struct jump_info
*target
,
2694 struct jump_info
**base
)
2696 node
->next
= target
;
2697 node
->prev
= target
->prev
;
2698 target
->prev
= node
;
2700 node
->prev
->next
= node
;
2705 /* Add unlinked node to the front of a list. */
2708 jump_info_add_front (struct jump_info
*node
,
2709 struct jump_info
**base
)
2713 node
->next
->prev
= node
;
2718 /* Move linked node to target position. */
2721 jump_info_move_linked (struct jump_info
*node
,
2722 struct jump_info
*target
,
2723 struct jump_info
**base
)
2726 jump_info_unlink (node
, base
);
2727 /* Insert node at target position. */
2728 jump_info_insert (node
, target
, base
);
2731 /* Test if two jumps intersect. */
2734 jump_info_intersect (const struct jump_info
*a
,
2735 const struct jump_info
*b
)
2737 return ((jump_info_max_address (a
) >= jump_info_min_address (b
))
2738 && (jump_info_min_address (a
) <= jump_info_max_address (b
)));
2741 /* Merge two compatible jump info objects. */
2744 jump_info_merge (struct jump_info
**base
)
2746 struct jump_info
*a
;
2748 for (a
= *base
; a
; a
= a
->next
)
2750 struct jump_info
*b
;
2752 for (b
= a
->next
; b
; b
= b
->next
)
2754 /* Merge both jumps into one. */
2755 if (a
->end
== b
->end
)
2757 /* Reallocate addresses. */
2758 size_t needed_size
= a
->start
.count
+ b
->start
.count
;
2761 if (needed_size
> a
->start
.max_count
)
2763 a
->start
.max_count
+= b
->start
.max_count
;
2764 a
->start
.addresses
=
2765 xrealloc (a
->start
.addresses
,
2766 a
->start
.max_count
* sizeof (bfd_vma
*));
2769 /* Append start addresses. */
2770 for (i
= 0; i
< b
->start
.count
; ++i
)
2771 a
->start
.addresses
[a
->start
.count
++] =
2772 b
->start
.addresses
[i
];
2774 /* Remove and delete jump. */
2775 struct jump_info
*tmp
= b
->prev
;
2776 jump_info_unlink (b
, base
);
2784 /* Sort jumps by their size and starting point using a stable
2785 minsort. This could be improved if sorting performance is
2786 an issue, for example by using mergesort. */
2789 jump_info_sort (struct jump_info
**base
)
2791 struct jump_info
*current_element
= *base
;
2793 while (current_element
)
2795 struct jump_info
*best_match
= current_element
;
2796 struct jump_info
*runner
= current_element
->next
;
2797 bfd_vma best_size
= jump_info_size (best_match
);
2801 bfd_vma runner_size
= jump_info_size (runner
);
2803 if ((runner_size
< best_size
)
2804 || ((runner_size
== best_size
)
2805 && (jump_info_min_address (runner
)
2806 < jump_info_min_address (best_match
))))
2808 best_match
= runner
;
2809 best_size
= runner_size
;
2812 runner
= runner
->next
;
2815 if (best_match
== current_element
)
2816 current_element
= current_element
->next
;
2818 jump_info_move_linked (best_match
, current_element
, base
);
2822 /* Visualize all jumps at a given address. */
2825 jump_info_visualize_address (bfd_vma address
,
2828 uint8_t *color_buffer
)
2830 struct jump_info
*ji
= detected_jumps
;
2831 size_t len
= (max_level
+ 1) * 3;
2833 /* Clear line buffer. */
2834 memset (line_buffer
, ' ', len
);
2835 memset (color_buffer
, 0, len
);
2837 /* Iterate over jumps and add their ASCII art. */
2840 /* Discard jumps that are never needed again. */
2841 if (jump_info_max_address (ji
) < address
)
2843 struct jump_info
*tmp
= ji
;
2846 jump_info_unlink (tmp
, &detected_jumps
);
2847 jump_info_free (tmp
);
2851 /* This jump intersects with the current address. */
2852 if (jump_info_min_address (ji
) <= address
)
2854 /* Hash target address to get an even
2855 distribution between all values. */
2856 bfd_vma hash_address
= jump_info_end_address (ji
);
2857 uint8_t color
= iterative_hash_object (hash_address
, 0);
2858 /* Fetch line offset. */
2859 int offset
= (max_level
- ji
->level
) * 3;
2861 /* Draw start line. */
2862 if (jump_info_is_start_address (ji
, address
))
2864 size_t i
= offset
+ 1;
2866 for (; i
< len
- 1; ++i
)
2867 if (line_buffer
[i
] == ' ')
2869 line_buffer
[i
] = '-';
2870 color_buffer
[i
] = color
;
2873 if (line_buffer
[i
] == ' ')
2875 line_buffer
[i
] = '-';
2876 color_buffer
[i
] = color
;
2878 else if (line_buffer
[i
] == '>')
2880 line_buffer
[i
] = 'X';
2881 color_buffer
[i
] = color
;
2884 if (line_buffer
[offset
] == ' ')
2886 if (address
<= ji
->end
)
2887 line_buffer
[offset
] =
2888 (jump_info_min_address (ji
) == address
) ? '/': '+';
2890 line_buffer
[offset
] =
2891 (jump_info_max_address (ji
) == address
) ? '\\': '+';
2892 color_buffer
[offset
] = color
;
2895 /* Draw jump target. */
2896 else if (jump_info_is_end_address (ji
, address
))
2898 size_t i
= offset
+ 1;
2900 for (; i
< len
- 1; ++i
)
2901 if (line_buffer
[i
] == ' ')
2903 line_buffer
[i
] = '-';
2904 color_buffer
[i
] = color
;
2907 if (line_buffer
[i
] == ' ')
2909 line_buffer
[i
] = '>';
2910 color_buffer
[i
] = color
;
2912 else if (line_buffer
[i
] == '-')
2914 line_buffer
[i
] = 'X';
2915 color_buffer
[i
] = color
;
2918 if (line_buffer
[offset
] == ' ')
2920 if (jump_info_min_address (ji
) < address
)
2921 line_buffer
[offset
] =
2922 (jump_info_max_address (ji
) > address
) ? '>' : '\\';
2924 line_buffer
[offset
] = '/';
2925 color_buffer
[offset
] = color
;
2928 /* Draw intermediate line segment. */
2929 else if (line_buffer
[offset
] == ' ')
2931 line_buffer
[offset
] = '|';
2932 color_buffer
[offset
] = color
;
2940 /* Clone of disassemble_bytes to detect jumps inside a function. */
2941 /* FIXME: is this correct? Can we strip it down even further? */
2943 static struct jump_info
*
2944 disassemble_jumps (struct disassemble_info
* inf
,
2945 disassembler_ftype disassemble_fn
,
2946 bfd_vma start_offset
,
2947 bfd_vma stop_offset
,
2950 arelent
** relppend
)
2952 struct objdump_disasm_info
*aux
;
2953 struct jump_info
*jumps
= NULL
;
2955 bfd_vma addr_offset
;
2956 unsigned int opb
= inf
->octets_per_byte
;
2960 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
2961 section
= inf
->section
;
2964 sfile
.buffer
= (char *) xmalloc (sfile
.alloc
);
2967 inf
->insn_info_valid
= 0;
2968 disassemble_set_printf (inf
, &sfile
, (fprintf_ftype
) objdump_sprintf
,
2969 (fprintf_styled_ftype
) objdump_styled_sprintf
);
2971 addr_offset
= start_offset
;
2972 while (addr_offset
< stop_offset
)
2974 int previous_octets
;
2976 /* Remember the length of the previous instruction. */
2977 previous_octets
= octets
;
2981 inf
->bytes_per_line
= 0;
2982 inf
->bytes_per_chunk
= 0;
2983 inf
->flags
= ((disassemble_all
? DISASSEMBLE_DATA
: 0)
2984 | (wide_output
? WIDE_OUTPUT
: 0));
2986 inf
->flags
|= USER_SPECIFIED_MACHINE_TYPE
;
2988 if (inf
->disassembler_needs_relocs
2989 && (bfd_get_file_flags (aux
->abfd
) & EXEC_P
) == 0
2990 && (bfd_get_file_flags (aux
->abfd
) & DYNAMIC
) == 0
2991 && *relppp
< relppend
)
2993 bfd_signed_vma distance_to_rel
;
2995 distance_to_rel
= (**relppp
)->address
- (rel_offset
+ addr_offset
);
2997 /* Check to see if the current reloc is associated with
2998 the instruction that we are about to disassemble. */
2999 if (distance_to_rel
== 0
3000 /* FIXME: This is wrong. We are trying to catch
3001 relocs that are addressed part way through the
3002 current instruction, as might happen with a packed
3003 VLIW instruction. Unfortunately we do not know the
3004 length of the current instruction since we have not
3005 disassembled it yet. Instead we take a guess based
3006 upon the length of the previous instruction. The
3007 proper solution is to have a new target-specific
3008 disassembler function which just returns the length
3009 of an instruction at a given address without trying
3010 to display its disassembly. */
3011 || (distance_to_rel
> 0
3012 && distance_to_rel
< (bfd_signed_vma
) (previous_octets
/ opb
)))
3014 inf
->flags
|= INSN_HAS_RELOC
;
3018 if (! disassemble_all
3019 && (section
->flags
& (SEC_CODE
| SEC_HAS_CONTENTS
))
3020 == (SEC_CODE
| SEC_HAS_CONTENTS
))
3021 /* Set a stop_vma so that the disassembler will not read
3022 beyond the next symbol. We assume that symbols appear on
3023 the boundaries between instructions. We only do this when
3024 disassembling code of course, and when -D is in effect. */
3025 inf
->stop_vma
= section
->vma
+ stop_offset
;
3027 inf
->stop_offset
= stop_offset
;
3029 /* Extract jump information. */
3030 inf
->insn_info_valid
= 0;
3031 disassembler_in_comment
= false;
3032 octets
= (*disassemble_fn
) (section
->vma
+ addr_offset
, inf
);
3033 /* Test if a jump was detected. */
3034 if (inf
->insn_info_valid
3035 && ((inf
->insn_type
== dis_branch
)
3036 || (inf
->insn_type
== dis_condbranch
)
3037 || (inf
->insn_type
== dis_jsr
)
3038 || (inf
->insn_type
== dis_condjsr
))
3039 && (inf
->target
>= section
->vma
+ start_offset
)
3040 && (inf
->target
< section
->vma
+ stop_offset
))
3042 struct jump_info
*ji
=
3043 jump_info_new (section
->vma
+ addr_offset
, inf
->target
, -1);
3044 jump_info_add_front (ji
, &jumps
);
3049 addr_offset
+= octets
/ opb
;
3052 disassemble_set_printf (inf
, (void *) stdout
, (fprintf_ftype
) fprintf
,
3053 (fprintf_styled_ftype
) fprintf_styled
);
3054 free (sfile
.buffer
);
3057 jump_info_merge (&jumps
);
3058 /* Process jumps. */
3059 jump_info_sort (&jumps
);
3061 /* Group jumps by level. */
3062 struct jump_info
*last_jump
= jumps
;
3067 /* The last jump is part of the next group. */
3068 struct jump_info
*base
= last_jump
;
3069 /* Increment level. */
3070 base
->level
= ++max_level
;
3072 /* Find jumps that can be combined on the same
3073 level, with the largest jumps tested first.
3074 This has the advantage that large jumps are on
3075 lower levels and do not intersect with small
3076 jumps that get grouped on higher levels. */
3077 struct jump_info
*exchange_item
= last_jump
->next
;
3078 struct jump_info
*it
= exchange_item
;
3080 for (; it
; it
= it
->next
)
3082 /* Test if the jump intersects with any
3083 jump from current group. */
3085 struct jump_info
*it_collision
;
3087 for (it_collision
= base
;
3088 it_collision
!= exchange_item
;
3089 it_collision
= it_collision
->next
)
3091 /* This jump intersects so we leave it out. */
3092 if (jump_info_intersect (it_collision
, it
))
3099 /* Add jump to group. */
3102 /* Move current element to the front. */
3103 if (it
!= exchange_item
)
3105 struct jump_info
*save
= it
->prev
;
3106 jump_info_move_linked (it
, exchange_item
, &jumps
);
3112 last_jump
= exchange_item
;
3113 exchange_item
= exchange_item
->next
;
3115 last_jump
->level
= max_level
;
3119 /* Move to next group. */
3120 last_jump
= exchange_item
;
3126 /* The number of zeroes we want to see before we start skipping them.
3127 The number is arbitrarily chosen. */
3129 #define DEFAULT_SKIP_ZEROES 8
3131 /* The number of zeroes to skip at the end of a section. If the
3132 number of zeroes at the end is between SKIP_ZEROES_AT_END and
3133 SKIP_ZEROES, they will be disassembled. If there are fewer than
3134 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
3135 attempt to avoid disassembling zeroes inserted by section
3138 #define DEFAULT_SKIP_ZEROES_AT_END 3
3141 null_print (const void * stream ATTRIBUTE_UNUSED
, const char * format ATTRIBUTE_UNUSED
, ...)
3146 /* Like null_print, but takes the extra STYLE argument. As this is not
3147 going to print anything, the extra argument is just ignored. */
3150 null_styled_print (const void * stream ATTRIBUTE_UNUSED
,
3151 enum disassembler_style style ATTRIBUTE_UNUSED
,
3152 const char * format ATTRIBUTE_UNUSED
, ...)
3157 /* Print out jump visualization. */
3160 print_jump_visualisation (bfd_vma addr
, int max_level
, char *line_buffer
,
3161 uint8_t *color_buffer
)
3166 jump_info_visualize_address (addr
, max_level
, line_buffer
, color_buffer
);
3168 size_t line_buffer_size
= strlen (line_buffer
);
3169 char last_color
= 0;
3172 for (i
= 0; i
<= line_buffer_size
; ++i
)
3176 uint8_t color
= (i
< line_buffer_size
) ? color_buffer
[i
]: 0;
3178 if (color
!= last_color
)
3181 if (extended_color_output
)
3182 /* Use extended 8bit color, but
3183 do not choose dark colors. */
3184 printf ("\033[38;5;%dm", 124 + (color
% 108));
3186 /* Use simple terminal colors. */
3187 printf ("\033[%dm", 31 + (color
% 7));
3194 putchar ((i
< line_buffer_size
) ? line_buffer
[i
]: ' ');
3198 /* Disassemble some data in memory between given values. */
3201 disassemble_bytes (struct disassemble_info
*inf
,
3202 disassembler_ftype disassemble_fn
,
3205 bfd_vma start_offset
,
3206 bfd_vma stop_offset
,
3211 struct objdump_disasm_info
*aux
;
3213 unsigned int octets_per_line
;
3214 unsigned int skip_addr_chars
;
3215 bfd_vma addr_offset
;
3216 unsigned int opb
= inf
->octets_per_byte
;
3217 unsigned int skip_zeroes
= inf
->skip_zeroes
;
3218 unsigned int skip_zeroes_at_end
= inf
->skip_zeroes_at_end
;
3222 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
3223 section
= inf
->section
;
3226 sfile
.buffer
= (char *) xmalloc (sfile
.alloc
);
3230 octets_per_line
= insn_width
;
3232 octets_per_line
= 4;
3234 octets_per_line
= 16;
3236 /* Figure out how many characters to skip at the start of an
3237 address, to make the disassembly look nicer. We discard leading
3238 zeroes in chunks of 4, ensuring that there is always a leading
3240 skip_addr_chars
= 0;
3241 if (!no_addresses
&& !prefix_addresses
)
3245 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ section
->size
/ opb
);
3247 while (buf
[skip_addr_chars
] == '0')
3250 /* Don't discard zeros on overflow. */
3251 if (buf
[skip_addr_chars
] == '\0' && section
->vma
!= 0)
3252 skip_addr_chars
= 0;
3254 if (skip_addr_chars
!= 0)
3255 skip_addr_chars
= (skip_addr_chars
- 1) & -4;
3258 inf
->insn_info_valid
= 0;
3260 /* Determine maximum level. */
3261 uint8_t *color_buffer
= NULL
;
3262 char *line_buffer
= NULL
;
3265 /* Some jumps were detected. */
3268 struct jump_info
*ji
;
3270 /* Find maximum jump level. */
3271 for (ji
= detected_jumps
; ji
; ji
= ji
->next
)
3273 if (ji
->level
> max_level
)
3274 max_level
= ji
->level
;
3277 /* Allocate buffers. */
3278 size_t len
= (max_level
+ 1) * 3 + 1;
3279 line_buffer
= xmalloc (len
);
3280 line_buffer
[len
- 1] = 0;
3281 color_buffer
= xmalloc (len
);
3282 color_buffer
[len
- 1] = 0;
3285 addr_offset
= start_offset
;
3286 while (addr_offset
< stop_offset
)
3288 bool need_nl
= false;
3292 /* Make sure we don't use relocs from previous instructions. */
3295 /* If we see more than SKIP_ZEROES octets of zeroes, we just
3297 if (! disassemble_zeroes
)
3298 for (; addr_offset
* opb
+ octets
< stop_offset
* opb
; octets
++)
3299 if (data
[addr_offset
* opb
+ octets
] != 0)
3301 if (! disassemble_zeroes
3302 && (inf
->insn_info_valid
== 0
3303 || inf
->branch_delay_insns
== 0)
3304 && (octets
>= skip_zeroes
3305 || (addr_offset
* opb
+ octets
== stop_offset
* opb
3306 && octets
< skip_zeroes_at_end
)))
3308 /* If there are more nonzero octets to follow, we only skip
3309 zeroes in multiples of 4, to try to avoid running over
3310 the start of an instruction which happens to start with
3312 if (addr_offset
* opb
+ octets
!= stop_offset
* opb
)
3315 /* If we are going to display more data, and we are displaying
3316 file offsets, then tell the user how many zeroes we skip
3317 and the file offset from where we resume dumping. */
3318 if (display_file_offsets
3319 && addr_offset
+ octets
/ opb
< stop_offset
)
3320 printf (_("\t... (skipping %lu zeroes, "
3321 "resuming at file offset: 0x%lx)\n"),
3322 (unsigned long) (octets
/ opb
),
3323 (unsigned long) (section
->filepos
3324 + addr_offset
+ octets
/ opb
));
3331 unsigned int bpc
= 0;
3332 unsigned int pb
= 0;
3334 if (with_line_numbers
|| with_source_code
)
3335 show_line (aux
->abfd
, section
, addr_offset
);
3339 else if (!prefix_addresses
)
3343 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ addr_offset
);
3344 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
3348 printf ("%s:\t", buf
+ skip_addr_chars
);
3352 aux
->require_sec
= true;
3353 objdump_print_address (section
->vma
+ addr_offset
, inf
);
3354 aux
->require_sec
= false;
3358 print_jump_visualisation (section
->vma
+ addr_offset
,
3359 max_level
, line_buffer
,
3367 disassemble_set_printf
3368 (inf
, &sfile
, (fprintf_ftype
) objdump_sprintf
,
3369 (fprintf_styled_ftype
) objdump_styled_sprintf
);
3370 inf
->bytes_per_line
= 0;
3371 inf
->bytes_per_chunk
= 0;
3372 inf
->flags
= ((disassemble_all
? DISASSEMBLE_DATA
: 0)
3373 | (wide_output
? WIDE_OUTPUT
: 0));
3375 inf
->flags
|= USER_SPECIFIED_MACHINE_TYPE
;
3377 if (inf
->disassembler_needs_relocs
3378 && (bfd_get_file_flags (aux
->abfd
) & EXEC_P
) == 0
3379 && (bfd_get_file_flags (aux
->abfd
) & DYNAMIC
) == 0
3380 && *relppp
< relppend
)
3382 bfd_signed_vma distance_to_rel
;
3383 int max_reloc_offset
3384 = aux
->abfd
->arch_info
->max_reloc_offset_into_insn
;
3386 distance_to_rel
= ((**relppp
)->address
- rel_offset
3390 if (distance_to_rel
> 0
3391 && (max_reloc_offset
< 0
3392 || distance_to_rel
<= max_reloc_offset
))
3394 /* This reloc *might* apply to the current insn,
3395 starting somewhere inside it. Discover the length
3396 of the current insn so that the check below will
3399 insn_size
= insn_width
;
3402 /* We find the length by calling the dissassembler
3403 function with a dummy print handler. This should
3404 work unless the disassembler is not expecting to
3405 be called multiple times for the same address.
3407 This does mean disassembling the instruction
3408 twice, but we only do this when there is a high
3409 probability that there is a reloc that will
3410 affect the instruction. */
3411 disassemble_set_printf
3412 (inf
, inf
->stream
, (fprintf_ftype
) null_print
,
3413 (fprintf_styled_ftype
) null_styled_print
);
3414 insn_size
= disassemble_fn (section
->vma
3415 + addr_offset
, inf
);
3416 disassemble_set_printf
3418 (fprintf_ftype
) objdump_sprintf
,
3419 (fprintf_styled_ftype
) objdump_styled_sprintf
);
3423 /* Check to see if the current reloc is associated with
3424 the instruction that we are about to disassemble. */
3425 if (distance_to_rel
== 0
3426 || (distance_to_rel
> 0
3427 && distance_to_rel
< insn_size
/ (int) opb
))
3429 inf
->flags
|= INSN_HAS_RELOC
;
3430 aux
->reloc
= **relppp
;
3434 if (! disassemble_all
3435 && ((section
->flags
& (SEC_CODE
| SEC_HAS_CONTENTS
))
3436 == (SEC_CODE
| SEC_HAS_CONTENTS
)))
3437 /* Set a stop_vma so that the disassembler will not read
3438 beyond the next symbol. We assume that symbols appear on
3439 the boundaries between instructions. We only do this when
3440 disassembling code of course, and when -D is in effect. */
3441 inf
->stop_vma
= section
->vma
+ stop_offset
;
3443 inf
->stop_offset
= stop_offset
;
3444 disassembler_in_comment
= false;
3445 insn_size
= (*disassemble_fn
) (section
->vma
+ addr_offset
, inf
);
3449 disassemble_set_printf (inf
, stdout
, (fprintf_ftype
) fprintf
,
3450 (fprintf_styled_ftype
) fprintf_styled
);
3451 if (insn_width
== 0 && inf
->bytes_per_line
!= 0)
3452 octets_per_line
= inf
->bytes_per_line
;
3453 if (insn_size
< (int) opb
)
3456 printf ("%s\n", sfile
.buffer
);
3459 non_fatal (_("disassemble_fn returned length %d"),
3470 octets
= octets_per_line
;
3471 if (addr_offset
+ octets
/ opb
> stop_offset
)
3472 octets
= (stop_offset
- addr_offset
) * opb
;
3474 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ octets
; ++j
)
3476 if (ISPRINT (data
[j
]))
3477 buf
[j
- addr_offset
* opb
] = data
[j
];
3479 buf
[j
- addr_offset
* opb
] = '.';
3481 buf
[j
- addr_offset
* opb
] = '\0';
3484 if (prefix_addresses
3486 : show_raw_insn
>= 0)
3490 /* If ! prefix_addresses and ! wide_output, we print
3491 octets_per_line octets per line. */
3493 if (pb
> octets_per_line
&& ! prefix_addresses
&& ! wide_output
)
3494 pb
= octets_per_line
;
3496 if (inf
->bytes_per_chunk
)
3497 bpc
= inf
->bytes_per_chunk
;
3501 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
3503 /* PR 21580: Check for a buffer ending early. */
3504 if (j
+ bpc
<= stop_offset
* opb
)
3508 if (inf
->display_endian
== BFD_ENDIAN_LITTLE
)
3510 for (k
= bpc
; k
-- != 0; )
3511 printf ("%02x", (unsigned) data
[j
+ k
]);
3515 for (k
= 0; k
< bpc
; k
++)
3516 printf ("%02x", (unsigned) data
[j
+ k
]);
3522 for (; pb
< octets_per_line
; pb
+= bpc
)
3526 for (k
= 0; k
< bpc
; k
++)
3531 /* Separate raw data from instruction by extra space. */
3541 printf ("%s", sfile
.buffer
);
3543 if (prefix_addresses
3545 : show_raw_insn
>= 0)
3553 j
= addr_offset
* opb
+ pb
;
3559 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ j
/ opb
);
3560 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
3564 printf ("%s:\t", buf
+ skip_addr_chars
);
3567 print_jump_visualisation (section
->vma
+ j
/ opb
,
3568 max_level
, line_buffer
,
3571 pb
+= octets_per_line
;
3574 for (; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
3576 /* PR 21619: Check for a buffer ending early. */
3577 if (j
+ bpc
<= stop_offset
* opb
)
3581 if (inf
->display_endian
== BFD_ENDIAN_LITTLE
)
3583 for (k
= bpc
; k
-- != 0; )
3584 printf ("%02x", (unsigned) data
[j
+ k
]);
3588 for (k
= 0; k
< bpc
; k
++)
3589 printf ("%02x", (unsigned) data
[j
+ k
]);
3603 while ((*relppp
) < relppend
3604 && (**relppp
)->address
< rel_offset
+ addr_offset
+ octets
/ opb
)
3606 if (dump_reloc_info
|| dump_dynamic_reloc_info
)
3619 objdump_print_value (section
->vma
- rel_offset
+ q
->address
,
3624 if (q
->howto
== NULL
)
3625 printf ("*unknown*\t");
3626 else if (q
->howto
->name
)
3627 printf ("%s\t", q
->howto
->name
);
3629 printf ("%d\t", q
->howto
->type
);
3631 if (q
->sym_ptr_ptr
== NULL
|| *q
->sym_ptr_ptr
== NULL
)
3632 printf ("*unknown*");
3635 const char *sym_name
;
3637 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
3638 if (sym_name
!= NULL
&& *sym_name
!= '\0')
3639 objdump_print_symname (aux
->abfd
, inf
, *q
->sym_ptr_ptr
);
3644 sym_sec
= bfd_asymbol_section (*q
->sym_ptr_ptr
);
3645 sym_name
= bfd_section_name (sym_sec
);
3646 if (sym_name
== NULL
|| *sym_name
== '\0')
3647 sym_name
= "*unknown*";
3648 printf ("%s", sanitize_string (sym_name
));
3654 bfd_vma addend
= q
->addend
;
3655 if ((bfd_signed_vma
) addend
< 0)
3662 objdump_print_value (addend
, inf
, true);
3674 addr_offset
+= octets
/ opb
;
3677 free (sfile
.buffer
);
3679 free (color_buffer
);
3683 disassemble_section (bfd
*abfd
, asection
*section
, void *inf
)
3685 const struct elf_backend_data
*bed
;
3686 bfd_vma sign_adjust
= 0;
3687 struct disassemble_info
*pinfo
= (struct disassemble_info
*) inf
;
3688 struct objdump_disasm_info
*paux
;
3689 unsigned int opb
= pinfo
->octets_per_byte
;
3690 bfd_byte
*data
= NULL
;
3691 bfd_size_type datasize
= 0;
3692 arelent
**rel_pp
= NULL
;
3693 arelent
**rel_ppstart
= NULL
;
3694 arelent
**rel_ppend
;
3695 bfd_vma stop_offset
;
3696 asymbol
*sym
= NULL
;
3700 unsigned long addr_offset
;
3704 stop_offset_reached
,
3709 if (only_list
== NULL
)
3711 /* Sections that do not contain machine
3712 code are not normally disassembled. */
3713 if ((section
->flags
& SEC_HAS_CONTENTS
) == 0)
3716 if (! disassemble_all
3717 && (section
->flags
& SEC_CODE
) == 0)
3720 else if (!process_section_p (section
))
3723 datasize
= bfd_section_size (section
);
3727 if (start_address
== (bfd_vma
) -1
3728 || start_address
< section
->vma
)
3731 addr_offset
= start_address
- section
->vma
;
3733 if (stop_address
== (bfd_vma
) -1)
3734 stop_offset
= datasize
/ opb
;
3737 if (stop_address
< section
->vma
)
3740 stop_offset
= stop_address
- section
->vma
;
3741 if (stop_offset
> datasize
/ opb
)
3742 stop_offset
= datasize
/ opb
;
3745 if (addr_offset
>= stop_offset
)
3748 /* Decide which set of relocs to use. Load them if necessary. */
3749 paux
= (struct objdump_disasm_info
*) pinfo
->application_data
;
3750 if (pinfo
->dynrelbuf
&& dump_dynamic_reloc_info
)
3752 rel_pp
= pinfo
->dynrelbuf
;
3753 rel_count
= pinfo
->dynrelcount
;
3754 /* Dynamic reloc addresses are absolute, non-dynamic are section
3755 relative. REL_OFFSET specifies the reloc address corresponding
3756 to the start of this section. */
3757 rel_offset
= section
->vma
;
3765 if ((section
->flags
& SEC_RELOC
) != 0
3766 && (dump_reloc_info
|| pinfo
->disassembler_needs_relocs
))
3770 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
3772 my_bfd_nonfatal (bfd_get_filename (abfd
));
3776 rel_pp
= (arelent
**) xmalloc (relsize
);
3777 rel_count
= bfd_canonicalize_reloc (abfd
, section
, rel_pp
, syms
);
3780 my_bfd_nonfatal (bfd_get_filename (abfd
));
3785 else if (rel_count
> 1)
3786 /* Sort the relocs by address. */
3787 qsort (rel_pp
, rel_count
, sizeof (arelent
*), compare_relocs
);
3788 rel_ppstart
= rel_pp
;
3792 rel_ppend
= PTR_ADD (rel_pp
, rel_count
);
3794 if (!bfd_malloc_and_get_section (abfd
, section
, &data
))
3796 non_fatal (_("Reading section %s failed because: %s"),
3797 section
->name
, bfd_errmsg (bfd_get_error ()));
3801 pinfo
->buffer
= data
;
3802 pinfo
->buffer_vma
= section
->vma
;
3803 pinfo
->buffer_length
= datasize
;
3804 pinfo
->section
= section
;
3806 /* Sort the symbols into value and section order. */
3807 compare_section
= section
;
3808 if (sorted_symcount
> 1)
3809 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
3811 /* Skip over the relocs belonging to addresses below the
3813 while (rel_pp
< rel_ppend
3814 && (*rel_pp
)->address
< rel_offset
+ addr_offset
)
3817 printf (_("\nDisassembly of section %s:\n"), sanitize_string (section
->name
));
3819 /* Find the nearest symbol forwards from our current position. */
3820 paux
->require_sec
= true;
3821 sym
= (asymbol
*) find_symbol_for_address (section
->vma
+ addr_offset
,
3822 (struct disassemble_info
*) inf
,
3824 paux
->require_sec
= false;
3826 /* PR 9774: If the target used signed addresses then we must make
3827 sure that we sign extend the value that we calculate for 'addr'
3828 in the loop below. */
3829 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3830 && (bed
= get_elf_backend_data (abfd
)) != NULL
3831 && bed
->sign_extend_vma
)
3832 sign_adjust
= (bfd_vma
) 1 << (bed
->s
->arch_size
- 1);
3834 /* Disassemble a block of instructions up to the address associated with
3835 the symbol we have just found. Then print the symbol and find the
3836 next symbol on. Repeat until we have disassembled the entire section
3837 or we have reached the end of the address range we are interested in. */
3838 do_print
= paux
->symbol
== NULL
;
3839 loop_until
= stop_offset_reached
;
3841 while (addr_offset
< stop_offset
)
3845 bfd_vma nextstop_offset
;
3848 addr
= section
->vma
+ addr_offset
;
3849 addr
= ((addr
& ((sign_adjust
<< 1) - 1)) ^ sign_adjust
) - sign_adjust
;
3851 if (sym
!= NULL
&& bfd_asymbol_value (sym
) <= addr
)
3856 (x
< sorted_symcount
3857 && (bfd_asymbol_value (sorted_syms
[x
]) <= addr
));
3861 pinfo
->symbols
= sorted_syms
+ place
;
3862 pinfo
->num_symbols
= x
- place
;
3863 pinfo
->symtab_pos
= place
;
3867 pinfo
->symbols
= NULL
;
3868 pinfo
->num_symbols
= 0;
3869 pinfo
->symtab_pos
= -1;
3872 /* If we are only disassembling from a specific symbol,
3873 check to see if we should start or stop displaying. */
3874 if (sym
&& paux
->symbol
)
3878 /* See if we should stop printing. */
3882 if (sym
->flags
& BSF_FUNCTION
)
3886 case stop_offset_reached
:
3887 /* Handled by the while loop. */
3891 /* FIXME: There is an implicit assumption here
3892 that the name of sym is different from
3894 if (! bfd_is_local_label (abfd
, sym
))
3901 const char * name
= bfd_asymbol_name (sym
);
3902 char * alloc
= NULL
;
3904 if (do_demangle
&& name
[0] != '\0')
3906 /* Demangle the name. */
3907 alloc
= bfd_demangle (abfd
, name
, demangle_flags
);
3912 /* We are not currently printing. Check to see
3913 if the current symbol matches the requested symbol. */
3914 if (streq (name
, paux
->symbol
))
3918 /* Skip over the relocs belonging to addresses below the
3920 const bfd_vma sym_offset
= bfd_asymbol_value (sym
) - section
->vma
;
3921 while (rel_pp
< rel_ppend
&&
3922 (*rel_pp
)->address
- rel_offset
< sym_offset
)
3925 if (sym
->flags
& BSF_FUNCTION
)
3927 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3928 && ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
> 0)
3930 /* Sym is a function symbol with a size associated
3931 with it. Turn on automatic disassembly for the
3932 next VALUE bytes. */
3933 stop_offset
= addr_offset
3934 + ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
3935 loop_until
= stop_offset_reached
;
3939 /* Otherwise we need to tell the loop heuristic to
3940 loop until the next function symbol is encountered. */
3941 loop_until
= function_sym
;
3946 /* Otherwise loop until the next symbol is encountered. */
3947 loop_until
= next_sym
;
3955 if (! prefix_addresses
&& do_print
)
3957 pinfo
->fprintf_func (pinfo
->stream
, "\n");
3958 objdump_print_addr_with_sym (abfd
, section
, sym
, addr
,
3960 pinfo
->fprintf_func (pinfo
->stream
, ":\n");
3962 if (sym
!= NULL
&& show_all_symbols
)
3964 for (++place
; place
< sorted_symcount
; place
++)
3966 sym
= sorted_syms
[place
];
3968 if (bfd_asymbol_value (sym
) != addr
)
3970 if (! pinfo
->symbol_is_valid (sym
, pinfo
))
3972 if (strcmp (bfd_section_name (sym
->section
), bfd_section_name (section
)) != 0)
3975 objdump_print_addr_with_sym (abfd
, section
, sym
, addr
, pinfo
, false);
3976 pinfo
->fprintf_func (pinfo
->stream
, ":\n");
3981 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > addr
)
3983 else if (sym
== NULL
)
3987 #define is_valid_next_sym(SYM) \
3988 (strcmp (bfd_section_name ((SYM)->section), bfd_section_name (section)) == 0 \
3989 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
3990 && pinfo->symbol_is_valid (SYM, pinfo))
3992 /* Search forward for the next appropriate symbol in
3993 SECTION. Note that all the symbols are sorted
3994 together into one big array, and that some sections
3995 may have overlapping addresses. */
3996 while (place
< sorted_symcount
3997 && ! is_valid_next_sym (sorted_syms
[place
]))
4000 if (place
>= sorted_symcount
)
4003 nextsym
= sorted_syms
[place
];
4006 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > addr
)
4007 nextstop_offset
= bfd_asymbol_value (sym
) - section
->vma
;
4008 else if (nextsym
== NULL
)
4009 nextstop_offset
= stop_offset
;
4011 nextstop_offset
= bfd_asymbol_value (nextsym
) - section
->vma
;
4013 if (nextstop_offset
> stop_offset
4014 || nextstop_offset
<= addr_offset
)
4015 nextstop_offset
= stop_offset
;
4017 /* If a symbol is explicitly marked as being an object
4018 rather than a function, just dump the bytes without
4019 disassembling them. */
4022 || sym
->section
!= section
4023 || bfd_asymbol_value (sym
) > addr
4024 || ((sym
->flags
& BSF_OBJECT
) == 0
4025 && (strstr (bfd_asymbol_name (sym
), "gnu_compiled")
4027 && (strstr (bfd_asymbol_name (sym
), "gcc2_compiled")
4029 || (sym
->flags
& BSF_FUNCTION
) != 0)
4036 /* Resolve symbol name. */
4037 if (visualize_jumps
&& abfd
&& sym
&& sym
->name
)
4039 struct disassemble_info di
;
4042 sf
.alloc
= strlen (sym
->name
) + 40;
4043 sf
.buffer
= (char*) xmalloc (sf
.alloc
);
4045 disassemble_set_printf
4046 (&di
, &sf
, (fprintf_ftype
) objdump_sprintf
,
4047 (fprintf_styled_ftype
) objdump_styled_sprintf
);
4049 objdump_print_symname (abfd
, &di
, sym
);
4051 /* Fetch jump information. */
4052 detected_jumps
= disassemble_jumps
4053 (pinfo
, paux
->disassemble_fn
,
4054 addr_offset
, nextstop_offset
,
4055 rel_offset
, &rel_pp
, rel_ppend
);
4057 /* Free symbol name. */
4061 /* Add jumps to output. */
4062 disassemble_bytes (pinfo
, paux
->disassemble_fn
, insns
, data
,
4063 addr_offset
, nextstop_offset
,
4064 rel_offset
, &rel_pp
, rel_ppend
);
4067 while (detected_jumps
)
4069 detected_jumps
= jump_info_free (detected_jumps
);
4073 addr_offset
= nextstop_offset
;
4079 if (rel_ppstart
!= NULL
)
4083 /* Disassemble the contents of an object file. */
4086 disassemble_data (bfd
*abfd
)
4088 struct disassemble_info disasm_info
;
4089 struct objdump_disasm_info aux
;
4093 prev_functionname
= NULL
;
4095 prev_discriminator
= 0;
4097 /* We make a copy of syms to sort. We don't want to sort syms
4098 because that will screw up the relocs. */
4099 sorted_symcount
= symcount
? symcount
: dynsymcount
;
4100 sorted_syms
= (asymbol
**) xmalloc ((sorted_symcount
+ synthcount
)
4101 * sizeof (asymbol
*));
4102 if (sorted_symcount
!= 0)
4104 memcpy (sorted_syms
, symcount
? syms
: dynsyms
,
4105 sorted_symcount
* sizeof (asymbol
*));
4107 sorted_symcount
= remove_useless_symbols (sorted_syms
, sorted_symcount
);
4110 for (i
= 0; i
< synthcount
; ++i
)
4112 sorted_syms
[sorted_symcount
] = synthsyms
+ i
;
4116 init_disassemble_info (&disasm_info
, stdout
, (fprintf_ftype
) fprintf
,
4117 (fprintf_styled_ftype
) fprintf_styled
);
4118 disasm_info
.application_data
= (void *) &aux
;
4120 aux
.require_sec
= false;
4121 disasm_info
.dynrelbuf
= NULL
;
4122 disasm_info
.dynrelcount
= 0;
4124 aux
.symbol
= disasm_sym
;
4126 disasm_info
.print_address_func
= objdump_print_address
;
4127 disasm_info
.symbol_at_address_func
= objdump_symbol_at_address
;
4129 if (machine
!= NULL
)
4131 const bfd_arch_info_type
*inf
= bfd_scan_arch (machine
);
4135 non_fatal (_("can't use supplied machine %s"), machine
);
4139 abfd
->arch_info
= inf
;
4142 if (endian
!= BFD_ENDIAN_UNKNOWN
)
4144 struct bfd_target
*xvec
;
4146 xvec
= (struct bfd_target
*) xmalloc (sizeof (struct bfd_target
));
4147 memcpy (xvec
, abfd
->xvec
, sizeof (struct bfd_target
));
4148 xvec
->byteorder
= endian
;
4152 /* Use libopcodes to locate a suitable disassembler. */
4153 aux
.disassemble_fn
= disassembler (bfd_get_arch (abfd
),
4154 bfd_big_endian (abfd
),
4155 bfd_get_mach (abfd
), abfd
);
4156 if (!aux
.disassemble_fn
)
4158 non_fatal (_("can't disassemble for architecture %s\n"),
4159 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
4165 disasm_info
.flavour
= bfd_get_flavour (abfd
);
4166 disasm_info
.arch
= bfd_get_arch (abfd
);
4167 disasm_info
.mach
= bfd_get_mach (abfd
);
4168 disasm_info
.disassembler_options
= disassembler_options
;
4169 disasm_info
.octets_per_byte
= bfd_octets_per_byte (abfd
, NULL
);
4170 disasm_info
.skip_zeroes
= DEFAULT_SKIP_ZEROES
;
4171 disasm_info
.skip_zeroes_at_end
= DEFAULT_SKIP_ZEROES_AT_END
;
4172 disasm_info
.disassembler_needs_relocs
= false;
4174 if (bfd_big_endian (abfd
))
4175 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_BIG
;
4176 else if (bfd_little_endian (abfd
))
4177 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
4179 /* ??? Aborting here seems too drastic. We could default to big or little
4181 disasm_info
.endian
= BFD_ENDIAN_UNKNOWN
;
4183 disasm_info
.endian_code
= disasm_info
.endian
;
4185 /* Allow the target to customize the info structure. */
4186 disassemble_init_for_target (& disasm_info
);
4188 /* Pre-load the dynamic relocs as we may need them during the disassembly. */
4189 long relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
4193 disasm_info
.dynrelbuf
= (arelent
**) xmalloc (relsize
);
4194 disasm_info
.dynrelcount
4195 = bfd_canonicalize_dynamic_reloc (abfd
, disasm_info
.dynrelbuf
, dynsyms
);
4196 if (disasm_info
.dynrelcount
< 0)
4198 my_bfd_nonfatal (bfd_get_filename (abfd
));
4199 free (disasm_info
.dynrelbuf
);
4200 disasm_info
.dynrelbuf
= NULL
;
4201 disasm_info
.dynrelcount
= 0;
4203 else if (disasm_info
.dynrelcount
> 1)
4204 /* Sort the relocs by address. */
4205 qsort (disasm_info
.dynrelbuf
, disasm_info
.dynrelcount
,
4206 sizeof (arelent
*), compare_relocs
);
4209 disasm_info
.symtab
= sorted_syms
;
4210 disasm_info
.symtab_size
= sorted_symcount
;
4212 bfd_map_over_sections (abfd
, disassemble_section
, & disasm_info
);
4214 free (disasm_info
.dynrelbuf
);
4215 disasm_info
.dynrelbuf
= NULL
;
4217 disassemble_free_target (&disasm_info
);
4221 load_specific_debug_section (enum dwarf_section_display_enum debug
,
4222 asection
*sec
, void *file
)
4224 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
4225 bfd
*abfd
= (bfd
*) file
;
4231 if (section
->start
!= NULL
)
4233 /* If it is already loaded, do nothing. */
4234 if (streq (section
->filename
, bfd_get_filename (abfd
)))
4236 free (section
->start
);
4239 section
->filename
= bfd_get_filename (abfd
);
4240 section
->reloc_info
= NULL
;
4241 section
->num_relocs
= 0;
4242 section
->address
= bfd_section_vma (sec
);
4243 section
->size
= bfd_section_size (sec
);
4244 /* PR 24360: On 32-bit hosts sizeof (size_t) < sizeof (bfd_size_type). */
4245 alloced
= amt
= section
->size
+ 1;
4248 || (bfd_get_size (abfd
) != 0 && alloced
>= bfd_get_size (abfd
)))
4250 section
->start
= NULL
;
4251 free_debug_section (debug
);
4252 printf (_("\nSection '%s' has an invalid size: %#" PRIx64
".\n"),
4253 sanitize_string (section
->name
),
4258 section
->start
= contents
= xmalloc (alloced
);
4259 /* Ensure any string section has a terminating NUL. */
4260 section
->start
[section
->size
] = 0;
4262 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4263 && debug_displays
[debug
].relocate
)
4265 ret
= bfd_simple_get_relocated_section_contents (abfd
,
4271 long reloc_size
= bfd_get_reloc_upper_bound (abfd
, sec
);
4278 relocs
= (arelent
**) xmalloc (reloc_size
);
4280 reloc_count
= bfd_canonicalize_reloc (abfd
, sec
, relocs
, syms
);
4281 if (reloc_count
<= 0)
4285 section
->reloc_info
= relocs
;
4286 section
->num_relocs
= reloc_count
;
4292 ret
= bfd_get_full_section_contents (abfd
, sec
, &contents
);
4296 free_debug_section (debug
);
4297 printf (_("\nCan't get contents for section '%s'.\n"),
4298 sanitize_string (section
->name
));
4306 reloc_at (struct dwarf_section
* dsec
, uint64_t offset
)
4311 if (dsec
== NULL
|| dsec
->reloc_info
== NULL
)
4314 relocs
= (arelent
**) dsec
->reloc_info
;
4316 for (; (rp
= * relocs
) != NULL
; ++ relocs
)
4317 if (rp
->address
== offset
)
4324 load_debug_section (enum dwarf_section_display_enum debug
, void *file
)
4326 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
4327 bfd
*abfd
= (bfd
*) file
;
4331 if (!dump_any_debugging
)
4334 /* If it is already loaded, do nothing. */
4335 if (section
->start
!= NULL
)
4337 if (streq (section
->filename
, bfd_get_filename (abfd
)))
4340 /* Locate the debug section. */
4341 name
= section
->uncompressed_name
;
4342 sec
= bfd_get_section_by_name (abfd
, name
);
4345 name
= section
->compressed_name
;
4347 sec
= bfd_get_section_by_name (abfd
, name
);
4351 name
= section
->xcoff_name
;
4353 sec
= bfd_get_section_by_name (abfd
, name
);
4358 section
->name
= name
;
4359 return load_specific_debug_section (debug
, sec
, file
);
4363 free_debug_section (enum dwarf_section_display_enum debug
)
4365 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
4367 free ((char *) section
->start
);
4368 section
->start
= NULL
;
4369 section
->address
= 0;
4371 free ((char*) section
->reloc_info
);
4372 section
->reloc_info
= NULL
;
4373 section
->num_relocs
= 0;
4377 close_debug_file (void * file
)
4379 bfd
* abfd
= (bfd
*) file
;
4385 open_debug_file (const char * pathname
)
4389 data
= bfd_openr (pathname
, NULL
);
4393 if (! bfd_check_format (data
, bfd_object
))
4400 dump_dwarf_section (bfd
*abfd
, asection
*section
,
4403 const char *name
= bfd_section_name (section
);
4406 bool is_mainfile
= *(bool *) arg
;
4411 if (!is_mainfile
&& !process_links
4412 && (section
->flags
& SEC_DEBUGGING
) == 0)
4415 if (startswith (name
, ".gnu.linkonce.wi."))
4416 match
= ".debug_info";
4420 for (i
= 0; i
< max
; i
++)
4421 if ((strcmp (debug_displays
[i
].section
.uncompressed_name
, match
) == 0
4422 || strcmp (debug_displays
[i
].section
.compressed_name
, match
) == 0
4423 || strcmp (debug_displays
[i
].section
.xcoff_name
, match
) == 0)
4424 && debug_displays
[i
].enabled
!= NULL
4425 && *debug_displays
[i
].enabled
)
4427 struct dwarf_section
*sec
= &debug_displays
[i
].section
;
4429 if (strcmp (sec
->uncompressed_name
, match
) == 0)
4430 sec
->name
= sec
->uncompressed_name
;
4431 else if (strcmp (sec
->compressed_name
, match
) == 0)
4432 sec
->name
= sec
->compressed_name
;
4434 sec
->name
= sec
->xcoff_name
;
4435 if (load_specific_debug_section ((enum dwarf_section_display_enum
) i
,
4438 debug_displays
[i
].display (sec
, abfd
);
4440 if (i
!= info
&& i
!= abbrev
)
4441 free_debug_section ((enum dwarf_section_display_enum
) i
);
4447 /* Dump the dwarf debugging information. */
4450 dump_dwarf (bfd
*abfd
, bool is_mainfile
)
4452 /* The byte_get pointer should have been set at the start of dump_bfd(). */
4453 if (byte_get
== NULL
)
4455 warn (_("File %s does not contain any dwarf debug information\n"),
4456 bfd_get_filename (abfd
));
4460 switch (bfd_get_arch (abfd
))
4463 /* S12Z has a 24 bit address space. But the only known
4464 producer of dwarf_info encodes addresses into 32 bits. */
4469 eh_addr_size
= bfd_arch_bits_per_address (abfd
) / 8;
4473 init_dwarf_regnames_by_bfd_arch_and_mach (bfd_get_arch (abfd
),
4474 bfd_get_mach (abfd
));
4476 bfd_map_over_sections (abfd
, dump_dwarf_section
, (void *) &is_mainfile
);
4479 /* Read ABFD's section SECT_NAME into *CONTENTS, and return a pointer to
4480 the section. Return NULL on failure. */
4483 read_section (bfd
*abfd
, const char *sect_name
, bfd_byte
**contents
)
4488 sec
= bfd_get_section_by_name (abfd
, sect_name
);
4491 printf (_("No %s section present\n\n"), sanitize_string (sect_name
));
4495 if ((bfd_section_flags (sec
) & SEC_HAS_CONTENTS
) == 0)
4496 bfd_set_error (bfd_error_no_contents
);
4497 else if (bfd_malloc_and_get_section (abfd
, sec
, contents
))
4500 non_fatal (_("reading %s section of %s failed: %s"),
4501 sect_name
, bfd_get_filename (abfd
),
4502 bfd_errmsg (bfd_get_error ()));
4507 /* Stabs entries use a 12 byte format:
4508 4 byte string table index
4510 1 byte stab other field
4511 2 byte stab desc field
4513 FIXME: This will have to change for a 64 bit object format. */
4515 #define STRDXOFF (0)
4517 #define OTHEROFF (5)
4520 #define STABSIZE (12)
4522 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
4523 using string table section STRSECT_NAME (in `strtab'). */
4526 print_section_stabs (bfd
*abfd
,
4527 const char *stabsect_name
,
4528 unsigned *string_offset_ptr
)
4531 unsigned file_string_table_offset
= 0;
4532 unsigned next_file_string_table_offset
= *string_offset_ptr
;
4533 bfd_byte
*stabp
, *stabs_end
;
4536 stabs_end
= PTR_ADD (stabp
, stab_size
);
4538 printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name
));
4539 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
4541 /* Loop through all symbols and print them.
4543 We start the index at -1 because there is a dummy symbol on
4544 the front of stabs-in-{coff,elf} sections that supplies sizes. */
4545 for (i
= -1; (size_t) (stabs_end
- stabp
) >= STABSIZE
; stabp
+= STABSIZE
, i
++)
4549 unsigned char type
, other
;
4550 unsigned short desc
;
4553 strx
= bfd_h_get_32 (abfd
, stabp
+ STRDXOFF
);
4554 type
= bfd_h_get_8 (abfd
, stabp
+ TYPEOFF
);
4555 other
= bfd_h_get_8 (abfd
, stabp
+ OTHEROFF
);
4556 desc
= bfd_h_get_16 (abfd
, stabp
+ DESCOFF
);
4557 value
= bfd_h_get_32 (abfd
, stabp
+ VALOFF
);
4559 printf ("\n%-6d ", i
);
4560 /* Either print the stab name, or, if unnamed, print its number
4561 again (makes consistent formatting for tools like awk). */
4562 name
= bfd_get_stab_name (type
);
4564 printf ("%-6s", sanitize_string (name
));
4565 else if (type
== N_UNDF
)
4568 printf ("%-6d", type
);
4569 printf (" %-6d %-6d ", other
, desc
);
4570 bfd_printf_vma (abfd
, value
);
4571 printf (" %-6lu", strx
);
4573 /* Symbols with type == 0 (N_UNDF) specify the length of the
4574 string table associated with this file. We use that info
4575 to know how to relocate the *next* file's string table indices. */
4578 file_string_table_offset
= next_file_string_table_offset
;
4579 next_file_string_table_offset
+= value
;
4583 bfd_size_type amt
= strx
+ file_string_table_offset
;
4585 /* Using the (possibly updated) string table offset, print the
4586 string (if any) associated with this symbol. */
4587 if (amt
< stabstr_size
)
4588 /* PR 17512: file: 079-79389-0.001:0.1.
4589 FIXME: May need to sanitize this string before displaying. */
4590 printf (" %.*s", (int)(stabstr_size
- amt
), strtab
+ amt
);
4596 *string_offset_ptr
= next_file_string_table_offset
;
4601 const char * section_name
;
4602 const char * string_section_name
;
4603 unsigned string_offset
;
4608 find_stabs_section (bfd
*abfd
, asection
*section
, void *names
)
4611 stab_section_names
* sought
= (stab_section_names
*) names
;
4613 /* Check for section names for which stabsect_name is a prefix, to
4614 handle .stab.N, etc. */
4615 len
= strlen (sought
->section_name
);
4617 /* If the prefix matches, and the files section name ends with a
4618 nul or a digit, then we match. I.e., we want either an exact
4619 match or a section followed by a number. */
4620 if (strncmp (sought
->section_name
, section
->name
, len
) == 0
4621 && (section
->name
[len
] == 0
4622 || (section
->name
[len
] == '.' && ISDIGIT (section
->name
[len
+ 1]))))
4627 s
= read_section (abfd
, sought
->string_section_name
, &strtab
);
4629 stabstr_size
= bfd_section_size (s
);
4634 s
= read_section (abfd
, section
->name
, &stabs
);
4637 stab_size
= bfd_section_size (s
);
4638 print_section_stabs (abfd
, section
->name
, &sought
->string_offset
);
4646 dump_stabs_section (bfd
*abfd
, char *stabsect_name
, char *strsect_name
)
4648 stab_section_names s
;
4650 s
.section_name
= stabsect_name
;
4651 s
.string_section_name
= strsect_name
;
4652 s
.string_offset
= 0;
4654 bfd_map_over_sections (abfd
, find_stabs_section
, & s
);
4660 /* Dump the any sections containing stabs debugging information. */
4663 dump_stabs (bfd
*abfd
)
4665 dump_stabs_section (abfd
, ".stab", ".stabstr");
4666 dump_stabs_section (abfd
, ".stab.excl", ".stab.exclstr");
4667 dump_stabs_section (abfd
, ".stab.index", ".stab.indexstr");
4670 dump_stabs_section (abfd
, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
4672 dump_stabs_section (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
4676 dump_bfd_header (bfd
*abfd
)
4680 printf (_("architecture: %s, "),
4681 bfd_printable_arch_mach (bfd_get_arch (abfd
),
4682 bfd_get_mach (abfd
)));
4683 printf (_("flags 0x%08x:\n"), abfd
->flags
& ~BFD_FLAGS_FOR_BFD_USE_MASK
);
4685 #define PF(x, y) if (abfd->flags & x) {printf ("%s%s", comma, y); comma=", ";}
4686 PF (HAS_RELOC
, "HAS_RELOC");
4687 PF (EXEC_P
, "EXEC_P");
4688 PF (HAS_LINENO
, "HAS_LINENO");
4689 PF (HAS_DEBUG
, "HAS_DEBUG");
4690 PF (HAS_SYMS
, "HAS_SYMS");
4691 PF (HAS_LOCALS
, "HAS_LOCALS");
4692 PF (DYNAMIC
, "DYNAMIC");
4693 PF (WP_TEXT
, "WP_TEXT");
4694 PF (D_PAGED
, "D_PAGED");
4695 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
4696 printf (_("\nstart address 0x"));
4697 bfd_printf_vma (abfd
, abfd
->start_address
);
4702 #ifdef ENABLE_LIBCTF
4703 /* Formatting callback function passed to ctf_dump. Returns either the pointer
4704 it is passed, or a pointer to newly-allocated storage, in which case
4705 dump_ctf() will free it when it no longer needs it. */
4708 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED
,
4711 const char *blanks
= arg
;
4714 if (asprintf (&new_s
, "%s%s", blanks
, s
) < 0)
4719 /* Make a ctfsect suitable for ctf_bfdopen_ctfsect(). */
4721 make_ctfsect (const char *name
, bfd_byte
*data
,
4726 ctfsect
.cts_name
= name
;
4727 ctfsect
.cts_entsize
= 1;
4728 ctfsect
.cts_size
= size
;
4729 ctfsect
.cts_data
= data
;
4734 /* Dump CTF errors/warnings. */
4736 dump_ctf_errs (ctf_dict_t
*fp
)
4738 ctf_next_t
*it
= NULL
;
4743 /* Dump accumulated errors and warnings. */
4744 while ((errtext
= ctf_errwarning_next (fp
, &it
, &is_warning
, &err
)) != NULL
)
4746 non_fatal (_("%s: %s"), is_warning
? _("warning"): _("error"),
4750 if (err
!= ECTF_NEXT_END
)
4752 non_fatal (_("CTF error: cannot get CTF errors: `%s'"),
4757 /* Dump one CTF archive member. */
4760 dump_ctf_archive_member (ctf_dict_t
*ctf
, const char *name
, ctf_dict_t
*parent
,
4763 const char *things
[] = {"Header", "Labels", "Data objects",
4764 "Function objects", "Variables", "Types", "Strings",
4769 /* Don't print out the name of the default-named archive member if it appears
4770 first in the list. The name .ctf appears everywhere, even for things that
4771 aren't really archives, so printing it out is liable to be confusing; also,
4772 the common case by far is for only one archive member to exist, and hiding
4773 it in that case seems worthwhile. */
4775 if (strcmp (name
, ".ctf") != 0 || member
!= 0)
4776 printf (_("\nCTF archive member: %s:\n"), sanitize_string (name
));
4778 if (ctf_parent_name (ctf
) != NULL
)
4779 ctf_import (ctf
, parent
);
4781 for (i
= 0, thing
= things
; *thing
[0]; thing
++, i
++)
4783 ctf_dump_state_t
*s
= NULL
;
4786 printf ("\n %s:\n", *thing
);
4787 while ((item
= ctf_dump (ctf
, &s
, i
, dump_ctf_indent_lines
,
4788 (void *) " ")) != NULL
)
4790 printf ("%s\n", item
);
4794 if (ctf_errno (ctf
))
4796 non_fatal (_("Iteration failed: %s, %s"), *thing
,
4797 ctf_errmsg (ctf_errno (ctf
)));
4802 dump_ctf_errs (ctf
);
4805 /* Dump the CTF debugging information. */
4808 dump_ctf (bfd
*abfd
, const char *sect_name
, const char *parent_name
)
4811 ctf_archive_t
*ctfa
= NULL
;
4816 ctf_next_t
*i
= NULL
;
4821 if (sect_name
== NULL
)
4824 sec
= read_section (abfd
, sect_name
, &ctfdata
);
4827 my_bfd_nonfatal (bfd_get_filename (abfd
));
4831 /* Load the CTF file and dump it. Preload the parent dict, since it will
4832 need to be imported into every child in turn. */
4834 ctfsect
= make_ctfsect (sect_name
, ctfdata
, bfd_section_size (sec
));
4835 if ((ctfa
= ctf_bfdopen_ctfsect (abfd
, &ctfsect
, &err
)) == NULL
)
4837 dump_ctf_errs (NULL
);
4838 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err
));
4839 my_bfd_nonfatal (bfd_get_filename (abfd
));
4844 if ((parent
= ctf_dict_open (ctfa
, parent_name
, &err
)) == NULL
)
4846 dump_ctf_errs (NULL
);
4847 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err
));
4848 my_bfd_nonfatal (bfd_get_filename (abfd
));
4854 printf (_("Contents of CTF section %s:\n"), sanitize_string (sect_name
));
4856 while ((fp
= ctf_archive_next (ctfa
, &i
, &name
, 0, &err
)) != NULL
)
4857 dump_ctf_archive_member (fp
, name
, parent
, member
++);
4858 if (err
!= ECTF_NEXT_END
)
4860 dump_ctf_errs (NULL
);
4861 non_fatal (_("CTF archive member open failure: %s"), ctf_errmsg (err
));
4862 my_bfd_nonfatal (bfd_get_filename (abfd
));
4864 ctf_dict_close (parent
);
4870 dump_ctf (bfd
*abfd ATTRIBUTE_UNUSED
, const char *sect_name ATTRIBUTE_UNUSED
,
4871 const char *parent_name ATTRIBUTE_UNUSED
) {}
4875 dump_section_sframe (bfd
*abfd ATTRIBUTE_UNUSED
,
4876 const char * sect_name
)
4879 sframe_decoder_ctx
*sfd_ctx
= NULL
;
4880 bfd_size_type sf_size
;
4881 bfd_byte
*sframe_data
;
4885 if (sect_name
== NULL
)
4886 sect_name
= ".sframe";
4888 sec
= read_section (abfd
, sect_name
, &sframe_data
);
4891 my_bfd_nonfatal (bfd_get_filename (abfd
));
4894 sf_size
= bfd_section_size (sec
);
4895 sf_vma
= bfd_section_vma (sec
);
4897 /* Decode the contents of the section. */
4898 sfd_ctx
= sframe_decode ((const char*)sframe_data
, sf_size
, &err
);
4901 my_bfd_nonfatal (bfd_get_filename (abfd
));
4906 printf (_("Contents of the SFrame section %s:"),
4907 sanitize_string (sect_name
));
4908 /* Dump the contents as text. */
4909 dump_sframe (sfd_ctx
, sf_vma
);
4911 sframe_decoder_free (&sfd_ctx
);
4917 dump_bfd_private_header (bfd
*abfd
)
4919 if (!bfd_print_private_bfd_data (abfd
, stdout
))
4920 non_fatal (_("warning: private headers incomplete: %s"),
4921 bfd_errmsg (bfd_get_error ()));
4925 dump_target_specific (bfd
*abfd
)
4927 const struct objdump_private_desc
* const *desc
;
4928 struct objdump_private_option
*opt
;
4931 /* Find the desc. */
4932 for (desc
= objdump_private_vectors
; *desc
!= NULL
; desc
++)
4933 if ((*desc
)->filter (abfd
))
4938 non_fatal (_("option -P/--private not supported by this file"));
4942 /* Clear all options. */
4943 for (opt
= (*desc
)->options
; opt
->name
; opt
++)
4944 opt
->selected
= false;
4946 /* Decode options. */
4947 b
= dump_private_options
;
4950 e
= strchr (b
, ',');
4955 for (opt
= (*desc
)->options
; opt
->name
; opt
++)
4956 if (strcmp (opt
->name
, b
) == 0)
4958 opt
->selected
= true;
4961 if (opt
->name
== NULL
)
4962 non_fatal (_("target specific dump '%s' not supported"), b
);
4973 (*desc
)->dump (abfd
);
4976 /* Display a section in hexadecimal format with associated characters.
4977 Each line prefixed by the zero padded address. */
4980 dump_section (bfd
*abfd
, asection
*section
, void *dummy ATTRIBUTE_UNUSED
)
4982 bfd_byte
*data
= NULL
;
4983 bfd_size_type datasize
;
4984 bfd_vma addr_offset
;
4985 bfd_vma start_offset
;
4986 bfd_vma stop_offset
;
4987 unsigned int opb
= bfd_octets_per_byte (abfd
, section
);
4988 /* Bytes per line. */
4989 const int onaline
= 16;
4994 if (only_list
== NULL
)
4996 if ((section
->flags
& SEC_HAS_CONTENTS
) == 0)
4999 else if (!process_section_p (section
))
5002 if ((datasize
= bfd_section_size (section
)) == 0)
5005 /* Compute the address range to display. */
5006 if (start_address
== (bfd_vma
) -1
5007 || start_address
< section
->vma
)
5010 start_offset
= start_address
- section
->vma
;
5012 if (stop_address
== (bfd_vma
) -1)
5013 stop_offset
= datasize
/ opb
;
5016 if (stop_address
< section
->vma
)
5019 stop_offset
= stop_address
- section
->vma
;
5021 if (stop_offset
> datasize
/ opb
)
5022 stop_offset
= datasize
/ opb
;
5025 if (start_offset
>= stop_offset
)
5028 printf (_("Contents of section %s:"), sanitize_string (section
->name
));
5029 if (display_file_offsets
)
5030 printf (_(" (Starting at file offset: 0x%lx)"),
5031 (unsigned long) (section
->filepos
+ start_offset
));
5034 if (bfd_is_section_compressed (abfd
, section
) && ! decompressed_dumps
)
5035 printf (_(" NOTE: This section is compressed, but its contents have NOT been expanded for this dump.\n"));
5037 if (!bfd_get_full_section_contents (abfd
, section
, &data
))
5039 non_fatal (_("Reading section %s failed because: %s"),
5040 section
->name
, bfd_errmsg (bfd_get_error ()));
5046 bfd_sprintf_vma (abfd
, buf
, start_offset
+ section
->vma
);
5047 if (strlen (buf
) >= sizeof (buf
))
5051 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
5053 count
= strlen (buf
) - count
;
5057 bfd_sprintf_vma (abfd
, buf
, stop_offset
+ section
->vma
- 1);
5058 if (strlen (buf
) >= sizeof (buf
))
5062 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
5064 count
= strlen (buf
) - count
;
5068 for (addr_offset
= start_offset
;
5069 addr_offset
< stop_offset
; addr_offset
+= onaline
/ opb
)
5073 bfd_sprintf_vma (abfd
, buf
, (addr_offset
+ section
->vma
));
5074 count
= strlen (buf
);
5075 if ((size_t) count
>= sizeof (buf
))
5079 while (count
< width
)
5084 fputs (buf
+ count
- width
, stdout
);
5087 for (j
= addr_offset
* opb
;
5088 j
< addr_offset
* opb
+ onaline
; j
++)
5090 if (j
< stop_offset
* opb
)
5091 printf ("%02x", (unsigned) (data
[j
]));
5099 for (j
= addr_offset
* opb
;
5100 j
< addr_offset
* opb
+ onaline
; j
++)
5102 if (j
>= stop_offset
* opb
)
5105 printf ("%c", ISPRINT (data
[j
]) ? data
[j
] : '.');
5112 /* Actually display the various requested regions. */
5115 dump_data (bfd
*abfd
)
5117 bfd_map_over_sections (abfd
, dump_section
, NULL
);
5120 /* Should perhaps share code and display with nm? */
5123 dump_symbols (bfd
*abfd ATTRIBUTE_UNUSED
, bool dynamic
)
5132 max_count
= dynsymcount
;
5133 printf ("DYNAMIC SYMBOL TABLE:\n");
5138 max_count
= symcount
;
5139 printf ("SYMBOL TABLE:\n");
5143 printf (_("no symbols\n"));
5145 for (count
= 0; count
< max_count
; count
++)
5149 if (*current
== NULL
)
5150 printf (_("no information for symbol number %ld\n"), count
);
5152 else if ((cur_bfd
= bfd_asymbol_bfd (*current
)) == NULL
)
5153 printf (_("could not determine the type of symbol number %ld\n"),
5156 else if (process_section_p ((* current
)->section
)
5157 && (dump_special_syms
5158 || !bfd_is_target_special_symbol (cur_bfd
, *current
)))
5160 const char *name
= (*current
)->name
;
5162 if (do_demangle
&& name
!= NULL
&& *name
!= '\0')
5166 /* If we want to demangle the name, we demangle it
5167 here, and temporarily clobber it while calling
5168 bfd_print_symbol. FIXME: This is a gross hack. */
5169 alloc
= bfd_demangle (cur_bfd
, name
, demangle_flags
);
5171 (*current
)->name
= alloc
;
5172 bfd_print_symbol (cur_bfd
, stdout
, *current
,
5173 bfd_print_symbol_all
);
5176 (*current
)->name
= name
;
5180 else if (unicode_display
!= unicode_default
5181 && name
!= NULL
&& *name
!= '\0')
5183 const char * sanitized_name
;
5185 /* If we want to sanitize the name, we do it here, and
5186 temporarily clobber it while calling bfd_print_symbol.
5187 FIXME: This is a gross hack. */
5188 sanitized_name
= sanitize_string (name
);
5189 if (sanitized_name
!= name
)
5190 (*current
)->name
= sanitized_name
;
5192 sanitized_name
= NULL
;
5193 bfd_print_symbol (cur_bfd
, stdout
, *current
,
5194 bfd_print_symbol_all
);
5195 if (sanitized_name
!= NULL
)
5196 (*current
)->name
= name
;
5199 bfd_print_symbol (cur_bfd
, stdout
, *current
,
5200 bfd_print_symbol_all
);
5210 dump_reloc_set (bfd
*abfd
, asection
*sec
, arelent
**relpp
, long relcount
)
5213 char *last_filename
, *last_functionname
;
5214 unsigned int last_line
;
5215 unsigned int last_discriminator
;
5217 /* Get column headers lined up reasonably. */
5225 bfd_sprintf_vma (abfd
, buf
, (bfd_vma
) -1);
5226 width
= strlen (buf
) - 7;
5228 printf ("OFFSET %*s TYPE %*s VALUE\n", width
, "", 12, "");
5231 last_filename
= NULL
;
5232 last_functionname
= NULL
;
5234 last_discriminator
= 0;
5236 for (p
= relpp
; relcount
&& *p
!= NULL
; p
++, relcount
--)
5239 const char *filename
, *functionname
;
5240 unsigned int linenumber
;
5241 unsigned int discriminator
;
5242 const char *sym_name
;
5243 const char *section_name
;
5244 bfd_vma addend2
= 0;
5246 if (start_address
!= (bfd_vma
) -1
5247 && q
->address
< start_address
)
5249 if (stop_address
!= (bfd_vma
) -1
5250 && q
->address
> stop_address
)
5253 if (with_line_numbers
5255 && bfd_find_nearest_line_discriminator (abfd
, sec
, syms
, q
->address
,
5256 &filename
, &functionname
,
5257 &linenumber
, &discriminator
))
5259 if (functionname
!= NULL
5260 && (last_functionname
== NULL
5261 || strcmp (functionname
, last_functionname
) != 0))
5263 printf ("%s():\n", sanitize_string (functionname
));
5264 if (last_functionname
!= NULL
)
5265 free (last_functionname
);
5266 last_functionname
= xstrdup (functionname
);
5270 && (linenumber
!= last_line
5271 || (filename
!= NULL
5272 && last_filename
!= NULL
5273 && filename_cmp (filename
, last_filename
) != 0)
5274 || (discriminator
!= last_discriminator
)))
5276 if (discriminator
> 0)
5277 printf ("%s:%u\n", filename
== NULL
? "???" :
5278 sanitize_string (filename
), linenumber
);
5280 printf ("%s:%u (discriminator %u)\n",
5281 filename
== NULL
? "???" : sanitize_string (filename
),
5282 linenumber
, discriminator
);
5283 last_line
= linenumber
;
5284 last_discriminator
= discriminator
;
5285 if (last_filename
!= NULL
)
5286 free (last_filename
);
5287 if (filename
== NULL
)
5288 last_filename
= NULL
;
5290 last_filename
= xstrdup (filename
);
5294 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
5296 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
5297 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
5302 section_name
= NULL
;
5305 bfd_printf_vma (abfd
, q
->address
);
5306 if (q
->howto
== NULL
)
5307 printf (" *unknown* ");
5308 else if (q
->howto
->name
)
5310 const char *name
= q
->howto
->name
;
5312 /* R_SPARC_OLO10 relocations contain two addends.
5313 But because 'arelent' lacks enough storage to
5314 store them both, the 64-bit ELF Sparc backend
5315 records this as two relocations. One R_SPARC_LO10
5316 and one R_SPARC_13, both pointing to the same
5317 address. This is merely so that we have some
5318 place to store both addend fields.
5320 Undo this transformation, otherwise the output
5321 will be confusing. */
5322 if (abfd
->xvec
->flavour
== bfd_target_elf_flavour
5323 && elf_tdata (abfd
)->elf_header
->e_machine
== EM_SPARCV9
5325 && !strcmp (q
->howto
->name
, "R_SPARC_LO10"))
5327 arelent
*q2
= *(p
+ 1);
5330 && q
->address
== q2
->address
5331 && !strcmp (q2
->howto
->name
, "R_SPARC_13"))
5333 name
= "R_SPARC_OLO10";
5334 addend2
= q2
->addend
;
5338 printf (" %-16s ", name
);
5341 printf (" %-16d ", q
->howto
->type
);
5345 objdump_print_symname (abfd
, NULL
, *q
->sym_ptr_ptr
);
5349 if (section_name
== NULL
)
5350 section_name
= "*unknown*";
5351 printf ("[%s]", sanitize_string (section_name
));
5356 bfd_signed_vma addend
= q
->addend
;
5364 bfd_printf_vma (abfd
, addend
);
5369 bfd_printf_vma (abfd
, addend2
);
5375 if (last_filename
!= NULL
)
5376 free (last_filename
);
5377 if (last_functionname
!= NULL
)
5378 free (last_functionname
);
5382 dump_relocs_in_section (bfd
*abfd
,
5384 void *dummy ATTRIBUTE_UNUSED
)
5390 if ( bfd_is_abs_section (section
)
5391 || bfd_is_und_section (section
)
5392 || bfd_is_com_section (section
)
5393 || (! process_section_p (section
))
5394 || ((section
->flags
& SEC_RELOC
) == 0))
5397 printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section
->name
));
5399 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
5402 printf (" (none)\n\n");
5413 relpp
= (arelent
**) xmalloc (relsize
);
5414 relcount
= bfd_canonicalize_reloc (abfd
, section
, relpp
, syms
);
5420 non_fatal (_("failed to read relocs in: %s"),
5421 sanitize_string (bfd_get_filename (abfd
)));
5422 my_bfd_nonfatal (_("error message was"));
5424 else if (relcount
== 0)
5425 printf (" (none)\n\n");
5429 dump_reloc_set (abfd
, section
, relpp
, relcount
);
5436 dump_relocs (bfd
*abfd
)
5438 bfd_map_over_sections (abfd
, dump_relocs_in_section
, NULL
);
5442 dump_dynamic_relocs (bfd
*abfd
)
5448 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
5450 printf ("DYNAMIC RELOCATION RECORDS");
5454 printf (" (none)\n\n");
5465 relpp
= (arelent
**) xmalloc (relsize
);
5466 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
5472 non_fatal (_("failed to read relocs in: %s"),
5473 sanitize_string (bfd_get_filename (abfd
)));
5474 my_bfd_nonfatal (_("error message was"));
5476 else if (relcount
== 0)
5477 printf (" (none)\n\n");
5481 dump_reloc_set (abfd
, NULL
, relpp
, relcount
);
5487 /* Creates a table of paths, to search for source files. */
5490 add_include_path (const char *path
)
5494 include_path_count
++;
5495 include_paths
= (const char **)
5496 xrealloc (include_paths
, include_path_count
* sizeof (*include_paths
));
5497 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5498 if (path
[1] == ':' && path
[2] == 0)
5499 path
= concat (path
, ".", (const char *) 0);
5501 include_paths
[include_path_count
- 1] = path
;
5505 adjust_addresses (bfd
*abfd ATTRIBUTE_UNUSED
,
5509 if ((section
->flags
& SEC_DEBUGGING
) == 0)
5511 bool *has_reloc_p
= (bool *) arg
;
5512 section
->vma
+= adjust_section_vma
;
5514 section
->lma
+= adjust_section_vma
;
5518 /* Return the sign-extended form of an ARCH_SIZE sized VMA. */
5521 sign_extend_address (bfd
*abfd ATTRIBUTE_UNUSED
,
5526 mask
= (bfd_vma
) 1 << (arch_size
- 1);
5527 return (((vma
& ((mask
<< 1) - 1)) ^ mask
) - mask
);
5531 might_need_separate_debug_info (bool is_mainfile
)
5533 /* We do not follow links from debug info files. */
5537 /* Since do_follow_links might be enabled by default, only treat it as an
5538 indication that separate files should be loaded if setting it was a
5539 deliberate user action. */
5540 if (DEFAULT_FOR_FOLLOW_LINKS
== 0 && do_follow_links
)
5543 if (process_links
|| dump_symtab
|| dump_debugging
5544 || dump_dwarf_section_info
|| with_source_code
)
5550 /* Dump selected contents of ABFD. */
5553 dump_bfd (bfd
*abfd
, bool is_mainfile
)
5555 const struct elf_backend_data
* bed
;
5557 if (bfd_big_endian (abfd
))
5558 byte_get
= byte_get_big_endian
;
5559 else if (bfd_little_endian (abfd
))
5560 byte_get
= byte_get_little_endian
;
5564 /* Load any separate debug information files. */
5565 if (byte_get
!= NULL
&& might_need_separate_debug_info (is_mainfile
))
5567 load_separate_debug_files (abfd
, bfd_get_filename (abfd
));
5569 /* If asked to do so, recursively dump the separate files. */
5570 if (do_follow_links
)
5574 for (i
= first_separate_info
; i
!= NULL
; i
= i
->next
)
5575 dump_bfd (i
->handle
, false);
5579 /* Adjust user-specified start and stop limits for targets that use
5580 signed addresses. */
5581 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
5582 && (bed
= get_elf_backend_data (abfd
)) != NULL
5583 && bed
->sign_extend_vma
)
5585 start_address
= sign_extend_address (abfd
, start_address
,
5587 stop_address
= sign_extend_address (abfd
, stop_address
,
5591 /* If we are adjusting section VMA's, change them all now. Changing
5592 the BFD information is a hack. However, we must do it, or
5593 bfd_find_nearest_line will not do the right thing. */
5594 if (adjust_section_vma
!= 0)
5596 bool has_reloc
= (abfd
->flags
& HAS_RELOC
);
5597 bfd_map_over_sections (abfd
, adjust_addresses
, &has_reloc
);
5600 if (is_mainfile
|| process_links
)
5602 if (! dump_debugging_tags
&& ! suppress_bfd_header
)
5603 printf (_("\n%s: file format %s\n"),
5604 sanitize_string (bfd_get_filename (abfd
)),
5607 print_arelt_descr (stdout
, abfd
, true, false);
5608 if (dump_file_header
)
5609 dump_bfd_header (abfd
);
5610 if (dump_private_headers
)
5611 dump_bfd_private_header (abfd
);
5612 if (dump_private_options
!= NULL
)
5613 dump_target_specific (abfd
);
5614 if (! dump_debugging_tags
&& ! suppress_bfd_header
)
5622 || dump_dwarf_section_info
)
5624 syms
= slurp_symtab (abfd
);
5626 /* If following links, load any symbol tables from the linked files as well. */
5627 if (do_follow_links
&& is_mainfile
)
5631 for (i
= first_separate_info
; i
!= NULL
; i
= i
->next
)
5633 asymbol
** extra_syms
;
5634 long old_symcount
= symcount
;
5636 extra_syms
= slurp_symtab (i
->handle
);
5640 if (old_symcount
== 0)
5646 syms
= xrealloc (syms
, ((symcount
+ old_symcount
+ 1)
5647 * sizeof (asymbol
*)));
5648 memcpy (syms
+ old_symcount
,
5650 (symcount
+ 1) * sizeof (asymbol
*));
5654 symcount
+= old_symcount
;
5659 if (is_mainfile
|| process_links
)
5661 if (dump_section_headers
)
5662 dump_headers (abfd
);
5664 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
5665 || (disassemble
&& bfd_get_dynamic_symtab_upper_bound (abfd
) > 0))
5666 dynsyms
= slurp_dynamic_symtab (abfd
);
5670 synthcount
= bfd_get_synthetic_symtab (abfd
, symcount
, syms
,
5671 dynsymcount
, dynsyms
,
5678 dump_symbols (abfd
, false);
5679 if (dump_dynamic_symtab
)
5680 dump_symbols (abfd
, true);
5682 if (dump_dwarf_section_info
)
5683 dump_dwarf (abfd
, is_mainfile
);
5684 if (is_mainfile
|| process_links
)
5686 if (dump_ctf_section_info
)
5687 dump_ctf (abfd
, dump_ctf_section_name
, dump_ctf_parent_name
);
5688 if (dump_sframe_section_info
)
5689 dump_section_sframe (abfd
, dump_sframe_section_name
);
5690 if (dump_stab_section_info
)
5692 if (dump_reloc_info
&& ! disassemble
)
5694 if (dump_dynamic_reloc_info
&& ! disassemble
)
5695 dump_dynamic_relocs (abfd
);
5696 if (dump_section_contents
)
5699 disassemble_data (abfd
);
5706 dhandle
= read_debugging_info (abfd
, syms
, symcount
, true);
5707 if (dhandle
!= NULL
)
5709 if (!print_debugging_info (stdout
, dhandle
, abfd
, syms
,
5711 dump_debugging_tags
!= 0))
5713 non_fatal (_("%s: printing debugging information failed"),
5714 bfd_get_filename (abfd
));
5718 /* PR 6483: If there was no STABS debug info in the file, try
5720 else if (! dump_dwarf_section_info
)
5722 dwarf_select_sections_all ();
5723 dump_dwarf (abfd
, is_mainfile
);
5750 free_debug_memory ();
5754 display_object_bfd (bfd
*abfd
)
5758 if (bfd_check_format_matches (abfd
, bfd_object
, &matching
))
5760 dump_bfd (abfd
, true);
5764 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
5766 my_bfd_nonfatal (bfd_get_filename (abfd
));
5767 list_matching_formats (matching
);
5771 if (bfd_get_error () != bfd_error_file_not_recognized
)
5773 my_bfd_nonfatal (bfd_get_filename (abfd
));
5777 if (bfd_check_format_matches (abfd
, bfd_core
, &matching
))
5779 dump_bfd (abfd
, true);
5783 my_bfd_nonfatal (bfd_get_filename (abfd
));
5785 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
5786 list_matching_formats (matching
);
5790 display_any_bfd (bfd
*file
, int level
)
5792 /* Decompress sections unless dumping the section contents. */
5793 if (!dump_section_contents
|| decompressed_dumps
)
5794 file
->flags
|= BFD_DECOMPRESS
;
5796 /* If the file is an archive, process all of its elements. */
5797 if (bfd_check_format (file
, bfd_archive
))
5800 bfd
*last_arfile
= NULL
;
5803 printf (_("In archive %s:\n"), sanitize_string (bfd_get_filename (file
)));
5804 else if (level
> 100)
5806 /* Prevent corrupted files from spinning us into an
5807 infinite loop. 100 is an arbitrary heuristic. */
5808 non_fatal (_("Archive nesting is too deep"));
5813 printf (_("In nested archive %s:\n"),
5814 sanitize_string (bfd_get_filename (file
)));
5818 bfd_set_error (bfd_error_no_error
);
5820 arfile
= bfd_openr_next_archived_file (file
, arfile
);
5823 if (bfd_get_error () != bfd_error_no_more_archived_files
)
5824 my_bfd_nonfatal (bfd_get_filename (file
));
5828 display_any_bfd (arfile
, level
+ 1);
5830 if (last_arfile
!= NULL
)
5832 bfd_close (last_arfile
);
5833 /* PR 17512: file: ac585d01. */
5834 if (arfile
== last_arfile
)
5840 last_arfile
= arfile
;
5843 if (last_arfile
!= NULL
)
5844 bfd_close (last_arfile
);
5847 display_object_bfd (file
);
5851 display_file (char *filename
, char *target
, bool last_file
)
5855 if (get_file_size (filename
) < 1)
5861 file
= bfd_openr (filename
, target
);
5864 my_bfd_nonfatal (filename
);
5868 display_any_bfd (file
, 0);
5870 /* This is an optimization to improve the speed of objdump, especially when
5871 dumping a file with lots of associated debug informatiom. Calling
5872 bfd_close on such a file can take a non-trivial amount of time as there
5873 are lots of lists to walk and buffers to free. This is only really
5874 necessary however if we are about to load another file and we need the
5875 memory back. Otherwise, if we are about to exit, then we can save (a lot
5876 of) time by only doing a quick close, and allowing the OS to reclaim the
5881 bfd_close_all_done (file
);
5885 main (int argc
, char **argv
)
5888 char *target
= default_target
;
5889 bool seenflag
= false;
5891 #ifdef HAVE_LC_MESSAGES
5892 setlocale (LC_MESSAGES
, "");
5894 setlocale (LC_CTYPE
, "");
5896 bindtextdomain (PACKAGE
, LOCALEDIR
);
5897 textdomain (PACKAGE
);
5899 program_name
= *argv
;
5900 xmalloc_set_program_name (program_name
);
5901 bfd_set_error_program_name (program_name
);
5903 expandargv (&argc
, &argv
);
5905 if (bfd_init () != BFD_INIT_MAGIC
)
5906 fatal (_("fatal error: libbfd ABI mismatch"));
5907 set_default_bfd_target ();
5909 while ((c
= getopt_long (argc
, argv
,
5910 "CDE:FGHI:LM:P:RSTU:VW::Zab:defghij:lm:prstvwxz",
5911 long_options
, (int *) 0))
5917 break; /* We've been given a long option. */
5922 decompressed_dumps
= true;
5927 if (disassembler_options
)
5928 /* Ignore potential memory leak for now. */
5929 options
= concat (disassembler_options
, ",",
5930 optarg
, (const char *) NULL
);
5933 disassembler_options
= remove_whitespace_and_extra_commas (options
);
5940 display_file_offsets
= true;
5943 with_line_numbers
= true;
5952 enum demangling_styles style
;
5954 style
= cplus_demangle_name_to_style (optarg
);
5955 if (style
== unknown_demangling
)
5956 fatal (_("unknown demangling style `%s'"),
5959 cplus_demangle_set_style (style
);
5962 case OPTION_RECURSE_LIMIT
:
5963 demangle_flags
&= ~ DMGL_NO_RECURSE_LIMIT
;
5965 case OPTION_NO_RECURSE_LIMIT
:
5966 demangle_flags
|= DMGL_NO_RECURSE_LIMIT
;
5969 do_wide
= wide_output
= true;
5971 case OPTION_ADJUST_VMA
:
5972 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
5974 case OPTION_START_ADDRESS
:
5975 start_address
= parse_vma (optarg
, "--start-address");
5976 if ((stop_address
!= (bfd_vma
) -1) && stop_address
<= start_address
)
5977 fatal (_("error: the start address should be before the end address"));
5979 case OPTION_STOP_ADDRESS
:
5980 stop_address
= parse_vma (optarg
, "--stop-address");
5981 if ((start_address
!= (bfd_vma
) -1) && stop_address
<= start_address
)
5982 fatal (_("error: the stop address should be after the start address"));
5986 prefix_length
= strlen (prefix
);
5987 /* Remove an unnecessary trailing '/' */
5988 while (IS_DIR_SEPARATOR (prefix
[prefix_length
- 1]))
5991 case OPTION_PREFIX_STRIP
:
5992 prefix_strip
= atoi (optarg
);
5993 if (prefix_strip
< 0)
5994 fatal (_("error: prefix strip must be non-negative"));
5996 case OPTION_INSN_WIDTH
:
5997 insn_width
= strtoul (optarg
, NULL
, 0);
5998 if (insn_width
<= 0)
5999 fatal (_("error: instruction width must be positive"));
6001 case OPTION_INLINES
:
6002 unwind_inlines
= true;
6004 case OPTION_VISUALIZE_JUMPS
:
6005 visualize_jumps
= true;
6006 color_output
= false;
6007 extended_color_output
= false;
6010 if (streq (optarg
, "color"))
6011 color_output
= true;
6012 else if (streq (optarg
, "extended-color"))
6014 color_output
= true;
6015 extended_color_output
= true;
6017 else if (streq (optarg
, "off"))
6018 visualize_jumps
= false;
6021 non_fatal (_("unrecognized argument to --visualize-option"));
6026 case OPTION_DISASSEMBLER_COLOR
:
6027 if (streq (optarg
, "off"))
6028 disassembler_color
= off
;
6029 else if (streq (optarg
, "terminal"))
6030 disassembler_color
= on_if_terminal_output
;
6031 else if (streq (optarg
, "color")
6032 || streq (optarg
, "colour")
6033 || streq (optarg
, "on"))
6034 disassembler_color
= on
;
6035 else if (streq (optarg
, "extended")
6036 || streq (optarg
, "extended-color")
6037 || streq (optarg
, "extended-colour"))
6038 disassembler_color
= extended
;
6041 non_fatal (_("unrecognized argument to --disassembler-color"));
6046 if (strcmp (optarg
, "B") == 0)
6047 endian
= BFD_ENDIAN_BIG
;
6048 else if (strcmp (optarg
, "L") == 0)
6049 endian
= BFD_ENDIAN_LITTLE
;
6052 non_fatal (_("unrecognized -E option"));
6057 if (strncmp (optarg
, "big", strlen (optarg
)) == 0)
6058 endian
= BFD_ENDIAN_BIG
;
6059 else if (strncmp (optarg
, "little", strlen (optarg
)) == 0)
6060 endian
= BFD_ENDIAN_LITTLE
;
6063 non_fatal (_("unrecognized --endian type `%s'"), optarg
);
6069 dump_file_header
= true;
6073 formats_info
= true;
6077 add_include_path (optarg
);
6080 dump_private_headers
= true;
6084 dump_private_options
= optarg
;
6088 dump_private_headers
= true;
6090 dump_reloc_info
= true;
6091 dump_file_header
= true;
6092 dump_ar_hdrs
= true;
6093 dump_section_headers
= true;
6101 dump_dynamic_symtab
= true;
6107 disasm_sym
= optarg
;
6110 disassemble_zeroes
= true;
6114 disassemble_all
= true;
6119 with_source_code
= true;
6122 case OPTION_SOURCE_COMMENT
:
6124 with_source_code
= true;
6127 source_comment
= xstrdup (sanitize_string (optarg
));
6129 source_comment
= xstrdup ("# ");
6137 dump_debugging_tags
= 1;
6142 process_links
= true;
6143 do_follow_links
= true;
6149 if (dwarf_select_sections_by_letters (optarg
))
6150 dump_dwarf_section_info
= true;
6154 dump_dwarf_section_info
= true;
6155 dwarf_select_sections_all ();
6162 if (dwarf_select_sections_by_names (optarg
))
6163 dump_dwarf_section_info
= true;
6167 dwarf_select_sections_all ();
6168 dump_dwarf_section_info
= true;
6171 case OPTION_DWARF_DEPTH
:
6174 dwarf_cutoff_level
= strtoul (optarg
, & cp
, 0);
6177 case OPTION_DWARF_START
:
6180 dwarf_start_die
= strtoul (optarg
, & cp
, 0);
6181 suppress_bfd_header
= 1;
6184 case OPTION_DWARF_CHECK
:
6187 #ifdef ENABLE_LIBCTF
6189 dump_ctf_section_info
= true;
6191 dump_ctf_section_name
= xstrdup (optarg
);
6194 case OPTION_CTF_PARENT
:
6195 dump_ctf_parent_name
= xstrdup (optarg
);
6199 dump_sframe_section_info
= true;
6201 dump_sframe_section_name
= xstrdup (optarg
);
6205 dump_stab_section_info
= true;
6209 dump_section_contents
= true;
6213 dump_reloc_info
= true;
6217 dump_dynamic_reloc_info
= true;
6221 dump_ar_hdrs
= true;
6225 dump_section_headers
= true;
6230 show_version
= true;
6235 if (streq (optarg
, "default") || streq (optarg
, "d"))
6236 unicode_display
= unicode_default
;
6237 else if (streq (optarg
, "locale") || streq (optarg
, "l"))
6238 unicode_display
= unicode_locale
;
6239 else if (streq (optarg
, "escape") || streq (optarg
, "e"))
6240 unicode_display
= unicode_escape
;
6241 else if (streq (optarg
, "invalid") || streq (optarg
, "i"))
6242 unicode_display
= unicode_invalid
;
6243 else if (streq (optarg
, "hex") || streq (optarg
, "x"))
6244 unicode_display
= unicode_hex
;
6245 else if (streq (optarg
, "highlight") || streq (optarg
, "h"))
6246 unicode_display
= unicode_highlight
;
6248 fatal (_("invalid argument to -U/--unicode: %s"), optarg
);
6253 /* No need to set seenflag or to break - usage() does not return. */
6259 if (disassembler_color
== on_if_terminal_output
)
6260 disassembler_color
= isatty (1) ? on
: off
;
6263 print_version ("objdump");
6268 dump_any_debugging
= (dump_debugging
6269 || dump_dwarf_section_info
6271 || with_source_code
);
6274 exit_status
= display_info ();
6278 display_file ("a.out", target
, true);
6280 for (; optind
< argc
;)
6282 display_file (argv
[optind
], target
, optind
== argc
- 1);
6288 free (dump_ctf_section_name
);
6289 free (dump_ctf_parent_name
);
6290 free ((void *) source_comment
);