objdump doesn't accept -L option
[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 = PTR_ADD (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 const char *name;
3715
3716 /* If it is already loaded, do nothing. */
3717 if (section->start != NULL)
3718 {
3719 if (streq (section->filename, bfd_get_filename (abfd)))
3720 return true;
3721 }
3722 /* Locate the debug section. */
3723 name = section->uncompressed_name;
3724 sec = bfd_get_section_by_name (abfd, name);
3725 if (sec == NULL)
3726 {
3727 name = section->compressed_name;
3728 if (*name)
3729 sec = bfd_get_section_by_name (abfd, name);
3730 }
3731 if (sec == NULL)
3732 {
3733 name = section->xcoff_name;
3734 if (*name)
3735 sec = bfd_get_section_by_name (abfd, name);
3736 }
3737 if (sec == NULL)
3738 return false;
3739
3740 section->name = name;
3741 return load_specific_debug_section (debug, sec, file);
3742 }
3743
3744 void
3745 free_debug_section (enum dwarf_section_display_enum debug)
3746 {
3747 struct dwarf_section *section = &debug_displays [debug].section;
3748
3749 free ((char *) section->start);
3750 section->start = NULL;
3751 section->address = 0;
3752 section->size = 0;
3753 }
3754
3755 void
3756 close_debug_file (void * file)
3757 {
3758 bfd * abfd = (bfd *) file;
3759
3760 bfd_close (abfd);
3761 }
3762
3763 void *
3764 open_debug_file (const char * pathname)
3765 {
3766 bfd * data;
3767
3768 data = bfd_openr (pathname, NULL);
3769 if (data == NULL)
3770 return NULL;
3771
3772 if (! bfd_check_format (data, bfd_object))
3773 return NULL;
3774
3775 return data;
3776 }
3777
3778 #if HAVE_LIBDEBUGINFOD
3779 /* Return a hex string represention of the build-id. */
3780
3781 unsigned char *
3782 get_build_id (void * data)
3783 {
3784 unsigned i;
3785 char * build_id_str;
3786 bfd * abfd = (bfd *) data;
3787 const struct bfd_build_id * build_id;
3788
3789 build_id = abfd->build_id;
3790 if (build_id == NULL)
3791 return NULL;
3792
3793 build_id_str = malloc (build_id->size * 2 + 1);
3794 if (build_id_str == NULL)
3795 return NULL;
3796
3797 for (i = 0; i < build_id->size; i++)
3798 sprintf (build_id_str + (i * 2), "%02x", build_id->data[i]);
3799 build_id_str[build_id->size * 2] = '\0';
3800
3801 return (unsigned char *)build_id_str;
3802 }
3803 #endif /* HAVE_LIBDEBUGINFOD */
3804
3805 static void
3806 dump_dwarf_section (bfd *abfd, asection *section,
3807 void *arg ATTRIBUTE_UNUSED)
3808 {
3809 const char *name = bfd_section_name (section);
3810 const char *match;
3811 int i;
3812
3813 if (*name == 0)
3814 return;
3815
3816 if (startswith (name, ".gnu.linkonce.wi."))
3817 match = ".debug_info";
3818 else
3819 match = name;
3820
3821 for (i = 0; i < max; i++)
3822 if ((strcmp (debug_displays [i].section.uncompressed_name, match) == 0
3823 || strcmp (debug_displays [i].section.compressed_name, match) == 0
3824 || strcmp (debug_displays [i].section.xcoff_name, match) == 0)
3825 && debug_displays [i].enabled != NULL
3826 && *debug_displays [i].enabled)
3827 {
3828 struct dwarf_section *sec = &debug_displays [i].section;
3829
3830 if (strcmp (sec->uncompressed_name, match) == 0)
3831 sec->name = sec->uncompressed_name;
3832 else if (strcmp (sec->compressed_name, match) == 0)
3833 sec->name = sec->compressed_name;
3834 else
3835 sec->name = sec->xcoff_name;
3836 if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
3837 section, abfd))
3838 {
3839 debug_displays [i].display (sec, abfd);
3840
3841 if (i != info && i != abbrev)
3842 free_debug_section ((enum dwarf_section_display_enum) i);
3843 }
3844 break;
3845 }
3846 }
3847
3848 /* Dump the dwarf debugging information. */
3849
3850 static void
3851 dump_dwarf (bfd *abfd)
3852 {
3853 /* The byte_get pointer should have been set at the start of dump_bfd(). */
3854 if (byte_get == NULL)
3855 {
3856 warn (_("File %s does not contain any dwarf debug information\n"),
3857 bfd_get_filename (abfd));
3858 return;
3859 }
3860
3861 switch (bfd_get_arch (abfd))
3862 {
3863 case bfd_arch_s12z:
3864 /* S12Z has a 24 bit address space. But the only known
3865 producer of dwarf_info encodes addresses into 32 bits. */
3866 eh_addr_size = 4;
3867 break;
3868
3869 default:
3870 eh_addr_size = bfd_arch_bits_per_address (abfd) / 8;
3871 break;
3872 }
3873
3874 init_dwarf_regnames_by_bfd_arch_and_mach (bfd_get_arch (abfd),
3875 bfd_get_mach (abfd));
3876
3877 bfd_map_over_sections (abfd, dump_dwarf_section, NULL);
3878 }
3879 \f
3880 /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
3881 it. Return NULL on failure. */
3882
3883 static bfd_byte *
3884 read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr,
3885 bfd_size_type *entsize_ptr)
3886 {
3887 asection *stabsect;
3888 bfd_byte *contents;
3889
3890 stabsect = bfd_get_section_by_name (abfd, sect_name);
3891 if (stabsect == NULL)
3892 {
3893 printf (_("No %s section present\n\n"),
3894 sanitize_string (sect_name));
3895 return false;
3896 }
3897
3898 if (!bfd_malloc_and_get_section (abfd, stabsect, &contents))
3899 {
3900 non_fatal (_("reading %s section of %s failed: %s"),
3901 sect_name, bfd_get_filename (abfd),
3902 bfd_errmsg (bfd_get_error ()));
3903 exit_status = 1;
3904 free (contents);
3905 return NULL;
3906 }
3907
3908 *size_ptr = bfd_section_size (stabsect);
3909 if (entsize_ptr)
3910 *entsize_ptr = stabsect->entsize;
3911
3912 return contents;
3913 }
3914
3915 /* Stabs entries use a 12 byte format:
3916 4 byte string table index
3917 1 byte stab type
3918 1 byte stab other field
3919 2 byte stab desc field
3920 4 byte stab value
3921 FIXME: This will have to change for a 64 bit object format. */
3922
3923 #define STRDXOFF (0)
3924 #define TYPEOFF (4)
3925 #define OTHEROFF (5)
3926 #define DESCOFF (6)
3927 #define VALOFF (8)
3928 #define STABSIZE (12)
3929
3930 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
3931 using string table section STRSECT_NAME (in `strtab'). */
3932
3933 static void
3934 print_section_stabs (bfd *abfd,
3935 const char *stabsect_name,
3936 unsigned *string_offset_ptr)
3937 {
3938 int i;
3939 unsigned file_string_table_offset = 0;
3940 unsigned next_file_string_table_offset = *string_offset_ptr;
3941 bfd_byte *stabp, *stabs_end;
3942
3943 stabp = stabs;
3944 stabs_end = stabp + stab_size;
3945
3946 printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name));
3947 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
3948
3949 /* Loop through all symbols and print them.
3950
3951 We start the index at -1 because there is a dummy symbol on
3952 the front of stabs-in-{coff,elf} sections that supplies sizes. */
3953 for (i = -1; stabp <= stabs_end - STABSIZE; stabp += STABSIZE, i++)
3954 {
3955 const char *name;
3956 unsigned long strx;
3957 unsigned char type, other;
3958 unsigned short desc;
3959 bfd_vma value;
3960
3961 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
3962 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
3963 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
3964 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
3965 value = bfd_h_get_32 (abfd, stabp + VALOFF);
3966
3967 printf ("\n%-6d ", i);
3968 /* Either print the stab name, or, if unnamed, print its number
3969 again (makes consistent formatting for tools like awk). */
3970 name = bfd_get_stab_name (type);
3971 if (name != NULL)
3972 printf ("%-6s", sanitize_string (name));
3973 else if (type == N_UNDF)
3974 printf ("HdrSym");
3975 else
3976 printf ("%-6d", type);
3977 printf (" %-6d %-6d ", other, desc);
3978 bfd_printf_vma (abfd, value);
3979 printf (" %-6lu", strx);
3980
3981 /* Symbols with type == 0 (N_UNDF) specify the length of the
3982 string table associated with this file. We use that info
3983 to know how to relocate the *next* file's string table indices. */
3984 if (type == N_UNDF)
3985 {
3986 file_string_table_offset = next_file_string_table_offset;
3987 next_file_string_table_offset += value;
3988 }
3989 else
3990 {
3991 bfd_size_type amt = strx + file_string_table_offset;
3992
3993 /* Using the (possibly updated) string table offset, print the
3994 string (if any) associated with this symbol. */
3995 if (amt < stabstr_size)
3996 /* PR 17512: file: 079-79389-0.001:0.1.
3997 FIXME: May need to sanitize this string before displaying. */
3998 printf (" %.*s", (int)(stabstr_size - amt), strtab + amt);
3999 else
4000 printf (" *");
4001 }
4002 }
4003 printf ("\n\n");
4004 *string_offset_ptr = next_file_string_table_offset;
4005 }
4006
4007 typedef struct
4008 {
4009 const char * section_name;
4010 const char * string_section_name;
4011 unsigned string_offset;
4012 }
4013 stab_section_names;
4014
4015 static void
4016 find_stabs_section (bfd *abfd, asection *section, void *names)
4017 {
4018 int len;
4019 stab_section_names * sought = (stab_section_names *) names;
4020
4021 /* Check for section names for which stabsect_name is a prefix, to
4022 handle .stab.N, etc. */
4023 len = strlen (sought->section_name);
4024
4025 /* If the prefix matches, and the files section name ends with a
4026 nul or a digit, then we match. I.e., we want either an exact
4027 match or a section followed by a number. */
4028 if (strncmp (sought->section_name, section->name, len) == 0
4029 && (section->name[len] == 0
4030 || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
4031 {
4032 if (strtab == NULL)
4033 strtab = read_section_stabs (abfd, sought->string_section_name,
4034 &stabstr_size, NULL);
4035
4036 if (strtab)
4037 {
4038 stabs = read_section_stabs (abfd, section->name, &stab_size, NULL);
4039 if (stabs)
4040 print_section_stabs (abfd, section->name, &sought->string_offset);
4041 }
4042 }
4043 }
4044
4045 static void
4046 dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
4047 {
4048 stab_section_names s;
4049
4050 s.section_name = stabsect_name;
4051 s.string_section_name = strsect_name;
4052 s.string_offset = 0;
4053
4054 bfd_map_over_sections (abfd, find_stabs_section, & s);
4055
4056 free (strtab);
4057 strtab = NULL;
4058 }
4059
4060 /* Dump the any sections containing stabs debugging information. */
4061
4062 static void
4063 dump_stabs (bfd *abfd)
4064 {
4065 dump_stabs_section (abfd, ".stab", ".stabstr");
4066 dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
4067 dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
4068
4069 /* For Darwin. */
4070 dump_stabs_section (abfd, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
4071
4072 dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
4073 }
4074 \f
4075 static void
4076 dump_bfd_header (bfd *abfd)
4077 {
4078 char *comma = "";
4079
4080 printf (_("architecture: %s, "),
4081 bfd_printable_arch_mach (bfd_get_arch (abfd),
4082 bfd_get_mach (abfd)));
4083 printf (_("flags 0x%08x:\n"), abfd->flags & ~BFD_FLAGS_FOR_BFD_USE_MASK);
4084
4085 #define PF(x, y) if (abfd->flags & x) {printf ("%s%s", comma, y); comma=", ";}
4086 PF (HAS_RELOC, "HAS_RELOC");
4087 PF (EXEC_P, "EXEC_P");
4088 PF (HAS_LINENO, "HAS_LINENO");
4089 PF (HAS_DEBUG, "HAS_DEBUG");
4090 PF (HAS_SYMS, "HAS_SYMS");
4091 PF (HAS_LOCALS, "HAS_LOCALS");
4092 PF (DYNAMIC, "DYNAMIC");
4093 PF (WP_TEXT, "WP_TEXT");
4094 PF (D_PAGED, "D_PAGED");
4095 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
4096 printf (_("\nstart address 0x"));
4097 bfd_printf_vma (abfd, abfd->start_address);
4098 printf ("\n");
4099 }
4100 \f
4101
4102 #ifdef ENABLE_LIBCTF
4103 /* Formatting callback function passed to ctf_dump. Returns either the pointer
4104 it is passed, or a pointer to newly-allocated storage, in which case
4105 dump_ctf() will free it when it no longer needs it. */
4106
4107 static char *
4108 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
4109 char *s, void *arg)
4110 {
4111 const char *blanks = arg;
4112 char *new_s;
4113
4114 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
4115 return s;
4116 return new_s;
4117 }
4118
4119 /* Make a ctfsect suitable for ctf_bfdopen_ctfsect(). */
4120 static ctf_sect_t
4121 make_ctfsect (const char *name, bfd_byte *data,
4122 bfd_size_type size)
4123 {
4124 ctf_sect_t ctfsect;
4125
4126 ctfsect.cts_name = name;
4127 ctfsect.cts_entsize = 1;
4128 ctfsect.cts_size = size;
4129 ctfsect.cts_data = data;
4130
4131 return ctfsect;
4132 }
4133
4134 /* Dump CTF errors/warnings. */
4135 static void
4136 dump_ctf_errs (ctf_dict_t *fp)
4137 {
4138 ctf_next_t *it = NULL;
4139 char *errtext;
4140 int is_warning;
4141 int err;
4142
4143 /* Dump accumulated errors and warnings. */
4144 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
4145 {
4146 non_fatal (_("%s: %s"), is_warning ? _("warning"): _("error"),
4147 errtext);
4148 free (errtext);
4149 }
4150 if (err != ECTF_NEXT_END)
4151 {
4152 non_fatal (_("CTF error: cannot get CTF errors: `%s'"),
4153 ctf_errmsg (err));
4154 }
4155 }
4156
4157 /* Dump one CTF archive member. */
4158
4159 static int
4160 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, void *arg)
4161 {
4162 ctf_dict_t *parent = (ctf_dict_t *) arg;
4163 const char *things[] = {"Header", "Labels", "Data objects",
4164 "Function objects", "Variables", "Types", "Strings",
4165 ""};
4166 const char **thing;
4167 size_t i;
4168
4169 /* Only print out the name of non-default-named archive members.
4170 The name .ctf appears everywhere, even for things that aren't
4171 really archives, so printing it out is liable to be confusing.
4172
4173 The parent, if there is one, is the default-owned archive member:
4174 avoid importing it into itself. (This does no harm, but looks
4175 confusing.) */
4176
4177 if (strcmp (name, ".ctf") != 0)
4178 {
4179 printf (_("\nCTF archive member: %s:\n"), sanitize_string (name));
4180 ctf_import (ctf, parent);
4181 }
4182
4183 for (i = 0, thing = things; *thing[0]; thing++, i++)
4184 {
4185 ctf_dump_state_t *s = NULL;
4186 char *item;
4187
4188 printf ("\n %s:\n", *thing);
4189 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
4190 (void *) " ")) != NULL)
4191 {
4192 printf ("%s\n", item);
4193 free (item);
4194 }
4195
4196 if (ctf_errno (ctf))
4197 {
4198 non_fatal (_("Iteration failed: %s, %s"), *thing,
4199 ctf_errmsg (ctf_errno (ctf)));
4200 break;
4201 }
4202 }
4203
4204 dump_ctf_errs (ctf);
4205
4206 return 0;
4207 }
4208
4209 /* Dump the CTF debugging information. */
4210
4211 static void
4212 dump_ctf (bfd *abfd, const char *sect_name, const char *parent_name)
4213 {
4214 ctf_archive_t *ctfa, *parenta = NULL, *lookparent;
4215 bfd_byte *ctfdata, *parentdata = NULL;
4216 bfd_size_type ctfsize, parentsize;
4217 ctf_sect_t ctfsect;
4218 ctf_dict_t *parent = NULL;
4219 int err;
4220
4221 if ((ctfdata = read_section_stabs (abfd, sect_name, &ctfsize, NULL)) == NULL)
4222 bfd_fatal (bfd_get_filename (abfd));
4223
4224 if (parent_name
4225 && (parentdata = read_section_stabs (abfd, parent_name, &parentsize,
4226 NULL)) == NULL)
4227 bfd_fatal (bfd_get_filename (abfd));
4228
4229 /* Load the CTF file and dump it. */
4230
4231 ctfsect = make_ctfsect (sect_name, ctfdata, ctfsize);
4232 if ((ctfa = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
4233 {
4234 dump_ctf_errs (NULL);
4235 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
4236 bfd_fatal (bfd_get_filename (abfd));
4237 }
4238
4239 if (parentdata)
4240 {
4241 ctfsect = make_ctfsect (parent_name, parentdata, parentsize);
4242 if ((parenta = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
4243 {
4244 dump_ctf_errs (NULL);
4245 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
4246 bfd_fatal (bfd_get_filename (abfd));
4247 }
4248
4249 lookparent = parenta;
4250 }
4251 else
4252 lookparent = ctfa;
4253
4254 /* Assume that the applicable parent archive member is the default one.
4255 (This is what all known implementations are expected to do, if they
4256 put CTFs and their parents in archives together.) */
4257 if ((parent = ctf_dict_open (lookparent, NULL, &err)) == NULL)
4258 {
4259 dump_ctf_errs (NULL);
4260 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
4261 bfd_fatal (bfd_get_filename (abfd));
4262 }
4263
4264 printf (_("Contents of CTF section %s:\n"), sanitize_string (sect_name));
4265
4266 if ((err = ctf_archive_iter (ctfa, dump_ctf_archive_member, parent)) != 0)
4267 {
4268 dump_ctf_errs (NULL);
4269 non_fatal (_("CTF archive member open failure: %s"), ctf_errmsg (err));
4270 bfd_fatal (bfd_get_filename (abfd));
4271 }
4272 ctf_dict_close (parent);
4273 ctf_close (ctfa);
4274 ctf_close (parenta);
4275 free (parentdata);
4276 free (ctfdata);
4277 }
4278 #else
4279 static void
4280 dump_ctf (bfd *abfd ATTRIBUTE_UNUSED, const char *sect_name ATTRIBUTE_UNUSED,
4281 const char *parent_name ATTRIBUTE_UNUSED) {}
4282 #endif
4283
4284 \f
4285 static void
4286 dump_bfd_private_header (bfd *abfd)
4287 {
4288 if (!bfd_print_private_bfd_data (abfd, stdout))
4289 non_fatal (_("warning: private headers incomplete: %s"),
4290 bfd_errmsg (bfd_get_error ()));
4291 }
4292
4293 static void
4294 dump_target_specific (bfd *abfd)
4295 {
4296 const struct objdump_private_desc * const *desc;
4297 struct objdump_private_option *opt;
4298 char *e, *b;
4299
4300 /* Find the desc. */
4301 for (desc = objdump_private_vectors; *desc != NULL; desc++)
4302 if ((*desc)->filter (abfd))
4303 break;
4304
4305 if (*desc == NULL)
4306 {
4307 non_fatal (_("option -P/--private not supported by this file"));
4308 return;
4309 }
4310
4311 /* Clear all options. */
4312 for (opt = (*desc)->options; opt->name; opt++)
4313 opt->selected = false;
4314
4315 /* Decode options. */
4316 b = dump_private_options;
4317 do
4318 {
4319 e = strchr (b, ',');
4320
4321 if (e)
4322 *e = 0;
4323
4324 for (opt = (*desc)->options; opt->name; opt++)
4325 if (strcmp (opt->name, b) == 0)
4326 {
4327 opt->selected = true;
4328 break;
4329 }
4330 if (opt->name == NULL)
4331 non_fatal (_("target specific dump '%s' not supported"), b);
4332
4333 if (e)
4334 {
4335 *e = ',';
4336 b = e + 1;
4337 }
4338 }
4339 while (e != NULL);
4340
4341 /* Dump. */
4342 (*desc)->dump (abfd);
4343 }
4344 \f
4345 /* Display a section in hexadecimal format with associated characters.
4346 Each line prefixed by the zero padded address. */
4347
4348 static void
4349 dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
4350 {
4351 bfd_byte *data = NULL;
4352 bfd_size_type datasize;
4353 bfd_vma addr_offset;
4354 bfd_vma start_offset;
4355 bfd_vma stop_offset;
4356 unsigned int opb = bfd_octets_per_byte (abfd, section);
4357 /* Bytes per line. */
4358 const int onaline = 16;
4359 char buf[64];
4360 int count;
4361 int width;
4362
4363 if (! process_section_p (section))
4364 return;
4365
4366 if ((section->flags & SEC_HAS_CONTENTS) == 0)
4367 return;
4368
4369 if ((datasize = bfd_section_size (section)) == 0)
4370 return;
4371
4372 /* Compute the address range to display. */
4373 if (start_address == (bfd_vma) -1
4374 || start_address < section->vma)
4375 start_offset = 0;
4376 else
4377 start_offset = start_address - section->vma;
4378
4379 if (stop_address == (bfd_vma) -1)
4380 stop_offset = datasize / opb;
4381 else
4382 {
4383 if (stop_address < section->vma)
4384 stop_offset = 0;
4385 else
4386 stop_offset = stop_address - section->vma;
4387
4388 if (stop_offset > datasize / opb)
4389 stop_offset = datasize / opb;
4390 }
4391
4392 if (start_offset >= stop_offset)
4393 return;
4394
4395 printf (_("Contents of section %s:"), sanitize_string (section->name));
4396 if (display_file_offsets)
4397 printf (_(" (Starting at file offset: 0x%lx)"),
4398 (unsigned long) (section->filepos + start_offset));
4399 printf ("\n");
4400
4401 if (!bfd_get_full_section_contents (abfd, section, &data))
4402 {
4403 non_fatal (_("Reading section %s failed because: %s"),
4404 section->name, bfd_errmsg (bfd_get_error ()));
4405 return;
4406 }
4407
4408 width = 4;
4409
4410 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
4411 if (strlen (buf) >= sizeof (buf))
4412 abort ();
4413
4414 count = 0;
4415 while (buf[count] == '0' && buf[count+1] != '\0')
4416 count++;
4417 count = strlen (buf) - count;
4418 if (count > width)
4419 width = count;
4420
4421 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
4422 if (strlen (buf) >= sizeof (buf))
4423 abort ();
4424
4425 count = 0;
4426 while (buf[count] == '0' && buf[count+1] != '\0')
4427 count++;
4428 count = strlen (buf) - count;
4429 if (count > width)
4430 width = count;
4431
4432 for (addr_offset = start_offset;
4433 addr_offset < stop_offset; addr_offset += onaline / opb)
4434 {
4435 bfd_size_type j;
4436
4437 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
4438 count = strlen (buf);
4439 if ((size_t) count >= sizeof (buf))
4440 abort ();
4441
4442 putchar (' ');
4443 while (count < width)
4444 {
4445 putchar ('0');
4446 count++;
4447 }
4448 fputs (buf + count - width, stdout);
4449 putchar (' ');
4450
4451 for (j = addr_offset * opb;
4452 j < addr_offset * opb + onaline; j++)
4453 {
4454 if (j < stop_offset * opb)
4455 printf ("%02x", (unsigned) (data[j]));
4456 else
4457 printf (" ");
4458 if ((j & 3) == 3)
4459 printf (" ");
4460 }
4461
4462 printf (" ");
4463 for (j = addr_offset * opb;
4464 j < addr_offset * opb + onaline; j++)
4465 {
4466 if (j >= stop_offset * opb)
4467 printf (" ");
4468 else
4469 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
4470 }
4471 putchar ('\n');
4472 }
4473 free (data);
4474 }
4475
4476 /* Actually display the various requested regions. */
4477
4478 static void
4479 dump_data (bfd *abfd)
4480 {
4481 bfd_map_over_sections (abfd, dump_section, NULL);
4482 }
4483
4484 /* Should perhaps share code and display with nm? */
4485
4486 static void
4487 dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bool dynamic)
4488 {
4489 asymbol **current;
4490 long max_count;
4491 long count;
4492
4493 if (dynamic)
4494 {
4495 current = dynsyms;
4496 max_count = dynsymcount;
4497 printf ("DYNAMIC SYMBOL TABLE:\n");
4498 }
4499 else
4500 {
4501 current = syms;
4502 max_count = symcount;
4503 printf ("SYMBOL TABLE:\n");
4504 }
4505
4506 if (max_count == 0)
4507 printf (_("no symbols\n"));
4508
4509 for (count = 0; count < max_count; count++)
4510 {
4511 bfd *cur_bfd;
4512
4513 if (*current == NULL)
4514 printf (_("no information for symbol number %ld\n"), count);
4515
4516 else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
4517 printf (_("could not determine the type of symbol number %ld\n"),
4518 count);
4519
4520 else if (process_section_p ((* current)->section)
4521 && (dump_special_syms
4522 || !bfd_is_target_special_symbol (cur_bfd, *current)))
4523 {
4524 const char *name = (*current)->name;
4525
4526 if (do_demangle && name != NULL && *name != '\0')
4527 {
4528 char *alloc;
4529
4530 /* If we want to demangle the name, we demangle it
4531 here, and temporarily clobber it while calling
4532 bfd_print_symbol. FIXME: This is a gross hack. */
4533 alloc = bfd_demangle (cur_bfd, name, demangle_flags);
4534 if (alloc != NULL)
4535 (*current)->name = alloc;
4536 bfd_print_symbol (cur_bfd, stdout, *current,
4537 bfd_print_symbol_all);
4538 if (alloc != NULL)
4539 {
4540 (*current)->name = name;
4541 free (alloc);
4542 }
4543 }
4544 else
4545 bfd_print_symbol (cur_bfd, stdout, *current,
4546 bfd_print_symbol_all);
4547 printf ("\n");
4548 }
4549
4550 current++;
4551 }
4552 printf ("\n\n");
4553 }
4554 \f
4555 static void
4556 dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
4557 {
4558 arelent **p;
4559 char *last_filename, *last_functionname;
4560 unsigned int last_line;
4561 unsigned int last_discriminator;
4562
4563 /* Get column headers lined up reasonably. */
4564 {
4565 static int width;
4566
4567 if (width == 0)
4568 {
4569 char buf[30];
4570
4571 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
4572 width = strlen (buf) - 7;
4573 }
4574 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
4575 }
4576
4577 last_filename = NULL;
4578 last_functionname = NULL;
4579 last_line = 0;
4580 last_discriminator = 0;
4581
4582 for (p = relpp; relcount && *p != NULL; p++, relcount--)
4583 {
4584 arelent *q = *p;
4585 const char *filename, *functionname;
4586 unsigned int linenumber;
4587 unsigned int discriminator;
4588 const char *sym_name;
4589 const char *section_name;
4590 bfd_vma addend2 = 0;
4591
4592 if (start_address != (bfd_vma) -1
4593 && q->address < start_address)
4594 continue;
4595 if (stop_address != (bfd_vma) -1
4596 && q->address > stop_address)
4597 continue;
4598
4599 if (with_line_numbers
4600 && sec != NULL
4601 && bfd_find_nearest_line_discriminator (abfd, sec, syms, q->address,
4602 &filename, &functionname,
4603 &linenumber, &discriminator))
4604 {
4605 if (functionname != NULL
4606 && (last_functionname == NULL
4607 || strcmp (functionname, last_functionname) != 0))
4608 {
4609 printf ("%s():\n", sanitize_string (functionname));
4610 if (last_functionname != NULL)
4611 free (last_functionname);
4612 last_functionname = xstrdup (functionname);
4613 }
4614
4615 if (linenumber > 0
4616 && (linenumber != last_line
4617 || (filename != NULL
4618 && last_filename != NULL
4619 && filename_cmp (filename, last_filename) != 0)
4620 || (discriminator != last_discriminator)))
4621 {
4622 if (discriminator > 0)
4623 printf ("%s:%u\n", filename == NULL ? "???" :
4624 sanitize_string (filename), linenumber);
4625 else
4626 printf ("%s:%u (discriminator %u)\n",
4627 filename == NULL ? "???" : sanitize_string (filename),
4628 linenumber, discriminator);
4629 last_line = linenumber;
4630 last_discriminator = discriminator;
4631 if (last_filename != NULL)
4632 free (last_filename);
4633 if (filename == NULL)
4634 last_filename = NULL;
4635 else
4636 last_filename = xstrdup (filename);
4637 }
4638 }
4639
4640 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
4641 {
4642 sym_name = (*(q->sym_ptr_ptr))->name;
4643 section_name = (*(q->sym_ptr_ptr))->section->name;
4644 }
4645 else
4646 {
4647 sym_name = NULL;
4648 section_name = NULL;
4649 }
4650
4651 bfd_printf_vma (abfd, q->address);
4652 if (q->howto == NULL)
4653 printf (" *unknown* ");
4654 else if (q->howto->name)
4655 {
4656 const char *name = q->howto->name;
4657
4658 /* R_SPARC_OLO10 relocations contain two addends.
4659 But because 'arelent' lacks enough storage to
4660 store them both, the 64-bit ELF Sparc backend
4661 records this as two relocations. One R_SPARC_LO10
4662 and one R_SPARC_13, both pointing to the same
4663 address. This is merely so that we have some
4664 place to store both addend fields.
4665
4666 Undo this transformation, otherwise the output
4667 will be confusing. */
4668 if (abfd->xvec->flavour == bfd_target_elf_flavour
4669 && elf_tdata (abfd)->elf_header->e_machine == EM_SPARCV9
4670 && relcount > 1
4671 && !strcmp (q->howto->name, "R_SPARC_LO10"))
4672 {
4673 arelent *q2 = *(p + 1);
4674 if (q2 != NULL
4675 && q2->howto
4676 && q->address == q2->address
4677 && !strcmp (q2->howto->name, "R_SPARC_13"))
4678 {
4679 name = "R_SPARC_OLO10";
4680 addend2 = q2->addend;
4681 p++;
4682 }
4683 }
4684 printf (" %-16s ", name);
4685 }
4686 else
4687 printf (" %-16d ", q->howto->type);
4688
4689 if (sym_name)
4690 {
4691 objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
4692 }
4693 else
4694 {
4695 if (section_name == NULL)
4696 section_name = "*unknown*";
4697 printf ("[%s]", sanitize_string (section_name));
4698 }
4699
4700 if (q->addend)
4701 {
4702 bfd_signed_vma addend = q->addend;
4703 if (addend < 0)
4704 {
4705 printf ("-0x");
4706 addend = -addend;
4707 }
4708 else
4709 printf ("+0x");
4710 bfd_printf_vma (abfd, addend);
4711 }
4712 if (addend2)
4713 {
4714 printf ("+0x");
4715 bfd_printf_vma (abfd, addend2);
4716 }
4717
4718 printf ("\n");
4719 }
4720
4721 if (last_filename != NULL)
4722 free (last_filename);
4723 if (last_functionname != NULL)
4724 free (last_functionname);
4725 }
4726
4727 static void
4728 dump_relocs_in_section (bfd *abfd,
4729 asection *section,
4730 void *dummy ATTRIBUTE_UNUSED)
4731 {
4732 arelent **relpp = NULL;
4733 long relcount;
4734 long relsize;
4735
4736 if ( bfd_is_abs_section (section)
4737 || bfd_is_und_section (section)
4738 || bfd_is_com_section (section)
4739 || (! process_section_p (section))
4740 || ((section->flags & SEC_RELOC) == 0))
4741 return;
4742
4743 printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section->name));
4744
4745 relsize = bfd_get_reloc_upper_bound (abfd, section);
4746 if (relsize == 0)
4747 {
4748 printf (" (none)\n\n");
4749 return;
4750 }
4751
4752 if (relsize < 0)
4753 relcount = relsize;
4754 else
4755 {
4756 relpp = (arelent **) xmalloc (relsize);
4757 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
4758 }
4759
4760 if (relcount < 0)
4761 {
4762 printf ("\n");
4763 non_fatal (_("failed to read relocs in: %s"),
4764 sanitize_string (bfd_get_filename (abfd)));
4765 bfd_fatal (_("error message was"));
4766 }
4767 else if (relcount == 0)
4768 printf (" (none)\n\n");
4769 else
4770 {
4771 printf ("\n");
4772 dump_reloc_set (abfd, section, relpp, relcount);
4773 printf ("\n\n");
4774 }
4775 free (relpp);
4776 }
4777
4778 static void
4779 dump_relocs (bfd *abfd)
4780 {
4781 bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
4782 }
4783
4784 static void
4785 dump_dynamic_relocs (bfd *abfd)
4786 {
4787 long relsize;
4788 arelent **relpp;
4789 long relcount;
4790
4791 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4792 if (relsize < 0)
4793 bfd_fatal (bfd_get_filename (abfd));
4794
4795 printf ("DYNAMIC RELOCATION RECORDS");
4796
4797 if (relsize == 0)
4798 printf (" (none)\n\n");
4799 else
4800 {
4801 relpp = (arelent **) xmalloc (relsize);
4802 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
4803
4804 if (relcount < 0)
4805 bfd_fatal (bfd_get_filename (abfd));
4806 else if (relcount == 0)
4807 printf (" (none)\n\n");
4808 else
4809 {
4810 printf ("\n");
4811 dump_reloc_set (abfd, NULL, relpp, relcount);
4812 printf ("\n\n");
4813 }
4814 free (relpp);
4815 }
4816 }
4817
4818 /* Creates a table of paths, to search for source files. */
4819
4820 static void
4821 add_include_path (const char *path)
4822 {
4823 if (path[0] == 0)
4824 return;
4825 include_path_count++;
4826 include_paths = (const char **)
4827 xrealloc (include_paths, include_path_count * sizeof (*include_paths));
4828 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
4829 if (path[1] == ':' && path[2] == 0)
4830 path = concat (path, ".", (const char *) 0);
4831 #endif
4832 include_paths[include_path_count - 1] = path;
4833 }
4834
4835 static void
4836 adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
4837 asection *section,
4838 void *arg)
4839 {
4840 if ((section->flags & SEC_DEBUGGING) == 0)
4841 {
4842 bool *has_reloc_p = (bool *) arg;
4843 section->vma += adjust_section_vma;
4844 if (*has_reloc_p)
4845 section->lma += adjust_section_vma;
4846 }
4847 }
4848
4849 /* Return the sign-extended form of an ARCH_SIZE sized VMA. */
4850
4851 static bfd_vma
4852 sign_extend_address (bfd *abfd ATTRIBUTE_UNUSED,
4853 bfd_vma vma,
4854 unsigned arch_size)
4855 {
4856 bfd_vma mask;
4857 mask = (bfd_vma) 1 << (arch_size - 1);
4858 return (((vma & ((mask << 1) - 1)) ^ mask) - mask);
4859 }
4860
4861 /* Dump selected contents of ABFD. */
4862
4863 static void
4864 dump_bfd (bfd *abfd, bool is_mainfile)
4865 {
4866 const struct elf_backend_data * bed;
4867
4868 if (bfd_big_endian (abfd))
4869 byte_get = byte_get_big_endian;
4870 else if (bfd_little_endian (abfd))
4871 byte_get = byte_get_little_endian;
4872 else
4873 byte_get = NULL;
4874
4875 /* Load any separate debug information files.
4876 We do this now and without checking do_follow_links because separate
4877 debug info files may contain symbol tables that we will need when
4878 displaying information about the main file. Any memory allocated by
4879 load_separate_debug_files will be released when we call
4880 free_debug_memory below.
4881
4882 The test on is_mainfile is there because the chain of separate debug
4883 info files is a global variable shared by all invocations of dump_bfd. */
4884 if (is_mainfile)
4885 {
4886 load_separate_debug_files (abfd, bfd_get_filename (abfd));
4887
4888 /* If asked to do so, recursively dump the separate files. */
4889 if (do_follow_links)
4890 {
4891 separate_info * i;
4892
4893 for (i = first_separate_info; i != NULL; i = i->next)
4894 dump_bfd (i->handle, false);
4895 }
4896 }
4897
4898 /* Adjust user-specified start and stop limits for targets that use
4899 signed addresses. */
4900 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
4901 && (bed = get_elf_backend_data (abfd)) != NULL
4902 && bed->sign_extend_vma)
4903 {
4904 start_address = sign_extend_address (abfd, start_address,
4905 bed->s->arch_size);
4906 stop_address = sign_extend_address (abfd, stop_address,
4907 bed->s->arch_size);
4908 }
4909
4910 /* If we are adjusting section VMA's, change them all now. Changing
4911 the BFD information is a hack. However, we must do it, or
4912 bfd_find_nearest_line will not do the right thing. */
4913 if (adjust_section_vma != 0)
4914 {
4915 bool has_reloc = (abfd->flags & HAS_RELOC);
4916 bfd_map_over_sections (abfd, adjust_addresses, &has_reloc);
4917 }
4918
4919 if (! is_mainfile && ! process_links)
4920 return;
4921
4922 if (! dump_debugging_tags && ! suppress_bfd_header)
4923 printf (_("\n%s: file format %s\n"),
4924 sanitize_string (bfd_get_filename (abfd)),
4925 abfd->xvec->name);
4926 if (dump_ar_hdrs)
4927 print_arelt_descr (stdout, abfd, true, false);
4928 if (dump_file_header)
4929 dump_bfd_header (abfd);
4930 if (dump_private_headers)
4931 dump_bfd_private_header (abfd);
4932 if (dump_private_options != NULL)
4933 dump_target_specific (abfd);
4934 if (! dump_debugging_tags && ! suppress_bfd_header)
4935 putchar ('\n');
4936
4937 if (dump_symtab
4938 || dump_reloc_info
4939 || disassemble
4940 || dump_debugging
4941 || dump_dwarf_section_info)
4942 {
4943 syms = slurp_symtab (abfd);
4944
4945 /* If following links, load any symbol tables from the linked files as well. */
4946 if (do_follow_links && is_mainfile)
4947 {
4948 separate_info * i;
4949
4950 for (i = first_separate_info; i != NULL; i = i->next)
4951 {
4952 asymbol ** extra_syms;
4953 long old_symcount = symcount;
4954
4955 extra_syms = slurp_symtab (i->handle);
4956
4957 if (extra_syms)
4958 {
4959 if (old_symcount == 0)
4960 {
4961 syms = extra_syms;
4962 }
4963 else
4964 {
4965 syms = xrealloc (syms, ((symcount + old_symcount + 1)
4966 * sizeof (asymbol *)));
4967 memcpy (syms + old_symcount,
4968 extra_syms,
4969 (symcount + 1) * sizeof (asymbol *));
4970 }
4971 }
4972
4973 symcount += old_symcount;
4974 }
4975 }
4976 }
4977
4978 if (dump_section_headers)
4979 dump_headers (abfd);
4980
4981 if (dump_dynamic_symtab || dump_dynamic_reloc_info
4982 || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
4983 dynsyms = slurp_dynamic_symtab (abfd);
4984
4985 if (disassemble)
4986 {
4987 synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
4988 dynsymcount, dynsyms, &synthsyms);
4989 if (synthcount < 0)
4990 synthcount = 0;
4991 }
4992
4993 if (dump_symtab)
4994 dump_symbols (abfd, false);
4995 if (dump_dynamic_symtab)
4996 dump_symbols (abfd, true);
4997 if (dump_dwarf_section_info)
4998 dump_dwarf (abfd);
4999 if (dump_ctf_section_info)
5000 dump_ctf (abfd, dump_ctf_section_name, dump_ctf_parent_name);
5001 if (dump_stab_section_info)
5002 dump_stabs (abfd);
5003 if (dump_reloc_info && ! disassemble)
5004 dump_relocs (abfd);
5005 if (dump_dynamic_reloc_info && ! disassemble)
5006 dump_dynamic_relocs (abfd);
5007 if (dump_section_contents)
5008 dump_data (abfd);
5009 if (disassemble)
5010 disassemble_data (abfd);
5011
5012 if (dump_debugging)
5013 {
5014 void *dhandle;
5015
5016 dhandle = read_debugging_info (abfd, syms, symcount, true);
5017 if (dhandle != NULL)
5018 {
5019 if (!print_debugging_info (stdout, dhandle, abfd, syms,
5020 bfd_demangle,
5021 dump_debugging_tags != 0))
5022 {
5023 non_fatal (_("%s: printing debugging information failed"),
5024 bfd_get_filename (abfd));
5025 exit_status = 1;
5026 }
5027
5028 free (dhandle);
5029 }
5030 /* PR 6483: If there was no STABS debug info in the file, try
5031 DWARF instead. */
5032 else if (! dump_dwarf_section_info)
5033 {
5034 dwarf_select_sections_all ();
5035 dump_dwarf (abfd);
5036 }
5037 }
5038
5039 if (syms)
5040 {
5041 free (syms);
5042 syms = NULL;
5043 }
5044
5045 if (dynsyms)
5046 {
5047 free (dynsyms);
5048 dynsyms = NULL;
5049 }
5050
5051 if (synthsyms)
5052 {
5053 free (synthsyms);
5054 synthsyms = NULL;
5055 }
5056
5057 symcount = 0;
5058 dynsymcount = 0;
5059 synthcount = 0;
5060
5061 if (is_mainfile)
5062 free_debug_memory ();
5063 }
5064
5065 static void
5066 display_object_bfd (bfd *abfd)
5067 {
5068 char **matching;
5069
5070 if (bfd_check_format_matches (abfd, bfd_object, &matching))
5071 {
5072 dump_bfd (abfd, true);
5073 return;
5074 }
5075
5076 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
5077 {
5078 nonfatal (bfd_get_filename (abfd));
5079 list_matching_formats (matching);
5080 free (matching);
5081 return;
5082 }
5083
5084 if (bfd_get_error () != bfd_error_file_not_recognized)
5085 {
5086 nonfatal (bfd_get_filename (abfd));
5087 return;
5088 }
5089
5090 if (bfd_check_format_matches (abfd, bfd_core, &matching))
5091 {
5092 dump_bfd (abfd, true);
5093 return;
5094 }
5095
5096 nonfatal (bfd_get_filename (abfd));
5097
5098 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
5099 {
5100 list_matching_formats (matching);
5101 free (matching);
5102 }
5103 }
5104
5105 static void
5106 display_any_bfd (bfd *file, int level)
5107 {
5108 /* Decompress sections unless dumping the section contents. */
5109 if (!dump_section_contents)
5110 file->flags |= BFD_DECOMPRESS;
5111
5112 /* If the file is an archive, process all of its elements. */
5113 if (bfd_check_format (file, bfd_archive))
5114 {
5115 bfd *arfile = NULL;
5116 bfd *last_arfile = NULL;
5117
5118 if (level == 0)
5119 printf (_("In archive %s:\n"), sanitize_string (bfd_get_filename (file)));
5120 else if (level > 100)
5121 {
5122 /* Prevent corrupted files from spinning us into an
5123 infinite loop. 100 is an arbitrary heuristic. */
5124 fatal (_("Archive nesting is too deep"));
5125 return;
5126 }
5127 else
5128 printf (_("In nested archive %s:\n"),
5129 sanitize_string (bfd_get_filename (file)));
5130
5131 for (;;)
5132 {
5133 bfd_set_error (bfd_error_no_error);
5134
5135 arfile = bfd_openr_next_archived_file (file, arfile);
5136 if (arfile == NULL)
5137 {
5138 if (bfd_get_error () != bfd_error_no_more_archived_files)
5139 nonfatal (bfd_get_filename (file));
5140 break;
5141 }
5142
5143 display_any_bfd (arfile, level + 1);
5144
5145 if (last_arfile != NULL)
5146 {
5147 bfd_close (last_arfile);
5148 /* PR 17512: file: ac585d01. */
5149 if (arfile == last_arfile)
5150 {
5151 last_arfile = NULL;
5152 break;
5153 }
5154 }
5155 last_arfile = arfile;
5156 }
5157
5158 if (last_arfile != NULL)
5159 bfd_close (last_arfile);
5160 }
5161 else
5162 display_object_bfd (file);
5163 }
5164
5165 static void
5166 display_file (char *filename, char *target, bool last_file)
5167 {
5168 bfd *file;
5169
5170 if (get_file_size (filename) < 1)
5171 {
5172 exit_status = 1;
5173 return;
5174 }
5175
5176 file = bfd_openr (filename, target);
5177 if (file == NULL)
5178 {
5179 nonfatal (filename);
5180 return;
5181 }
5182
5183 display_any_bfd (file, 0);
5184
5185 /* This is an optimization to improve the speed of objdump, especially when
5186 dumping a file with lots of associated debug informatiom. Calling
5187 bfd_close on such a file can take a non-trivial amount of time as there
5188 are lots of lists to walk and buffers to free. This is only really
5189 necessary however if we are about to load another file and we need the
5190 memory back. Otherwise, if we are about to exit, then we can save (a lot
5191 of) time by only doing a quick close, and allowing the OS to reclaim the
5192 memory for us. */
5193 if (! last_file)
5194 bfd_close (file);
5195 else
5196 bfd_close_all_done (file);
5197 }
5198 \f
5199 int
5200 main (int argc, char **argv)
5201 {
5202 int c;
5203 char *target = default_target;
5204 bool seenflag = false;
5205
5206 #ifdef HAVE_LC_MESSAGES
5207 setlocale (LC_MESSAGES, "");
5208 #endif
5209 setlocale (LC_CTYPE, "");
5210
5211 bindtextdomain (PACKAGE, LOCALEDIR);
5212 textdomain (PACKAGE);
5213
5214 program_name = *argv;
5215 xmalloc_set_program_name (program_name);
5216 bfd_set_error_program_name (program_name);
5217
5218 START_PROGRESS (program_name, 0);
5219
5220 expandargv (&argc, &argv);
5221
5222 if (bfd_init () != BFD_INIT_MAGIC)
5223 fatal (_("fatal error: libbfd ABI mismatch"));
5224 set_default_bfd_target ();
5225
5226 while ((c = getopt_long (argc, argv,
5227 "CDE:FGHI:LM:P:RSTVW::ab:defghij:lm:prstvwxz",
5228 long_options, (int *) 0))
5229 != EOF)
5230 {
5231 switch (c)
5232 {
5233 case 0:
5234 break; /* We've been given a long option. */
5235 case 'm':
5236 machine = optarg;
5237 break;
5238 case 'M':
5239 {
5240 char *options;
5241 if (disassembler_options)
5242 /* Ignore potential memory leak for now. */
5243 options = concat (disassembler_options, ",",
5244 optarg, (const char *) NULL);
5245 else
5246 options = optarg;
5247 disassembler_options = remove_whitespace_and_extra_commas (options);
5248 }
5249 break;
5250 case 'j':
5251 add_only (optarg);
5252 break;
5253 case 'F':
5254 display_file_offsets = true;
5255 break;
5256 case 'l':
5257 with_line_numbers = true;
5258 break;
5259 case 'b':
5260 target = optarg;
5261 break;
5262 case 'C':
5263 do_demangle = true;
5264 if (optarg != NULL)
5265 {
5266 enum demangling_styles style;
5267
5268 style = cplus_demangle_name_to_style (optarg);
5269 if (style == unknown_demangling)
5270 fatal (_("unknown demangling style `%s'"),
5271 optarg);
5272
5273 cplus_demangle_set_style (style);
5274 }
5275 break;
5276 case OPTION_RECURSE_LIMIT:
5277 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5278 break;
5279 case OPTION_NO_RECURSE_LIMIT:
5280 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5281 break;
5282 case 'w':
5283 do_wide = wide_output = true;
5284 break;
5285 case OPTION_ADJUST_VMA:
5286 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
5287 break;
5288 case OPTION_START_ADDRESS:
5289 start_address = parse_vma (optarg, "--start-address");
5290 if ((stop_address != (bfd_vma) -1) && stop_address <= start_address)
5291 fatal (_("error: the start address should be before the end address"));
5292 break;
5293 case OPTION_STOP_ADDRESS:
5294 stop_address = parse_vma (optarg, "--stop-address");
5295 if ((start_address != (bfd_vma) -1) && stop_address <= start_address)
5296 fatal (_("error: the stop address should be after the start address"));
5297 break;
5298 case OPTION_PREFIX:
5299 prefix = optarg;
5300 prefix_length = strlen (prefix);
5301 /* Remove an unnecessary trailing '/' */
5302 while (IS_DIR_SEPARATOR (prefix[prefix_length - 1]))
5303 prefix_length--;
5304 break;
5305 case OPTION_PREFIX_STRIP:
5306 prefix_strip = atoi (optarg);
5307 if (prefix_strip < 0)
5308 fatal (_("error: prefix strip must be non-negative"));
5309 break;
5310 case OPTION_INSN_WIDTH:
5311 insn_width = strtoul (optarg, NULL, 0);
5312 if (insn_width <= 0)
5313 fatal (_("error: instruction width must be positive"));
5314 break;
5315 case OPTION_INLINES:
5316 unwind_inlines = true;
5317 break;
5318 case OPTION_VISUALIZE_JUMPS:
5319 visualize_jumps = true;
5320 color_output = false;
5321 extended_color_output = false;
5322 if (optarg != NULL)
5323 {
5324 if (streq (optarg, "color"))
5325 color_output = true;
5326 else if (streq (optarg, "extended-color"))
5327 {
5328 color_output = true;
5329 extended_color_output = true;
5330 }
5331 else if (streq (optarg, "off"))
5332 visualize_jumps = false;
5333 else
5334 nonfatal (_("unrecognized argument to --visualize-option"));
5335 }
5336 break;
5337 case 'E':
5338 if (strcmp (optarg, "B") == 0)
5339 endian = BFD_ENDIAN_BIG;
5340 else if (strcmp (optarg, "L") == 0)
5341 endian = BFD_ENDIAN_LITTLE;
5342 else
5343 {
5344 nonfatal (_("unrecognized -E option"));
5345 usage (stderr, 1);
5346 }
5347 break;
5348 case OPTION_ENDIAN:
5349 if (strncmp (optarg, "big", strlen (optarg)) == 0)
5350 endian = BFD_ENDIAN_BIG;
5351 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
5352 endian = BFD_ENDIAN_LITTLE;
5353 else
5354 {
5355 non_fatal (_("unrecognized --endian type `%s'"), optarg);
5356 exit_status = 1;
5357 usage (stderr, 1);
5358 }
5359 break;
5360
5361 case 'f':
5362 dump_file_header = true;
5363 seenflag = true;
5364 break;
5365 case 'i':
5366 formats_info = true;
5367 seenflag = true;
5368 break;
5369 case 'I':
5370 add_include_path (optarg);
5371 break;
5372 case 'p':
5373 dump_private_headers = true;
5374 seenflag = true;
5375 break;
5376 case 'P':
5377 dump_private_options = optarg;
5378 seenflag = true;
5379 break;
5380 case 'x':
5381 dump_private_headers = true;
5382 dump_symtab = true;
5383 dump_reloc_info = true;
5384 dump_file_header = true;
5385 dump_ar_hdrs = true;
5386 dump_section_headers = true;
5387 seenflag = true;
5388 break;
5389 case 't':
5390 dump_symtab = true;
5391 seenflag = true;
5392 break;
5393 case 'T':
5394 dump_dynamic_symtab = true;
5395 seenflag = true;
5396 break;
5397 case 'd':
5398 disassemble = true;
5399 seenflag = true;
5400 disasm_sym = optarg;
5401 break;
5402 case 'z':
5403 disassemble_zeroes = true;
5404 break;
5405 case 'D':
5406 disassemble = true;
5407 disassemble_all = true;
5408 seenflag = true;
5409 break;
5410 case 'S':
5411 disassemble = true;
5412 with_source_code = true;
5413 seenflag = true;
5414 break;
5415 case OPTION_SOURCE_COMMENT:
5416 disassemble = true;
5417 with_source_code = true;
5418 seenflag = true;
5419 if (optarg)
5420 source_comment = xstrdup (sanitize_string (optarg));
5421 else
5422 source_comment = xstrdup ("# ");
5423 break;
5424 case 'g':
5425 dump_debugging = 1;
5426 seenflag = true;
5427 break;
5428 case 'e':
5429 dump_debugging = 1;
5430 dump_debugging_tags = 1;
5431 do_demangle = true;
5432 seenflag = true;
5433 break;
5434 case 'L':
5435 process_links = true;
5436 do_follow_links = true;
5437 break;
5438 case 'W':
5439 dump_dwarf_section_info = true;
5440 seenflag = true;
5441 if (optarg)
5442 dwarf_select_sections_by_letters (optarg);
5443 else
5444 dwarf_select_sections_all ();
5445 break;
5446 case OPTION_DWARF:
5447 dump_dwarf_section_info = true;
5448 seenflag = true;
5449 if (optarg)
5450 dwarf_select_sections_by_names (optarg);
5451 else
5452 dwarf_select_sections_all ();
5453 break;
5454 case OPTION_DWARF_DEPTH:
5455 {
5456 char *cp;
5457 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
5458 }
5459 break;
5460 case OPTION_DWARF_START:
5461 {
5462 char *cp;
5463 dwarf_start_die = strtoul (optarg, & cp, 0);
5464 suppress_bfd_header = 1;
5465 }
5466 break;
5467 case OPTION_DWARF_CHECK:
5468 dwarf_check = true;
5469 break;
5470 #ifdef ENABLE_LIBCTF
5471 case OPTION_CTF:
5472 dump_ctf_section_info = true;
5473 dump_ctf_section_name = xstrdup (optarg);
5474 seenflag = true;
5475 break;
5476 case OPTION_CTF_PARENT:
5477 dump_ctf_parent_name = xstrdup (optarg);
5478 break;
5479 #endif
5480 case 'G':
5481 dump_stab_section_info = true;
5482 seenflag = true;
5483 break;
5484 case 's':
5485 dump_section_contents = true;
5486 seenflag = true;
5487 break;
5488 case 'r':
5489 dump_reloc_info = true;
5490 seenflag = true;
5491 break;
5492 case 'R':
5493 dump_dynamic_reloc_info = true;
5494 seenflag = true;
5495 break;
5496 case 'a':
5497 dump_ar_hdrs = true;
5498 seenflag = true;
5499 break;
5500 case 'h':
5501 dump_section_headers = true;
5502 seenflag = true;
5503 break;
5504 case 'v':
5505 case 'V':
5506 show_version = true;
5507 seenflag = true;
5508 break;
5509
5510 case 'H':
5511 usage (stdout, 0);
5512 /* No need to set seenflag or to break - usage() does not return. */
5513 default:
5514 usage (stderr, 1);
5515 }
5516 }
5517
5518 if (show_version)
5519 print_version ("objdump");
5520
5521 if (!seenflag)
5522 usage (stderr, 2);
5523
5524 if (formats_info)
5525 exit_status = display_info ();
5526 else
5527 {
5528 if (optind == argc)
5529 display_file ("a.out", target, true);
5530 else
5531 for (; optind < argc;)
5532 {
5533 display_file (argv[optind], target, optind == argc - 1);
5534 optind++;
5535 }
5536 }
5537
5538 free_only_list ();
5539 free (dump_ctf_section_name);
5540 free (dump_ctf_parent_name);
5541 free ((void *) source_comment);
5542
5543 END_PROGRESS (program_name);
5544
5545 return exit_status;
5546 }