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