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