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