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