]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame_incremental - binutils/objdump.c
[gdb/tdep] Fix selftest scoped_mmap on freebsd
[thirdparty/binutils-gdb.git] / binutils / objdump.c
... / ...
CommitLineData
1/* objdump.c -- dump information about an object file.
2 Copyright (C) 1990-2025 Free Software Foundation, Inc.
3
4 This file is part of GNU Binutils.
5
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)
9 any later version.
10
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.
15
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. */
20
21
22/* Objdump overview.
23
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.
28
29 The flow of execution is as follows:
30
31 1. Command line arguments are checked for control switches and the
32 information to be displayed is selected.
33
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.
37
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
40 called.
41
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.
45
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. */
50
51#include "sysdep.h"
52#include "bfd.h"
53#include "elf-bfd.h"
54#include "coff-bfd.h"
55#include "bucomm.h"
56#include "elfcomm.h"
57#include "demanguse.h"
58#include "dwarf.h"
59#include "ctf-api.h"
60#include "sframe-api.h"
61#include "getopt.h"
62#include "safe-ctype.h"
63#include "dis-asm.h"
64#include "libiberty.h"
65#include "demangle.h"
66#include "filenames.h"
67#include "debug.h"
68#include "budbg.h"
69#include "objdump.h"
70
71#ifdef HAVE_MMAP
72#include <sys/mman.h>
73#endif
74
75#ifdef HAVE_LIBDEBUGINFOD
76#include <elfutils/debuginfod.h>
77#endif
78
79/* Internal headers for the ELF .stab-dump code - sorry. */
80#define BYTES_IN_WORD 32
81#include "aout/aout64.h"
82
83/* Exit status. */
84static int exit_status = 0;
85
86static char *default_target = NULL; /* Default at runtime. */
87
88/* The following variables are set based on arguments passed on the
89 command line. */
90static int show_version = 0; /* Show the version number. */
91static int dump_section_contents; /* -s */
92static int dump_section_headers; /* -h */
93static bool dump_file_header; /* -f */
94static int dump_symtab; /* -t */
95static int dump_dynamic_symtab; /* -T */
96static int dump_reloc_info; /* -r */
97static int dump_dynamic_reloc_info; /* -R */
98static int dump_ar_hdrs; /* -a */
99static int dump_private_headers; /* -p */
100static char *dump_private_options; /* -P */
101static int no_addresses; /* --no-addresses */
102static int prefix_addresses; /* --prefix-addresses */
103static int with_line_numbers; /* -l */
104static bool with_source_code; /* -S */
105static int show_raw_insn; /* --show-raw-insn */
106static int dump_dwarf_section_info; /* --dwarf */
107static int dump_stab_section_info; /* --stabs */
108static int dump_ctf_section_info; /* --ctf */
109static char *dump_ctf_section_name;
110static char *dump_ctf_parent_name; /* --ctf-parent */
111static char *dump_ctf_parent_section_name; /* --ctf-parent-section */
112static int dump_sframe_section_info; /* --sframe */
113static char *dump_sframe_section_name;
114static int do_demangle; /* -C, --demangle */
115static bool disassemble; /* -d */
116static bool disassemble_all; /* -D */
117static int disassemble_zeroes; /* --disassemble-zeroes */
118static bool formats_info; /* -i */
119int wide_output; /* -w */
120#define MAX_INSN_WIDTH 49
121static unsigned long insn_width; /* --insn-width */
122static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
123static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
124static int dump_debugging; /* --debugging */
125static int dump_debugging_tags; /* --debugging-tags */
126static int suppress_bfd_header;
127static int dump_special_syms = 0; /* --special-syms */
128static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
129static int file_start_context = 0; /* --file-start-context */
130static bool display_file_offsets; /* -F */
131static const char *prefix; /* --prefix */
132static int prefix_strip; /* --prefix-strip */
133static size_t prefix_length;
134static bool unwind_inlines; /* --inlines. */
135static const char * source_comment; /* --source_comment. */
136static bool visualize_jumps = false; /* --visualize-jumps. */
137static bool color_output = false; /* --visualize-jumps=color. */
138static bool extended_color_output = false; /* --visualize-jumps=extended-color. */
139static int process_links = false; /* --process-links. */
140static int show_all_symbols; /* --show-all-symbols. */
141static bool decompressed_dumps = false; /* -Z, --decompress. */
142
143static struct symbol_entry
144 {
145 const char *name;
146 struct symbol_entry *next;
147 } *disasm_sym_list; /* Disassembly start symbol(s). */
148
149static enum color_selection
150 {
151 on_if_terminal_output,
152 on, /* --disassembler-color=color. */
153 off, /* --disassembler-color=off. */
154 extended /* --disassembler-color=extended-color. */
155 } disassembler_color =
156#if DEFAULT_FOR_COLORED_DISASSEMBLY
157 on_if_terminal_output;
158#else
159 off;
160#endif
161
162static int dump_any_debugging;
163static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
164
165/* This is reset to false each time we enter the disassembler, and set true
166 when the disassembler emits something in the dis_style_comment_start
167 style. Once this is true, all further output on that line is done in
168 the comment style. This only has an effect when disassembler coloring
169 is turned on. */
170static bool disassembler_in_comment = false;
171
172/* A structure to record the sections mentioned in -j switches. */
173struct only
174{
175 const char *name; /* The name of the section. */
176 bool seen; /* A flag to indicate that the section has been found in one or more input files. */
177 struct only *next; /* Pointer to the next structure in the list. */
178};
179/* Pointer to an array of 'only' structures.
180 This pointer is NULL if the -j switch has not been used. */
181static struct only * only_list = NULL;
182
183/* Variables for handling include file path table. */
184static const char **include_paths;
185static int include_path_count;
186
187/* Extra info to pass to the section disassembler and address printing
188 function. */
189struct objdump_disasm_info
190{
191 bfd *abfd;
192 bool require_sec;
193 disassembler_ftype disassemble_fn;
194 arelent *reloc;
195 struct symbol_entry *symbol_list;
196};
197
198/* Architecture to disassemble for, or default if NULL. */
199static char *machine = NULL;
200
201/* Target specific options to the disassembler. */
202static char *disassembler_options = NULL;
203
204/* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
205static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
206
207/* The symbol table. */
208static asymbol **syms;
209
210/* Number of symbols in `syms'. */
211static long symcount = 0;
212
213/* The sorted symbol table. */
214static asymbol **sorted_syms;
215
216/* Number of symbols in `sorted_syms'. */
217static long sorted_symcount = 0;
218
219/* The dynamic symbol table. */
220static asymbol **dynsyms;
221
222/* The synthetic symbol table. */
223static asymbol *synthsyms;
224static long synthcount = 0;
225
226/* Number of symbols in `dynsyms'. */
227static long dynsymcount = 0;
228
229static bfd_byte *stabs;
230static bfd_size_type stab_size;
231
232static bfd_byte *strtab;
233static bfd_size_type stabstr_size;
234
235/* Handlers for -P/--private. */
236static const struct objdump_private_desc * const objdump_private_vectors[] =
237 {
238 OBJDUMP_PRIVATE_VECTORS
239 NULL
240 };
241
242/* The list of detected jumps inside a function. */
243static struct jump_info *detected_jumps = NULL;
244
245typedef enum unicode_display_type
246{
247 unicode_default = 0,
248 unicode_locale,
249 unicode_escape,
250 unicode_hex,
251 unicode_highlight,
252 unicode_invalid
253} unicode_display_type;
254
255static unicode_display_type unicode_display = unicode_default;
256\f
257static void usage (FILE *, int) ATTRIBUTE_NORETURN;
258static void
259usage (FILE *stream, int status)
260{
261 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
262 fprintf (stream, _(" Display information from object <file(s)>.\n"));
263 fprintf (stream, _(" At least one of the following switches must be given:\n"));
264 fprintf (stream, _("\
265 -a, --archive-headers Display archive header information\n"));
266 fprintf (stream, _("\
267 -f, --file-headers Display the contents of the overall file header\n"));
268 fprintf (stream, _("\
269 -p, --private-headers Display object format specific file header contents\n"));
270 fprintf (stream, _("\
271 -P, --private=OPT,OPT... Display object format specific contents\n"));
272 fprintf (stream, _("\
273 -h, --[section-]headers Display the contents of the section headers\n"));
274 fprintf (stream, _("\
275 -x, --all-headers Display the contents of all headers\n"));
276 fprintf (stream, _("\
277 -d, --disassemble Display assembler contents of executable sections\n"));
278 fprintf (stream, _("\
279 -D, --disassemble-all Display assembler contents of all sections\n"));
280 fprintf (stream, _("\
281 --disassemble=<sym> Display assembler contents from <sym>\n"));
282 fprintf (stream, _("\
283 -S, --source Intermix source code with disassembly\n"));
284 fprintf (stream, _("\
285 --source-comment[=<txt>] Prefix lines of source code with <txt>\n"));
286 fprintf (stream, _("\
287 -s, --full-contents Display the full contents of all sections requested\n"));
288 fprintf (stream, _("\
289 -Z, --decompress Decompress section(s) before displaying their contents\n"));
290 fprintf (stream, _("\
291 -g, --debugging Display debug information in object file\n"));
292 fprintf (stream, _("\
293 -e, --debugging-tags Display debug information using ctags style\n"));
294 fprintf (stream, _("\
295 -G, --stabs Display (in raw form) any STABS info in the file\n"));
296 fprintf (stream, _("\
297 -W, --dwarf[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
298 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
299 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
300 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
301 U/=trace_info]\n\
302 Display the contents of DWARF debug sections\n"));
303 fprintf (stream, _("\
304 -Wk,--dwarf=links Display the contents of sections that link to\n\
305 separate debuginfo files\n"));
306#if DEFAULT_FOR_FOLLOW_LINKS
307 fprintf (stream, _("\
308 -WK,--dwarf=follow-links\n\
309 Follow links to separate debug info files (default)\n"));
310 fprintf (stream, _("\
311 -WN,--dwarf=no-follow-links\n\
312 Do not follow links to separate debug info files\n"));
313#else
314 fprintf (stream, _("\
315 -WK,--dwarf=follow-links\n\
316 Follow links to separate debug info files\n"));
317 fprintf (stream, _("\
318 -WN,--dwarf=no-follow-links\n\
319 Do not follow links to separate debug info files\n\
320 (default)\n"));
321#endif
322#if HAVE_LIBDEBUGINFOD
323 fprintf (stream, _("\
324 -WD --dwarf=use-debuginfod\n\
325 When following links, also query debuginfod servers (default)\n"));
326 fprintf (stream, _("\
327 -WE --dwarf=do-not-use-debuginfod\n\
328 When following links, do not query debuginfod servers\n"));
329#endif
330 fprintf (stream, _("\
331 -L, --process-links Display the contents of non-debug sections in\n\
332 separate debuginfo files. (Implies -WK)\n"));
333#ifdef ENABLE_LIBCTF
334 fprintf (stream, _("\
335 --ctf[=SECTION] Display CTF info from SECTION, (default `.ctf')\n"));
336#endif
337 fprintf (stream, _("\
338 --sframe[=SECTION] Display SFrame info from SECTION, (default '.sframe')\n"));
339 fprintf (stream, _("\
340 -t, --syms Display the contents of the symbol table(s)\n"));
341 fprintf (stream, _("\
342 -T, --dynamic-syms Display the contents of the dynamic symbol table\n"));
343 fprintf (stream, _("\
344 -r, --reloc Display the relocation entries in the file\n"));
345 fprintf (stream, _("\
346 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n"));
347 fprintf (stream, _("\
348 @<file> Read options from <file>\n"));
349 fprintf (stream, _("\
350 -v, --version Display this program's version number\n"));
351 fprintf (stream, _("\
352 -i, --info List object formats and architectures supported\n"));
353 fprintf (stream, _("\
354 -H, --help Display this information\n"));
355
356 if (status != 2)
357 {
358 const struct objdump_private_desc * const *desc;
359
360 fprintf (stream, _("\n The following switches are optional:\n"));
361 fprintf (stream, _("\
362 -b, --target=BFDNAME Specify the target object format as BFDNAME\n"));
363 fprintf (stream, _("\
364 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n"));
365 fprintf (stream, _("\
366 -j, --section=NAME Only display information for section NAME\n"));
367 fprintf (stream, _("\
368 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n"));
369 fprintf (stream, _("\
370 -EB --endian=big Assume big endian format when disassembling\n"));
371 fprintf (stream, _("\
372 -EL --endian=little Assume little endian format when disassembling\n"));
373 fprintf (stream, _("\
374 --file-start-context Include context from start of file (with -S)\n"));
375 fprintf (stream, _("\
376 -I, --include=DIR Add DIR to search list for source files\n"));
377 fprintf (stream, _("\
378 -l, --line-numbers Include line numbers and filenames in output\n"));
379 fprintf (stream, _("\
380 -F, --file-offsets Include file offsets when displaying information\n"));
381 fprintf (stream, _("\
382 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n"));
383 display_demangler_styles (stream, _("\
384 STYLE can be "));
385 fprintf (stream, _("\
386 --recurse-limit Enable a limit on recursion whilst demangling\n\
387 (default)\n"));
388 fprintf (stream, _("\
389 --no-recurse-limit Disable a limit on recursion whilst demangling\n"));
390 fprintf (stream, _("\
391 -w, --wide Format output for more than 80 columns\n"));
392 fprintf (stream, _("\
393 -U[d|l|i|x|e|h] Controls the display of UTF-8 unicode characters\n\
394 --unicode=[default|locale|invalid|hex|escape|highlight]\n"));
395 fprintf (stream, _("\
396 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n"));
397 fprintf (stream, _("\
398 --start-address=ADDR Only process data whose address is >= ADDR\n"));
399 fprintf (stream, _("\
400 --stop-address=ADDR Only process data whose address is < ADDR\n"));
401 fprintf (stream, _("\
402 --no-addresses Do not print address alongside disassembly\n"));
403 fprintf (stream, _("\
404 --prefix-addresses Print complete address alongside disassembly\n"));
405 fprintf (stream, _("\
406 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n"));
407 fprintf (stream, _("\
408 --insn-width=WIDTH Display WIDTH bytes on a single line for -d\n"));
409 fprintf (stream, _("\
410 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n"));
411 fprintf (stream, _("\
412 --show-all-symbols When disassembling, display all symbols at a given address\n"));
413 fprintf (stream, _("\
414 --special-syms Include special symbols in symbol dumps\n"));
415 fprintf (stream, _("\
416 --inlines Print all inlines for source line (with -l)\n"));
417 fprintf (stream, _("\
418 --prefix=PREFIX Add PREFIX to absolute paths for -S\n"));
419 fprintf (stream, _("\
420 --prefix-strip=LEVEL Strip initial directory names for -S\n"));
421 fprintf (stream, _("\
422 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
423 fprintf (stream, _("\
424 --dwarf-start=N Display DIEs starting at offset N\n"));
425 fprintf (stream, _("\
426 --dwarf-check Make additional dwarf consistency checks.\n"));
427#ifdef ENABLE_LIBCTF
428 fprintf (stream, _("\
429 --ctf-parent=NAME Use CTF archive member NAME as the CTF parent\n"));
430#endif
431 fprintf (stream, _("\
432 --visualize-jumps Visualize jumps by drawing ASCII art lines\n"));
433 fprintf (stream, _("\
434 --visualize-jumps=color Use colors in the ASCII art\n"));
435 fprintf (stream, _("\
436 --visualize-jumps=extended-color\n\
437 Use extended 8-bit color codes\n"));
438 fprintf (stream, _("\
439 --visualize-jumps=off Disable jump visualization\n"));
440#if DEFAULT_FOR_COLORED_DISASSEMBLY
441 fprintf (stream, _("\
442 --disassembler-color=off Disable disassembler color output.\n"));
443 fprintf (stream, _("\
444 --disassembler-color=terminal Enable disassembler color output if displaying on a terminal. (default)\n"));
445#else
446 fprintf (stream, _("\
447 --disassembler-color=off Disable disassembler color output. (default)\n"));
448 fprintf (stream, _("\
449 --disassembler-color=terminal Enable disassembler color output if displaying on a terminal.\n"));
450#endif
451 fprintf (stream, _("\
452 --disassembler-color=on Enable disassembler color output.\n"));
453 fprintf (stream, _("\
454 --disassembler-color=extended Use 8-bit colors in disassembler output.\n\n"));
455
456 list_supported_targets (program_name, stream);
457 list_supported_architectures (program_name, stream);
458
459 disassembler_usage (stream);
460
461 if (objdump_private_vectors[0] != NULL)
462 {
463 fprintf (stream,
464 _("\nOptions supported for -P/--private switch:\n"));
465 for (desc = objdump_private_vectors; *desc != NULL; desc++)
466 (*desc)->help (stream);
467 }
468 }
469 if (REPORT_BUGS_TO[0] && status == 0)
470 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
471 exit (status);
472}
473
474/* 150 isn't special; it's just an arbitrary non-ASCII char value. */
475enum option_values
476 {
477 OPTION_ENDIAN=150,
478 OPTION_START_ADDRESS,
479 OPTION_STOP_ADDRESS,
480 OPTION_DWARF,
481 OPTION_PREFIX,
482 OPTION_PREFIX_STRIP,
483 OPTION_INSN_WIDTH,
484 OPTION_ADJUST_VMA,
485 OPTION_DWARF_DEPTH,
486 OPTION_DWARF_CHECK,
487 OPTION_DWARF_START,
488 OPTION_RECURSE_LIMIT,
489 OPTION_NO_RECURSE_LIMIT,
490 OPTION_INLINES,
491 OPTION_SOURCE_COMMENT,
492#ifdef ENABLE_LIBCTF
493 OPTION_CTF,
494 OPTION_CTF_PARENT,
495 OPTION_CTF_PARENT_SECTION,
496#endif
497 OPTION_SFRAME,
498 OPTION_VISUALIZE_JUMPS,
499 OPTION_DISASSEMBLER_COLOR
500 };
501
502static struct option long_options[]=
503{
504 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
505 {"all-headers", no_argument, NULL, 'x'},
506 {"architecture", required_argument, NULL, 'm'},
507 {"archive-headers", no_argument, NULL, 'a'},
508#ifdef ENABLE_LIBCTF
509 {"ctf", optional_argument, NULL, OPTION_CTF},
510 {"ctf-parent", required_argument, NULL, OPTION_CTF_PARENT},
511 {"ctf-parent-section", required_argument, NULL, OPTION_CTF_PARENT_SECTION},
512#endif
513 {"debugging", no_argument, NULL, 'g'},
514 {"debugging-tags", no_argument, NULL, 'e'},
515 {"decompress", no_argument, NULL, 'Z'},
516 {"demangle", optional_argument, NULL, 'C'},
517 {"disassemble", optional_argument, NULL, 'd'},
518 {"disassemble-all", no_argument, NULL, 'D'},
519 {"disassemble-zeroes", no_argument, NULL, 'z'},
520 {"disassembler-options", required_argument, NULL, 'M'},
521 {"dwarf", optional_argument, NULL, OPTION_DWARF},
522 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
523 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
524 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
525 {"dynamic-reloc", no_argument, NULL, 'R'},
526 {"dynamic-syms", no_argument, NULL, 'T'},
527 {"endian", required_argument, NULL, OPTION_ENDIAN},
528 {"file-headers", no_argument, NULL, 'f'},
529 {"file-offsets", no_argument, NULL, 'F'},
530 {"file-start-context", no_argument, &file_start_context, 1},
531 {"full-contents", no_argument, NULL, 's'},
532 {"headers", no_argument, NULL, 'h'},
533 {"help", no_argument, NULL, 'H'},
534 {"include", required_argument, NULL, 'I'},
535 {"info", no_argument, NULL, 'i'},
536 {"inlines", no_argument, 0, OPTION_INLINES},
537 {"insn-width", required_argument, NULL, OPTION_INSN_WIDTH},
538 {"line-numbers", no_argument, NULL, 'l'},
539 {"no-addresses", no_argument, &no_addresses, 1},
540 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
541 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
542 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
543 {"prefix", required_argument, NULL, OPTION_PREFIX},
544 {"prefix-addresses", no_argument, &prefix_addresses, 1},
545 {"prefix-strip", required_argument, NULL, OPTION_PREFIX_STRIP},
546 {"private", required_argument, NULL, 'P'},
547 {"private-headers", no_argument, NULL, 'p'},
548 {"process-links", no_argument, &process_links, true},
549 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
550 {"recursion-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
551 {"reloc", no_argument, NULL, 'r'},
552 {"section", required_argument, NULL, 'j'},
553 {"section-headers", no_argument, NULL, 'h'},
554 {"sframe", optional_argument, NULL, OPTION_SFRAME},
555 {"show-all-symbols", no_argument, &show_all_symbols, 1},
556 {"show-raw-insn", no_argument, &show_raw_insn, 1},
557 {"source", no_argument, NULL, 'S'},
558 {"source-comment", optional_argument, NULL, OPTION_SOURCE_COMMENT},
559 {"special-syms", no_argument, &dump_special_syms, 1},
560 {"stabs", no_argument, NULL, 'G'},
561 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
562 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
563 {"syms", no_argument, NULL, 't'},
564 {"target", required_argument, NULL, 'b'},
565 {"unicode", required_argument, NULL, 'U'},
566 {"version", no_argument, NULL, 'V'},
567 {"visualize-jumps", optional_argument, 0, OPTION_VISUALIZE_JUMPS},
568 {"wide", no_argument, NULL, 'w'},
569 {"disassembler-color", required_argument, NULL, OPTION_DISASSEMBLER_COLOR},
570 {NULL, no_argument, NULL, 0}
571};
572\f
573static void
574my_bfd_nonfatal (const char *msg)
575{
576 bfd_nonfatal (msg);
577 exit_status = 1;
578}
579
580/* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
581 The conversion format is controlled by the unicode_display variable.
582 Returns the number of characters added to OUT.
583 Returns the number of bytes consumed from IN in CONSUMED.
584 Always consumes at least one byte and displays at least one character. */
585
586static unsigned int
587display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
588{
589 char * orig_out = out;
590 unsigned int nchars = 0;
591 unsigned int j;
592
593 if (unicode_display == unicode_default)
594 goto invalid;
595
596 if (in[0] < 0xc0)
597 goto invalid;
598
599 if ((in[1] & 0xc0) != 0x80)
600 goto invalid;
601
602 if ((in[0] & 0x20) == 0)
603 {
604 nchars = 2;
605 goto valid;
606 }
607
608 if ((in[2] & 0xc0) != 0x80)
609 goto invalid;
610
611 if ((in[0] & 0x10) == 0)
612 {
613 nchars = 3;
614 goto valid;
615 }
616
617 if ((in[3] & 0xc0) != 0x80)
618 goto invalid;
619
620 nchars = 4;
621
622 valid:
623 switch (unicode_display)
624 {
625 case unicode_locale:
626 /* Copy the bytes into the output buffer as is. */
627 memcpy (out, in, nchars);
628 out += nchars;
629 break;
630
631 case unicode_invalid:
632 case unicode_hex:
633 *out++ = unicode_display == unicode_hex ? '<' : '{';
634 *out++ = '0';
635 *out++ = 'x';
636 for (j = 0; j < nchars; j++)
637 out += sprintf (out, "%02x", in [j]);
638 *out++ = unicode_display == unicode_hex ? '>' : '}';
639 break;
640
641 case unicode_highlight:
642 if (isatty (1))
643 out += sprintf (out, "\x1B[31;47m"); /* Red. */
644 /* Fall through. */
645 case unicode_escape:
646 switch (nchars)
647 {
648 case 2:
649 out += sprintf (out, "\\u%02x%02x",
650 ((in[0] & 0x1c) >> 2),
651 ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
652 break;
653
654 case 3:
655 out += sprintf (out, "\\u%02x%02x",
656 ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
657 ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
658 break;
659
660 case 4:
661 out += sprintf (out, "\\u%02x%02x%02x",
662 ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
663 ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
664 ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
665 break;
666 default:
667 /* URG. */
668 break;
669 }
670
671 if (unicode_display == unicode_highlight && isatty (1))
672 out += sprintf (out, "\x1B[0m"); /* Default colour. */
673 break;
674
675 default:
676 /* URG */
677 break;
678 }
679
680 * consumed = nchars;
681 return out - orig_out;
682
683 invalid:
684 /* Not a valid UTF-8 sequence. */
685 *out = *in;
686 * consumed = 1;
687 return 1;
688}
689
690/* Returns a version of IN with any control characters
691 replaced by escape sequences. Uses a static buffer
692 if necessary.
693
694 If unicode display is enabled, then also handles the
695 conversion of unicode characters. */
696
697static const char *
698sanitize_string (const char * in)
699{
700 static char * buffer = NULL;
701 static size_t buffer_len = 0;
702 const char * original = in;
703 char * out;
704
705 /* Paranoia. */
706 if (in == NULL)
707 return "";
708
709 /* See if any conversion is necessary. In the majority
710 of cases it will not be needed. */
711 do
712 {
713 unsigned char c = *in++;
714
715 if (c == 0)
716 return original;
717
718 if (ISCNTRL (c))
719 break;
720
721 if (unicode_display != unicode_default && c >= 0xc0)
722 break;
723 }
724 while (1);
725
726 /* Copy the input, translating as needed. */
727 in = original;
728 /* For 2 char unicode, max out is 12 (colour escapes) + 6, ie. 9 per in
729 For hex, max out is 8 for 2 char unicode, ie. 4 per in.
730 3 and 4 char unicode produce less output for input. */
731 size_t max_needed = strlen (in) * 9 + 1;
732 if (buffer_len < max_needed)
733 {
734 buffer_len = max_needed;
735 free (buffer);
736 buffer = xmalloc (buffer_len);
737 }
738
739 out = buffer;
740 do
741 {
742 unsigned char c = *in++;
743
744 if (c == 0)
745 break;
746
747 if (ISCNTRL (c))
748 {
749 *out++ = '^';
750 *out++ = c + 0x40;
751 }
752 else if (unicode_display != unicode_default && c >= 0xc0)
753 {
754 unsigned int num_consumed;
755
756 out += display_utf8 ((const unsigned char *) --in, out, &num_consumed);
757 in += num_consumed;
758 }
759 else
760 *out++ = c;
761 }
762 while (1);
763
764 *out = 0;
765 return buffer;
766}
767
768\f
769/* Returns TRUE if the specified section should be dumped. */
770
771static bool
772process_section_p (asection * section)
773{
774 struct only * only;
775
776 if (only_list == NULL)
777 return true;
778
779 for (only = only_list; only; only = only->next)
780 if (strcmp (only->name, section->name) == 0)
781 {
782 only->seen = true;
783 return true;
784 }
785
786 return false;
787}
788
789/* Add an entry to the 'only' list. */
790
791static void
792add_only (char * name)
793{
794 struct only * only;
795
796 /* First check to make sure that we do not
797 already have an entry for this name. */
798 for (only = only_list; only; only = only->next)
799 if (strcmp (only->name, name) == 0)
800 return;
801
802 only = xmalloc (sizeof * only);
803 only->name = name;
804 only->seen = false;
805 only->next = only_list;
806 only_list = only;
807}
808
809/* Release the memory used by the 'only' list.
810 PR 11225: Issue a warning message for unseen sections.
811 Only do this if none of the sections were seen. This is mainly to support
812 tools like the GAS testsuite where an object file is dumped with a list of
813 generic section names known to be present in a range of different file
814 formats. */
815
816static void
817free_only_list (void)
818{
819 bool at_least_one_seen = false;
820 struct only * only;
821 struct only * next;
822
823 if (only_list == NULL)
824 return;
825
826 for (only = only_list; only; only = only->next)
827 if (only->seen)
828 {
829 at_least_one_seen = true;
830 break;
831 }
832
833 for (only = only_list; only; only = next)
834 {
835 if (! at_least_one_seen)
836 {
837 non_fatal (_("section '%s' mentioned in a -j option, "
838 "but not found in any input file"),
839 only->name);
840 exit_status = 1;
841 }
842 next = only->next;
843 free (only);
844 }
845}
846
847\f
848static void
849dump_section_header (bfd *abfd, asection *section, void *data)
850{
851 char *comma = "";
852 unsigned int opb = bfd_octets_per_byte (abfd, section);
853 int longest_section_name = *((int *) data);
854
855 /* Ignore linker created section. See elfNN_ia64_object_p in
856 bfd/elfxx-ia64.c. */
857 if (section->flags & SEC_LINKER_CREATED)
858 return;
859
860 /* PR 10413: Skip sections that we are ignoring. */
861 if (! process_section_p (section))
862 return;
863
864 printf ("%3d %-*s %08lx ", section->index, longest_section_name,
865 sanitize_string (bfd_section_name (section)),
866 (unsigned long) bfd_section_size (section) / opb);
867 bfd_printf_vma (abfd, bfd_section_vma (section));
868 printf (" ");
869 bfd_printf_vma (abfd, section->lma);
870 printf (" %08lx 2**%u", (unsigned long) section->filepos,
871 bfd_section_alignment (section));
872 if (! wide_output)
873 printf ("\n ");
874 printf (" ");
875
876#define PF(x, y) \
877 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
878
879 PF (SEC_HAS_CONTENTS, "CONTENTS");
880 PF (SEC_ALLOC, "ALLOC");
881 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
882 PF (SEC_LOAD, "LOAD");
883 PF (SEC_RELOC, "RELOC");
884 PF (SEC_READONLY, "READONLY");
885 PF (SEC_CODE, "CODE");
886 PF (SEC_DATA, "DATA");
887 PF (SEC_ROM, "ROM");
888 PF (SEC_DEBUGGING, "DEBUGGING");
889 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
890 PF (SEC_EXCLUDE, "EXCLUDE");
891 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
892 if (bfd_get_arch (abfd) == bfd_arch_tic54x)
893 {
894 PF (SEC_TIC54X_BLOCK, "BLOCK");
895 PF (SEC_TIC54X_CLINK, "CLINK");
896 }
897 PF (SEC_SMALL_DATA, "SMALL_DATA");
898 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
899 {
900 PF (SEC_COFF_SHARED, "SHARED");
901 PF (SEC_COFF_NOREAD, "NOREAD");
902 }
903 else if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
904 {
905 PF (SEC_ELF_OCTETS, "OCTETS");
906 PF (SEC_ELF_PURECODE, "PURECODE");
907 }
908 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
909 PF (SEC_GROUP, "GROUP");
910 if (bfd_get_arch (abfd) == bfd_arch_mep)
911 {
912 PF (SEC_MEP_VLIW, "VLIW");
913 }
914
915 if ((section->flags & SEC_LINK_ONCE) != 0)
916 {
917 const char *ls;
918 struct coff_comdat_info *comdat;
919
920 switch (section->flags & SEC_LINK_DUPLICATES)
921 {
922 default:
923 abort ();
924 case SEC_LINK_DUPLICATES_DISCARD:
925 ls = "LINK_ONCE_DISCARD";
926 break;
927 case SEC_LINK_DUPLICATES_ONE_ONLY:
928 ls = "LINK_ONCE_ONE_ONLY";
929 break;
930 case SEC_LINK_DUPLICATES_SAME_SIZE:
931 ls = "LINK_ONCE_SAME_SIZE";
932 break;
933 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
934 ls = "LINK_ONCE_SAME_CONTENTS";
935 break;
936 }
937 printf ("%s%s", comma, ls);
938
939 comdat = bfd_coff_get_comdat_section (abfd, section);
940 if (comdat != NULL)
941 printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
942
943 comma = ", ";
944 }
945
946 if (bfd_is_section_compressed (abfd, section))
947 printf ("%sCOMPRESSED", comma);
948
949 printf ("\n");
950#undef PF
951}
952
953/* Called on each SECTION in ABFD, update the int variable pointed to by
954 DATA which contains the string length of the longest section name. */
955
956static void
957find_longest_section_name (bfd *abfd ATTRIBUTE_UNUSED,
958 asection *section, void *data)
959{
960 int *longest_so_far = (int *) data;
961 const char *name;
962 int len;
963
964 /* Ignore linker created section. */
965 if (section->flags & SEC_LINKER_CREATED)
966 return;
967
968 /* Skip sections that we are ignoring. */
969 if (! process_section_p (section))
970 return;
971
972 name = bfd_section_name (section);
973 len = (int) strlen (name);
974 if (len > *longest_so_far)
975 *longest_so_far = len;
976}
977
978static void
979dump_headers (bfd *abfd)
980{
981 /* The default width of 13 is just an arbitrary choice. */
982 int max_section_name_length = 13;
983 int bfd_vma_width;
984
985#ifndef BFD64
986 bfd_vma_width = 10;
987#else
988 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
989 if (bfd_get_arch_size (abfd) == 32)
990 bfd_vma_width = 10;
991 else
992 bfd_vma_width = 18;
993#endif
994
995 printf (_("Sections:\n"));
996
997 if (wide_output)
998 bfd_map_over_sections (abfd, find_longest_section_name,
999 &max_section_name_length);
1000
1001 printf (_("Idx %-*s Size %-*s%-*sFile off Algn"),
1002 max_section_name_length, "Name",
1003 bfd_vma_width, "VMA",
1004 bfd_vma_width, "LMA");
1005
1006 if (wide_output)
1007 printf (_(" Flags"));
1008 printf ("\n");
1009
1010 bfd_map_over_sections (abfd, dump_section_header,
1011 &max_section_name_length);
1012}
1013\f
1014static asymbol **
1015slurp_symtab (bfd *abfd)
1016{
1017 symcount = 0;
1018 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
1019 return NULL;
1020
1021 long storage = bfd_get_symtab_upper_bound (abfd);
1022 if (storage < 0)
1023 {
1024 non_fatal (_("failed to read symbol table from: %s"),
1025 bfd_get_filename (abfd));
1026 my_bfd_nonfatal (_("error message was"));
1027 }
1028
1029 if (storage <= 0)
1030 return NULL;
1031
1032 asymbol **sy = (asymbol **) xmalloc (storage);
1033 symcount = bfd_canonicalize_symtab (abfd, sy);
1034 if (symcount < 0)
1035 {
1036 my_bfd_nonfatal (bfd_get_filename (abfd));
1037 free (sy);
1038 sy = NULL;
1039 symcount = 0;
1040 }
1041 return sy;
1042}
1043
1044/* Read in the dynamic symbols. */
1045
1046static asymbol **
1047slurp_dynamic_symtab (bfd *abfd)
1048{
1049 dynsymcount = 0;
1050 long storage = bfd_get_dynamic_symtab_upper_bound (abfd);
1051 if (storage < 0)
1052 {
1053 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
1054 {
1055 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
1056 exit_status = 1;
1057 return NULL;
1058 }
1059
1060 my_bfd_nonfatal (bfd_get_filename (abfd));
1061 }
1062
1063 if (storage <= 0)
1064 return NULL;
1065
1066 asymbol **sy = (asymbol **) xmalloc (storage);
1067 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
1068 if (dynsymcount < 0)
1069 {
1070 my_bfd_nonfatal (bfd_get_filename (abfd));
1071 free (sy);
1072 sy = NULL;
1073 dynsymcount = 0;
1074 }
1075 return sy;
1076}
1077
1078/* Some symbol names are significant and should be kept in the
1079 table of sorted symbol names, even if they are marked as
1080 debugging/section symbols. */
1081
1082static bool
1083is_significant_symbol_name (const char * name)
1084{
1085 return startswith (name, ".plt") || startswith (name, ".got");
1086}
1087
1088/* Filter out (in place) symbols that are useless for disassembly.
1089 COUNT is the number of elements in SYMBOLS.
1090 Return the number of useful symbols. */
1091
1092static long
1093remove_useless_symbols (asymbol **symbols, long count)
1094{
1095 asymbol **in_ptr = symbols, **out_ptr = symbols;
1096
1097 while (--count >= 0)
1098 {
1099 asymbol *sym = *in_ptr++;
1100
1101 if (sym->name == NULL || sym->name[0] == '\0')
1102 continue;
1103 if ((sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
1104 && ! is_significant_symbol_name (sym->name))
1105 continue;
1106 if (bfd_is_und_section (sym->section)
1107 || bfd_is_com_section (sym->section))
1108 continue;
1109
1110 *out_ptr++ = sym;
1111 }
1112 return out_ptr - symbols;
1113}
1114
1115/* Return true iff SEC1 and SEC2 are the same section.
1116 This would just be a simple pointer comparison except that one of
1117 the sections might be from a separate debug info file. */
1118
1119static bool
1120is_same_section (const asection *sec1, const asection *sec2)
1121{
1122 if (sec1 == sec2)
1123 return true;
1124 if (sec1->owner == sec2->owner
1125 || sec1->owner == NULL
1126 || sec2->owner == NULL)
1127 return false;
1128 /* OK, so we have one section in a debug info file. (Or they both
1129 are, but the way this function is currently used sec1 will be in
1130 a normal object.) Compare names, vma and size. This ought to
1131 cover all the usual cases. */
1132 return (sec1->vma == sec2->vma
1133 && sec1->size == sec2->size
1134 && strcmp (sec1->name, sec2->name) == 0);
1135}
1136
1137static const asection *compare_section;
1138
1139/* Sort symbols into value order. */
1140
1141static int
1142compare_symbols (const void *ap, const void *bp)
1143{
1144 const asymbol *a = * (const asymbol **) ap;
1145 const asymbol *b = * (const asymbol **) bp;
1146 const char *an;
1147 const char *bn;
1148 size_t anl;
1149 size_t bnl;
1150 bool as, af, bs, bf;
1151 flagword aflags;
1152 flagword bflags;
1153
1154 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
1155 return 1;
1156 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
1157 return -1;
1158
1159 /* Prefer symbols from the section currently being disassembled.
1160 Don't sort symbols from other sections by section, since there
1161 isn't much reason to prefer one section over another otherwise. */
1162 as = is_same_section (compare_section, a->section);
1163 bs = is_same_section (compare_section, b->section);
1164 if (as && !bs)
1165 return -1;
1166 if (!as && bs)
1167 return 1;
1168
1169 an = bfd_asymbol_name (a);
1170 bn = bfd_asymbol_name (b);
1171 anl = strlen (an);
1172 bnl = strlen (bn);
1173
1174 /* The symbols gnu_compiled and gcc2_compiled convey no real
1175 information, so put them after other symbols with the same value. */
1176 af = (strstr (an, "gnu_compiled") != NULL
1177 || strstr (an, "gcc2_compiled") != NULL);
1178 bf = (strstr (bn, "gnu_compiled") != NULL
1179 || strstr (bn, "gcc2_compiled") != NULL);
1180
1181 if (af && ! bf)
1182 return 1;
1183 if (! af && bf)
1184 return -1;
1185
1186 /* We use a heuristic for the file name, to try to sort it after
1187 more useful symbols. It may not work on non Unix systems, but it
1188 doesn't really matter; the only difference is precisely which
1189 symbol names get printed. */
1190
1191#define file_symbol(s, sn, snl) \
1192 (((s)->flags & BSF_FILE) != 0 \
1193 || ((snl) > 2 \
1194 && (sn)[(snl) - 2] == '.' \
1195 && ((sn)[(snl) - 1] == 'o' \
1196 || (sn)[(snl) - 1] == 'a')))
1197
1198 af = file_symbol (a, an, anl);
1199 bf = file_symbol (b, bn, bnl);
1200
1201 if (af && ! bf)
1202 return 1;
1203 if (! af && bf)
1204 return -1;
1205
1206 /* Sort function and object symbols before global symbols before
1207 local symbols before section symbols before debugging symbols. */
1208
1209 aflags = a->flags;
1210 bflags = b->flags;
1211
1212 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
1213 {
1214 if ((aflags & BSF_DEBUGGING) != 0)
1215 return 1;
1216 else
1217 return -1;
1218 }
1219 if ((aflags & BSF_SECTION_SYM) != (bflags & BSF_SECTION_SYM))
1220 {
1221 if ((aflags & BSF_SECTION_SYM) != 0)
1222 return 1;
1223 else
1224 return -1;
1225 }
1226 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
1227 {
1228 if ((aflags & BSF_FUNCTION) != 0)
1229 return -1;
1230 else
1231 return 1;
1232 }
1233 if ((aflags & BSF_OBJECT) != (bflags & BSF_OBJECT))
1234 {
1235 if ((aflags & BSF_OBJECT) != 0)
1236 return -1;
1237 else
1238 return 1;
1239 }
1240 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
1241 {
1242 if ((aflags & BSF_LOCAL) != 0)
1243 return 1;
1244 else
1245 return -1;
1246 }
1247 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
1248 {
1249 if ((aflags & BSF_GLOBAL) != 0)
1250 return -1;
1251 else
1252 return 1;
1253 }
1254
1255 /* Sort larger size ELF symbols before smaller. See PR20337. */
1256 bfd_vma asz = 0;
1257 if ((a->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0
1258 && bfd_get_flavour (bfd_asymbol_bfd (a)) == bfd_target_elf_flavour)
1259 asz = ((elf_symbol_type *) a)->internal_elf_sym.st_size;
1260 bfd_vma bsz = 0;
1261 if ((b->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0
1262 && bfd_get_flavour (bfd_asymbol_bfd (b)) == bfd_target_elf_flavour)
1263 bsz = ((elf_symbol_type *) b)->internal_elf_sym.st_size;
1264 if (asz != bsz)
1265 return asz > bsz ? -1 : 1;
1266
1267 /* Symbols that start with '.' might be section names, so sort them
1268 after symbols that don't start with '.'. */
1269 if (an[0] == '.' && bn[0] != '.')
1270 return 1;
1271 if (an[0] != '.' && bn[0] == '.')
1272 return -1;
1273
1274 /* Finally, if we can't distinguish them in any other way, try to
1275 get consistent results by sorting the symbols by name. */
1276 return strcmp (an, bn);
1277}
1278
1279/* Sort relocs into address order. */
1280
1281static int
1282compare_relocs (const void *ap, const void *bp)
1283{
1284 const arelent *a = * (const arelent **) ap;
1285 const arelent *b = * (const arelent **) bp;
1286
1287 if (a->address > b->address)
1288 return 1;
1289 else if (a->address < b->address)
1290 return -1;
1291
1292 /* So that associated relocations tied to the same address show up
1293 in the correct order, we don't do any further sorting. */
1294 if (a > b)
1295 return 1;
1296 else if (a < b)
1297 return -1;
1298 else
1299 return 0;
1300}
1301
1302/* Print an address (VMA) to the output stream in INFO.
1303 If SKIP_ZEROES is TRUE, omit leading zeroes. */
1304
1305static void
1306objdump_print_value (bfd_vma vma, struct disassemble_info *inf,
1307 bool skip_zeroes)
1308{
1309 char buf[30];
1310 char *p;
1311 struct objdump_disasm_info *aux;
1312
1313 aux = (struct objdump_disasm_info *) inf->application_data;
1314 bfd_sprintf_vma (aux->abfd, buf, vma);
1315 if (! skip_zeroes)
1316 p = buf;
1317 else
1318 {
1319 for (p = buf; *p == '0'; ++p)
1320 ;
1321 if (*p == '\0')
1322 --p;
1323 }
1324 (*inf->fprintf_styled_func) (inf->stream, dis_style_address, "%s", p);
1325}
1326
1327/* Print the name of a symbol. */
1328
1329static void
1330objdump_print_symname (bfd *abfd, struct disassemble_info *inf,
1331 asymbol *sym)
1332{
1333 char *alloc;
1334 const char *name, *version_string = NULL;
1335 bool hidden = false;
1336
1337 alloc = NULL;
1338 name = bfd_asymbol_name (sym);
1339 if (do_demangle && name[0] != '\0')
1340 {
1341 /* Demangle the name. */
1342 alloc = bfd_demangle (abfd, name, demangle_flags);
1343 if (alloc != NULL)
1344 name = alloc;
1345 }
1346
1347 if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
1348 version_string = bfd_get_symbol_version_string (abfd, sym, true,
1349 &hidden);
1350
1351 if (bfd_is_und_section (bfd_asymbol_section (sym)))
1352 hidden = true;
1353
1354 name = sanitize_string (name);
1355
1356 if (inf != NULL)
1357 {
1358 (*inf->fprintf_styled_func) (inf->stream, dis_style_symbol, "%s", name);
1359 if (version_string && *version_string != '\0')
1360 (*inf->fprintf_styled_func) (inf->stream, dis_style_symbol,
1361 hidden ? "@%s" : "@@%s",
1362 version_string);
1363 }
1364 else
1365 {
1366 printf ("%s", name);
1367 if (version_string && *version_string != '\0')
1368 printf (hidden ? "@%s" : "@@%s", version_string);
1369 }
1370
1371 if (alloc != NULL)
1372 free (alloc);
1373}
1374
1375static inline bool
1376sym_ok (bool want_section,
1377 bfd *abfd ATTRIBUTE_UNUSED,
1378 long place,
1379 asection *sec,
1380 struct disassemble_info *inf)
1381{
1382 if (want_section && !is_same_section (sec, sorted_syms[place]->section))
1383 return false;
1384
1385 return inf->symbol_is_valid (sorted_syms[place], inf);
1386}
1387
1388/* Locate a symbol given a bfd and a section (from INFO->application_data),
1389 and a VMA. If INFO->application_data->require_sec is TRUE, then always
1390 require the symbol to be in the section. Returns NULL if there is no
1391 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
1392 of the symbol in sorted_syms. */
1393
1394static asymbol *
1395find_symbol_for_address (bfd_vma vma,
1396 struct disassemble_info *inf,
1397 long *place)
1398{
1399 /* @@ Would it speed things up to cache the last two symbols returned,
1400 and maybe their address ranges? For many processors, only one memory
1401 operand can be present at a time, so the 2-entry cache wouldn't be
1402 constantly churned by code doing heavy memory accesses. */
1403
1404 /* Indices in `sorted_syms'. */
1405 long min = 0;
1406 long max_count = sorted_symcount;
1407 long thisplace;
1408 struct objdump_disasm_info *aux;
1409 bfd *abfd;
1410 asection *sec;
1411 unsigned int opb;
1412 bool want_section;
1413 long rel_count;
1414
1415 if (sorted_symcount < 1)
1416 return NULL;
1417
1418 aux = (struct objdump_disasm_info *) inf->application_data;
1419 abfd = aux->abfd;
1420 sec = inf->section;
1421 opb = inf->octets_per_byte;
1422
1423 /* Perform a binary search looking for the closest symbol to the
1424 required value. We are searching the range (min, max_count]. */
1425 while (min + 1 < max_count)
1426 {
1427 asymbol *sym;
1428
1429 thisplace = (max_count + min) / 2;
1430 sym = sorted_syms[thisplace];
1431
1432 if (bfd_asymbol_value (sym) > vma)
1433 max_count = thisplace;
1434 else if (bfd_asymbol_value (sym) < vma)
1435 min = thisplace;
1436 else
1437 {
1438 min = thisplace;
1439 break;
1440 }
1441 }
1442
1443 /* The symbol we want is now in min, the low end of the range we
1444 were searching. If there are several symbols with the same
1445 value, we want the first one. */
1446 thisplace = min;
1447 while (thisplace > 0
1448 && (bfd_asymbol_value (sorted_syms[thisplace])
1449 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
1450 --thisplace;
1451
1452 /* Prefer a symbol in the current section if we have multple symbols
1453 with the same value, as can occur with overlays or zero size
1454 sections. */
1455 min = thisplace;
1456 while (min < max_count
1457 && (bfd_asymbol_value (sorted_syms[min])
1458 == bfd_asymbol_value (sorted_syms[thisplace])))
1459 {
1460 if (sym_ok (true, abfd, min, sec, inf))
1461 {
1462 thisplace = min;
1463
1464 if (place != NULL)
1465 *place = thisplace;
1466
1467 return sorted_syms[thisplace];
1468 }
1469 ++min;
1470 }
1471
1472 /* If the file is relocatable, and the symbol could be from this
1473 section, prefer a symbol from this section over symbols from
1474 others, even if the other symbol's value might be closer.
1475
1476 Note that this may be wrong for some symbol references if the
1477 sections have overlapping memory ranges, but in that case there's
1478 no way to tell what's desired without looking at the relocation
1479 table.
1480
1481 Also give the target a chance to reject symbols. */
1482 want_section = (aux->require_sec
1483 || ((abfd->flags & HAS_RELOC) != 0
1484 && vma >= bfd_section_vma (sec)
1485 && vma < (bfd_section_vma (sec)
1486 + bfd_section_size (sec) / opb)));
1487
1488 if (! sym_ok (want_section, abfd, thisplace, sec, inf))
1489 {
1490 long i;
1491 long newplace = sorted_symcount;
1492
1493 for (i = min - 1; i >= 0; i--)
1494 {
1495 if (sym_ok (want_section, abfd, i, sec, inf))
1496 {
1497 if (newplace == sorted_symcount)
1498 newplace = i;
1499
1500 if (bfd_asymbol_value (sorted_syms[i])
1501 != bfd_asymbol_value (sorted_syms[newplace]))
1502 break;
1503
1504 /* Remember this symbol and keep searching until we reach
1505 an earlier address. */
1506 newplace = i;
1507 }
1508 }
1509
1510 if (newplace != sorted_symcount)
1511 thisplace = newplace;
1512 else
1513 {
1514 /* We didn't find a good symbol with a smaller value.
1515 Look for one with a larger value. */
1516 for (i = thisplace + 1; i < sorted_symcount; i++)
1517 {
1518 if (sym_ok (want_section, abfd, i, sec, inf))
1519 {
1520 thisplace = i;
1521 break;
1522 }
1523 }
1524 }
1525
1526 if (! sym_ok (want_section, abfd, thisplace, sec, inf))
1527 /* There is no suitable symbol. */
1528 return NULL;
1529 }
1530
1531 /* If we have not found an exact match for the specified address
1532 and we have dynamic relocations available, then we can produce
1533 a better result by matching a relocation to the address and
1534 using the symbol associated with that relocation. */
1535 rel_count = inf->dynrelcount;
1536 if (!want_section
1537 && sorted_syms[thisplace]->value != vma
1538 && rel_count > 0
1539 && inf->dynrelbuf != NULL
1540 && inf->dynrelbuf[0]->address <= vma
1541 && inf->dynrelbuf[rel_count - 1]->address >= vma
1542 /* If we have matched a synthetic symbol, then stick with that. */
1543 && (sorted_syms[thisplace]->flags & BSF_SYNTHETIC) == 0)
1544 {
1545 arelent ** rel_low;
1546 arelent ** rel_high;
1547
1548 rel_low = inf->dynrelbuf;
1549 rel_high = rel_low + rel_count - 1;
1550 while (rel_low <= rel_high)
1551 {
1552 arelent **rel_mid = &rel_low[(rel_high - rel_low) / 2];
1553 arelent * rel = *rel_mid;
1554
1555 if (rel->address == vma)
1556 {
1557 /* Absolute relocations do not provide a more helpful
1558 symbolic address. Find a non-absolute relocation
1559 with the same address. */
1560 arelent **rel_vma = rel_mid;
1561 for (rel_mid--;
1562 rel_mid >= rel_low && rel_mid[0]->address == vma;
1563 rel_mid--)
1564 rel_vma = rel_mid;
1565
1566 for (; rel_vma <= rel_high && rel_vma[0]->address == vma;
1567 rel_vma++)
1568 {
1569 rel = *rel_vma;
1570 if (rel->sym_ptr_ptr != NULL
1571 && ! bfd_is_abs_section ((* rel->sym_ptr_ptr)->section))
1572 {
1573 if (place != NULL)
1574 * place = thisplace;
1575 return * rel->sym_ptr_ptr;
1576 }
1577 }
1578 break;
1579 }
1580
1581 if (vma < rel->address)
1582 rel_high = rel_mid;
1583 else if (vma >= rel_mid[1]->address)
1584 rel_low = rel_mid + 1;
1585 else
1586 break;
1587 }
1588 }
1589
1590 if (place != NULL)
1591 *place = thisplace;
1592
1593 return sorted_syms[thisplace];
1594}
1595
1596/* Print an address and the offset to the nearest symbol. */
1597
1598static void
1599objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
1600 bfd_vma vma, struct disassemble_info *inf,
1601 bool skip_zeroes)
1602{
1603 if (!no_addresses)
1604 {
1605 objdump_print_value (vma, inf, skip_zeroes);
1606 (*inf->fprintf_styled_func) (inf->stream, dis_style_text, " ");
1607 }
1608
1609 if (sym == NULL)
1610 {
1611 bfd_vma secaddr;
1612
1613 (*inf->fprintf_styled_func) (inf->stream, dis_style_text,"<");
1614 (*inf->fprintf_styled_func) (inf->stream, dis_style_symbol, "%s",
1615 sanitize_string (bfd_section_name (sec)));
1616 secaddr = bfd_section_vma (sec);
1617 if (vma < secaddr)
1618 {
1619 (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate,
1620 "-0x");
1621 objdump_print_value (secaddr - vma, inf, true);
1622 }
1623 else if (vma > secaddr)
1624 {
1625 (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate, "+0x");
1626 objdump_print_value (vma - secaddr, inf, true);
1627 }
1628 (*inf->fprintf_styled_func) (inf->stream, dis_style_text, ">");
1629 }
1630 else
1631 {
1632 (*inf->fprintf_styled_func) (inf->stream, dis_style_text, "<");
1633
1634 objdump_print_symname (abfd, inf, sym);
1635
1636 if (bfd_asymbol_value (sym) == vma)
1637 ;
1638 /* Undefined symbols in an executables and dynamic objects do not have
1639 a value associated with them, so it does not make sense to display
1640 an offset relative to them. Normally we would not be provided with
1641 this kind of symbol, but the target backend might choose to do so,
1642 and the code in find_symbol_for_address might return an as yet
1643 unresolved symbol associated with a dynamic reloc. */
1644 else if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
1645 && bfd_is_und_section (sym->section))
1646 ;
1647 else if (bfd_asymbol_value (sym) > vma)
1648 {
1649 (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate,"-0x");
1650 objdump_print_value (bfd_asymbol_value (sym) - vma, inf, true);
1651 }
1652 else if (vma > bfd_asymbol_value (sym))
1653 {
1654 (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate, "+0x");
1655 objdump_print_value (vma - bfd_asymbol_value (sym), inf, true);
1656 }
1657
1658 (*inf->fprintf_styled_func) (inf->stream, dis_style_text, ">");
1659 }
1660
1661 if (display_file_offsets)
1662 inf->fprintf_styled_func (inf->stream, dis_style_text,
1663 _(" (File Offset: 0x%lx)"),
1664 (long int)(sec->filepos + (vma - sec->vma)));
1665}
1666
1667/* Displays all symbols in the sorted symbol table starting at PLACE
1668 which match the address VMA. Assumes that show_all_symbols == true. */
1669
1670static void
1671display_extra_syms (long place,
1672 bfd_vma vma,
1673 struct disassemble_info *inf)
1674{
1675 struct objdump_disasm_info *aux = (struct objdump_disasm_info *) inf->application_data;
1676
1677 if (place == 0)
1678 return;
1679
1680 bool first = true;
1681
1682 for (; place < sorted_symcount; place++)
1683 {
1684 asymbol *sym = sorted_syms[place];
1685
1686 if (bfd_asymbol_value (sym) != vma)
1687 break;
1688
1689 if (! inf->symbol_is_valid (sym, inf))
1690 continue;
1691
1692 if (first && ! do_wide)
1693 inf->fprintf_styled_func (inf->stream, dis_style_immediate, ",\n\t<");
1694 else
1695 inf->fprintf_styled_func (inf->stream, dis_style_immediate, ", <");
1696
1697 objdump_print_symname (aux->abfd, inf, sym);
1698 inf->fprintf_styled_func (inf->stream, dis_style_immediate, ">");
1699 first = false;
1700 }
1701}
1702
1703/* Print an address (VMA), symbolically if possible.
1704 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
1705
1706static void
1707objdump_print_addr (bfd_vma vma,
1708 struct disassemble_info *inf,
1709 bool skip_zeroes)
1710{
1711 struct objdump_disasm_info *aux;
1712 asymbol *sym = NULL;
1713 bool skip_find = false;
1714 long place = 0;
1715
1716 aux = (struct objdump_disasm_info *) inf->application_data;
1717
1718 if (sorted_symcount < 1)
1719 {
1720 if (!no_addresses)
1721 {
1722 (*inf->fprintf_styled_func) (inf->stream, dis_style_address, "0x");
1723 objdump_print_value (vma, inf, skip_zeroes);
1724 }
1725
1726 if (display_file_offsets)
1727 inf->fprintf_styled_func (inf->stream, dis_style_text,
1728 _(" (File Offset: 0x%lx)"),
1729 (long int) (inf->section->filepos
1730 + (vma - inf->section->vma)));
1731 return;
1732 }
1733
1734 if (aux->reloc != NULL
1735 && aux->reloc->sym_ptr_ptr != NULL
1736 && * aux->reloc->sym_ptr_ptr != NULL)
1737 {
1738 sym = * aux->reloc->sym_ptr_ptr;
1739
1740 /* Adjust the vma to the reloc. */
1741 vma += bfd_asymbol_value (sym);
1742
1743 if (bfd_is_und_section (bfd_asymbol_section (sym)))
1744 skip_find = true;
1745 }
1746
1747 if (!skip_find)
1748 sym = find_symbol_for_address (vma, inf, &place);
1749
1750 objdump_print_addr_with_sym (aux->abfd, inf->section, sym, vma, inf,
1751 skip_zeroes);
1752
1753 /* If requested, display any extra symbols at this address. */
1754 if (sym == NULL || ! show_all_symbols)
1755 return;
1756
1757 if (place)
1758 display_extra_syms (place + 1, vma, inf);
1759
1760 /* If we found an absolute symbol in the reloc (ie: "*ABS*+0x....")
1761 and there is a valid symbol at the address contained in the absolute symbol
1762 then display any extra symbols that match this address. This helps
1763 particularly with relocations for PLT entries. */
1764 if (startswith (sym->name, BFD_ABS_SECTION_NAME "+"))
1765 {
1766 bfd_vma addr = strtoul (sym->name + strlen (BFD_ABS_SECTION_NAME "+"), NULL, 0);
1767
1768 if (addr && addr != vma)
1769 {
1770 sym = find_symbol_for_address (addr, inf, &place);
1771
1772 if (sym)
1773 display_extra_syms (place, addr, inf);
1774 }
1775 }
1776}
1777
1778/* Print VMA to INFO. This function is passed to the disassembler
1779 routine. */
1780
1781static void
1782objdump_print_address (bfd_vma vma, struct disassemble_info *inf)
1783{
1784 objdump_print_addr (vma, inf, ! prefix_addresses);
1785}
1786
1787/* Determine if the given address has a symbol associated with it. */
1788
1789static asymbol *
1790objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * inf)
1791{
1792 asymbol * sym;
1793
1794 sym = find_symbol_for_address (vma, inf, NULL);
1795 if (sym != NULL && bfd_asymbol_value (sym) == vma)
1796 return sym;
1797
1798 return NULL;
1799}
1800
1801/* Hold the last function name and the last line number we displayed
1802 in a disassembly. */
1803
1804static char *prev_functionname;
1805static unsigned int prev_line;
1806static unsigned int prev_discriminator;
1807
1808/* We keep a list of all files that we have seen when doing a
1809 disassembly with source, so that we know how much of the file to
1810 display. This can be important for inlined functions. */
1811
1812struct print_file_list
1813{
1814 struct print_file_list *next;
1815 const char *filename;
1816 const char *modname;
1817 const char *map;
1818 size_t mapsize;
1819 const char **linemap;
1820 unsigned maxline;
1821 unsigned last_line;
1822 unsigned max_printed;
1823 int first;
1824};
1825
1826static struct print_file_list *print_files;
1827
1828/* The number of preceding context lines to show when we start
1829 displaying a file for the first time. */
1830
1831#define SHOW_PRECEDING_CONTEXT_LINES (5)
1832
1833#if HAVE_LIBDEBUGINFOD
1834/* Return a hex string represention of the build-id. */
1835
1836unsigned char *
1837get_build_id (void * data)
1838{
1839 unsigned i;
1840 char * build_id_str;
1841 bfd * abfd = (bfd *) data;
1842 const struct bfd_build_id * build_id;
1843
1844 build_id = abfd->build_id;
1845 if (build_id == NULL)
1846 return NULL;
1847
1848 build_id_str = malloc (build_id->size * 2 + 1);
1849 if (build_id_str == NULL)
1850 return NULL;
1851
1852 for (i = 0; i < build_id->size; i++)
1853 sprintf (build_id_str + (i * 2), "%02x", build_id->data[i]);
1854 build_id_str[build_id->size * 2] = '\0';
1855
1856 return (unsigned char *) build_id_str;
1857}
1858
1859/* Search for a separate debug file matching ABFD's build-id. */
1860
1861static bfd *
1862find_separate_debug (const bfd * abfd)
1863{
1864 const struct bfd_build_id * build_id = abfd->build_id;
1865 separate_info * i = first_separate_info;
1866
1867 if (build_id == NULL || i == NULL)
1868 return NULL;
1869
1870 while (i != NULL)
1871 {
1872 const bfd * i_bfd = (bfd *) i->handle;
1873
1874 if (abfd != NULL && i_bfd->build_id != NULL)
1875 {
1876 const unsigned char * data = i_bfd->build_id->data;
1877 size_t size = i_bfd->build_id->size;
1878
1879 if (size == build_id->size
1880 && memcmp (data, build_id->data, size) == 0)
1881 return (bfd *) i->handle;
1882 }
1883
1884 i = i->next;
1885 }
1886
1887 return NULL;
1888}
1889
1890/* Search for a separate debug file matching ABFD's .gnu_debugaltlink
1891 build-id. */
1892
1893static bfd *
1894find_alt_debug (const bfd * abfd)
1895{
1896 size_t namelen;
1897 size_t id_len;
1898 const char * name;
1899 struct dwarf_section * section;
1900 const struct bfd_build_id * build_id = abfd->build_id;
1901 separate_info * i = first_separate_info;
1902
1903 if (i == NULL
1904 || build_id == NULL
1905 || !load_debug_section (gnu_debugaltlink, (void *) abfd))
1906 return NULL;
1907
1908 section = &debug_displays[gnu_debugaltlink].section;
1909 if (section == NULL)
1910 return NULL;
1911
1912 name = (const char *) section->start;
1913 namelen = strnlen (name, section->size) + 1;
1914 if (namelen == 1)
1915 return NULL;
1916 if (namelen >= section->size)
1917 return NULL;
1918
1919 id_len = section->size - namelen;
1920 if (id_len < 0x14)
1921 return NULL;
1922
1923 /* Compare the .gnu_debugaltlink build-id with the build-ids of the
1924 known separate_info files. */
1925 while (i != NULL)
1926 {
1927 const bfd * i_bfd = (bfd *) i->handle;
1928
1929 if (i_bfd != NULL && i_bfd->build_id != NULL)
1930 {
1931 const unsigned char * data = i_bfd->build_id->data;
1932 size_t size = i_bfd->build_id->size;
1933
1934 if (id_len == size
1935 && memcmp (section->start + namelen, data, size) == 0)
1936 return (bfd *) i->handle;
1937 }
1938
1939 i = i->next;
1940 }
1941
1942 return NULL;
1943}
1944
1945#endif /* HAVE_LIBDEBUGINFOD */
1946
1947/* Reads the contents of file FN into memory. Returns a pointer to the buffer.
1948 Also returns the size of the buffer in SIZE_RETURN and a filled out
1949 stat structure in FST_RETURN. Returns NULL upon failure. */
1950
1951static const char *
1952slurp_file (const char * fn,
1953 size_t * size_return,
1954 struct stat * fst_return,
1955 bfd * abfd ATTRIBUTE_UNUSED)
1956{
1957#ifdef HAVE_MMAP
1958 int ps;
1959 size_t msize;
1960#endif
1961 const char *map;
1962 int fd;
1963
1964 /* Paranoia. */
1965 if (fn == NULL || * fn == 0 || size_return == NULL || fst_return == NULL)
1966 return NULL;
1967
1968 fd = open (fn, O_RDONLY | O_BINARY);
1969
1970#if HAVE_LIBDEBUGINFOD
1971 if (fd < 0 && use_debuginfod && fn[0] == '/' && abfd != NULL)
1972 {
1973 unsigned char *build_id = get_build_id (abfd);
1974
1975 if (build_id)
1976 {
1977 debuginfod_client *client = debuginfod_begin ();
1978
1979 if (client)
1980 {
1981 fd = debuginfod_find_source (client, build_id, 0, fn, NULL);
1982 debuginfod_end (client);
1983 }
1984 free (build_id);
1985 }
1986 }
1987#endif
1988
1989 if (fd < 0)
1990 return NULL;
1991
1992 if (fstat (fd, fst_return) < 0)
1993 {
1994 close (fd);
1995 return NULL;
1996 }
1997
1998 *size_return = fst_return->st_size;
1999
2000#ifdef HAVE_MMAP
2001 ps = getpagesize ();
2002 msize = (*size_return + ps - 1) & ~(ps - 1);
2003 map = mmap (NULL, msize, PROT_READ, MAP_SHARED, fd, 0);
2004 if (map != (char *) -1L)
2005 {
2006 close (fd);
2007 return map;
2008 }
2009#endif
2010
2011 map = (const char *) malloc (*size_return);
2012 if (!map || (size_t) read (fd, (char *) map, *size_return) != *size_return)
2013 {
2014 free ((void *) map);
2015 map = NULL;
2016 }
2017 close (fd);
2018 return map;
2019}
2020
2021#define line_map_decrease 5
2022
2023/* Precompute array of lines for a mapped file. */
2024
2025static const char **
2026index_file (const char *map, size_t size, unsigned int *maxline)
2027{
2028 const char *p, *lstart, *end;
2029 int chars_per_line = 45; /* First iteration will use 40. */
2030 unsigned int lineno;
2031 const char **linemap = NULL;
2032 unsigned long line_map_size = 0;
2033
2034 lineno = 0;
2035 lstart = map;
2036 end = map + size;
2037
2038 for (p = map; p < end; p++)
2039 {
2040 if (*p == '\n')
2041 {
2042 if (p + 1 < end && p[1] == '\r')
2043 p++;
2044 }
2045 else if (*p == '\r')
2046 {
2047 if (p + 1 < end && p[1] == '\n')
2048 p++;
2049 }
2050 else
2051 continue;
2052
2053 /* End of line found. */
2054
2055 if (linemap == NULL || line_map_size < lineno + 1)
2056 {
2057 unsigned long newsize;
2058
2059 chars_per_line -= line_map_decrease;
2060 if (chars_per_line <= 1)
2061 chars_per_line = 1;
2062 line_map_size = size / chars_per_line + 1;
2063 if (line_map_size < lineno + 1)
2064 line_map_size = lineno + 1;
2065 newsize = line_map_size * sizeof (char *);
2066 linemap = (const char **) xrealloc (linemap, newsize);
2067 }
2068
2069 linemap[lineno++] = lstart;
2070 lstart = p + 1;
2071 }
2072
2073 *maxline = lineno;
2074 return linemap;
2075}
2076
2077/* Tries to open MODNAME, and if successful adds a node to print_files
2078 linked list and returns that node. Also fills in the stat structure
2079 pointed to by FST_RETURN. Returns NULL on failure. */
2080
2081static struct print_file_list *
2082try_print_file_open (const char * origname,
2083 const char * modname,
2084 struct stat * fst_return,
2085 bfd * abfd)
2086{
2087 struct print_file_list *p;
2088
2089 p = (struct print_file_list *) xmalloc (sizeof (struct print_file_list));
2090
2091 p->map = slurp_file (modname, &p->mapsize, fst_return, abfd);
2092 if (p->map == NULL)
2093 {
2094 free (p);
2095 return NULL;
2096 }
2097
2098 p->linemap = index_file (p->map, p->mapsize, &p->maxline);
2099 p->last_line = 0;
2100 p->max_printed = 0;
2101 p->filename = origname;
2102 p->modname = modname;
2103 p->next = print_files;
2104 p->first = 1;
2105 print_files = p;
2106 return p;
2107}
2108
2109/* If the source file, as described in the symtab, is not found
2110 try to locate it in one of the paths specified with -I
2111 If found, add location to print_files linked list. */
2112
2113static struct print_file_list *
2114update_source_path (const char *filename, bfd *abfd)
2115{
2116 struct print_file_list *p;
2117 const char *fname;
2118 struct stat fst;
2119 int i;
2120
2121 p = try_print_file_open (filename, filename, &fst, abfd);
2122 if (p == NULL)
2123 {
2124 if (include_path_count == 0)
2125 return NULL;
2126
2127 /* Get the name of the file. */
2128 fname = lbasename (filename);
2129
2130 /* If file exists under a new path, we need to add it to the list
2131 so that show_line knows about it. */
2132 for (i = 0; i < include_path_count; i++)
2133 {
2134 char *modname = concat (include_paths[i], "/", fname,
2135 (const char *) 0);
2136
2137 p = try_print_file_open (filename, modname, &fst, abfd);
2138 if (p)
2139 break;
2140
2141 free (modname);
2142 }
2143 }
2144
2145 if (p != NULL)
2146 {
2147 long mtime = bfd_get_mtime (abfd);
2148
2149 if (fst.st_mtime > mtime)
2150 warn (_("source file %s is more recent than object file\n"),
2151 filename);
2152 }
2153
2154 return p;
2155}
2156
2157/* Print a source file line. */
2158
2159static void
2160print_line (struct print_file_list *p, unsigned int linenum)
2161{
2162 const char *l;
2163 size_t len;
2164
2165 if (linenum >= p->maxline)
2166 return;
2167 l = p->linemap [linenum];
2168 if (source_comment != NULL && strlen (l) > 0)
2169 printf ("%s", source_comment);
2170 len = strcspn (l, "\n\r");
2171 /* Test fwrite return value to quiet glibc warning. */
2172 if (len == 0 || fwrite (l, len, 1, stdout) == 1)
2173 putchar ('\n');
2174}
2175
2176/* Print a range of source code lines. */
2177
2178static void
2179dump_lines (struct print_file_list *p, unsigned int start, unsigned int end)
2180{
2181 if (p->map == NULL)
2182 return;
2183 if (start != 0)
2184 --start;
2185 while (start < end)
2186 {
2187 print_line (p, start);
2188 start++;
2189 }
2190}
2191
2192/* Show the line number, or the source line, in a disassembly
2193 listing. */
2194
2195static void
2196show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
2197{
2198 const char *filename;
2199 const char *functionname;
2200 unsigned int linenumber;
2201 unsigned int discriminator;
2202 bool reloc;
2203 char *path = NULL;
2204
2205 if (! with_line_numbers && ! with_source_code)
2206 return;
2207
2208#ifdef HAVE_LIBDEBUGINFOD
2209 {
2210 bfd *debug_bfd;
2211 const char *alt_filename = NULL;
2212
2213 if (use_debuginfod)
2214 {
2215 bfd *alt_bfd;
2216
2217 /* PR 29075: Check for separate debuginfo and .gnu_debugaltlink files.
2218 They need to be passed to bfd_find_nearest_line_with_alt in case they
2219 were downloaded from debuginfod. Otherwise libbfd will attempt to
2220 search for them and fail to locate them. */
2221 debug_bfd = find_separate_debug (abfd);
2222 if (debug_bfd == NULL)
2223 debug_bfd = abfd;
2224
2225 alt_bfd = find_alt_debug (debug_bfd);
2226 if (alt_bfd != NULL)
2227 alt_filename = bfd_get_filename (alt_bfd);
2228 }
2229 else
2230 debug_bfd = abfd;
2231
2232 bfd_set_error (bfd_error_no_error);
2233 if (! bfd_find_nearest_line_with_alt (debug_bfd, alt_filename,
2234 section, syms,
2235 addr_offset, &filename,
2236 &functionname, &linenumber,
2237 &discriminator))
2238 {
2239 if (bfd_get_error () == bfd_error_no_error)
2240 return;
2241 if (! bfd_find_nearest_line_discriminator (abfd, section, syms,
2242 addr_offset, &filename,
2243 &functionname, &linenumber,
2244 &discriminator))
2245 return;
2246 }
2247 }
2248#else
2249 if (! bfd_find_nearest_line_discriminator (abfd, section, syms, addr_offset,
2250 &filename, &functionname,
2251 &linenumber, &discriminator))
2252 return;
2253#endif
2254
2255 if (filename != NULL && *filename == '\0')
2256 filename = NULL;
2257 if (functionname != NULL && *functionname == '\0')
2258 functionname = NULL;
2259
2260 if (filename
2261 && IS_ABSOLUTE_PATH (filename)
2262 && prefix)
2263 {
2264 char *path_up;
2265 const char *fname = filename;
2266
2267 path = xmalloc (prefix_length + 1 + strlen (filename));
2268
2269 if (prefix_length)
2270 memcpy (path, prefix, prefix_length);
2271 path_up = path + prefix_length;
2272
2273 /* Build relocated filename, stripping off leading directories
2274 from the initial filename if requested. */
2275 if (prefix_strip > 0)
2276 {
2277 int level = 0;
2278 const char *s;
2279
2280 /* Skip selected directory levels. */
2281 for (s = fname + 1; *s != '\0' && level < prefix_strip; s++)
2282 if (IS_DIR_SEPARATOR (*s))
2283 {
2284 fname = s;
2285 level++;
2286 }
2287 }
2288
2289 /* Update complete filename. */
2290 strcpy (path_up, fname);
2291
2292 filename = path;
2293 reloc = true;
2294 }
2295 else
2296 reloc = false;
2297
2298 if (with_line_numbers)
2299 {
2300 if (functionname != NULL
2301 && (prev_functionname == NULL
2302 || strcmp (functionname, prev_functionname) != 0))
2303 {
2304 char *demangle_alloc = NULL;
2305 if (do_demangle && functionname[0] != '\0')
2306 {
2307 /* Demangle the name. */
2308 demangle_alloc = bfd_demangle (abfd, functionname,
2309 demangle_flags);
2310 }
2311
2312 /* Demangling adds trailing parens, so don't print those. */
2313 if (demangle_alloc != NULL)
2314 printf ("%s:\n", sanitize_string (demangle_alloc));
2315 else
2316 printf ("%s():\n", sanitize_string (functionname));
2317
2318 prev_line = -1;
2319 free (demangle_alloc);
2320 }
2321 if (linenumber > 0
2322 && (linenumber != prev_line
2323 || discriminator != prev_discriminator))
2324 {
2325 if (discriminator > 0)
2326 printf ("%s:%u (discriminator %u)\n",
2327 filename == NULL ? "???" : sanitize_string (filename),
2328 linenumber, discriminator);
2329 else
2330 printf ("%s:%u\n", filename == NULL
2331 ? "???" : sanitize_string (filename),
2332 linenumber);
2333 }
2334 if (unwind_inlines)
2335 {
2336 const char *filename2;
2337 const char *functionname2;
2338 unsigned line2;
2339
2340 while (bfd_find_inliner_info (abfd, &filename2, &functionname2,
2341 &line2))
2342 {
2343 printf ("inlined by %s:%u",
2344 sanitize_string (filename2), line2);
2345 printf (" (%s)\n", sanitize_string (functionname2));
2346 }
2347 }
2348 }
2349
2350 if (with_source_code
2351 && filename != NULL
2352 && linenumber > 0)
2353 {
2354 struct print_file_list **pp, *p;
2355 unsigned l;
2356
2357 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
2358 if (filename_cmp ((*pp)->filename, filename) == 0)
2359 break;
2360 p = *pp;
2361
2362 if (p == NULL)
2363 {
2364 if (reloc)
2365 filename = xstrdup (filename);
2366 p = update_source_path (filename, abfd);
2367 }
2368
2369 if (p != NULL && linenumber != p->last_line)
2370 {
2371 if (file_start_context && p->first)
2372 l = 1;
2373 else
2374 {
2375 l = linenumber - SHOW_PRECEDING_CONTEXT_LINES;
2376 if (l >= linenumber)
2377 l = 1;
2378 if (p->max_printed >= l)
2379 {
2380 if (p->max_printed < linenumber)
2381 l = p->max_printed + 1;
2382 else
2383 l = linenumber;
2384 }
2385 }
2386 dump_lines (p, l, linenumber);
2387 if (p->max_printed < linenumber)
2388 p->max_printed = linenumber;
2389 p->last_line = linenumber;
2390 p->first = 0;
2391 }
2392 }
2393
2394 if (functionname != NULL
2395 && (prev_functionname == NULL
2396 || strcmp (functionname, prev_functionname) != 0))
2397 {
2398 if (prev_functionname != NULL)
2399 free (prev_functionname);
2400 prev_functionname = (char *) xmalloc (strlen (functionname) + 1);
2401 strcpy (prev_functionname, functionname);
2402 }
2403
2404 if (linenumber > 0 && linenumber != prev_line)
2405 prev_line = linenumber;
2406
2407 if (discriminator != prev_discriminator)
2408 prev_discriminator = discriminator;
2409
2410 if (path)
2411 free (path);
2412}
2413
2414/* Pseudo FILE object for strings. */
2415typedef struct
2416{
2417 char *buffer;
2418 size_t pos;
2419 size_t alloc;
2420} SFILE;
2421
2422/* sprintf to a "stream". */
2423
2424static int ATTRIBUTE_PRINTF_2
2425objdump_sprintf (SFILE *f, const char *format, ...)
2426{
2427 size_t n;
2428 va_list args;
2429
2430 while (1)
2431 {
2432 size_t space = f->alloc - f->pos;
2433
2434 va_start (args, format);
2435 n = vsnprintf (f->buffer + f->pos, space, format, args);
2436 va_end (args);
2437
2438 if (space > n)
2439 break;
2440
2441 f->alloc = (f->alloc + n) * 2;
2442 f->buffer = (char *) xrealloc (f->buffer, f->alloc);
2443 }
2444 f->pos += n;
2445
2446 return n;
2447}
2448
2449/* Return an integer greater than, or equal to zero, representing the color
2450 for STYLE, or -1 if no color should be used. */
2451
2452static int
2453objdump_color_for_disassembler_style (enum disassembler_style style)
2454{
2455 int color = -1;
2456
2457 if (style == dis_style_comment_start)
2458 disassembler_in_comment = true;
2459
2460 if (disassembler_color == on)
2461 {
2462 if (disassembler_in_comment)
2463 return color;
2464
2465 switch (style)
2466 {
2467 case dis_style_symbol:
2468 color = 32;
2469 break;
2470 case dis_style_assembler_directive:
2471 case dis_style_sub_mnemonic:
2472 case dis_style_mnemonic:
2473 color = 33;
2474 break;
2475 case dis_style_register:
2476 color = 34;
2477 break;
2478 case dis_style_address:
2479 case dis_style_address_offset:
2480 case dis_style_immediate:
2481 color = 35;
2482 break;
2483 default:
2484 case dis_style_text:
2485 color = -1;
2486 break;
2487 }
2488 }
2489 else if (disassembler_color == extended)
2490 {
2491 if (disassembler_in_comment)
2492 return 250;
2493
2494 switch (style)
2495 {
2496 case dis_style_symbol:
2497 color = 40;
2498 break;
2499 case dis_style_assembler_directive:
2500 case dis_style_sub_mnemonic:
2501 case dis_style_mnemonic:
2502 color = 142;
2503 break;
2504 case dis_style_register:
2505 color = 27;
2506 break;
2507 case dis_style_address:
2508 case dis_style_address_offset:
2509 case dis_style_immediate:
2510 color = 134;
2511 break;
2512 default:
2513 case dis_style_text:
2514 color = -1;
2515 break;
2516 }
2517 }
2518 else if (disassembler_color != off)
2519 bfd_fatal (_("disassembly color not correctly selected"));
2520
2521 return color;
2522}
2523
2524/* Like objdump_sprintf, but add in escape sequences to highlight the
2525 content according to STYLE. */
2526
2527static int ATTRIBUTE_PRINTF_3
2528objdump_styled_sprintf (SFILE *f, enum disassembler_style style,
2529 const char *format, ...)
2530{
2531 size_t n;
2532 va_list args;
2533 int color = objdump_color_for_disassembler_style (style);
2534
2535 if (color >= 0)
2536 {
2537 while (1)
2538 {
2539 size_t space = f->alloc - f->pos;
2540
2541 if (disassembler_color == on)
2542 n = snprintf (f->buffer + f->pos, space, "\033[%dm", color);
2543 else
2544 n = snprintf (f->buffer + f->pos, space, "\033[38;5;%dm", color);
2545 if (space > n)
2546 break;
2547
2548 f->alloc = (f->alloc + n) * 2;
2549 f->buffer = (char *) xrealloc (f->buffer, f->alloc);
2550 }
2551 f->pos += n;
2552 }
2553
2554 while (1)
2555 {
2556 size_t space = f->alloc - f->pos;
2557
2558 va_start (args, format);
2559 n = vsnprintf (f->buffer + f->pos, space, format, args);
2560 va_end (args);
2561
2562 if (space > n)
2563 break;
2564
2565 f->alloc = (f->alloc + n) * 2;
2566 f->buffer = (char *) xrealloc (f->buffer, f->alloc);
2567 }
2568 f->pos += n;
2569
2570 if (color >= 0)
2571 {
2572 while (1)
2573 {
2574 size_t space = f->alloc - f->pos;
2575
2576 n = snprintf (f->buffer + f->pos, space, "\033[0m");
2577
2578 if (space > n)
2579 break;
2580
2581 f->alloc = (f->alloc + n) * 2;
2582 f->buffer = (char *) xrealloc (f->buffer, f->alloc);
2583 }
2584 f->pos += n;
2585 }
2586
2587 return n;
2588}
2589
2590/* We discard the styling information here. This function is only used
2591 when objdump is printing auxiliary information, the symbol headers, and
2592 disassembly address, or the bytes of the disassembled instruction. We
2593 don't (currently) apply styling to any of this stuff, so, for now, just
2594 print the content with no additional style added. */
2595
2596static int ATTRIBUTE_PRINTF_3
2597fprintf_styled (FILE *f, enum disassembler_style style ATTRIBUTE_UNUSED,
2598 const char *fmt, ...)
2599{
2600 int res;
2601 va_list ap;
2602
2603 va_start (ap, fmt);
2604 res = vfprintf (f, fmt, ap);
2605 va_end (ap);
2606
2607 return res;
2608}
2609
2610/* Code for generating (colored) diagrams of control flow start and end
2611 points. */
2612
2613/* Structure used to store the properties of a jump. */
2614
2615struct jump_info
2616{
2617 /* The next jump, or NULL if this is the last object. */
2618 struct jump_info *next;
2619 /* The previous jump, or NULL if this is the first object. */
2620 struct jump_info *prev;
2621 /* The start addresses of the jump. */
2622 struct
2623 {
2624 /* The list of start addresses. */
2625 bfd_vma *addresses;
2626 /* The number of elements. */
2627 size_t count;
2628 /* The maximum number of elements that fit into the array. */
2629 size_t max_count;
2630 } start;
2631 /* The end address of the jump. */
2632 bfd_vma end;
2633 /* The drawing level of the jump. */
2634 int level;
2635};
2636
2637/* Construct a jump object for a jump from start
2638 to end with the corresponding level. */
2639
2640static struct jump_info *
2641jump_info_new (bfd_vma start, bfd_vma end, int level)
2642{
2643 struct jump_info *result = xmalloc (sizeof (struct jump_info));
2644
2645 result->next = NULL;
2646 result->prev = NULL;
2647 result->start.addresses = xmalloc (sizeof (bfd_vma *) * 2);
2648 result->start.addresses[0] = start;
2649 result->start.count = 1;
2650 result->start.max_count = 2;
2651 result->end = end;
2652 result->level = level;
2653
2654 return result;
2655}
2656
2657/* Free a jump object and return the next object
2658 or NULL if this was the last one. */
2659
2660static struct jump_info *
2661jump_info_free (struct jump_info *ji)
2662{
2663 struct jump_info *result = NULL;
2664
2665 if (ji)
2666 {
2667 result = ji->next;
2668 if (ji->start.addresses)
2669 free (ji->start.addresses);
2670 free (ji);
2671 }
2672
2673 return result;
2674}
2675
2676/* Get the smallest value of all start and end addresses. */
2677
2678static bfd_vma
2679jump_info_min_address (const struct jump_info *ji)
2680{
2681 bfd_vma min_address = ji->end;
2682 size_t i;
2683
2684 for (i = ji->start.count; i-- > 0;)
2685 if (ji->start.addresses[i] < min_address)
2686 min_address = ji->start.addresses[i];
2687 return min_address;
2688}
2689
2690/* Get the largest value of all start and end addresses. */
2691
2692static bfd_vma
2693jump_info_max_address (const struct jump_info *ji)
2694{
2695 bfd_vma max_address = ji->end;
2696 size_t i;
2697
2698 for (i = ji->start.count; i-- > 0;)
2699 if (ji->start.addresses[i] > max_address)
2700 max_address = ji->start.addresses[i];
2701 return max_address;
2702}
2703
2704/* Get the target address of a jump. */
2705
2706static bfd_vma
2707jump_info_end_address (const struct jump_info *ji)
2708{
2709 return ji->end;
2710}
2711
2712/* Test if an address is one of the start addresses of a jump. */
2713
2714static bool
2715jump_info_is_start_address (const struct jump_info *ji, bfd_vma address)
2716{
2717 bool result = false;
2718 size_t i;
2719
2720 for (i = ji->start.count; i-- > 0;)
2721 if (address == ji->start.addresses[i])
2722 {
2723 result = true;
2724 break;
2725 }
2726
2727 return result;
2728}
2729
2730/* Test if an address is the target address of a jump. */
2731
2732static bool
2733jump_info_is_end_address (const struct jump_info *ji, bfd_vma address)
2734{
2735 return (address == ji->end);
2736}
2737
2738/* Get the difference between the smallest and largest address of a jump. */
2739
2740static bfd_vma
2741jump_info_size (const struct jump_info *ji)
2742{
2743 return jump_info_max_address (ji) - jump_info_min_address (ji);
2744}
2745
2746/* Unlink a jump object from a list. */
2747
2748static void
2749jump_info_unlink (struct jump_info *node,
2750 struct jump_info **base)
2751{
2752 if (node->next)
2753 node->next->prev = node->prev;
2754 if (node->prev)
2755 node->prev->next = node->next;
2756 else
2757 *base = node->next;
2758 node->next = NULL;
2759 node->prev = NULL;
2760}
2761
2762/* Insert unlinked jump info node into a list. */
2763
2764static void
2765jump_info_insert (struct jump_info *node,
2766 struct jump_info *target,
2767 struct jump_info **base)
2768{
2769 node->next = target;
2770 node->prev = target->prev;
2771 target->prev = node;
2772 if (node->prev)
2773 node->prev->next = node;
2774 else
2775 *base = node;
2776}
2777
2778/* Add unlinked node to the front of a list. */
2779
2780static void
2781jump_info_add_front (struct jump_info *node,
2782 struct jump_info **base)
2783{
2784 node->next = *base;
2785 if (node->next)
2786 node->next->prev = node;
2787 node->prev = NULL;
2788 *base = node;
2789}
2790
2791/* Move linked node to target position. */
2792
2793static void
2794jump_info_move_linked (struct jump_info *node,
2795 struct jump_info *target,
2796 struct jump_info **base)
2797{
2798 /* Unlink node. */
2799 jump_info_unlink (node, base);
2800 /* Insert node at target position. */
2801 jump_info_insert (node, target, base);
2802}
2803
2804/* Test if two jumps intersect. */
2805
2806static bool
2807jump_info_intersect (const struct jump_info *a,
2808 const struct jump_info *b)
2809{
2810 return ((jump_info_max_address (a) >= jump_info_min_address (b))
2811 && (jump_info_min_address (a) <= jump_info_max_address (b)));
2812}
2813
2814/* Merge two compatible jump info objects. */
2815
2816static void
2817jump_info_merge (struct jump_info **base)
2818{
2819 struct jump_info *a;
2820
2821 for (a = *base; a; a = a->next)
2822 {
2823 struct jump_info *b;
2824
2825 for (b = a->next; b; b = b->next)
2826 {
2827 /* Merge both jumps into one. */
2828 if (a->end == b->end)
2829 {
2830 /* Reallocate addresses. */
2831 size_t needed_size = a->start.count + b->start.count;
2832 size_t i;
2833
2834 if (needed_size > a->start.max_count)
2835 {
2836 a->start.max_count += b->start.max_count;
2837 a->start.addresses =
2838 xrealloc (a->start.addresses,
2839 a->start.max_count * sizeof (bfd_vma *));
2840 }
2841
2842 /* Append start addresses. */
2843 for (i = 0; i < b->start.count; ++i)
2844 a->start.addresses[a->start.count++] =
2845 b->start.addresses[i];
2846
2847 /* Remove and delete jump. */
2848 struct jump_info *tmp = b->prev;
2849 jump_info_unlink (b, base);
2850 jump_info_free (b);
2851 b = tmp;
2852 }
2853 }
2854 }
2855}
2856
2857/* Sort jumps by their size and starting point using a stable
2858 minsort. This could be improved if sorting performance is
2859 an issue, for example by using mergesort. */
2860
2861static void
2862jump_info_sort (struct jump_info **base)
2863{
2864 struct jump_info *current_element = *base;
2865
2866 while (current_element)
2867 {
2868 struct jump_info *best_match = current_element;
2869 struct jump_info *runner = current_element->next;
2870 bfd_vma best_size = jump_info_size (best_match);
2871
2872 while (runner)
2873 {
2874 bfd_vma runner_size = jump_info_size (runner);
2875
2876 if ((runner_size < best_size)
2877 || ((runner_size == best_size)
2878 && (jump_info_min_address (runner)
2879 < jump_info_min_address (best_match))))
2880 {
2881 best_match = runner;
2882 best_size = runner_size;
2883 }
2884
2885 runner = runner->next;
2886 }
2887
2888 if (best_match == current_element)
2889 current_element = current_element->next;
2890 else
2891 jump_info_move_linked (best_match, current_element, base);
2892 }
2893}
2894
2895/* Visualize all jumps at a given address. */
2896
2897static void
2898jump_info_visualize_address (bfd_vma address,
2899 int max_level,
2900 char *line_buffer,
2901 uint8_t *color_buffer)
2902{
2903 struct jump_info *ji = detected_jumps;
2904 size_t len = (max_level + 1) * 3;
2905
2906 /* Clear line buffer. */
2907 memset (line_buffer, ' ', len);
2908 memset (color_buffer, 0, len);
2909
2910 /* Iterate over jumps and add their ASCII art. */
2911 while (ji)
2912 {
2913 /* Discard jumps that are never needed again. */
2914 if (jump_info_max_address (ji) < address)
2915 {
2916 struct jump_info *tmp = ji;
2917
2918 ji = ji->next;
2919 jump_info_unlink (tmp, &detected_jumps);
2920 jump_info_free (tmp);
2921 continue;
2922 }
2923
2924 /* This jump intersects with the current address. */
2925 if (jump_info_min_address (ji) <= address)
2926 {
2927 /* Hash target address to get an even
2928 distribution between all values. */
2929 bfd_vma hash_address = jump_info_end_address (ji);
2930 uint8_t color = iterative_hash_object (hash_address, 0);
2931 /* Fetch line offset. */
2932 int offset = (max_level - ji->level) * 3;
2933
2934 /* Draw start line. */
2935 if (jump_info_is_start_address (ji, address))
2936 {
2937 size_t i = offset + 1;
2938
2939 for (; i < len - 1; ++i)
2940 if (line_buffer[i] == ' ')
2941 {
2942 line_buffer[i] = '-';
2943 color_buffer[i] = color;
2944 }
2945
2946 if (line_buffer[i] == ' ')
2947 {
2948 line_buffer[i] = '-';
2949 color_buffer[i] = color;
2950 }
2951 else if (line_buffer[i] == '>')
2952 {
2953 line_buffer[i] = 'X';
2954 color_buffer[i] = color;
2955 }
2956
2957 if (line_buffer[offset] == ' ')
2958 {
2959 if (address <= ji->end)
2960 line_buffer[offset] =
2961 (jump_info_min_address (ji) == address) ? ',': '+';
2962 else
2963 line_buffer[offset] =
2964 (jump_info_max_address (ji) == address) ? '\'': '+';
2965 color_buffer[offset] = color;
2966 }
2967 }
2968 /* Draw jump target. */
2969 else if (jump_info_is_end_address (ji, address))
2970 {
2971 size_t i = offset + 1;
2972
2973 for (; i < len - 1; ++i)
2974 if (line_buffer[i] == ' ')
2975 {
2976 line_buffer[i] = '-';
2977 color_buffer[i] = color;
2978 }
2979
2980 if (line_buffer[i] == ' ')
2981 {
2982 line_buffer[i] = '>';
2983 color_buffer[i] = color;
2984 }
2985 else if (line_buffer[i] == '-')
2986 {
2987 line_buffer[i] = 'X';
2988 color_buffer[i] = color;
2989 }
2990
2991 if (line_buffer[offset] == ' ')
2992 {
2993 if (jump_info_min_address (ji) < address)
2994 line_buffer[offset] =
2995 (jump_info_max_address (ji) > address) ? '>' : '\'';
2996 else
2997 line_buffer[offset] = ',';
2998 color_buffer[offset] = color;
2999 }
3000 }
3001 /* Draw intermediate line segment. */
3002 else if (line_buffer[offset] == ' ')
3003 {
3004 line_buffer[offset] = '|';
3005 color_buffer[offset] = color;
3006 }
3007 }
3008
3009 ji = ji->next;
3010 }
3011}
3012
3013/* Clone of disassemble_bytes to detect jumps inside a function. */
3014/* FIXME: is this correct? Can we strip it down even further? */
3015
3016static struct jump_info *
3017disassemble_jumps (struct disassemble_info * inf,
3018 disassembler_ftype disassemble_fn,
3019 bfd_vma start_offset,
3020 bfd_vma stop_offset,
3021 bfd_vma rel_offset,
3022 arelent ** relpp,
3023 arelent ** relppend)
3024{
3025 struct objdump_disasm_info *aux;
3026 struct jump_info *jumps = NULL;
3027 asection *section;
3028 bfd_vma addr_offset;
3029 unsigned int opb = inf->octets_per_byte;
3030 int octets = opb;
3031 SFILE sfile;
3032
3033 aux = (struct objdump_disasm_info *) inf->application_data;
3034 section = inf->section;
3035
3036 sfile.alloc = 120;
3037 sfile.buffer = (char *) xmalloc (sfile.alloc);
3038 sfile.pos = 0;
3039
3040 inf->insn_info_valid = 0;
3041 disassemble_set_printf (inf, &sfile, (fprintf_ftype) objdump_sprintf,
3042 (fprintf_styled_ftype) objdump_styled_sprintf);
3043
3044 addr_offset = start_offset;
3045 while (addr_offset < stop_offset)
3046 {
3047 int previous_octets;
3048
3049 /* Remember the length of the previous instruction. */
3050 previous_octets = octets;
3051 octets = 0;
3052
3053 sfile.pos = 0;
3054 inf->bytes_per_line = 0;
3055 inf->bytes_per_chunk = 0;
3056 inf->flags = ((disassemble_all ? DISASSEMBLE_DATA : 0)
3057 | (wide_output ? WIDE_OUTPUT : 0));
3058 if (machine)
3059 inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
3060
3061 if (inf->disassembler_needs_relocs
3062 && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
3063 && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
3064 && relpp < relppend)
3065 {
3066 bfd_signed_vma distance_to_rel;
3067
3068 distance_to_rel = (*relpp)->address - (rel_offset + addr_offset);
3069
3070 /* Check to see if the current reloc is associated with
3071 the instruction that we are about to disassemble. */
3072 if (distance_to_rel == 0
3073 /* FIXME: This is wrong. We are trying to catch
3074 relocs that are addressed part way through the
3075 current instruction, as might happen with a packed
3076 VLIW instruction. Unfortunately we do not know the
3077 length of the current instruction since we have not
3078 disassembled it yet. Instead we take a guess based
3079 upon the length of the previous instruction. The
3080 proper solution is to have a new target-specific
3081 disassembler function which just returns the length
3082 of an instruction at a given address without trying
3083 to display its disassembly. */
3084 || (distance_to_rel > 0
3085 && distance_to_rel < (bfd_signed_vma) (previous_octets/ opb)))
3086 {
3087 inf->flags |= INSN_HAS_RELOC;
3088 }
3089 }
3090
3091 if (! disassemble_all
3092 && (section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
3093 == (SEC_CODE | SEC_HAS_CONTENTS))
3094 /* Set a stop_vma so that the disassembler will not read
3095 beyond the next symbol. We assume that symbols appear on
3096 the boundaries between instructions. We only do this when
3097 disassembling code of course, and when -D is in effect. */
3098 inf->stop_vma = section->vma + stop_offset;
3099
3100 inf->stop_offset = stop_offset;
3101
3102 /* Extract jump information. */
3103 inf->insn_info_valid = 0;
3104 disassembler_in_comment = false;
3105 octets = (*disassemble_fn) (section->vma + addr_offset, inf);
3106 /* Test if a jump was detected. */
3107 if (inf->insn_info_valid
3108 && ((inf->insn_type == dis_branch)
3109 || (inf->insn_type == dis_condbranch)
3110 || (inf->insn_type == dis_jsr)
3111 || (inf->insn_type == dis_condjsr))
3112 && (inf->target >= section->vma + start_offset)
3113 && (inf->target < section->vma + stop_offset))
3114 {
3115 struct jump_info *ji =
3116 jump_info_new (section->vma + addr_offset, inf->target, -1);
3117 jump_info_add_front (ji, &jumps);
3118 }
3119
3120 inf->stop_vma = 0;
3121
3122 addr_offset += octets / opb;
3123 }
3124
3125 disassemble_set_printf (inf, (void *) stdout, (fprintf_ftype) fprintf,
3126 (fprintf_styled_ftype) fprintf_styled);
3127 free (sfile.buffer);
3128
3129 /* Merge jumps. */
3130 jump_info_merge (&jumps);
3131 /* Process jumps. */
3132 jump_info_sort (&jumps);
3133
3134 /* Group jumps by level. */
3135 struct jump_info *last_jump = jumps;
3136 int max_level = -1;
3137
3138 while (last_jump)
3139 {
3140 /* The last jump is part of the next group. */
3141 struct jump_info *base = last_jump;
3142 /* Increment level. */
3143 base->level = ++max_level;
3144
3145 /* Find jumps that can be combined on the same
3146 level, with the largest jumps tested first.
3147 This has the advantage that large jumps are on
3148 lower levels and do not intersect with small
3149 jumps that get grouped on higher levels. */
3150 struct jump_info *exchange_item = last_jump->next;
3151 struct jump_info *it = exchange_item;
3152
3153 for (; it; it = it->next)
3154 {
3155 /* Test if the jump intersects with any
3156 jump from current group. */
3157 bool ok = true;
3158 struct jump_info *it_collision;
3159
3160 for (it_collision = base;
3161 it_collision != exchange_item;
3162 it_collision = it_collision->next)
3163 {
3164 /* This jump intersects so we leave it out. */
3165 if (jump_info_intersect (it_collision, it))
3166 {
3167 ok = false;
3168 break;
3169 }
3170 }
3171
3172 /* Add jump to group. */
3173 if (ok)
3174 {
3175 /* Move current element to the front. */
3176 if (it != exchange_item)
3177 {
3178 struct jump_info *save = it->prev;
3179 jump_info_move_linked (it, exchange_item, &jumps);
3180 last_jump = it;
3181 it = save;
3182 }
3183 else
3184 {
3185 last_jump = exchange_item;
3186 exchange_item = exchange_item->next;
3187 }
3188 last_jump->level = max_level;
3189 }
3190 }
3191
3192 /* Move to next group. */
3193 last_jump = exchange_item;
3194 }
3195
3196 return jumps;
3197}
3198
3199/* The number of zeroes we want to see before we start skipping them.
3200 The number is arbitrarily chosen. */
3201
3202#define DEFAULT_SKIP_ZEROES 8
3203
3204/* The number of zeroes to skip at the end of a section. If the
3205 number of zeroes at the end is between SKIP_ZEROES_AT_END and
3206 SKIP_ZEROES, they will be disassembled. If there are fewer than
3207 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
3208 attempt to avoid disassembling zeroes inserted by section
3209 alignment. */
3210
3211#define DEFAULT_SKIP_ZEROES_AT_END 3
3212
3213static int
3214null_print (const void * stream ATTRIBUTE_UNUSED, const char * format ATTRIBUTE_UNUSED, ...)
3215{
3216 return 1;
3217}
3218
3219/* Like null_print, but takes the extra STYLE argument. As this is not
3220 going to print anything, the extra argument is just ignored. */
3221
3222static int
3223null_styled_print (const void * stream ATTRIBUTE_UNUSED,
3224 enum disassembler_style style ATTRIBUTE_UNUSED,
3225 const char * format ATTRIBUTE_UNUSED, ...)
3226{
3227 return 1;
3228}
3229
3230/* Print out jump visualization. */
3231
3232static void
3233print_jump_visualisation (bfd_vma addr, int max_level, char *line_buffer,
3234 uint8_t *color_buffer)
3235{
3236 if (!line_buffer)
3237 return;
3238
3239 jump_info_visualize_address (addr, max_level, line_buffer, color_buffer);
3240
3241 size_t line_buffer_size = strlen (line_buffer);
3242 char last_color = 0;
3243 size_t i;
3244
3245 for (i = 0; i <= line_buffer_size; ++i)
3246 {
3247 if (color_output)
3248 {
3249 uint8_t color = (i < line_buffer_size) ? color_buffer[i]: 0;
3250
3251 if (color != last_color)
3252 {
3253 if (color)
3254 if (extended_color_output)
3255 /* Use extended 8bit color, but
3256 do not choose dark colors. */
3257 printf ("\033[38;5;%dm", 124 + (color % 108));
3258 else
3259 /* Use simple terminal colors. */
3260 printf ("\033[%dm", 31 + (color % 7));
3261 else
3262 /* Clear color. */
3263 printf ("\033[0m");
3264 last_color = color;
3265 }
3266 }
3267 putchar ((i < line_buffer_size) ? line_buffer[i]: ' ');
3268 }
3269}
3270
3271/* Disassemble some data in memory between given values. */
3272
3273static void
3274disassemble_bytes (struct disassemble_info *inf,
3275 disassembler_ftype disassemble_fn,
3276 bool insns,
3277 bfd_byte *data,
3278 bfd_vma start_offset,
3279 bfd_vma stop_offset,
3280 bfd_vma rel_offset,
3281 arelent **relpp,
3282 arelent **relppend)
3283{
3284 struct objdump_disasm_info *aux;
3285 asection *section;
3286 unsigned int octets_per_line;
3287 unsigned int skip_addr_chars;
3288 bfd_vma addr_offset;
3289 unsigned int opb = inf->octets_per_byte;
3290 unsigned int skip_zeroes = inf->skip_zeroes;
3291 unsigned int skip_zeroes_at_end = inf->skip_zeroes_at_end;
3292 size_t octets;
3293 SFILE sfile;
3294
3295 aux = (struct objdump_disasm_info *) inf->application_data;
3296 section = inf->section;
3297
3298 sfile.alloc = 120;
3299 sfile.buffer = (char *) xmalloc (sfile.alloc);
3300 sfile.pos = 0;
3301
3302 if (insn_width)
3303 octets_per_line = insn_width;
3304 else if (insns)
3305 octets_per_line = 4;
3306 else
3307 octets_per_line = 16;
3308
3309 /* Figure out how many characters to skip at the start of an
3310 address, to make the disassembly look nicer. We discard leading
3311 zeroes in chunks of 4, ensuring that there is always a leading
3312 zero remaining. */
3313 skip_addr_chars = 0;
3314 if (!no_addresses && !prefix_addresses)
3315 {
3316 char buf[30];
3317
3318 bfd_sprintf_vma (aux->abfd, buf, section->vma + section->size / opb);
3319
3320 while (buf[skip_addr_chars] == '0')
3321 ++skip_addr_chars;
3322
3323 /* Don't discard zeros on overflow. */
3324 if (buf[skip_addr_chars] == '\0' && section->vma != 0)
3325 skip_addr_chars = 0;
3326
3327 if (skip_addr_chars != 0)
3328 skip_addr_chars = (skip_addr_chars - 1) & -4;
3329 }
3330
3331 inf->insn_info_valid = 0;
3332
3333 /* Determine maximum level. */
3334 uint8_t *color_buffer = NULL;
3335 char *line_buffer = NULL;
3336 int max_level = -1;
3337
3338 /* Some jumps were detected. */
3339 if (detected_jumps)
3340 {
3341 struct jump_info *ji;
3342
3343 /* Find maximum jump level. */
3344 for (ji = detected_jumps; ji; ji = ji->next)
3345 {
3346 if (ji->level > max_level)
3347 max_level = ji->level;
3348 }
3349
3350 /* Allocate buffers. */
3351 size_t len = (max_level + 1) * 3 + 1;
3352 line_buffer = xmalloc (len);
3353 line_buffer[len - 1] = 0;
3354 color_buffer = xmalloc (len);
3355 color_buffer[len - 1] = 0;
3356 }
3357
3358 addr_offset = start_offset;
3359 while (addr_offset < stop_offset)
3360 {
3361 bool need_nl = false;
3362
3363 octets = 0;
3364
3365 /* Make sure we don't use relocs from previous instructions. */
3366 aux->reloc = NULL;
3367
3368 /* If we see more than SKIP_ZEROES octets of zeroes, we just
3369 print `...'. */
3370 if (! disassemble_zeroes)
3371 for (; octets < (stop_offset - addr_offset) * opb; octets++)
3372 if (data[addr_offset * opb + octets] != 0)
3373 break;
3374 if (! disassemble_zeroes
3375 && (inf->insn_info_valid == 0
3376 || inf->branch_delay_insns == 0)
3377 && (octets >= skip_zeroes
3378 || (octets == (stop_offset - addr_offset) * opb
3379 && octets < skip_zeroes_at_end)))
3380 {
3381 /* If there are more nonzero octets to follow, we only skip
3382 zeroes in multiples of 4, to try to avoid running over
3383 the start of an instruction which happens to start with
3384 zero. */
3385 if (octets != (stop_offset - addr_offset) * opb)
3386 octets &= ~3;
3387
3388 /* If we are going to display more data, and we are displaying
3389 file offsets, then tell the user how many zeroes we skip
3390 and the file offset from where we resume dumping. */
3391 if (display_file_offsets
3392 && octets / opb < stop_offset - addr_offset)
3393 printf (_("\t... (skipping %lu zeroes, "
3394 "resuming at file offset: 0x%lx)\n"),
3395 (unsigned long) (octets / opb),
3396 (unsigned long) (section->filepos
3397 + addr_offset + octets / opb));
3398 else
3399 printf ("\t...\n");
3400 }
3401 else
3402 {
3403 char buf[MAX_INSN_WIDTH + 1];
3404 unsigned int bpc = 0;
3405 unsigned int pb = 0;
3406
3407 if (with_line_numbers || with_source_code)
3408 show_line (aux->abfd, section, addr_offset);
3409
3410 if (no_addresses)
3411 printf ("\t");
3412 else if (!prefix_addresses)
3413 {
3414 char *s;
3415
3416 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
3417 for (s = buf + skip_addr_chars; *s == '0'; s++)
3418 *s = ' ';
3419 if (*s == '\0')
3420 *--s = '0';
3421 printf ("%s:\t", buf + skip_addr_chars);
3422 }
3423 else
3424 {
3425 aux->require_sec = true;
3426 objdump_print_address (section->vma + addr_offset, inf);
3427 aux->require_sec = false;
3428 putchar (' ');
3429 }
3430
3431 print_jump_visualisation (section->vma + addr_offset,
3432 max_level, line_buffer,
3433 color_buffer);
3434
3435 if (insns)
3436 {
3437 int insn_size;
3438
3439 sfile.pos = 0;
3440 disassemble_set_printf
3441 (inf, &sfile, (fprintf_ftype) objdump_sprintf,
3442 (fprintf_styled_ftype) objdump_styled_sprintf);
3443 inf->bytes_per_line = 0;
3444 inf->bytes_per_chunk = 0;
3445 inf->flags = ((disassemble_all ? DISASSEMBLE_DATA : 0)
3446 | (wide_output ? WIDE_OUTPUT : 0));
3447 if (machine)
3448 inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
3449
3450 if (inf->disassembler_needs_relocs
3451 && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
3452 && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
3453 && relpp < relppend)
3454 {
3455 bfd_signed_vma distance_to_rel;
3456 int max_reloc_offset
3457 = aux->abfd->arch_info->max_reloc_offset_into_insn;
3458
3459 distance_to_rel = ((*relpp)->address - rel_offset
3460 - addr_offset);
3461
3462 insn_size = 0;
3463 if (distance_to_rel > 0
3464 && (max_reloc_offset < 0
3465 || distance_to_rel <= max_reloc_offset))
3466 {
3467 /* This reloc *might* apply to the current insn,
3468 starting somewhere inside it. Discover the length
3469 of the current insn so that the check below will
3470 work. */
3471 if (insn_width)
3472 insn_size = insn_width;
3473 else
3474 {
3475 /* We find the length by calling the dissassembler
3476 function with a dummy print handler. This should
3477 work unless the disassembler is not expecting to
3478 be called multiple times for the same address.
3479
3480 This does mean disassembling the instruction
3481 twice, but we only do this when there is a high
3482 probability that there is a reloc that will
3483 affect the instruction. */
3484 disassemble_set_printf
3485 (inf, inf->stream, (fprintf_ftype) null_print,
3486 (fprintf_styled_ftype) null_styled_print);
3487 insn_size = disassemble_fn (section->vma
3488 + addr_offset, inf);
3489 disassemble_set_printf
3490 (inf, inf->stream,
3491 (fprintf_ftype) objdump_sprintf,
3492 (fprintf_styled_ftype) objdump_styled_sprintf);
3493 }
3494 }
3495
3496 /* Check to see if the current reloc is associated with
3497 the instruction that we are about to disassemble. */
3498 if (distance_to_rel == 0
3499 || (distance_to_rel > 0
3500 && distance_to_rel < insn_size / (int) opb))
3501 {
3502 inf->flags |= INSN_HAS_RELOC;
3503 aux->reloc = *relpp;
3504 }
3505 }
3506
3507 if (! disassemble_all
3508 && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
3509 == (SEC_CODE | SEC_HAS_CONTENTS)))
3510 /* Set a stop_vma so that the disassembler will not read
3511 beyond the next symbol. We assume that symbols appear on
3512 the boundaries between instructions. We only do this when
3513 disassembling code of course, and when -D is in effect. */
3514 inf->stop_vma = section->vma + stop_offset;
3515
3516 inf->stop_offset = stop_offset;
3517 disassembler_in_comment = false;
3518 insn_size = (*disassemble_fn) (section->vma + addr_offset, inf);
3519 octets = insn_size;
3520
3521 inf->stop_vma = 0;
3522 disassemble_set_printf (inf, stdout, (fprintf_ftype) fprintf,
3523 (fprintf_styled_ftype) fprintf_styled);
3524 if (insn_width == 0 && inf->bytes_per_line != 0)
3525 octets_per_line = inf->bytes_per_line;
3526 if (insn_size < (int) opb)
3527 {
3528 if (sfile.pos)
3529 printf ("%s\n", sfile.buffer);
3530 if (insn_size >= 0)
3531 {
3532 non_fatal (_("disassemble_fn returned length %d"),
3533 insn_size);
3534 exit_status = 1;
3535 }
3536 break;
3537 }
3538 }
3539 else
3540 {
3541 bfd_vma j;
3542
3543 octets = octets_per_line;
3544 if (octets / opb > stop_offset - addr_offset)
3545 octets = (stop_offset - addr_offset) * opb;
3546
3547 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
3548 {
3549 if (ISPRINT (data[j]))
3550 buf[j - addr_offset * opb] = data[j];
3551 else
3552 buf[j - addr_offset * opb] = '.';
3553 }
3554 buf[j - addr_offset * opb] = '\0';
3555 }
3556
3557 if (prefix_addresses
3558 ? show_raw_insn > 0
3559 : show_raw_insn >= 0)
3560 {
3561 bfd_vma j;
3562
3563 /* If ! prefix_addresses and ! wide_output, we print
3564 octets_per_line octets per line. */
3565 pb = octets;
3566 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
3567 pb = octets_per_line;
3568
3569 if (inf->bytes_per_chunk)
3570 bpc = inf->bytes_per_chunk;
3571 else
3572 bpc = 1;
3573
3574 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
3575 {
3576 /* PR 21580: Check for a buffer ending early. */
3577 if (j + bpc <= stop_offset * opb)
3578 {
3579 unsigned int k;
3580
3581 if (inf->display_endian == BFD_ENDIAN_LITTLE)
3582 {
3583 for (k = bpc; k-- != 0; )
3584 printf ("%02x", (unsigned) data[j + k]);
3585 }
3586 else
3587 {
3588 for (k = 0; k < bpc; k++)
3589 printf ("%02x", (unsigned) data[j + k]);
3590 }
3591 }
3592 putchar (' ');
3593 }
3594
3595 for (; pb < octets_per_line; pb += bpc)
3596 {
3597 unsigned int k;
3598
3599 for (k = 0; k < bpc; k++)
3600 printf (" ");
3601 putchar (' ');
3602 }
3603
3604 /* Separate raw data from instruction by extra space. */
3605 if (insns)
3606 putchar ('\t');
3607 else
3608 printf (" ");
3609 }
3610
3611 if (! insns)
3612 printf ("%s", buf);
3613 else if (sfile.pos)
3614 printf ("%s", sfile.buffer);
3615
3616 if (prefix_addresses
3617 ? show_raw_insn > 0
3618 : show_raw_insn >= 0)
3619 {
3620 while (pb < octets)
3621 {
3622 bfd_vma j;
3623 char *s;
3624
3625 putchar ('\n');
3626 j = addr_offset * opb + pb;
3627
3628 if (no_addresses)
3629 printf ("\t");
3630 else
3631 {
3632 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
3633 for (s = buf + skip_addr_chars; *s == '0'; s++)
3634 *s = ' ';
3635 if (*s == '\0')
3636 *--s = '0';
3637 printf ("%s:\t", buf + skip_addr_chars);
3638 }
3639
3640 print_jump_visualisation (section->vma + j / opb,
3641 max_level, line_buffer,
3642 color_buffer);
3643
3644 pb += octets_per_line;
3645 if (pb > octets)
3646 pb = octets;
3647 for (; j < addr_offset * opb + pb; j += bpc)
3648 {
3649 /* PR 21619: Check for a buffer ending early. */
3650 if (j + bpc <= stop_offset * opb)
3651 {
3652 unsigned int k;
3653
3654 if (inf->display_endian == BFD_ENDIAN_LITTLE)
3655 {
3656 for (k = bpc; k-- != 0; )
3657 printf ("%02x", (unsigned) data[j + k]);
3658 }
3659 else
3660 {
3661 for (k = 0; k < bpc; k++)
3662 printf ("%02x", (unsigned) data[j + k]);
3663 }
3664 }
3665 putchar (' ');
3666 }
3667 }
3668 }
3669
3670 if (!wide_output)
3671 putchar ('\n');
3672 else
3673 need_nl = true;
3674 }
3675
3676 while (relpp < relppend
3677 && (*relpp)->address < rel_offset + addr_offset + octets / opb)
3678 {
3679 if (dump_reloc_info || dump_dynamic_reloc_info)
3680 {
3681 arelent *q;
3682
3683 q = *relpp;
3684
3685 if (wide_output)
3686 putchar ('\t');
3687 else
3688 printf ("\t\t\t");
3689
3690 if (!no_addresses)
3691 {
3692 objdump_print_value (section->vma - rel_offset + q->address,
3693 inf, true);
3694 printf (": ");
3695 }
3696
3697 if (q->howto == NULL)
3698 printf ("*unknown*\t");
3699 else if (q->howto->name)
3700 printf ("%s\t", q->howto->name);
3701 else
3702 printf ("%d\t", q->howto->type);
3703
3704 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
3705 printf ("*unknown*");
3706 else
3707 {
3708 const char *sym_name;
3709
3710 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
3711 if (sym_name != NULL && *sym_name != '\0')
3712 objdump_print_symname (aux->abfd, inf, *q->sym_ptr_ptr);
3713 else
3714 {
3715 asection *sym_sec;
3716
3717 sym_sec = bfd_asymbol_section (*q->sym_ptr_ptr);
3718 sym_name = bfd_section_name (sym_sec);
3719 if (sym_name == NULL || *sym_name == '\0')
3720 sym_name = "*unknown*";
3721 printf ("%s", sanitize_string (sym_name));
3722 }
3723 }
3724
3725 if (q->addend)
3726 {
3727 bfd_vma addend = q->addend;
3728 if ((bfd_signed_vma) addend < 0)
3729 {
3730 printf ("-0x");
3731 addend = -addend;
3732 }
3733 else
3734 printf ("+0x");
3735 objdump_print_value (addend, inf, true);
3736 }
3737
3738 printf ("\n");
3739 need_nl = false;
3740 }
3741 ++relpp;
3742 }
3743
3744 if (need_nl)
3745 printf ("\n");
3746
3747 addr_offset += octets / opb;
3748 }
3749
3750 free (sfile.buffer);
3751 free (line_buffer);
3752 free (color_buffer);
3753}
3754
3755static void
3756disassemble_section (bfd *abfd, asection *section, void *inf)
3757{
3758 const struct elf_backend_data *bed;
3759 bfd_vma sign_adjust = 0;
3760 struct disassemble_info *pinfo = (struct disassemble_info *) inf;
3761 struct objdump_disasm_info *paux;
3762 unsigned int opb = pinfo->octets_per_byte;
3763 bfd_byte *data = NULL;
3764 bfd_size_type datasize = 0;
3765 arelent **rel_pp = NULL;
3766 arelent **rel_ppstart = NULL;
3767 arelent **rel_ppend;
3768 bfd_vma stop_offset;
3769 asymbol *sym = NULL;
3770 long place = 0;
3771 long rel_count;
3772 bfd_vma rel_offset;
3773 unsigned long addr_offset;
3774 bool do_print;
3775 enum loop_control
3776 {
3777 stop_offset_reached,
3778 function_sym,
3779 next_sym
3780 } loop_until;
3781
3782 if (only_list == NULL)
3783 {
3784 /* Sections that do not contain machine
3785 code are not normally disassembled. */
3786 if ((section->flags & SEC_HAS_CONTENTS) == 0)
3787 return;
3788
3789 if (! disassemble_all
3790 && (section->flags & SEC_CODE) == 0)
3791 return;
3792 }
3793 else if (!process_section_p (section))
3794 return;
3795
3796 datasize = bfd_section_size (section);
3797 if (datasize == 0)
3798 return;
3799
3800 if (start_address == (bfd_vma) -1
3801 || start_address < section->vma)
3802 addr_offset = 0;
3803 else
3804 addr_offset = start_address - section->vma;
3805
3806 if (stop_address == (bfd_vma) -1)
3807 stop_offset = datasize / opb;
3808 else
3809 {
3810 if (stop_address < section->vma)
3811 stop_offset = 0;
3812 else
3813 stop_offset = stop_address - section->vma;
3814 if (stop_offset > datasize / opb)
3815 stop_offset = datasize / opb;
3816 }
3817
3818 if (addr_offset >= stop_offset)
3819 return;
3820
3821 /* Decide which set of relocs to use. Load them if necessary. */
3822 paux = (struct objdump_disasm_info *) pinfo->application_data;
3823 if (pinfo->dynrelbuf && dump_dynamic_reloc_info)
3824 {
3825 rel_pp = pinfo->dynrelbuf;
3826 rel_count = pinfo->dynrelcount;
3827 /* Dynamic reloc addresses are absolute, non-dynamic are section
3828 relative. REL_OFFSET specifies the reloc address corresponding
3829 to the start of this section. */
3830 rel_offset = section->vma;
3831 }
3832 else
3833 {
3834 rel_count = 0;
3835 rel_pp = NULL;
3836 rel_offset = 0;
3837
3838 if ((section->flags & SEC_RELOC) != 0
3839 && (dump_reloc_info || pinfo->disassembler_needs_relocs))
3840 {
3841 long relsize;
3842
3843 relsize = bfd_get_reloc_upper_bound (abfd, section);
3844 if (relsize < 0)
3845 my_bfd_nonfatal (bfd_get_filename (abfd));
3846
3847 if (relsize > 0)
3848 {
3849 rel_pp = (arelent **) xmalloc (relsize);
3850 rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
3851 if (rel_count < 0)
3852 {
3853 my_bfd_nonfatal (bfd_get_filename (abfd));
3854 free (rel_pp);
3855 rel_pp = NULL;
3856 rel_count = 0;
3857 }
3858 else if (rel_count > 1)
3859 /* Sort the relocs by address. */
3860 qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
3861 rel_ppstart = rel_pp;
3862 }
3863 }
3864 }
3865 rel_ppend = PTR_ADD (rel_pp, rel_count);
3866
3867 if (!bfd_malloc_and_get_section (abfd, section, &data))
3868 {
3869 non_fatal (_("Reading section %s failed because: %s"),
3870 section->name, bfd_errmsg (bfd_get_error ()));
3871 free (rel_ppstart);
3872 return;
3873 }
3874
3875 pinfo->buffer = data;
3876 pinfo->buffer_vma = section->vma;
3877 pinfo->buffer_length = datasize;
3878 pinfo->section = section;
3879
3880 /* Sort the symbols into value and section order. */
3881 compare_section = section;
3882 if (sorted_symcount > 1)
3883 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
3884
3885 printf (_("\nDisassembly of section %s:\n"), sanitize_string (section->name));
3886
3887 /* Find the nearest symbol forwards from our current position. */
3888 paux->require_sec = true;
3889 sym = (asymbol *) find_symbol_for_address (section->vma + addr_offset,
3890 (struct disassemble_info *) inf,
3891 &place);
3892 paux->require_sec = false;
3893
3894 /* PR 9774: If the target used signed addresses then we must make
3895 sure that we sign extend the value that we calculate for 'addr'
3896 in the loop below. */
3897 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
3898 && (bed = get_elf_backend_data (abfd)) != NULL
3899 && bed->sign_extend_vma)
3900 sign_adjust = (bfd_vma) 1 << (bed->s->arch_size - 1);
3901
3902 /* Disassemble a block of instructions up to the address associated with
3903 the symbol we have just found. Then print the symbol and find the
3904 next symbol on. Repeat until we have disassembled the entire section
3905 or we have reached the end of the address range we are interested in. */
3906 do_print = paux->symbol_list == NULL;
3907 loop_until = stop_offset_reached;
3908
3909 while (addr_offset < stop_offset)
3910 {
3911 bfd_vma addr;
3912 asymbol *nextsym;
3913 bfd_vma nextstop_offset;
3914 bool insns;
3915
3916 /* Skip over the relocs belonging to addresses below the
3917 start address. */
3918 while (rel_pp < rel_ppend
3919 && (*rel_pp)->address < rel_offset + addr_offset)
3920 ++rel_pp;
3921
3922 addr = section->vma + addr_offset;
3923 addr = ((addr & ((sign_adjust << 1) - 1)) ^ sign_adjust) - sign_adjust;
3924
3925 if (sym != NULL && bfd_asymbol_value (sym) <= addr)
3926 {
3927 int x;
3928
3929 for (x = place;
3930 (x < sorted_symcount
3931 && (bfd_asymbol_value (sorted_syms[x]) <= addr));
3932 ++x)
3933 continue;
3934
3935 pinfo->symbols = sorted_syms + place;
3936 pinfo->num_symbols = x - place;
3937 pinfo->symtab_pos = place;
3938 }
3939 else
3940 {
3941 pinfo->symbols = NULL;
3942 pinfo->num_symbols = 0;
3943 pinfo->symtab_pos = -1;
3944 }
3945
3946 /* If we are only disassembling from specific symbols,
3947 check to see if we should start or stop displaying. */
3948 if (sym && paux->symbol_list)
3949 {
3950 if (do_print)
3951 {
3952 /* See if we should stop printing. */
3953 switch (loop_until)
3954 {
3955 case function_sym:
3956 if (sym->flags & BSF_FUNCTION)
3957 do_print = false;
3958 break;
3959
3960 case stop_offset_reached:
3961 /* Handled by the while loop. */
3962 break;
3963
3964 case next_sym:
3965 if (! bfd_is_local_label (abfd, sym))
3966 do_print = false;
3967 break;
3968 }
3969 }
3970
3971 if (!do_print)
3972 {
3973 const char * name = bfd_asymbol_name (sym);
3974 char * alloc = NULL;
3975
3976 if (do_demangle && name[0] != '\0')
3977 {
3978 /* Demangle the name. */
3979 alloc = bfd_demangle (abfd, name, demangle_flags);
3980 if (alloc != NULL)
3981 name = alloc;
3982 }
3983
3984 /* We are not currently printing. Check to see
3985 if the current symbol matches any of the requested symbols. */
3986 for (const struct symbol_entry *ent = paux->symbol_list;
3987 ent;
3988 ent = ent->next)
3989 if (streq (name, ent->name))
3990 {
3991 do_print = true;
3992 break;
3993 }
3994 if (do_print
3995 && bfd_asymbol_value (sym) <= addr)
3996 {
3997 do_print = true;
3998
3999 loop_until = next_sym;
4000 if (sym->flags & BSF_FUNCTION)
4001 {
4002 loop_until = function_sym;
4003
4004 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
4005 {
4006 bfd_size_type fsize =
4007 ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
4008 bfd_vma fend =
4009 bfd_asymbol_value (sym) - section->vma + fsize;
4010 if (fend > addr_offset && fend <= stop_offset)
4011 {
4012 /* Sym is a function symbol with a valid
4013 size associated with it. Disassemble
4014 to the end of the function. */
4015 stop_offset = fend;
4016 loop_until = stop_offset_reached;
4017 }
4018 }
4019 }
4020 }
4021
4022 free (alloc);
4023 }
4024 }
4025
4026 if (! prefix_addresses && do_print)
4027 {
4028 pinfo->fprintf_func (pinfo->stream, "\n");
4029 objdump_print_addr_with_sym (abfd, section, sym, addr,
4030 pinfo, false);
4031 pinfo->fprintf_func (pinfo->stream, ":\n");
4032
4033 if (sym != NULL && show_all_symbols)
4034 {
4035 for (++place; place < sorted_symcount; place++)
4036 {
4037 sym = sorted_syms[place];
4038
4039 if (bfd_asymbol_value (sym) != addr)
4040 break;
4041 if (! pinfo->symbol_is_valid (sym, pinfo))
4042 continue;
4043 if (strcmp (bfd_section_name (sym->section), bfd_section_name (section)) != 0)
4044 break;
4045
4046 objdump_print_addr_with_sym (abfd, section, sym, addr, pinfo, false);
4047 pinfo->fprintf_func (pinfo->stream, ":\n");
4048 }
4049 }
4050 }
4051
4052 if (sym != NULL && bfd_asymbol_value (sym) > addr)
4053 nextsym = sym;
4054 else if (sym == NULL)
4055 nextsym = NULL;
4056 else
4057 {
4058#define is_valid_next_sym(SYM) \
4059 (strcmp (bfd_section_name ((SYM)->section), bfd_section_name (section)) == 0 \
4060 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
4061 && pinfo->symbol_is_valid (SYM, pinfo))
4062
4063 /* Search forward for the next appropriate symbol in
4064 SECTION. Note that all the symbols are sorted
4065 together into one big array, and that some sections
4066 may have overlapping addresses. */
4067 while (place < sorted_symcount
4068 && ! is_valid_next_sym (sorted_syms [place]))
4069 ++place;
4070
4071 if (place >= sorted_symcount)
4072 nextsym = NULL;
4073 else
4074 nextsym = sorted_syms[place];
4075 }
4076
4077 if (sym != NULL && bfd_asymbol_value (sym) > addr)
4078 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
4079 else if (nextsym == NULL)
4080 nextstop_offset = stop_offset;
4081 else
4082 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
4083
4084 if (nextstop_offset > stop_offset
4085 || nextstop_offset <= addr_offset)
4086 nextstop_offset = stop_offset;
4087
4088 /* If a symbol is explicitly marked as being an object
4089 rather than a function, just dump the bytes without
4090 disassembling them. */
4091 if (disassemble_all
4092 || sym == NULL
4093 || sym->section != section
4094 || bfd_asymbol_value (sym) > addr
4095 || ((sym->flags & BSF_OBJECT) == 0
4096 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
4097 == NULL)
4098 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
4099 == NULL))
4100 || (sym->flags & BSF_FUNCTION) != 0)
4101 insns = true;
4102 else
4103 insns = false;
4104
4105 if (do_print)
4106 {
4107 /* Resolve symbol name. */
4108 if (visualize_jumps && abfd && sym && sym->name)
4109 {
4110 struct disassemble_info di;
4111 SFILE sf;
4112
4113 sf.alloc = strlen (sym->name) + 40;
4114 sf.buffer = (char*) xmalloc (sf.alloc);
4115 sf.pos = 0;
4116 disassemble_set_printf
4117 (&di, &sf, (fprintf_ftype) objdump_sprintf,
4118 (fprintf_styled_ftype) objdump_styled_sprintf);
4119
4120 objdump_print_symname (abfd, &di, sym);
4121
4122 /* Fetch jump information. */
4123 detected_jumps = disassemble_jumps (pinfo, paux->disassemble_fn,
4124 addr_offset, nextstop_offset,
4125 rel_offset, rel_pp, rel_ppend);
4126 /* Free symbol name. */
4127 free (sf.buffer);
4128 }
4129
4130 /* Add jumps to output. */
4131 disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
4132 addr_offset, nextstop_offset,
4133 rel_offset, rel_pp, rel_ppend);
4134
4135 /* Free jumps. */
4136 while (detected_jumps)
4137 {
4138 detected_jumps = jump_info_free (detected_jumps);
4139 }
4140 }
4141
4142 addr_offset = nextstop_offset;
4143 sym = nextsym;
4144 }
4145
4146 free (data);
4147 free (rel_ppstart);
4148}
4149
4150/* Disassemble the contents of an object file. */
4151
4152static void
4153disassemble_data (bfd *abfd)
4154{
4155 struct disassemble_info disasm_info;
4156 struct objdump_disasm_info aux;
4157 long i;
4158
4159 print_files = NULL;
4160 prev_functionname = NULL;
4161 prev_line = -1;
4162 prev_discriminator = 0;
4163
4164 /* We make a copy of syms to sort. We don't want to sort syms
4165 because that will screw up the relocs. */
4166 sorted_symcount = symcount ? symcount : dynsymcount;
4167 sorted_syms = (asymbol **) xmalloc ((sorted_symcount + synthcount)
4168 * sizeof (asymbol *));
4169 if (sorted_symcount != 0)
4170 {
4171 memcpy (sorted_syms, symcount ? syms : dynsyms,
4172 sorted_symcount * sizeof (asymbol *));
4173
4174 sorted_symcount = remove_useless_symbols (sorted_syms, sorted_symcount);
4175 }
4176
4177 for (i = 0; i < synthcount; ++i)
4178 {
4179 sorted_syms[sorted_symcount] = synthsyms + i;
4180 ++sorted_symcount;
4181 }
4182
4183 init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf,
4184 (fprintf_styled_ftype) fprintf_styled);
4185 disasm_info.application_data = (void *) &aux;
4186 aux.abfd = abfd;
4187 aux.require_sec = false;
4188 disasm_info.dynrelbuf = NULL;
4189 disasm_info.dynrelcount = 0;
4190 aux.reloc = NULL;
4191 aux.symbol_list = disasm_sym_list;
4192
4193 disasm_info.print_address_func = objdump_print_address;
4194 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
4195
4196 if (machine != NULL)
4197 {
4198 const bfd_arch_info_type *inf = bfd_scan_arch (machine);
4199
4200 if (inf == NULL)
4201 {
4202 non_fatal (_("can't use supplied machine %s"), machine);
4203 exit_status = 1;
4204 }
4205 else
4206 abfd->arch_info = inf;
4207 }
4208
4209 const struct bfd_target *old_xvec = NULL;
4210 if (endian != BFD_ENDIAN_UNKNOWN)
4211 {
4212 struct bfd_target *xvec = xmalloc (sizeof (*xvec));
4213 old_xvec = abfd->xvec;
4214 memcpy (xvec, old_xvec, sizeof (*xvec));
4215 xvec->byteorder = endian;
4216 abfd->xvec = xvec;
4217 }
4218
4219 /* Use libopcodes to locate a suitable disassembler. */
4220 aux.disassemble_fn = disassembler (bfd_get_arch (abfd),
4221 bfd_big_endian (abfd),
4222 bfd_get_mach (abfd), abfd);
4223 if (!aux.disassemble_fn)
4224 {
4225 non_fatal (_("can't disassemble for architecture %s\n"),
4226 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
4227 exit_status = 1;
4228 goto out;
4229 }
4230
4231 disasm_info.flavour = bfd_get_flavour (abfd);
4232 disasm_info.arch = bfd_get_arch (abfd);
4233 disasm_info.mach = bfd_get_mach (abfd);
4234 disasm_info.disassembler_options = disassembler_options;
4235 disasm_info.octets_per_byte = bfd_octets_per_byte (abfd, NULL);
4236 disasm_info.skip_zeroes = DEFAULT_SKIP_ZEROES;
4237 disasm_info.skip_zeroes_at_end = DEFAULT_SKIP_ZEROES_AT_END;
4238 disasm_info.disassembler_needs_relocs = false;
4239
4240 if (bfd_big_endian (abfd))
4241 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
4242 else if (bfd_little_endian (abfd))
4243 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
4244 else
4245 /* ??? Aborting here seems too drastic. We could default to big or little
4246 instead. */
4247 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
4248
4249 disasm_info.endian_code = disasm_info.endian;
4250
4251 /* Allow the target to customize the info structure. */
4252 disassemble_init_for_target (& disasm_info);
4253
4254 /* Pre-load the dynamic relocs as we may need them during the disassembly. */
4255 long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4256
4257 if (relsize > 0)
4258 {
4259 disasm_info.dynrelbuf = (arelent **) xmalloc (relsize);
4260 disasm_info.dynrelcount
4261 = bfd_canonicalize_dynamic_reloc (abfd, disasm_info.dynrelbuf, dynsyms);
4262 if (disasm_info.dynrelcount < 0)
4263 {
4264 my_bfd_nonfatal (bfd_get_filename (abfd));
4265 free (disasm_info.dynrelbuf);
4266 disasm_info.dynrelbuf = NULL;
4267 disasm_info.dynrelcount = 0;
4268 }
4269 else if (disasm_info.dynrelcount > 1)
4270 /* Sort the relocs by address. */
4271 qsort (disasm_info.dynrelbuf, disasm_info.dynrelcount,
4272 sizeof (arelent *), compare_relocs);
4273 }
4274
4275 disasm_info.symtab = sorted_syms;
4276 disasm_info.symtab_size = sorted_symcount;
4277
4278 bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
4279
4280 free (disasm_info.dynrelbuf);
4281 disasm_info.dynrelbuf = NULL;
4282 disassemble_free_target (&disasm_info);
4283 out:
4284 free (sorted_syms);
4285 sorted_syms = NULL;
4286 if (old_xvec)
4287 {
4288 free ((void *) abfd->xvec);
4289 abfd->xvec = old_xvec;
4290 }
4291}
4292\f
4293static bool
4294load_specific_debug_section (enum dwarf_section_display_enum debug,
4295 asection *sec, void *file)
4296{
4297 struct dwarf_section *section = &debug_displays [debug].section;
4298 bfd *abfd = (bfd *) file;
4299 bfd_byte *contents;
4300 bfd_size_type amt;
4301 size_t alloced;
4302 bool ret;
4303
4304 if (section->start != NULL)
4305 {
4306 /* If it is already loaded, do nothing. */
4307 if (streq (section->filename, bfd_get_filename (abfd)))
4308 return true;
4309 free (section->start);
4310 section->start = NULL;
4311 }
4312
4313 section->filename = bfd_get_filename (abfd);
4314 section->reloc_info = NULL;
4315 section->num_relocs = 0;
4316 section->address = bfd_section_vma (sec);
4317 section->size = bfd_section_size (sec);
4318 /* PR 24360: On 32-bit hosts sizeof (size_t) < sizeof (bfd_size_type). */
4319 alloced = amt = section->size + 1;
4320 if (alloced != amt
4321 || alloced == 0
4322 || bfd_section_size_insane (abfd, sec))
4323 {
4324 printf (_("\nSection '%s' has an invalid size: %#" PRIx64 ".\n"),
4325 sanitize_string (section->name),
4326 section->size);
4327 free_debug_section (debug);
4328 return false;
4329 }
4330
4331 ret = false;
4332 if ((sec->flags & SEC_HAS_CONTENTS) != 0)
4333 {
4334 section->start = contents = xmalloc (alloced);
4335 /* Ensure any string section has a terminating NUL. */
4336 section->start[section->size] = 0;
4337
4338 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4339 && debug_displays [debug].relocate)
4340 {
4341 ret = bfd_simple_get_relocated_section_contents (abfd,
4342 sec,
4343 section->start,
4344 syms) != NULL;
4345 if (ret)
4346 {
4347 long reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
4348
4349 if (reloc_size > 0)
4350 {
4351 long reloc_count;
4352 arelent **relocs;
4353
4354 relocs = (arelent **) xmalloc (reloc_size);
4355
4356 reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, syms);
4357 if (reloc_count <= 0)
4358 free (relocs);
4359 else
4360 {
4361 section->reloc_info = relocs;
4362 section->num_relocs = reloc_count;
4363 }
4364 }
4365 }
4366 }
4367 else
4368 ret = bfd_get_full_section_contents (abfd, sec, &contents);
4369 }
4370
4371 if (!ret)
4372 {
4373 printf (_("\nCan't get contents for section '%s'.\n"),
4374 sanitize_string (section->name));
4375 free_debug_section (debug);
4376 return false;
4377 }
4378
4379 return true;
4380}
4381
4382bool
4383reloc_at (struct dwarf_section * dsec, uint64_t offset)
4384{
4385 arelent ** relocs;
4386 arelent * rp;
4387
4388 if (dsec == NULL || dsec->reloc_info == NULL)
4389 return false;
4390
4391 relocs = (arelent **) dsec->reloc_info;
4392
4393 for (; (rp = * relocs) != NULL; ++ relocs)
4394 if (rp->address == offset)
4395 return true;
4396
4397 return false;
4398}
4399
4400bool
4401load_debug_section (enum dwarf_section_display_enum debug, void *file)
4402{
4403 struct dwarf_section *section = &debug_displays [debug].section;
4404 bfd *abfd = (bfd *) file;
4405 asection *sec;
4406 const char *name;
4407
4408 if (!dump_any_debugging)
4409 return false;
4410
4411 /* If it is already loaded, do nothing. */
4412 if (section->start != NULL)
4413 {
4414 if (streq (section->filename, bfd_get_filename (abfd)))
4415 return true;
4416 }
4417 /* Locate the debug section. */
4418 name = section->uncompressed_name;
4419 sec = bfd_get_section_by_name (abfd, name);
4420 if (sec == NULL)
4421 {
4422 name = section->compressed_name;
4423 if (*name)
4424 sec = bfd_get_section_by_name (abfd, name);
4425 }
4426 if (sec == NULL)
4427 {
4428 name = section->xcoff_name;
4429 if (*name)
4430 sec = bfd_get_section_by_name (abfd, name);
4431 }
4432 if (sec == NULL)
4433 return false;
4434
4435 section->name = name;
4436 return load_specific_debug_section (debug, sec, file);
4437}
4438
4439void
4440free_debug_section (enum dwarf_section_display_enum debug)
4441{
4442 struct dwarf_section *section = &debug_displays [debug].section;
4443
4444 free ((char *) section->start);
4445 section->start = NULL;
4446 section->address = 0;
4447 section->size = 0;
4448 free ((char*) section->reloc_info);
4449 section->reloc_info = NULL;
4450 section->num_relocs= 0;
4451}
4452
4453void
4454close_debug_file (void * file)
4455{
4456 bfd * abfd = (bfd *) file;
4457
4458 bfd_close (abfd);
4459}
4460
4461void *
4462open_debug_file (const char * pathname)
4463{
4464 bfd * data;
4465
4466 data = bfd_openr (pathname, NULL);
4467 if (data == NULL)
4468 return NULL;
4469
4470 /* Decompress sections unless dumping the section contents. */
4471 if (!dump_section_contents || decompressed_dumps)
4472 data->flags |= BFD_DECOMPRESS;
4473
4474 if (! bfd_check_format (data, bfd_object))
4475 return NULL;
4476
4477 return data;
4478}
4479
4480static void
4481dump_dwarf_section (bfd *abfd, asection *section,
4482 void *arg)
4483{
4484 const char *name = bfd_section_name (section);
4485 const char *match;
4486 int i;
4487 bool is_mainfile = *(bool *) arg;
4488
4489 if (*name == 0)
4490 return;
4491
4492 if (!is_mainfile && !process_links
4493 && (section->flags & SEC_DEBUGGING) == 0)
4494 return;
4495
4496 if (startswith (name, ".gnu.linkonce.wi."))
4497 match = ".debug_info";
4498 else
4499 match = name;
4500
4501 for (i = 0; i < max; i++)
4502 if ((strcmp (debug_displays [i].section.uncompressed_name, match) == 0
4503 || strcmp (debug_displays [i].section.compressed_name, match) == 0
4504 || strcmp (debug_displays [i].section.xcoff_name, match) == 0)
4505 && debug_displays [i].enabled != NULL
4506 && *debug_displays [i].enabled)
4507 {
4508 struct dwarf_section *sec = &debug_displays [i].section;
4509
4510 if (strcmp (sec->uncompressed_name, match) == 0)
4511 sec->name = sec->uncompressed_name;
4512 else if (strcmp (sec->compressed_name, match) == 0)
4513 sec->name = sec->compressed_name;
4514 else
4515 sec->name = sec->xcoff_name;
4516 if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
4517 section, abfd))
4518 {
4519 debug_displays [i].display (sec, abfd);
4520
4521 if (i != info && i != abbrev)
4522 free_debug_section ((enum dwarf_section_display_enum) i);
4523 }
4524 break;
4525 }
4526}
4527
4528/* Dump the dwarf debugging information. */
4529
4530static void
4531dump_dwarf (bfd *abfd, bool is_mainfile)
4532{
4533 /* The byte_get pointer should have been set at the start of dump_bfd(). */
4534 if (byte_get == NULL)
4535 {
4536 warn (_("File %s does not contain any dwarf debug information\n"),
4537 bfd_get_filename (abfd));
4538 return;
4539 }
4540
4541 switch (bfd_get_arch (abfd))
4542 {
4543 case bfd_arch_s12z:
4544 /* S12Z has a 24 bit address space. But the only known
4545 producer of dwarf_info encodes addresses into 32 bits. */
4546 eh_addr_size = 4;
4547 break;
4548
4549 default:
4550 eh_addr_size = bfd_arch_bits_per_address (abfd) / 8;
4551 break;
4552 }
4553
4554 init_dwarf_regnames_by_bfd_arch_and_mach (bfd_get_arch (abfd),
4555 bfd_get_mach (abfd));
4556
4557 bfd_map_over_sections (abfd, dump_dwarf_section, (void *) &is_mainfile);
4558}
4559\f
4560/* Read ABFD's section SECT_NAME into *CONTENTS, and return a pointer to
4561 the section. Return NULL on failure. */
4562
4563static asection *
4564read_section (bfd *abfd, const char *sect_name, bfd_byte **contents)
4565{
4566 asection *sec;
4567
4568 *contents = NULL;
4569 sec = bfd_get_section_by_name (abfd, sect_name);
4570 if (sec == NULL)
4571 {
4572 printf (_("No %s section present\n\n"), sanitize_string (sect_name));
4573 return NULL;
4574 }
4575
4576 if ((bfd_section_flags (sec) & SEC_HAS_CONTENTS) == 0)
4577 bfd_set_error (bfd_error_no_contents);
4578 else if (bfd_malloc_and_get_section (abfd, sec, contents))
4579 return sec;
4580
4581 non_fatal (_("reading %s section of %s failed: %s"),
4582 sect_name, bfd_get_filename (abfd),
4583 bfd_errmsg (bfd_get_error ()));
4584 exit_status = 1;
4585 return NULL;
4586}
4587
4588/* Stabs entries use a 12 byte format:
4589 4 byte string table index
4590 1 byte stab type
4591 1 byte stab other field
4592 2 byte stab desc field
4593 4 byte stab value
4594 FIXME: This will have to change for a 64 bit object format. */
4595
4596#define STRDXOFF (0)
4597#define TYPEOFF (4)
4598#define OTHEROFF (5)
4599#define DESCOFF (6)
4600#define VALOFF (8)
4601#define STABSIZE (12)
4602
4603/* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
4604 using string table section STRSECT_NAME (in `strtab'). */
4605
4606static void
4607print_section_stabs (bfd *abfd,
4608 const char *stabsect_name,
4609 unsigned *string_offset_ptr)
4610{
4611 int i;
4612 unsigned file_string_table_offset = 0;
4613 unsigned next_file_string_table_offset = *string_offset_ptr;
4614 bfd_byte *stabp, *stabs_end;
4615
4616 stabp = stabs;
4617 stabs_end = PTR_ADD (stabp, stab_size);
4618
4619 printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name));
4620 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
4621
4622 /* Loop through all symbols and print them.
4623
4624 We start the index at -1 because there is a dummy symbol on
4625 the front of stabs-in-{coff,elf} sections that supplies sizes. */
4626 for (i = -1; (size_t) (stabs_end - stabp) >= STABSIZE; stabp += STABSIZE, i++)
4627 {
4628 const char *name;
4629 unsigned long strx;
4630 unsigned char type, other;
4631 unsigned short desc;
4632 bfd_vma value;
4633
4634 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
4635 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
4636 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
4637 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
4638 value = bfd_h_get_32 (abfd, stabp + VALOFF);
4639
4640 printf ("\n%-6d ", i);
4641 /* Either print the stab name, or, if unnamed, print its number
4642 again (makes consistent formatting for tools like awk). */
4643 name = bfd_get_stab_name (type);
4644 if (name != NULL)
4645 printf ("%-6s", sanitize_string (name));
4646 else if (type == N_UNDF)
4647 printf ("HdrSym");
4648 else
4649 printf ("%-6d", type);
4650 printf (" %-6d %-6d ", other, desc);
4651 bfd_printf_vma (abfd, value);
4652 printf (" %-6lu", strx);
4653
4654 /* Symbols with type == 0 (N_UNDF) specify the length of the
4655 string table associated with this file. We use that info
4656 to know how to relocate the *next* file's string table indices. */
4657 if (type == N_UNDF)
4658 {
4659 file_string_table_offset = next_file_string_table_offset;
4660 next_file_string_table_offset += value;
4661 }
4662 else
4663 {
4664 bfd_size_type amt = strx + file_string_table_offset;
4665
4666 /* Using the (possibly updated) string table offset, print the
4667 string (if any) associated with this symbol. */
4668 if (amt < stabstr_size)
4669 /* PR 17512: file: 079-79389-0.001:0.1.
4670 FIXME: May need to sanitize this string before displaying. */
4671 printf (" %.*s", (int)(stabstr_size - amt), strtab + amt);
4672 else
4673 printf (" *");
4674 }
4675 }
4676 printf ("\n\n");
4677 *string_offset_ptr = next_file_string_table_offset;
4678}
4679
4680typedef struct
4681{
4682 const char * section_name;
4683 const char * string_section_name;
4684 unsigned string_offset;
4685}
4686stab_section_names;
4687
4688static void
4689find_stabs_section (bfd *abfd, asection *section, void *names)
4690{
4691 int len;
4692 stab_section_names * sought = (stab_section_names *) names;
4693
4694 /* Check for section names for which stabsect_name is a prefix, to
4695 handle .stab.N, etc. */
4696 len = strlen (sought->section_name);
4697
4698 /* If the prefix matches, and the files section name ends with a
4699 nul or a digit, then we match. I.e., we want either an exact
4700 match or a section followed by a number. */
4701 if (strncmp (sought->section_name, section->name, len) == 0
4702 && (section->name[len] == 0
4703 || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
4704 {
4705 asection *s;
4706 if (strtab == NULL)
4707 {
4708 s = read_section (abfd, sought->string_section_name, &strtab);
4709 if (s != NULL)
4710 stabstr_size = bfd_section_size (s);
4711 }
4712
4713 if (strtab)
4714 {
4715 s = read_section (abfd, section->name, &stabs);
4716 if (s != NULL)
4717 {
4718 stab_size = bfd_section_size (s);
4719 print_section_stabs (abfd, section->name, &sought->string_offset);
4720 free (stabs);
4721 }
4722 }
4723 }
4724}
4725
4726static void
4727dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
4728{
4729 stab_section_names s;
4730
4731 s.section_name = stabsect_name;
4732 s.string_section_name = strsect_name;
4733 s.string_offset = 0;
4734
4735 bfd_map_over_sections (abfd, find_stabs_section, & s);
4736
4737 free (strtab);
4738 strtab = NULL;
4739}
4740
4741/* Dump the any sections containing stabs debugging information. */
4742
4743static void
4744dump_stabs (bfd *abfd)
4745{
4746 dump_stabs_section (abfd, ".stab", ".stabstr");
4747 dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
4748 dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
4749
4750 /* For Darwin. */
4751 dump_stabs_section (abfd, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
4752
4753 dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
4754}
4755\f
4756static void
4757dump_bfd_header (bfd *abfd)
4758{
4759 char *comma = "";
4760
4761 printf (_("architecture: %s, "),
4762 bfd_printable_arch_mach (bfd_get_arch (abfd),
4763 bfd_get_mach (abfd)));
4764 printf (_("flags 0x%08x:\n"), abfd->flags & ~BFD_FLAGS_FOR_BFD_USE_MASK);
4765
4766#define PF(x, y) if (abfd->flags & x) {printf ("%s%s", comma, y); comma=", ";}
4767 PF (HAS_RELOC, "HAS_RELOC");
4768 PF (EXEC_P, "EXEC_P");
4769 PF (HAS_LINENO, "HAS_LINENO");
4770 PF (HAS_DEBUG, "HAS_DEBUG");
4771 PF (HAS_SYMS, "HAS_SYMS");
4772 PF (HAS_LOCALS, "HAS_LOCALS");
4773 PF (DYNAMIC, "DYNAMIC");
4774 PF (WP_TEXT, "WP_TEXT");
4775 PF (D_PAGED, "D_PAGED");
4776 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
4777 printf (_("\nstart address 0x"));
4778 bfd_printf_vma (abfd, abfd->start_address);
4779 printf ("\n");
4780}
4781\f
4782
4783#ifdef ENABLE_LIBCTF
4784/* Formatting callback function passed to ctf_dump. Returns either the pointer
4785 it is passed, or a pointer to newly-allocated storage, in which case
4786 dump_ctf() will free it when it no longer needs it. */
4787
4788static char *
4789dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
4790 char *s, void *arg)
4791{
4792 const char *blanks = arg;
4793 return xasprintf ("%s%s", blanks, s);
4794}
4795
4796/* Make a ctfsect suitable for ctf_bfdopen_ctfsect(). */
4797static ctf_sect_t
4798make_ctfsect (const char *name, bfd_byte *data,
4799 bfd_size_type size)
4800{
4801 ctf_sect_t ctfsect;
4802
4803 ctfsect.cts_name = name;
4804 ctfsect.cts_entsize = 1;
4805 ctfsect.cts_size = size;
4806 ctfsect.cts_data = data;
4807
4808 return ctfsect;
4809}
4810
4811/* Dump CTF errors/warnings. */
4812static void
4813dump_ctf_errs (ctf_dict_t *fp)
4814{
4815 ctf_next_t *it = NULL;
4816 char *errtext;
4817 int is_warning;
4818 int err;
4819
4820 /* Dump accumulated errors and warnings. */
4821 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
4822 {
4823 non_fatal (_("%s: %s"), is_warning ? _("warning"): _("error"),
4824 errtext);
4825 free (errtext);
4826 }
4827 if (err != ECTF_NEXT_END)
4828 {
4829 non_fatal (_("CTF error: cannot get CTF errors: `%s'"),
4830 ctf_errmsg (err));
4831 }
4832}
4833
4834/* Dump one CTF archive member. */
4835
4836static void
4837dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
4838 size_t member)
4839{
4840 const char *things[] = {"Header", "Labels", "Data objects",
4841 "Function objects", "Variables", "Types", "Strings",
4842 ""};
4843 const char **thing;
4844 size_t i;
4845
4846 /* Don't print out the name of the default-named archive member if it appears
4847 first in the list. The name .ctf appears everywhere, even for things that
4848 aren't really archives, so printing it out is liable to be confusing; also,
4849 the common case by far is for only one archive member to exist, and hiding
4850 it in that case seems worthwhile. */
4851
4852 if (strcmp (name, ".ctf") != 0 || member != 0)
4853 printf (_("\nCTF archive member: %s:\n"), sanitize_string (name));
4854
4855 if (ctf_parent_name (ctf) != NULL)
4856 ctf_import (ctf, parent);
4857
4858 for (i = 0, thing = things; *thing[0]; thing++, i++)
4859 {
4860 ctf_dump_state_t *s = NULL;
4861 char *item;
4862
4863 printf ("\n %s:\n", *thing);
4864 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
4865 (void *) " ")) != NULL)
4866 {
4867 printf ("%s\n", item);
4868 free (item);
4869 }
4870
4871 if (ctf_errno (ctf))
4872 {
4873 non_fatal (_("Iteration failed: %s, %s"), *thing,
4874 ctf_errmsg (ctf_errno (ctf)));
4875 break;
4876 }
4877 }
4878
4879 dump_ctf_errs (ctf);
4880}
4881
4882/* Dump the CTF debugging information. */
4883
4884static void
4885dump_ctf (bfd *abfd, const char *sect_name, const char *parent_name,
4886 const char *parent_sect_name)
4887{
4888 asection *sec, *psec = NULL;
4889 ctf_archive_t *ctfa;
4890 ctf_archive_t *ctfpa = NULL;
4891 bfd_byte *ctfdata = NULL;
4892 bfd_byte *ctfpdata = NULL;
4893 ctf_sect_t ctfsect;
4894 ctf_dict_t *parent;
4895 ctf_dict_t *fp;
4896 ctf_next_t *i = NULL;
4897 const char *name;
4898 size_t member = 0;
4899 int err;
4900
4901 if (sect_name == NULL)
4902 sect_name = ".ctf";
4903
4904 sec = read_section (abfd, sect_name, &ctfdata);
4905 if (sec == NULL)
4906 {
4907 my_bfd_nonfatal (bfd_get_filename (abfd));
4908 return;
4909 }
4910
4911 /* Load the CTF file and dump it. Preload the parent dict, since it will
4912 need to be imported into every child in turn. The parent dict may come
4913 from a different section entirely. */
4914
4915 ctfsect = make_ctfsect (sect_name, ctfdata, bfd_section_size (sec));
4916 if ((ctfa = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
4917 {
4918 dump_ctf_errs (NULL);
4919 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
4920 my_bfd_nonfatal (bfd_get_filename (abfd));
4921 free (ctfdata);
4922 return;
4923 }
4924
4925 if (parent_sect_name)
4926 {
4927 psec = read_section (abfd, parent_sect_name, &ctfpdata);
4928 if (sec == NULL)
4929 {
4930 my_bfd_nonfatal (bfd_get_filename (abfd));
4931 free (ctfdata);
4932 return;
4933 }
4934
4935 ctfsect = make_ctfsect (parent_sect_name, ctfpdata, bfd_section_size (psec));
4936 if ((ctfpa = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
4937 {
4938 dump_ctf_errs (NULL);
4939 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
4940 my_bfd_nonfatal (bfd_get_filename (abfd));
4941 free (ctfdata);
4942 free (ctfpdata);
4943 return;
4944 }
4945 }
4946 else
4947 ctfpa = ctfa;
4948
4949 if ((parent = ctf_dict_open (ctfpa, parent_name, &err)) == NULL)
4950 {
4951 dump_ctf_errs (NULL);
4952 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
4953 my_bfd_nonfatal (bfd_get_filename (abfd));
4954 ctf_close (ctfa);
4955 free (ctfdata);
4956 free (ctfpdata);
4957 return;
4958 }
4959
4960 printf (_("Contents of CTF section %s:\n"), sanitize_string (sect_name));
4961
4962 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
4963 {
4964 dump_ctf_archive_member (fp, name, parent, member++);
4965 ctf_dict_close (fp);
4966 }
4967 if (err != ECTF_NEXT_END)
4968 {
4969 dump_ctf_errs (NULL);
4970 non_fatal (_("CTF archive member open failure: %s"), ctf_errmsg (err));
4971 my_bfd_nonfatal (bfd_get_filename (abfd));
4972 }
4973 ctf_dict_close (parent);
4974 ctf_close (ctfa);
4975 free (ctfdata);
4976 if (parent_sect_name)
4977 {
4978 ctf_close (ctfpa);
4979 free (ctfpdata);
4980 }
4981}
4982#else
4983static void
4984dump_ctf (bfd *abfd ATTRIBUTE_UNUSED, const char *sect_name ATTRIBUTE_UNUSED,
4985 const char *parent_name ATTRIBUTE_UNUSED,
4986 const char *parent_sect_name ATTRIBUTE_UNUSED) {}
4987#endif
4988
4989static void
4990dump_section_sframe (bfd *abfd ATTRIBUTE_UNUSED,
4991 const char * sect_name)
4992{
4993 asection *sec;
4994 sframe_decoder_ctx *sfd_ctx = NULL;
4995 bfd_size_type sf_size;
4996 bfd_byte *sframe_data;
4997 bfd_vma sf_vma;
4998 int err = 0;
4999
5000 if (sect_name == NULL)
5001 sect_name = ".sframe";
5002
5003 sec = read_section (abfd, sect_name, &sframe_data);
5004 if (sec == NULL)
5005 {
5006 my_bfd_nonfatal (bfd_get_filename (abfd));
5007 return;
5008 }
5009 sf_size = bfd_section_size (sec);
5010 sf_vma = bfd_section_vma (sec);
5011
5012 /* Decode the contents of the section. */
5013 sfd_ctx = sframe_decode ((const char*)sframe_data, sf_size, &err);
5014 if (!sfd_ctx)
5015 {
5016 my_bfd_nonfatal (bfd_get_filename (abfd));
5017 free (sframe_data);
5018 return;
5019 }
5020
5021 printf (_("Contents of the SFrame section %s:"),
5022 sanitize_string (sect_name));
5023 /* Dump the contents as text. */
5024 dump_sframe (sfd_ctx, sf_vma);
5025
5026 sframe_decoder_free (&sfd_ctx);
5027 free (sframe_data);
5028}
5029
5030\f
5031static void
5032dump_bfd_private_header (bfd *abfd)
5033{
5034 if (!bfd_print_private_bfd_data (abfd, stdout))
5035 non_fatal (_("warning: private headers incomplete: %s"),
5036 bfd_errmsg (bfd_get_error ()));
5037}
5038
5039static void
5040dump_target_specific (bfd *abfd)
5041{
5042 const struct objdump_private_desc * const *desc;
5043 struct objdump_private_option *opt;
5044 char *e, *b;
5045
5046 /* Find the desc. */
5047 for (desc = objdump_private_vectors; *desc != NULL; desc++)
5048 if ((*desc)->filter (abfd))
5049 break;
5050
5051 if (*desc == NULL)
5052 {
5053 non_fatal (_("option -P/--private not supported by this file"));
5054 return;
5055 }
5056
5057 /* Clear all options. */
5058 for (opt = (*desc)->options; opt->name; opt++)
5059 opt->selected = false;
5060
5061 /* Decode options. */
5062 b = dump_private_options;
5063 do
5064 {
5065 e = strchr (b, ',');
5066
5067 if (e)
5068 *e = 0;
5069
5070 for (opt = (*desc)->options; opt->name; opt++)
5071 if (strcmp (opt->name, b) == 0)
5072 {
5073 opt->selected = true;
5074 break;
5075 }
5076 if (opt->name == NULL)
5077 non_fatal (_("target specific dump '%s' not supported"), b);
5078
5079 if (e)
5080 {
5081 *e = ',';
5082 b = e + 1;
5083 }
5084 }
5085 while (e != NULL);
5086
5087 /* Dump. */
5088 (*desc)->dump (abfd);
5089}
5090\f
5091/* Display a section in hexadecimal format with associated characters.
5092 Each line prefixed by the zero padded address. */
5093
5094static void
5095dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
5096{
5097 bfd_byte *data = NULL;
5098 bfd_size_type datasize;
5099 bfd_vma addr_offset;
5100 bfd_vma start_offset;
5101 bfd_vma stop_offset;
5102 unsigned int opb = bfd_octets_per_byte (abfd, section);
5103 /* Bytes per line. */
5104 const int onaline = 16;
5105 char buf[64];
5106 int count;
5107 int width;
5108
5109 if (only_list == NULL)
5110 {
5111 if ((section->flags & SEC_HAS_CONTENTS) == 0)
5112 return;
5113 }
5114 else if (!process_section_p (section))
5115 return;
5116
5117 if ((datasize = bfd_section_size (section)) == 0)
5118 return;
5119
5120 /* Compute the address range to display. */
5121 if (start_address == (bfd_vma) -1
5122 || start_address < section->vma)
5123 start_offset = 0;
5124 else
5125 start_offset = start_address - section->vma;
5126
5127 if (stop_address == (bfd_vma) -1)
5128 stop_offset = datasize / opb;
5129 else
5130 {
5131 if (stop_address < section->vma)
5132 stop_offset = 0;
5133 else
5134 stop_offset = stop_address - section->vma;
5135
5136 if (stop_offset > datasize / opb)
5137 stop_offset = datasize / opb;
5138 }
5139
5140 if (start_offset >= stop_offset)
5141 return;
5142
5143 printf (_("Contents of section %s:"), sanitize_string (section->name));
5144 if (display_file_offsets)
5145 printf (_(" (Starting at file offset: 0x%lx)"),
5146 (unsigned long) (section->filepos + start_offset));
5147 printf ("\n");
5148
5149 if (bfd_is_section_compressed (abfd, section) && ! decompressed_dumps)
5150 printf (_(" NOTE: This section is compressed, but its contents have NOT been expanded for this dump.\n"));
5151
5152 if (!bfd_get_full_section_contents (abfd, section, &data))
5153 {
5154 non_fatal (_("Reading section %s failed because: %s"),
5155 section->name, bfd_errmsg (bfd_get_error ()));
5156 return;
5157 }
5158
5159 width = 4;
5160
5161 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
5162 if (strlen (buf) >= sizeof (buf))
5163 abort ();
5164
5165 count = 0;
5166 while (buf[count] == '0' && buf[count+1] != '\0')
5167 count++;
5168 count = strlen (buf) - count;
5169 if (count > width)
5170 width = count;
5171
5172 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
5173 if (strlen (buf) >= sizeof (buf))
5174 abort ();
5175
5176 count = 0;
5177 while (buf[count] == '0' && buf[count+1] != '\0')
5178 count++;
5179 count = strlen (buf) - count;
5180 if (count > width)
5181 width = count;
5182
5183 for (addr_offset = start_offset;
5184 addr_offset < stop_offset; addr_offset += onaline / opb)
5185 {
5186 bfd_size_type j;
5187
5188 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
5189 count = strlen (buf);
5190 if ((size_t) count >= sizeof (buf))
5191 abort ();
5192
5193 putchar (' ');
5194 while (count < width)
5195 {
5196 putchar ('0');
5197 count++;
5198 }
5199 fputs (buf + count - width, stdout);
5200 putchar (' ');
5201
5202 for (j = addr_offset * opb;
5203 j < addr_offset * opb + onaline; j++)
5204 {
5205 if (j < stop_offset * opb)
5206 printf ("%02x", (unsigned) (data[j]));
5207 else
5208 printf (" ");
5209 if ((j & 3) == 3)
5210 printf (" ");
5211 }
5212
5213 printf (" ");
5214 for (j = addr_offset * opb;
5215 j < addr_offset * opb + onaline; j++)
5216 {
5217 if (j >= stop_offset * opb)
5218 printf (" ");
5219 else
5220 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
5221 }
5222 putchar ('\n');
5223 }
5224 free (data);
5225}
5226
5227/* Actually display the various requested regions. */
5228
5229static void
5230dump_data (bfd *abfd)
5231{
5232 bfd_map_over_sections (abfd, dump_section, NULL);
5233}
5234
5235/* Should perhaps share code and display with nm? */
5236
5237static void
5238dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bool dynamic)
5239{
5240 asymbol **current;
5241 long max_count;
5242 long count;
5243
5244 if (dynamic)
5245 {
5246 current = dynsyms;
5247 max_count = dynsymcount;
5248 printf ("DYNAMIC SYMBOL TABLE:\n");
5249 }
5250 else
5251 {
5252 current = syms;
5253 max_count = symcount;
5254 printf ("SYMBOL TABLE:\n");
5255 }
5256
5257 if (max_count == 0)
5258 printf (_("no symbols\n"));
5259
5260 for (count = 0; count < max_count; count++)
5261 {
5262 bfd *cur_bfd;
5263
5264 if (*current == NULL)
5265 printf (_("no information for symbol number %ld\n"), count);
5266
5267 else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
5268 printf (_("could not determine the type of symbol number %ld\n"),
5269 count);
5270
5271 else if (process_section_p ((* current)->section)
5272 && (dump_special_syms
5273 || !bfd_is_target_special_symbol (cur_bfd, *current)))
5274 {
5275 const char *name = (*current)->name;
5276
5277 if (do_demangle && name != NULL && *name != '\0')
5278 {
5279 char *alloc;
5280
5281 /* If we want to demangle the name, we demangle it
5282 here, and temporarily clobber it while calling
5283 bfd_print_symbol. FIXME: This is a gross hack. */
5284 alloc = bfd_demangle (cur_bfd, name, demangle_flags);
5285 if (alloc != NULL)
5286 (*current)->name = alloc;
5287 bfd_print_symbol (cur_bfd, stdout, *current,
5288 bfd_print_symbol_all);
5289 if (alloc != NULL)
5290 {
5291 (*current)->name = name;
5292 free (alloc);
5293 }
5294 }
5295 else if (unicode_display != unicode_default
5296 && name != NULL && *name != '\0')
5297 {
5298 const char * sanitized_name;
5299
5300 /* If we want to sanitize the name, we do it here, and
5301 temporarily clobber it while calling bfd_print_symbol.
5302 FIXME: This is a gross hack. */
5303 sanitized_name = sanitize_string (name);
5304 if (sanitized_name != name)
5305 (*current)->name = sanitized_name;
5306 else
5307 sanitized_name = NULL;
5308 bfd_print_symbol (cur_bfd, stdout, *current,
5309 bfd_print_symbol_all);
5310 if (sanitized_name != NULL)
5311 (*current)->name = name;
5312 }
5313 else
5314 bfd_print_symbol (cur_bfd, stdout, *current,
5315 bfd_print_symbol_all);
5316 printf ("\n");
5317 }
5318
5319 current++;
5320 }
5321 printf ("\n\n");
5322}
5323\f
5324static void
5325dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
5326{
5327 arelent **p;
5328 char *last_filename, *last_functionname;
5329 unsigned int last_line;
5330 unsigned int last_discriminator;
5331
5332 /* Get column headers lined up reasonably. */
5333 {
5334 static int width;
5335
5336 if (width == 0)
5337 {
5338 char buf[30];
5339
5340 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
5341 width = strlen (buf) - 7;
5342 }
5343 printf ("OFFSET %*s TYPE %*s VALUE\n", width, "", 12, "");
5344 }
5345
5346 last_filename = NULL;
5347 last_functionname = NULL;
5348 last_line = 0;
5349 last_discriminator = 0;
5350
5351 for (p = relpp; relcount && *p != NULL; p++, relcount--)
5352 {
5353 arelent *q = *p;
5354 const char *filename, *functionname;
5355 unsigned int linenumber;
5356 unsigned int discriminator;
5357 const char *sym_name;
5358 const char *section_name;
5359 bfd_vma addend2 = 0;
5360
5361 if (start_address != (bfd_vma) -1
5362 && q->address < start_address)
5363 continue;
5364 if (stop_address != (bfd_vma) -1
5365 && q->address > stop_address)
5366 continue;
5367
5368 if (with_line_numbers
5369 && sec != NULL
5370 && bfd_find_nearest_line_discriminator (abfd, sec, syms, q->address,
5371 &filename, &functionname,
5372 &linenumber, &discriminator))
5373 {
5374 if (functionname != NULL
5375 && (last_functionname == NULL
5376 || strcmp (functionname, last_functionname) != 0))
5377 {
5378 printf ("%s():\n", sanitize_string (functionname));
5379 if (last_functionname != NULL)
5380 free (last_functionname);
5381 last_functionname = xstrdup (functionname);
5382 }
5383
5384 if (linenumber > 0
5385 && (linenumber != last_line
5386 || (filename != NULL
5387 && last_filename != NULL
5388 && filename_cmp (filename, last_filename) != 0)
5389 || (discriminator != last_discriminator)))
5390 {
5391 if (discriminator > 0)
5392 printf ("%s:%u\n", filename == NULL ? "???" :
5393 sanitize_string (filename), linenumber);
5394 else
5395 printf ("%s:%u (discriminator %u)\n",
5396 filename == NULL ? "???" : sanitize_string (filename),
5397 linenumber, discriminator);
5398 last_line = linenumber;
5399 last_discriminator = discriminator;
5400 if (last_filename != NULL)
5401 free (last_filename);
5402 if (filename == NULL)
5403 last_filename = NULL;
5404 else
5405 last_filename = xstrdup (filename);
5406 }
5407 }
5408
5409 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
5410 {
5411 sym_name = (*(q->sym_ptr_ptr))->name;
5412 section_name = (*(q->sym_ptr_ptr))->section->name;
5413 }
5414 else
5415 {
5416 sym_name = NULL;
5417 section_name = NULL;
5418 }
5419
5420 bfd_printf_vma (abfd, q->address);
5421 if (q->howto == NULL)
5422 printf (" *unknown* ");
5423 else if (q->howto->name)
5424 {
5425 const char *name = q->howto->name;
5426
5427 /* R_SPARC_OLO10 relocations contain two addends.
5428 But because 'arelent' lacks enough storage to
5429 store them both, the 64-bit ELF Sparc backend
5430 records this as two relocations. One R_SPARC_LO10
5431 and one R_SPARC_13, both pointing to the same
5432 address. This is merely so that we have some
5433 place to store both addend fields.
5434
5435 Undo this transformation, otherwise the output
5436 will be confusing. */
5437 if (abfd->xvec->flavour == bfd_target_elf_flavour
5438 && elf_tdata (abfd)->elf_header->e_machine == EM_SPARCV9
5439 && relcount > 1
5440 && !strcmp (q->howto->name, "R_SPARC_LO10"))
5441 {
5442 arelent *q2 = *(p + 1);
5443 if (q2 != NULL
5444 && q2->howto
5445 && q->address == q2->address
5446 && !strcmp (q2->howto->name, "R_SPARC_13"))
5447 {
5448 name = "R_SPARC_OLO10";
5449 addend2 = q2->addend;
5450 p++;
5451 }
5452 }
5453 printf (" %-16s ", name);
5454 }
5455 else
5456 printf (" %-16d ", q->howto->type);
5457
5458 if (sym_name)
5459 {
5460 objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
5461 }
5462 else
5463 {
5464 if (section_name == NULL)
5465 section_name = "*unknown*";
5466 printf ("[%s]", sanitize_string (section_name));
5467 }
5468
5469 if (q->addend)
5470 {
5471 bfd_signed_vma addend = q->addend;
5472 if (addend < 0)
5473 {
5474 printf ("-0x");
5475 addend = -addend;
5476 }
5477 else
5478 printf ("+0x");
5479 bfd_printf_vma (abfd, addend);
5480 }
5481 if (addend2)
5482 {
5483 printf ("+0x");
5484 bfd_printf_vma (abfd, addend2);
5485 }
5486
5487 printf ("\n");
5488 }
5489
5490 if (last_filename != NULL)
5491 free (last_filename);
5492 if (last_functionname != NULL)
5493 free (last_functionname);
5494}
5495
5496static void
5497dump_relocs_in_section (bfd *abfd,
5498 asection *section,
5499 void *counter)
5500{
5501 arelent **relpp;
5502 long relcount;
5503 long relsize;
5504
5505 if ( bfd_is_abs_section (section)
5506 || bfd_is_und_section (section)
5507 || bfd_is_com_section (section)
5508 || (! process_section_p (section))
5509 || ((section->flags & SEC_RELOC) == 0))
5510 return;
5511
5512 printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section->name));
5513
5514 relsize = bfd_get_reloc_upper_bound (abfd, section);
5515 if (relsize == 0)
5516 {
5517 printf (" (none)\n\n");
5518 return;
5519 }
5520
5521 if (relsize < 0)
5522 {
5523 relpp = NULL;
5524 relcount = relsize;
5525 }
5526 else
5527 {
5528 relpp = (arelent **) xmalloc (relsize);
5529 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
5530 }
5531
5532 if (relcount < 0)
5533 {
5534 printf ("\n");
5535 non_fatal (_("failed to read relocs in: %s"),
5536 sanitize_string (bfd_get_filename (abfd)));
5537 my_bfd_nonfatal (_("error message was"));
5538 }
5539 else if (relcount == 0)
5540 printf (" (none)\n\n");
5541 else
5542 {
5543 printf ("\n");
5544 dump_reloc_set (abfd, section, relpp, relcount);
5545 printf ("\n\n");
5546 }
5547 free (relpp);
5548
5549 * ((unsigned int *) counter) += 1;
5550}
5551
5552static void
5553is_relr_section (bfd *abfd ATTRIBUTE_UNUSED,
5554 asection * section, void *data)
5555{
5556 if (section->flags & SEC_LINKER_CREATED)
5557 return;
5558
5559 struct bfd_elf_section_data * esd = elf_section_data (section);
5560 if (esd == NULL)
5561 return;
5562
5563 if (esd->this_hdr.sh_type == SHT_RELR)
5564 * ((bool *) data) = true;
5565}
5566
5567static bool
5568contains_relr_relocs (bfd *abfd)
5569{
5570 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
5571 return false;
5572
5573 bool result = false;
5574
5575 bfd_map_over_sections (abfd, is_relr_section, &result);
5576
5577 return result;
5578}
5579
5580static void
5581dump_relocs (bfd *abfd)
5582{
5583 unsigned int counter = 0;
5584
5585 bfd_map_over_sections (abfd, dump_relocs_in_section, & counter);
5586
5587 if (counter == 0 && contains_relr_relocs (abfd))
5588 {
5589 printf (_("%s: This file does not contain any ordinary relocations.\n"),
5590 sanitize_string (bfd_get_filename (abfd)));
5591
5592 printf (_("%s: It does however contain RELR relocations. These can be displayed by the readelf program\n"),
5593 sanitize_string (bfd_get_filename (abfd)));
5594 }
5595}
5596
5597static void
5598dump_dynamic_relocs (bfd *abfd)
5599{
5600 long relsize;
5601 arelent **relpp = NULL;
5602 long relcount;
5603
5604 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
5605
5606 printf ("DYNAMIC RELOCATION RECORDS");
5607
5608 if (relsize <= 0)
5609 goto none;
5610
5611 relpp = (arelent **) xmalloc (relsize);
5612 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
5613
5614 if (relcount < 0)
5615 {
5616 printf ("\n");
5617 non_fatal (_("failed to read relocs in: %s"),
5618 sanitize_string (bfd_get_filename (abfd)));
5619 my_bfd_nonfatal (_("error message was"));
5620 }
5621 else if (relcount == 0)
5622 goto none;
5623 else
5624 {
5625 printf ("\n");
5626 dump_reloc_set (abfd, NULL, relpp, relcount);
5627 printf ("\n\n");
5628 }
5629 free (relpp);
5630 return;
5631
5632 none:
5633 printf (" (none)\n\n");
5634
5635 if (contains_relr_relocs (abfd))
5636 printf (_("%s: contains RELR relocations which are not displayed by %s.\n\
5637These can be displayed by the readelf program instead.\n"),
5638 sanitize_string (bfd_get_filename (abfd)),
5639 program_name);
5640
5641 free (relpp);
5642}
5643
5644/* Creates a table of paths, to search for source files. */
5645
5646static void
5647add_include_path (const char *path)
5648{
5649 if (path[0] == 0)
5650 return;
5651 include_path_count++;
5652 include_paths = (const char **)
5653 xrealloc (include_paths, include_path_count * sizeof (*include_paths));
5654#ifdef HAVE_DOS_BASED_FILE_SYSTEM
5655 if (path[1] == ':' && path[2] == 0)
5656 path = concat (path, ".", (const char *) 0);
5657#endif
5658 include_paths[include_path_count - 1] = path;
5659}
5660
5661static void
5662adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
5663 asection *section,
5664 void *arg)
5665{
5666 if ((section->flags & SEC_DEBUGGING) == 0)
5667 {
5668 bool *has_reloc_p = (bool *) arg;
5669 section->vma += adjust_section_vma;
5670 if (*has_reloc_p)
5671 section->lma += adjust_section_vma;
5672 }
5673}
5674
5675/* Return the sign-extended form of an ARCH_SIZE sized VMA. */
5676
5677static bfd_vma
5678sign_extend_address (bfd *abfd ATTRIBUTE_UNUSED,
5679 bfd_vma vma,
5680 unsigned arch_size)
5681{
5682 bfd_vma mask;
5683 mask = (bfd_vma) 1 << (arch_size - 1);
5684 return (((vma & ((mask << 1) - 1)) ^ mask) - mask);
5685}
5686
5687static bool
5688might_need_separate_debug_info (bool is_mainfile)
5689{
5690 /* We do not follow links from debug info files. */
5691 if (! is_mainfile)
5692 return false;
5693
5694 /* Since do_follow_links might be enabled by default, only treat it as an
5695 indication that separate files should be loaded if setting it was a
5696 deliberate user action. */
5697 if (DEFAULT_FOR_FOLLOW_LINKS == 0 && do_follow_links)
5698 return true;
5699
5700 if (process_links || dump_symtab || dump_debugging
5701 || dump_dwarf_section_info || with_source_code)
5702 return true;
5703
5704 return false;
5705}
5706
5707/* Dump selected contents of ABFD. */
5708
5709static void
5710dump_bfd (bfd *abfd, bool is_mainfile)
5711{
5712 const struct elf_backend_data * bed;
5713
5714 if (bfd_big_endian (abfd))
5715 byte_get = byte_get_big_endian;
5716 else if (bfd_little_endian (abfd))
5717 byte_get = byte_get_little_endian;
5718 else
5719 byte_get = NULL;
5720
5721 /* Load any separate debug information files. */
5722 if (byte_get != NULL && might_need_separate_debug_info (is_mainfile))
5723 {
5724 load_separate_debug_files (abfd, bfd_get_filename (abfd));
5725
5726 /* If asked to do so, recursively dump the separate files. */
5727 if (do_follow_links)
5728 {
5729 separate_info * i;
5730
5731 for (i = first_separate_info; i != NULL; i = i->next)
5732 dump_bfd (i->handle, false);
5733 }
5734 }
5735
5736 /* Adjust user-specified start and stop limits for targets that use
5737 signed addresses. */
5738 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
5739 && (bed = get_elf_backend_data (abfd)) != NULL
5740 && bed->sign_extend_vma)
5741 {
5742 start_address = sign_extend_address (abfd, start_address,
5743 bed->s->arch_size);
5744 stop_address = sign_extend_address (abfd, stop_address,
5745 bed->s->arch_size);
5746 }
5747
5748 /* If we are adjusting section VMA's, change them all now. Changing
5749 the BFD information is a hack. However, we must do it, or
5750 bfd_find_nearest_line will not do the right thing. */
5751 if (adjust_section_vma != 0)
5752 {
5753 bool has_reloc = (abfd->flags & HAS_RELOC);
5754 bfd_map_over_sections (abfd, adjust_addresses, &has_reloc);
5755 }
5756
5757 if (is_mainfile || process_links)
5758 {
5759 if (! dump_debugging_tags && ! suppress_bfd_header)
5760 printf (_("\n%s: file format %s\n"),
5761 sanitize_string (bfd_get_filename (abfd)),
5762 abfd->xvec->name);
5763 if (dump_ar_hdrs)
5764 print_arelt_descr (stdout, abfd, true, false);
5765 if (dump_file_header)
5766 dump_bfd_header (abfd);
5767 if (dump_private_headers)
5768 dump_bfd_private_header (abfd);
5769 if (dump_private_options != NULL)
5770 dump_target_specific (abfd);
5771 if (! dump_debugging_tags && ! suppress_bfd_header)
5772 putchar ('\n');
5773 }
5774
5775 if (dump_symtab
5776 || dump_reloc_info
5777 || disassemble
5778 || dump_debugging
5779 || dump_dwarf_section_info)
5780 {
5781 syms = slurp_symtab (abfd);
5782
5783 /* If following links, load any symbol tables from the linked files as well. */
5784 if (do_follow_links && is_mainfile)
5785 {
5786 separate_info * i;
5787
5788 for (i = first_separate_info; i != NULL; i = i->next)
5789 {
5790 asymbol ** extra_syms;
5791 long old_symcount = symcount;
5792
5793 extra_syms = slurp_symtab (i->handle);
5794
5795 if (extra_syms)
5796 {
5797 if (old_symcount == 0)
5798 {
5799 free (syms);
5800 syms = extra_syms;
5801 }
5802 else
5803 {
5804 syms = xrealloc (syms, ((symcount + old_symcount + 1)
5805 * sizeof (asymbol *)));
5806 memcpy (syms + old_symcount,
5807 extra_syms,
5808 (symcount + 1) * sizeof (asymbol *));
5809 free (extra_syms);
5810 }
5811 }
5812
5813 symcount += old_symcount;
5814 }
5815 }
5816 }
5817
5818 if (is_mainfile || process_links)
5819 {
5820 if (dump_section_headers)
5821 dump_headers (abfd);
5822
5823 if (dump_dynamic_symtab || dump_dynamic_reloc_info
5824 || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
5825 dynsyms = slurp_dynamic_symtab (abfd);
5826
5827 if (disassemble)
5828 {
5829 synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
5830 dynsymcount, dynsyms,
5831 &synthsyms);
5832 if (synthcount < 0)
5833 synthcount = 0;
5834 }
5835
5836 if (dump_symtab)
5837 dump_symbols (abfd, false);
5838 if (dump_dynamic_symtab)
5839 dump_symbols (abfd, true);
5840 }
5841 if (dump_dwarf_section_info)
5842 dump_dwarf (abfd, is_mainfile);
5843 if (is_mainfile || process_links)
5844 {
5845 if (dump_ctf_section_info)
5846 dump_ctf (abfd, dump_ctf_section_name, dump_ctf_parent_name,
5847 dump_ctf_parent_section_name);
5848 if (dump_sframe_section_info)
5849 dump_section_sframe (abfd, dump_sframe_section_name);
5850 if (dump_stab_section_info)
5851 dump_stabs (abfd);
5852 if (dump_reloc_info && ! disassemble)
5853 dump_relocs (abfd);
5854 if (dump_dynamic_reloc_info && ! disassemble)
5855 dump_dynamic_relocs (abfd);
5856 if (dump_section_contents)
5857 dump_data (abfd);
5858 if (disassemble)
5859 disassemble_data (abfd);
5860 }
5861
5862 if (dump_debugging)
5863 {
5864 void *dhandle;
5865
5866 dhandle = read_debugging_info (abfd, syms, symcount, true);
5867 if (dhandle != NULL)
5868 {
5869 if (!print_debugging_info (stdout, dhandle, abfd, syms,
5870 bfd_demangle,
5871 dump_debugging_tags != 0))
5872 {
5873 non_fatal (_("%s: printing debugging information failed"),
5874 bfd_get_filename (abfd));
5875 exit_status = 1;
5876 }
5877 }
5878 /* PR 6483: If there was no STABS debug info in the file, try
5879 DWARF instead. */
5880 else if (! dump_dwarf_section_info)
5881 {
5882 dwarf_select_sections_all ();
5883 dump_dwarf (abfd, is_mainfile);
5884 }
5885 }
5886
5887 if (syms)
5888 {
5889 free (syms);
5890 syms = NULL;
5891 }
5892
5893 if (dynsyms)
5894 {
5895 free (dynsyms);
5896 dynsyms = NULL;
5897 }
5898
5899 if (synthsyms)
5900 {
5901 free (synthsyms);
5902 synthsyms = NULL;
5903 }
5904
5905 symcount = 0;
5906 dynsymcount = 0;
5907 synthcount = 0;
5908
5909 if (is_mainfile)
5910 free_debug_memory ();
5911}
5912
5913static void
5914display_object_bfd (bfd *abfd)
5915{
5916 char **matching;
5917
5918 if (bfd_check_format_matches (abfd, bfd_object, &matching))
5919 {
5920 dump_bfd (abfd, true);
5921 return;
5922 }
5923
5924 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
5925 {
5926 my_bfd_nonfatal (bfd_get_filename (abfd));
5927 list_matching_formats (matching);
5928 return;
5929 }
5930
5931 if (bfd_get_error () != bfd_error_file_not_recognized)
5932 {
5933 my_bfd_nonfatal (bfd_get_filename (abfd));
5934 return;
5935 }
5936
5937 if (bfd_check_format_matches (abfd, bfd_core, &matching))
5938 {
5939 dump_bfd (abfd, true);
5940 return;
5941 }
5942
5943 my_bfd_nonfatal (bfd_get_filename (abfd));
5944
5945 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
5946 list_matching_formats (matching);
5947}
5948
5949static void
5950display_any_bfd (bfd *file, int level)
5951{
5952 /* Decompress sections unless dumping the section contents. */
5953 if (!dump_section_contents || decompressed_dumps)
5954 file->flags |= BFD_DECOMPRESS;
5955
5956 /* If the file is an archive, process all of its elements. */
5957 if (bfd_check_format (file, bfd_archive))
5958 {
5959 if (level == 0)
5960 printf (_("In archive %s:\n"), sanitize_string (bfd_get_filename (file)));
5961 else if (level > 100)
5962 {
5963 /* Prevent corrupted files from spinning us into an
5964 infinite loop. 100 is an arbitrary heuristic. */
5965 non_fatal (_("Archive nesting is too deep"));
5966 exit_status = 1;
5967 return;
5968 }
5969 else
5970 printf (_("In nested archive %s:\n"),
5971 sanitize_string (bfd_get_filename (file)));
5972
5973 bfd *last_arfile = NULL;
5974 for (;;)
5975 {
5976 bfd *arfile = bfd_openr_next_archived_file (file, last_arfile);
5977 if (arfile == NULL
5978 || arfile == last_arfile)
5979 {
5980 if (arfile != NULL)
5981 bfd_set_error (bfd_error_malformed_archive);
5982 if (bfd_get_error () != bfd_error_no_more_archived_files)
5983 my_bfd_nonfatal (bfd_get_filename (file));
5984 break;
5985 }
5986
5987 if (last_arfile != NULL)
5988 bfd_close (last_arfile);
5989
5990 display_any_bfd (arfile, level + 1);
5991
5992 last_arfile = arfile;
5993 }
5994
5995 if (last_arfile != NULL)
5996 bfd_close (last_arfile);
5997 }
5998 else
5999 display_object_bfd (file);
6000}
6001
6002static void
6003display_file (char *filename, char *target)
6004{
6005 bfd *file;
6006
6007 if (get_file_size (filename) < 1)
6008 {
6009 exit_status = 1;
6010 return;
6011 }
6012
6013 file = bfd_openr (filename, target);
6014 if (file == NULL)
6015 {
6016 my_bfd_nonfatal (filename);
6017 return;
6018 }
6019
6020 display_any_bfd (file, 0);
6021
6022 bfd_close (file);
6023}
6024\f
6025int
6026main (int argc, char **argv)
6027{
6028 int c;
6029 char *target = default_target;
6030 bool seenflag = false;
6031
6032#ifdef HAVE_LC_MESSAGES
6033 setlocale (LC_MESSAGES, "");
6034#endif
6035 setlocale (LC_CTYPE, "");
6036
6037 bindtextdomain (PACKAGE, LOCALEDIR);
6038 textdomain (PACKAGE);
6039
6040 program_name = *argv;
6041 xmalloc_set_program_name (program_name);
6042 bfd_set_error_program_name (program_name);
6043
6044 expandargv (&argc, &argv);
6045
6046 if (bfd_init () != BFD_INIT_MAGIC)
6047 fatal (_("fatal error: libbfd ABI mismatch"));
6048 set_default_bfd_target ();
6049
6050 while ((c = getopt_long (argc, argv,
6051 "CDE:FGHI:LM:P:RSTU:VW::Zab:defghij:lm:prstvwxz",
6052 long_options, (int *) 0))
6053 != EOF)
6054 {
6055 switch (c)
6056 {
6057 case 0:
6058 break; /* We've been given a long option. */
6059 case 'm':
6060 machine = optarg;
6061 break;
6062 case 'Z':
6063 decompressed_dumps = true;
6064 break;
6065 case 'M':
6066 {
6067 char *options;
6068 if (disassembler_options)
6069 options = concat (disassembler_options, ",",
6070 optarg, (const char *) NULL);
6071 else
6072 options = xstrdup (optarg);
6073 free (disassembler_options);
6074 disassembler_options = remove_whitespace_and_extra_commas (options);
6075 if (!disassembler_options)
6076 free (options);
6077 }
6078 break;
6079 case 'j':
6080 add_only (optarg);
6081 break;
6082 case 'F':
6083 display_file_offsets = true;
6084 break;
6085 case 'l':
6086 with_line_numbers = true;
6087 break;
6088 case 'b':
6089 target = optarg;
6090 break;
6091 case 'C':
6092 do_demangle = true;
6093 if (optarg != NULL)
6094 {
6095 enum demangling_styles style;
6096
6097 style = cplus_demangle_name_to_style (optarg);
6098 if (style == unknown_demangling)
6099 fatal (_("unknown demangling style `%s'"),
6100 optarg);
6101
6102 cplus_demangle_set_style (style);
6103 }
6104 break;
6105 case OPTION_RECURSE_LIMIT:
6106 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
6107 break;
6108 case OPTION_NO_RECURSE_LIMIT:
6109 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
6110 break;
6111 case 'w':
6112 do_wide = wide_output = true;
6113 break;
6114 case OPTION_ADJUST_VMA:
6115 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
6116 break;
6117 case OPTION_START_ADDRESS:
6118 start_address = parse_vma (optarg, "--start-address");
6119 if ((stop_address != (bfd_vma) -1) && stop_address <= start_address)
6120 fatal (_("error: the start address should be before the end address"));
6121 break;
6122 case OPTION_STOP_ADDRESS:
6123 stop_address = parse_vma (optarg, "--stop-address");
6124 if ((start_address != (bfd_vma) -1) && stop_address <= start_address)
6125 fatal (_("error: the stop address should be after the start address"));
6126 break;
6127 case OPTION_PREFIX:
6128 prefix = optarg;
6129 prefix_length = strlen (prefix);
6130 /* Remove an unnecessary trailing '/' */
6131 while (IS_DIR_SEPARATOR (prefix[prefix_length - 1]))
6132 prefix_length--;
6133 break;
6134 case OPTION_PREFIX_STRIP:
6135 prefix_strip = atoi (optarg);
6136 if (prefix_strip < 0)
6137 fatal (_("error: prefix strip must be non-negative"));
6138 break;
6139 case OPTION_INSN_WIDTH:
6140 insn_width = strtoul (optarg, NULL, 0);
6141 if (insn_width - 1 >= MAX_INSN_WIDTH)
6142 fatal (_("error: instruction width must be in the range 1 to "
6143 XSTRING (MAX_INSN_WIDTH)));
6144 break;
6145 case OPTION_INLINES:
6146 unwind_inlines = true;
6147 break;
6148 case OPTION_VISUALIZE_JUMPS:
6149 visualize_jumps = true;
6150 color_output = false;
6151 extended_color_output = false;
6152 if (optarg != NULL)
6153 {
6154 if (streq (optarg, "color"))
6155 color_output = true;
6156 else if (streq (optarg, "extended-color"))
6157 {
6158 color_output = true;
6159 extended_color_output = true;
6160 }
6161 else if (streq (optarg, "off"))
6162 visualize_jumps = false;
6163 else
6164 {
6165 non_fatal (_("unrecognized argument to --visualize-option"));
6166 usage (stderr, 1);
6167 }
6168 }
6169 break;
6170 case OPTION_DISASSEMBLER_COLOR:
6171 if (streq (optarg, "off"))
6172 disassembler_color = off;
6173 else if (streq (optarg, "terminal"))
6174 disassembler_color = on_if_terminal_output;
6175 else if (streq (optarg, "color")
6176 || streq (optarg, "colour")
6177 || streq (optarg, "on"))
6178 disassembler_color = on;
6179 else if (streq (optarg, "extended")
6180 || streq (optarg, "extended-color")
6181 || streq (optarg, "extended-colour"))
6182 disassembler_color = extended;
6183 else
6184 {
6185 non_fatal (_("unrecognized argument to --disassembler-color"));
6186 usage (stderr, 1);
6187 }
6188 break;
6189 case 'E':
6190 if (strcmp (optarg, "B") == 0)
6191 endian = BFD_ENDIAN_BIG;
6192 else if (strcmp (optarg, "L") == 0)
6193 endian = BFD_ENDIAN_LITTLE;
6194 else
6195 {
6196 non_fatal (_("unrecognized -E option"));
6197 usage (stderr, 1);
6198 }
6199 break;
6200 case OPTION_ENDIAN:
6201 if (strncmp (optarg, "big", strlen (optarg)) == 0)
6202 endian = BFD_ENDIAN_BIG;
6203 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
6204 endian = BFD_ENDIAN_LITTLE;
6205 else
6206 {
6207 non_fatal (_("unrecognized --endian type `%s'"), optarg);
6208 usage (stderr, 1);
6209 }
6210 break;
6211
6212 case 'f':
6213 dump_file_header = true;
6214 seenflag = true;
6215 break;
6216 case 'i':
6217 formats_info = true;
6218 seenflag = true;
6219 break;
6220 case 'I':
6221 add_include_path (optarg);
6222 break;
6223 case 'p':
6224 dump_private_headers = true;
6225 seenflag = true;
6226 break;
6227 case 'P':
6228 dump_private_options = optarg;
6229 seenflag = true;
6230 break;
6231 case 'x':
6232 dump_private_headers = true;
6233 dump_symtab = true;
6234 dump_reloc_info = true;
6235 dump_file_header = true;
6236 dump_ar_hdrs = true;
6237 dump_section_headers = true;
6238 seenflag = true;
6239 break;
6240 case 't':
6241 dump_symtab = true;
6242 seenflag = true;
6243 break;
6244 case 'T':
6245 dump_dynamic_symtab = true;
6246 seenflag = true;
6247 break;
6248 case 'd':
6249 disassemble = true;
6250 seenflag = true;
6251 if (optarg)
6252 {
6253 struct symbol_entry *sym = xmalloc (sizeof (*sym));
6254
6255 sym->name = optarg;
6256 sym->next = disasm_sym_list;
6257 disasm_sym_list = sym;
6258 }
6259 break;
6260 case 'z':
6261 disassemble_zeroes = true;
6262 break;
6263 case 'D':
6264 disassemble = true;
6265 disassemble_all = true;
6266 seenflag = true;
6267 break;
6268 case 'S':
6269 disassemble = true;
6270 with_source_code = true;
6271 seenflag = true;
6272 break;
6273 case OPTION_SOURCE_COMMENT:
6274 disassemble = true;
6275 with_source_code = true;
6276 seenflag = true;
6277 if (optarg)
6278 source_comment = xstrdup (sanitize_string (optarg));
6279 else
6280 source_comment = xstrdup ("# ");
6281 break;
6282 case 'g':
6283 dump_debugging = 1;
6284 seenflag = true;
6285 break;
6286 case 'e':
6287 dump_debugging = 1;
6288 dump_debugging_tags = 1;
6289 do_demangle = true;
6290 seenflag = true;
6291 break;
6292 case 'L':
6293 process_links = true;
6294 do_follow_links = true;
6295 break;
6296 case 'W':
6297 seenflag = true;
6298 if (optarg)
6299 {
6300 if (dwarf_select_sections_by_letters (optarg))
6301 dump_dwarf_section_info = true;
6302 }
6303 else
6304 {
6305 dump_dwarf_section_info = true;
6306 dwarf_select_sections_all ();
6307 }
6308 break;
6309 case OPTION_DWARF:
6310 seenflag = true;
6311 if (optarg)
6312 {
6313 if (dwarf_select_sections_by_names (optarg))
6314 dump_dwarf_section_info = true;
6315 }
6316 else
6317 {
6318 dwarf_select_sections_all ();
6319 dump_dwarf_section_info = true;
6320 }
6321 break;
6322 case OPTION_DWARF_DEPTH:
6323 {
6324 char *cp;
6325 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
6326 }
6327 break;
6328 case OPTION_DWARF_START:
6329 {
6330 char *cp;
6331 dwarf_start_die = strtoul (optarg, & cp, 0);
6332 suppress_bfd_header = 1;
6333 }
6334 break;
6335 case OPTION_DWARF_CHECK:
6336 dwarf_check = true;
6337 break;
6338#ifdef ENABLE_LIBCTF
6339 case OPTION_CTF:
6340 dump_ctf_section_info = true;
6341 if (optarg)
6342 dump_ctf_section_name = xstrdup (optarg);
6343 seenflag = true;
6344 break;
6345 case OPTION_CTF_PARENT:
6346 dump_ctf_parent_name = xstrdup (optarg);
6347 break;
6348 case OPTION_CTF_PARENT_SECTION:
6349 dump_ctf_parent_section_name = xstrdup (optarg);
6350 break;
6351#endif
6352 case OPTION_SFRAME:
6353 dump_sframe_section_info = true;
6354 if (optarg)
6355 dump_sframe_section_name = xstrdup (optarg);
6356 seenflag = true;
6357 break;
6358 case 'G':
6359 dump_stab_section_info = true;
6360 seenflag = true;
6361 break;
6362 case 's':
6363 dump_section_contents = true;
6364 seenflag = true;
6365 break;
6366 case 'r':
6367 dump_reloc_info = true;
6368 seenflag = true;
6369 break;
6370 case 'R':
6371 dump_dynamic_reloc_info = true;
6372 seenflag = true;
6373 break;
6374 case 'a':
6375 dump_ar_hdrs = true;
6376 seenflag = true;
6377 break;
6378 case 'h':
6379 dump_section_headers = true;
6380 seenflag = true;
6381 break;
6382 case 'v':
6383 case 'V':
6384 show_version = true;
6385 seenflag = true;
6386 break;
6387
6388 case 'U':
6389 if (streq (optarg, "default") || streq (optarg, "d"))
6390 unicode_display = unicode_default;
6391 else if (streq (optarg, "locale") || streq (optarg, "l"))
6392 unicode_display = unicode_locale;
6393 else if (streq (optarg, "escape") || streq (optarg, "e"))
6394 unicode_display = unicode_escape;
6395 else if (streq (optarg, "invalid") || streq (optarg, "i"))
6396 unicode_display = unicode_invalid;
6397 else if (streq (optarg, "hex") || streq (optarg, "x"))
6398 unicode_display = unicode_hex;
6399 else if (streq (optarg, "highlight") || streq (optarg, "h"))
6400 unicode_display = unicode_highlight;
6401 else
6402 fatal (_("invalid argument to -U/--unicode: %s"), optarg);
6403 break;
6404
6405 case 'H':
6406 usage (stdout, 0);
6407 /* No need to set seenflag or to break - usage() does not return. */
6408 default:
6409 usage (stderr, 1);
6410 }
6411 }
6412
6413 if (disassembler_color == on_if_terminal_output)
6414 disassembler_color = isatty (1) ? on : off;
6415
6416 if (show_version)
6417 print_version ("objdump");
6418
6419 if (!seenflag)
6420 usage (stderr, 2);
6421
6422 dump_any_debugging = (dump_debugging
6423 || dump_dwarf_section_info
6424 || process_links
6425 || with_source_code);
6426
6427 if (formats_info)
6428 exit_status = display_info ();
6429 else
6430 {
6431 if (optind == argc)
6432 display_file ("a.out", target);
6433 else
6434 for (; optind < argc;)
6435 {
6436 display_file (argv[optind], target);
6437 optind++;
6438 }
6439 }
6440
6441 free_only_list ();
6442 free (dump_ctf_section_name);
6443 free (dump_ctf_parent_name);
6444 free ((void *) source_comment);
6445 free (dump_ctf_parent_section_name);
6446
6447 return exit_status;
6448}