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