1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "libiberty.h"
32 #ifdef ANSI_PROTOTYPES
38 /* Internal headers for the ELF .stab-dump code - sorry. */
39 #define BYTES_IN_WORD 32
40 #include "aout/aout64.h"
42 #ifdef NEED_DECLARATION_FPRINTF
43 /* This is needed by INIT_DISASSEMBLE_INFO. */
44 extern int fprintf
PARAMS ((FILE *, const char *, ...));
48 static int exit_status
= 0;
50 static char *default_target
= NULL
; /* default at runtime */
52 static int show_version
= 0; /* show the version number */
53 static int dump_section_contents
; /* -s */
54 static int dump_section_headers
; /* -h */
55 static boolean dump_file_header
; /* -f */
56 static int dump_symtab
; /* -t */
57 static int dump_dynamic_symtab
; /* -T */
58 static int dump_reloc_info
; /* -r */
59 static int dump_dynamic_reloc_info
; /* -R */
60 static int dump_ar_hdrs
; /* -a */
61 static int dump_private_headers
; /* -p */
62 static int prefix_addresses
; /* --prefix-addresses */
63 static int with_line_numbers
; /* -l */
64 static boolean with_source_code
; /* -S */
65 static int show_raw_insn
; /* --show-raw-insn */
66 static int dump_stab_section_info
; /* --stabs */
67 static int do_demangle
; /* -C, --demangle */
68 static boolean disassemble
; /* -d */
69 static boolean disassemble_all
; /* -D */
70 static int disassemble_zeroes
; /* --disassemble-zeroes */
71 static boolean formats_info
; /* -i */
72 static char *only
; /* -j secname */
73 static int wide_output
; /* -w */
74 static bfd_vma start_address
= (bfd_vma
) -1; /* --start-address */
75 static bfd_vma stop_address
= (bfd_vma
) -1; /* --stop-address */
76 static int dump_debugging
; /* --debugging */
77 static bfd_vma adjust_section_vma
= 0; /* --adjust-vma */
79 /* Extra info to pass to the disassembler address printing function. */
80 struct objdump_disasm_info
{
86 /* Architecture to disassemble for, or default if NULL. */
87 static char *machine
= (char *) NULL
;
89 /* Target specific options to the disassembler. */
90 static char *disassembler_options
= (char *) NULL
;
92 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
93 static enum bfd_endian endian
= BFD_ENDIAN_UNKNOWN
;
95 /* The symbol table. */
96 static asymbol
**syms
;
98 /* Number of symbols in `syms'. */
99 static long symcount
= 0;
101 /* The sorted symbol table. */
102 static asymbol
**sorted_syms
;
104 /* Number of symbols in `sorted_syms'. */
105 static long sorted_symcount
= 0;
107 /* The dynamic symbol table. */
108 static asymbol
**dynsyms
;
110 /* Number of symbols in `dynsyms'. */
111 static long dynsymcount
= 0;
113 /* Static declarations. */
116 usage
PARAMS ((FILE *, int));
119 nonfatal
PARAMS ((const char *));
122 display_file
PARAMS ((char *filename
, char *target
));
125 dump_section_header
PARAMS ((bfd
*, asection
*, PTR
));
128 dump_headers
PARAMS ((bfd
*));
131 dump_data
PARAMS ((bfd
*abfd
));
134 dump_relocs
PARAMS ((bfd
*abfd
));
137 dump_dynamic_relocs
PARAMS ((bfd
* abfd
));
140 dump_reloc_set
PARAMS ((bfd
*, asection
*, arelent
**, long));
143 dump_symbols
PARAMS ((bfd
*abfd
, boolean dynamic
));
146 dump_bfd_header
PARAMS ((bfd
*));
149 dump_bfd_private_header
PARAMS ((bfd
*));
152 display_bfd
PARAMS ((bfd
*abfd
));
155 display_target_list
PARAMS ((void));
158 display_info_table
PARAMS ((int, int));
161 display_target_tables
PARAMS ((void));
164 display_info
PARAMS ((void));
167 objdump_print_value
PARAMS ((bfd_vma
, struct disassemble_info
*, boolean
));
170 objdump_print_symname
PARAMS ((bfd
*, struct disassemble_info
*, asymbol
*));
173 find_symbol_for_address
PARAMS ((bfd
*, asection
*, bfd_vma
, boolean
, long *));
176 objdump_print_addr_with_sym
PARAMS ((bfd
*, asection
*, asymbol
*, bfd_vma
,
177 struct disassemble_info
*, boolean
));
180 objdump_print_addr
PARAMS ((bfd_vma
, struct disassemble_info
*, boolean
));
183 objdump_print_address
PARAMS ((bfd_vma
, struct disassemble_info
*));
186 show_line
PARAMS ((bfd
*, asection
*, bfd_vma
));
189 disassemble_bytes
PARAMS ((struct disassemble_info
*, disassembler_ftype
,
190 boolean
, bfd_byte
*, bfd_vma
, bfd_vma
,
191 arelent
***, arelent
**));
194 disassemble_data
PARAMS ((bfd
*));
197 endian_string
PARAMS ((enum bfd_endian
));
200 slurp_symtab
PARAMS ((bfd
*));
203 slurp_dynamic_symtab
PARAMS ((bfd
*));
206 remove_useless_symbols
PARAMS ((asymbol
**, long));
209 compare_symbols
PARAMS ((const PTR
, const PTR
));
212 compare_relocs
PARAMS ((const PTR
, const PTR
));
215 dump_stabs
PARAMS ((bfd
*));
218 read_section_stabs
PARAMS ((bfd
*, const char *, const char *));
221 print_section_stabs
PARAMS ((bfd
*, const char *, const char *));
224 usage (stream
, status
)
228 fprintf (stream
, _("\
229 Usage: %s [-ahifCdDprRtTxsSlw] [-b bfdname] [-m machine] \n\
230 [-j section-name] [-M disassembler-options]\n\
231 [--archive-headers] [--target=bfdname] [--debugging] [--disassemble]\n\
232 [--disassemble-all] [--disassemble-zeroes] [--file-headers]\n\
233 [--section-headers] [--headers]\n\
234 [--info] [--section=section-name] [--line-numbers] [--source]\n"),
236 fprintf (stream
, _("\
237 [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
238 [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
239 [--wide] [--version] [--help] [--private-headers]\n\
240 [--start-address=addr] [--stop-address=addr]\n\
241 [--prefix-addresses] [--[no-]show-raw-insn] [--demangle]\n\
242 [--adjust-vma=offset] [-EB|-EL] [--endian={big|little}] objfile...\n\
243 at least one option besides -l (--line-numbers) must be given\n"));
244 list_supported_targets (program_name
, stream
);
246 fprintf (stream
, _("Report bugs to bug-gnu-utils@gnu.org\n"));
250 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
252 #define OPTION_ENDIAN (150)
253 #define OPTION_START_ADDRESS (OPTION_ENDIAN + 1)
254 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
255 #define OPTION_ADJUST_VMA (OPTION_STOP_ADDRESS + 1)
257 static struct option long_options
[]=
259 {"adjust-vma", required_argument
, NULL
, OPTION_ADJUST_VMA
},
260 {"all-headers", no_argument
, NULL
, 'x'},
261 {"private-headers", no_argument
, NULL
, 'p'},
262 {"architecture", required_argument
, NULL
, 'm'},
263 {"archive-headers", no_argument
, NULL
, 'a'},
264 {"debugging", no_argument
, &dump_debugging
, 1},
265 {"demangle", no_argument
, &do_demangle
, 1},
266 {"disassemble", no_argument
, NULL
, 'd'},
267 {"disassemble-all", no_argument
, NULL
, 'D'},
268 {"disassembler-options", required_argument
, NULL
, 'M'},
269 {"disassemble-zeroes", no_argument
, &disassemble_zeroes
, 1},
270 {"dynamic-reloc", no_argument
, NULL
, 'R'},
271 {"dynamic-syms", no_argument
, NULL
, 'T'},
272 {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
273 {"file-headers", no_argument
, NULL
, 'f'},
274 {"full-contents", no_argument
, NULL
, 's'},
275 {"headers", no_argument
, NULL
, 'h'},
276 {"help", no_argument
, NULL
, 'H'},
277 {"info", no_argument
, NULL
, 'i'},
278 {"line-numbers", no_argument
, NULL
, 'l'},
279 {"no-show-raw-insn", no_argument
, &show_raw_insn
, -1},
280 {"prefix-addresses", no_argument
, &prefix_addresses
, 1},
281 {"reloc", no_argument
, NULL
, 'r'},
282 {"section", required_argument
, NULL
, 'j'},
283 {"section-headers", no_argument
, NULL
, 'h'},
284 {"show-raw-insn", no_argument
, &show_raw_insn
, 1},
285 {"source", no_argument
, NULL
, 'S'},
286 {"stabs", no_argument
, &dump_stab_section_info
, 1},
287 {"start-address", required_argument
, NULL
, OPTION_START_ADDRESS
},
288 {"stop-address", required_argument
, NULL
, OPTION_STOP_ADDRESS
},
289 {"syms", no_argument
, NULL
, 't'},
290 {"target", required_argument
, NULL
, 'b'},
291 {"version", no_argument
, &show_version
, 1},
292 {"wide", no_argument
, &wide_output
, 'w'},
293 {0, no_argument
, 0, 0}
305 dump_section_header (abfd
, section
, ignored
)
306 bfd
*abfd ATTRIBUTE_UNUSED
;
308 PTR ignored ATTRIBUTE_UNUSED
;
312 printf ("%3d %-13s %08lx ", section
->index
,
313 bfd_get_section_name (abfd
, section
),
314 (unsigned long) bfd_section_size (abfd
, section
));
315 printf_vma (bfd_get_section_vma (abfd
, section
));
317 printf_vma (section
->lma
);
318 printf (" %08lx 2**%u", section
->filepos
,
319 bfd_get_section_alignment (abfd
, section
));
325 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
327 PF (SEC_HAS_CONTENTS
, "CONTENTS");
328 PF (SEC_ALLOC
, "ALLOC");
329 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
330 PF (SEC_CONSTRUCTOR_TEXT
, "CONSTRUCTOR TEXT");
331 PF (SEC_CONSTRUCTOR_DATA
, "CONSTRUCTOR DATA");
332 PF (SEC_CONSTRUCTOR_BSS
, "CONSTRUCTOR BSS");
333 PF (SEC_LOAD
, "LOAD");
334 PF (SEC_RELOC
, "RELOC");
336 PF (SEC_BALIGN
, "BALIGN");
338 PF (SEC_READONLY
, "READONLY");
339 PF (SEC_CODE
, "CODE");
340 PF (SEC_DATA
, "DATA");
342 PF (SEC_DEBUGGING
, "DEBUGGING");
343 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
344 PF (SEC_EXCLUDE
, "EXCLUDE");
345 PF (SEC_SORT_ENTRIES
, "SORT_ENTRIES");
346 PF (SEC_SMALL_DATA
, "SMALL_DATA");
347 PF (SEC_SHARED
, "SHARED");
349 if ((section
->flags
& SEC_LINK_ONCE
) != 0)
353 switch (section
->flags
& SEC_LINK_DUPLICATES
)
357 case SEC_LINK_DUPLICATES_DISCARD
:
358 ls
= "LINK_ONCE_DISCARD";
360 case SEC_LINK_DUPLICATES_ONE_ONLY
:
361 ls
= "LINK_ONCE_ONE_ONLY";
363 case SEC_LINK_DUPLICATES_SAME_SIZE
:
364 ls
= "LINK_ONCE_SAME_SIZE";
366 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
367 ls
= "LINK_ONCE_SAME_CONTENTS";
370 printf ("%s%s", comma
, ls
);
382 printf (_("Sections:\n"));
384 printf (_("Idx Name Size VMA LMA File off Algn\n"));
386 printf (_("Idx Name Size VMA LMA File off Algn\n"));
388 bfd_map_over_sections (abfd
, dump_section_header
, (PTR
) NULL
);
395 asymbol
**sy
= (asymbol
**) NULL
;
398 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
400 fprintf (stderr
, _("%s: no symbols\n"), bfd_get_filename (abfd
));
405 storage
= bfd_get_symtab_upper_bound (abfd
);
407 bfd_fatal (bfd_get_filename (abfd
));
411 sy
= (asymbol
**) xmalloc (storage
);
413 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
415 bfd_fatal (bfd_get_filename (abfd
));
417 fprintf (stderr
, _("%s: no symbols\n"), bfd_get_filename (abfd
));
421 /* Read in the dynamic symbols. */
424 slurp_dynamic_symtab (abfd
)
427 asymbol
**sy
= (asymbol
**) NULL
;
430 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
433 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
435 fprintf (stderr
, _("%s: %s: not a dynamic object\n"),
436 program_name
, bfd_get_filename (abfd
));
441 bfd_fatal (bfd_get_filename (abfd
));
446 sy
= (asymbol
**) xmalloc (storage
);
448 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
450 bfd_fatal (bfd_get_filename (abfd
));
451 if (dynsymcount
== 0)
452 fprintf (stderr
, _("%s: %s: No dynamic symbols\n"),
453 program_name
, bfd_get_filename (abfd
));
457 /* Filter out (in place) symbols that are useless for disassembly.
458 COUNT is the number of elements in SYMBOLS.
459 Return the number of useful symbols. */
462 remove_useless_symbols (symbols
, count
)
466 register asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
470 asymbol
*sym
= *in_ptr
++;
472 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
474 if (sym
->flags
& (BSF_DEBUGGING
))
476 if (bfd_is_und_section (sym
->section
)
477 || bfd_is_com_section (sym
->section
))
482 return out_ptr
- symbols
;
485 /* Sort symbols into value order. */
488 compare_symbols (ap
, bp
)
492 const asymbol
*a
= *(const asymbol
**)ap
;
493 const asymbol
*b
= *(const asymbol
**)bp
;
497 flagword aflags
, bflags
;
499 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
501 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
504 if (a
->section
> b
->section
)
506 else if (a
->section
< b
->section
)
509 an
= bfd_asymbol_name (a
);
510 bn
= bfd_asymbol_name (b
);
514 /* The symbols gnu_compiled and gcc2_compiled convey no real
515 information, so put them after other symbols with the same value. */
517 af
= (strstr (an
, "gnu_compiled") != NULL
518 || strstr (an
, "gcc2_compiled") != NULL
);
519 bf
= (strstr (bn
, "gnu_compiled") != NULL
520 || strstr (bn
, "gcc2_compiled") != NULL
);
527 /* We use a heuristic for the file name, to try to sort it after
528 more useful symbols. It may not work on non Unix systems, but it
529 doesn't really matter; the only difference is precisely which
530 symbol names get printed. */
532 #define file_symbol(s, sn, snl) \
533 (((s)->flags & BSF_FILE) != 0 \
534 || ((sn)[(snl) - 2] == '.' \
535 && ((sn)[(snl) - 1] == 'o' \
536 || (sn)[(snl) - 1] == 'a')))
538 af
= file_symbol (a
, an
, anl
);
539 bf
= file_symbol (b
, bn
, bnl
);
546 /* Try to sort global symbols before local symbols before function
547 symbols before debugging symbols. */
552 if ((aflags
& BSF_DEBUGGING
) != (bflags
& BSF_DEBUGGING
))
554 if ((aflags
& BSF_DEBUGGING
) != 0)
559 if ((aflags
& BSF_FUNCTION
) != (bflags
& BSF_FUNCTION
))
561 if ((aflags
& BSF_FUNCTION
) != 0)
566 if ((aflags
& BSF_LOCAL
) != (bflags
& BSF_LOCAL
))
568 if ((aflags
& BSF_LOCAL
) != 0)
573 if ((aflags
& BSF_GLOBAL
) != (bflags
& BSF_GLOBAL
))
575 if ((aflags
& BSF_GLOBAL
) != 0)
581 /* Symbols that start with '.' might be section names, so sort them
582 after symbols that don't start with '.'. */
583 if (an
[0] == '.' && bn
[0] != '.')
585 if (an
[0] != '.' && bn
[0] == '.')
588 /* Finally, if we can't distinguish them in any other way, try to
589 get consistent results by sorting the symbols by name. */
590 return strcmp (an
, bn
);
593 /* Sort relocs into address order. */
596 compare_relocs (ap
, bp
)
600 const arelent
*a
= *(const arelent
**)ap
;
601 const arelent
*b
= *(const arelent
**)bp
;
603 if (a
->address
> b
->address
)
605 else if (a
->address
< b
->address
)
608 /* So that associated relocations tied to the same address show up
609 in the correct order, we don't do any further sorting. */
618 /* Print VMA to STREAM. If SKIP_ZEROES is true, omit leading zeroes. */
621 objdump_print_value (vma
, info
, skip_zeroes
)
623 struct disassemble_info
*info
;
629 sprintf_vma (buf
, vma
);
634 for (p
= buf
; *p
== '0'; ++p
)
639 (*info
->fprintf_func
) (info
->stream
, "%s", p
);
642 /* Print the name of a symbol. */
645 objdump_print_symname (abfd
, info
, sym
)
647 struct disassemble_info
*info
;
655 name
= bfd_asymbol_name (sym
);
656 if (! do_demangle
|| name
[0] == '\0')
660 /* Demangle the name. */
661 if (bfd_get_symbol_leading_char (abfd
) == name
[0])
664 alloc
= cplus_demangle (name
, DMGL_ANSI
| DMGL_PARAMS
);
672 (*info
->fprintf_func
) (info
->stream
, "%s", print
);
674 printf ("%s", print
);
680 /* Locate a symbol given a bfd, a section, and a VMA. If REQUIRE_SEC
681 is true, then always require the symbol to be in the section. This
682 returns NULL if there is no suitable symbol. If PLACE is not NULL,
683 then *PLACE is set to the index of the symbol in sorted_syms. */
686 find_symbol_for_address (abfd
, sec
, vma
, require_sec
, place
)
693 /* @@ Would it speed things up to cache the last two symbols returned,
694 and maybe their address ranges? For many processors, only one memory
695 operand can be present at a time, so the 2-entry cache wouldn't be
696 constantly churned by code doing heavy memory accesses. */
698 /* Indices in `sorted_syms'. */
700 long max
= sorted_symcount
;
703 if (sorted_symcount
< 1)
706 /* Perform a binary search looking for the closest symbol to the
707 required value. We are searching the range (min, max]. */
708 while (min
+ 1 < max
)
712 thisplace
= (max
+ min
) / 2;
713 sym
= sorted_syms
[thisplace
];
715 if (bfd_asymbol_value (sym
) > vma
)
717 else if (bfd_asymbol_value (sym
) < vma
)
726 /* The symbol we want is now in min, the low end of the range we
727 were searching. If there are several symbols with the same
728 value, we want the first one. */
731 && (bfd_asymbol_value (sorted_syms
[thisplace
])
732 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
735 /* If the file is relocateable, and the symbol could be from this
736 section, prefer a symbol from this section over symbols from
737 others, even if the other symbol's value might be closer.
739 Note that this may be wrong for some symbol references if the
740 sections have overlapping memory ranges, but in that case there's
741 no way to tell what's desired without looking at the relocation
744 if (sorted_syms
[thisplace
]->section
!= sec
746 || ((abfd
->flags
& HAS_RELOC
) != 0
747 && vma
>= bfd_get_section_vma (abfd
, sec
)
748 && vma
< (bfd_get_section_vma (abfd
, sec
)
749 + bfd_section_size (abfd
, sec
)))))
753 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
755 if (bfd_asymbol_value (sorted_syms
[i
])
756 != bfd_asymbol_value (sorted_syms
[thisplace
]))
762 if (sorted_syms
[i
]->section
== sec
764 || sorted_syms
[i
- 1]->section
!= sec
765 || (bfd_asymbol_value (sorted_syms
[i
])
766 != bfd_asymbol_value (sorted_syms
[i
- 1]))))
773 if (sorted_syms
[thisplace
]->section
!= sec
)
775 /* We didn't find a good symbol with a smaller value.
776 Look for one with a larger value. */
777 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
779 if (sorted_syms
[i
]->section
== sec
)
787 if (sorted_syms
[thisplace
]->section
!= sec
789 || ((abfd
->flags
& HAS_RELOC
) != 0
790 && vma
>= bfd_get_section_vma (abfd
, sec
)
791 && vma
< (bfd_get_section_vma (abfd
, sec
)
792 + bfd_section_size (abfd
, sec
)))))
794 /* There is no suitable symbol. */
802 return sorted_syms
[thisplace
];
805 /* Print an address to INFO symbolically. */
808 objdump_print_addr_with_sym (abfd
, sec
, sym
, vma
, info
, skip_zeroes
)
813 struct disassemble_info
*info
;
816 objdump_print_value (vma
, info
, skip_zeroes
);
822 (*info
->fprintf_func
) (info
->stream
, " <%s",
823 bfd_get_section_name (abfd
, sec
));
824 secaddr
= bfd_get_section_vma (abfd
, sec
);
827 (*info
->fprintf_func
) (info
->stream
, "-0x");
828 objdump_print_value (secaddr
- vma
, info
, true);
830 else if (vma
> secaddr
)
832 (*info
->fprintf_func
) (info
->stream
, "+0x");
833 objdump_print_value (vma
- secaddr
, info
, true);
835 (*info
->fprintf_func
) (info
->stream
, ">");
839 (*info
->fprintf_func
) (info
->stream
, " <");
840 objdump_print_symname (abfd
, info
, sym
);
841 if (bfd_asymbol_value (sym
) > vma
)
843 (*info
->fprintf_func
) (info
->stream
, "-0x");
844 objdump_print_value (bfd_asymbol_value (sym
) - vma
, info
, true);
846 else if (vma
> bfd_asymbol_value (sym
))
848 (*info
->fprintf_func
) (info
->stream
, "+0x");
849 objdump_print_value (vma
- bfd_asymbol_value (sym
), info
, true);
851 (*info
->fprintf_func
) (info
->stream
, ">");
855 /* Print VMA to INFO, symbolically if possible. If SKIP_ZEROES is
856 true, don't output leading zeroes. */
859 objdump_print_addr (vma
, info
, skip_zeroes
)
861 struct disassemble_info
*info
;
864 struct objdump_disasm_info
*aux
;
867 if (sorted_symcount
< 1)
869 (*info
->fprintf_func
) (info
->stream
, "0x");
870 objdump_print_value (vma
, info
, skip_zeroes
);
874 aux
= (struct objdump_disasm_info
*) info
->application_data
;
875 sym
= find_symbol_for_address (aux
->abfd
, aux
->sec
, vma
, aux
->require_sec
,
877 objdump_print_addr_with_sym (aux
->abfd
, aux
->sec
, sym
, vma
, info
,
881 /* Print VMA to INFO. This function is passed to the disassembler
885 objdump_print_address (vma
, info
)
887 struct disassemble_info
*info
;
889 objdump_print_addr (vma
, info
, ! prefix_addresses
);
892 /* Determine of the given address has a symbol associated with it. */
895 objdump_symbol_at_address (vma
, info
)
897 struct disassemble_info
* info
;
899 struct objdump_disasm_info
* aux
;
902 /* No symbols - do not bother checking. */
903 if (sorted_symcount
< 1)
906 aux
= (struct objdump_disasm_info
*) info
->application_data
;
907 sym
= find_symbol_for_address (aux
->abfd
, aux
->sec
, vma
, aux
->require_sec
,
910 return (sym
!= NULL
&& (bfd_asymbol_value (sym
) == vma
));
913 /* Hold the last function name and the last line number we displayed
916 static char *prev_functionname
;
917 static unsigned int prev_line
;
919 /* We keep a list of all files that we have seen when doing a
920 dissassembly with source, so that we know how much of the file to
921 display. This can be important for inlined functions. */
923 struct print_file_list
925 struct print_file_list
*next
;
931 static struct print_file_list
*print_files
;
933 /* The number of preceding context lines to show when we start
934 displaying a file for the first time. */
936 #define SHOW_PRECEDING_CONTEXT_LINES (5)
938 /* Skip ahead to a given line in a file, optionally printing each
942 skip_to_line
PARAMS ((struct print_file_list
*, unsigned int, boolean
));
945 skip_to_line (p
, line
, show
)
946 struct print_file_list
*p
;
950 while (p
->line
< line
)
954 if (fgets (buf
, sizeof buf
, p
->f
) == NULL
)
964 if (strchr (buf
, '\n') != NULL
)
969 /* Show the line number, or the source line, in a dissassembly
973 show_line (abfd
, section
, off
)
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
, off
, &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
->filename
, "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
;
1068 f
= fopen (filename
, "r");
1073 p
= ((struct print_file_list
*)
1074 xmalloc (sizeof (struct print_file_list
)));
1075 p
->filename
= xmalloc (strlen (filename
) + 1);
1076 strcpy (p
->filename
, filename
);
1080 if (print_files
!= NULL
&& print_files
->f
!= NULL
)
1082 fclose (print_files
->f
);
1083 print_files
->f
= NULL
;
1085 p
->next
= print_files
;
1088 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
1091 skip_to_line (p
, l
, false);
1093 skip_to_line (p
, line
, true);
1098 if (functionname
!= NULL
1099 && (prev_functionname
== NULL
1100 || strcmp (functionname
, prev_functionname
) != 0))
1102 if (prev_functionname
!= NULL
)
1103 free (prev_functionname
);
1104 prev_functionname
= xmalloc (strlen (functionname
) + 1);
1105 strcpy (prev_functionname
, functionname
);
1108 if (line
> 0 && line
!= prev_line
)
1112 /* Pseudo FILE object for strings. */
1120 /* sprintf to a "stream" */
1123 #ifdef ANSI_PROTOTYPES
1124 objdump_sprintf (SFILE
*f
, const char *format
, ...)
1126 objdump_sprintf (va_alist
)
1130 #ifndef ANSI_PROTOTYPES
1138 #ifdef ANSI_PROTOTYPES
1139 va_start (args
, format
);
1142 f
= va_arg (args
, SFILE
*);
1143 format
= va_arg (args
, const char *);
1146 vasprintf (&buf
, format
, args
);
1152 fprintf (stderr
, _("Out of virtual memory\n"));
1158 while ((size_t) ((f
->buffer
+ f
->size
) - f
->current
) < n
+ 1)
1162 curroff
= f
->current
- f
->buffer
;
1164 f
->buffer
= xrealloc (f
->buffer
, f
->size
);
1165 f
->current
= f
->buffer
+ curroff
;
1168 memcpy (f
->current
, buf
, n
);
1170 f
->current
[0] = '\0';
1177 /* The number of zeroes we want to see before we start skipping them.
1178 The number is arbitrarily chosen. */
1180 #define SKIP_ZEROES (8)
1182 /* The number of zeroes to skip at the end of a section. If the
1183 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1184 SKIP_ZEROES, they will be disassembled. If there are fewer than
1185 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1186 attempt to avoid disassembling zeroes inserted by section
1189 #define SKIP_ZEROES_AT_END (3)
1191 /* Disassemble some data in memory between given values. */
1194 disassemble_bytes (info
, disassemble_fn
, insns
, data
, start
, stop
, relppp
,
1196 struct disassemble_info
*info
;
1197 disassembler_ftype disassemble_fn
;
1205 struct objdump_disasm_info
*aux
;
1209 int skip_addr_chars
;
1212 aux
= (struct objdump_disasm_info
*) info
->application_data
;
1218 bytes_per_line
= 16;
1220 /* Figure out how many characters to skip at the start of an
1221 address, to make the disassembly look nicer. We discard leading
1222 zeroes in chunks of 4, ensuring that there is always a leading
1224 skip_addr_chars
= 0;
1225 if (! prefix_addresses
)
1231 section
->vma
+ bfd_section_size (section
->owner
, section
));
1233 while (s
[0] == '0' && s
[1] == '0' && s
[2] == '0' && s
[3] == '0'
1236 skip_addr_chars
+= 4;
1241 info
->insn_info_valid
= 0;
1249 boolean need_nl
= false;
1251 /* If we see more than SKIP_ZEROES bytes of zeroes, we just
1253 for (z
= i
; z
< stop
; z
++)
1256 if (! disassemble_zeroes
1257 && (info
->insn_info_valid
== 0
1258 || info
->branch_delay_insns
== 0)
1259 && (z
- i
>= SKIP_ZEROES
1260 || (z
== stop
&& z
- i
< SKIP_ZEROES_AT_END
)))
1264 /* If there are more nonzero bytes to follow, we only skip
1265 zeroes in multiples of 4, to try to avoid running over
1266 the start of an instruction which happens to start with
1269 z
= i
+ ((z
- i
) &~ 3);
1282 if (with_line_numbers
|| with_source_code
)
1283 show_line (aux
->abfd
, section
, i
);
1285 if (! prefix_addresses
)
1289 sprintf_vma (buf
, section
->vma
+ i
);
1290 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
1294 printf ("%s:\t", buf
+ skip_addr_chars
);
1298 aux
->require_sec
= true;
1299 objdump_print_address (section
->vma
+ i
, info
);
1300 aux
->require_sec
= false;
1307 sfile
.buffer
= xmalloc (sfile
.size
);
1308 sfile
.current
= sfile
.buffer
;
1309 info
->fprintf_func
= (fprintf_ftype
) objdump_sprintf
;
1310 info
->stream
= (FILE *) &sfile
;
1311 info
->bytes_per_line
= 0;
1312 info
->bytes_per_chunk
= 0;
1314 /* FIXME: This is wrong. It tests the number of bytes
1315 in the last instruction, not the current one. */
1316 if (*relppp
< relppend
1317 && (**relppp
)->address
>= i
1318 && (**relppp
)->address
< i
+ bytes
)
1319 info
->flags
= INSN_HAS_RELOC
;
1323 bytes
= (*disassemble_fn
) (section
->vma
+ i
, info
);
1324 info
->fprintf_func
= (fprintf_ftype
) fprintf
;
1325 info
->stream
= stdout
;
1326 if (info
->bytes_per_line
!= 0)
1327 bytes_per_line
= info
->bytes_per_line
;
1330 if (sfile
.current
!= sfile
.buffer
)
1331 printf ("%s\n", sfile
.buffer
);
1332 free (sfile
.buffer
);
1340 bytes
= bytes_per_line
;
1341 if (i
+ bytes
> stop
)
1344 for (j
= i
; j
< i
+ bytes
; ++j
)
1346 if (isprint (data
[j
]))
1347 buf
[j
- i
] = data
[j
];
1354 if (prefix_addresses
1356 : show_raw_insn
>= 0)
1360 /* If ! prefix_addresses and ! wide_output, we print
1361 bytes_per_line bytes per line. */
1363 if (pb
> bytes_per_line
&& ! prefix_addresses
&& ! wide_output
)
1364 pb
= bytes_per_line
;
1366 if (info
->bytes_per_chunk
)
1367 bpc
= info
->bytes_per_chunk
;
1371 for (j
= i
; j
< i
+ pb
; j
+= bpc
)
1374 if (bpc
> 1 && info
->display_endian
== BFD_ENDIAN_LITTLE
)
1376 for (k
= bpc
- 1; k
>= 0; k
--)
1377 printf ("%02x", (unsigned) data
[j
+ k
]);
1382 for (k
= 0; k
< bpc
; k
++)
1383 printf ("%02x", (unsigned) data
[j
+ k
]);
1388 for (; pb
< bytes_per_line
; pb
+= bpc
)
1392 for (k
= 0; k
< bpc
; k
++)
1397 /* Separate raw data from instruction by extra space. */
1408 printf ("%s", sfile
.buffer
);
1409 free (sfile
.buffer
);
1412 if (prefix_addresses
1414 : show_raw_insn
>= 0)
1424 sprintf_vma (buf
, section
->vma
+ j
);
1425 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
1429 printf ("%s:\t", buf
+ skip_addr_chars
);
1431 pb
+= bytes_per_line
;
1434 for (; j
< i
+ pb
; j
+= bpc
)
1438 if (bpc
> 1 && info
->display_endian
== BFD_ENDIAN_LITTLE
)
1440 for (k
= bpc
- 1; k
>= 0; k
--)
1441 printf ("%02x", (unsigned) data
[j
+ k
]);
1446 for (k
= 0; k
< bpc
; k
++)
1447 printf ("%02x", (unsigned) data
[j
+ k
]);
1461 && (section
->flags
& SEC_RELOC
) != 0)
1463 while ((*relppp
) < relppend
1464 && ((**relppp
)->address
>= (bfd_vma
) i
1465 && (**relppp
)->address
< (bfd_vma
) i
+ bytes
))
1476 objdump_print_value (section
->vma
+ q
->address
, info
, true);
1478 printf (": %s\t", q
->howto
->name
);
1480 if (q
->sym_ptr_ptr
== NULL
|| *q
->sym_ptr_ptr
== NULL
)
1481 printf ("*unknown*");
1484 const char *sym_name
;
1486 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
1487 if (sym_name
!= NULL
&& *sym_name
!= '\0')
1488 objdump_print_symname (aux
->abfd
, info
, *q
->sym_ptr_ptr
);
1493 sym_sec
= bfd_get_section (*q
->sym_ptr_ptr
);
1494 sym_name
= bfd_get_section_name (aux
->abfd
, sym_sec
);
1495 if (sym_name
== NULL
|| *sym_name
== '\0')
1496 sym_name
= "*unknown*";
1497 printf ("%s", sym_name
);
1504 objdump_print_value (q
->addend
, info
, true);
1520 /* Disassemble the contents of an object file. */
1523 disassemble_data (abfd
)
1527 disassembler_ftype disassemble_fn
;
1528 struct disassemble_info disasm_info
;
1529 struct objdump_disasm_info aux
;
1533 prev_functionname
= NULL
;
1536 /* We make a copy of syms to sort. We don't want to sort syms
1537 because that will screw up the relocs. */
1538 sorted_syms
= (asymbol
**) xmalloc (symcount
* sizeof (asymbol
*));
1539 memcpy (sorted_syms
, syms
, symcount
* sizeof (asymbol
*));
1541 sorted_symcount
= remove_useless_symbols (sorted_syms
, symcount
);
1543 /* Sort the symbols into section and symbol order */
1544 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
1546 INIT_DISASSEMBLE_INFO(disasm_info
, stdout
, fprintf
);
1547 disasm_info
.application_data
= (PTR
) &aux
;
1549 aux
.require_sec
= false;
1550 disasm_info
.print_address_func
= objdump_print_address
;
1551 disasm_info
.symbol_at_address_func
= objdump_symbol_at_address
;
1553 if (machine
!= (char *) NULL
)
1555 const bfd_arch_info_type
*info
= bfd_scan_arch (machine
);
1558 fprintf (stderr
, _("%s: Can't use supplied machine %s\n"),
1563 abfd
->arch_info
= info
;
1566 if (endian
!= BFD_ENDIAN_UNKNOWN
)
1568 struct bfd_target
*xvec
;
1570 xvec
= (struct bfd_target
*) xmalloc (sizeof (struct bfd_target
));
1571 memcpy (xvec
, abfd
->xvec
, sizeof (struct bfd_target
));
1572 xvec
->byteorder
= endian
;
1576 disassemble_fn
= disassembler (abfd
);
1577 if (!disassemble_fn
)
1579 fprintf (stderr
, _("%s: Can't disassemble for architecture %s\n"),
1581 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
1586 disasm_info
.flavour
= bfd_get_flavour (abfd
);
1587 disasm_info
.arch
= bfd_get_arch (abfd
);
1588 disasm_info
.mach
= bfd_get_mach (abfd
);
1589 disasm_info
.disassembler_options
= disassembler_options
;
1591 if (bfd_big_endian (abfd
))
1592 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_BIG
;
1593 else if (bfd_little_endian (abfd
))
1594 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
1596 /* ??? Aborting here seems too drastic. We could default to big or little
1598 disasm_info
.endian
= BFD_ENDIAN_UNKNOWN
;
1600 for (section
= abfd
->sections
;
1601 section
!= (asection
*) NULL
;
1602 section
= section
->next
)
1604 bfd_byte
*data
= NULL
;
1605 bfd_size_type datasize
= 0;
1606 arelent
**relbuf
= NULL
;
1607 arelent
**relpp
= NULL
;
1608 arelent
**relppend
= NULL
;
1610 asymbol
*sym
= NULL
;
1613 if ((section
->flags
& SEC_LOAD
) == 0
1614 || (! disassemble_all
1616 && (section
->flags
& SEC_CODE
) == 0))
1618 if (only
!= (char *) NULL
&& strcmp (only
, section
->name
) != 0)
1622 && (section
->flags
& SEC_RELOC
) != 0)
1626 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
1628 bfd_fatal (bfd_get_filename (abfd
));
1634 relbuf
= (arelent
**) xmalloc (relsize
);
1635 relcount
= bfd_canonicalize_reloc (abfd
, section
, relbuf
, syms
);
1637 bfd_fatal (bfd_get_filename (abfd
));
1639 /* Sort the relocs by address. */
1640 qsort (relbuf
, relcount
, sizeof (arelent
*), compare_relocs
);
1643 relppend
= relpp
+ relcount
;
1645 /* Skip over the relocs belonging to addresses below the
1647 if (start_address
!= (bfd_vma
) -1)
1649 while (relpp
< relppend
1650 && (*relpp
)->address
< start_address
)
1656 printf (_("Disassembly of section %s:\n"), section
->name
);
1658 datasize
= bfd_get_section_size_before_reloc (section
);
1662 data
= (bfd_byte
*) xmalloc ((size_t) datasize
);
1664 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
1667 disasm_info
.buffer
= data
;
1668 disasm_info
.buffer_vma
= section
->vma
;
1669 disasm_info
.buffer_length
= datasize
;
1670 if (start_address
== (bfd_vma
) -1
1671 || start_address
< disasm_info
.buffer_vma
)
1674 i
= start_address
- disasm_info
.buffer_vma
;
1675 if (stop_address
== (bfd_vma
) -1)
1679 if (stop_address
< disasm_info
.buffer_vma
)
1682 stop
= stop_address
- disasm_info
.buffer_vma
;
1683 if (stop
> disasm_info
.buffer_length
)
1684 stop
= disasm_info
.buffer_length
;
1687 sym
= find_symbol_for_address (abfd
, section
, section
->vma
+ i
,
1696 if (sym
!= NULL
&& bfd_asymbol_value (sym
) <= section
->vma
+ i
)
1701 (x
< sorted_symcount
1702 && bfd_asymbol_value (sorted_syms
[x
]) <= section
->vma
+ i
);
1705 disasm_info
.symbols
= & sorted_syms
[place
];
1706 disasm_info
.num_symbols
= x
- place
;
1709 disasm_info
.symbols
= NULL
;
1711 if (! prefix_addresses
)
1714 objdump_print_addr_with_sym (abfd
, section
, sym
,
1721 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > section
->vma
+ i
)
1723 else if (sym
== NULL
)
1727 while (place
< sorted_symcount
1728 /* ??? Why the test for != section? */
1729 && (sorted_syms
[place
]->section
!= section
1730 || (bfd_asymbol_value (sorted_syms
[place
])
1731 <= bfd_asymbol_value (sym
))))
1733 if (place
>= sorted_symcount
)
1736 nextsym
= sorted_syms
[place
];
1739 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > section
->vma
+ i
)
1741 nextstop
= bfd_asymbol_value (sym
) - section
->vma
;
1742 if (nextstop
> stop
)
1745 else if (nextsym
== NULL
)
1749 nextstop
= bfd_asymbol_value (nextsym
) - section
->vma
;
1750 if (nextstop
> stop
)
1754 /* If a symbol is explicitly marked as being an object
1755 rather than a function, just dump the bytes without
1756 disassembling them. */
1759 || bfd_asymbol_value (sym
) > section
->vma
+ i
1760 || ((sym
->flags
& BSF_OBJECT
) == 0
1761 && (strstr (bfd_asymbol_name (sym
), "gnu_compiled")
1763 && (strstr (bfd_asymbol_name (sym
), "gcc2_compiled")
1765 || (sym
->flags
& BSF_FUNCTION
) != 0)
1770 disassemble_bytes (&disasm_info
, disassemble_fn
, insns
, data
, i
,
1771 nextstop
, &relpp
, relppend
);
1785 /* Define a table of stab values and print-strings. We wish the initializer
1786 could be a direct-mapped table, but instead we build one the first
1789 static void dump_section_stabs
PARAMS ((bfd
*abfd
, char *stabsect_name
,
1790 char *strsect_name
));
1792 /* Dump the stabs sections from an object file that has a section that
1793 uses Sun stabs encoding. */
1799 dump_section_stabs (abfd
, ".stab", ".stabstr");
1800 dump_section_stabs (abfd
, ".stab.excl", ".stab.exclstr");
1801 dump_section_stabs (abfd
, ".stab.index", ".stab.indexstr");
1802 dump_section_stabs (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1805 static bfd_byte
*stabs
;
1806 static bfd_size_type stab_size
;
1808 static char *strtab
;
1809 static bfd_size_type stabstr_size
;
1811 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1812 and string table section STRSECT_NAME into `strtab'.
1813 If the section exists and was read, allocate the space and return true.
1814 Otherwise return false. */
1817 read_section_stabs (abfd
, stabsect_name
, strsect_name
)
1819 const char *stabsect_name
;
1820 const char *strsect_name
;
1822 asection
*stabsect
, *stabstrsect
;
1824 stabsect
= bfd_get_section_by_name (abfd
, stabsect_name
);
1827 printf (_("No %s section present\n\n"), stabsect_name
);
1831 stabstrsect
= bfd_get_section_by_name (abfd
, strsect_name
);
1832 if (0 == stabstrsect
)
1834 fprintf (stderr
, _("%s: %s has no %s section\n"), program_name
,
1835 bfd_get_filename (abfd
), strsect_name
);
1840 stab_size
= bfd_section_size (abfd
, stabsect
);
1841 stabstr_size
= bfd_section_size (abfd
, stabstrsect
);
1843 stabs
= (bfd_byte
*) xmalloc (stab_size
);
1844 strtab
= (char *) xmalloc (stabstr_size
);
1846 if (! bfd_get_section_contents (abfd
, stabsect
, (PTR
) stabs
, 0, stab_size
))
1848 fprintf (stderr
, _("%s: Reading %s section of %s failed: %s\n"),
1849 program_name
, stabsect_name
, bfd_get_filename (abfd
),
1850 bfd_errmsg (bfd_get_error ()));
1857 if (! bfd_get_section_contents (abfd
, stabstrsect
, (PTR
) strtab
, 0,
1860 fprintf (stderr
, _("%s: Reading %s section of %s failed: %s\n"),
1861 program_name
, strsect_name
, bfd_get_filename (abfd
),
1862 bfd_errmsg (bfd_get_error ()));
1872 /* Stabs entries use a 12 byte format:
1873 4 byte string table index
1875 1 byte stab other field
1876 2 byte stab desc field
1878 FIXME: This will have to change for a 64 bit object format. */
1880 #define STRDXOFF (0)
1882 #define OTHEROFF (5)
1885 #define STABSIZE (12)
1887 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1888 using string table section STRSECT_NAME (in `strtab'). */
1891 print_section_stabs (abfd
, stabsect_name
, strsect_name
)
1893 const char *stabsect_name
;
1894 const char *strsect_name ATTRIBUTE_UNUSED
;
1897 unsigned file_string_table_offset
= 0, next_file_string_table_offset
= 0;
1898 bfd_byte
*stabp
, *stabs_end
;
1901 stabs_end
= stabp
+ stab_size
;
1903 printf (_("Contents of %s section:\n\n"), stabsect_name
);
1904 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1906 /* Loop through all symbols and print them.
1908 We start the index at -1 because there is a dummy symbol on
1909 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1911 for (i
= -1; stabp
< stabs_end
; stabp
+= STABSIZE
, i
++)
1915 unsigned char type
, other
;
1916 unsigned short desc
;
1919 strx
= bfd_h_get_32 (abfd
, stabp
+ STRDXOFF
);
1920 type
= bfd_h_get_8 (abfd
, stabp
+ TYPEOFF
);
1921 other
= bfd_h_get_8 (abfd
, stabp
+ OTHEROFF
);
1922 desc
= bfd_h_get_16 (abfd
, stabp
+ DESCOFF
);
1923 value
= bfd_h_get_32 (abfd
, stabp
+ VALOFF
);
1925 printf ("\n%-6d ", i
);
1926 /* Either print the stab name, or, if unnamed, print its number
1927 again (makes consistent formatting for tools like awk). */
1928 name
= bfd_get_stab_name (type
);
1930 printf ("%-6s", name
);
1931 else if (type
== N_UNDF
)
1934 printf ("%-6d", type
);
1935 printf (" %-6d %-6d ", other
, desc
);
1937 printf (" %-6lu", strx
);
1939 /* Symbols with type == 0 (N_UNDF) specify the length of the
1940 string table associated with this file. We use that info
1941 to know how to relocate the *next* file's string table indices. */
1945 file_string_table_offset
= next_file_string_table_offset
;
1946 next_file_string_table_offset
+= value
;
1950 /* Using the (possibly updated) string table offset, print the
1951 string (if any) associated with this symbol. */
1953 if ((strx
+ file_string_table_offset
) < stabstr_size
)
1954 printf (" %s", &strtab
[strx
+ file_string_table_offset
]);
1963 dump_section_stabs (abfd
, stabsect_name
, strsect_name
)
1965 char *stabsect_name
;
1970 /* Check for section names for which stabsect_name is a prefix, to
1971 handle .stab0, etc. */
1972 for (s
= abfd
->sections
;
1978 len
= strlen (stabsect_name
);
1980 /* If the prefix matches, and the files section name ends with a
1981 nul or a digit, then we match. I.e., we want either an exact
1982 match or a section followed by a number. */
1983 if (strncmp (stabsect_name
, s
->name
, len
) == 0
1984 && (s
->name
[len
] == '\000'
1985 || isdigit ((unsigned char) s
->name
[len
])))
1987 if (read_section_stabs (abfd
, s
->name
, strsect_name
))
1989 print_section_stabs (abfd
, s
->name
, strsect_name
);
1998 dump_bfd_header (abfd
)
2003 printf (_("architecture: %s, "),
2004 bfd_printable_arch_mach (bfd_get_arch (abfd
),
2005 bfd_get_mach (abfd
)));
2006 printf (_("flags 0x%08x:\n"), abfd
->flags
);
2008 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2009 PF (HAS_RELOC
, "HAS_RELOC");
2010 PF (EXEC_P
, "EXEC_P");
2011 PF (HAS_LINENO
, "HAS_LINENO");
2012 PF (HAS_DEBUG
, "HAS_DEBUG");
2013 PF (HAS_SYMS
, "HAS_SYMS");
2014 PF (HAS_LOCALS
, "HAS_LOCALS");
2015 PF (DYNAMIC
, "DYNAMIC");
2016 PF (WP_TEXT
, "WP_TEXT");
2017 PF (D_PAGED
, "D_PAGED");
2018 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
2019 printf (_("\nstart address 0x"));
2020 printf_vma (abfd
->start_address
);
2025 dump_bfd_private_header (abfd
)
2028 bfd_print_private_bfd_data (abfd
, stdout
);
2037 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
2039 nonfatal (bfd_get_filename (abfd
));
2040 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
2042 list_matching_formats (matching
);
2048 /* If we are adjusting section VMA's, change them all now. Changing
2049 the BFD information is a hack. However, we must do it, or
2050 bfd_find_nearest_line will not do the right thing. */
2051 if (adjust_section_vma
!= 0)
2055 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2057 s
->vma
+= adjust_section_vma
;
2058 s
->lma
+= adjust_section_vma
;
2062 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd
),
2065 print_arelt_descr (stdout
, abfd
, true);
2066 if (dump_file_header
)
2067 dump_bfd_header (abfd
);
2068 if (dump_private_headers
)
2069 dump_bfd_private_header (abfd
);
2071 if (dump_section_headers
)
2072 dump_headers (abfd
);
2073 if (dump_symtab
|| dump_reloc_info
|| disassemble
|| dump_debugging
)
2075 syms
= slurp_symtab (abfd
);
2077 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
)
2079 dynsyms
= slurp_dynamic_symtab (abfd
);
2082 dump_symbols (abfd
, false);
2083 if (dump_dynamic_symtab
)
2084 dump_symbols (abfd
, true);
2085 if (dump_stab_section_info
)
2087 if (dump_reloc_info
&& ! disassemble
)
2089 if (dump_dynamic_reloc_info
)
2090 dump_dynamic_relocs (abfd
);
2091 if (dump_section_contents
)
2094 disassemble_data (abfd
);
2099 dhandle
= read_debugging_info (abfd
, syms
, symcount
);
2100 if (dhandle
!= NULL
)
2102 if (! print_debugging_info (stdout
, dhandle
))
2105 _("%s: printing debugging information failed\n"),
2106 bfd_get_filename (abfd
));
2124 display_file (filename
, target
)
2128 bfd
*file
, *arfile
= (bfd
*) NULL
;
2130 file
= bfd_openr (filename
, target
);
2133 nonfatal (filename
);
2137 if (bfd_check_format (file
, bfd_archive
) == true)
2139 bfd
*last_arfile
= NULL
;
2141 printf (_("In archive %s:\n"), bfd_get_filename (file
));
2144 bfd_set_error (bfd_error_no_error
);
2146 arfile
= bfd_openr_next_archived_file (file
, arfile
);
2149 if (bfd_get_error () != bfd_error_no_more_archived_files
)
2150 nonfatal (bfd_get_filename (file
));
2154 display_bfd (arfile
);
2156 if (last_arfile
!= NULL
)
2157 bfd_close (last_arfile
);
2158 last_arfile
= arfile
;
2161 if (last_arfile
!= NULL
)
2162 bfd_close (last_arfile
);
2170 /* Actually display the various requested regions */
2178 bfd_size_type datasize
= 0;
2180 bfd_size_type start
, stop
;
2182 for (section
= abfd
->sections
; section
!= NULL
; section
=
2187 if (only
== (char *) NULL
||
2188 strcmp (only
, section
->name
) == 0)
2190 if (section
->flags
& SEC_HAS_CONTENTS
)
2192 printf (_("Contents of section %s:\n"), section
->name
);
2194 if (bfd_section_size (abfd
, section
) == 0)
2196 data
= (bfd_byte
*) xmalloc ((size_t) bfd_section_size (abfd
, section
));
2197 datasize
= bfd_section_size (abfd
, section
);
2200 bfd_get_section_contents (abfd
, section
, (PTR
) data
, 0, bfd_section_size (abfd
, section
));
2202 if (start_address
== (bfd_vma
) -1
2203 || start_address
< section
->vma
)
2206 start
= start_address
- section
->vma
;
2207 if (stop_address
== (bfd_vma
) -1)
2208 stop
= bfd_section_size (abfd
, section
);
2211 if (stop_address
< section
->vma
)
2214 stop
= stop_address
- section
->vma
;
2215 if (stop
> bfd_section_size (abfd
, section
))
2216 stop
= bfd_section_size (abfd
, section
);
2218 for (i
= start
; i
< stop
; i
+= onaline
)
2222 printf (" %04lx ", (unsigned long int) (i
+ section
->vma
));
2223 for (j
= i
; j
< i
+ onaline
; j
++)
2226 printf ("%02x", (unsigned) (data
[j
]));
2234 for (j
= i
; j
< i
+ onaline
; j
++)
2239 printf ("%c", isprint (data
[j
]) ? data
[j
] : '.');
2249 /* Should perhaps share code and display with nm? */
2251 dump_symbols (abfd
, dynamic
)
2252 bfd
*abfd ATTRIBUTE_UNUSED
;
2265 printf ("DYNAMIC SYMBOL TABLE:\n");
2273 printf ("SYMBOL TABLE:\n");
2276 for (count
= 0; count
< max
; count
++)
2280 bfd
*cur_bfd
= bfd_asymbol_bfd (*current
);
2282 if (cur_bfd
!= NULL
)
2287 name
= bfd_asymbol_name (*current
);
2289 if (do_demangle
&& name
!= NULL
&& *name
!= '\0')
2293 /* If we want to demangle the name, we demangle it
2294 here, and temporarily clobber it while calling
2295 bfd_print_symbol. FIXME: This is a gross hack. */
2298 if (bfd_get_symbol_leading_char (cur_bfd
) == *n
)
2300 alloc
= cplus_demangle (n
, DMGL_ANSI
| DMGL_PARAMS
);
2302 (*current
)->name
= alloc
;
2304 (*current
)->name
= n
;
2307 bfd_print_symbol (cur_bfd
, stdout
, *current
,
2308 bfd_print_symbol_all
);
2310 (*current
)->name
= name
;
2331 for (a
= abfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
2335 if (bfd_is_abs_section (a
))
2337 if (bfd_is_und_section (a
))
2339 if (bfd_is_com_section (a
))
2344 if (strcmp (only
, a
->name
))
2347 else if ((a
->flags
& SEC_RELOC
) == 0)
2350 relsize
= bfd_get_reloc_upper_bound (abfd
, a
);
2352 bfd_fatal (bfd_get_filename (abfd
));
2354 printf ("RELOCATION RECORDS FOR [%s]:", a
->name
);
2358 printf (" (none)\n\n");
2362 relpp
= (arelent
**) xmalloc (relsize
);
2363 relcount
= bfd_canonicalize_reloc (abfd
, a
, relpp
, syms
);
2365 bfd_fatal (bfd_get_filename (abfd
));
2366 else if (relcount
== 0)
2368 printf (" (none)\n\n");
2373 dump_reloc_set (abfd
, a
, relpp
, relcount
);
2382 dump_dynamic_relocs (abfd
)
2389 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
2391 bfd_fatal (bfd_get_filename (abfd
));
2393 printf ("DYNAMIC RELOCATION RECORDS");
2397 printf (" (none)\n\n");
2401 relpp
= (arelent
**) xmalloc (relsize
);
2402 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
2404 bfd_fatal (bfd_get_filename (abfd
));
2405 else if (relcount
== 0)
2407 printf (" (none)\n\n");
2412 dump_reloc_set (abfd
, (asection
*) NULL
, relpp
, relcount
);
2420 dump_reloc_set (abfd
, sec
, relpp
, relcount
)
2427 char *last_filename
, *last_functionname
;
2428 unsigned int last_line
;
2430 /* Get column headers lined up reasonably. */
2436 sprintf_vma (buf
, (bfd_vma
) -1);
2437 width
= strlen (buf
) - 7;
2439 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
2442 last_filename
= NULL
;
2443 last_functionname
= NULL
;
2446 for (p
= relpp
; relcount
&& *p
!= (arelent
*) NULL
; p
++, relcount
--)
2449 const char *filename
, *functionname
;
2451 const char *sym_name
;
2452 const char *section_name
;
2454 if (start_address
!= (bfd_vma
) -1
2455 && q
->address
< start_address
)
2457 if (stop_address
!= (bfd_vma
) -1
2458 && q
->address
> stop_address
)
2461 if (with_line_numbers
2463 && bfd_find_nearest_line (abfd
, sec
, syms
, q
->address
,
2464 &filename
, &functionname
, &line
))
2466 if (functionname
!= NULL
2467 && (last_functionname
== NULL
2468 || strcmp (functionname
, last_functionname
) != 0))
2470 printf ("%s():\n", functionname
);
2471 if (last_functionname
!= NULL
)
2472 free (last_functionname
);
2473 last_functionname
= xstrdup (functionname
);
2476 && (line
!= last_line
2477 || (filename
!= NULL
2478 && last_filename
!= NULL
2479 && strcmp (filename
, last_filename
) != 0)))
2481 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
2483 if (last_filename
!= NULL
)
2484 free (last_filename
);
2485 if (filename
== NULL
)
2486 last_filename
= NULL
;
2488 last_filename
= xstrdup (filename
);
2492 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
2494 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
2495 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
2500 section_name
= NULL
;
2504 printf_vma (q
->address
);
2506 printf (" %-16s ", q
->howto
->name
);
2508 printf (" %-16d ", q
->howto
->type
);
2509 objdump_print_symname (abfd
, (struct disassemble_info
*) NULL
,
2514 if (section_name
== (CONST
char *) NULL
)
2515 section_name
= "*unknown*";
2516 printf_vma (q
->address
);
2517 printf (" %-16s [%s]",
2524 printf_vma (q
->addend
);
2530 /* The length of the longest architecture name + 1. */
2531 #define LONGEST_ARCH sizeof("rs6000:6000")
2534 endian_string (endian
)
2535 enum bfd_endian endian
;
2537 if (endian
== BFD_ENDIAN_BIG
)
2538 return "big endian";
2539 else if (endian
== BFD_ENDIAN_LITTLE
)
2540 return "little endian";
2542 return "endianness unknown";
2545 /* List the targets that BFD is configured to support, each followed
2546 by its endianness and the architectures it supports. */
2549 display_target_list ()
2551 extern bfd_target
*bfd_target_vector
[];
2555 dummy_name
= choose_temp_base ();
2556 for (t
= 0; bfd_target_vector
[t
]; t
++)
2558 bfd_target
*p
= bfd_target_vector
[t
];
2559 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
2562 printf ("%s\n (header %s, data %s)\n", p
->name
,
2563 endian_string (p
->header_byteorder
),
2564 endian_string (p
->byteorder
));
2568 nonfatal (dummy_name
);
2572 if (! bfd_set_format (abfd
, bfd_object
))
2574 if (bfd_get_error () != bfd_error_invalid_operation
)
2579 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
2580 if (bfd_set_arch_mach (abfd
, (enum bfd_architecture
) a
, 0))
2582 bfd_printable_arch_mach ((enum bfd_architecture
) a
, 0));
2584 unlink (dummy_name
);
2588 /* Print a table showing which architectures are supported for entries
2589 FIRST through LAST-1 of bfd_target_vector (targets across,
2590 architectures down). */
2593 display_info_table (first
, last
)
2597 extern bfd_target
*bfd_target_vector
[];
2601 /* Print heading of target names. */
2602 printf ("\n%*s", (int) LONGEST_ARCH
, " ");
2603 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
2604 printf ("%s ", bfd_target_vector
[t
]->name
);
2607 dummy_name
= choose_temp_base ();
2608 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
2609 if (strcmp (bfd_printable_arch_mach (a
, 0), "UNKNOWN!") != 0)
2611 printf ("%*s ", (int) LONGEST_ARCH
- 1,
2612 bfd_printable_arch_mach (a
, 0));
2613 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
2615 bfd_target
*p
= bfd_target_vector
[t
];
2617 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
2627 if (! bfd_set_format (abfd
, bfd_object
))
2629 if (bfd_get_error () != bfd_error_invalid_operation
)
2637 if (! bfd_set_arch_mach (abfd
, a
, 0))
2642 printf ("%s ", p
->name
);
2645 int l
= strlen (p
->name
);
2653 unlink (dummy_name
);
2657 /* Print tables of all the target-architecture combinations that
2658 BFD has been configured to support. */
2661 display_target_tables ()
2664 extern bfd_target
*bfd_target_vector
[];
2668 colum
= getenv ("COLUMNS");
2670 columns
= atoi (colum
);
2675 while (bfd_target_vector
[t
] != NULL
)
2679 wid
= LONGEST_ARCH
+ strlen (bfd_target_vector
[t
]->name
) + 1;
2681 while (wid
< columns
&& bfd_target_vector
[t
] != NULL
)
2685 newwid
= wid
+ strlen (bfd_target_vector
[t
]->name
) + 1;
2686 if (newwid
>= columns
)
2691 display_info_table (oldt
, t
);
2698 printf (_("BFD header file version %s\n"), BFD_VERSION
);
2699 display_target_list ();
2700 display_target_tables ();
2709 char *target
= default_target
;
2710 boolean seenflag
= false;
2712 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2713 setlocale (LC_MESSAGES
, "");
2715 bindtextdomain (PACKAGE
, LOCALEDIR
);
2716 textdomain (PACKAGE
);
2718 program_name
= *argv
;
2719 xmalloc_set_program_name (program_name
);
2721 START_PROGRESS (program_name
, 0);
2724 set_default_bfd_target ();
2726 while ((c
= getopt_long (argc
, argv
, "pib:m:M:VCdDlfahrRtTxsSj:wE:",
2727 long_options
, (int *) 0))
2730 if (c
!= 'l' && c
!= OPTION_START_ADDRESS
&& c
!= OPTION_STOP_ADDRESS
)
2735 break; /* we've been given a long option */
2740 disassembler_options
= optarg
;
2746 with_line_numbers
= 1;
2752 dump_file_header
= true;
2755 formats_info
= true;
2758 dump_private_headers
= 1;
2761 dump_private_headers
= 1;
2763 dump_reloc_info
= 1;
2764 dump_file_header
= true;
2766 dump_section_headers
= 1;
2772 dump_dynamic_symtab
= 1;
2781 disassemble
= disassemble_all
= true;
2785 with_source_code
= true;
2788 dump_section_contents
= 1;
2791 dump_reloc_info
= 1;
2794 dump_dynamic_reloc_info
= 1;
2800 dump_section_headers
= 1;
2810 case OPTION_ADJUST_VMA
:
2811 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
2813 case OPTION_START_ADDRESS
:
2814 start_address
= parse_vma (optarg
, "--start-address");
2816 case OPTION_STOP_ADDRESS
:
2817 stop_address
= parse_vma (optarg
, "--stop-address");
2820 if (strcmp (optarg
, "B") == 0)
2821 endian
= BFD_ENDIAN_BIG
;
2822 else if (strcmp (optarg
, "L") == 0)
2823 endian
= BFD_ENDIAN_LITTLE
;
2826 fprintf (stderr
, _("%s: unrecognized -E option\n"),
2832 if (strncmp (optarg
, "big", strlen (optarg
)) == 0)
2833 endian
= BFD_ENDIAN_BIG
;
2834 else if (strncmp (optarg
, "little", strlen (optarg
)) == 0)
2835 endian
= BFD_ENDIAN_LITTLE
;
2838 fprintf (stderr
, _("%s: unrecognized --endian type `%s'\n"),
2839 program_name
, optarg
);
2849 print_version ("objdump");
2851 if (seenflag
== false)
2861 display_file ("a.out", target
);
2863 for (; optind
< argc
;)
2864 display_file (argv
[optind
++], target
);
2867 END_PROGRESS (program_name
);