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