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. */
57 #include "demanguse.h"
60 #include "sframe-api.h"
62 #include "safe-ctype.h"
64 #include "libiberty.h"
66 #include "filenames.h"
75 #ifdef HAVE_LIBDEBUGINFOD
76 #include <elfutils/debuginfod.h>
79 /* Internal headers for the ELF .stab-dump code - sorry. */
80 #define BYTES_IN_WORD 32
81 #include "aout/aout64.h"
84 static int exit_status
= 0;
86 static char *default_target
= NULL
; /* Default at runtime. */
88 /* The following variables are set based on arguments passed on the
90 static int show_version
= 0; /* Show the version number. */
91 static int dump_section_contents
; /* -s */
92 static int dump_section_headers
; /* -h */
93 static bool dump_file_header
; /* -f */
94 static int dump_symtab
; /* -t */
95 static int dump_dynamic_symtab
; /* -T */
96 static int dump_reloc_info
; /* -r */
97 static int dump_dynamic_reloc_info
; /* -R */
98 static int dump_ar_hdrs
; /* -a */
99 static int dump_private_headers
; /* -p */
100 static char *dump_private_options
; /* -P */
101 static int no_addresses
; /* --no-addresses */
102 static int prefix_addresses
; /* --prefix-addresses */
103 static int with_line_numbers
; /* -l */
104 static bool with_source_code
; /* -S */
105 static int show_raw_insn
; /* --show-raw-insn */
106 static int dump_dwarf_section_info
; /* --dwarf */
107 static int dump_stab_section_info
; /* --stabs */
108 static int dump_ctf_section_info
; /* --ctf */
109 static char *dump_ctf_section_name
;
110 static char *dump_ctf_parent_name
; /* --ctf-parent */
111 static int dump_sframe_section_info
; /* --sframe */
112 static char *dump_sframe_section_name
;
113 static int do_demangle
; /* -C, --demangle */
114 static bool disassemble
; /* -d */
115 static bool disassemble_all
; /* -D */
116 static int disassemble_zeroes
; /* --disassemble-zeroes */
117 static bool formats_info
; /* -i */
118 int wide_output
; /* -w */
119 static int insn_width
; /* --insn-width */
120 static bfd_vma start_address
= (bfd_vma
) -1; /* --start-address */
121 static bfd_vma stop_address
= (bfd_vma
) -1; /* --stop-address */
122 static int dump_debugging
; /* --debugging */
123 static int dump_debugging_tags
; /* --debugging-tags */
124 static int suppress_bfd_header
;
125 static int dump_special_syms
= 0; /* --special-syms */
126 static bfd_vma adjust_section_vma
= 0; /* --adjust-vma */
127 static int file_start_context
= 0; /* --file-start-context */
128 static bool display_file_offsets
; /* -F */
129 static const char *prefix
; /* --prefix */
130 static int prefix_strip
; /* --prefix-strip */
131 static size_t prefix_length
;
132 static bool unwind_inlines
; /* --inlines. */
133 static const char * disasm_sym
; /* Disassembly start symbol. */
134 static const char * source_comment
; /* --source_comment. */
135 static bool visualize_jumps
= false; /* --visualize-jumps. */
136 static bool color_output
= false; /* --visualize-jumps=color. */
137 static bool extended_color_output
= false; /* --visualize-jumps=extended-color. */
138 static int process_links
= false; /* --process-links. */
139 static int show_all_symbols
; /* --show-all-symbols. */
141 static enum color_selection
143 on_if_terminal_output
,
144 on
, /* --disassembler-color=color. */
145 off
, /* --disassembler-color=off. */
146 extended
/* --disassembler-color=extended-color. */
147 } disassembler_color
=
148 #if DEFAULT_FOR_COLORED_DISASSEMBLY
149 on_if_terminal_output
;
154 static int dump_any_debugging
;
155 static int demangle_flags
= DMGL_ANSI
| DMGL_PARAMS
;
157 /* This is reset to false each time we enter the disassembler, and set true
158 when the disassembler emits something in the dis_style_comment_start
159 style. Once this is true, all further output on that line is done in
160 the comment style. This only has an effect when disassembler coloring
162 static bool disassembler_in_comment
= false;
164 /* A structure to record the sections mentioned in -j switches. */
167 const char *name
; /* The name of the section. */
168 bool seen
; /* A flag to indicate that the section has been found in one or more input files. */
169 struct only
*next
; /* Pointer to the next structure in the list. */
171 /* Pointer to an array of 'only' structures.
172 This pointer is NULL if the -j switch has not been used. */
173 static struct only
* only_list
= NULL
;
175 /* Variables for handling include file path table. */
176 static const char **include_paths
;
177 static int include_path_count
;
179 /* Extra info to pass to the section disassembler and address printing
181 struct objdump_disasm_info
185 disassembler_ftype disassemble_fn
;
190 /* Architecture to disassemble for, or default if NULL. */
191 static char *machine
= NULL
;
193 /* Target specific options to the disassembler. */
194 static char *disassembler_options
= NULL
;
196 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
197 static enum bfd_endian endian
= BFD_ENDIAN_UNKNOWN
;
199 /* The symbol table. */
200 static asymbol
**syms
;
202 /* Number of symbols in `syms'. */
203 static long symcount
= 0;
205 /* The sorted symbol table. */
206 static asymbol
**sorted_syms
;
208 /* Number of symbols in `sorted_syms'. */
209 static long sorted_symcount
= 0;
211 /* The dynamic symbol table. */
212 static asymbol
**dynsyms
;
214 /* The synthetic symbol table. */
215 static asymbol
*synthsyms
;
216 static long synthcount
= 0;
218 /* Number of symbols in `dynsyms'. */
219 static long dynsymcount
= 0;
221 static bfd_byte
*stabs
;
222 static bfd_size_type stab_size
;
224 static bfd_byte
*strtab
;
225 static bfd_size_type stabstr_size
;
227 /* Handlers for -P/--private. */
228 static const struct objdump_private_desc
* const objdump_private_vectors
[] =
230 OBJDUMP_PRIVATE_VECTORS
234 /* The list of detected jumps inside a function. */
235 static struct jump_info
*detected_jumps
= NULL
;
237 typedef enum unicode_display_type
245 } unicode_display_type
;
247 static unicode_display_type unicode_display
= unicode_default
;
249 static void usage (FILE *, int) ATTRIBUTE_NORETURN
;
251 usage (FILE *stream
, int status
)
253 fprintf (stream
, _("Usage: %s <option(s)> <file(s)>\n"), program_name
);
254 fprintf (stream
, _(" Display information from object <file(s)>.\n"));
255 fprintf (stream
, _(" At least one of the following switches must be given:\n"));
256 fprintf (stream
, _("\
257 -a, --archive-headers Display archive header information\n"));
258 fprintf (stream
, _("\
259 -f, --file-headers Display the contents of the overall file header\n"));
260 fprintf (stream
, _("\
261 -p, --private-headers Display object format specific file header contents\n"));
262 fprintf (stream
, _("\
263 -P, --private=OPT,OPT... Display object format specific contents\n"));
264 fprintf (stream
, _("\
265 -h, --[section-]headers Display the contents of the section headers\n"));
266 fprintf (stream
, _("\
267 -x, --all-headers Display the contents of all headers\n"));
268 fprintf (stream
, _("\
269 -d, --disassemble Display assembler contents of executable sections\n"));
270 fprintf (stream
, _("\
271 -D, --disassemble-all Display assembler contents of all sections\n"));
272 fprintf (stream
, _("\
273 --disassemble=<sym> Display assembler contents from <sym>\n"));
274 fprintf (stream
, _("\
275 -S, --source Intermix source code with disassembly\n"));
276 fprintf (stream
, _("\
277 --source-comment[=<txt>] Prefix lines of source code with <txt>\n"));
278 fprintf (stream
, _("\
279 -s, --full-contents Display the full contents of all sections requested\n"));
280 fprintf (stream
, _("\
281 -g, --debugging Display debug information in object file\n"));
282 fprintf (stream
, _("\
283 -e, --debugging-tags Display debug information using ctags style\n"));
284 fprintf (stream
, _("\
285 -G, --stabs Display (in raw form) any STABS info in the file\n"));
286 fprintf (stream
, _("\
287 -W, --dwarf[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
288 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
289 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
290 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
292 Display the contents of DWARF debug sections\n"));
293 fprintf (stream
, _("\
294 -Wk,--dwarf=links Display the contents of sections that link to\n\
295 separate debuginfo files\n"));
296 #if DEFAULT_FOR_FOLLOW_LINKS
297 fprintf (stream
, _("\
298 -WK,--dwarf=follow-links\n\
299 Follow links to separate debug info files (default)\n"));
300 fprintf (stream
, _("\
301 -WN,--dwarf=no-follow-links\n\
302 Do not follow links to separate debug info files\n"));
304 fprintf (stream
, _("\
305 -WK,--dwarf=follow-links\n\
306 Follow links to separate debug info files\n"));
307 fprintf (stream
, _("\
308 -WN,--dwarf=no-follow-links\n\
309 Do not follow links to separate debug info files\n\
312 #if HAVE_LIBDEBUGINFOD
313 fprintf (stream
, _("\
314 -WD --dwarf=use-debuginfod\n\
315 When following links, also query debuginfod servers (default)\n"));
316 fprintf (stream
, _("\
317 -WE --dwarf=do-not-use-debuginfod\n\
318 When following links, do not query debuginfod servers\n"));
320 fprintf (stream
, _("\
321 -L, --process-links Display the contents of non-debug sections in\n\
322 separate debuginfo files. (Implies -WK)\n"));
324 fprintf (stream
, _("\
325 --ctf[=SECTION] Display CTF info from SECTION, (default `.ctf')\n"));
327 fprintf (stream
, _("\
328 --sframe[=SECTION] Display SFrame info from SECTION, (default '.sframe')\n"));
329 fprintf (stream
, _("\
330 -t, --syms Display the contents of the symbol table(s)\n"));
331 fprintf (stream
, _("\
332 -T, --dynamic-syms Display the contents of the dynamic symbol table\n"));
333 fprintf (stream
, _("\
334 -r, --reloc Display the relocation entries in the file\n"));
335 fprintf (stream
, _("\
336 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n"));
337 fprintf (stream
, _("\
338 @<file> Read options from <file>\n"));
339 fprintf (stream
, _("\
340 -v, --version Display this program's version number\n"));
341 fprintf (stream
, _("\
342 -i, --info List object formats and architectures supported\n"));
343 fprintf (stream
, _("\
344 -H, --help Display this information\n"));
348 const struct objdump_private_desc
* const *desc
;
350 fprintf (stream
, _("\n The following switches are optional:\n"));
351 fprintf (stream
, _("\
352 -b, --target=BFDNAME Specify the target object format as BFDNAME\n"));
353 fprintf (stream
, _("\
354 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n"));
355 fprintf (stream
, _("\
356 -j, --section=NAME Only display information for section NAME\n"));
357 fprintf (stream
, _("\
358 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n"));
359 fprintf (stream
, _("\
360 -EB --endian=big Assume big endian format when disassembling\n"));
361 fprintf (stream
, _("\
362 -EL --endian=little Assume little endian format when disassembling\n"));
363 fprintf (stream
, _("\
364 --file-start-context Include context from start of file (with -S)\n"));
365 fprintf (stream
, _("\
366 -I, --include=DIR Add DIR to search list for source files\n"));
367 fprintf (stream
, _("\
368 -l, --line-numbers Include line numbers and filenames in output\n"));
369 fprintf (stream
, _("\
370 -F, --file-offsets Include file offsets when displaying information\n"));
371 fprintf (stream
, _("\
372 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n"));
373 display_demangler_styles (stream
, _("\
375 fprintf (stream
, _("\
376 --recurse-limit Enable a limit on recursion whilst demangling\n\
378 fprintf (stream
, _("\
379 --no-recurse-limit Disable a limit on recursion whilst demangling\n"));
380 fprintf (stream
, _("\
381 -w, --wide Format output for more than 80 columns\n"));
382 fprintf (stream
, _("\
383 -U[d|l|i|x|e|h] Controls the display of UTF-8 unicode characters\n\
384 --unicode=[default|locale|invalid|hex|escape|highlight]\n"));
385 fprintf (stream
, _("\
386 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n"));
387 fprintf (stream
, _("\
388 --start-address=ADDR Only process data whose address is >= ADDR\n"));
389 fprintf (stream
, _("\
390 --stop-address=ADDR Only process data whose address is < ADDR\n"));
391 fprintf (stream
, _("\
392 --no-addresses Do not print address alongside disassembly\n"));
393 fprintf (stream
, _("\
394 --prefix-addresses Print complete address alongside disassembly\n"));
395 fprintf (stream
, _("\
396 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n"));
397 fprintf (stream
, _("\
398 --insn-width=WIDTH Display WIDTH bytes on a single line for -d\n"));
399 fprintf (stream
, _("\
400 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n"));
401 fprintf (stream
, _("\
402 --show-all-symbols When disassembling, display all symbols at a given address\n"));
403 fprintf (stream
, _("\
404 --special-syms Include special symbols in symbol dumps\n"));
405 fprintf (stream
, _("\
406 --inlines Print all inlines for source line (with -l)\n"));
407 fprintf (stream
, _("\
408 --prefix=PREFIX Add PREFIX to absolute paths for -S\n"));
409 fprintf (stream
, _("\
410 --prefix-strip=LEVEL Strip initial directory names for -S\n"));
411 fprintf (stream
, _("\
412 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
413 fprintf (stream
, _("\
414 --dwarf-start=N Display DIEs starting at offset N\n"));
415 fprintf (stream
, _("\
416 --dwarf-check Make additional dwarf consistency checks.\n"));
418 fprintf (stream
, _("\
419 --ctf-parent=NAME Use CTF archive member NAME as the CTF parent\n"));
421 fprintf (stream
, _("\
422 --visualize-jumps Visualize jumps by drawing ASCII art lines\n"));
423 fprintf (stream
, _("\
424 --visualize-jumps=color Use colors in the ASCII art\n"));
425 fprintf (stream
, _("\
426 --visualize-jumps=extended-color\n\
427 Use extended 8-bit color codes\n"));
428 fprintf (stream
, _("\
429 --visualize-jumps=off Disable jump visualization\n"));
430 #if DEFAULT_FOR_COLORED_DISASSEMBLY
431 fprintf (stream
, _("\
432 --disassembler-color=off Disable disassembler color output.\n"));
433 fprintf (stream
, _("\
434 --disassembler-color=terminal Enable disassembler color output if displaying on a terminal. (default)\n"));
436 fprintf (stream
, _("\
437 --disassembler-color=off Disable disassembler color output. (default)\n"));
438 fprintf (stream
, _("\
439 --disassembler-color=terminal Enable disassembler color output if displaying on a terminal.\n"));
441 fprintf (stream
, _("\
442 --disassembler-color=on Enable disassembler color output.\n"));
443 fprintf (stream
, _("\
444 --disassembler-color=extended Use 8-bit colors in disassembler output.\n\n"));
446 list_supported_targets (program_name
, stream
);
447 list_supported_architectures (program_name
, stream
);
449 disassembler_usage (stream
);
451 if (objdump_private_vectors
[0] != NULL
)
454 _("\nOptions supported for -P/--private switch:\n"));
455 for (desc
= objdump_private_vectors
; *desc
!= NULL
; desc
++)
456 (*desc
)->help (stream
);
459 if (REPORT_BUGS_TO
[0] && status
== 0)
460 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
464 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
468 OPTION_START_ADDRESS
,
478 OPTION_RECURSE_LIMIT
,
479 OPTION_NO_RECURSE_LIMIT
,
481 OPTION_SOURCE_COMMENT
,
487 OPTION_VISUALIZE_JUMPS
,
488 OPTION_DISASSEMBLER_COLOR
491 static struct option long_options
[]=
493 {"adjust-vma", required_argument
, NULL
, OPTION_ADJUST_VMA
},
494 {"all-headers", no_argument
, NULL
, 'x'},
495 {"architecture", required_argument
, NULL
, 'm'},
496 {"archive-headers", no_argument
, NULL
, 'a'},
498 {"ctf", optional_argument
, NULL
, OPTION_CTF
},
499 {"ctf-parent", required_argument
, NULL
, OPTION_CTF_PARENT
},
501 {"debugging", no_argument
, NULL
, 'g'},
502 {"debugging-tags", no_argument
, NULL
, 'e'},
503 {"demangle", optional_argument
, NULL
, 'C'},
504 {"disassemble", optional_argument
, NULL
, 'd'},
505 {"disassemble-all", no_argument
, NULL
, 'D'},
506 {"disassemble-zeroes", no_argument
, NULL
, 'z'},
507 {"disassembler-options", required_argument
, NULL
, 'M'},
508 {"dwarf", optional_argument
, NULL
, OPTION_DWARF
},
509 {"dwarf-check", no_argument
, 0, OPTION_DWARF_CHECK
},
510 {"dwarf-depth", required_argument
, 0, OPTION_DWARF_DEPTH
},
511 {"dwarf-start", required_argument
, 0, OPTION_DWARF_START
},
512 {"dynamic-reloc", no_argument
, NULL
, 'R'},
513 {"dynamic-syms", no_argument
, NULL
, 'T'},
514 {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
515 {"file-headers", no_argument
, NULL
, 'f'},
516 {"file-offsets", no_argument
, NULL
, 'F'},
517 {"file-start-context", no_argument
, &file_start_context
, 1},
518 {"full-contents", no_argument
, NULL
, 's'},
519 {"headers", no_argument
, NULL
, 'h'},
520 {"help", no_argument
, NULL
, 'H'},
521 {"include", required_argument
, NULL
, 'I'},
522 {"info", no_argument
, NULL
, 'i'},
523 {"inlines", no_argument
, 0, OPTION_INLINES
},
524 {"insn-width", required_argument
, NULL
, OPTION_INSN_WIDTH
},
525 {"line-numbers", no_argument
, NULL
, 'l'},
526 {"no-addresses", no_argument
, &no_addresses
, 1},
527 {"no-recurse-limit", no_argument
, NULL
, OPTION_NO_RECURSE_LIMIT
},
528 {"no-recursion-limit", no_argument
, NULL
, OPTION_NO_RECURSE_LIMIT
},
529 {"no-show-raw-insn", no_argument
, &show_raw_insn
, -1},
530 {"prefix", required_argument
, NULL
, OPTION_PREFIX
},
531 {"prefix-addresses", no_argument
, &prefix_addresses
, 1},
532 {"prefix-strip", required_argument
, NULL
, OPTION_PREFIX_STRIP
},
533 {"private", required_argument
, NULL
, 'P'},
534 {"private-headers", no_argument
, NULL
, 'p'},
535 {"process-links", no_argument
, &process_links
, true},
536 {"recurse-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
537 {"recursion-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
538 {"reloc", no_argument
, NULL
, 'r'},
539 {"section", required_argument
, NULL
, 'j'},
540 {"section-headers", no_argument
, NULL
, 'h'},
541 {"sframe", optional_argument
, NULL
, OPTION_SFRAME
},
542 {"show-all-symbols", no_argument
, &show_all_symbols
, 1},
543 {"show-raw-insn", no_argument
, &show_raw_insn
, 1},
544 {"source", no_argument
, NULL
, 'S'},
545 {"source-comment", optional_argument
, NULL
, OPTION_SOURCE_COMMENT
},
546 {"special-syms", no_argument
, &dump_special_syms
, 1},
547 {"stabs", no_argument
, NULL
, 'G'},
548 {"start-address", required_argument
, NULL
, OPTION_START_ADDRESS
},
549 {"stop-address", required_argument
, NULL
, OPTION_STOP_ADDRESS
},
550 {"syms", no_argument
, NULL
, 't'},
551 {"target", required_argument
, NULL
, 'b'},
552 {"unicode", required_argument
, NULL
, 'U'},
553 {"version", no_argument
, NULL
, 'V'},
554 {"visualize-jumps", optional_argument
, 0, OPTION_VISUALIZE_JUMPS
},
555 {"wide", no_argument
, NULL
, 'w'},
556 {"disassembler-color", required_argument
, NULL
, OPTION_DISASSEMBLER_COLOR
},
557 {NULL
, no_argument
, NULL
, 0}
561 my_bfd_nonfatal (const char *msg
)
567 /* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
568 The conversion format is controlled by the unicode_display variable.
569 Returns the number of characters added to OUT.
570 Returns the number of bytes consumed from IN in CONSUMED.
571 Always consumes at least one byte and displays at least one character. */
574 display_utf8 (const unsigned char * in
, char * out
, unsigned int * consumed
)
576 char * orig_out
= out
;
577 unsigned int nchars
= 0;
580 if (unicode_display
== unicode_default
)
586 if ((in
[1] & 0xc0) != 0x80)
589 if ((in
[0] & 0x20) == 0)
595 if ((in
[2] & 0xc0) != 0x80)
598 if ((in
[0] & 0x10) == 0)
604 if ((in
[3] & 0xc0) != 0x80)
610 switch (unicode_display
)
613 /* Copy the bytes into the output buffer as is. */
614 memcpy (out
, in
, nchars
);
618 case unicode_invalid
:
620 *out
++ = unicode_display
== unicode_hex
? '<' : '{';
623 for (j
= 0; j
< nchars
; j
++)
624 out
+= sprintf (out
, "%02x", in
[j
]);
625 *out
++ = 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
, "\x1B[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 /* For 2 char unicode, max out is 12 (colour escapes) + 6, ie. 9 per in
716 For hex, max out is 8 for 2 char unicode, ie. 4 per in.
717 3 and 4 char unicode produce less output for input. */
718 size_t max_needed
= strlen (in
) * 9 + 1;
719 if (buffer_len
< max_needed
)
721 buffer_len
= max_needed
;
723 buffer
= xmalloc (buffer_len
);
729 unsigned char c
= *in
++;
739 else if (unicode_display
!= unicode_default
&& c
>= 0xc0)
741 unsigned int num_consumed
;
743 out
+= display_utf8 ((const unsigned char *) --in
, out
, &num_consumed
);
756 /* Returns TRUE if the specified section should be dumped. */
759 process_section_p (asection
* section
)
763 if (only_list
== NULL
)
766 for (only
= only_list
; only
; only
= only
->next
)
767 if (strcmp (only
->name
, section
->name
) == 0)
776 /* Add an entry to the 'only' list. */
779 add_only (char * name
)
783 /* First check to make sure that we do not
784 already have an entry for this name. */
785 for (only
= only_list
; only
; only
= only
->next
)
786 if (strcmp (only
->name
, name
) == 0)
789 only
= xmalloc (sizeof * only
);
792 only
->next
= only_list
;
796 /* Release the memory used by the 'only' list.
797 PR 11225: Issue a warning message for unseen sections.
798 Only do this if none of the sections were seen. This is mainly to support
799 tools like the GAS testsuite where an object file is dumped with a list of
800 generic section names known to be present in a range of different file
804 free_only_list (void)
806 bool at_least_one_seen
= false;
810 if (only_list
== NULL
)
813 for (only
= only_list
; only
; only
= only
->next
)
816 at_least_one_seen
= true;
820 for (only
= only_list
; only
; only
= next
)
822 if (! at_least_one_seen
)
824 non_fatal (_("section '%s' mentioned in a -j option, "
825 "but not found in any input file"),
836 dump_section_header (bfd
*abfd
, asection
*section
, void *data
)
839 unsigned int opb
= bfd_octets_per_byte (abfd
, section
);
840 int longest_section_name
= *((int *) data
);
842 /* Ignore linker created section. See elfNN_ia64_object_p in
844 if (section
->flags
& SEC_LINKER_CREATED
)
847 /* PR 10413: Skip sections that we are ignoring. */
848 if (! process_section_p (section
))
851 printf ("%3d %-*s %08lx ", section
->index
, longest_section_name
,
852 sanitize_string (bfd_section_name (section
)),
853 (unsigned long) bfd_section_size (section
) / opb
);
854 bfd_printf_vma (abfd
, bfd_section_vma (section
));
856 bfd_printf_vma (abfd
, section
->lma
);
857 printf (" %08lx 2**%u", (unsigned long) section
->filepos
,
858 bfd_section_alignment (section
));
864 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
866 PF (SEC_HAS_CONTENTS
, "CONTENTS");
867 PF (SEC_ALLOC
, "ALLOC");
868 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
869 PF (SEC_LOAD
, "LOAD");
870 PF (SEC_RELOC
, "RELOC");
871 PF (SEC_READONLY
, "READONLY");
872 PF (SEC_CODE
, "CODE");
873 PF (SEC_DATA
, "DATA");
875 PF (SEC_DEBUGGING
, "DEBUGGING");
876 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
877 PF (SEC_EXCLUDE
, "EXCLUDE");
878 PF (SEC_SORT_ENTRIES
, "SORT_ENTRIES");
879 if (bfd_get_arch (abfd
) == bfd_arch_tic54x
)
881 PF (SEC_TIC54X_BLOCK
, "BLOCK");
882 PF (SEC_TIC54X_CLINK
, "CLINK");
884 PF (SEC_SMALL_DATA
, "SMALL_DATA");
885 if (bfd_get_flavour (abfd
) == bfd_target_coff_flavour
)
887 PF (SEC_COFF_SHARED
, "SHARED");
888 PF (SEC_COFF_NOREAD
, "NOREAD");
890 else if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
892 PF (SEC_ELF_OCTETS
, "OCTETS");
893 PF (SEC_ELF_PURECODE
, "PURECODE");
895 PF (SEC_THREAD_LOCAL
, "THREAD_LOCAL");
896 PF (SEC_GROUP
, "GROUP");
897 if (bfd_get_arch (abfd
) == bfd_arch_mep
)
899 PF (SEC_MEP_VLIW
, "VLIW");
902 if ((section
->flags
& SEC_LINK_ONCE
) != 0)
905 struct coff_comdat_info
*comdat
;
907 switch (section
->flags
& SEC_LINK_DUPLICATES
)
911 case SEC_LINK_DUPLICATES_DISCARD
:
912 ls
= "LINK_ONCE_DISCARD";
914 case SEC_LINK_DUPLICATES_ONE_ONLY
:
915 ls
= "LINK_ONCE_ONE_ONLY";
917 case SEC_LINK_DUPLICATES_SAME_SIZE
:
918 ls
= "LINK_ONCE_SAME_SIZE";
920 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
921 ls
= "LINK_ONCE_SAME_CONTENTS";
924 printf ("%s%s", comma
, ls
);
926 comdat
= bfd_coff_get_comdat_section (abfd
, section
);
928 printf (" (COMDAT %s %ld)", comdat
->name
, comdat
->symbol
);
937 /* Called on each SECTION in ABFD, update the int variable pointed to by
938 DATA which contains the string length of the longest section name. */
941 find_longest_section_name (bfd
*abfd ATTRIBUTE_UNUSED
,
942 asection
*section
, void *data
)
944 int *longest_so_far
= (int *) data
;
948 /* Ignore linker created section. */
949 if (section
->flags
& SEC_LINKER_CREATED
)
952 /* Skip sections that we are ignoring. */
953 if (! process_section_p (section
))
956 name
= bfd_section_name (section
);
957 len
= (int) strlen (name
);
958 if (len
> *longest_so_far
)
959 *longest_so_far
= len
;
963 dump_headers (bfd
*abfd
)
965 /* The default width of 13 is just an arbitrary choice. */
966 int max_section_name_length
= 13;
972 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
973 if (bfd_get_arch_size (abfd
) == 32)
979 printf (_("Sections:\n"));
982 bfd_map_over_sections (abfd
, find_longest_section_name
,
983 &max_section_name_length
);
985 printf (_("Idx %-*s Size %-*s%-*sFile off Algn"),
986 max_section_name_length
, "Name",
987 bfd_vma_width
, "VMA",
988 bfd_vma_width
, "LMA");
991 printf (_(" Flags"));
994 bfd_map_over_sections (abfd
, dump_section_header
,
995 &max_section_name_length
);
999 slurp_symtab (bfd
*abfd
)
1002 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
1005 long storage
= bfd_get_symtab_upper_bound (abfd
);
1008 non_fatal (_("failed to read symbol table from: %s"),
1009 bfd_get_filename (abfd
));
1010 my_bfd_nonfatal (_("error message was"));
1016 asymbol
**sy
= (asymbol
**) xmalloc (storage
);
1017 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
1020 my_bfd_nonfatal (bfd_get_filename (abfd
));
1028 /* Read in the dynamic symbols. */
1031 slurp_dynamic_symtab (bfd
*abfd
)
1034 long storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
1037 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
1039 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd
));
1044 my_bfd_nonfatal (bfd_get_filename (abfd
));
1050 asymbol
**sy
= (asymbol
**) xmalloc (storage
);
1051 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
1052 if (dynsymcount
< 0)
1054 my_bfd_nonfatal (bfd_get_filename (abfd
));
1062 /* Some symbol names are significant and should be kept in the
1063 table of sorted symbol names, even if they are marked as
1064 debugging/section symbols. */
1067 is_significant_symbol_name (const char * name
)
1069 return startswith (name
, ".plt") || startswith (name
, ".got");
1072 /* Filter out (in place) symbols that are useless for disassembly.
1073 COUNT is the number of elements in SYMBOLS.
1074 Return the number of useful symbols. */
1077 remove_useless_symbols (asymbol
**symbols
, long count
)
1079 asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
1081 while (--count
>= 0)
1083 asymbol
*sym
= *in_ptr
++;
1085 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
1087 if ((sym
->flags
& (BSF_DEBUGGING
| BSF_SECTION_SYM
))
1088 && ! is_significant_symbol_name (sym
->name
))
1090 if (bfd_is_und_section (sym
->section
)
1091 || bfd_is_com_section (sym
->section
))
1096 return out_ptr
- symbols
;
1099 static const asection
*compare_section
;
1101 /* Sort symbols into value order. */
1104 compare_symbols (const void *ap
, const void *bp
)
1106 const asymbol
*a
= * (const asymbol
**) ap
;
1107 const asymbol
*b
= * (const asymbol
**) bp
;
1112 bool as
, af
, bs
, bf
;
1116 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
1118 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
1121 /* Prefer symbols from the section currently being disassembled.
1122 Don't sort symbols from other sections by section, since there
1123 isn't much reason to prefer one section over another otherwise.
1124 See sym_ok comment for why we compare by section name. */
1125 as
= strcmp (compare_section
->name
, a
->section
->name
) == 0;
1126 bs
= strcmp (compare_section
->name
, b
->section
->name
) == 0;
1132 an
= bfd_asymbol_name (a
);
1133 bn
= bfd_asymbol_name (b
);
1137 /* The symbols gnu_compiled and gcc2_compiled convey no real
1138 information, so put them after other symbols with the same value. */
1139 af
= (strstr (an
, "gnu_compiled") != NULL
1140 || strstr (an
, "gcc2_compiled") != NULL
);
1141 bf
= (strstr (bn
, "gnu_compiled") != NULL
1142 || strstr (bn
, "gcc2_compiled") != NULL
);
1149 /* We use a heuristic for the file name, to try to sort it after
1150 more useful symbols. It may not work on non Unix systems, but it
1151 doesn't really matter; the only difference is precisely which
1152 symbol names get printed. */
1154 #define file_symbol(s, sn, snl) \
1155 (((s)->flags & BSF_FILE) != 0 \
1157 && (sn)[(snl) - 2] == '.' \
1158 && ((sn)[(snl) - 1] == 'o' \
1159 || (sn)[(snl) - 1] == 'a')))
1161 af
= file_symbol (a
, an
, anl
);
1162 bf
= file_symbol (b
, bn
, bnl
);
1169 /* Sort function and object symbols before global symbols before
1170 local symbols before section symbols before debugging symbols. */
1175 if ((aflags
& BSF_DEBUGGING
) != (bflags
& BSF_DEBUGGING
))
1177 if ((aflags
& BSF_DEBUGGING
) != 0)
1182 if ((aflags
& BSF_SECTION_SYM
) != (bflags
& BSF_SECTION_SYM
))
1184 if ((aflags
& BSF_SECTION_SYM
) != 0)
1189 if ((aflags
& BSF_FUNCTION
) != (bflags
& BSF_FUNCTION
))
1191 if ((aflags
& BSF_FUNCTION
) != 0)
1196 if ((aflags
& BSF_OBJECT
) != (bflags
& BSF_OBJECT
))
1198 if ((aflags
& BSF_OBJECT
) != 0)
1203 if ((aflags
& BSF_LOCAL
) != (bflags
& BSF_LOCAL
))
1205 if ((aflags
& BSF_LOCAL
) != 0)
1210 if ((aflags
& BSF_GLOBAL
) != (bflags
& BSF_GLOBAL
))
1212 if ((aflags
& BSF_GLOBAL
) != 0)
1218 /* Sort larger size ELF symbols before smaller. See PR20337. */
1220 if ((a
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0
1221 && bfd_get_flavour (bfd_asymbol_bfd (a
)) == bfd_target_elf_flavour
)
1222 asz
= ((elf_symbol_type
*) a
)->internal_elf_sym
.st_size
;
1224 if ((b
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0
1225 && bfd_get_flavour (bfd_asymbol_bfd (b
)) == bfd_target_elf_flavour
)
1226 bsz
= ((elf_symbol_type
*) b
)->internal_elf_sym
.st_size
;
1228 return asz
> bsz
? -1 : 1;
1230 /* Symbols that start with '.' might be section names, so sort them
1231 after symbols that don't start with '.'. */
1232 if (an
[0] == '.' && bn
[0] != '.')
1234 if (an
[0] != '.' && bn
[0] == '.')
1237 /* Finally, if we can't distinguish them in any other way, try to
1238 get consistent results by sorting the symbols by name. */
1239 return strcmp (an
, bn
);
1242 /* Sort relocs into address order. */
1245 compare_relocs (const void *ap
, const void *bp
)
1247 const arelent
*a
= * (const arelent
**) ap
;
1248 const arelent
*b
= * (const arelent
**) bp
;
1250 if (a
->address
> b
->address
)
1252 else if (a
->address
< b
->address
)
1255 /* So that associated relocations tied to the same address show up
1256 in the correct order, we don't do any further sorting. */
1265 /* Print an address (VMA) to the output stream in INFO.
1266 If SKIP_ZEROES is TRUE, omit leading zeroes. */
1269 objdump_print_value (bfd_vma vma
, struct disassemble_info
*inf
,
1274 struct objdump_disasm_info
*aux
;
1276 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1277 bfd_sprintf_vma (aux
->abfd
, buf
, vma
);
1282 for (p
= buf
; *p
== '0'; ++p
)
1287 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_address
, "%s", p
);
1290 /* Print the name of a symbol. */
1293 objdump_print_symname (bfd
*abfd
, struct disassemble_info
*inf
,
1297 const char *name
, *version_string
= NULL
;
1298 bool hidden
= false;
1301 name
= bfd_asymbol_name (sym
);
1302 if (do_demangle
&& name
[0] != '\0')
1304 /* Demangle the name. */
1305 alloc
= bfd_demangle (abfd
, name
, demangle_flags
);
1310 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0)
1311 version_string
= bfd_get_symbol_version_string (abfd
, sym
, true,
1314 if (bfd_is_und_section (bfd_asymbol_section (sym
)))
1317 name
= sanitize_string (name
);
1321 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_symbol
, "%s", name
);
1322 if (version_string
&& *version_string
!= '\0')
1323 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_symbol
,
1324 hidden
? "@%s" : "@@%s",
1329 printf ("%s", name
);
1330 if (version_string
&& *version_string
!= '\0')
1331 printf (hidden
? "@%s" : "@@%s", version_string
);
1339 sym_ok (bool want_section
,
1340 bfd
*abfd ATTRIBUTE_UNUSED
,
1343 struct disassemble_info
*inf
)
1347 /* NB: An object file can have different sections with the same
1348 section name. Compare compare section pointers if they have
1350 if (sorted_syms
[place
]->section
->owner
== sec
->owner
1351 && sorted_syms
[place
]->section
!= sec
)
1354 /* Note - we cannot just compare section pointers because they could
1355 be different, but the same... Ie the symbol that we are trying to
1356 find could have come from a separate debug info file. Under such
1357 circumstances the symbol will be associated with a section in the
1358 debug info file, whilst the section we want is in a normal file.
1359 So the section pointers will be different, but the section names
1360 will be the same. */
1361 if (strcmp (bfd_section_name (sorted_syms
[place
]->section
),
1362 bfd_section_name (sec
)) != 0)
1366 return inf
->symbol_is_valid (sorted_syms
[place
], inf
);
1369 /* Locate a symbol given a bfd and a section (from INFO->application_data),
1370 and a VMA. If INFO->application_data->require_sec is TRUE, then always
1371 require the symbol to be in the section. Returns NULL if there is no
1372 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
1373 of the symbol in sorted_syms. */
1376 find_symbol_for_address (bfd_vma vma
,
1377 struct disassemble_info
*inf
,
1380 /* @@ Would it speed things up to cache the last two symbols returned,
1381 and maybe their address ranges? For many processors, only one memory
1382 operand can be present at a time, so the 2-entry cache wouldn't be
1383 constantly churned by code doing heavy memory accesses. */
1385 /* Indices in `sorted_syms'. */
1387 long max_count
= sorted_symcount
;
1389 struct objdump_disasm_info
*aux
;
1396 if (sorted_symcount
< 1)
1399 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1402 opb
= inf
->octets_per_byte
;
1404 /* Perform a binary search looking for the closest symbol to the
1405 required value. We are searching the range (min, max_count]. */
1406 while (min
+ 1 < max_count
)
1410 thisplace
= (max_count
+ min
) / 2;
1411 sym
= sorted_syms
[thisplace
];
1413 if (bfd_asymbol_value (sym
) > vma
)
1414 max_count
= thisplace
;
1415 else if (bfd_asymbol_value (sym
) < vma
)
1424 /* The symbol we want is now in min, the low end of the range we
1425 were searching. If there are several symbols with the same
1426 value, we want the first one. */
1428 while (thisplace
> 0
1429 && (bfd_asymbol_value (sorted_syms
[thisplace
])
1430 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
1433 /* Prefer a symbol in the current section if we have multple symbols
1434 with the same value, as can occur with overlays or zero size
1437 while (min
< max_count
1438 && (bfd_asymbol_value (sorted_syms
[min
])
1439 == bfd_asymbol_value (sorted_syms
[thisplace
])))
1441 if (sym_ok (true, abfd
, min
, sec
, inf
))
1448 return sorted_syms
[thisplace
];
1453 /* If the file is relocatable, and the symbol could be from this
1454 section, prefer a symbol from this section over symbols from
1455 others, even if the other symbol's value might be closer.
1457 Note that this may be wrong for some symbol references if the
1458 sections have overlapping memory ranges, but in that case there's
1459 no way to tell what's desired without looking at the relocation
1462 Also give the target a chance to reject symbols. */
1463 want_section
= (aux
->require_sec
1464 || ((abfd
->flags
& HAS_RELOC
) != 0
1465 && vma
>= bfd_section_vma (sec
)
1466 && vma
< (bfd_section_vma (sec
)
1467 + bfd_section_size (sec
) / opb
)));
1469 if (! sym_ok (want_section
, abfd
, thisplace
, sec
, inf
))
1472 long newplace
= sorted_symcount
;
1474 for (i
= min
- 1; i
>= 0; i
--)
1476 if (sym_ok (want_section
, abfd
, i
, sec
, inf
))
1478 if (newplace
== sorted_symcount
)
1481 if (bfd_asymbol_value (sorted_syms
[i
])
1482 != bfd_asymbol_value (sorted_syms
[newplace
]))
1485 /* Remember this symbol and keep searching until we reach
1486 an earlier address. */
1491 if (newplace
!= sorted_symcount
)
1492 thisplace
= newplace
;
1495 /* We didn't find a good symbol with a smaller value.
1496 Look for one with a larger value. */
1497 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
1499 if (sym_ok (want_section
, abfd
, i
, sec
, inf
))
1507 if (! sym_ok (want_section
, abfd
, thisplace
, sec
, inf
))
1508 /* There is no suitable symbol. */
1512 /* If we have not found an exact match for the specified address
1513 and we have dynamic relocations available, then we can produce
1514 a better result by matching a relocation to the address and
1515 using the symbol associated with that relocation. */
1516 rel_count
= inf
->dynrelcount
;
1518 && sorted_syms
[thisplace
]->value
!= vma
1520 && inf
->dynrelbuf
!= NULL
1521 && inf
->dynrelbuf
[0]->address
<= vma
1522 && inf
->dynrelbuf
[rel_count
- 1]->address
>= vma
1523 /* If we have matched a synthetic symbol, then stick with that. */
1524 && (sorted_syms
[thisplace
]->flags
& BSF_SYNTHETIC
) == 0)
1527 arelent
** rel_high
;
1529 rel_low
= inf
->dynrelbuf
;
1530 rel_high
= rel_low
+ rel_count
- 1;
1531 while (rel_low
<= rel_high
)
1533 arelent
**rel_mid
= &rel_low
[(rel_high
- rel_low
) / 2];
1534 arelent
* rel
= *rel_mid
;
1536 if (rel
->address
== vma
)
1538 /* Absolute relocations do not provide a more helpful
1539 symbolic address. Find a non-absolute relocation
1540 with the same address. */
1541 arelent
**rel_vma
= rel_mid
;
1543 rel_mid
>= rel_low
&& rel_mid
[0]->address
== vma
;
1547 for (; rel_vma
<= rel_high
&& rel_vma
[0]->address
== vma
;
1551 if (rel
->sym_ptr_ptr
!= NULL
1552 && ! bfd_is_abs_section ((* rel
->sym_ptr_ptr
)->section
))
1555 * place
= thisplace
;
1556 return * rel
->sym_ptr_ptr
;
1562 if (vma
< rel
->address
)
1564 else if (vma
>= rel_mid
[1]->address
)
1565 rel_low
= rel_mid
+ 1;
1574 return sorted_syms
[thisplace
];
1577 /* Print an address and the offset to the nearest symbol. */
1580 objdump_print_addr_with_sym (bfd
*abfd
, asection
*sec
, asymbol
*sym
,
1581 bfd_vma vma
, struct disassemble_info
*inf
,
1586 objdump_print_value (vma
, inf
, skip_zeroes
);
1587 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, " ");
1594 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
,"<");
1595 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_symbol
, "%s",
1596 sanitize_string (bfd_section_name (sec
)));
1597 secaddr
= bfd_section_vma (sec
);
1600 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
,
1602 objdump_print_value (secaddr
- vma
, inf
, true);
1604 else if (vma
> secaddr
)
1606 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
, "+0x");
1607 objdump_print_value (vma
- secaddr
, inf
, true);
1609 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, ">");
1613 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, "<");
1615 objdump_print_symname (abfd
, inf
, sym
);
1617 if (bfd_asymbol_value (sym
) == vma
)
1619 /* Undefined symbols in an executables and dynamic objects do not have
1620 a value associated with them, so it does not make sense to display
1621 an offset relative to them. Normally we would not be provided with
1622 this kind of symbol, but the target backend might choose to do so,
1623 and the code in find_symbol_for_address might return an as yet
1624 unresolved symbol associated with a dynamic reloc. */
1625 else if ((bfd_get_file_flags (abfd
) & (EXEC_P
| DYNAMIC
))
1626 && bfd_is_und_section (sym
->section
))
1628 else if (bfd_asymbol_value (sym
) > vma
)
1630 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
,"-0x");
1631 objdump_print_value (bfd_asymbol_value (sym
) - vma
, inf
, true);
1633 else if (vma
> bfd_asymbol_value (sym
))
1635 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
, "+0x");
1636 objdump_print_value (vma
- bfd_asymbol_value (sym
), inf
, true);
1639 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, ">");
1642 if (display_file_offsets
)
1643 inf
->fprintf_styled_func (inf
->stream
, dis_style_text
,
1644 _(" (File Offset: 0x%lx)"),
1645 (long int)(sec
->filepos
+ (vma
- sec
->vma
)));
1648 /* Print an address (VMA), symbolically if possible.
1649 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
1652 objdump_print_addr (bfd_vma vma
,
1653 struct disassemble_info
*inf
,
1656 struct objdump_disasm_info
*aux
;
1657 asymbol
*sym
= NULL
;
1658 bool skip_find
= false;
1660 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1662 if (sorted_symcount
< 1)
1666 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_address
, "0x");
1667 objdump_print_value (vma
, inf
, skip_zeroes
);
1670 if (display_file_offsets
)
1671 inf
->fprintf_styled_func (inf
->stream
, dis_style_text
,
1672 _(" (File Offset: 0x%lx)"),
1673 (long int) (inf
->section
->filepos
1674 + (vma
- inf
->section
->vma
)));
1678 if (aux
->reloc
!= NULL
1679 && aux
->reloc
->sym_ptr_ptr
!= NULL
1680 && * aux
->reloc
->sym_ptr_ptr
!= NULL
)
1682 sym
= * aux
->reloc
->sym_ptr_ptr
;
1684 /* Adjust the vma to the reloc. */
1685 vma
+= bfd_asymbol_value (sym
);
1687 if (bfd_is_und_section (bfd_asymbol_section (sym
)))
1692 sym
= find_symbol_for_address (vma
, inf
, NULL
);
1694 objdump_print_addr_with_sym (aux
->abfd
, inf
->section
, sym
, vma
, inf
,
1698 /* Print VMA to INFO. This function is passed to the disassembler
1702 objdump_print_address (bfd_vma vma
, struct disassemble_info
*inf
)
1704 objdump_print_addr (vma
, inf
, ! prefix_addresses
);
1707 /* Determine if the given address has a symbol associated with it. */
1710 objdump_symbol_at_address (bfd_vma vma
, struct disassemble_info
* inf
)
1714 sym
= find_symbol_for_address (vma
, inf
, NULL
);
1715 if (sym
!= NULL
&& bfd_asymbol_value (sym
) == vma
)
1721 /* Hold the last function name and the last line number we displayed
1722 in a disassembly. */
1724 static char *prev_functionname
;
1725 static unsigned int prev_line
;
1726 static unsigned int prev_discriminator
;
1728 /* We keep a list of all files that we have seen when doing a
1729 disassembly with source, so that we know how much of the file to
1730 display. This can be important for inlined functions. */
1732 struct print_file_list
1734 struct print_file_list
*next
;
1735 const char *filename
;
1736 const char *modname
;
1739 const char **linemap
;
1742 unsigned max_printed
;
1746 static struct print_file_list
*print_files
;
1748 /* The number of preceding context lines to show when we start
1749 displaying a file for the first time. */
1751 #define SHOW_PRECEDING_CONTEXT_LINES (5)
1753 #if HAVE_LIBDEBUGINFOD
1754 /* Return a hex string represention of the build-id. */
1757 get_build_id (void * data
)
1760 char * build_id_str
;
1761 bfd
* abfd
= (bfd
*) data
;
1762 const struct bfd_build_id
* build_id
;
1764 build_id
= abfd
->build_id
;
1765 if (build_id
== NULL
)
1768 build_id_str
= malloc (build_id
->size
* 2 + 1);
1769 if (build_id_str
== NULL
)
1772 for (i
= 0; i
< build_id
->size
; i
++)
1773 sprintf (build_id_str
+ (i
* 2), "%02x", build_id
->data
[i
]);
1774 build_id_str
[build_id
->size
* 2] = '\0';
1776 return (unsigned char *) build_id_str
;
1779 /* Search for a separate debug file matching ABFD's build-id. */
1782 find_separate_debug (const bfd
* abfd
)
1784 const struct bfd_build_id
* build_id
= abfd
->build_id
;
1785 separate_info
* i
= first_separate_info
;
1787 if (build_id
== NULL
|| i
== NULL
)
1792 const bfd
* i_bfd
= (bfd
*) i
->handle
;
1794 if (abfd
!= NULL
&& i_bfd
->build_id
!= NULL
)
1796 const unsigned char * data
= i_bfd
->build_id
->data
;
1797 size_t size
= i_bfd
->build_id
->size
;
1799 if (size
== build_id
->size
1800 && memcmp (data
, build_id
->data
, size
) == 0)
1801 return (bfd
*) i
->handle
;
1810 /* Search for a separate debug file matching ABFD's .gnu_debugaltlink
1814 find_alt_debug (const bfd
* abfd
)
1819 struct dwarf_section
* section
;
1820 const struct bfd_build_id
* build_id
= abfd
->build_id
;
1821 separate_info
* i
= first_separate_info
;
1825 || !load_debug_section (gnu_debugaltlink
, (void *) abfd
))
1828 section
= &debug_displays
[gnu_debugaltlink
].section
;
1829 if (section
== NULL
)
1832 name
= (const char *) section
->start
;
1833 namelen
= strnlen (name
, section
->size
) + 1;
1836 if (namelen
>= section
->size
)
1839 id_len
= section
->size
- namelen
;
1843 /* Compare the .gnu_debugaltlink build-id with the build-ids of the
1844 known separate_info files. */
1847 const bfd
* i_bfd
= (bfd
*) i
->handle
;
1849 if (i_bfd
!= NULL
&& i_bfd
->build_id
!= NULL
)
1851 const unsigned char * data
= i_bfd
->build_id
->data
;
1852 size_t size
= i_bfd
->build_id
->size
;
1855 && memcmp (section
->start
+ namelen
, data
, size
) == 0)
1856 return (bfd
*) i
->handle
;
1865 #endif /* HAVE_LIBDEBUGINFOD */
1867 /* Reads the contents of file FN into memory. Returns a pointer to the buffer.
1868 Also returns the size of the buffer in SIZE_RETURN and a filled out
1869 stat structure in FST_RETURN. Returns NULL upon failure. */
1872 slurp_file (const char * fn
,
1873 size_t * size_return
,
1874 struct stat
* fst_return
,
1875 bfd
* abfd ATTRIBUTE_UNUSED
)
1885 if (fn
== NULL
|| * fn
== 0 || size_return
== NULL
|| fst_return
== NULL
)
1888 fd
= open (fn
, O_RDONLY
| O_BINARY
);
1890 #if HAVE_LIBDEBUGINFOD
1891 if (fd
< 0 && use_debuginfod
&& fn
[0] == '/' && abfd
!= NULL
)
1893 unsigned char *build_id
= get_build_id (abfd
);
1897 debuginfod_client
*client
= debuginfod_begin ();
1901 fd
= debuginfod_find_source (client
, build_id
, 0, fn
, NULL
);
1902 debuginfod_end (client
);
1912 if (fstat (fd
, fst_return
) < 0)
1918 *size_return
= fst_return
->st_size
;
1921 ps
= getpagesize ();
1922 msize
= (*size_return
+ ps
- 1) & ~(ps
- 1);
1923 map
= mmap (NULL
, msize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1924 if (map
!= (char *) -1L)
1931 map
= (const char *) malloc (*size_return
);
1932 if (!map
|| (size_t) read (fd
, (char *) map
, *size_return
) != *size_return
)
1934 free ((void *) map
);
1941 #define line_map_decrease 5
1943 /* Precompute array of lines for a mapped file. */
1945 static const char **
1946 index_file (const char *map
, size_t size
, unsigned int *maxline
)
1948 const char *p
, *lstart
, *end
;
1949 int chars_per_line
= 45; /* First iteration will use 40. */
1950 unsigned int lineno
;
1951 const char **linemap
= NULL
;
1952 unsigned long line_map_size
= 0;
1958 for (p
= map
; p
< end
; p
++)
1962 if (p
+ 1 < end
&& p
[1] == '\r')
1965 else if (*p
== '\r')
1967 if (p
+ 1 < end
&& p
[1] == '\n')
1973 /* End of line found. */
1975 if (linemap
== NULL
|| line_map_size
< lineno
+ 1)
1977 unsigned long newsize
;
1979 chars_per_line
-= line_map_decrease
;
1980 if (chars_per_line
<= 1)
1982 line_map_size
= size
/ chars_per_line
+ 1;
1983 if (line_map_size
< lineno
+ 1)
1984 line_map_size
= lineno
+ 1;
1985 newsize
= line_map_size
* sizeof (char *);
1986 linemap
= (const char **) xrealloc (linemap
, newsize
);
1989 linemap
[lineno
++] = lstart
;
1997 /* Tries to open MODNAME, and if successful adds a node to print_files
1998 linked list and returns that node. Also fills in the stat structure
1999 pointed to by FST_RETURN. Returns NULL on failure. */
2001 static struct print_file_list
*
2002 try_print_file_open (const char * origname
,
2003 const char * modname
,
2004 struct stat
* fst_return
,
2007 struct print_file_list
*p
;
2009 p
= (struct print_file_list
*) xmalloc (sizeof (struct print_file_list
));
2011 p
->map
= slurp_file (modname
, &p
->mapsize
, fst_return
, abfd
);
2018 p
->linemap
= index_file (p
->map
, p
->mapsize
, &p
->maxline
);
2021 p
->filename
= origname
;
2022 p
->modname
= modname
;
2023 p
->next
= print_files
;
2029 /* If the source file, as described in the symtab, is not found
2030 try to locate it in one of the paths specified with -I
2031 If found, add location to print_files linked list. */
2033 static struct print_file_list
*
2034 update_source_path (const char *filename
, bfd
*abfd
)
2036 struct print_file_list
*p
;
2041 p
= try_print_file_open (filename
, filename
, &fst
, abfd
);
2044 if (include_path_count
== 0)
2047 /* Get the name of the file. */
2048 fname
= lbasename (filename
);
2050 /* If file exists under a new path, we need to add it to the list
2051 so that show_line knows about it. */
2052 for (i
= 0; i
< include_path_count
; i
++)
2054 char *modname
= concat (include_paths
[i
], "/", fname
,
2057 p
= try_print_file_open (filename
, modname
, &fst
, abfd
);
2067 long mtime
= bfd_get_mtime (abfd
);
2069 if (fst
.st_mtime
> mtime
)
2070 warn (_("source file %s is more recent than object file\n"),
2077 /* Print a source file line. */
2080 print_line (struct print_file_list
*p
, unsigned int linenum
)
2085 if (linenum
>= p
->maxline
)
2087 l
= p
->linemap
[linenum
];
2088 if (source_comment
!= NULL
&& strlen (l
) > 0)
2089 printf ("%s", source_comment
);
2090 len
= strcspn (l
, "\n\r");
2091 /* Test fwrite return value to quiet glibc warning. */
2092 if (len
== 0 || fwrite (l
, len
, 1, stdout
) == 1)
2096 /* Print a range of source code lines. */
2099 dump_lines (struct print_file_list
*p
, unsigned int start
, unsigned int end
)
2107 print_line (p
, start
);
2112 /* Show the line number, or the source line, in a disassembly
2116 show_line (bfd
*abfd
, asection
*section
, bfd_vma addr_offset
)
2118 const char *filename
;
2119 const char *functionname
;
2120 unsigned int linenumber
;
2121 unsigned int discriminator
;
2125 if (! with_line_numbers
&& ! with_source_code
)
2128 #ifdef HAVE_LIBDEBUGINFOD
2131 const char *alt_filename
= NULL
;
2137 /* PR 29075: Check for separate debuginfo and .gnu_debugaltlink files.
2138 They need to be passed to bfd_find_nearest_line_with_alt in case they
2139 were downloaded from debuginfod. Otherwise libbfd will attempt to
2140 search for them and fail to locate them. */
2141 debug_bfd
= find_separate_debug (abfd
);
2142 if (debug_bfd
== NULL
)
2145 alt_bfd
= find_alt_debug (debug_bfd
);
2146 if (alt_bfd
!= NULL
)
2147 alt_filename
= bfd_get_filename (alt_bfd
);
2152 bfd_set_error (bfd_error_no_error
);
2153 if (! bfd_find_nearest_line_with_alt (debug_bfd
, alt_filename
,
2155 addr_offset
, &filename
,
2156 &functionname
, &linenumber
,
2159 if (bfd_get_error () == bfd_error_no_error
)
2161 if (! bfd_find_nearest_line_discriminator (abfd
, section
, syms
,
2162 addr_offset
, &filename
,
2163 &functionname
, &linenumber
,
2169 if (! bfd_find_nearest_line_discriminator (abfd
, section
, syms
, addr_offset
,
2170 &filename
, &functionname
,
2171 &linenumber
, &discriminator
))
2175 if (filename
!= NULL
&& *filename
== '\0')
2177 if (functionname
!= NULL
&& *functionname
== '\0')
2178 functionname
= NULL
;
2181 && IS_ABSOLUTE_PATH (filename
)
2185 const char *fname
= filename
;
2187 path
= xmalloc (prefix_length
+ 1 + strlen (filename
));
2190 memcpy (path
, prefix
, prefix_length
);
2191 path_up
= path
+ prefix_length
;
2193 /* Build relocated filename, stripping off leading directories
2194 from the initial filename if requested. */
2195 if (prefix_strip
> 0)
2200 /* Skip selected directory levels. */
2201 for (s
= fname
+ 1; *s
!= '\0' && level
< prefix_strip
; s
++)
2202 if (IS_DIR_SEPARATOR (*s
))
2209 /* Update complete filename. */
2210 strcpy (path_up
, fname
);
2218 if (with_line_numbers
)
2220 if (functionname
!= NULL
2221 && (prev_functionname
== NULL
2222 || strcmp (functionname
, prev_functionname
) != 0))
2224 char *demangle_alloc
= NULL
;
2225 if (do_demangle
&& functionname
[0] != '\0')
2227 /* Demangle the name. */
2228 demangle_alloc
= bfd_demangle (abfd
, functionname
,
2232 /* Demangling adds trailing parens, so don't print those. */
2233 if (demangle_alloc
!= NULL
)
2234 printf ("%s:\n", sanitize_string (demangle_alloc
));
2236 printf ("%s():\n", sanitize_string (functionname
));
2239 free (demangle_alloc
);
2242 && (linenumber
!= prev_line
2243 || discriminator
!= prev_discriminator
))
2245 if (discriminator
> 0)
2246 printf ("%s:%u (discriminator %u)\n",
2247 filename
== NULL
? "???" : sanitize_string (filename
),
2248 linenumber
, discriminator
);
2250 printf ("%s:%u\n", filename
== NULL
2251 ? "???" : sanitize_string (filename
),
2256 const char *filename2
;
2257 const char *functionname2
;
2260 while (bfd_find_inliner_info (abfd
, &filename2
, &functionname2
,
2263 printf ("inlined by %s:%u",
2264 sanitize_string (filename2
), line2
);
2265 printf (" (%s)\n", sanitize_string (functionname2
));
2270 if (with_source_code
2274 struct print_file_list
**pp
, *p
;
2277 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
2278 if (filename_cmp ((*pp
)->filename
, filename
) == 0)
2285 filename
= xstrdup (filename
);
2286 p
= update_source_path (filename
, abfd
);
2289 if (p
!= NULL
&& linenumber
!= p
->last_line
)
2291 if (file_start_context
&& p
->first
)
2295 l
= linenumber
- SHOW_PRECEDING_CONTEXT_LINES
;
2296 if (l
>= linenumber
)
2298 if (p
->max_printed
>= l
)
2300 if (p
->max_printed
< linenumber
)
2301 l
= p
->max_printed
+ 1;
2306 dump_lines (p
, l
, linenumber
);
2307 if (p
->max_printed
< linenumber
)
2308 p
->max_printed
= linenumber
;
2309 p
->last_line
= linenumber
;
2314 if (functionname
!= NULL
2315 && (prev_functionname
== NULL
2316 || strcmp (functionname
, prev_functionname
) != 0))
2318 if (prev_functionname
!= NULL
)
2319 free (prev_functionname
);
2320 prev_functionname
= (char *) xmalloc (strlen (functionname
) + 1);
2321 strcpy (prev_functionname
, functionname
);
2324 if (linenumber
> 0 && linenumber
!= prev_line
)
2325 prev_line
= linenumber
;
2327 if (discriminator
!= prev_discriminator
)
2328 prev_discriminator
= discriminator
;
2334 /* Pseudo FILE object for strings. */
2342 /* sprintf to a "stream". */
2344 static int ATTRIBUTE_PRINTF_2
2345 objdump_sprintf (SFILE
*f
, const char *format
, ...)
2352 size_t space
= f
->alloc
- f
->pos
;
2354 va_start (args
, format
);
2355 n
= vsnprintf (f
->buffer
+ f
->pos
, space
, format
, args
);
2361 f
->alloc
= (f
->alloc
+ n
) * 2;
2362 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2369 /* Return an integer greater than, or equal to zero, representing the color
2370 for STYLE, or -1 if no color should be used. */
2373 objdump_color_for_disassembler_style (enum disassembler_style style
)
2377 if (style
== dis_style_comment_start
)
2378 disassembler_in_comment
= true;
2380 if (disassembler_color
== on
)
2382 if (disassembler_in_comment
)
2387 case dis_style_symbol
:
2390 case dis_style_assembler_directive
:
2391 case dis_style_sub_mnemonic
:
2392 case dis_style_mnemonic
:
2395 case dis_style_register
:
2398 case dis_style_address
:
2399 case dis_style_address_offset
:
2400 case dis_style_immediate
:
2404 case dis_style_text
:
2409 else if (disassembler_color
== extended
)
2411 if (disassembler_in_comment
)
2416 case dis_style_symbol
:
2419 case dis_style_assembler_directive
:
2420 case dis_style_sub_mnemonic
:
2421 case dis_style_mnemonic
:
2424 case dis_style_register
:
2427 case dis_style_address
:
2428 case dis_style_address_offset
:
2429 case dis_style_immediate
:
2433 case dis_style_text
:
2438 else if (disassembler_color
!= off
)
2439 bfd_fatal (_("disassembly color not correctly selected"));
2444 /* Like objdump_sprintf, but add in escape sequences to highlight the
2445 content according to STYLE. */
2447 static int ATTRIBUTE_PRINTF_3
2448 objdump_styled_sprintf (SFILE
*f
, enum disassembler_style style
,
2449 const char *format
, ...)
2453 int color
= objdump_color_for_disassembler_style (style
);
2459 size_t space
= f
->alloc
- f
->pos
;
2461 if (disassembler_color
== on
)
2462 n
= snprintf (f
->buffer
+ f
->pos
, space
, "\033[%dm", color
);
2464 n
= snprintf (f
->buffer
+ f
->pos
, space
, "\033[38;5;%dm", color
);
2468 f
->alloc
= (f
->alloc
+ n
) * 2;
2469 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2476 size_t space
= f
->alloc
- f
->pos
;
2478 va_start (args
, format
);
2479 n
= vsnprintf (f
->buffer
+ f
->pos
, space
, format
, args
);
2485 f
->alloc
= (f
->alloc
+ n
) * 2;
2486 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2494 size_t space
= f
->alloc
- f
->pos
;
2496 n
= snprintf (f
->buffer
+ f
->pos
, space
, "\033[0m");
2501 f
->alloc
= (f
->alloc
+ n
) * 2;
2502 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2510 /* We discard the styling information here. This function is only used
2511 when objdump is printing auxiliary information, the symbol headers, and
2512 disassembly address, or the bytes of the disassembled instruction. We
2513 don't (currently) apply styling to any of this stuff, so, for now, just
2514 print the content with no additional style added. */
2516 static int ATTRIBUTE_PRINTF_3
2517 fprintf_styled (FILE *f
, enum disassembler_style style ATTRIBUTE_UNUSED
,
2518 const char *fmt
, ...)
2524 res
= vfprintf (f
, fmt
, ap
);
2530 /* Code for generating (colored) diagrams of control flow start and end
2533 /* Structure used to store the properties of a jump. */
2537 /* The next jump, or NULL if this is the last object. */
2538 struct jump_info
*next
;
2539 /* The previous jump, or NULL if this is the first object. */
2540 struct jump_info
*prev
;
2541 /* The start addresses of the jump. */
2544 /* The list of start addresses. */
2546 /* The number of elements. */
2548 /* The maximum number of elements that fit into the array. */
2551 /* The end address of the jump. */
2553 /* The drawing level of the jump. */
2557 /* Construct a jump object for a jump from start
2558 to end with the corresponding level. */
2560 static struct jump_info
*
2561 jump_info_new (bfd_vma start
, bfd_vma end
, int level
)
2563 struct jump_info
*result
= xmalloc (sizeof (struct jump_info
));
2565 result
->next
= NULL
;
2566 result
->prev
= NULL
;
2567 result
->start
.addresses
= xmalloc (sizeof (bfd_vma
*) * 2);
2568 result
->start
.addresses
[0] = start
;
2569 result
->start
.count
= 1;
2570 result
->start
.max_count
= 2;
2572 result
->level
= level
;
2577 /* Free a jump object and return the next object
2578 or NULL if this was the last one. */
2580 static struct jump_info
*
2581 jump_info_free (struct jump_info
*ji
)
2583 struct jump_info
*result
= NULL
;
2588 if (ji
->start
.addresses
)
2589 free (ji
->start
.addresses
);
2596 /* Get the smallest value of all start and end addresses. */
2599 jump_info_min_address (const struct jump_info
*ji
)
2601 bfd_vma min_address
= ji
->end
;
2604 for (i
= ji
->start
.count
; i
-- > 0;)
2605 if (ji
->start
.addresses
[i
] < min_address
)
2606 min_address
= ji
->start
.addresses
[i
];
2610 /* Get the largest value of all start and end addresses. */
2613 jump_info_max_address (const struct jump_info
*ji
)
2615 bfd_vma max_address
= ji
->end
;
2618 for (i
= ji
->start
.count
; i
-- > 0;)
2619 if (ji
->start
.addresses
[i
] > max_address
)
2620 max_address
= ji
->start
.addresses
[i
];
2624 /* Get the target address of a jump. */
2627 jump_info_end_address (const struct jump_info
*ji
)
2632 /* Test if an address is one of the start addresses of a jump. */
2635 jump_info_is_start_address (const struct jump_info
*ji
, bfd_vma address
)
2637 bool result
= false;
2640 for (i
= ji
->start
.count
; i
-- > 0;)
2641 if (address
== ji
->start
.addresses
[i
])
2650 /* Test if an address is the target address of a jump. */
2653 jump_info_is_end_address (const struct jump_info
*ji
, bfd_vma address
)
2655 return (address
== ji
->end
);
2658 /* Get the difference between the smallest and largest address of a jump. */
2661 jump_info_size (const struct jump_info
*ji
)
2663 return jump_info_max_address (ji
) - jump_info_min_address (ji
);
2666 /* Unlink a jump object from a list. */
2669 jump_info_unlink (struct jump_info
*node
,
2670 struct jump_info
**base
)
2673 node
->next
->prev
= node
->prev
;
2675 node
->prev
->next
= node
->next
;
2682 /* Insert unlinked jump info node into a list. */
2685 jump_info_insert (struct jump_info
*node
,
2686 struct jump_info
*target
,
2687 struct jump_info
**base
)
2689 node
->next
= target
;
2690 node
->prev
= target
->prev
;
2691 target
->prev
= node
;
2693 node
->prev
->next
= node
;
2698 /* Add unlinked node to the front of a list. */
2701 jump_info_add_front (struct jump_info
*node
,
2702 struct jump_info
**base
)
2706 node
->next
->prev
= node
;
2711 /* Move linked node to target position. */
2714 jump_info_move_linked (struct jump_info
*node
,
2715 struct jump_info
*target
,
2716 struct jump_info
**base
)
2719 jump_info_unlink (node
, base
);
2720 /* Insert node at target position. */
2721 jump_info_insert (node
, target
, base
);
2724 /* Test if two jumps intersect. */
2727 jump_info_intersect (const struct jump_info
*a
,
2728 const struct jump_info
*b
)
2730 return ((jump_info_max_address (a
) >= jump_info_min_address (b
))
2731 && (jump_info_min_address (a
) <= jump_info_max_address (b
)));
2734 /* Merge two compatible jump info objects. */
2737 jump_info_merge (struct jump_info
**base
)
2739 struct jump_info
*a
;
2741 for (a
= *base
; a
; a
= a
->next
)
2743 struct jump_info
*b
;
2745 for (b
= a
->next
; b
; b
= b
->next
)
2747 /* Merge both jumps into one. */
2748 if (a
->end
== b
->end
)
2750 /* Reallocate addresses. */
2751 size_t needed_size
= a
->start
.count
+ b
->start
.count
;
2754 if (needed_size
> a
->start
.max_count
)
2756 a
->start
.max_count
+= b
->start
.max_count
;
2757 a
->start
.addresses
=
2758 xrealloc (a
->start
.addresses
,
2759 a
->start
.max_count
* sizeof (bfd_vma
*));
2762 /* Append start addresses. */
2763 for (i
= 0; i
< b
->start
.count
; ++i
)
2764 a
->start
.addresses
[a
->start
.count
++] =
2765 b
->start
.addresses
[i
];
2767 /* Remove and delete jump. */
2768 struct jump_info
*tmp
= b
->prev
;
2769 jump_info_unlink (b
, base
);
2777 /* Sort jumps by their size and starting point using a stable
2778 minsort. This could be improved if sorting performance is
2779 an issue, for example by using mergesort. */
2782 jump_info_sort (struct jump_info
**base
)
2784 struct jump_info
*current_element
= *base
;
2786 while (current_element
)
2788 struct jump_info
*best_match
= current_element
;
2789 struct jump_info
*runner
= current_element
->next
;
2790 bfd_vma best_size
= jump_info_size (best_match
);
2794 bfd_vma runner_size
= jump_info_size (runner
);
2796 if ((runner_size
< best_size
)
2797 || ((runner_size
== best_size
)
2798 && (jump_info_min_address (runner
)
2799 < jump_info_min_address (best_match
))))
2801 best_match
= runner
;
2802 best_size
= runner_size
;
2805 runner
= runner
->next
;
2808 if (best_match
== current_element
)
2809 current_element
= current_element
->next
;
2811 jump_info_move_linked (best_match
, current_element
, base
);
2815 /* Visualize all jumps at a given address. */
2818 jump_info_visualize_address (bfd_vma address
,
2821 uint8_t *color_buffer
)
2823 struct jump_info
*ji
= detected_jumps
;
2824 size_t len
= (max_level
+ 1) * 3;
2826 /* Clear line buffer. */
2827 memset (line_buffer
, ' ', len
);
2828 memset (color_buffer
, 0, len
);
2830 /* Iterate over jumps and add their ASCII art. */
2833 /* Discard jumps that are never needed again. */
2834 if (jump_info_max_address (ji
) < address
)
2836 struct jump_info
*tmp
= ji
;
2839 jump_info_unlink (tmp
, &detected_jumps
);
2840 jump_info_free (tmp
);
2844 /* This jump intersects with the current address. */
2845 if (jump_info_min_address (ji
) <= address
)
2847 /* Hash target address to get an even
2848 distribution between all values. */
2849 bfd_vma hash_address
= jump_info_end_address (ji
);
2850 uint8_t color
= iterative_hash_object (hash_address
, 0);
2851 /* Fetch line offset. */
2852 int offset
= (max_level
- ji
->level
) * 3;
2854 /* Draw start line. */
2855 if (jump_info_is_start_address (ji
, address
))
2857 size_t i
= offset
+ 1;
2859 for (; i
< len
- 1; ++i
)
2860 if (line_buffer
[i
] == ' ')
2862 line_buffer
[i
] = '-';
2863 color_buffer
[i
] = color
;
2866 if (line_buffer
[i
] == ' ')
2868 line_buffer
[i
] = '-';
2869 color_buffer
[i
] = color
;
2871 else if (line_buffer
[i
] == '>')
2873 line_buffer
[i
] = 'X';
2874 color_buffer
[i
] = color
;
2877 if (line_buffer
[offset
] == ' ')
2879 if (address
<= ji
->end
)
2880 line_buffer
[offset
] =
2881 (jump_info_min_address (ji
) == address
) ? '/': '+';
2883 line_buffer
[offset
] =
2884 (jump_info_max_address (ji
) == address
) ? '\\': '+';
2885 color_buffer
[offset
] = color
;
2888 /* Draw jump target. */
2889 else if (jump_info_is_end_address (ji
, address
))
2891 size_t i
= offset
+ 1;
2893 for (; i
< len
- 1; ++i
)
2894 if (line_buffer
[i
] == ' ')
2896 line_buffer
[i
] = '-';
2897 color_buffer
[i
] = color
;
2900 if (line_buffer
[i
] == ' ')
2902 line_buffer
[i
] = '>';
2903 color_buffer
[i
] = color
;
2905 else if (line_buffer
[i
] == '-')
2907 line_buffer
[i
] = 'X';
2908 color_buffer
[i
] = color
;
2911 if (line_buffer
[offset
] == ' ')
2913 if (jump_info_min_address (ji
) < address
)
2914 line_buffer
[offset
] =
2915 (jump_info_max_address (ji
) > address
) ? '>' : '\\';
2917 line_buffer
[offset
] = '/';
2918 color_buffer
[offset
] = color
;
2921 /* Draw intermediate line segment. */
2922 else if (line_buffer
[offset
] == ' ')
2924 line_buffer
[offset
] = '|';
2925 color_buffer
[offset
] = color
;
2933 /* Clone of disassemble_bytes to detect jumps inside a function. */
2934 /* FIXME: is this correct? Can we strip it down even further? */
2936 static struct jump_info
*
2937 disassemble_jumps (struct disassemble_info
* inf
,
2938 disassembler_ftype disassemble_fn
,
2939 bfd_vma start_offset
,
2940 bfd_vma stop_offset
,
2943 arelent
** relppend
)
2945 struct objdump_disasm_info
*aux
;
2946 struct jump_info
*jumps
= NULL
;
2948 bfd_vma addr_offset
;
2949 unsigned int opb
= inf
->octets_per_byte
;
2953 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
2954 section
= inf
->section
;
2957 sfile
.buffer
= (char *) xmalloc (sfile
.alloc
);
2960 inf
->insn_info_valid
= 0;
2961 disassemble_set_printf (inf
, &sfile
, (fprintf_ftype
) objdump_sprintf
,
2962 (fprintf_styled_ftype
) objdump_styled_sprintf
);
2964 addr_offset
= start_offset
;
2965 while (addr_offset
< stop_offset
)
2967 int previous_octets
;
2969 /* Remember the length of the previous instruction. */
2970 previous_octets
= octets
;
2974 inf
->bytes_per_line
= 0;
2975 inf
->bytes_per_chunk
= 0;
2976 inf
->flags
= ((disassemble_all
? DISASSEMBLE_DATA
: 0)
2977 | (wide_output
? WIDE_OUTPUT
: 0));
2979 inf
->flags
|= USER_SPECIFIED_MACHINE_TYPE
;
2981 if (inf
->disassembler_needs_relocs
2982 && (bfd_get_file_flags (aux
->abfd
) & EXEC_P
) == 0
2983 && (bfd_get_file_flags (aux
->abfd
) & DYNAMIC
) == 0
2984 && *relppp
< relppend
)
2986 bfd_signed_vma distance_to_rel
;
2988 distance_to_rel
= (**relppp
)->address
- (rel_offset
+ addr_offset
);
2990 /* Check to see if the current reloc is associated with
2991 the instruction that we are about to disassemble. */
2992 if (distance_to_rel
== 0
2993 /* FIXME: This is wrong. We are trying to catch
2994 relocs that are addressed part way through the
2995 current instruction, as might happen with a packed
2996 VLIW instruction. Unfortunately we do not know the
2997 length of the current instruction since we have not
2998 disassembled it yet. Instead we take a guess based
2999 upon the length of the previous instruction. The
3000 proper solution is to have a new target-specific
3001 disassembler function which just returns the length
3002 of an instruction at a given address without trying
3003 to display its disassembly. */
3004 || (distance_to_rel
> 0
3005 && distance_to_rel
< (bfd_signed_vma
) (previous_octets
/ opb
)))
3007 inf
->flags
|= INSN_HAS_RELOC
;
3011 if (! disassemble_all
3012 && (section
->flags
& (SEC_CODE
| SEC_HAS_CONTENTS
))
3013 == (SEC_CODE
| SEC_HAS_CONTENTS
))
3014 /* Set a stop_vma so that the disassembler will not read
3015 beyond the next symbol. We assume that symbols appear on
3016 the boundaries between instructions. We only do this when
3017 disassembling code of course, and when -D is in effect. */
3018 inf
->stop_vma
= section
->vma
+ stop_offset
;
3020 inf
->stop_offset
= stop_offset
;
3022 /* Extract jump information. */
3023 inf
->insn_info_valid
= 0;
3024 disassembler_in_comment
= false;
3025 octets
= (*disassemble_fn
) (section
->vma
+ addr_offset
, inf
);
3026 /* Test if a jump was detected. */
3027 if (inf
->insn_info_valid
3028 && ((inf
->insn_type
== dis_branch
)
3029 || (inf
->insn_type
== dis_condbranch
)
3030 || (inf
->insn_type
== dis_jsr
)
3031 || (inf
->insn_type
== dis_condjsr
))
3032 && (inf
->target
>= section
->vma
+ start_offset
)
3033 && (inf
->target
< section
->vma
+ stop_offset
))
3035 struct jump_info
*ji
=
3036 jump_info_new (section
->vma
+ addr_offset
, inf
->target
, -1);
3037 jump_info_add_front (ji
, &jumps
);
3042 addr_offset
+= octets
/ opb
;
3045 disassemble_set_printf (inf
, (void *) stdout
, (fprintf_ftype
) fprintf
,
3046 (fprintf_styled_ftype
) fprintf_styled
);
3047 free (sfile
.buffer
);
3050 jump_info_merge (&jumps
);
3051 /* Process jumps. */
3052 jump_info_sort (&jumps
);
3054 /* Group jumps by level. */
3055 struct jump_info
*last_jump
= jumps
;
3060 /* The last jump is part of the next group. */
3061 struct jump_info
*base
= last_jump
;
3062 /* Increment level. */
3063 base
->level
= ++max_level
;
3065 /* Find jumps that can be combined on the same
3066 level, with the largest jumps tested first.
3067 This has the advantage that large jumps are on
3068 lower levels and do not intersect with small
3069 jumps that get grouped on higher levels. */
3070 struct jump_info
*exchange_item
= last_jump
->next
;
3071 struct jump_info
*it
= exchange_item
;
3073 for (; it
; it
= it
->next
)
3075 /* Test if the jump intersects with any
3076 jump from current group. */
3078 struct jump_info
*it_collision
;
3080 for (it_collision
= base
;
3081 it_collision
!= exchange_item
;
3082 it_collision
= it_collision
->next
)
3084 /* This jump intersects so we leave it out. */
3085 if (jump_info_intersect (it_collision
, it
))
3092 /* Add jump to group. */
3095 /* Move current element to the front. */
3096 if (it
!= exchange_item
)
3098 struct jump_info
*save
= it
->prev
;
3099 jump_info_move_linked (it
, exchange_item
, &jumps
);
3105 last_jump
= exchange_item
;
3106 exchange_item
= exchange_item
->next
;
3108 last_jump
->level
= max_level
;
3112 /* Move to next group. */
3113 last_jump
= exchange_item
;
3119 /* The number of zeroes we want to see before we start skipping them.
3120 The number is arbitrarily chosen. */
3122 #define DEFAULT_SKIP_ZEROES 8
3124 /* The number of zeroes to skip at the end of a section. If the
3125 number of zeroes at the end is between SKIP_ZEROES_AT_END and
3126 SKIP_ZEROES, they will be disassembled. If there are fewer than
3127 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
3128 attempt to avoid disassembling zeroes inserted by section
3131 #define DEFAULT_SKIP_ZEROES_AT_END 3
3134 null_print (const void * stream ATTRIBUTE_UNUSED
, const char * format ATTRIBUTE_UNUSED
, ...)
3139 /* Like null_print, but takes the extra STYLE argument. As this is not
3140 going to print anything, the extra argument is just ignored. */
3143 null_styled_print (const void * stream ATTRIBUTE_UNUSED
,
3144 enum disassembler_style style ATTRIBUTE_UNUSED
,
3145 const char * format ATTRIBUTE_UNUSED
, ...)
3150 /* Print out jump visualization. */
3153 print_jump_visualisation (bfd_vma addr
, int max_level
, char *line_buffer
,
3154 uint8_t *color_buffer
)
3159 jump_info_visualize_address (addr
, max_level
, line_buffer
, color_buffer
);
3161 size_t line_buffer_size
= strlen (line_buffer
);
3162 char last_color
= 0;
3165 for (i
= 0; i
<= line_buffer_size
; ++i
)
3169 uint8_t color
= (i
< line_buffer_size
) ? color_buffer
[i
]: 0;
3171 if (color
!= last_color
)
3174 if (extended_color_output
)
3175 /* Use extended 8bit color, but
3176 do not choose dark colors. */
3177 printf ("\033[38;5;%dm", 124 + (color
% 108));
3179 /* Use simple terminal colors. */
3180 printf ("\033[%dm", 31 + (color
% 7));
3187 putchar ((i
< line_buffer_size
) ? line_buffer
[i
]: ' ');
3191 /* Disassemble some data in memory between given values. */
3194 disassemble_bytes (struct disassemble_info
*inf
,
3195 disassembler_ftype disassemble_fn
,
3198 bfd_vma start_offset
,
3199 bfd_vma stop_offset
,
3204 struct objdump_disasm_info
*aux
;
3206 unsigned int octets_per_line
;
3207 unsigned int skip_addr_chars
;
3208 bfd_vma addr_offset
;
3209 unsigned int opb
= inf
->octets_per_byte
;
3210 unsigned int skip_zeroes
= inf
->skip_zeroes
;
3211 unsigned int skip_zeroes_at_end
= inf
->skip_zeroes_at_end
;
3215 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
3216 section
= inf
->section
;
3219 sfile
.buffer
= (char *) xmalloc (sfile
.alloc
);
3223 octets_per_line
= insn_width
;
3225 octets_per_line
= 4;
3227 octets_per_line
= 16;
3229 /* Figure out how many characters to skip at the start of an
3230 address, to make the disassembly look nicer. We discard leading
3231 zeroes in chunks of 4, ensuring that there is always a leading
3233 skip_addr_chars
= 0;
3234 if (!no_addresses
&& !prefix_addresses
)
3238 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ section
->size
/ opb
);
3240 while (buf
[skip_addr_chars
] == '0')
3243 /* Don't discard zeros on overflow. */
3244 if (buf
[skip_addr_chars
] == '\0' && section
->vma
!= 0)
3245 skip_addr_chars
= 0;
3247 if (skip_addr_chars
!= 0)
3248 skip_addr_chars
= (skip_addr_chars
- 1) & -4;
3251 inf
->insn_info_valid
= 0;
3253 /* Determine maximum level. */
3254 uint8_t *color_buffer
= NULL
;
3255 char *line_buffer
= NULL
;
3258 /* Some jumps were detected. */
3261 struct jump_info
*ji
;
3263 /* Find maximum jump level. */
3264 for (ji
= detected_jumps
; ji
; ji
= ji
->next
)
3266 if (ji
->level
> max_level
)
3267 max_level
= ji
->level
;
3270 /* Allocate buffers. */
3271 size_t len
= (max_level
+ 1) * 3 + 1;
3272 line_buffer
= xmalloc (len
);
3273 line_buffer
[len
- 1] = 0;
3274 color_buffer
= xmalloc (len
);
3275 color_buffer
[len
- 1] = 0;
3278 addr_offset
= start_offset
;
3279 while (addr_offset
< stop_offset
)
3281 bool need_nl
= false;
3285 /* Make sure we don't use relocs from previous instructions. */
3288 /* If we see more than SKIP_ZEROES octets of zeroes, we just
3290 if (! disassemble_zeroes
)
3291 for (; addr_offset
* opb
+ octets
< stop_offset
* opb
; octets
++)
3292 if (data
[addr_offset
* opb
+ octets
] != 0)
3294 if (! disassemble_zeroes
3295 && (inf
->insn_info_valid
== 0
3296 || inf
->branch_delay_insns
== 0)
3297 && (octets
>= skip_zeroes
3298 || (addr_offset
* opb
+ octets
== stop_offset
* opb
3299 && octets
< skip_zeroes_at_end
)))
3301 /* If there are more nonzero octets to follow, we only skip
3302 zeroes in multiples of 4, to try to avoid running over
3303 the start of an instruction which happens to start with
3305 if (addr_offset
* opb
+ octets
!= stop_offset
* opb
)
3308 /* If we are going to display more data, and we are displaying
3309 file offsets, then tell the user how many zeroes we skip
3310 and the file offset from where we resume dumping. */
3311 if (display_file_offsets
3312 && addr_offset
+ octets
/ opb
< stop_offset
)
3313 printf (_("\t... (skipping %lu zeroes, "
3314 "resuming at file offset: 0x%lx)\n"),
3315 (unsigned long) (octets
/ opb
),
3316 (unsigned long) (section
->filepos
3317 + addr_offset
+ octets
/ opb
));
3324 unsigned int bpc
= 0;
3325 unsigned int pb
= 0;
3327 if (with_line_numbers
|| with_source_code
)
3328 show_line (aux
->abfd
, section
, addr_offset
);
3332 else if (!prefix_addresses
)
3336 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ addr_offset
);
3337 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
3341 printf ("%s:\t", buf
+ skip_addr_chars
);
3345 aux
->require_sec
= true;
3346 objdump_print_address (section
->vma
+ addr_offset
, inf
);
3347 aux
->require_sec
= false;
3351 print_jump_visualisation (section
->vma
+ addr_offset
,
3352 max_level
, line_buffer
,
3360 disassemble_set_printf
3361 (inf
, &sfile
, (fprintf_ftype
) objdump_sprintf
,
3362 (fprintf_styled_ftype
) objdump_styled_sprintf
);
3363 inf
->bytes_per_line
= 0;
3364 inf
->bytes_per_chunk
= 0;
3365 inf
->flags
= ((disassemble_all
? DISASSEMBLE_DATA
: 0)
3366 | (wide_output
? WIDE_OUTPUT
: 0));
3368 inf
->flags
|= USER_SPECIFIED_MACHINE_TYPE
;
3370 if (inf
->disassembler_needs_relocs
3371 && (bfd_get_file_flags (aux
->abfd
) & EXEC_P
) == 0
3372 && (bfd_get_file_flags (aux
->abfd
) & DYNAMIC
) == 0
3373 && *relppp
< relppend
)
3375 bfd_signed_vma distance_to_rel
;
3376 int max_reloc_offset
3377 = aux
->abfd
->arch_info
->max_reloc_offset_into_insn
;
3379 distance_to_rel
= ((**relppp
)->address
- rel_offset
3383 if (distance_to_rel
> 0
3384 && (max_reloc_offset
< 0
3385 || distance_to_rel
<= max_reloc_offset
))
3387 /* This reloc *might* apply to the current insn,
3388 starting somewhere inside it. Discover the length
3389 of the current insn so that the check below will
3392 insn_size
= insn_width
;
3395 /* We find the length by calling the dissassembler
3396 function with a dummy print handler. This should
3397 work unless the disassembler is not expecting to
3398 be called multiple times for the same address.
3400 This does mean disassembling the instruction
3401 twice, but we only do this when there is a high
3402 probability that there is a reloc that will
3403 affect the instruction. */
3404 disassemble_set_printf
3405 (inf
, inf
->stream
, (fprintf_ftype
) null_print
,
3406 (fprintf_styled_ftype
) null_styled_print
);
3407 insn_size
= disassemble_fn (section
->vma
3408 + addr_offset
, inf
);
3409 disassemble_set_printf
3411 (fprintf_ftype
) objdump_sprintf
,
3412 (fprintf_styled_ftype
) objdump_styled_sprintf
);
3416 /* Check to see if the current reloc is associated with
3417 the instruction that we are about to disassemble. */
3418 if (distance_to_rel
== 0
3419 || (distance_to_rel
> 0
3420 && distance_to_rel
< insn_size
/ (int) opb
))
3422 inf
->flags
|= INSN_HAS_RELOC
;
3423 aux
->reloc
= **relppp
;
3427 if (! disassemble_all
3428 && ((section
->flags
& (SEC_CODE
| SEC_HAS_CONTENTS
))
3429 == (SEC_CODE
| SEC_HAS_CONTENTS
)))
3430 /* Set a stop_vma so that the disassembler will not read
3431 beyond the next symbol. We assume that symbols appear on
3432 the boundaries between instructions. We only do this when
3433 disassembling code of course, and when -D is in effect. */
3434 inf
->stop_vma
= section
->vma
+ stop_offset
;
3436 inf
->stop_offset
= stop_offset
;
3437 disassembler_in_comment
= false;
3438 insn_size
= (*disassemble_fn
) (section
->vma
+ addr_offset
, inf
);
3442 disassemble_set_printf (inf
, stdout
, (fprintf_ftype
) fprintf
,
3443 (fprintf_styled_ftype
) fprintf_styled
);
3444 if (insn_width
== 0 && inf
->bytes_per_line
!= 0)
3445 octets_per_line
= inf
->bytes_per_line
;
3446 if (insn_size
< (int) opb
)
3449 printf ("%s\n", sfile
.buffer
);
3452 non_fatal (_("disassemble_fn returned length %d"),
3463 octets
= octets_per_line
;
3464 if (addr_offset
+ octets
/ opb
> stop_offset
)
3465 octets
= (stop_offset
- addr_offset
) * opb
;
3467 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ octets
; ++j
)
3469 if (ISPRINT (data
[j
]))
3470 buf
[j
- addr_offset
* opb
] = data
[j
];
3472 buf
[j
- addr_offset
* opb
] = '.';
3474 buf
[j
- addr_offset
* opb
] = '\0';
3477 if (prefix_addresses
3479 : show_raw_insn
>= 0)
3483 /* If ! prefix_addresses and ! wide_output, we print
3484 octets_per_line octets per line. */
3486 if (pb
> octets_per_line
&& ! prefix_addresses
&& ! wide_output
)
3487 pb
= octets_per_line
;
3489 if (inf
->bytes_per_chunk
)
3490 bpc
= inf
->bytes_per_chunk
;
3494 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
3496 /* PR 21580: Check for a buffer ending early. */
3497 if (j
+ bpc
<= stop_offset
* opb
)
3501 if (inf
->display_endian
== BFD_ENDIAN_LITTLE
)
3503 for (k
= bpc
; k
-- != 0; )
3504 printf ("%02x", (unsigned) data
[j
+ k
]);
3508 for (k
= 0; k
< bpc
; k
++)
3509 printf ("%02x", (unsigned) data
[j
+ k
]);
3515 for (; pb
< octets_per_line
; pb
+= bpc
)
3519 for (k
= 0; k
< bpc
; k
++)
3524 /* Separate raw data from instruction by extra space. */
3534 printf ("%s", sfile
.buffer
);
3536 if (prefix_addresses
3538 : show_raw_insn
>= 0)
3546 j
= addr_offset
* opb
+ pb
;
3552 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ j
/ opb
);
3553 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
3557 printf ("%s:\t", buf
+ skip_addr_chars
);
3560 print_jump_visualisation (section
->vma
+ j
/ opb
,
3561 max_level
, line_buffer
,
3564 pb
+= octets_per_line
;
3567 for (; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
3569 /* PR 21619: Check for a buffer ending early. */
3570 if (j
+ bpc
<= stop_offset
* opb
)
3574 if (inf
->display_endian
== BFD_ENDIAN_LITTLE
)
3576 for (k
= bpc
; k
-- != 0; )
3577 printf ("%02x", (unsigned) data
[j
+ k
]);
3581 for (k
= 0; k
< bpc
; k
++)
3582 printf ("%02x", (unsigned) data
[j
+ k
]);
3596 while ((*relppp
) < relppend
3597 && (**relppp
)->address
< rel_offset
+ addr_offset
+ octets
/ opb
)
3599 if (dump_reloc_info
|| dump_dynamic_reloc_info
)
3612 objdump_print_value (section
->vma
- rel_offset
+ q
->address
,
3617 if (q
->howto
== NULL
)
3618 printf ("*unknown*\t");
3619 else if (q
->howto
->name
)
3620 printf ("%s\t", q
->howto
->name
);
3622 printf ("%d\t", q
->howto
->type
);
3624 if (q
->sym_ptr_ptr
== NULL
|| *q
->sym_ptr_ptr
== NULL
)
3625 printf ("*unknown*");
3628 const char *sym_name
;
3630 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
3631 if (sym_name
!= NULL
&& *sym_name
!= '\0')
3632 objdump_print_symname (aux
->abfd
, inf
, *q
->sym_ptr_ptr
);
3637 sym_sec
= bfd_asymbol_section (*q
->sym_ptr_ptr
);
3638 sym_name
= bfd_section_name (sym_sec
);
3639 if (sym_name
== NULL
|| *sym_name
== '\0')
3640 sym_name
= "*unknown*";
3641 printf ("%s", sanitize_string (sym_name
));
3647 bfd_vma addend
= q
->addend
;
3648 if ((bfd_signed_vma
) addend
< 0)
3655 objdump_print_value (addend
, inf
, true);
3667 addr_offset
+= octets
/ opb
;
3670 free (sfile
.buffer
);
3672 free (color_buffer
);
3676 disassemble_section (bfd
*abfd
, asection
*section
, void *inf
)
3678 const struct elf_backend_data
*bed
;
3679 bfd_vma sign_adjust
= 0;
3680 struct disassemble_info
*pinfo
= (struct disassemble_info
*) inf
;
3681 struct objdump_disasm_info
*paux
;
3682 unsigned int opb
= pinfo
->octets_per_byte
;
3683 bfd_byte
*data
= NULL
;
3684 bfd_size_type datasize
= 0;
3685 arelent
**rel_pp
= NULL
;
3686 arelent
**rel_ppstart
= NULL
;
3687 arelent
**rel_ppend
;
3688 bfd_vma stop_offset
;
3689 asymbol
*sym
= NULL
;
3693 unsigned long addr_offset
;
3697 stop_offset_reached
,
3702 if (only_list
== NULL
)
3704 /* Sections that do not contain machine
3705 code are not normally disassembled. */
3706 if ((section
->flags
& SEC_HAS_CONTENTS
) == 0)
3709 if (! disassemble_all
3710 && (section
->flags
& SEC_CODE
) == 0)
3713 else if (!process_section_p (section
))
3716 datasize
= bfd_section_size (section
);
3720 if (start_address
== (bfd_vma
) -1
3721 || start_address
< section
->vma
)
3724 addr_offset
= start_address
- section
->vma
;
3726 if (stop_address
== (bfd_vma
) -1)
3727 stop_offset
= datasize
/ opb
;
3730 if (stop_address
< section
->vma
)
3733 stop_offset
= stop_address
- section
->vma
;
3734 if (stop_offset
> datasize
/ opb
)
3735 stop_offset
= datasize
/ opb
;
3738 if (addr_offset
>= stop_offset
)
3741 /* Decide which set of relocs to use. Load them if necessary. */
3742 paux
= (struct objdump_disasm_info
*) pinfo
->application_data
;
3743 if (pinfo
->dynrelbuf
&& dump_dynamic_reloc_info
)
3745 rel_pp
= pinfo
->dynrelbuf
;
3746 rel_count
= pinfo
->dynrelcount
;
3747 /* Dynamic reloc addresses are absolute, non-dynamic are section
3748 relative. REL_OFFSET specifies the reloc address corresponding
3749 to the start of this section. */
3750 rel_offset
= section
->vma
;
3758 if ((section
->flags
& SEC_RELOC
) != 0
3759 && (dump_reloc_info
|| pinfo
->disassembler_needs_relocs
))
3763 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
3765 my_bfd_nonfatal (bfd_get_filename (abfd
));
3769 rel_pp
= (arelent
**) xmalloc (relsize
);
3770 rel_count
= bfd_canonicalize_reloc (abfd
, section
, rel_pp
, syms
);
3773 my_bfd_nonfatal (bfd_get_filename (abfd
));
3778 else if (rel_count
> 1)
3779 /* Sort the relocs by address. */
3780 qsort (rel_pp
, rel_count
, sizeof (arelent
*), compare_relocs
);
3781 rel_ppstart
= rel_pp
;
3785 rel_ppend
= PTR_ADD (rel_pp
, rel_count
);
3787 if (!bfd_malloc_and_get_section (abfd
, section
, &data
))
3789 non_fatal (_("Reading section %s failed because: %s"),
3790 section
->name
, bfd_errmsg (bfd_get_error ()));
3794 pinfo
->buffer
= data
;
3795 pinfo
->buffer_vma
= section
->vma
;
3796 pinfo
->buffer_length
= datasize
;
3797 pinfo
->section
= section
;
3799 /* Sort the symbols into value and section order. */
3800 compare_section
= section
;
3801 if (sorted_symcount
> 1)
3802 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
3804 /* Skip over the relocs belonging to addresses below the
3806 while (rel_pp
< rel_ppend
3807 && (*rel_pp
)->address
< rel_offset
+ addr_offset
)
3810 printf (_("\nDisassembly of section %s:\n"), sanitize_string (section
->name
));
3812 /* Find the nearest symbol forwards from our current position. */
3813 paux
->require_sec
= true;
3814 sym
= (asymbol
*) find_symbol_for_address (section
->vma
+ addr_offset
,
3815 (struct disassemble_info
*) inf
,
3817 paux
->require_sec
= false;
3819 /* PR 9774: If the target used signed addresses then we must make
3820 sure that we sign extend the value that we calculate for 'addr'
3821 in the loop below. */
3822 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3823 && (bed
= get_elf_backend_data (abfd
)) != NULL
3824 && bed
->sign_extend_vma
)
3825 sign_adjust
= (bfd_vma
) 1 << (bed
->s
->arch_size
- 1);
3827 /* Disassemble a block of instructions up to the address associated with
3828 the symbol we have just found. Then print the symbol and find the
3829 next symbol on. Repeat until we have disassembled the entire section
3830 or we have reached the end of the address range we are interested in. */
3831 do_print
= paux
->symbol
== NULL
;
3832 loop_until
= stop_offset_reached
;
3834 while (addr_offset
< stop_offset
)
3838 bfd_vma nextstop_offset
;
3841 addr
= section
->vma
+ addr_offset
;
3842 addr
= ((addr
& ((sign_adjust
<< 1) - 1)) ^ sign_adjust
) - sign_adjust
;
3844 if (sym
!= NULL
&& bfd_asymbol_value (sym
) <= addr
)
3849 (x
< sorted_symcount
3850 && (bfd_asymbol_value (sorted_syms
[x
]) <= addr
));
3854 pinfo
->symbols
= sorted_syms
+ place
;
3855 pinfo
->num_symbols
= x
- place
;
3856 pinfo
->symtab_pos
= place
;
3860 pinfo
->symbols
= NULL
;
3861 pinfo
->num_symbols
= 0;
3862 pinfo
->symtab_pos
= -1;
3865 /* If we are only disassembling from a specific symbol,
3866 check to see if we should start or stop displaying. */
3867 if (sym
&& paux
->symbol
)
3871 /* See if we should stop printing. */
3875 if (sym
->flags
& BSF_FUNCTION
)
3879 case stop_offset_reached
:
3880 /* Handled by the while loop. */
3884 /* FIXME: There is an implicit assumption here
3885 that the name of sym is different from
3887 if (! bfd_is_local_label (abfd
, sym
))
3894 const char * name
= bfd_asymbol_name (sym
);
3895 char * alloc
= NULL
;
3897 if (do_demangle
&& name
[0] != '\0')
3899 /* Demangle the name. */
3900 alloc
= bfd_demangle (abfd
, name
, demangle_flags
);
3905 /* We are not currently printing. Check to see
3906 if the current symbol matches the requested symbol. */
3907 if (streq (name
, paux
->symbol
))
3911 /* Skip over the relocs belonging to addresses below the
3913 const bfd_vma sym_offset
= bfd_asymbol_value (sym
) - section
->vma
;
3914 while (rel_pp
< rel_ppend
&&
3915 (*rel_pp
)->address
- rel_offset
< sym_offset
)
3918 if (sym
->flags
& BSF_FUNCTION
)
3920 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3921 && ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
> 0)
3923 /* Sym is a function symbol with a size associated
3924 with it. Turn on automatic disassembly for the
3925 next VALUE bytes. */
3926 stop_offset
= addr_offset
3927 + ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
3928 loop_until
= stop_offset_reached
;
3932 /* Otherwise we need to tell the loop heuristic to
3933 loop until the next function symbol is encountered. */
3934 loop_until
= function_sym
;
3939 /* Otherwise loop until the next symbol is encountered. */
3940 loop_until
= next_sym
;
3948 if (! prefix_addresses
&& do_print
)
3950 pinfo
->fprintf_func (pinfo
->stream
, "\n");
3951 objdump_print_addr_with_sym (abfd
, section
, sym
, addr
,
3953 pinfo
->fprintf_func (pinfo
->stream
, ":\n");
3955 if (sym
!= NULL
&& show_all_symbols
)
3957 for (++place
; place
< sorted_symcount
; place
++)
3959 sym
= sorted_syms
[place
];
3961 if (bfd_asymbol_value (sym
) != addr
)
3963 if (! pinfo
->symbol_is_valid (sym
, pinfo
))
3965 if (strcmp (bfd_section_name (sym
->section
), bfd_section_name (section
)) != 0)
3968 objdump_print_addr_with_sym (abfd
, section
, sym
, addr
, pinfo
, false);
3969 pinfo
->fprintf_func (pinfo
->stream
, ":\n");
3974 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > addr
)
3976 else if (sym
== NULL
)
3980 #define is_valid_next_sym(SYM) \
3981 (strcmp (bfd_section_name ((SYM)->section), bfd_section_name (section)) == 0 \
3982 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
3983 && pinfo->symbol_is_valid (SYM, pinfo))
3985 /* Search forward for the next appropriate symbol in
3986 SECTION. Note that all the symbols are sorted
3987 together into one big array, and that some sections
3988 may have overlapping addresses. */
3989 while (place
< sorted_symcount
3990 && ! is_valid_next_sym (sorted_syms
[place
]))
3993 if (place
>= sorted_symcount
)
3996 nextsym
= sorted_syms
[place
];
3999 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > addr
)
4000 nextstop_offset
= bfd_asymbol_value (sym
) - section
->vma
;
4001 else if (nextsym
== NULL
)
4002 nextstop_offset
= stop_offset
;
4004 nextstop_offset
= bfd_asymbol_value (nextsym
) - section
->vma
;
4006 if (nextstop_offset
> stop_offset
4007 || nextstop_offset
<= addr_offset
)
4008 nextstop_offset
= stop_offset
;
4010 /* If a symbol is explicitly marked as being an object
4011 rather than a function, just dump the bytes without
4012 disassembling them. */
4015 || sym
->section
!= section
4016 || bfd_asymbol_value (sym
) > addr
4017 || ((sym
->flags
& BSF_OBJECT
) == 0
4018 && (strstr (bfd_asymbol_name (sym
), "gnu_compiled")
4020 && (strstr (bfd_asymbol_name (sym
), "gcc2_compiled")
4022 || (sym
->flags
& BSF_FUNCTION
) != 0)
4029 /* Resolve symbol name. */
4030 if (visualize_jumps
&& abfd
&& sym
&& sym
->name
)
4032 struct disassemble_info di
;
4035 sf
.alloc
= strlen (sym
->name
) + 40;
4036 sf
.buffer
= (char*) xmalloc (sf
.alloc
);
4038 disassemble_set_printf
4039 (&di
, &sf
, (fprintf_ftype
) objdump_sprintf
,
4040 (fprintf_styled_ftype
) objdump_styled_sprintf
);
4042 objdump_print_symname (abfd
, &di
, sym
);
4044 /* Fetch jump information. */
4045 detected_jumps
= disassemble_jumps
4046 (pinfo
, paux
->disassemble_fn
,
4047 addr_offset
, nextstop_offset
,
4048 rel_offset
, &rel_pp
, rel_ppend
);
4050 /* Free symbol name. */
4054 /* Add jumps to output. */
4055 disassemble_bytes (pinfo
, paux
->disassemble_fn
, insns
, data
,
4056 addr_offset
, nextstop_offset
,
4057 rel_offset
, &rel_pp
, rel_ppend
);
4060 while (detected_jumps
)
4062 detected_jumps
= jump_info_free (detected_jumps
);
4066 addr_offset
= nextstop_offset
;
4072 if (rel_ppstart
!= NULL
)
4076 /* Disassemble the contents of an object file. */
4079 disassemble_data (bfd
*abfd
)
4081 struct disassemble_info disasm_info
;
4082 struct objdump_disasm_info aux
;
4086 prev_functionname
= NULL
;
4088 prev_discriminator
= 0;
4090 /* We make a copy of syms to sort. We don't want to sort syms
4091 because that will screw up the relocs. */
4092 sorted_symcount
= symcount
? symcount
: dynsymcount
;
4093 sorted_syms
= (asymbol
**) xmalloc ((sorted_symcount
+ synthcount
)
4094 * sizeof (asymbol
*));
4095 if (sorted_symcount
!= 0)
4097 memcpy (sorted_syms
, symcount
? syms
: dynsyms
,
4098 sorted_symcount
* sizeof (asymbol
*));
4100 sorted_symcount
= remove_useless_symbols (sorted_syms
, sorted_symcount
);
4103 for (i
= 0; i
< synthcount
; ++i
)
4105 sorted_syms
[sorted_symcount
] = synthsyms
+ i
;
4109 init_disassemble_info (&disasm_info
, stdout
, (fprintf_ftype
) fprintf
,
4110 (fprintf_styled_ftype
) fprintf_styled
);
4111 disasm_info
.application_data
= (void *) &aux
;
4113 aux
.require_sec
= false;
4114 disasm_info
.dynrelbuf
= NULL
;
4115 disasm_info
.dynrelcount
= 0;
4117 aux
.symbol
= disasm_sym
;
4119 disasm_info
.print_address_func
= objdump_print_address
;
4120 disasm_info
.symbol_at_address_func
= objdump_symbol_at_address
;
4122 if (machine
!= NULL
)
4124 const bfd_arch_info_type
*inf
= bfd_scan_arch (machine
);
4128 non_fatal (_("can't use supplied machine %s"), machine
);
4132 abfd
->arch_info
= inf
;
4135 if (endian
!= BFD_ENDIAN_UNKNOWN
)
4137 struct bfd_target
*xvec
;
4139 xvec
= (struct bfd_target
*) xmalloc (sizeof (struct bfd_target
));
4140 memcpy (xvec
, abfd
->xvec
, sizeof (struct bfd_target
));
4141 xvec
->byteorder
= endian
;
4145 /* Use libopcodes to locate a suitable disassembler. */
4146 aux
.disassemble_fn
= disassembler (bfd_get_arch (abfd
),
4147 bfd_big_endian (abfd
),
4148 bfd_get_mach (abfd
), abfd
);
4149 if (!aux
.disassemble_fn
)
4151 non_fatal (_("can't disassemble for architecture %s\n"),
4152 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
4157 disasm_info
.flavour
= bfd_get_flavour (abfd
);
4158 disasm_info
.arch
= bfd_get_arch (abfd
);
4159 disasm_info
.mach
= bfd_get_mach (abfd
);
4160 disasm_info
.disassembler_options
= disassembler_options
;
4161 disasm_info
.octets_per_byte
= bfd_octets_per_byte (abfd
, NULL
);
4162 disasm_info
.skip_zeroes
= DEFAULT_SKIP_ZEROES
;
4163 disasm_info
.skip_zeroes_at_end
= DEFAULT_SKIP_ZEROES_AT_END
;
4164 disasm_info
.disassembler_needs_relocs
= false;
4166 if (bfd_big_endian (abfd
))
4167 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_BIG
;
4168 else if (bfd_little_endian (abfd
))
4169 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
4171 /* ??? Aborting here seems too drastic. We could default to big or little
4173 disasm_info
.endian
= BFD_ENDIAN_UNKNOWN
;
4175 disasm_info
.endian_code
= disasm_info
.endian
;
4177 /* Allow the target to customize the info structure. */
4178 disassemble_init_for_target (& disasm_info
);
4180 /* Pre-load the dynamic relocs as we may need them during the disassembly. */
4181 long relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
4185 disasm_info
.dynrelbuf
= (arelent
**) xmalloc (relsize
);
4186 disasm_info
.dynrelcount
4187 = bfd_canonicalize_dynamic_reloc (abfd
, disasm_info
.dynrelbuf
, dynsyms
);
4188 if (disasm_info
.dynrelcount
< 0)
4190 my_bfd_nonfatal (bfd_get_filename (abfd
));
4191 free (disasm_info
.dynrelbuf
);
4192 disasm_info
.dynrelbuf
= NULL
;
4193 disasm_info
.dynrelcount
= 0;
4195 else if (disasm_info
.dynrelcount
> 1)
4196 /* Sort the relocs by address. */
4197 qsort (disasm_info
.dynrelbuf
, disasm_info
.dynrelcount
,
4198 sizeof (arelent
*), compare_relocs
);
4201 disasm_info
.symtab
= sorted_syms
;
4202 disasm_info
.symtab_size
= sorted_symcount
;
4204 bfd_map_over_sections (abfd
, disassemble_section
, & disasm_info
);
4206 free (disasm_info
.dynrelbuf
);
4207 disasm_info
.dynrelbuf
= NULL
;
4209 disassemble_free_target (&disasm_info
);
4213 load_specific_debug_section (enum dwarf_section_display_enum debug
,
4214 asection
*sec
, void *file
)
4216 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
4217 bfd
*abfd
= (bfd
*) file
;
4223 if (section
->start
!= NULL
)
4225 /* If it is already loaded, do nothing. */
4226 if (streq (section
->filename
, bfd_get_filename (abfd
)))
4228 free (section
->start
);
4231 section
->filename
= bfd_get_filename (abfd
);
4232 section
->reloc_info
= NULL
;
4233 section
->num_relocs
= 0;
4234 section
->address
= bfd_section_vma (sec
);
4235 section
->size
= bfd_section_size (sec
);
4236 /* PR 24360: On 32-bit hosts sizeof (size_t) < sizeof (bfd_size_type). */
4237 alloced
= amt
= section
->size
+ 1;
4240 || (bfd_get_size (abfd
) != 0 && alloced
>= bfd_get_size (abfd
)))
4242 section
->start
= NULL
;
4243 free_debug_section (debug
);
4244 printf (_("\nSection '%s' has an invalid size: %#" PRIx64
".\n"),
4245 sanitize_string (section
->name
),
4250 section
->start
= contents
= xmalloc (alloced
);
4251 /* Ensure any string section has a terminating NUL. */
4252 section
->start
[section
->size
] = 0;
4254 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4255 && debug_displays
[debug
].relocate
)
4257 ret
= bfd_simple_get_relocated_section_contents (abfd
,
4263 long reloc_size
= bfd_get_reloc_upper_bound (abfd
, sec
);
4270 relocs
= (arelent
**) xmalloc (reloc_size
);
4272 reloc_count
= bfd_canonicalize_reloc (abfd
, sec
, relocs
, syms
);
4273 if (reloc_count
<= 0)
4277 section
->reloc_info
= relocs
;
4278 section
->num_relocs
= reloc_count
;
4284 ret
= bfd_get_full_section_contents (abfd
, sec
, &contents
);
4288 free_debug_section (debug
);
4289 printf (_("\nCan't get contents for section '%s'.\n"),
4290 sanitize_string (section
->name
));
4298 reloc_at (struct dwarf_section
* dsec
, uint64_t offset
)
4303 if (dsec
== NULL
|| dsec
->reloc_info
== NULL
)
4306 relocs
= (arelent
**) dsec
->reloc_info
;
4308 for (; (rp
= * relocs
) != NULL
; ++ relocs
)
4309 if (rp
->address
== offset
)
4316 load_debug_section (enum dwarf_section_display_enum debug
, void *file
)
4318 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
4319 bfd
*abfd
= (bfd
*) file
;
4323 if (!dump_any_debugging
)
4326 /* If it is already loaded, do nothing. */
4327 if (section
->start
!= NULL
)
4329 if (streq (section
->filename
, bfd_get_filename (abfd
)))
4332 /* Locate the debug section. */
4333 name
= section
->uncompressed_name
;
4334 sec
= bfd_get_section_by_name (abfd
, name
);
4337 name
= section
->compressed_name
;
4339 sec
= bfd_get_section_by_name (abfd
, name
);
4343 name
= section
->xcoff_name
;
4345 sec
= bfd_get_section_by_name (abfd
, name
);
4350 section
->name
= name
;
4351 return load_specific_debug_section (debug
, sec
, file
);
4355 free_debug_section (enum dwarf_section_display_enum debug
)
4357 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
4359 free ((char *) section
->start
);
4360 section
->start
= NULL
;
4361 section
->address
= 0;
4363 free ((char*) section
->reloc_info
);
4364 section
->reloc_info
= NULL
;
4365 section
->num_relocs
= 0;
4369 close_debug_file (void * file
)
4371 bfd
* abfd
= (bfd
*) file
;
4377 open_debug_file (const char * pathname
)
4381 data
= bfd_openr (pathname
, NULL
);
4385 if (! bfd_check_format (data
, bfd_object
))
4392 dump_dwarf_section (bfd
*abfd
, asection
*section
,
4395 const char *name
= bfd_section_name (section
);
4398 bool is_mainfile
= *(bool *) arg
;
4403 if (!is_mainfile
&& !process_links
4404 && (section
->flags
& SEC_DEBUGGING
) == 0)
4407 if (startswith (name
, ".gnu.linkonce.wi."))
4408 match
= ".debug_info";
4412 for (i
= 0; i
< max
; i
++)
4413 if ((strcmp (debug_displays
[i
].section
.uncompressed_name
, match
) == 0
4414 || strcmp (debug_displays
[i
].section
.compressed_name
, match
) == 0
4415 || strcmp (debug_displays
[i
].section
.xcoff_name
, match
) == 0)
4416 && debug_displays
[i
].enabled
!= NULL
4417 && *debug_displays
[i
].enabled
)
4419 struct dwarf_section
*sec
= &debug_displays
[i
].section
;
4421 if (strcmp (sec
->uncompressed_name
, match
) == 0)
4422 sec
->name
= sec
->uncompressed_name
;
4423 else if (strcmp (sec
->compressed_name
, match
) == 0)
4424 sec
->name
= sec
->compressed_name
;
4426 sec
->name
= sec
->xcoff_name
;
4427 if (load_specific_debug_section ((enum dwarf_section_display_enum
) i
,
4430 debug_displays
[i
].display (sec
, abfd
);
4432 if (i
!= info
&& i
!= abbrev
)
4433 free_debug_section ((enum dwarf_section_display_enum
) i
);
4439 /* Dump the dwarf debugging information. */
4442 dump_dwarf (bfd
*abfd
, bool is_mainfile
)
4444 /* The byte_get pointer should have been set at the start of dump_bfd(). */
4445 if (byte_get
== NULL
)
4447 warn (_("File %s does not contain any dwarf debug information\n"),
4448 bfd_get_filename (abfd
));
4452 switch (bfd_get_arch (abfd
))
4455 /* S12Z has a 24 bit address space. But the only known
4456 producer of dwarf_info encodes addresses into 32 bits. */
4461 eh_addr_size
= bfd_arch_bits_per_address (abfd
) / 8;
4465 init_dwarf_regnames_by_bfd_arch_and_mach (bfd_get_arch (abfd
),
4466 bfd_get_mach (abfd
));
4468 bfd_map_over_sections (abfd
, dump_dwarf_section
, (void *) &is_mainfile
);
4471 /* Read ABFD's section SECT_NAME into *CONTENTS, and return a pointer to
4472 the section. Return NULL on failure. */
4475 read_section (bfd
*abfd
, const char *sect_name
, bfd_byte
**contents
)
4480 sec
= bfd_get_section_by_name (abfd
, sect_name
);
4483 printf (_("No %s section present\n\n"), sanitize_string (sect_name
));
4487 if ((bfd_section_flags (sec
) & SEC_HAS_CONTENTS
) == 0)
4488 bfd_set_error (bfd_error_no_contents
);
4489 else if (bfd_malloc_and_get_section (abfd
, sec
, contents
))
4492 non_fatal (_("reading %s section of %s failed: %s"),
4493 sect_name
, bfd_get_filename (abfd
),
4494 bfd_errmsg (bfd_get_error ()));
4499 /* Stabs entries use a 12 byte format:
4500 4 byte string table index
4502 1 byte stab other field
4503 2 byte stab desc field
4505 FIXME: This will have to change for a 64 bit object format. */
4507 #define STRDXOFF (0)
4509 #define OTHEROFF (5)
4512 #define STABSIZE (12)
4514 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
4515 using string table section STRSECT_NAME (in `strtab'). */
4518 print_section_stabs (bfd
*abfd
,
4519 const char *stabsect_name
,
4520 unsigned *string_offset_ptr
)
4523 unsigned file_string_table_offset
= 0;
4524 unsigned next_file_string_table_offset
= *string_offset_ptr
;
4525 bfd_byte
*stabp
, *stabs_end
;
4528 stabs_end
= PTR_ADD (stabp
, stab_size
);
4530 printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name
));
4531 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
4533 /* Loop through all symbols and print them.
4535 We start the index at -1 because there is a dummy symbol on
4536 the front of stabs-in-{coff,elf} sections that supplies sizes. */
4537 for (i
= -1; (size_t) (stabs_end
- stabp
) >= STABSIZE
; stabp
+= STABSIZE
, i
++)
4541 unsigned char type
, other
;
4542 unsigned short desc
;
4545 strx
= bfd_h_get_32 (abfd
, stabp
+ STRDXOFF
);
4546 type
= bfd_h_get_8 (abfd
, stabp
+ TYPEOFF
);
4547 other
= bfd_h_get_8 (abfd
, stabp
+ OTHEROFF
);
4548 desc
= bfd_h_get_16 (abfd
, stabp
+ DESCOFF
);
4549 value
= bfd_h_get_32 (abfd
, stabp
+ VALOFF
);
4551 printf ("\n%-6d ", i
);
4552 /* Either print the stab name, or, if unnamed, print its number
4553 again (makes consistent formatting for tools like awk). */
4554 name
= bfd_get_stab_name (type
);
4556 printf ("%-6s", sanitize_string (name
));
4557 else if (type
== N_UNDF
)
4560 printf ("%-6d", type
);
4561 printf (" %-6d %-6d ", other
, desc
);
4562 bfd_printf_vma (abfd
, value
);
4563 printf (" %-6lu", strx
);
4565 /* Symbols with type == 0 (N_UNDF) specify the length of the
4566 string table associated with this file. We use that info
4567 to know how to relocate the *next* file's string table indices. */
4570 file_string_table_offset
= next_file_string_table_offset
;
4571 next_file_string_table_offset
+= value
;
4575 bfd_size_type amt
= strx
+ file_string_table_offset
;
4577 /* Using the (possibly updated) string table offset, print the
4578 string (if any) associated with this symbol. */
4579 if (amt
< stabstr_size
)
4580 /* PR 17512: file: 079-79389-0.001:0.1.
4581 FIXME: May need to sanitize this string before displaying. */
4582 printf (" %.*s", (int)(stabstr_size
- amt
), strtab
+ amt
);
4588 *string_offset_ptr
= next_file_string_table_offset
;
4593 const char * section_name
;
4594 const char * string_section_name
;
4595 unsigned string_offset
;
4600 find_stabs_section (bfd
*abfd
, asection
*section
, void *names
)
4603 stab_section_names
* sought
= (stab_section_names
*) names
;
4605 /* Check for section names for which stabsect_name is a prefix, to
4606 handle .stab.N, etc. */
4607 len
= strlen (sought
->section_name
);
4609 /* If the prefix matches, and the files section name ends with a
4610 nul or a digit, then we match. I.e., we want either an exact
4611 match or a section followed by a number. */
4612 if (strncmp (sought
->section_name
, section
->name
, len
) == 0
4613 && (section
->name
[len
] == 0
4614 || (section
->name
[len
] == '.' && ISDIGIT (section
->name
[len
+ 1]))))
4619 s
= read_section (abfd
, sought
->string_section_name
, &strtab
);
4621 stabstr_size
= bfd_section_size (s
);
4626 s
= read_section (abfd
, section
->name
, &stabs
);
4629 stab_size
= bfd_section_size (s
);
4630 print_section_stabs (abfd
, section
->name
, &sought
->string_offset
);
4638 dump_stabs_section (bfd
*abfd
, char *stabsect_name
, char *strsect_name
)
4640 stab_section_names s
;
4642 s
.section_name
= stabsect_name
;
4643 s
.string_section_name
= strsect_name
;
4644 s
.string_offset
= 0;
4646 bfd_map_over_sections (abfd
, find_stabs_section
, & s
);
4652 /* Dump the any sections containing stabs debugging information. */
4655 dump_stabs (bfd
*abfd
)
4657 dump_stabs_section (abfd
, ".stab", ".stabstr");
4658 dump_stabs_section (abfd
, ".stab.excl", ".stab.exclstr");
4659 dump_stabs_section (abfd
, ".stab.index", ".stab.indexstr");
4662 dump_stabs_section (abfd
, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
4664 dump_stabs_section (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
4668 dump_bfd_header (bfd
*abfd
)
4672 printf (_("architecture: %s, "),
4673 bfd_printable_arch_mach (bfd_get_arch (abfd
),
4674 bfd_get_mach (abfd
)));
4675 printf (_("flags 0x%08x:\n"), abfd
->flags
& ~BFD_FLAGS_FOR_BFD_USE_MASK
);
4677 #define PF(x, y) if (abfd->flags & x) {printf ("%s%s", comma, y); comma=", ";}
4678 PF (HAS_RELOC
, "HAS_RELOC");
4679 PF (EXEC_P
, "EXEC_P");
4680 PF (HAS_LINENO
, "HAS_LINENO");
4681 PF (HAS_DEBUG
, "HAS_DEBUG");
4682 PF (HAS_SYMS
, "HAS_SYMS");
4683 PF (HAS_LOCALS
, "HAS_LOCALS");
4684 PF (DYNAMIC
, "DYNAMIC");
4685 PF (WP_TEXT
, "WP_TEXT");
4686 PF (D_PAGED
, "D_PAGED");
4687 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
4688 printf (_("\nstart address 0x"));
4689 bfd_printf_vma (abfd
, abfd
->start_address
);
4694 #ifdef ENABLE_LIBCTF
4695 /* Formatting callback function passed to ctf_dump. Returns either the pointer
4696 it is passed, or a pointer to newly-allocated storage, in which case
4697 dump_ctf() will free it when it no longer needs it. */
4700 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED
,
4703 const char *blanks
= arg
;
4706 if (asprintf (&new_s
, "%s%s", blanks
, s
) < 0)
4711 /* Make a ctfsect suitable for ctf_bfdopen_ctfsect(). */
4713 make_ctfsect (const char *name
, bfd_byte
*data
,
4718 ctfsect
.cts_name
= name
;
4719 ctfsect
.cts_entsize
= 1;
4720 ctfsect
.cts_size
= size
;
4721 ctfsect
.cts_data
= data
;
4726 /* Dump CTF errors/warnings. */
4728 dump_ctf_errs (ctf_dict_t
*fp
)
4730 ctf_next_t
*it
= NULL
;
4735 /* Dump accumulated errors and warnings. */
4736 while ((errtext
= ctf_errwarning_next (fp
, &it
, &is_warning
, &err
)) != NULL
)
4738 non_fatal (_("%s: %s"), is_warning
? _("warning"): _("error"),
4742 if (err
!= ECTF_NEXT_END
)
4744 non_fatal (_("CTF error: cannot get CTF errors: `%s'"),
4749 /* Dump one CTF archive member. */
4752 dump_ctf_archive_member (ctf_dict_t
*ctf
, const char *name
, ctf_dict_t
*parent
,
4755 const char *things
[] = {"Header", "Labels", "Data objects",
4756 "Function objects", "Variables", "Types", "Strings",
4761 /* Don't print out the name of the default-named archive member if it appears
4762 first in the list. The name .ctf appears everywhere, even for things that
4763 aren't really archives, so printing it out is liable to be confusing; also,
4764 the common case by far is for only one archive member to exist, and hiding
4765 it in that case seems worthwhile. */
4767 if (strcmp (name
, ".ctf") != 0 || member
!= 0)
4768 printf (_("\nCTF archive member: %s:\n"), sanitize_string (name
));
4770 if (ctf_parent_name (ctf
) != NULL
)
4771 ctf_import (ctf
, parent
);
4773 for (i
= 0, thing
= things
; *thing
[0]; thing
++, i
++)
4775 ctf_dump_state_t
*s
= NULL
;
4778 printf ("\n %s:\n", *thing
);
4779 while ((item
= ctf_dump (ctf
, &s
, i
, dump_ctf_indent_lines
,
4780 (void *) " ")) != NULL
)
4782 printf ("%s\n", item
);
4786 if (ctf_errno (ctf
))
4788 non_fatal (_("Iteration failed: %s, %s"), *thing
,
4789 ctf_errmsg (ctf_errno (ctf
)));
4794 dump_ctf_errs (ctf
);
4797 /* Dump the CTF debugging information. */
4800 dump_ctf (bfd
*abfd
, const char *sect_name
, const char *parent_name
)
4803 ctf_archive_t
*ctfa
= NULL
;
4808 ctf_next_t
*i
= NULL
;
4813 if (sect_name
== NULL
)
4816 sec
= read_section (abfd
, sect_name
, &ctfdata
);
4819 my_bfd_nonfatal (bfd_get_filename (abfd
));
4823 /* Load the CTF file and dump it. Preload the parent dict, since it will
4824 need to be imported into every child in turn. */
4826 ctfsect
= make_ctfsect (sect_name
, ctfdata
, bfd_section_size (sec
));
4827 if ((ctfa
= ctf_bfdopen_ctfsect (abfd
, &ctfsect
, &err
)) == NULL
)
4829 dump_ctf_errs (NULL
);
4830 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err
));
4831 my_bfd_nonfatal (bfd_get_filename (abfd
));
4836 if ((parent
= ctf_dict_open (ctfa
, parent_name
, &err
)) == NULL
)
4838 dump_ctf_errs (NULL
);
4839 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err
));
4840 my_bfd_nonfatal (bfd_get_filename (abfd
));
4846 printf (_("Contents of CTF section %s:\n"), sanitize_string (sect_name
));
4848 while ((fp
= ctf_archive_next (ctfa
, &i
, &name
, 0, &err
)) != NULL
)
4849 dump_ctf_archive_member (fp
, name
, parent
, member
++);
4850 if (err
!= ECTF_NEXT_END
)
4852 dump_ctf_errs (NULL
);
4853 non_fatal (_("CTF archive member open failure: %s"), ctf_errmsg (err
));
4854 my_bfd_nonfatal (bfd_get_filename (abfd
));
4856 ctf_dict_close (parent
);
4862 dump_ctf (bfd
*abfd ATTRIBUTE_UNUSED
, const char *sect_name ATTRIBUTE_UNUSED
,
4863 const char *parent_name ATTRIBUTE_UNUSED
) {}
4867 dump_section_sframe (bfd
*abfd ATTRIBUTE_UNUSED
,
4868 const char * sect_name
)
4871 sframe_decoder_ctx
*sfd_ctx
= NULL
;
4872 bfd_size_type sf_size
;
4873 bfd_byte
*sframe_data
;
4877 if (sect_name
== NULL
)
4878 sect_name
= ".sframe";
4880 sec
= read_section (abfd
, sect_name
, &sframe_data
);
4883 my_bfd_nonfatal (bfd_get_filename (abfd
));
4886 sf_size
= bfd_section_size (sec
);
4887 sf_vma
= bfd_section_vma (sec
);
4889 /* Decode the contents of the section. */
4890 sfd_ctx
= sframe_decode ((const char*)sframe_data
, sf_size
, &err
);
4893 my_bfd_nonfatal (bfd_get_filename (abfd
));
4898 printf (_("Contents of the SFrame section %s:"),
4899 sanitize_string (sect_name
));
4900 /* Dump the contents as text. */
4901 dump_sframe (sfd_ctx
, sf_vma
);
4903 sframe_decoder_free (&sfd_ctx
);
4909 dump_bfd_private_header (bfd
*abfd
)
4911 if (!bfd_print_private_bfd_data (abfd
, stdout
))
4912 non_fatal (_("warning: private headers incomplete: %s"),
4913 bfd_errmsg (bfd_get_error ()));
4917 dump_target_specific (bfd
*abfd
)
4919 const struct objdump_private_desc
* const *desc
;
4920 struct objdump_private_option
*opt
;
4923 /* Find the desc. */
4924 for (desc
= objdump_private_vectors
; *desc
!= NULL
; desc
++)
4925 if ((*desc
)->filter (abfd
))
4930 non_fatal (_("option -P/--private not supported by this file"));
4934 /* Clear all options. */
4935 for (opt
= (*desc
)->options
; opt
->name
; opt
++)
4936 opt
->selected
= false;
4938 /* Decode options. */
4939 b
= dump_private_options
;
4942 e
= strchr (b
, ',');
4947 for (opt
= (*desc
)->options
; opt
->name
; opt
++)
4948 if (strcmp (opt
->name
, b
) == 0)
4950 opt
->selected
= true;
4953 if (opt
->name
== NULL
)
4954 non_fatal (_("target specific dump '%s' not supported"), b
);
4965 (*desc
)->dump (abfd
);
4968 /* Display a section in hexadecimal format with associated characters.
4969 Each line prefixed by the zero padded address. */
4972 dump_section (bfd
*abfd
, asection
*section
, void *dummy ATTRIBUTE_UNUSED
)
4974 bfd_byte
*data
= NULL
;
4975 bfd_size_type datasize
;
4976 bfd_vma addr_offset
;
4977 bfd_vma start_offset
;
4978 bfd_vma stop_offset
;
4979 unsigned int opb
= bfd_octets_per_byte (abfd
, section
);
4980 /* Bytes per line. */
4981 const int onaline
= 16;
4986 if (only_list
== NULL
)
4988 if ((section
->flags
& SEC_HAS_CONTENTS
) == 0)
4991 else if (!process_section_p (section
))
4994 if ((datasize
= bfd_section_size (section
)) == 0)
4997 /* Compute the address range to display. */
4998 if (start_address
== (bfd_vma
) -1
4999 || start_address
< section
->vma
)
5002 start_offset
= start_address
- section
->vma
;
5004 if (stop_address
== (bfd_vma
) -1)
5005 stop_offset
= datasize
/ opb
;
5008 if (stop_address
< section
->vma
)
5011 stop_offset
= stop_address
- section
->vma
;
5013 if (stop_offset
> datasize
/ opb
)
5014 stop_offset
= datasize
/ opb
;
5017 if (start_offset
>= stop_offset
)
5020 printf (_("Contents of section %s:"), sanitize_string (section
->name
));
5021 if (display_file_offsets
)
5022 printf (_(" (Starting at file offset: 0x%lx)"),
5023 (unsigned long) (section
->filepos
+ start_offset
));
5026 if (!bfd_get_full_section_contents (abfd
, section
, &data
))
5028 non_fatal (_("Reading section %s failed because: %s"),
5029 section
->name
, bfd_errmsg (bfd_get_error ()));
5035 bfd_sprintf_vma (abfd
, buf
, start_offset
+ section
->vma
);
5036 if (strlen (buf
) >= sizeof (buf
))
5040 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
5042 count
= strlen (buf
) - count
;
5046 bfd_sprintf_vma (abfd
, buf
, stop_offset
+ section
->vma
- 1);
5047 if (strlen (buf
) >= sizeof (buf
))
5051 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
5053 count
= strlen (buf
) - count
;
5057 for (addr_offset
= start_offset
;
5058 addr_offset
< stop_offset
; addr_offset
+= onaline
/ opb
)
5062 bfd_sprintf_vma (abfd
, buf
, (addr_offset
+ section
->vma
));
5063 count
= strlen (buf
);
5064 if ((size_t) count
>= sizeof (buf
))
5068 while (count
< width
)
5073 fputs (buf
+ count
- width
, stdout
);
5076 for (j
= addr_offset
* opb
;
5077 j
< addr_offset
* opb
+ onaline
; j
++)
5079 if (j
< stop_offset
* opb
)
5080 printf ("%02x", (unsigned) (data
[j
]));
5088 for (j
= addr_offset
* opb
;
5089 j
< addr_offset
* opb
+ onaline
; j
++)
5091 if (j
>= stop_offset
* opb
)
5094 printf ("%c", ISPRINT (data
[j
]) ? data
[j
] : '.');
5101 /* Actually display the various requested regions. */
5104 dump_data (bfd
*abfd
)
5106 bfd_map_over_sections (abfd
, dump_section
, NULL
);
5109 /* Should perhaps share code and display with nm? */
5112 dump_symbols (bfd
*abfd ATTRIBUTE_UNUSED
, bool dynamic
)
5121 max_count
= dynsymcount
;
5122 printf ("DYNAMIC SYMBOL TABLE:\n");
5127 max_count
= symcount
;
5128 printf ("SYMBOL TABLE:\n");
5132 printf (_("no symbols\n"));
5134 for (count
= 0; count
< max_count
; count
++)
5138 if (*current
== NULL
)
5139 printf (_("no information for symbol number %ld\n"), count
);
5141 else if ((cur_bfd
= bfd_asymbol_bfd (*current
)) == NULL
)
5142 printf (_("could not determine the type of symbol number %ld\n"),
5145 else if (process_section_p ((* current
)->section
)
5146 && (dump_special_syms
5147 || !bfd_is_target_special_symbol (cur_bfd
, *current
)))
5149 const char *name
= (*current
)->name
;
5151 if (do_demangle
&& name
!= NULL
&& *name
!= '\0')
5155 /* If we want to demangle the name, we demangle it
5156 here, and temporarily clobber it while calling
5157 bfd_print_symbol. FIXME: This is a gross hack. */
5158 alloc
= bfd_demangle (cur_bfd
, name
, demangle_flags
);
5160 (*current
)->name
= alloc
;
5161 bfd_print_symbol (cur_bfd
, stdout
, *current
,
5162 bfd_print_symbol_all
);
5165 (*current
)->name
= name
;
5169 else if (unicode_display
!= unicode_default
5170 && name
!= NULL
&& *name
!= '\0')
5172 const char * sanitized_name
;
5174 /* If we want to sanitize the name, we do it here, and
5175 temporarily clobber it while calling bfd_print_symbol.
5176 FIXME: This is a gross hack. */
5177 sanitized_name
= sanitize_string (name
);
5178 if (sanitized_name
!= name
)
5179 (*current
)->name
= sanitized_name
;
5181 sanitized_name
= NULL
;
5182 bfd_print_symbol (cur_bfd
, stdout
, *current
,
5183 bfd_print_symbol_all
);
5184 if (sanitized_name
!= NULL
)
5185 (*current
)->name
= name
;
5188 bfd_print_symbol (cur_bfd
, stdout
, *current
,
5189 bfd_print_symbol_all
);
5199 dump_reloc_set (bfd
*abfd
, asection
*sec
, arelent
**relpp
, long relcount
)
5202 char *last_filename
, *last_functionname
;
5203 unsigned int last_line
;
5204 unsigned int last_discriminator
;
5206 /* Get column headers lined up reasonably. */
5214 bfd_sprintf_vma (abfd
, buf
, (bfd_vma
) -1);
5215 width
= strlen (buf
) - 7;
5217 printf ("OFFSET %*s TYPE %*s VALUE\n", width
, "", 12, "");
5220 last_filename
= NULL
;
5221 last_functionname
= NULL
;
5223 last_discriminator
= 0;
5225 for (p
= relpp
; relcount
&& *p
!= NULL
; p
++, relcount
--)
5228 const char *filename
, *functionname
;
5229 unsigned int linenumber
;
5230 unsigned int discriminator
;
5231 const char *sym_name
;
5232 const char *section_name
;
5233 bfd_vma addend2
= 0;
5235 if (start_address
!= (bfd_vma
) -1
5236 && q
->address
< start_address
)
5238 if (stop_address
!= (bfd_vma
) -1
5239 && q
->address
> stop_address
)
5242 if (with_line_numbers
5244 && bfd_find_nearest_line_discriminator (abfd
, sec
, syms
, q
->address
,
5245 &filename
, &functionname
,
5246 &linenumber
, &discriminator
))
5248 if (functionname
!= NULL
5249 && (last_functionname
== NULL
5250 || strcmp (functionname
, last_functionname
) != 0))
5252 printf ("%s():\n", sanitize_string (functionname
));
5253 if (last_functionname
!= NULL
)
5254 free (last_functionname
);
5255 last_functionname
= xstrdup (functionname
);
5259 && (linenumber
!= last_line
5260 || (filename
!= NULL
5261 && last_filename
!= NULL
5262 && filename_cmp (filename
, last_filename
) != 0)
5263 || (discriminator
!= last_discriminator
)))
5265 if (discriminator
> 0)
5266 printf ("%s:%u\n", filename
== NULL
? "???" :
5267 sanitize_string (filename
), linenumber
);
5269 printf ("%s:%u (discriminator %u)\n",
5270 filename
== NULL
? "???" : sanitize_string (filename
),
5271 linenumber
, discriminator
);
5272 last_line
= linenumber
;
5273 last_discriminator
= discriminator
;
5274 if (last_filename
!= NULL
)
5275 free (last_filename
);
5276 if (filename
== NULL
)
5277 last_filename
= NULL
;
5279 last_filename
= xstrdup (filename
);
5283 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
5285 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
5286 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
5291 section_name
= NULL
;
5294 bfd_printf_vma (abfd
, q
->address
);
5295 if (q
->howto
== NULL
)
5296 printf (" *unknown* ");
5297 else if (q
->howto
->name
)
5299 const char *name
= q
->howto
->name
;
5301 /* R_SPARC_OLO10 relocations contain two addends.
5302 But because 'arelent' lacks enough storage to
5303 store them both, the 64-bit ELF Sparc backend
5304 records this as two relocations. One R_SPARC_LO10
5305 and one R_SPARC_13, both pointing to the same
5306 address. This is merely so that we have some
5307 place to store both addend fields.
5309 Undo this transformation, otherwise the output
5310 will be confusing. */
5311 if (abfd
->xvec
->flavour
== bfd_target_elf_flavour
5312 && elf_tdata (abfd
)->elf_header
->e_machine
== EM_SPARCV9
5314 && !strcmp (q
->howto
->name
, "R_SPARC_LO10"))
5316 arelent
*q2
= *(p
+ 1);
5319 && q
->address
== q2
->address
5320 && !strcmp (q2
->howto
->name
, "R_SPARC_13"))
5322 name
= "R_SPARC_OLO10";
5323 addend2
= q2
->addend
;
5327 printf (" %-16s ", name
);
5330 printf (" %-16d ", q
->howto
->type
);
5334 objdump_print_symname (abfd
, NULL
, *q
->sym_ptr_ptr
);
5338 if (section_name
== NULL
)
5339 section_name
= "*unknown*";
5340 printf ("[%s]", sanitize_string (section_name
));
5345 bfd_signed_vma addend
= q
->addend
;
5353 bfd_printf_vma (abfd
, addend
);
5358 bfd_printf_vma (abfd
, addend2
);
5364 if (last_filename
!= NULL
)
5365 free (last_filename
);
5366 if (last_functionname
!= NULL
)
5367 free (last_functionname
);
5371 dump_relocs_in_section (bfd
*abfd
,
5373 void *dummy ATTRIBUTE_UNUSED
)
5379 if ( bfd_is_abs_section (section
)
5380 || bfd_is_und_section (section
)
5381 || bfd_is_com_section (section
)
5382 || (! process_section_p (section
))
5383 || ((section
->flags
& SEC_RELOC
) == 0))
5386 printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section
->name
));
5388 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
5391 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 my_bfd_nonfatal (_("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 printf ("DYNAMIC RELOCATION RECORDS");
5443 printf (" (none)\n\n");
5454 relpp
= (arelent
**) xmalloc (relsize
);
5455 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
5461 non_fatal (_("failed to read relocs in: %s"),
5462 sanitize_string (bfd_get_filename (abfd
)));
5463 my_bfd_nonfatal (_("error message was"));
5465 else if (relcount
== 0)
5466 printf (" (none)\n\n");
5470 dump_reloc_set (abfd
, NULL
, relpp
, relcount
);
5476 /* Creates a table of paths, to search for source files. */
5479 add_include_path (const char *path
)
5483 include_path_count
++;
5484 include_paths
= (const char **)
5485 xrealloc (include_paths
, include_path_count
* sizeof (*include_paths
));
5486 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5487 if (path
[1] == ':' && path
[2] == 0)
5488 path
= concat (path
, ".", (const char *) 0);
5490 include_paths
[include_path_count
- 1] = path
;
5494 adjust_addresses (bfd
*abfd ATTRIBUTE_UNUSED
,
5498 if ((section
->flags
& SEC_DEBUGGING
) == 0)
5500 bool *has_reloc_p
= (bool *) arg
;
5501 section
->vma
+= adjust_section_vma
;
5503 section
->lma
+= adjust_section_vma
;
5507 /* Return the sign-extended form of an ARCH_SIZE sized VMA. */
5510 sign_extend_address (bfd
*abfd ATTRIBUTE_UNUSED
,
5515 mask
= (bfd_vma
) 1 << (arch_size
- 1);
5516 return (((vma
& ((mask
<< 1) - 1)) ^ mask
) - mask
);
5520 might_need_separate_debug_info (bool is_mainfile
)
5522 /* We do not follow links from debug info files. */
5526 /* Since do_follow_links might be enabled by default, only treat it as an
5527 indication that separate files should be loaded if setting it was a
5528 deliberate user action. */
5529 if (DEFAULT_FOR_FOLLOW_LINKS
== 0 && do_follow_links
)
5532 if (process_links
|| dump_symtab
|| dump_debugging
5533 || dump_dwarf_section_info
|| with_source_code
)
5539 /* Dump selected contents of ABFD. */
5542 dump_bfd (bfd
*abfd
, bool is_mainfile
)
5544 const struct elf_backend_data
* bed
;
5546 if (bfd_big_endian (abfd
))
5547 byte_get
= byte_get_big_endian
;
5548 else if (bfd_little_endian (abfd
))
5549 byte_get
= byte_get_little_endian
;
5553 /* Load any separate debug information files. */
5554 if (byte_get
!= NULL
&& might_need_separate_debug_info (is_mainfile
))
5556 load_separate_debug_files (abfd
, bfd_get_filename (abfd
));
5558 /* If asked to do so, recursively dump the separate files. */
5559 if (do_follow_links
)
5563 for (i
= first_separate_info
; i
!= NULL
; i
= i
->next
)
5564 dump_bfd (i
->handle
, false);
5568 /* Adjust user-specified start and stop limits for targets that use
5569 signed addresses. */
5570 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
5571 && (bed
= get_elf_backend_data (abfd
)) != NULL
5572 && bed
->sign_extend_vma
)
5574 start_address
= sign_extend_address (abfd
, start_address
,
5576 stop_address
= sign_extend_address (abfd
, stop_address
,
5580 /* If we are adjusting section VMA's, change them all now. Changing
5581 the BFD information is a hack. However, we must do it, or
5582 bfd_find_nearest_line will not do the right thing. */
5583 if (adjust_section_vma
!= 0)
5585 bool has_reloc
= (abfd
->flags
& HAS_RELOC
);
5586 bfd_map_over_sections (abfd
, adjust_addresses
, &has_reloc
);
5589 if (is_mainfile
|| process_links
)
5591 if (! dump_debugging_tags
&& ! suppress_bfd_header
)
5592 printf (_("\n%s: file format %s\n"),
5593 sanitize_string (bfd_get_filename (abfd
)),
5596 print_arelt_descr (stdout
, abfd
, true, false);
5597 if (dump_file_header
)
5598 dump_bfd_header (abfd
);
5599 if (dump_private_headers
)
5600 dump_bfd_private_header (abfd
);
5601 if (dump_private_options
!= NULL
)
5602 dump_target_specific (abfd
);
5603 if (! dump_debugging_tags
&& ! suppress_bfd_header
)
5611 || dump_dwarf_section_info
)
5613 syms
= slurp_symtab (abfd
);
5615 /* If following links, load any symbol tables from the linked files as well. */
5616 if (do_follow_links
&& is_mainfile
)
5620 for (i
= first_separate_info
; i
!= NULL
; i
= i
->next
)
5622 asymbol
** extra_syms
;
5623 long old_symcount
= symcount
;
5625 extra_syms
= slurp_symtab (i
->handle
);
5629 if (old_symcount
== 0)
5635 syms
= xrealloc (syms
, ((symcount
+ old_symcount
+ 1)
5636 * sizeof (asymbol
*)));
5637 memcpy (syms
+ old_symcount
,
5639 (symcount
+ 1) * sizeof (asymbol
*));
5643 symcount
+= old_symcount
;
5648 if (is_mainfile
|| process_links
)
5650 if (dump_section_headers
)
5651 dump_headers (abfd
);
5653 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
5654 || (disassemble
&& bfd_get_dynamic_symtab_upper_bound (abfd
) > 0))
5655 dynsyms
= slurp_dynamic_symtab (abfd
);
5659 synthcount
= bfd_get_synthetic_symtab (abfd
, symcount
, syms
,
5660 dynsymcount
, dynsyms
,
5667 dump_symbols (abfd
, false);
5668 if (dump_dynamic_symtab
)
5669 dump_symbols (abfd
, true);
5671 if (dump_dwarf_section_info
)
5672 dump_dwarf (abfd
, is_mainfile
);
5673 if (is_mainfile
|| process_links
)
5675 if (dump_ctf_section_info
)
5676 dump_ctf (abfd
, dump_ctf_section_name
, dump_ctf_parent_name
);
5677 if (dump_sframe_section_info
)
5678 dump_section_sframe (abfd
, dump_sframe_section_name
);
5679 if (dump_stab_section_info
)
5681 if (dump_reloc_info
&& ! disassemble
)
5683 if (dump_dynamic_reloc_info
&& ! disassemble
)
5684 dump_dynamic_relocs (abfd
);
5685 if (dump_section_contents
)
5688 disassemble_data (abfd
);
5695 dhandle
= read_debugging_info (abfd
, syms
, symcount
, true);
5696 if (dhandle
!= NULL
)
5698 if (!print_debugging_info (stdout
, dhandle
, abfd
, syms
,
5700 dump_debugging_tags
!= 0))
5702 non_fatal (_("%s: printing debugging information failed"),
5703 bfd_get_filename (abfd
));
5707 /* PR 6483: If there was no STABS debug info in the file, try
5709 else if (! dump_dwarf_section_info
)
5711 dwarf_select_sections_all ();
5712 dump_dwarf (abfd
, is_mainfile
);
5739 free_debug_memory ();
5743 display_object_bfd (bfd
*abfd
)
5747 if (bfd_check_format_matches (abfd
, bfd_object
, &matching
))
5749 dump_bfd (abfd
, true);
5753 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
5755 my_bfd_nonfatal (bfd_get_filename (abfd
));
5756 list_matching_formats (matching
);
5760 if (bfd_get_error () != bfd_error_file_not_recognized
)
5762 my_bfd_nonfatal (bfd_get_filename (abfd
));
5766 if (bfd_check_format_matches (abfd
, bfd_core
, &matching
))
5768 dump_bfd (abfd
, true);
5772 my_bfd_nonfatal (bfd_get_filename (abfd
));
5774 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
5775 list_matching_formats (matching
);
5779 display_any_bfd (bfd
*file
, int level
)
5781 /* Decompress sections unless dumping the section contents. */
5782 if (!dump_section_contents
)
5783 file
->flags
|= BFD_DECOMPRESS
;
5785 /* If the file is an archive, process all of its elements. */
5786 if (bfd_check_format (file
, bfd_archive
))
5789 bfd
*last_arfile
= NULL
;
5792 printf (_("In archive %s:\n"), sanitize_string (bfd_get_filename (file
)));
5793 else if (level
> 100)
5795 /* Prevent corrupted files from spinning us into an
5796 infinite loop. 100 is an arbitrary heuristic. */
5797 non_fatal (_("Archive nesting is too deep"));
5802 printf (_("In nested archive %s:\n"),
5803 sanitize_string (bfd_get_filename (file
)));
5807 bfd_set_error (bfd_error_no_error
);
5809 arfile
= bfd_openr_next_archived_file (file
, arfile
);
5812 if (bfd_get_error () != bfd_error_no_more_archived_files
)
5813 my_bfd_nonfatal (bfd_get_filename (file
));
5817 display_any_bfd (arfile
, level
+ 1);
5819 if (last_arfile
!= NULL
)
5821 bfd_close (last_arfile
);
5822 /* PR 17512: file: ac585d01. */
5823 if (arfile
== last_arfile
)
5829 last_arfile
= arfile
;
5832 if (last_arfile
!= NULL
)
5833 bfd_close (last_arfile
);
5836 display_object_bfd (file
);
5840 display_file (char *filename
, char *target
, bool last_file
)
5844 if (get_file_size (filename
) < 1)
5850 file
= bfd_openr (filename
, target
);
5853 my_bfd_nonfatal (filename
);
5857 display_any_bfd (file
, 0);
5859 /* This is an optimization to improve the speed of objdump, especially when
5860 dumping a file with lots of associated debug informatiom. Calling
5861 bfd_close on such a file can take a non-trivial amount of time as there
5862 are lots of lists to walk and buffers to free. This is only really
5863 necessary however if we are about to load another file and we need the
5864 memory back. Otherwise, if we are about to exit, then we can save (a lot
5865 of) time by only doing a quick close, and allowing the OS to reclaim the
5870 bfd_close_all_done (file
);
5874 main (int argc
, char **argv
)
5877 char *target
= default_target
;
5878 bool seenflag
= false;
5880 #ifdef HAVE_LC_MESSAGES
5881 setlocale (LC_MESSAGES
, "");
5883 setlocale (LC_CTYPE
, "");
5885 bindtextdomain (PACKAGE
, LOCALEDIR
);
5886 textdomain (PACKAGE
);
5888 program_name
= *argv
;
5889 xmalloc_set_program_name (program_name
);
5890 bfd_set_error_program_name (program_name
);
5892 expandargv (&argc
, &argv
);
5894 if (bfd_init () != BFD_INIT_MAGIC
)
5895 fatal (_("fatal error: libbfd ABI mismatch"));
5896 set_default_bfd_target ();
5898 while ((c
= getopt_long (argc
, argv
,
5899 "CDE:FGHI:LM:P:RSTU:VW::ab:defghij:lm:prstvwxz",
5900 long_options
, (int *) 0))
5906 break; /* We've been given a long option. */
5913 if (disassembler_options
)
5914 /* Ignore potential memory leak for now. */
5915 options
= concat (disassembler_options
, ",",
5916 optarg
, (const char *) NULL
);
5919 disassembler_options
= remove_whitespace_and_extra_commas (options
);
5926 display_file_offsets
= true;
5929 with_line_numbers
= true;
5938 enum demangling_styles style
;
5940 style
= cplus_demangle_name_to_style (optarg
);
5941 if (style
== unknown_demangling
)
5942 fatal (_("unknown demangling style `%s'"),
5945 cplus_demangle_set_style (style
);
5948 case OPTION_RECURSE_LIMIT
:
5949 demangle_flags
&= ~ DMGL_NO_RECURSE_LIMIT
;
5951 case OPTION_NO_RECURSE_LIMIT
:
5952 demangle_flags
|= DMGL_NO_RECURSE_LIMIT
;
5955 do_wide
= wide_output
= true;
5957 case OPTION_ADJUST_VMA
:
5958 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
5960 case OPTION_START_ADDRESS
:
5961 start_address
= parse_vma (optarg
, "--start-address");
5962 if ((stop_address
!= (bfd_vma
) -1) && stop_address
<= start_address
)
5963 fatal (_("error: the start address should be before the end address"));
5965 case OPTION_STOP_ADDRESS
:
5966 stop_address
= parse_vma (optarg
, "--stop-address");
5967 if ((start_address
!= (bfd_vma
) -1) && stop_address
<= start_address
)
5968 fatal (_("error: the stop address should be after the start address"));
5972 prefix_length
= strlen (prefix
);
5973 /* Remove an unnecessary trailing '/' */
5974 while (IS_DIR_SEPARATOR (prefix
[prefix_length
- 1]))
5977 case OPTION_PREFIX_STRIP
:
5978 prefix_strip
= atoi (optarg
);
5979 if (prefix_strip
< 0)
5980 fatal (_("error: prefix strip must be non-negative"));
5982 case OPTION_INSN_WIDTH
:
5983 insn_width
= strtoul (optarg
, NULL
, 0);
5984 if (insn_width
<= 0)
5985 fatal (_("error: instruction width must be positive"));
5987 case OPTION_INLINES
:
5988 unwind_inlines
= true;
5990 case OPTION_VISUALIZE_JUMPS
:
5991 visualize_jumps
= true;
5992 color_output
= false;
5993 extended_color_output
= false;
5996 if (streq (optarg
, "color"))
5997 color_output
= true;
5998 else if (streq (optarg
, "extended-color"))
6000 color_output
= true;
6001 extended_color_output
= true;
6003 else if (streq (optarg
, "off"))
6004 visualize_jumps
= false;
6007 non_fatal (_("unrecognized argument to --visualize-option"));
6012 case OPTION_DISASSEMBLER_COLOR
:
6013 if (streq (optarg
, "off"))
6014 disassembler_color
= off
;
6015 else if (streq (optarg
, "terminal"))
6016 disassembler_color
= on_if_terminal_output
;
6017 else if (streq (optarg
, "color")
6018 || streq (optarg
, "colour")
6019 || streq (optarg
, "on"))
6020 disassembler_color
= on
;
6021 else if (streq (optarg
, "extended")
6022 || streq (optarg
, "extended-color")
6023 || streq (optarg
, "extended-colour"))
6024 disassembler_color
= extended
;
6027 non_fatal (_("unrecognized argument to --disassembler-color"));
6032 if (strcmp (optarg
, "B") == 0)
6033 endian
= BFD_ENDIAN_BIG
;
6034 else if (strcmp (optarg
, "L") == 0)
6035 endian
= BFD_ENDIAN_LITTLE
;
6038 non_fatal (_("unrecognized -E option"));
6043 if (strncmp (optarg
, "big", strlen (optarg
)) == 0)
6044 endian
= BFD_ENDIAN_BIG
;
6045 else if (strncmp (optarg
, "little", strlen (optarg
)) == 0)
6046 endian
= BFD_ENDIAN_LITTLE
;
6049 non_fatal (_("unrecognized --endian type `%s'"), optarg
);
6055 dump_file_header
= true;
6059 formats_info
= true;
6063 add_include_path (optarg
);
6066 dump_private_headers
= true;
6070 dump_private_options
= optarg
;
6074 dump_private_headers
= true;
6076 dump_reloc_info
= true;
6077 dump_file_header
= true;
6078 dump_ar_hdrs
= true;
6079 dump_section_headers
= true;
6087 dump_dynamic_symtab
= true;
6093 disasm_sym
= optarg
;
6096 disassemble_zeroes
= true;
6100 disassemble_all
= true;
6105 with_source_code
= true;
6108 case OPTION_SOURCE_COMMENT
:
6110 with_source_code
= true;
6113 source_comment
= xstrdup (sanitize_string (optarg
));
6115 source_comment
= xstrdup ("# ");
6123 dump_debugging_tags
= 1;
6128 process_links
= true;
6129 do_follow_links
= true;
6135 if (dwarf_select_sections_by_letters (optarg
))
6136 dump_dwarf_section_info
= true;
6140 dump_dwarf_section_info
= true;
6141 dwarf_select_sections_all ();
6148 if (dwarf_select_sections_by_names (optarg
))
6149 dump_dwarf_section_info
= true;
6153 dwarf_select_sections_all ();
6154 dump_dwarf_section_info
= true;
6157 case OPTION_DWARF_DEPTH
:
6160 dwarf_cutoff_level
= strtoul (optarg
, & cp
, 0);
6163 case OPTION_DWARF_START
:
6166 dwarf_start_die
= strtoul (optarg
, & cp
, 0);
6167 suppress_bfd_header
= 1;
6170 case OPTION_DWARF_CHECK
:
6173 #ifdef ENABLE_LIBCTF
6175 dump_ctf_section_info
= true;
6177 dump_ctf_section_name
= xstrdup (optarg
);
6180 case OPTION_CTF_PARENT
:
6181 dump_ctf_parent_name
= xstrdup (optarg
);
6185 dump_sframe_section_info
= true;
6187 dump_sframe_section_name
= xstrdup (optarg
);
6191 dump_stab_section_info
= true;
6195 dump_section_contents
= true;
6199 dump_reloc_info
= true;
6203 dump_dynamic_reloc_info
= true;
6207 dump_ar_hdrs
= true;
6211 dump_section_headers
= true;
6216 show_version
= true;
6221 if (streq (optarg
, "default") || streq (optarg
, "d"))
6222 unicode_display
= unicode_default
;
6223 else if (streq (optarg
, "locale") || streq (optarg
, "l"))
6224 unicode_display
= unicode_locale
;
6225 else if (streq (optarg
, "escape") || streq (optarg
, "e"))
6226 unicode_display
= unicode_escape
;
6227 else if (streq (optarg
, "invalid") || streq (optarg
, "i"))
6228 unicode_display
= unicode_invalid
;
6229 else if (streq (optarg
, "hex") || streq (optarg
, "x"))
6230 unicode_display
= unicode_hex
;
6231 else if (streq (optarg
, "highlight") || streq (optarg
, "h"))
6232 unicode_display
= unicode_highlight
;
6234 fatal (_("invalid argument to -U/--unicode: %s"), optarg
);
6239 /* No need to set seenflag or to break - usage() does not return. */
6245 if (disassembler_color
== on_if_terminal_output
)
6246 disassembler_color
= isatty (1) ? on
: off
;
6249 print_version ("objdump");
6254 dump_any_debugging
= (dump_debugging
6255 || dump_dwarf_section_info
6257 || with_source_code
);
6260 exit_status
= display_info ();
6264 display_file ("a.out", target
, true);
6266 for (; optind
< argc
;)
6268 display_file (argv
[optind
], target
, optind
== argc
- 1);
6274 free (dump_ctf_section_name
);
6275 free (dump_ctf_parent_name
);
6276 free ((void *) source_comment
);