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