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