1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
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 2, 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include "safe-ctype.h"
30 #include "libiberty.h"
35 /* Internal headers for the ELF .stab-dump code - sorry. */
36 #define BYTES_IN_WORD 32
37 #include "aout/aout64.h"
39 #ifdef NEED_DECLARATION_FPRINTF
40 /* This is needed by INIT_DISASSEMBLE_INFO. */
41 extern int fprintf (FILE *, const char *, ...);
45 static int exit_status
= 0;
47 static char *default_target
= NULL
; /* Default at runtime. */
49 static int show_version
= 0; /* Show the version number. */
50 static int dump_section_contents
; /* -s */
51 static int dump_section_headers
; /* -h */
52 static bfd_boolean dump_file_header
; /* -f */
53 static int dump_symtab
; /* -t */
54 static int dump_dynamic_symtab
; /* -T */
55 static int dump_reloc_info
; /* -r */
56 static int dump_dynamic_reloc_info
; /* -R */
57 static int dump_ar_hdrs
; /* -a */
58 static int dump_private_headers
; /* -p */
59 static int prefix_addresses
; /* --prefix-addresses */
60 static int with_line_numbers
; /* -l */
61 static bfd_boolean with_source_code
; /* -S */
62 static int show_raw_insn
; /* --show-raw-insn */
63 static int dump_stab_section_info
; /* --stabs */
64 static int do_demangle
; /* -C, --demangle */
65 static bfd_boolean disassemble
; /* -d */
66 static bfd_boolean disassemble_all
; /* -D */
67 static int disassemble_zeroes
; /* --disassemble-zeroes */
68 static bfd_boolean formats_info
; /* -i */
69 static char **only
; /* -j secname */
70 static size_t only_size
= 0;
71 static size_t only_used
= 0;
72 static int wide_output
; /* -w */
73 static bfd_vma start_address
= (bfd_vma
) -1; /* --start-address */
74 static bfd_vma stop_address
= (bfd_vma
) -1; /* --stop-address */
75 static int dump_debugging
; /* --debugging */
76 static int dump_debugging_tags
; /* --debugging-tags */
77 static bfd_vma adjust_section_vma
= 0; /* --adjust-vma */
78 static int file_start_context
= 0; /* --file-start-context */
80 /* Variables for handling include file path table. */
81 static const char **include_paths
;
82 static int include_path_count
;
84 /* Extra info to pass to the disassembler address printing function. */
85 struct objdump_disasm_info
89 bfd_boolean require_sec
;
92 /* Architecture to disassemble for, or default if NULL. */
93 static char *machine
= NULL
;
95 /* Target specific options to the disassembler. */
96 static char *disassembler_options
= NULL
;
98 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
99 static enum bfd_endian endian
= BFD_ENDIAN_UNKNOWN
;
101 /* The symbol table. */
102 static asymbol
**syms
;
104 /* Number of symbols in `syms'. */
105 static long symcount
= 0;
107 /* The sorted symbol table. */
108 static asymbol
**sorted_syms
;
110 /* Number of symbols in `sorted_syms'. */
111 static long sorted_symcount
= 0;
113 /* The dynamic symbol table. */
114 static asymbol
**dynsyms
;
116 /* Number of symbols in `dynsyms'. */
117 static long dynsymcount
= 0;
119 static bfd_byte
*stabs
;
120 static bfd_size_type stab_size
;
123 static bfd_size_type stabstr_size
;
125 /* Forward declarations. */
127 static void dump_data (bfd
*);
128 static void dump_relocs (bfd
*);
129 static void dump_dynamic_relocs (bfd
*);
130 static void dump_reloc_set (bfd
*, asection
*, arelent
**, long);
131 static void dump_symbols (bfd
*, bfd_boolean
);
132 static void dump_section_stabs (bfd
*, char *, char *);
135 usage (FILE *stream
, int status
)
137 fprintf (stream
, _("Usage: %s <option(s)> <file(s)>\n"), program_name
);
138 fprintf (stream
, _(" Display information from object <file(s)>.\n"));
139 fprintf (stream
, _(" At least one of the following switches must be given:\n"));
140 fprintf (stream
, _("\
141 -a, --archive-headers Display archive header information\n\
142 -f, --file-headers Display the contents of the overall file header\n\
143 -p, --private-headers Display object format specific file header contents\n\
144 -h, --[section-]headers Display the contents of the section headers\n\
145 -x, --all-headers Display the contents of all headers\n\
146 -d, --disassemble Display assembler contents of executable sections\n\
147 -D, --disassemble-all Display assembler contents of all sections\n\
148 -S, --source Intermix source code with disassembly\n\
149 -s, --full-contents Display the full contents of all sections requested\n\
150 -g, --debugging Display debug information in object file\n\
151 -e, --debugging-tags Display debug information using ctags style\n\
152 -G, --stabs Display (in raw form) any STABS info in the file\n\
153 -t, --syms Display the contents of the symbol table(s)\n\
154 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
155 -r, --reloc Display the relocation entries in the file\n\
156 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
157 -v, --version Display this program's version number\n\
158 -i, --info List object formats and architectures supported\n\
159 -H, --help Display this information\n\
163 fprintf (stream
, _("\n The following switches are optional:\n"));
164 fprintf (stream
, _("\
165 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
166 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
167 -j, --section=NAME Only display information for section NAME\n\
168 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
169 -EB --endian=big Assume big endian format when disassembling\n\
170 -EL --endian=little Assume little endian format when disassembling\n\
171 --file-start-context Include context from start of file (with -S)\n\
172 -I, --include=DIR Add DIR to search list for source files\n\
173 -l, --line-numbers Include line numbers and filenames in output\n\
174 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
175 The STYLE, if specified, can be `auto', `gnu',\n\
176 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
178 -w, --wide Format output for more than 80 columns\n\
179 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
180 --start-address=ADDR Only process data whose address is >= ADDR\n\
181 --stop-address=ADDR Only process data whose address is <= ADDR\n\
182 --prefix-addresses Print complete address alongside disassembly\n\
183 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
184 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
186 list_supported_targets (program_name
, stream
);
187 list_supported_architectures (program_name
, stream
);
189 disassembler_usage (stream
);
192 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
196 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
200 OPTION_START_ADDRESS
,
205 static struct option long_options
[]=
207 {"adjust-vma", required_argument
, NULL
, OPTION_ADJUST_VMA
},
208 {"all-headers", no_argument
, NULL
, 'x'},
209 {"private-headers", no_argument
, NULL
, 'p'},
210 {"architecture", required_argument
, NULL
, 'm'},
211 {"archive-headers", no_argument
, NULL
, 'a'},
212 {"debugging", no_argument
, NULL
, 'g'},
213 {"debugging-tags", no_argument
, NULL
, 'e'},
214 {"demangle", optional_argument
, NULL
, 'C'},
215 {"disassemble", no_argument
, NULL
, 'd'},
216 {"disassemble-all", no_argument
, NULL
, 'D'},
217 {"disassembler-options", required_argument
, NULL
, 'M'},
218 {"disassemble-zeroes", no_argument
, NULL
, 'z'},
219 {"dynamic-reloc", no_argument
, NULL
, 'R'},
220 {"dynamic-syms", no_argument
, NULL
, 'T'},
221 {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
222 {"file-headers", no_argument
, NULL
, 'f'},
223 {"file-start-context", no_argument
, &file_start_context
, 1},
224 {"full-contents", no_argument
, NULL
, 's'},
225 {"headers", no_argument
, NULL
, 'h'},
226 {"help", no_argument
, NULL
, 'H'},
227 {"info", no_argument
, NULL
, 'i'},
228 {"line-numbers", no_argument
, NULL
, 'l'},
229 {"no-show-raw-insn", no_argument
, &show_raw_insn
, -1},
230 {"prefix-addresses", no_argument
, &prefix_addresses
, 1},
231 {"reloc", no_argument
, NULL
, 'r'},
232 {"section", required_argument
, NULL
, 'j'},
233 {"section-headers", no_argument
, NULL
, 'h'},
234 {"show-raw-insn", no_argument
, &show_raw_insn
, 1},
235 {"source", no_argument
, NULL
, 'S'},
236 {"include", required_argument
, NULL
, 'I'},
237 {"stabs", no_argument
, NULL
, 'G'},
238 {"start-address", required_argument
, NULL
, OPTION_START_ADDRESS
},
239 {"stop-address", required_argument
, NULL
, OPTION_STOP_ADDRESS
},
240 {"syms", no_argument
, NULL
, 't'},
241 {"target", required_argument
, NULL
, 'b'},
242 {"version", no_argument
, NULL
, 'V'},
243 {"wide", no_argument
, NULL
, 'w'},
244 {0, no_argument
, 0, 0}
248 nonfatal (const char *msg
)
255 dump_section_header (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*section
,
256 void *ignored ATTRIBUTE_UNUSED
)
259 unsigned int opb
= bfd_octets_per_byte (abfd
);
261 printf ("%3d %-13s %08lx ", section
->index
,
262 bfd_get_section_name (abfd
, section
),
263 (unsigned long) bfd_section_size (abfd
, section
) / opb
);
264 bfd_printf_vma (abfd
, bfd_get_section_vma (abfd
, section
));
266 bfd_printf_vma (abfd
, section
->lma
);
267 printf (" %08lx 2**%u", (unsigned long) section
->filepos
,
268 bfd_get_section_alignment (abfd
, section
));
274 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
276 PF (SEC_HAS_CONTENTS
, "CONTENTS");
277 PF (SEC_ALLOC
, "ALLOC");
278 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
279 PF (SEC_LOAD
, "LOAD");
280 PF (SEC_RELOC
, "RELOC");
281 PF (SEC_READONLY
, "READONLY");
282 PF (SEC_CODE
, "CODE");
283 PF (SEC_DATA
, "DATA");
285 PF (SEC_DEBUGGING
, "DEBUGGING");
286 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
287 PF (SEC_EXCLUDE
, "EXCLUDE");
288 PF (SEC_SORT_ENTRIES
, "SORT_ENTRIES");
289 PF (SEC_BLOCK
, "BLOCK");
290 PF (SEC_CLINK
, "CLINK");
291 PF (SEC_SMALL_DATA
, "SMALL_DATA");
292 PF (SEC_SHARED
, "SHARED");
293 PF (SEC_ARCH_BIT_0
, "ARCH_BIT_0");
294 PF (SEC_THREAD_LOCAL
, "THREAD_LOCAL");
296 if ((section
->flags
& SEC_LINK_ONCE
) != 0)
300 switch (section
->flags
& SEC_LINK_DUPLICATES
)
304 case SEC_LINK_DUPLICATES_DISCARD
:
305 ls
= "LINK_ONCE_DISCARD";
307 case SEC_LINK_DUPLICATES_ONE_ONLY
:
308 ls
= "LINK_ONCE_ONE_ONLY";
310 case SEC_LINK_DUPLICATES_SAME_SIZE
:
311 ls
= "LINK_ONCE_SAME_SIZE";
313 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
314 ls
= "LINK_ONCE_SAME_CONTENTS";
317 printf ("%s%s", comma
, ls
);
319 if (section
->comdat
!= NULL
)
320 printf (" (COMDAT %s %ld)", section
->comdat
->name
,
321 section
->comdat
->symbol
);
331 dump_headers (bfd
*abfd
)
333 printf (_("Sections:\n"));
336 printf (_("Idx Name Size VMA LMA File off Algn"));
338 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
339 if (bfd_get_arch_size (abfd
) == 32)
340 printf (_("Idx Name Size VMA LMA File off Algn"));
342 printf (_("Idx Name Size VMA LMA File off Algn"));
346 printf (_(" Flags"));
347 if (abfd
->flags
& HAS_LOAD_PAGE
)
351 bfd_map_over_sections (abfd
, dump_section_header
, NULL
);
355 slurp_symtab (bfd
*abfd
)
360 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
366 storage
= bfd_get_symtab_upper_bound (abfd
);
368 bfd_fatal (bfd_get_filename (abfd
));
370 sy
= xmalloc (storage
);
372 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
374 bfd_fatal (bfd_get_filename (abfd
));
378 /* Read in the dynamic symbols. */
381 slurp_dynamic_symtab (bfd
*abfd
)
386 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
389 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
391 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd
));
396 bfd_fatal (bfd_get_filename (abfd
));
399 sy
= xmalloc (storage
);
401 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
403 bfd_fatal (bfd_get_filename (abfd
));
407 /* Filter out (in place) symbols that are useless for disassembly.
408 COUNT is the number of elements in SYMBOLS.
409 Return the number of useful symbols. */
412 remove_useless_symbols (asymbol
**symbols
, long count
)
414 asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
418 asymbol
*sym
= *in_ptr
++;
420 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
422 if (sym
->flags
& (BSF_DEBUGGING
))
424 if (bfd_is_und_section (sym
->section
)
425 || bfd_is_com_section (sym
->section
))
430 return out_ptr
- symbols
;
433 /* Sort symbols into value order. */
436 compare_symbols (const void *ap
, const void *bp
)
438 const asymbol
*a
= * (const asymbol
**) ap
;
439 const asymbol
*b
= * (const asymbol
**) bp
;
449 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
451 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
454 if (a
->section
> b
->section
)
456 else if (a
->section
< b
->section
)
459 an
= bfd_asymbol_name (a
);
460 bn
= bfd_asymbol_name (b
);
464 /* The symbols gnu_compiled and gcc2_compiled convey no real
465 information, so put them after other symbols with the same value. */
466 af
= (strstr (an
, "gnu_compiled") != NULL
467 || strstr (an
, "gcc2_compiled") != NULL
);
468 bf
= (strstr (bn
, "gnu_compiled") != NULL
469 || strstr (bn
, "gcc2_compiled") != NULL
);
476 /* We use a heuristic for the file name, to try to sort it after
477 more useful symbols. It may not work on non Unix systems, but it
478 doesn't really matter; the only difference is precisely which
479 symbol names get printed. */
481 #define file_symbol(s, sn, snl) \
482 (((s)->flags & BSF_FILE) != 0 \
483 || ((sn)[(snl) - 2] == '.' \
484 && ((sn)[(snl) - 1] == 'o' \
485 || (sn)[(snl) - 1] == 'a')))
487 af
= file_symbol (a
, an
, anl
);
488 bf
= file_symbol (b
, bn
, bnl
);
495 /* Try to sort global symbols before local symbols before function
496 symbols before debugging symbols. */
501 if ((aflags
& BSF_DEBUGGING
) != (bflags
& BSF_DEBUGGING
))
503 if ((aflags
& BSF_DEBUGGING
) != 0)
508 if ((aflags
& BSF_FUNCTION
) != (bflags
& BSF_FUNCTION
))
510 if ((aflags
& BSF_FUNCTION
) != 0)
515 if ((aflags
& BSF_LOCAL
) != (bflags
& BSF_LOCAL
))
517 if ((aflags
& BSF_LOCAL
) != 0)
522 if ((aflags
& BSF_GLOBAL
) != (bflags
& BSF_GLOBAL
))
524 if ((aflags
& BSF_GLOBAL
) != 0)
530 /* Symbols that start with '.' might be section names, so sort them
531 after symbols that don't start with '.'. */
532 if (an
[0] == '.' && bn
[0] != '.')
534 if (an
[0] != '.' && bn
[0] == '.')
537 /* Finally, if we can't distinguish them in any other way, try to
538 get consistent results by sorting the symbols by name. */
539 return strcmp (an
, bn
);
542 /* Sort relocs into address order. */
545 compare_relocs (const void *ap
, const void *bp
)
547 const arelent
*a
= * (const arelent
**) ap
;
548 const arelent
*b
= * (const arelent
**) bp
;
550 if (a
->address
> b
->address
)
552 else if (a
->address
< b
->address
)
555 /* So that associated relocations tied to the same address show up
556 in the correct order, we don't do any further sorting. */
565 /* Print VMA to STREAM. If SKIP_ZEROES is TRUE, omit leading zeroes. */
568 objdump_print_value (bfd_vma vma
, struct disassemble_info
*info
,
569 bfd_boolean skip_zeroes
)
573 struct objdump_disasm_info
*aux
574 = (struct objdump_disasm_info
*) info
->application_data
;
576 bfd_sprintf_vma (aux
->abfd
, buf
, vma
);
581 for (p
= buf
; *p
== '0'; ++p
)
586 (*info
->fprintf_func
) (info
->stream
, "%s", p
);
589 /* Print the name of a symbol. */
592 objdump_print_symname (bfd
*abfd
, struct disassemble_info
*info
,
599 name
= bfd_asymbol_name (sym
);
600 if (do_demangle
&& name
[0] != '\0')
602 /* Demangle the name. */
603 alloc
= demangle (abfd
, name
);
608 (*info
->fprintf_func
) (info
->stream
, "%s", name
);
616 /* Locate a symbol given a bfd, a section, and a VMA. If REQUIRE_SEC
617 is TRUE, then always require the symbol to be in the section. This
618 returns NULL if there is no suitable symbol. If PLACE is not NULL,
619 then *PLACE is set to the index of the symbol in sorted_syms. */
622 find_symbol_for_address (bfd
*abfd
, asection
*sec
, bfd_vma vma
,
623 bfd_boolean require_sec
, long *place
)
625 /* @@ Would it speed things up to cache the last two symbols returned,
626 and maybe their address ranges? For many processors, only one memory
627 operand can be present at a time, so the 2-entry cache wouldn't be
628 constantly churned by code doing heavy memory accesses. */
630 /* Indices in `sorted_syms'. */
632 long max
= sorted_symcount
;
634 unsigned int opb
= bfd_octets_per_byte (abfd
);
636 if (sorted_symcount
< 1)
639 /* Perform a binary search looking for the closest symbol to the
640 required value. We are searching the range (min, max]. */
641 while (min
+ 1 < max
)
645 thisplace
= (max
+ min
) / 2;
646 sym
= sorted_syms
[thisplace
];
648 if (bfd_asymbol_value (sym
) > vma
)
650 else if (bfd_asymbol_value (sym
) < vma
)
659 /* The symbol we want is now in min, the low end of the range we
660 were searching. If there are several symbols with the same
661 value, we want the first one. */
664 && (bfd_asymbol_value (sorted_syms
[thisplace
])
665 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
668 /* If the file is relocatable, and the symbol could be from this
669 section, prefer a symbol from this section over symbols from
670 others, even if the other symbol's value might be closer.
672 Note that this may be wrong for some symbol references if the
673 sections have overlapping memory ranges, but in that case there's
674 no way to tell what's desired without looking at the relocation
676 if (sorted_syms
[thisplace
]->section
!= sec
678 || ((abfd
->flags
& HAS_RELOC
) != 0
679 && vma
>= bfd_get_section_vma (abfd
, sec
)
680 && vma
< (bfd_get_section_vma (abfd
, sec
)
681 + bfd_section_size (abfd
, sec
) / opb
))))
685 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
687 if (bfd_asymbol_value (sorted_syms
[i
])
688 != bfd_asymbol_value (sorted_syms
[thisplace
]))
696 if (sorted_syms
[i
]->section
== sec
698 || sorted_syms
[i
- 1]->section
!= sec
699 || (bfd_asymbol_value (sorted_syms
[i
])
700 != bfd_asymbol_value (sorted_syms
[i
- 1]))))
707 if (sorted_syms
[thisplace
]->section
!= sec
)
709 /* We didn't find a good symbol with a smaller value.
710 Look for one with a larger value. */
711 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
713 if (sorted_syms
[i
]->section
== sec
)
721 if (sorted_syms
[thisplace
]->section
!= sec
723 || ((abfd
->flags
& HAS_RELOC
) != 0
724 && vma
>= bfd_get_section_vma (abfd
, sec
)
725 && vma
< (bfd_get_section_vma (abfd
, sec
)
726 + bfd_section_size (abfd
, sec
)))))
728 /* There is no suitable symbol. */
736 return sorted_syms
[thisplace
];
739 /* Print an address to INFO symbolically. */
742 objdump_print_addr_with_sym (bfd
*abfd
, asection
*sec
, asymbol
*sym
,
743 bfd_vma vma
, struct disassemble_info
*info
,
744 bfd_boolean skip_zeroes
)
746 objdump_print_value (vma
, info
, skip_zeroes
);
752 (*info
->fprintf_func
) (info
->stream
, " <%s",
753 bfd_get_section_name (abfd
, sec
));
754 secaddr
= bfd_get_section_vma (abfd
, sec
);
757 (*info
->fprintf_func
) (info
->stream
, "-0x");
758 objdump_print_value (secaddr
- vma
, info
, TRUE
);
760 else if (vma
> secaddr
)
762 (*info
->fprintf_func
) (info
->stream
, "+0x");
763 objdump_print_value (vma
- secaddr
, info
, TRUE
);
765 (*info
->fprintf_func
) (info
->stream
, ">");
769 (*info
->fprintf_func
) (info
->stream
, " <");
770 objdump_print_symname (abfd
, info
, sym
);
771 if (bfd_asymbol_value (sym
) > vma
)
773 (*info
->fprintf_func
) (info
->stream
, "-0x");
774 objdump_print_value (bfd_asymbol_value (sym
) - vma
, info
, TRUE
);
776 else if (vma
> bfd_asymbol_value (sym
))
778 (*info
->fprintf_func
) (info
->stream
, "+0x");
779 objdump_print_value (vma
- bfd_asymbol_value (sym
), info
, TRUE
);
781 (*info
->fprintf_func
) (info
->stream
, ">");
785 /* Print VMA to INFO, symbolically if possible. If SKIP_ZEROES is
786 TRUE, don't output leading zeroes. */
789 objdump_print_addr (bfd_vma vma
, struct disassemble_info
*info
,
790 bfd_boolean skip_zeroes
)
792 struct objdump_disasm_info
*aux
;
795 if (sorted_symcount
< 1)
797 (*info
->fprintf_func
) (info
->stream
, "0x");
798 objdump_print_value (vma
, info
, skip_zeroes
);
802 aux
= (struct objdump_disasm_info
*) info
->application_data
;
803 sym
= find_symbol_for_address (aux
->abfd
, aux
->sec
, vma
, aux
->require_sec
,
805 objdump_print_addr_with_sym (aux
->abfd
, aux
->sec
, sym
, vma
, info
,
809 /* Print VMA to INFO. This function is passed to the disassembler
813 objdump_print_address (bfd_vma vma
, struct disassemble_info
*info
)
815 objdump_print_addr (vma
, info
, ! prefix_addresses
);
818 /* Determine of the given address has a symbol associated with it. */
821 objdump_symbol_at_address (bfd_vma vma
, struct disassemble_info
* info
)
823 struct objdump_disasm_info
* aux
;
826 /* No symbols - do not bother checking. */
827 if (sorted_symcount
< 1)
830 aux
= (struct objdump_disasm_info
*) info
->application_data
;
831 sym
= find_symbol_for_address (aux
->abfd
, aux
->sec
, vma
, aux
->require_sec
,
834 return (sym
!= NULL
&& (bfd_asymbol_value (sym
) == vma
));
837 /* Hold the last function name and the last line number we displayed
840 static char *prev_functionname
;
841 static unsigned int prev_line
;
843 /* We keep a list of all files that we have seen when doing a
844 dissassembly with source, so that we know how much of the file to
845 display. This can be important for inlined functions. */
847 struct print_file_list
849 struct print_file_list
*next
;
850 const char *filename
;
856 static struct print_file_list
*print_files
;
858 /* The number of preceding context lines to show when we start
859 displaying a file for the first time. */
861 #define SHOW_PRECEDING_CONTEXT_LINES (5)
863 /* Tries to open MODNAME, and if successful adds a node to print_files
864 linked list and returns that node. Returns NULL on failure. */
866 static struct print_file_list
*
867 try_print_file_open (const char *origname
, const char *modname
)
869 struct print_file_list
*p
;
872 f
= fopen (modname
, "r");
876 if (print_files
!= NULL
&& print_files
->f
!= NULL
)
878 fclose (print_files
->f
);
879 print_files
->f
= NULL
;
882 p
= xmalloc (sizeof (struct print_file_list
));
883 p
->filename
= origname
;
884 p
->modname
= modname
;
887 p
->next
= print_files
;
892 /* If the the source file, as described in the symtab, is not found
893 try to locate it in one of the paths specified with -I
894 If found, add location to print_files linked list. */
896 static struct print_file_list
*
897 update_source_path (const char *filename
)
899 struct print_file_list
*p
;
903 if (filename
== NULL
)
906 p
= try_print_file_open (filename
, filename
);
910 if (include_path_count
== 0)
913 /* Get the name of the file. */
914 fname
= strrchr (filename
, '/');
915 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
917 /* We could have a mixed forward/back slash case. */
918 char *backslash
= strrchr (filename
, '\\');
919 if (fname
== NULL
|| (backslash
!= NULL
&& backslash
> fname
))
921 if (fname
== NULL
&& filename
[0] != '\0' && filename
[1] == ':')
922 fname
= filename
+ 1;
930 /* If file exists under a new path, we need to add it to the list
931 so that show_line knows about it. */
932 for (i
= 0; i
< include_path_count
; i
++)
934 char *modname
= concat (include_paths
[i
], "/", fname
, (const char *) 0);
936 p
= try_print_file_open (filename
, modname
);
946 /* Skip ahead to a given line in a file, optionally printing each
950 skip_to_line (struct print_file_list
*p
, unsigned int line
,
953 while (p
->line
< line
)
957 if (fgets (buf
, sizeof buf
, p
->f
) == NULL
)
967 if (strchr (buf
, '\n') != NULL
)
972 /* Show the line number, or the source line, in a dissassembly
976 show_line (bfd
*abfd
, asection
*section
, bfd_vma addr_offset
)
978 const char *filename
;
979 const char *functionname
;
982 if (! with_line_numbers
&& ! with_source_code
)
985 if (! bfd_find_nearest_line (abfd
, section
, syms
, addr_offset
, &filename
,
986 &functionname
, &line
))
989 if (filename
!= NULL
&& *filename
== '\0')
991 if (functionname
!= NULL
&& *functionname
== '\0')
994 if (with_line_numbers
)
996 if (functionname
!= NULL
997 && (prev_functionname
== NULL
998 || strcmp (functionname
, prev_functionname
) != 0))
999 printf ("%s():\n", functionname
);
1000 if (line
> 0 && line
!= prev_line
)
1001 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
1004 if (with_source_code
1008 struct print_file_list
**pp
, *p
;
1010 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1011 if (strcmp ((*pp
)->filename
, filename
) == 0)
1017 if (p
!= print_files
)
1021 /* We have reencountered a file name which we saw
1022 earlier. This implies that either we are dumping out
1023 code from an included file, or the same file was
1024 linked in more than once. There are two common cases
1025 of an included file: inline functions in a header
1026 file, and a bison or flex skeleton file. In the
1027 former case we want to just start printing (but we
1028 back up a few lines to give context); in the latter
1029 case we want to continue from where we left off. I
1030 can't think of a good way to distinguish the cases,
1031 so I used a heuristic based on the file name. */
1032 if (strcmp (p
->filename
+ strlen (p
->filename
) - 2, ".h") != 0)
1036 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
1043 p
->f
= fopen (p
->modname
, "r");
1047 skip_to_line (p
, l
, FALSE
);
1049 if (print_files
->f
!= NULL
)
1051 fclose (print_files
->f
);
1052 print_files
->f
= NULL
;
1058 skip_to_line (p
, line
, TRUE
);
1060 p
->next
= print_files
;
1066 p
= update_source_path (filename
);
1072 if (file_start_context
)
1075 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
1078 skip_to_line (p
, l
, FALSE
);
1080 skip_to_line (p
, line
, TRUE
);
1085 if (functionname
!= NULL
1086 && (prev_functionname
== NULL
1087 || strcmp (functionname
, prev_functionname
) != 0))
1089 if (prev_functionname
!= NULL
)
1090 free (prev_functionname
);
1091 prev_functionname
= xmalloc (strlen (functionname
) + 1);
1092 strcpy (prev_functionname
, functionname
);
1095 if (line
> 0 && line
!= prev_line
)
1099 /* Pseudo FILE object for strings. */
1107 /* sprintf to a "stream". */
1110 objdump_sprintf (SFILE
*f
, const char *format
, ...)
1116 va_start (args
, format
);
1118 vasprintf (&buf
, format
, args
);
1123 fatal (_("Out of virtual memory"));
1128 while ((size_t) ((f
->buffer
+ f
->size
) - f
->current
) < n
+ 1)
1132 curroff
= f
->current
- f
->buffer
;
1134 f
->buffer
= xrealloc (f
->buffer
, f
->size
);
1135 f
->current
= f
->buffer
+ curroff
;
1138 memcpy (f
->current
, buf
, n
);
1140 f
->current
[0] = '\0';
1148 /* The number of zeroes we want to see before we start skipping them.
1149 The number is arbitrarily chosen. */
1152 #define SKIP_ZEROES (8)
1155 /* The number of zeroes to skip at the end of a section. If the
1156 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1157 SKIP_ZEROES, they will be disassembled. If there are fewer than
1158 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1159 attempt to avoid disassembling zeroes inserted by section
1162 #ifndef SKIP_ZEROES_AT_END
1163 #define SKIP_ZEROES_AT_END (3)
1166 /* Disassemble some data in memory between given values. */
1169 disassemble_bytes (struct disassemble_info
* info
,
1170 disassembler_ftype disassemble_fn
,
1173 bfd_vma start_offset
,
1174 bfd_vma stop_offset
,
1177 arelent
** relppend
)
1179 struct objdump_disasm_info
*aux
;
1181 int octets_per_line
;
1182 bfd_boolean done_dot
;
1183 int skip_addr_chars
;
1184 bfd_vma addr_offset
;
1185 int opb
= info
->octets_per_byte
;
1187 aux
= (struct objdump_disasm_info
*) info
->application_data
;
1191 octets_per_line
= 4;
1193 octets_per_line
= 16;
1195 /* Figure out how many characters to skip at the start of an
1196 address, to make the disassembly look nicer. We discard leading
1197 zeroes in chunks of 4, ensuring that there is always a leading
1199 skip_addr_chars
= 0;
1200 if (! prefix_addresses
)
1208 + bfd_section_size (section
->owner
, section
) / opb
));
1210 while (s
[0] == '0' && s
[1] == '0' && s
[2] == '0' && s
[3] == '0'
1213 skip_addr_chars
+= 4;
1218 info
->insn_info_valid
= 0;
1221 addr_offset
= start_offset
;
1222 while (addr_offset
< stop_offset
)
1226 bfd_boolean need_nl
= FALSE
;
1228 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1230 for (z
= addr_offset
* opb
; z
< stop_offset
* opb
; z
++)
1233 if (! disassemble_zeroes
1234 && (info
->insn_info_valid
== 0
1235 || info
->branch_delay_insns
== 0)
1236 && (z
- addr_offset
* opb
>= SKIP_ZEROES
1237 || (z
== stop_offset
* opb
&&
1238 z
- addr_offset
* opb
< SKIP_ZEROES_AT_END
)))
1242 /* If there are more nonzero octets to follow, we only skip
1243 zeroes in multiples of 4, to try to avoid running over
1244 the start of an instruction which happens to start with
1246 if (z
!= stop_offset
* opb
)
1247 z
= addr_offset
* opb
+ ((z
- addr_offset
* opb
) &~ 3);
1249 octets
= z
- addr_offset
* opb
;
1260 if (with_line_numbers
|| with_source_code
)
1261 /* The line number tables will refer to unadjusted
1262 section VMAs, so we must undo any VMA modifications
1263 when calling show_line. */
1264 show_line (aux
->abfd
, section
, addr_offset
- adjust_section_vma
);
1266 if (! prefix_addresses
)
1270 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ addr_offset
);
1271 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
1275 printf ("%s:\t", buf
+ skip_addr_chars
);
1279 aux
->require_sec
= TRUE
;
1280 objdump_print_address (section
->vma
+ addr_offset
, info
);
1281 aux
->require_sec
= FALSE
;
1288 sfile
.buffer
= xmalloc (sfile
.size
);
1289 sfile
.current
= sfile
.buffer
;
1290 info
->fprintf_func
= (fprintf_ftype
) objdump_sprintf
;
1291 info
->stream
= (FILE *) &sfile
;
1292 info
->bytes_per_line
= 0;
1293 info
->bytes_per_chunk
= 0;
1295 #ifdef DISASSEMBLER_NEEDS_RELOCS
1296 /* FIXME: This is wrong. It tests the number of octets
1297 in the last instruction, not the current one. */
1298 if (*relppp
< relppend
1299 && (**relppp
)->address
>= rel_offset
+ addr_offset
1300 && ((**relppp
)->address
1301 < rel_offset
+ addr_offset
+ octets
/ opb
))
1302 info
->flags
= INSN_HAS_RELOC
;
1307 octets
= (*disassemble_fn
) (section
->vma
+ addr_offset
, info
);
1308 info
->fprintf_func
= (fprintf_ftype
) fprintf
;
1309 info
->stream
= stdout
;
1310 if (info
->bytes_per_line
!= 0)
1311 octets_per_line
= info
->bytes_per_line
;
1314 if (sfile
.current
!= sfile
.buffer
)
1315 printf ("%s\n", sfile
.buffer
);
1316 free (sfile
.buffer
);
1324 octets
= octets_per_line
;
1325 if (addr_offset
+ octets
/ opb
> stop_offset
)
1326 octets
= (stop_offset
- addr_offset
) * opb
;
1328 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ octets
; ++j
)
1330 if (ISPRINT (data
[j
]))
1331 buf
[j
- addr_offset
* opb
] = data
[j
];
1333 buf
[j
- addr_offset
* opb
] = '.';
1335 buf
[j
- addr_offset
* opb
] = '\0';
1338 if (prefix_addresses
1340 : show_raw_insn
>= 0)
1344 /* If ! prefix_addresses and ! wide_output, we print
1345 octets_per_line octets per line. */
1347 if (pb
> octets_per_line
&& ! prefix_addresses
&& ! wide_output
)
1348 pb
= octets_per_line
;
1350 if (info
->bytes_per_chunk
)
1351 bpc
= info
->bytes_per_chunk
;
1355 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
1358 if (bpc
> 1 && info
->display_endian
== BFD_ENDIAN_LITTLE
)
1360 for (k
= bpc
- 1; k
>= 0; k
--)
1361 printf ("%02x", (unsigned) data
[j
+ k
]);
1366 for (k
= 0; k
< bpc
; k
++)
1367 printf ("%02x", (unsigned) data
[j
+ k
]);
1372 for (; pb
< octets_per_line
; pb
+= bpc
)
1376 for (k
= 0; k
< bpc
; k
++)
1381 /* Separate raw data from instruction by extra space. */
1392 printf ("%s", sfile
.buffer
);
1393 free (sfile
.buffer
);
1396 if (prefix_addresses
1398 : show_raw_insn
>= 0)
1406 j
= addr_offset
* opb
+ pb
;
1408 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ j
/ opb
);
1409 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
1413 printf ("%s:\t", buf
+ skip_addr_chars
);
1415 pb
+= octets_per_line
;
1418 for (; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
1422 if (bpc
> 1 && info
->display_endian
== BFD_ENDIAN_LITTLE
)
1424 for (k
= bpc
- 1; k
>= 0; k
--)
1425 printf ("%02x", (unsigned) data
[j
+ k
]);
1430 for (k
= 0; k
< bpc
; k
++)
1431 printf ("%02x", (unsigned) data
[j
+ k
]);
1444 while ((*relppp
) < relppend
1445 && (**relppp
)->address
< rel_offset
+ addr_offset
+ octets
/ opb
)
1447 if (dump_reloc_info
|| dump_dynamic_reloc_info
)
1458 objdump_print_value (section
->vma
+ q
->address
, info
, TRUE
);
1460 printf (": %s\t", q
->howto
->name
);
1462 if (q
->sym_ptr_ptr
== NULL
|| *q
->sym_ptr_ptr
== NULL
)
1463 printf ("*unknown*");
1466 const char *sym_name
;
1468 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
1469 if (sym_name
!= NULL
&& *sym_name
!= '\0')
1470 objdump_print_symname (aux
->abfd
, info
, *q
->sym_ptr_ptr
);
1475 sym_sec
= bfd_get_section (*q
->sym_ptr_ptr
);
1476 sym_name
= bfd_get_section_name (aux
->abfd
, sym_sec
);
1477 if (sym_name
== NULL
|| *sym_name
== '\0')
1478 sym_name
= "*unknown*";
1479 printf ("%s", sym_name
);
1486 objdump_print_value (q
->addend
, info
, TRUE
);
1498 addr_offset
+= octets
/ opb
;
1502 /* Disassemble the contents of an object file. */
1505 disassemble_data (bfd
*abfd
)
1507 unsigned long addr_offset
;
1508 disassembler_ftype disassemble_fn
;
1509 struct disassemble_info disasm_info
;
1510 struct objdump_disasm_info aux
;
1513 arelent
**dynrelbuf
= NULL
;
1518 prev_functionname
= NULL
;
1521 /* We make a copy of syms to sort. We don't want to sort syms
1522 because that will screw up the relocs. */
1523 sorted_syms
= xmalloc (symcount
* sizeof (asymbol
*));
1524 memcpy (sorted_syms
, syms
, symcount
* sizeof (asymbol
*));
1526 sorted_symcount
= remove_useless_symbols (sorted_syms
, symcount
);
1528 /* Sort the symbols into section and symbol order. */
1529 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
1531 INIT_DISASSEMBLE_INFO (disasm_info
, stdout
, fprintf
);
1533 disasm_info
.application_data
= (void *) &aux
;
1535 aux
.require_sec
= FALSE
;
1536 disasm_info
.print_address_func
= objdump_print_address
;
1537 disasm_info
.symbol_at_address_func
= objdump_symbol_at_address
;
1539 if (machine
!= NULL
)
1541 const bfd_arch_info_type
*info
= bfd_scan_arch (machine
);
1544 fatal (_("Can't use supplied machine %s"), machine
);
1546 abfd
->arch_info
= info
;
1549 if (endian
!= BFD_ENDIAN_UNKNOWN
)
1551 struct bfd_target
*xvec
;
1553 xvec
= xmalloc (sizeof (struct bfd_target
));
1554 memcpy (xvec
, abfd
->xvec
, sizeof (struct bfd_target
));
1555 xvec
->byteorder
= endian
;
1559 disassemble_fn
= disassembler (abfd
);
1560 if (!disassemble_fn
)
1562 non_fatal (_("Can't disassemble for architecture %s\n"),
1563 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
1568 opb
= bfd_octets_per_byte (abfd
);
1570 disasm_info
.flavour
= bfd_get_flavour (abfd
);
1571 disasm_info
.arch
= bfd_get_arch (abfd
);
1572 disasm_info
.mach
= bfd_get_mach (abfd
);
1573 disasm_info
.disassembler_options
= disassembler_options
;
1574 disasm_info
.octets_per_byte
= opb
;
1576 if (bfd_big_endian (abfd
))
1577 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_BIG
;
1578 else if (bfd_little_endian (abfd
))
1579 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
1581 /* ??? Aborting here seems too drastic. We could default to big or little
1583 disasm_info
.endian
= BFD_ENDIAN_UNKNOWN
;
1585 if (dump_dynamic_reloc_info
)
1587 long relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
1590 bfd_fatal (bfd_get_filename (abfd
));
1594 dynrelbuf
= xmalloc (relsize
);
1595 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, dynrelbuf
, dynsyms
);
1597 bfd_fatal (bfd_get_filename (abfd
));
1599 /* Sort the relocs by address. */
1600 qsort (dynrelbuf
, relcount
, sizeof (arelent
*), compare_relocs
);
1604 for (section
= abfd
->sections
;
1606 section
= section
->next
)
1608 bfd_byte
*data
= NULL
;
1609 bfd_size_type datasize
= 0;
1610 arelent
**relbuf
= NULL
;
1611 arelent
**relpp
= NULL
;
1612 arelent
**relppend
= NULL
;
1613 unsigned long stop_offset
;
1614 asymbol
*sym
= NULL
;
1617 /* Sections that do not contain machine code are not normally
1619 if (! disassemble_all
1621 && (section
->flags
& SEC_CODE
) == 0)
1628 for (i
= 0; i
< only_used
; i
++)
1629 if (strcmp (only
[i
], section
->name
) == 0)
1636 if (dynrelbuf
== NULL
)
1638 if ((section
->flags
& SEC_RELOC
) != 0
1639 #ifndef DISASSEMBLER_NEEDS_RELOCS
1642 && dynrelbuf
== NULL
)
1646 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
1648 bfd_fatal (bfd_get_filename (abfd
));
1652 relbuf
= xmalloc (relsize
);
1653 relcount
= bfd_canonicalize_reloc (abfd
, section
, relbuf
, syms
);
1655 bfd_fatal (bfd_get_filename (abfd
));
1657 /* Sort the relocs by address. */
1658 qsort (relbuf
, relcount
, sizeof (arelent
*), compare_relocs
);
1662 datasize
= bfd_get_section_size_before_reloc (section
);
1666 printf (_("Disassembly of section %s:\n"), section
->name
);
1668 data
= xmalloc (datasize
);
1670 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
1673 disasm_info
.buffer
= data
;
1674 disasm_info
.buffer_vma
= section
->vma
;
1675 disasm_info
.buffer_length
= datasize
;
1676 disasm_info
.section
= section
;
1678 if (start_address
== (bfd_vma
) -1
1679 || start_address
< disasm_info
.buffer_vma
)
1682 addr_offset
= start_address
- disasm_info
.buffer_vma
;
1687 /* Dynamic reloc addresses are absolute, non-dynamic are
1688 section relative. REL_OFFSET specifies the reloc address
1689 corresponnding to the start of this section. */
1690 rel_offset
= disasm_info
.buffer_vma
;
1697 relppend
= relpp
+ relcount
;
1699 /* Skip over the relocs belonging to addresses below the
1701 while (relpp
< relppend
1702 && (*relpp
)->address
< rel_offset
+ addr_offset
)
1705 if (stop_address
== (bfd_vma
) -1)
1706 stop_offset
= datasize
/ opb
;
1709 if (stop_address
< disasm_info
.buffer_vma
)
1712 stop_offset
= stop_address
- disasm_info
.buffer_vma
;
1713 if (stop_offset
> disasm_info
.buffer_length
/ opb
)
1714 stop_offset
= disasm_info
.buffer_length
/ opb
;
1717 sym
= find_symbol_for_address (abfd
, section
, section
->vma
+ addr_offset
,
1720 while (addr_offset
< stop_offset
)
1723 unsigned long nextstop_offset
;
1727 && bfd_asymbol_value (sym
) <= section
->vma
+ addr_offset
)
1732 (x
< sorted_symcount
1733 && (bfd_asymbol_value (sorted_syms
[x
])
1734 <= section
->vma
+ addr_offset
));
1738 disasm_info
.symbols
= & sorted_syms
[place
];
1739 disasm_info
.num_symbols
= x
- place
;
1742 disasm_info
.symbols
= NULL
;
1744 if (! prefix_addresses
)
1746 (* disasm_info
.fprintf_func
) (disasm_info
.stream
, "\n");
1747 objdump_print_addr_with_sym (abfd
, section
, sym
,
1748 section
->vma
+ addr_offset
,
1751 (* disasm_info
.fprintf_func
) (disasm_info
.stream
, ":\n");
1755 && bfd_asymbol_value (sym
) > section
->vma
+ addr_offset
)
1757 else if (sym
== NULL
)
1761 /* Search forward for the next appropriate symbol in
1762 SECTION. Note that all the symbols are sorted
1763 together into one big array, and that some sections
1764 may have overlapping addresses. */
1765 while (place
< sorted_symcount
1766 && (sorted_syms
[place
]->section
!= section
1767 || (bfd_asymbol_value (sorted_syms
[place
])
1768 <= bfd_asymbol_value (sym
))))
1770 if (place
>= sorted_symcount
)
1773 nextsym
= sorted_syms
[place
];
1777 && bfd_asymbol_value (sym
) > section
->vma
+ addr_offset
)
1779 nextstop_offset
= bfd_asymbol_value (sym
) - section
->vma
;
1780 if (nextstop_offset
> stop_offset
)
1781 nextstop_offset
= stop_offset
;
1783 else if (nextsym
== NULL
)
1784 nextstop_offset
= stop_offset
;
1787 nextstop_offset
= bfd_asymbol_value (nextsym
) - section
->vma
;
1788 if (nextstop_offset
> stop_offset
)
1789 nextstop_offset
= stop_offset
;
1792 /* If a symbol is explicitly marked as being an object
1793 rather than a function, just dump the bytes without
1794 disassembling them. */
1797 || bfd_asymbol_value (sym
) > section
->vma
+ addr_offset
1798 || ((sym
->flags
& BSF_OBJECT
) == 0
1799 && (strstr (bfd_asymbol_name (sym
), "gnu_compiled")
1801 && (strstr (bfd_asymbol_name (sym
), "gcc2_compiled")
1803 || (sym
->flags
& BSF_FUNCTION
) != 0)
1808 disassemble_bytes (&disasm_info
, disassemble_fn
, insns
, data
,
1809 addr_offset
, nextstop_offset
,
1810 rel_offset
, &relpp
, relppend
);
1812 addr_offset
= nextstop_offset
;
1822 if (dynrelbuf
!= NULL
)
1827 /* Dump the stabs sections from an object file that has a section that
1828 uses Sun stabs encoding. */
1831 dump_stabs (bfd
*abfd
)
1833 dump_section_stabs (abfd
, ".stab", ".stabstr");
1834 dump_section_stabs (abfd
, ".stab.excl", ".stab.exclstr");
1835 dump_section_stabs (abfd
, ".stab.index", ".stab.indexstr");
1836 dump_section_stabs (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1839 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1840 and string table section STRSECT_NAME into `strtab'.
1841 If the section exists and was read, allocate the space and return TRUE.
1842 Otherwise return FALSE. */
1845 read_section_stabs (bfd
*abfd
, const char *stabsect_name
,
1846 const char *strsect_name
)
1848 asection
*stabsect
, *stabstrsect
;
1850 stabsect
= bfd_get_section_by_name (abfd
, stabsect_name
);
1853 printf (_("No %s section present\n\n"), stabsect_name
);
1857 stabstrsect
= bfd_get_section_by_name (abfd
, strsect_name
);
1858 if (0 == stabstrsect
)
1860 non_fatal (_("%s has no %s section"),
1861 bfd_get_filename (abfd
), strsect_name
);
1866 stab_size
= bfd_section_size (abfd
, stabsect
);
1867 stabstr_size
= bfd_section_size (abfd
, stabstrsect
);
1869 stabs
= xmalloc (stab_size
);
1870 strtab
= xmalloc (stabstr_size
);
1872 if (! bfd_get_section_contents (abfd
, stabsect
, stabs
, 0, stab_size
))
1874 non_fatal (_("Reading %s section of %s failed: %s"),
1875 stabsect_name
, bfd_get_filename (abfd
),
1876 bfd_errmsg (bfd_get_error ()));
1883 if (! bfd_get_section_contents (abfd
, stabstrsect
, (void *) strtab
, 0,
1886 non_fatal (_("Reading %s section of %s failed: %s\n"),
1887 strsect_name
, bfd_get_filename (abfd
),
1888 bfd_errmsg (bfd_get_error ()));
1898 /* Stabs entries use a 12 byte format:
1899 4 byte string table index
1901 1 byte stab other field
1902 2 byte stab desc field
1904 FIXME: This will have to change for a 64 bit object format. */
1906 #define STRDXOFF (0)
1908 #define OTHEROFF (5)
1911 #define STABSIZE (12)
1913 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1914 using string table section STRSECT_NAME (in `strtab'). */
1917 print_section_stabs (bfd
*abfd
, const char *stabsect_name
,
1918 const char *strsect_name ATTRIBUTE_UNUSED
)
1921 unsigned file_string_table_offset
= 0;
1922 unsigned next_file_string_table_offset
= 0;
1923 bfd_byte
*stabp
, *stabs_end
;
1926 stabs_end
= stabp
+ stab_size
;
1928 printf (_("Contents of %s section:\n\n"), stabsect_name
);
1929 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1931 /* Loop through all symbols and print them.
1933 We start the index at -1 because there is a dummy symbol on
1934 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1935 for (i
= -1; stabp
< stabs_end
; stabp
+= STABSIZE
, i
++)
1939 unsigned char type
, other
;
1940 unsigned short desc
;
1943 strx
= bfd_h_get_32 (abfd
, stabp
+ STRDXOFF
);
1944 type
= bfd_h_get_8 (abfd
, stabp
+ TYPEOFF
);
1945 other
= bfd_h_get_8 (abfd
, stabp
+ OTHEROFF
);
1946 desc
= bfd_h_get_16 (abfd
, stabp
+ DESCOFF
);
1947 value
= bfd_h_get_32 (abfd
, stabp
+ VALOFF
);
1949 printf ("\n%-6d ", i
);
1950 /* Either print the stab name, or, if unnamed, print its number
1951 again (makes consistent formatting for tools like awk). */
1952 name
= bfd_get_stab_name (type
);
1954 printf ("%-6s", name
);
1955 else if (type
== N_UNDF
)
1958 printf ("%-6d", type
);
1959 printf (" %-6d %-6d ", other
, desc
);
1960 bfd_printf_vma (abfd
, value
);
1961 printf (" %-6lu", strx
);
1963 /* Symbols with type == 0 (N_UNDF) specify the length of the
1964 string table associated with this file. We use that info
1965 to know how to relocate the *next* file's string table indices. */
1968 file_string_table_offset
= next_file_string_table_offset
;
1969 next_file_string_table_offset
+= value
;
1973 /* Using the (possibly updated) string table offset, print the
1974 string (if any) associated with this symbol. */
1975 if ((strx
+ file_string_table_offset
) < stabstr_size
)
1976 printf (" %s", &strtab
[strx
+ file_string_table_offset
]);
1985 dump_section_stabs (bfd
*abfd
, char *stabsect_name
, char *strsect_name
)
1989 /* Check for section names for which stabsect_name is a prefix, to
1990 handle .stab0, etc. */
1991 for (s
= abfd
->sections
;
1997 len
= strlen (stabsect_name
);
1999 /* If the prefix matches, and the files section name ends with a
2000 nul or a digit, then we match. I.e., we want either an exact
2001 match or a section followed by a number. */
2002 if (strncmp (stabsect_name
, s
->name
, len
) == 0
2003 && (s
->name
[len
] == '\000'
2004 || ISDIGIT (s
->name
[len
])))
2006 if (read_section_stabs (abfd
, s
->name
, strsect_name
))
2008 print_section_stabs (abfd
, s
->name
, strsect_name
);
2018 dump_bfd_header (bfd
*abfd
)
2022 printf (_("architecture: %s, "),
2023 bfd_printable_arch_mach (bfd_get_arch (abfd
),
2024 bfd_get_mach (abfd
)));
2025 printf (_("flags 0x%08x:\n"), abfd
->flags
);
2027 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2028 PF (HAS_RELOC
, "HAS_RELOC");
2029 PF (EXEC_P
, "EXEC_P");
2030 PF (HAS_LINENO
, "HAS_LINENO");
2031 PF (HAS_DEBUG
, "HAS_DEBUG");
2032 PF (HAS_SYMS
, "HAS_SYMS");
2033 PF (HAS_LOCALS
, "HAS_LOCALS");
2034 PF (DYNAMIC
, "DYNAMIC");
2035 PF (WP_TEXT
, "WP_TEXT");
2036 PF (D_PAGED
, "D_PAGED");
2037 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
2038 PF (HAS_LOAD_PAGE
, "HAS_LOAD_PAGE");
2039 printf (_("\nstart address 0x"));
2040 bfd_printf_vma (abfd
, abfd
->start_address
);
2046 dump_bfd_private_header (bfd
*abfd
)
2048 bfd_print_private_bfd_data (abfd
, stdout
);
2051 /* Dump selected contents of ABFD. */
2054 dump_bfd (bfd
*abfd
)
2056 /* If we are adjusting section VMA's, change them all now. Changing
2057 the BFD information is a hack. However, we must do it, or
2058 bfd_find_nearest_line will not do the right thing. */
2059 if (adjust_section_vma
!= 0)
2063 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2065 s
->vma
+= adjust_section_vma
;
2066 s
->lma
+= adjust_section_vma
;
2070 if (! dump_debugging_tags
)
2071 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd
),
2074 print_arelt_descr (stdout
, abfd
, TRUE
);
2075 if (dump_file_header
)
2076 dump_bfd_header (abfd
);
2077 if (dump_private_headers
)
2078 dump_bfd_private_header (abfd
);
2079 if (! dump_debugging_tags
)
2081 if (dump_section_headers
)
2082 dump_headers (abfd
);
2084 if (dump_symtab
|| dump_reloc_info
|| disassemble
|| dump_debugging
)
2085 syms
= slurp_symtab (abfd
);
2086 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
)
2087 dynsyms
= slurp_dynamic_symtab (abfd
);
2090 dump_symbols (abfd
, FALSE
);
2091 if (dump_dynamic_symtab
)
2092 dump_symbols (abfd
, TRUE
);
2093 if (dump_stab_section_info
)
2095 if (dump_reloc_info
&& ! disassemble
)
2097 if (dump_dynamic_reloc_info
&& ! disassemble
)
2098 dump_dynamic_relocs (abfd
);
2099 if (dump_section_contents
)
2102 disassemble_data (abfd
);
2107 dhandle
= read_debugging_info (abfd
, syms
, symcount
);
2108 if (dhandle
!= NULL
)
2110 if (! print_debugging_info (stdout
, dhandle
, abfd
, syms
, demangle
,
2111 dump_debugging_tags
? TRUE
: FALSE
))
2113 non_fatal (_("%s: printing debugging information failed"),
2114 bfd_get_filename (abfd
));
2134 display_bfd (bfd
*abfd
)
2138 if (bfd_check_format_matches (abfd
, bfd_object
, &matching
))
2144 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
2146 nonfatal (bfd_get_filename (abfd
));
2147 list_matching_formats (matching
);
2152 if (bfd_get_error () != bfd_error_file_not_recognized
)
2154 nonfatal (bfd_get_filename (abfd
));
2158 if (bfd_check_format_matches (abfd
, bfd_core
, &matching
))
2164 nonfatal (bfd_get_filename (abfd
));
2166 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
2168 list_matching_formats (matching
);
2174 display_file (char *filename
, char *target
)
2176 bfd
*file
, *arfile
= NULL
;
2178 file
= bfd_openr (filename
, target
);
2181 nonfatal (filename
);
2185 if (bfd_check_format (file
, bfd_archive
))
2187 bfd
*last_arfile
= NULL
;
2189 printf (_("In archive %s:\n"), bfd_get_filename (file
));
2192 bfd_set_error (bfd_error_no_error
);
2194 arfile
= bfd_openr_next_archived_file (file
, arfile
);
2197 if (bfd_get_error () != bfd_error_no_more_archived_files
)
2198 nonfatal (bfd_get_filename (file
));
2202 display_bfd (arfile
);
2204 if (last_arfile
!= NULL
)
2205 bfd_close (last_arfile
);
2206 last_arfile
= arfile
;
2209 if (last_arfile
!= NULL
)
2210 bfd_close (last_arfile
);
2218 /* Actually display the various requested regions. */
2221 dump_data (bfd
*abfd
)
2225 bfd_size_type datasize
= 0;
2226 bfd_size_type addr_offset
;
2227 bfd_size_type start_offset
, stop_offset
;
2228 unsigned int opb
= bfd_octets_per_byte (abfd
);
2230 for (section
= abfd
->sections
; section
!= NULL
; section
=
2236 for (i
= 0; i
< only_used
; i
++)
2237 if (strcmp (only
[i
], section
->name
) == 0)
2240 if (only_used
== 0 || i
!= only_used
)
2242 if (section
->flags
& SEC_HAS_CONTENTS
)
2247 printf (_("Contents of section %s:\n"), section
->name
);
2249 if (bfd_section_size (abfd
, section
) == 0)
2251 data
= xmalloc (bfd_section_size (abfd
, section
));
2252 datasize
= bfd_section_size (abfd
, section
);
2255 bfd_get_section_contents (abfd
, section
, data
, 0,
2256 bfd_section_size (abfd
, section
));
2258 if (start_address
== (bfd_vma
) -1
2259 || start_address
< section
->vma
)
2262 start_offset
= start_address
- section
->vma
;
2263 if (stop_address
== (bfd_vma
) -1)
2264 stop_offset
= bfd_section_size (abfd
, section
) / opb
;
2267 if (stop_address
< section
->vma
)
2270 stop_offset
= stop_address
- section
->vma
;
2271 if (stop_offset
> bfd_section_size (abfd
, section
) / opb
)
2272 stop_offset
= bfd_section_size (abfd
, section
) / opb
;
2277 bfd_sprintf_vma (abfd
, buf
, start_offset
+ section
->vma
);
2278 if (strlen (buf
) >= sizeof (buf
))
2281 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
2283 count
= strlen (buf
) - count
;
2287 bfd_sprintf_vma (abfd
, buf
, stop_offset
+ section
->vma
- 1);
2288 if (strlen (buf
) >= sizeof (buf
))
2291 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
2293 count
= strlen (buf
) - count
;
2297 for (addr_offset
= start_offset
;
2298 addr_offset
< stop_offset
; addr_offset
+= onaline
/ opb
)
2302 bfd_sprintf_vma (abfd
, buf
, (addr_offset
+ section
->vma
));
2303 count
= strlen (buf
);
2304 if ((size_t) count
>= sizeof (buf
))
2307 while (count
< width
)
2312 fputs (buf
+ count
- width
, stdout
);
2315 for (j
= addr_offset
* opb
;
2316 j
< addr_offset
* opb
+ onaline
; j
++)
2318 if (j
< stop_offset
* opb
)
2319 printf ("%02x", (unsigned) (data
[j
]));
2327 for (j
= addr_offset
* opb
;
2328 j
< addr_offset
* opb
+ onaline
; j
++)
2330 if (j
>= stop_offset
* opb
)
2333 printf ("%c", ISPRINT (data
[j
]) ? data
[j
] : '.');
2343 /* Should perhaps share code and display with nm? */
2346 dump_symbols (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_boolean dynamic
)
2356 printf ("DYNAMIC SYMBOL TABLE:\n");
2362 printf ("SYMBOL TABLE:\n");
2366 printf (_("no symbols\n"));
2368 for (count
= 0; count
< max
; count
++)
2372 bfd
*cur_bfd
= bfd_asymbol_bfd (*current
);
2374 if (cur_bfd
!= NULL
)
2379 name
= (*current
)->name
;
2381 if (do_demangle
&& name
!= NULL
&& *name
!= '\0')
2383 /* If we want to demangle the name, we demangle it
2384 here, and temporarily clobber it while calling
2385 bfd_print_symbol. FIXME: This is a gross hack. */
2386 alloc
= demangle (cur_bfd
, name
);
2387 (*current
)->name
= alloc
;
2390 bfd_print_symbol (cur_bfd
, stdout
, *current
,
2391 bfd_print_symbol_all
);
2393 (*current
)->name
= name
;
2407 dump_relocs (bfd
*abfd
)
2413 for (a
= abfd
->sections
; a
!= NULL
; a
= a
->next
)
2417 if (bfd_is_abs_section (a
))
2419 if (bfd_is_und_section (a
))
2421 if (bfd_is_com_section (a
))
2428 for (i
= 0; i
< only_used
; i
++)
2429 if (strcmp (only
[i
], a
->name
) == 0)
2435 else if ((a
->flags
& SEC_RELOC
) == 0)
2438 relsize
= bfd_get_reloc_upper_bound (abfd
, a
);
2440 bfd_fatal (bfd_get_filename (abfd
));
2442 printf ("RELOCATION RECORDS FOR [%s]:", a
->name
);
2446 printf (" (none)\n\n");
2450 relpp
= xmalloc (relsize
);
2451 relcount
= bfd_canonicalize_reloc (abfd
, a
, relpp
, syms
);
2454 bfd_fatal (bfd_get_filename (abfd
));
2455 else if (relcount
== 0)
2456 printf (" (none)\n\n");
2460 dump_reloc_set (abfd
, a
, relpp
, relcount
);
2469 dump_dynamic_relocs (bfd
*abfd
)
2475 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
2477 bfd_fatal (bfd_get_filename (abfd
));
2479 printf ("DYNAMIC RELOCATION RECORDS");
2482 printf (" (none)\n\n");
2485 relpp
= xmalloc (relsize
);
2486 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
2489 bfd_fatal (bfd_get_filename (abfd
));
2490 else if (relcount
== 0)
2491 printf (" (none)\n\n");
2495 dump_reloc_set (abfd
, NULL
, relpp
, relcount
);
2503 dump_reloc_set (bfd
*abfd
, asection
*sec
, arelent
**relpp
, long relcount
)
2506 char *last_filename
, *last_functionname
;
2507 unsigned int last_line
;
2509 /* Get column headers lined up reasonably. */
2516 bfd_sprintf_vma (abfd
, buf
, (bfd_vma
) -1);
2517 width
= strlen (buf
) - 7;
2519 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
2522 last_filename
= NULL
;
2523 last_functionname
= NULL
;
2526 for (p
= relpp
; relcount
&& *p
!= NULL
; p
++, relcount
--)
2529 const char *filename
, *functionname
;
2531 const char *sym_name
;
2532 const char *section_name
;
2534 if (start_address
!= (bfd_vma
) -1
2535 && q
->address
< start_address
)
2537 if (stop_address
!= (bfd_vma
) -1
2538 && q
->address
> stop_address
)
2541 if (with_line_numbers
2543 && bfd_find_nearest_line (abfd
, sec
, syms
, q
->address
,
2544 &filename
, &functionname
, &line
))
2546 if (functionname
!= NULL
2547 && (last_functionname
== NULL
2548 || strcmp (functionname
, last_functionname
) != 0))
2550 printf ("%s():\n", functionname
);
2551 if (last_functionname
!= NULL
)
2552 free (last_functionname
);
2553 last_functionname
= xstrdup (functionname
);
2557 && (line
!= last_line
2558 || (filename
!= NULL
2559 && last_filename
!= NULL
2560 && strcmp (filename
, last_filename
) != 0)))
2562 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
2564 if (last_filename
!= NULL
)
2565 free (last_filename
);
2566 if (filename
== NULL
)
2567 last_filename
= NULL
;
2569 last_filename
= xstrdup (filename
);
2573 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
2575 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
2576 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
2581 section_name
= NULL
;
2586 bfd_printf_vma (abfd
, q
->address
);
2588 printf (" %-16s ", q
->howto
->name
);
2590 printf (" %-16d ", q
->howto
->type
);
2591 objdump_print_symname (abfd
, NULL
, *q
->sym_ptr_ptr
);
2595 if (section_name
== NULL
)
2596 section_name
= "*unknown*";
2597 bfd_printf_vma (abfd
, q
->address
);
2598 printf (" %-16s [%s]",
2606 bfd_printf_vma (abfd
, q
->addend
);
2613 /* Creates a table of paths, to search for source files. */
2616 add_include_path (const char *path
)
2620 include_path_count
++;
2621 include_paths
= xrealloc (include_paths
,
2622 include_path_count
* sizeof (*include_paths
));
2623 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2624 if (path
[1] == ':' && path
[2] == 0)
2625 path
= concat (path
, ".", (const char *) 0);
2627 include_paths
[include_path_count
- 1] = path
;
2631 main (int argc
, char **argv
)
2634 char *target
= default_target
;
2635 bfd_boolean seenflag
= FALSE
;
2637 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2638 setlocale (LC_MESSAGES
, "");
2640 #if defined (HAVE_SETLOCALE)
2641 setlocale (LC_CTYPE
, "");
2643 bindtextdomain (PACKAGE
, LOCALEDIR
);
2644 textdomain (PACKAGE
);
2646 program_name
= *argv
;
2647 xmalloc_set_program_name (program_name
);
2649 START_PROGRESS (program_name
, 0);
2652 set_default_bfd_target ();
2654 while ((c
= getopt_long (argc
, argv
, "pib:m:M:VvCdDlfaHhrRtTxsSI:j:wE:zgeG",
2655 long_options
, (int *) 0))
2661 break; /* We've been given a long option. */
2666 if (disassembler_options
)
2667 /* Ignore potential memory leak for now. */
2668 disassembler_options
= concat (disassembler_options
, ",",
2671 disassembler_options
= optarg
;
2674 if (only_used
== only_size
)
2677 only
= xrealloc (only
, only_size
* sizeof (char *));
2679 only
[only_used
++] = optarg
;
2682 with_line_numbers
= TRUE
;
2691 enum demangling_styles style
;
2693 style
= cplus_demangle_name_to_style (optarg
);
2694 if (style
== unknown_demangling
)
2695 fatal (_("unknown demangling style `%s'"),
2698 cplus_demangle_set_style (style
);
2704 case OPTION_ADJUST_VMA
:
2705 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
2707 case OPTION_START_ADDRESS
:
2708 start_address
= parse_vma (optarg
, "--start-address");
2710 case OPTION_STOP_ADDRESS
:
2711 stop_address
= parse_vma (optarg
, "--stop-address");
2714 if (strcmp (optarg
, "B") == 0)
2715 endian
= BFD_ENDIAN_BIG
;
2716 else if (strcmp (optarg
, "L") == 0)
2717 endian
= BFD_ENDIAN_LITTLE
;
2720 non_fatal (_("unrecognized -E option"));
2725 if (strncmp (optarg
, "big", strlen (optarg
)) == 0)
2726 endian
= BFD_ENDIAN_BIG
;
2727 else if (strncmp (optarg
, "little", strlen (optarg
)) == 0)
2728 endian
= BFD_ENDIAN_LITTLE
;
2731 non_fatal (_("unrecognized --endian type `%s'"), optarg
);
2737 dump_file_header
= TRUE
;
2741 formats_info
= TRUE
;
2745 add_include_path (optarg
);
2748 dump_private_headers
= TRUE
;
2752 dump_private_headers
= TRUE
;
2754 dump_reloc_info
= TRUE
;
2755 dump_file_header
= TRUE
;
2756 dump_ar_hdrs
= TRUE
;
2757 dump_section_headers
= TRUE
;
2765 dump_dynamic_symtab
= TRUE
;
2773 disassemble_zeroes
= TRUE
;
2777 disassemble_all
= TRUE
;
2782 with_source_code
= TRUE
;
2791 dump_debugging_tags
= 1;
2796 dump_stab_section_info
= TRUE
;
2800 dump_section_contents
= TRUE
;
2804 dump_reloc_info
= TRUE
;
2808 dump_dynamic_reloc_info
= TRUE
;
2812 dump_ar_hdrs
= TRUE
;
2816 dump_section_headers
= TRUE
;
2824 show_version
= TRUE
;
2834 print_version ("objdump");
2840 exit_status
= display_info ();
2844 display_file ("a.out", target
);
2846 for (; optind
< argc
;)
2847 display_file (argv
[optind
++], target
);
2850 END_PROGRESS (program_name
);