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