1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
26 Objdump displays information about one or more object files, either on
27 their own, or inside libraries. It is commonly used as a disassembler,
28 but it can also display information about file headers, symbol tables,
29 relocations, debugging directives and more.
31 The flow of execution is as follows:
33 1. Command line arguments are checked for control switches and the
34 information to be displayed is selected.
36 2. Any remaining arguments are assumed to be object files, and they are
37 processed in order by display_bfd(). If the file is an archive each
38 of its elements is processed in turn.
40 3. The file's target architecture and binary file format are determined
41 by bfd_check_format(). If they are recognised, then dump_bfd() is
44 4. dump_bfd() in turn calls separate functions to display the requested
45 item(s) of information(s). For example disassemble_data() is called if
46 a disassembly has been requested.
48 When disassembling the code loops through blocks of instructions bounded
49 by symbols, calling disassemble_bytes() on each block. The actual
50 disassembling is done by the libopcodes library, via a function pointer
51 supplied by the disassembler() function. */
61 #include "safe-ctype.h"
63 #include "libiberty.h"
65 #include "filenames.h"
75 /* Internal headers for the ELF .stab-dump code - sorry. */
76 #define BYTES_IN_WORD 32
77 #include "aout/aout64.h"
80 static int exit_status
= 0;
82 static char *default_target
= NULL
; /* Default at runtime. */
84 /* The following variables are set based on arguments passed on the
86 static int show_version
= 0; /* Show the version number. */
87 static int dump_section_contents
; /* -s */
88 static int dump_section_headers
; /* -h */
89 static bfd_boolean dump_file_header
; /* -f */
90 static int dump_symtab
; /* -t */
91 static int dump_dynamic_symtab
; /* -T */
92 static int dump_reloc_info
; /* -r */
93 static int dump_dynamic_reloc_info
; /* -R */
94 static int dump_ar_hdrs
; /* -a */
95 static int dump_private_headers
; /* -p */
96 static int prefix_addresses
; /* --prefix-addresses */
97 static int with_line_numbers
; /* -l */
98 static bfd_boolean with_source_code
; /* -S */
99 static int show_raw_insn
; /* --show-raw-insn */
100 static int dump_dwarf_section_info
; /* --dwarf */
101 static int dump_stab_section_info
; /* --stabs */
102 static int do_demangle
; /* -C, --demangle */
103 static bfd_boolean disassemble
; /* -d */
104 static bfd_boolean disassemble_all
; /* -D */
105 static int disassemble_zeroes
; /* --disassemble-zeroes */
106 static bfd_boolean formats_info
; /* -i */
107 static int wide_output
; /* -w */
108 static int insn_width
; /* --insn-width */
109 static bfd_vma start_address
= (bfd_vma
) -1; /* --start-address */
110 static bfd_vma stop_address
= (bfd_vma
) -1; /* --stop-address */
111 static int dump_debugging
; /* --debugging */
112 static int dump_debugging_tags
; /* --debugging-tags */
113 static int dump_special_syms
= 0; /* --special-syms */
114 static bfd_vma adjust_section_vma
= 0; /* --adjust-vma */
115 static int file_start_context
= 0; /* --file-start-context */
116 static bfd_boolean display_file_offsets
;/* -F */
117 static const char *prefix
; /* --prefix */
118 static int prefix_strip
; /* --prefix-strip */
119 static size_t prefix_length
;
121 /* A structure to record the sections mentioned in -j switches. */
124 const char * name
; /* The name of the section. */
125 bfd_boolean seen
; /* A flag to indicate that the section has been found in one or more input files. */
126 struct only
* next
; /* Pointer to the next structure in the list. */
128 /* Pointer to an array of 'only' structures.
129 This pointer is NULL if the -j switch has not been used. */
130 static struct only
* only_list
= NULL
;
132 /* Variables for handling include file path table. */
133 static const char **include_paths
;
134 static int include_path_count
;
136 /* Extra info to pass to the section disassembler and address printing
138 struct objdump_disasm_info
142 bfd_boolean require_sec
;
143 arelent
** dynrelbuf
;
145 disassembler_ftype disassemble_fn
;
149 /* Architecture to disassemble for, or default if NULL. */
150 static char *machine
= NULL
;
152 /* Target specific options to the disassembler. */
153 static char *disassembler_options
= NULL
;
155 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
156 static enum bfd_endian endian
= BFD_ENDIAN_UNKNOWN
;
158 /* The symbol table. */
159 static asymbol
**syms
;
161 /* Number of symbols in `syms'. */
162 static long symcount
= 0;
164 /* The sorted symbol table. */
165 static asymbol
**sorted_syms
;
167 /* Number of symbols in `sorted_syms'. */
168 static long sorted_symcount
= 0;
170 /* The dynamic symbol table. */
171 static asymbol
**dynsyms
;
173 /* The synthetic symbol table. */
174 static asymbol
*synthsyms
;
175 static long synthcount
= 0;
177 /* Number of symbols in `dynsyms'. */
178 static long dynsymcount
= 0;
180 static bfd_byte
*stabs
;
181 static bfd_size_type stab_size
;
184 static bfd_size_type stabstr_size
;
186 static bfd_boolean is_relocatable
= FALSE
;
189 usage (FILE *stream
, int status
)
191 fprintf (stream
, _("Usage: %s <option(s)> <file(s)>\n"), program_name
);
192 fprintf (stream
, _(" Display information from object <file(s)>.\n"));
193 fprintf (stream
, _(" At least one of the following switches must be given:\n"));
194 fprintf (stream
, _("\
195 -a, --archive-headers Display archive header information\n\
196 -f, --file-headers Display the contents of the overall file header\n\
197 -p, --private-headers Display object format specific file header contents\n\
198 -h, --[section-]headers Display the contents of the section headers\n\
199 -x, --all-headers Display the contents of all headers\n\
200 -d, --disassemble Display assembler contents of executable sections\n\
201 -D, --disassemble-all Display assembler contents of all sections\n\
202 -S, --source Intermix source code with disassembly\n\
203 -s, --full-contents Display the full contents of all sections requested\n\
204 -g, --debugging Display debug information in object file\n\
205 -e, --debugging-tags Display debug information using ctags style\n\
206 -G, --stabs Display (in raw form) any STABS info in the file\n\
207 -W[lLiaprmfFsoRt] or\n\
208 --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
209 =frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
210 =gdb_index,=trace_info,=trace_abbrev,=trace_aranges]\n\
211 Display DWARF info in the file\n\
212 -t, --syms Display the contents of the symbol table(s)\n\
213 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
214 -r, --reloc Display the relocation entries in the file\n\
215 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
216 @<file> Read options from <file>\n\
217 -v, --version Display this program's version number\n\
218 -i, --info List object formats and architectures supported\n\
219 -H, --help Display this information\n\
223 fprintf (stream
, _("\n The following switches are optional:\n"));
224 fprintf (stream
, _("\
225 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
226 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
227 -j, --section=NAME Only display information for section NAME\n\
228 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
229 -EB --endian=big Assume big endian format when disassembling\n\
230 -EL --endian=little Assume little endian format when disassembling\n\
231 --file-start-context Include context from start of file (with -S)\n\
232 -I, --include=DIR Add DIR to search list for source files\n\
233 -l, --line-numbers Include line numbers and filenames in output\n\
234 -F, --file-offsets Include file offsets when displaying information\n\
235 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
236 The STYLE, if specified, can be `auto', `gnu',\n\
237 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
239 -w, --wide Format output for more than 80 columns\n\
240 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
241 --start-address=ADDR Only process data whose address is >= ADDR\n\
242 --stop-address=ADDR Only process data whose address is <= ADDR\n\
243 --prefix-addresses Print complete address alongside disassembly\n\
244 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
245 --insn-width=WIDTH Display WIDTH bytes on a signle line for -d\n\
246 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
247 --special-syms Include special symbols in symbol dumps\n\
248 --prefix=PREFIX Add PREFIX to absolute paths for -S\n\
249 --prefix-strip=LEVEL Strip initial directory names for -S\n\
251 list_supported_targets (program_name
, stream
);
252 list_supported_architectures (program_name
, stream
);
254 disassembler_usage (stream
);
256 if (REPORT_BUGS_TO
[0] && status
== 0)
257 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
261 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
265 OPTION_START_ADDRESS
,
274 static struct option long_options
[]=
276 {"adjust-vma", required_argument
, NULL
, OPTION_ADJUST_VMA
},
277 {"all-headers", no_argument
, NULL
, 'x'},
278 {"private-headers", no_argument
, NULL
, 'p'},
279 {"architecture", required_argument
, NULL
, 'm'},
280 {"archive-headers", no_argument
, NULL
, 'a'},
281 {"debugging", no_argument
, NULL
, 'g'},
282 {"debugging-tags", no_argument
, NULL
, 'e'},
283 {"demangle", optional_argument
, NULL
, 'C'},
284 {"disassemble", no_argument
, NULL
, 'd'},
285 {"disassemble-all", no_argument
, NULL
, 'D'},
286 {"disassembler-options", required_argument
, NULL
, 'M'},
287 {"disassemble-zeroes", no_argument
, NULL
, 'z'},
288 {"dynamic-reloc", no_argument
, NULL
, 'R'},
289 {"dynamic-syms", no_argument
, NULL
, 'T'},
290 {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
291 {"file-headers", no_argument
, NULL
, 'f'},
292 {"file-offsets", no_argument
, NULL
, 'F'},
293 {"file-start-context", no_argument
, &file_start_context
, 1},
294 {"full-contents", no_argument
, NULL
, 's'},
295 {"headers", no_argument
, NULL
, 'h'},
296 {"help", no_argument
, NULL
, 'H'},
297 {"info", no_argument
, NULL
, 'i'},
298 {"line-numbers", no_argument
, NULL
, 'l'},
299 {"no-show-raw-insn", no_argument
, &show_raw_insn
, -1},
300 {"prefix-addresses", no_argument
, &prefix_addresses
, 1},
301 {"reloc", no_argument
, NULL
, 'r'},
302 {"section", required_argument
, NULL
, 'j'},
303 {"section-headers", no_argument
, NULL
, 'h'},
304 {"show-raw-insn", no_argument
, &show_raw_insn
, 1},
305 {"source", no_argument
, NULL
, 'S'},
306 {"special-syms", no_argument
, &dump_special_syms
, 1},
307 {"include", required_argument
, NULL
, 'I'},
308 {"dwarf", optional_argument
, NULL
, OPTION_DWARF
},
309 {"stabs", no_argument
, NULL
, 'G'},
310 {"start-address", required_argument
, NULL
, OPTION_START_ADDRESS
},
311 {"stop-address", required_argument
, NULL
, OPTION_STOP_ADDRESS
},
312 {"syms", no_argument
, NULL
, 't'},
313 {"target", required_argument
, NULL
, 'b'},
314 {"version", no_argument
, NULL
, 'V'},
315 {"wide", no_argument
, NULL
, 'w'},
316 {"prefix", required_argument
, NULL
, OPTION_PREFIX
},
317 {"prefix-strip", required_argument
, NULL
, OPTION_PREFIX_STRIP
},
318 {"insn-width", required_argument
, NULL
, OPTION_INSN_WIDTH
},
319 {0, no_argument
, 0, 0}
323 nonfatal (const char *msg
)
329 /* Returns TRUE if the specified section should be dumped. */
332 process_section_p (asection
* section
)
336 if (only_list
== NULL
)
339 for (only
= only_list
; only
; only
= only
->next
)
340 if (strcmp (only
->name
, section
->name
) == 0)
349 /* Add an entry to the 'only' list. */
352 add_only (char * name
)
356 /* First check to make sure that we do not
357 already have an entry for this name. */
358 for (only
= only_list
; only
; only
= only
->next
)
359 if (strcmp (only
->name
, name
) == 0)
362 only
= xmalloc (sizeof * only
);
365 only
->next
= only_list
;
369 /* Release the memory used by the 'only' list.
370 PR 11225: Issue a warning message for unseen sections.
371 Only do this if none of the sections were seen. This is mainly to support
372 tools like the GAS testsuite where an object file is dumped with a list of
373 generic section names known to be present in a range of different file
377 free_only_list (void)
379 bfd_boolean at_least_one_seen
= FALSE
;
383 if (only_list
== NULL
)
386 for (only
= only_list
; only
; only
= only
->next
)
389 at_least_one_seen
= TRUE
;
393 for (only
= only_list
; only
; only
= next
)
395 if (! at_least_one_seen
)
397 non_fatal (_("section '%s' mentioned in a -j option, "
398 "but not found in any input file"),
409 dump_section_header (bfd
*abfd
, asection
*section
,
410 void *ignored ATTRIBUTE_UNUSED
)
413 unsigned int opb
= bfd_octets_per_byte (abfd
);
415 /* Ignore linker created section. See elfNN_ia64_object_p in
417 if (section
->flags
& SEC_LINKER_CREATED
)
420 /* PR 10413: Skip sections that we are ignoring. */
421 if (! process_section_p (section
))
424 printf ("%3d %-13s %08lx ", section
->index
,
425 bfd_get_section_name (abfd
, section
),
426 (unsigned long) bfd_section_size (abfd
, section
) / opb
);
427 bfd_printf_vma (abfd
, bfd_get_section_vma (abfd
, section
));
429 bfd_printf_vma (abfd
, section
->lma
);
430 printf (" %08lx 2**%u", (unsigned long) section
->filepos
,
431 bfd_get_section_alignment (abfd
, section
));
437 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
439 PF (SEC_HAS_CONTENTS
, "CONTENTS");
440 PF (SEC_ALLOC
, "ALLOC");
441 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
442 PF (SEC_LOAD
, "LOAD");
443 PF (SEC_RELOC
, "RELOC");
444 PF (SEC_READONLY
, "READONLY");
445 PF (SEC_CODE
, "CODE");
446 PF (SEC_DATA
, "DATA");
448 PF (SEC_DEBUGGING
, "DEBUGGING");
449 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
450 PF (SEC_EXCLUDE
, "EXCLUDE");
451 PF (SEC_SORT_ENTRIES
, "SORT_ENTRIES");
452 if (bfd_get_arch (abfd
) == bfd_arch_tic54x
)
454 PF (SEC_TIC54X_BLOCK
, "BLOCK");
455 PF (SEC_TIC54X_CLINK
, "CLINK");
457 PF (SEC_SMALL_DATA
, "SMALL_DATA");
458 if (bfd_get_flavour (abfd
) == bfd_target_coff_flavour
)
459 PF (SEC_COFF_SHARED
, "SHARED");
460 PF (SEC_THREAD_LOCAL
, "THREAD_LOCAL");
461 PF (SEC_GROUP
, "GROUP");
463 if ((section
->flags
& SEC_LINK_ONCE
) != 0)
466 struct coff_comdat_info
*comdat
;
468 switch (section
->flags
& SEC_LINK_DUPLICATES
)
472 case SEC_LINK_DUPLICATES_DISCARD
:
473 ls
= "LINK_ONCE_DISCARD";
475 case SEC_LINK_DUPLICATES_ONE_ONLY
:
476 ls
= "LINK_ONCE_ONE_ONLY";
478 case SEC_LINK_DUPLICATES_SAME_SIZE
:
479 ls
= "LINK_ONCE_SAME_SIZE";
481 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
482 ls
= "LINK_ONCE_SAME_CONTENTS";
485 printf ("%s%s", comma
, ls
);
487 comdat
= bfd_coff_get_comdat_section (abfd
, section
);
489 printf (" (COMDAT %s %ld)", comdat
->name
, comdat
->symbol
);
499 dump_headers (bfd
*abfd
)
501 printf (_("Sections:\n"));
504 printf (_("Idx Name Size VMA LMA File off Algn"));
506 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
507 if (bfd_get_arch_size (abfd
) == 32)
508 printf (_("Idx Name Size VMA LMA File off Algn"));
510 printf (_("Idx Name Size VMA LMA File off Algn"));
514 printf (_(" Flags"));
517 bfd_map_over_sections (abfd
, dump_section_header
, NULL
);
521 slurp_symtab (bfd
*abfd
)
526 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
532 storage
= bfd_get_symtab_upper_bound (abfd
);
534 bfd_fatal (bfd_get_filename (abfd
));
536 sy
= (asymbol
**) xmalloc (storage
);
538 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
540 bfd_fatal (bfd_get_filename (abfd
));
544 /* Read in the dynamic symbols. */
547 slurp_dynamic_symtab (bfd
*abfd
)
552 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
555 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
557 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd
));
563 bfd_fatal (bfd_get_filename (abfd
));
566 sy
= (asymbol
**) xmalloc (storage
);
568 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
570 bfd_fatal (bfd_get_filename (abfd
));
574 /* Filter out (in place) symbols that are useless for disassembly.
575 COUNT is the number of elements in SYMBOLS.
576 Return the number of useful symbols. */
579 remove_useless_symbols (asymbol
**symbols
, long count
)
581 asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
585 asymbol
*sym
= *in_ptr
++;
587 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
589 if (sym
->flags
& (BSF_DEBUGGING
| BSF_SECTION_SYM
))
591 if (bfd_is_und_section (sym
->section
)
592 || bfd_is_com_section (sym
->section
))
597 return out_ptr
- symbols
;
600 /* Sort symbols into value order. */
603 compare_symbols (const void *ap
, const void *bp
)
605 const asymbol
*a
= * (const asymbol
**) ap
;
606 const asymbol
*b
= * (const asymbol
**) bp
;
616 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
618 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
621 if (a
->section
> b
->section
)
623 else if (a
->section
< b
->section
)
626 an
= bfd_asymbol_name (a
);
627 bn
= bfd_asymbol_name (b
);
631 /* The symbols gnu_compiled and gcc2_compiled convey no real
632 information, so put them after other symbols with the same value. */
633 af
= (strstr (an
, "gnu_compiled") != NULL
634 || strstr (an
, "gcc2_compiled") != NULL
);
635 bf
= (strstr (bn
, "gnu_compiled") != NULL
636 || strstr (bn
, "gcc2_compiled") != NULL
);
643 /* We use a heuristic for the file name, to try to sort it after
644 more useful symbols. It may not work on non Unix systems, but it
645 doesn't really matter; the only difference is precisely which
646 symbol names get printed. */
648 #define file_symbol(s, sn, snl) \
649 (((s)->flags & BSF_FILE) != 0 \
650 || ((sn)[(snl) - 2] == '.' \
651 && ((sn)[(snl) - 1] == 'o' \
652 || (sn)[(snl) - 1] == 'a')))
654 af
= file_symbol (a
, an
, anl
);
655 bf
= file_symbol (b
, bn
, bnl
);
662 /* Try to sort global symbols before local symbols before function
663 symbols before debugging symbols. */
668 if ((aflags
& BSF_DEBUGGING
) != (bflags
& BSF_DEBUGGING
))
670 if ((aflags
& BSF_DEBUGGING
) != 0)
675 if ((aflags
& BSF_FUNCTION
) != (bflags
& BSF_FUNCTION
))
677 if ((aflags
& BSF_FUNCTION
) != 0)
682 if ((aflags
& BSF_LOCAL
) != (bflags
& BSF_LOCAL
))
684 if ((aflags
& BSF_LOCAL
) != 0)
689 if ((aflags
& BSF_GLOBAL
) != (bflags
& BSF_GLOBAL
))
691 if ((aflags
& BSF_GLOBAL
) != 0)
697 /* Symbols that start with '.' might be section names, so sort them
698 after symbols that don't start with '.'. */
699 if (an
[0] == '.' && bn
[0] != '.')
701 if (an
[0] != '.' && bn
[0] == '.')
704 /* Finally, if we can't distinguish them in any other way, try to
705 get consistent results by sorting the symbols by name. */
706 return strcmp (an
, bn
);
709 /* Sort relocs into address order. */
712 compare_relocs (const void *ap
, const void *bp
)
714 const arelent
*a
= * (const arelent
**) ap
;
715 const arelent
*b
= * (const arelent
**) bp
;
717 if (a
->address
> b
->address
)
719 else if (a
->address
< b
->address
)
722 /* So that associated relocations tied to the same address show up
723 in the correct order, we don't do any further sorting. */
732 /* Print an address (VMA) to the output stream in INFO.
733 If SKIP_ZEROES is TRUE, omit leading zeroes. */
736 objdump_print_value (bfd_vma vma
, struct disassemble_info
*inf
,
737 bfd_boolean skip_zeroes
)
741 struct objdump_disasm_info
*aux
;
743 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
744 bfd_sprintf_vma (aux
->abfd
, buf
, vma
);
749 for (p
= buf
; *p
== '0'; ++p
)
754 (*inf
->fprintf_func
) (inf
->stream
, "%s", p
);
757 /* Print the name of a symbol. */
760 objdump_print_symname (bfd
*abfd
, struct disassemble_info
*inf
,
767 name
= bfd_asymbol_name (sym
);
768 if (do_demangle
&& name
[0] != '\0')
770 /* Demangle the name. */
771 alloc
= bfd_demangle (abfd
, name
, DMGL_ANSI
| DMGL_PARAMS
);
777 (*inf
->fprintf_func
) (inf
->stream
, "%s", name
);
785 /* Locate a symbol given a bfd and a section (from INFO->application_data),
786 and a VMA. If INFO->application_data->require_sec is TRUE, then always
787 require the symbol to be in the section. Returns NULL if there is no
788 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
789 of the symbol in sorted_syms. */
792 find_symbol_for_address (bfd_vma vma
,
793 struct disassemble_info
*inf
,
796 /* @@ Would it speed things up to cache the last two symbols returned,
797 and maybe their address ranges? For many processors, only one memory
798 operand can be present at a time, so the 2-entry cache wouldn't be
799 constantly churned by code doing heavy memory accesses. */
801 /* Indices in `sorted_syms'. */
803 long max_count
= sorted_symcount
;
805 struct objdump_disasm_info
*aux
;
809 bfd_boolean want_section
;
811 if (sorted_symcount
< 1)
814 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
817 opb
= inf
->octets_per_byte
;
819 /* Perform a binary search looking for the closest symbol to the
820 required value. We are searching the range (min, max_count]. */
821 while (min
+ 1 < max_count
)
825 thisplace
= (max_count
+ min
) / 2;
826 sym
= sorted_syms
[thisplace
];
828 if (bfd_asymbol_value (sym
) > vma
)
829 max_count
= thisplace
;
830 else if (bfd_asymbol_value (sym
) < vma
)
839 /* The symbol we want is now in min, the low end of the range we
840 were searching. If there are several symbols with the same
841 value, we want the first one. */
844 && (bfd_asymbol_value (sorted_syms
[thisplace
])
845 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
848 /* Prefer a symbol in the current section if we have multple symbols
849 with the same value, as can occur with overlays or zero size
852 while (min
< max_count
853 && (bfd_asymbol_value (sorted_syms
[min
])
854 == bfd_asymbol_value (sorted_syms
[thisplace
])))
856 if (sorted_syms
[min
]->section
== sec
857 && inf
->symbol_is_valid (sorted_syms
[min
], inf
))
864 return sorted_syms
[thisplace
];
869 /* If the file is relocatable, and the symbol could be from this
870 section, prefer a symbol from this section over symbols from
871 others, even if the other symbol's value might be closer.
873 Note that this may be wrong for some symbol references if the
874 sections have overlapping memory ranges, but in that case there's
875 no way to tell what's desired without looking at the relocation
878 Also give the target a chance to reject symbols. */
879 want_section
= (aux
->require_sec
880 || ((abfd
->flags
& HAS_RELOC
) != 0
881 && vma
>= bfd_get_section_vma (abfd
, sec
)
882 && vma
< (bfd_get_section_vma (abfd
, sec
)
883 + bfd_section_size (abfd
, sec
) / opb
)));
884 if ((sorted_syms
[thisplace
]->section
!= sec
&& want_section
)
885 || ! inf
->symbol_is_valid (sorted_syms
[thisplace
], inf
))
888 long newplace
= sorted_symcount
;
890 for (i
= min
- 1; i
>= 0; i
--)
892 if ((sorted_syms
[i
]->section
== sec
|| !want_section
)
893 && inf
->symbol_is_valid (sorted_syms
[i
], inf
))
895 if (newplace
== sorted_symcount
)
898 if (bfd_asymbol_value (sorted_syms
[i
])
899 != bfd_asymbol_value (sorted_syms
[newplace
]))
902 /* Remember this symbol and keep searching until we reach
903 an earlier address. */
908 if (newplace
!= sorted_symcount
)
909 thisplace
= newplace
;
912 /* We didn't find a good symbol with a smaller value.
913 Look for one with a larger value. */
914 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
916 if ((sorted_syms
[i
]->section
== sec
|| !want_section
)
917 && inf
->symbol_is_valid (sorted_syms
[i
], inf
))
925 if ((sorted_syms
[thisplace
]->section
!= sec
&& want_section
)
926 || ! inf
->symbol_is_valid (sorted_syms
[thisplace
], inf
))
927 /* There is no suitable symbol. */
934 return sorted_syms
[thisplace
];
937 /* Print an address and the offset to the nearest symbol. */
940 objdump_print_addr_with_sym (bfd
*abfd
, asection
*sec
, asymbol
*sym
,
941 bfd_vma vma
, struct disassemble_info
*inf
,
942 bfd_boolean skip_zeroes
)
944 objdump_print_value (vma
, inf
, skip_zeroes
);
950 (*inf
->fprintf_func
) (inf
->stream
, " <%s",
951 bfd_get_section_name (abfd
, sec
));
952 secaddr
= bfd_get_section_vma (abfd
, sec
);
955 (*inf
->fprintf_func
) (inf
->stream
, "-0x");
956 objdump_print_value (secaddr
- vma
, inf
, TRUE
);
958 else if (vma
> secaddr
)
960 (*inf
->fprintf_func
) (inf
->stream
, "+0x");
961 objdump_print_value (vma
- secaddr
, inf
, TRUE
);
963 (*inf
->fprintf_func
) (inf
->stream
, ">");
967 (*inf
->fprintf_func
) (inf
->stream
, " <");
968 objdump_print_symname (abfd
, inf
, sym
);
969 if (bfd_asymbol_value (sym
) > vma
)
971 (*inf
->fprintf_func
) (inf
->stream
, "-0x");
972 objdump_print_value (bfd_asymbol_value (sym
) - vma
, inf
, TRUE
);
974 else if (vma
> bfd_asymbol_value (sym
))
976 (*inf
->fprintf_func
) (inf
->stream
, "+0x");
977 objdump_print_value (vma
- bfd_asymbol_value (sym
), inf
, TRUE
);
979 (*inf
->fprintf_func
) (inf
->stream
, ">");
982 if (display_file_offsets
)
983 inf
->fprintf_func (inf
->stream
, _(" (File Offset: 0x%lx)"),
984 (long int)(sec
->filepos
+ (vma
- sec
->vma
)));
987 /* Print an address (VMA), symbolically if possible.
988 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
991 objdump_print_addr (bfd_vma vma
,
992 struct disassemble_info
*inf
,
993 bfd_boolean skip_zeroes
)
995 struct objdump_disasm_info
*aux
;
997 bfd_boolean skip_find
= FALSE
;
999 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1001 if (sorted_symcount
< 1)
1003 (*inf
->fprintf_func
) (inf
->stream
, "0x");
1004 objdump_print_value (vma
, inf
, skip_zeroes
);
1006 if (display_file_offsets
)
1007 inf
->fprintf_func (inf
->stream
, _(" (File Offset: 0x%lx)"),
1008 (long int)(aux
->sec
->filepos
+ (vma
- aux
->sec
->vma
)));
1012 if (aux
->reloc
!= NULL
1013 && aux
->reloc
->sym_ptr_ptr
!= NULL
1014 && * aux
->reloc
->sym_ptr_ptr
!= NULL
)
1016 sym
= * aux
->reloc
->sym_ptr_ptr
;
1018 /* Adjust the vma to the reloc. */
1019 vma
+= bfd_asymbol_value (sym
);
1021 if (bfd_is_und_section (bfd_get_section (sym
)))
1026 sym
= find_symbol_for_address (vma
, inf
, NULL
);
1028 objdump_print_addr_with_sym (aux
->abfd
, aux
->sec
, sym
, vma
, inf
,
1032 /* Print VMA to INFO. This function is passed to the disassembler
1036 objdump_print_address (bfd_vma vma
, struct disassemble_info
*inf
)
1038 objdump_print_addr (vma
, inf
, ! prefix_addresses
);
1041 /* Determine if the given address has a symbol associated with it. */
1044 objdump_symbol_at_address (bfd_vma vma
, struct disassemble_info
* inf
)
1048 sym
= find_symbol_for_address (vma
, inf
, NULL
);
1050 return (sym
!= NULL
&& (bfd_asymbol_value (sym
) == vma
));
1053 /* Hold the last function name and the last line number we displayed
1054 in a disassembly. */
1056 static char *prev_functionname
;
1057 static unsigned int prev_line
;
1059 /* We keep a list of all files that we have seen when doing a
1060 disassembly with source, so that we know how much of the file to
1061 display. This can be important for inlined functions. */
1063 struct print_file_list
1065 struct print_file_list
*next
;
1066 const char *filename
;
1067 const char *modname
;
1070 const char **linemap
;
1076 static struct print_file_list
*print_files
;
1078 /* The number of preceding context lines to show when we start
1079 displaying a file for the first time. */
1081 #define SHOW_PRECEDING_CONTEXT_LINES (5)
1083 /* Read a complete file into memory. */
1086 slurp_file (const char *fn
, size_t *size
)
1089 int ps
= getpagesize ();
1094 int fd
= open (fn
, O_RDONLY
| O_BINARY
);
1098 if (fstat (fd
, &st
) < 0)
1102 msize
= (*size
+ ps
- 1) & ~(ps
- 1);
1103 map
= mmap (NULL
, msize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1104 if (map
!= (char *)-1L)
1110 map
= (const char *) malloc (*size
);
1111 if (!map
|| (size_t) read (fd
, (char *)map
, *size
) != *size
)
1120 #define line_map_decrease 5
1122 /* Precompute array of lines for a mapped file. */
1124 static const char **
1125 index_file (const char *map
, size_t size
, unsigned int *maxline
)
1127 const char *p
, *lstart
, *end
;
1128 int chars_per_line
= 45; /* First iteration will use 40. */
1129 unsigned int lineno
;
1130 const char **linemap
= NULL
;
1131 unsigned long line_map_size
= 0;
1137 for (p
= map
; p
< end
; p
++)
1141 if (p
+ 1 < end
&& p
[1] == '\r')
1144 else if (*p
== '\r')
1146 if (p
+ 1 < end
&& p
[1] == '\n')
1152 /* End of line found. */
1154 if (linemap
== NULL
|| line_map_size
< lineno
+ 1)
1156 unsigned long newsize
;
1158 chars_per_line
-= line_map_decrease
;
1159 if (chars_per_line
<= 1)
1161 line_map_size
= size
/ chars_per_line
+ 1;
1162 if (line_map_size
< lineno
+ 1)
1163 line_map_size
= lineno
+ 1;
1164 newsize
= line_map_size
* sizeof (char *);
1165 linemap
= (const char **) xrealloc (linemap
, newsize
);
1168 linemap
[lineno
++] = lstart
;
1176 /* Tries to open MODNAME, and if successful adds a node to print_files
1177 linked list and returns that node. Returns NULL on failure. */
1179 static struct print_file_list
*
1180 try_print_file_open (const char *origname
, const char *modname
)
1182 struct print_file_list
*p
;
1184 p
= (struct print_file_list
*) xmalloc (sizeof (struct print_file_list
));
1186 p
->map
= slurp_file (modname
, &p
->mapsize
);
1193 p
->linemap
= index_file (p
->map
, p
->mapsize
, &p
->maxline
);
1195 p
->filename
= origname
;
1196 p
->modname
= modname
;
1197 p
->next
= print_files
;
1203 /* If the the source file, as described in the symtab, is not found
1204 try to locate it in one of the paths specified with -I
1205 If found, add location to print_files linked list. */
1207 static struct print_file_list
*
1208 update_source_path (const char *filename
)
1210 struct print_file_list
*p
;
1214 p
= try_print_file_open (filename
, filename
);
1218 if (include_path_count
== 0)
1221 /* Get the name of the file. */
1222 fname
= lbasename (filename
);
1224 /* If file exists under a new path, we need to add it to the list
1225 so that show_line knows about it. */
1226 for (i
= 0; i
< include_path_count
; i
++)
1228 char *modname
= concat (include_paths
[i
], "/", fname
, (const char *) 0);
1230 p
= try_print_file_open (filename
, modname
);
1240 /* Print a source file line. */
1243 print_line (struct print_file_list
*p
, unsigned int linenum
)
1249 if (linenum
>= p
->maxline
)
1251 l
= p
->linemap
[linenum
];
1252 /* Test fwrite return value to quiet glibc warning. */
1253 len
= strcspn (l
, "\n\r");
1254 if (len
== 0 || fwrite (l
, len
, 1, stdout
) == 1)
1258 /* Print a range of source code lines. */
1261 dump_lines (struct print_file_list
*p
, unsigned int start
, unsigned int end
)
1265 while (start
<= end
)
1267 print_line (p
, start
);
1272 /* Show the line number, or the source line, in a disassembly
1276 show_line (bfd
*abfd
, asection
*section
, bfd_vma addr_offset
)
1278 const char *filename
;
1279 const char *functionname
;
1280 unsigned int linenumber
;
1283 if (! with_line_numbers
&& ! with_source_code
)
1286 if (! bfd_find_nearest_line (abfd
, section
, syms
, addr_offset
, &filename
,
1287 &functionname
, &linenumber
))
1290 if (filename
!= NULL
&& *filename
== '\0')
1292 if (functionname
!= NULL
&& *functionname
== '\0')
1293 functionname
= NULL
;
1296 && IS_ABSOLUTE_PATH (filename
)
1300 const char *fname
= filename
;
1301 char *path
= (char *) alloca (prefix_length
+ PATH_MAX
+ 1);
1304 memcpy (path
, prefix
, prefix_length
);
1305 path_up
= path
+ prefix_length
;
1307 /* Build relocated filename, stripping off leading directories
1308 from the initial filename if requested. */
1309 if (prefix_strip
> 0)
1314 /* Skip selected directory levels. */
1315 for (s
= fname
+ 1; *s
!= '\0' && level
< prefix_strip
; s
++)
1316 if (IS_DIR_SEPARATOR(*s
))
1323 /* Update complete filename. */
1324 strncpy (path_up
, fname
, PATH_MAX
);
1325 path_up
[PATH_MAX
] = '\0';
1333 if (with_line_numbers
)
1335 if (functionname
!= NULL
1336 && (prev_functionname
== NULL
1337 || strcmp (functionname
, prev_functionname
) != 0))
1338 printf ("%s():\n", functionname
);
1339 if (linenumber
> 0 && linenumber
!= prev_line
)
1340 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, linenumber
);
1343 if (with_source_code
1347 struct print_file_list
**pp
, *p
;
1350 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1351 if (strcmp ((*pp
)->filename
, filename
) == 0)
1358 filename
= xstrdup (filename
);
1359 p
= update_source_path (filename
);
1362 if (p
!= NULL
&& linenumber
!= p
->last_line
)
1364 if (file_start_context
&& p
->first
)
1368 l
= linenumber
- SHOW_PRECEDING_CONTEXT_LINES
;
1369 if (l
>= linenumber
)
1371 if (p
->last_line
>= l
&& p
->last_line
<= linenumber
)
1372 l
= p
->last_line
+ 1;
1374 dump_lines (p
, l
, linenumber
);
1375 p
->last_line
= linenumber
;
1380 if (functionname
!= NULL
1381 && (prev_functionname
== NULL
1382 || strcmp (functionname
, prev_functionname
) != 0))
1384 if (prev_functionname
!= NULL
)
1385 free (prev_functionname
);
1386 prev_functionname
= (char *) xmalloc (strlen (functionname
) + 1);
1387 strcpy (prev_functionname
, functionname
);
1390 if (linenumber
> 0 && linenumber
!= prev_line
)
1391 prev_line
= linenumber
;
1394 /* Pseudo FILE object for strings. */
1402 /* sprintf to a "stream". */
1404 static int ATTRIBUTE_PRINTF_2
1405 objdump_sprintf (SFILE
*f
, const char *format
, ...)
1412 size_t space
= f
->alloc
- f
->pos
;
1414 va_start (args
, format
);
1415 n
= vsnprintf (f
->buffer
+ f
->pos
, space
, format
, args
);
1421 f
->alloc
= (f
->alloc
+ n
) * 2;
1422 f
->buffer
= (char *) xrealloc (f
->buffer
, f
->alloc
);
1429 /* The number of zeroes we want to see before we start skipping them.
1430 The number is arbitrarily chosen. */
1432 #define DEFAULT_SKIP_ZEROES 8
1434 /* The number of zeroes to skip at the end of a section. If the
1435 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1436 SKIP_ZEROES, they will be disassembled. If there are fewer than
1437 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1438 attempt to avoid disassembling zeroes inserted by section
1441 #define DEFAULT_SKIP_ZEROES_AT_END 3
1443 /* Disassemble some data in memory between given values. */
1446 disassemble_bytes (struct disassemble_info
* inf
,
1447 disassembler_ftype disassemble_fn
,
1450 bfd_vma start_offset
,
1451 bfd_vma stop_offset
,
1454 arelent
** relppend
)
1456 struct objdump_disasm_info
*aux
;
1458 int octets_per_line
;
1459 int skip_addr_chars
;
1460 bfd_vma addr_offset
;
1461 unsigned int opb
= inf
->octets_per_byte
;
1462 unsigned int skip_zeroes
= inf
->skip_zeroes
;
1463 unsigned int skip_zeroes_at_end
= inf
->skip_zeroes_at_end
;
1467 aux
= (struct objdump_disasm_info
*) inf
->application_data
;
1471 sfile
.buffer
= (char *) xmalloc (sfile
.alloc
);
1475 octets_per_line
= insn_width
;
1477 octets_per_line
= 4;
1479 octets_per_line
= 16;
1481 /* Figure out how many characters to skip at the start of an
1482 address, to make the disassembly look nicer. We discard leading
1483 zeroes in chunks of 4, ensuring that there is always a leading
1485 skip_addr_chars
= 0;
1486 if (! prefix_addresses
)
1490 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ section
->size
/ opb
);
1492 while (buf
[skip_addr_chars
] == '0')
1495 /* Don't discard zeros on overflow. */
1496 if (buf
[skip_addr_chars
] == '\0' && section
->vma
!= 0)
1497 skip_addr_chars
= 0;
1499 if (skip_addr_chars
!= 0)
1500 skip_addr_chars
= (skip_addr_chars
- 1) & -4;
1503 inf
->insn_info_valid
= 0;
1505 addr_offset
= start_offset
;
1506 while (addr_offset
< stop_offset
)
1509 bfd_boolean need_nl
= FALSE
;
1510 int previous_octets
;
1512 /* Remember the length of the previous instruction. */
1513 previous_octets
= octets
;
1516 /* Make sure we don't use relocs from previous instructions. */
1519 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1521 for (z
= addr_offset
* opb
; z
< stop_offset
* opb
; z
++)
1524 if (! disassemble_zeroes
1525 && (inf
->insn_info_valid
== 0
1526 || inf
->branch_delay_insns
== 0)
1527 && (z
- addr_offset
* opb
>= skip_zeroes
1528 || (z
== stop_offset
* opb
&&
1529 z
- addr_offset
* opb
< skip_zeroes_at_end
)))
1531 /* If there are more nonzero octets to follow, we only skip
1532 zeroes in multiples of 4, to try to avoid running over
1533 the start of an instruction which happens to start with
1535 if (z
!= stop_offset
* opb
)
1536 z
= addr_offset
* opb
+ ((z
- addr_offset
* opb
) &~ 3);
1538 octets
= z
- addr_offset
* opb
;
1540 /* If we are going to display more data, and we are displaying
1541 file offsets, then tell the user how many zeroes we skip
1542 and the file offset from where we resume dumping. */
1543 if (display_file_offsets
&& ((addr_offset
+ (octets
/ opb
)) < stop_offset
))
1544 printf ("\t... (skipping %d zeroes, resuming at file offset: 0x%lx)\n",
1546 (unsigned long) (section
->filepos
1547 + (addr_offset
+ (octets
/ opb
))));
1557 if (with_line_numbers
|| with_source_code
)
1558 show_line (aux
->abfd
, section
, addr_offset
);
1560 if (! prefix_addresses
)
1564 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ addr_offset
);
1565 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
1569 printf ("%s:\t", buf
+ skip_addr_chars
);
1573 aux
->require_sec
= TRUE
;
1574 objdump_print_address (section
->vma
+ addr_offset
, inf
);
1575 aux
->require_sec
= FALSE
;
1582 inf
->fprintf_func
= (fprintf_ftype
) objdump_sprintf
;
1583 inf
->stream
= &sfile
;
1584 inf
->bytes_per_line
= 0;
1585 inf
->bytes_per_chunk
= 0;
1586 inf
->flags
= disassemble_all
? DISASSEMBLE_DATA
: 0;
1588 inf
->flags
|= USER_SPECIFIED_MACHINE_TYPE
;
1590 if (inf
->disassembler_needs_relocs
1591 && (bfd_get_file_flags (aux
->abfd
) & EXEC_P
) == 0
1592 && (bfd_get_file_flags (aux
->abfd
) & DYNAMIC
) == 0
1593 && *relppp
< relppend
)
1595 bfd_signed_vma distance_to_rel
;
1597 distance_to_rel
= (**relppp
)->address
1598 - (rel_offset
+ addr_offset
);
1600 /* Check to see if the current reloc is associated with
1601 the instruction that we are about to disassemble. */
1602 if (distance_to_rel
== 0
1603 /* FIXME: This is wrong. We are trying to catch
1604 relocs that are addressed part way through the
1605 current instruction, as might happen with a packed
1606 VLIW instruction. Unfortunately we do not know the
1607 length of the current instruction since we have not
1608 disassembled it yet. Instead we take a guess based
1609 upon the length of the previous instruction. The
1610 proper solution is to have a new target-specific
1611 disassembler function which just returns the length
1612 of an instruction at a given address without trying
1613 to display its disassembly. */
1614 || (distance_to_rel
> 0
1615 && distance_to_rel
< (bfd_signed_vma
) (previous_octets
/ opb
)))
1617 inf
->flags
|= INSN_HAS_RELOC
;
1618 aux
->reloc
= **relppp
;
1622 octets
= (*disassemble_fn
) (section
->vma
+ addr_offset
, inf
);
1623 inf
->fprintf_func
= (fprintf_ftype
) fprintf
;
1624 inf
->stream
= stdout
;
1625 if (insn_width
== 0 && inf
->bytes_per_line
!= 0)
1626 octets_per_line
= inf
->bytes_per_line
;
1627 if (octets
< (int) opb
)
1630 printf ("%s\n", sfile
.buffer
);
1633 non_fatal (_("disassemble_fn returned length %d"),
1644 octets
= octets_per_line
;
1645 if (addr_offset
+ octets
/ opb
> stop_offset
)
1646 octets
= (stop_offset
- addr_offset
) * opb
;
1648 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ octets
; ++j
)
1650 if (ISPRINT (data
[j
]))
1651 buf
[j
- addr_offset
* opb
] = data
[j
];
1653 buf
[j
- addr_offset
* opb
] = '.';
1655 buf
[j
- addr_offset
* opb
] = '\0';
1658 if (prefix_addresses
1660 : show_raw_insn
>= 0)
1664 /* If ! prefix_addresses and ! wide_output, we print
1665 octets_per_line octets per line. */
1667 if (pb
> octets_per_line
&& ! prefix_addresses
&& ! wide_output
)
1668 pb
= octets_per_line
;
1670 if (inf
->bytes_per_chunk
)
1671 bpc
= inf
->bytes_per_chunk
;
1675 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
1679 if (bpc
> 1 && inf
->display_endian
== BFD_ENDIAN_LITTLE
)
1681 for (k
= bpc
- 1; k
>= 0; k
--)
1682 printf ("%02x", (unsigned) data
[j
+ k
]);
1687 for (k
= 0; k
< bpc
; k
++)
1688 printf ("%02x", (unsigned) data
[j
+ k
]);
1693 for (; pb
< octets_per_line
; pb
+= bpc
)
1697 for (k
= 0; k
< bpc
; k
++)
1702 /* Separate raw data from instruction by extra space. */
1712 printf ("%s", sfile
.buffer
);
1714 if (prefix_addresses
1716 : show_raw_insn
>= 0)
1724 j
= addr_offset
* opb
+ pb
;
1726 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ j
/ opb
);
1727 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
1731 printf ("%s:\t", buf
+ skip_addr_chars
);
1733 pb
+= octets_per_line
;
1736 for (; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
1740 if (bpc
> 1 && inf
->display_endian
== BFD_ENDIAN_LITTLE
)
1742 for (k
= bpc
- 1; k
>= 0; k
--)
1743 printf ("%02x", (unsigned) data
[j
+ k
]);
1748 for (k
= 0; k
< bpc
; k
++)
1749 printf ("%02x", (unsigned) data
[j
+ k
]);
1762 while ((*relppp
) < relppend
1763 && (**relppp
)->address
< rel_offset
+ addr_offset
+ octets
/ opb
)
1765 if (dump_reloc_info
|| dump_dynamic_reloc_info
)
1776 objdump_print_value (section
->vma
- rel_offset
+ q
->address
,
1779 if (q
->howto
== NULL
)
1780 printf (": *unknown*\t");
1781 else if (q
->howto
->name
)
1782 printf (": %s\t", q
->howto
->name
);
1784 printf (": %d\t", q
->howto
->type
);
1786 if (q
->sym_ptr_ptr
== NULL
|| *q
->sym_ptr_ptr
== NULL
)
1787 printf ("*unknown*");
1790 const char *sym_name
;
1792 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
1793 if (sym_name
!= NULL
&& *sym_name
!= '\0')
1794 objdump_print_symname (aux
->abfd
, inf
, *q
->sym_ptr_ptr
);
1799 sym_sec
= bfd_get_section (*q
->sym_ptr_ptr
);
1800 sym_name
= bfd_get_section_name (aux
->abfd
, sym_sec
);
1801 if (sym_name
== NULL
|| *sym_name
== '\0')
1802 sym_name
= "*unknown*";
1803 printf ("%s", sym_name
);
1810 objdump_print_value (q
->addend
, inf
, TRUE
);
1822 addr_offset
+= octets
/ opb
;
1825 free (sfile
.buffer
);
1829 disassemble_section (bfd
*abfd
, asection
*section
, void *inf
)
1831 const struct elf_backend_data
* bed
;
1832 bfd_vma sign_adjust
= 0;
1833 struct disassemble_info
* pinfo
= (struct disassemble_info
*) inf
;
1834 struct objdump_disasm_info
* paux
;
1835 unsigned int opb
= pinfo
->octets_per_byte
;
1836 bfd_byte
* data
= NULL
;
1837 bfd_size_type datasize
= 0;
1838 arelent
** rel_pp
= NULL
;
1839 arelent
** rel_ppstart
= NULL
;
1840 arelent
** rel_ppend
;
1841 unsigned long stop_offset
;
1842 asymbol
* sym
= NULL
;
1846 unsigned long addr_offset
;
1848 /* Sections that do not contain machine
1849 code are not normally disassembled. */
1850 if (! disassemble_all
1851 && only_list
== NULL
1852 && ((section
->flags
& (SEC_CODE
| SEC_HAS_CONTENTS
))
1853 != (SEC_CODE
| SEC_HAS_CONTENTS
)))
1856 if (! process_section_p (section
))
1859 datasize
= bfd_get_section_size (section
);
1863 /* Decide which set of relocs to use. Load them if necessary. */
1864 paux
= (struct objdump_disasm_info
*) pinfo
->application_data
;
1865 if (paux
->dynrelbuf
)
1867 rel_pp
= paux
->dynrelbuf
;
1868 rel_count
= paux
->dynrelcount
;
1869 /* Dynamic reloc addresses are absolute, non-dynamic are section
1870 relative. REL_OFFSET specifies the reloc address corresponding
1871 to the start of this section. */
1872 rel_offset
= section
->vma
;
1880 if ((section
->flags
& SEC_RELOC
) != 0
1881 && (dump_reloc_info
|| pinfo
->disassembler_needs_relocs
))
1885 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
1887 bfd_fatal (bfd_get_filename (abfd
));
1891 rel_ppstart
= rel_pp
= (arelent
**) xmalloc (relsize
);
1892 rel_count
= bfd_canonicalize_reloc (abfd
, section
, rel_pp
, syms
);
1894 bfd_fatal (bfd_get_filename (abfd
));
1896 /* Sort the relocs by address. */
1897 qsort (rel_pp
, rel_count
, sizeof (arelent
*), compare_relocs
);
1901 rel_ppend
= rel_pp
+ rel_count
;
1903 data
= (bfd_byte
*) xmalloc (datasize
);
1905 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
1907 paux
->sec
= section
;
1908 pinfo
->buffer
= data
;
1909 pinfo
->buffer_vma
= section
->vma
;
1910 pinfo
->buffer_length
= datasize
;
1911 pinfo
->section
= section
;
1913 if (start_address
== (bfd_vma
) -1
1914 || start_address
< pinfo
->buffer_vma
)
1917 addr_offset
= start_address
- pinfo
->buffer_vma
;
1919 if (stop_address
== (bfd_vma
) -1)
1920 stop_offset
= datasize
/ opb
;
1923 if (stop_address
< pinfo
->buffer_vma
)
1926 stop_offset
= stop_address
- pinfo
->buffer_vma
;
1927 if (stop_offset
> pinfo
->buffer_length
/ opb
)
1928 stop_offset
= pinfo
->buffer_length
/ opb
;
1931 /* Skip over the relocs belonging to addresses below the
1933 while (rel_pp
< rel_ppend
1934 && (*rel_pp
)->address
< rel_offset
+ addr_offset
)
1937 if (addr_offset
< stop_offset
)
1938 printf (_("\nDisassembly of section %s:\n"), section
->name
);
1940 /* Find the nearest symbol forwards from our current position. */
1941 paux
->require_sec
= TRUE
;
1942 sym
= (asymbol
*) find_symbol_for_address (section
->vma
+ addr_offset
,
1943 (struct disassemble_info
*) inf
,
1945 paux
->require_sec
= FALSE
;
1947 /* PR 9774: If the target used signed addresses then we must make
1948 sure that we sign extend the value that we calculate for 'addr'
1949 in the loop below. */
1950 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1951 && (bed
= get_elf_backend_data (abfd
)) != NULL
1952 && bed
->sign_extend_vma
)
1953 sign_adjust
= (bfd_vma
) 1 << (bed
->s
->arch_size
- 1);
1955 /* Disassemble a block of instructions up to the address associated with
1956 the symbol we have just found. Then print the symbol and find the
1957 next symbol on. Repeat until we have disassembled the entire section
1958 or we have reached the end of the address range we are interested in. */
1959 while (addr_offset
< stop_offset
)
1963 unsigned long nextstop_offset
;
1966 addr
= section
->vma
+ addr_offset
;
1967 addr
= ((addr
& ((sign_adjust
<< 1) - 1)) ^ sign_adjust
) - sign_adjust
;
1969 if (sym
!= NULL
&& bfd_asymbol_value (sym
) <= addr
)
1974 (x
< sorted_symcount
1975 && (bfd_asymbol_value (sorted_syms
[x
]) <= addr
));
1979 pinfo
->symbols
= sorted_syms
+ place
;
1980 pinfo
->num_symbols
= x
- place
;
1981 pinfo
->symtab_pos
= place
;
1985 pinfo
->symbols
= NULL
;
1986 pinfo
->num_symbols
= 0;
1987 pinfo
->symtab_pos
= -1;
1990 if (! prefix_addresses
)
1992 pinfo
->fprintf_func (pinfo
->stream
, "\n");
1993 objdump_print_addr_with_sym (abfd
, section
, sym
, addr
,
1995 pinfo
->fprintf_func (pinfo
->stream
, ":\n");
1998 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > addr
)
2000 else if (sym
== NULL
)
2004 #define is_valid_next_sym(SYM) \
2005 ((SYM)->section == section \
2006 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
2007 && pinfo->symbol_is_valid (SYM, pinfo))
2009 /* Search forward for the next appropriate symbol in
2010 SECTION. Note that all the symbols are sorted
2011 together into one big array, and that some sections
2012 may have overlapping addresses. */
2013 while (place
< sorted_symcount
2014 && ! is_valid_next_sym (sorted_syms
[place
]))
2017 if (place
>= sorted_symcount
)
2020 nextsym
= sorted_syms
[place
];
2023 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > addr
)
2024 nextstop_offset
= bfd_asymbol_value (sym
) - section
->vma
;
2025 else if (nextsym
== NULL
)
2026 nextstop_offset
= stop_offset
;
2028 nextstop_offset
= bfd_asymbol_value (nextsym
) - section
->vma
;
2030 if (nextstop_offset
> stop_offset
2031 || nextstop_offset
<= addr_offset
)
2032 nextstop_offset
= stop_offset
;
2034 /* If a symbol is explicitly marked as being an object
2035 rather than a function, just dump the bytes without
2036 disassembling them. */
2039 || sym
->section
!= section
2040 || bfd_asymbol_value (sym
) > addr
2041 || ((sym
->flags
& BSF_OBJECT
) == 0
2042 && (strstr (bfd_asymbol_name (sym
), "gnu_compiled")
2044 && (strstr (bfd_asymbol_name (sym
), "gcc2_compiled")
2046 || (sym
->flags
& BSF_FUNCTION
) != 0)
2051 disassemble_bytes (pinfo
, paux
->disassemble_fn
, insns
, data
,
2052 addr_offset
, nextstop_offset
,
2053 rel_offset
, &rel_pp
, rel_ppend
);
2055 addr_offset
= nextstop_offset
;
2061 if (rel_ppstart
!= NULL
)
2065 /* Disassemble the contents of an object file. */
2068 disassemble_data (bfd
*abfd
)
2070 struct disassemble_info disasm_info
;
2071 struct objdump_disasm_info aux
;
2075 prev_functionname
= NULL
;
2078 /* We make a copy of syms to sort. We don't want to sort syms
2079 because that will screw up the relocs. */
2080 sorted_symcount
= symcount
? symcount
: dynsymcount
;
2081 sorted_syms
= (asymbol
**) xmalloc ((sorted_symcount
+ synthcount
)
2082 * sizeof (asymbol
*));
2083 memcpy (sorted_syms
, symcount
? syms
: dynsyms
,
2084 sorted_symcount
* sizeof (asymbol
*));
2086 sorted_symcount
= remove_useless_symbols (sorted_syms
, sorted_symcount
);
2088 for (i
= 0; i
< synthcount
; ++i
)
2090 sorted_syms
[sorted_symcount
] = synthsyms
+ i
;
2094 /* Sort the symbols into section and symbol order. */
2095 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
2097 init_disassemble_info (&disasm_info
, stdout
, (fprintf_ftype
) fprintf
);
2099 disasm_info
.application_data
= (void *) &aux
;
2101 aux
.require_sec
= FALSE
;
2102 aux
.dynrelbuf
= NULL
;
2103 aux
.dynrelcount
= 0;
2106 disasm_info
.print_address_func
= objdump_print_address
;
2107 disasm_info
.symbol_at_address_func
= objdump_symbol_at_address
;
2109 if (machine
!= NULL
)
2111 const bfd_arch_info_type
*inf
= bfd_scan_arch (machine
);
2114 fatal (_("can't use supplied machine %s"), machine
);
2116 abfd
->arch_info
= inf
;
2119 if (endian
!= BFD_ENDIAN_UNKNOWN
)
2121 struct bfd_target
*xvec
;
2123 xvec
= (struct bfd_target
*) xmalloc (sizeof (struct bfd_target
));
2124 memcpy (xvec
, abfd
->xvec
, sizeof (struct bfd_target
));
2125 xvec
->byteorder
= endian
;
2129 /* Use libopcodes to locate a suitable disassembler. */
2130 aux
.disassemble_fn
= disassembler (abfd
);
2131 if (!aux
.disassemble_fn
)
2133 non_fatal (_("can't disassemble for architecture %s\n"),
2134 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
2139 disasm_info
.flavour
= bfd_get_flavour (abfd
);
2140 disasm_info
.arch
= bfd_get_arch (abfd
);
2141 disasm_info
.mach
= bfd_get_mach (abfd
);
2142 disasm_info
.disassembler_options
= disassembler_options
;
2143 disasm_info
.octets_per_byte
= bfd_octets_per_byte (abfd
);
2144 disasm_info
.skip_zeroes
= DEFAULT_SKIP_ZEROES
;
2145 disasm_info
.skip_zeroes_at_end
= DEFAULT_SKIP_ZEROES_AT_END
;
2146 disasm_info
.disassembler_needs_relocs
= FALSE
;
2148 if (bfd_big_endian (abfd
))
2149 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_BIG
;
2150 else if (bfd_little_endian (abfd
))
2151 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
2153 /* ??? Aborting here seems too drastic. We could default to big or little
2155 disasm_info
.endian
= BFD_ENDIAN_UNKNOWN
;
2157 /* Allow the target to customize the info structure. */
2158 disassemble_init_for_target (& disasm_info
);
2160 /* Pre-load the dynamic relocs if we are going
2161 to be dumping them along with the disassembly. */
2162 if (dump_dynamic_reloc_info
)
2164 long relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
2167 bfd_fatal (bfd_get_filename (abfd
));
2171 aux
.dynrelbuf
= (arelent
**) xmalloc (relsize
);
2172 aux
.dynrelcount
= bfd_canonicalize_dynamic_reloc (abfd
,
2175 if (aux
.dynrelcount
< 0)
2176 bfd_fatal (bfd_get_filename (abfd
));
2178 /* Sort the relocs by address. */
2179 qsort (aux
.dynrelbuf
, aux
.dynrelcount
, sizeof (arelent
*),
2183 disasm_info
.symtab
= sorted_syms
;
2184 disasm_info
.symtab_size
= sorted_symcount
;
2186 bfd_map_over_sections (abfd
, disassemble_section
, & disasm_info
);
2188 if (aux
.dynrelbuf
!= NULL
)
2189 free (aux
.dynrelbuf
);
2194 load_specific_debug_section (enum dwarf_section_display_enum debug
,
2195 asection
*sec
, void *file
)
2197 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
2198 bfd
*abfd
= (bfd
*) file
;
2201 /* If it is already loaded, do nothing. */
2202 if (section
->start
!= NULL
)
2205 section
->address
= 0;
2206 section
->size
= bfd_get_section_size (sec
);
2207 section
->start
= NULL
;
2208 ret
= bfd_get_full_section_contents (abfd
, sec
, §ion
->start
);
2212 free_debug_section (debug
);
2213 printf (_("\nCan't get contents for section '%s'.\n"),
2218 if (is_relocatable
&& debug_displays
[debug
].relocate
)
2220 /* We want to relocate the data we've already read (and
2221 decompressed), so we store a pointer to the data in
2222 the bfd_section, and tell it that the contents are
2223 already in memory. */
2224 sec
->contents
= section
->start
;
2225 sec
->flags
|= SEC_IN_MEMORY
;
2226 sec
->size
= section
->size
;
2228 ret
= bfd_simple_get_relocated_section_contents (abfd
,
2235 free_debug_section (debug
);
2236 printf (_("\nCan't get contents for section '%s'.\n"),
2246 load_debug_section (enum dwarf_section_display_enum debug
, void *file
)
2248 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
2249 bfd
*abfd
= (bfd
*) file
;
2252 /* If it is already loaded, do nothing. */
2253 if (section
->start
!= NULL
)
2256 /* Locate the debug section. */
2257 sec
= bfd_get_section_by_name (abfd
, section
->uncompressed_name
);
2259 section
->name
= section
->uncompressed_name
;
2262 sec
= bfd_get_section_by_name (abfd
, section
->compressed_name
);
2264 section
->name
= section
->compressed_name
;
2269 return load_specific_debug_section (debug
, sec
, file
);
2273 free_debug_section (enum dwarf_section_display_enum debug
)
2275 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
2277 if (section
->start
== NULL
)
2280 free ((char *) section
->start
);
2281 section
->start
= NULL
;
2282 section
->address
= 0;
2287 dump_dwarf_section (bfd
*abfd
, asection
*section
,
2288 void *arg ATTRIBUTE_UNUSED
)
2290 const char *name
= bfd_get_section_name (abfd
, section
);
2294 if (CONST_STRNEQ (name
, ".gnu.linkonce.wi."))
2295 match
= ".debug_info";
2299 for (i
= 0; i
< max
; i
++)
2300 if ((strcmp (debug_displays
[i
].section
.uncompressed_name
, match
) == 0
2301 || strcmp (debug_displays
[i
].section
.compressed_name
, match
) == 0)
2302 && debug_displays
[i
].enabled
!= NULL
2303 && *debug_displays
[i
].enabled
)
2305 struct dwarf_section
*sec
= &debug_displays
[i
].section
;
2307 if (strcmp (sec
->uncompressed_name
, match
) == 0)
2308 sec
->name
= sec
->uncompressed_name
;
2310 sec
->name
= sec
->compressed_name
;
2311 if (load_specific_debug_section ((enum dwarf_section_display_enum
) i
,
2314 debug_displays
[i
].display (sec
, abfd
);
2316 if (i
!= info
&& i
!= abbrev
)
2317 free_debug_section ((enum dwarf_section_display_enum
) i
);
2323 /* Dump the dwarf debugging information. */
2326 dump_dwarf (bfd
*abfd
)
2328 is_relocatable
= (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0;
2330 eh_addr_size
= bfd_arch_bits_per_address (abfd
) / 8;
2332 if (bfd_big_endian (abfd
))
2333 byte_get
= byte_get_big_endian
;
2334 else if (bfd_little_endian (abfd
))
2335 byte_get
= byte_get_little_endian
;
2339 switch (bfd_get_arch (abfd
))
2342 switch (bfd_get_mach (abfd
))
2344 case bfd_mach_x86_64
:
2345 case bfd_mach_x86_64_intel_syntax
:
2346 init_dwarf_regnames_x86_64 ();
2350 init_dwarf_regnames_i386 ();
2359 bfd_map_over_sections (abfd
, dump_dwarf_section
, NULL
);
2361 free_debug_memory ();
2364 /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
2365 it. Return NULL on failure. */
2368 read_section_stabs (bfd
*abfd
, const char *sect_name
, bfd_size_type
*size_ptr
)
2374 stabsect
= bfd_get_section_by_name (abfd
, sect_name
);
2375 if (stabsect
== NULL
)
2377 printf (_("No %s section present\n\n"), sect_name
);
2381 size
= bfd_section_size (abfd
, stabsect
);
2382 contents
= (char *) xmalloc (size
);
2384 if (! bfd_get_section_contents (abfd
, stabsect
, contents
, 0, size
))
2386 non_fatal (_("reading %s section of %s failed: %s"),
2387 sect_name
, bfd_get_filename (abfd
),
2388 bfd_errmsg (bfd_get_error ()));
2399 /* Stabs entries use a 12 byte format:
2400 4 byte string table index
2402 1 byte stab other field
2403 2 byte stab desc field
2405 FIXME: This will have to change for a 64 bit object format. */
2407 #define STRDXOFF (0)
2409 #define OTHEROFF (5)
2412 #define STABSIZE (12)
2414 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
2415 using string table section STRSECT_NAME (in `strtab'). */
2418 print_section_stabs (bfd
*abfd
,
2419 const char *stabsect_name
,
2420 unsigned *string_offset_ptr
)
2423 unsigned file_string_table_offset
= 0;
2424 unsigned next_file_string_table_offset
= *string_offset_ptr
;
2425 bfd_byte
*stabp
, *stabs_end
;
2428 stabs_end
= stabp
+ stab_size
;
2430 printf (_("Contents of %s section:\n\n"), stabsect_name
);
2431 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
2433 /* Loop through all symbols and print them.
2435 We start the index at -1 because there is a dummy symbol on
2436 the front of stabs-in-{coff,elf} sections that supplies sizes. */
2437 for (i
= -1; stabp
< stabs_end
; stabp
+= STABSIZE
, i
++)
2441 unsigned char type
, other
;
2442 unsigned short desc
;
2445 strx
= bfd_h_get_32 (abfd
, stabp
+ STRDXOFF
);
2446 type
= bfd_h_get_8 (abfd
, stabp
+ TYPEOFF
);
2447 other
= bfd_h_get_8 (abfd
, stabp
+ OTHEROFF
);
2448 desc
= bfd_h_get_16 (abfd
, stabp
+ DESCOFF
);
2449 value
= bfd_h_get_32 (abfd
, stabp
+ VALOFF
);
2451 printf ("\n%-6d ", i
);
2452 /* Either print the stab name, or, if unnamed, print its number
2453 again (makes consistent formatting for tools like awk). */
2454 name
= bfd_get_stab_name (type
);
2456 printf ("%-6s", name
);
2457 else if (type
== N_UNDF
)
2460 printf ("%-6d", type
);
2461 printf (" %-6d %-6d ", other
, desc
);
2462 bfd_printf_vma (abfd
, value
);
2463 printf (" %-6lu", strx
);
2465 /* Symbols with type == 0 (N_UNDF) specify the length of the
2466 string table associated with this file. We use that info
2467 to know how to relocate the *next* file's string table indices. */
2470 file_string_table_offset
= next_file_string_table_offset
;
2471 next_file_string_table_offset
+= value
;
2475 /* Using the (possibly updated) string table offset, print the
2476 string (if any) associated with this symbol. */
2477 if ((strx
+ file_string_table_offset
) < stabstr_size
)
2478 printf (" %s", &strtab
[strx
+ file_string_table_offset
]);
2484 *string_offset_ptr
= next_file_string_table_offset
;
2489 const char * section_name
;
2490 const char * string_section_name
;
2491 unsigned string_offset
;
2496 find_stabs_section (bfd
*abfd
, asection
*section
, void *names
)
2499 stab_section_names
* sought
= (stab_section_names
*) names
;
2501 /* Check for section names for which stabsect_name is a prefix, to
2502 handle .stab.N, etc. */
2503 len
= strlen (sought
->section_name
);
2505 /* If the prefix matches, and the files section name ends with a
2506 nul or a digit, then we match. I.e., we want either an exact
2507 match or a section followed by a number. */
2508 if (strncmp (sought
->section_name
, section
->name
, len
) == 0
2509 && (section
->name
[len
] == 0
2510 || (section
->name
[len
] == '.' && ISDIGIT (section
->name
[len
+ 1]))))
2513 strtab
= read_section_stabs (abfd
, sought
->string_section_name
,
2518 stabs
= (bfd_byte
*) read_section_stabs (abfd
, section
->name
,
2521 print_section_stabs (abfd
, section
->name
, &sought
->string_offset
);
2527 dump_stabs_section (bfd
*abfd
, char *stabsect_name
, char *strsect_name
)
2529 stab_section_names s
;
2531 s
.section_name
= stabsect_name
;
2532 s
.string_section_name
= strsect_name
;
2533 s
.string_offset
= 0;
2535 bfd_map_over_sections (abfd
, find_stabs_section
, & s
);
2541 /* Dump the any sections containing stabs debugging information. */
2544 dump_stabs (bfd
*abfd
)
2546 dump_stabs_section (abfd
, ".stab", ".stabstr");
2547 dump_stabs_section (abfd
, ".stab.excl", ".stab.exclstr");
2548 dump_stabs_section (abfd
, ".stab.index", ".stab.indexstr");
2551 dump_stabs_section (abfd
, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
2553 dump_stabs_section (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
2557 dump_bfd_header (bfd
*abfd
)
2561 printf (_("architecture: %s, "),
2562 bfd_printable_arch_mach (bfd_get_arch (abfd
),
2563 bfd_get_mach (abfd
)));
2564 printf (_("flags 0x%08x:\n"), abfd
->flags
& ~BFD_FLAGS_FOR_BFD_USE_MASK
);
2566 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2567 PF (HAS_RELOC
, "HAS_RELOC");
2568 PF (EXEC_P
, "EXEC_P");
2569 PF (HAS_LINENO
, "HAS_LINENO");
2570 PF (HAS_DEBUG
, "HAS_DEBUG");
2571 PF (HAS_SYMS
, "HAS_SYMS");
2572 PF (HAS_LOCALS
, "HAS_LOCALS");
2573 PF (DYNAMIC
, "DYNAMIC");
2574 PF (WP_TEXT
, "WP_TEXT");
2575 PF (D_PAGED
, "D_PAGED");
2576 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
2577 PF (HAS_LOAD_PAGE
, "HAS_LOAD_PAGE");
2578 printf (_("\nstart address 0x"));
2579 bfd_printf_vma (abfd
, abfd
->start_address
);
2585 dump_bfd_private_header (bfd
*abfd
)
2587 bfd_print_private_bfd_data (abfd
, stdout
);
2591 /* Display a section in hexadecimal format with associated characters.
2592 Each line prefixed by the zero padded address. */
2595 dump_section (bfd
*abfd
, asection
*section
, void *dummy ATTRIBUTE_UNUSED
)
2598 bfd_size_type datasize
;
2599 bfd_size_type addr_offset
;
2600 bfd_size_type start_offset
;
2601 bfd_size_type stop_offset
;
2602 unsigned int opb
= bfd_octets_per_byte (abfd
);
2603 /* Bytes per line. */
2604 const int onaline
= 16;
2609 if ((section
->flags
& SEC_HAS_CONTENTS
) == 0)
2612 if (! process_section_p (section
))
2615 if ((datasize
= bfd_section_size (abfd
, section
)) == 0)
2618 /* Compute the address range to display. */
2619 if (start_address
== (bfd_vma
) -1
2620 || start_address
< section
->vma
)
2623 start_offset
= start_address
- section
->vma
;
2625 if (stop_address
== (bfd_vma
) -1)
2626 stop_offset
= datasize
/ opb
;
2629 if (stop_address
< section
->vma
)
2632 stop_offset
= stop_address
- section
->vma
;
2634 if (stop_offset
> datasize
/ opb
)
2635 stop_offset
= datasize
/ opb
;
2638 if (start_offset
>= stop_offset
)
2641 printf (_("Contents of section %s:"), section
->name
);
2642 if (display_file_offsets
)
2643 printf (_(" (Starting at file offset: 0x%lx)"),
2644 (unsigned long) (section
->filepos
+ start_offset
));
2647 if (!bfd_get_full_section_contents (abfd
, section
, &data
))
2649 non_fatal (_("Reading section failed"));
2655 bfd_sprintf_vma (abfd
, buf
, start_offset
+ section
->vma
);
2656 if (strlen (buf
) >= sizeof (buf
))
2660 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
2662 count
= strlen (buf
) - count
;
2666 bfd_sprintf_vma (abfd
, buf
, stop_offset
+ section
->vma
- 1);
2667 if (strlen (buf
) >= sizeof (buf
))
2671 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
2673 count
= strlen (buf
) - count
;
2677 for (addr_offset
= start_offset
;
2678 addr_offset
< stop_offset
; addr_offset
+= onaline
/ opb
)
2682 bfd_sprintf_vma (abfd
, buf
, (addr_offset
+ section
->vma
));
2683 count
= strlen (buf
);
2684 if ((size_t) count
>= sizeof (buf
))
2688 while (count
< width
)
2693 fputs (buf
+ count
- width
, stdout
);
2696 for (j
= addr_offset
* opb
;
2697 j
< addr_offset
* opb
+ onaline
; j
++)
2699 if (j
< stop_offset
* opb
)
2700 printf ("%02x", (unsigned) (data
[j
]));
2708 for (j
= addr_offset
* opb
;
2709 j
< addr_offset
* opb
+ onaline
; j
++)
2711 if (j
>= stop_offset
* opb
)
2714 printf ("%c", ISPRINT (data
[j
]) ? data
[j
] : '.');
2721 /* Actually display the various requested regions. */
2724 dump_data (bfd
*abfd
)
2726 bfd_map_over_sections (abfd
, dump_section
, NULL
);
2729 /* Should perhaps share code and display with nm? */
2732 dump_symbols (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_boolean dynamic
)
2741 max_count
= dynsymcount
;
2742 printf ("DYNAMIC SYMBOL TABLE:\n");
2747 max_count
= symcount
;
2748 printf ("SYMBOL TABLE:\n");
2752 printf (_("no symbols\n"));
2754 for (count
= 0; count
< max_count
; count
++)
2758 if (*current
== NULL
)
2759 printf (_("no information for symbol number %ld\n"), count
);
2761 else if ((cur_bfd
= bfd_asymbol_bfd (*current
)) == NULL
)
2762 printf (_("could not determine the type of symbol number %ld\n"),
2765 else if (process_section_p ((* current
)->section
)
2766 && (dump_special_syms
2767 || !bfd_is_target_special_symbol (cur_bfd
, *current
)))
2769 const char *name
= (*current
)->name
;
2771 if (do_demangle
&& name
!= NULL
&& *name
!= '\0')
2775 /* If we want to demangle the name, we demangle it
2776 here, and temporarily clobber it while calling
2777 bfd_print_symbol. FIXME: This is a gross hack. */
2778 alloc
= bfd_demangle (cur_bfd
, name
, DMGL_ANSI
| DMGL_PARAMS
);
2780 (*current
)->name
= alloc
;
2781 bfd_print_symbol (cur_bfd
, stdout
, *current
,
2782 bfd_print_symbol_all
);
2785 (*current
)->name
= name
;
2790 bfd_print_symbol (cur_bfd
, stdout
, *current
,
2791 bfd_print_symbol_all
);
2801 dump_reloc_set (bfd
*abfd
, asection
*sec
, arelent
**relpp
, long relcount
)
2804 char *last_filename
, *last_functionname
;
2805 unsigned int last_line
;
2807 /* Get column headers lined up reasonably. */
2815 bfd_sprintf_vma (abfd
, buf
, (bfd_vma
) -1);
2816 width
= strlen (buf
) - 7;
2818 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
2821 last_filename
= NULL
;
2822 last_functionname
= NULL
;
2825 for (p
= relpp
; relcount
&& *p
!= NULL
; p
++, relcount
--)
2828 const char *filename
, *functionname
;
2829 unsigned int linenumber
;
2830 const char *sym_name
;
2831 const char *section_name
;
2833 if (start_address
!= (bfd_vma
) -1
2834 && q
->address
< start_address
)
2836 if (stop_address
!= (bfd_vma
) -1
2837 && q
->address
> stop_address
)
2840 if (with_line_numbers
2842 && bfd_find_nearest_line (abfd
, sec
, syms
, q
->address
,
2843 &filename
, &functionname
, &linenumber
))
2845 if (functionname
!= NULL
2846 && (last_functionname
== NULL
2847 || strcmp (functionname
, last_functionname
) != 0))
2849 printf ("%s():\n", functionname
);
2850 if (last_functionname
!= NULL
)
2851 free (last_functionname
);
2852 last_functionname
= xstrdup (functionname
);
2856 && (linenumber
!= last_line
2857 || (filename
!= NULL
2858 && last_filename
!= NULL
2859 && strcmp (filename
, last_filename
) != 0)))
2861 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, linenumber
);
2862 last_line
= linenumber
;
2863 if (last_filename
!= NULL
)
2864 free (last_filename
);
2865 if (filename
== NULL
)
2866 last_filename
= NULL
;
2868 last_filename
= xstrdup (filename
);
2872 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
2874 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
2875 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
2880 section_name
= NULL
;
2883 bfd_printf_vma (abfd
, q
->address
);
2884 if (q
->howto
== NULL
)
2885 printf (" *unknown* ");
2886 else if (q
->howto
->name
)
2887 printf (" %-16s ", q
->howto
->name
);
2889 printf (" %-16d ", q
->howto
->type
);
2893 objdump_print_symname (abfd
, NULL
, *q
->sym_ptr_ptr
);
2897 if (section_name
== NULL
)
2898 section_name
= "*unknown*";
2899 printf ("[%s]", section_name
);
2905 bfd_printf_vma (abfd
, q
->addend
);
2913 dump_relocs_in_section (bfd
*abfd
,
2915 void *dummy ATTRIBUTE_UNUSED
)
2921 if ( bfd_is_abs_section (section
)
2922 || bfd_is_und_section (section
)
2923 || bfd_is_com_section (section
)
2924 || (! process_section_p (section
))
2925 || ((section
->flags
& SEC_RELOC
) == 0))
2928 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
2930 bfd_fatal (bfd_get_filename (abfd
));
2932 printf ("RELOCATION RECORDS FOR [%s]:", section
->name
);
2936 printf (" (none)\n\n");
2940 relpp
= (arelent
**) xmalloc (relsize
);
2941 relcount
= bfd_canonicalize_reloc (abfd
, section
, relpp
, syms
);
2944 bfd_fatal (bfd_get_filename (abfd
));
2945 else if (relcount
== 0)
2946 printf (" (none)\n\n");
2950 dump_reloc_set (abfd
, section
, relpp
, relcount
);
2957 dump_relocs (bfd
*abfd
)
2959 bfd_map_over_sections (abfd
, dump_relocs_in_section
, NULL
);
2963 dump_dynamic_relocs (bfd
*abfd
)
2969 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
2971 bfd_fatal (bfd_get_filename (abfd
));
2973 printf ("DYNAMIC RELOCATION RECORDS");
2976 printf (" (none)\n\n");
2979 relpp
= (arelent
**) xmalloc (relsize
);
2980 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
2983 bfd_fatal (bfd_get_filename (abfd
));
2984 else if (relcount
== 0)
2985 printf (" (none)\n\n");
2989 dump_reloc_set (abfd
, NULL
, relpp
, relcount
);
2996 /* Creates a table of paths, to search for source files. */
2999 add_include_path (const char *path
)
3003 include_path_count
++;
3004 include_paths
= (const char **)
3005 xrealloc (include_paths
, include_path_count
* sizeof (*include_paths
));
3006 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3007 if (path
[1] == ':' && path
[2] == 0)
3008 path
= concat (path
, ".", (const char *) 0);
3010 include_paths
[include_path_count
- 1] = path
;
3014 adjust_addresses (bfd
*abfd ATTRIBUTE_UNUSED
,
3018 if ((section
->flags
& SEC_DEBUGGING
) == 0)
3020 bfd_boolean
*has_reloc_p
= (bfd_boolean
*) arg
;
3021 section
->vma
+= adjust_section_vma
;
3023 section
->lma
+= adjust_section_vma
;
3027 /* Dump selected contents of ABFD. */
3030 dump_bfd (bfd
*abfd
)
3032 /* If we are adjusting section VMA's, change them all now. Changing
3033 the BFD information is a hack. However, we must do it, or
3034 bfd_find_nearest_line will not do the right thing. */
3035 if (adjust_section_vma
!= 0)
3037 bfd_boolean has_reloc
= (abfd
->flags
& HAS_RELOC
);
3038 bfd_map_over_sections (abfd
, adjust_addresses
, &has_reloc
);
3041 if (! dump_debugging_tags
)
3042 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd
),
3045 print_arelt_descr (stdout
, abfd
, TRUE
);
3046 if (dump_file_header
)
3047 dump_bfd_header (abfd
);
3048 if (dump_private_headers
)
3049 dump_bfd_private_header (abfd
);
3050 if (! dump_debugging_tags
)
3052 if (dump_section_headers
)
3053 dump_headers (abfd
);
3059 || dump_dwarf_section_info
)
3060 syms
= slurp_symtab (abfd
);
3061 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
3062 || (disassemble
&& bfd_get_dynamic_symtab_upper_bound (abfd
) > 0))
3063 dynsyms
= slurp_dynamic_symtab (abfd
);
3066 synthcount
= bfd_get_synthetic_symtab (abfd
, symcount
, syms
,
3067 dynsymcount
, dynsyms
, &synthsyms
);
3073 dump_symbols (abfd
, FALSE
);
3074 if (dump_dynamic_symtab
)
3075 dump_symbols (abfd
, TRUE
);
3076 if (dump_dwarf_section_info
)
3078 if (dump_stab_section_info
)
3080 if (dump_reloc_info
&& ! disassemble
)
3082 if (dump_dynamic_reloc_info
&& ! disassemble
)
3083 dump_dynamic_relocs (abfd
);
3084 if (dump_section_contents
)
3087 disassemble_data (abfd
);
3093 dhandle
= read_debugging_info (abfd
, syms
, symcount
, TRUE
);
3094 if (dhandle
!= NULL
)
3096 if (!print_debugging_info (stdout
, dhandle
, abfd
, syms
,
3098 dump_debugging_tags
? TRUE
: FALSE
))
3100 non_fatal (_("%s: printing debugging information failed"),
3101 bfd_get_filename (abfd
));
3105 /* PR 6483: If there was no STABS or IEEE debug
3106 info in the file, try DWARF instead. */
3107 else if (! dump_dwarf_section_info
)
3137 display_bfd (bfd
*abfd
)
3141 if (bfd_check_format_matches (abfd
, bfd_object
, &matching
))
3147 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
3149 nonfatal (bfd_get_filename (abfd
));
3150 list_matching_formats (matching
);
3155 if (bfd_get_error () != bfd_error_file_not_recognized
)
3157 nonfatal (bfd_get_filename (abfd
));
3161 if (bfd_check_format_matches (abfd
, bfd_core
, &matching
))
3167 nonfatal (bfd_get_filename (abfd
));
3169 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
3171 list_matching_formats (matching
);
3177 display_file (char *filename
, char *target
)
3182 if (get_file_size (filename
) < 1)
3188 file
= bfd_openr (filename
, target
);
3191 nonfatal (filename
);
3195 /* Decompress sections unless dumping the section contents. */
3196 if (!dump_section_contents
)
3197 file
->flags
|= BFD_DECOMPRESS
;
3199 /* If the file is an archive, process all of its elements. */
3200 if (bfd_check_format (file
, bfd_archive
))
3202 bfd
*last_arfile
= NULL
;
3204 printf (_("In archive %s:\n"), bfd_get_filename (file
));
3207 bfd_set_error (bfd_error_no_error
);
3209 arfile
= bfd_openr_next_archived_file (file
, arfile
);
3212 if (bfd_get_error () != bfd_error_no_more_archived_files
)
3213 nonfatal (bfd_get_filename (file
));
3217 display_bfd (arfile
);
3219 if (last_arfile
!= NULL
)
3220 bfd_close (last_arfile
);
3221 last_arfile
= arfile
;
3224 if (last_arfile
!= NULL
)
3225 bfd_close (last_arfile
);
3234 main (int argc
, char **argv
)
3237 char *target
= default_target
;
3238 bfd_boolean seenflag
= FALSE
;
3240 #if defined (HAVE_SETLOCALE)
3241 #if defined (HAVE_LC_MESSAGES)
3242 setlocale (LC_MESSAGES
, "");
3244 setlocale (LC_CTYPE
, "");
3247 bindtextdomain (PACKAGE
, LOCALEDIR
);
3248 textdomain (PACKAGE
);
3250 program_name
= *argv
;
3251 xmalloc_set_program_name (program_name
);
3253 START_PROGRESS (program_name
, 0);
3255 expandargv (&argc
, &argv
);
3258 set_default_bfd_target ();
3260 while ((c
= getopt_long (argc
, argv
,
3261 "pib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::",
3262 long_options
, (int *) 0))
3268 break; /* We've been given a long option. */
3273 if (disassembler_options
)
3274 /* Ignore potential memory leak for now. */
3275 disassembler_options
= concat (disassembler_options
, ",",
3276 optarg
, (const char *) NULL
);
3278 disassembler_options
= optarg
;
3284 display_file_offsets
= TRUE
;
3287 with_line_numbers
= TRUE
;
3296 enum demangling_styles style
;
3298 style
= cplus_demangle_name_to_style (optarg
);
3299 if (style
== unknown_demangling
)
3300 fatal (_("unknown demangling style `%s'"),
3303 cplus_demangle_set_style (style
);
3309 case OPTION_ADJUST_VMA
:
3310 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
3312 case OPTION_START_ADDRESS
:
3313 start_address
= parse_vma (optarg
, "--start-address");
3314 if ((stop_address
!= (bfd_vma
) -1) && stop_address
<= start_address
)
3315 fatal (_("error: the start address should be before the end address"));
3317 case OPTION_STOP_ADDRESS
:
3318 stop_address
= parse_vma (optarg
, "--stop-address");
3319 if ((start_address
!= (bfd_vma
) -1) && stop_address
<= start_address
)
3320 fatal (_("error: the stop address should be after the start address"));
3324 prefix_length
= strlen (prefix
);
3325 /* Remove an unnecessary trailing '/' */
3326 while (IS_DIR_SEPARATOR (prefix
[prefix_length
- 1]))
3329 case OPTION_PREFIX_STRIP
:
3330 prefix_strip
= atoi (optarg
);
3331 if (prefix_strip
< 0)
3332 fatal (_("error: prefix strip must be non-negative"));
3334 case OPTION_INSN_WIDTH
:
3335 insn_width
= strtoul (optarg
, NULL
, 0);
3336 if (insn_width
<= 0)
3337 fatal (_("error: instruction width must be positive"));
3340 if (strcmp (optarg
, "B") == 0)
3341 endian
= BFD_ENDIAN_BIG
;
3342 else if (strcmp (optarg
, "L") == 0)
3343 endian
= BFD_ENDIAN_LITTLE
;
3346 nonfatal (_("unrecognized -E option"));
3351 if (strncmp (optarg
, "big", strlen (optarg
)) == 0)
3352 endian
= BFD_ENDIAN_BIG
;
3353 else if (strncmp (optarg
, "little", strlen (optarg
)) == 0)
3354 endian
= BFD_ENDIAN_LITTLE
;
3357 non_fatal (_("unrecognized --endian type `%s'"), optarg
);
3364 dump_file_header
= TRUE
;
3368 formats_info
= TRUE
;
3372 add_include_path (optarg
);
3375 dump_private_headers
= TRUE
;
3379 dump_private_headers
= TRUE
;
3381 dump_reloc_info
= TRUE
;
3382 dump_file_header
= TRUE
;
3383 dump_ar_hdrs
= TRUE
;
3384 dump_section_headers
= TRUE
;
3392 dump_dynamic_symtab
= TRUE
;
3400 disassemble_zeroes
= TRUE
;
3404 disassemble_all
= TRUE
;
3409 with_source_code
= TRUE
;
3418 dump_debugging_tags
= 1;
3423 dump_dwarf_section_info
= TRUE
;
3426 dwarf_select_sections_by_letters (optarg
);
3428 dwarf_select_sections_all ();
3431 dump_dwarf_section_info
= TRUE
;
3434 dwarf_select_sections_by_names (optarg
);
3436 dwarf_select_sections_all ();
3439 dump_stab_section_info
= TRUE
;
3443 dump_section_contents
= TRUE
;
3447 dump_reloc_info
= TRUE
;
3451 dump_dynamic_reloc_info
= TRUE
;
3455 dump_ar_hdrs
= TRUE
;
3459 dump_section_headers
= TRUE
;
3467 show_version
= TRUE
;
3477 print_version ("objdump");
3483 exit_status
= display_info ();
3487 display_file ("a.out", target
);
3489 for (; optind
< argc
;)
3490 display_file (argv
[optind
++], target
);
3495 END_PROGRESS (program_name
);