1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "libiberty.h"
30 /* Internal headers for the ELF .stab-dump code - sorry. */
31 #define BYTES_IN_WORD 32
32 #include "aout/aout64.h"
34 #ifdef NEED_DECLARATION_FPRINTF
35 /* This is needed by INIT_DISASSEMBLE_INFO. */
36 extern int fprintf
PARAMS ((FILE *, const char *));
39 char *default_target
= NULL
; /* default at runtime */
41 extern char *program_version
;
43 int show_version
= 0; /* show the version number */
44 int dump_section_contents
; /* -s */
45 int dump_section_headers
; /* -h */
46 boolean dump_file_header
; /* -f */
47 int dump_symtab
; /* -t */
48 int dump_dynamic_symtab
; /* -T */
49 int dump_reloc_info
; /* -r */
50 int dump_dynamic_reloc_info
; /* -R */
51 int dump_ar_hdrs
; /* -a */
52 int dump_private_headers
; /* -p */
53 int with_line_numbers
; /* -l */
54 boolean with_source_code
; /* -S */
55 int show_raw_insn
; /* --show-raw-insn */
56 int dump_stab_section_info
; /* --stabs */
57 boolean disassemble
; /* -d */
58 boolean disassemble_all
; /* -D */
59 boolean formats_info
; /* -i */
60 char *only
; /* -j secname */
61 int wide_output
; /* -w */
62 bfd_vma start_address
= (bfd_vma
) -1; /* --start-address */
63 bfd_vma stop_address
= (bfd_vma
) -1; /* --stop-address */
64 int dump_debugging
; /* --debugging */
66 /* Extra info to pass to the disassembler address printing function. */
67 struct objdump_disasm_info
{
73 /* Architecture to disassemble for, or default if NULL. */
74 char *machine
= (char *) NULL
;
76 /* The symbol table. */
79 /* Number of symbols in `syms'. */
82 /* The sorted symbol table. */
83 asymbol
**sorted_syms
;
85 /* Number of symbols in `sorted_syms'. */
86 long sorted_symcount
= 0;
88 /* The dynamic symbol table. */
91 /* Number of symbols in `dynsyms'. */
94 /* Forward declarations. */
97 display_file
PARAMS ((char *filename
, char *target
));
100 dump_data
PARAMS ((bfd
*abfd
));
103 dump_relocs
PARAMS ((bfd
*abfd
));
106 dump_dynamic_relocs
PARAMS ((bfd
* abfd
));
109 dump_reloc_set
PARAMS ((bfd
*, arelent
**, long));
112 dump_symbols
PARAMS ((bfd
*abfd
, boolean dynamic
));
115 display_bfd
PARAMS ((bfd
*abfd
));
118 objdump_print_value
PARAMS ((bfd_vma
, FILE *));
121 objdump_print_address
PARAMS ((bfd_vma
, struct disassemble_info
*));
124 show_line
PARAMS ((bfd
*, asection
*, bfd_vma
));
127 endian_string
PARAMS ((enum bfd_endian
));
130 usage (stream
, status
)
135 Usage: %s [-ahifdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
136 [--archive-headers] [--target=bfdname] [--debugging] [--disassemble]\n\
137 [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
138 [--info] [--section=section-name] [--line-numbers] [--source]\n",
141 [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
142 [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
143 [--wide] [--version] [--help] [--private-headers]\n\
144 [--start-address=addr] [--stop-address=addr]\n\
145 [--show-raw-insn] objfile...\n\
146 at least one option besides -l (--line-numbers) must be given\n");
147 list_supported_targets (program_name
, stream
);
151 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
153 #define OPTION_START_ADDRESS (150)
154 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
156 static struct option long_options
[]=
158 {"all-headers", no_argument
, NULL
, 'x'},
159 {"private-headers", no_argument
, NULL
, 'p'},
160 {"architecture", required_argument
, NULL
, 'm'},
161 {"archive-headers", no_argument
, NULL
, 'a'},
162 {"debugging", no_argument
, &dump_debugging
, 1},
163 {"disassemble", no_argument
, NULL
, 'd'},
164 {"disassemble-all", no_argument
, NULL
, 'D'},
165 {"dynamic-reloc", no_argument
, NULL
, 'R'},
166 {"dynamic-syms", no_argument
, NULL
, 'T'},
167 {"file-headers", no_argument
, NULL
, 'f'},
168 {"full-contents", no_argument
, NULL
, 's'},
169 {"headers", no_argument
, NULL
, 'h'},
170 {"help", no_argument
, NULL
, 'H'},
171 {"info", no_argument
, NULL
, 'i'},
172 {"line-numbers", no_argument
, NULL
, 'l'},
173 {"reloc", no_argument
, NULL
, 'r'},
174 {"section", required_argument
, NULL
, 'j'},
175 {"section-headers", no_argument
, NULL
, 'h'},
176 {"show-raw-insn", no_argument
, &show_raw_insn
, 1},
177 {"source", no_argument
, NULL
, 'S'},
178 {"stabs", no_argument
, &dump_stab_section_info
, 1},
179 {"start-address", required_argument
, NULL
, OPTION_START_ADDRESS
},
180 {"stop-address", required_argument
, NULL
, OPTION_STOP_ADDRESS
},
181 {"syms", no_argument
, NULL
, 't'},
182 {"target", required_argument
, NULL
, 'b'},
183 {"version", no_argument
, &show_version
, 1},
184 {"wide", no_argument
, &wide_output
, 'w'},
185 {0, no_argument
, 0, 0}
189 dump_section_header (abfd
, section
, ignored
)
196 printf ("%3d %-14s%08lx ", section
->index
,
197 bfd_get_section_name (abfd
, section
),
198 (unsigned long) bfd_section_size (abfd
, section
));
199 printf_vma (bfd_get_section_vma (abfd
, section
));
201 printf_vma (section
->lma
);
202 printf (" %08lx 2**%u", section
->filepos
,
203 bfd_get_section_alignment (abfd
, section
));
209 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
211 PF (SEC_HAS_CONTENTS
, "CONTENTS");
212 PF (SEC_ALLOC
, "ALLOC");
213 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
214 PF (SEC_CONSTRUCTOR_TEXT
, "CONSTRUCTOR TEXT");
215 PF (SEC_CONSTRUCTOR_DATA
, "CONSTRUCTOR DATA");
216 PF (SEC_CONSTRUCTOR_BSS
, "CONSTRUCTOR BSS");
217 PF (SEC_LOAD
, "LOAD");
218 PF (SEC_RELOC
, "RELOC");
220 PF (SEC_BALIGN
, "BALIGN");
222 PF (SEC_READONLY
, "READONLY");
223 PF (SEC_CODE
, "CODE");
224 PF (SEC_DATA
, "DATA");
226 PF (SEC_DEBUGGING
, "DEBUGGING");
227 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
228 PF (SEC_EXCLUDE
, "EXCLUDE");
229 PF (SEC_SORT_ENTRIES
, "SORT ENTRIES");
238 printf ("Sections:\n");
240 printf ("Idx Name Size VMA LMA File off Algn\n");
242 printf ("Idx Name Size VMA LMA File off Algn\n");
244 bfd_map_over_sections (abfd
, dump_section_header
, (PTR
) NULL
);
251 asymbol
**sy
= (asymbol
**) NULL
;
254 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
256 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd
));
261 storage
= bfd_get_symtab_upper_bound (abfd
);
263 bfd_fatal (bfd_get_filename (abfd
));
267 sy
= (asymbol
**) xmalloc (storage
);
269 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
271 bfd_fatal (bfd_get_filename (abfd
));
273 fprintf (stderr
, "%s: %s: No symbols\n",
274 program_name
, bfd_get_filename (abfd
));
278 /* Read in the dynamic symbols. */
281 slurp_dynamic_symtab (abfd
)
284 asymbol
**sy
= (asymbol
**) NULL
;
287 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
290 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
292 fprintf (stderr
, "%s: %s: not a dynamic object\n",
293 program_name
, bfd_get_filename (abfd
));
298 bfd_fatal (bfd_get_filename (abfd
));
303 sy
= (asymbol
**) xmalloc (storage
);
305 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
307 bfd_fatal (bfd_get_filename (abfd
));
308 if (dynsymcount
== 0)
309 fprintf (stderr
, "%s: %s: No dynamic symbols\n",
310 program_name
, bfd_get_filename (abfd
));
314 /* Filter out (in place) symbols that are useless for disassembly.
315 COUNT is the number of elements in SYMBOLS.
316 Return the number of useful symbols. */
319 remove_useless_symbols (symbols
, count
)
323 register asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
327 asymbol
*sym
= *in_ptr
++;
329 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
331 if (sym
->flags
& (BSF_DEBUGGING
))
333 if (bfd_is_und_section (sym
->section
)
334 || bfd_is_com_section (sym
->section
))
339 return out_ptr
- symbols
;
342 /* Sort symbols into value order. */
345 compare_symbols (ap
, bp
)
349 const asymbol
*a
= *(const asymbol
**)ap
;
350 const asymbol
*b
= *(const asymbol
**)bp
;
354 flagword aflags
, bflags
;
356 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
358 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
361 if (a
->section
> b
->section
)
363 else if (a
->section
< b
->section
)
366 an
= bfd_asymbol_name (a
);
367 bn
= bfd_asymbol_name (b
);
371 /* The symbols gnu_compiled and gcc2_compiled convey no real
372 information, so put them after other symbols with the same value. */
374 af
= (strstr (an
, "gnu_compiled") != NULL
375 || strstr (an
, "gcc2_compiled") != NULL
);
376 bf
= (strstr (bn
, "gnu_compiled") != NULL
377 || strstr (bn
, "gcc2_compiled") != NULL
);
384 /* We use a heuristic for the file name, to try to sort it after
385 more useful symbols. It may not work on non Unix systems, but it
386 doesn't really matter; the only difference is precisely which
387 symbol names get printed. */
389 #define file_symbol(s, sn, snl) \
390 (((s)->flags & BSF_FILE) != 0 \
391 || ((sn)[(snl) - 2] == '.' \
392 && ((sn)[(snl) - 1] == 'o' \
393 || (sn)[(snl) - 1] == 'a')))
395 af
= file_symbol (a
, an
, anl
);
396 bf
= file_symbol (b
, bn
, bnl
);
403 /* Finally, try to sort global symbols before local symbols before
404 debugging symbols. */
409 if ((aflags
& BSF_DEBUGGING
) != (bflags
& BSF_DEBUGGING
))
411 if ((aflags
& BSF_DEBUGGING
) != 0)
416 if ((aflags
& BSF_LOCAL
) != (bflags
& BSF_LOCAL
))
418 if ((aflags
& BSF_LOCAL
) != 0)
427 /* Sort relocs into address order. */
430 compare_relocs (ap
, bp
)
434 const arelent
*a
= *(const arelent
**)ap
;
435 const arelent
*b
= *(const arelent
**)bp
;
437 if (a
->address
> b
->address
)
439 else if (a
->address
< b
->address
)
442 /* So that associated relocations tied to the same address show up
443 in the correct order, we don't do any further sorting. */
452 /* Print VMA to STREAM with no leading zeroes. */
455 objdump_print_value (vma
, stream
)
462 sprintf_vma (buf
, vma
);
463 for (p
= buf
; *p
== '0'; ++p
)
465 fprintf (stream
, "%s", p
);
468 /* Print VMA symbolically to INFO if possible. */
471 objdump_print_address (vma
, info
)
473 struct disassemble_info
*info
;
475 /* @@ Would it speed things up to cache the last two symbols returned,
476 and maybe their address ranges? For many processors, only one memory
477 operand can be present at a time, so the 2-entry cache wouldn't be
478 constantly churned by code doing heavy memory accesses. */
480 /* Indices in `sorted_syms'. */
482 long max
= sorted_symcount
;
485 fprintf_vma (info
->stream
, vma
);
487 if (sorted_symcount
< 1)
490 /* Perform a binary search looking for the closest symbol to the
491 required value. We are searching the range (min, max]. */
492 while (min
+ 1 < max
)
496 thisplace
= (max
+ min
) / 2;
497 sym
= sorted_syms
[thisplace
];
499 if (bfd_asymbol_value (sym
) > vma
)
501 else if (bfd_asymbol_value (sym
) < vma
)
510 /* The symbol we want is now in min, the low end of the range we
511 were searching. If there are several symbols with the same
512 value, we want the first one. */
515 && (bfd_asymbol_value (sorted_syms
[thisplace
])
516 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
520 /* If the file is relocateable, and the symbol could be from this
521 section, prefer a symbol from this section over symbols from
522 others, even if the other symbol's value might be closer.
524 Note that this may be wrong for some symbol references if the
525 sections have overlapping memory ranges, but in that case there's
526 no way to tell what's desired without looking at the relocation
528 struct objdump_disasm_info
*aux
;
531 aux
= (struct objdump_disasm_info
*) info
->application_data
;
532 if (sorted_syms
[thisplace
]->section
!= aux
->sec
534 || ((aux
->abfd
->flags
& HAS_RELOC
) != 0
535 && vma
>= bfd_get_section_vma (aux
->abfd
, aux
->sec
)
536 && vma
< (bfd_get_section_vma (aux
->abfd
, aux
->sec
)
537 + bfd_section_size (aux
->abfd
, aux
->sec
)))))
539 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
541 if (bfd_asymbol_value (sorted_syms
[i
])
542 != bfd_asymbol_value (sorted_syms
[thisplace
]))
548 if (sorted_syms
[i
]->section
== aux
->sec
550 || sorted_syms
[i
- 1]->section
!= aux
->sec
551 || (bfd_asymbol_value (sorted_syms
[i
])
552 != bfd_asymbol_value (sorted_syms
[i
- 1]))))
559 if (sorted_syms
[thisplace
]->section
!= aux
->sec
)
561 /* We didn't find a good symbol with a smaller value.
562 Look for one with a larger value. */
563 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
565 if (sorted_syms
[i
]->section
== aux
->sec
)
573 if (sorted_syms
[thisplace
]->section
!= aux
->sec
575 || ((aux
->abfd
->flags
& HAS_RELOC
) != 0
576 && vma
>= bfd_get_section_vma (aux
->abfd
, aux
->sec
)
577 && vma
< (bfd_get_section_vma (aux
->abfd
, aux
->sec
)
578 + bfd_section_size (aux
->abfd
, aux
->sec
)))))
582 fprintf (info
->stream
, " <%s",
583 bfd_get_section_name (aux
->abfd
, aux
->sec
));
584 secaddr
= bfd_get_section_vma (aux
->abfd
, aux
->sec
);
587 fprintf (info
->stream
, "-");
588 objdump_print_value (secaddr
- vma
, info
->stream
);
590 else if (vma
> secaddr
)
592 fprintf (info
->stream
, "+");
593 objdump_print_value (vma
- secaddr
, info
->stream
);
595 fprintf (info
->stream
, ">");
601 fprintf (info
->stream
, " <%s", sorted_syms
[thisplace
]->name
);
602 if (bfd_asymbol_value (sorted_syms
[thisplace
]) > vma
)
604 fprintf (info
->stream
, "-");
605 objdump_print_value (bfd_asymbol_value (sorted_syms
[thisplace
]) - vma
,
608 else if (vma
> bfd_asymbol_value (sorted_syms
[thisplace
]))
610 fprintf (info
->stream
, "+");
611 objdump_print_value (vma
- bfd_asymbol_value (sorted_syms
[thisplace
]),
614 fprintf (info
->stream
, ">");
617 /* Hold the last function name and the last line number we displayed
620 static char *prev_functionname
;
621 static unsigned int prev_line
;
623 /* We keep a list of all files that we have seen when doing a
624 dissassembly with source, so that we know how much of the file to
625 display. This can be important for inlined functions. */
627 struct print_file_list
629 struct print_file_list
*next
;
635 static struct print_file_list
*print_files
;
637 /* The number of preceding context lines to show when we start
638 displaying a file for the first time. */
640 #define SHOW_PRECEDING_CONTEXT_LINES (5)
642 /* Skip ahead to a given line in a file, optionally printing each
646 skip_to_line
PARAMS ((struct print_file_list
*, unsigned int, boolean
));
649 skip_to_line (p
, line
, show
)
650 struct print_file_list
*p
;
654 while (p
->line
< line
)
658 if (fgets (buf
, sizeof buf
, p
->f
) == NULL
)
668 if (strchr (buf
, '\n') != NULL
)
673 /* Show the line number, or the source line, in a dissassembly
677 show_line (abfd
, section
, off
)
682 CONST
char *filename
;
683 CONST
char *functionname
;
686 if (! with_line_numbers
&& ! with_source_code
)
689 if (! bfd_find_nearest_line (abfd
, section
, syms
, off
, &filename
,
690 &functionname
, &line
))
693 if (filename
!= NULL
&& *filename
== '\0')
695 if (functionname
!= NULL
&& *functionname
== '\0')
698 if (with_line_numbers
)
700 if (functionname
!= NULL
701 && (prev_functionname
== NULL
702 || strcmp (functionname
, prev_functionname
) != 0))
703 printf ("%s():\n", functionname
);
704 if (line
> 0 && line
!= prev_line
)
705 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
712 struct print_file_list
**pp
, *p
;
714 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
715 if (strcmp ((*pp
)->filename
, filename
) == 0)
721 if (p
!= print_files
)
725 /* We have reencountered a file name which we saw
726 earlier. This implies that either we are dumping out
727 code from an included file, or the same file was
728 linked in more than once. There are two common cases
729 of an included file: inline functions in a header
730 file, and a bison or flex skeleton file. In the
731 former case we want to just start printing (but we
732 back up a few lines to give context); in the latter
733 case we want to continue from where we left off. I
734 can't think of a good way to distinguish the cases,
735 so I used a heuristic based on the file name. */
736 if (strcmp (p
->filename
+ strlen (p
->filename
) - 2, ".h") != 0)
740 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
747 p
->f
= fopen (p
->filename
, "r");
751 skip_to_line (p
, l
, false);
753 if (print_files
->f
!= NULL
)
755 fclose (print_files
->f
);
756 print_files
->f
= NULL
;
762 skip_to_line (p
, line
, true);
764 p
->next
= print_files
;
772 f
= fopen (filename
, "r");
777 p
= ((struct print_file_list
*)
778 xmalloc (sizeof (struct print_file_list
)));
779 p
->filename
= xmalloc (strlen (filename
) + 1);
780 strcpy (p
->filename
, filename
);
784 if (print_files
!= NULL
&& print_files
->f
!= NULL
)
786 fclose (print_files
->f
);
787 print_files
->f
= NULL
;
789 p
->next
= print_files
;
792 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
795 skip_to_line (p
, l
, false);
797 skip_to_line (p
, line
, true);
802 if (functionname
!= NULL
803 && (prev_functionname
== NULL
804 || strcmp (functionname
, prev_functionname
) != 0))
806 if (prev_functionname
!= NULL
)
807 free (prev_functionname
);
808 prev_functionname
= xmalloc (strlen (functionname
) + 1);
809 strcpy (prev_functionname
, functionname
);
812 if (line
> 0 && line
!= prev_line
)
817 disassemble_data (abfd
)
821 disassembler_ftype disassemble_fn
= 0; /* New style */
822 struct disassemble_info disasm_info
;
823 struct objdump_disasm_info aux
;
825 boolean done_dot
= false;
828 prev_functionname
= NULL
;
831 /* We make a copy of syms to sort. We don't want to sort syms
832 because that will screw up the relocs. */
833 sorted_syms
= (asymbol
**) xmalloc (symcount
* sizeof (asymbol
*));
834 memcpy (sorted_syms
, syms
, symcount
* sizeof (asymbol
*));
836 sorted_symcount
= remove_useless_symbols (sorted_syms
, symcount
);
838 /* Sort the symbols into section and symbol order */
839 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
841 INIT_DISASSEMBLE_INFO(disasm_info
, stdout
, fprintf
);
842 disasm_info
.application_data
= (PTR
) &aux
;
844 disasm_info
.print_address_func
= objdump_print_address
;
846 disasm_info
.flags
|= DISASM_RAW_INSN_FLAG
;
848 if (machine
!= (char *) NULL
)
850 const bfd_arch_info_type
*info
= bfd_scan_arch (machine
);
853 fprintf (stderr
, "%s: Can't use supplied machine %s\n",
858 abfd
->arch_info
= info
;
861 disassemble_fn
= disassembler (abfd
);
864 fprintf (stderr
, "%s: Can't disassemble for architecture %s\n",
866 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
870 disasm_info
.arch
= bfd_get_arch (abfd
);
871 disasm_info
.mach
= bfd_get_mach (abfd
);
872 if (bfd_big_endian (abfd
))
873 disasm_info
.endian
= BFD_ENDIAN_BIG
;
874 else if (bfd_little_endian (abfd
))
875 disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
877 /* ??? Aborting here seems too drastic. We could default to big or little
879 disasm_info
.endian
= BFD_ENDIAN_UNKNOWN
;
881 for (section
= abfd
->sections
;
882 section
!= (asection
*) NULL
;
883 section
= section
->next
)
885 bfd_byte
*data
= NULL
;
886 bfd_size_type datasize
= 0;
887 arelent
**relbuf
= NULL
;
888 arelent
**relpp
= NULL
;
889 arelent
**relppend
= NULL
;
892 if ((section
->flags
& SEC_LOAD
) == 0
893 || (! disassemble_all
895 && (section
->flags
& SEC_CODE
) == 0))
897 if (only
!= (char *) NULL
&& strcmp (only
, section
->name
) != 0)
901 && (section
->flags
& SEC_RELOC
) != 0)
905 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
907 bfd_fatal (bfd_get_filename (abfd
));
913 relbuf
= (arelent
**) xmalloc (relsize
);
914 relcount
= bfd_canonicalize_reloc (abfd
, section
, relbuf
, syms
);
916 bfd_fatal (bfd_get_filename (abfd
));
918 /* Sort the relocs by address. */
919 qsort (relbuf
, relcount
, sizeof (arelent
*), compare_relocs
);
922 relppend
= relpp
+ relcount
;
926 printf ("Disassembly of section %s:\n", section
->name
);
928 datasize
= bfd_get_section_size_before_reloc (section
);
932 data
= (bfd_byte
*) xmalloc ((size_t) datasize
);
934 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
937 disasm_info
.buffer
= data
;
938 disasm_info
.buffer_vma
= section
->vma
;
939 disasm_info
.buffer_length
= datasize
;
940 if (start_address
== (bfd_vma
) -1
941 || start_address
< disasm_info
.buffer_vma
)
944 i
= start_address
- disasm_info
.buffer_vma
;
945 if (stop_address
== (bfd_vma
) -1)
949 if (stop_address
< disasm_info
.buffer_vma
)
952 stop
= stop_address
- disasm_info
.buffer_vma
;
953 if (stop
> disasm_info
.buffer_length
)
954 stop
= disasm_info
.buffer_length
;
959 boolean need_nl
= false;
961 if (data
[i
] == 0 && data
[i
+ 1] == 0 && data
[i
+ 2] == 0 &&
964 if (done_dot
== false)
974 if (with_line_numbers
|| with_source_code
)
975 show_line (abfd
, section
, i
);
976 aux
.require_sec
= true;
977 objdump_print_address (section
->vma
+ i
, &disasm_info
);
978 aux
.require_sec
= false;
981 bytes
= (*disassemble_fn
) (section
->vma
+ i
, &disasm_info
);
992 && (section
->flags
& SEC_RELOC
) != 0)
994 while (relpp
< relppend
995 && ((*relpp
)->address
>= (bfd_vma
) i
996 && (*relpp
)->address
< (bfd_vma
) i
+ bytes
))
999 const char *sym_name
;
1003 printf ("\t\tRELOC: ");
1005 printf_vma (section
->vma
+ q
->address
);
1007 printf (" %s ", q
->howto
->name
);
1009 if (q
->sym_ptr_ptr
!= NULL
1010 && *q
->sym_ptr_ptr
!= NULL
)
1012 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
1013 if (sym_name
== NULL
|| *sym_name
== '\0')
1017 sym_sec
= bfd_get_section (*q
->sym_ptr_ptr
);
1018 sym_name
= bfd_get_section_name (abfd
, sym_sec
);
1019 if (sym_name
== NULL
|| *sym_name
== '\0')
1020 sym_name
= "*unknown*";
1024 printf ("%s", sym_name
);
1029 printf_vma (q
->addend
);
1052 /* Define a table of stab values and print-strings. We wish the initializer
1053 could be a direct-mapped table, but instead we build one the first
1063 struct stab_print stab_print
[] = {
1064 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
1065 #include "aout/stab.def"
1066 #undef __define_stab
1070 void dump_section_stabs
PARAMS ((bfd
*abfd
, char *stabsect_name
,
1071 char *strsect_name
));
1073 /* Dump the stabs sections from an object file that has a section that
1074 uses Sun stabs encoding. It has to use some hooks into BFD because
1075 string table sections are not normally visible to BFD callers. */
1081 /* Allocate and initialize stab name array if first time. */
1082 if (stab_name
== NULL
)
1086 stab_name
= (char **) xmalloc (256 * sizeof(char *));
1087 /* Clear the array. */
1088 for (i
= 0; i
< 256; i
++)
1089 stab_name
[i
] = NULL
;
1090 /* Fill in the defined stabs. */
1091 for (i
= 0; *stab_print
[i
].string
; i
++)
1092 stab_name
[stab_print
[i
].value
] = stab_print
[i
].string
;
1095 dump_section_stabs (abfd
, ".stab", ".stabstr");
1096 dump_section_stabs (abfd
, ".stab.excl", ".stab.exclstr");
1097 dump_section_stabs (abfd
, ".stab.index", ".stab.indexstr");
1098 dump_section_stabs (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1101 static struct internal_nlist
*stabs
;
1102 static bfd_size_type stab_size
;
1104 static char *strtab
;
1105 static bfd_size_type stabstr_size
;
1107 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1108 and string table section STRSECT_NAME into `strtab'.
1109 If the section exists and was read, allocate the space and return true.
1110 Otherwise return false. */
1113 read_section_stabs (abfd
, stabsect_name
, strsect_name
)
1115 char *stabsect_name
;
1118 asection
*stabsect
, *stabstrsect
;
1120 stabsect
= bfd_get_section_by_name (abfd
, stabsect_name
);
1123 printf ("No %s section present\n\n", stabsect_name
);
1127 stabstrsect
= bfd_get_section_by_name (abfd
, strsect_name
);
1128 if (0 == stabstrsect
)
1130 fprintf (stderr
, "%s: %s has no %s section\n", program_name
,
1131 bfd_get_filename (abfd
), strsect_name
);
1135 stab_size
= bfd_section_size (abfd
, stabsect
);
1136 stabstr_size
= bfd_section_size (abfd
, stabstrsect
);
1138 stabs
= (struct internal_nlist
*) xmalloc (stab_size
);
1139 strtab
= (char *) xmalloc (stabstr_size
);
1141 if (! bfd_get_section_contents (abfd
, stabsect
, (PTR
) stabs
, 0, stab_size
))
1143 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1144 program_name
, stabsect_name
, bfd_get_filename (abfd
),
1145 bfd_errmsg (bfd_get_error ()));
1151 if (! bfd_get_section_contents (abfd
, stabstrsect
, (PTR
) strtab
, 0,
1154 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1155 program_name
, strsect_name
, bfd_get_filename (abfd
),
1156 bfd_errmsg (bfd_get_error ()));
1165 #define SWAP_SYMBOL(symp, abfd) \
1167 (symp)->n_strx = bfd_h_get_32(abfd, \
1168 (unsigned char *)&(symp)->n_strx); \
1169 (symp)->n_desc = bfd_h_get_16 (abfd, \
1170 (unsigned char *)&(symp)->n_desc); \
1171 (symp)->n_value = bfd_h_get_32 (abfd, \
1172 (unsigned char *)&(symp)->n_value); \
1175 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1176 using string table section STRSECT_NAME (in `strtab'). */
1179 print_section_stabs (abfd
, stabsect_name
, strsect_name
)
1181 char *stabsect_name
;
1185 unsigned file_string_table_offset
= 0, next_file_string_table_offset
= 0;
1186 struct internal_nlist
*stabp
= stabs
,
1187 *stabs_end
= (struct internal_nlist
*) (stab_size
+ (char *) stabs
);
1189 printf ("Contents of %s section:\n\n", stabsect_name
);
1190 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1192 /* Loop through all symbols and print them.
1194 We start the index at -1 because there is a dummy symbol on
1195 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1197 for (i
= -1; stabp
< stabs_end
; stabp
++, i
++)
1199 SWAP_SYMBOL (stabp
, abfd
);
1200 printf ("\n%-6d ", i
);
1201 /* Either print the stab name, or, if unnamed, print its number
1202 again (makes consistent formatting for tools like awk). */
1203 if (stab_name
[stabp
->n_type
])
1204 printf ("%-6s", stab_name
[stabp
->n_type
]);
1205 else if (stabp
->n_type
== N_UNDF
)
1208 printf ("%-6d", stabp
->n_type
);
1209 printf (" %-6d %-6d ", stabp
->n_other
, stabp
->n_desc
);
1210 printf_vma (stabp
->n_value
);
1211 printf (" %-6lu", stabp
->n_strx
);
1213 /* Symbols with type == 0 (N_UNDF) specify the length of the
1214 string table associated with this file. We use that info
1215 to know how to relocate the *next* file's string table indices. */
1217 if (stabp
->n_type
== N_UNDF
)
1219 file_string_table_offset
= next_file_string_table_offset
;
1220 next_file_string_table_offset
+= stabp
->n_value
;
1224 /* Using the (possibly updated) string table offset, print the
1225 string (if any) associated with this symbol. */
1227 if ((stabp
->n_strx
+ file_string_table_offset
) < stabstr_size
)
1228 printf (" %s", &strtab
[stabp
->n_strx
+ file_string_table_offset
]);
1237 dump_section_stabs (abfd
, stabsect_name
, strsect_name
)
1239 char *stabsect_name
;
1244 /* Check for section names for which stabsect_name is a prefix, to
1245 handle .stab0, etc. */
1246 for (s
= abfd
->sections
;
1250 if (strncmp (stabsect_name
, s
->name
, strlen (stabsect_name
)) == 0
1251 && strncmp (strsect_name
, s
->name
, strlen (strsect_name
)) != 0)
1253 if (read_section_stabs (abfd
, s
->name
, strsect_name
))
1255 print_section_stabs (abfd
, s
->name
, strsect_name
);
1264 dump_bfd_header (abfd
)
1269 printf ("architecture: %s, ",
1270 bfd_printable_arch_mach (bfd_get_arch (abfd
),
1271 bfd_get_mach (abfd
)));
1272 printf ("flags 0x%08x:\n", abfd
->flags
);
1274 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1275 PF (HAS_RELOC
, "HAS_RELOC");
1276 PF (EXEC_P
, "EXEC_P");
1277 PF (HAS_LINENO
, "HAS_LINENO");
1278 PF (HAS_DEBUG
, "HAS_DEBUG");
1279 PF (HAS_SYMS
, "HAS_SYMS");
1280 PF (HAS_LOCALS
, "HAS_LOCALS");
1281 PF (DYNAMIC
, "DYNAMIC");
1282 PF (WP_TEXT
, "WP_TEXT");
1283 PF (D_PAGED
, "D_PAGED");
1284 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
1285 printf ("\nstart address 0x");
1286 printf_vma (abfd
->start_address
);
1290 dump_bfd_private_header (abfd
)
1293 bfd_print_private_bfd_data (abfd
, stdout
);
1301 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
1303 bfd_nonfatal (bfd_get_filename (abfd
));
1304 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1306 list_matching_formats (matching
);
1312 printf ("\n%s: file format %s\n", bfd_get_filename (abfd
),
1315 print_arelt_descr (stdout
, abfd
, true);
1316 if (dump_file_header
)
1317 dump_bfd_header (abfd
);
1318 if (dump_private_headers
)
1319 dump_bfd_private_header (abfd
);
1321 if (dump_section_headers
)
1322 dump_headers (abfd
);
1323 if (dump_symtab
|| dump_reloc_info
|| disassemble
|| dump_debugging
)
1325 syms
= slurp_symtab (abfd
);
1327 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
)
1329 dynsyms
= slurp_dynamic_symtab (abfd
);
1332 dump_symbols (abfd
, false);
1333 if (dump_dynamic_symtab
)
1334 dump_symbols (abfd
, true);
1335 if (dump_stab_section_info
)
1337 if (dump_reloc_info
&& ! disassemble
)
1339 if (dump_dynamic_reloc_info
)
1340 dump_dynamic_relocs (abfd
);
1341 if (dump_section_contents
)
1344 disassemble_data (abfd
);
1349 dhandle
= read_debugging_info (abfd
, syms
, symcount
);
1350 if (dhandle
!= NULL
)
1352 if (! print_debugging_info (stdout
, dhandle
))
1353 fprintf (stderr
, "%s: printing debugging information failed\n",
1354 bfd_get_filename (abfd
));
1370 display_file (filename
, target
)
1374 bfd
*file
, *arfile
= (bfd
*) NULL
;
1376 file
= bfd_openr (filename
, target
);
1379 bfd_nonfatal (filename
);
1383 if (bfd_check_format (file
, bfd_archive
) == true)
1385 bfd
*last_arfile
= NULL
;
1387 printf ("In archive %s:\n", bfd_get_filename (file
));
1390 bfd_set_error (bfd_error_no_error
);
1392 arfile
= bfd_openr_next_archived_file (file
, arfile
);
1395 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1397 bfd_nonfatal (bfd_get_filename (file
));
1402 display_bfd (arfile
);
1404 if (last_arfile
!= NULL
)
1405 bfd_close (last_arfile
);
1406 last_arfile
= arfile
;
1409 if (last_arfile
!= NULL
)
1410 bfd_close (last_arfile
);
1418 /* Actually display the various requested regions */
1426 bfd_size_type datasize
= 0;
1428 bfd_size_type start
, stop
;
1430 for (section
= abfd
->sections
; section
!= NULL
; section
=
1435 if (only
== (char *) NULL
||
1436 strcmp (only
, section
->name
) == 0)
1438 if (section
->flags
& SEC_HAS_CONTENTS
)
1440 printf ("Contents of section %s:\n", section
->name
);
1442 if (bfd_section_size (abfd
, section
) == 0)
1444 data
= (bfd_byte
*) xmalloc ((size_t) bfd_section_size (abfd
, section
));
1445 datasize
= bfd_section_size (abfd
, section
);
1448 bfd_get_section_contents (abfd
, section
, (PTR
) data
, 0, bfd_section_size (abfd
, section
));
1450 if (start_address
== (bfd_vma
) -1
1451 || start_address
< section
->vma
)
1454 start
= start_address
- section
->vma
;
1455 if (stop_address
== (bfd_vma
) -1)
1456 stop
= bfd_section_size (abfd
, section
);
1459 if (stop_address
< section
->vma
)
1462 stop
= stop_address
- section
->vma
;
1463 if (stop
> bfd_section_size (abfd
, section
))
1464 stop
= bfd_section_size (abfd
, section
);
1466 for (i
= start
; i
< stop
; i
+= onaline
)
1470 printf (" %04lx ", (unsigned long int) (i
+ section
->vma
));
1471 for (j
= i
; j
< i
+ onaline
; j
++)
1474 printf ("%02x", (unsigned) (data
[j
]));
1482 for (j
= i
; j
< i
+ onaline
; j
++)
1487 printf ("%c", isprint (data
[j
]) ? data
[j
] : '.');
1497 /* Should perhaps share code and display with nm? */
1499 dump_symbols (abfd
, dynamic
)
1513 printf ("DYNAMIC SYMBOL TABLE:\n");
1521 printf ("SYMBOL TABLE:\n");
1524 for (count
= 0; count
< max
; count
++)
1528 bfd
*cur_bfd
= bfd_asymbol_bfd(*current
);
1531 bfd_print_symbol (cur_bfd
,
1533 *current
, bfd_print_symbol_all
);
1551 for (a
= abfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1555 if (bfd_is_abs_section (a
))
1557 if (bfd_is_und_section (a
))
1559 if (bfd_is_com_section (a
))
1564 if (strcmp (only
, a
->name
))
1567 else if ((a
->flags
& SEC_RELOC
) == 0)
1570 relsize
= bfd_get_reloc_upper_bound (abfd
, a
);
1572 bfd_fatal (bfd_get_filename (abfd
));
1574 printf ("RELOCATION RECORDS FOR [%s]:", a
->name
);
1578 printf (" (none)\n\n");
1582 relpp
= (arelent
**) xmalloc (relsize
);
1583 relcount
= bfd_canonicalize_reloc (abfd
, a
, relpp
, syms
);
1585 bfd_fatal (bfd_get_filename (abfd
));
1586 else if (relcount
== 0)
1588 printf (" (none)\n\n");
1593 dump_reloc_set (abfd
, relpp
, relcount
);
1602 dump_dynamic_relocs (abfd
)
1609 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
1611 bfd_fatal (bfd_get_filename (abfd
));
1613 printf ("DYNAMIC RELOCATION RECORDS");
1617 printf (" (none)\n\n");
1621 relpp
= (arelent
**) xmalloc (relsize
);
1622 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
1624 bfd_fatal (bfd_get_filename (abfd
));
1625 else if (relcount
== 0)
1627 printf (" (none)\n\n");
1632 dump_reloc_set (abfd
, relpp
, relcount
);
1640 dump_reloc_set (abfd
, relpp
, relcount
)
1647 /* Get column headers lined up reasonably. */
1653 sprintf_vma (buf
, (bfd_vma
) -1);
1654 width
= strlen (buf
) - 7;
1656 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
1659 for (p
= relpp
; relcount
&& *p
!= (arelent
*) NULL
; p
++, relcount
--)
1662 CONST
char *sym_name
;
1663 CONST
char *section_name
;
1665 if (start_address
!= (bfd_vma
) -1
1666 && q
->address
< start_address
)
1668 if (stop_address
!= (bfd_vma
) -1
1669 && q
->address
> stop_address
)
1672 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
1674 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
1675 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
1680 section_name
= NULL
;
1684 printf_vma (q
->address
);
1685 printf (" %-16s %s",
1691 if (section_name
== (CONST
char *) NULL
)
1692 section_name
= "*unknown*";
1693 printf_vma (q
->address
);
1694 printf (" %-16s [%s]",
1701 printf_vma (q
->addend
);
1707 /* The length of the longest architecture name + 1. */
1708 #define LONGEST_ARCH sizeof("rs6000:6000")
1715 endian_string (endian
)
1716 enum bfd_endian endian
;
1718 if (endian
== BFD_ENDIAN_BIG
)
1719 return "big endian";
1720 else if (endian
== BFD_ENDIAN_LITTLE
)
1721 return "little endian";
1723 return "endianness unknown";
1726 /* List the targets that BFD is configured to support, each followed
1727 by its endianness and the architectures it supports. */
1730 display_target_list ()
1732 extern char *tmpnam ();
1733 extern bfd_target
*bfd_target_vector
[];
1734 char tmparg
[L_tmpnam
];
1738 dummy_name
= tmpnam (tmparg
);
1739 for (t
= 0; bfd_target_vector
[t
]; t
++)
1741 bfd_target
*p
= bfd_target_vector
[t
];
1742 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1745 printf ("%s\n (header %s, data %s)\n", p
->name
,
1746 endian_string (p
->header_byteorder
),
1747 endian_string (p
->byteorder
));
1751 bfd_nonfatal (dummy_name
);
1755 if (! bfd_set_format (abfd
, bfd_object
))
1757 if (bfd_get_error () != bfd_error_invalid_operation
)
1758 bfd_nonfatal (p
->name
);
1762 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1763 if (bfd_set_arch_mach (abfd
, (enum bfd_architecture
) a
, 0))
1765 bfd_printable_arch_mach ((enum bfd_architecture
) a
, 0));
1767 unlink (dummy_name
);
1770 /* Print a table showing which architectures are supported for entries
1771 FIRST through LAST-1 of bfd_target_vector (targets across,
1772 architectures down). */
1775 display_info_table (first
, last
)
1779 extern bfd_target
*bfd_target_vector
[];
1780 extern char *tmpnam ();
1781 char tmparg
[L_tmpnam
];
1785 /* Print heading of target names. */
1786 printf ("\n%*s", (int) LONGEST_ARCH
, " ");
1787 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1788 printf ("%s ", bfd_target_vector
[t
]->name
);
1791 dummy_name
= tmpnam (tmparg
);
1792 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1793 if (strcmp (bfd_printable_arch_mach (a
, 0), "UNKNOWN!") != 0)
1795 printf ("%*s ", (int) LONGEST_ARCH
- 1,
1796 bfd_printable_arch_mach (a
, 0));
1797 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1799 bfd_target
*p
= bfd_target_vector
[t
];
1801 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1805 bfd_nonfatal (p
->name
);
1811 if (! bfd_set_format (abfd
, bfd_object
))
1813 if (bfd_get_error () != bfd_error_invalid_operation
)
1814 bfd_nonfatal (p
->name
);
1821 if (! bfd_set_arch_mach (abfd
, a
, 0))
1826 printf ("%s ", p
->name
);
1829 int l
= strlen (p
->name
);
1837 unlink (dummy_name
);
1840 /* Print tables of all the target-architecture combinations that
1841 BFD has been configured to support. */
1844 display_target_tables ()
1847 extern bfd_target
*bfd_target_vector
[];
1849 extern char *getenv ();
1852 colum
= getenv ("COLUMNS");
1854 columns
= atoi (colum
);
1859 while (bfd_target_vector
[t
] != NULL
)
1863 wid
= LONGEST_ARCH
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1865 while (wid
< columns
&& bfd_target_vector
[t
] != NULL
)
1869 newwid
= wid
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1870 if (newwid
>= columns
)
1875 display_info_table (oldt
, t
);
1882 printf ("BFD header file version %s\n", BFD_VERSION
);
1883 display_target_list ();
1884 display_target_tables ();
1893 char *target
= default_target
;
1894 boolean seenflag
= false;
1896 program_name
= *argv
;
1897 xmalloc_set_program_name (program_name
);
1899 START_PROGRESS (program_name
, 0);
1903 while ((c
= getopt_long (argc
, argv
, "pib:m:VdDlfahrRtTxsSj:w", long_options
,
1907 if (c
!= 'l' && c
!= OPTION_START_ADDRESS
&& c
!= OPTION_STOP_ADDRESS
)
1912 break; /* we've been given a long option */
1920 with_line_numbers
= 1;
1926 dump_file_header
= true;
1929 formats_info
= true;
1932 dump_private_headers
= 1;
1935 dump_private_headers
= 1;
1937 dump_reloc_info
= 1;
1938 dump_file_header
= true;
1940 dump_section_headers
= 1;
1946 dump_dynamic_symtab
= 1;
1952 disassemble
= disassemble_all
= true;
1956 with_source_code
= true;
1959 dump_section_contents
= 1;
1962 dump_reloc_info
= 1;
1965 dump_dynamic_reloc_info
= 1;
1971 dump_section_headers
= 1;
1981 case OPTION_START_ADDRESS
:
1982 start_address
= parse_vma (optarg
, "--start-address");
1984 case OPTION_STOP_ADDRESS
:
1985 stop_address
= parse_vma (optarg
, "--stop-address");
1994 printf ("GNU %s version %s\n", program_name
, program_version
);
1998 if (seenflag
== false)
2008 display_file ("a.out", target
);
2010 for (; optind
< argc
;)
2011 display_file (argv
[optind
++], target
);
2014 END_PROGRESS (program_name
);