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