1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 1991, 1992, 1993, 1994 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
28 #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 #ifndef FPRINTF_ALREADY_DECLARED
35 extern int fprintf
PARAMS ((FILE *, CONST
char *, ...));
38 char *default_target
= NULL
; /* default at runtime */
40 extern char *program_version
;
42 int show_version
= 0; /* show the version number */
43 int dump_section_contents
; /* -s */
44 int dump_section_headers
; /* -h */
45 boolean dump_file_header
; /* -f */
46 int dump_symtab
; /* -t */
47 int dump_dynamic_symtab
; /* -T */
48 int dump_reloc_info
; /* -r */
49 int dump_dynamic_reloc_info
; /* -R */
50 int dump_ar_hdrs
; /* -a */
51 int with_line_numbers
; /* -l */
52 boolean with_source_code
; /* -S */
53 int dump_stab_section_info
; /* --stabs */
54 boolean disassemble
; /* -d */
55 boolean disassemble_all
; /* -D */
56 boolean formats_info
; /* -i */
57 char *only
; /* -j secname */
59 /* Extra info to pass to the disassembler address printing function. */
60 struct objdump_disasm_info
{
66 /* Architecture to disassemble for, or default if NULL. */
67 char *machine
= (char *) NULL
;
69 /* The symbol table. */
72 /* Number of symbols in `syms'. */
75 /* The sorted symbol table. */
76 asymbol
**sorted_syms
;
78 /* Number of symbols in `sorted_syms'. */
79 long sorted_symcount
= 0;
81 /* The dynamic symbol table. */
84 /* Number of symbols in `dynsyms'. */
87 /* Forward declarations. */
90 display_file
PARAMS ((char *filename
, char *target
));
93 dump_data
PARAMS ((bfd
*abfd
));
96 dump_relocs
PARAMS ((bfd
*abfd
));
99 dump_dynamic_relocs
PARAMS ((bfd
* abfd
));
102 dump_reloc_set
PARAMS ((bfd
*, arelent
**, long));
105 dump_symbols
PARAMS ((bfd
*abfd
, boolean dynamic
));
108 display_bfd
PARAMS ((bfd
*abfd
));
111 objdump_print_address
PARAMS ((bfd_vma
, struct disassemble_info
*));
114 show_line
PARAMS ((bfd
*, asection
*, bfd_vma
));
117 usage (stream
, status
)
122 Usage: %s [-ahifdDrRtTxsSl] [-b bfdname] [-m machine] [-j section-name]\n\
123 [--archive-headers] [--target=bfdname] [--disassemble]\n\
124 [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
125 [--info] [--section=section-name] [--line-numbers] [--source]\n\
126 [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
127 [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
128 [--version] [--help] objfile...\n\
129 at least one option besides -l (--line-numbers) must be given\n",
131 list_supported_targets (program_name
, stream
);
135 static struct option long_options
[]=
137 {"all-headers", no_argument
, NULL
, 'x'},
138 {"architecture", required_argument
, NULL
, 'm'},
139 {"archive-headers", no_argument
, NULL
, 'a'},
140 {"disassemble", no_argument
, NULL
, 'd'},
141 {"disassemble-all", no_argument
, NULL
, 'D'},
142 {"dynamic-reloc", no_argument
, NULL
, 'R'},
143 {"dynamic-syms", no_argument
, NULL
, 'T'},
144 {"file-headers", no_argument
, NULL
, 'f'},
145 {"full-contents", no_argument
, NULL
, 's'},
146 {"headers", no_argument
, NULL
, 'h'},
147 {"help", no_argument
, NULL
, 'H'},
148 {"info", no_argument
, NULL
, 'i'},
149 {"line-numbers", no_argument
, NULL
, 'l'},
150 {"reloc", no_argument
, NULL
, 'r'},
151 {"section", required_argument
, NULL
, 'j'},
152 {"section-headers", no_argument
, NULL
, 'h'},
153 {"source", no_argument
, NULL
, 'S'},
154 {"stabs", no_argument
, &dump_stab_section_info
, 1},
155 {"syms", no_argument
, NULL
, 't'},
156 {"target", required_argument
, NULL
, 'b'},
157 {"version", no_argument
, &show_version
, 1},
158 {0, no_argument
, 0, 0}
162 dump_section_header (abfd
, section
, ignored
)
170 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
173 printf ("SECTION %d [%s]\t: size %08x",
176 (unsigned) bfd_get_section_size_before_reloc (section
));
178 printf_vma (section
->vma
);
179 printf (" align 2**%u\n ",
180 section
->alignment_power
);
181 PF (SEC_ALLOC
, "ALLOC");
182 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
183 PF (SEC_CONSTRUCTOR_TEXT
, "CONSTRUCTOR TEXT");
184 PF (SEC_CONSTRUCTOR_DATA
, "CONSTRUCTOR DATA");
185 PF (SEC_CONSTRUCTOR_BSS
, "CONSTRUCTOR BSS");
186 PF (SEC_LOAD
, "LOAD");
187 PF (SEC_RELOC
, "RELOC");
189 PF (SEC_BALIGN
, "BALIGN");
191 PF (SEC_READONLY
, "READONLY");
192 PF (SEC_CODE
, "CODE");
193 PF (SEC_DATA
, "DATA");
195 PF (SEC_DEBUGGING
, "DEBUGGING");
196 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
205 bfd_map_over_sections (abfd
, dump_section_header
, (PTR
) NULL
);
212 asymbol
**sy
= (asymbol
**) NULL
;
215 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
217 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd
));
221 storage
= bfd_get_symtab_upper_bound (abfd
);
223 bfd_fatal (bfd_get_filename (abfd
));
227 sy
= (asymbol
**) xmalloc (storage
);
229 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
231 bfd_fatal (bfd_get_filename (abfd
));
233 fprintf (stderr
, "%s: %s: No symbols\n",
234 program_name
, bfd_get_filename (abfd
));
238 /* Read in the dynamic symbols. */
241 slurp_dynamic_symtab (abfd
)
244 asymbol
**sy
= (asymbol
**) NULL
;
247 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
250 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
252 fprintf (stderr
, "%s: %s: not a dynamic object\n",
253 program_name
, bfd_get_filename (abfd
));
257 bfd_fatal (bfd_get_filename (abfd
));
262 sy
= (asymbol
**) xmalloc (storage
);
264 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
266 bfd_fatal (bfd_get_filename (abfd
));
267 if (dynsymcount
== 0)
268 fprintf (stderr
, "%s: %s: No dynamic symbols\n",
269 program_name
, bfd_get_filename (abfd
));
273 /* Filter out (in place) symbols that are useless for disassembly.
274 COUNT is the number of elements in SYMBOLS.
275 Return the number of useful symbols. */
278 remove_useless_symbols (symbols
, count
)
282 register asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
286 asymbol
*sym
= *in_ptr
++;
288 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
290 if (sym
->flags
& (BSF_DEBUGGING
))
292 if (bfd_is_und_section (sym
->section
)
293 || bfd_is_com_section (sym
->section
))
298 return out_ptr
- symbols
;
301 /* Sort symbols into value order. */
304 compare_symbols (ap
, bp
)
308 const asymbol
*a
= *(const asymbol
**)ap
;
309 const asymbol
*b
= *(const asymbol
**)bp
;
311 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
313 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
316 if (a
->section
> b
->section
)
318 else if (a
->section
< b
->section
)
323 /* Sort relocs into address order. */
326 compare_relocs (ap
, bp
)
330 const arelent
*a
= *(const arelent
**)ap
;
331 const arelent
*b
= *(const arelent
**)bp
;
333 if (a
->address
> b
->address
)
335 else if (a
->address
< b
->address
)
338 return compare_symbols ((const PTR
) a
->sym_ptr_ptr
,
339 (const PTR
) b
->sym_ptr_ptr
);
342 /* Print VMA symbolically to INFO if possible. */
345 objdump_print_address (vma
, info
)
347 struct disassemble_info
*info
;
349 /* @@ For relocateable files, should filter out symbols belonging to
350 the wrong section. Unfortunately, not enough information is supplied
351 to this routine to determine the correct section in all cases. */
352 /* @@ Would it speed things up to cache the last two symbols returned,
353 and maybe their address ranges? For many processors, only one memory
354 operand can be present at a time, so the 2-entry cache wouldn't be
355 constantly churned by code doing heavy memory accesses. */
357 /* Indices in `sorted_syms'. */
359 long max
= sorted_symcount
;
362 bfd_signed_vma vardiff
;
364 fprintf_vma (info
->stream
, vma
);
366 if (sorted_symcount
< 1)
369 /* Perform a binary search looking for the closest symbol to the
370 required value. We are searching the range (min, max]. */
371 while (min
+ 1 < max
)
375 thisplace
= (max
+ min
) / 2;
376 sym
= sorted_syms
[thisplace
];
378 vardiff
= bfd_asymbol_value (sym
) - vma
;
382 else if (vardiff
< 0)
391 /* The symbol we want is now in min, the low end of the range we
396 /* If this symbol isn't global, search for one with the same value
398 bfd_vma val
= bfd_asymbol_value (sorted_syms
[thisplace
]);
400 if (sorted_syms
[thisplace
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
401 for (i
= thisplace
- 1; i
>= 0; i
--)
403 if (bfd_asymbol_value (sorted_syms
[i
]) == val
404 && (!(sorted_syms
[i
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
405 || ((sorted_syms
[thisplace
]->flags
& BSF_DEBUGGING
)
406 && !(sorted_syms
[i
]->flags
& BSF_DEBUGGING
))))
412 if (sorted_syms
[thisplace
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
413 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
415 if (bfd_asymbol_value (sorted_syms
[i
]) == val
416 && (!(sorted_syms
[i
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
417 || ((sorted_syms
[thisplace
]->flags
& BSF_DEBUGGING
)
418 && !(sorted_syms
[i
]->flags
& BSF_DEBUGGING
))))
426 /* If the file is relocateable, and the symbol could be from this
427 section, prefer a symbol from this section over symbols from
428 others, even if the other symbol's value might be closer.
430 Note that this may be wrong for some symbol references if the
431 sections have overlapping memory ranges, but in that case there's
432 no way to tell what's desired without looking at the relocation
434 struct objdump_disasm_info
*aux
;
437 aux
= (struct objdump_disasm_info
*) info
->application_data
;
438 if (sorted_syms
[thisplace
]->section
!= aux
->sec
440 || ((aux
->abfd
->flags
& HAS_RELOC
) != 0
441 && vma
>= bfd_get_section_vma (aux
->abfd
, aux
->sec
)
442 && vma
< (bfd_get_section_vma (aux
->abfd
, aux
->sec
)
443 + bfd_get_section_size_before_reloc (aux
->sec
)))))
445 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
447 if (bfd_asymbol_value (sorted_syms
[i
])
448 != bfd_asymbol_value (sorted_syms
[thisplace
]))
454 if (sorted_syms
[i
]->section
== aux
->sec
)
461 if (sorted_syms
[thisplace
]->section
!= aux
->sec
)
463 /* We didn't find a good symbol with a smaller value.
464 Look for one with a larger value. */
465 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
467 if (sorted_syms
[i
]->section
== aux
->sec
)
477 fprintf (info
->stream
, " <%s", sorted_syms
[thisplace
]->name
);
478 if (bfd_asymbol_value (sorted_syms
[thisplace
]) > vma
)
480 char buf
[30], *p
= buf
;
481 sprintf_vma (buf
, bfd_asymbol_value (sorted_syms
[thisplace
]) - vma
);
484 fprintf (info
->stream
, "-%s", p
);
486 else if (vma
> bfd_asymbol_value (sorted_syms
[thisplace
]))
488 char buf
[30], *p
= buf
;
489 sprintf_vma (buf
, vma
- bfd_asymbol_value (sorted_syms
[thisplace
]));
492 fprintf (info
->stream
, "+%s", p
);
494 fprintf (info
->stream
, ">");
497 /* Hold the last function name and the last line number we displayed
500 static char *prev_functionname
;
501 static unsigned int prev_line
;
503 /* We keep a list of all files that we have seen when doing a
504 dissassembly with source, so that we know how much of the file to
505 display. This can be important for inlined functions. */
507 struct print_file_list
509 struct print_file_list
*next
;
515 static struct print_file_list
*print_files
;
517 /* The number of preceding context lines to show when we start
518 displaying a file for the first time. */
520 #define SHOW_PRECEDING_CONTEXT_LINES (5)
522 /* Skip ahead to a given line in a file, optionally printing each
526 skip_to_line
PARAMS ((struct print_file_list
*, unsigned int, boolean
));
529 skip_to_line (p
, line
, show
)
530 struct print_file_list
*p
;
534 while (p
->line
< line
)
538 if (fgets (buf
, sizeof buf
, p
->f
) == NULL
)
548 if (strchr (buf
, '\n') != NULL
)
553 /* Show the line number, or the source line, in a dissassembly
557 show_line (abfd
, section
, off
)
562 CONST
char *filename
;
563 CONST
char *functionname
;
566 if (! with_line_numbers
&& ! with_source_code
)
569 if (! bfd_find_nearest_line (abfd
, section
, syms
, off
, &filename
,
570 &functionname
, &line
))
573 if (filename
!= NULL
&& *filename
== '\0')
575 if (functionname
!= NULL
&& *functionname
== '\0')
578 if (with_line_numbers
)
580 if (functionname
!= NULL
581 && (prev_functionname
== NULL
582 || strcmp (functionname
, prev_functionname
) != 0))
583 printf ("%s():\n", functionname
);
584 if (line
> 0 && line
!= prev_line
)
585 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
592 struct print_file_list
**pp
, *p
;
594 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
595 if (strcmp ((*pp
)->filename
, filename
) == 0)
601 if (p
!= print_files
)
605 /* We have reencountered a file name which we saw
606 earlier. This implies that either we are dumping out
607 code from an included file, or the same file was
608 linked in more than once. There are two common cases
609 of an included file: inline functions in a header
610 file, and a bison or flex skeleton file. In the
611 former case we want to just start printing (but we
612 back up a few lines to give context); in the latter
613 case we want to continue from where we left off. I
614 can't think of a good way to distinguish the cases,
615 so I used a heuristic based on the file name. */
616 if (strcmp (p
->filename
+ strlen (p
->filename
) - 2, ".h") != 0)
620 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
627 p
->f
= fopen (p
->filename
, "r");
631 skip_to_line (p
, l
, false);
633 if (print_files
->f
!= NULL
)
635 fclose (print_files
->f
);
636 print_files
->f
= NULL
;
642 skip_to_line (p
, line
, true);
644 p
->next
= print_files
;
652 f
= fopen (filename
, "r");
657 p
= ((struct print_file_list
*)
658 xmalloc (sizeof (struct print_file_list
)));
659 p
->filename
= xmalloc (strlen (filename
) + 1);
660 strcpy (p
->filename
, filename
);
664 if (print_files
!= NULL
&& print_files
->f
!= NULL
)
666 fclose (print_files
->f
);
667 print_files
->f
= NULL
;
669 p
->next
= print_files
;
672 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
675 skip_to_line (p
, l
, false);
677 skip_to_line (p
, line
, true);
682 if (functionname
!= NULL
683 && (prev_functionname
== NULL
684 || strcmp (functionname
, prev_functionname
) != 0))
686 if (prev_functionname
!= NULL
)
687 free (prev_functionname
);
688 prev_functionname
= xmalloc (strlen (functionname
) + 1);
689 strcpy (prev_functionname
, functionname
);
692 if (line
> 0 && line
!= prev_line
)
697 disassemble_data (abfd
)
701 unsigned int (*print
) () = 0; /* Old style */
702 disassembler_ftype disassemble_fn
= 0; /* New style */
703 struct disassemble_info disasm_info
;
704 struct objdump_disasm_info aux
;
706 boolean done_dot
= false;
709 prev_functionname
= NULL
;
712 /* We make a copy of syms to sort. We don't want to sort syms
713 because that will screw up the relocs. */
714 sorted_syms
= (asymbol
**) xmalloc (symcount
* sizeof (asymbol
*));
715 memcpy (sorted_syms
, syms
, symcount
* sizeof (asymbol
*));
717 sorted_symcount
= remove_useless_symbols (sorted_syms
, symcount
);
719 /* Sort the symbols into section and symbol order */
720 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
722 INIT_DISASSEMBLE_INFO(disasm_info
, stdout
);
723 disasm_info
.application_data
= (PTR
) &aux
;
725 disasm_info
.print_address_func
= objdump_print_address
;
727 if (machine
!= (char *) NULL
)
729 bfd_arch_info_type
*info
= bfd_scan_arch (machine
);
732 fprintf (stderr
, "%s: Can't use supplied machine %s\n",
737 abfd
->arch_info
= info
;
740 /* See if we can disassemble using bfd. */
742 if (abfd
->arch_info
->disassemble
)
744 print
= abfd
->arch_info
->disassemble
;
748 disassemble_fn
= disassembler (abfd
);
751 fprintf (stderr
, "%s: Can't disassemble for architecture %s\n",
753 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
758 for (section
= abfd
->sections
;
759 section
!= (asection
*) NULL
;
760 section
= section
->next
)
762 bfd_byte
*data
= NULL
;
763 bfd_size_type datasize
= 0;
764 arelent
**relbuf
= NULL
;
765 arelent
**relpp
= NULL
;
766 arelent
**relppend
= NULL
;
768 if ((section
->flags
& SEC_LOAD
) == 0
769 || (! disassemble_all
771 && (section
->flags
& SEC_CODE
) == 0))
773 if (only
!= (char *) NULL
&& strcmp (only
, section
->name
) != 0)
777 && (section
->flags
& SEC_RELOC
) != 0)
781 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
783 bfd_fatal (bfd_get_filename (abfd
));
789 relbuf
= (arelent
**) xmalloc (relsize
);
790 relcount
= bfd_canonicalize_reloc (abfd
, section
, relbuf
, syms
);
792 bfd_fatal (bfd_get_filename (abfd
));
794 /* Sort the relocs by address. */
795 qsort (relbuf
, relcount
, sizeof (arelent
*), compare_relocs
);
798 relppend
= relpp
+ relcount
;
802 printf ("Disassembly of section %s:\n", section
->name
);
804 datasize
= bfd_get_section_size_before_reloc (section
);
808 data
= (bfd_byte
*) xmalloc ((size_t) datasize
);
810 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
813 disasm_info
.buffer
= data
;
814 disasm_info
.buffer_vma
= section
->vma
;
815 disasm_info
.buffer_length
= datasize
;
817 while (i
< disasm_info
.buffer_length
)
821 if (data
[i
] == 0 && data
[i
+ 1] == 0 && data
[i
+ 2] == 0 &&
824 if (done_dot
== false)
834 if (with_line_numbers
|| with_source_code
)
835 show_line (abfd
, section
, i
);
836 aux
.require_sec
= true;
837 objdump_print_address (section
->vma
+ i
, &disasm_info
);
838 aux
.require_sec
= false;
844 bytes
= (*disassemble_fn
) (section
->vma
+ i
, &disasm_info
);
851 bytes
= print (section
->vma
+ i
, data
+ i
, stdout
);
857 && (section
->flags
& SEC_RELOC
) != 0)
859 while (relpp
< relppend
860 && ((*relpp
)->address
>= i
861 && (*relpp
)->address
< i
+ bytes
))
864 const char *sym_name
;
868 printf ("\t\tRELOC: ");
870 printf_vma (section
->vma
+ q
->address
);
872 printf (" %s ", q
->howto
->name
);
874 if (q
->sym_ptr_ptr
!= NULL
875 && *q
->sym_ptr_ptr
!= NULL
)
877 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
878 if (sym_name
== NULL
|| *sym_name
== '\0')
882 sym_sec
= bfd_get_section (*q
->sym_ptr_ptr
);
883 sym_name
= bfd_get_section_name (abfd
, sym_sec
);
884 if (sym_name
== NULL
|| *sym_name
== '\0')
885 sym_name
= "*unknown*";
889 printf ("%s", sym_name
);
894 printf_vma (q
->addend
);
913 /* Define a table of stab values and print-strings. We wish the initializer
914 could be a direct-mapped table, but instead we build one the first
924 struct stab_print stab_print
[] = {
925 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
926 #include "aout/stab.def"
931 void dump_section_stabs
PARAMS ((bfd
*abfd
, char *stabsect_name
,
932 char *strsect_name
));
934 /* Dump the stabs sections from an object file that has a section that
935 uses Sun stabs encoding. It has to use some hooks into BFD because
936 string table sections are not normally visible to BFD callers. */
942 /* Allocate and initialize stab name array if first time. */
943 if (stab_name
== NULL
)
947 stab_name
= (char **) xmalloc (256 * sizeof(char *));
948 /* Clear the array. */
949 for (i
= 0; i
< 256; i
++)
951 /* Fill in the defined stabs. */
952 for (i
= 0; *stab_print
[i
].string
; i
++)
953 stab_name
[stab_print
[i
].value
] = stab_print
[i
].string
;
956 dump_section_stabs (abfd
, ".stab", ".stabstr");
957 dump_section_stabs (abfd
, ".stab.excl", ".stab.exclstr");
958 dump_section_stabs (abfd
, ".stab.index", ".stab.indexstr");
959 dump_section_stabs (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
962 static struct internal_nlist
*stabs
;
963 static bfd_size_type stab_size
;
966 static bfd_size_type stabstr_size
;
968 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
969 and string table section STRSECT_NAME into `strtab'.
970 If the section exists and was read, allocate the space and return true.
971 Otherwise return false. */
974 read_section_stabs (abfd
, stabsect_name
, strsect_name
)
979 asection
*stabsect
, *stabstrsect
;
981 stabsect
= bfd_get_section_by_name (abfd
, stabsect_name
);
984 printf ("No %s section present\n\n", stabsect_name
);
988 stabstrsect
= bfd_get_section_by_name (abfd
, strsect_name
);
989 if (0 == stabstrsect
)
991 fprintf (stderr
, "%s: %s has no %s section\n", program_name
,
992 bfd_get_filename (abfd
), strsect_name
);
996 stab_size
= bfd_section_size (abfd
, stabsect
);
997 stabstr_size
= bfd_section_size (abfd
, stabstrsect
);
999 stabs
= (struct internal_nlist
*) xmalloc (stab_size
);
1000 strtab
= (char *) xmalloc (stabstr_size
);
1002 if (! bfd_get_section_contents (abfd
, stabsect
, (PTR
) stabs
, 0, stab_size
))
1004 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1005 program_name
, stabsect_name
, bfd_get_filename (abfd
),
1006 bfd_errmsg (bfd_get_error ()));
1012 if (! bfd_get_section_contents (abfd
, stabstrsect
, (PTR
) strtab
, 0,
1015 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1016 program_name
, strsect_name
, bfd_get_filename (abfd
),
1017 bfd_errmsg (bfd_get_error ()));
1026 #define SWAP_SYMBOL(symp, abfd) \
1028 (symp)->n_strx = bfd_h_get_32(abfd, \
1029 (unsigned char *)&(symp)->n_strx); \
1030 (symp)->n_desc = bfd_h_get_16 (abfd, \
1031 (unsigned char *)&(symp)->n_desc); \
1032 (symp)->n_value = bfd_h_get_32 (abfd, \
1033 (unsigned char *)&(symp)->n_value); \
1036 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1037 using string table section STRSECT_NAME (in `strtab'). */
1040 print_section_stabs (abfd
, stabsect_name
, strsect_name
)
1042 char *stabsect_name
;
1046 unsigned file_string_table_offset
= 0, next_file_string_table_offset
= 0;
1047 struct internal_nlist
*stabp
= stabs
,
1048 *stabs_end
= (struct internal_nlist
*) (stab_size
+ (char *) stabs
);
1050 printf ("Contents of %s section:\n\n", stabsect_name
);
1051 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1053 /* Loop through all symbols and print them.
1055 We start the index at -1 because there is a dummy symbol on
1056 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1058 for (i
= -1; stabp
< stabs_end
; stabp
++, i
++)
1060 SWAP_SYMBOL (stabp
, abfd
);
1061 printf ("\n%-6d ", i
);
1062 /* Either print the stab name, or, if unnamed, print its number
1063 again (makes consistent formatting for tools like awk). */
1064 if (stab_name
[stabp
->n_type
])
1065 printf ("%-6s", stab_name
[stabp
->n_type
]);
1066 else if (stabp
->n_type
== N_UNDF
)
1069 printf ("%-6d", stabp
->n_type
);
1070 printf (" %-6d %-6d ", stabp
->n_other
, stabp
->n_desc
);
1071 printf_vma (stabp
->n_value
);
1072 printf (" %-6lu", stabp
->n_strx
);
1074 /* Symbols with type == 0 (N_UNDF) specify the length of the
1075 string table associated with this file. We use that info
1076 to know how to relocate the *next* file's string table indices. */
1078 if (stabp
->n_type
== N_UNDF
)
1080 file_string_table_offset
= next_file_string_table_offset
;
1081 next_file_string_table_offset
+= stabp
->n_value
;
1085 /* Using the (possibly updated) string table offset, print the
1086 string (if any) associated with this symbol. */
1088 if ((stabp
->n_strx
+ file_string_table_offset
) < stabstr_size
)
1089 printf (" %s", &strtab
[stabp
->n_strx
+ file_string_table_offset
]);
1098 dump_section_stabs (abfd
, stabsect_name
, strsect_name
)
1100 char *stabsect_name
;
1103 if (read_section_stabs (abfd
, stabsect_name
, strsect_name
))
1105 print_section_stabs (abfd
, stabsect_name
, strsect_name
);
1112 dump_bfd_header (abfd
)
1117 printf ("architecture: %s, ",
1118 bfd_printable_arch_mach (bfd_get_arch (abfd
),
1119 bfd_get_mach (abfd
)));
1120 printf ("flags 0x%08x:\n", abfd
->flags
);
1122 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1123 PF (HAS_RELOC
, "HAS_RELOC");
1124 PF (EXEC_P
, "EXEC_P");
1125 PF (HAS_LINENO
, "HAS_LINENO");
1126 PF (HAS_DEBUG
, "HAS_DEBUG");
1127 PF (HAS_SYMS
, "HAS_SYMS");
1128 PF (HAS_LOCALS
, "HAS_LOCALS");
1129 PF (DYNAMIC
, "DYNAMIC");
1130 PF (WP_TEXT
, "WP_TEXT");
1131 PF (D_PAGED
, "D_PAGED");
1132 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
1133 printf ("\nstart address 0x");
1134 printf_vma (abfd
->start_address
);
1143 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
1145 bfd_nonfatal (bfd_get_filename (abfd
));
1146 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1148 list_matching_formats (matching
);
1154 printf ("\n%s: file format %s\n", bfd_get_filename (abfd
),
1157 print_arelt_descr (stdout
, abfd
, true);
1158 if (dump_file_header
)
1159 dump_bfd_header (abfd
);
1161 if (dump_section_headers
)
1162 dump_headers (abfd
);
1163 if (dump_symtab
|| dump_reloc_info
|| disassemble
)
1165 syms
= slurp_symtab (abfd
);
1167 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
)
1169 dynsyms
= slurp_dynamic_symtab (abfd
);
1172 dump_symbols (abfd
, false);
1173 if (dump_dynamic_symtab
)
1174 dump_symbols (abfd
, true);
1175 if (dump_stab_section_info
)
1177 if (dump_reloc_info
&& ! disassemble
)
1179 if (dump_dynamic_reloc_info
)
1180 dump_dynamic_relocs (abfd
);
1181 if (dump_section_contents
)
1184 disassemble_data (abfd
);
1188 display_file (filename
, target
)
1192 bfd
*file
, *arfile
= (bfd
*) NULL
;
1194 file
= bfd_openr (filename
, target
);
1197 bfd_nonfatal (filename
);
1201 if (bfd_check_format (file
, bfd_archive
) == true)
1203 bfd
*last_arfile
= NULL
;
1205 printf ("In archive %s:\n", bfd_get_filename (file
));
1208 bfd_set_error (bfd_error_no_error
);
1210 arfile
= bfd_openr_next_archived_file (file
, arfile
);
1213 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1215 bfd_nonfatal (bfd_get_filename (file
));
1220 display_bfd (arfile
);
1222 if (last_arfile
!= NULL
)
1223 bfd_close (last_arfile
);
1224 last_arfile
= arfile
;
1227 if (last_arfile
!= NULL
)
1228 bfd_close (last_arfile
);
1236 /* Actually display the various requested regions */
1244 bfd_size_type datasize
= 0;
1247 for (section
= abfd
->sections
; section
!= NULL
; section
=
1252 if (only
== (char *) NULL
||
1253 strcmp (only
, section
->name
) == 0)
1255 if (section
->flags
& SEC_HAS_CONTENTS
)
1257 printf ("Contents of section %s:\n", section
->name
);
1259 if (bfd_section_size (abfd
, section
) == 0)
1261 data
= (bfd_byte
*) xmalloc ((size_t) bfd_section_size (abfd
, section
));
1262 datasize
= bfd_section_size (abfd
, section
);
1265 bfd_get_section_contents (abfd
, section
, (PTR
) data
, 0, bfd_section_size (abfd
, section
));
1267 for (i
= 0; i
< bfd_section_size (abfd
, section
); i
+= onaline
)
1271 printf (" %04lx ", (unsigned long int) (i
+ section
->vma
));
1272 for (j
= i
; j
< i
+ onaline
; j
++)
1274 if (j
< bfd_section_size (abfd
, section
))
1275 printf ("%02x", (unsigned) (data
[j
]));
1283 for (j
= i
; j
< i
+ onaline
; j
++)
1285 if (j
>= bfd_section_size (abfd
, section
))
1288 printf ("%c", isprint (data
[j
]) ? data
[j
] : '.');
1298 /* Should perhaps share code and display with nm? */
1300 dump_symbols (abfd
, dynamic
)
1314 printf ("DYNAMIC SYMBOL TABLE:\n");
1322 printf ("SYMBOL TABLE:\n");
1325 for (count
= 0; count
< max
; count
++)
1329 bfd
*cur_bfd
= bfd_asymbol_bfd(*current
);
1332 bfd_print_symbol (cur_bfd
,
1334 *current
, bfd_print_symbol_all
);
1352 for (a
= abfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1356 if (bfd_is_abs_section (a
))
1358 if (bfd_is_und_section (a
))
1360 if (bfd_is_com_section (a
))
1365 if (strcmp (only
, a
->name
))
1368 else if ((a
->flags
& SEC_RELOC
) == 0)
1371 printf ("RELOCATION RECORDS FOR [%s]:", a
->name
);
1373 relsize
= bfd_get_reloc_upper_bound (abfd
, a
);
1375 bfd_fatal (bfd_get_filename (abfd
));
1379 printf (" (none)\n\n");
1383 relpp
= (arelent
**) xmalloc (relsize
);
1384 relcount
= bfd_canonicalize_reloc (abfd
, a
, relpp
, syms
);
1386 bfd_fatal (bfd_get_filename (abfd
));
1387 else if (relcount
== 0)
1389 printf (" (none)\n\n");
1394 dump_reloc_set (abfd
, relpp
, relcount
);
1403 dump_dynamic_relocs (abfd
)
1410 printf ("DYNAMIC RELOCATION RECORDS");
1412 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
1414 bfd_fatal (bfd_get_filename (abfd
));
1418 printf (" (none)\n\n");
1422 relpp
= (arelent
**) xmalloc (relsize
);
1423 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
1425 bfd_fatal (bfd_get_filename (abfd
));
1426 else if (relcount
== 0)
1428 printf (" (none)\n\n");
1433 dump_reloc_set (abfd
, relpp
, relcount
);
1441 dump_reloc_set (abfd
, relpp
, relcount
)
1448 /* Get column headers lined up reasonably. */
1454 sprintf_vma (buf
, (bfd_vma
) -1);
1455 width
= strlen (buf
) - 7;
1457 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
1460 for (p
= relpp
; relcount
&& *p
!= (arelent
*) NULL
; p
++, relcount
--)
1463 CONST
char *sym_name
;
1464 CONST
char *section_name
;
1466 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
1468 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
1469 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
1474 section_name
= NULL
;
1478 printf_vma (q
->address
);
1479 printf (" %-16s %s",
1485 if (section_name
== (CONST
char *) NULL
)
1486 section_name
= "*unknown*";
1487 printf_vma (q
->address
);
1488 printf (" %-16s [%s]",
1495 printf_vma (q
->addend
);
1501 /* The length of the longest architecture name + 1. */
1502 #define LONGEST_ARCH sizeof("rs6000:6000")
1508 /* List the targets that BFD is configured to support, each followed
1509 by its endianness and the architectures it supports. */
1512 display_target_list ()
1514 extern char *tmpnam ();
1515 extern bfd_target
*bfd_target_vector
[];
1516 char tmparg
[L_tmpnam
];
1520 dummy_name
= tmpnam (tmparg
);
1521 for (t
= 0; bfd_target_vector
[t
]; t
++)
1523 bfd_target
*p
= bfd_target_vector
[t
];
1524 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1527 printf ("%s\n (header %s, data %s)\n", p
->name
,
1528 p
->header_byteorder_big_p
? "big endian" : "little endian",
1529 p
->byteorder_big_p
? "big endian" : "little endian");
1533 bfd_nonfatal (dummy_name
);
1537 if (! bfd_set_format (abfd
, bfd_object
))
1539 if (bfd_get_error () != bfd_error_invalid_operation
)
1540 bfd_nonfatal (p
->name
);
1544 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1545 if (bfd_set_arch_mach (abfd
, (enum bfd_architecture
) a
, 0))
1547 bfd_printable_arch_mach ((enum bfd_architecture
) a
, 0));
1549 unlink (dummy_name
);
1552 /* Print a table showing which architectures are supported for entries
1553 FIRST through LAST-1 of bfd_target_vector (targets across,
1554 architectures down). */
1557 display_info_table (first
, last
)
1561 extern bfd_target
*bfd_target_vector
[];
1562 extern char *tmpnam ();
1563 char tmparg
[L_tmpnam
];
1567 /* Print heading of target names. */
1568 printf ("\n%*s", (int) LONGEST_ARCH
, " ");
1569 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1570 printf ("%s ", bfd_target_vector
[t
]->name
);
1573 dummy_name
= tmpnam (tmparg
);
1574 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1575 if (strcmp (bfd_printable_arch_mach (a
, 0), "UNKNOWN!") != 0)
1577 printf ("%*s ", (int) LONGEST_ARCH
- 1,
1578 bfd_printable_arch_mach (a
, 0));
1579 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1581 bfd_target
*p
= bfd_target_vector
[t
];
1583 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1587 bfd_nonfatal (p
->name
);
1593 if (! bfd_set_format (abfd
, bfd_object
))
1595 if (bfd_get_error () != bfd_error_invalid_operation
)
1596 bfd_nonfatal (p
->name
);
1603 if (! bfd_set_arch_mach (abfd
, a
, 0))
1608 printf ("%s ", p
->name
);
1611 int l
= strlen (p
->name
);
1619 unlink (dummy_name
);
1622 /* Print tables of all the target-architecture combinations that
1623 BFD has been configured to support. */
1626 display_target_tables ()
1629 extern bfd_target
*bfd_target_vector
[];
1631 extern char *getenv ();
1634 colum
= getenv ("COLUMNS");
1636 columns
= atoi (colum
);
1641 while (bfd_target_vector
[t
] != NULL
)
1645 wid
= LONGEST_ARCH
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1647 while (wid
< columns
&& bfd_target_vector
[t
] != NULL
)
1651 newwid
= wid
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1652 if (newwid
>= columns
)
1657 display_info_table (oldt
, t
);
1664 printf ("BFD header file version %s\n", BFD_VERSION
);
1665 display_target_list ();
1666 display_target_tables ();
1675 char *target
= default_target
;
1676 boolean seenflag
= false;
1678 program_name
= *argv
;
1679 xmalloc_set_program_name (program_name
);
1681 START_PROGRESS (program_name
, 0);
1685 while ((c
= getopt_long (argc
, argv
, "ib:m:VdDlfahrRtTxsSj:", long_options
,
1693 break; /* we've been given a long option */
1701 with_line_numbers
= 1;
1707 dump_file_header
= true;
1710 formats_info
= true;
1714 dump_reloc_info
= 1;
1715 dump_file_header
= true;
1717 dump_section_headers
= 1;
1723 dump_dynamic_symtab
= 1;
1729 disassemble
= disassemble_all
= true;
1733 with_source_code
= true;
1736 dump_section_contents
= 1;
1739 dump_reloc_info
= 1;
1742 dump_dynamic_reloc_info
= 1;
1748 dump_section_headers
= 1;
1762 printf ("GNU %s version %s\n", program_name
, program_version
);
1766 if (seenflag
== false)
1776 display_file ("a.out", target
);
1778 for (; optind
< argc
;)
1779 display_file (argv
[optind
++], target
);
1782 END_PROGRESS (program_name
);