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