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 */
58 int wide_output
; /* -w */
60 /* Extra info to pass to the disassembler address printing function. */
61 struct objdump_disasm_info
{
67 /* Architecture to disassemble for, or default if NULL. */
68 char *machine
= (char *) NULL
;
70 /* The symbol table. */
73 /* Number of symbols in `syms'. */
76 /* The sorted symbol table. */
77 asymbol
**sorted_syms
;
79 /* Number of symbols in `sorted_syms'. */
80 long sorted_symcount
= 0;
82 /* The dynamic symbol table. */
85 /* Number of symbols in `dynsyms'. */
88 /* Forward declarations. */
91 display_file
PARAMS ((char *filename
, char *target
));
94 dump_data
PARAMS ((bfd
*abfd
));
97 dump_relocs
PARAMS ((bfd
*abfd
));
100 dump_dynamic_relocs
PARAMS ((bfd
* abfd
));
103 dump_reloc_set
PARAMS ((bfd
*, arelent
**, long));
106 dump_symbols
PARAMS ((bfd
*abfd
, boolean dynamic
));
109 display_bfd
PARAMS ((bfd
*abfd
));
112 objdump_print_address
PARAMS ((bfd_vma
, struct disassemble_info
*));
115 show_line
PARAMS ((bfd
*, asection
*, bfd_vma
));
118 usage (stream
, status
)
123 Usage: %s [-ahifdDrRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
124 [--archive-headers] [--target=bfdname] [--disassemble]\n\
125 [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
126 [--info] [--section=section-name] [--line-numbers] [--source]\n",
129 [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
130 [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
131 [--wide] [--version] [--help] objfile...\n\
132 at least one option besides -l (--line-numbers) must be given\n");
133 list_supported_targets (program_name
, stream
);
137 static struct option long_options
[]=
139 {"all-headers", no_argument
, NULL
, 'x'},
140 {"architecture", required_argument
, NULL
, 'm'},
141 {"archive-headers", no_argument
, NULL
, 'a'},
142 {"disassemble", no_argument
, NULL
, 'd'},
143 {"disassemble-all", no_argument
, NULL
, 'D'},
144 {"dynamic-reloc", no_argument
, NULL
, 'R'},
145 {"dynamic-syms", no_argument
, NULL
, 'T'},
146 {"file-headers", no_argument
, NULL
, 'f'},
147 {"full-contents", no_argument
, NULL
, 's'},
148 {"headers", no_argument
, NULL
, 'h'},
149 {"help", no_argument
, NULL
, 'H'},
150 {"info", no_argument
, NULL
, 'i'},
151 {"line-numbers", no_argument
, NULL
, 'l'},
152 {"reloc", no_argument
, NULL
, 'r'},
153 {"section", required_argument
, NULL
, 'j'},
154 {"section-headers", no_argument
, NULL
, 'h'},
155 {"source", no_argument
, NULL
, 'S'},
156 {"stabs", no_argument
, &dump_stab_section_info
, 1},
157 {"syms", no_argument
, NULL
, 't'},
158 {"target", required_argument
, NULL
, 'b'},
159 {"version", no_argument
, &show_version
, 1},
160 {"wide", no_argument
, &wide_output
, 'w'},
161 {0, no_argument
, 0, 0}
165 dump_section_header (abfd
, section
, ignored
)
173 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
176 printf ("SECTION %d [%s]\t: size %08x",
179 (unsigned) bfd_get_section_size_before_reloc (section
));
181 printf_vma (section
->vma
);
183 printf_vma (section
->lma
);
184 printf (" align 2**%u%s ",
185 section
->alignment_power
, (wide_output
) ? "" : "\n");
186 PF (SEC_ALLOC
, "ALLOC");
187 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
188 PF (SEC_CONSTRUCTOR_TEXT
, "CONSTRUCTOR TEXT");
189 PF (SEC_CONSTRUCTOR_DATA
, "CONSTRUCTOR DATA");
190 PF (SEC_CONSTRUCTOR_BSS
, "CONSTRUCTOR BSS");
191 PF (SEC_LOAD
, "LOAD");
192 PF (SEC_RELOC
, "RELOC");
194 PF (SEC_BALIGN
, "BALIGN");
196 PF (SEC_READONLY
, "READONLY");
197 PF (SEC_CODE
, "CODE");
198 PF (SEC_DATA
, "DATA");
200 PF (SEC_DEBUGGING
, "DEBUGGING");
201 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
210 bfd_map_over_sections (abfd
, dump_section_header
, (PTR
) NULL
);
217 asymbol
**sy
= (asymbol
**) NULL
;
220 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
222 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd
));
226 storage
= bfd_get_symtab_upper_bound (abfd
);
228 bfd_fatal (bfd_get_filename (abfd
));
232 sy
= (asymbol
**) xmalloc (storage
);
234 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
236 bfd_fatal (bfd_get_filename (abfd
));
238 fprintf (stderr
, "%s: %s: No symbols\n",
239 program_name
, bfd_get_filename (abfd
));
243 /* Read in the dynamic symbols. */
246 slurp_dynamic_symtab (abfd
)
249 asymbol
**sy
= (asymbol
**) NULL
;
252 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
255 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
257 fprintf (stderr
, "%s: %s: not a dynamic object\n",
258 program_name
, bfd_get_filename (abfd
));
262 bfd_fatal (bfd_get_filename (abfd
));
267 sy
= (asymbol
**) xmalloc (storage
);
269 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
271 bfd_fatal (bfd_get_filename (abfd
));
272 if (dynsymcount
== 0)
273 fprintf (stderr
, "%s: %s: No dynamic symbols\n",
274 program_name
, bfd_get_filename (abfd
));
278 /* Filter out (in place) symbols that are useless for disassembly.
279 COUNT is the number of elements in SYMBOLS.
280 Return the number of useful symbols. */
283 remove_useless_symbols (symbols
, count
)
287 register asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
291 asymbol
*sym
= *in_ptr
++;
293 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
295 if (sym
->flags
& (BSF_DEBUGGING
))
297 if (bfd_is_und_section (sym
->section
)
298 || bfd_is_com_section (sym
->section
))
303 return out_ptr
- symbols
;
306 /* Sort symbols into value order. */
309 compare_symbols (ap
, bp
)
313 const asymbol
*a
= *(const asymbol
**)ap
;
314 const asymbol
*b
= *(const asymbol
**)bp
;
316 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
318 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
321 if (a
->section
> b
->section
)
323 else if (a
->section
< b
->section
)
328 /* Sort relocs into address order. */
331 compare_relocs (ap
, bp
)
335 const arelent
*a
= *(const arelent
**)ap
;
336 const arelent
*b
= *(const arelent
**)bp
;
338 if (a
->address
> b
->address
)
340 else if (a
->address
< b
->address
)
343 return compare_symbols ((const PTR
) a
->sym_ptr_ptr
,
344 (const PTR
) b
->sym_ptr_ptr
);
347 /* Print VMA symbolically to INFO if possible. */
350 objdump_print_address (vma
, info
)
352 struct disassemble_info
*info
;
354 /* @@ For relocateable files, should filter out symbols belonging to
355 the wrong section. Unfortunately, not enough information is supplied
356 to this routine to determine the correct section in all cases. */
357 /* @@ Would it speed things up to cache the last two symbols returned,
358 and maybe their address ranges? For many processors, only one memory
359 operand can be present at a time, so the 2-entry cache wouldn't be
360 constantly churned by code doing heavy memory accesses. */
362 /* Indices in `sorted_syms'. */
364 long max
= sorted_symcount
;
367 fprintf_vma (info
->stream
, vma
);
369 if (sorted_symcount
< 1)
372 /* Perform a binary search looking for the closest symbol to the
373 required value. We are searching the range (min, max]. */
374 while (min
+ 1 < max
)
378 thisplace
= (max
+ min
) / 2;
379 sym
= sorted_syms
[thisplace
];
381 if (bfd_asymbol_value (sym
) > vma
)
383 else if (bfd_asymbol_value (sym
) < vma
)
392 /* The symbol we want is now in min, the low end of the range we
397 /* If this symbol isn't global, search for one with the same value
399 bfd_vma val
= bfd_asymbol_value (sorted_syms
[thisplace
]);
401 if (sorted_syms
[thisplace
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
402 for (i
= thisplace
- 1; i
>= 0; i
--)
404 if (bfd_asymbol_value (sorted_syms
[i
]) == val
405 && (!(sorted_syms
[i
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
406 || ((sorted_syms
[thisplace
]->flags
& BSF_DEBUGGING
)
407 && !(sorted_syms
[i
]->flags
& BSF_DEBUGGING
))))
413 if (sorted_syms
[thisplace
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
414 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
416 if (bfd_asymbol_value (sorted_syms
[i
]) == val
417 && (!(sorted_syms
[i
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
418 || ((sorted_syms
[thisplace
]->flags
& BSF_DEBUGGING
)
419 && !(sorted_syms
[i
]->flags
& BSF_DEBUGGING
))))
427 /* If the file is relocateable, and the symbol could be from this
428 section, prefer a symbol from this section over symbols from
429 others, even if the other symbol's value might be closer.
431 Note that this may be wrong for some symbol references if the
432 sections have overlapping memory ranges, but in that case there's
433 no way to tell what's desired without looking at the relocation
435 struct objdump_disasm_info
*aux
;
438 aux
= (struct objdump_disasm_info
*) info
->application_data
;
439 if (sorted_syms
[thisplace
]->section
!= aux
->sec
441 || ((aux
->abfd
->flags
& HAS_RELOC
) != 0
442 && vma
>= bfd_get_section_vma (aux
->abfd
, aux
->sec
)
443 && vma
< (bfd_get_section_vma (aux
->abfd
, aux
->sec
)
444 + bfd_get_section_size_before_reloc (aux
->sec
)))))
446 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
448 if (bfd_asymbol_value (sorted_syms
[i
])
449 != bfd_asymbol_value (sorted_syms
[thisplace
]))
455 if (sorted_syms
[i
]->section
== aux
->sec
)
462 if (sorted_syms
[thisplace
]->section
!= aux
->sec
)
464 /* We didn't find a good symbol with a smaller value.
465 Look for one with a larger value. */
466 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
468 if (sorted_syms
[i
]->section
== aux
->sec
)
478 fprintf (info
->stream
, " <%s", sorted_syms
[thisplace
]->name
);
479 if (bfd_asymbol_value (sorted_syms
[thisplace
]) > vma
)
481 char buf
[30], *p
= buf
;
482 sprintf_vma (buf
, bfd_asymbol_value (sorted_syms
[thisplace
]) - vma
);
485 fprintf (info
->stream
, "-%s", p
);
487 else if (vma
> bfd_asymbol_value (sorted_syms
[thisplace
]))
489 char buf
[30], *p
= buf
;
490 sprintf_vma (buf
, vma
- bfd_asymbol_value (sorted_syms
[thisplace
]));
493 fprintf (info
->stream
, "+%s", p
);
495 fprintf (info
->stream
, ">");
498 /* Hold the last function name and the last line number we displayed
501 static char *prev_functionname
;
502 static unsigned int prev_line
;
504 /* We keep a list of all files that we have seen when doing a
505 dissassembly with source, so that we know how much of the file to
506 display. This can be important for inlined functions. */
508 struct print_file_list
510 struct print_file_list
*next
;
516 static struct print_file_list
*print_files
;
518 /* The number of preceding context lines to show when we start
519 displaying a file for the first time. */
521 #define SHOW_PRECEDING_CONTEXT_LINES (5)
523 /* Skip ahead to a given line in a file, optionally printing each
527 skip_to_line
PARAMS ((struct print_file_list
*, unsigned int, boolean
));
530 skip_to_line (p
, line
, show
)
531 struct print_file_list
*p
;
535 while (p
->line
< line
)
539 if (fgets (buf
, sizeof buf
, p
->f
) == NULL
)
549 if (strchr (buf
, '\n') != NULL
)
554 /* Show the line number, or the source line, in a dissassembly
558 show_line (abfd
, section
, off
)
563 CONST
char *filename
;
564 CONST
char *functionname
;
567 if (! with_line_numbers
&& ! with_source_code
)
570 if (! bfd_find_nearest_line (abfd
, section
, syms
, off
, &filename
,
571 &functionname
, &line
))
574 if (filename
!= NULL
&& *filename
== '\0')
576 if (functionname
!= NULL
&& *functionname
== '\0')
579 if (with_line_numbers
)
581 if (functionname
!= NULL
582 && (prev_functionname
== NULL
583 || strcmp (functionname
, prev_functionname
) != 0))
584 printf ("%s():\n", functionname
);
585 if (line
> 0 && line
!= prev_line
)
586 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
593 struct print_file_list
**pp
, *p
;
595 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
596 if (strcmp ((*pp
)->filename
, filename
) == 0)
602 if (p
!= print_files
)
606 /* We have reencountered a file name which we saw
607 earlier. This implies that either we are dumping out
608 code from an included file, or the same file was
609 linked in more than once. There are two common cases
610 of an included file: inline functions in a header
611 file, and a bison or flex skeleton file. In the
612 former case we want to just start printing (but we
613 back up a few lines to give context); in the latter
614 case we want to continue from where we left off. I
615 can't think of a good way to distinguish the cases,
616 so I used a heuristic based on the file name. */
617 if (strcmp (p
->filename
+ strlen (p
->filename
) - 2, ".h") != 0)
621 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
628 p
->f
= fopen (p
->filename
, "r");
632 skip_to_line (p
, l
, false);
634 if (print_files
->f
!= NULL
)
636 fclose (print_files
->f
);
637 print_files
->f
= NULL
;
643 skip_to_line (p
, line
, true);
645 p
->next
= print_files
;
653 f
= fopen (filename
, "r");
658 p
= ((struct print_file_list
*)
659 xmalloc (sizeof (struct print_file_list
)));
660 p
->filename
= xmalloc (strlen (filename
) + 1);
661 strcpy (p
->filename
, filename
);
665 if (print_files
!= NULL
&& print_files
->f
!= NULL
)
667 fclose (print_files
->f
);
668 print_files
->f
= NULL
;
670 p
->next
= print_files
;
673 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
676 skip_to_line (p
, l
, false);
678 skip_to_line (p
, line
, true);
683 if (functionname
!= NULL
684 && (prev_functionname
== NULL
685 || strcmp (functionname
, prev_functionname
) != 0))
687 if (prev_functionname
!= NULL
)
688 free (prev_functionname
);
689 prev_functionname
= xmalloc (strlen (functionname
) + 1);
690 strcpy (prev_functionname
, functionname
);
693 if (line
> 0 && line
!= prev_line
)
698 disassemble_data (abfd
)
702 unsigned int (*print
) () = 0; /* Old style */
703 disassembler_ftype disassemble_fn
= 0; /* New style */
704 struct disassemble_info disasm_info
;
705 struct objdump_disasm_info aux
;
707 boolean done_dot
= false;
710 prev_functionname
= NULL
;
713 /* We make a copy of syms to sort. We don't want to sort syms
714 because that will screw up the relocs. */
715 sorted_syms
= (asymbol
**) xmalloc (symcount
* sizeof (asymbol
*));
716 memcpy (sorted_syms
, syms
, symcount
* sizeof (asymbol
*));
718 sorted_symcount
= remove_useless_symbols (sorted_syms
, symcount
);
720 /* Sort the symbols into section and symbol order */
721 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
723 INIT_DISASSEMBLE_INFO(disasm_info
, stdout
);
724 disasm_info
.application_data
= (PTR
) &aux
;
726 disasm_info
.print_address_func
= objdump_print_address
;
728 if (machine
!= (char *) NULL
)
730 bfd_arch_info_type
*info
= bfd_scan_arch (machine
);
733 fprintf (stderr
, "%s: Can't use supplied machine %s\n",
738 abfd
->arch_info
= info
;
741 /* See if we can disassemble using bfd. */
743 if (abfd
->arch_info
->disassemble
)
745 print
= abfd
->arch_info
->disassemble
;
749 disassemble_fn
= disassembler (abfd
);
752 fprintf (stderr
, "%s: Can't disassemble for architecture %s\n",
754 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
759 for (section
= abfd
->sections
;
760 section
!= (asection
*) NULL
;
761 section
= section
->next
)
763 bfd_byte
*data
= NULL
;
764 bfd_size_type datasize
= 0;
765 arelent
**relbuf
= NULL
;
766 arelent
**relpp
= NULL
;
767 arelent
**relppend
= NULL
;
769 if ((section
->flags
& SEC_LOAD
) == 0
770 || (! disassemble_all
772 && (section
->flags
& SEC_CODE
) == 0))
774 if (only
!= (char *) NULL
&& strcmp (only
, section
->name
) != 0)
778 && (section
->flags
& SEC_RELOC
) != 0)
782 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
784 bfd_fatal (bfd_get_filename (abfd
));
790 relbuf
= (arelent
**) xmalloc (relsize
);
791 relcount
= bfd_canonicalize_reloc (abfd
, section
, relbuf
, syms
);
793 bfd_fatal (bfd_get_filename (abfd
));
795 /* Sort the relocs by address. */
796 qsort (relbuf
, relcount
, sizeof (arelent
*), compare_relocs
);
799 relppend
= relpp
+ relcount
;
803 printf ("Disassembly of section %s:\n", section
->name
);
805 datasize
= bfd_get_section_size_before_reloc (section
);
809 data
= (bfd_byte
*) xmalloc ((size_t) datasize
);
811 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
814 disasm_info
.buffer
= data
;
815 disasm_info
.buffer_vma
= section
->vma
;
816 disasm_info
.buffer_length
= datasize
;
818 while (i
< disasm_info
.buffer_length
)
821 boolean need_nl
= false;
823 if (data
[i
] == 0 && data
[i
+ 1] == 0 && data
[i
+ 2] == 0 &&
826 if (done_dot
== false)
836 if (with_line_numbers
|| with_source_code
)
837 show_line (abfd
, section
, i
);
838 aux
.require_sec
= true;
839 objdump_print_address (section
->vma
+ i
, &disasm_info
);
840 aux
.require_sec
= false;
846 bytes
= (*disassemble_fn
) (section
->vma
+ i
, &disasm_info
);
853 bytes
= print (section
->vma
+ i
, data
+ i
, stdout
);
862 && (section
->flags
& SEC_RELOC
) != 0)
864 while (relpp
< relppend
865 && ((*relpp
)->address
>= i
866 && (*relpp
)->address
< i
+ bytes
))
869 const char *sym_name
;
873 printf ("\t\tRELOC: ");
875 printf_vma (section
->vma
+ q
->address
);
877 printf (" %s ", q
->howto
->name
);
879 if (q
->sym_ptr_ptr
!= NULL
880 && *q
->sym_ptr_ptr
!= NULL
)
882 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
883 if (sym_name
== NULL
|| *sym_name
== '\0')
887 sym_sec
= bfd_get_section (*q
->sym_ptr_ptr
);
888 sym_name
= bfd_get_section_name (abfd
, sym_sec
);
889 if (sym_name
== NULL
|| *sym_name
== '\0')
890 sym_name
= "*unknown*";
894 printf ("%s", sym_name
);
899 printf_vma (q
->addend
);
921 /* Define a table of stab values and print-strings. We wish the initializer
922 could be a direct-mapped table, but instead we build one the first
932 struct stab_print stab_print
[] = {
933 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
934 #include "aout/stab.def"
939 void dump_section_stabs
PARAMS ((bfd
*abfd
, char *stabsect_name
,
940 char *strsect_name
));
942 /* Dump the stabs sections from an object file that has a section that
943 uses Sun stabs encoding. It has to use some hooks into BFD because
944 string table sections are not normally visible to BFD callers. */
950 /* Allocate and initialize stab name array if first time. */
951 if (stab_name
== NULL
)
955 stab_name
= (char **) xmalloc (256 * sizeof(char *));
956 /* Clear the array. */
957 for (i
= 0; i
< 256; i
++)
959 /* Fill in the defined stabs. */
960 for (i
= 0; *stab_print
[i
].string
; i
++)
961 stab_name
[stab_print
[i
].value
] = stab_print
[i
].string
;
964 dump_section_stabs (abfd
, ".stab", ".stabstr");
965 dump_section_stabs (abfd
, ".stab.excl", ".stab.exclstr");
966 dump_section_stabs (abfd
, ".stab.index", ".stab.indexstr");
967 dump_section_stabs (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
970 static struct internal_nlist
*stabs
;
971 static bfd_size_type stab_size
;
974 static bfd_size_type stabstr_size
;
976 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
977 and string table section STRSECT_NAME into `strtab'.
978 If the section exists and was read, allocate the space and return true.
979 Otherwise return false. */
982 read_section_stabs (abfd
, stabsect_name
, strsect_name
)
987 asection
*stabsect
, *stabstrsect
;
989 stabsect
= bfd_get_section_by_name (abfd
, stabsect_name
);
992 printf ("No %s section present\n\n", stabsect_name
);
996 stabstrsect
= bfd_get_section_by_name (abfd
, strsect_name
);
997 if (0 == stabstrsect
)
999 fprintf (stderr
, "%s: %s has no %s section\n", program_name
,
1000 bfd_get_filename (abfd
), strsect_name
);
1004 stab_size
= bfd_section_size (abfd
, stabsect
);
1005 stabstr_size
= bfd_section_size (abfd
, stabstrsect
);
1007 stabs
= (struct internal_nlist
*) xmalloc (stab_size
);
1008 strtab
= (char *) xmalloc (stabstr_size
);
1010 if (! bfd_get_section_contents (abfd
, stabsect
, (PTR
) stabs
, 0, stab_size
))
1012 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1013 program_name
, stabsect_name
, bfd_get_filename (abfd
),
1014 bfd_errmsg (bfd_get_error ()));
1020 if (! bfd_get_section_contents (abfd
, stabstrsect
, (PTR
) strtab
, 0,
1023 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1024 program_name
, strsect_name
, bfd_get_filename (abfd
),
1025 bfd_errmsg (bfd_get_error ()));
1034 #define SWAP_SYMBOL(symp, abfd) \
1036 (symp)->n_strx = bfd_h_get_32(abfd, \
1037 (unsigned char *)&(symp)->n_strx); \
1038 (symp)->n_desc = bfd_h_get_16 (abfd, \
1039 (unsigned char *)&(symp)->n_desc); \
1040 (symp)->n_value = bfd_h_get_32 (abfd, \
1041 (unsigned char *)&(symp)->n_value); \
1044 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1045 using string table section STRSECT_NAME (in `strtab'). */
1048 print_section_stabs (abfd
, stabsect_name
, strsect_name
)
1050 char *stabsect_name
;
1054 unsigned file_string_table_offset
= 0, next_file_string_table_offset
= 0;
1055 struct internal_nlist
*stabp
= stabs
,
1056 *stabs_end
= (struct internal_nlist
*) (stab_size
+ (char *) stabs
);
1058 printf ("Contents of %s section:\n\n", stabsect_name
);
1059 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1061 /* Loop through all symbols and print them.
1063 We start the index at -1 because there is a dummy symbol on
1064 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1066 for (i
= -1; stabp
< stabs_end
; stabp
++, i
++)
1068 SWAP_SYMBOL (stabp
, abfd
);
1069 printf ("\n%-6d ", i
);
1070 /* Either print the stab name, or, if unnamed, print its number
1071 again (makes consistent formatting for tools like awk). */
1072 if (stab_name
[stabp
->n_type
])
1073 printf ("%-6s", stab_name
[stabp
->n_type
]);
1074 else if (stabp
->n_type
== N_UNDF
)
1077 printf ("%-6d", stabp
->n_type
);
1078 printf (" %-6d %-6d ", stabp
->n_other
, stabp
->n_desc
);
1079 printf_vma (stabp
->n_value
);
1080 printf (" %-6lu", stabp
->n_strx
);
1082 /* Symbols with type == 0 (N_UNDF) specify the length of the
1083 string table associated with this file. We use that info
1084 to know how to relocate the *next* file's string table indices. */
1086 if (stabp
->n_type
== N_UNDF
)
1088 file_string_table_offset
= next_file_string_table_offset
;
1089 next_file_string_table_offset
+= stabp
->n_value
;
1093 /* Using the (possibly updated) string table offset, print the
1094 string (if any) associated with this symbol. */
1096 if ((stabp
->n_strx
+ file_string_table_offset
) < stabstr_size
)
1097 printf (" %s", &strtab
[stabp
->n_strx
+ file_string_table_offset
]);
1106 dump_section_stabs (abfd
, stabsect_name
, strsect_name
)
1108 char *stabsect_name
;
1112 if (read_section_stabs (abfd
, stabsect_name
, strsect_name
))
1114 print_section_stabs (abfd
, stabsect_name
, strsect_name
);
1119 /* Check for names which are supersets of the selected name */
1120 for (s
= abfd
->sections
;
1124 if (strncmp (stabsect_name
, s
->name
, strlen (stabsect_name
)) == 0)
1126 read_section_stabs (abfd
, s
->name
, strsect_name
);
1127 print_section_stabs (abfd
, s
->name
, strsect_name
);
1135 dump_bfd_header (abfd
)
1140 printf ("architecture: %s, ",
1141 bfd_printable_arch_mach (bfd_get_arch (abfd
),
1142 bfd_get_mach (abfd
)));
1143 printf ("flags 0x%08x:\n", abfd
->flags
);
1145 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1146 PF (HAS_RELOC
, "HAS_RELOC");
1147 PF (EXEC_P
, "EXEC_P");
1148 PF (HAS_LINENO
, "HAS_LINENO");
1149 PF (HAS_DEBUG
, "HAS_DEBUG");
1150 PF (HAS_SYMS
, "HAS_SYMS");
1151 PF (HAS_LOCALS
, "HAS_LOCALS");
1152 PF (DYNAMIC
, "DYNAMIC");
1153 PF (WP_TEXT
, "WP_TEXT");
1154 PF (D_PAGED
, "D_PAGED");
1155 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
1156 printf ("\nstart address 0x");
1157 printf_vma (abfd
->start_address
);
1166 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
1168 bfd_nonfatal (bfd_get_filename (abfd
));
1169 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1171 list_matching_formats (matching
);
1177 printf ("\n%s: file format %s\n", bfd_get_filename (abfd
),
1180 print_arelt_descr (stdout
, abfd
, true);
1181 if (dump_file_header
)
1182 dump_bfd_header (abfd
);
1184 if (dump_section_headers
)
1185 dump_headers (abfd
);
1186 if (dump_symtab
|| dump_reloc_info
|| disassemble
)
1188 syms
= slurp_symtab (abfd
);
1190 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
)
1192 dynsyms
= slurp_dynamic_symtab (abfd
);
1195 dump_symbols (abfd
, false);
1196 if (dump_dynamic_symtab
)
1197 dump_symbols (abfd
, true);
1198 if (dump_stab_section_info
)
1200 if (dump_reloc_info
&& ! disassemble
)
1202 if (dump_dynamic_reloc_info
)
1203 dump_dynamic_relocs (abfd
);
1204 if (dump_section_contents
)
1207 disassemble_data (abfd
);
1211 display_file (filename
, target
)
1215 bfd
*file
, *arfile
= (bfd
*) NULL
;
1217 file
= bfd_openr (filename
, target
);
1220 bfd_nonfatal (filename
);
1224 if (bfd_check_format (file
, bfd_archive
) == true)
1226 bfd
*last_arfile
= NULL
;
1228 printf ("In archive %s:\n", bfd_get_filename (file
));
1231 bfd_set_error (bfd_error_no_error
);
1233 arfile
= bfd_openr_next_archived_file (file
, arfile
);
1236 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1238 bfd_nonfatal (bfd_get_filename (file
));
1243 display_bfd (arfile
);
1245 if (last_arfile
!= NULL
)
1246 bfd_close (last_arfile
);
1247 last_arfile
= arfile
;
1250 if (last_arfile
!= NULL
)
1251 bfd_close (last_arfile
);
1259 /* Actually display the various requested regions */
1267 bfd_size_type datasize
= 0;
1270 for (section
= abfd
->sections
; section
!= NULL
; section
=
1275 if (only
== (char *) NULL
||
1276 strcmp (only
, section
->name
) == 0)
1278 if (section
->flags
& SEC_HAS_CONTENTS
)
1280 printf ("Contents of section %s:\n", section
->name
);
1282 if (bfd_section_size (abfd
, section
) == 0)
1284 data
= (bfd_byte
*) xmalloc ((size_t) bfd_section_size (abfd
, section
));
1285 datasize
= bfd_section_size (abfd
, section
);
1288 bfd_get_section_contents (abfd
, section
, (PTR
) data
, 0, bfd_section_size (abfd
, section
));
1290 for (i
= 0; i
< bfd_section_size (abfd
, section
); i
+= onaline
)
1294 printf (" %04lx ", (unsigned long int) (i
+ section
->vma
));
1295 for (j
= i
; j
< i
+ onaline
; j
++)
1297 if (j
< bfd_section_size (abfd
, section
))
1298 printf ("%02x", (unsigned) (data
[j
]));
1306 for (j
= i
; j
< i
+ onaline
; j
++)
1308 if (j
>= bfd_section_size (abfd
, section
))
1311 printf ("%c", isprint (data
[j
]) ? data
[j
] : '.');
1321 /* Should perhaps share code and display with nm? */
1323 dump_symbols (abfd
, dynamic
)
1337 printf ("DYNAMIC SYMBOL TABLE:\n");
1345 printf ("SYMBOL TABLE:\n");
1348 for (count
= 0; count
< max
; count
++)
1352 bfd
*cur_bfd
= bfd_asymbol_bfd(*current
);
1355 bfd_print_symbol (cur_bfd
,
1357 *current
, bfd_print_symbol_all
);
1375 for (a
= abfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1379 if (bfd_is_abs_section (a
))
1381 if (bfd_is_und_section (a
))
1383 if (bfd_is_com_section (a
))
1388 if (strcmp (only
, a
->name
))
1391 else if ((a
->flags
& SEC_RELOC
) == 0)
1394 printf ("RELOCATION RECORDS FOR [%s]:", a
->name
);
1396 relsize
= bfd_get_reloc_upper_bound (abfd
, a
);
1398 bfd_fatal (bfd_get_filename (abfd
));
1402 printf (" (none)\n\n");
1406 relpp
= (arelent
**) xmalloc (relsize
);
1407 relcount
= bfd_canonicalize_reloc (abfd
, a
, relpp
, syms
);
1409 bfd_fatal (bfd_get_filename (abfd
));
1410 else if (relcount
== 0)
1412 printf (" (none)\n\n");
1417 dump_reloc_set (abfd
, relpp
, relcount
);
1426 dump_dynamic_relocs (abfd
)
1433 printf ("DYNAMIC RELOCATION RECORDS");
1435 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
1437 bfd_fatal (bfd_get_filename (abfd
));
1441 printf (" (none)\n\n");
1445 relpp
= (arelent
**) xmalloc (relsize
);
1446 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
1448 bfd_fatal (bfd_get_filename (abfd
));
1449 else if (relcount
== 0)
1451 printf (" (none)\n\n");
1456 dump_reloc_set (abfd
, relpp
, relcount
);
1464 dump_reloc_set (abfd
, relpp
, relcount
)
1471 /* Get column headers lined up reasonably. */
1477 sprintf_vma (buf
, (bfd_vma
) -1);
1478 width
= strlen (buf
) - 7;
1480 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
1483 for (p
= relpp
; relcount
&& *p
!= (arelent
*) NULL
; p
++, relcount
--)
1486 CONST
char *sym_name
;
1487 CONST
char *section_name
;
1489 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
1491 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
1492 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
1497 section_name
= NULL
;
1501 printf_vma (q
->address
);
1502 printf (" %-16s %s",
1508 if (section_name
== (CONST
char *) NULL
)
1509 section_name
= "*unknown*";
1510 printf_vma (q
->address
);
1511 printf (" %-16s [%s]",
1518 printf_vma (q
->addend
);
1524 /* The length of the longest architecture name + 1. */
1525 #define LONGEST_ARCH sizeof("rs6000:6000")
1531 /* List the targets that BFD is configured to support, each followed
1532 by its endianness and the architectures it supports. */
1535 display_target_list ()
1537 extern char *tmpnam ();
1538 extern bfd_target
*bfd_target_vector
[];
1539 char tmparg
[L_tmpnam
];
1543 dummy_name
= tmpnam (tmparg
);
1544 for (t
= 0; bfd_target_vector
[t
]; t
++)
1546 bfd_target
*p
= bfd_target_vector
[t
];
1547 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1550 printf ("%s\n (header %s, data %s)\n", p
->name
,
1551 p
->header_byteorder_big_p
? "big endian" : "little endian",
1552 p
->byteorder_big_p
? "big endian" : "little endian");
1556 bfd_nonfatal (dummy_name
);
1560 if (! bfd_set_format (abfd
, bfd_object
))
1562 if (bfd_get_error () != bfd_error_invalid_operation
)
1563 bfd_nonfatal (p
->name
);
1567 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1568 if (bfd_set_arch_mach (abfd
, (enum bfd_architecture
) a
, 0))
1570 bfd_printable_arch_mach ((enum bfd_architecture
) a
, 0));
1572 unlink (dummy_name
);
1575 /* Print a table showing which architectures are supported for entries
1576 FIRST through LAST-1 of bfd_target_vector (targets across,
1577 architectures down). */
1580 display_info_table (first
, last
)
1584 extern bfd_target
*bfd_target_vector
[];
1585 extern char *tmpnam ();
1586 char tmparg
[L_tmpnam
];
1590 /* Print heading of target names. */
1591 printf ("\n%*s", (int) LONGEST_ARCH
, " ");
1592 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1593 printf ("%s ", bfd_target_vector
[t
]->name
);
1596 dummy_name
= tmpnam (tmparg
);
1597 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1598 if (strcmp (bfd_printable_arch_mach (a
, 0), "UNKNOWN!") != 0)
1600 printf ("%*s ", (int) LONGEST_ARCH
- 1,
1601 bfd_printable_arch_mach (a
, 0));
1602 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1604 bfd_target
*p
= bfd_target_vector
[t
];
1606 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1610 bfd_nonfatal (p
->name
);
1616 if (! bfd_set_format (abfd
, bfd_object
))
1618 if (bfd_get_error () != bfd_error_invalid_operation
)
1619 bfd_nonfatal (p
->name
);
1626 if (! bfd_set_arch_mach (abfd
, a
, 0))
1631 printf ("%s ", p
->name
);
1634 int l
= strlen (p
->name
);
1642 unlink (dummy_name
);
1645 /* Print tables of all the target-architecture combinations that
1646 BFD has been configured to support. */
1649 display_target_tables ()
1652 extern bfd_target
*bfd_target_vector
[];
1654 extern char *getenv ();
1657 colum
= getenv ("COLUMNS");
1659 columns
= atoi (colum
);
1664 while (bfd_target_vector
[t
] != NULL
)
1668 wid
= LONGEST_ARCH
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1670 while (wid
< columns
&& bfd_target_vector
[t
] != NULL
)
1674 newwid
= wid
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1675 if (newwid
>= columns
)
1680 display_info_table (oldt
, t
);
1687 printf ("BFD header file version %s\n", BFD_VERSION
);
1688 display_target_list ();
1689 display_target_tables ();
1698 char *target
= default_target
;
1699 boolean seenflag
= false;
1701 program_name
= *argv
;
1702 xmalloc_set_program_name (program_name
);
1704 START_PROGRESS (program_name
, 0);
1708 while ((c
= getopt_long (argc
, argv
, "ib:m:VdDlfahrRtTxsSj:w", long_options
,
1716 break; /* we've been given a long option */
1724 with_line_numbers
= 1;
1730 dump_file_header
= true;
1733 formats_info
= true;
1737 dump_reloc_info
= 1;
1738 dump_file_header
= true;
1740 dump_section_headers
= 1;
1746 dump_dynamic_symtab
= 1;
1752 disassemble
= disassemble_all
= true;
1756 with_source_code
= true;
1759 dump_section_contents
= 1;
1762 dump_reloc_info
= 1;
1765 dump_dynamic_reloc_info
= 1;
1771 dump_section_headers
= 1;
1788 printf ("GNU %s version %s\n", program_name
, program_version
);
1792 if (seenflag
== false)
1802 display_file ("a.out", target
);
1804 for (; optind
< argc
;)
1805 display_file (argv
[optind
++], target
);
1808 END_PROGRESS (program_name
);