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 #ifdef HAVE_LIBDEBUGINFOD
76 #include <elfutils/debuginfod.h>
79 /* Internal headers for the ELF .stab-dump code - sorry. */
80 #define BYTES_IN_WORD 32
81 #include "aout/aout64.h"
84 static int exit_status
= 0;
86 static char *default_target
= NULL
; /* Default at runtime. */
88 /* The following variables are set based on arguments passed on the
90 static int show_version
= 0; /* Show the version number. */
91 static int dump_section_contents
; /* -s */
92 static int dump_section_headers
; /* -h */
93 static bool dump_file_header
; /* -f */
94 static int dump_symtab
; /* -t */
95 static int dump_dynamic_symtab
; /* -T */
96 static int dump_reloc_info
; /* -r */
97 static int dump_dynamic_reloc_info
; /* -R */
98 static int dump_ar_hdrs
; /* -a */
99 static int dump_private_headers
; /* -p */
100 static char *dump_private_options
; /* -P */
101 static int no_addresses
; /* --no-addresses */
102 static int prefix_addresses
; /* --prefix-addresses */
103 static int with_line_numbers
; /* -l */
104 static bool with_source_code
; /* -S */
105 static int show_raw_insn
; /* --show-raw-insn */
106 static int dump_dwarf_section_info
; /* --dwarf */
107 static int dump_stab_section_info
; /* --stabs */
108 static int dump_ctf_section_info
; /* --ctf */
109 static char *dump_ctf_section_name
;
110 static char *dump_ctf_parent_name
; /* --ctf-parent */
111 static int do_demangle
; /* -C, --demangle */
112 static bool disassemble
; /* -d */
113 static bool disassemble_all
; /* -D */
114 static int disassemble_zeroes
; /* --disassemble-zeroes */
115 static bool formats_info
; /* -i */
116 static int wide_output
; /* -w */
117 static int insn_width
; /* --insn-width */
118 static bfd_vma start_address
= (bfd_vma
) -1; /* --start-address */
119 static bfd_vma stop_address
= (bfd_vma
) -1; /* --stop-address */
120 static int dump_debugging
; /* --debugging */
121 static int dump_debugging_tags
; /* --debugging-tags */
122 static int suppress_bfd_header
;
123 static int dump_special_syms
= 0; /* --special-syms */
124 static bfd_vma adjust_section_vma
= 0; /* --adjust-vma */
125 static int file_start_context
= 0; /* --file-start-context */
126 static bool display_file_offsets
; /* -F */
127 static const char *prefix
; /* --prefix */
128 static int prefix_strip
; /* --prefix-strip */
129 static size_t prefix_length
;
130 static bool unwind_inlines
; /* --inlines. */
131 static const char * disasm_sym
; /* Disassembly start symbol. */
132 static const char * source_comment
; /* --source_comment. */
133 static bool visualize_jumps
= false; /* --visualize-jumps. */
134 static bool color_output
= false; /* --visualize-jumps=color. */
135 static bool extended_color_output
= false; /* --visualize-jumps=extended-color. */
136 static int process_links
= false; /* --process-links. */
138 static enum color_selection
140 on_if_terminal_output
,
141 on
, /* --disassembler-color=color. */
142 off
, /* --disassembler-color=off. */
143 extended
/* --disassembler-color=extended-color. */
144 } disassembler_color
= on_if_terminal_output
;
146 static int dump_any_debugging
;
147 static int demangle_flags
= DMGL_ANSI
| DMGL_PARAMS
;
149 /* This is reset to false each time we enter the disassembler, and set true
150 when the disassembler emits something in the dis_style_comment_start
151 style. Once this is true, all further output on that line is done in
152 the comment style. This only has an effect when disassembler coloring
154 static bool disassembler_in_comment
= false;
156 /* A structure to record the sections mentioned in -j switches. */
159 const char *name
; /* The name of the section. */
160 bool seen
; /* A flag to indicate that the section has been found in one or more input files. */
161 struct only
*next
; /* Pointer to the next structure in the list. */
163 /* Pointer to an array of 'only' structures.
164 This pointer is NULL if the -j switch has not been used. */
165 static struct only
* only_list
= NULL
;
167 /* Variables for handling include file path table. */
168 static const char **include_paths
;
169 static int include_path_count
;
171 /* Extra info to pass to the section disassembler and address printing
173 struct objdump_disasm_info
177 disassembler_ftype disassemble_fn
;
182 /* Architecture to disassemble for, or default if NULL. */
183 static char *machine
= NULL
;
185 /* Target specific options to the disassembler. */
186 static char *disassembler_options
= NULL
;
188 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
189 static enum bfd_endian endian
= BFD_ENDIAN_UNKNOWN
;
191 /* The symbol table. */
192 static asymbol
**syms
;
194 /* Number of symbols in `syms'. */
195 static long symcount
= 0;
197 /* The sorted symbol table. */
198 static asymbol
**sorted_syms
;
200 /* Number of symbols in `sorted_syms'. */
201 static long sorted_symcount
= 0;
203 /* The dynamic symbol table. */
204 static asymbol
**dynsyms
;
206 /* The synthetic symbol table. */
207 static asymbol
*synthsyms
;
208 static long synthcount
= 0;
210 /* Number of symbols in `dynsyms'. */
211 static long dynsymcount
= 0;
213 static bfd_byte
*stabs
;
214 static bfd_size_type stab_size
;
216 static bfd_byte
*strtab
;
217 static bfd_size_type stabstr_size
;
219 /* Handlers for -P/--private. */
220 static const struct objdump_private_desc
* const objdump_private_vectors
[] =
222 OBJDUMP_PRIVATE_VECTORS
226 /* The list of detected jumps inside a function. */
227 static struct jump_info
*detected_jumps
= NULL
;
229 typedef enum unicode_display_type
237 } unicode_display_type
;
239 static unicode_display_type unicode_display
= unicode_default
;
241 static void usage (FILE *, int) ATTRIBUTE_NORETURN
;
243 usage (FILE *stream
, int status
)
245 fprintf (stream
, _("Usage: %s <option(s)> <file(s)>\n"), program_name
);
246 fprintf (stream
, _(" Display information from object <file(s)>.\n"));
247 fprintf (stream
, _(" At least one of the following switches must be given:\n"));
248 fprintf (stream
, _("\
249 -a, --archive-headers Display archive header information\n"));
250 fprintf (stream
, _("\
251 -f, --file-headers Display the contents of the overall file header\n"));
252 fprintf (stream
, _("\
253 -p, --private-headers Display object format specific file header contents\n"));
254 fprintf (stream
, _("\
255 -P, --private=OPT,OPT... Display object format specific contents\n"));
256 fprintf (stream
, _("\
257 -h, --[section-]headers Display the contents of the section headers\n"));
258 fprintf (stream
, _("\
259 -x, --all-headers Display the contents of all headers\n"));
260 fprintf (stream
, _("\
261 -d, --disassemble Display assembler contents of executable sections\n"));
262 fprintf (stream
, _("\
263 -D, --disassemble-all Display assembler contents of all sections\n"));
264 fprintf (stream
, _("\
265 --disassemble=<sym> Display assembler contents from <sym>\n"));
266 fprintf (stream
, _("\
267 -S, --source Intermix source code with disassembly\n"));
268 fprintf (stream
, _("\
269 --source-comment[=<txt>] Prefix lines of source code with <txt>\n"));
270 fprintf (stream
, _("\
271 -s, --full-contents Display the full contents of all sections requested\n"));
272 fprintf (stream
, _("\
273 -g, --debugging Display debug information in object file\n"));
274 fprintf (stream
, _("\
275 -e, --debugging-tags Display debug information using ctags style\n"));
276 fprintf (stream
, _("\
277 -G, --stabs Display (in raw form) any STABS info in the file\n"));
278 fprintf (stream
, _("\
279 -W, --dwarf[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
280 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
281 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
282 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
284 Display the contents of DWARF debug sections\n"));
285 fprintf (stream
, _("\
286 -Wk,--dwarf=links Display the contents of sections that link to\n\
287 separate debuginfo files\n"));
288 #if DEFAULT_FOR_FOLLOW_LINKS
289 fprintf (stream
, _("\
290 -WK,--dwarf=follow-links\n\
291 Follow links to separate debug info files (default)\n"));
292 fprintf (stream
, _("\
293 -WN,--dwarf=no-follow-links\n\
294 Do not follow links to separate debug info files\n"));
296 fprintf (stream
, _("\
297 -WK,--dwarf=follow-links\n\
298 Follow links to separate debug info files\n"));
299 fprintf (stream
, _("\
300 -WN,--dwarf=no-follow-links\n\
301 Do not follow links to separate debug info files\n\
304 #if HAVE_LIBDEBUGINFOD
305 fprintf (stream
, _("\
306 -WD --dwarf=use-debuginfod\n\
307 When following links, also query debuginfod servers (default)\n"));
308 fprintf (stream
, _("\
309 -WE --dwarf=do-not-use-debuginfod\n\
310 When following links, do not query debuginfod servers\n"));
312 fprintf (stream
, _("\
313 -L, --process-links Display the contents of non-debug sections in\n\
314 separate debuginfo files. (Implies -WK)\n"));
316 fprintf (stream
, _("\
317 --ctf[=SECTION] Display CTF info from SECTION, (default `.ctf')\n"));
319 fprintf (stream
, _("\
320 -t, --syms Display the contents of the symbol table(s)\n"));
321 fprintf (stream
, _("\
322 -T, --dynamic-syms Display the contents of the dynamic symbol table\n"));
323 fprintf (stream
, _("\
324 -r, --reloc Display the relocation entries in the file\n"));
325 fprintf (stream
, _("\
326 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n"));
327 fprintf (stream
, _("\
328 @<file> Read options from <file>\n"));
329 fprintf (stream
, _("\
330 -v, --version Display this program's version number\n"));
331 fprintf (stream
, _("\
332 -i, --info List object formats and architectures supported\n"));
333 fprintf (stream
, _("\
334 -H, --help Display this information\n"));
338 const struct objdump_private_desc
* const *desc
;
340 fprintf (stream
, _("\n The following switches are optional:\n"));
341 fprintf (stream
, _("\
342 -b, --target=BFDNAME Specify the target object format as BFDNAME\n"));
343 fprintf (stream
, _("\
344 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n"));
345 fprintf (stream
, _("\
346 -j, --section=NAME Only display information for section NAME\n"));
347 fprintf (stream
, _("\
348 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n"));
349 fprintf (stream
, _("\
350 -EB --endian=big Assume big endian format when disassembling\n"));
351 fprintf (stream
, _("\
352 -EL --endian=little Assume little endian format when disassembling\n"));
353 fprintf (stream
, _("\
354 --file-start-context Include context from start of file (with -S)\n"));
355 fprintf (stream
, _("\
356 -I, --include=DIR Add DIR to search list for source files\n"));
357 fprintf (stream
, _("\
358 -l, --line-numbers Include line numbers and filenames in output\n"));
359 fprintf (stream
, _("\
360 -F, --file-offsets Include file offsets when displaying information\n"));
361 fprintf (stream
, _("\
362 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n"));
363 display_demangler_styles (stream
, _("\
365 fprintf (stream
, _("\
366 --recurse-limit Enable a limit on recursion whilst demangling\n\
368 fprintf (stream
, _("\
369 --no-recurse-limit Disable a limit on recursion whilst demangling\n"));
370 fprintf (stream
, _("\
371 -w, --wide Format output for more than 80 columns\n"));
372 fprintf (stream
, _("\
373 -U[d|l|i|x|e|h] Controls the display of UTF-8 unicode characters\n\
374 --unicode=[default|locale|invalid|hex|escape|highlight]\n"));
375 fprintf (stream
, _("\
376 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n"));
377 fprintf (stream
, _("\
378 --start-address=ADDR Only process data whose address is >= ADDR\n"));
379 fprintf (stream
, _("\
380 --stop-address=ADDR Only process data whose address is < ADDR\n"));
381 fprintf (stream
, _("\
382 --no-addresses Do not print address alongside disassembly\n"));
383 fprintf (stream
, _("\
384 --prefix-addresses Print complete address alongside disassembly\n"));
385 fprintf (stream
, _("\
386 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n"));
387 fprintf (stream
, _("\
388 --insn-width=WIDTH Display WIDTH bytes on a single line for -d\n"));
389 fprintf (stream
, _("\
390 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n"));
391 fprintf (stream
, _("\
392 --special-syms Include special symbols in symbol dumps\n"));
393 fprintf (stream
, _("\
394 --inlines Print all inlines for source line (with -l)\n"));
395 fprintf (stream
, _("\
396 --prefix=PREFIX Add PREFIX to absolute paths for -S\n"));
397 fprintf (stream
, _("\
398 --prefix-strip=LEVEL Strip initial directory names for -S\n"));
399 fprintf (stream
, _("\
400 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
401 fprintf (stream
, _("\
402 --dwarf-start=N Display DIEs starting at offset N\n"));
403 fprintf (stream
, _("\
404 --dwarf-check Make additional dwarf consistency checks.\n"));
406 fprintf (stream
, _("\
407 --ctf-parent=NAME Use CTF archive member NAME as the CTF parent\n"));
409 fprintf (stream
, _("\
410 --visualize-jumps Visualize jumps by drawing ASCII art lines\n"));
411 fprintf (stream
, _("\
412 --visualize-jumps=color Use colors in the ASCII art\n"));
413 fprintf (stream
, _("\
414 --visualize-jumps=extended-color\n\
415 Use extended 8-bit color codes\n"));
416 fprintf (stream
, _("\
417 --visualize-jumps=off Disable jump visualization\n\n"));
418 fprintf (stream
, _("\
419 --disassembler-color=off Disable disassembler color output.\n\n"));
420 fprintf (stream
, _("\
421 --disassembler-color=color Use basic colors in disassembler output.\n\n"));
422 fprintf (stream
, _("\
423 --disassembler-color=extended-color Use 8-bit colors in disassembler output.\n\n"));
425 list_supported_targets (program_name
, stream
);
426 list_supported_architectures (program_name
, stream
);
428 disassembler_usage (stream
);
430 if (objdump_private_vectors
[0] != NULL
)
433 _("\nOptions supported for -P/--private switch:\n"));
434 for (desc
= objdump_private_vectors
; *desc
!= NULL
; desc
++)
435 (*desc
)->help (stream
);
438 if (REPORT_BUGS_TO
[0] && status
== 0)
439 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
443 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
447 OPTION_START_ADDRESS
,
457 OPTION_RECURSE_LIMIT
,
458 OPTION_NO_RECURSE_LIMIT
,
460 OPTION_SOURCE_COMMENT
,
465 OPTION_VISUALIZE_JUMPS
,
466 OPTION_DISASSEMBLER_COLOR
469 static struct option long_options
[]=
471 {"adjust-vma", required_argument
, NULL
, OPTION_ADJUST_VMA
},
472 {"all-headers", no_argument
, NULL
, 'x'},
473 {"architecture", required_argument
, NULL
, 'm'},
474 {"archive-headers", no_argument
, NULL
, 'a'},
476 {"ctf", optional_argument
, NULL
, OPTION_CTF
},
477 {"ctf-parent", required_argument
, NULL
, OPTION_CTF_PARENT
},
479 {"debugging", no_argument
, NULL
, 'g'},
480 {"debugging-tags", no_argument
, NULL
, 'e'},
481 {"demangle", optional_argument
, NULL
, 'C'},
482 {"disassemble", optional_argument
, NULL
, 'd'},
483 {"disassemble-all", no_argument
, NULL
, 'D'},
484 {"disassemble-zeroes", no_argument
, NULL
, 'z'},
485 {"disassembler-options", required_argument
, NULL
, 'M'},
486 {"dwarf", optional_argument
, NULL
, OPTION_DWARF
},
487 {"dwarf-check", no_argument
, 0, OPTION_DWARF_CHECK
},
488 {"dwarf-depth", required_argument
, 0, OPTION_DWARF_DEPTH
},
489 {"dwarf-start", required_argument
, 0, OPTION_DWARF_START
},
490 {"dynamic-reloc", no_argument
, NULL
, 'R'},
491 {"dynamic-syms", no_argument
, NULL
, 'T'},
492 {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
493 {"file-headers", no_argument
, NULL
, 'f'},
494 {"file-offsets", no_argument
, NULL
, 'F'},
495 {"file-start-context", no_argument
, &file_start_context
, 1},
496 {"full-contents", no_argument
, NULL
, 's'},
497 {"headers", no_argument
, NULL
, 'h'},
498 {"help", no_argument
, NULL
, 'H'},
499 {"include", required_argument
, NULL
, 'I'},
500 {"info", no_argument
, NULL
, 'i'},
501 {"inlines", no_argument
, 0, OPTION_INLINES
},
502 {"insn-width", required_argument
, NULL
, OPTION_INSN_WIDTH
},
503 {"line-numbers", no_argument
, NULL
, 'l'},
504 {"no-addresses", no_argument
, &no_addresses
, 1},
505 {"no-recurse-limit", no_argument
, NULL
, OPTION_NO_RECURSE_LIMIT
},
506 {"no-recursion-limit", no_argument
, NULL
, OPTION_NO_RECURSE_LIMIT
},
507 {"no-show-raw-insn", no_argument
, &show_raw_insn
, -1},
508 {"prefix", required_argument
, NULL
, OPTION_PREFIX
},
509 {"prefix-addresses", no_argument
, &prefix_addresses
, 1},
510 {"prefix-strip", required_argument
, NULL
, OPTION_PREFIX_STRIP
},
511 {"private", required_argument
, NULL
, 'P'},
512 {"private-headers", no_argument
, NULL
, 'p'},
513 {"process-links", no_argument
, &process_links
, true},
514 {"recurse-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
515 {"recursion-limit", no_argument
, NULL
, OPTION_RECURSE_LIMIT
},
516 {"reloc", no_argument
, NULL
, 'r'},
517 {"section", required_argument
, NULL
, 'j'},
518 {"section-headers", no_argument
, NULL
, 'h'},
519 {"show-raw-insn", no_argument
, &show_raw_insn
, 1},
520 {"source", no_argument
, NULL
, 'S'},
521 {"source-comment", optional_argument
, NULL
, OPTION_SOURCE_COMMENT
},
522 {"special-syms", no_argument
, &dump_special_syms
, 1},
523 {"stabs", no_argument
, NULL
, 'G'},
524 {"start-address", required_argument
, NULL
, OPTION_START_ADDRESS
},
525 {"stop-address", required_argument
, NULL
, OPTION_STOP_ADDRESS
},
526 {"syms", no_argument
, NULL
, 't'},
527 {"target", required_argument
, NULL
, 'b'},
528 {"unicode", required_argument
, NULL
, 'U'},
529 {"version", no_argument
, NULL
, 'V'},
530 {"visualize-jumps", optional_argument
, 0, OPTION_VISUALIZE_JUMPS
},
531 {"wide", no_argument
, NULL
, 'w'},
532 {"disassembler-color", required_argument
, NULL
, OPTION_DISASSEMBLER_COLOR
},
533 {NULL
, no_argument
, NULL
, 0}
537 nonfatal (const char *msg
)
543 /* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
544 The conversion format is controlled by the unicode_display variable.
545 Returns the number of characters added to OUT.
546 Returns the number of bytes consumed from IN in CONSUMED.
547 Always consumes at least one byte and displays at least one character. */
550 display_utf8 (const unsigned char * in
, char * out
, unsigned int * consumed
)
552 char * orig_out
= out
;
553 unsigned int nchars
= 0;
556 if (unicode_display
== unicode_default
)
562 if ((in
[1] & 0xc0) != 0x80)
565 if ((in
[0] & 0x20) == 0)
571 if ((in
[2] & 0xc0) != 0x80)
574 if ((in
[0] & 0x10) == 0)
580 if ((in
[3] & 0xc0) != 0x80)
586 switch (unicode_display
)
589 /* Copy the bytes into the output buffer as is. */
590 memcpy (out
, in
, nchars
);
594 case unicode_invalid
:
596 out
+= sprintf (out
, "%c", unicode_display
== unicode_hex
? '<' : '{');
597 out
+= sprintf (out
, "0x");
598 for (j
= 0; j
< nchars
; j
++)
599 out
+= sprintf (out
, "%02x", in
[j
]);
600 out
+= sprintf (out
, "%c", unicode_display
== unicode_hex
? '>' : '}');
603 case unicode_highlight
:
605 out
+= sprintf (out
, "\x1B[31;47m"); /* Red. */
611 out
+= sprintf (out
, "\\u%02x%02x",
612 ((in
[0] & 0x1c) >> 2),
613 ((in
[0] & 0x03) << 6) | (in
[1] & 0x3f));
617 out
+= sprintf (out
, "\\u%02x%02x",
618 ((in
[0] & 0x0f) << 4) | ((in
[1] & 0x3c) >> 2),
619 ((in
[1] & 0x03) << 6) | ((in
[2] & 0x3f)));
623 out
+= sprintf (out
, "\\u%02x%02x%02x",
624 ((in
[0] & 0x07) << 6) | ((in
[1] & 0x3c) >> 2),
625 ((in
[1] & 0x03) << 6) | ((in
[2] & 0x3c) >> 2),
626 ((in
[2] & 0x03) << 6) | ((in
[3] & 0x3f)));
633 if (unicode_display
== unicode_highlight
&& isatty (1))
634 out
+= sprintf (out
, "\033[0m"); /* Default colour. */
643 return out
- orig_out
;
646 /* Not a valid UTF-8 sequence. */
652 /* Returns a version of IN with any control characters
653 replaced by escape sequences. Uses a static buffer
656 If unicode display is enabled, then also handles the
657 conversion of unicode characters. */
660 sanitize_string (const char * in
)
662 static char * buffer
= NULL
;
663 static size_t buffer_len
= 0;
664 const char * original
= in
;
671 /* See if any conversion is necessary. In the majority
672 of cases it will not be needed. */
675 unsigned char c
= *in
++;
683 if (unicode_display
!= unicode_default
&& c
>= 0xc0)
688 /* Copy the input, translating as needed. */
690 if (buffer_len
< (strlen (in
) * 9))
692 free ((void *) buffer
);
693 buffer_len
= strlen (in
) * 9;
694 buffer
= xmalloc (buffer_len
+ 1);
700 unsigned char c
= *in
++;
710 else if (unicode_display
!= unicode_default
&& c
>= 0xc0)
712 unsigned int num_consumed
;
714 out
+= display_utf8 ((const unsigned char *)(in
- 1), out
, & num_consumed
);
715 in
+= num_consumed
- 1;
727 /* Returns TRUE if the specified section should be dumped. */
730 process_section_p (asection
* section
)
734 if (only_list
== NULL
)
737 for (only
= only_list
; only
; only
= only
->next
)
738 if (strcmp (only
->name
, section
->name
) == 0)
747 /* Add an entry to the 'only' list. */
750 add_only (char * name
)
754 /* First check to make sure that we do not
755 already have an entry for this name. */
756 for (only
= only_list
; only
; only
= only
->next
)
757 if (strcmp (only
->name
, name
) == 0)
760 only
= xmalloc (sizeof * only
);
763 only
->next
= only_list
;
767 /* Release the memory used by the 'only' list.
768 PR 11225: Issue a warning message for unseen sections.
769 Only do this if none of the sections were seen. This is mainly to support
770 tools like the GAS testsuite where an object file is dumped with a list of
771 generic section names known to be present in a range of different file
775 free_only_list (void)
777 bool at_least_one_seen
= false;
781 if (only_list
== NULL
)
784 for (only
= only_list
; only
; only
= only
->next
)
787 at_least_one_seen
= true;
791 for (only
= only_list
; only
; only
= next
)
793 if (! at_least_one_seen
)
795 non_fatal (_("section '%s' mentioned in a -j option, "
796 "but not found in any input file"),
807 dump_section_header (bfd
*abfd
, asection
*section
, void *data
)
810 unsigned int opb
= bfd_octets_per_byte (abfd
, section
);
811 int longest_section_name
= *((int *) data
);
813 /* Ignore linker created section. See elfNN_ia64_object_p in
815 if (section
->flags
& SEC_LINKER_CREATED
)
818 /* PR 10413: Skip sections that we are ignoring. */
819 if (! process_section_p (section
))
822 printf ("%3d %-*s %08lx ", section
->index
, longest_section_name
,
823 sanitize_string (bfd_section_name (section
)),
824 (unsigned long) bfd_section_size (section
) / opb
);
825 bfd_printf_vma (abfd
, bfd_section_vma (section
));
827 bfd_printf_vma (abfd
, section
->lma
);
828 printf (" %08lx 2**%u", (unsigned long) section
->filepos
,
829 bfd_section_alignment (section
));
835 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
837 PF (SEC_HAS_CONTENTS
, "CONTENTS");
838 PF (SEC_ALLOC
, "ALLOC");
839 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
840 PF (SEC_LOAD
, "LOAD");
841 PF (SEC_RELOC
, "RELOC");
842 PF (SEC_READONLY
, "READONLY");
843 PF (SEC_CODE
, "CODE");
844 PF (SEC_DATA
, "DATA");
846 PF (SEC_DEBUGGING
, "DEBUGGING");
847 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
848 PF (SEC_EXCLUDE
, "EXCLUDE");
849 PF (SEC_SORT_ENTRIES
, "SORT_ENTRIES");
850 if (bfd_get_arch (abfd
) == bfd_arch_tic54x
)
852 PF (SEC_TIC54X_BLOCK
, "BLOCK");
853 PF (SEC_TIC54X_CLINK
, "CLINK");
855 PF (SEC_SMALL_DATA
, "SMALL_DATA");
856 if (bfd_get_flavour (abfd
) == bfd_target_coff_flavour
)
858 PF (SEC_COFF_SHARED
, "SHARED");
859 PF (SEC_COFF_NOREAD
, "NOREAD");
861 else if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
863 PF (SEC_ELF_OCTETS
, "OCTETS");
864 PF (SEC_ELF_PURECODE
, "PURECODE");
866 PF (SEC_THREAD_LOCAL
, "THREAD_LOCAL");
867 PF (SEC_GROUP
, "GROUP");
868 if (bfd_get_arch (abfd
) == bfd_arch_mep
)
870 PF (SEC_MEP_VLIW
, "VLIW");
873 if ((section
->flags
& SEC_LINK_ONCE
) != 0)
876 struct coff_comdat_info
*comdat
;
878 switch (section
->flags
& SEC_LINK_DUPLICATES
)
882 case SEC_LINK_DUPLICATES_DISCARD
:
883 ls
= "LINK_ONCE_DISCARD";
885 case SEC_LINK_DUPLICATES_ONE_ONLY
:
886 ls
= "LINK_ONCE_ONE_ONLY";
888 case SEC_LINK_DUPLICATES_SAME_SIZE
:
889 ls
= "LINK_ONCE_SAME_SIZE";
891 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
892 ls
= "LINK_ONCE_SAME_CONTENTS";
895 printf ("%s%s", comma
, ls
);
897 comdat
= bfd_coff_get_comdat_section (abfd
, section
);
899 printf (" (COMDAT %s %ld)", comdat
->name
, comdat
->symbol
);
908 /* Called on each SECTION in ABFD, update the int variable pointed to by
909 DATA which contains the string length of the longest section name. */
912 find_longest_section_name (bfd
*abfd ATTRIBUTE_UNUSED
,
913 asection
*section
, void *data
)
915 int *longest_so_far
= (int *) data
;
919 /* Ignore linker created section. */
920 if (section
->flags
& SEC_LINKER_CREATED
)
923 /* Skip sections that we are ignoring. */
924 if (! process_section_p (section
))
927 name
= bfd_section_name (section
);
928 len
= (int) strlen (name
);
929 if (len
> *longest_so_far
)
930 *longest_so_far
= len
;
934 dump_headers (bfd
*abfd
)
936 /* The default width of 13 is just an arbitrary choice. */
937 int max_section_name_length
= 13;
943 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
944 if (bfd_get_arch_size (abfd
) == 32)
950 printf (_("Sections:\n"));
953 bfd_map_over_sections (abfd
, find_longest_section_name
,
954 &max_section_name_length
);
956 printf (_("Idx %-*s Size %-*s%-*sFile off Algn"),
957 max_section_name_length
, "Name",
958 bfd_vma_width
, "VMA",
959 bfd_vma_width
, "LMA");
962 printf (_(" Flags"));
965 bfd_map_over_sections (abfd
, dump_section_header
,
966 &max_section_name_length
);
970 slurp_symtab (bfd
*abfd
)
975 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
981 storage
= bfd_get_symtab_upper_bound (abfd
);
984 non_fatal (_("failed to read symbol table from: %s"), bfd_get_filename (abfd
));
985 bfd_fatal (_("error message was"));
990 off_t filesize
= bfd_get_file_size (abfd
);
994 && filesize
< storage
995 /* The MMO file format supports its own special compression
996 technique, so its sections can be larger than the file size. */
997 && bfd_get_flavour (abfd
) != bfd_target_mmo_flavour
)
999 bfd_nonfatal_message (bfd_get_filename (abfd
), abfd
, NULL
,
1000 _("error: symbol table size (%#lx) "
1001 "is larger than filesize (%#lx)"),
1002 storage
, (long) filesize
);
1008 sy
= (asymbol
**) xmalloc (storage
);
1011 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
1013 bfd_fatal (bfd_get_filename (abfd
));
1017 /* Read in the dynamic symbols. */
1020 slurp_dynamic_symtab (bfd
*abfd
)
1022 asymbol
**sy
= NULL
;
1025 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
1028 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
1030 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd
));
1036 bfd_fatal (bfd_get_filename (abfd
));
1040 sy
= (asymbol
**) xmalloc (storage
);
1042 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
1043 if (dynsymcount
< 0)
1044 bfd_fatal (bfd_get_filename (abfd
));
1048 /* Some symbol names are significant and should be kept in the
1049 table of sorted symbol names, even if they are marked as
1050 debugging/section symbols. */
1053 is_significant_symbol_name (const char * name
)
1055 return startswith (name
, ".plt") || startswith (name
, ".got");
1058 /* Filter out (in place) symbols that are useless for disassembly.
1059 COUNT is the number of elements in SYMBOLS.
1060 Return the number of useful symbols. */
1063 remove_useless_symbols (asymbol
**symbols
, long count
)
1065 asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
1067 while (--count
>= 0)
1069 asymbol
*sym
= *in_ptr
++;
1071 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
1073 if ((sym
->flags
& (BSF_DEBUGGING
| BSF_SECTION_SYM
))
1074 && ! is_significant_symbol_name (sym
->name
))
1076 if (bfd_is_und_section (sym
->section
)
1077 || bfd_is_com_section (sym
->section
))
1082 return out_ptr
- symbols
;
1085 static const asection
*compare_section
;
1087 /* Sort symbols into value order. */
1090 compare_symbols (const void *ap
, const void *bp
)
1092 const asymbol
*a
= * (const asymbol
**) ap
;
1093 const asymbol
*b
= * (const asymbol
**) bp
;
1098 bool as
, af
, bs
, bf
;
1102 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
1104 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
1107 /* Prefer symbols from the section currently being disassembled.
1108 Don't sort symbols from other sections by section, since there
1109 isn't much reason to prefer one section over another otherwise.
1110 See sym_ok comment for why we compare by section name. */
1111 as
= strcmp (compare_section
->name
, a
->section
->name
) == 0;
1112 bs
= strcmp (compare_section
->name
, b
->section
->name
) == 0;
1118 an
= bfd_asymbol_name (a
);
1119 bn
= bfd_asymbol_name (b
);
1123 /* The symbols gnu_compiled and gcc2_compiled convey no real
1124 information, so put them after other symbols with the same value. */
1125 af
= (strstr (an
, "gnu_compiled") != NULL
1126 || strstr (an
, "gcc2_compiled") != NULL
);
1127 bf
= (strstr (bn
, "gnu_compiled") != NULL
1128 || strstr (bn
, "gcc2_compiled") != NULL
);
1135 /* We use a heuristic for the file name, to try to sort it after
1136 more useful symbols. It may not work on non Unix systems, but it
1137 doesn't really matter; the only difference is precisely which
1138 symbol names get printed. */
1140 #define file_symbol(s, sn, snl) \
1141 (((s)->flags & BSF_FILE) != 0 \
1143 && (sn)[(snl) - 2] == '.' \
1144 && ((sn)[(snl) - 1] == 'o' \
1145 || (sn)[(snl) - 1] == 'a')))
1147 af
= file_symbol (a
, an
, anl
);
1148 bf
= file_symbol (b
, bn
, bnl
);
1155 /* Sort function and object symbols before global symbols before
1156 local symbols before section symbols before debugging symbols. */
1161 if ((aflags
& BSF_DEBUGGING
) != (bflags
& BSF_DEBUGGING
))
1163 if ((aflags
& BSF_DEBUGGING
) != 0)
1168 if ((aflags
& BSF_SECTION_SYM
) != (bflags
& BSF_SECTION_SYM
))
1170 if ((aflags
& BSF_SECTION_SYM
) != 0)
1175 if ((aflags
& BSF_FUNCTION
) != (bflags
& BSF_FUNCTION
))
1177 if ((aflags
& BSF_FUNCTION
) != 0)
1182 if ((aflags
& BSF_OBJECT
) != (bflags
& BSF_OBJECT
))
1184 if ((aflags
& BSF_OBJECT
) != 0)
1189 if ((aflags
& BSF_LOCAL
) != (bflags
& BSF_LOCAL
))
1191 if ((aflags
& BSF_LOCAL
) != 0)
1196 if ((aflags
& BSF_GLOBAL
) != (bflags
& BSF_GLOBAL
))
1198 if ((aflags
& BSF_GLOBAL
) != 0)
1204 if (bfd_get_flavour (bfd_asymbol_bfd (a
)) == bfd_target_elf_flavour
1205 && bfd_get_flavour (bfd_asymbol_bfd (b
)) == bfd_target_elf_flavour
)
1210 if ((a
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0)
1211 asz
= ((elf_symbol_type
*) a
)->internal_elf_sym
.st_size
;
1213 if ((b
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0)
1214 bsz
= ((elf_symbol_type
*) b
)->internal_elf_sym
.st_size
;
1216 return asz
> bsz
? -1 : 1;
1219 /* Symbols that start with '.' might be section names, so sort them
1220 after symbols that don't start with '.'. */
1221 if (an
[0] == '.' && bn
[0] != '.')
1223 if (an
[0] != '.' && bn
[0] == '.')
1226 /* Finally, if we can't distinguish them in any other way, try to
1227 get consistent results by sorting the symbols by name. */
1228 return strcmp (an
, bn
);
1231 /* Sort relocs into address order. */
1234 compare_relocs (const void *ap
, const void *bp
)
1236 const arelent
*a
= * (const arelent
**) ap
;
1237 const arelent
*b
= * (const arelent
**) bp
;
1239 if (a
->address
> b
->address
)
1241 else if (a
->address
< b
->address
)
1244 /* So that associated relocations tied to the same address show up
1245 in the correct order, we don't do any further sorting. */
1254 /* Print an address (VMA) to the output stream in INFO.
1255 If SKIP_ZEROES is TRUE, omit leading zeroes. */
1258 objdump_print_value (bfd_vma vma
, struct disassemble_info
*inf
,
1263 struct objdump_disasm_info
*aux
;
1265 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1266 bfd_sprintf_vma (aux
->abfd
, buf
, vma
);
1271 for (p
= buf
; *p
== '0'; ++p
)
1276 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_address
, "%s", p
);
1279 /* Print the name of a symbol. */
1282 objdump_print_symname (bfd
*abfd
, struct disassemble_info
*inf
,
1286 const char *name
, *version_string
= NULL
;
1287 bool hidden
= false;
1290 name
= bfd_asymbol_name (sym
);
1291 if (do_demangle
&& name
[0] != '\0')
1293 /* Demangle the name. */
1294 alloc
= bfd_demangle (abfd
, name
, demangle_flags
);
1299 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_SYNTHETIC
)) == 0)
1300 version_string
= bfd_get_symbol_version_string (abfd
, sym
, true,
1303 if (bfd_is_und_section (bfd_asymbol_section (sym
)))
1306 name
= sanitize_string (name
);
1310 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_symbol
, "%s", name
);
1311 if (version_string
&& *version_string
!= '\0')
1312 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_symbol
,
1313 hidden
? "@%s" : "@@%s",
1318 printf ("%s", name
);
1319 if (version_string
&& *version_string
!= '\0')
1320 printf (hidden
? "@%s" : "@@%s", version_string
);
1328 sym_ok (bool want_section
,
1329 bfd
*abfd ATTRIBUTE_UNUSED
,
1332 struct disassemble_info
*inf
)
1336 /* NB: An object file can have different sections with the same
1337 section name. Compare compare section pointers if they have
1339 if (sorted_syms
[place
]->section
->owner
== sec
->owner
1340 && sorted_syms
[place
]->section
!= sec
)
1343 /* Note - we cannot just compare section pointers because they could
1344 be different, but the same... Ie the symbol that we are trying to
1345 find could have come from a separate debug info file. Under such
1346 circumstances the symbol will be associated with a section in the
1347 debug info file, whilst the section we want is in a normal file.
1348 So the section pointers will be different, but the section names
1349 will be the same. */
1350 if (strcmp (bfd_section_name (sorted_syms
[place
]->section
),
1351 bfd_section_name (sec
)) != 0)
1355 return inf
->symbol_is_valid (sorted_syms
[place
], inf
);
1358 /* Locate a symbol given a bfd and a section (from INFO->application_data),
1359 and a VMA. If INFO->application_data->require_sec is TRUE, then always
1360 require the symbol to be in the section. Returns NULL if there is no
1361 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
1362 of the symbol in sorted_syms. */
1365 find_symbol_for_address (bfd_vma vma
,
1366 struct disassemble_info
*inf
,
1369 /* @@ Would it speed things up to cache the last two symbols returned,
1370 and maybe their address ranges? For many processors, only one memory
1371 operand can be present at a time, so the 2-entry cache wouldn't be
1372 constantly churned by code doing heavy memory accesses. */
1374 /* Indices in `sorted_syms'. */
1376 long max_count
= sorted_symcount
;
1378 struct objdump_disasm_info
*aux
;
1385 if (sorted_symcount
< 1)
1388 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1391 opb
= inf
->octets_per_byte
;
1393 /* Perform a binary search looking for the closest symbol to the
1394 required value. We are searching the range (min, max_count]. */
1395 while (min
+ 1 < max_count
)
1399 thisplace
= (max_count
+ min
) / 2;
1400 sym
= sorted_syms
[thisplace
];
1402 if (bfd_asymbol_value (sym
) > vma
)
1403 max_count
= thisplace
;
1404 else if (bfd_asymbol_value (sym
) < vma
)
1413 /* The symbol we want is now in min, the low end of the range we
1414 were searching. If there are several symbols with the same
1415 value, we want the first one. */
1417 while (thisplace
> 0
1418 && (bfd_asymbol_value (sorted_syms
[thisplace
])
1419 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
1422 /* Prefer a symbol in the current section if we have multple symbols
1423 with the same value, as can occur with overlays or zero size
1426 while (min
< max_count
1427 && (bfd_asymbol_value (sorted_syms
[min
])
1428 == bfd_asymbol_value (sorted_syms
[thisplace
])))
1430 if (sym_ok (true, abfd
, min
, sec
, inf
))
1437 return sorted_syms
[thisplace
];
1442 /* If the file is relocatable, and the symbol could be from this
1443 section, prefer a symbol from this section over symbols from
1444 others, even if the other symbol's value might be closer.
1446 Note that this may be wrong for some symbol references if the
1447 sections have overlapping memory ranges, but in that case there's
1448 no way to tell what's desired without looking at the relocation
1451 Also give the target a chance to reject symbols. */
1452 want_section
= (aux
->require_sec
1453 || ((abfd
->flags
& HAS_RELOC
) != 0
1454 && vma
>= bfd_section_vma (sec
)
1455 && vma
< (bfd_section_vma (sec
)
1456 + bfd_section_size (sec
) / opb
)));
1458 if (! sym_ok (want_section
, abfd
, thisplace
, sec
, inf
))
1461 long newplace
= sorted_symcount
;
1463 for (i
= min
- 1; i
>= 0; i
--)
1465 if (sym_ok (want_section
, abfd
, i
, sec
, inf
))
1467 if (newplace
== sorted_symcount
)
1470 if (bfd_asymbol_value (sorted_syms
[i
])
1471 != bfd_asymbol_value (sorted_syms
[newplace
]))
1474 /* Remember this symbol and keep searching until we reach
1475 an earlier address. */
1480 if (newplace
!= sorted_symcount
)
1481 thisplace
= newplace
;
1484 /* We didn't find a good symbol with a smaller value.
1485 Look for one with a larger value. */
1486 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
1488 if (sym_ok (want_section
, abfd
, i
, sec
, inf
))
1496 if (! sym_ok (want_section
, abfd
, thisplace
, sec
, inf
))
1497 /* There is no suitable symbol. */
1501 /* If we have not found an exact match for the specified address
1502 and we have dynamic relocations available, then we can produce
1503 a better result by matching a relocation to the address and
1504 using the symbol associated with that relocation. */
1505 rel_count
= inf
->dynrelcount
;
1507 && sorted_syms
[thisplace
]->value
!= vma
1509 && inf
->dynrelbuf
!= NULL
1510 && inf
->dynrelbuf
[0]->address
<= vma
1511 && inf
->dynrelbuf
[rel_count
- 1]->address
>= vma
1512 /* If we have matched a synthetic symbol, then stick with that. */
1513 && (sorted_syms
[thisplace
]->flags
& BSF_SYNTHETIC
) == 0)
1516 arelent
** rel_high
;
1518 rel_low
= inf
->dynrelbuf
;
1519 rel_high
= rel_low
+ rel_count
- 1;
1520 while (rel_low
<= rel_high
)
1522 arelent
**rel_mid
= &rel_low
[(rel_high
- rel_low
) / 2];
1523 arelent
* rel
= *rel_mid
;
1525 if (rel
->address
== vma
)
1527 /* Absolute relocations do not provide a more helpful
1528 symbolic address. Find a non-absolute relocation
1529 with the same address. */
1530 arelent
**rel_vma
= rel_mid
;
1532 rel_mid
>= rel_low
&& rel_mid
[0]->address
== vma
;
1536 for (; rel_vma
<= rel_high
&& rel_vma
[0]->address
== vma
;
1540 if (rel
->sym_ptr_ptr
!= NULL
1541 && ! bfd_is_abs_section ((* rel
->sym_ptr_ptr
)->section
))
1544 * place
= thisplace
;
1545 return * rel
->sym_ptr_ptr
;
1551 if (vma
< rel
->address
)
1553 else if (vma
>= rel_mid
[1]->address
)
1554 rel_low
= rel_mid
+ 1;
1563 return sorted_syms
[thisplace
];
1566 /* Print an address and the offset to the nearest symbol. */
1569 objdump_print_addr_with_sym (bfd
*abfd
, asection
*sec
, asymbol
*sym
,
1570 bfd_vma vma
, struct disassemble_info
*inf
,
1575 objdump_print_value (vma
, inf
, skip_zeroes
);
1576 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, " ");
1583 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
,"<");
1584 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_symbol
, "%s",
1585 sanitize_string (bfd_section_name (sec
)));
1586 secaddr
= bfd_section_vma (sec
);
1589 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
,
1591 objdump_print_value (secaddr
- vma
, inf
, true);
1593 else if (vma
> secaddr
)
1595 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
, "+0x");
1596 objdump_print_value (vma
- secaddr
, inf
, true);
1598 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, ">");
1602 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, "<");
1604 objdump_print_symname (abfd
, inf
, sym
);
1606 if (bfd_asymbol_value (sym
) == vma
)
1608 /* Undefined symbols in an executables and dynamic objects do not have
1609 a value associated with them, so it does not make sense to display
1610 an offset relative to them. Normally we would not be provided with
1611 this kind of symbol, but the target backend might choose to do so,
1612 and the code in find_symbol_for_address might return an as yet
1613 unresolved symbol associated with a dynamic reloc. */
1614 else if ((bfd_get_file_flags (abfd
) & (EXEC_P
| DYNAMIC
))
1615 && bfd_is_und_section (sym
->section
))
1617 else if (bfd_asymbol_value (sym
) > vma
)
1619 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
,"-0x");
1620 objdump_print_value (bfd_asymbol_value (sym
) - vma
, inf
, true);
1622 else if (vma
> bfd_asymbol_value (sym
))
1624 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_immediate
, "+0x");
1625 objdump_print_value (vma
- bfd_asymbol_value (sym
), inf
, true);
1628 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_text
, ">");
1631 if (display_file_offsets
)
1632 inf
->fprintf_styled_func (inf
->stream
, dis_style_text
,
1633 _(" (File Offset: 0x%lx)"),
1634 (long int)(sec
->filepos
+ (vma
- sec
->vma
)));
1637 /* Print an address (VMA), symbolically if possible.
1638 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
1641 objdump_print_addr (bfd_vma vma
,
1642 struct disassemble_info
*inf
,
1645 struct objdump_disasm_info
*aux
;
1646 asymbol
*sym
= NULL
;
1647 bool skip_find
= false;
1649 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1651 if (sorted_symcount
< 1)
1655 (*inf
->fprintf_styled_func
) (inf
->stream
, dis_style_address
, "0x");
1656 objdump_print_value (vma
, inf
, skip_zeroes
);
1659 if (display_file_offsets
)
1660 inf
->fprintf_styled_func (inf
->stream
, dis_style_text
,
1661 _(" (File Offset: 0x%lx)"),
1662 (long int) (inf
->section
->filepos
1663 + (vma
- inf
->section
->vma
)));
1667 if (aux
->reloc
!= NULL
1668 && aux
->reloc
->sym_ptr_ptr
!= NULL
1669 && * aux
->reloc
->sym_ptr_ptr
!= NULL
)
1671 sym
= * aux
->reloc
->sym_ptr_ptr
;
1673 /* Adjust the vma to the reloc. */
1674 vma
+= bfd_asymbol_value (sym
);
1676 if (bfd_is_und_section (bfd_asymbol_section (sym
)))
1681 sym
= find_symbol_for_address (vma
, inf
, NULL
);
1683 objdump_print_addr_with_sym (aux
->abfd
, inf
->section
, sym
, vma
, inf
,
1687 /* Print VMA to INFO. This function is passed to the disassembler
1691 objdump_print_address (bfd_vma vma
, struct disassemble_info
*inf
)
1693 objdump_print_addr (vma
, inf
, ! prefix_addresses
);
1696 /* Determine if the given address has a symbol associated with it. */
1699 objdump_symbol_at_address (bfd_vma vma
, struct disassemble_info
* inf
)
1703 sym
= find_symbol_for_address (vma
, inf
, NULL
);
1704 if (sym
!= NULL
&& bfd_asymbol_value (sym
) == vma
)
1710 /* Hold the last function name and the last line number we displayed
1711 in a disassembly. */
1713 static char *prev_functionname
;
1714 static unsigned int prev_line
;
1715 static unsigned int prev_discriminator
;
1717 /* We keep a list of all files that we have seen when doing a
1718 disassembly with source, so that we know how much of the file to
1719 display. This can be important for inlined functions. */
1721 struct print_file_list
1723 struct print_file_list
*next
;
1724 const char *filename
;
1725 const char *modname
;
1728 const char **linemap
;
1731 unsigned max_printed
;
1735 static struct print_file_list
*print_files
;
1737 /* The number of preceding context lines to show when we start
1738 displaying a file for the first time. */
1740 #define SHOW_PRECEDING_CONTEXT_LINES (5)
1742 #if HAVE_LIBDEBUGINFOD
1743 /* Return a hex string represention of the build-id. */
1746 get_build_id (void * data
)
1749 char * build_id_str
;
1750 bfd
* abfd
= (bfd
*) data
;
1751 const struct bfd_build_id
* build_id
;
1753 build_id
= abfd
->build_id
;
1754 if (build_id
== NULL
)
1757 build_id_str
= malloc (build_id
->size
* 2 + 1);
1758 if (build_id_str
== NULL
)
1761 for (i
= 0; i
< build_id
->size
; i
++)
1762 sprintf (build_id_str
+ (i
* 2), "%02x", build_id
->data
[i
]);
1763 build_id_str
[build_id
->size
* 2] = '\0';
1765 return (unsigned char *) build_id_str
;
1768 /* Search for a separate debug file matching ABFD's build-id. */
1771 find_separate_debug (const bfd
* abfd
)
1773 const struct bfd_build_id
* build_id
= abfd
->build_id
;
1774 separate_info
* i
= first_separate_info
;
1776 if (build_id
== NULL
|| i
== NULL
)
1781 const bfd
* i_bfd
= (bfd
*) i
->handle
;
1783 if (abfd
!= NULL
&& i_bfd
->build_id
!= NULL
)
1785 const unsigned char * data
= i_bfd
->build_id
->data
;
1786 size_t size
= i_bfd
->build_id
->size
;
1788 if (size
== build_id
->size
1789 && memcmp (data
, build_id
->data
, size
) == 0)
1790 return (bfd
*) i
->handle
;
1799 /* Search for a separate debug file matching ABFD's .gnu_debugaltlink
1803 find_alt_debug (const bfd
* abfd
)
1808 struct dwarf_section
* section
;
1809 const struct bfd_build_id
* build_id
= abfd
->build_id
;
1810 separate_info
* i
= first_separate_info
;
1814 || !load_debug_section (gnu_debugaltlink
, (void *) abfd
))
1817 section
= &debug_displays
[gnu_debugaltlink
].section
;
1818 if (section
== NULL
)
1821 name
= (const char *) section
->start
;
1822 namelen
= strnlen (name
, section
->size
) + 1;
1825 if (namelen
>= section
->size
)
1828 id_len
= section
->size
- namelen
;
1832 /* Compare the .gnu_debugaltlink build-id with the build-ids of the
1833 known separate_info files. */
1836 const bfd
* i_bfd
= (bfd
*) i
->handle
;
1838 if (i_bfd
!= NULL
&& i_bfd
->build_id
!= NULL
)
1840 const unsigned char * data
= i_bfd
->build_id
->data
;
1841 size_t size
= i_bfd
->build_id
->size
;
1844 && memcmp (section
->start
+ namelen
, data
, size
) == 0)
1845 return (bfd
*) i
->handle
;
1854 #endif /* HAVE_LIBDEBUGINFOD */
1856 /* Reads the contents of file FN into memory. Returns a pointer to the buffer.
1857 Also returns the size of the buffer in SIZE_RETURN and a filled out
1858 stat structure in FST_RETURN. Returns NULL upon failure. */
1861 slurp_file (const char * fn
,
1862 size_t * size_return
,
1863 struct stat
* fst_return
,
1864 bfd
* abfd ATTRIBUTE_UNUSED
)
1874 if (fn
== NULL
|| * fn
== 0 || size_return
== NULL
|| fst_return
== NULL
)
1877 fd
= open (fn
, O_RDONLY
| O_BINARY
);
1879 #if HAVE_LIBDEBUGINFOD
1880 if (fd
< 0 && use_debuginfod
&& fn
[0] == '/' && abfd
!= NULL
)
1882 unsigned char * build_id
;
1883 debuginfod_client
* client
;
1885 client
= debuginfod_begin ();
1889 build_id
= get_build_id (abfd
);
1890 fd
= debuginfod_find_source (client
, build_id
, 0, fn
, NULL
);
1892 debuginfod_end (client
);
1899 if (fstat (fd
, fst_return
) < 0)
1905 *size_return
= fst_return
->st_size
;
1908 ps
= getpagesize ();
1909 msize
= (*size_return
+ ps
- 1) & ~(ps
- 1);
1910 map
= mmap (NULL
, msize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1911 if (map
!= (char *) -1L)
1918 map
= (const char *) malloc (*size_return
);
1919 if (!map
|| (size_t) read (fd
, (char *) map
, *size_return
) != *size_return
)
1921 free ((void *) map
);
1928 #define line_map_decrease 5
1930 /* Precompute array of lines for a mapped file. */
1932 static const char **
1933 index_file (const char *map
, size_t size
, unsigned int *maxline
)
1935 const char *p
, *lstart
, *end
;
1936 int chars_per_line
= 45; /* First iteration will use 40. */
1937 unsigned int lineno
;
1938 const char **linemap
= NULL
;
1939 unsigned long line_map_size
= 0;
1945 for (p
= map
; p
< end
; p
++)
1949 if (p
+ 1 < end
&& p
[1] == '\r')
1952 else if (*p
== '\r')
1954 if (p
+ 1 < end
&& p
[1] == '\n')
1960 /* End of line found. */
1962 if (linemap
== NULL
|| line_map_size
< lineno
+ 1)
1964 unsigned long newsize
;
1966 chars_per_line
-= line_map_decrease
;
1967 if (chars_per_line
<= 1)
1969 line_map_size
= size
/ chars_per_line
+ 1;
1970 if (line_map_size
< lineno
+ 1)
1971 line_map_size
= lineno
+ 1;
1972 newsize
= line_map_size
* sizeof (char *);
1973 linemap
= (const char **) xrealloc (linemap
, newsize
);
1976 linemap
[lineno
++] = lstart
;
1984 /* Tries to open MODNAME, and if successful adds a node to print_files
1985 linked list and returns that node. Also fills in the stat structure
1986 pointed to by FST_RETURN. Returns NULL on failure. */
1988 static struct print_file_list
*
1989 try_print_file_open (const char * origname
,
1990 const char * modname
,
1991 struct stat
* fst_return
,
1994 struct print_file_list
*p
;
1996 p
= (struct print_file_list
*) xmalloc (sizeof (struct print_file_list
));
1998 p
->map
= slurp_file (modname
, &p
->mapsize
, fst_return
, abfd
);
2005 p
->linemap
= index_file (p
->map
, p
->mapsize
, &p
->maxline
);
2008 p
->filename
= origname
;
2009 p
->modname
= modname
;
2010 p
->next
= print_files
;
2016 /* If the source file, as described in the symtab, is not found
2017 try to locate it in one of the paths specified with -I
2018 If found, add location to print_files linked list. */
2020 static struct print_file_list
*
2021 update_source_path (const char *filename
, bfd
*abfd
)
2023 struct print_file_list
*p
;
2028 p
= try_print_file_open (filename
, filename
, &fst
, abfd
);
2031 if (include_path_count
== 0)
2034 /* Get the name of the file. */
2035 fname
= lbasename (filename
);
2037 /* If file exists under a new path, we need to add it to the list
2038 so that show_line knows about it. */
2039 for (i
= 0; i
< include_path_count
; i
++)
2041 char *modname
= concat (include_paths
[i
], "/", fname
,
2044 p
= try_print_file_open (filename
, modname
, &fst
, abfd
);
2054 long mtime
= bfd_get_mtime (abfd
);
2056 if (fst
.st_mtime
> mtime
)
2057 warn (_("source file %s is more recent than object file\n"),
2064 /* Print a source file line. */
2067 print_line (struct print_file_list
*p
, unsigned int linenum
)
2072 if (linenum
>= p
->maxline
)
2074 l
= p
->linemap
[linenum
];
2075 if (source_comment
!= NULL
&& strlen (l
) > 0)
2076 printf ("%s", source_comment
);
2077 len
= strcspn (l
, "\n\r");
2078 /* Test fwrite return value to quiet glibc warning. */
2079 if (len
== 0 || fwrite (l
, len
, 1, stdout
) == 1)
2083 /* Print a range of source code lines. */
2086 dump_lines (struct print_file_list
*p
, unsigned int start
, unsigned int end
)
2094 print_line (p
, start
);
2099 /* Show the line number, or the source line, in a disassembly
2103 show_line (bfd
*abfd
, asection
*section
, bfd_vma addr_offset
)
2105 const char *filename
;
2106 const char *functionname
;
2107 unsigned int linenumber
;
2108 unsigned int discriminator
;
2112 if (! with_line_numbers
&& ! with_source_code
)
2115 #ifdef HAVE_LIBDEBUGINFOD
2118 const char *alt_filename
= NULL
;
2124 /* PR 29075: Check for separate debuginfo and .gnu_debugaltlink files.
2125 They need to be passed to bfd_find_nearest_line_with_alt in case they
2126 were downloaded from debuginfod. Otherwise libbfd will attempt to
2127 search for them and fail to locate them. */
2128 debug_bfd
= find_separate_debug (abfd
);
2129 if (debug_bfd
== NULL
)
2132 alt_bfd
= find_alt_debug (debug_bfd
);
2133 if (alt_bfd
!= NULL
)
2134 alt_filename
= bfd_get_filename (alt_bfd
);
2139 bfd_set_error (bfd_error_no_error
);
2140 if (! bfd_find_nearest_line_with_alt (debug_bfd
, alt_filename
,
2142 addr_offset
, &filename
,
2143 &functionname
, &linenumber
,
2146 if (bfd_get_error () == bfd_error_no_error
)
2148 if (! bfd_find_nearest_line_discriminator (abfd
, section
, syms
,
2149 addr_offset
, &filename
,
2150 &functionname
, &linenumber
,
2156 if (! bfd_find_nearest_line_discriminator (abfd
, section
, syms
, addr_offset
,
2157 &filename
, &functionname
,
2158 &linenumber
, &discriminator
))
2162 if (filename
!= NULL
&& *filename
== '\0')
2164 if (functionname
!= NULL
&& *functionname
== '\0')
2165 functionname
= NULL
;
2168 && IS_ABSOLUTE_PATH (filename
)
2172 const char *fname
= filename
;
2174 path
= xmalloc (prefix_length
+ 1 + strlen (filename
));
2177 memcpy (path
, prefix
, prefix_length
);
2178 path_up
= path
+ prefix_length
;
2180 /* Build relocated filename, stripping off leading directories
2181 from the initial filename if requested. */
2182 if (prefix_strip
> 0)
2187 /* Skip selected directory levels. */
2188 for (s
= fname
+ 1; *s
!= '\0' && level
< prefix_strip
; s
++)
2189 if (IS_DIR_SEPARATOR (*s
))
2196 /* Update complete filename. */
2197 strcpy (path_up
, fname
);
2205 if (with_line_numbers
)
2207 if (functionname
!= NULL
2208 && (prev_functionname
== NULL
2209 || strcmp (functionname
, prev_functionname
) != 0))
2211 char *demangle_alloc
= NULL
;
2212 if (do_demangle
&& functionname
[0] != '\0')
2214 /* Demangle the name. */
2215 demangle_alloc
= bfd_demangle (abfd
, functionname
,
2219 /* Demangling adds trailing parens, so don't print those. */
2220 if (demangle_alloc
!= NULL
)
2221 printf ("%s:\n", sanitize_string (demangle_alloc
));
2223 printf ("%s():\n", sanitize_string (functionname
));
2226 free (demangle_alloc
);
2229 && (linenumber
!= prev_line
2230 || discriminator
!= prev_discriminator
))
2232 if (discriminator
> 0)
2233 printf ("%s:%u (discriminator %u)\n",
2234 filename
== NULL
? "???" : sanitize_string (filename
),
2235 linenumber
, discriminator
);
2237 printf ("%s:%u\n", filename
== NULL
2238 ? "???" : sanitize_string (filename
),
2243 const char *filename2
;
2244 const char *functionname2
;
2247 while (bfd_find_inliner_info (abfd
, &filename2
, &functionname2
,
2250 printf ("inlined by %s:%u",
2251 sanitize_string (filename2
), line2
);
2252 printf (" (%s)\n", sanitize_string (functionname2
));
2257 if (with_source_code
2261 struct print_file_list
**pp
, *p
;
2264 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
2265 if (filename_cmp ((*pp
)->filename
, filename
) == 0)
2272 filename
= xstrdup (filename
);
2273 p
= update_source_path (filename
, abfd
);
2276 if (p
!= NULL
&& linenumber
!= p
->last_line
)
2278 if (file_start_context
&& p
->first
)
2282 l
= linenumber
- SHOW_PRECEDING_CONTEXT_LINES
;
2283 if (l
>= linenumber
)
2285 if (p
->max_printed
>= l
)
2287 if (p
->max_printed
< linenumber
)
2288 l
= p
->max_printed
+ 1;
2293 dump_lines (p
, l
, linenumber
);
2294 if (p
->max_printed
< linenumber
)
2295 p
->max_printed
= linenumber
;
2296 p
->last_line
= linenumber
;
2301 if (functionname
!= NULL
2302 && (prev_functionname
== NULL
2303 || strcmp (functionname
, prev_functionname
) != 0))
2305 if (prev_functionname
!= NULL
)
2306 free (prev_functionname
);
2307 prev_functionname
= (char *) xmalloc (strlen (functionname
) + 1);
2308 strcpy (prev_functionname
, functionname
);
2311 if (linenumber
> 0 && linenumber
!= prev_line
)
2312 prev_line
= linenumber
;
2314 if (discriminator
!= prev_discriminator
)
2315 prev_discriminator
= discriminator
;
2321 /* Pseudo FILE object for strings. */
2329 /* sprintf to a "stream". */
2331 static int ATTRIBUTE_PRINTF_2
2332 objdump_sprintf (SFILE
*f
, const char *format
, ...)
2339 size_t space
= f
->alloc
- f
->pos
;
2341 va_start (args
, format
);
2342 n
= vsnprintf (f
->buffer
+ f
->pos
, space
, format
, args
);
2348 f
->alloc
= (f
->alloc
+ n
) * 2;
2349 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2356 /* Return an integer greater than, or equal to zero, representing the color
2357 for STYLE, or -1 if no color should be used. */
2360 objdump_color_for_disassembler_style (enum disassembler_style style
)
2364 if (style
== dis_style_comment_start
)
2365 disassembler_in_comment
= true;
2367 if (disassembler_color
== on
)
2369 if (disassembler_in_comment
)
2374 case dis_style_symbol
:
2377 case dis_style_assembler_directive
:
2378 case dis_style_sub_mnemonic
:
2379 case dis_style_mnemonic
:
2382 case dis_style_register
:
2385 case dis_style_address
:
2386 case dis_style_address_offset
:
2387 case dis_style_immediate
:
2391 case dis_style_text
:
2396 else if (disassembler_color
== extended
)
2398 if (disassembler_in_comment
)
2403 case dis_style_symbol
:
2406 case dis_style_assembler_directive
:
2407 case dis_style_sub_mnemonic
:
2408 case dis_style_mnemonic
:
2411 case dis_style_register
:
2414 case dis_style_address
:
2415 case dis_style_address_offset
:
2416 case dis_style_immediate
:
2420 case dis_style_text
:
2425 else if (disassembler_color
!= off
)
2426 bfd_fatal (_("disassembly color not correctly selected"));
2431 /* Like objdump_sprintf, but add in escape sequences to highlight the
2432 content according to STYLE. */
2434 static int ATTRIBUTE_PRINTF_3
2435 objdump_styled_sprintf (SFILE
*f
, enum disassembler_style style
,
2436 const char *format
, ...)
2440 int color
= objdump_color_for_disassembler_style (style
);
2446 size_t space
= f
->alloc
- f
->pos
;
2448 if (disassembler_color
== on
)
2449 n
= snprintf (f
->buffer
+ f
->pos
, space
, "\033[%dm", color
);
2451 n
= snprintf (f
->buffer
+ f
->pos
, space
, "\033[38;5;%dm", color
);
2455 f
->alloc
= (f
->alloc
+ n
) * 2;
2456 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2463 size_t space
= f
->alloc
- f
->pos
;
2465 va_start (args
, format
);
2466 n
= vsnprintf (f
->buffer
+ f
->pos
, space
, format
, args
);
2472 f
->alloc
= (f
->alloc
+ n
) * 2;
2473 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2481 size_t space
= f
->alloc
- f
->pos
;
2483 n
= snprintf (f
->buffer
+ f
->pos
, space
, "\033[0m");
2488 f
->alloc
= (f
->alloc
+ n
) * 2;
2489 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
2497 /* We discard the styling information here. This function is only used
2498 when objdump is printing auxiliary information, the symbol headers, and
2499 disassembly address, or the bytes of the disassembled instruction. We
2500 don't (currently) apply styling to any of this stuff, so, for now, just
2501 print the content with no additional style added. */
2503 static int ATTRIBUTE_PRINTF_3
2504 fprintf_styled (FILE *f
, enum disassembler_style style ATTRIBUTE_UNUSED
,
2505 const char *fmt
, ...)
2511 res
= vfprintf (f
, fmt
, ap
);
2517 /* Code for generating (colored) diagrams of control flow start and end
2520 /* Structure used to store the properties of a jump. */
2524 /* The next jump, or NULL if this is the last object. */
2525 struct jump_info
*next
;
2526 /* The previous jump, or NULL if this is the first object. */
2527 struct jump_info
*prev
;
2528 /* The start addresses of the jump. */
2531 /* The list of start addresses. */
2533 /* The number of elements. */
2535 /* The maximum number of elements that fit into the array. */
2538 /* The end address of the jump. */
2540 /* The drawing level of the jump. */
2544 /* Construct a jump object for a jump from start
2545 to end with the corresponding level. */
2547 static struct jump_info
*
2548 jump_info_new (bfd_vma start
, bfd_vma end
, int level
)
2550 struct jump_info
*result
= xmalloc (sizeof (struct jump_info
));
2552 result
->next
= NULL
;
2553 result
->prev
= NULL
;
2554 result
->start
.addresses
= xmalloc (sizeof (bfd_vma
*) * 2);
2555 result
->start
.addresses
[0] = start
;
2556 result
->start
.count
= 1;
2557 result
->start
.max_count
= 2;
2559 result
->level
= level
;
2564 /* Free a jump object and return the next object
2565 or NULL if this was the last one. */
2567 static struct jump_info
*
2568 jump_info_free (struct jump_info
*ji
)
2570 struct jump_info
*result
= NULL
;
2575 if (ji
->start
.addresses
)
2576 free (ji
->start
.addresses
);
2583 /* Get the smallest value of all start and end addresses. */
2586 jump_info_min_address (const struct jump_info
*ji
)
2588 bfd_vma min_address
= ji
->end
;
2591 for (i
= ji
->start
.count
; i
-- > 0;)
2592 if (ji
->start
.addresses
[i
] < min_address
)
2593 min_address
= ji
->start
.addresses
[i
];
2597 /* Get the largest value of all start and end addresses. */
2600 jump_info_max_address (const struct jump_info
*ji
)
2602 bfd_vma max_address
= ji
->end
;
2605 for (i
= ji
->start
.count
; i
-- > 0;)
2606 if (ji
->start
.addresses
[i
] > max_address
)
2607 max_address
= ji
->start
.addresses
[i
];
2611 /* Get the target address of a jump. */
2614 jump_info_end_address (const struct jump_info
*ji
)
2619 /* Test if an address is one of the start addresses of a jump. */
2622 jump_info_is_start_address (const struct jump_info
*ji
, bfd_vma address
)
2624 bool result
= false;
2627 for (i
= ji
->start
.count
; i
-- > 0;)
2628 if (address
== ji
->start
.addresses
[i
])
2637 /* Test if an address is the target address of a jump. */
2640 jump_info_is_end_address (const struct jump_info
*ji
, bfd_vma address
)
2642 return (address
== ji
->end
);
2645 /* Get the difference between the smallest and largest address of a jump. */
2648 jump_info_size (const struct jump_info
*ji
)
2650 return jump_info_max_address (ji
) - jump_info_min_address (ji
);
2653 /* Unlink a jump object from a list. */
2656 jump_info_unlink (struct jump_info
*node
,
2657 struct jump_info
**base
)
2660 node
->next
->prev
= node
->prev
;
2662 node
->prev
->next
= node
->next
;
2669 /* Insert unlinked jump info node into a list. */
2672 jump_info_insert (struct jump_info
*node
,
2673 struct jump_info
*target
,
2674 struct jump_info
**base
)
2676 node
->next
= target
;
2677 node
->prev
= target
->prev
;
2678 target
->prev
= node
;
2680 node
->prev
->next
= node
;
2685 /* Add unlinked node to the front of a list. */
2688 jump_info_add_front (struct jump_info
*node
,
2689 struct jump_info
**base
)
2693 node
->next
->prev
= node
;
2698 /* Move linked node to target position. */
2701 jump_info_move_linked (struct jump_info
*node
,
2702 struct jump_info
*target
,
2703 struct jump_info
**base
)
2706 jump_info_unlink (node
, base
);
2707 /* Insert node at target position. */
2708 jump_info_insert (node
, target
, base
);
2711 /* Test if two jumps intersect. */
2714 jump_info_intersect (const struct jump_info
*a
,
2715 const struct jump_info
*b
)
2717 return ((jump_info_max_address (a
) >= jump_info_min_address (b
))
2718 && (jump_info_min_address (a
) <= jump_info_max_address (b
)));
2721 /* Merge two compatible jump info objects. */
2724 jump_info_merge (struct jump_info
**base
)
2726 struct jump_info
*a
;
2728 for (a
= *base
; a
; a
= a
->next
)
2730 struct jump_info
*b
;
2732 for (b
= a
->next
; b
; b
= b
->next
)
2734 /* Merge both jumps into one. */
2735 if (a
->end
== b
->end
)
2737 /* Reallocate addresses. */
2738 size_t needed_size
= a
->start
.count
+ b
->start
.count
;
2741 if (needed_size
> a
->start
.max_count
)
2743 a
->start
.max_count
+= b
->start
.max_count
;
2744 a
->start
.addresses
=
2745 xrealloc (a
->start
.addresses
,
2746 a
->start
.max_count
* sizeof (bfd_vma
*));
2749 /* Append start addresses. */
2750 for (i
= 0; i
< b
->start
.count
; ++i
)
2751 a
->start
.addresses
[a
->start
.count
++] =
2752 b
->start
.addresses
[i
];
2754 /* Remove and delete jump. */
2755 struct jump_info
*tmp
= b
->prev
;
2756 jump_info_unlink (b
, base
);
2764 /* Sort jumps by their size and starting point using a stable
2765 minsort. This could be improved if sorting performance is
2766 an issue, for example by using mergesort. */
2769 jump_info_sort (struct jump_info
**base
)
2771 struct jump_info
*current_element
= *base
;
2773 while (current_element
)
2775 struct jump_info
*best_match
= current_element
;
2776 struct jump_info
*runner
= current_element
->next
;
2777 bfd_vma best_size
= jump_info_size (best_match
);
2781 bfd_vma runner_size
= jump_info_size (runner
);
2783 if ((runner_size
< best_size
)
2784 || ((runner_size
== best_size
)
2785 && (jump_info_min_address (runner
)
2786 < jump_info_min_address (best_match
))))
2788 best_match
= runner
;
2789 best_size
= runner_size
;
2792 runner
= runner
->next
;
2795 if (best_match
== current_element
)
2796 current_element
= current_element
->next
;
2798 jump_info_move_linked (best_match
, current_element
, base
);
2802 /* Visualize all jumps at a given address. */
2805 jump_info_visualize_address (bfd_vma address
,
2808 uint8_t *color_buffer
)
2810 struct jump_info
*ji
= detected_jumps
;
2811 size_t len
= (max_level
+ 1) * 3;
2813 /* Clear line buffer. */
2814 memset (line_buffer
, ' ', len
);
2815 memset (color_buffer
, 0, len
);
2817 /* Iterate over jumps and add their ASCII art. */
2820 /* Discard jumps that are never needed again. */
2821 if (jump_info_max_address (ji
) < address
)
2823 struct jump_info
*tmp
= ji
;
2826 jump_info_unlink (tmp
, &detected_jumps
);
2827 jump_info_free (tmp
);
2831 /* This jump intersects with the current address. */
2832 if (jump_info_min_address (ji
) <= address
)
2834 /* Hash target address to get an even
2835 distribution between all values. */
2836 bfd_vma hash_address
= jump_info_end_address (ji
);
2837 uint8_t color
= iterative_hash_object (hash_address
, 0);
2838 /* Fetch line offset. */
2839 int offset
= (max_level
- ji
->level
) * 3;
2841 /* Draw start line. */
2842 if (jump_info_is_start_address (ji
, address
))
2844 size_t i
= offset
+ 1;
2846 for (; i
< len
- 1; ++i
)
2847 if (line_buffer
[i
] == ' ')
2849 line_buffer
[i
] = '-';
2850 color_buffer
[i
] = color
;
2853 if (line_buffer
[i
] == ' ')
2855 line_buffer
[i
] = '-';
2856 color_buffer
[i
] = color
;
2858 else if (line_buffer
[i
] == '>')
2860 line_buffer
[i
] = 'X';
2861 color_buffer
[i
] = color
;
2864 if (line_buffer
[offset
] == ' ')
2866 if (address
<= ji
->end
)
2867 line_buffer
[offset
] =
2868 (jump_info_min_address (ji
) == address
) ? '/': '+';
2870 line_buffer
[offset
] =
2871 (jump_info_max_address (ji
) == address
) ? '\\': '+';
2872 color_buffer
[offset
] = color
;
2875 /* Draw jump target. */
2876 else if (jump_info_is_end_address (ji
, address
))
2878 size_t i
= offset
+ 1;
2880 for (; i
< len
- 1; ++i
)
2881 if (line_buffer
[i
] == ' ')
2883 line_buffer
[i
] = '-';
2884 color_buffer
[i
] = color
;
2887 if (line_buffer
[i
] == ' ')
2889 line_buffer
[i
] = '>';
2890 color_buffer
[i
] = color
;
2892 else if (line_buffer
[i
] == '-')
2894 line_buffer
[i
] = 'X';
2895 color_buffer
[i
] = color
;
2898 if (line_buffer
[offset
] == ' ')
2900 if (jump_info_min_address (ji
) < address
)
2901 line_buffer
[offset
] =
2902 (jump_info_max_address (ji
) > address
) ? '>' : '\\';
2904 line_buffer
[offset
] = '/';
2905 color_buffer
[offset
] = color
;
2908 /* Draw intermediate line segment. */
2909 else if (line_buffer
[offset
] == ' ')
2911 line_buffer
[offset
] = '|';
2912 color_buffer
[offset
] = color
;
2920 /* Clone of disassemble_bytes to detect jumps inside a function. */
2921 /* FIXME: is this correct? Can we strip it down even further? */
2923 static struct jump_info
*
2924 disassemble_jumps (struct disassemble_info
* inf
,
2925 disassembler_ftype disassemble_fn
,
2926 bfd_vma start_offset
,
2927 bfd_vma stop_offset
,
2930 arelent
** relppend
)
2932 struct objdump_disasm_info
*aux
;
2933 struct jump_info
*jumps
= NULL
;
2935 bfd_vma addr_offset
;
2936 unsigned int opb
= inf
->octets_per_byte
;
2940 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
2941 section
= inf
->section
;
2944 sfile
.buffer
= (char *) xmalloc (sfile
.alloc
);
2947 inf
->insn_info_valid
= 0;
2948 disassemble_set_printf (inf
, &sfile
, (fprintf_ftype
) objdump_sprintf
,
2949 (fprintf_styled_ftype
) objdump_styled_sprintf
);
2951 addr_offset
= start_offset
;
2952 while (addr_offset
< stop_offset
)
2954 int previous_octets
;
2956 /* Remember the length of the previous instruction. */
2957 previous_octets
= octets
;
2961 inf
->bytes_per_line
= 0;
2962 inf
->bytes_per_chunk
= 0;
2963 inf
->flags
= ((disassemble_all
? DISASSEMBLE_DATA
: 0)
2964 | (wide_output
? WIDE_OUTPUT
: 0));
2966 inf
->flags
|= USER_SPECIFIED_MACHINE_TYPE
;
2968 if (inf
->disassembler_needs_relocs
2969 && (bfd_get_file_flags (aux
->abfd
) & EXEC_P
) == 0
2970 && (bfd_get_file_flags (aux
->abfd
) & DYNAMIC
) == 0
2971 && *relppp
< relppend
)
2973 bfd_signed_vma distance_to_rel
;
2975 distance_to_rel
= (**relppp
)->address
- (rel_offset
+ addr_offset
);
2977 /* Check to see if the current reloc is associated with
2978 the instruction that we are about to disassemble. */
2979 if (distance_to_rel
== 0
2980 /* FIXME: This is wrong. We are trying to catch
2981 relocs that are addressed part way through the
2982 current instruction, as might happen with a packed
2983 VLIW instruction. Unfortunately we do not know the
2984 length of the current instruction since we have not
2985 disassembled it yet. Instead we take a guess based
2986 upon the length of the previous instruction. The
2987 proper solution is to have a new target-specific
2988 disassembler function which just returns the length
2989 of an instruction at a given address without trying
2990 to display its disassembly. */
2991 || (distance_to_rel
> 0
2992 && distance_to_rel
< (bfd_signed_vma
) (previous_octets
/ opb
)))
2994 inf
->flags
|= INSN_HAS_RELOC
;
2998 if (! disassemble_all
2999 && (section
->flags
& (SEC_CODE
| SEC_HAS_CONTENTS
))
3000 == (SEC_CODE
| SEC_HAS_CONTENTS
))
3001 /* Set a stop_vma so that the disassembler will not read
3002 beyond the next symbol. We assume that symbols appear on
3003 the boundaries between instructions. We only do this when
3004 disassembling code of course, and when -D is in effect. */
3005 inf
->stop_vma
= section
->vma
+ stop_offset
;
3007 inf
->stop_offset
= stop_offset
;
3009 /* Extract jump information. */
3010 inf
->insn_info_valid
= 0;
3011 disassembler_in_comment
= false;
3012 octets
= (*disassemble_fn
) (section
->vma
+ addr_offset
, inf
);
3013 /* Test if a jump was detected. */
3014 if (inf
->insn_info_valid
3015 && ((inf
->insn_type
== dis_branch
)
3016 || (inf
->insn_type
== dis_condbranch
)
3017 || (inf
->insn_type
== dis_jsr
)
3018 || (inf
->insn_type
== dis_condjsr
))
3019 && (inf
->target
>= section
->vma
+ start_offset
)
3020 && (inf
->target
< section
->vma
+ stop_offset
))
3022 struct jump_info
*ji
=
3023 jump_info_new (section
->vma
+ addr_offset
, inf
->target
, -1);
3024 jump_info_add_front (ji
, &jumps
);
3029 addr_offset
+= octets
/ opb
;
3032 disassemble_set_printf (inf
, (void *) stdout
, (fprintf_ftype
) fprintf
,
3033 (fprintf_styled_ftype
) fprintf_styled
);
3034 free (sfile
.buffer
);
3037 jump_info_merge (&jumps
);
3038 /* Process jumps. */
3039 jump_info_sort (&jumps
);
3041 /* Group jumps by level. */
3042 struct jump_info
*last_jump
= jumps
;
3047 /* The last jump is part of the next group. */
3048 struct jump_info
*base
= last_jump
;
3049 /* Increment level. */
3050 base
->level
= ++max_level
;
3052 /* Find jumps that can be combined on the same
3053 level, with the largest jumps tested first.
3054 This has the advantage that large jumps are on
3055 lower levels and do not intersect with small
3056 jumps that get grouped on higher levels. */
3057 struct jump_info
*exchange_item
= last_jump
->next
;
3058 struct jump_info
*it
= exchange_item
;
3060 for (; it
; it
= it
->next
)
3062 /* Test if the jump intersects with any
3063 jump from current group. */
3065 struct jump_info
*it_collision
;
3067 for (it_collision
= base
;
3068 it_collision
!= exchange_item
;
3069 it_collision
= it_collision
->next
)
3071 /* This jump intersects so we leave it out. */
3072 if (jump_info_intersect (it_collision
, it
))
3079 /* Add jump to group. */
3082 /* Move current element to the front. */
3083 if (it
!= exchange_item
)
3085 struct jump_info
*save
= it
->prev
;
3086 jump_info_move_linked (it
, exchange_item
, &jumps
);
3092 last_jump
= exchange_item
;
3093 exchange_item
= exchange_item
->next
;
3095 last_jump
->level
= max_level
;
3099 /* Move to next group. */
3100 last_jump
= exchange_item
;
3106 /* The number of zeroes we want to see before we start skipping them.
3107 The number is arbitrarily chosen. */
3109 #define DEFAULT_SKIP_ZEROES 8
3111 /* The number of zeroes to skip at the end of a section. If the
3112 number of zeroes at the end is between SKIP_ZEROES_AT_END and
3113 SKIP_ZEROES, they will be disassembled. If there are fewer than
3114 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
3115 attempt to avoid disassembling zeroes inserted by section
3118 #define DEFAULT_SKIP_ZEROES_AT_END 3
3121 null_print (const void * stream ATTRIBUTE_UNUSED
, const char * format ATTRIBUTE_UNUSED
, ...)
3126 /* Like null_print, but takes the extra STYLE argument. As this is not
3127 going to print anything, the extra argument is just ignored. */
3130 null_styled_print (const void * stream ATTRIBUTE_UNUSED
,
3131 enum disassembler_style style ATTRIBUTE_UNUSED
,
3132 const char * format ATTRIBUTE_UNUSED
, ...)
3137 /* Print out jump visualization. */
3140 print_jump_visualisation (bfd_vma addr
, int max_level
, char *line_buffer
,
3141 uint8_t *color_buffer
)
3146 jump_info_visualize_address (addr
, max_level
, line_buffer
, color_buffer
);
3148 size_t line_buffer_size
= strlen (line_buffer
);
3149 char last_color
= 0;
3152 for (i
= 0; i
<= line_buffer_size
; ++i
)
3156 uint8_t color
= (i
< line_buffer_size
) ? color_buffer
[i
]: 0;
3158 if (color
!= last_color
)
3161 if (extended_color_output
)
3162 /* Use extended 8bit color, but
3163 do not choose dark colors. */
3164 printf ("\033[38;5;%dm", 124 + (color
% 108));
3166 /* Use simple terminal colors. */
3167 printf ("\033[%dm", 31 + (color
% 7));
3174 putchar ((i
< line_buffer_size
) ? line_buffer
[i
]: ' ');
3178 /* Disassemble some data in memory between given values. */
3181 disassemble_bytes (struct disassemble_info
*inf
,
3182 disassembler_ftype disassemble_fn
,
3185 bfd_vma start_offset
,
3186 bfd_vma stop_offset
,
3191 struct objdump_disasm_info
*aux
;
3193 unsigned int octets_per_line
;
3194 unsigned int skip_addr_chars
;
3195 bfd_vma addr_offset
;
3196 unsigned int opb
= inf
->octets_per_byte
;
3197 unsigned int skip_zeroes
= inf
->skip_zeroes
;
3198 unsigned int skip_zeroes_at_end
= inf
->skip_zeroes_at_end
;
3202 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
3203 section
= inf
->section
;
3206 sfile
.buffer
= (char *) xmalloc (sfile
.alloc
);
3210 octets_per_line
= insn_width
;
3212 octets_per_line
= 4;
3214 octets_per_line
= 16;
3216 /* Figure out how many characters to skip at the start of an
3217 address, to make the disassembly look nicer. We discard leading
3218 zeroes in chunks of 4, ensuring that there is always a leading
3220 skip_addr_chars
= 0;
3221 if (!no_addresses
&& !prefix_addresses
)
3225 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ section
->size
/ opb
);
3227 while (buf
[skip_addr_chars
] == '0')
3230 /* Don't discard zeros on overflow. */
3231 if (buf
[skip_addr_chars
] == '\0' && section
->vma
!= 0)
3232 skip_addr_chars
= 0;
3234 if (skip_addr_chars
!= 0)
3235 skip_addr_chars
= (skip_addr_chars
- 1) & -4;
3238 inf
->insn_info_valid
= 0;
3240 /* Determine maximum level. */
3241 uint8_t *color_buffer
= NULL
;
3242 char *line_buffer
= NULL
;
3245 /* Some jumps were detected. */
3248 struct jump_info
*ji
;
3250 /* Find maximum jump level. */
3251 for (ji
= detected_jumps
; ji
; ji
= ji
->next
)
3253 if (ji
->level
> max_level
)
3254 max_level
= ji
->level
;
3257 /* Allocate buffers. */
3258 size_t len
= (max_level
+ 1) * 3 + 1;
3259 line_buffer
= xmalloc (len
);
3260 line_buffer
[len
- 1] = 0;
3261 color_buffer
= xmalloc (len
);
3262 color_buffer
[len
- 1] = 0;
3265 addr_offset
= start_offset
;
3266 while (addr_offset
< stop_offset
)
3268 bool need_nl
= false;
3272 /* Make sure we don't use relocs from previous instructions. */
3275 /* If we see more than SKIP_ZEROES octets of zeroes, we just
3277 if (! disassemble_zeroes
)
3278 for (; addr_offset
* opb
+ octets
< stop_offset
* opb
; octets
++)
3279 if (data
[addr_offset
* opb
+ octets
] != 0)
3281 if (! disassemble_zeroes
3282 && (inf
->insn_info_valid
== 0
3283 || inf
->branch_delay_insns
== 0)
3284 && (octets
>= skip_zeroes
3285 || (addr_offset
* opb
+ octets
== stop_offset
* opb
3286 && octets
< skip_zeroes_at_end
)))
3288 /* If there are more nonzero octets to follow, we only skip
3289 zeroes in multiples of 4, to try to avoid running over
3290 the start of an instruction which happens to start with
3292 if (addr_offset
* opb
+ octets
!= stop_offset
* opb
)
3295 /* If we are going to display more data, and we are displaying
3296 file offsets, then tell the user how many zeroes we skip
3297 and the file offset from where we resume dumping. */
3298 if (display_file_offsets
3299 && addr_offset
+ octets
/ opb
< stop_offset
)
3300 printf (_("\t... (skipping %lu zeroes, "
3301 "resuming at file offset: 0x%lx)\n"),
3302 (unsigned long) (octets
/ opb
),
3303 (unsigned long) (section
->filepos
3304 + addr_offset
+ octets
/ opb
));
3311 unsigned int bpc
= 0;
3312 unsigned int pb
= 0;
3314 if (with_line_numbers
|| with_source_code
)
3315 show_line (aux
->abfd
, section
, addr_offset
);
3319 else if (!prefix_addresses
)
3323 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ addr_offset
);
3324 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
3328 printf ("%s:\t", buf
+ skip_addr_chars
);
3332 aux
->require_sec
= true;
3333 objdump_print_address (section
->vma
+ addr_offset
, inf
);
3334 aux
->require_sec
= false;
3338 print_jump_visualisation (section
->vma
+ addr_offset
,
3339 max_level
, line_buffer
,
3347 disassemble_set_printf
3348 (inf
, &sfile
, (fprintf_ftype
) objdump_sprintf
,
3349 (fprintf_styled_ftype
) objdump_styled_sprintf
);
3350 inf
->bytes_per_line
= 0;
3351 inf
->bytes_per_chunk
= 0;
3352 inf
->flags
= ((disassemble_all
? DISASSEMBLE_DATA
: 0)
3353 | (wide_output
? WIDE_OUTPUT
: 0));
3355 inf
->flags
|= USER_SPECIFIED_MACHINE_TYPE
;
3357 if (inf
->disassembler_needs_relocs
3358 && (bfd_get_file_flags (aux
->abfd
) & EXEC_P
) == 0
3359 && (bfd_get_file_flags (aux
->abfd
) & DYNAMIC
) == 0
3360 && *relppp
< relppend
)
3362 bfd_signed_vma distance_to_rel
;
3363 int max_reloc_offset
3364 = aux
->abfd
->arch_info
->max_reloc_offset_into_insn
;
3366 distance_to_rel
= ((**relppp
)->address
- rel_offset
3370 if (distance_to_rel
> 0
3371 && (max_reloc_offset
< 0
3372 || distance_to_rel
<= max_reloc_offset
))
3374 /* This reloc *might* apply to the current insn,
3375 starting somewhere inside it. Discover the length
3376 of the current insn so that the check below will
3379 insn_size
= insn_width
;
3382 /* We find the length by calling the dissassembler
3383 function with a dummy print handler. This should
3384 work unless the disassembler is not expecting to
3385 be called multiple times for the same address.
3387 This does mean disassembling the instruction
3388 twice, but we only do this when there is a high
3389 probability that there is a reloc that will
3390 affect the instruction. */
3391 disassemble_set_printf
3392 (inf
, inf
->stream
, (fprintf_ftype
) null_print
,
3393 (fprintf_styled_ftype
) null_styled_print
);
3394 insn_size
= disassemble_fn (section
->vma
3395 + addr_offset
, inf
);
3396 disassemble_set_printf
3398 (fprintf_ftype
) objdump_sprintf
,
3399 (fprintf_styled_ftype
) objdump_styled_sprintf
);
3403 /* Check to see if the current reloc is associated with
3404 the instruction that we are about to disassemble. */
3405 if (distance_to_rel
== 0
3406 || (distance_to_rel
> 0
3407 && distance_to_rel
< insn_size
/ (int) opb
))
3409 inf
->flags
|= INSN_HAS_RELOC
;
3410 aux
->reloc
= **relppp
;
3414 if (! disassemble_all
3415 && ((section
->flags
& (SEC_CODE
| SEC_HAS_CONTENTS
))
3416 == (SEC_CODE
| SEC_HAS_CONTENTS
)))
3417 /* Set a stop_vma so that the disassembler will not read
3418 beyond the next symbol. We assume that symbols appear on
3419 the boundaries between instructions. We only do this when
3420 disassembling code of course, and when -D is in effect. */
3421 inf
->stop_vma
= section
->vma
+ stop_offset
;
3423 inf
->stop_offset
= stop_offset
;
3424 disassembler_in_comment
= false;
3425 insn_size
= (*disassemble_fn
) (section
->vma
+ addr_offset
, inf
);
3429 disassemble_set_printf (inf
, stdout
, (fprintf_ftype
) fprintf
,
3430 (fprintf_styled_ftype
) fprintf_styled
);
3431 if (insn_width
== 0 && inf
->bytes_per_line
!= 0)
3432 octets_per_line
= inf
->bytes_per_line
;
3433 if (insn_size
< (int) opb
)
3436 printf ("%s\n", sfile
.buffer
);
3439 non_fatal (_("disassemble_fn returned length %d"),
3450 octets
= octets_per_line
;
3451 if (addr_offset
+ octets
/ opb
> stop_offset
)
3452 octets
= (stop_offset
- addr_offset
) * opb
;
3454 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ octets
; ++j
)
3456 if (ISPRINT (data
[j
]))
3457 buf
[j
- addr_offset
* opb
] = data
[j
];
3459 buf
[j
- addr_offset
* opb
] = '.';
3461 buf
[j
- addr_offset
* opb
] = '\0';
3464 if (prefix_addresses
3466 : show_raw_insn
>= 0)
3470 /* If ! prefix_addresses and ! wide_output, we print
3471 octets_per_line octets per line. */
3473 if (pb
> octets_per_line
&& ! prefix_addresses
&& ! wide_output
)
3474 pb
= octets_per_line
;
3476 if (inf
->bytes_per_chunk
)
3477 bpc
= inf
->bytes_per_chunk
;
3481 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
3483 /* PR 21580: Check for a buffer ending early. */
3484 if (j
+ bpc
<= stop_offset
* opb
)
3488 if (inf
->display_endian
== BFD_ENDIAN_LITTLE
)
3490 for (k
= bpc
; k
-- != 0; )
3491 printf ("%02x", (unsigned) data
[j
+ k
]);
3495 for (k
= 0; k
< bpc
; k
++)
3496 printf ("%02x", (unsigned) data
[j
+ k
]);
3502 for (; pb
< octets_per_line
; pb
+= bpc
)
3506 for (k
= 0; k
< bpc
; k
++)
3511 /* Separate raw data from instruction by extra space. */
3521 printf ("%s", sfile
.buffer
);
3523 if (prefix_addresses
3525 : show_raw_insn
>= 0)
3533 j
= addr_offset
* opb
+ pb
;
3539 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ j
/ opb
);
3540 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
3544 printf ("%s:\t", buf
+ skip_addr_chars
);
3547 print_jump_visualisation (section
->vma
+ j
/ opb
,
3548 max_level
, line_buffer
,
3551 pb
+= octets_per_line
;
3554 for (; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
3556 /* PR 21619: Check for a buffer ending early. */
3557 if (j
+ bpc
<= stop_offset
* opb
)
3561 if (inf
->display_endian
== BFD_ENDIAN_LITTLE
)
3563 for (k
= bpc
; k
-- != 0; )
3564 printf ("%02x", (unsigned) data
[j
+ k
]);
3568 for (k
= 0; k
< bpc
; k
++)
3569 printf ("%02x", (unsigned) data
[j
+ k
]);
3583 while ((*relppp
) < relppend
3584 && (**relppp
)->address
< rel_offset
+ addr_offset
+ octets
/ opb
)
3586 if (dump_reloc_info
|| dump_dynamic_reloc_info
)
3599 objdump_print_value (section
->vma
- rel_offset
+ q
->address
,
3604 if (q
->howto
== NULL
)
3605 printf ("*unknown*\t");
3606 else if (q
->howto
->name
)
3607 printf ("%s\t", q
->howto
->name
);
3609 printf ("%d\t", q
->howto
->type
);
3611 if (q
->sym_ptr_ptr
== NULL
|| *q
->sym_ptr_ptr
== NULL
)
3612 printf ("*unknown*");
3615 const char *sym_name
;
3617 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
3618 if (sym_name
!= NULL
&& *sym_name
!= '\0')
3619 objdump_print_symname (aux
->abfd
, inf
, *q
->sym_ptr_ptr
);
3624 sym_sec
= bfd_asymbol_section (*q
->sym_ptr_ptr
);
3625 sym_name
= bfd_section_name (sym_sec
);
3626 if (sym_name
== NULL
|| *sym_name
== '\0')
3627 sym_name
= "*unknown*";
3628 printf ("%s", sanitize_string (sym_name
));
3634 bfd_vma addend
= q
->addend
;
3635 if ((bfd_signed_vma
) addend
< 0)
3642 objdump_print_value (addend
, inf
, true);
3654 addr_offset
+= octets
/ opb
;
3657 free (sfile
.buffer
);
3659 free (color_buffer
);
3663 disassemble_section (bfd
*abfd
, asection
*section
, void *inf
)
3665 const struct elf_backend_data
*bed
;
3666 bfd_vma sign_adjust
= 0;
3667 struct disassemble_info
*pinfo
= (struct disassemble_info
*) inf
;
3668 struct objdump_disasm_info
*paux
;
3669 unsigned int opb
= pinfo
->octets_per_byte
;
3670 bfd_byte
*data
= NULL
;
3671 bfd_size_type datasize
= 0;
3672 arelent
**rel_pp
= NULL
;
3673 arelent
**rel_ppstart
= NULL
;
3674 arelent
**rel_ppend
;
3675 bfd_vma stop_offset
;
3676 asymbol
*sym
= NULL
;
3680 unsigned long addr_offset
;
3684 stop_offset_reached
,
3689 /* Sections that do not contain machine
3690 code are not normally disassembled. */
3691 if (! disassemble_all
3692 && only_list
== NULL
3693 && ((section
->flags
& (SEC_CODE
| SEC_HAS_CONTENTS
))
3694 != (SEC_CODE
| SEC_HAS_CONTENTS
)))
3697 if (! process_section_p (section
))
3700 datasize
= bfd_section_size (section
);
3704 if (start_address
== (bfd_vma
) -1
3705 || start_address
< section
->vma
)
3708 addr_offset
= start_address
- section
->vma
;
3710 if (stop_address
== (bfd_vma
) -1)
3711 stop_offset
= datasize
/ opb
;
3714 if (stop_address
< section
->vma
)
3717 stop_offset
= stop_address
- section
->vma
;
3718 if (stop_offset
> datasize
/ opb
)
3719 stop_offset
= datasize
/ opb
;
3722 if (addr_offset
>= stop_offset
)
3725 /* Decide which set of relocs to use. Load them if necessary. */
3726 paux
= (struct objdump_disasm_info
*) pinfo
->application_data
;
3727 if (pinfo
->dynrelbuf
&& dump_dynamic_reloc_info
)
3729 rel_pp
= pinfo
->dynrelbuf
;
3730 rel_count
= pinfo
->dynrelcount
;
3731 /* Dynamic reloc addresses are absolute, non-dynamic are section
3732 relative. REL_OFFSET specifies the reloc address corresponding
3733 to the start of this section. */
3734 rel_offset
= section
->vma
;
3742 if ((section
->flags
& SEC_RELOC
) != 0
3743 && (dump_reloc_info
|| pinfo
->disassembler_needs_relocs
))
3747 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
3749 bfd_fatal (bfd_get_filename (abfd
));
3753 rel_ppstart
= rel_pp
= (arelent
**) xmalloc (relsize
);
3754 rel_count
= bfd_canonicalize_reloc (abfd
, section
, rel_pp
, syms
);
3756 bfd_fatal (bfd_get_filename (abfd
));
3758 /* Sort the relocs by address. */
3759 qsort (rel_pp
, rel_count
, sizeof (arelent
*), compare_relocs
);
3763 rel_ppend
= PTR_ADD (rel_pp
, rel_count
);
3765 if (!bfd_malloc_and_get_section (abfd
, section
, &data
))
3767 non_fatal (_("Reading section %s failed because: %s"),
3768 section
->name
, bfd_errmsg (bfd_get_error ()));
3772 pinfo
->buffer
= data
;
3773 pinfo
->buffer_vma
= section
->vma
;
3774 pinfo
->buffer_length
= datasize
;
3775 pinfo
->section
= section
;
3777 /* Sort the symbols into value and section order. */
3778 compare_section
= section
;
3779 if (sorted_symcount
> 1)
3780 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
3782 /* Skip over the relocs belonging to addresses below the
3784 while (rel_pp
< rel_ppend
3785 && (*rel_pp
)->address
< rel_offset
+ addr_offset
)
3788 printf (_("\nDisassembly of section %s:\n"), sanitize_string (section
->name
));
3790 /* Find the nearest symbol forwards from our current position. */
3791 paux
->require_sec
= true;
3792 sym
= (asymbol
*) find_symbol_for_address (section
->vma
+ addr_offset
,
3793 (struct disassemble_info
*) inf
,
3795 paux
->require_sec
= false;
3797 /* PR 9774: If the target used signed addresses then we must make
3798 sure that we sign extend the value that we calculate for 'addr'
3799 in the loop below. */
3800 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3801 && (bed
= get_elf_backend_data (abfd
)) != NULL
3802 && bed
->sign_extend_vma
)
3803 sign_adjust
= (bfd_vma
) 1 << (bed
->s
->arch_size
- 1);
3805 /* Disassemble a block of instructions up to the address associated with
3806 the symbol we have just found. Then print the symbol and find the
3807 next symbol on. Repeat until we have disassembled the entire section
3808 or we have reached the end of the address range we are interested in. */
3809 do_print
= paux
->symbol
== NULL
;
3810 loop_until
= stop_offset_reached
;
3812 while (addr_offset
< stop_offset
)
3816 bfd_vma nextstop_offset
;
3819 addr
= section
->vma
+ addr_offset
;
3820 addr
= ((addr
& ((sign_adjust
<< 1) - 1)) ^ sign_adjust
) - sign_adjust
;
3822 if (sym
!= NULL
&& bfd_asymbol_value (sym
) <= addr
)
3827 (x
< sorted_symcount
3828 && (bfd_asymbol_value (sorted_syms
[x
]) <= addr
));
3832 pinfo
->symbols
= sorted_syms
+ place
;
3833 pinfo
->num_symbols
= x
- place
;
3834 pinfo
->symtab_pos
= place
;
3838 pinfo
->symbols
= NULL
;
3839 pinfo
->num_symbols
= 0;
3840 pinfo
->symtab_pos
= -1;
3843 /* If we are only disassembling from a specific symbol,
3844 check to see if we should start or stop displaying. */
3845 if (sym
&& paux
->symbol
)
3849 /* See if we should stop printing. */
3853 if (sym
->flags
& BSF_FUNCTION
)
3857 case stop_offset_reached
:
3858 /* Handled by the while loop. */
3862 /* FIXME: There is an implicit assumption here
3863 that the name of sym is different from
3865 if (! bfd_is_local_label (abfd
, sym
))
3872 const char * name
= bfd_asymbol_name (sym
);
3873 char * alloc
= NULL
;
3875 if (do_demangle
&& name
[0] != '\0')
3877 /* Demangle the name. */
3878 alloc
= bfd_demangle (abfd
, name
, demangle_flags
);
3883 /* We are not currently printing. Check to see
3884 if the current symbol matches the requested symbol. */
3885 if (streq (name
, paux
->symbol
))
3889 if (sym
->flags
& BSF_FUNCTION
)
3891 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
3892 && ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
> 0)
3894 /* Sym is a function symbol with a size associated
3895 with it. Turn on automatic disassembly for the
3896 next VALUE bytes. */
3897 stop_offset
= addr_offset
3898 + ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
3899 loop_until
= stop_offset_reached
;
3903 /* Otherwise we need to tell the loop heuristic to
3904 loop until the next function symbol is encountered. */
3905 loop_until
= function_sym
;
3910 /* Otherwise loop until the next symbol is encountered. */
3911 loop_until
= next_sym
;
3919 if (! prefix_addresses
&& do_print
)
3921 pinfo
->fprintf_func (pinfo
->stream
, "\n");
3922 objdump_print_addr_with_sym (abfd
, section
, sym
, addr
,
3924 pinfo
->fprintf_func (pinfo
->stream
, ":\n");
3927 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > addr
)
3929 else if (sym
== NULL
)
3933 #define is_valid_next_sym(SYM) \
3934 (strcmp (bfd_section_name ((SYM)->section), bfd_section_name (section)) == 0 \
3935 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
3936 && pinfo->symbol_is_valid (SYM, pinfo))
3938 /* Search forward for the next appropriate symbol in
3939 SECTION. Note that all the symbols are sorted
3940 together into one big array, and that some sections
3941 may have overlapping addresses. */
3942 while (place
< sorted_symcount
3943 && ! is_valid_next_sym (sorted_syms
[place
]))
3946 if (place
>= sorted_symcount
)
3949 nextsym
= sorted_syms
[place
];
3952 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > addr
)
3953 nextstop_offset
= bfd_asymbol_value (sym
) - section
->vma
;
3954 else if (nextsym
== NULL
)
3955 nextstop_offset
= stop_offset
;
3957 nextstop_offset
= bfd_asymbol_value (nextsym
) - section
->vma
;
3959 if (nextstop_offset
> stop_offset
3960 || nextstop_offset
<= addr_offset
)
3961 nextstop_offset
= stop_offset
;
3963 /* If a symbol is explicitly marked as being an object
3964 rather than a function, just dump the bytes without
3965 disassembling them. */
3968 || sym
->section
!= section
3969 || bfd_asymbol_value (sym
) > addr
3970 || ((sym
->flags
& BSF_OBJECT
) == 0
3971 && (strstr (bfd_asymbol_name (sym
), "gnu_compiled")
3973 && (strstr (bfd_asymbol_name (sym
), "gcc2_compiled")
3975 || (sym
->flags
& BSF_FUNCTION
) != 0)
3982 /* Resolve symbol name. */
3983 if (visualize_jumps
&& abfd
&& sym
&& sym
->name
)
3985 struct disassemble_info di
;
3988 sf
.alloc
= strlen (sym
->name
) + 40;
3989 sf
.buffer
= (char*) xmalloc (sf
.alloc
);
3991 disassemble_set_printf
3992 (&di
, &sf
, (fprintf_ftype
) objdump_sprintf
,
3993 (fprintf_styled_ftype
) objdump_styled_sprintf
);
3995 objdump_print_symname (abfd
, &di
, sym
);
3997 /* Fetch jump information. */
3998 detected_jumps
= disassemble_jumps
3999 (pinfo
, paux
->disassemble_fn
,
4000 addr_offset
, nextstop_offset
,
4001 rel_offset
, &rel_pp
, rel_ppend
);
4003 /* Free symbol name. */
4007 /* Add jumps to output. */
4008 disassemble_bytes (pinfo
, paux
->disassemble_fn
, insns
, data
,
4009 addr_offset
, nextstop_offset
,
4010 rel_offset
, &rel_pp
, rel_ppend
);
4013 while (detected_jumps
)
4015 detected_jumps
= jump_info_free (detected_jumps
);
4019 addr_offset
= nextstop_offset
;
4025 if (rel_ppstart
!= NULL
)
4029 /* Disassemble the contents of an object file. */
4032 disassemble_data (bfd
*abfd
)
4034 struct disassemble_info disasm_info
;
4035 struct objdump_disasm_info aux
;
4039 prev_functionname
= NULL
;
4041 prev_discriminator
= 0;
4043 /* We make a copy of syms to sort. We don't want to sort syms
4044 because that will screw up the relocs. */
4045 sorted_symcount
= symcount
? symcount
: dynsymcount
;
4046 sorted_syms
= (asymbol
**) xmalloc ((sorted_symcount
+ synthcount
)
4047 * sizeof (asymbol
*));
4048 if (sorted_symcount
!= 0)
4050 memcpy (sorted_syms
, symcount
? syms
: dynsyms
,
4051 sorted_symcount
* sizeof (asymbol
*));
4053 sorted_symcount
= remove_useless_symbols (sorted_syms
, sorted_symcount
);
4056 for (i
= 0; i
< synthcount
; ++i
)
4058 sorted_syms
[sorted_symcount
] = synthsyms
+ i
;
4062 init_disassemble_info (&disasm_info
, stdout
, (fprintf_ftype
) fprintf
,
4063 (fprintf_styled_ftype
) fprintf_styled
);
4064 disasm_info
.application_data
= (void *) &aux
;
4066 aux
.require_sec
= false;
4067 disasm_info
.dynrelbuf
= NULL
;
4068 disasm_info
.dynrelcount
= 0;
4070 aux
.symbol
= disasm_sym
;
4072 disasm_info
.print_address_func
= objdump_print_address
;
4073 disasm_info
.symbol_at_address_func
= objdump_symbol_at_address
;
4075 if (machine
!= NULL
)
4077 const bfd_arch_info_type
*inf
= bfd_scan_arch (machine
);
4080 fatal (_("can't use supplied machine %s"), machine
);
4082 abfd
->arch_info
= inf
;
4085 if (endian
!= BFD_ENDIAN_UNKNOWN
)
4087 struct bfd_target
*xvec
;
4089 xvec
= (struct bfd_target
*) xmalloc (sizeof (struct bfd_target
));
4090 memcpy (xvec
, abfd
->xvec
, sizeof (struct bfd_target
));
4091 xvec
->byteorder
= endian
;
4095 /* Use libopcodes to locate a suitable disassembler. */
4096 aux
.disassemble_fn
= disassembler (bfd_get_arch (abfd
),
4097 bfd_big_endian (abfd
),
4098 bfd_get_mach (abfd
), abfd
);
4099 if (!aux
.disassemble_fn
)
4101 non_fatal (_("can't disassemble for architecture %s\n"),
4102 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
4107 disasm_info
.flavour
= bfd_get_flavour (abfd
);
4108 disasm_info
.arch
= bfd_get_arch (abfd
);
4109 disasm_info
.mach
= bfd_get_mach (abfd
);
4110 disasm_info
.disassembler_options
= disassembler_options
;
4111 disasm_info
.octets_per_byte
= bfd_octets_per_byte (abfd
, NULL
);
4112 disasm_info
.skip_zeroes
= DEFAULT_SKIP_ZEROES
;
4113 disasm_info
.skip_zeroes_at_end
= DEFAULT_SKIP_ZEROES_AT_END
;
4114 disasm_info
.disassembler_needs_relocs
= false;
4116 if (bfd_big_endian (abfd
))
4117 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_BIG
;
4118 else if (bfd_little_endian (abfd
))
4119 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
4121 /* ??? Aborting here seems too drastic. We could default to big or little
4123 disasm_info
.endian
= BFD_ENDIAN_UNKNOWN
;
4125 disasm_info
.endian_code
= disasm_info
.endian
;
4127 /* Allow the target to customize the info structure. */
4128 disassemble_init_for_target (& disasm_info
);
4130 /* Pre-load the dynamic relocs as we may need them during the disassembly. */
4131 long relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
4133 if (relsize
< 0 && dump_dynamic_reloc_info
)
4134 bfd_fatal (bfd_get_filename (abfd
));
4138 disasm_info
.dynrelbuf
= (arelent
**) xmalloc (relsize
);
4139 disasm_info
.dynrelcount
4140 = bfd_canonicalize_dynamic_reloc (abfd
, disasm_info
.dynrelbuf
, dynsyms
);
4141 if (disasm_info
.dynrelcount
< 0)
4142 bfd_fatal (bfd_get_filename (abfd
));
4144 /* Sort the relocs by address. */
4145 qsort (disasm_info
.dynrelbuf
, disasm_info
.dynrelcount
, sizeof (arelent
*),
4149 disasm_info
.symtab
= sorted_syms
;
4150 disasm_info
.symtab_size
= sorted_symcount
;
4152 bfd_map_over_sections (abfd
, disassemble_section
, & disasm_info
);
4154 free (disasm_info
.dynrelbuf
);
4155 disasm_info
.dynrelbuf
= NULL
;
4157 disassemble_free_target (&disasm_info
);
4161 load_specific_debug_section (enum dwarf_section_display_enum debug
,
4162 asection
*sec
, void *file
)
4164 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
4165 bfd
*abfd
= (bfd
*) file
;
4171 if (section
->start
!= NULL
)
4173 /* If it is already loaded, do nothing. */
4174 if (streq (section
->filename
, bfd_get_filename (abfd
)))
4176 free (section
->start
);
4179 section
->filename
= bfd_get_filename (abfd
);
4180 section
->reloc_info
= NULL
;
4181 section
->num_relocs
= 0;
4182 section
->address
= bfd_section_vma (sec
);
4183 section
->size
= bfd_section_size (sec
);
4184 /* PR 24360: On 32-bit hosts sizeof (size_t) < sizeof (bfd_size_type). */
4185 alloced
= amt
= section
->size
+ 1;
4186 if (alloced
!= amt
|| alloced
== 0)
4188 section
->start
= NULL
;
4189 free_debug_section (debug
);
4190 printf (_("\nSection '%s' has an invalid size: %#llx.\n"),
4191 sanitize_string (section
->name
),
4192 (unsigned long long) section
->size
);
4196 section
->start
= contents
= xmalloc (alloced
);
4197 /* Ensure any string section has a terminating NUL. */
4198 section
->start
[section
->size
] = 0;
4200 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4201 && debug_displays
[debug
].relocate
)
4203 ret
= bfd_simple_get_relocated_section_contents (abfd
,
4209 long reloc_size
= bfd_get_reloc_upper_bound (abfd
, sec
);
4213 unsigned long reloc_count
;
4216 relocs
= (arelent
**) xmalloc (reloc_size
);
4218 reloc_count
= bfd_canonicalize_reloc (abfd
, sec
, relocs
, NULL
);
4219 if (reloc_count
== 0)
4223 section
->reloc_info
= relocs
;
4224 section
->num_relocs
= reloc_count
;
4230 ret
= bfd_get_full_section_contents (abfd
, sec
, &contents
);
4234 free_debug_section (debug
);
4235 printf (_("\nCan't get contents for section '%s'.\n"),
4236 sanitize_string (section
->name
));
4244 reloc_at (struct dwarf_section
* dsec
, uint64_t offset
)
4249 if (dsec
== NULL
|| dsec
->reloc_info
== NULL
)
4252 relocs
= (arelent
**) dsec
->reloc_info
;
4254 for (; (rp
= * relocs
) != NULL
; ++ relocs
)
4255 if (rp
->address
== offset
)
4262 load_debug_section (enum dwarf_section_display_enum debug
, void *file
)
4264 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
4265 bfd
*abfd
= (bfd
*) file
;
4269 if (!dump_any_debugging
)
4272 /* If it is already loaded, do nothing. */
4273 if (section
->start
!= NULL
)
4275 if (streq (section
->filename
, bfd_get_filename (abfd
)))
4278 /* Locate the debug section. */
4279 name
= section
->uncompressed_name
;
4280 sec
= bfd_get_section_by_name (abfd
, name
);
4283 name
= section
->compressed_name
;
4285 sec
= bfd_get_section_by_name (abfd
, name
);
4289 name
= section
->xcoff_name
;
4291 sec
= bfd_get_section_by_name (abfd
, name
);
4296 section
->name
= name
;
4297 return load_specific_debug_section (debug
, sec
, file
);
4301 free_debug_section (enum dwarf_section_display_enum debug
)
4303 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
4305 free ((char *) section
->start
);
4306 section
->start
= NULL
;
4307 section
->address
= 0;
4309 free ((char*) section
->reloc_info
);
4310 section
->reloc_info
= NULL
;
4311 section
->num_relocs
= 0;
4315 close_debug_file (void * file
)
4317 bfd
* abfd
= (bfd
*) file
;
4323 open_debug_file (const char * pathname
)
4327 data
= bfd_openr (pathname
, NULL
);
4331 if (! bfd_check_format (data
, bfd_object
))
4338 dump_dwarf_section (bfd
*abfd
, asection
*section
,
4341 const char *name
= bfd_section_name (section
);
4344 bool is_mainfile
= *(bool *) arg
;
4349 if (!is_mainfile
&& !process_links
4350 && (section
->flags
& SEC_DEBUGGING
) == 0)
4353 if (startswith (name
, ".gnu.linkonce.wi."))
4354 match
= ".debug_info";
4358 for (i
= 0; i
< max
; i
++)
4359 if ((strcmp (debug_displays
[i
].section
.uncompressed_name
, match
) == 0
4360 || strcmp (debug_displays
[i
].section
.compressed_name
, match
) == 0
4361 || strcmp (debug_displays
[i
].section
.xcoff_name
, match
) == 0)
4362 && debug_displays
[i
].enabled
!= NULL
4363 && *debug_displays
[i
].enabled
)
4365 struct dwarf_section
*sec
= &debug_displays
[i
].section
;
4367 if (strcmp (sec
->uncompressed_name
, match
) == 0)
4368 sec
->name
= sec
->uncompressed_name
;
4369 else if (strcmp (sec
->compressed_name
, match
) == 0)
4370 sec
->name
= sec
->compressed_name
;
4372 sec
->name
= sec
->xcoff_name
;
4373 if (load_specific_debug_section ((enum dwarf_section_display_enum
) i
,
4376 debug_displays
[i
].display (sec
, abfd
);
4378 if (i
!= info
&& i
!= abbrev
)
4379 free_debug_section ((enum dwarf_section_display_enum
) i
);
4385 /* Dump the dwarf debugging information. */
4388 dump_dwarf (bfd
*abfd
, bool is_mainfile
)
4390 /* The byte_get pointer should have been set at the start of dump_bfd(). */
4391 if (byte_get
== NULL
)
4393 warn (_("File %s does not contain any dwarf debug information\n"),
4394 bfd_get_filename (abfd
));
4398 switch (bfd_get_arch (abfd
))
4401 /* S12Z has a 24 bit address space. But the only known
4402 producer of dwarf_info encodes addresses into 32 bits. */
4407 eh_addr_size
= bfd_arch_bits_per_address (abfd
) / 8;
4411 init_dwarf_regnames_by_bfd_arch_and_mach (bfd_get_arch (abfd
),
4412 bfd_get_mach (abfd
));
4414 bfd_map_over_sections (abfd
, dump_dwarf_section
, (void *) &is_mainfile
);
4417 /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
4418 it. Return NULL on failure. */
4421 read_section_stabs (bfd
*abfd
, const char *sect_name
, bfd_size_type
*size_ptr
,
4422 bfd_size_type
*entsize_ptr
)
4427 stabsect
= bfd_get_section_by_name (abfd
, sect_name
);
4428 if (stabsect
== NULL
)
4430 printf (_("No %s section present\n\n"),
4431 sanitize_string (sect_name
));
4435 if (!bfd_malloc_and_get_section (abfd
, stabsect
, &contents
))
4437 non_fatal (_("reading %s section of %s failed: %s"),
4438 sect_name
, bfd_get_filename (abfd
),
4439 bfd_errmsg (bfd_get_error ()));
4445 *size_ptr
= bfd_section_size (stabsect
);
4447 *entsize_ptr
= stabsect
->entsize
;
4452 /* Stabs entries use a 12 byte format:
4453 4 byte string table index
4455 1 byte stab other field
4456 2 byte stab desc field
4458 FIXME: This will have to change for a 64 bit object format. */
4460 #define STRDXOFF (0)
4462 #define OTHEROFF (5)
4465 #define STABSIZE (12)
4467 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
4468 using string table section STRSECT_NAME (in `strtab'). */
4471 print_section_stabs (bfd
*abfd
,
4472 const char *stabsect_name
,
4473 unsigned *string_offset_ptr
)
4476 unsigned file_string_table_offset
= 0;
4477 unsigned next_file_string_table_offset
= *string_offset_ptr
;
4478 bfd_byte
*stabp
, *stabs_end
;
4481 stabs_end
= stabp
+ stab_size
;
4483 printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name
));
4484 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
4486 /* Loop through all symbols and print them.
4488 We start the index at -1 because there is a dummy symbol on
4489 the front of stabs-in-{coff,elf} sections that supplies sizes. */
4490 for (i
= -1; stabp
<= stabs_end
- STABSIZE
; stabp
+= STABSIZE
, i
++)
4494 unsigned char type
, other
;
4495 unsigned short desc
;
4498 strx
= bfd_h_get_32 (abfd
, stabp
+ STRDXOFF
);
4499 type
= bfd_h_get_8 (abfd
, stabp
+ TYPEOFF
);
4500 other
= bfd_h_get_8 (abfd
, stabp
+ OTHEROFF
);
4501 desc
= bfd_h_get_16 (abfd
, stabp
+ DESCOFF
);
4502 value
= bfd_h_get_32 (abfd
, stabp
+ VALOFF
);
4504 printf ("\n%-6d ", i
);
4505 /* Either print the stab name, or, if unnamed, print its number
4506 again (makes consistent formatting for tools like awk). */
4507 name
= bfd_get_stab_name (type
);
4509 printf ("%-6s", sanitize_string (name
));
4510 else if (type
== N_UNDF
)
4513 printf ("%-6d", type
);
4514 printf (" %-6d %-6d ", other
, desc
);
4515 bfd_printf_vma (abfd
, value
);
4516 printf (" %-6lu", strx
);
4518 /* Symbols with type == 0 (N_UNDF) specify the length of the
4519 string table associated with this file. We use that info
4520 to know how to relocate the *next* file's string table indices. */
4523 file_string_table_offset
= next_file_string_table_offset
;
4524 next_file_string_table_offset
+= value
;
4528 bfd_size_type amt
= strx
+ file_string_table_offset
;
4530 /* Using the (possibly updated) string table offset, print the
4531 string (if any) associated with this symbol. */
4532 if (amt
< stabstr_size
)
4533 /* PR 17512: file: 079-79389-0.001:0.1.
4534 FIXME: May need to sanitize this string before displaying. */
4535 printf (" %.*s", (int)(stabstr_size
- amt
), strtab
+ amt
);
4541 *string_offset_ptr
= next_file_string_table_offset
;
4546 const char * section_name
;
4547 const char * string_section_name
;
4548 unsigned string_offset
;
4553 find_stabs_section (bfd
*abfd
, asection
*section
, void *names
)
4556 stab_section_names
* sought
= (stab_section_names
*) names
;
4558 /* Check for section names for which stabsect_name is a prefix, to
4559 handle .stab.N, etc. */
4560 len
= strlen (sought
->section_name
);
4562 /* If the prefix matches, and the files section name ends with a
4563 nul or a digit, then we match. I.e., we want either an exact
4564 match or a section followed by a number. */
4565 if (strncmp (sought
->section_name
, section
->name
, len
) == 0
4566 && (section
->name
[len
] == 0
4567 || (section
->name
[len
] == '.' && ISDIGIT (section
->name
[len
+ 1]))))
4570 strtab
= read_section_stabs (abfd
, sought
->string_section_name
,
4571 &stabstr_size
, NULL
);
4575 stabs
= read_section_stabs (abfd
, section
->name
, &stab_size
, NULL
);
4577 print_section_stabs (abfd
, section
->name
, &sought
->string_offset
);
4583 dump_stabs_section (bfd
*abfd
, char *stabsect_name
, char *strsect_name
)
4585 stab_section_names s
;
4587 s
.section_name
= stabsect_name
;
4588 s
.string_section_name
= strsect_name
;
4589 s
.string_offset
= 0;
4591 bfd_map_over_sections (abfd
, find_stabs_section
, & s
);
4597 /* Dump the any sections containing stabs debugging information. */
4600 dump_stabs (bfd
*abfd
)
4602 dump_stabs_section (abfd
, ".stab", ".stabstr");
4603 dump_stabs_section (abfd
, ".stab.excl", ".stab.exclstr");
4604 dump_stabs_section (abfd
, ".stab.index", ".stab.indexstr");
4607 dump_stabs_section (abfd
, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
4609 dump_stabs_section (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
4613 dump_bfd_header (bfd
*abfd
)
4617 printf (_("architecture: %s, "),
4618 bfd_printable_arch_mach (bfd_get_arch (abfd
),
4619 bfd_get_mach (abfd
)));
4620 printf (_("flags 0x%08x:\n"), abfd
->flags
& ~BFD_FLAGS_FOR_BFD_USE_MASK
);
4622 #define PF(x, y) if (abfd->flags & x) {printf ("%s%s", comma, y); comma=", ";}
4623 PF (HAS_RELOC
, "HAS_RELOC");
4624 PF (EXEC_P
, "EXEC_P");
4625 PF (HAS_LINENO
, "HAS_LINENO");
4626 PF (HAS_DEBUG
, "HAS_DEBUG");
4627 PF (HAS_SYMS
, "HAS_SYMS");
4628 PF (HAS_LOCALS
, "HAS_LOCALS");
4629 PF (DYNAMIC
, "DYNAMIC");
4630 PF (WP_TEXT
, "WP_TEXT");
4631 PF (D_PAGED
, "D_PAGED");
4632 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
4633 printf (_("\nstart address 0x"));
4634 bfd_printf_vma (abfd
, abfd
->start_address
);
4639 #ifdef ENABLE_LIBCTF
4640 /* Formatting callback function passed to ctf_dump. Returns either the pointer
4641 it is passed, or a pointer to newly-allocated storage, in which case
4642 dump_ctf() will free it when it no longer needs it. */
4645 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED
,
4648 const char *blanks
= arg
;
4651 if (asprintf (&new_s
, "%s%s", blanks
, s
) < 0)
4656 /* Make a ctfsect suitable for ctf_bfdopen_ctfsect(). */
4658 make_ctfsect (const char *name
, bfd_byte
*data
,
4663 ctfsect
.cts_name
= name
;
4664 ctfsect
.cts_entsize
= 1;
4665 ctfsect
.cts_size
= size
;
4666 ctfsect
.cts_data
= data
;
4671 /* Dump CTF errors/warnings. */
4673 dump_ctf_errs (ctf_dict_t
*fp
)
4675 ctf_next_t
*it
= NULL
;
4680 /* Dump accumulated errors and warnings. */
4681 while ((errtext
= ctf_errwarning_next (fp
, &it
, &is_warning
, &err
)) != NULL
)
4683 non_fatal (_("%s: %s"), is_warning
? _("warning"): _("error"),
4687 if (err
!= ECTF_NEXT_END
)
4689 non_fatal (_("CTF error: cannot get CTF errors: `%s'"),
4694 /* Dump one CTF archive member. */
4697 dump_ctf_archive_member (ctf_dict_t
*ctf
, const char *name
, ctf_dict_t
*parent
,
4700 const char *things
[] = {"Header", "Labels", "Data objects",
4701 "Function objects", "Variables", "Types", "Strings",
4706 /* Don't print out the name of the default-named archive member if it appears
4707 first in the list. The name .ctf appears everywhere, even for things that
4708 aren't really archives, so printing it out is liable to be confusing; also,
4709 the common case by far is for only one archive member to exist, and hiding
4710 it in that case seems worthwhile. */
4712 if (strcmp (name
, ".ctf") != 0 || member
!= 0)
4713 printf (_("\nCTF archive member: %s:\n"), sanitize_string (name
));
4715 if (ctf_parent_name (ctf
) != NULL
)
4716 ctf_import (ctf
, parent
);
4718 for (i
= 0, thing
= things
; *thing
[0]; thing
++, i
++)
4720 ctf_dump_state_t
*s
= NULL
;
4723 printf ("\n %s:\n", *thing
);
4724 while ((item
= ctf_dump (ctf
, &s
, i
, dump_ctf_indent_lines
,
4725 (void *) " ")) != NULL
)
4727 printf ("%s\n", item
);
4731 if (ctf_errno (ctf
))
4733 non_fatal (_("Iteration failed: %s, %s"), *thing
,
4734 ctf_errmsg (ctf_errno (ctf
)));
4739 dump_ctf_errs (ctf
);
4742 /* Dump the CTF debugging information. */
4745 dump_ctf (bfd
*abfd
, const char *sect_name
, const char *parent_name
)
4747 ctf_archive_t
*ctfa
= NULL
;
4748 bfd_byte
*ctfdata
= NULL
;
4749 bfd_size_type ctfsize
;
4753 ctf_next_t
*i
= NULL
;
4758 if (sect_name
== NULL
)
4761 if ((ctfdata
= read_section_stabs (abfd
, sect_name
, &ctfsize
, NULL
)) == NULL
)
4762 bfd_fatal (bfd_get_filename (abfd
));
4764 /* Load the CTF file and dump it. Preload the parent dict, since it will
4765 need to be imported into every child in turn. */
4767 ctfsect
= make_ctfsect (sect_name
, ctfdata
, ctfsize
);
4768 if ((ctfa
= ctf_bfdopen_ctfsect (abfd
, &ctfsect
, &err
)) == NULL
)
4770 dump_ctf_errs (NULL
);
4771 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err
));
4772 bfd_fatal (bfd_get_filename (abfd
));
4775 if ((parent
= ctf_dict_open (ctfa
, parent_name
, &err
)) == NULL
)
4777 dump_ctf_errs (NULL
);
4778 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err
));
4779 bfd_fatal (bfd_get_filename (abfd
));
4782 printf (_("Contents of CTF section %s:\n"), sanitize_string (sect_name
));
4784 while ((fp
= ctf_archive_next (ctfa
, &i
, &name
, 0, &err
)) != NULL
)
4785 dump_ctf_archive_member (fp
, name
, parent
, member
++);
4786 if (err
!= ECTF_NEXT_END
)
4788 dump_ctf_errs (NULL
);
4789 non_fatal (_("CTF archive member open failure: %s"), ctf_errmsg (err
));
4790 bfd_fatal (bfd_get_filename (abfd
));
4792 ctf_dict_close (parent
);
4798 dump_ctf (bfd
*abfd ATTRIBUTE_UNUSED
, const char *sect_name ATTRIBUTE_UNUSED
,
4799 const char *parent_name ATTRIBUTE_UNUSED
) {}
4804 dump_bfd_private_header (bfd
*abfd
)
4806 if (!bfd_print_private_bfd_data (abfd
, stdout
))
4807 non_fatal (_("warning: private headers incomplete: %s"),
4808 bfd_errmsg (bfd_get_error ()));
4812 dump_target_specific (bfd
*abfd
)
4814 const struct objdump_private_desc
* const *desc
;
4815 struct objdump_private_option
*opt
;
4818 /* Find the desc. */
4819 for (desc
= objdump_private_vectors
; *desc
!= NULL
; desc
++)
4820 if ((*desc
)->filter (abfd
))
4825 non_fatal (_("option -P/--private not supported by this file"));
4829 /* Clear all options. */
4830 for (opt
= (*desc
)->options
; opt
->name
; opt
++)
4831 opt
->selected
= false;
4833 /* Decode options. */
4834 b
= dump_private_options
;
4837 e
= strchr (b
, ',');
4842 for (opt
= (*desc
)->options
; opt
->name
; opt
++)
4843 if (strcmp (opt
->name
, b
) == 0)
4845 opt
->selected
= true;
4848 if (opt
->name
== NULL
)
4849 non_fatal (_("target specific dump '%s' not supported"), b
);
4860 (*desc
)->dump (abfd
);
4863 /* Display a section in hexadecimal format with associated characters.
4864 Each line prefixed by the zero padded address. */
4867 dump_section (bfd
*abfd
, asection
*section
, void *dummy ATTRIBUTE_UNUSED
)
4869 bfd_byte
*data
= NULL
;
4870 bfd_size_type datasize
;
4871 bfd_vma addr_offset
;
4872 bfd_vma start_offset
;
4873 bfd_vma stop_offset
;
4874 unsigned int opb
= bfd_octets_per_byte (abfd
, section
);
4875 /* Bytes per line. */
4876 const int onaline
= 16;
4881 if (! process_section_p (section
))
4884 if ((section
->flags
& SEC_HAS_CONTENTS
) == 0)
4887 if ((datasize
= bfd_section_size (section
)) == 0)
4890 /* Compute the address range to display. */
4891 if (start_address
== (bfd_vma
) -1
4892 || start_address
< section
->vma
)
4895 start_offset
= start_address
- section
->vma
;
4897 if (stop_address
== (bfd_vma
) -1)
4898 stop_offset
= datasize
/ opb
;
4901 if (stop_address
< section
->vma
)
4904 stop_offset
= stop_address
- section
->vma
;
4906 if (stop_offset
> datasize
/ opb
)
4907 stop_offset
= datasize
/ opb
;
4910 if (start_offset
>= stop_offset
)
4913 printf (_("Contents of section %s:"), sanitize_string (section
->name
));
4914 if (display_file_offsets
)
4915 printf (_(" (Starting at file offset: 0x%lx)"),
4916 (unsigned long) (section
->filepos
+ start_offset
));
4919 if (!bfd_get_full_section_contents (abfd
, section
, &data
))
4921 non_fatal (_("Reading section %s failed because: %s"),
4922 section
->name
, bfd_errmsg (bfd_get_error ()));
4928 bfd_sprintf_vma (abfd
, buf
, start_offset
+ section
->vma
);
4929 if (strlen (buf
) >= sizeof (buf
))
4933 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
4935 count
= strlen (buf
) - count
;
4939 bfd_sprintf_vma (abfd
, buf
, stop_offset
+ section
->vma
- 1);
4940 if (strlen (buf
) >= sizeof (buf
))
4944 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
4946 count
= strlen (buf
) - count
;
4950 for (addr_offset
= start_offset
;
4951 addr_offset
< stop_offset
; addr_offset
+= onaline
/ opb
)
4955 bfd_sprintf_vma (abfd
, buf
, (addr_offset
+ section
->vma
));
4956 count
= strlen (buf
);
4957 if ((size_t) count
>= sizeof (buf
))
4961 while (count
< width
)
4966 fputs (buf
+ count
- width
, stdout
);
4969 for (j
= addr_offset
* opb
;
4970 j
< addr_offset
* opb
+ onaline
; j
++)
4972 if (j
< stop_offset
* opb
)
4973 printf ("%02x", (unsigned) (data
[j
]));
4981 for (j
= addr_offset
* opb
;
4982 j
< addr_offset
* opb
+ onaline
; j
++)
4984 if (j
>= stop_offset
* opb
)
4987 printf ("%c", ISPRINT (data
[j
]) ? data
[j
] : '.');
4994 /* Actually display the various requested regions. */
4997 dump_data (bfd
*abfd
)
4999 bfd_map_over_sections (abfd
, dump_section
, NULL
);
5002 /* Should perhaps share code and display with nm? */
5005 dump_symbols (bfd
*abfd ATTRIBUTE_UNUSED
, bool dynamic
)
5014 max_count
= dynsymcount
;
5015 printf ("DYNAMIC SYMBOL TABLE:\n");
5020 max_count
= symcount
;
5021 printf ("SYMBOL TABLE:\n");
5025 printf (_("no symbols\n"));
5027 for (count
= 0; count
< max_count
; count
++)
5031 if (*current
== NULL
)
5032 printf (_("no information for symbol number %ld\n"), count
);
5034 else if ((cur_bfd
= bfd_asymbol_bfd (*current
)) == NULL
)
5035 printf (_("could not determine the type of symbol number %ld\n"),
5038 else if (process_section_p ((* current
)->section
)
5039 && (dump_special_syms
5040 || !bfd_is_target_special_symbol (cur_bfd
, *current
)))
5042 const char *name
= (*current
)->name
;
5044 if (do_demangle
&& name
!= NULL
&& *name
!= '\0')
5048 /* If we want to demangle the name, we demangle it
5049 here, and temporarily clobber it while calling
5050 bfd_print_symbol. FIXME: This is a gross hack. */
5051 alloc
= bfd_demangle (cur_bfd
, name
, demangle_flags
);
5053 (*current
)->name
= alloc
;
5054 bfd_print_symbol (cur_bfd
, stdout
, *current
,
5055 bfd_print_symbol_all
);
5058 (*current
)->name
= name
;
5062 else if (unicode_display
!= unicode_default
5063 && name
!= NULL
&& *name
!= '\0')
5065 const char * sanitized_name
;
5067 /* If we want to sanitize the name, we do it here, and
5068 temporarily clobber it while calling bfd_print_symbol.
5069 FIXME: This is a gross hack. */
5070 sanitized_name
= sanitize_string (name
);
5071 if (sanitized_name
!= name
)
5072 (*current
)->name
= sanitized_name
;
5074 sanitized_name
= NULL
;
5075 bfd_print_symbol (cur_bfd
, stdout
, *current
,
5076 bfd_print_symbol_all
);
5077 if (sanitized_name
!= NULL
)
5078 (*current
)->name
= name
;
5081 bfd_print_symbol (cur_bfd
, stdout
, *current
,
5082 bfd_print_symbol_all
);
5092 dump_reloc_set (bfd
*abfd
, asection
*sec
, arelent
**relpp
, long relcount
)
5095 char *last_filename
, *last_functionname
;
5096 unsigned int last_line
;
5097 unsigned int last_discriminator
;
5099 /* Get column headers lined up reasonably. */
5107 bfd_sprintf_vma (abfd
, buf
, (bfd_vma
) -1);
5108 width
= strlen (buf
) - 7;
5110 printf ("OFFSET %*s TYPE %*s VALUE\n", width
, "", 12, "");
5113 last_filename
= NULL
;
5114 last_functionname
= NULL
;
5116 last_discriminator
= 0;
5118 for (p
= relpp
; relcount
&& *p
!= NULL
; p
++, relcount
--)
5121 const char *filename
, *functionname
;
5122 unsigned int linenumber
;
5123 unsigned int discriminator
;
5124 const char *sym_name
;
5125 const char *section_name
;
5126 bfd_vma addend2
= 0;
5128 if (start_address
!= (bfd_vma
) -1
5129 && q
->address
< start_address
)
5131 if (stop_address
!= (bfd_vma
) -1
5132 && q
->address
> stop_address
)
5135 if (with_line_numbers
5137 && bfd_find_nearest_line_discriminator (abfd
, sec
, syms
, q
->address
,
5138 &filename
, &functionname
,
5139 &linenumber
, &discriminator
))
5141 if (functionname
!= NULL
5142 && (last_functionname
== NULL
5143 || strcmp (functionname
, last_functionname
) != 0))
5145 printf ("%s():\n", sanitize_string (functionname
));
5146 if (last_functionname
!= NULL
)
5147 free (last_functionname
);
5148 last_functionname
= xstrdup (functionname
);
5152 && (linenumber
!= last_line
5153 || (filename
!= NULL
5154 && last_filename
!= NULL
5155 && filename_cmp (filename
, last_filename
) != 0)
5156 || (discriminator
!= last_discriminator
)))
5158 if (discriminator
> 0)
5159 printf ("%s:%u\n", filename
== NULL
? "???" :
5160 sanitize_string (filename
), linenumber
);
5162 printf ("%s:%u (discriminator %u)\n",
5163 filename
== NULL
? "???" : sanitize_string (filename
),
5164 linenumber
, discriminator
);
5165 last_line
= linenumber
;
5166 last_discriminator
= discriminator
;
5167 if (last_filename
!= NULL
)
5168 free (last_filename
);
5169 if (filename
== NULL
)
5170 last_filename
= NULL
;
5172 last_filename
= xstrdup (filename
);
5176 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
5178 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
5179 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
5184 section_name
= NULL
;
5187 bfd_printf_vma (abfd
, q
->address
);
5188 if (q
->howto
== NULL
)
5189 printf (" *unknown* ");
5190 else if (q
->howto
->name
)
5192 const char *name
= q
->howto
->name
;
5194 /* R_SPARC_OLO10 relocations contain two addends.
5195 But because 'arelent' lacks enough storage to
5196 store them both, the 64-bit ELF Sparc backend
5197 records this as two relocations. One R_SPARC_LO10
5198 and one R_SPARC_13, both pointing to the same
5199 address. This is merely so that we have some
5200 place to store both addend fields.
5202 Undo this transformation, otherwise the output
5203 will be confusing. */
5204 if (abfd
->xvec
->flavour
== bfd_target_elf_flavour
5205 && elf_tdata (abfd
)->elf_header
->e_machine
== EM_SPARCV9
5207 && !strcmp (q
->howto
->name
, "R_SPARC_LO10"))
5209 arelent
*q2
= *(p
+ 1);
5212 && q
->address
== q2
->address
5213 && !strcmp (q2
->howto
->name
, "R_SPARC_13"))
5215 name
= "R_SPARC_OLO10";
5216 addend2
= q2
->addend
;
5220 printf (" %-16s ", name
);
5223 printf (" %-16d ", q
->howto
->type
);
5227 objdump_print_symname (abfd
, NULL
, *q
->sym_ptr_ptr
);
5231 if (section_name
== NULL
)
5232 section_name
= "*unknown*";
5233 printf ("[%s]", sanitize_string (section_name
));
5238 bfd_signed_vma addend
= q
->addend
;
5246 bfd_printf_vma (abfd
, addend
);
5251 bfd_printf_vma (abfd
, addend2
);
5257 if (last_filename
!= NULL
)
5258 free (last_filename
);
5259 if (last_functionname
!= NULL
)
5260 free (last_functionname
);
5264 dump_relocs_in_section (bfd
*abfd
,
5266 void *dummy ATTRIBUTE_UNUSED
)
5268 arelent
**relpp
= NULL
;
5272 if ( bfd_is_abs_section (section
)
5273 || bfd_is_und_section (section
)
5274 || bfd_is_com_section (section
)
5275 || (! process_section_p (section
))
5276 || ((section
->flags
& SEC_RELOC
) == 0))
5279 printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section
->name
));
5281 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
5284 printf (" (none)\n\n");
5292 relpp
= (arelent
**) xmalloc (relsize
);
5293 relcount
= bfd_canonicalize_reloc (abfd
, section
, relpp
, syms
);
5299 non_fatal (_("failed to read relocs in: %s"),
5300 sanitize_string (bfd_get_filename (abfd
)));
5301 bfd_fatal (_("error message was"));
5303 else if (relcount
== 0)
5304 printf (" (none)\n\n");
5308 dump_reloc_set (abfd
, section
, relpp
, relcount
);
5315 dump_relocs (bfd
*abfd
)
5317 bfd_map_over_sections (abfd
, dump_relocs_in_section
, NULL
);
5321 dump_dynamic_relocs (bfd
*abfd
)
5327 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
5329 bfd_fatal (bfd_get_filename (abfd
));
5331 printf ("DYNAMIC RELOCATION RECORDS");
5334 printf (" (none)\n\n");
5337 relpp
= (arelent
**) xmalloc (relsize
);
5338 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
5341 bfd_fatal (bfd_get_filename (abfd
));
5342 else if (relcount
== 0)
5343 printf (" (none)\n\n");
5347 dump_reloc_set (abfd
, NULL
, relpp
, relcount
);
5354 /* Creates a table of paths, to search for source files. */
5357 add_include_path (const char *path
)
5361 include_path_count
++;
5362 include_paths
= (const char **)
5363 xrealloc (include_paths
, include_path_count
* sizeof (*include_paths
));
5364 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5365 if (path
[1] == ':' && path
[2] == 0)
5366 path
= concat (path
, ".", (const char *) 0);
5368 include_paths
[include_path_count
- 1] = path
;
5372 adjust_addresses (bfd
*abfd ATTRIBUTE_UNUSED
,
5376 if ((section
->flags
& SEC_DEBUGGING
) == 0)
5378 bool *has_reloc_p
= (bool *) arg
;
5379 section
->vma
+= adjust_section_vma
;
5381 section
->lma
+= adjust_section_vma
;
5385 /* Return the sign-extended form of an ARCH_SIZE sized VMA. */
5388 sign_extend_address (bfd
*abfd ATTRIBUTE_UNUSED
,
5393 mask
= (bfd_vma
) 1 << (arch_size
- 1);
5394 return (((vma
& ((mask
<< 1) - 1)) ^ mask
) - mask
);
5398 might_need_separate_debug_info (bool is_mainfile
)
5400 /* We do not follow links from debug info files. */
5404 /* Since do_follow_links might be enabled by default, only treat it as an
5405 indication that separate files should be loaded if setting it was a
5406 deliberate user action. */
5407 if (DEFAULT_FOR_FOLLOW_LINKS
== 0 && do_follow_links
)
5410 if (process_links
|| dump_symtab
|| dump_debugging
5411 || dump_dwarf_section_info
|| with_source_code
)
5417 /* Dump selected contents of ABFD. */
5420 dump_bfd (bfd
*abfd
, bool is_mainfile
)
5422 const struct elf_backend_data
* bed
;
5424 if (bfd_big_endian (abfd
))
5425 byte_get
= byte_get_big_endian
;
5426 else if (bfd_little_endian (abfd
))
5427 byte_get
= byte_get_little_endian
;
5431 /* Load any separate debug information files. */
5432 if (byte_get
!= NULL
&& might_need_separate_debug_info (is_mainfile
))
5434 load_separate_debug_files (abfd
, bfd_get_filename (abfd
));
5436 /* If asked to do so, recursively dump the separate files. */
5437 if (do_follow_links
)
5441 for (i
= first_separate_info
; i
!= NULL
; i
= i
->next
)
5442 dump_bfd (i
->handle
, false);
5446 /* Adjust user-specified start and stop limits for targets that use
5447 signed addresses. */
5448 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
5449 && (bed
= get_elf_backend_data (abfd
)) != NULL
5450 && bed
->sign_extend_vma
)
5452 start_address
= sign_extend_address (abfd
, start_address
,
5454 stop_address
= sign_extend_address (abfd
, stop_address
,
5458 /* If we are adjusting section VMA's, change them all now. Changing
5459 the BFD information is a hack. However, we must do it, or
5460 bfd_find_nearest_line will not do the right thing. */
5461 if (adjust_section_vma
!= 0)
5463 bool has_reloc
= (abfd
->flags
& HAS_RELOC
);
5464 bfd_map_over_sections (abfd
, adjust_addresses
, &has_reloc
);
5467 if (is_mainfile
|| process_links
)
5469 if (! dump_debugging_tags
&& ! suppress_bfd_header
)
5470 printf (_("\n%s: file format %s\n"),
5471 sanitize_string (bfd_get_filename (abfd
)),
5474 print_arelt_descr (stdout
, abfd
, true, false);
5475 if (dump_file_header
)
5476 dump_bfd_header (abfd
);
5477 if (dump_private_headers
)
5478 dump_bfd_private_header (abfd
);
5479 if (dump_private_options
!= NULL
)
5480 dump_target_specific (abfd
);
5481 if (! dump_debugging_tags
&& ! suppress_bfd_header
)
5489 || dump_dwarf_section_info
)
5491 syms
= slurp_symtab (abfd
);
5493 /* If following links, load any symbol tables from the linked files as well. */
5494 if (do_follow_links
&& is_mainfile
)
5498 for (i
= first_separate_info
; i
!= NULL
; i
= i
->next
)
5500 asymbol
** extra_syms
;
5501 long old_symcount
= symcount
;
5503 extra_syms
= slurp_symtab (i
->handle
);
5507 if (old_symcount
== 0)
5513 syms
= xrealloc (syms
, ((symcount
+ old_symcount
+ 1)
5514 * sizeof (asymbol
*)));
5515 memcpy (syms
+ old_symcount
,
5517 (symcount
+ 1) * sizeof (asymbol
*));
5521 symcount
+= old_symcount
;
5526 if (is_mainfile
|| process_links
)
5528 if (dump_section_headers
)
5529 dump_headers (abfd
);
5531 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
5532 || (disassemble
&& bfd_get_dynamic_symtab_upper_bound (abfd
) > 0))
5533 dynsyms
= slurp_dynamic_symtab (abfd
);
5537 synthcount
= bfd_get_synthetic_symtab (abfd
, symcount
, syms
,
5538 dynsymcount
, dynsyms
,
5545 dump_symbols (abfd
, false);
5546 if (dump_dynamic_symtab
)
5547 dump_symbols (abfd
, true);
5549 if (dump_dwarf_section_info
)
5550 dump_dwarf (abfd
, is_mainfile
);
5551 if (is_mainfile
|| process_links
)
5553 if (dump_ctf_section_info
)
5554 dump_ctf (abfd
, dump_ctf_section_name
, dump_ctf_parent_name
);
5555 if (dump_stab_section_info
)
5557 if (dump_reloc_info
&& ! disassemble
)
5559 if (dump_dynamic_reloc_info
&& ! disassemble
)
5560 dump_dynamic_relocs (abfd
);
5561 if (dump_section_contents
)
5564 disassemble_data (abfd
);
5571 dhandle
= read_debugging_info (abfd
, syms
, symcount
, true);
5572 if (dhandle
!= NULL
)
5574 if (!print_debugging_info (stdout
, dhandle
, abfd
, syms
,
5576 dump_debugging_tags
!= 0))
5578 non_fatal (_("%s: printing debugging information failed"),
5579 bfd_get_filename (abfd
));
5585 /* PR 6483: If there was no STABS debug info in the file, try
5587 else if (! dump_dwarf_section_info
)
5589 dwarf_select_sections_all ();
5590 dump_dwarf (abfd
, is_mainfile
);
5617 free_debug_memory ();
5621 display_object_bfd (bfd
*abfd
)
5625 if (bfd_check_format_matches (abfd
, bfd_object
, &matching
))
5627 dump_bfd (abfd
, true);
5631 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
5633 nonfatal (bfd_get_filename (abfd
));
5634 list_matching_formats (matching
);
5638 if (bfd_get_error () != bfd_error_file_not_recognized
)
5640 nonfatal (bfd_get_filename (abfd
));
5644 if (bfd_check_format_matches (abfd
, bfd_core
, &matching
))
5646 dump_bfd (abfd
, true);
5650 nonfatal (bfd_get_filename (abfd
));
5652 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
5653 list_matching_formats (matching
);
5657 display_any_bfd (bfd
*file
, int level
)
5659 /* Decompress sections unless dumping the section contents. */
5660 if (!dump_section_contents
)
5661 file
->flags
|= BFD_DECOMPRESS
;
5663 /* If the file is an archive, process all of its elements. */
5664 if (bfd_check_format (file
, bfd_archive
))
5667 bfd
*last_arfile
= NULL
;
5670 printf (_("In archive %s:\n"), sanitize_string (bfd_get_filename (file
)));
5671 else if (level
> 100)
5673 /* Prevent corrupted files from spinning us into an
5674 infinite loop. 100 is an arbitrary heuristic. */
5675 fatal (_("Archive nesting is too deep"));
5679 printf (_("In nested archive %s:\n"),
5680 sanitize_string (bfd_get_filename (file
)));
5684 bfd_set_error (bfd_error_no_error
);
5686 arfile
= bfd_openr_next_archived_file (file
, arfile
);
5689 if (bfd_get_error () != bfd_error_no_more_archived_files
)
5690 nonfatal (bfd_get_filename (file
));
5694 display_any_bfd (arfile
, level
+ 1);
5696 if (last_arfile
!= NULL
)
5698 bfd_close (last_arfile
);
5699 /* PR 17512: file: ac585d01. */
5700 if (arfile
== last_arfile
)
5706 last_arfile
= arfile
;
5709 if (last_arfile
!= NULL
)
5710 bfd_close (last_arfile
);
5713 display_object_bfd (file
);
5717 display_file (char *filename
, char *target
, bool last_file
)
5721 if (get_file_size (filename
) < 1)
5727 file
= bfd_openr (filename
, target
);
5730 nonfatal (filename
);
5734 display_any_bfd (file
, 0);
5736 /* This is an optimization to improve the speed of objdump, especially when
5737 dumping a file with lots of associated debug informatiom. Calling
5738 bfd_close on such a file can take a non-trivial amount of time as there
5739 are lots of lists to walk and buffers to free. This is only really
5740 necessary however if we are about to load another file and we need the
5741 memory back. Otherwise, if we are about to exit, then we can save (a lot
5742 of) time by only doing a quick close, and allowing the OS to reclaim the
5747 bfd_close_all_done (file
);
5751 main (int argc
, char **argv
)
5754 char *target
= default_target
;
5755 bool seenflag
= false;
5757 #ifdef HAVE_LC_MESSAGES
5758 setlocale (LC_MESSAGES
, "");
5760 setlocale (LC_CTYPE
, "");
5762 bindtextdomain (PACKAGE
, LOCALEDIR
);
5763 textdomain (PACKAGE
);
5765 program_name
= *argv
;
5766 xmalloc_set_program_name (program_name
);
5767 bfd_set_error_program_name (program_name
);
5769 START_PROGRESS (program_name
, 0);
5771 expandargv (&argc
, &argv
);
5773 if (bfd_init () != BFD_INIT_MAGIC
)
5774 fatal (_("fatal error: libbfd ABI mismatch"));
5775 set_default_bfd_target ();
5777 while ((c
= getopt_long (argc
, argv
,
5778 "CDE:FGHI:LM:P:RSTU:VW::ab:defghij:lm:prstvwxz",
5779 long_options
, (int *) 0))
5785 break; /* We've been given a long option. */
5792 if (disassembler_options
)
5793 /* Ignore potential memory leak for now. */
5794 options
= concat (disassembler_options
, ",",
5795 optarg
, (const char *) NULL
);
5798 disassembler_options
= remove_whitespace_and_extra_commas (options
);
5805 display_file_offsets
= true;
5808 with_line_numbers
= true;
5817 enum demangling_styles style
;
5819 style
= cplus_demangle_name_to_style (optarg
);
5820 if (style
== unknown_demangling
)
5821 fatal (_("unknown demangling style `%s'"),
5824 cplus_demangle_set_style (style
);
5827 case OPTION_RECURSE_LIMIT
:
5828 demangle_flags
&= ~ DMGL_NO_RECURSE_LIMIT
;
5830 case OPTION_NO_RECURSE_LIMIT
:
5831 demangle_flags
|= DMGL_NO_RECURSE_LIMIT
;
5834 do_wide
= wide_output
= true;
5836 case OPTION_ADJUST_VMA
:
5837 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
5839 case OPTION_START_ADDRESS
:
5840 start_address
= parse_vma (optarg
, "--start-address");
5841 if ((stop_address
!= (bfd_vma
) -1) && stop_address
<= start_address
)
5842 fatal (_("error: the start address should be before the end address"));
5844 case OPTION_STOP_ADDRESS
:
5845 stop_address
= parse_vma (optarg
, "--stop-address");
5846 if ((start_address
!= (bfd_vma
) -1) && stop_address
<= start_address
)
5847 fatal (_("error: the stop address should be after the start address"));
5851 prefix_length
= strlen (prefix
);
5852 /* Remove an unnecessary trailing '/' */
5853 while (IS_DIR_SEPARATOR (prefix
[prefix_length
- 1]))
5856 case OPTION_PREFIX_STRIP
:
5857 prefix_strip
= atoi (optarg
);
5858 if (prefix_strip
< 0)
5859 fatal (_("error: prefix strip must be non-negative"));
5861 case OPTION_INSN_WIDTH
:
5862 insn_width
= strtoul (optarg
, NULL
, 0);
5863 if (insn_width
<= 0)
5864 fatal (_("error: instruction width must be positive"));
5866 case OPTION_INLINES
:
5867 unwind_inlines
= true;
5869 case OPTION_VISUALIZE_JUMPS
:
5870 visualize_jumps
= true;
5871 color_output
= false;
5872 extended_color_output
= false;
5875 if (streq (optarg
, "color"))
5876 color_output
= true;
5877 else if (streq (optarg
, "extended-color"))
5879 color_output
= true;
5880 extended_color_output
= true;
5882 else if (streq (optarg
, "off"))
5883 visualize_jumps
= false;
5885 nonfatal (_("unrecognized argument to --visualize-option"));
5888 case OPTION_DISASSEMBLER_COLOR
:
5889 if (streq (optarg
, "off"))
5890 disassembler_color
= off
;
5891 else if (streq (optarg
, "color"))
5892 disassembler_color
= on
;
5893 else if (streq (optarg
, "extended-color"))
5894 disassembler_color
= extended
;
5896 nonfatal (_("unrecognized argument to --disassembler-color"));
5899 if (strcmp (optarg
, "B") == 0)
5900 endian
= BFD_ENDIAN_BIG
;
5901 else if (strcmp (optarg
, "L") == 0)
5902 endian
= BFD_ENDIAN_LITTLE
;
5905 nonfatal (_("unrecognized -E option"));
5910 if (strncmp (optarg
, "big", strlen (optarg
)) == 0)
5911 endian
= BFD_ENDIAN_BIG
;
5912 else if (strncmp (optarg
, "little", strlen (optarg
)) == 0)
5913 endian
= BFD_ENDIAN_LITTLE
;
5916 non_fatal (_("unrecognized --endian type `%s'"), optarg
);
5923 dump_file_header
= true;
5927 formats_info
= true;
5931 add_include_path (optarg
);
5934 dump_private_headers
= true;
5938 dump_private_options
= optarg
;
5942 dump_private_headers
= true;
5944 dump_reloc_info
= true;
5945 dump_file_header
= true;
5946 dump_ar_hdrs
= true;
5947 dump_section_headers
= true;
5955 dump_dynamic_symtab
= true;
5961 disasm_sym
= optarg
;
5964 disassemble_zeroes
= true;
5968 disassemble_all
= true;
5973 with_source_code
= true;
5976 case OPTION_SOURCE_COMMENT
:
5978 with_source_code
= true;
5981 source_comment
= xstrdup (sanitize_string (optarg
));
5983 source_comment
= xstrdup ("# ");
5991 dump_debugging_tags
= 1;
5996 process_links
= true;
5997 do_follow_links
= true;
6003 if (dwarf_select_sections_by_letters (optarg
))
6004 dump_dwarf_section_info
= true;
6008 dump_dwarf_section_info
= true;
6009 dwarf_select_sections_all ();
6016 if (dwarf_select_sections_by_names (optarg
))
6017 dump_dwarf_section_info
= true;
6021 dwarf_select_sections_all ();
6022 dump_dwarf_section_info
= true;
6025 case OPTION_DWARF_DEPTH
:
6028 dwarf_cutoff_level
= strtoul (optarg
, & cp
, 0);
6031 case OPTION_DWARF_START
:
6034 dwarf_start_die
= strtoul (optarg
, & cp
, 0);
6035 suppress_bfd_header
= 1;
6038 case OPTION_DWARF_CHECK
:
6041 #ifdef ENABLE_LIBCTF
6043 dump_ctf_section_info
= true;
6045 dump_ctf_section_name
= xstrdup (optarg
);
6048 case OPTION_CTF_PARENT
:
6049 dump_ctf_parent_name
= xstrdup (optarg
);
6053 dump_stab_section_info
= true;
6057 dump_section_contents
= true;
6061 dump_reloc_info
= true;
6065 dump_dynamic_reloc_info
= true;
6069 dump_ar_hdrs
= true;
6073 dump_section_headers
= true;
6078 show_version
= true;
6083 if (streq (optarg
, "default") || streq (optarg
, "d"))
6084 unicode_display
= unicode_default
;
6085 else if (streq (optarg
, "locale") || streq (optarg
, "l"))
6086 unicode_display
= unicode_locale
;
6087 else if (streq (optarg
, "escape") || streq (optarg
, "e"))
6088 unicode_display
= unicode_escape
;
6089 else if (streq (optarg
, "invalid") || streq (optarg
, "i"))
6090 unicode_display
= unicode_invalid
;
6091 else if (streq (optarg
, "hex") || streq (optarg
, "x"))
6092 unicode_display
= unicode_hex
;
6093 else if (streq (optarg
, "highlight") || streq (optarg
, "h"))
6094 unicode_display
= unicode_highlight
;
6096 fatal (_("invalid argument to -U/--unicode: %s"), optarg
);
6101 /* No need to set seenflag or to break - usage() does not return. */
6107 if (disassembler_color
== on_if_terminal_output
)
6108 disassembler_color
= isatty (1) ? on
: off
;
6111 print_version ("objdump");
6116 dump_any_debugging
= (dump_debugging
6117 || dump_dwarf_section_info
6119 || with_source_code
);
6122 exit_status
= display_info ();
6126 display_file ("a.out", target
, true);
6128 for (; optind
< argc
;)
6130 display_file (argv
[optind
], target
, optind
== argc
- 1);
6136 free (dump_ctf_section_name
);
6137 free (dump_ctf_parent_name
);
6138 free ((void *) source_comment
);
6140 END_PROGRESS (program_name
);