1 /* objdump.c -- dump information about an object file.
2 Copyright (C) 1990-2023 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. */
58 #include "demanguse.h"
61 #include "sframe-api.h"
63 #include "safe-ctype.h"
65 #include "libiberty.h"
67 #include "filenames.h"
76 #ifdef HAVE_LIBDEBUGINFOD
77 #include <elfutils/debuginfod.h>
80 /* Internal headers for the ELF .stab-dump code - sorry. */
81 #define BYTES_IN_WORD 32
82 #include "aout/aout64.h"
85 static int exit_status
= 0;
87 static char *default_target
= NULL
; /* Default at runtime. */
89 /* The following variables are set based on arguments passed on the
91 static int show_version
= 0; /* Show the version number. */
92 static int dump_section_contents
; /* -s */
93 static int dump_section_headers
; /* -h */
94 static bool dump_file_header
; /* -f */
95 static int dump_symtab
; /* -t */
96 static int dump_dynamic_symtab
; /* -T */
97 static int dump_reloc_info
; /* -r */
98 static int dump_dynamic_reloc_info
; /* -R */
99 static int dump_ar_hdrs
; /* -a */
100 static int dump_private_headers
; /* -p */
101 static char *dump_private_options
; /* -P */
102 static int no_addresses
; /* --no-addresses */
103 static int prefix_addresses
; /* --prefix-addresses */
104 static int with_line_numbers
; /* -l */
105 static bool with_source_code
; /* -S */
106 static int show_raw_insn
; /* --show-raw-insn */
107 static int dump_dwarf_section_info
; /* --dwarf */
108 static int dump_stab_section_info
; /* --stabs */
109 static int dump_ctf_section_info
; /* --ctf */
110 static char *dump_ctf_section_name
;
111 static char *dump_ctf_parent_name
; /* --ctf-parent */
112 static int dump_sframe_section_info
; /* --sframe */
113 static char *dump_sframe_section_name
;
114 static int do_demangle
; /* -C, --demangle */
115 static bool disassemble
; /* -d */
116 static bool disassemble_all
; /* -D */
117 static int disassemble_zeroes
; /* --disassemble-zeroes */
118 static bool formats_info
; /* -i */
119 static int wide_output
; /* -w */
120 static int insn_width
; /* --insn-width */
121 static bfd_vma start_address
= (bfd_vma
) -1; /* --start-address */
122 static bfd_vma stop_address
= (bfd_vma
) -1; /* --stop-address */
123 static int dump_debugging
; /* --debugging */
124 static int dump_debugging_tags
; /* --debugging-tags */
125 static int suppress_bfd_header
;
126 static int dump_special_syms
= 0; /* --special-syms */
127 static bfd_vma adjust_section_vma
= 0; /* --adjust-vma */
128 static int file_start_context
= 0; /* --file-start-context */
129 static bool display_file_offsets
; /* -F */
130 static const char *prefix
; /* --prefix */
131 static int prefix_strip
; /* --prefix-strip */
132 static size_t prefix_length
;
133 static bool unwind_inlines
; /* --inlines. */
134 static const char * disasm_sym
; /* Disassembly start symbol. */
135 static const char * source_comment
; /* --source_comment. */
136 static bool visualize_jumps
= false; /* --visualize-jumps. */
137 static bool color_output
= false; /* --visualize-jumps=color. */
138 static bool extended_color_output
= false; /* --visualize-jumps=extended-color. */
139 static int process_links
= false; /* --process-links. */
140 static int show_all_symbols
; /* --show-all-symbols. */
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 -g, --debugging Display debug information in object file\n"));
283 fprintf (stream
, _("\
284 -e, --debugging-tags Display debug information using ctags style\n"));
285 fprintf (stream
, _("\
286 -G, --stabs Display (in raw form) any STABS info in the file\n"));
287 fprintf (stream
, _("\
288 -W, --dwarf[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
289 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
290 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
291 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
293 Display the contents of DWARF debug sections\n"));
294 fprintf (stream
, _("\
295 -Wk,--dwarf=links Display the contents of sections that link to\n\
296 separate debuginfo files\n"));
297 #if DEFAULT_FOR_FOLLOW_LINKS
298 fprintf (stream
, _("\
299 -WK,--dwarf=follow-links\n\
300 Follow links to separate debug info files (default)\n"));
301 fprintf (stream
, _("\
302 -WN,--dwarf=no-follow-links\n\
303 Do not follow links to separate debug info files\n"));
305 fprintf (stream
, _("\
306 -WK,--dwarf=follow-links\n\
307 Follow links to separate debug info files\n"));
308 fprintf (stream
, _("\
309 -WN,--dwarf=no-follow-links\n\
310 Do not follow links to separate debug info files\n\
313 #if HAVE_LIBDEBUGINFOD
314 fprintf (stream
, _("\
315 -WD --dwarf=use-debuginfod\n\
316 When following links, also query debuginfod servers (default)\n"));
317 fprintf (stream
, _("\
318 -WE --dwarf=do-not-use-debuginfod\n\
319 When following links, do not query debuginfod servers\n"));
321 fprintf (stream
, _("\
322 -L, --process-links Display the contents of non-debug sections in\n\
323 separate debuginfo files. (Implies -WK)\n"));
325 fprintf (stream
, _("\
326 --ctf[=SECTION] Display CTF info from SECTION, (default `.ctf')\n"));
328 fprintf (stream
, _("\
329 --sframe[=SECTION] Display SFrame info from SECTION, (default '.sframe')\n"));
330 fprintf (stream
, _("\
331 -t, --syms Display the contents of the symbol table(s)\n"));
332 fprintf (stream
, _("\
333 -T, --dynamic-syms Display the contents of the dynamic symbol table\n"));
334 fprintf (stream
, _("\
335 -r, --reloc Display the relocation entries in the file\n"));
336 fprintf (stream
, _("\
337 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n"));
338 fprintf (stream
, _("\
339 @<file> Read options from <file>\n"));
340 fprintf (stream
, _("\
341 -v, --version Display this program's version number\n"));
342 fprintf (stream
, _("\
343 -i, --info List object formats and architectures supported\n"));
344 fprintf (stream
, _("\
345 -H, --help Display this information\n"));
349 const struct objdump_private_desc
* const *desc
;
351 fprintf (stream
, _("\n The following switches are optional:\n"));
352 fprintf (stream
, _("\
353 -b, --target=BFDNAME Specify the target object format as BFDNAME\n"));
354 fprintf (stream
, _("\
355 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n"));
356 fprintf (stream
, _("\
357 -j, --section=NAME Only display information for section NAME\n"));
358 fprintf (stream
, _("\
359 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n"));
360 fprintf (stream
, _("\
361 -EB --endian=big Assume big endian format when disassembling\n"));
362 fprintf (stream
, _("\
363 -EL --endian=little Assume little endian format when disassembling\n"));
364 fprintf (stream
, _("\
365 --file-start-context Include context from start of file (with -S)\n"));
366 fprintf (stream
, _("\
367 -I, --include=DIR Add DIR to search list for source files\n"));
368 fprintf (stream
, _("\
369 -l, --line-numbers Include line numbers and filenames in output\n"));
370 fprintf (stream
, _("\
371 -F, --file-offsets Include file offsets when displaying information\n"));
372 fprintf (stream
, _("\
373 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n"));
374 display_demangler_styles (stream
, _("\
376 fprintf (stream
, _("\
377 --recurse-limit Enable a limit on recursion whilst demangling\n\
379 fprintf (stream
, _("\
380 --no-recurse-limit Disable a limit on recursion whilst demangling\n"));
381 fprintf (stream
, _("\
382 -w, --wide Format output for more than 80 columns\n"));
383 fprintf (stream
, _("\
384 -U[d|l|i|x|e|h] Controls the display of UTF-8 unicode characters\n\
385 --unicode=[default|locale|invalid|hex|escape|highlight]\n"));
386 fprintf (stream
, _("\
387 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n"));
388 fprintf (stream
, _("\
389 --start-address=ADDR Only process data whose address is >= ADDR\n"));
390 fprintf (stream
, _("\
391 --stop-address=ADDR Only process data whose address is < ADDR\n"));
392 fprintf (stream
, _("\
393 --no-addresses Do not print address alongside disassembly\n"));
394 fprintf (stream
, _("\
395 --prefix-addresses Print complete address alongside disassembly\n"));
396 fprintf (stream
, _("\
397 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n"));
398 fprintf (stream
, _("\
399 --insn-width=WIDTH Display WIDTH bytes on a single line for -d\n"));
400 fprintf (stream
, _("\
401 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n"));
402 fprintf (stream
, _("\
403 --show-all-symbols When disassembling, display all symbols at a given address\n"));
404 fprintf (stream
, _("\
405 --special-syms Include special symbols in symbol dumps\n"));
406 fprintf (stream
, _("\
407 --inlines Print all inlines for source line (with -l)\n"));
408 fprintf (stream
, _("\
409 --prefix=PREFIX Add PREFIX to absolute paths for -S\n"));
410 fprintf (stream
, _("\
411 --prefix-strip=LEVEL Strip initial directory names for -S\n"));
412 fprintf (stream
, _("\
413 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
414 fprintf (stream
, _("\
415 --dwarf-start=N Display DIEs starting at offset N\n"));
416 fprintf (stream
, _("\
417 --dwarf-check Make additional dwarf consistency checks.\n"));
419 fprintf (stream
, _("\
420 --ctf-parent=NAME Use CTF archive member NAME as the CTF parent\n"));
422 fprintf (stream
, _("\
423 --visualize-jumps Visualize jumps by drawing ASCII art lines\n"));
424 fprintf (stream
, _("\
425 --visualize-jumps=color Use colors in the ASCII art\n"));
426 fprintf (stream
, _("\
427 --visualize-jumps=extended-color\n\
428 Use extended 8-bit color codes\n"));
429 fprintf (stream
, _("\
430 --visualize-jumps=off Disable jump visualization\n"));
431 #if DEFAULT_FOR_COLORED_DISASSEMBLY
432 fprintf (stream
, _("\
433 --disassembler-color=off Disable disassembler color output.\n"));
434 fprintf (stream
, _("\
435 --disassembler-color=terminal Enable disassembler color output if displaying on a terminal. (default)\n"));
437 fprintf (stream
, _("\
438 --disassembler-color=off Disable disassembler color output. (default)\n"));
439 fprintf (stream
, _("\
440 --disassembler-color=terminal Enable disassembler color output if displaying on a terminal.\n"));
442 fprintf (stream
, _("\
443 --disassembler-color=on Enable disassembler color output.\n"));
444 fprintf (stream
, _("\
445 --disassembler-color=extended Use 8-bit colors in disassembler output.\n\n"));
447 list_supported_targets (program_name
, stream
);
448 list_supported_architectures (program_name
, stream
);
450 disassembler_usage (stream
);
452 if (objdump_private_vectors
[0] != NULL
)
455 _("\nOptions supported for -P/--private switch:\n"));
456 for (desc
= objdump_private_vectors
; *desc
!= NULL
; desc
++)
457 (*desc
)->help (stream
);
460 if (REPORT_BUGS_TO
[0] && status
== 0)
461 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
465 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
469 OPTION_START_ADDRESS
,
479 OPTION_RECURSE_LIMIT
,
480 OPTION_NO_RECURSE_LIMIT
,
482 OPTION_SOURCE_COMMENT
,
488 OPTION_VISUALIZE_JUMPS
,
489 OPTION_DISASSEMBLER_COLOR
492 static struct option long_options
[]=
494 {"adjust-vma", required_argument
, NULL
, OPTION_ADJUST_VMA
},
495 {"all-headers", no_argument
, NULL
, 'x'},
496 {"architecture", required_argument
, NULL
, 'm'},
497 {"archive-headers", no_argument
, NULL
, 'a'},
499 {"ctf", optional_argument
, NULL
, OPTION_CTF
},
500 {"ctf-parent", required_argument
, NULL
, OPTION_CTF_PARENT
},
502 {"debugging", no_argument
, NULL
, 'g'},
503 {"debugging-tags", no_argument
, NULL
, 'e'},
504 {"demangle", optional_argument
, NULL
, 'C'},
505 {"disassemble", optional_argument
, NULL
, 'd'},
506 {"disassemble-all", no_argument
, NULL
, 'D'},
507 {"disassemble-zeroes", no_argument
, NULL
, 'z'},
508 {"disassembler-options", required_argument
, NULL
, 'M'},
509 {"dwarf", optional_argument
, NULL
, OPTION_DWARF
},
510 {"dwarf-check", no_argument
, 0, OPTION_DWARF_CHECK
},
511 {"dwarf-depth", required_argument
, 0, OPTION_DWARF_DEPTH
},
512 {"dwarf-start", required_argument
, 0, OPTION_DWARF_START
},
513 {"dynamic-reloc", no_argument
, NULL
, 'R'},
514 {"dynamic-syms", no_argument
, NULL
, 'T'},
515 {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
516 {"file-headers", no_argument
, NULL
, 'f'},
517 {"file-offsets", no_argument
, NULL
, 'F'},
518 {"file-start-context", no_argument
, &file_start_context
, 1},
519 {"full-contents", no_argument
, NULL
, 's'},
520 {"headers", no_argument
, NULL
, 'h'},
521 {"help", no_argument
, NULL
, 'H'},
522 {"include", required_argument
, NULL
, 'I'},
523 {"info", no_argument
, NULL
, 'i'},
524 {"inlines", no_argument
, 0, OPTION_INLINES
},
525 {"insn-width", required_argument
, NULL
, OPTION_INSN_WIDTH
},
526 {"line-numbers", no_argument
, NULL
, 'l'},
527 {"no-addresses", no_argument
, &no_addresses
, 1},
528 {"no-recurse-limit", no_argument
, NULL
, OPTION_NO_RECURSE_LIMIT
},
529 {"no-recursion-limit", no_argument
, NULL
, OPTION_NO_RECURSE_LIMIT
},
530 {"no-show-raw-insn", no_argument
, &show_raw_insn
, -1},
531 {"prefix", required_argument
, NULL
, OPTION_PREFIX
},
532 {"prefix-addresses", no_argument
, &prefix_addresses
, 1},
533 {"prefix-strip", required_argument
, NULL
, OPTION_PREFIX_STRIP
},
534 {"private", required_argument
, NULL
, 'P'},
535 {"private-headers", no_argument
, NULL
, 'p'},
536 {"process-links", no_argument
, &process_links
, true},
537 {"recurse-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
538 {"recursion-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
539 {"reloc", no_argument
, NULL
, 'r'},
540 {"section", required_argument
, NULL
, 'j'},
541 {"section-headers", no_argument
, NULL
, 'h'},
542 {"sframe", optional_argument
, NULL
, OPTION_SFRAME
},
543 {"show-all-symbols", no_argument
, &show_all_symbols
, 1},
544 {"show-raw-insn", no_argument
, &show_raw_insn
, 1},
545 {"source", no_argument
, NULL
, 'S'},
546 {"source-comment", optional_argument
, NULL
, OPTION_SOURCE_COMMENT
},
547 {"special-syms", no_argument
, &dump_special_syms
, 1},
548 {"stabs", no_argument
, NULL
, 'G'},
549 {"start-address", required_argument
, NULL
, OPTION_START_ADDRESS
},
550 {"stop-address", required_argument
, NULL
, OPTION_STOP_ADDRESS
},
551 {"syms", no_argument
, NULL
, 't'},
552 {"target", required_argument
, NULL
, 'b'},
553 {"unicode", required_argument
, NULL
, 'U'},
554 {"version", no_argument
, NULL
, 'V'},
555 {"visualize-jumps", optional_argument
, 0, OPTION_VISUALIZE_JUMPS
},
556 {"wide", no_argument
, NULL
, 'w'},
557 {"disassembler-color", required_argument
, NULL
, OPTION_DISASSEMBLER_COLOR
},
558 {NULL
, no_argument
, NULL
, 0}
562 nonfatal (const char *msg
)
568 /* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
569 The conversion format is controlled by the unicode_display variable.
570 Returns the number of characters added to OUT.
571 Returns the number of bytes consumed from IN in CONSUMED.
572 Always consumes at least one byte and displays at least one character. */
575 display_utf8 (const unsigned char * in
, char * out
, unsigned int * consumed
)
577 char * orig_out
= out
;
578 unsigned int nchars
= 0;
581 if (unicode_display
== unicode_default
)
587 if ((in
[1] & 0xc0) != 0x80)
590 if ((in
[0] & 0x20) == 0)
596 if ((in
[2] & 0xc0) != 0x80)
599 if ((in
[0] & 0x10) == 0)
605 if ((in
[3] & 0xc0) != 0x80)
611 switch (unicode_display
)
614 /* Copy the bytes into the output buffer as is. */
615 memcpy (out
, in
, nchars
);
619 case unicode_invalid
:
621 out
+= sprintf (out
, "%c", unicode_display
== unicode_hex
? '<' : '{');
622 out
+= sprintf (out
, "0x");
623 for (j
= 0; j
< nchars
; j
++)
624 out
+= sprintf (out
, "%02x", in
[j
]);
625 out
+= sprintf (out
, "%c", unicode_display
== unicode_hex
? '>' : '}');
628 case unicode_highlight
:
630 out
+= sprintf (out
, "\x1B[31;47m"); /* Red. */
636 out
+= sprintf (out
, "\\u%02x%02x",
637 ((in
[0] & 0x1c) >> 2),
638 ((in
[0] & 0x03) << 6) | (in
[1] & 0x3f));
642 out
+= sprintf (out
, "\\u%02x%02x",
643 ((in
[0] & 0x0f) << 4) | ((in
[1] & 0x3c) >> 2),
644 ((in
[1] & 0x03) << 6) | ((in
[2] & 0x3f)));
648 out
+= sprintf (out
, "\\u%02x%02x%02x",
649 ((in
[0] & 0x07) << 6) | ((in
[1] & 0x3c) >> 2),
650 ((in
[1] & 0x03) << 6) | ((in
[2] & 0x3c) >> 2),
651 ((in
[2] & 0x03) << 6) | ((in
[3] & 0x3f)));
658 if (unicode_display
== unicode_highlight
&& isatty (1))
659 out
+= sprintf (out
, "\033[0m"); /* Default colour. */
668 return out
- orig_out
;
671 /* Not a valid UTF-8 sequence. */
677 /* Returns a version of IN with any control characters
678 replaced by escape sequences. Uses a static buffer
681 If unicode display is enabled, then also handles the
682 conversion of unicode characters. */
685 sanitize_string (const char * in
)
687 static char * buffer
= NULL
;
688 static size_t buffer_len
= 0;
689 const char * original
= in
;
696 /* See if any conversion is necessary. In the majority
697 of cases it will not be needed. */
700 unsigned char c
= *in
++;
708 if (unicode_display
!= unicode_default
&& c
>= 0xc0)
713 /* Copy the input, translating as needed. */
715 if (buffer_len
< (strlen (in
) * 9))
717 free ((void *) buffer
);
718 buffer_len
= strlen (in
) * 9;
719 buffer
= xmalloc (buffer_len
+ 1);
725 unsigned char c
= *in
++;
735 else if (unicode_display
!= unicode_default
&& c
>= 0xc0)
737 unsigned int num_consumed
;
739 out
+= display_utf8 ((const unsigned char *)(in
- 1), out
, & num_consumed
);
740 in
+= num_consumed
- 1;
752 /* Returns TRUE if the specified section should be dumped. */
755 process_section_p (asection
* section
)
759 if (only_list
== NULL
)
762 for (only
= only_list
; only
; only
= only
->next
)
763 if (strcmp (only
->name
, section
->name
) == 0)
772 /* Add an entry to the 'only' list. */
775 add_only (char * name
)
779 /* First check to make sure that we do not
780 already have an entry for this name. */
781 for (only
= only_list
; only
; only
= only
->next
)
782 if (strcmp (only
->name
, name
) == 0)
785 only
= xmalloc (sizeof * only
);
788 only
->next
= only_list
;
792 /* Release the memory used by the 'only' list.
793 PR 11225: Issue a warning message for unseen sections.
794 Only do this if none of the sections were seen. This is mainly to support
795 tools like the GAS testsuite where an object file is dumped with a list of
796 generic section names known to be present in a range of different file
800 free_only_list (void)
802 bool at_least_one_seen
= false;
806 if (only_list
== NULL
)
809 for (only
= only_list
; only
; only
= only
->next
)
812 at_least_one_seen
= true;
816 for (only
= only_list
; only
; only
= next
)
818 if (! at_least_one_seen
)
820 non_fatal (_("section '%s' mentioned in a -j option, "
821 "but not found in any input file"),
832 dump_section_header (bfd
*abfd
, asection
*section
, void *data
)
835 unsigned int opb
= bfd_octets_per_byte (abfd
, section
);
836 int longest_section_name
= *((int *) data
);
838 /* Ignore linker created section. See elfNN_ia64_object_p in
840 if (section
->flags
& SEC_LINKER_CREATED
)
843 /* PR 10413: Skip sections that we are ignoring. */
844 if (! process_section_p (section
))
847 printf ("%3d %-*s %08lx ", section
->index
, longest_section_name
,
848 sanitize_string (bfd_section_name (section
)),
849 (unsigned long) bfd_section_size (section
) / opb
);
850 bfd_printf_vma (abfd
, bfd_section_vma (section
));
852 bfd_printf_vma (abfd
, section
->lma
);
853 printf (" %08lx 2**%u", (unsigned long) section
->filepos
,
854 bfd_section_alignment (section
));
860 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
862 PF (SEC_HAS_CONTENTS
, "CONTENTS");
863 PF (SEC_ALLOC
, "ALLOC");
864 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
865 PF (SEC_LOAD
, "LOAD");
866 PF (SEC_RELOC
, "RELOC");
867 PF (SEC_READONLY
, "READONLY");
868 PF (SEC_CODE
, "CODE");
869 PF (SEC_DATA
, "DATA");
871 PF (SEC_DEBUGGING
, "DEBUGGING");
872 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
873 PF (SEC_EXCLUDE
, "EXCLUDE");
874 PF (SEC_SORT_ENTRIES
, "SORT_ENTRIES");
875 if (bfd_get_arch (abfd
) == bfd_arch_tic54x
)
877 PF (SEC_TIC54X_BLOCK
, "BLOCK");
878 PF (SEC_TIC54X_CLINK
, "CLINK");
880 PF (SEC_SMALL_DATA
, "SMALL_DATA");
881 if (bfd_get_flavour (abfd
) == bfd_target_coff_flavour
)
883 PF (SEC_COFF_SHARED
, "SHARED");
884 PF (SEC_COFF_NOREAD
, "NOREAD");
886 else if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
888 PF (SEC_ELF_OCTETS
, "OCTETS");
889 PF (SEC_ELF_PURECODE
, "PURECODE");
891 PF (SEC_THREAD_LOCAL
, "THREAD_LOCAL");
892 PF (SEC_GROUP
, "GROUP");
893 if (bfd_get_arch (abfd
) == bfd_arch_mep
)
895 PF (SEC_MEP_VLIW
, "VLIW");
898 if ((section
->flags
& SEC_LINK_ONCE
) != 0)
901 struct coff_comdat_info
*comdat
;
903 switch (section
->flags
& SEC_LINK_DUPLICATES
)
907 case SEC_LINK_DUPLICATES_DISCARD
:
908 ls
= "LINK_ONCE_DISCARD";
910 case SEC_LINK_DUPLICATES_ONE_ONLY
:
911 ls
= "LINK_ONCE_ONE_ONLY";
913 case SEC_LINK_DUPLICATES_SAME_SIZE
:
914 ls
= "LINK_ONCE_SAME_SIZE";
916 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
917 ls
= "LINK_ONCE_SAME_CONTENTS";
920 printf ("%s%s", comma
, ls
);
922 comdat
= bfd_coff_get_comdat_section (abfd
, section
);
924 printf (" (COMDAT %s %ld)", comdat
->name
, comdat
->symbol
);
933 /* Called on each SECTION in ABFD, update the int variable pointed to by
934 DATA which contains the string length of the longest section name. */
937 find_longest_section_name (bfd
*abfd ATTRIBUTE_UNUSED
,
938 asection
*section
, void *data
)
940 int *longest_so_far
= (int *) data
;
944 /* Ignore linker created section. */
945 if (section
->flags
& SEC_LINKER_CREATED
)
948 /* Skip sections that we are ignoring. */
949 if (! process_section_p (section
))
952 name
= bfd_section_name (section
);
953 len
= (int) strlen (name
);
954 if (len
> *longest_so_far
)
955 *longest_so_far
= len
;
959 dump_headers (bfd
*abfd
)
961 /* The default width of 13 is just an arbitrary choice. */
962 int max_section_name_length
= 13;
968 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
969 if (bfd_get_arch_size (abfd
) == 32)
975 printf (_("Sections:\n"));
978 bfd_map_over_sections (abfd
, find_longest_section_name
,
979 &max_section_name_length
);
981 printf (_("Idx %-*s Size %-*s%-*sFile off Algn"),
982 max_section_name_length
, "Name",
983 bfd_vma_width
, "VMA",
984 bfd_vma_width
, "LMA");
987 printf (_(" Flags"));
990 bfd_map_over_sections (abfd
, dump_section_header
,
991 &max_section_name_length
);
995 slurp_symtab (bfd
*abfd
)
1000 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
1006 storage
= bfd_get_symtab_upper_bound (abfd
);
1009 non_fatal (_("failed to read symbol table from: %s"), bfd_get_filename (abfd
));
1010 bfd_fatal (_("error message was"));
1015 off_t filesize
= bfd_get_file_size (abfd
);
1019 && filesize
< storage
1020 /* The MMO file format supports its own special compression
1021 technique, so its sections can be larger than the file size. */
1022 && bfd_get_flavour (abfd
) != bfd_target_mmo_flavour
)
1024 bfd_nonfatal_message (bfd_get_filename (abfd
), abfd
, NULL
,
1025 _("error: symbol table size (%#lx) "
1026 "is larger than filesize (%#lx)"),
1027 storage
, (long) filesize
);
1033 sy
= (asymbol
**) xmalloc (storage
);
1036 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
1038 bfd_fatal (bfd_get_filename (abfd
));
1042 /* Read in the dynamic symbols. */
1045 slurp_dynamic_symtab (bfd
*abfd
)
1047 asymbol
**sy
= NULL
;
1050 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
1053 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
1055 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd
));
1061 bfd_fatal (bfd_get_filename (abfd
));
1065 sy
= (asymbol
**) xmalloc (storage
);
1067 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
1068 if (dynsymcount
< 0)
1069 bfd_fatal (bfd_get_filename (abfd
));
1073 /* Some symbol names are significant and should be kept in the
1074 table of sorted symbol names, even if they are marked as
1075 debugging/section symbols. */
1078 is_significant_symbol_name (const char * name
)
1080 return startswith (name
, ".plt") || startswith (name
, ".got");
1083 /* Filter out (in place) symbols that are useless for disassembly.
1084 COUNT is the number of elements in SYMBOLS.
1085 Return the number of useful symbols. */
1088 remove_useless_symbols (asymbol
**symbols
, long count
)
1090 asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
1092 while (--count
>= 0)
1094 asymbol
*sym
= *in_ptr
++;
1096 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
1098 if ((sym
->flags
& (BSF_DEBUGGING
| BSF_SECTION_SYM
))
1099 && ! is_significant_symbol_name (sym
->name
))
1101 if (bfd_is_und_section (sym
->section
)
1102 || bfd_is_com_section (sym
->section
))
1107 return out_ptr
- symbols
;
1110 static const asection
*compare_section
;
1112 /* Sort symbols into value order. */
1115 compare_symbols (const void *ap
, const void *bp
)
1117 const asymbol
*a
= * (const asymbol
**) ap
;
1118 const asymbol
*b
= * (const asymbol
**) bp
;
1123 bool as
, af
, bs
, bf
;
1127 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
1129 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
1132 /* Prefer symbols from the section currently being disassembled.
1133 Don't sort symbols from other sections by section, since there
1134 isn't much reason to prefer one section over another otherwise.
1135 See sym_ok comment for why we compare by section name. */
1136 as
= strcmp (compare_section
->name
, a
->section
->name
) == 0;
1137 bs
= strcmp (compare_section
->name
, b
->section
->name
) == 0;
1143 an
= bfd_asymbol_name (a
);
1144 bn
= bfd_asymbol_name (b
);
1148 /* The symbols gnu_compiled and gcc2_compiled convey no real
1149 information, so put them after other symbols with the same value. */
1150 af
= (strstr (an
, "gnu_compiled") != NULL
1151 || strstr (an
, "gcc2_compiled") != NULL
);
1152 bf
= (strstr (bn
, "gnu_compiled") != NULL
1153 || strstr (bn
, "gcc2_compiled") != NULL
);
1160 /* We use a heuristic for the file name, to try to sort it after
1161 more useful symbols. It may not work on non Unix systems, but it
1162 doesn't really matter; the only difference is precisely which
1163 symbol names get printed. */
1165 #define file_symbol(s, sn, snl) \
1166 (((s)->flags & BSF_FILE) != 0 \
1168 && (sn)[(snl) - 2] == '.' \
1169 && ((sn)[(snl) - 1] == 'o' \
1170 || (sn)[(snl) - 1] == 'a')))
1172 af
= file_symbol (a
, an
, anl
);
1173 bf
= file_symbol (b
, bn
, bnl
);
1180 /* Sort function and object symbols before global symbols before
1181 local symbols before section symbols before debugging symbols. */
1186 if ((aflags
& BSF_DEBUGGING
) != (bflags
& BSF_DEBUGGING
))
1188 if ((aflags
& BSF_DEBUGGING
) != 0)
1193 if ((aflags
& BSF_SECTION_SYM
) != (bflags
& BSF_SECTION_SYM
))
1195 if ((aflags
& BSF_SECTION_SYM
) != 0)
1200 if ((aflags
& BSF_FUNCTION
) != (bflags
& BSF_FUNCTION
))
1202 if ((aflags
& BSF_FUNCTION
) != 0)
1207 if ((aflags
& BSF_OBJECT
) != (bflags
& BSF_OBJECT
))
1209 if ((aflags
& BSF_OBJECT
) != 0)
1214 if ((aflags
& BSF_LOCAL
) != (bflags
& BSF_LOCAL
))
1216 if ((aflags
& BSF_LOCAL
) != 0)
1221 if ((aflags
& BSF_GLOBAL
) != (bflags
& BSF_GLOBAL
))
1223 if ((aflags
& BSF_GLOBAL
) != 0)
1229 /* Sort larger size ELF symbols before smaller. See PR20337. */
1231 if ((a
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0
1232 && bfd_get_flavour (bfd_asymbol_bfd (a
)) == bfd_target_elf_flavour
)
1233 asz
= ((elf_symbol_type
*) a
)->internal_elf_sym
.st_size
;
1235 if ((b
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0
1236 && bfd_get_flavour (bfd_asymbol_bfd (b
)) == bfd_target_elf_flavour
)
1237 bsz
= ((elf_symbol_type
*) b
)->internal_elf_sym
.st_size
;
1239 return asz
> bsz
? -1 : 1;
1241 /* Symbols that start with '.' might be section names, so sort them
1242 after symbols that don't start with '.'. */
1243 if (an
[0] == '.' && bn
[0] != '.')
1245 if (an
[0] != '.' && bn
[0] == '.')
1248 /* Finally, if we can't distinguish them in any other way, try to
1249 get consistent results by sorting the symbols by name. */
1250 return strcmp (an
, bn
);
1253 /* Sort relocs into address order. */
1256 compare_relocs (const void *ap
, const void *bp
)
1258 const arelent
*a
= * (const arelent
**) ap
;
1259 const arelent
*b
= * (const arelent
**) bp
;
1261 if (a
->address
> b
->address
)
1263 else if (a
->address
< b
->address
)
1266 /* So that associated relocations tied to the same address show up
1267 in the correct order, we don't do any further sorting. */
1276 /* Print an address (VMA) to the output stream in INFO.
1277 If SKIP_ZEROES is TRUE, omit leading zeroes. */
1280 objdump_print_value (bfd_vma vma
, struct disassemble_info
*inf
,
1285 struct objdump_disasm_info
*aux
;
1287 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1288 bfd_sprintf_vma (aux
->abfd
, buf
, vma
);
1293 for (p
= buf
; *p
== '0'; ++p
)
1298 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_address
, "%s", p
);
1301 /* Print the name of a symbol. */
1304 objdump_print_symname (bfd
*abfd
, struct disassemble_info
*inf
,
1308 const char *name
, *version_string
= NULL
;
1309 bool hidden
= false;
1312 name
= bfd_asymbol_name (sym
);
1313 if (do_demangle
&& name
[0] != '\0')
1315 /* Demangle the name. */
1316 alloc
= bfd_demangle (abfd
, name
, demangle_flags
);
1321 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0)
1322 version_string
= bfd_get_symbol_version_string (abfd
, sym
, true,
1325 if (bfd_is_und_section (bfd_asymbol_section (sym
)))
1328 name
= sanitize_string (name
);
1332 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_symbol
, "%s", name
);
1333 if (version_string
&& *version_string
!= '\0')
1334 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_symbol
,
1335 hidden
? "@%s" : "@@%s",
1340 printf ("%s", name
);
1341 if (version_string
&& *version_string
!= '\0')
1342 printf (hidden
? "@%s" : "@@%s", version_string
);
1350 sym_ok (bool want_section
,
1351 bfd
*abfd ATTRIBUTE_UNUSED
,
1354 struct disassemble_info
*inf
)
1358 /* NB: An object file can have different sections with the same
1359 section name. Compare compare section pointers if they have
1361 if (sorted_syms
[place
]->section
->owner
== sec
->owner
1362 && sorted_syms
[place
]->section
!= sec
)
1365 /* Note - we cannot just compare section pointers because they could
1366 be different, but the same... Ie the symbol that we are trying to
1367 find could have come from a separate debug info file. Under such
1368 circumstances the symbol will be associated with a section in the
1369 debug info file, whilst the section we want is in a normal file.
1370 So the section pointers will be different, but the section names
1371 will be the same. */
1372 if (strcmp (bfd_section_name (sorted_syms
[place
]->section
),
1373 bfd_section_name (sec
)) != 0)
1377 return inf
->symbol_is_valid (sorted_syms
[place
], inf
);
1380 /* Locate a symbol given a bfd and a section (from INFO->application_data),
1381 and a VMA. If INFO->application_data->require_sec is TRUE, then always
1382 require the symbol to be in the section. Returns NULL if there is no
1383 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
1384 of the symbol in sorted_syms. */
1387 find_symbol_for_address (bfd_vma vma
,
1388 struct disassemble_info
*inf
,
1391 /* @@ Would it speed things up to cache the last two symbols returned,
1392 and maybe their address ranges? For many processors, only one memory
1393 operand can be present at a time, so the 2-entry cache wouldn't be
1394 constantly churned by code doing heavy memory accesses. */
1396 /* Indices in `sorted_syms'. */
1398 long max_count
= sorted_symcount
;
1400 struct objdump_disasm_info
*aux
;
1407 if (sorted_symcount
< 1)
1410 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1413 opb
= inf
->octets_per_byte
;
1415 /* Perform a binary search looking for the closest symbol to the
1416 required value. We are searching the range (min, max_count]. */
1417 while (min
+ 1 < max_count
)
1421 thisplace
= (max_count
+ min
) / 2;
1422 sym
= sorted_syms
[thisplace
];
1424 if (bfd_asymbol_value (sym
) > vma
)
1425 max_count
= thisplace
;
1426 else if (bfd_asymbol_value (sym
) < vma
)
1435 /* The symbol we want is now in min, the low end of the range we
1436 were searching. If there are several symbols with the same
1437 value, we want the first one. */
1439 while (thisplace
> 0
1440 && (bfd_asymbol_value (sorted_syms
[thisplace
])
1441 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
1444 /* Prefer a symbol in the current section if we have multple symbols
1445 with the same value, as can occur with overlays or zero size
1448 while (min
< max_count
1449 && (bfd_asymbol_value (sorted_syms
[min
])
1450 == bfd_asymbol_value (sorted_syms
[thisplace
])))
1452 if (sym_ok (true, abfd
, min
, sec
, inf
))
1459 return sorted_syms
[thisplace
];
1464 /* If the file is relocatable, and the symbol could be from this
1465 section, prefer a symbol from this section over symbols from
1466 others, even if the other symbol's value might be closer.
1468 Note that this may be wrong for some symbol references if the
1469 sections have overlapping memory ranges, but in that case there's
1470 no way to tell what's desired without looking at the relocation
1473 Also give the target a chance to reject symbols. */
1474 want_section
= (aux
->require_sec
1475 || ((abfd
->flags
& HAS_RELOC
) != 0
1476 && vma
>= bfd_section_vma (sec
)
1477 && vma
< (bfd_section_vma (sec
)
1478 + bfd_section_size (sec
) / opb
)));
1480 if (! sym_ok (want_section
, abfd
, thisplace
, sec
, inf
))
1483 long newplace
= sorted_symcount
;
1485 for (i
= min
- 1; i
>= 0; i
--)
1487 if (sym_ok (want_section
, abfd
, i
, sec
, inf
))
1489 if (newplace
== sorted_symcount
)
1492 if (bfd_asymbol_value (sorted_syms
[i
])
1493 != bfd_asymbol_value (sorted_syms
[newplace
]))
1496 /* Remember this symbol and keep searching until we reach
1497 an earlier address. */
1502 if (newplace
!= sorted_symcount
)
1503 thisplace
= newplace
;
1506 /* We didn't find a good symbol with a smaller value.
1507 Look for one with a larger value. */
1508 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
1510 if (sym_ok (want_section
, abfd
, i
, sec
, inf
))
1518 if (! sym_ok (want_section
, abfd
, thisplace
, sec
, inf
))
1519 /* There is no suitable symbol. */
1523 /* If we have not found an exact match for the specified address
1524 and we have dynamic relocations available, then we can produce
1525 a better result by matching a relocation to the address and
1526 using the symbol associated with that relocation. */
1527 rel_count
= inf
->dynrelcount
;
1529 && sorted_syms
[thisplace
]->value
!= vma
1531 && inf
->dynrelbuf
!= NULL
1532 && inf
->dynrelbuf
[0]->address
<= vma
1533 && inf
->dynrelbuf
[rel_count
- 1]->address
>= vma
1534 /* If we have matched a synthetic symbol, then stick with that. */
1535 && (sorted_syms
[thisplace
]->flags
& BSF_SYNTHETIC
) == 0)
1538 arelent
** rel_high
;
1540 rel_low
= inf
->dynrelbuf
;
1541 rel_high
= rel_low
+ rel_count
- 1;
1542 while (rel_low
<= rel_high
)
1544 arelent
**rel_mid
= &rel_low
[(rel_high
- rel_low
) / 2];
1545 arelent
* rel
= *rel_mid
;
1547 if (rel
->address
== vma
)
1549 /* Absolute relocations do not provide a more helpful
1550 symbolic address. Find a non-absolute relocation
1551 with the same address. */
1552 arelent
**rel_vma
= rel_mid
;
1554 rel_mid
>= rel_low
&& rel_mid
[0]->address
== vma
;
1558 for (; rel_vma
<= rel_high
&& rel_vma
[0]->address
== vma
;
1562 if (rel
->sym_ptr_ptr
!= NULL
1563 && ! bfd_is_abs_section ((* rel
->sym_ptr_ptr
)->section
))
1566 * place
= thisplace
;
1567 return * rel
->sym_ptr_ptr
;
1573 if (vma
< rel
->address
)
1575 else if (vma
>= rel_mid
[1]->address
)
1576 rel_low
= rel_mid
+ 1;
1585 return sorted_syms
[thisplace
];
1588 /* Print an address and the offset to the nearest symbol. */
1591 objdump_print_addr_with_sym (bfd
*abfd
, asection
*sec
, asymbol
*sym
,
1592 bfd_vma vma
, struct disassemble_info
*inf
,
1597 objdump_print_value (vma
, inf
, skip_zeroes
);
1598 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, " ");
1605 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
,"<");
1606 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_symbol
, "%s",
1607 sanitize_string (bfd_section_name (sec
)));
1608 secaddr
= bfd_section_vma (sec
);
1611 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
,
1613 objdump_print_value (secaddr
- vma
, inf
, true);
1615 else if (vma
> secaddr
)
1617 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
, "+0x");
1618 objdump_print_value (vma
- secaddr
, inf
, true);
1620 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, ">");
1624 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, "<");
1626 objdump_print_symname (abfd
, inf
, sym
);
1628 if (bfd_asymbol_value (sym
) == vma
)
1630 /* Undefined symbols in an executables and dynamic objects do not have
1631 a value associated with them, so it does not make sense to display
1632 an offset relative to them. Normally we would not be provided with
1633 this kind of symbol, but the target backend might choose to do so,
1634 and the code in find_symbol_for_address might return an as yet
1635 unresolved symbol associated with a dynamic reloc. */
1636 else if ((bfd_get_file_flags (abfd
) & (EXEC_P
| DYNAMIC
))
1637 && bfd_is_und_section (sym
->section
))
1639 else if (bfd_asymbol_value (sym
) > vma
)
1641 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
,"-0x");
1642 objdump_print_value (bfd_asymbol_value (sym
) - vma
, inf
, true);
1644 else if (vma
> bfd_asymbol_value (sym
))
1646 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
, "+0x");
1647 objdump_print_value (vma
- bfd_asymbol_value (sym
), inf
, true);
1650 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, ">");
1653 if (display_file_offsets
)
1654 inf
->fprintf_styled_func (inf
->stream
, dis_style_text
,
1655 _(" (File Offset: 0x%lx)"),
1656 (long int)(sec
->filepos
+ (vma
- sec
->vma
)));
1659 /* Print an address (VMA), symbolically if possible.
1660 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
1663 objdump_print_addr (bfd_vma vma
,
1664 struct disassemble_info
*inf
,
1667 struct objdump_disasm_info
*aux
;
1668 asymbol
*sym
= NULL
;
1669 bool skip_find
= false;
1671 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1673 if (sorted_symcount
< 1)
1677 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_address
, "0x");
1678 objdump_print_value (vma
, inf
, skip_zeroes
);
1681 if (display_file_offsets
)
1682 inf
->fprintf_styled_func (inf
->stream
, dis_style_text
,
1683 _(" (File Offset: 0x%lx)"),
1684 (long int) (inf
->section
->filepos
1685 + (vma
- inf
->section
->vma
)));
1689 if (aux
->reloc
!= NULL
1690 && aux
->reloc
->sym_ptr_ptr
!= NULL
1691 && * aux
->reloc
->sym_ptr_ptr
!= NULL
)
1693 sym
= * aux
->reloc
->sym_ptr_ptr
;
1695 /* Adjust the vma to the reloc. */
1696 vma
+= bfd_asymbol_value (sym
);
1698 if (bfd_is_und_section (bfd_asymbol_section (sym
)))
1703 sym
= find_symbol_for_address (vma
, inf
, NULL
);
1705 objdump_print_addr_with_sym (aux
->abfd
, inf
->section
, sym
, vma
, inf
,
1709 /* Print VMA to INFO. This function is passed to the disassembler
1713 objdump_print_address (bfd_vma vma
, struct disassemble_info
*inf
)
1715 objdump_print_addr (vma
, inf
, ! prefix_addresses
);
1718 /* Determine if the given address has a symbol associated with it. */
1721 objdump_symbol_at_address (bfd_vma vma
, struct disassemble_info
* inf
)
1725 sym
= find_symbol_for_address (vma
, inf
, NULL
);
1726 if (sym
!= NULL
&& bfd_asymbol_value (sym
) == vma
)
1732 /* Hold the last function name and the last line number we displayed
1733 in a disassembly. */
1735 static char *prev_functionname
;
1736 static unsigned int prev_line
;
1737 static unsigned int prev_discriminator
;
1739 /* We keep a list of all files that we have seen when doing a
1740 disassembly with source, so that we know how much of the file to
1741 display. This can be important for inlined functions. */
1743 struct print_file_list
1745 struct print_file_list
*next
;
1746 const char *filename
;
1747 const char *modname
;
1750 const char **linemap
;
1753 unsigned max_printed
;
1757 static struct print_file_list
*print_files
;
1759 /* The number of preceding context lines to show when we start
1760 displaying a file for the first time. */
1762 #define SHOW_PRECEDING_CONTEXT_LINES (5)
1764 #if HAVE_LIBDEBUGINFOD
1765 /* Return a hex string represention of the build-id. */
1768 get_build_id (void * data
)
1771 char * build_id_str
;
1772 bfd
* abfd
= (bfd
*) data
;
1773 const struct bfd_build_id
* build_id
;
1775 build_id
= abfd
->build_id
;
1776 if (build_id
== NULL
)
1779 build_id_str
= malloc (build_id
->size
* 2 + 1);
1780 if (build_id_str
== NULL
)
1783 for (i
= 0; i
< build_id
->size
; i
++)
1784 sprintf (build_id_str
+ (i
* 2), "%02x", build_id
->data
[i
]);
1785 build_id_str
[build_id
->size
* 2] = '\0';
1787 return (unsigned char *) build_id_str
;
1790 /* Search for a separate debug file matching ABFD's build-id. */
1793 find_separate_debug (const bfd
* abfd
)
1795 const struct bfd_build_id
* build_id
= abfd
->build_id
;
1796 separate_info
* i
= first_separate_info
;
1798 if (build_id
== NULL
|| i
== NULL
)
1803 const bfd
* i_bfd
= (bfd
*) i
->handle
;
1805 if (abfd
!= NULL
&& i_bfd
->build_id
!= NULL
)
1807 const unsigned char * data
= i_bfd
->build_id
->data
;
1808 size_t size
= i_bfd
->build_id
->size
;
1810 if (size
== build_id
->size
1811 && memcmp (data
, build_id
->data
, size
) == 0)
1812 return (bfd
*) i
->handle
;
1821 /* Search for a separate debug file matching ABFD's .gnu_debugaltlink
1825 find_alt_debug (const bfd
* abfd
)
1830 struct dwarf_section
* section
;
1831 const struct bfd_build_id
* build_id
= abfd
->build_id
;
1832 separate_info
* i
= first_separate_info
;
1836 || !load_debug_section (gnu_debugaltlink
, (void *) abfd
))
1839 section
= &debug_displays
[gnu_debugaltlink
].section
;
1840 if (section
== NULL
)
1843 name
= (const char *) section
->start
;
1844 namelen
= strnlen (name
, section
->size
) + 1;
1847 if (namelen
>= section
->size
)
1850 id_len
= section
->size
- namelen
;
1854 /* Compare the .gnu_debugaltlink build-id with the build-ids of the
1855 known separate_info files. */
1858 const bfd
* i_bfd
= (bfd
*) i
->handle
;
1860 if (i_bfd
!= NULL
&& i_bfd
->build_id
!= NULL
)
1862 const unsigned char * data
= i_bfd
->build_id
->data
;
1863 size_t size
= i_bfd
->build_id
->size
;
1866 && memcmp (section
->start
+ namelen
, data
, size
) == 0)
1867 return (bfd
*) i
->handle
;
1876 #endif /* HAVE_LIBDEBUGINFOD */
1878 /* Reads the contents of file FN into memory. Returns a pointer to the buffer.
1879 Also returns the size of the buffer in SIZE_RETURN and a filled out
1880 stat structure in FST_RETURN. Returns NULL upon failure. */
1883 slurp_file (const char * fn
,
1884 size_t * size_return
,
1885 struct stat
* fst_return
,
1886 bfd
* abfd ATTRIBUTE_UNUSED
)
1896 if (fn
== NULL
|| * fn
== 0 || size_return
== NULL
|| fst_return
== NULL
)
1899 fd
= open (fn
, O_RDONLY
| O_BINARY
);
1901 #if HAVE_LIBDEBUGINFOD
1902 if (fd
< 0 && use_debuginfod
&& fn
[0] == '/' && abfd
!= NULL
)
1904 unsigned char *build_id
= get_build_id (abfd
);
1908 debuginfod_client
*client
= debuginfod_begin ();
1912 fd
= debuginfod_find_source (client
, build_id
, 0, fn
, NULL
);
1913 debuginfod_end (client
);
1923 if (fstat (fd
, fst_return
) < 0)
1929 *size_return
= fst_return
->st_size
;
1932 ps
= getpagesize ();
1933 msize
= (*size_return
+ ps
- 1) & ~(ps
- 1);
1934 map
= mmap (NULL
, msize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1935 if (map
!= (char *) -1L)
1942 map
= (const char *) malloc (*size_return
);
1943 if (!map
|| (size_t) read (fd
, (char *) map
, *size_return
) != *size_return
)
1945 free ((void *) map
);
1952 #define line_map_decrease 5
1954 /* Precompute array of lines for a mapped file. */
1956 static const char **
1957 index_file (const char *map
, size_t size
, unsigned int *maxline
)
1959 const char *p
, *lstart
, *end
;
1960 int chars_per_line
= 45; /* First iteration will use 40. */
1961 unsigned int lineno
;
1962 const char **linemap
= NULL
;
1963 unsigned long line_map_size
= 0;
1969 for (p
= map
; p
< end
; p
++)
1973 if (p
+ 1 < end
&& p
[1] == '\r')
1976 else if (*p
== '\r')
1978 if (p
+ 1 < end
&& p
[1] == '\n')
1984 /* End of line found. */
1986 if (linemap
== NULL
|| line_map_size
< lineno
+ 1)
1988 unsigned long newsize
;
1990 chars_per_line
-= line_map_decrease
;
1991 if (chars_per_line
<= 1)
1993 line_map_size
= size
/ chars_per_line
+ 1;
1994 if (line_map_size
< lineno
+ 1)
1995 line_map_size
= lineno
+ 1;
1996 newsize
= line_map_size
* sizeof (char *);
1997 linemap
= (const char **) xrealloc (linemap
, newsize
);
2000 linemap
[lineno
++] = lstart
;
2008 /* Tries to open MODNAME, and if successful adds a node to print_files
2009 linked list and returns that node. Also fills in the stat structure
2010 pointed to by FST_RETURN. Returns NULL on failure. */
2012 static struct print_file_list
*
2013 try_print_file_open (const char * origname
,
2014 const char * modname
,
2015 struct stat
* fst_return
,
2018 struct print_file_list
*p
;
2020 p
= (struct print_file_list
*) xmalloc (sizeof (struct print_file_list
));
2022 p
->map
= slurp_file (modname
, &p
->mapsize
, fst_return
, abfd
);
2029 p
->linemap
= index_file (p
->map
, p
->mapsize
, &p
->maxline
);
2032 p
->filename
= origname
;
2033 p
->modname
= modname
;
2034 p
->next
= print_files
;
2040 /* If the source file, as described in the symtab, is not found
2041 try to locate it in one of the paths specified with -I
2042 If found, add location to print_files linked list. */
2044 static struct print_file_list
*
2045 update_source_path (const char *filename
, bfd
*abfd
)
2047 struct print_file_list
*p
;
2052 p
= try_print_file_open (filename
, filename
, &fst
, abfd
);
2055 if (include_path_count
== 0)
2058 /* Get the name of the file. */
2059 fname
= lbasename (filename
);
2061 /* If file exists under a new path, we need to add it to the list
2062 so that show_line knows about it. */
2063 for (i
= 0; i
< include_path_count
; i
++)
2065 char *modname
= concat (include_paths
[i
], "/", fname
,
2068 p
= try_print_file_open (filename
, modname
, &fst
, abfd
);
2078 long mtime
= bfd_get_mtime (abfd
);
2080 if (fst
.st_mtime
> mtime
)
2081 warn (_("source file %s is more recent than object file\n"),
2088 /* Print a source file line. */
2091 print_line (struct print_file_list
*p
, unsigned int linenum
)
2096 if (linenum
>= p
->maxline
)
2098 l
= p
->linemap
[linenum
];
2099 if (source_comment
!= NULL
&& strlen (l
) > 0)
2100 printf ("%s", source_comment
);
2101 len
= strcspn (l
, "\n\r");
2102 /* Test fwrite return value to quiet glibc warning. */
2103 if (len
== 0 || fwrite (l
, len
, 1, stdout
) == 1)
2107 /* Print a range of source code lines. */
2110 dump_lines (struct print_file_list
*p
, unsigned int start
, unsigned int end
)
2118 print_line (p
, start
);
2123 /* Show the line number, or the source line, in a disassembly
2127 show_line (bfd
*abfd
, asection
*section
, bfd_vma addr_offset
)
2129 const char *filename
;
2130 const char *functionname
;
2131 unsigned int linenumber
;
2132 unsigned int discriminator
;
2136 if (! with_line_numbers
&& ! with_source_code
)
2139 #ifdef HAVE_LIBDEBUGINFOD
2142 const char *alt_filename
= NULL
;
2148 /* PR 29075: Check for separate debuginfo and .gnu_debugaltlink files.
2149 They need to be passed to bfd_find_nearest_line_with_alt in case they
2150 were downloaded from debuginfod. Otherwise libbfd will attempt to
2151 search for them and fail to locate them. */
2152 debug_bfd
= find_separate_debug (abfd
);
2153 if (debug_bfd
== NULL
)
2156 alt_bfd
= find_alt_debug (debug_bfd
);
2157 if (alt_bfd
!= NULL
)
2158 alt_filename
= bfd_get_filename (alt_bfd
);
2163 bfd_set_error (bfd_error_no_error
);
2164 if (! bfd_find_nearest_line_with_alt (debug_bfd
, alt_filename
,
2166 addr_offset
, &filename
,
2167 &functionname
, &linenumber
,
2170 if (bfd_get_error () == bfd_error_no_error
)
2172 if (! bfd_find_nearest_line_discriminator (abfd
, section
, syms
,
2173 addr_offset
, &filename
,
2174 &functionname
, &linenumber
,
2180 if (! bfd_find_nearest_line_discriminator (abfd
, section
, syms
, addr_offset
,
2181 &filename
, &functionname
,
2182 &linenumber
, &discriminator
))
2186 if (filename
!= NULL
&& *filename
== '\0')
2188 if (functionname
!= NULL
&& *functionname
== '\0')
2189 functionname
= NULL
;
2192 && IS_ABSOLUTE_PATH (filename
)
2196 const char *fname
= filename
;
2198 path
= xmalloc (prefix_length
+ 1 + strlen (filename
));
2201 memcpy (path
, prefix
, prefix_length
);
2202 path_up
= path
+ prefix_length
;
2204 /* Build relocated filename, stripping off leading directories
2205 from the initial filename if requested. */
2206 if (prefix_strip
> 0)
2211 /* Skip selected directory levels. */
2212 for (s
= fname
+ 1; *s
!= '\0' && level
< prefix_strip
; s
++)
2213 if (IS_DIR_SEPARATOR (*s
))
2220 /* Update complete filename. */
2221 strcpy (path_up
, fname
);
2229 if (with_line_numbers
)
2231 if (functionname
!= NULL
2232 && (prev_functionname
== NULL
2233 || strcmp (functionname
, prev_functionname
) != 0))
2235 char *demangle_alloc
= NULL
;
2236 if (do_demangle
&& functionname
[0] != '\0')
2238 /* Demangle the name. */
2239 demangle_alloc
= bfd_demangle (abfd
, functionname
,
2243 /* Demangling adds trailing parens, so don't print those. */
2244 if (demangle_alloc
!= NULL
)
2245 printf ("%s:\n", sanitize_string (demangle_alloc
));
2247 printf ("%s():\n", sanitize_string (functionname
));
2250 free (demangle_alloc
);
2253 && (linenumber
!= prev_line
2254 || discriminator
!= prev_discriminator
))
2256 if (discriminator
> 0)
2257 printf ("%s:%u (discriminator %u)\n",
2258 filename
== NULL
? "???" : sanitize_string (filename
),
2259 linenumber
, discriminator
);
2261 printf ("%s:%u\n", filename
== NULL
2262 ? "???" : sanitize_string (filename
),
2267 const char *filename2
;
2268 const char *functionname2
;
2271 while (bfd_find_inliner_info (abfd
, &filename2
, &functionname2
,
2274 printf ("inlined by %s:%u",
2275 sanitize_string (filename2
), line2
);
2276 printf (" (%s)\n", sanitize_string (functionname2
));
2281 if (with_source_code
2285 struct print_file_list
**pp
, *p
;
2288 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
2289 if (filename_cmp ((*pp
)->filename
, filename
) == 0)
2296 filename
= xstrdup (filename
);
2297 p
= update_source_path (filename
, abfd
);
2300 if (p
!= NULL
&& linenumber
!= p
->last_line
)
2302 if (file_start_context
&& p
->first
)
2306 l
= linenumber
- SHOW_PRECEDING_CONTEXT_LINES
;
2307 if (l
>= linenumber
)
2309 if (p
->max_printed
>= l
)
2311 if (p
->max_printed
< linenumber
)
2312 l
= p
->max_printed
+ 1;
2317 dump_lines (p
, l
, linenumber
);
2318 if (p
->max_printed
< linenumber
)
2319 p
->max_printed
= linenumber
;
2320 p
->last_line
= linenumber
;
2325 if (functionname
!= NULL
2326 && (prev_functionname
== NULL
2327 || strcmp (functionname
, prev_functionname
) != 0))
2329 if (prev_functionname
!= NULL
)
2330 free (prev_functionname
);
2331 prev_functionname
= (char *) xmalloc (strlen (functionname
) + 1);
2332 strcpy (prev_functionname
, functionname
);
2335 if (linenumber
> 0 && linenumber
!= prev_line
)
2336 prev_line
= linenumber
;
2338 if (discriminator
!= prev_discriminator
)
2339 prev_discriminator
= discriminator
;
2345 /* Pseudo FILE object for strings. */
2353 /* sprintf to a "stream". */
2355 static int ATTRIBUTE_PRINTF_2
2356 objdump_sprintf (SFILE
*f
, const char *format
, ...)
2363 size_t space
= f
->alloc
- f
->pos
;
2365 va_start (args
, format
);
2366 n
= vsnprintf (f
->buffer
+ f
->pos
, space
, format
, args
);
2372 f
->alloc
= (f
->alloc
+ n
) * 2;
2373 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2380 /* Return an integer greater than, or equal to zero, representing the color
2381 for STYLE, or -1 if no color should be used. */
2384 objdump_color_for_disassembler_style (enum disassembler_style style
)
2388 if (style
== dis_style_comment_start
)
2389 disassembler_in_comment
= true;
2391 if (disassembler_color
== on
)
2393 if (disassembler_in_comment
)
2398 case dis_style_symbol
:
2401 case dis_style_assembler_directive
:
2402 case dis_style_sub_mnemonic
:
2403 case dis_style_mnemonic
:
2406 case dis_style_register
:
2409 case dis_style_address
:
2410 case dis_style_address_offset
:
2411 case dis_style_immediate
:
2415 case dis_style_text
:
2420 else if (disassembler_color
== extended
)
2422 if (disassembler_in_comment
)
2427 case dis_style_symbol
:
2430 case dis_style_assembler_directive
:
2431 case dis_style_sub_mnemonic
:
2432 case dis_style_mnemonic
:
2435 case dis_style_register
:
2438 case dis_style_address
:
2439 case dis_style_address_offset
:
2440 case dis_style_immediate
:
2444 case dis_style_text
:
2449 else if (disassembler_color
!= off
)
2450 bfd_fatal (_("disassembly color not correctly selected"));
2455 /* Like objdump_sprintf, but add in escape sequences to highlight the
2456 content according to STYLE. */
2458 static int ATTRIBUTE_PRINTF_3
2459 objdump_styled_sprintf (SFILE
*f
, enum disassembler_style style
,
2460 const char *format
, ...)
2464 int color
= objdump_color_for_disassembler_style (style
);
2470 size_t space
= f
->alloc
- f
->pos
;
2472 if (disassembler_color
== on
)
2473 n
= snprintf (f
->buffer
+ f
->pos
, space
, "\033[%dm", color
);
2475 n
= snprintf (f
->buffer
+ f
->pos
, space
, "\033[38;5;%dm", color
);
2479 f
->alloc
= (f
->alloc
+ n
) * 2;
2480 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2487 size_t space
= f
->alloc
- f
->pos
;
2489 va_start (args
, format
);
2490 n
= vsnprintf (f
->buffer
+ f
->pos
, space
, format
, args
);
2496 f
->alloc
= (f
->alloc
+ n
) * 2;
2497 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2505 size_t space
= f
->alloc
- f
->pos
;
2507 n
= snprintf (f
->buffer
+ f
->pos
, space
, "\033[0m");
2512 f
->alloc
= (f
->alloc
+ n
) * 2;
2513 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2521 /* We discard the styling information here. This function is only used
2522 when objdump is printing auxiliary information, the symbol headers, and
2523 disassembly address, or the bytes of the disassembled instruction. We
2524 don't (currently) apply styling to any of this stuff, so, for now, just
2525 print the content with no additional style added. */
2527 static int ATTRIBUTE_PRINTF_3
2528 fprintf_styled (FILE *f
, enum disassembler_style style ATTRIBUTE_UNUSED
,
2529 const char *fmt
, ...)
2535 res
= vfprintf (f
, fmt
, ap
);
2541 /* Code for generating (colored) diagrams of control flow start and end
2544 /* Structure used to store the properties of a jump. */
2548 /* The next jump, or NULL if this is the last object. */
2549 struct jump_info
*next
;
2550 /* The previous jump, or NULL if this is the first object. */
2551 struct jump_info
*prev
;
2552 /* The start addresses of the jump. */
2555 /* The list of start addresses. */
2557 /* The number of elements. */
2559 /* The maximum number of elements that fit into the array. */
2562 /* The end address of the jump. */
2564 /* The drawing level of the jump. */
2568 /* Construct a jump object for a jump from start
2569 to end with the corresponding level. */
2571 static struct jump_info
*
2572 jump_info_new (bfd_vma start
, bfd_vma end
, int level
)
2574 struct jump_info
*result
= xmalloc (sizeof (struct jump_info
));
2576 result
->next
= NULL
;
2577 result
->prev
= NULL
;
2578 result
->start
.addresses
= xmalloc (sizeof (bfd_vma
*) * 2);
2579 result
->start
.addresses
[0] = start
;
2580 result
->start
.count
= 1;
2581 result
->start
.max_count
= 2;
2583 result
->level
= level
;
2588 /* Free a jump object and return the next object
2589 or NULL if this was the last one. */
2591 static struct jump_info
*
2592 jump_info_free (struct jump_info
*ji
)
2594 struct jump_info
*result
= NULL
;
2599 if (ji
->start
.addresses
)
2600 free (ji
->start
.addresses
);
2607 /* Get the smallest value of all start and end addresses. */
2610 jump_info_min_address (const struct jump_info
*ji
)
2612 bfd_vma min_address
= ji
->end
;
2615 for (i
= ji
->start
.count
; i
-- > 0;)
2616 if (ji
->start
.addresses
[i
] < min_address
)
2617 min_address
= ji
->start
.addresses
[i
];
2621 /* Get the largest value of all start and end addresses. */
2624 jump_info_max_address (const struct jump_info
*ji
)
2626 bfd_vma max_address
= ji
->end
;
2629 for (i
= ji
->start
.count
; i
-- > 0;)
2630 if (ji
->start
.addresses
[i
] > max_address
)
2631 max_address
= ji
->start
.addresses
[i
];
2635 /* Get the target address of a jump. */
2638 jump_info_end_address (const struct jump_info
*ji
)
2643 /* Test if an address is one of the start addresses of a jump. */
2646 jump_info_is_start_address (const struct jump_info
*ji
, bfd_vma address
)
2648 bool result
= false;
2651 for (i
= ji
->start
.count
; i
-- > 0;)
2652 if (address
== ji
->start
.addresses
[i
])
2661 /* Test if an address is the target address of a jump. */
2664 jump_info_is_end_address (const struct jump_info
*ji
, bfd_vma address
)
2666 return (address
== ji
->end
);
2669 /* Get the difference between the smallest and largest address of a jump. */
2672 jump_info_size (const struct jump_info
*ji
)
2674 return jump_info_max_address (ji
) - jump_info_min_address (ji
);
2677 /* Unlink a jump object from a list. */
2680 jump_info_unlink (struct jump_info
*node
,
2681 struct jump_info
**base
)
2684 node
->next
->prev
= node
->prev
;
2686 node
->prev
->next
= node
->next
;
2693 /* Insert unlinked jump info node into a list. */
2696 jump_info_insert (struct jump_info
*node
,
2697 struct jump_info
*target
,
2698 struct jump_info
**base
)
2700 node
->next
= target
;
2701 node
->prev
= target
->prev
;
2702 target
->prev
= node
;
2704 node
->prev
->next
= node
;
2709 /* Add unlinked node to the front of a list. */
2712 jump_info_add_front (struct jump_info
*node
,
2713 struct jump_info
**base
)
2717 node
->next
->prev
= node
;
2722 /* Move linked node to target position. */
2725 jump_info_move_linked (struct jump_info
*node
,
2726 struct jump_info
*target
,
2727 struct jump_info
**base
)
2730 jump_info_unlink (node
, base
);
2731 /* Insert node at target position. */
2732 jump_info_insert (node
, target
, base
);
2735 /* Test if two jumps intersect. */
2738 jump_info_intersect (const struct jump_info
*a
,
2739 const struct jump_info
*b
)
2741 return ((jump_info_max_address (a
) >= jump_info_min_address (b
))
2742 && (jump_info_min_address (a
) <= jump_info_max_address (b
)));
2745 /* Merge two compatible jump info objects. */
2748 jump_info_merge (struct jump_info
**base
)
2750 struct jump_info
*a
;
2752 for (a
= *base
; a
; a
= a
->next
)
2754 struct jump_info
*b
;
2756 for (b
= a
->next
; b
; b
= b
->next
)
2758 /* Merge both jumps into one. */
2759 if (a
->end
== b
->end
)
2761 /* Reallocate addresses. */
2762 size_t needed_size
= a
->start
.count
+ b
->start
.count
;
2765 if (needed_size
> a
->start
.max_count
)
2767 a
->start
.max_count
+= b
->start
.max_count
;
2768 a
->start
.addresses
=
2769 xrealloc (a
->start
.addresses
,
2770 a
->start
.max_count
* sizeof (bfd_vma
*));
2773 /* Append start addresses. */
2774 for (i
= 0; i
< b
->start
.count
; ++i
)
2775 a
->start
.addresses
[a
->start
.count
++] =
2776 b
->start
.addresses
[i
];
2778 /* Remove and delete jump. */
2779 struct jump_info
*tmp
= b
->prev
;
2780 jump_info_unlink (b
, base
);
2788 /* Sort jumps by their size and starting point using a stable
2789 minsort. This could be improved if sorting performance is
2790 an issue, for example by using mergesort. */
2793 jump_info_sort (struct jump_info
**base
)
2795 struct jump_info
*current_element
= *base
;
2797 while (current_element
)
2799 struct jump_info
*best_match
= current_element
;
2800 struct jump_info
*runner
= current_element
->next
;
2801 bfd_vma best_size
= jump_info_size (best_match
);
2805 bfd_vma runner_size
= jump_info_size (runner
);
2807 if ((runner_size
< best_size
)
2808 || ((runner_size
== best_size
)
2809 && (jump_info_min_address (runner
)
2810 < jump_info_min_address (best_match
))))
2812 best_match
= runner
;
2813 best_size
= runner_size
;
2816 runner
= runner
->next
;
2819 if (best_match
== current_element
)
2820 current_element
= current_element
->next
;
2822 jump_info_move_linked (best_match
, current_element
, base
);
2826 /* Visualize all jumps at a given address. */
2829 jump_info_visualize_address (bfd_vma address
,
2832 uint8_t *color_buffer
)
2834 struct jump_info
*ji
= detected_jumps
;
2835 size_t len
= (max_level
+ 1) * 3;
2837 /* Clear line buffer. */
2838 memset (line_buffer
, ' ', len
);
2839 memset (color_buffer
, 0, len
);
2841 /* Iterate over jumps and add their ASCII art. */
2844 /* Discard jumps that are never needed again. */
2845 if (jump_info_max_address (ji
) < address
)
2847 struct jump_info
*tmp
= ji
;
2850 jump_info_unlink (tmp
, &detected_jumps
);
2851 jump_info_free (tmp
);
2855 /* This jump intersects with the current address. */
2856 if (jump_info_min_address (ji
) <= address
)
2858 /* Hash target address to get an even
2859 distribution between all values. */
2860 bfd_vma hash_address
= jump_info_end_address (ji
);
2861 uint8_t color
= iterative_hash_object (hash_address
, 0);
2862 /* Fetch line offset. */
2863 int offset
= (max_level
- ji
->level
) * 3;
2865 /* Draw start line. */
2866 if (jump_info_is_start_address (ji
, address
))
2868 size_t i
= offset
+ 1;
2870 for (; i
< len
- 1; ++i
)
2871 if (line_buffer
[i
] == ' ')
2873 line_buffer
[i
] = '-';
2874 color_buffer
[i
] = color
;
2877 if (line_buffer
[i
] == ' ')
2879 line_buffer
[i
] = '-';
2880 color_buffer
[i
] = color
;
2882 else if (line_buffer
[i
] == '>')
2884 line_buffer
[i
] = 'X';
2885 color_buffer
[i
] = color
;
2888 if (line_buffer
[offset
] == ' ')
2890 if (address
<= ji
->end
)
2891 line_buffer
[offset
] =
2892 (jump_info_min_address (ji
) == address
) ? '/': '+';
2894 line_buffer
[offset
] =
2895 (jump_info_max_address (ji
) == address
) ? '\\': '+';
2896 color_buffer
[offset
] = color
;
2899 /* Draw jump target. */
2900 else if (jump_info_is_end_address (ji
, address
))
2902 size_t i
= offset
+ 1;
2904 for (; i
< len
- 1; ++i
)
2905 if (line_buffer
[i
] == ' ')
2907 line_buffer
[i
] = '-';
2908 color_buffer
[i
] = color
;
2911 if (line_buffer
[i
] == ' ')
2913 line_buffer
[i
] = '>';
2914 color_buffer
[i
] = color
;
2916 else if (line_buffer
[i
] == '-')
2918 line_buffer
[i
] = 'X';
2919 color_buffer
[i
] = color
;
2922 if (line_buffer
[offset
] == ' ')
2924 if (jump_info_min_address (ji
) < address
)
2925 line_buffer
[offset
] =
2926 (jump_info_max_address (ji
) > address
) ? '>' : '\\';
2928 line_buffer
[offset
] = '/';
2929 color_buffer
[offset
] = color
;
2932 /* Draw intermediate line segment. */
2933 else if (line_buffer
[offset
] == ' ')
2935 line_buffer
[offset
] = '|';
2936 color_buffer
[offset
] = color
;
2944 /* Clone of disassemble_bytes to detect jumps inside a function. */
2945 /* FIXME: is this correct? Can we strip it down even further? */
2947 static struct jump_info
*
2948 disassemble_jumps (struct disassemble_info
* inf
,
2949 disassembler_ftype disassemble_fn
,
2950 bfd_vma start_offset
,
2951 bfd_vma stop_offset
,
2954 arelent
** relppend
)
2956 struct objdump_disasm_info
*aux
;
2957 struct jump_info
*jumps
= NULL
;
2959 bfd_vma addr_offset
;
2960 unsigned int opb
= inf
->octets_per_byte
;
2964 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
2965 section
= inf
->section
;
2968 sfile
.buffer
= (char *) xmalloc (sfile
.alloc
);
2971 inf
->insn_info_valid
= 0;
2972 disassemble_set_printf (inf
, &sfile
, (fprintf_ftype
) objdump_sprintf
,
2973 (fprintf_styled_ftype
) objdump_styled_sprintf
);
2975 addr_offset
= start_offset
;
2976 while (addr_offset
< stop_offset
)
2978 int previous_octets
;
2980 /* Remember the length of the previous instruction. */
2981 previous_octets
= octets
;
2985 inf
->bytes_per_line
= 0;
2986 inf
->bytes_per_chunk
= 0;
2987 inf
->flags
= ((disassemble_all
? DISASSEMBLE_DATA
: 0)
2988 | (wide_output
? WIDE_OUTPUT
: 0));
2990 inf
->flags
|= USER_SPECIFIED_MACHINE_TYPE
;
2992 if (inf
->disassembler_needs_relocs
2993 && (bfd_get_file_flags (aux
->abfd
) & EXEC_P
) == 0
2994 && (bfd_get_file_flags (aux
->abfd
) & DYNAMIC
) == 0
2995 && *relppp
< relppend
)
2997 bfd_signed_vma distance_to_rel
;
2999 distance_to_rel
= (**relppp
)->address
- (rel_offset
+ addr_offset
);
3001 /* Check to see if the current reloc is associated with
3002 the instruction that we are about to disassemble. */
3003 if (distance_to_rel
== 0
3004 /* FIXME: This is wrong. We are trying to catch
3005 relocs that are addressed part way through the
3006 current instruction, as might happen with a packed
3007 VLIW instruction. Unfortunately we do not know the
3008 length of the current instruction since we have not
3009 disassembled it yet. Instead we take a guess based
3010 upon the length of the previous instruction. The
3011 proper solution is to have a new target-specific
3012 disassembler function which just returns the length
3013 of an instruction at a given address without trying
3014 to display its disassembly. */
3015 || (distance_to_rel
> 0
3016 && distance_to_rel
< (bfd_signed_vma
) (previous_octets
/ opb
)))
3018 inf
->flags
|= INSN_HAS_RELOC
;
3022 if (! disassemble_all
3023 && (section
->flags
& (SEC_CODE
| SEC_HAS_CONTENTS
))
3024 == (SEC_CODE
| SEC_HAS_CONTENTS
))
3025 /* Set a stop_vma so that the disassembler will not read
3026 beyond the next symbol. We assume that symbols appear on
3027 the boundaries between instructions. We only do this when
3028 disassembling code of course, and when -D is in effect. */
3029 inf
->stop_vma
= section
->vma
+ stop_offset
;
3031 inf
->stop_offset
= stop_offset
;
3033 /* Extract jump information. */
3034 inf
->insn_info_valid
= 0;
3035 disassembler_in_comment
= false;
3036 octets
= (*disassemble_fn
) (section
->vma
+ addr_offset
, inf
);
3037 /* Test if a jump was detected. */
3038 if (inf
->insn_info_valid
3039 && ((inf
->insn_type
== dis_branch
)
3040 || (inf
->insn_type
== dis_condbranch
)
3041 || (inf
->insn_type
== dis_jsr
)
3042 || (inf
->insn_type
== dis_condjsr
))
3043 && (inf
->target
>= section
->vma
+ start_offset
)
3044 && (inf
->target
< section
->vma
+ stop_offset
))
3046 struct jump_info
*ji
=
3047 jump_info_new (section
->vma
+ addr_offset
, inf
->target
, -1);
3048 jump_info_add_front (ji
, &jumps
);
3053 addr_offset
+= octets
/ opb
;
3056 disassemble_set_printf (inf
, (void *) stdout
, (fprintf_ftype
) fprintf
,
3057 (fprintf_styled_ftype
) fprintf_styled
);
3058 free (sfile
.buffer
);
3061 jump_info_merge (&jumps
);
3062 /* Process jumps. */
3063 jump_info_sort (&jumps
);
3065 /* Group jumps by level. */
3066 struct jump_info
*last_jump
= jumps
;
3071 /* The last jump is part of the next group. */
3072 struct jump_info
*base
= last_jump
;
3073 /* Increment level. */
3074 base
->level
= ++max_level
;
3076 /* Find jumps that can be combined on the same
3077 level, with the largest jumps tested first.
3078 This has the advantage that large jumps are on
3079 lower levels and do not intersect with small
3080 jumps that get grouped on higher levels. */
3081 struct jump_info
*exchange_item
= last_jump
->next
;
3082 struct jump_info
*it
= exchange_item
;
3084 for (; it
; it
= it
->next
)
3086 /* Test if the jump intersects with any
3087 jump from current group. */
3089 struct jump_info
*it_collision
;
3091 for (it_collision
= base
;
3092 it_collision
!= exchange_item
;
3093 it_collision
= it_collision
->next
)
3095 /* This jump intersects so we leave it out. */
3096 if (jump_info_intersect (it_collision
, it
))
3103 /* Add jump to group. */
3106 /* Move current element to the front. */
3107 if (it
!= exchange_item
)
3109 struct jump_info
*save
= it
->prev
;
3110 jump_info_move_linked (it
, exchange_item
, &jumps
);
3116 last_jump
= exchange_item
;
3117 exchange_item
= exchange_item
->next
;
3119 last_jump
->level
= max_level
;
3123 /* Move to next group. */
3124 last_jump
= exchange_item
;
3130 /* The number of zeroes we want to see before we start skipping them.
3131 The number is arbitrarily chosen. */
3133 #define DEFAULT_SKIP_ZEROES 8
3135 /* The number of zeroes to skip at the end of a section. If the
3136 number of zeroes at the end is between SKIP_ZEROES_AT_END and
3137 SKIP_ZEROES, they will be disassembled. If there are fewer than
3138 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
3139 attempt to avoid disassembling zeroes inserted by section
3142 #define DEFAULT_SKIP_ZEROES_AT_END 3
3145 null_print (const void * stream ATTRIBUTE_UNUSED
, const char * format ATTRIBUTE_UNUSED
, ...)
3150 /* Like null_print, but takes the extra STYLE argument. As this is not
3151 going to print anything, the extra argument is just ignored. */
3154 null_styled_print (const void * stream ATTRIBUTE_UNUSED
,
3155 enum disassembler_style style ATTRIBUTE_UNUSED
,
3156 const char * format ATTRIBUTE_UNUSED
, ...)
3161 /* Print out jump visualization. */
3164 print_jump_visualisation (bfd_vma addr
, int max_level
, char *line_buffer
,
3165 uint8_t *color_buffer
)
3170 jump_info_visualize_address (addr
, max_level
, line_buffer
, color_buffer
);
3172 size_t line_buffer_size
= strlen (line_buffer
);
3173 char last_color
= 0;
3176 for (i
= 0; i
<= line_buffer_size
; ++i
)
3180 uint8_t color
= (i
< line_buffer_size
) ? color_buffer
[i
]: 0;
3182 if (color
!= last_color
)
3185 if (extended_color_output
)
3186 /* Use extended 8bit color, but
3187 do not choose dark colors. */
3188 printf ("\033[38;5;%dm", 124 + (color
% 108));
3190 /* Use simple terminal colors. */
3191 printf ("\033[%dm", 31 + (color
% 7));
3198 putchar ((i
< line_buffer_size
) ? line_buffer
[i
]: ' ');
3202 /* Disassemble some data in memory between given values. */
3205 disassemble_bytes (struct disassemble_info
*inf
,
3206 disassembler_ftype disassemble_fn
,
3209 bfd_vma start_offset
,
3210 bfd_vma stop_offset
,
3215 struct objdump_disasm_info
*aux
;
3217 unsigned int octets_per_line
;
3218 unsigned int skip_addr_chars
;
3219 bfd_vma addr_offset
;
3220 unsigned int opb
= inf
->octets_per_byte
;
3221 unsigned int skip_zeroes
= inf
->skip_zeroes
;
3222 unsigned int skip_zeroes_at_end
= inf
->skip_zeroes_at_end
;
3226 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
3227 section
= inf
->section
;
3230 sfile
.buffer
= (char *) xmalloc (sfile
.alloc
);
3234 octets_per_line
= insn_width
;
3236 octets_per_line
= 4;
3238 octets_per_line
= 16;
3240 /* Figure out how many characters to skip at the start of an
3241 address, to make the disassembly look nicer. We discard leading
3242 zeroes in chunks of 4, ensuring that there is always a leading
3244 skip_addr_chars
= 0;
3245 if (!no_addresses
&& !prefix_addresses
)
3249 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ section
->size
/ opb
);
3251 while (buf
[skip_addr_chars
] == '0')
3254 /* Don't discard zeros on overflow. */
3255 if (buf
[skip_addr_chars
] == '\0' && section
->vma
!= 0)
3256 skip_addr_chars
= 0;
3258 if (skip_addr_chars
!= 0)
3259 skip_addr_chars
= (skip_addr_chars
- 1) & -4;
3262 inf
->insn_info_valid
= 0;
3264 /* Determine maximum level. */
3265 uint8_t *color_buffer
= NULL
;
3266 char *line_buffer
= NULL
;
3269 /* Some jumps were detected. */
3272 struct jump_info
*ji
;
3274 /* Find maximum jump level. */
3275 for (ji
= detected_jumps
; ji
; ji
= ji
->next
)
3277 if (ji
->level
> max_level
)
3278 max_level
= ji
->level
;
3281 /* Allocate buffers. */
3282 size_t len
= (max_level
+ 1) * 3 + 1;
3283 line_buffer
= xmalloc (len
);
3284 line_buffer
[len
- 1] = 0;
3285 color_buffer
= xmalloc (len
);
3286 color_buffer
[len
- 1] = 0;
3289 addr_offset
= start_offset
;
3290 while (addr_offset
< stop_offset
)
3292 bool need_nl
= false;
3296 /* Make sure we don't use relocs from previous instructions. */
3299 /* If we see more than SKIP_ZEROES octets of zeroes, we just
3301 if (! disassemble_zeroes
)
3302 for (; addr_offset
* opb
+ octets
< stop_offset
* opb
; octets
++)
3303 if (data
[addr_offset
* opb
+ octets
] != 0)
3305 if (! disassemble_zeroes
3306 && (inf
->insn_info_valid
== 0
3307 || inf
->branch_delay_insns
== 0)
3308 && (octets
>= skip_zeroes
3309 || (addr_offset
* opb
+ octets
== stop_offset
* opb
3310 && octets
< skip_zeroes_at_end
)))
3312 /* If there are more nonzero octets to follow, we only skip
3313 zeroes in multiples of 4, to try to avoid running over
3314 the start of an instruction which happens to start with
3316 if (addr_offset
* opb
+ octets
!= stop_offset
* opb
)
3319 /* If we are going to display more data, and we are displaying
3320 file offsets, then tell the user how many zeroes we skip
3321 and the file offset from where we resume dumping. */
3322 if (display_file_offsets
3323 && addr_offset
+ octets
/ opb
< stop_offset
)
3324 printf (_("\t... (skipping %lu zeroes, "
3325 "resuming at file offset: 0x%lx)\n"),
3326 (unsigned long) (octets
/ opb
),
3327 (unsigned long) (section
->filepos
3328 + addr_offset
+ octets
/ opb
));
3335 unsigned int bpc
= 0;
3336 unsigned int pb
= 0;
3338 if (with_line_numbers
|| with_source_code
)
3339 show_line (aux
->abfd
, section
, addr_offset
);
3343 else if (!prefix_addresses
)
3347 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ addr_offset
);
3348 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
3352 printf ("%s:\t", buf
+ skip_addr_chars
);
3356 aux
->require_sec
= true;
3357 objdump_print_address (section
->vma
+ addr_offset
, inf
);
3358 aux
->require_sec
= false;
3362 print_jump_visualisation (section
->vma
+ addr_offset
,
3363 max_level
, line_buffer
,
3371 disassemble_set_printf
3372 (inf
, &sfile
, (fprintf_ftype
) objdump_sprintf
,
3373 (fprintf_styled_ftype
) objdump_styled_sprintf
);
3374 inf
->bytes_per_line
= 0;
3375 inf
->bytes_per_chunk
= 0;
3376 inf
->flags
= ((disassemble_all
? DISASSEMBLE_DATA
: 0)
3377 | (wide_output
? WIDE_OUTPUT
: 0));
3379 inf
->flags
|= USER_SPECIFIED_MACHINE_TYPE
;
3381 if (inf
->disassembler_needs_relocs
3382 && (bfd_get_file_flags (aux
->abfd
) & EXEC_P
) == 0
3383 && (bfd_get_file_flags (aux
->abfd
) & DYNAMIC
) == 0
3384 && *relppp
< relppend
)
3386 bfd_signed_vma distance_to_rel
;
3387 int max_reloc_offset
3388 = aux
->abfd
->arch_info
->max_reloc_offset_into_insn
;
3390 distance_to_rel
= ((**relppp
)->address
- rel_offset
3394 if (distance_to_rel
> 0
3395 && (max_reloc_offset
< 0
3396 || distance_to_rel
<= max_reloc_offset
))
3398 /* This reloc *might* apply to the current insn,
3399 starting somewhere inside it. Discover the length
3400 of the current insn so that the check below will
3403 insn_size
= insn_width
;
3406 /* We find the length by calling the dissassembler
3407 function with a dummy print handler. This should
3408 work unless the disassembler is not expecting to
3409 be called multiple times for the same address.
3411 This does mean disassembling the instruction
3412 twice, but we only do this when there is a high
3413 probability that there is a reloc that will
3414 affect the instruction. */
3415 disassemble_set_printf
3416 (inf
, inf
->stream
, (fprintf_ftype
) null_print
,
3417 (fprintf_styled_ftype
) null_styled_print
);
3418 insn_size
= disassemble_fn (section
->vma
3419 + addr_offset
, inf
);
3420 disassemble_set_printf
3422 (fprintf_ftype
) objdump_sprintf
,
3423 (fprintf_styled_ftype
) objdump_styled_sprintf
);
3427 /* Check to see if the current reloc is associated with
3428 the instruction that we are about to disassemble. */
3429 if (distance_to_rel
== 0
3430 || (distance_to_rel
> 0
3431 && distance_to_rel
< insn_size
/ (int) opb
))
3433 inf
->flags
|= INSN_HAS_RELOC
;
3434 aux
->reloc
= **relppp
;
3438 if (! disassemble_all
3439 && ((section
->flags
& (SEC_CODE
| SEC_HAS_CONTENTS
))
3440 == (SEC_CODE
| SEC_HAS_CONTENTS
)))
3441 /* Set a stop_vma so that the disassembler will not read
3442 beyond the next symbol. We assume that symbols appear on
3443 the boundaries between instructions. We only do this when
3444 disassembling code of course, and when -D is in effect. */
3445 inf
->stop_vma
= section
->vma
+ stop_offset
;
3447 inf
->stop_offset
= stop_offset
;
3448 disassembler_in_comment
= false;
3449 insn_size
= (*disassemble_fn
) (section
->vma
+ addr_offset
, inf
);
3453 disassemble_set_printf (inf
, stdout
, (fprintf_ftype
) fprintf
,
3454 (fprintf_styled_ftype
) fprintf_styled
);
3455 if (insn_width
== 0 && inf
->bytes_per_line
!= 0)
3456 octets_per_line
= inf
->bytes_per_line
;
3457 if (insn_size
< (int) opb
)
3460 printf ("%s\n", sfile
.buffer
);
3463 non_fatal (_("disassemble_fn returned length %d"),
3474 octets
= octets_per_line
;
3475 if (addr_offset
+ octets
/ opb
> stop_offset
)
3476 octets
= (stop_offset
- addr_offset
) * opb
;
3478 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ octets
; ++j
)
3480 if (ISPRINT (data
[j
]))
3481 buf
[j
- addr_offset
* opb
] = data
[j
];
3483 buf
[j
- addr_offset
* opb
] = '.';
3485 buf
[j
- addr_offset
* opb
] = '\0';
3488 if (prefix_addresses
3490 : show_raw_insn
>= 0)
3494 /* If ! prefix_addresses and ! wide_output, we print
3495 octets_per_line octets per line. */
3497 if (pb
> octets_per_line
&& ! prefix_addresses
&& ! wide_output
)
3498 pb
= octets_per_line
;
3500 if (inf
->bytes_per_chunk
)
3501 bpc
= inf
->bytes_per_chunk
;
3505 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
3507 /* PR 21580: Check for a buffer ending early. */
3508 if (j
+ bpc
<= stop_offset
* opb
)
3512 if (inf
->display_endian
== BFD_ENDIAN_LITTLE
)
3514 for (k
= bpc
; k
-- != 0; )
3515 printf ("%02x", (unsigned) data
[j
+ k
]);
3519 for (k
= 0; k
< bpc
; k
++)
3520 printf ("%02x", (unsigned) data
[j
+ k
]);
3526 for (; pb
< octets_per_line
; pb
+= bpc
)
3530 for (k
= 0; k
< bpc
; k
++)
3535 /* Separate raw data from instruction by extra space. */
3545 printf ("%s", sfile
.buffer
);
3547 if (prefix_addresses
3549 : show_raw_insn
>= 0)
3557 j
= addr_offset
* opb
+ pb
;
3563 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ j
/ opb
);
3564 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
3568 printf ("%s:\t", buf
+ skip_addr_chars
);
3571 print_jump_visualisation (section
->vma
+ j
/ opb
,
3572 max_level
, line_buffer
,
3575 pb
+= octets_per_line
;
3578 for (; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
3580 /* PR 21619: Check for a buffer ending early. */
3581 if (j
+ bpc
<= stop_offset
* opb
)
3585 if (inf
->display_endian
== BFD_ENDIAN_LITTLE
)
3587 for (k
= bpc
; k
-- != 0; )
3588 printf ("%02x", (unsigned) data
[j
+ k
]);
3592 for (k
= 0; k
< bpc
; k
++)
3593 printf ("%02x", (unsigned) data
[j
+ k
]);
3607 while ((*relppp
) < relppend
3608 && (**relppp
)->address
< rel_offset
+ addr_offset
+ octets
/ opb
)
3610 if (dump_reloc_info
|| dump_dynamic_reloc_info
)
3623 objdump_print_value (section
->vma
- rel_offset
+ q
->address
,
3628 if (q
->howto
== NULL
)
3629 printf ("*unknown*\t");
3630 else if (q
->howto
->name
)
3631 printf ("%s\t", q
->howto
->name
);
3633 printf ("%d\t", q
->howto
->type
);
3635 if (q
->sym_ptr_ptr
== NULL
|| *q
->sym_ptr_ptr
== NULL
)
3636 printf ("*unknown*");
3639 const char *sym_name
;
3641 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
3642 if (sym_name
!= NULL
&& *sym_name
!= '\0')
3643 objdump_print_symname (aux
->abfd
, inf
, *q
->sym_ptr_ptr
);
3648 sym_sec
= bfd_asymbol_section (*q
->sym_ptr_ptr
);
3649 sym_name
= bfd_section_name (sym_sec
);
3650 if (sym_name
== NULL
|| *sym_name
== '\0')
3651 sym_name
= "*unknown*";
3652 printf ("%s", sanitize_string (sym_name
));
3658 bfd_vma addend
= q
->addend
;
3659 if ((bfd_signed_vma
) addend
< 0)
3666 objdump_print_value (addend
, inf
, true);
3678 addr_offset
+= octets
/ opb
;
3681 free (sfile
.buffer
);
3683 free (color_buffer
);
3687 disassemble_section (bfd
*abfd
, asection
*section
, void *inf
)
3689 const struct elf_backend_data
*bed
;
3690 bfd_vma sign_adjust
= 0;
3691 struct disassemble_info
*pinfo
= (struct disassemble_info
*) inf
;
3692 struct objdump_disasm_info
*paux
;
3693 unsigned int opb
= pinfo
->octets_per_byte
;
3694 bfd_byte
*data
= NULL
;
3695 bfd_size_type datasize
= 0;
3696 arelent
**rel_pp
= NULL
;
3697 arelent
**rel_ppstart
= NULL
;
3698 arelent
**rel_ppend
;
3699 bfd_vma stop_offset
;
3700 asymbol
*sym
= NULL
;
3704 unsigned long addr_offset
;
3708 stop_offset_reached
,
3713 /* Sections that do not contain machine
3714 code are not normally disassembled. */
3715 if (! disassemble_all
3716 && only_list
== NULL
3717 && ((section
->flags
& (SEC_CODE
| SEC_HAS_CONTENTS
))
3718 != (SEC_CODE
| SEC_HAS_CONTENTS
)))
3721 if (! process_section_p (section
))
3724 datasize
= bfd_section_size (section
);
3728 if (start_address
== (bfd_vma
) -1
3729 || start_address
< section
->vma
)
3732 addr_offset
= start_address
- section
->vma
;
3734 if (stop_address
== (bfd_vma
) -1)
3735 stop_offset
= datasize
/ opb
;
3738 if (stop_address
< section
->vma
)
3741 stop_offset
= stop_address
- section
->vma
;
3742 if (stop_offset
> datasize
/ opb
)
3743 stop_offset
= datasize
/ opb
;
3746 if (addr_offset
>= stop_offset
)
3749 /* Decide which set of relocs to use. Load them if necessary. */
3750 paux
= (struct objdump_disasm_info
*) pinfo
->application_data
;
3751 if (pinfo
->dynrelbuf
&& dump_dynamic_reloc_info
)
3753 rel_pp
= pinfo
->dynrelbuf
;
3754 rel_count
= pinfo
->dynrelcount
;
3755 /* Dynamic reloc addresses are absolute, non-dynamic are section
3756 relative. REL_OFFSET specifies the reloc address corresponding
3757 to the start of this section. */
3758 rel_offset
= section
->vma
;
3766 if ((section
->flags
& SEC_RELOC
) != 0
3767 && (dump_reloc_info
|| pinfo
->disassembler_needs_relocs
))
3771 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
3773 bfd_fatal (bfd_get_filename (abfd
));
3777 rel_ppstart
= rel_pp
= (arelent
**) xmalloc (relsize
);
3778 rel_count
= bfd_canonicalize_reloc (abfd
, section
, rel_pp
, syms
);
3780 bfd_fatal (bfd_get_filename (abfd
));
3782 /* Sort the relocs by address. */
3783 qsort (rel_pp
, rel_count
, sizeof (arelent
*), compare_relocs
);
3787 rel_ppend
= PTR_ADD (rel_pp
, rel_count
);
3789 if (!bfd_malloc_and_get_section (abfd
, section
, &data
))
3791 non_fatal (_("Reading section %s failed because: %s"),
3792 section
->name
, bfd_errmsg (bfd_get_error ()));
3796 pinfo
->buffer
= data
;
3797 pinfo
->buffer_vma
= section
->vma
;
3798 pinfo
->buffer_length
= datasize
;
3799 pinfo
->section
= section
;
3801 /* Sort the symbols into value and section order. */
3802 compare_section
= section
;
3803 if (sorted_symcount
> 1)
3804 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
3806 /* Skip over the relocs belonging to addresses below the
3808 while (rel_pp
< rel_ppend
3809 && (*rel_pp
)->address
< rel_offset
+ addr_offset
)
3812 printf (_("\nDisassembly of section %s:\n"), sanitize_string (section
->name
));
3814 /* Find the nearest symbol forwards from our current position. */
3815 paux
->require_sec
= true;
3816 sym
= (asymbol
*) find_symbol_for_address (section
->vma
+ addr_offset
,
3817 (struct disassemble_info
*) inf
,
3819 paux
->require_sec
= false;
3821 /* PR 9774: If the target used signed addresses then we must make
3822 sure that we sign extend the value that we calculate for 'addr'
3823 in the loop below. */
3824 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3825 && (bed
= get_elf_backend_data (abfd
)) != NULL
3826 && bed
->sign_extend_vma
)
3827 sign_adjust
= (bfd_vma
) 1 << (bed
->s
->arch_size
- 1);
3829 /* Disassemble a block of instructions up to the address associated with
3830 the symbol we have just found. Then print the symbol and find the
3831 next symbol on. Repeat until we have disassembled the entire section
3832 or we have reached the end of the address range we are interested in. */
3833 do_print
= paux
->symbol
== NULL
;
3834 loop_until
= stop_offset_reached
;
3836 while (addr_offset
< stop_offset
)
3840 bfd_vma nextstop_offset
;
3843 addr
= section
->vma
+ addr_offset
;
3844 addr
= ((addr
& ((sign_adjust
<< 1) - 1)) ^ sign_adjust
) - sign_adjust
;
3846 if (sym
!= NULL
&& bfd_asymbol_value (sym
) <= addr
)
3851 (x
< sorted_symcount
3852 && (bfd_asymbol_value (sorted_syms
[x
]) <= addr
));
3856 pinfo
->symbols
= sorted_syms
+ place
;
3857 pinfo
->num_symbols
= x
- place
;
3858 pinfo
->symtab_pos
= place
;
3862 pinfo
->symbols
= NULL
;
3863 pinfo
->num_symbols
= 0;
3864 pinfo
->symtab_pos
= -1;
3867 /* If we are only disassembling from a specific symbol,
3868 check to see if we should start or stop displaying. */
3869 if (sym
&& paux
->symbol
)
3873 /* See if we should stop printing. */
3877 if (sym
->flags
& BSF_FUNCTION
)
3881 case stop_offset_reached
:
3882 /* Handled by the while loop. */
3886 /* FIXME: There is an implicit assumption here
3887 that the name of sym is different from
3889 if (! bfd_is_local_label (abfd
, sym
))
3896 const char * name
= bfd_asymbol_name (sym
);
3897 char * alloc
= NULL
;
3899 if (do_demangle
&& name
[0] != '\0')
3901 /* Demangle the name. */
3902 alloc
= bfd_demangle (abfd
, name
, demangle_flags
);
3907 /* We are not currently printing. Check to see
3908 if the current symbol matches the requested symbol. */
3909 if (streq (name
, paux
->symbol
))
3913 if (sym
->flags
& BSF_FUNCTION
)
3915 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3916 && ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
> 0)
3918 /* Sym is a function symbol with a size associated
3919 with it. Turn on automatic disassembly for the
3920 next VALUE bytes. */
3921 stop_offset
= addr_offset
3922 + ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
3923 loop_until
= stop_offset_reached
;
3927 /* Otherwise we need to tell the loop heuristic to
3928 loop until the next function symbol is encountered. */
3929 loop_until
= function_sym
;
3934 /* Otherwise loop until the next symbol is encountered. */
3935 loop_until
= next_sym
;
3943 if (! prefix_addresses
&& do_print
)
3945 pinfo
->fprintf_func (pinfo
->stream
, "\n");
3946 objdump_print_addr_with_sym (abfd
, section
, sym
, addr
,
3948 pinfo
->fprintf_func (pinfo
->stream
, ":\n");
3950 if (sym
!= NULL
&& show_all_symbols
)
3952 for (++place
; place
< sorted_symcount
; place
++)
3954 sym
= sorted_syms
[place
];
3956 if (bfd_asymbol_value (sym
) != addr
)
3958 if (! pinfo
->symbol_is_valid (sym
, pinfo
))
3960 if (strcmp (bfd_section_name (sym
->section
), bfd_section_name (section
)) != 0)
3963 objdump_print_addr_with_sym (abfd
, section
, sym
, addr
, pinfo
, false);
3964 pinfo
->fprintf_func (pinfo
->stream
, ":\n");
3969 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > addr
)
3971 else if (sym
== NULL
)
3975 #define is_valid_next_sym(SYM) \
3976 (strcmp (bfd_section_name ((SYM)->section), bfd_section_name (section)) == 0 \
3977 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
3978 && pinfo->symbol_is_valid (SYM, pinfo))
3980 /* Search forward for the next appropriate symbol in
3981 SECTION. Note that all the symbols are sorted
3982 together into one big array, and that some sections
3983 may have overlapping addresses. */
3984 while (place
< sorted_symcount
3985 && ! is_valid_next_sym (sorted_syms
[place
]))
3988 if (place
>= sorted_symcount
)
3991 nextsym
= sorted_syms
[place
];
3994 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > addr
)
3995 nextstop_offset
= bfd_asymbol_value (sym
) - section
->vma
;
3996 else if (nextsym
== NULL
)
3997 nextstop_offset
= stop_offset
;
3999 nextstop_offset
= bfd_asymbol_value (nextsym
) - section
->vma
;
4001 if (nextstop_offset
> stop_offset
4002 || nextstop_offset
<= addr_offset
)
4003 nextstop_offset
= stop_offset
;
4005 /* If a symbol is explicitly marked as being an object
4006 rather than a function, just dump the bytes without
4007 disassembling them. */
4010 || sym
->section
!= section
4011 || bfd_asymbol_value (sym
) > addr
4012 || ((sym
->flags
& BSF_OBJECT
) == 0
4013 && (strstr (bfd_asymbol_name (sym
), "gnu_compiled")
4015 && (strstr (bfd_asymbol_name (sym
), "gcc2_compiled")
4017 || (sym
->flags
& BSF_FUNCTION
) != 0)
4024 /* Resolve symbol name. */
4025 if (visualize_jumps
&& abfd
&& sym
&& sym
->name
)
4027 struct disassemble_info di
;
4030 sf
.alloc
= strlen (sym
->name
) + 40;
4031 sf
.buffer
= (char*) xmalloc (sf
.alloc
);
4033 disassemble_set_printf
4034 (&di
, &sf
, (fprintf_ftype
) objdump_sprintf
,
4035 (fprintf_styled_ftype
) objdump_styled_sprintf
);
4037 objdump_print_symname (abfd
, &di
, sym
);
4039 /* Fetch jump information. */
4040 detected_jumps
= disassemble_jumps
4041 (pinfo
, paux
->disassemble_fn
,
4042 addr_offset
, nextstop_offset
,
4043 rel_offset
, &rel_pp
, rel_ppend
);
4045 /* Free symbol name. */
4049 /* Add jumps to output. */
4050 disassemble_bytes (pinfo
, paux
->disassemble_fn
, insns
, data
,
4051 addr_offset
, nextstop_offset
,
4052 rel_offset
, &rel_pp
, rel_ppend
);
4055 while (detected_jumps
)
4057 detected_jumps
= jump_info_free (detected_jumps
);
4061 addr_offset
= nextstop_offset
;
4067 if (rel_ppstart
!= NULL
)
4071 /* Disassemble the contents of an object file. */
4074 disassemble_data (bfd
*abfd
)
4076 struct disassemble_info disasm_info
;
4077 struct objdump_disasm_info aux
;
4081 prev_functionname
= NULL
;
4083 prev_discriminator
= 0;
4085 /* We make a copy of syms to sort. We don't want to sort syms
4086 because that will screw up the relocs. */
4087 sorted_symcount
= symcount
? symcount
: dynsymcount
;
4088 sorted_syms
= (asymbol
**) xmalloc ((sorted_symcount
+ synthcount
)
4089 * sizeof (asymbol
*));
4090 if (sorted_symcount
!= 0)
4092 memcpy (sorted_syms
, symcount
? syms
: dynsyms
,
4093 sorted_symcount
* sizeof (asymbol
*));
4095 sorted_symcount
= remove_useless_symbols (sorted_syms
, sorted_symcount
);
4098 for (i
= 0; i
< synthcount
; ++i
)
4100 sorted_syms
[sorted_symcount
] = synthsyms
+ i
;
4104 init_disassemble_info (&disasm_info
, stdout
, (fprintf_ftype
) fprintf
,
4105 (fprintf_styled_ftype
) fprintf_styled
);
4106 disasm_info
.application_data
= (void *) &aux
;
4108 aux
.require_sec
= false;
4109 disasm_info
.dynrelbuf
= NULL
;
4110 disasm_info
.dynrelcount
= 0;
4112 aux
.symbol
= disasm_sym
;
4114 disasm_info
.print_address_func
= objdump_print_address
;
4115 disasm_info
.symbol_at_address_func
= objdump_symbol_at_address
;
4117 if (machine
!= NULL
)
4119 const bfd_arch_info_type
*inf
= bfd_scan_arch (machine
);
4122 fatal (_("can't use supplied machine %s"), machine
);
4124 abfd
->arch_info
= inf
;
4127 if (endian
!= BFD_ENDIAN_UNKNOWN
)
4129 struct bfd_target
*xvec
;
4131 xvec
= (struct bfd_target
*) xmalloc (sizeof (struct bfd_target
));
4132 memcpy (xvec
, abfd
->xvec
, sizeof (struct bfd_target
));
4133 xvec
->byteorder
= endian
;
4137 /* Use libopcodes to locate a suitable disassembler. */
4138 aux
.disassemble_fn
= disassembler (bfd_get_arch (abfd
),
4139 bfd_big_endian (abfd
),
4140 bfd_get_mach (abfd
), abfd
);
4141 if (!aux
.disassemble_fn
)
4143 non_fatal (_("can't disassemble for architecture %s\n"),
4144 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
4149 disasm_info
.flavour
= bfd_get_flavour (abfd
);
4150 disasm_info
.arch
= bfd_get_arch (abfd
);
4151 disasm_info
.mach
= bfd_get_mach (abfd
);
4152 disasm_info
.disassembler_options
= disassembler_options
;
4153 disasm_info
.octets_per_byte
= bfd_octets_per_byte (abfd
, NULL
);
4154 disasm_info
.skip_zeroes
= DEFAULT_SKIP_ZEROES
;
4155 disasm_info
.skip_zeroes_at_end
= DEFAULT_SKIP_ZEROES_AT_END
;
4156 disasm_info
.disassembler_needs_relocs
= false;
4158 if (bfd_big_endian (abfd
))
4159 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_BIG
;
4160 else if (bfd_little_endian (abfd
))
4161 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
4163 /* ??? Aborting here seems too drastic. We could default to big or little
4165 disasm_info
.endian
= BFD_ENDIAN_UNKNOWN
;
4167 disasm_info
.endian_code
= disasm_info
.endian
;
4169 /* Allow the target to customize the info structure. */
4170 disassemble_init_for_target (& disasm_info
);
4172 /* Pre-load the dynamic relocs as we may need them during the disassembly. */
4173 long relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
4175 if (relsize
< 0 && dump_dynamic_reloc_info
)
4176 bfd_fatal (bfd_get_filename (abfd
));
4180 disasm_info
.dynrelbuf
= (arelent
**) xmalloc (relsize
);
4181 disasm_info
.dynrelcount
4182 = bfd_canonicalize_dynamic_reloc (abfd
, disasm_info
.dynrelbuf
, dynsyms
);
4183 if (disasm_info
.dynrelcount
< 0)
4184 bfd_fatal (bfd_get_filename (abfd
));
4186 /* Sort the relocs by address. */
4187 qsort (disasm_info
.dynrelbuf
, disasm_info
.dynrelcount
, sizeof (arelent
*),
4191 disasm_info
.symtab
= sorted_syms
;
4192 disasm_info
.symtab_size
= sorted_symcount
;
4194 bfd_map_over_sections (abfd
, disassemble_section
, & disasm_info
);
4196 free (disasm_info
.dynrelbuf
);
4197 disasm_info
.dynrelbuf
= NULL
;
4199 disassemble_free_target (&disasm_info
);
4203 load_specific_debug_section (enum dwarf_section_display_enum debug
,
4204 asection
*sec
, void *file
)
4206 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
4207 bfd
*abfd
= (bfd
*) file
;
4213 if (section
->start
!= NULL
)
4215 /* If it is already loaded, do nothing. */
4216 if (streq (section
->filename
, bfd_get_filename (abfd
)))
4218 free (section
->start
);
4221 section
->filename
= bfd_get_filename (abfd
);
4222 section
->reloc_info
= NULL
;
4223 section
->num_relocs
= 0;
4224 section
->address
= bfd_section_vma (sec
);
4225 section
->size
= bfd_section_size (sec
);
4226 /* PR 24360: On 32-bit hosts sizeof (size_t) < sizeof (bfd_size_type). */
4227 alloced
= amt
= section
->size
+ 1;
4230 || (bfd_get_size (abfd
) != 0 && alloced
>= bfd_get_size (abfd
)))
4232 section
->start
= NULL
;
4233 free_debug_section (debug
);
4234 printf (_("\nSection '%s' has an invalid size: %#" PRIx64
".\n"),
4235 sanitize_string (section
->name
),
4240 section
->start
= contents
= xmalloc (alloced
);
4241 /* Ensure any string section has a terminating NUL. */
4242 section
->start
[section
->size
] = 0;
4244 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4245 && debug_displays
[debug
].relocate
)
4247 ret
= bfd_simple_get_relocated_section_contents (abfd
,
4253 long reloc_size
= bfd_get_reloc_upper_bound (abfd
, sec
);
4260 relocs
= (arelent
**) xmalloc (reloc_size
);
4262 reloc_count
= bfd_canonicalize_reloc (abfd
, sec
, relocs
, syms
);
4263 if (reloc_count
<= 0)
4267 section
->reloc_info
= relocs
;
4268 section
->num_relocs
= reloc_count
;
4274 ret
= bfd_get_full_section_contents (abfd
, sec
, &contents
);
4278 free_debug_section (debug
);
4279 printf (_("\nCan't get contents for section '%s'.\n"),
4280 sanitize_string (section
->name
));
4288 reloc_at (struct dwarf_section
* dsec
, uint64_t offset
)
4293 if (dsec
== NULL
|| dsec
->reloc_info
== NULL
)
4296 relocs
= (arelent
**) dsec
->reloc_info
;
4298 for (; (rp
= * relocs
) != NULL
; ++ relocs
)
4299 if (rp
->address
== offset
)
4306 load_debug_section (enum dwarf_section_display_enum debug
, void *file
)
4308 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
4309 bfd
*abfd
= (bfd
*) file
;
4313 if (!dump_any_debugging
)
4316 /* If it is already loaded, do nothing. */
4317 if (section
->start
!= NULL
)
4319 if (streq (section
->filename
, bfd_get_filename (abfd
)))
4322 /* Locate the debug section. */
4323 name
= section
->uncompressed_name
;
4324 sec
= bfd_get_section_by_name (abfd
, name
);
4327 name
= section
->compressed_name
;
4329 sec
= bfd_get_section_by_name (abfd
, name
);
4333 name
= section
->xcoff_name
;
4335 sec
= bfd_get_section_by_name (abfd
, name
);
4340 section
->name
= name
;
4341 return load_specific_debug_section (debug
, sec
, file
);
4345 free_debug_section (enum dwarf_section_display_enum debug
)
4347 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
4349 free ((char *) section
->start
);
4350 section
->start
= NULL
;
4351 section
->address
= 0;
4353 free ((char*) section
->reloc_info
);
4354 section
->reloc_info
= NULL
;
4355 section
->num_relocs
= 0;
4359 close_debug_file (void * file
)
4361 bfd
* abfd
= (bfd
*) file
;
4367 open_debug_file (const char * pathname
)
4371 data
= bfd_openr (pathname
, NULL
);
4375 if (! bfd_check_format (data
, bfd_object
))
4382 dump_dwarf_section (bfd
*abfd
, asection
*section
,
4385 const char *name
= bfd_section_name (section
);
4388 bool is_mainfile
= *(bool *) arg
;
4393 if (!is_mainfile
&& !process_links
4394 && (section
->flags
& SEC_DEBUGGING
) == 0)
4397 if (startswith (name
, ".gnu.linkonce.wi."))
4398 match
= ".debug_info";
4402 for (i
= 0; i
< max
; i
++)
4403 if ((strcmp (debug_displays
[i
].section
.uncompressed_name
, match
) == 0
4404 || strcmp (debug_displays
[i
].section
.compressed_name
, match
) == 0
4405 || strcmp (debug_displays
[i
].section
.xcoff_name
, match
) == 0)
4406 && debug_displays
[i
].enabled
!= NULL
4407 && *debug_displays
[i
].enabled
)
4409 struct dwarf_section
*sec
= &debug_displays
[i
].section
;
4411 if (strcmp (sec
->uncompressed_name
, match
) == 0)
4412 sec
->name
= sec
->uncompressed_name
;
4413 else if (strcmp (sec
->compressed_name
, match
) == 0)
4414 sec
->name
= sec
->compressed_name
;
4416 sec
->name
= sec
->xcoff_name
;
4417 if (load_specific_debug_section ((enum dwarf_section_display_enum
) i
,
4420 debug_displays
[i
].display (sec
, abfd
);
4422 if (i
!= info
&& i
!= abbrev
)
4423 free_debug_section ((enum dwarf_section_display_enum
) i
);
4429 /* Dump the dwarf debugging information. */
4432 dump_dwarf (bfd
*abfd
, bool is_mainfile
)
4434 /* The byte_get pointer should have been set at the start of dump_bfd(). */
4435 if (byte_get
== NULL
)
4437 warn (_("File %s does not contain any dwarf debug information\n"),
4438 bfd_get_filename (abfd
));
4442 switch (bfd_get_arch (abfd
))
4445 /* S12Z has a 24 bit address space. But the only known
4446 producer of dwarf_info encodes addresses into 32 bits. */
4451 eh_addr_size
= bfd_arch_bits_per_address (abfd
) / 8;
4455 init_dwarf_regnames_by_bfd_arch_and_mach (bfd_get_arch (abfd
),
4456 bfd_get_mach (abfd
));
4458 bfd_map_over_sections (abfd
, dump_dwarf_section
, (void *) &is_mainfile
);
4461 /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
4462 it. Return NULL on failure. */
4465 read_section_stabs (bfd
*abfd
, const char *sect_name
, bfd_size_type
*size_ptr
,
4466 bfd_size_type
*entsize_ptr
)
4471 stabsect
= bfd_get_section_by_name (abfd
, sect_name
);
4472 if (stabsect
== NULL
)
4474 printf (_("No %s section present\n\n"),
4475 sanitize_string (sect_name
));
4479 if (!bfd_malloc_and_get_section (abfd
, stabsect
, &contents
))
4481 non_fatal (_("reading %s section of %s failed: %s"),
4482 sect_name
, bfd_get_filename (abfd
),
4483 bfd_errmsg (bfd_get_error ()));
4489 *size_ptr
= bfd_section_size (stabsect
);
4491 *entsize_ptr
= stabsect
->entsize
;
4496 /* Stabs entries use a 12 byte format:
4497 4 byte string table index
4499 1 byte stab other field
4500 2 byte stab desc field
4502 FIXME: This will have to change for a 64 bit object format. */
4504 #define STRDXOFF (0)
4506 #define OTHEROFF (5)
4509 #define STABSIZE (12)
4511 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
4512 using string table section STRSECT_NAME (in `strtab'). */
4515 print_section_stabs (bfd
*abfd
,
4516 const char *stabsect_name
,
4517 unsigned *string_offset_ptr
)
4520 unsigned file_string_table_offset
= 0;
4521 unsigned next_file_string_table_offset
= *string_offset_ptr
;
4522 bfd_byte
*stabp
, *stabs_end
;
4525 stabs_end
= stabp
+ stab_size
;
4527 printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name
));
4528 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
4530 /* Loop through all symbols and print them.
4532 We start the index at -1 because there is a dummy symbol on
4533 the front of stabs-in-{coff,elf} sections that supplies sizes. */
4534 for (i
= -1; stabp
<= stabs_end
- STABSIZE
; stabp
+= STABSIZE
, i
++)
4538 unsigned char type
, other
;
4539 unsigned short desc
;
4542 strx
= bfd_h_get_32 (abfd
, stabp
+ STRDXOFF
);
4543 type
= bfd_h_get_8 (abfd
, stabp
+ TYPEOFF
);
4544 other
= bfd_h_get_8 (abfd
, stabp
+ OTHEROFF
);
4545 desc
= bfd_h_get_16 (abfd
, stabp
+ DESCOFF
);
4546 value
= bfd_h_get_32 (abfd
, stabp
+ VALOFF
);
4548 printf ("\n%-6d ", i
);
4549 /* Either print the stab name, or, if unnamed, print its number
4550 again (makes consistent formatting for tools like awk). */
4551 name
= bfd_get_stab_name (type
);
4553 printf ("%-6s", sanitize_string (name
));
4554 else if (type
== N_UNDF
)
4557 printf ("%-6d", type
);
4558 printf (" %-6d %-6d ", other
, desc
);
4559 bfd_printf_vma (abfd
, value
);
4560 printf (" %-6lu", strx
);
4562 /* Symbols with type == 0 (N_UNDF) specify the length of the
4563 string table associated with this file. We use that info
4564 to know how to relocate the *next* file's string table indices. */
4567 file_string_table_offset
= next_file_string_table_offset
;
4568 next_file_string_table_offset
+= value
;
4572 bfd_size_type amt
= strx
+ file_string_table_offset
;
4574 /* Using the (possibly updated) string table offset, print the
4575 string (if any) associated with this symbol. */
4576 if (amt
< stabstr_size
)
4577 /* PR 17512: file: 079-79389-0.001:0.1.
4578 FIXME: May need to sanitize this string before displaying. */
4579 printf (" %.*s", (int)(stabstr_size
- amt
), strtab
+ amt
);
4585 *string_offset_ptr
= next_file_string_table_offset
;
4590 const char * section_name
;
4591 const char * string_section_name
;
4592 unsigned string_offset
;
4597 find_stabs_section (bfd
*abfd
, asection
*section
, void *names
)
4600 stab_section_names
* sought
= (stab_section_names
*) names
;
4602 /* Check for section names for which stabsect_name is a prefix, to
4603 handle .stab.N, etc. */
4604 len
= strlen (sought
->section_name
);
4606 /* If the prefix matches, and the files section name ends with a
4607 nul or a digit, then we match. I.e., we want either an exact
4608 match or a section followed by a number. */
4609 if (strncmp (sought
->section_name
, section
->name
, len
) == 0
4610 && (section
->name
[len
] == 0
4611 || (section
->name
[len
] == '.' && ISDIGIT (section
->name
[len
+ 1]))))
4614 strtab
= read_section_stabs (abfd
, sought
->string_section_name
,
4615 &stabstr_size
, NULL
);
4619 stabs
= read_section_stabs (abfd
, section
->name
, &stab_size
, NULL
);
4621 print_section_stabs (abfd
, section
->name
, &sought
->string_offset
);
4627 dump_stabs_section (bfd
*abfd
, char *stabsect_name
, char *strsect_name
)
4629 stab_section_names s
;
4631 s
.section_name
= stabsect_name
;
4632 s
.string_section_name
= strsect_name
;
4633 s
.string_offset
= 0;
4635 bfd_map_over_sections (abfd
, find_stabs_section
, & s
);
4641 /* Dump the any sections containing stabs debugging information. */
4644 dump_stabs (bfd
*abfd
)
4646 dump_stabs_section (abfd
, ".stab", ".stabstr");
4647 dump_stabs_section (abfd
, ".stab.excl", ".stab.exclstr");
4648 dump_stabs_section (abfd
, ".stab.index", ".stab.indexstr");
4651 dump_stabs_section (abfd
, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
4653 dump_stabs_section (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
4657 dump_bfd_header (bfd
*abfd
)
4661 printf (_("architecture: %s, "),
4662 bfd_printable_arch_mach (bfd_get_arch (abfd
),
4663 bfd_get_mach (abfd
)));
4664 printf (_("flags 0x%08x:\n"), abfd
->flags
& ~BFD_FLAGS_FOR_BFD_USE_MASK
);
4666 #define PF(x, y) if (abfd->flags & x) {printf ("%s%s", comma, y); comma=", ";}
4667 PF (HAS_RELOC
, "HAS_RELOC");
4668 PF (EXEC_P
, "EXEC_P");
4669 PF (HAS_LINENO
, "HAS_LINENO");
4670 PF (HAS_DEBUG
, "HAS_DEBUG");
4671 PF (HAS_SYMS
, "HAS_SYMS");
4672 PF (HAS_LOCALS
, "HAS_LOCALS");
4673 PF (DYNAMIC
, "DYNAMIC");
4674 PF (WP_TEXT
, "WP_TEXT");
4675 PF (D_PAGED
, "D_PAGED");
4676 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
4677 printf (_("\nstart address 0x"));
4678 bfd_printf_vma (abfd
, abfd
->start_address
);
4683 #ifdef ENABLE_LIBCTF
4684 /* Formatting callback function passed to ctf_dump. Returns either the pointer
4685 it is passed, or a pointer to newly-allocated storage, in which case
4686 dump_ctf() will free it when it no longer needs it. */
4689 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED
,
4692 const char *blanks
= arg
;
4695 if (asprintf (&new_s
, "%s%s", blanks
, s
) < 0)
4700 /* Make a ctfsect suitable for ctf_bfdopen_ctfsect(). */
4702 make_ctfsect (const char *name
, bfd_byte
*data
,
4707 ctfsect
.cts_name
= name
;
4708 ctfsect
.cts_entsize
= 1;
4709 ctfsect
.cts_size
= size
;
4710 ctfsect
.cts_data
= data
;
4715 /* Dump CTF errors/warnings. */
4717 dump_ctf_errs (ctf_dict_t
*fp
)
4719 ctf_next_t
*it
= NULL
;
4724 /* Dump accumulated errors and warnings. */
4725 while ((errtext
= ctf_errwarning_next (fp
, &it
, &is_warning
, &err
)) != NULL
)
4727 non_fatal (_("%s: %s"), is_warning
? _("warning"): _("error"),
4731 if (err
!= ECTF_NEXT_END
)
4733 non_fatal (_("CTF error: cannot get CTF errors: `%s'"),
4738 /* Dump one CTF archive member. */
4741 dump_ctf_archive_member (ctf_dict_t
*ctf
, const char *name
, ctf_dict_t
*parent
,
4744 const char *things
[] = {"Header", "Labels", "Data objects",
4745 "Function objects", "Variables", "Types", "Strings",
4750 /* Don't print out the name of the default-named archive member if it appears
4751 first in the list. The name .ctf appears everywhere, even for things that
4752 aren't really archives, so printing it out is liable to be confusing; also,
4753 the common case by far is for only one archive member to exist, and hiding
4754 it in that case seems worthwhile. */
4756 if (strcmp (name
, ".ctf") != 0 || member
!= 0)
4757 printf (_("\nCTF archive member: %s:\n"), sanitize_string (name
));
4759 if (ctf_parent_name (ctf
) != NULL
)
4760 ctf_import (ctf
, parent
);
4762 for (i
= 0, thing
= things
; *thing
[0]; thing
++, i
++)
4764 ctf_dump_state_t
*s
= NULL
;
4767 printf ("\n %s:\n", *thing
);
4768 while ((item
= ctf_dump (ctf
, &s
, i
, dump_ctf_indent_lines
,
4769 (void *) " ")) != NULL
)
4771 printf ("%s\n", item
);
4775 if (ctf_errno (ctf
))
4777 non_fatal (_("Iteration failed: %s, %s"), *thing
,
4778 ctf_errmsg (ctf_errno (ctf
)));
4783 dump_ctf_errs (ctf
);
4786 /* Dump the CTF debugging information. */
4789 dump_ctf (bfd
*abfd
, const char *sect_name
, const char *parent_name
)
4791 ctf_archive_t
*ctfa
= NULL
;
4792 bfd_byte
*ctfdata
= NULL
;
4793 bfd_size_type ctfsize
;
4797 ctf_next_t
*i
= NULL
;
4802 if (sect_name
== NULL
)
4805 if ((ctfdata
= read_section_stabs (abfd
, sect_name
, &ctfsize
, NULL
)) == NULL
)
4806 bfd_fatal (bfd_get_filename (abfd
));
4808 /* Load the CTF file and dump it. Preload the parent dict, since it will
4809 need to be imported into every child in turn. */
4811 ctfsect
= make_ctfsect (sect_name
, ctfdata
, ctfsize
);
4812 if ((ctfa
= ctf_bfdopen_ctfsect (abfd
, &ctfsect
, &err
)) == NULL
)
4814 dump_ctf_errs (NULL
);
4815 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err
));
4816 bfd_fatal (bfd_get_filename (abfd
));
4819 if ((parent
= ctf_dict_open (ctfa
, parent_name
, &err
)) == NULL
)
4821 dump_ctf_errs (NULL
);
4822 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err
));
4823 bfd_fatal (bfd_get_filename (abfd
));
4826 printf (_("Contents of CTF section %s:\n"), sanitize_string (sect_name
));
4828 while ((fp
= ctf_archive_next (ctfa
, &i
, &name
, 0, &err
)) != NULL
)
4829 dump_ctf_archive_member (fp
, name
, parent
, member
++);
4830 if (err
!= ECTF_NEXT_END
)
4832 dump_ctf_errs (NULL
);
4833 non_fatal (_("CTF archive member open failure: %s"), ctf_errmsg (err
));
4834 bfd_fatal (bfd_get_filename (abfd
));
4836 ctf_dict_close (parent
);
4842 dump_ctf (bfd
*abfd ATTRIBUTE_UNUSED
, const char *sect_name ATTRIBUTE_UNUSED
,
4843 const char *parent_name ATTRIBUTE_UNUSED
) {}
4847 read_section_sframe (bfd
*abfd
, const char *sect_name
, bfd_size_type
*size_ptr
,
4848 bfd_vma
*sframe_vma
)
4850 asection
*sframe_sect
;
4853 sframe_sect
= bfd_get_section_by_name (abfd
, sect_name
);
4854 if (sframe_sect
== NULL
)
4856 printf (_("No %s section present\n\n"),
4857 sanitize_string (sect_name
));
4861 if (!bfd_malloc_and_get_section (abfd
, sframe_sect
, &contents
))
4863 non_fatal (_("reading %s section of %s failed: %s"),
4864 sect_name
, bfd_get_filename (abfd
),
4865 bfd_errmsg (bfd_get_error ()));
4871 *size_ptr
= bfd_section_size (sframe_sect
);
4872 *sframe_vma
= bfd_section_vma (sframe_sect
);
4878 dump_section_sframe (bfd
*abfd ATTRIBUTE_UNUSED
,
4879 const char * sect_name
)
4881 sframe_decoder_ctx
*sfd_ctx
= NULL
;
4882 bfd_size_type sf_size
;
4883 bfd_byte
*sframe_data
= NULL
;
4887 if (sect_name
== NULL
)
4888 sect_name
= ".sframe";
4890 sframe_data
= read_section_sframe (abfd
, sect_name
, &sf_size
, &sf_vma
);
4892 if (sframe_data
== NULL
)
4893 bfd_fatal (bfd_get_filename (abfd
));
4895 /* Decode the contents of the section. */
4896 sfd_ctx
= sframe_decode ((const char*)sframe_data
, sf_size
, &err
);
4900 bfd_fatal (bfd_get_filename (abfd
));
4903 printf (_("Contents of the SFrame section %s:"),
4904 sanitize_string (sect_name
));
4905 /* Dump the contents as text. */
4906 dump_sframe (sfd_ctx
, sf_vma
);
4909 sframe_decoder_free (&sfd_ctx
);
4914 dump_bfd_private_header (bfd
*abfd
)
4916 if (!bfd_print_private_bfd_data (abfd
, stdout
))
4917 non_fatal (_("warning: private headers incomplete: %s"),
4918 bfd_errmsg (bfd_get_error ()));
4922 dump_target_specific (bfd
*abfd
)
4924 const struct objdump_private_desc
* const *desc
;
4925 struct objdump_private_option
*opt
;
4928 /* Find the desc. */
4929 for (desc
= objdump_private_vectors
; *desc
!= NULL
; desc
++)
4930 if ((*desc
)->filter (abfd
))
4935 non_fatal (_("option -P/--private not supported by this file"));
4939 /* Clear all options. */
4940 for (opt
= (*desc
)->options
; opt
->name
; opt
++)
4941 opt
->selected
= false;
4943 /* Decode options. */
4944 b
= dump_private_options
;
4947 e
= strchr (b
, ',');
4952 for (opt
= (*desc
)->options
; opt
->name
; opt
++)
4953 if (strcmp (opt
->name
, b
) == 0)
4955 opt
->selected
= true;
4958 if (opt
->name
== NULL
)
4959 non_fatal (_("target specific dump '%s' not supported"), b
);
4970 (*desc
)->dump (abfd
);
4973 /* Display a section in hexadecimal format with associated characters.
4974 Each line prefixed by the zero padded address. */
4977 dump_section (bfd
*abfd
, asection
*section
, void *dummy ATTRIBUTE_UNUSED
)
4979 bfd_byte
*data
= NULL
;
4980 bfd_size_type datasize
;
4981 bfd_vma addr_offset
;
4982 bfd_vma start_offset
;
4983 bfd_vma stop_offset
;
4984 unsigned int opb
= bfd_octets_per_byte (abfd
, section
);
4985 /* Bytes per line. */
4986 const int onaline
= 16;
4991 if (! process_section_p (section
))
4994 if ((section
->flags
& SEC_HAS_CONTENTS
) == 0)
4997 if ((datasize
= bfd_section_size (section
)) == 0)
5000 /* Compute the address range to display. */
5001 if (start_address
== (bfd_vma
) -1
5002 || start_address
< section
->vma
)
5005 start_offset
= start_address
- section
->vma
;
5007 if (stop_address
== (bfd_vma
) -1)
5008 stop_offset
= datasize
/ opb
;
5011 if (stop_address
< section
->vma
)
5014 stop_offset
= stop_address
- section
->vma
;
5016 if (stop_offset
> datasize
/ opb
)
5017 stop_offset
= datasize
/ opb
;
5020 if (start_offset
>= stop_offset
)
5023 printf (_("Contents of section %s:"), sanitize_string (section
->name
));
5024 if (display_file_offsets
)
5025 printf (_(" (Starting at file offset: 0x%lx)"),
5026 (unsigned long) (section
->filepos
+ start_offset
));
5029 if (!bfd_get_full_section_contents (abfd
, section
, &data
))
5031 non_fatal (_("Reading section %s failed because: %s"),
5032 section
->name
, bfd_errmsg (bfd_get_error ()));
5038 bfd_sprintf_vma (abfd
, buf
, start_offset
+ section
->vma
);
5039 if (strlen (buf
) >= sizeof (buf
))
5043 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
5045 count
= strlen (buf
) - count
;
5049 bfd_sprintf_vma (abfd
, buf
, stop_offset
+ section
->vma
- 1);
5050 if (strlen (buf
) >= sizeof (buf
))
5054 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
5056 count
= strlen (buf
) - count
;
5060 for (addr_offset
= start_offset
;
5061 addr_offset
< stop_offset
; addr_offset
+= onaline
/ opb
)
5065 bfd_sprintf_vma (abfd
, buf
, (addr_offset
+ section
->vma
));
5066 count
= strlen (buf
);
5067 if ((size_t) count
>= sizeof (buf
))
5071 while (count
< width
)
5076 fputs (buf
+ count
- width
, stdout
);
5079 for (j
= addr_offset
* opb
;
5080 j
< addr_offset
* opb
+ onaline
; j
++)
5082 if (j
< stop_offset
* opb
)
5083 printf ("%02x", (unsigned) (data
[j
]));
5091 for (j
= addr_offset
* opb
;
5092 j
< addr_offset
* opb
+ onaline
; j
++)
5094 if (j
>= stop_offset
* opb
)
5097 printf ("%c", ISPRINT (data
[j
]) ? data
[j
] : '.');
5104 /* Actually display the various requested regions. */
5107 dump_data (bfd
*abfd
)
5109 bfd_map_over_sections (abfd
, dump_section
, NULL
);
5112 /* Should perhaps share code and display with nm? */
5115 dump_symbols (bfd
*abfd ATTRIBUTE_UNUSED
, bool dynamic
)
5124 max_count
= dynsymcount
;
5125 printf ("DYNAMIC SYMBOL TABLE:\n");
5130 max_count
= symcount
;
5131 printf ("SYMBOL TABLE:\n");
5135 printf (_("no symbols\n"));
5137 for (count
= 0; count
< max_count
; count
++)
5141 if (*current
== NULL
)
5142 printf (_("no information for symbol number %ld\n"), count
);
5144 else if ((cur_bfd
= bfd_asymbol_bfd (*current
)) == NULL
)
5145 printf (_("could not determine the type of symbol number %ld\n"),
5148 else if (process_section_p ((* current
)->section
)
5149 && (dump_special_syms
5150 || !bfd_is_target_special_symbol (cur_bfd
, *current
)))
5152 const char *name
= (*current
)->name
;
5154 if (do_demangle
&& name
!= NULL
&& *name
!= '\0')
5158 /* If we want to demangle the name, we demangle it
5159 here, and temporarily clobber it while calling
5160 bfd_print_symbol. FIXME: This is a gross hack. */
5161 alloc
= bfd_demangle (cur_bfd
, name
, demangle_flags
);
5163 (*current
)->name
= alloc
;
5164 bfd_print_symbol (cur_bfd
, stdout
, *current
,
5165 bfd_print_symbol_all
);
5168 (*current
)->name
= name
;
5172 else if (unicode_display
!= unicode_default
5173 && name
!= NULL
&& *name
!= '\0')
5175 const char * sanitized_name
;
5177 /* If we want to sanitize the name, we do it here, and
5178 temporarily clobber it while calling bfd_print_symbol.
5179 FIXME: This is a gross hack. */
5180 sanitized_name
= sanitize_string (name
);
5181 if (sanitized_name
!= name
)
5182 (*current
)->name
= sanitized_name
;
5184 sanitized_name
= NULL
;
5185 bfd_print_symbol (cur_bfd
, stdout
, *current
,
5186 bfd_print_symbol_all
);
5187 if (sanitized_name
!= NULL
)
5188 (*current
)->name
= name
;
5191 bfd_print_symbol (cur_bfd
, stdout
, *current
,
5192 bfd_print_symbol_all
);
5202 dump_reloc_set (bfd
*abfd
, asection
*sec
, arelent
**relpp
, long relcount
)
5205 char *last_filename
, *last_functionname
;
5206 unsigned int last_line
;
5207 unsigned int last_discriminator
;
5209 /* Get column headers lined up reasonably. */
5217 bfd_sprintf_vma (abfd
, buf
, (bfd_vma
) -1);
5218 width
= strlen (buf
) - 7;
5220 printf ("OFFSET %*s TYPE %*s VALUE\n", width
, "", 12, "");
5223 last_filename
= NULL
;
5224 last_functionname
= NULL
;
5226 last_discriminator
= 0;
5228 for (p
= relpp
; relcount
&& *p
!= NULL
; p
++, relcount
--)
5231 const char *filename
, *functionname
;
5232 unsigned int linenumber
;
5233 unsigned int discriminator
;
5234 const char *sym_name
;
5235 const char *section_name
;
5236 bfd_vma addend2
= 0;
5238 if (start_address
!= (bfd_vma
) -1
5239 && q
->address
< start_address
)
5241 if (stop_address
!= (bfd_vma
) -1
5242 && q
->address
> stop_address
)
5245 if (with_line_numbers
5247 && bfd_find_nearest_line_discriminator (abfd
, sec
, syms
, q
->address
,
5248 &filename
, &functionname
,
5249 &linenumber
, &discriminator
))
5251 if (functionname
!= NULL
5252 && (last_functionname
== NULL
5253 || strcmp (functionname
, last_functionname
) != 0))
5255 printf ("%s():\n", sanitize_string (functionname
));
5256 if (last_functionname
!= NULL
)
5257 free (last_functionname
);
5258 last_functionname
= xstrdup (functionname
);
5262 && (linenumber
!= last_line
5263 || (filename
!= NULL
5264 && last_filename
!= NULL
5265 && filename_cmp (filename
, last_filename
) != 0)
5266 || (discriminator
!= last_discriminator
)))
5268 if (discriminator
> 0)
5269 printf ("%s:%u\n", filename
== NULL
? "???" :
5270 sanitize_string (filename
), linenumber
);
5272 printf ("%s:%u (discriminator %u)\n",
5273 filename
== NULL
? "???" : sanitize_string (filename
),
5274 linenumber
, discriminator
);
5275 last_line
= linenumber
;
5276 last_discriminator
= discriminator
;
5277 if (last_filename
!= NULL
)
5278 free (last_filename
);
5279 if (filename
== NULL
)
5280 last_filename
= NULL
;
5282 last_filename
= xstrdup (filename
);
5286 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
5288 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
5289 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
5294 section_name
= NULL
;
5297 bfd_printf_vma (abfd
, q
->address
);
5298 if (q
->howto
== NULL
)
5299 printf (" *unknown* ");
5300 else if (q
->howto
->name
)
5302 const char *name
= q
->howto
->name
;
5304 /* R_SPARC_OLO10 relocations contain two addends.
5305 But because 'arelent' lacks enough storage to
5306 store them both, the 64-bit ELF Sparc backend
5307 records this as two relocations. One R_SPARC_LO10
5308 and one R_SPARC_13, both pointing to the same
5309 address. This is merely so that we have some
5310 place to store both addend fields.
5312 Undo this transformation, otherwise the output
5313 will be confusing. */
5314 if (abfd
->xvec
->flavour
== bfd_target_elf_flavour
5315 && elf_tdata (abfd
)->elf_header
->e_machine
== EM_SPARCV9
5317 && !strcmp (q
->howto
->name
, "R_SPARC_LO10"))
5319 arelent
*q2
= *(p
+ 1);
5322 && q
->address
== q2
->address
5323 && !strcmp (q2
->howto
->name
, "R_SPARC_13"))
5325 name
= "R_SPARC_OLO10";
5326 addend2
= q2
->addend
;
5330 printf (" %-16s ", name
);
5333 printf (" %-16d ", q
->howto
->type
);
5337 objdump_print_symname (abfd
, NULL
, *q
->sym_ptr_ptr
);
5341 if (section_name
== NULL
)
5342 section_name
= "*unknown*";
5343 printf ("[%s]", sanitize_string (section_name
));
5348 bfd_signed_vma addend
= q
->addend
;
5356 bfd_printf_vma (abfd
, addend
);
5361 bfd_printf_vma (abfd
, addend2
);
5367 if (last_filename
!= NULL
)
5368 free (last_filename
);
5369 if (last_functionname
!= NULL
)
5370 free (last_functionname
);
5374 dump_relocs_in_section (bfd
*abfd
,
5376 void *dummy ATTRIBUTE_UNUSED
)
5378 arelent
**relpp
= NULL
;
5382 if ( bfd_is_abs_section (section
)
5383 || bfd_is_und_section (section
)
5384 || bfd_is_com_section (section
)
5385 || (! process_section_p (section
))
5386 || ((section
->flags
& SEC_RELOC
) == 0))
5389 printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section
->name
));
5391 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
5394 printf (" (none)\n\n");
5402 relpp
= (arelent
**) xmalloc (relsize
);
5403 relcount
= bfd_canonicalize_reloc (abfd
, section
, relpp
, syms
);
5409 non_fatal (_("failed to read relocs in: %s"),
5410 sanitize_string (bfd_get_filename (abfd
)));
5411 bfd_fatal (_("error message was"));
5413 else if (relcount
== 0)
5414 printf (" (none)\n\n");
5418 dump_reloc_set (abfd
, section
, relpp
, relcount
);
5425 dump_relocs (bfd
*abfd
)
5427 bfd_map_over_sections (abfd
, dump_relocs_in_section
, NULL
);
5431 dump_dynamic_relocs (bfd
*abfd
)
5437 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
5439 bfd_fatal (bfd_get_filename (abfd
));
5441 printf ("DYNAMIC RELOCATION RECORDS");
5444 printf (" (none)\n\n");
5447 relpp
= (arelent
**) xmalloc (relsize
);
5448 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
5451 bfd_fatal (bfd_get_filename (abfd
));
5452 else if (relcount
== 0)
5453 printf (" (none)\n\n");
5457 dump_reloc_set (abfd
, NULL
, relpp
, relcount
);
5464 /* Creates a table of paths, to search for source files. */
5467 add_include_path (const char *path
)
5471 include_path_count
++;
5472 include_paths
= (const char **)
5473 xrealloc (include_paths
, include_path_count
* sizeof (*include_paths
));
5474 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5475 if (path
[1] == ':' && path
[2] == 0)
5476 path
= concat (path
, ".", (const char *) 0);
5478 include_paths
[include_path_count
- 1] = path
;
5482 adjust_addresses (bfd
*abfd ATTRIBUTE_UNUSED
,
5486 if ((section
->flags
& SEC_DEBUGGING
) == 0)
5488 bool *has_reloc_p
= (bool *) arg
;
5489 section
->vma
+= adjust_section_vma
;
5491 section
->lma
+= adjust_section_vma
;
5495 /* Return the sign-extended form of an ARCH_SIZE sized VMA. */
5498 sign_extend_address (bfd
*abfd ATTRIBUTE_UNUSED
,
5503 mask
= (bfd_vma
) 1 << (arch_size
- 1);
5504 return (((vma
& ((mask
<< 1) - 1)) ^ mask
) - mask
);
5508 might_need_separate_debug_info (bool is_mainfile
)
5510 /* We do not follow links from debug info files. */
5514 /* Since do_follow_links might be enabled by default, only treat it as an
5515 indication that separate files should be loaded if setting it was a
5516 deliberate user action. */
5517 if (DEFAULT_FOR_FOLLOW_LINKS
== 0 && do_follow_links
)
5520 if (process_links
|| dump_symtab
|| dump_debugging
5521 || dump_dwarf_section_info
|| with_source_code
)
5527 /* Dump selected contents of ABFD. */
5530 dump_bfd (bfd
*abfd
, bool is_mainfile
)
5532 const struct elf_backend_data
* bed
;
5534 if (bfd_big_endian (abfd
))
5535 byte_get
= byte_get_big_endian
;
5536 else if (bfd_little_endian (abfd
))
5537 byte_get
= byte_get_little_endian
;
5541 /* Load any separate debug information files. */
5542 if (byte_get
!= NULL
&& might_need_separate_debug_info (is_mainfile
))
5544 load_separate_debug_files (abfd
, bfd_get_filename (abfd
));
5546 /* If asked to do so, recursively dump the separate files. */
5547 if (do_follow_links
)
5551 for (i
= first_separate_info
; i
!= NULL
; i
= i
->next
)
5552 dump_bfd (i
->handle
, false);
5556 /* Adjust user-specified start and stop limits for targets that use
5557 signed addresses. */
5558 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
5559 && (bed
= get_elf_backend_data (abfd
)) != NULL
5560 && bed
->sign_extend_vma
)
5562 start_address
= sign_extend_address (abfd
, start_address
,
5564 stop_address
= sign_extend_address (abfd
, stop_address
,
5568 /* If we are adjusting section VMA's, change them all now. Changing
5569 the BFD information is a hack. However, we must do it, or
5570 bfd_find_nearest_line will not do the right thing. */
5571 if (adjust_section_vma
!= 0)
5573 bool has_reloc
= (abfd
->flags
& HAS_RELOC
);
5574 bfd_map_over_sections (abfd
, adjust_addresses
, &has_reloc
);
5577 if (is_mainfile
|| process_links
)
5579 if (! dump_debugging_tags
&& ! suppress_bfd_header
)
5580 printf (_("\n%s: file format %s\n"),
5581 sanitize_string (bfd_get_filename (abfd
)),
5584 print_arelt_descr (stdout
, abfd
, true, false);
5585 if (dump_file_header
)
5586 dump_bfd_header (abfd
);
5587 if (dump_private_headers
)
5588 dump_bfd_private_header (abfd
);
5589 if (dump_private_options
!= NULL
)
5590 dump_target_specific (abfd
);
5591 if (! dump_debugging_tags
&& ! suppress_bfd_header
)
5599 || dump_dwarf_section_info
)
5601 syms
= slurp_symtab (abfd
);
5603 /* If following links, load any symbol tables from the linked files as well. */
5604 if (do_follow_links
&& is_mainfile
)
5608 for (i
= first_separate_info
; i
!= NULL
; i
= i
->next
)
5610 asymbol
** extra_syms
;
5611 long old_symcount
= symcount
;
5613 extra_syms
= slurp_symtab (i
->handle
);
5617 if (old_symcount
== 0)
5623 syms
= xrealloc (syms
, ((symcount
+ old_symcount
+ 1)
5624 * sizeof (asymbol
*)));
5625 memcpy (syms
+ old_symcount
,
5627 (symcount
+ 1) * sizeof (asymbol
*));
5631 symcount
+= old_symcount
;
5636 if (is_mainfile
|| process_links
)
5638 if (dump_section_headers
)
5639 dump_headers (abfd
);
5641 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
5642 || (disassemble
&& bfd_get_dynamic_symtab_upper_bound (abfd
) > 0))
5643 dynsyms
= slurp_dynamic_symtab (abfd
);
5647 synthcount
= bfd_get_synthetic_symtab (abfd
, symcount
, syms
,
5648 dynsymcount
, dynsyms
,
5655 dump_symbols (abfd
, false);
5656 if (dump_dynamic_symtab
)
5657 dump_symbols (abfd
, true);
5659 if (dump_dwarf_section_info
)
5660 dump_dwarf (abfd
, is_mainfile
);
5661 if (is_mainfile
|| process_links
)
5663 if (dump_ctf_section_info
)
5664 dump_ctf (abfd
, dump_ctf_section_name
, dump_ctf_parent_name
);
5665 if (dump_sframe_section_info
)
5666 dump_section_sframe (abfd
, dump_sframe_section_name
);
5667 if (dump_stab_section_info
)
5669 if (dump_reloc_info
&& ! disassemble
)
5671 if (dump_dynamic_reloc_info
&& ! disassemble
)
5672 dump_dynamic_relocs (abfd
);
5673 if (dump_section_contents
)
5676 disassemble_data (abfd
);
5683 dhandle
= read_debugging_info (abfd
, syms
, symcount
, true);
5684 if (dhandle
!= NULL
)
5686 if (!print_debugging_info (stdout
, dhandle
, abfd
, syms
,
5688 dump_debugging_tags
!= 0))
5690 non_fatal (_("%s: printing debugging information failed"),
5691 bfd_get_filename (abfd
));
5697 /* PR 6483: If there was no STABS debug info in the file, try
5699 else if (! dump_dwarf_section_info
)
5701 dwarf_select_sections_all ();
5702 dump_dwarf (abfd
, is_mainfile
);
5729 free_debug_memory ();
5733 display_object_bfd (bfd
*abfd
)
5737 if (bfd_check_format_matches (abfd
, bfd_object
, &matching
))
5739 dump_bfd (abfd
, true);
5743 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
5745 nonfatal (bfd_get_filename (abfd
));
5746 list_matching_formats (matching
);
5750 if (bfd_get_error () != bfd_error_file_not_recognized
)
5752 nonfatal (bfd_get_filename (abfd
));
5756 if (bfd_check_format_matches (abfd
, bfd_core
, &matching
))
5758 dump_bfd (abfd
, true);
5762 nonfatal (bfd_get_filename (abfd
));
5764 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
5765 list_matching_formats (matching
);
5769 display_any_bfd (bfd
*file
, int level
)
5771 /* Decompress sections unless dumping the section contents. */
5772 if (!dump_section_contents
)
5773 file
->flags
|= BFD_DECOMPRESS
;
5775 /* If the file is an archive, process all of its elements. */
5776 if (bfd_check_format (file
, bfd_archive
))
5779 bfd
*last_arfile
= NULL
;
5782 printf (_("In archive %s:\n"), sanitize_string (bfd_get_filename (file
)));
5783 else if (level
> 100)
5785 /* Prevent corrupted files from spinning us into an
5786 infinite loop. 100 is an arbitrary heuristic. */
5787 fatal (_("Archive nesting is too deep"));
5791 printf (_("In nested archive %s:\n"),
5792 sanitize_string (bfd_get_filename (file
)));
5796 bfd_set_error (bfd_error_no_error
);
5798 arfile
= bfd_openr_next_archived_file (file
, arfile
);
5801 if (bfd_get_error () != bfd_error_no_more_archived_files
)
5802 nonfatal (bfd_get_filename (file
));
5806 display_any_bfd (arfile
, level
+ 1);
5808 if (last_arfile
!= NULL
)
5810 bfd_close (last_arfile
);
5811 /* PR 17512: file: ac585d01. */
5812 if (arfile
== last_arfile
)
5818 last_arfile
= arfile
;
5821 if (last_arfile
!= NULL
)
5822 bfd_close (last_arfile
);
5825 display_object_bfd (file
);
5829 display_file (char *filename
, char *target
, bool last_file
)
5833 if (get_file_size (filename
) < 1)
5839 file
= bfd_openr (filename
, target
);
5842 nonfatal (filename
);
5846 display_any_bfd (file
, 0);
5848 /* This is an optimization to improve the speed of objdump, especially when
5849 dumping a file with lots of associated debug informatiom. Calling
5850 bfd_close on such a file can take a non-trivial amount of time as there
5851 are lots of lists to walk and buffers to free. This is only really
5852 necessary however if we are about to load another file and we need the
5853 memory back. Otherwise, if we are about to exit, then we can save (a lot
5854 of) time by only doing a quick close, and allowing the OS to reclaim the
5859 bfd_close_all_done (file
);
5863 main (int argc
, char **argv
)
5866 char *target
= default_target
;
5867 bool seenflag
= false;
5869 #ifdef HAVE_LC_MESSAGES
5870 setlocale (LC_MESSAGES
, "");
5872 setlocale (LC_CTYPE
, "");
5874 bindtextdomain (PACKAGE
, LOCALEDIR
);
5875 textdomain (PACKAGE
);
5877 program_name
= *argv
;
5878 xmalloc_set_program_name (program_name
);
5879 bfd_set_error_program_name (program_name
);
5881 START_PROGRESS (program_name
, 0);
5883 expandargv (&argc
, &argv
);
5885 if (bfd_init () != BFD_INIT_MAGIC
)
5886 fatal (_("fatal error: libbfd ABI mismatch"));
5887 set_default_bfd_target ();
5889 while ((c
= getopt_long (argc
, argv
,
5890 "CDE:FGHI:LM:P:RSTU:VW::ab:defghij:lm:prstvwxz",
5891 long_options
, (int *) 0))
5897 break; /* We've been given a long option. */
5904 if (disassembler_options
)
5905 /* Ignore potential memory leak for now. */
5906 options
= concat (disassembler_options
, ",",
5907 optarg
, (const char *) NULL
);
5910 disassembler_options
= remove_whitespace_and_extra_commas (options
);
5917 display_file_offsets
= true;
5920 with_line_numbers
= true;
5929 enum demangling_styles style
;
5931 style
= cplus_demangle_name_to_style (optarg
);
5932 if (style
== unknown_demangling
)
5933 fatal (_("unknown demangling style `%s'"),
5936 cplus_demangle_set_style (style
);
5939 case OPTION_RECURSE_LIMIT
:
5940 demangle_flags
&= ~ DMGL_NO_RECURSE_LIMIT
;
5942 case OPTION_NO_RECURSE_LIMIT
:
5943 demangle_flags
|= DMGL_NO_RECURSE_LIMIT
;
5946 do_wide
= wide_output
= true;
5948 case OPTION_ADJUST_VMA
:
5949 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
5951 case OPTION_START_ADDRESS
:
5952 start_address
= parse_vma (optarg
, "--start-address");
5953 if ((stop_address
!= (bfd_vma
) -1) && stop_address
<= start_address
)
5954 fatal (_("error: the start address should be before the end address"));
5956 case OPTION_STOP_ADDRESS
:
5957 stop_address
= parse_vma (optarg
, "--stop-address");
5958 if ((start_address
!= (bfd_vma
) -1) && stop_address
<= start_address
)
5959 fatal (_("error: the stop address should be after the start address"));
5963 prefix_length
= strlen (prefix
);
5964 /* Remove an unnecessary trailing '/' */
5965 while (IS_DIR_SEPARATOR (prefix
[prefix_length
- 1]))
5968 case OPTION_PREFIX_STRIP
:
5969 prefix_strip
= atoi (optarg
);
5970 if (prefix_strip
< 0)
5971 fatal (_("error: prefix strip must be non-negative"));
5973 case OPTION_INSN_WIDTH
:
5974 insn_width
= strtoul (optarg
, NULL
, 0);
5975 if (insn_width
<= 0)
5976 fatal (_("error: instruction width must be positive"));
5978 case OPTION_INLINES
:
5979 unwind_inlines
= true;
5981 case OPTION_VISUALIZE_JUMPS
:
5982 visualize_jumps
= true;
5983 color_output
= false;
5984 extended_color_output
= false;
5987 if (streq (optarg
, "color"))
5988 color_output
= true;
5989 else if (streq (optarg
, "extended-color"))
5991 color_output
= true;
5992 extended_color_output
= true;
5994 else if (streq (optarg
, "off"))
5995 visualize_jumps
= false;
5997 nonfatal (_("unrecognized argument to --visualize-option"));
6000 case OPTION_DISASSEMBLER_COLOR
:
6001 if (streq (optarg
, "off"))
6002 disassembler_color
= off
;
6003 else if (streq (optarg
, "terminal"))
6004 disassembler_color
= on_if_terminal_output
;
6005 else if (streq (optarg
, "color")
6006 || streq (optarg
, "colour")
6007 || streq (optarg
, "on"))
6008 disassembler_color
= on
;
6009 else if (streq (optarg
, "extended")
6010 || streq (optarg
, "extended-color")
6011 || streq (optarg
, "extended-colour"))
6012 disassembler_color
= extended
;
6014 nonfatal (_("unrecognized argument to --disassembler-color"));
6017 if (strcmp (optarg
, "B") == 0)
6018 endian
= BFD_ENDIAN_BIG
;
6019 else if (strcmp (optarg
, "L") == 0)
6020 endian
= BFD_ENDIAN_LITTLE
;
6023 nonfatal (_("unrecognized -E option"));
6028 if (strncmp (optarg
, "big", strlen (optarg
)) == 0)
6029 endian
= BFD_ENDIAN_BIG
;
6030 else if (strncmp (optarg
, "little", strlen (optarg
)) == 0)
6031 endian
= BFD_ENDIAN_LITTLE
;
6034 non_fatal (_("unrecognized --endian type `%s'"), optarg
);
6041 dump_file_header
= true;
6045 formats_info
= true;
6049 add_include_path (optarg
);
6052 dump_private_headers
= true;
6056 dump_private_options
= optarg
;
6060 dump_private_headers
= true;
6062 dump_reloc_info
= true;
6063 dump_file_header
= true;
6064 dump_ar_hdrs
= true;
6065 dump_section_headers
= true;
6073 dump_dynamic_symtab
= true;
6079 disasm_sym
= optarg
;
6082 disassemble_zeroes
= true;
6086 disassemble_all
= true;
6091 with_source_code
= true;
6094 case OPTION_SOURCE_COMMENT
:
6096 with_source_code
= true;
6099 source_comment
= xstrdup (sanitize_string (optarg
));
6101 source_comment
= xstrdup ("# ");
6109 dump_debugging_tags
= 1;
6114 process_links
= true;
6115 do_follow_links
= true;
6121 if (dwarf_select_sections_by_letters (optarg
))
6122 dump_dwarf_section_info
= true;
6126 dump_dwarf_section_info
= true;
6127 dwarf_select_sections_all ();
6134 if (dwarf_select_sections_by_names (optarg
))
6135 dump_dwarf_section_info
= true;
6139 dwarf_select_sections_all ();
6140 dump_dwarf_section_info
= true;
6143 case OPTION_DWARF_DEPTH
:
6146 dwarf_cutoff_level
= strtoul (optarg
, & cp
, 0);
6149 case OPTION_DWARF_START
:
6152 dwarf_start_die
= strtoul (optarg
, & cp
, 0);
6153 suppress_bfd_header
= 1;
6156 case OPTION_DWARF_CHECK
:
6159 #ifdef ENABLE_LIBCTF
6161 dump_ctf_section_info
= true;
6163 dump_ctf_section_name
= xstrdup (optarg
);
6166 case OPTION_CTF_PARENT
:
6167 dump_ctf_parent_name
= xstrdup (optarg
);
6171 dump_sframe_section_info
= true;
6173 dump_sframe_section_name
= xstrdup (optarg
);
6177 dump_stab_section_info
= true;
6181 dump_section_contents
= true;
6185 dump_reloc_info
= true;
6189 dump_dynamic_reloc_info
= true;
6193 dump_ar_hdrs
= true;
6197 dump_section_headers
= true;
6202 show_version
= true;
6207 if (streq (optarg
, "default") || streq (optarg
, "d"))
6208 unicode_display
= unicode_default
;
6209 else if (streq (optarg
, "locale") || streq (optarg
, "l"))
6210 unicode_display
= unicode_locale
;
6211 else if (streq (optarg
, "escape") || streq (optarg
, "e"))
6212 unicode_display
= unicode_escape
;
6213 else if (streq (optarg
, "invalid") || streq (optarg
, "i"))
6214 unicode_display
= unicode_invalid
;
6215 else if (streq (optarg
, "hex") || streq (optarg
, "x"))
6216 unicode_display
= unicode_hex
;
6217 else if (streq (optarg
, "highlight") || streq (optarg
, "h"))
6218 unicode_display
= unicode_highlight
;
6220 fatal (_("invalid argument to -U/--unicode: %s"), optarg
);
6225 /* No need to set seenflag or to break - usage() does not return. */
6231 if (disassembler_color
== on_if_terminal_output
)
6232 disassembler_color
= isatty (1) ? on
: off
;
6235 print_version ("objdump");
6240 dump_any_debugging
= (dump_debugging
6241 || dump_dwarf_section_info
6243 || with_source_code
);
6246 exit_status
= display_info ();
6250 display_file ("a.out", target
, true);
6252 for (; optind
< argc
;)
6254 display_file (argv
[optind
], target
, optind
== argc
- 1);
6260 free (dump_ctf_section_name
);
6261 free (dump_ctf_parent_name
);
6262 free ((void *) source_comment
);
6264 END_PROGRESS (program_name
);