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