1 /* objdump.c -- dump information about an object file.
2 Copyright (C) 1990-2022 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
24 Objdump displays information about one or more object files, either on
25 their own, or inside libraries. It is commonly used as a disassembler,
26 but it can also display information about file headers, symbol tables,
27 relocations, debugging directives and more.
29 The flow of execution is as follows:
31 1. Command line arguments are checked for control switches and the
32 information to be displayed is selected.
34 2. Any remaining arguments are assumed to be object files, and they are
35 processed in order by display_bfd(). If the file is an archive each
36 of its elements is processed in turn.
38 3. The file's target architecture and binary file format are determined
39 by bfd_check_format(). If they are recognised, then dump_bfd() is
42 4. dump_bfd() in turn calls separate functions to display the requested
43 item(s) of information(s). For example disassemble_data() is called if
44 a disassembly has been requested.
46 When disassembling the code loops through blocks of instructions bounded
47 by symbols, calling disassemble_bytes() on each block. The actual
48 disassembling is done by the libopcodes library, via a function pointer
49 supplied by the disassembler() function. */
58 #include "demanguse.h"
62 #include "safe-ctype.h"
64 #include "libiberty.h"
66 #include "filenames.h"
75 /* Internal headers for the ELF .stab-dump code - sorry. */
76 #define BYTES_IN_WORD 32
77 #include "aout/aout64.h"
80 static int exit_status
= 0;
82 static char *default_target
= NULL
; /* Default at runtime. */
84 /* The following variables are set based on arguments passed on the
86 static int show_version
= 0; /* Show the version number. */
87 static int dump_section_contents
; /* -s */
88 static int dump_section_headers
; /* -h */
89 static bool dump_file_header
; /* -f */
90 static int dump_symtab
; /* -t */
91 static int dump_dynamic_symtab
; /* -T */
92 static int dump_reloc_info
; /* -r */
93 static int dump_dynamic_reloc_info
; /* -R */
94 static int dump_ar_hdrs
; /* -a */
95 static int dump_private_headers
; /* -p */
96 static char *dump_private_options
; /* -P */
97 static int no_addresses
; /* --no-addresses */
98 static int prefix_addresses
; /* --prefix-addresses */
99 static int with_line_numbers
; /* -l */
100 static bool with_source_code
; /* -S */
101 static int show_raw_insn
; /* --show-raw-insn */
102 static int dump_dwarf_section_info
; /* --dwarf */
103 static int dump_stab_section_info
; /* --stabs */
104 static int dump_ctf_section_info
; /* --ctf */
105 static char *dump_ctf_section_name
;
106 static char *dump_ctf_parent_name
; /* --ctf-parent */
107 static int do_demangle
; /* -C, --demangle */
108 static bool disassemble
; /* -d */
109 static bool disassemble_all
; /* -D */
110 static int disassemble_zeroes
; /* --disassemble-zeroes */
111 static bool formats_info
; /* -i */
112 static int wide_output
; /* -w */
113 static int insn_width
; /* --insn-width */
114 static bfd_vma start_address
= (bfd_vma
) -1; /* --start-address */
115 static bfd_vma stop_address
= (bfd_vma
) -1; /* --stop-address */
116 static int dump_debugging
; /* --debugging */
117 static int dump_debugging_tags
; /* --debugging-tags */
118 static int suppress_bfd_header
;
119 static int dump_special_syms
= 0; /* --special-syms */
120 static bfd_vma adjust_section_vma
= 0; /* --adjust-vma */
121 static int file_start_context
= 0; /* --file-start-context */
122 static bool display_file_offsets
; /* -F */
123 static const char *prefix
; /* --prefix */
124 static int prefix_strip
; /* --prefix-strip */
125 static size_t prefix_length
;
126 static bool unwind_inlines
; /* --inlines. */
127 static const char * disasm_sym
; /* Disassembly start symbol. */
128 static const char * source_comment
; /* --source_comment. */
129 static bool visualize_jumps
= false; /* --visualize-jumps. */
130 static bool color_output
= false; /* --visualize-jumps=color. */
131 static bool extended_color_output
= false; /* --visualize-jumps=extended-color. */
132 static int process_links
= false; /* --process-links. */
133 static bool disassembler_color
= false; /* --disassembler-color=color. */
134 static bool disassembler_extended_color
= false; /* --disassembler-color=extended-color. */
136 static int dump_any_debugging
;
137 static int demangle_flags
= DMGL_ANSI
| DMGL_PARAMS
;
139 /* This is reset to false each time we enter the disassembler, and set true
140 when the disassembler emits something in the dis_style_comment_start
141 style. Once this is true, all further output on that line is done in
142 the comment style. This only has an effect when disassembler coloring
144 static bool disassembler_in_comment
= false;
146 /* A structure to record the sections mentioned in -j switches. */
149 const char *name
; /* The name of the section. */
150 bool seen
; /* A flag to indicate that the section has been found in one or more input files. */
151 struct only
*next
; /* Pointer to the next structure in the list. */
153 /* Pointer to an array of 'only' structures.
154 This pointer is NULL if the -j switch has not been used. */
155 static struct only
* only_list
= NULL
;
157 /* Variables for handling include file path table. */
158 static const char **include_paths
;
159 static int include_path_count
;
161 /* Extra info to pass to the section disassembler and address printing
163 struct objdump_disasm_info
167 disassembler_ftype disassemble_fn
;
172 /* Architecture to disassemble for, or default if NULL. */
173 static char *machine
= NULL
;
175 /* Target specific options to the disassembler. */
176 static char *disassembler_options
= NULL
;
178 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
179 static enum bfd_endian endian
= BFD_ENDIAN_UNKNOWN
;
181 /* The symbol table. */
182 static asymbol
**syms
;
184 /* Number of symbols in `syms'. */
185 static long symcount
= 0;
187 /* The sorted symbol table. */
188 static asymbol
**sorted_syms
;
190 /* Number of symbols in `sorted_syms'. */
191 static long sorted_symcount
= 0;
193 /* The dynamic symbol table. */
194 static asymbol
**dynsyms
;
196 /* The synthetic symbol table. */
197 static asymbol
*synthsyms
;
198 static long synthcount
= 0;
200 /* Number of symbols in `dynsyms'. */
201 static long dynsymcount
= 0;
203 static bfd_byte
*stabs
;
204 static bfd_size_type stab_size
;
206 static bfd_byte
*strtab
;
207 static bfd_size_type stabstr_size
;
209 /* Handlers for -P/--private. */
210 static const struct objdump_private_desc
* const objdump_private_vectors
[] =
212 OBJDUMP_PRIVATE_VECTORS
216 /* The list of detected jumps inside a function. */
217 static struct jump_info
*detected_jumps
= NULL
;
219 typedef enum unicode_display_type
227 } unicode_display_type
;
229 static unicode_display_type unicode_display
= unicode_default
;
231 static void usage (FILE *, int) ATTRIBUTE_NORETURN
;
233 usage (FILE *stream
, int status
)
235 fprintf (stream
, _("Usage: %s <option(s)> <file(s)>\n"), program_name
);
236 fprintf (stream
, _(" Display information from object <file(s)>.\n"));
237 fprintf (stream
, _(" At least one of the following switches must be given:\n"));
238 fprintf (stream
, _("\
239 -a, --archive-headers Display archive header information\n"));
240 fprintf (stream
, _("\
241 -f, --file-headers Display the contents of the overall file header\n"));
242 fprintf (stream
, _("\
243 -p, --private-headers Display object format specific file header contents\n"));
244 fprintf (stream
, _("\
245 -P, --private=OPT,OPT... Display object format specific contents\n"));
246 fprintf (stream
, _("\
247 -h, --[section-]headers Display the contents of the section headers\n"));
248 fprintf (stream
, _("\
249 -x, --all-headers Display the contents of all headers\n"));
250 fprintf (stream
, _("\
251 -d, --disassemble Display assembler contents of executable sections\n"));
252 fprintf (stream
, _("\
253 -D, --disassemble-all Display assembler contents of all sections\n"));
254 fprintf (stream
, _("\
255 --disassemble=<sym> Display assembler contents from <sym>\n"));
256 fprintf (stream
, _("\
257 -S, --source Intermix source code with disassembly\n"));
258 fprintf (stream
, _("\
259 --source-comment[=<txt>] Prefix lines of source code with <txt>\n"));
260 fprintf (stream
, _("\
261 -s, --full-contents Display the full contents of all sections requested\n"));
262 fprintf (stream
, _("\
263 -g, --debugging Display debug information in object file\n"));
264 fprintf (stream
, _("\
265 -e, --debugging-tags Display debug information using ctags style\n"));
266 fprintf (stream
, _("\
267 -G, --stabs Display (in raw form) any STABS info in the file\n"));
268 fprintf (stream
, _("\
269 -W, --dwarf[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
270 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
271 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
272 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
274 Display the contents of DWARF debug sections\n"));
275 fprintf (stream
, _("\
276 -Wk,--dwarf=links Display the contents of sections that link to\n\
277 separate debuginfo files\n"));
278 #if DEFAULT_FOR_FOLLOW_LINKS
279 fprintf (stream
, _("\
280 -WK,--dwarf=follow-links\n\
281 Follow links to separate debug info files (default)\n"));
282 fprintf (stream
, _("\
283 -WN,--dwarf=no-follow-links\n\
284 Do not follow links to separate debug info files\n"));
286 fprintf (stream
, _("\
287 -WK,--dwarf=follow-links\n\
288 Follow links to separate debug info files\n"));
289 fprintf (stream
, _("\
290 -WN,--dwarf=no-follow-links\n\
291 Do not follow links to separate debug info files\n\
294 #if HAVE_LIBDEBUGINFOD
295 fprintf (stream
, _("\
296 -WD --dwarf=use-debuginfod\n\
297 When following links, also query debuginfod servers (default)\n"));
298 fprintf (stream
, _("\
299 -WE --dwarf=do-not-use-debuginfod\n\
300 When following links, do not query debuginfod servers\n"));
302 fprintf (stream
, _("\
303 -L, --process-links Display the contents of non-debug sections in\n\
304 separate debuginfo files. (Implies -WK)\n"));
306 fprintf (stream
, _("\
307 --ctf[=SECTION] Display CTF info from SECTION, (default `.ctf')\n"));
309 fprintf (stream
, _("\
310 -t, --syms Display the contents of the symbol table(s)\n"));
311 fprintf (stream
, _("\
312 -T, --dynamic-syms Display the contents of the dynamic symbol table\n"));
313 fprintf (stream
, _("\
314 -r, --reloc Display the relocation entries in the file\n"));
315 fprintf (stream
, _("\
316 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n"));
317 fprintf (stream
, _("\
318 @<file> Read options from <file>\n"));
319 fprintf (stream
, _("\
320 -v, --version Display this program's version number\n"));
321 fprintf (stream
, _("\
322 -i, --info List object formats and architectures supported\n"));
323 fprintf (stream
, _("\
324 -H, --help Display this information\n"));
328 const struct objdump_private_desc
* const *desc
;
330 fprintf (stream
, _("\n The following switches are optional:\n"));
331 fprintf (stream
, _("\
332 -b, --target=BFDNAME Specify the target object format as BFDNAME\n"));
333 fprintf (stream
, _("\
334 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n"));
335 fprintf (stream
, _("\
336 -j, --section=NAME Only display information for section NAME\n"));
337 fprintf (stream
, _("\
338 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n"));
339 fprintf (stream
, _("\
340 -EB --endian=big Assume big endian format when disassembling\n"));
341 fprintf (stream
, _("\
342 -EL --endian=little Assume little endian format when disassembling\n"));
343 fprintf (stream
, _("\
344 --file-start-context Include context from start of file (with -S)\n"));
345 fprintf (stream
, _("\
346 -I, --include=DIR Add DIR to search list for source files\n"));
347 fprintf (stream
, _("\
348 -l, --line-numbers Include line numbers and filenames in output\n"));
349 fprintf (stream
, _("\
350 -F, --file-offsets Include file offsets when displaying information\n"));
351 fprintf (stream
, _("\
352 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n"));
353 display_demangler_styles (stream
, _("\
355 fprintf (stream
, _("\
356 --recurse-limit Enable a limit on recursion whilst demangling\n\
358 fprintf (stream
, _("\
359 --no-recurse-limit Disable a limit on recursion whilst demangling\n"));
360 fprintf (stream
, _("\
361 -w, --wide Format output for more than 80 columns\n"));
362 fprintf (stream
, _("\
363 -U[d|l|i|x|e|h] Controls the display of UTF-8 unicode characters\n\
364 --unicode=[default|locale|invalid|hex|escape|highlight]\n"));
365 fprintf (stream
, _("\
366 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n"));
367 fprintf (stream
, _("\
368 --start-address=ADDR Only process data whose address is >= ADDR\n"));
369 fprintf (stream
, _("\
370 --stop-address=ADDR Only process data whose address is < ADDR\n"));
371 fprintf (stream
, _("\
372 --no-addresses Do not print address alongside disassembly\n"));
373 fprintf (stream
, _("\
374 --prefix-addresses Print complete address alongside disassembly\n"));
375 fprintf (stream
, _("\
376 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n"));
377 fprintf (stream
, _("\
378 --insn-width=WIDTH Display WIDTH bytes on a single line for -d\n"));
379 fprintf (stream
, _("\
380 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n"));
381 fprintf (stream
, _("\
382 --special-syms Include special symbols in symbol dumps\n"));
383 fprintf (stream
, _("\
384 --inlines Print all inlines for source line (with -l)\n"));
385 fprintf (stream
, _("\
386 --prefix=PREFIX Add PREFIX to absolute paths for -S\n"));
387 fprintf (stream
, _("\
388 --prefix-strip=LEVEL Strip initial directory names for -S\n"));
389 fprintf (stream
, _("\
390 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
391 fprintf (stream
, _("\
392 --dwarf-start=N Display DIEs starting at offset N\n"));
393 fprintf (stream
, _("\
394 --dwarf-check Make additional dwarf consistency checks.\n"));
396 fprintf (stream
, _("\
397 --ctf-parent=NAME Use CTF archive member NAME as the CTF parent\n"));
399 fprintf (stream
, _("\
400 --visualize-jumps Visualize jumps by drawing ASCII art lines\n"));
401 fprintf (stream
, _("\
402 --visualize-jumps=color Use colors in the ASCII art\n"));
403 fprintf (stream
, _("\
404 --visualize-jumps=extended-color\n\
405 Use extended 8-bit color codes\n"));
406 fprintf (stream
, _("\
407 --visualize-jumps=off Disable jump visualization\n\n"));
408 fprintf (stream
, _("\
409 --disassembler-color=off Disable disassembler color output.\n\n"));
410 fprintf (stream
, _("\
411 --disassembler-color=color Use basic colors in disassembler output.\n\n"));
413 list_supported_targets (program_name
, stream
);
414 list_supported_architectures (program_name
, stream
);
416 disassembler_usage (stream
);
418 if (objdump_private_vectors
[0] != NULL
)
421 _("\nOptions supported for -P/--private switch:\n"));
422 for (desc
= objdump_private_vectors
; *desc
!= NULL
; desc
++)
423 (*desc
)->help (stream
);
426 if (REPORT_BUGS_TO
[0] && status
== 0)
427 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
431 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
435 OPTION_START_ADDRESS
,
445 OPTION_RECURSE_LIMIT
,
446 OPTION_NO_RECURSE_LIMIT
,
448 OPTION_SOURCE_COMMENT
,
453 OPTION_VISUALIZE_JUMPS
,
454 OPTION_DISASSEMBLER_COLOR
457 static struct option long_options
[]=
459 {"adjust-vma", required_argument
, NULL
, OPTION_ADJUST_VMA
},
460 {"all-headers", no_argument
, NULL
, 'x'},
461 {"architecture", required_argument
, NULL
, 'm'},
462 {"archive-headers", no_argument
, NULL
, 'a'},
464 {"ctf", optional_argument
, NULL
, OPTION_CTF
},
465 {"ctf-parent", required_argument
, NULL
, OPTION_CTF_PARENT
},
467 {"debugging", no_argument
, NULL
, 'g'},
468 {"debugging-tags", no_argument
, NULL
, 'e'},
469 {"demangle", optional_argument
, NULL
, 'C'},
470 {"disassemble", optional_argument
, NULL
, 'd'},
471 {"disassemble-all", no_argument
, NULL
, 'D'},
472 {"disassemble-zeroes", no_argument
, NULL
, 'z'},
473 {"disassembler-options", required_argument
, NULL
, 'M'},
474 {"dwarf", optional_argument
, NULL
, OPTION_DWARF
},
475 {"dwarf-check", no_argument
, 0, OPTION_DWARF_CHECK
},
476 {"dwarf-depth", required_argument
, 0, OPTION_DWARF_DEPTH
},
477 {"dwarf-start", required_argument
, 0, OPTION_DWARF_START
},
478 {"dynamic-reloc", no_argument
, NULL
, 'R'},
479 {"dynamic-syms", no_argument
, NULL
, 'T'},
480 {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
481 {"file-headers", no_argument
, NULL
, 'f'},
482 {"file-offsets", no_argument
, NULL
, 'F'},
483 {"file-start-context", no_argument
, &file_start_context
, 1},
484 {"full-contents", no_argument
, NULL
, 's'},
485 {"headers", no_argument
, NULL
, 'h'},
486 {"help", no_argument
, NULL
, 'H'},
487 {"include", required_argument
, NULL
, 'I'},
488 {"info", no_argument
, NULL
, 'i'},
489 {"inlines", no_argument
, 0, OPTION_INLINES
},
490 {"insn-width", required_argument
, NULL
, OPTION_INSN_WIDTH
},
491 {"line-numbers", no_argument
, NULL
, 'l'},
492 {"no-addresses", no_argument
, &no_addresses
, 1},
493 {"no-recurse-limit", no_argument
, NULL
, OPTION_NO_RECURSE_LIMIT
},
494 {"no-recursion-limit", no_argument
, NULL
, OPTION_NO_RECURSE_LIMIT
},
495 {"no-show-raw-insn", no_argument
, &show_raw_insn
, -1},
496 {"prefix", required_argument
, NULL
, OPTION_PREFIX
},
497 {"prefix-addresses", no_argument
, &prefix_addresses
, 1},
498 {"prefix-strip", required_argument
, NULL
, OPTION_PREFIX_STRIP
},
499 {"private", required_argument
, NULL
, 'P'},
500 {"private-headers", no_argument
, NULL
, 'p'},
501 {"process-links", no_argument
, &process_links
, true},
502 {"recurse-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
503 {"recursion-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
504 {"reloc", no_argument
, NULL
, 'r'},
505 {"section", required_argument
, NULL
, 'j'},
506 {"section-headers", no_argument
, NULL
, 'h'},
507 {"show-raw-insn", no_argument
, &show_raw_insn
, 1},
508 {"source", no_argument
, NULL
, 'S'},
509 {"source-comment", optional_argument
, NULL
, OPTION_SOURCE_COMMENT
},
510 {"special-syms", no_argument
, &dump_special_syms
, 1},
511 {"stabs", no_argument
, NULL
, 'G'},
512 {"start-address", required_argument
, NULL
, OPTION_START_ADDRESS
},
513 {"stop-address", required_argument
, NULL
, OPTION_STOP_ADDRESS
},
514 {"syms", no_argument
, NULL
, 't'},
515 {"target", required_argument
, NULL
, 'b'},
516 {"unicode", required_argument
, NULL
, 'U'},
517 {"version", no_argument
, NULL
, 'V'},
518 {"visualize-jumps", optional_argument
, 0, OPTION_VISUALIZE_JUMPS
},
519 {"wide", no_argument
, NULL
, 'w'},
520 {"disassembler-color", required_argument
, NULL
, OPTION_DISASSEMBLER_COLOR
},
521 {NULL
, no_argument
, NULL
, 0}
525 nonfatal (const char *msg
)
531 /* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
532 The conversion format is controlled by the unicode_display variable.
533 Returns the number of characters added to OUT.
534 Returns the number of bytes consumed from IN in CONSUMED.
535 Always consumes at least one byte and displays at least one character. */
538 display_utf8 (const unsigned char * in
, char * out
, unsigned int * consumed
)
540 char * orig_out
= out
;
541 unsigned int nchars
= 0;
544 if (unicode_display
== unicode_default
)
550 if ((in
[1] & 0xc0) != 0x80)
553 if ((in
[0] & 0x20) == 0)
559 if ((in
[2] & 0xc0) != 0x80)
562 if ((in
[0] & 0x10) == 0)
568 if ((in
[3] & 0xc0) != 0x80)
574 switch (unicode_display
)
577 /* Copy the bytes into the output buffer as is. */
578 memcpy (out
, in
, nchars
);
582 case unicode_invalid
:
584 out
+= sprintf (out
, "%c", unicode_display
== unicode_hex
? '<' : '{');
585 out
+= sprintf (out
, "0x");
586 for (j
= 0; j
< nchars
; j
++)
587 out
+= sprintf (out
, "%02x", in
[j
]);
588 out
+= sprintf (out
, "%c", unicode_display
== unicode_hex
? '>' : '}');
591 case unicode_highlight
:
593 out
+= sprintf (out
, "\x1B[31;47m"); /* Red. */
599 out
+= sprintf (out
, "\\u%02x%02x",
600 ((in
[0] & 0x1c) >> 2),
601 ((in
[0] & 0x03) << 6) | (in
[1] & 0x3f));
605 out
+= sprintf (out
, "\\u%02x%02x",
606 ((in
[0] & 0x0f) << 4) | ((in
[1] & 0x3c) >> 2),
607 ((in
[1] & 0x03) << 6) | ((in
[2] & 0x3f)));
611 out
+= sprintf (out
, "\\u%02x%02x%02x",
612 ((in
[0] & 0x07) << 6) | ((in
[1] & 0x3c) >> 2),
613 ((in
[1] & 0x03) << 6) | ((in
[2] & 0x3c) >> 2),
614 ((in
[2] & 0x03) << 6) | ((in
[3] & 0x3f)));
621 if (unicode_display
== unicode_highlight
&& isatty (1))
622 out
+= sprintf (out
, "\033[0m"); /* Default colour. */
631 return out
- orig_out
;
634 /* Not a valid UTF-8 sequence. */
640 /* Returns a version of IN with any control characters
641 replaced by escape sequences. Uses a static buffer
644 If unicode display is enabled, then also handles the
645 conversion of unicode characters. */
648 sanitize_string (const char * in
)
650 static char * buffer
= NULL
;
651 static size_t buffer_len
= 0;
652 const char * original
= in
;
659 /* See if any conversion is necessary. In the majority
660 of cases it will not be needed. */
663 unsigned char c
= *in
++;
671 if (unicode_display
!= unicode_default
&& c
>= 0xc0)
676 /* Copy the input, translating as needed. */
678 if (buffer_len
< (strlen (in
) * 9))
680 free ((void *) buffer
);
681 buffer_len
= strlen (in
) * 9;
682 buffer
= xmalloc (buffer_len
+ 1);
688 unsigned char c
= *in
++;
698 else if (unicode_display
!= unicode_default
&& c
>= 0xc0)
700 unsigned int num_consumed
;
702 out
+= display_utf8 ((const unsigned char *)(in
- 1), out
, & num_consumed
);
703 in
+= num_consumed
- 1;
715 /* Returns TRUE if the specified section should be dumped. */
718 process_section_p (asection
* section
)
722 if (only_list
== NULL
)
725 for (only
= only_list
; only
; only
= only
->next
)
726 if (strcmp (only
->name
, section
->name
) == 0)
735 /* Add an entry to the 'only' list. */
738 add_only (char * name
)
742 /* First check to make sure that we do not
743 already have an entry for this name. */
744 for (only
= only_list
; only
; only
= only
->next
)
745 if (strcmp (only
->name
, name
) == 0)
748 only
= xmalloc (sizeof * only
);
751 only
->next
= only_list
;
755 /* Release the memory used by the 'only' list.
756 PR 11225: Issue a warning message for unseen sections.
757 Only do this if none of the sections were seen. This is mainly to support
758 tools like the GAS testsuite where an object file is dumped with a list of
759 generic section names known to be present in a range of different file
763 free_only_list (void)
765 bool at_least_one_seen
= false;
769 if (only_list
== NULL
)
772 for (only
= only_list
; only
; only
= only
->next
)
775 at_least_one_seen
= true;
779 for (only
= only_list
; only
; only
= next
)
781 if (! at_least_one_seen
)
783 non_fatal (_("section '%s' mentioned in a -j option, "
784 "but not found in any input file"),
795 dump_section_header (bfd
*abfd
, asection
*section
, void *data
)
798 unsigned int opb
= bfd_octets_per_byte (abfd
, section
);
799 int longest_section_name
= *((int *) data
);
801 /* Ignore linker created section. See elfNN_ia64_object_p in
803 if (section
->flags
& SEC_LINKER_CREATED
)
806 /* PR 10413: Skip sections that we are ignoring. */
807 if (! process_section_p (section
))
810 printf ("%3d %-*s %08lx ", section
->index
, longest_section_name
,
811 sanitize_string (bfd_section_name (section
)),
812 (unsigned long) bfd_section_size (section
) / opb
);
813 bfd_printf_vma (abfd
, bfd_section_vma (section
));
815 bfd_printf_vma (abfd
, section
->lma
);
816 printf (" %08lx 2**%u", (unsigned long) section
->filepos
,
817 bfd_section_alignment (section
));
823 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
825 PF (SEC_HAS_CONTENTS
, "CONTENTS");
826 PF (SEC_ALLOC
, "ALLOC");
827 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
828 PF (SEC_LOAD
, "LOAD");
829 PF (SEC_RELOC
, "RELOC");
830 PF (SEC_READONLY
, "READONLY");
831 PF (SEC_CODE
, "CODE");
832 PF (SEC_DATA
, "DATA");
834 PF (SEC_DEBUGGING
, "DEBUGGING");
835 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
836 PF (SEC_EXCLUDE
, "EXCLUDE");
837 PF (SEC_SORT_ENTRIES
, "SORT_ENTRIES");
838 if (bfd_get_arch (abfd
) == bfd_arch_tic54x
)
840 PF (SEC_TIC54X_BLOCK
, "BLOCK");
841 PF (SEC_TIC54X_CLINK
, "CLINK");
843 PF (SEC_SMALL_DATA
, "SMALL_DATA");
844 if (bfd_get_flavour (abfd
) == bfd_target_coff_flavour
)
846 PF (SEC_COFF_SHARED
, "SHARED");
847 PF (SEC_COFF_NOREAD
, "NOREAD");
849 else if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
851 PF (SEC_ELF_OCTETS
, "OCTETS");
852 PF (SEC_ELF_PURECODE
, "PURECODE");
854 PF (SEC_THREAD_LOCAL
, "THREAD_LOCAL");
855 PF (SEC_GROUP
, "GROUP");
856 if (bfd_get_arch (abfd
) == bfd_arch_mep
)
858 PF (SEC_MEP_VLIW
, "VLIW");
861 if ((section
->flags
& SEC_LINK_ONCE
) != 0)
864 struct coff_comdat_info
*comdat
;
866 switch (section
->flags
& SEC_LINK_DUPLICATES
)
870 case SEC_LINK_DUPLICATES_DISCARD
:
871 ls
= "LINK_ONCE_DISCARD";
873 case SEC_LINK_DUPLICATES_ONE_ONLY
:
874 ls
= "LINK_ONCE_ONE_ONLY";
876 case SEC_LINK_DUPLICATES_SAME_SIZE
:
877 ls
= "LINK_ONCE_SAME_SIZE";
879 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
880 ls
= "LINK_ONCE_SAME_CONTENTS";
883 printf ("%s%s", comma
, ls
);
885 comdat
= bfd_coff_get_comdat_section (abfd
, section
);
887 printf (" (COMDAT %s %ld)", comdat
->name
, comdat
->symbol
);
896 /* Called on each SECTION in ABFD, update the int variable pointed to by
897 DATA which contains the string length of the longest section name. */
900 find_longest_section_name (bfd
*abfd ATTRIBUTE_UNUSED
,
901 asection
*section
, void *data
)
903 int *longest_so_far
= (int *) data
;
907 /* Ignore linker created section. */
908 if (section
->flags
& SEC_LINKER_CREATED
)
911 /* Skip sections that we are ignoring. */
912 if (! process_section_p (section
))
915 name
= bfd_section_name (section
);
916 len
= (int) strlen (name
);
917 if (len
> *longest_so_far
)
918 *longest_so_far
= len
;
922 dump_headers (bfd
*abfd
)
924 /* The default width of 13 is just an arbitrary choice. */
925 int max_section_name_length
= 13;
931 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
932 if (bfd_get_arch_size (abfd
) == 32)
938 printf (_("Sections:\n"));
941 bfd_map_over_sections (abfd
, find_longest_section_name
,
942 &max_section_name_length
);
944 printf (_("Idx %-*s Size %-*s%-*sFile off Algn"),
945 max_section_name_length
, "Name",
946 bfd_vma_width
, "VMA",
947 bfd_vma_width
, "LMA");
950 printf (_(" Flags"));
953 bfd_map_over_sections (abfd
, dump_section_header
,
954 &max_section_name_length
);
958 slurp_symtab (bfd
*abfd
)
963 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
969 storage
= bfd_get_symtab_upper_bound (abfd
);
972 non_fatal (_("failed to read symbol table from: %s"), bfd_get_filename (abfd
));
973 bfd_fatal (_("error message was"));
978 off_t filesize
= bfd_get_file_size (abfd
);
982 && filesize
< storage
983 /* The MMO file format supports its own special compression
984 technique, so its sections can be larger than the file size. */
985 && bfd_get_flavour (abfd
) != bfd_target_mmo_flavour
)
987 bfd_nonfatal_message (bfd_get_filename (abfd
), abfd
, NULL
,
988 _("error: symbol table size (%#lx) "
989 "is larger than filesize (%#lx)"),
990 storage
, (long) filesize
);
996 sy
= (asymbol
**) xmalloc (storage
);
999 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
1001 bfd_fatal (bfd_get_filename (abfd
));
1005 /* Read in the dynamic symbols. */
1008 slurp_dynamic_symtab (bfd
*abfd
)
1010 asymbol
**sy
= NULL
;
1013 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
1016 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
1018 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd
));
1024 bfd_fatal (bfd_get_filename (abfd
));
1028 sy
= (asymbol
**) xmalloc (storage
);
1030 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
1031 if (dynsymcount
< 0)
1032 bfd_fatal (bfd_get_filename (abfd
));
1036 /* Some symbol names are significant and should be kept in the
1037 table of sorted symbol names, even if they are marked as
1038 debugging/section symbols. */
1041 is_significant_symbol_name (const char * name
)
1043 return startswith (name
, ".plt") || startswith (name
, ".got");
1046 /* Filter out (in place) symbols that are useless for disassembly.
1047 COUNT is the number of elements in SYMBOLS.
1048 Return the number of useful symbols. */
1051 remove_useless_symbols (asymbol
**symbols
, long count
)
1053 asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
1055 while (--count
>= 0)
1057 asymbol
*sym
= *in_ptr
++;
1059 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
1061 if ((sym
->flags
& (BSF_DEBUGGING
| BSF_SECTION_SYM
))
1062 && ! is_significant_symbol_name (sym
->name
))
1064 if (bfd_is_und_section (sym
->section
)
1065 || bfd_is_com_section (sym
->section
))
1070 return out_ptr
- symbols
;
1073 static const asection
*compare_section
;
1075 /* Sort symbols into value order. */
1078 compare_symbols (const void *ap
, const void *bp
)
1080 const asymbol
*a
= * (const asymbol
**) ap
;
1081 const asymbol
*b
= * (const asymbol
**) bp
;
1086 bool as
, af
, bs
, bf
;
1090 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
1092 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
1095 /* Prefer symbols from the section currently being disassembled.
1096 Don't sort symbols from other sections by section, since there
1097 isn't much reason to prefer one section over another otherwise.
1098 See sym_ok comment for why we compare by section name. */
1099 as
= strcmp (compare_section
->name
, a
->section
->name
) == 0;
1100 bs
= strcmp (compare_section
->name
, b
->section
->name
) == 0;
1106 an
= bfd_asymbol_name (a
);
1107 bn
= bfd_asymbol_name (b
);
1111 /* The symbols gnu_compiled and gcc2_compiled convey no real
1112 information, so put them after other symbols with the same value. */
1113 af
= (strstr (an
, "gnu_compiled") != NULL
1114 || strstr (an
, "gcc2_compiled") != NULL
);
1115 bf
= (strstr (bn
, "gnu_compiled") != NULL
1116 || strstr (bn
, "gcc2_compiled") != NULL
);
1123 /* We use a heuristic for the file name, to try to sort it after
1124 more useful symbols. It may not work on non Unix systems, but it
1125 doesn't really matter; the only difference is precisely which
1126 symbol names get printed. */
1128 #define file_symbol(s, sn, snl) \
1129 (((s)->flags & BSF_FILE) != 0 \
1131 && (sn)[(snl) - 2] == '.' \
1132 && ((sn)[(snl) - 1] == 'o' \
1133 || (sn)[(snl) - 1] == 'a')))
1135 af
= file_symbol (a
, an
, anl
);
1136 bf
= file_symbol (b
, bn
, bnl
);
1143 /* Sort function and object symbols before global symbols before
1144 local symbols before section symbols before debugging symbols. */
1149 if ((aflags
& BSF_DEBUGGING
) != (bflags
& BSF_DEBUGGING
))
1151 if ((aflags
& BSF_DEBUGGING
) != 0)
1156 if ((aflags
& BSF_SECTION_SYM
) != (bflags
& BSF_SECTION_SYM
))
1158 if ((aflags
& BSF_SECTION_SYM
) != 0)
1163 if ((aflags
& BSF_FUNCTION
) != (bflags
& BSF_FUNCTION
))
1165 if ((aflags
& BSF_FUNCTION
) != 0)
1170 if ((aflags
& BSF_OBJECT
) != (bflags
& BSF_OBJECT
))
1172 if ((aflags
& BSF_OBJECT
) != 0)
1177 if ((aflags
& BSF_LOCAL
) != (bflags
& BSF_LOCAL
))
1179 if ((aflags
& BSF_LOCAL
) != 0)
1184 if ((aflags
& BSF_GLOBAL
) != (bflags
& BSF_GLOBAL
))
1186 if ((aflags
& BSF_GLOBAL
) != 0)
1192 if (bfd_get_flavour (bfd_asymbol_bfd (a
)) == bfd_target_elf_flavour
1193 && bfd_get_flavour (bfd_asymbol_bfd (b
)) == bfd_target_elf_flavour
)
1198 if ((a
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0)
1199 asz
= ((elf_symbol_type
*) a
)->internal_elf_sym
.st_size
;
1201 if ((b
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0)
1202 bsz
= ((elf_symbol_type
*) b
)->internal_elf_sym
.st_size
;
1204 return asz
> bsz
? -1 : 1;
1207 /* Symbols that start with '.' might be section names, so sort them
1208 after symbols that don't start with '.'. */
1209 if (an
[0] == '.' && bn
[0] != '.')
1211 if (an
[0] != '.' && bn
[0] == '.')
1214 /* Finally, if we can't distinguish them in any other way, try to
1215 get consistent results by sorting the symbols by name. */
1216 return strcmp (an
, bn
);
1219 /* Sort relocs into address order. */
1222 compare_relocs (const void *ap
, const void *bp
)
1224 const arelent
*a
= * (const arelent
**) ap
;
1225 const arelent
*b
= * (const arelent
**) bp
;
1227 if (a
->address
> b
->address
)
1229 else if (a
->address
< b
->address
)
1232 /* So that associated relocations tied to the same address show up
1233 in the correct order, we don't do any further sorting. */
1242 /* Print an address (VMA) to the output stream in INFO.
1243 If SKIP_ZEROES is TRUE, omit leading zeroes. */
1246 objdump_print_value (bfd_vma vma
, struct disassemble_info
*inf
,
1251 struct objdump_disasm_info
*aux
;
1253 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1254 bfd_sprintf_vma (aux
->abfd
, buf
, vma
);
1259 for (p
= buf
; *p
== '0'; ++p
)
1264 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_address
, "%s", p
);
1267 /* Print the name of a symbol. */
1270 objdump_print_symname (bfd
*abfd
, struct disassemble_info
*inf
,
1274 const char *name
, *version_string
= NULL
;
1275 bool hidden
= false;
1278 name
= bfd_asymbol_name (sym
);
1279 if (do_demangle
&& name
[0] != '\0')
1281 /* Demangle the name. */
1282 alloc
= bfd_demangle (abfd
, name
, demangle_flags
);
1287 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0)
1288 version_string
= bfd_get_symbol_version_string (abfd
, sym
, true,
1291 if (bfd_is_und_section (bfd_asymbol_section (sym
)))
1294 name
= sanitize_string (name
);
1298 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_symbol
, "%s", name
);
1299 if (version_string
&& *version_string
!= '\0')
1300 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_symbol
,
1301 hidden
? "@%s" : "@@%s",
1306 printf ("%s", name
);
1307 if (version_string
&& *version_string
!= '\0')
1308 printf (hidden
? "@%s" : "@@%s", version_string
);
1316 sym_ok (bool want_section
,
1317 bfd
*abfd ATTRIBUTE_UNUSED
,
1320 struct disassemble_info
*inf
)
1324 /* NB: An object file can have different sections with the same
1325 section name. Compare compare section pointers if they have
1327 if (sorted_syms
[place
]->section
->owner
== sec
->owner
1328 && sorted_syms
[place
]->section
!= sec
)
1331 /* Note - we cannot just compare section pointers because they could
1332 be different, but the same... Ie the symbol that we are trying to
1333 find could have come from a separate debug info file. Under such
1334 circumstances the symbol will be associated with a section in the
1335 debug info file, whilst the section we want is in a normal file.
1336 So the section pointers will be different, but the section names
1337 will be the same. */
1338 if (strcmp (bfd_section_name (sorted_syms
[place
]->section
),
1339 bfd_section_name (sec
)) != 0)
1343 return inf
->symbol_is_valid (sorted_syms
[place
], inf
);
1346 /* Locate a symbol given a bfd and a section (from INFO->application_data),
1347 and a VMA. If INFO->application_data->require_sec is TRUE, then always
1348 require the symbol to be in the section. Returns NULL if there is no
1349 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
1350 of the symbol in sorted_syms. */
1353 find_symbol_for_address (bfd_vma vma
,
1354 struct disassemble_info
*inf
,
1357 /* @@ Would it speed things up to cache the last two symbols returned,
1358 and maybe their address ranges? For many processors, only one memory
1359 operand can be present at a time, so the 2-entry cache wouldn't be
1360 constantly churned by code doing heavy memory accesses. */
1362 /* Indices in `sorted_syms'. */
1364 long max_count
= sorted_symcount
;
1366 struct objdump_disasm_info
*aux
;
1373 if (sorted_symcount
< 1)
1376 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1379 opb
= inf
->octets_per_byte
;
1381 /* Perform a binary search looking for the closest symbol to the
1382 required value. We are searching the range (min, max_count]. */
1383 while (min
+ 1 < max_count
)
1387 thisplace
= (max_count
+ min
) / 2;
1388 sym
= sorted_syms
[thisplace
];
1390 if (bfd_asymbol_value (sym
) > vma
)
1391 max_count
= thisplace
;
1392 else if (bfd_asymbol_value (sym
) < vma
)
1401 /* The symbol we want is now in min, the low end of the range we
1402 were searching. If there are several symbols with the same
1403 value, we want the first one. */
1405 while (thisplace
> 0
1406 && (bfd_asymbol_value (sorted_syms
[thisplace
])
1407 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
1410 /* Prefer a symbol in the current section if we have multple symbols
1411 with the same value, as can occur with overlays or zero size
1414 while (min
< max_count
1415 && (bfd_asymbol_value (sorted_syms
[min
])
1416 == bfd_asymbol_value (sorted_syms
[thisplace
])))
1418 if (sym_ok (true, abfd
, min
, sec
, inf
))
1425 return sorted_syms
[thisplace
];
1430 /* If the file is relocatable, and the symbol could be from this
1431 section, prefer a symbol from this section over symbols from
1432 others, even if the other symbol's value might be closer.
1434 Note that this may be wrong for some symbol references if the
1435 sections have overlapping memory ranges, but in that case there's
1436 no way to tell what's desired without looking at the relocation
1439 Also give the target a chance to reject symbols. */
1440 want_section
= (aux
->require_sec
1441 || ((abfd
->flags
& HAS_RELOC
) != 0
1442 && vma
>= bfd_section_vma (sec
)
1443 && vma
< (bfd_section_vma (sec
)
1444 + bfd_section_size (sec
) / opb
)));
1446 if (! sym_ok (want_section
, abfd
, thisplace
, sec
, inf
))
1449 long newplace
= sorted_symcount
;
1451 for (i
= min
- 1; i
>= 0; i
--)
1453 if (sym_ok (want_section
, abfd
, i
, sec
, inf
))
1455 if (newplace
== sorted_symcount
)
1458 if (bfd_asymbol_value (sorted_syms
[i
])
1459 != bfd_asymbol_value (sorted_syms
[newplace
]))
1462 /* Remember this symbol and keep searching until we reach
1463 an earlier address. */
1468 if (newplace
!= sorted_symcount
)
1469 thisplace
= newplace
;
1472 /* We didn't find a good symbol with a smaller value.
1473 Look for one with a larger value. */
1474 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
1476 if (sym_ok (want_section
, abfd
, i
, sec
, inf
))
1484 if (! sym_ok (want_section
, abfd
, thisplace
, sec
, inf
))
1485 /* There is no suitable symbol. */
1489 /* If we have not found an exact match for the specified address
1490 and we have dynamic relocations available, then we can produce
1491 a better result by matching a relocation to the address and
1492 using the symbol associated with that relocation. */
1493 rel_count
= inf
->dynrelcount
;
1495 && sorted_syms
[thisplace
]->value
!= vma
1497 && inf
->dynrelbuf
!= NULL
1498 && inf
->dynrelbuf
[0]->address
<= vma
1499 && inf
->dynrelbuf
[rel_count
- 1]->address
>= vma
1500 /* If we have matched a synthetic symbol, then stick with that. */
1501 && (sorted_syms
[thisplace
]->flags
& BSF_SYNTHETIC
) == 0)
1504 arelent
** rel_high
;
1506 rel_low
= inf
->dynrelbuf
;
1507 rel_high
= rel_low
+ rel_count
- 1;
1508 while (rel_low
<= rel_high
)
1510 arelent
**rel_mid
= &rel_low
[(rel_high
- rel_low
) / 2];
1511 arelent
* rel
= *rel_mid
;
1513 if (rel
->address
== vma
)
1515 /* Absolute relocations do not provide a more helpful
1516 symbolic address. Find a non-absolute relocation
1517 with the same address. */
1518 arelent
**rel_vma
= rel_mid
;
1520 rel_mid
>= rel_low
&& rel_mid
[0]->address
== vma
;
1524 for (; rel_vma
<= rel_high
&& rel_vma
[0]->address
== vma
;
1528 if (rel
->sym_ptr_ptr
!= NULL
1529 && ! bfd_is_abs_section ((* rel
->sym_ptr_ptr
)->section
))
1532 * place
= thisplace
;
1533 return * rel
->sym_ptr_ptr
;
1539 if (vma
< rel
->address
)
1541 else if (vma
>= rel_mid
[1]->address
)
1542 rel_low
= rel_mid
+ 1;
1551 return sorted_syms
[thisplace
];
1554 /* Print an address and the offset to the nearest symbol. */
1557 objdump_print_addr_with_sym (bfd
*abfd
, asection
*sec
, asymbol
*sym
,
1558 bfd_vma vma
, struct disassemble_info
*inf
,
1563 objdump_print_value (vma
, inf
, skip_zeroes
);
1564 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, " ");
1571 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
,"<");
1572 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_symbol
, "%s",
1573 sanitize_string (bfd_section_name (sec
)));
1574 secaddr
= bfd_section_vma (sec
);
1577 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
,
1579 objdump_print_value (secaddr
- vma
, inf
, true);
1581 else if (vma
> secaddr
)
1583 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
, "+0x");
1584 objdump_print_value (vma
- secaddr
, inf
, true);
1586 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, ">");
1590 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, "<");
1592 objdump_print_symname (abfd
, inf
, sym
);
1594 if (bfd_asymbol_value (sym
) == vma
)
1596 /* Undefined symbols in an executables and dynamic objects do not have
1597 a value associated with them, so it does not make sense to display
1598 an offset relative to them. Normally we would not be provided with
1599 this kind of symbol, but the target backend might choose to do so,
1600 and the code in find_symbol_for_address might return an as yet
1601 unresolved symbol associated with a dynamic reloc. */
1602 else if ((bfd_get_file_flags (abfd
) & (EXEC_P
| DYNAMIC
))
1603 && bfd_is_und_section (sym
->section
))
1605 else if (bfd_asymbol_value (sym
) > vma
)
1607 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
,"-0x");
1608 objdump_print_value (bfd_asymbol_value (sym
) - vma
, inf
, true);
1610 else if (vma
> bfd_asymbol_value (sym
))
1612 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
, "+0x");
1613 objdump_print_value (vma
- bfd_asymbol_value (sym
), inf
, true);
1616 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, ">");
1619 if (display_file_offsets
)
1620 inf
->fprintf_styled_func (inf
->stream
, dis_style_text
,
1621 _(" (File Offset: 0x%lx)"),
1622 (long int)(sec
->filepos
+ (vma
- sec
->vma
)));
1625 /* Print an address (VMA), symbolically if possible.
1626 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
1629 objdump_print_addr (bfd_vma vma
,
1630 struct disassemble_info
*inf
,
1633 struct objdump_disasm_info
*aux
;
1634 asymbol
*sym
= NULL
;
1635 bool skip_find
= false;
1637 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1639 if (sorted_symcount
< 1)
1643 (*inf
->fprintf_func
) (inf
->stream
, "0x");
1644 objdump_print_value (vma
, inf
, skip_zeroes
);
1647 if (display_file_offsets
)
1648 inf
->fprintf_func (inf
->stream
, _(" (File Offset: 0x%lx)"),
1649 (long int) (inf
->section
->filepos
1650 + (vma
- inf
->section
->vma
)));
1654 if (aux
->reloc
!= NULL
1655 && aux
->reloc
->sym_ptr_ptr
!= NULL
1656 && * aux
->reloc
->sym_ptr_ptr
!= NULL
)
1658 sym
= * aux
->reloc
->sym_ptr_ptr
;
1660 /* Adjust the vma to the reloc. */
1661 vma
+= bfd_asymbol_value (sym
);
1663 if (bfd_is_und_section (bfd_asymbol_section (sym
)))
1668 sym
= find_symbol_for_address (vma
, inf
, NULL
);
1670 objdump_print_addr_with_sym (aux
->abfd
, inf
->section
, sym
, vma
, inf
,
1674 /* Print VMA to INFO. This function is passed to the disassembler
1678 objdump_print_address (bfd_vma vma
, struct disassemble_info
*inf
)
1680 objdump_print_addr (vma
, inf
, ! prefix_addresses
);
1683 /* Determine if the given address has a symbol associated with it. */
1686 objdump_symbol_at_address (bfd_vma vma
, struct disassemble_info
* inf
)
1690 sym
= find_symbol_for_address (vma
, inf
, NULL
);
1691 if (sym
!= NULL
&& bfd_asymbol_value (sym
) == vma
)
1697 /* Hold the last function name and the last line number we displayed
1698 in a disassembly. */
1700 static char *prev_functionname
;
1701 static unsigned int prev_line
;
1702 static unsigned int prev_discriminator
;
1704 /* We keep a list of all files that we have seen when doing a
1705 disassembly with source, so that we know how much of the file to
1706 display. This can be important for inlined functions. */
1708 struct print_file_list
1710 struct print_file_list
*next
;
1711 const char *filename
;
1712 const char *modname
;
1715 const char **linemap
;
1718 unsigned max_printed
;
1722 static struct print_file_list
*print_files
;
1724 /* The number of preceding context lines to show when we start
1725 displaying a file for the first time. */
1727 #define SHOW_PRECEDING_CONTEXT_LINES (5)
1729 /* Read a complete file into memory. */
1732 slurp_file (const char *fn
, size_t *size
, struct stat
*fst
)
1735 int ps
= getpagesize ();
1739 int fd
= open (fn
, O_RDONLY
| O_BINARY
);
1743 if (fstat (fd
, fst
) < 0)
1748 *size
= fst
->st_size
;
1750 msize
= (*size
+ ps
- 1) & ~(ps
- 1);
1751 map
= mmap (NULL
, msize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1752 if (map
!= (char *) -1L)
1758 map
= (const char *) malloc (*size
);
1759 if (!map
|| (size_t) read (fd
, (char *) map
, *size
) != *size
)
1761 free ((void *) map
);
1768 #define line_map_decrease 5
1770 /* Precompute array of lines for a mapped file. */
1772 static const char **
1773 index_file (const char *map
, size_t size
, unsigned int *maxline
)
1775 const char *p
, *lstart
, *end
;
1776 int chars_per_line
= 45; /* First iteration will use 40. */
1777 unsigned int lineno
;
1778 const char **linemap
= NULL
;
1779 unsigned long line_map_size
= 0;
1785 for (p
= map
; p
< end
; p
++)
1789 if (p
+ 1 < end
&& p
[1] == '\r')
1792 else if (*p
== '\r')
1794 if (p
+ 1 < end
&& p
[1] == '\n')
1800 /* End of line found. */
1802 if (linemap
== NULL
|| line_map_size
< lineno
+ 1)
1804 unsigned long newsize
;
1806 chars_per_line
-= line_map_decrease
;
1807 if (chars_per_line
<= 1)
1809 line_map_size
= size
/ chars_per_line
+ 1;
1810 if (line_map_size
< lineno
+ 1)
1811 line_map_size
= lineno
+ 1;
1812 newsize
= line_map_size
* sizeof (char *);
1813 linemap
= (const char **) xrealloc (linemap
, newsize
);
1816 linemap
[lineno
++] = lstart
;
1824 /* Tries to open MODNAME, and if successful adds a node to print_files
1825 linked list and returns that node. Returns NULL on failure. */
1827 static struct print_file_list
*
1828 try_print_file_open (const char *origname
, const char *modname
, struct stat
*fst
)
1830 struct print_file_list
*p
;
1832 p
= (struct print_file_list
*) xmalloc (sizeof (struct print_file_list
));
1834 p
->map
= slurp_file (modname
, &p
->mapsize
, fst
);
1841 p
->linemap
= index_file (p
->map
, p
->mapsize
, &p
->maxline
);
1844 p
->filename
= origname
;
1845 p
->modname
= modname
;
1846 p
->next
= print_files
;
1852 /* If the source file, as described in the symtab, is not found
1853 try to locate it in one of the paths specified with -I
1854 If found, add location to print_files linked list. */
1856 static struct print_file_list
*
1857 update_source_path (const char *filename
, bfd
*abfd
)
1859 struct print_file_list
*p
;
1864 p
= try_print_file_open (filename
, filename
, &fst
);
1867 if (include_path_count
== 0)
1870 /* Get the name of the file. */
1871 fname
= lbasename (filename
);
1873 /* If file exists under a new path, we need to add it to the list
1874 so that show_line knows about it. */
1875 for (i
= 0; i
< include_path_count
; i
++)
1877 char *modname
= concat (include_paths
[i
], "/", fname
,
1880 p
= try_print_file_open (filename
, modname
, &fst
);
1890 long mtime
= bfd_get_mtime (abfd
);
1892 if (fst
.st_mtime
> mtime
)
1893 warn (_("source file %s is more recent than object file\n"),
1900 /* Print a source file line. */
1903 print_line (struct print_file_list
*p
, unsigned int linenum
)
1909 if (linenum
>= p
->maxline
)
1911 l
= p
->linemap
[linenum
];
1912 if (source_comment
!= NULL
&& strlen (l
) > 0)
1913 printf ("%s", source_comment
);
1914 len
= strcspn (l
, "\n\r");
1915 /* Test fwrite return value to quiet glibc warning. */
1916 if (len
== 0 || fwrite (l
, len
, 1, stdout
) == 1)
1920 /* Print a range of source code lines. */
1923 dump_lines (struct print_file_list
*p
, unsigned int start
, unsigned int end
)
1927 while (start
<= end
)
1929 print_line (p
, start
);
1934 /* Show the line number, or the source line, in a disassembly
1938 show_line (bfd
*abfd
, asection
*section
, bfd_vma addr_offset
)
1940 const char *filename
;
1941 const char *functionname
;
1942 unsigned int linenumber
;
1943 unsigned int discriminator
;
1947 if (! with_line_numbers
&& ! with_source_code
)
1950 if (! bfd_find_nearest_line_discriminator (abfd
, section
, syms
, addr_offset
,
1951 &filename
, &functionname
,
1952 &linenumber
, &discriminator
))
1955 if (filename
!= NULL
&& *filename
== '\0')
1957 if (functionname
!= NULL
&& *functionname
== '\0')
1958 functionname
= NULL
;
1961 && IS_ABSOLUTE_PATH (filename
)
1965 const char *fname
= filename
;
1967 path
= xmalloc (prefix_length
+ 1 + strlen (filename
));
1970 memcpy (path
, prefix
, prefix_length
);
1971 path_up
= path
+ prefix_length
;
1973 /* Build relocated filename, stripping off leading directories
1974 from the initial filename if requested. */
1975 if (prefix_strip
> 0)
1980 /* Skip selected directory levels. */
1981 for (s
= fname
+ 1; *s
!= '\0' && level
< prefix_strip
; s
++)
1982 if (IS_DIR_SEPARATOR (*s
))
1989 /* Update complete filename. */
1990 strcpy (path_up
, fname
);
1998 if (with_line_numbers
)
2000 if (functionname
!= NULL
2001 && (prev_functionname
== NULL
2002 || strcmp (functionname
, prev_functionname
) != 0))
2004 char *demangle_alloc
= NULL
;
2005 if (do_demangle
&& functionname
[0] != '\0')
2007 /* Demangle the name. */
2008 demangle_alloc
= bfd_demangle (abfd
, functionname
,
2012 /* Demangling adds trailing parens, so don't print those. */
2013 if (demangle_alloc
!= NULL
)
2014 printf ("%s:\n", sanitize_string (demangle_alloc
));
2016 printf ("%s():\n", sanitize_string (functionname
));
2019 free (demangle_alloc
);
2022 && (linenumber
!= prev_line
2023 || discriminator
!= prev_discriminator
))
2025 if (discriminator
> 0)
2026 printf ("%s:%u (discriminator %u)\n",
2027 filename
== NULL
? "???" : sanitize_string (filename
),
2028 linenumber
, discriminator
);
2030 printf ("%s:%u\n", filename
== NULL
2031 ? "???" : sanitize_string (filename
),
2036 const char *filename2
;
2037 const char *functionname2
;
2040 while (bfd_find_inliner_info (abfd
, &filename2
, &functionname2
,
2043 printf ("inlined by %s:%u",
2044 sanitize_string (filename2
), line2
);
2045 printf (" (%s)\n", sanitize_string (functionname2
));
2050 if (with_source_code
2054 struct print_file_list
**pp
, *p
;
2057 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
2058 if (filename_cmp ((*pp
)->filename
, filename
) == 0)
2065 filename
= xstrdup (filename
);
2066 p
= update_source_path (filename
, abfd
);
2069 if (p
!= NULL
&& linenumber
!= p
->last_line
)
2071 if (file_start_context
&& p
->first
)
2075 l
= linenumber
- SHOW_PRECEDING_CONTEXT_LINES
;
2076 if (l
>= linenumber
)
2078 if (p
->max_printed
>= l
)
2080 if (p
->max_printed
< linenumber
)
2081 l
= p
->max_printed
+ 1;
2086 dump_lines (p
, l
, linenumber
);
2087 if (p
->max_printed
< linenumber
)
2088 p
->max_printed
= linenumber
;
2089 p
->last_line
= linenumber
;
2094 if (functionname
!= NULL
2095 && (prev_functionname
== NULL
2096 || strcmp (functionname
, prev_functionname
) != 0))
2098 if (prev_functionname
!= NULL
)
2099 free (prev_functionname
);
2100 prev_functionname
= (char *) xmalloc (strlen (functionname
) + 1);
2101 strcpy (prev_functionname
, functionname
);
2104 if (linenumber
> 0 && linenumber
!= prev_line
)
2105 prev_line
= linenumber
;
2107 if (discriminator
!= prev_discriminator
)
2108 prev_discriminator
= discriminator
;
2114 /* Pseudo FILE object for strings. */
2122 /* sprintf to a "stream". */
2124 static int ATTRIBUTE_PRINTF_2
2125 objdump_sprintf (SFILE
*f
, const char *format
, ...)
2132 size_t space
= f
->alloc
- f
->pos
;
2134 va_start (args
, format
);
2135 n
= vsnprintf (f
->buffer
+ f
->pos
, space
, format
, args
);
2141 f
->alloc
= (f
->alloc
+ n
) * 2;
2142 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2149 /* Return an integer greater than, or equal to zero, representing the color
2150 for STYLE, or -1 if no color should be used. */
2153 objdump_color_for_disassembler_style (enum disassembler_style style
)
2157 if (style
== dis_style_comment_start
)
2158 disassembler_in_comment
= true;
2160 if (disassembler_color
)
2162 if (disassembler_in_comment
)
2167 case dis_style_symbol
: color
= 32; break;
2168 case dis_style_assembler_directive
:
2169 case dis_style_mnemonic
: color
= 33; break;
2170 case dis_style_register
: color
= 34; break;
2171 case dis_style_address
:
2172 case dis_style_address_offset
:
2173 case dis_style_immediate
: color
= 35; break;
2175 case dis_style_text
: color
= -1; break;
2178 else if (disassembler_extended_color
)
2180 if (disassembler_in_comment
)
2185 case dis_style_symbol
: color
= 40; break;
2186 case dis_style_assembler_directive
:
2187 case dis_style_mnemonic
: color
= 142; break;
2188 case dis_style_register
: color
= 27; break;
2189 case dis_style_address
:
2190 case dis_style_address_offset
:
2191 case dis_style_immediate
: color
= 134; break;
2193 case dis_style_text
: color
= -1; break;
2200 /* Like objdump_sprintf, but add in escape sequences to highlight the
2201 content according to STYLE. */
2203 static int ATTRIBUTE_PRINTF_3
2204 objdump_styled_sprintf (SFILE
*f
, enum disassembler_style style
,
2205 const char *format
, ...)
2209 int color
= objdump_color_for_disassembler_style (style
);
2215 size_t space
= f
->alloc
- f
->pos
;
2217 if (disassembler_color
)
2218 n
= snprintf (f
->buffer
+ f
->pos
, space
, "\033[%dm", color
);
2220 n
= snprintf (f
->buffer
+ f
->pos
, space
, "\033[38;5;%dm", color
);
2224 f
->alloc
= (f
->alloc
+ n
) * 2;
2225 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2232 size_t space
= f
->alloc
- f
->pos
;
2234 va_start (args
, format
);
2235 n
= vsnprintf (f
->buffer
+ f
->pos
, space
, format
, args
);
2241 f
->alloc
= (f
->alloc
+ n
) * 2;
2242 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2250 size_t space
= f
->alloc
- f
->pos
;
2252 n
= snprintf (f
->buffer
+ f
->pos
, space
, "\033[0m");
2257 f
->alloc
= (f
->alloc
+ n
) * 2;
2258 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2266 /* We discard the styling information here. This function is only used
2267 when objdump is printing auxiliary information, the symbol headers, and
2268 disassembly address, or the bytes of the disassembled instruction. We
2269 don't (currently) apply styling to any of this stuff, so, for now, just
2270 print the content with no additional style added. */
2272 static int ATTRIBUTE_PRINTF_3
2273 fprintf_styled (FILE *f
, enum disassembler_style style ATTRIBUTE_UNUSED
,
2274 const char *fmt
, ...)
2280 res
= vfprintf (f
, fmt
, ap
);
2286 /* Code for generating (colored) diagrams of control flow start and end
2289 /* Structure used to store the properties of a jump. */
2293 /* The next jump, or NULL if this is the last object. */
2294 struct jump_info
*next
;
2295 /* The previous jump, or NULL if this is the first object. */
2296 struct jump_info
*prev
;
2297 /* The start addresses of the jump. */
2300 /* The list of start addresses. */
2302 /* The number of elements. */
2304 /* The maximum number of elements that fit into the array. */
2307 /* The end address of the jump. */
2309 /* The drawing level of the jump. */
2313 /* Construct a jump object for a jump from start
2314 to end with the corresponding level. */
2316 static struct jump_info
*
2317 jump_info_new (bfd_vma start
, bfd_vma end
, int level
)
2319 struct jump_info
*result
= xmalloc (sizeof (struct jump_info
));
2321 result
->next
= NULL
;
2322 result
->prev
= NULL
;
2323 result
->start
.addresses
= xmalloc (sizeof (bfd_vma
*) * 2);
2324 result
->start
.addresses
[0] = start
;
2325 result
->start
.count
= 1;
2326 result
->start
.max_count
= 2;
2328 result
->level
= level
;
2333 /* Free a jump object and return the next object
2334 or NULL if this was the last one. */
2336 static struct jump_info
*
2337 jump_info_free (struct jump_info
*ji
)
2339 struct jump_info
*result
= NULL
;
2344 if (ji
->start
.addresses
)
2345 free (ji
->start
.addresses
);
2352 /* Get the smallest value of all start and end addresses. */
2355 jump_info_min_address (const struct jump_info
*ji
)
2357 bfd_vma min_address
= ji
->end
;
2360 for (i
= ji
->start
.count
; i
-- > 0;)
2361 if (ji
->start
.addresses
[i
] < min_address
)
2362 min_address
= ji
->start
.addresses
[i
];
2366 /* Get the largest value of all start and end addresses. */
2369 jump_info_max_address (const struct jump_info
*ji
)
2371 bfd_vma max_address
= ji
->end
;
2374 for (i
= ji
->start
.count
; i
-- > 0;)
2375 if (ji
->start
.addresses
[i
] > max_address
)
2376 max_address
= ji
->start
.addresses
[i
];
2380 /* Get the target address of a jump. */
2383 jump_info_end_address (const struct jump_info
*ji
)
2388 /* Test if an address is one of the start addresses of a jump. */
2391 jump_info_is_start_address (const struct jump_info
*ji
, bfd_vma address
)
2393 bool result
= false;
2396 for (i
= ji
->start
.count
; i
-- > 0;)
2397 if (address
== ji
->start
.addresses
[i
])
2406 /* Test if an address is the target address of a jump. */
2409 jump_info_is_end_address (const struct jump_info
*ji
, bfd_vma address
)
2411 return (address
== ji
->end
);
2414 /* Get the difference between the smallest and largest address of a jump. */
2417 jump_info_size (const struct jump_info
*ji
)
2419 return jump_info_max_address (ji
) - jump_info_min_address (ji
);
2422 /* Unlink a jump object from a list. */
2425 jump_info_unlink (struct jump_info
*node
,
2426 struct jump_info
**base
)
2429 node
->next
->prev
= node
->prev
;
2431 node
->prev
->next
= node
->next
;
2438 /* Insert unlinked jump info node into a list. */
2441 jump_info_insert (struct jump_info
*node
,
2442 struct jump_info
*target
,
2443 struct jump_info
**base
)
2445 node
->next
= target
;
2446 node
->prev
= target
->prev
;
2447 target
->prev
= node
;
2449 node
->prev
->next
= node
;
2454 /* Add unlinked node to the front of a list. */
2457 jump_info_add_front (struct jump_info
*node
,
2458 struct jump_info
**base
)
2462 node
->next
->prev
= node
;
2467 /* Move linked node to target position. */
2470 jump_info_move_linked (struct jump_info
*node
,
2471 struct jump_info
*target
,
2472 struct jump_info
**base
)
2475 jump_info_unlink (node
, base
);
2476 /* Insert node at target position. */
2477 jump_info_insert (node
, target
, base
);
2480 /* Test if two jumps intersect. */
2483 jump_info_intersect (const struct jump_info
*a
,
2484 const struct jump_info
*b
)
2486 return ((jump_info_max_address (a
) >= jump_info_min_address (b
))
2487 && (jump_info_min_address (a
) <= jump_info_max_address (b
)));
2490 /* Merge two compatible jump info objects. */
2493 jump_info_merge (struct jump_info
**base
)
2495 struct jump_info
*a
;
2497 for (a
= *base
; a
; a
= a
->next
)
2499 struct jump_info
*b
;
2501 for (b
= a
->next
; b
; b
= b
->next
)
2503 /* Merge both jumps into one. */
2504 if (a
->end
== b
->end
)
2506 /* Reallocate addresses. */
2507 size_t needed_size
= a
->start
.count
+ b
->start
.count
;
2510 if (needed_size
> a
->start
.max_count
)
2512 a
->start
.max_count
+= b
->start
.max_count
;
2513 a
->start
.addresses
=
2514 xrealloc (a
->start
.addresses
,
2515 a
->start
.max_count
* sizeof (bfd_vma
*));
2518 /* Append start addresses. */
2519 for (i
= 0; i
< b
->start
.count
; ++i
)
2520 a
->start
.addresses
[a
->start
.count
++] =
2521 b
->start
.addresses
[i
];
2523 /* Remove and delete jump. */
2524 struct jump_info
*tmp
= b
->prev
;
2525 jump_info_unlink (b
, base
);
2533 /* Sort jumps by their size and starting point using a stable
2534 minsort. This could be improved if sorting performance is
2535 an issue, for example by using mergesort. */
2538 jump_info_sort (struct jump_info
**base
)
2540 struct jump_info
*current_element
= *base
;
2542 while (current_element
)
2544 struct jump_info
*best_match
= current_element
;
2545 struct jump_info
*runner
= current_element
->next
;
2546 bfd_vma best_size
= jump_info_size (best_match
);
2550 bfd_vma runner_size
= jump_info_size (runner
);
2552 if ((runner_size
< best_size
)
2553 || ((runner_size
== best_size
)
2554 && (jump_info_min_address (runner
)
2555 < jump_info_min_address (best_match
))))
2557 best_match
= runner
;
2558 best_size
= runner_size
;
2561 runner
= runner
->next
;
2564 if (best_match
== current_element
)
2565 current_element
= current_element
->next
;
2567 jump_info_move_linked (best_match
, current_element
, base
);
2571 /* Visualize all jumps at a given address. */
2574 jump_info_visualize_address (bfd_vma address
,
2577 uint8_t *color_buffer
)
2579 struct jump_info
*ji
= detected_jumps
;
2580 size_t len
= (max_level
+ 1) * 3;
2582 /* Clear line buffer. */
2583 memset (line_buffer
, ' ', len
);
2584 memset (color_buffer
, 0, len
);
2586 /* Iterate over jumps and add their ASCII art. */
2589 /* Discard jumps that are never needed again. */
2590 if (jump_info_max_address (ji
) < address
)
2592 struct jump_info
*tmp
= ji
;
2595 jump_info_unlink (tmp
, &detected_jumps
);
2596 jump_info_free (tmp
);
2600 /* This jump intersects with the current address. */
2601 if (jump_info_min_address (ji
) <= address
)
2603 /* Hash target address to get an even
2604 distribution between all values. */
2605 bfd_vma hash_address
= jump_info_end_address (ji
);
2606 uint8_t color
= iterative_hash_object (hash_address
, 0);
2607 /* Fetch line offset. */
2608 int offset
= (max_level
- ji
->level
) * 3;
2610 /* Draw start line. */
2611 if (jump_info_is_start_address (ji
, address
))
2613 size_t i
= offset
+ 1;
2615 for (; i
< len
- 1; ++i
)
2616 if (line_buffer
[i
] == ' ')
2618 line_buffer
[i
] = '-';
2619 color_buffer
[i
] = color
;
2622 if (line_buffer
[i
] == ' ')
2624 line_buffer
[i
] = '-';
2625 color_buffer
[i
] = color
;
2627 else if (line_buffer
[i
] == '>')
2629 line_buffer
[i
] = 'X';
2630 color_buffer
[i
] = color
;
2633 if (line_buffer
[offset
] == ' ')
2635 if (address
<= ji
->end
)
2636 line_buffer
[offset
] =
2637 (jump_info_min_address (ji
) == address
) ? '/': '+';
2639 line_buffer
[offset
] =
2640 (jump_info_max_address (ji
) == address
) ? '\\': '+';
2641 color_buffer
[offset
] = color
;
2644 /* Draw jump target. */
2645 else if (jump_info_is_end_address (ji
, address
))
2647 size_t i
= offset
+ 1;
2649 for (; i
< len
- 1; ++i
)
2650 if (line_buffer
[i
] == ' ')
2652 line_buffer
[i
] = '-';
2653 color_buffer
[i
] = color
;
2656 if (line_buffer
[i
] == ' ')
2658 line_buffer
[i
] = '>';
2659 color_buffer
[i
] = color
;
2661 else if (line_buffer
[i
] == '-')
2663 line_buffer
[i
] = 'X';
2664 color_buffer
[i
] = color
;
2667 if (line_buffer
[offset
] == ' ')
2669 if (jump_info_min_address (ji
) < address
)
2670 line_buffer
[offset
] =
2671 (jump_info_max_address (ji
) > address
) ? '>' : '\\';
2673 line_buffer
[offset
] = '/';
2674 color_buffer
[offset
] = color
;
2677 /* Draw intermediate line segment. */
2678 else if (line_buffer
[offset
] == ' ')
2680 line_buffer
[offset
] = '|';
2681 color_buffer
[offset
] = color
;
2689 /* Clone of disassemble_bytes to detect jumps inside a function. */
2690 /* FIXME: is this correct? Can we strip it down even further? */
2692 static struct jump_info
*
2693 disassemble_jumps (struct disassemble_info
* inf
,
2694 disassembler_ftype disassemble_fn
,
2695 bfd_vma start_offset
,
2696 bfd_vma stop_offset
,
2699 arelent
** relppend
)
2701 struct objdump_disasm_info
*aux
;
2702 struct jump_info
*jumps
= NULL
;
2704 bfd_vma addr_offset
;
2705 unsigned int opb
= inf
->octets_per_byte
;
2709 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
2710 section
= inf
->section
;
2713 sfile
.buffer
= (char *) xmalloc (sfile
.alloc
);
2716 inf
->insn_info_valid
= 0;
2717 disassemble_set_printf (inf
, &sfile
, (fprintf_ftype
) objdump_sprintf
,
2718 (fprintf_styled_ftype
) objdump_styled_sprintf
);
2720 addr_offset
= start_offset
;
2721 while (addr_offset
< stop_offset
)
2723 int previous_octets
;
2725 /* Remember the length of the previous instruction. */
2726 previous_octets
= octets
;
2730 inf
->bytes_per_line
= 0;
2731 inf
->bytes_per_chunk
= 0;
2732 inf
->flags
= ((disassemble_all
? DISASSEMBLE_DATA
: 0)
2733 | (wide_output
? WIDE_OUTPUT
: 0));
2735 inf
->flags
|= USER_SPECIFIED_MACHINE_TYPE
;
2737 if (inf
->disassembler_needs_relocs
2738 && (bfd_get_file_flags (aux
->abfd
) & EXEC_P
) == 0
2739 && (bfd_get_file_flags (aux
->abfd
) & DYNAMIC
) == 0
2740 && *relppp
< relppend
)
2742 bfd_signed_vma distance_to_rel
;
2744 distance_to_rel
= (**relppp
)->address
- (rel_offset
+ addr_offset
);
2746 /* Check to see if the current reloc is associated with
2747 the instruction that we are about to disassemble. */
2748 if (distance_to_rel
== 0
2749 /* FIXME: This is wrong. We are trying to catch
2750 relocs that are addressed part way through the
2751 current instruction, as might happen with a packed
2752 VLIW instruction. Unfortunately we do not know the
2753 length of the current instruction since we have not
2754 disassembled it yet. Instead we take a guess based
2755 upon the length of the previous instruction. The
2756 proper solution is to have a new target-specific
2757 disassembler function which just returns the length
2758 of an instruction at a given address without trying
2759 to display its disassembly. */
2760 || (distance_to_rel
> 0
2761 && distance_to_rel
< (bfd_signed_vma
) (previous_octets
/ opb
)))
2763 inf
->flags
|= INSN_HAS_RELOC
;
2767 if (! disassemble_all
2768 && (section
->flags
& (SEC_CODE
| SEC_HAS_CONTENTS
))
2769 == (SEC_CODE
| SEC_HAS_CONTENTS
))
2770 /* Set a stop_vma so that the disassembler will not read
2771 beyond the next symbol. We assume that symbols appear on
2772 the boundaries between instructions. We only do this when
2773 disassembling code of course, and when -D is in effect. */
2774 inf
->stop_vma
= section
->vma
+ stop_offset
;
2776 inf
->stop_offset
= stop_offset
;
2778 /* Extract jump information. */
2779 inf
->insn_info_valid
= 0;
2780 disassembler_in_comment
= false;
2781 octets
= (*disassemble_fn
) (section
->vma
+ addr_offset
, inf
);
2782 /* Test if a jump was detected. */
2783 if (inf
->insn_info_valid
2784 && ((inf
->insn_type
== dis_branch
)
2785 || (inf
->insn_type
== dis_condbranch
)
2786 || (inf
->insn_type
== dis_jsr
)
2787 || (inf
->insn_type
== dis_condjsr
))
2788 && (inf
->target
>= section
->vma
+ start_offset
)
2789 && (inf
->target
< section
->vma
+ stop_offset
))
2791 struct jump_info
*ji
=
2792 jump_info_new (section
->vma
+ addr_offset
, inf
->target
, -1);
2793 jump_info_add_front (ji
, &jumps
);
2798 addr_offset
+= octets
/ opb
;
2801 disassemble_set_printf (inf
, (void *) stdout
, (fprintf_ftype
) fprintf
,
2802 (fprintf_styled_ftype
) fprintf_styled
);
2803 free (sfile
.buffer
);
2806 jump_info_merge (&jumps
);
2807 /* Process jumps. */
2808 jump_info_sort (&jumps
);
2810 /* Group jumps by level. */
2811 struct jump_info
*last_jump
= jumps
;
2816 /* The last jump is part of the next group. */
2817 struct jump_info
*base
= last_jump
;
2818 /* Increment level. */
2819 base
->level
= ++max_level
;
2821 /* Find jumps that can be combined on the same
2822 level, with the largest jumps tested first.
2823 This has the advantage that large jumps are on
2824 lower levels and do not intersect with small
2825 jumps that get grouped on higher levels. */
2826 struct jump_info
*exchange_item
= last_jump
->next
;
2827 struct jump_info
*it
= exchange_item
;
2829 for (; it
; it
= it
->next
)
2831 /* Test if the jump intersects with any
2832 jump from current group. */
2834 struct jump_info
*it_collision
;
2836 for (it_collision
= base
;
2837 it_collision
!= exchange_item
;
2838 it_collision
= it_collision
->next
)
2840 /* This jump intersects so we leave it out. */
2841 if (jump_info_intersect (it_collision
, it
))
2848 /* Add jump to group. */
2851 /* Move current element to the front. */
2852 if (it
!= exchange_item
)
2854 struct jump_info
*save
= it
->prev
;
2855 jump_info_move_linked (it
, exchange_item
, &jumps
);
2861 last_jump
= exchange_item
;
2862 exchange_item
= exchange_item
->next
;
2864 last_jump
->level
= max_level
;
2868 /* Move to next group. */
2869 last_jump
= exchange_item
;
2875 /* The number of zeroes we want to see before we start skipping them.
2876 The number is arbitrarily chosen. */
2878 #define DEFAULT_SKIP_ZEROES 8
2880 /* The number of zeroes to skip at the end of a section. If the
2881 number of zeroes at the end is between SKIP_ZEROES_AT_END and
2882 SKIP_ZEROES, they will be disassembled. If there are fewer than
2883 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
2884 attempt to avoid disassembling zeroes inserted by section
2887 #define DEFAULT_SKIP_ZEROES_AT_END 3
2890 null_print (const void * stream ATTRIBUTE_UNUSED
, const char * format ATTRIBUTE_UNUSED
, ...)
2895 /* Like null_print, but takes the extra STYLE argument. As this is not
2896 going to print anything, the extra argument is just ignored. */
2899 null_styled_print (const void * stream ATTRIBUTE_UNUSED
,
2900 enum disassembler_style style ATTRIBUTE_UNUSED
,
2901 const char * format ATTRIBUTE_UNUSED
, ...)
2906 /* Print out jump visualization. */
2909 print_jump_visualisation (bfd_vma addr
, int max_level
, char *line_buffer
,
2910 uint8_t *color_buffer
)
2915 jump_info_visualize_address (addr
, max_level
, line_buffer
, color_buffer
);
2917 size_t line_buffer_size
= strlen (line_buffer
);
2918 char last_color
= 0;
2921 for (i
= 0; i
<= line_buffer_size
; ++i
)
2925 uint8_t color
= (i
< line_buffer_size
) ? color_buffer
[i
]: 0;
2927 if (color
!= last_color
)
2930 if (extended_color_output
)
2931 /* Use extended 8bit color, but
2932 do not choose dark colors. */
2933 printf ("\033[38;5;%dm", 124 + (color
% 108));
2935 /* Use simple terminal colors. */
2936 printf ("\033[%dm", 31 + (color
% 7));
2943 putchar ((i
< line_buffer_size
) ? line_buffer
[i
]: ' ');
2947 /* Disassemble some data in memory between given values. */
2950 disassemble_bytes (struct disassemble_info
*inf
,
2951 disassembler_ftype disassemble_fn
,
2954 bfd_vma start_offset
,
2955 bfd_vma stop_offset
,
2960 struct objdump_disasm_info
*aux
;
2962 unsigned int octets_per_line
;
2963 unsigned int skip_addr_chars
;
2964 bfd_vma addr_offset
;
2965 unsigned int opb
= inf
->octets_per_byte
;
2966 unsigned int skip_zeroes
= inf
->skip_zeroes
;
2967 unsigned int skip_zeroes_at_end
= inf
->skip_zeroes_at_end
;
2971 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
2972 section
= inf
->section
;
2975 sfile
.buffer
= (char *) xmalloc (sfile
.alloc
);
2979 octets_per_line
= insn_width
;
2981 octets_per_line
= 4;
2983 octets_per_line
= 16;
2985 /* Figure out how many characters to skip at the start of an
2986 address, to make the disassembly look nicer. We discard leading
2987 zeroes in chunks of 4, ensuring that there is always a leading
2989 skip_addr_chars
= 0;
2990 if (!no_addresses
&& !prefix_addresses
)
2994 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ section
->size
/ opb
);
2996 while (buf
[skip_addr_chars
] == '0')
2999 /* Don't discard zeros on overflow. */
3000 if (buf
[skip_addr_chars
] == '\0' && section
->vma
!= 0)
3001 skip_addr_chars
= 0;
3003 if (skip_addr_chars
!= 0)
3004 skip_addr_chars
= (skip_addr_chars
- 1) & -4;
3007 inf
->insn_info_valid
= 0;
3009 /* Determine maximum level. */
3010 uint8_t *color_buffer
= NULL
;
3011 char *line_buffer
= NULL
;
3014 /* Some jumps were detected. */
3017 struct jump_info
*ji
;
3019 /* Find maximum jump level. */
3020 for (ji
= detected_jumps
; ji
; ji
= ji
->next
)
3022 if (ji
->level
> max_level
)
3023 max_level
= ji
->level
;
3026 /* Allocate buffers. */
3027 size_t len
= (max_level
+ 1) * 3 + 1;
3028 line_buffer
= xmalloc (len
);
3029 line_buffer
[len
- 1] = 0;
3030 color_buffer
= xmalloc (len
);
3031 color_buffer
[len
- 1] = 0;
3034 addr_offset
= start_offset
;
3035 while (addr_offset
< stop_offset
)
3037 bool need_nl
= false;
3041 /* Make sure we don't use relocs from previous instructions. */
3044 /* If we see more than SKIP_ZEROES octets of zeroes, we just
3046 if (! disassemble_zeroes
)
3047 for (; addr_offset
* opb
+ octets
< stop_offset
* opb
; octets
++)
3048 if (data
[addr_offset
* opb
+ octets
] != 0)
3050 if (! disassemble_zeroes
3051 && (inf
->insn_info_valid
== 0
3052 || inf
->branch_delay_insns
== 0)
3053 && (octets
>= skip_zeroes
3054 || (addr_offset
* opb
+ octets
== stop_offset
* opb
3055 && octets
< skip_zeroes_at_end
)))
3057 /* If there are more nonzero octets to follow, we only skip
3058 zeroes in multiples of 4, to try to avoid running over
3059 the start of an instruction which happens to start with
3061 if (addr_offset
* opb
+ octets
!= stop_offset
* opb
)
3064 /* If we are going to display more data, and we are displaying
3065 file offsets, then tell the user how many zeroes we skip
3066 and the file offset from where we resume dumping. */
3067 if (display_file_offsets
3068 && addr_offset
+ octets
/ opb
< stop_offset
)
3069 printf (_("\t... (skipping %lu zeroes, "
3070 "resuming at file offset: 0x%lx)\n"),
3071 (unsigned long) (octets
/ opb
),
3072 (unsigned long) (section
->filepos
3073 + addr_offset
+ octets
/ opb
));
3080 unsigned int bpc
= 0;
3081 unsigned int pb
= 0;
3083 if (with_line_numbers
|| with_source_code
)
3084 show_line (aux
->abfd
, section
, addr_offset
);
3088 else if (!prefix_addresses
)
3092 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ addr_offset
);
3093 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
3097 printf ("%s:\t", buf
+ skip_addr_chars
);
3101 aux
->require_sec
= true;
3102 objdump_print_address (section
->vma
+ addr_offset
, inf
);
3103 aux
->require_sec
= false;
3107 print_jump_visualisation (section
->vma
+ addr_offset
,
3108 max_level
, line_buffer
,
3116 disassemble_set_printf
3117 (inf
, &sfile
, (fprintf_ftype
) objdump_sprintf
,
3118 (fprintf_styled_ftype
) objdump_styled_sprintf
);
3119 inf
->bytes_per_line
= 0;
3120 inf
->bytes_per_chunk
= 0;
3121 inf
->flags
= ((disassemble_all
? DISASSEMBLE_DATA
: 0)
3122 | (wide_output
? WIDE_OUTPUT
: 0));
3124 inf
->flags
|= USER_SPECIFIED_MACHINE_TYPE
;
3126 if (inf
->disassembler_needs_relocs
3127 && (bfd_get_file_flags (aux
->abfd
) & EXEC_P
) == 0
3128 && (bfd_get_file_flags (aux
->abfd
) & DYNAMIC
) == 0
3129 && *relppp
< relppend
)
3131 bfd_signed_vma distance_to_rel
;
3132 int max_reloc_offset
3133 = aux
->abfd
->arch_info
->max_reloc_offset_into_insn
;
3135 distance_to_rel
= ((**relppp
)->address
- rel_offset
3139 if (distance_to_rel
> 0
3140 && (max_reloc_offset
< 0
3141 || distance_to_rel
<= max_reloc_offset
))
3143 /* This reloc *might* apply to the current insn,
3144 starting somewhere inside it. Discover the length
3145 of the current insn so that the check below will
3148 insn_size
= insn_width
;
3151 /* We find the length by calling the dissassembler
3152 function with a dummy print handler. This should
3153 work unless the disassembler is not expecting to
3154 be called multiple times for the same address.
3156 This does mean disassembling the instruction
3157 twice, but we only do this when there is a high
3158 probability that there is a reloc that will
3159 affect the instruction. */
3160 disassemble_set_printf
3161 (inf
, inf
->stream
, (fprintf_ftype
) null_print
,
3162 (fprintf_styled_ftype
) null_styled_print
);
3163 insn_size
= disassemble_fn (section
->vma
3164 + addr_offset
, inf
);
3165 disassemble_set_printf
3167 (fprintf_ftype
) objdump_sprintf
,
3168 (fprintf_styled_ftype
) objdump_styled_sprintf
);
3172 /* Check to see if the current reloc is associated with
3173 the instruction that we are about to disassemble. */
3174 if (distance_to_rel
== 0
3175 || (distance_to_rel
> 0
3176 && distance_to_rel
< insn_size
/ (int) opb
))
3178 inf
->flags
|= INSN_HAS_RELOC
;
3179 aux
->reloc
= **relppp
;
3183 if (! disassemble_all
3184 && ((section
->flags
& (SEC_CODE
| SEC_HAS_CONTENTS
))
3185 == (SEC_CODE
| SEC_HAS_CONTENTS
)))
3186 /* Set a stop_vma so that the disassembler will not read
3187 beyond the next symbol. We assume that symbols appear on
3188 the boundaries between instructions. We only do this when
3189 disassembling code of course, and when -D is in effect. */
3190 inf
->stop_vma
= section
->vma
+ stop_offset
;
3192 inf
->stop_offset
= stop_offset
;
3193 disassembler_in_comment
= false;
3194 insn_size
= (*disassemble_fn
) (section
->vma
+ addr_offset
, inf
);
3198 disassemble_set_printf (inf
, stdout
, (fprintf_ftype
) fprintf
,
3199 (fprintf_styled_ftype
) fprintf_styled
);
3200 if (insn_width
== 0 && inf
->bytes_per_line
!= 0)
3201 octets_per_line
= inf
->bytes_per_line
;
3202 if (insn_size
< (int) opb
)
3205 printf ("%s\n", sfile
.buffer
);
3208 non_fatal (_("disassemble_fn returned length %d"),
3219 octets
= octets_per_line
;
3220 if (addr_offset
+ octets
/ opb
> stop_offset
)
3221 octets
= (stop_offset
- addr_offset
) * opb
;
3223 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ octets
; ++j
)
3225 if (ISPRINT (data
[j
]))
3226 buf
[j
- addr_offset
* opb
] = data
[j
];
3228 buf
[j
- addr_offset
* opb
] = '.';
3230 buf
[j
- addr_offset
* opb
] = '\0';
3233 if (prefix_addresses
3235 : show_raw_insn
>= 0)
3239 /* If ! prefix_addresses and ! wide_output, we print
3240 octets_per_line octets per line. */
3242 if (pb
> octets_per_line
&& ! prefix_addresses
&& ! wide_output
)
3243 pb
= octets_per_line
;
3245 if (inf
->bytes_per_chunk
)
3246 bpc
= inf
->bytes_per_chunk
;
3250 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
3252 /* PR 21580: Check for a buffer ending early. */
3253 if (j
+ bpc
<= stop_offset
* opb
)
3257 if (inf
->display_endian
== BFD_ENDIAN_LITTLE
)
3259 for (k
= bpc
; k
-- != 0; )
3260 printf ("%02x", (unsigned) data
[j
+ k
]);
3264 for (k
= 0; k
< bpc
; k
++)
3265 printf ("%02x", (unsigned) data
[j
+ k
]);
3271 for (; pb
< octets_per_line
; pb
+= bpc
)
3275 for (k
= 0; k
< bpc
; k
++)
3280 /* Separate raw data from instruction by extra space. */
3290 printf ("%s", sfile
.buffer
);
3292 if (prefix_addresses
3294 : show_raw_insn
>= 0)
3302 j
= addr_offset
* opb
+ pb
;
3308 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ j
/ opb
);
3309 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
3313 printf ("%s:\t", buf
+ skip_addr_chars
);
3316 print_jump_visualisation (section
->vma
+ j
/ opb
,
3317 max_level
, line_buffer
,
3320 pb
+= octets_per_line
;
3323 for (; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
3325 /* PR 21619: Check for a buffer ending early. */
3326 if (j
+ bpc
<= stop_offset
* opb
)
3330 if (inf
->display_endian
== BFD_ENDIAN_LITTLE
)
3332 for (k
= bpc
; k
-- != 0; )
3333 printf ("%02x", (unsigned) data
[j
+ k
]);
3337 for (k
= 0; k
< bpc
; k
++)
3338 printf ("%02x", (unsigned) data
[j
+ k
]);
3352 while ((*relppp
) < relppend
3353 && (**relppp
)->address
< rel_offset
+ addr_offset
+ octets
/ opb
)
3355 if (dump_reloc_info
|| dump_dynamic_reloc_info
)
3368 objdump_print_value (section
->vma
- rel_offset
+ q
->address
,
3373 if (q
->howto
== NULL
)
3374 printf ("*unknown*\t");
3375 else if (q
->howto
->name
)
3376 printf ("%s\t", q
->howto
->name
);
3378 printf ("%d\t", q
->howto
->type
);
3380 if (q
->sym_ptr_ptr
== NULL
|| *q
->sym_ptr_ptr
== NULL
)
3381 printf ("*unknown*");
3384 const char *sym_name
;
3386 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
3387 if (sym_name
!= NULL
&& *sym_name
!= '\0')
3388 objdump_print_symname (aux
->abfd
, inf
, *q
->sym_ptr_ptr
);
3393 sym_sec
= bfd_asymbol_section (*q
->sym_ptr_ptr
);
3394 sym_name
= bfd_section_name (sym_sec
);
3395 if (sym_name
== NULL
|| *sym_name
== '\0')
3396 sym_name
= "*unknown*";
3397 printf ("%s", sanitize_string (sym_name
));
3403 bfd_vma addend
= q
->addend
;
3404 if ((bfd_signed_vma
) addend
< 0)
3411 objdump_print_value (addend
, inf
, true);
3423 addr_offset
+= octets
/ opb
;
3426 free (sfile
.buffer
);
3428 free (color_buffer
);
3432 disassemble_section (bfd
*abfd
, asection
*section
, void *inf
)
3434 const struct elf_backend_data
*bed
;
3435 bfd_vma sign_adjust
= 0;
3436 struct disassemble_info
*pinfo
= (struct disassemble_info
*) inf
;
3437 struct objdump_disasm_info
*paux
;
3438 unsigned int opb
= pinfo
->octets_per_byte
;
3439 bfd_byte
*data
= NULL
;
3440 bfd_size_type datasize
= 0;
3441 arelent
**rel_pp
= NULL
;
3442 arelent
**rel_ppstart
= NULL
;
3443 arelent
**rel_ppend
;
3444 bfd_vma stop_offset
;
3445 asymbol
*sym
= NULL
;
3449 unsigned long addr_offset
;
3453 stop_offset_reached
,
3458 /* Sections that do not contain machine
3459 code are not normally disassembled. */
3460 if (! disassemble_all
3461 && only_list
== NULL
3462 && ((section
->flags
& (SEC_CODE
| SEC_HAS_CONTENTS
))
3463 != (SEC_CODE
| SEC_HAS_CONTENTS
)))
3466 if (! process_section_p (section
))
3469 datasize
= bfd_section_size (section
);
3473 if (start_address
== (bfd_vma
) -1
3474 || start_address
< section
->vma
)
3477 addr_offset
= start_address
- section
->vma
;
3479 if (stop_address
== (bfd_vma
) -1)
3480 stop_offset
= datasize
/ opb
;
3483 if (stop_address
< section
->vma
)
3486 stop_offset
= stop_address
- section
->vma
;
3487 if (stop_offset
> datasize
/ opb
)
3488 stop_offset
= datasize
/ opb
;
3491 if (addr_offset
>= stop_offset
)
3494 /* Decide which set of relocs to use. Load them if necessary. */
3495 paux
= (struct objdump_disasm_info
*) pinfo
->application_data
;
3496 if (pinfo
->dynrelbuf
&& dump_dynamic_reloc_info
)
3498 rel_pp
= pinfo
->dynrelbuf
;
3499 rel_count
= pinfo
->dynrelcount
;
3500 /* Dynamic reloc addresses are absolute, non-dynamic are section
3501 relative. REL_OFFSET specifies the reloc address corresponding
3502 to the start of this section. */
3503 rel_offset
= section
->vma
;
3511 if ((section
->flags
& SEC_RELOC
) != 0
3512 && (dump_reloc_info
|| pinfo
->disassembler_needs_relocs
))
3516 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
3518 bfd_fatal (bfd_get_filename (abfd
));
3522 rel_ppstart
= rel_pp
= (arelent
**) xmalloc (relsize
);
3523 rel_count
= bfd_canonicalize_reloc (abfd
, section
, rel_pp
, syms
);
3525 bfd_fatal (bfd_get_filename (abfd
));
3527 /* Sort the relocs by address. */
3528 qsort (rel_pp
, rel_count
, sizeof (arelent
*), compare_relocs
);
3532 rel_ppend
= PTR_ADD (rel_pp
, rel_count
);
3534 if (!bfd_malloc_and_get_section (abfd
, section
, &data
))
3536 non_fatal (_("Reading section %s failed because: %s"),
3537 section
->name
, bfd_errmsg (bfd_get_error ()));
3541 pinfo
->buffer
= data
;
3542 pinfo
->buffer_vma
= section
->vma
;
3543 pinfo
->buffer_length
= datasize
;
3544 pinfo
->section
= section
;
3546 /* Sort the symbols into value and section order. */
3547 compare_section
= section
;
3548 if (sorted_symcount
> 1)
3549 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
3551 /* Skip over the relocs belonging to addresses below the
3553 while (rel_pp
< rel_ppend
3554 && (*rel_pp
)->address
< rel_offset
+ addr_offset
)
3557 printf (_("\nDisassembly of section %s:\n"), sanitize_string (section
->name
));
3559 /* Find the nearest symbol forwards from our current position. */
3560 paux
->require_sec
= true;
3561 sym
= (asymbol
*) find_symbol_for_address (section
->vma
+ addr_offset
,
3562 (struct disassemble_info
*) inf
,
3564 paux
->require_sec
= false;
3566 /* PR 9774: If the target used signed addresses then we must make
3567 sure that we sign extend the value that we calculate for 'addr'
3568 in the loop below. */
3569 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3570 && (bed
= get_elf_backend_data (abfd
)) != NULL
3571 && bed
->sign_extend_vma
)
3572 sign_adjust
= (bfd_vma
) 1 << (bed
->s
->arch_size
- 1);
3574 /* Disassemble a block of instructions up to the address associated with
3575 the symbol we have just found. Then print the symbol and find the
3576 next symbol on. Repeat until we have disassembled the entire section
3577 or we have reached the end of the address range we are interested in. */
3578 do_print
= paux
->symbol
== NULL
;
3579 loop_until
= stop_offset_reached
;
3581 while (addr_offset
< stop_offset
)
3585 bfd_vma nextstop_offset
;
3588 addr
= section
->vma
+ addr_offset
;
3589 addr
= ((addr
& ((sign_adjust
<< 1) - 1)) ^ sign_adjust
) - sign_adjust
;
3591 if (sym
!= NULL
&& bfd_asymbol_value (sym
) <= addr
)
3596 (x
< sorted_symcount
3597 && (bfd_asymbol_value (sorted_syms
[x
]) <= addr
));
3601 pinfo
->symbols
= sorted_syms
+ place
;
3602 pinfo
->num_symbols
= x
- place
;
3603 pinfo
->symtab_pos
= place
;
3607 pinfo
->symbols
= NULL
;
3608 pinfo
->num_symbols
= 0;
3609 pinfo
->symtab_pos
= -1;
3612 /* If we are only disassembling from a specific symbol,
3613 check to see if we should start or stop displaying. */
3614 if (sym
&& paux
->symbol
)
3618 /* See if we should stop printing. */
3622 if (sym
->flags
& BSF_FUNCTION
)
3626 case stop_offset_reached
:
3627 /* Handled by the while loop. */
3631 /* FIXME: There is an implicit assumption here
3632 that the name of sym is different from
3634 if (! bfd_is_local_label (abfd
, sym
))
3641 const char * name
= bfd_asymbol_name (sym
);
3642 char * alloc
= NULL
;
3644 if (do_demangle
&& name
[0] != '\0')
3646 /* Demangle the name. */
3647 alloc
= bfd_demangle (abfd
, name
, demangle_flags
);
3652 /* We are not currently printing. Check to see
3653 if the current symbol matches the requested symbol. */
3654 if (streq (name
, paux
->symbol
))
3658 if (sym
->flags
& BSF_FUNCTION
)
3660 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3661 && ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
> 0)
3663 /* Sym is a function symbol with a size associated
3664 with it. Turn on automatic disassembly for the
3665 next VALUE bytes. */
3666 stop_offset
= addr_offset
3667 + ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
3668 loop_until
= stop_offset_reached
;
3672 /* Otherwise we need to tell the loop heuristic to
3673 loop until the next function symbol is encountered. */
3674 loop_until
= function_sym
;
3679 /* Otherwise loop until the next symbol is encountered. */
3680 loop_until
= next_sym
;
3688 if (! prefix_addresses
&& do_print
)
3690 pinfo
->fprintf_func (pinfo
->stream
, "\n");
3691 objdump_print_addr_with_sym (abfd
, section
, sym
, addr
,
3693 pinfo
->fprintf_func (pinfo
->stream
, ":\n");
3696 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > addr
)
3698 else if (sym
== NULL
)
3702 #define is_valid_next_sym(SYM) \
3703 (strcmp (bfd_section_name ((SYM)->section), bfd_section_name (section)) == 0 \
3704 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
3705 && pinfo->symbol_is_valid (SYM, pinfo))
3707 /* Search forward for the next appropriate symbol in
3708 SECTION. Note that all the symbols are sorted
3709 together into one big array, and that some sections
3710 may have overlapping addresses. */
3711 while (place
< sorted_symcount
3712 && ! is_valid_next_sym (sorted_syms
[place
]))
3715 if (place
>= sorted_symcount
)
3718 nextsym
= sorted_syms
[place
];
3721 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > addr
)
3722 nextstop_offset
= bfd_asymbol_value (sym
) - section
->vma
;
3723 else if (nextsym
== NULL
)
3724 nextstop_offset
= stop_offset
;
3726 nextstop_offset
= bfd_asymbol_value (nextsym
) - section
->vma
;
3728 if (nextstop_offset
> stop_offset
3729 || nextstop_offset
<= addr_offset
)
3730 nextstop_offset
= stop_offset
;
3732 /* If a symbol is explicitly marked as being an object
3733 rather than a function, just dump the bytes without
3734 disassembling them. */
3737 || sym
->section
!= section
3738 || bfd_asymbol_value (sym
) > addr
3739 || ((sym
->flags
& BSF_OBJECT
) == 0
3740 && (strstr (bfd_asymbol_name (sym
), "gnu_compiled")
3742 && (strstr (bfd_asymbol_name (sym
), "gcc2_compiled")
3744 || (sym
->flags
& BSF_FUNCTION
) != 0)
3751 /* Resolve symbol name. */
3752 if (visualize_jumps
&& abfd
&& sym
&& sym
->name
)
3754 struct disassemble_info di
;
3757 sf
.alloc
= strlen (sym
->name
) + 40;
3758 sf
.buffer
= (char*) xmalloc (sf
.alloc
);
3760 disassemble_set_printf
3761 (&di
, &sf
, (fprintf_ftype
) objdump_sprintf
,
3762 (fprintf_styled_ftype
) objdump_styled_sprintf
);
3764 objdump_print_symname (abfd
, &di
, sym
);
3766 /* Fetch jump information. */
3767 detected_jumps
= disassemble_jumps
3768 (pinfo
, paux
->disassemble_fn
,
3769 addr_offset
, nextstop_offset
,
3770 rel_offset
, &rel_pp
, rel_ppend
);
3772 /* Free symbol name. */
3776 /* Add jumps to output. */
3777 disassemble_bytes (pinfo
, paux
->disassemble_fn
, insns
, data
,
3778 addr_offset
, nextstop_offset
,
3779 rel_offset
, &rel_pp
, rel_ppend
);
3782 while (detected_jumps
)
3784 detected_jumps
= jump_info_free (detected_jumps
);
3788 addr_offset
= nextstop_offset
;
3794 if (rel_ppstart
!= NULL
)
3798 /* Disassemble the contents of an object file. */
3801 disassemble_data (bfd
*abfd
)
3803 struct disassemble_info disasm_info
;
3804 struct objdump_disasm_info aux
;
3808 prev_functionname
= NULL
;
3810 prev_discriminator
= 0;
3812 /* We make a copy of syms to sort. We don't want to sort syms
3813 because that will screw up the relocs. */
3814 sorted_symcount
= symcount
? symcount
: dynsymcount
;
3815 sorted_syms
= (asymbol
**) xmalloc ((sorted_symcount
+ synthcount
)
3816 * sizeof (asymbol
*));
3817 if (sorted_symcount
!= 0)
3819 memcpy (sorted_syms
, symcount
? syms
: dynsyms
,
3820 sorted_symcount
* sizeof (asymbol
*));
3822 sorted_symcount
= remove_useless_symbols (sorted_syms
, sorted_symcount
);
3825 for (i
= 0; i
< synthcount
; ++i
)
3827 sorted_syms
[sorted_symcount
] = synthsyms
+ i
;
3831 init_disassemble_info (&disasm_info
, stdout
, (fprintf_ftype
) fprintf
,
3832 (fprintf_styled_ftype
) fprintf_styled
);
3833 disasm_info
.application_data
= (void *) &aux
;
3835 aux
.require_sec
= false;
3836 disasm_info
.dynrelbuf
= NULL
;
3837 disasm_info
.dynrelcount
= 0;
3839 aux
.symbol
= disasm_sym
;
3841 disasm_info
.print_address_func
= objdump_print_address
;
3842 disasm_info
.symbol_at_address_func
= objdump_symbol_at_address
;
3844 if (machine
!= NULL
)
3846 const bfd_arch_info_type
*inf
= bfd_scan_arch (machine
);
3849 fatal (_("can't use supplied machine %s"), machine
);
3851 abfd
->arch_info
= inf
;
3854 if (endian
!= BFD_ENDIAN_UNKNOWN
)
3856 struct bfd_target
*xvec
;
3858 xvec
= (struct bfd_target
*) xmalloc (sizeof (struct bfd_target
));
3859 memcpy (xvec
, abfd
->xvec
, sizeof (struct bfd_target
));
3860 xvec
->byteorder
= endian
;
3864 /* Use libopcodes to locate a suitable disassembler. */
3865 aux
.disassemble_fn
= disassembler (bfd_get_arch (abfd
),
3866 bfd_big_endian (abfd
),
3867 bfd_get_mach (abfd
), abfd
);
3868 if (!aux
.disassemble_fn
)
3870 non_fatal (_("can't disassemble for architecture %s\n"),
3871 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
3876 disasm_info
.flavour
= bfd_get_flavour (abfd
);
3877 disasm_info
.arch
= bfd_get_arch (abfd
);
3878 disasm_info
.mach
= bfd_get_mach (abfd
);
3879 disasm_info
.disassembler_options
= disassembler_options
;
3880 disasm_info
.octets_per_byte
= bfd_octets_per_byte (abfd
, NULL
);
3881 disasm_info
.skip_zeroes
= DEFAULT_SKIP_ZEROES
;
3882 disasm_info
.skip_zeroes_at_end
= DEFAULT_SKIP_ZEROES_AT_END
;
3883 disasm_info
.disassembler_needs_relocs
= false;
3885 if (bfd_big_endian (abfd
))
3886 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_BIG
;
3887 else if (bfd_little_endian (abfd
))
3888 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
3890 /* ??? Aborting here seems too drastic. We could default to big or little
3892 disasm_info
.endian
= BFD_ENDIAN_UNKNOWN
;
3894 disasm_info
.endian_code
= disasm_info
.endian
;
3896 /* Allow the target to customize the info structure. */
3897 disassemble_init_for_target (& disasm_info
);
3899 /* Pre-load the dynamic relocs as we may need them during the disassembly. */
3900 long relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
3902 if (relsize
< 0 && dump_dynamic_reloc_info
)
3903 bfd_fatal (bfd_get_filename (abfd
));
3907 disasm_info
.dynrelbuf
= (arelent
**) xmalloc (relsize
);
3908 disasm_info
.dynrelcount
3909 = bfd_canonicalize_dynamic_reloc (abfd
, disasm_info
.dynrelbuf
, dynsyms
);
3910 if (disasm_info
.dynrelcount
< 0)
3911 bfd_fatal (bfd_get_filename (abfd
));
3913 /* Sort the relocs by address. */
3914 qsort (disasm_info
.dynrelbuf
, disasm_info
.dynrelcount
, sizeof (arelent
*),
3918 disasm_info
.symtab
= sorted_syms
;
3919 disasm_info
.symtab_size
= sorted_symcount
;
3921 bfd_map_over_sections (abfd
, disassemble_section
, & disasm_info
);
3923 free (disasm_info
.dynrelbuf
);
3924 disasm_info
.dynrelbuf
= NULL
;
3926 disassemble_free_target (&disasm_info
);
3930 load_specific_debug_section (enum dwarf_section_display_enum debug
,
3931 asection
*sec
, void *file
)
3933 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
3934 bfd
*abfd
= (bfd
*) file
;
3940 if (section
->start
!= NULL
)
3942 /* If it is already loaded, do nothing. */
3943 if (streq (section
->filename
, bfd_get_filename (abfd
)))
3945 free (section
->start
);
3948 section
->filename
= bfd_get_filename (abfd
);
3949 section
->reloc_info
= NULL
;
3950 section
->num_relocs
= 0;
3951 section
->address
= bfd_section_vma (sec
);
3952 section
->size
= bfd_section_size (sec
);
3953 /* PR 24360: On 32-bit hosts sizeof (size_t) < sizeof (bfd_size_type). */
3954 alloced
= amt
= section
->size
+ 1;
3955 if (alloced
!= amt
|| alloced
== 0)
3957 section
->start
= NULL
;
3958 free_debug_section (debug
);
3959 printf (_("\nSection '%s' has an invalid size: %#llx.\n"),
3960 sanitize_string (section
->name
),
3961 (unsigned long long) section
->size
);
3965 section
->start
= contents
= xmalloc (alloced
);
3966 /* Ensure any string section has a terminating NUL. */
3967 section
->start
[section
->size
] = 0;
3969 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3970 && debug_displays
[debug
].relocate
)
3972 ret
= bfd_simple_get_relocated_section_contents (abfd
,
3978 long reloc_size
= bfd_get_reloc_upper_bound (abfd
, sec
);
3982 unsigned long reloc_count
;
3985 relocs
= (arelent
**) xmalloc (reloc_size
);
3987 reloc_count
= bfd_canonicalize_reloc (abfd
, sec
, relocs
, NULL
);
3988 if (reloc_count
== 0)
3992 section
->reloc_info
= relocs
;
3993 section
->num_relocs
= reloc_count
;
3999 ret
= bfd_get_full_section_contents (abfd
, sec
, &contents
);
4003 free_debug_section (debug
);
4004 printf (_("\nCan't get contents for section '%s'.\n"),
4005 sanitize_string (section
->name
));
4013 reloc_at (struct dwarf_section
* dsec
, dwarf_vma offset
)
4018 if (dsec
== NULL
|| dsec
->reloc_info
== NULL
)
4021 relocs
= (arelent
**) dsec
->reloc_info
;
4023 for (; (rp
= * relocs
) != NULL
; ++ relocs
)
4024 if (rp
->address
== offset
)
4031 load_debug_section (enum dwarf_section_display_enum debug
, void *file
)
4033 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
4034 bfd
*abfd
= (bfd
*) file
;
4038 if (!dump_any_debugging
)
4041 /* If it is already loaded, do nothing. */
4042 if (section
->start
!= NULL
)
4044 if (streq (section
->filename
, bfd_get_filename (abfd
)))
4047 /* Locate the debug section. */
4048 name
= section
->uncompressed_name
;
4049 sec
= bfd_get_section_by_name (abfd
, name
);
4052 name
= section
->compressed_name
;
4054 sec
= bfd_get_section_by_name (abfd
, name
);
4058 name
= section
->xcoff_name
;
4060 sec
= bfd_get_section_by_name (abfd
, name
);
4065 section
->name
= name
;
4066 return load_specific_debug_section (debug
, sec
, file
);
4070 free_debug_section (enum dwarf_section_display_enum debug
)
4072 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
4074 free ((char *) section
->start
);
4075 section
->start
= NULL
;
4076 section
->address
= 0;
4081 close_debug_file (void * file
)
4083 bfd
* abfd
= (bfd
*) file
;
4089 open_debug_file (const char * pathname
)
4093 data
= bfd_openr (pathname
, NULL
);
4097 if (! bfd_check_format (data
, bfd_object
))
4103 #if HAVE_LIBDEBUGINFOD
4104 /* Return a hex string represention of the build-id. */
4107 get_build_id (void * data
)
4110 char * build_id_str
;
4111 bfd
* abfd
= (bfd
*) data
;
4112 const struct bfd_build_id
* build_id
;
4114 build_id
= abfd
->build_id
;
4115 if (build_id
== NULL
)
4118 build_id_str
= malloc (build_id
->size
* 2 + 1);
4119 if (build_id_str
== NULL
)
4122 for (i
= 0; i
< build_id
->size
; i
++)
4123 sprintf (build_id_str
+ (i
* 2), "%02x", build_id
->data
[i
]);
4124 build_id_str
[build_id
->size
* 2] = '\0';
4126 return (unsigned char *)build_id_str
;
4128 #endif /* HAVE_LIBDEBUGINFOD */
4131 dump_dwarf_section (bfd
*abfd
, asection
*section
,
4134 const char *name
= bfd_section_name (section
);
4137 bool is_mainfile
= *(bool *) arg
;
4142 if (!is_mainfile
&& !process_links
4143 && (section
->flags
& SEC_DEBUGGING
) == 0)
4146 if (startswith (name
, ".gnu.linkonce.wi."))
4147 match
= ".debug_info";
4151 for (i
= 0; i
< max
; i
++)
4152 if ((strcmp (debug_displays
[i
].section
.uncompressed_name
, match
) == 0
4153 || strcmp (debug_displays
[i
].section
.compressed_name
, match
) == 0
4154 || strcmp (debug_displays
[i
].section
.xcoff_name
, match
) == 0)
4155 && debug_displays
[i
].enabled
!= NULL
4156 && *debug_displays
[i
].enabled
)
4158 struct dwarf_section
*sec
= &debug_displays
[i
].section
;
4160 if (strcmp (sec
->uncompressed_name
, match
) == 0)
4161 sec
->name
= sec
->uncompressed_name
;
4162 else if (strcmp (sec
->compressed_name
, match
) == 0)
4163 sec
->name
= sec
->compressed_name
;
4165 sec
->name
= sec
->xcoff_name
;
4166 if (load_specific_debug_section ((enum dwarf_section_display_enum
) i
,
4169 debug_displays
[i
].display (sec
, abfd
);
4171 if (i
!= info
&& i
!= abbrev
)
4172 free_debug_section ((enum dwarf_section_display_enum
) i
);
4178 /* Dump the dwarf debugging information. */
4181 dump_dwarf (bfd
*abfd
, bool is_mainfile
)
4183 /* The byte_get pointer should have been set at the start of dump_bfd(). */
4184 if (byte_get
== NULL
)
4186 warn (_("File %s does not contain any dwarf debug information\n"),
4187 bfd_get_filename (abfd
));
4191 switch (bfd_get_arch (abfd
))
4194 /* S12Z has a 24 bit address space. But the only known
4195 producer of dwarf_info encodes addresses into 32 bits. */
4200 eh_addr_size
= bfd_arch_bits_per_address (abfd
) / 8;
4204 init_dwarf_regnames_by_bfd_arch_and_mach (bfd_get_arch (abfd
),
4205 bfd_get_mach (abfd
));
4207 bfd_map_over_sections (abfd
, dump_dwarf_section
, (void *) &is_mainfile
);
4210 /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
4211 it. Return NULL on failure. */
4214 read_section_stabs (bfd
*abfd
, const char *sect_name
, bfd_size_type
*size_ptr
,
4215 bfd_size_type
*entsize_ptr
)
4220 stabsect
= bfd_get_section_by_name (abfd
, sect_name
);
4221 if (stabsect
== NULL
)
4223 printf (_("No %s section present\n\n"),
4224 sanitize_string (sect_name
));
4228 if (!bfd_malloc_and_get_section (abfd
, stabsect
, &contents
))
4230 non_fatal (_("reading %s section of %s failed: %s"),
4231 sect_name
, bfd_get_filename (abfd
),
4232 bfd_errmsg (bfd_get_error ()));
4238 *size_ptr
= bfd_section_size (stabsect
);
4240 *entsize_ptr
= stabsect
->entsize
;
4245 /* Stabs entries use a 12 byte format:
4246 4 byte string table index
4248 1 byte stab other field
4249 2 byte stab desc field
4251 FIXME: This will have to change for a 64 bit object format. */
4253 #define STRDXOFF (0)
4255 #define OTHEROFF (5)
4258 #define STABSIZE (12)
4260 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
4261 using string table section STRSECT_NAME (in `strtab'). */
4264 print_section_stabs (bfd
*abfd
,
4265 const char *stabsect_name
,
4266 unsigned *string_offset_ptr
)
4269 unsigned file_string_table_offset
= 0;
4270 unsigned next_file_string_table_offset
= *string_offset_ptr
;
4271 bfd_byte
*stabp
, *stabs_end
;
4274 stabs_end
= stabp
+ stab_size
;
4276 printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name
));
4277 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
4279 /* Loop through all symbols and print them.
4281 We start the index at -1 because there is a dummy symbol on
4282 the front of stabs-in-{coff,elf} sections that supplies sizes. */
4283 for (i
= -1; stabp
<= stabs_end
- STABSIZE
; stabp
+= STABSIZE
, i
++)
4287 unsigned char type
, other
;
4288 unsigned short desc
;
4291 strx
= bfd_h_get_32 (abfd
, stabp
+ STRDXOFF
);
4292 type
= bfd_h_get_8 (abfd
, stabp
+ TYPEOFF
);
4293 other
= bfd_h_get_8 (abfd
, stabp
+ OTHEROFF
);
4294 desc
= bfd_h_get_16 (abfd
, stabp
+ DESCOFF
);
4295 value
= bfd_h_get_32 (abfd
, stabp
+ VALOFF
);
4297 printf ("\n%-6d ", i
);
4298 /* Either print the stab name, or, if unnamed, print its number
4299 again (makes consistent formatting for tools like awk). */
4300 name
= bfd_get_stab_name (type
);
4302 printf ("%-6s", sanitize_string (name
));
4303 else if (type
== N_UNDF
)
4306 printf ("%-6d", type
);
4307 printf (" %-6d %-6d ", other
, desc
);
4308 bfd_printf_vma (abfd
, value
);
4309 printf (" %-6lu", strx
);
4311 /* Symbols with type == 0 (N_UNDF) specify the length of the
4312 string table associated with this file. We use that info
4313 to know how to relocate the *next* file's string table indices. */
4316 file_string_table_offset
= next_file_string_table_offset
;
4317 next_file_string_table_offset
+= value
;
4321 bfd_size_type amt
= strx
+ file_string_table_offset
;
4323 /* Using the (possibly updated) string table offset, print the
4324 string (if any) associated with this symbol. */
4325 if (amt
< stabstr_size
)
4326 /* PR 17512: file: 079-79389-0.001:0.1.
4327 FIXME: May need to sanitize this string before displaying. */
4328 printf (" %.*s", (int)(stabstr_size
- amt
), strtab
+ amt
);
4334 *string_offset_ptr
= next_file_string_table_offset
;
4339 const char * section_name
;
4340 const char * string_section_name
;
4341 unsigned string_offset
;
4346 find_stabs_section (bfd
*abfd
, asection
*section
, void *names
)
4349 stab_section_names
* sought
= (stab_section_names
*) names
;
4351 /* Check for section names for which stabsect_name is a prefix, to
4352 handle .stab.N, etc. */
4353 len
= strlen (sought
->section_name
);
4355 /* If the prefix matches, and the files section name ends with a
4356 nul or a digit, then we match. I.e., we want either an exact
4357 match or a section followed by a number. */
4358 if (strncmp (sought
->section_name
, section
->name
, len
) == 0
4359 && (section
->name
[len
] == 0
4360 || (section
->name
[len
] == '.' && ISDIGIT (section
->name
[len
+ 1]))))
4363 strtab
= read_section_stabs (abfd
, sought
->string_section_name
,
4364 &stabstr_size
, NULL
);
4368 stabs
= read_section_stabs (abfd
, section
->name
, &stab_size
, NULL
);
4370 print_section_stabs (abfd
, section
->name
, &sought
->string_offset
);
4376 dump_stabs_section (bfd
*abfd
, char *stabsect_name
, char *strsect_name
)
4378 stab_section_names s
;
4380 s
.section_name
= stabsect_name
;
4381 s
.string_section_name
= strsect_name
;
4382 s
.string_offset
= 0;
4384 bfd_map_over_sections (abfd
, find_stabs_section
, & s
);
4390 /* Dump the any sections containing stabs debugging information. */
4393 dump_stabs (bfd
*abfd
)
4395 dump_stabs_section (abfd
, ".stab", ".stabstr");
4396 dump_stabs_section (abfd
, ".stab.excl", ".stab.exclstr");
4397 dump_stabs_section (abfd
, ".stab.index", ".stab.indexstr");
4400 dump_stabs_section (abfd
, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
4402 dump_stabs_section (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
4406 dump_bfd_header (bfd
*abfd
)
4410 printf (_("architecture: %s, "),
4411 bfd_printable_arch_mach (bfd_get_arch (abfd
),
4412 bfd_get_mach (abfd
)));
4413 printf (_("flags 0x%08x:\n"), abfd
->flags
& ~BFD_FLAGS_FOR_BFD_USE_MASK
);
4415 #define PF(x, y) if (abfd->flags & x) {printf ("%s%s", comma, y); comma=", ";}
4416 PF (HAS_RELOC
, "HAS_RELOC");
4417 PF (EXEC_P
, "EXEC_P");
4418 PF (HAS_LINENO
, "HAS_LINENO");
4419 PF (HAS_DEBUG
, "HAS_DEBUG");
4420 PF (HAS_SYMS
, "HAS_SYMS");
4421 PF (HAS_LOCALS
, "HAS_LOCALS");
4422 PF (DYNAMIC
, "DYNAMIC");
4423 PF (WP_TEXT
, "WP_TEXT");
4424 PF (D_PAGED
, "D_PAGED");
4425 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
4426 printf (_("\nstart address 0x"));
4427 bfd_printf_vma (abfd
, abfd
->start_address
);
4432 #ifdef ENABLE_LIBCTF
4433 /* Formatting callback function passed to ctf_dump. Returns either the pointer
4434 it is passed, or a pointer to newly-allocated storage, in which case
4435 dump_ctf() will free it when it no longer needs it. */
4438 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED
,
4441 const char *blanks
= arg
;
4444 if (asprintf (&new_s
, "%s%s", blanks
, s
) < 0)
4449 /* Make a ctfsect suitable for ctf_bfdopen_ctfsect(). */
4451 make_ctfsect (const char *name
, bfd_byte
*data
,
4456 ctfsect
.cts_name
= name
;
4457 ctfsect
.cts_entsize
= 1;
4458 ctfsect
.cts_size
= size
;
4459 ctfsect
.cts_data
= data
;
4464 /* Dump CTF errors/warnings. */
4466 dump_ctf_errs (ctf_dict_t
*fp
)
4468 ctf_next_t
*it
= NULL
;
4473 /* Dump accumulated errors and warnings. */
4474 while ((errtext
= ctf_errwarning_next (fp
, &it
, &is_warning
, &err
)) != NULL
)
4476 non_fatal (_("%s: %s"), is_warning
? _("warning"): _("error"),
4480 if (err
!= ECTF_NEXT_END
)
4482 non_fatal (_("CTF error: cannot get CTF errors: `%s'"),
4487 /* Dump one CTF archive member. */
4490 dump_ctf_archive_member (ctf_dict_t
*ctf
, const char *name
, ctf_dict_t
*parent
,
4493 const char *things
[] = {"Header", "Labels", "Data objects",
4494 "Function objects", "Variables", "Types", "Strings",
4499 /* Don't print out the name of the default-named archive member if it appears
4500 first in the list. The name .ctf appears everywhere, even for things that
4501 aren't really archives, so printing it out is liable to be confusing; also,
4502 the common case by far is for only one archive member to exist, and hiding
4503 it in that case seems worthwhile. */
4505 if (strcmp (name
, ".ctf") != 0 || member
!= 0)
4506 printf (_("\nCTF archive member: %s:\n"), sanitize_string (name
));
4508 if (ctf_parent_name (ctf
) != NULL
)
4509 ctf_import (ctf
, parent
);
4511 for (i
= 0, thing
= things
; *thing
[0]; thing
++, i
++)
4513 ctf_dump_state_t
*s
= NULL
;
4516 printf ("\n %s:\n", *thing
);
4517 while ((item
= ctf_dump (ctf
, &s
, i
, dump_ctf_indent_lines
,
4518 (void *) " ")) != NULL
)
4520 printf ("%s\n", item
);
4524 if (ctf_errno (ctf
))
4526 non_fatal (_("Iteration failed: %s, %s"), *thing
,
4527 ctf_errmsg (ctf_errno (ctf
)));
4532 dump_ctf_errs (ctf
);
4535 /* Dump the CTF debugging information. */
4538 dump_ctf (bfd
*abfd
, const char *sect_name
, const char *parent_name
)
4540 ctf_archive_t
*ctfa
= NULL
;
4541 bfd_byte
*ctfdata
= NULL
;
4542 bfd_size_type ctfsize
;
4546 ctf_next_t
*i
= NULL
;
4551 if (sect_name
== NULL
)
4554 if ((ctfdata
= read_section_stabs (abfd
, sect_name
, &ctfsize
, NULL
)) == NULL
)
4555 bfd_fatal (bfd_get_filename (abfd
));
4557 /* Load the CTF file and dump it. Preload the parent dict, since it will
4558 need to be imported into every child in turn. */
4560 ctfsect
= make_ctfsect (sect_name
, ctfdata
, ctfsize
);
4561 if ((ctfa
= ctf_bfdopen_ctfsect (abfd
, &ctfsect
, &err
)) == NULL
)
4563 dump_ctf_errs (NULL
);
4564 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err
));
4565 bfd_fatal (bfd_get_filename (abfd
));
4568 if ((parent
= ctf_dict_open (ctfa
, parent_name
, &err
)) == NULL
)
4570 dump_ctf_errs (NULL
);
4571 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err
));
4572 bfd_fatal (bfd_get_filename (abfd
));
4575 printf (_("Contents of CTF section %s:\n"), sanitize_string (sect_name
));
4577 while ((fp
= ctf_archive_next (ctfa
, &i
, &name
, 0, &err
)) != NULL
)
4578 dump_ctf_archive_member (fp
, name
, parent
, member
++);
4579 if (err
!= ECTF_NEXT_END
)
4581 dump_ctf_errs (NULL
);
4582 non_fatal (_("CTF archive member open failure: %s"), ctf_errmsg (err
));
4583 bfd_fatal (bfd_get_filename (abfd
));
4585 ctf_dict_close (parent
);
4591 dump_ctf (bfd
*abfd ATTRIBUTE_UNUSED
, const char *sect_name ATTRIBUTE_UNUSED
,
4592 const char *parent_name ATTRIBUTE_UNUSED
) {}
4597 dump_bfd_private_header (bfd
*abfd
)
4599 if (!bfd_print_private_bfd_data (abfd
, stdout
))
4600 non_fatal (_("warning: private headers incomplete: %s"),
4601 bfd_errmsg (bfd_get_error ()));
4605 dump_target_specific (bfd
*abfd
)
4607 const struct objdump_private_desc
* const *desc
;
4608 struct objdump_private_option
*opt
;
4611 /* Find the desc. */
4612 for (desc
= objdump_private_vectors
; *desc
!= NULL
; desc
++)
4613 if ((*desc
)->filter (abfd
))
4618 non_fatal (_("option -P/--private not supported by this file"));
4622 /* Clear all options. */
4623 for (opt
= (*desc
)->options
; opt
->name
; opt
++)
4624 opt
->selected
= false;
4626 /* Decode options. */
4627 b
= dump_private_options
;
4630 e
= strchr (b
, ',');
4635 for (opt
= (*desc
)->options
; opt
->name
; opt
++)
4636 if (strcmp (opt
->name
, b
) == 0)
4638 opt
->selected
= true;
4641 if (opt
->name
== NULL
)
4642 non_fatal (_("target specific dump '%s' not supported"), b
);
4653 (*desc
)->dump (abfd
);
4656 /* Display a section in hexadecimal format with associated characters.
4657 Each line prefixed by the zero padded address. */
4660 dump_section (bfd
*abfd
, asection
*section
, void *dummy ATTRIBUTE_UNUSED
)
4662 bfd_byte
*data
= NULL
;
4663 bfd_size_type datasize
;
4664 bfd_vma addr_offset
;
4665 bfd_vma start_offset
;
4666 bfd_vma stop_offset
;
4667 unsigned int opb
= bfd_octets_per_byte (abfd
, section
);
4668 /* Bytes per line. */
4669 const int onaline
= 16;
4674 if (! process_section_p (section
))
4677 if ((section
->flags
& SEC_HAS_CONTENTS
) == 0)
4680 if ((datasize
= bfd_section_size (section
)) == 0)
4683 /* Compute the address range to display. */
4684 if (start_address
== (bfd_vma
) -1
4685 || start_address
< section
->vma
)
4688 start_offset
= start_address
- section
->vma
;
4690 if (stop_address
== (bfd_vma
) -1)
4691 stop_offset
= datasize
/ opb
;
4694 if (stop_address
< section
->vma
)
4697 stop_offset
= stop_address
- section
->vma
;
4699 if (stop_offset
> datasize
/ opb
)
4700 stop_offset
= datasize
/ opb
;
4703 if (start_offset
>= stop_offset
)
4706 printf (_("Contents of section %s:"), sanitize_string (section
->name
));
4707 if (display_file_offsets
)
4708 printf (_(" (Starting at file offset: 0x%lx)"),
4709 (unsigned long) (section
->filepos
+ start_offset
));
4712 if (!bfd_get_full_section_contents (abfd
, section
, &data
))
4714 non_fatal (_("Reading section %s failed because: %s"),
4715 section
->name
, bfd_errmsg (bfd_get_error ()));
4721 bfd_sprintf_vma (abfd
, buf
, start_offset
+ section
->vma
);
4722 if (strlen (buf
) >= sizeof (buf
))
4726 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
4728 count
= strlen (buf
) - count
;
4732 bfd_sprintf_vma (abfd
, buf
, stop_offset
+ section
->vma
- 1);
4733 if (strlen (buf
) >= sizeof (buf
))
4737 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
4739 count
= strlen (buf
) - count
;
4743 for (addr_offset
= start_offset
;
4744 addr_offset
< stop_offset
; addr_offset
+= onaline
/ opb
)
4748 bfd_sprintf_vma (abfd
, buf
, (addr_offset
+ section
->vma
));
4749 count
= strlen (buf
);
4750 if ((size_t) count
>= sizeof (buf
))
4754 while (count
< width
)
4759 fputs (buf
+ count
- width
, stdout
);
4762 for (j
= addr_offset
* opb
;
4763 j
< addr_offset
* opb
+ onaline
; j
++)
4765 if (j
< stop_offset
* opb
)
4766 printf ("%02x", (unsigned) (data
[j
]));
4774 for (j
= addr_offset
* opb
;
4775 j
< addr_offset
* opb
+ onaline
; j
++)
4777 if (j
>= stop_offset
* opb
)
4780 printf ("%c", ISPRINT (data
[j
]) ? data
[j
] : '.');
4787 /* Actually display the various requested regions. */
4790 dump_data (bfd
*abfd
)
4792 bfd_map_over_sections (abfd
, dump_section
, NULL
);
4795 /* Should perhaps share code and display with nm? */
4798 dump_symbols (bfd
*abfd ATTRIBUTE_UNUSED
, bool dynamic
)
4807 max_count
= dynsymcount
;
4808 printf ("DYNAMIC SYMBOL TABLE:\n");
4813 max_count
= symcount
;
4814 printf ("SYMBOL TABLE:\n");
4818 printf (_("no symbols\n"));
4820 for (count
= 0; count
< max_count
; count
++)
4824 if (*current
== NULL
)
4825 printf (_("no information for symbol number %ld\n"), count
);
4827 else if ((cur_bfd
= bfd_asymbol_bfd (*current
)) == NULL
)
4828 printf (_("could not determine the type of symbol number %ld\n"),
4831 else if (process_section_p ((* current
)->section
)
4832 && (dump_special_syms
4833 || !bfd_is_target_special_symbol (cur_bfd
, *current
)))
4835 const char *name
= (*current
)->name
;
4837 if (do_demangle
&& name
!= NULL
&& *name
!= '\0')
4841 /* If we want to demangle the name, we demangle it
4842 here, and temporarily clobber it while calling
4843 bfd_print_symbol. FIXME: This is a gross hack. */
4844 alloc
= bfd_demangle (cur_bfd
, name
, demangle_flags
);
4846 (*current
)->name
= alloc
;
4847 bfd_print_symbol (cur_bfd
, stdout
, *current
,
4848 bfd_print_symbol_all
);
4851 (*current
)->name
= name
;
4855 else if (unicode_display
!= unicode_default
4856 && name
!= NULL
&& *name
!= '\0')
4858 const char * sanitized_name
;
4860 /* If we want to sanitize the name, we do it here, and
4861 temporarily clobber it while calling bfd_print_symbol.
4862 FIXME: This is a gross hack. */
4863 sanitized_name
= sanitize_string (name
);
4864 if (sanitized_name
!= name
)
4865 (*current
)->name
= sanitized_name
;
4867 sanitized_name
= NULL
;
4868 bfd_print_symbol (cur_bfd
, stdout
, *current
,
4869 bfd_print_symbol_all
);
4870 if (sanitized_name
!= NULL
)
4871 (*current
)->name
= name
;
4874 bfd_print_symbol (cur_bfd
, stdout
, *current
,
4875 bfd_print_symbol_all
);
4885 dump_reloc_set (bfd
*abfd
, asection
*sec
, arelent
**relpp
, long relcount
)
4888 char *last_filename
, *last_functionname
;
4889 unsigned int last_line
;
4890 unsigned int last_discriminator
;
4892 /* Get column headers lined up reasonably. */
4900 bfd_sprintf_vma (abfd
, buf
, (bfd_vma
) -1);
4901 width
= strlen (buf
) - 7;
4903 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
4906 last_filename
= NULL
;
4907 last_functionname
= NULL
;
4909 last_discriminator
= 0;
4911 for (p
= relpp
; relcount
&& *p
!= NULL
; p
++, relcount
--)
4914 const char *filename
, *functionname
;
4915 unsigned int linenumber
;
4916 unsigned int discriminator
;
4917 const char *sym_name
;
4918 const char *section_name
;
4919 bfd_vma addend2
= 0;
4921 if (start_address
!= (bfd_vma
) -1
4922 && q
->address
< start_address
)
4924 if (stop_address
!= (bfd_vma
) -1
4925 && q
->address
> stop_address
)
4928 if (with_line_numbers
4930 && bfd_find_nearest_line_discriminator (abfd
, sec
, syms
, q
->address
,
4931 &filename
, &functionname
,
4932 &linenumber
, &discriminator
))
4934 if (functionname
!= NULL
4935 && (last_functionname
== NULL
4936 || strcmp (functionname
, last_functionname
) != 0))
4938 printf ("%s():\n", sanitize_string (functionname
));
4939 if (last_functionname
!= NULL
)
4940 free (last_functionname
);
4941 last_functionname
= xstrdup (functionname
);
4945 && (linenumber
!= last_line
4946 || (filename
!= NULL
4947 && last_filename
!= NULL
4948 && filename_cmp (filename
, last_filename
) != 0)
4949 || (discriminator
!= last_discriminator
)))
4951 if (discriminator
> 0)
4952 printf ("%s:%u\n", filename
== NULL
? "???" :
4953 sanitize_string (filename
), linenumber
);
4955 printf ("%s:%u (discriminator %u)\n",
4956 filename
== NULL
? "???" : sanitize_string (filename
),
4957 linenumber
, discriminator
);
4958 last_line
= linenumber
;
4959 last_discriminator
= discriminator
;
4960 if (last_filename
!= NULL
)
4961 free (last_filename
);
4962 if (filename
== NULL
)
4963 last_filename
= NULL
;
4965 last_filename
= xstrdup (filename
);
4969 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
4971 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
4972 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
4977 section_name
= NULL
;
4980 bfd_printf_vma (abfd
, q
->address
);
4981 if (q
->howto
== NULL
)
4982 printf (" *unknown* ");
4983 else if (q
->howto
->name
)
4985 const char *name
= q
->howto
->name
;
4987 /* R_SPARC_OLO10 relocations contain two addends.
4988 But because 'arelent' lacks enough storage to
4989 store them both, the 64-bit ELF Sparc backend
4990 records this as two relocations. One R_SPARC_LO10
4991 and one R_SPARC_13, both pointing to the same
4992 address. This is merely so that we have some
4993 place to store both addend fields.
4995 Undo this transformation, otherwise the output
4996 will be confusing. */
4997 if (abfd
->xvec
->flavour
== bfd_target_elf_flavour
4998 && elf_tdata (abfd
)->elf_header
->e_machine
== EM_SPARCV9
5000 && !strcmp (q
->howto
->name
, "R_SPARC_LO10"))
5002 arelent
*q2
= *(p
+ 1);
5005 && q
->address
== q2
->address
5006 && !strcmp (q2
->howto
->name
, "R_SPARC_13"))
5008 name
= "R_SPARC_OLO10";
5009 addend2
= q2
->addend
;
5013 printf (" %-16s ", name
);
5016 printf (" %-16d ", q
->howto
->type
);
5020 objdump_print_symname (abfd
, NULL
, *q
->sym_ptr_ptr
);
5024 if (section_name
== NULL
)
5025 section_name
= "*unknown*";
5026 printf ("[%s]", sanitize_string (section_name
));
5031 bfd_signed_vma addend
= q
->addend
;
5039 bfd_printf_vma (abfd
, addend
);
5044 bfd_printf_vma (abfd
, addend2
);
5050 if (last_filename
!= NULL
)
5051 free (last_filename
);
5052 if (last_functionname
!= NULL
)
5053 free (last_functionname
);
5057 dump_relocs_in_section (bfd
*abfd
,
5059 void *dummy ATTRIBUTE_UNUSED
)
5061 arelent
**relpp
= NULL
;
5065 if ( bfd_is_abs_section (section
)
5066 || bfd_is_und_section (section
)
5067 || bfd_is_com_section (section
)
5068 || (! process_section_p (section
))
5069 || ((section
->flags
& SEC_RELOC
) == 0))
5072 printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section
->name
));
5074 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
5077 printf (" (none)\n\n");
5085 relpp
= (arelent
**) xmalloc (relsize
);
5086 relcount
= bfd_canonicalize_reloc (abfd
, section
, relpp
, syms
);
5092 non_fatal (_("failed to read relocs in: %s"),
5093 sanitize_string (bfd_get_filename (abfd
)));
5094 bfd_fatal (_("error message was"));
5096 else if (relcount
== 0)
5097 printf (" (none)\n\n");
5101 dump_reloc_set (abfd
, section
, relpp
, relcount
);
5108 dump_relocs (bfd
*abfd
)
5110 bfd_map_over_sections (abfd
, dump_relocs_in_section
, NULL
);
5114 dump_dynamic_relocs (bfd
*abfd
)
5120 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
5122 bfd_fatal (bfd_get_filename (abfd
));
5124 printf ("DYNAMIC RELOCATION RECORDS");
5127 printf (" (none)\n\n");
5130 relpp
= (arelent
**) xmalloc (relsize
);
5131 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
5134 bfd_fatal (bfd_get_filename (abfd
));
5135 else if (relcount
== 0)
5136 printf (" (none)\n\n");
5140 dump_reloc_set (abfd
, NULL
, relpp
, relcount
);
5147 /* Creates a table of paths, to search for source files. */
5150 add_include_path (const char *path
)
5154 include_path_count
++;
5155 include_paths
= (const char **)
5156 xrealloc (include_paths
, include_path_count
* sizeof (*include_paths
));
5157 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5158 if (path
[1] == ':' && path
[2] == 0)
5159 path
= concat (path
, ".", (const char *) 0);
5161 include_paths
[include_path_count
- 1] = path
;
5165 adjust_addresses (bfd
*abfd ATTRIBUTE_UNUSED
,
5169 if ((section
->flags
& SEC_DEBUGGING
) == 0)
5171 bool *has_reloc_p
= (bool *) arg
;
5172 section
->vma
+= adjust_section_vma
;
5174 section
->lma
+= adjust_section_vma
;
5178 /* Return the sign-extended form of an ARCH_SIZE sized VMA. */
5181 sign_extend_address (bfd
*abfd ATTRIBUTE_UNUSED
,
5186 mask
= (bfd_vma
) 1 << (arch_size
- 1);
5187 return (((vma
& ((mask
<< 1) - 1)) ^ mask
) - mask
);
5190 /* Dump selected contents of ABFD. */
5193 dump_bfd (bfd
*abfd
, bool is_mainfile
)
5195 const struct elf_backend_data
* bed
;
5197 if (bfd_big_endian (abfd
))
5198 byte_get
= byte_get_big_endian
;
5199 else if (bfd_little_endian (abfd
))
5200 byte_get
= byte_get_little_endian
;
5204 /* Load any separate debug information files.
5205 We do this now and without checking do_follow_links because separate
5206 debug info files may contain symbol tables that we will need when
5207 displaying information about the main file. Any memory allocated by
5208 load_separate_debug_files will be released when we call
5209 free_debug_memory below.
5211 The test on is_mainfile is there because the chain of separate debug
5212 info files is a global variable shared by all invocations of dump_bfd. */
5213 if (byte_get
!= NULL
&& is_mainfile
)
5215 load_separate_debug_files (abfd
, bfd_get_filename (abfd
));
5217 /* If asked to do so, recursively dump the separate files. */
5218 if (do_follow_links
)
5222 for (i
= first_separate_info
; i
!= NULL
; i
= i
->next
)
5223 dump_bfd (i
->handle
, false);
5227 /* Adjust user-specified start and stop limits for targets that use
5228 signed addresses. */
5229 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
5230 && (bed
= get_elf_backend_data (abfd
)) != NULL
5231 && bed
->sign_extend_vma
)
5233 start_address
= sign_extend_address (abfd
, start_address
,
5235 stop_address
= sign_extend_address (abfd
, stop_address
,
5239 /* If we are adjusting section VMA's, change them all now. Changing
5240 the BFD information is a hack. However, we must do it, or
5241 bfd_find_nearest_line will not do the right thing. */
5242 if (adjust_section_vma
!= 0)
5244 bool has_reloc
= (abfd
->flags
& HAS_RELOC
);
5245 bfd_map_over_sections (abfd
, adjust_addresses
, &has_reloc
);
5248 if (is_mainfile
|| process_links
)
5250 if (! dump_debugging_tags
&& ! suppress_bfd_header
)
5251 printf (_("\n%s: file format %s\n"),
5252 sanitize_string (bfd_get_filename (abfd
)),
5255 print_arelt_descr (stdout
, abfd
, true, false);
5256 if (dump_file_header
)
5257 dump_bfd_header (abfd
);
5258 if (dump_private_headers
)
5259 dump_bfd_private_header (abfd
);
5260 if (dump_private_options
!= NULL
)
5261 dump_target_specific (abfd
);
5262 if (! dump_debugging_tags
&& ! suppress_bfd_header
)
5270 || dump_dwarf_section_info
)
5272 syms
= slurp_symtab (abfd
);
5274 /* If following links, load any symbol tables from the linked files as well. */
5275 if (do_follow_links
&& is_mainfile
)
5279 for (i
= first_separate_info
; i
!= NULL
; i
= i
->next
)
5281 asymbol
** extra_syms
;
5282 long old_symcount
= symcount
;
5284 extra_syms
= slurp_symtab (i
->handle
);
5288 if (old_symcount
== 0)
5294 syms
= xrealloc (syms
, ((symcount
+ old_symcount
+ 1)
5295 * sizeof (asymbol
*)));
5296 memcpy (syms
+ old_symcount
,
5298 (symcount
+ 1) * sizeof (asymbol
*));
5302 symcount
+= old_symcount
;
5307 if (is_mainfile
|| process_links
)
5309 if (dump_section_headers
)
5310 dump_headers (abfd
);
5312 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
5313 || (disassemble
&& bfd_get_dynamic_symtab_upper_bound (abfd
) > 0))
5314 dynsyms
= slurp_dynamic_symtab (abfd
);
5318 synthcount
= bfd_get_synthetic_symtab (abfd
, symcount
, syms
,
5319 dynsymcount
, dynsyms
,
5326 dump_symbols (abfd
, false);
5327 if (dump_dynamic_symtab
)
5328 dump_symbols (abfd
, true);
5330 if (dump_dwarf_section_info
)
5331 dump_dwarf (abfd
, is_mainfile
);
5332 if (is_mainfile
|| process_links
)
5334 if (dump_ctf_section_info
)
5335 dump_ctf (abfd
, dump_ctf_section_name
, dump_ctf_parent_name
);
5336 if (dump_stab_section_info
)
5338 if (dump_reloc_info
&& ! disassemble
)
5340 if (dump_dynamic_reloc_info
&& ! disassemble
)
5341 dump_dynamic_relocs (abfd
);
5342 if (dump_section_contents
)
5345 disassemble_data (abfd
);
5352 dhandle
= read_debugging_info (abfd
, syms
, symcount
, true);
5353 if (dhandle
!= NULL
)
5355 if (!print_debugging_info (stdout
, dhandle
, abfd
, syms
,
5357 dump_debugging_tags
!= 0))
5359 non_fatal (_("%s: printing debugging information failed"),
5360 bfd_get_filename (abfd
));
5366 /* PR 6483: If there was no STABS debug info in the file, try
5368 else if (! dump_dwarf_section_info
)
5370 dwarf_select_sections_all ();
5371 dump_dwarf (abfd
, is_mainfile
);
5398 free_debug_memory ();
5402 display_object_bfd (bfd
*abfd
)
5406 if (bfd_check_format_matches (abfd
, bfd_object
, &matching
))
5408 dump_bfd (abfd
, true);
5412 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
5414 nonfatal (bfd_get_filename (abfd
));
5415 list_matching_formats (matching
);
5420 if (bfd_get_error () != bfd_error_file_not_recognized
)
5422 nonfatal (bfd_get_filename (abfd
));
5426 if (bfd_check_format_matches (abfd
, bfd_core
, &matching
))
5428 dump_bfd (abfd
, true);
5432 nonfatal (bfd_get_filename (abfd
));
5434 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
5436 list_matching_formats (matching
);
5442 display_any_bfd (bfd
*file
, int level
)
5444 /* Decompress sections unless dumping the section contents. */
5445 if (!dump_section_contents
)
5446 file
->flags
|= BFD_DECOMPRESS
;
5448 /* If the file is an archive, process all of its elements. */
5449 if (bfd_check_format (file
, bfd_archive
))
5452 bfd
*last_arfile
= NULL
;
5455 printf (_("In archive %s:\n"), sanitize_string (bfd_get_filename (file
)));
5456 else if (level
> 100)
5458 /* Prevent corrupted files from spinning us into an
5459 infinite loop. 100 is an arbitrary heuristic. */
5460 fatal (_("Archive nesting is too deep"));
5464 printf (_("In nested archive %s:\n"),
5465 sanitize_string (bfd_get_filename (file
)));
5469 bfd_set_error (bfd_error_no_error
);
5471 arfile
= bfd_openr_next_archived_file (file
, arfile
);
5474 if (bfd_get_error () != bfd_error_no_more_archived_files
)
5475 nonfatal (bfd_get_filename (file
));
5479 display_any_bfd (arfile
, level
+ 1);
5481 if (last_arfile
!= NULL
)
5483 bfd_close (last_arfile
);
5484 /* PR 17512: file: ac585d01. */
5485 if (arfile
== last_arfile
)
5491 last_arfile
= arfile
;
5494 if (last_arfile
!= NULL
)
5495 bfd_close (last_arfile
);
5498 display_object_bfd (file
);
5502 display_file (char *filename
, char *target
, bool last_file
)
5506 if (get_file_size (filename
) < 1)
5512 file
= bfd_openr (filename
, target
);
5515 nonfatal (filename
);
5519 display_any_bfd (file
, 0);
5521 /* This is an optimization to improve the speed of objdump, especially when
5522 dumping a file with lots of associated debug informatiom. Calling
5523 bfd_close on such a file can take a non-trivial amount of time as there
5524 are lots of lists to walk and buffers to free. This is only really
5525 necessary however if we are about to load another file and we need the
5526 memory back. Otherwise, if we are about to exit, then we can save (a lot
5527 of) time by only doing a quick close, and allowing the OS to reclaim the
5532 bfd_close_all_done (file
);
5536 main (int argc
, char **argv
)
5539 char *target
= default_target
;
5540 bool seenflag
= false;
5542 #ifdef HAVE_LC_MESSAGES
5543 setlocale (LC_MESSAGES
, "");
5545 setlocale (LC_CTYPE
, "");
5547 bindtextdomain (PACKAGE
, LOCALEDIR
);
5548 textdomain (PACKAGE
);
5550 program_name
= *argv
;
5551 xmalloc_set_program_name (program_name
);
5552 bfd_set_error_program_name (program_name
);
5554 START_PROGRESS (program_name
, 0);
5556 expandargv (&argc
, &argv
);
5558 if (bfd_init () != BFD_INIT_MAGIC
)
5559 fatal (_("fatal error: libbfd ABI mismatch"));
5560 set_default_bfd_target ();
5562 while ((c
= getopt_long (argc
, argv
,
5563 "CDE:FGHI:LM:P:RSTU:VW::ab:defghij:lm:prstvwxz",
5564 long_options
, (int *) 0))
5570 break; /* We've been given a long option. */
5577 if (disassembler_options
)
5578 /* Ignore potential memory leak for now. */
5579 options
= concat (disassembler_options
, ",",
5580 optarg
, (const char *) NULL
);
5583 disassembler_options
= remove_whitespace_and_extra_commas (options
);
5590 display_file_offsets
= true;
5593 with_line_numbers
= true;
5602 enum demangling_styles style
;
5604 style
= cplus_demangle_name_to_style (optarg
);
5605 if (style
== unknown_demangling
)
5606 fatal (_("unknown demangling style `%s'"),
5609 cplus_demangle_set_style (style
);
5612 case OPTION_RECURSE_LIMIT
:
5613 demangle_flags
&= ~ DMGL_NO_RECURSE_LIMIT
;
5615 case OPTION_NO_RECURSE_LIMIT
:
5616 demangle_flags
|= DMGL_NO_RECURSE_LIMIT
;
5619 do_wide
= wide_output
= true;
5621 case OPTION_ADJUST_VMA
:
5622 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
5624 case OPTION_START_ADDRESS
:
5625 start_address
= parse_vma (optarg
, "--start-address");
5626 if ((stop_address
!= (bfd_vma
) -1) && stop_address
<= start_address
)
5627 fatal (_("error: the start address should be before the end address"));
5629 case OPTION_STOP_ADDRESS
:
5630 stop_address
= parse_vma (optarg
, "--stop-address");
5631 if ((start_address
!= (bfd_vma
) -1) && stop_address
<= start_address
)
5632 fatal (_("error: the stop address should be after the start address"));
5636 prefix_length
= strlen (prefix
);
5637 /* Remove an unnecessary trailing '/' */
5638 while (IS_DIR_SEPARATOR (prefix
[prefix_length
- 1]))
5641 case OPTION_PREFIX_STRIP
:
5642 prefix_strip
= atoi (optarg
);
5643 if (prefix_strip
< 0)
5644 fatal (_("error: prefix strip must be non-negative"));
5646 case OPTION_INSN_WIDTH
:
5647 insn_width
= strtoul (optarg
, NULL
, 0);
5648 if (insn_width
<= 0)
5649 fatal (_("error: instruction width must be positive"));
5651 case OPTION_INLINES
:
5652 unwind_inlines
= true;
5654 case OPTION_VISUALIZE_JUMPS
:
5655 visualize_jumps
= true;
5656 color_output
= false;
5657 extended_color_output
= false;
5660 if (streq (optarg
, "color"))
5661 color_output
= true;
5662 else if (streq (optarg
, "extended-color"))
5664 color_output
= true;
5665 extended_color_output
= true;
5667 else if (streq (optarg
, "off"))
5668 visualize_jumps
= false;
5670 nonfatal (_("unrecognized argument to --visualize-option"));
5673 case OPTION_DISASSEMBLER_COLOR
:
5674 if (streq (optarg
, "off"))
5675 disassembler_color
= false;
5676 else if (streq (optarg
, "color"))
5677 disassembler_color
= true;
5678 else if (streq (optarg
, "extended-color"))
5679 disassembler_extended_color
= true;
5681 nonfatal (_("unrecognized argument to --disassembler-color"));
5684 if (strcmp (optarg
, "B") == 0)
5685 endian
= BFD_ENDIAN_BIG
;
5686 else if (strcmp (optarg
, "L") == 0)
5687 endian
= BFD_ENDIAN_LITTLE
;
5690 nonfatal (_("unrecognized -E option"));
5695 if (strncmp (optarg
, "big", strlen (optarg
)) == 0)
5696 endian
= BFD_ENDIAN_BIG
;
5697 else if (strncmp (optarg
, "little", strlen (optarg
)) == 0)
5698 endian
= BFD_ENDIAN_LITTLE
;
5701 non_fatal (_("unrecognized --endian type `%s'"), optarg
);
5708 dump_file_header
= true;
5712 formats_info
= true;
5716 add_include_path (optarg
);
5719 dump_private_headers
= true;
5723 dump_private_options
= optarg
;
5727 dump_private_headers
= true;
5729 dump_reloc_info
= true;
5730 dump_file_header
= true;
5731 dump_ar_hdrs
= true;
5732 dump_section_headers
= true;
5740 dump_dynamic_symtab
= true;
5746 disasm_sym
= optarg
;
5749 disassemble_zeroes
= true;
5753 disassemble_all
= true;
5758 with_source_code
= true;
5761 case OPTION_SOURCE_COMMENT
:
5763 with_source_code
= true;
5766 source_comment
= xstrdup (sanitize_string (optarg
));
5768 source_comment
= xstrdup ("# ");
5776 dump_debugging_tags
= 1;
5781 process_links
= true;
5782 do_follow_links
= true;
5785 dump_dwarf_section_info
= true;
5788 dwarf_select_sections_by_letters (optarg
);
5790 dwarf_select_sections_all ();
5793 dump_dwarf_section_info
= true;
5796 dwarf_select_sections_by_names (optarg
);
5798 dwarf_select_sections_all ();
5800 case OPTION_DWARF_DEPTH
:
5803 dwarf_cutoff_level
= strtoul (optarg
, & cp
, 0);
5806 case OPTION_DWARF_START
:
5809 dwarf_start_die
= strtoul (optarg
, & cp
, 0);
5810 suppress_bfd_header
= 1;
5813 case OPTION_DWARF_CHECK
:
5816 #ifdef ENABLE_LIBCTF
5818 dump_ctf_section_info
= true;
5820 dump_ctf_section_name
= xstrdup (optarg
);
5823 case OPTION_CTF_PARENT
:
5824 dump_ctf_parent_name
= xstrdup (optarg
);
5828 dump_stab_section_info
= true;
5832 dump_section_contents
= true;
5836 dump_reloc_info
= true;
5840 dump_dynamic_reloc_info
= true;
5844 dump_ar_hdrs
= true;
5848 dump_section_headers
= true;
5853 show_version
= true;
5858 if (streq (optarg
, "default") || streq (optarg
, "d"))
5859 unicode_display
= unicode_default
;
5860 else if (streq (optarg
, "locale") || streq (optarg
, "l"))
5861 unicode_display
= unicode_locale
;
5862 else if (streq (optarg
, "escape") || streq (optarg
, "e"))
5863 unicode_display
= unicode_escape
;
5864 else if (streq (optarg
, "invalid") || streq (optarg
, "i"))
5865 unicode_display
= unicode_invalid
;
5866 else if (streq (optarg
, "hex") || streq (optarg
, "x"))
5867 unicode_display
= unicode_hex
;
5868 else if (streq (optarg
, "highlight") || streq (optarg
, "h"))
5869 unicode_display
= unicode_highlight
;
5871 fatal (_("invalid argument to -U/--unicode: %s"), optarg
);
5876 /* No need to set seenflag or to break - usage() does not return. */
5883 print_version ("objdump");
5888 dump_any_debugging
= (dump_debugging
5889 || dump_dwarf_section_info
5893 exit_status
= display_info ();
5897 display_file ("a.out", target
, true);
5899 for (; optind
< argc
;)
5901 display_file (argv
[optind
], target
, optind
== argc
- 1);
5907 free (dump_ctf_section_name
);
5908 free (dump_ctf_parent_name
);
5909 free ((void *) source_comment
);
5911 END_PROGRESS (program_name
);