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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "libiberty.h"
29 /* Internal headers for the ELF .stab-dump code - sorry. */
30 #define BYTES_IN_WORD 32
31 #include "aout/aout64.h"
33 #ifdef NEED_DECLARATION_FPRINTF
34 /* This is needed by INIT_DISASSEMBLE_INFO. */
35 extern int fprintf ();
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 dump_private_headers
; /* -p */
52 int with_line_numbers
; /* -l */
53 boolean with_source_code
; /* -S */
54 int dump_stab_section_info
; /* --stabs */
55 boolean disassemble
; /* -d */
56 boolean disassemble_all
; /* -D */
57 boolean formats_info
; /* -i */
58 char *only
; /* -j secname */
59 int wide_output
; /* -w */
61 /* Extra info to pass to the disassembler address printing function. */
62 struct objdump_disasm_info
{
68 /* Architecture to disassemble for, or default if NULL. */
69 char *machine
= (char *) NULL
;
71 /* The symbol table. */
74 /* Number of symbols in `syms'. */
77 /* The sorted symbol table. */
78 asymbol
**sorted_syms
;
80 /* Number of symbols in `sorted_syms'. */
81 long sorted_symcount
= 0;
83 /* The dynamic symbol table. */
86 /* Number of symbols in `dynsyms'. */
89 /* Forward declarations. */
92 display_file
PARAMS ((char *filename
, char *target
));
95 dump_data
PARAMS ((bfd
*abfd
));
98 dump_relocs
PARAMS ((bfd
*abfd
));
101 dump_dynamic_relocs
PARAMS ((bfd
* abfd
));
104 dump_reloc_set
PARAMS ((bfd
*, arelent
**, long));
107 dump_symbols
PARAMS ((bfd
*abfd
, boolean dynamic
));
110 display_bfd
PARAMS ((bfd
*abfd
));
113 objdump_print_address
PARAMS ((bfd_vma
, struct disassemble_info
*));
116 show_line
PARAMS ((bfd
*, asection
*, bfd_vma
));
119 usage (stream
, status
)
124 Usage: %s [-ahifdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
125 [--archive-headers] [--target=bfdname] [--disassemble]\n\
126 [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
127 [--info] [--section=section-name] [--line-numbers] [--source]\n",
130 [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
131 [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
132 [--wide] [--version] [--help] [--private-headers] objfile...\n\
133 at least one option besides -l (--line-numbers) must be given\n");
134 list_supported_targets (program_name
, stream
);
138 static struct option long_options
[]=
140 {"all-headers", no_argument
, NULL
, 'x'},
141 {"private-headers", no_argument
, NULL
, 'p'},
142 {"architecture", required_argument
, NULL
, 'm'},
143 {"archive-headers", no_argument
, NULL
, 'a'},
144 {"disassemble", no_argument
, NULL
, 'd'},
145 {"disassemble-all", no_argument
, NULL
, 'D'},
146 {"dynamic-reloc", no_argument
, NULL
, 'R'},
147 {"dynamic-syms", no_argument
, NULL
, 'T'},
148 {"file-headers", no_argument
, NULL
, 'f'},
149 {"full-contents", no_argument
, NULL
, 's'},
150 {"headers", no_argument
, NULL
, 'h'},
151 {"help", no_argument
, NULL
, 'H'},
152 {"info", no_argument
, NULL
, 'i'},
153 {"line-numbers", no_argument
, NULL
, 'l'},
154 {"reloc", no_argument
, NULL
, 'r'},
155 {"section", required_argument
, NULL
, 'j'},
156 {"section-headers", no_argument
, NULL
, 'h'},
157 {"source", no_argument
, NULL
, 'S'},
158 {"stabs", no_argument
, &dump_stab_section_info
, 1},
159 {"syms", no_argument
, NULL
, 't'},
160 {"target", required_argument
, NULL
, 'b'},
161 {"version", no_argument
, &show_version
, 1},
162 {"wide", no_argument
, &wide_output
, 'w'},
163 {0, no_argument
, 0, 0}
167 dump_section_header (abfd
, section
, ignored
)
175 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
178 printf ("SECTION %d [%s]\t: size %08x",
181 (unsigned) bfd_get_section_size_before_reloc (section
));
183 printf_vma (section
->vma
);
185 printf_vma (section
->lma
);
186 printf (" align 2**%u%s ",
187 section
->alignment_power
, (wide_output
) ? "" : "\n");
188 PF (SEC_ALLOC
, "ALLOC");
189 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
190 PF (SEC_CONSTRUCTOR_TEXT
, "CONSTRUCTOR TEXT");
191 PF (SEC_CONSTRUCTOR_DATA
, "CONSTRUCTOR DATA");
192 PF (SEC_CONSTRUCTOR_BSS
, "CONSTRUCTOR BSS");
193 PF (SEC_LOAD
, "LOAD");
194 PF (SEC_RELOC
, "RELOC");
196 PF (SEC_BALIGN
, "BALIGN");
198 PF (SEC_READONLY
, "READONLY");
199 PF (SEC_CODE
, "CODE");
200 PF (SEC_DATA
, "DATA");
202 PF (SEC_DEBUGGING
, "DEBUGGING");
203 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
212 bfd_map_over_sections (abfd
, dump_section_header
, (PTR
) NULL
);
219 asymbol
**sy
= (asymbol
**) NULL
;
222 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
224 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd
));
228 storage
= bfd_get_symtab_upper_bound (abfd
);
230 bfd_fatal (bfd_get_filename (abfd
));
234 sy
= (asymbol
**) xmalloc (storage
);
236 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
238 bfd_fatal (bfd_get_filename (abfd
));
240 fprintf (stderr
, "%s: %s: No symbols\n",
241 program_name
, bfd_get_filename (abfd
));
245 /* Read in the dynamic symbols. */
248 slurp_dynamic_symtab (abfd
)
251 asymbol
**sy
= (asymbol
**) NULL
;
254 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
257 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
259 fprintf (stderr
, "%s: %s: not a dynamic object\n",
260 program_name
, bfd_get_filename (abfd
));
264 bfd_fatal (bfd_get_filename (abfd
));
269 sy
= (asymbol
**) xmalloc (storage
);
271 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
273 bfd_fatal (bfd_get_filename (abfd
));
274 if (dynsymcount
== 0)
275 fprintf (stderr
, "%s: %s: No dynamic symbols\n",
276 program_name
, bfd_get_filename (abfd
));
280 /* Filter out (in place) symbols that are useless for disassembly.
281 COUNT is the number of elements in SYMBOLS.
282 Return the number of useful symbols. */
285 remove_useless_symbols (symbols
, count
)
289 register asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
293 asymbol
*sym
= *in_ptr
++;
295 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
297 if (sym
->flags
& (BSF_DEBUGGING
))
299 if (bfd_is_und_section (sym
->section
)
300 || bfd_is_com_section (sym
->section
))
305 return out_ptr
- symbols
;
308 /* Sort symbols into value order. */
311 compare_symbols (ap
, bp
)
315 const asymbol
*a
= *(const asymbol
**)ap
;
316 const asymbol
*b
= *(const asymbol
**)bp
;
318 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
320 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
323 if (a
->section
> b
->section
)
325 else if (a
->section
< b
->section
)
330 /* Sort relocs into address order. */
333 compare_relocs (ap
, bp
)
337 const arelent
*a
= *(const arelent
**)ap
;
338 const arelent
*b
= *(const arelent
**)bp
;
340 if (a
->address
> b
->address
)
342 else if (a
->address
< b
->address
)
345 /* So that associated relocations tied to the same address show up
346 in the correct order, we don't do any further sorting. */
355 /* Print VMA symbolically to INFO if possible. */
358 objdump_print_address (vma
, info
)
360 struct disassemble_info
*info
;
362 /* @@ For relocateable files, should filter out symbols belonging to
363 the wrong section. Unfortunately, not enough information is supplied
364 to this routine to determine the correct section in all cases. */
365 /* @@ Would it speed things up to cache the last two symbols returned,
366 and maybe their address ranges? For many processors, only one memory
367 operand can be present at a time, so the 2-entry cache wouldn't be
368 constantly churned by code doing heavy memory accesses. */
370 /* Indices in `sorted_syms'. */
372 long max
= sorted_symcount
;
375 fprintf_vma (info
->stream
, vma
);
377 if (sorted_symcount
< 1)
380 /* Perform a binary search looking for the closest symbol to the
381 required value. We are searching the range (min, max]. */
382 while (min
+ 1 < max
)
386 thisplace
= (max
+ min
) / 2;
387 sym
= sorted_syms
[thisplace
];
389 if (bfd_asymbol_value (sym
) > vma
)
391 else if (bfd_asymbol_value (sym
) < vma
)
400 /* The symbol we want is now in min, the low end of the range we
405 /* If this symbol isn't global, search for one with the same value
407 bfd_vma val
= bfd_asymbol_value (sorted_syms
[thisplace
]);
409 if (sorted_syms
[thisplace
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
410 for (i
= thisplace
- 1; i
>= 0; i
--)
412 if (bfd_asymbol_value (sorted_syms
[i
]) == val
413 && (!(sorted_syms
[i
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
414 || ((sorted_syms
[thisplace
]->flags
& BSF_DEBUGGING
)
415 && !(sorted_syms
[i
]->flags
& BSF_DEBUGGING
))))
421 if (sorted_syms
[thisplace
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
422 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
424 if (bfd_asymbol_value (sorted_syms
[i
]) == val
425 && (!(sorted_syms
[i
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
426 || ((sorted_syms
[thisplace
]->flags
& BSF_DEBUGGING
)
427 && !(sorted_syms
[i
]->flags
& BSF_DEBUGGING
))))
435 /* If the file is relocateable, and the symbol could be from this
436 section, prefer a symbol from this section over symbols from
437 others, even if the other symbol's value might be closer.
439 Note that this may be wrong for some symbol references if the
440 sections have overlapping memory ranges, but in that case there's
441 no way to tell what's desired without looking at the relocation
443 struct objdump_disasm_info
*aux
;
446 aux
= (struct objdump_disasm_info
*) info
->application_data
;
447 if (sorted_syms
[thisplace
]->section
!= aux
->sec
449 || ((aux
->abfd
->flags
& HAS_RELOC
) != 0
450 && vma
>= bfd_get_section_vma (aux
->abfd
, aux
->sec
)
451 && vma
< (bfd_get_section_vma (aux
->abfd
, aux
->sec
)
452 + bfd_get_section_size_before_reloc (aux
->sec
)))))
454 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
456 if (bfd_asymbol_value (sorted_syms
[i
])
457 != bfd_asymbol_value (sorted_syms
[thisplace
]))
463 if (sorted_syms
[i
]->section
== aux
->sec
)
470 if (sorted_syms
[thisplace
]->section
!= aux
->sec
)
472 /* We didn't find a good symbol with a smaller value.
473 Look for one with a larger value. */
474 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
476 if (sorted_syms
[i
]->section
== aux
->sec
)
486 fprintf (info
->stream
, " <%s", sorted_syms
[thisplace
]->name
);
487 if (bfd_asymbol_value (sorted_syms
[thisplace
]) > vma
)
489 char buf
[30], *p
= buf
;
490 sprintf_vma (buf
, bfd_asymbol_value (sorted_syms
[thisplace
]) - vma
);
493 fprintf (info
->stream
, "-%s", p
);
495 else if (vma
> bfd_asymbol_value (sorted_syms
[thisplace
]))
497 char buf
[30], *p
= buf
;
498 sprintf_vma (buf
, vma
- bfd_asymbol_value (sorted_syms
[thisplace
]));
501 fprintf (info
->stream
, "+%s", p
);
503 fprintf (info
->stream
, ">");
506 /* Hold the last function name and the last line number we displayed
509 static char *prev_functionname
;
510 static unsigned int prev_line
;
512 /* We keep a list of all files that we have seen when doing a
513 dissassembly with source, so that we know how much of the file to
514 display. This can be important for inlined functions. */
516 struct print_file_list
518 struct print_file_list
*next
;
524 static struct print_file_list
*print_files
;
526 /* The number of preceding context lines to show when we start
527 displaying a file for the first time. */
529 #define SHOW_PRECEDING_CONTEXT_LINES (5)
531 /* Skip ahead to a given line in a file, optionally printing each
535 skip_to_line
PARAMS ((struct print_file_list
*, unsigned int, boolean
));
538 skip_to_line (p
, line
, show
)
539 struct print_file_list
*p
;
543 while (p
->line
< line
)
547 if (fgets (buf
, sizeof buf
, p
->f
) == NULL
)
557 if (strchr (buf
, '\n') != NULL
)
562 /* Show the line number, or the source line, in a dissassembly
566 show_line (abfd
, section
, off
)
571 CONST
char *filename
;
572 CONST
char *functionname
;
575 if (! with_line_numbers
&& ! with_source_code
)
578 if (! bfd_find_nearest_line (abfd
, section
, syms
, off
, &filename
,
579 &functionname
, &line
))
582 if (filename
!= NULL
&& *filename
== '\0')
584 if (functionname
!= NULL
&& *functionname
== '\0')
587 if (with_line_numbers
)
589 if (functionname
!= NULL
590 && (prev_functionname
== NULL
591 || strcmp (functionname
, prev_functionname
) != 0))
592 printf ("%s():\n", functionname
);
593 if (line
> 0 && line
!= prev_line
)
594 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
601 struct print_file_list
**pp
, *p
;
603 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
604 if (strcmp ((*pp
)->filename
, filename
) == 0)
610 if (p
!= print_files
)
614 /* We have reencountered a file name which we saw
615 earlier. This implies that either we are dumping out
616 code from an included file, or the same file was
617 linked in more than once. There are two common cases
618 of an included file: inline functions in a header
619 file, and a bison or flex skeleton file. In the
620 former case we want to just start printing (but we
621 back up a few lines to give context); in the latter
622 case we want to continue from where we left off. I
623 can't think of a good way to distinguish the cases,
624 so I used a heuristic based on the file name. */
625 if (strcmp (p
->filename
+ strlen (p
->filename
) - 2, ".h") != 0)
629 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
636 p
->f
= fopen (p
->filename
, "r");
640 skip_to_line (p
, l
, false);
642 if (print_files
->f
!= NULL
)
644 fclose (print_files
->f
);
645 print_files
->f
= NULL
;
651 skip_to_line (p
, line
, true);
653 p
->next
= print_files
;
661 f
= fopen (filename
, "r");
666 p
= ((struct print_file_list
*)
667 xmalloc (sizeof (struct print_file_list
)));
668 p
->filename
= xmalloc (strlen (filename
) + 1);
669 strcpy (p
->filename
, filename
);
673 if (print_files
!= NULL
&& print_files
->f
!= NULL
)
675 fclose (print_files
->f
);
676 print_files
->f
= NULL
;
678 p
->next
= print_files
;
681 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
684 skip_to_line (p
, l
, false);
686 skip_to_line (p
, line
, true);
691 if (functionname
!= NULL
692 && (prev_functionname
== NULL
693 || strcmp (functionname
, prev_functionname
) != 0))
695 if (prev_functionname
!= NULL
)
696 free (prev_functionname
);
697 prev_functionname
= xmalloc (strlen (functionname
) + 1);
698 strcpy (prev_functionname
, functionname
);
701 if (line
> 0 && line
!= prev_line
)
706 disassemble_data (abfd
)
710 unsigned int (*print
) () = 0; /* Old style */
711 disassembler_ftype disassemble_fn
= 0; /* New style */
712 struct disassemble_info disasm_info
;
713 struct objdump_disasm_info aux
;
715 boolean done_dot
= false;
718 prev_functionname
= NULL
;
721 /* We make a copy of syms to sort. We don't want to sort syms
722 because that will screw up the relocs. */
723 sorted_syms
= (asymbol
**) xmalloc (symcount
* sizeof (asymbol
*));
724 memcpy (sorted_syms
, syms
, symcount
* sizeof (asymbol
*));
726 sorted_symcount
= remove_useless_symbols (sorted_syms
, symcount
);
728 /* Sort the symbols into section and symbol order */
729 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
731 INIT_DISASSEMBLE_INFO(disasm_info
, stdout
);
732 disasm_info
.application_data
= (PTR
) &aux
;
734 disasm_info
.print_address_func
= objdump_print_address
;
736 if (machine
!= (char *) NULL
)
738 bfd_arch_info_type
*info
= bfd_scan_arch (machine
);
741 fprintf (stderr
, "%s: Can't use supplied machine %s\n",
746 abfd
->arch_info
= info
;
749 /* See if we can disassemble using bfd. */
751 if (abfd
->arch_info
->disassemble
)
753 print
= abfd
->arch_info
->disassemble
;
757 disassemble_fn
= disassembler (abfd
);
760 fprintf (stderr
, "%s: Can't disassemble for architecture %s\n",
762 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
767 for (section
= abfd
->sections
;
768 section
!= (asection
*) NULL
;
769 section
= section
->next
)
771 bfd_byte
*data
= NULL
;
772 bfd_size_type datasize
= 0;
773 arelent
**relbuf
= NULL
;
774 arelent
**relpp
= NULL
;
775 arelent
**relppend
= NULL
;
777 if ((section
->flags
& SEC_LOAD
) == 0
778 || (! disassemble_all
780 && (section
->flags
& SEC_CODE
) == 0))
782 if (only
!= (char *) NULL
&& strcmp (only
, section
->name
) != 0)
786 && (section
->flags
& SEC_RELOC
) != 0)
790 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
792 bfd_fatal (bfd_get_filename (abfd
));
798 relbuf
= (arelent
**) xmalloc (relsize
);
799 relcount
= bfd_canonicalize_reloc (abfd
, section
, relbuf
, syms
);
801 bfd_fatal (bfd_get_filename (abfd
));
803 /* Sort the relocs by address. */
804 qsort (relbuf
, relcount
, sizeof (arelent
*), compare_relocs
);
807 relppend
= relpp
+ relcount
;
811 printf ("Disassembly of section %s:\n", section
->name
);
813 datasize
= bfd_get_section_size_before_reloc (section
);
817 data
= (bfd_byte
*) xmalloc ((size_t) datasize
);
819 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
822 disasm_info
.buffer
= data
;
823 disasm_info
.buffer_vma
= section
->vma
;
824 disasm_info
.buffer_length
= datasize
;
826 while (i
< disasm_info
.buffer_length
)
829 boolean need_nl
= false;
831 if (data
[i
] == 0 && data
[i
+ 1] == 0 && data
[i
+ 2] == 0 &&
834 if (done_dot
== false)
844 if (with_line_numbers
|| with_source_code
)
845 show_line (abfd
, section
, i
);
846 aux
.require_sec
= true;
847 objdump_print_address (section
->vma
+ i
, &disasm_info
);
848 aux
.require_sec
= false;
854 bytes
= (*disassemble_fn
) (section
->vma
+ i
, &disasm_info
);
861 bytes
= print (section
->vma
+ i
, data
+ i
, stdout
);
870 && (section
->flags
& SEC_RELOC
) != 0)
872 while (relpp
< relppend
873 && ((*relpp
)->address
>= (bfd_vma
) i
874 && (*relpp
)->address
< (bfd_vma
) i
+ bytes
))
877 const char *sym_name
;
881 printf ("\t\tRELOC: ");
883 printf_vma (section
->vma
+ q
->address
);
885 printf (" %s ", q
->howto
->name
);
887 if (q
->sym_ptr_ptr
!= NULL
888 && *q
->sym_ptr_ptr
!= NULL
)
890 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
891 if (sym_name
== NULL
|| *sym_name
== '\0')
895 sym_sec
= bfd_get_section (*q
->sym_ptr_ptr
);
896 sym_name
= bfd_get_section_name (abfd
, sym_sec
);
897 if (sym_name
== NULL
|| *sym_name
== '\0')
898 sym_name
= "*unknown*";
902 printf ("%s", sym_name
);
907 printf_vma (q
->addend
);
929 /* Define a table of stab values and print-strings. We wish the initializer
930 could be a direct-mapped table, but instead we build one the first
940 struct stab_print stab_print
[] = {
941 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
942 #include "aout/stab.def"
947 void dump_section_stabs
PARAMS ((bfd
*abfd
, char *stabsect_name
,
948 char *strsect_name
));
950 /* Dump the stabs sections from an object file that has a section that
951 uses Sun stabs encoding. It has to use some hooks into BFD because
952 string table sections are not normally visible to BFD callers. */
958 /* Allocate and initialize stab name array if first time. */
959 if (stab_name
== NULL
)
963 stab_name
= (char **) xmalloc (256 * sizeof(char *));
964 /* Clear the array. */
965 for (i
= 0; i
< 256; i
++)
967 /* Fill in the defined stabs. */
968 for (i
= 0; *stab_print
[i
].string
; i
++)
969 stab_name
[stab_print
[i
].value
] = stab_print
[i
].string
;
972 dump_section_stabs (abfd
, ".stab", ".stabstr");
973 dump_section_stabs (abfd
, ".stab.excl", ".stab.exclstr");
974 dump_section_stabs (abfd
, ".stab.index", ".stab.indexstr");
975 dump_section_stabs (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
978 static struct internal_nlist
*stabs
;
979 static bfd_size_type stab_size
;
982 static bfd_size_type stabstr_size
;
984 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
985 and string table section STRSECT_NAME into `strtab'.
986 If the section exists and was read, allocate the space and return true.
987 Otherwise return false. */
990 read_section_stabs (abfd
, stabsect_name
, strsect_name
)
995 asection
*stabsect
, *stabstrsect
;
997 stabsect
= bfd_get_section_by_name (abfd
, stabsect_name
);
1000 printf ("No %s section present\n\n", stabsect_name
);
1004 stabstrsect
= bfd_get_section_by_name (abfd
, strsect_name
);
1005 if (0 == stabstrsect
)
1007 fprintf (stderr
, "%s: %s has no %s section\n", program_name
,
1008 bfd_get_filename (abfd
), strsect_name
);
1012 stab_size
= bfd_section_size (abfd
, stabsect
);
1013 stabstr_size
= bfd_section_size (abfd
, stabstrsect
);
1015 stabs
= (struct internal_nlist
*) xmalloc (stab_size
);
1016 strtab
= (char *) xmalloc (stabstr_size
);
1018 if (! bfd_get_section_contents (abfd
, stabsect
, (PTR
) stabs
, 0, stab_size
))
1020 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1021 program_name
, stabsect_name
, bfd_get_filename (abfd
),
1022 bfd_errmsg (bfd_get_error ()));
1028 if (! bfd_get_section_contents (abfd
, stabstrsect
, (PTR
) strtab
, 0,
1031 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1032 program_name
, strsect_name
, bfd_get_filename (abfd
),
1033 bfd_errmsg (bfd_get_error ()));
1042 #define SWAP_SYMBOL(symp, abfd) \
1044 (symp)->n_strx = bfd_h_get_32(abfd, \
1045 (unsigned char *)&(symp)->n_strx); \
1046 (symp)->n_desc = bfd_h_get_16 (abfd, \
1047 (unsigned char *)&(symp)->n_desc); \
1048 (symp)->n_value = bfd_h_get_32 (abfd, \
1049 (unsigned char *)&(symp)->n_value); \
1052 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1053 using string table section STRSECT_NAME (in `strtab'). */
1056 print_section_stabs (abfd
, stabsect_name
, strsect_name
)
1058 char *stabsect_name
;
1062 unsigned file_string_table_offset
= 0, next_file_string_table_offset
= 0;
1063 struct internal_nlist
*stabp
= stabs
,
1064 *stabs_end
= (struct internal_nlist
*) (stab_size
+ (char *) stabs
);
1066 printf ("Contents of %s section:\n\n", stabsect_name
);
1067 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1069 /* Loop through all symbols and print them.
1071 We start the index at -1 because there is a dummy symbol on
1072 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1074 for (i
= -1; stabp
< stabs_end
; stabp
++, i
++)
1076 SWAP_SYMBOL (stabp
, abfd
);
1077 printf ("\n%-6d ", i
);
1078 /* Either print the stab name, or, if unnamed, print its number
1079 again (makes consistent formatting for tools like awk). */
1080 if (stab_name
[stabp
->n_type
])
1081 printf ("%-6s", stab_name
[stabp
->n_type
]);
1082 else if (stabp
->n_type
== N_UNDF
)
1085 printf ("%-6d", stabp
->n_type
);
1086 printf (" %-6d %-6d ", stabp
->n_other
, stabp
->n_desc
);
1087 printf_vma (stabp
->n_value
);
1088 printf (" %-6lu", stabp
->n_strx
);
1090 /* Symbols with type == 0 (N_UNDF) specify the length of the
1091 string table associated with this file. We use that info
1092 to know how to relocate the *next* file's string table indices. */
1094 if (stabp
->n_type
== N_UNDF
)
1096 file_string_table_offset
= next_file_string_table_offset
;
1097 next_file_string_table_offset
+= stabp
->n_value
;
1101 /* Using the (possibly updated) string table offset, print the
1102 string (if any) associated with this symbol. */
1104 if ((stabp
->n_strx
+ file_string_table_offset
) < stabstr_size
)
1105 printf (" %s", &strtab
[stabp
->n_strx
+ file_string_table_offset
]);
1114 dump_section_stabs (abfd
, stabsect_name
, strsect_name
)
1116 char *stabsect_name
;
1121 /* Check for section names for which stabsect_name is a prefix, to
1122 handle .stab0, etc. */
1123 for (s
= abfd
->sections
;
1127 if (strncmp (stabsect_name
, s
->name
, strlen (stabsect_name
)) == 0
1128 && strncmp (strsect_name
, s
->name
, strlen (strsect_name
)) != 0)
1130 if (read_section_stabs (abfd
, s
->name
, strsect_name
))
1132 print_section_stabs (abfd
, s
->name
, strsect_name
);
1141 dump_bfd_header (abfd
)
1146 printf ("architecture: %s, ",
1147 bfd_printable_arch_mach (bfd_get_arch (abfd
),
1148 bfd_get_mach (abfd
)));
1149 printf ("flags 0x%08x:\n", abfd
->flags
);
1151 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1152 PF (HAS_RELOC
, "HAS_RELOC");
1153 PF (EXEC_P
, "EXEC_P");
1154 PF (HAS_LINENO
, "HAS_LINENO");
1155 PF (HAS_DEBUG
, "HAS_DEBUG");
1156 PF (HAS_SYMS
, "HAS_SYMS");
1157 PF (HAS_LOCALS
, "HAS_LOCALS");
1158 PF (DYNAMIC
, "DYNAMIC");
1159 PF (WP_TEXT
, "WP_TEXT");
1160 PF (D_PAGED
, "D_PAGED");
1161 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
1162 printf ("\nstart address 0x");
1163 printf_vma (abfd
->start_address
);
1167 dump_bfd_private_header (abfd
)
1170 bfd_print_private_bfd_data (abfd
, stdout
);
1178 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
1180 bfd_nonfatal (bfd_get_filename (abfd
));
1181 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1183 list_matching_formats (matching
);
1189 printf ("\n%s: file format %s\n", bfd_get_filename (abfd
),
1192 print_arelt_descr (stdout
, abfd
, true);
1193 if (dump_file_header
)
1194 dump_bfd_header (abfd
);
1195 if (dump_private_headers
)
1196 dump_bfd_private_header (abfd
);
1198 if (dump_section_headers
)
1199 dump_headers (abfd
);
1200 if (dump_symtab
|| dump_reloc_info
|| disassemble
)
1202 syms
= slurp_symtab (abfd
);
1204 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
)
1206 dynsyms
= slurp_dynamic_symtab (abfd
);
1209 dump_symbols (abfd
, false);
1210 if (dump_dynamic_symtab
)
1211 dump_symbols (abfd
, true);
1212 if (dump_stab_section_info
)
1214 if (dump_reloc_info
&& ! disassemble
)
1216 if (dump_dynamic_reloc_info
)
1217 dump_dynamic_relocs (abfd
);
1218 if (dump_section_contents
)
1221 disassemble_data (abfd
);
1225 display_file (filename
, target
)
1229 bfd
*file
, *arfile
= (bfd
*) NULL
;
1231 file
= bfd_openr (filename
, target
);
1234 bfd_nonfatal (filename
);
1238 if (bfd_check_format (file
, bfd_archive
) == true)
1240 bfd
*last_arfile
= NULL
;
1242 printf ("In archive %s:\n", bfd_get_filename (file
));
1245 bfd_set_error (bfd_error_no_error
);
1247 arfile
= bfd_openr_next_archived_file (file
, arfile
);
1250 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1252 bfd_nonfatal (bfd_get_filename (file
));
1257 display_bfd (arfile
);
1259 if (last_arfile
!= NULL
)
1260 bfd_close (last_arfile
);
1261 last_arfile
= arfile
;
1264 if (last_arfile
!= NULL
)
1265 bfd_close (last_arfile
);
1273 /* Actually display the various requested regions */
1281 bfd_size_type datasize
= 0;
1284 for (section
= abfd
->sections
; section
!= NULL
; section
=
1289 if (only
== (char *) NULL
||
1290 strcmp (only
, section
->name
) == 0)
1292 if (section
->flags
& SEC_HAS_CONTENTS
)
1294 printf ("Contents of section %s:\n", section
->name
);
1296 if (bfd_section_size (abfd
, section
) == 0)
1298 data
= (bfd_byte
*) xmalloc ((size_t) bfd_section_size (abfd
, section
));
1299 datasize
= bfd_section_size (abfd
, section
);
1302 bfd_get_section_contents (abfd
, section
, (PTR
) data
, 0, bfd_section_size (abfd
, section
));
1304 for (i
= 0; i
< bfd_section_size (abfd
, section
); i
+= onaline
)
1308 printf (" %04lx ", (unsigned long int) (i
+ section
->vma
));
1309 for (j
= i
; j
< i
+ onaline
; j
++)
1311 if (j
< bfd_section_size (abfd
, section
))
1312 printf ("%02x", (unsigned) (data
[j
]));
1320 for (j
= i
; j
< i
+ onaline
; j
++)
1322 if (j
>= bfd_section_size (abfd
, section
))
1325 printf ("%c", isprint (data
[j
]) ? data
[j
] : '.');
1335 /* Should perhaps share code and display with nm? */
1337 dump_symbols (abfd
, dynamic
)
1351 printf ("DYNAMIC SYMBOL TABLE:\n");
1359 printf ("SYMBOL TABLE:\n");
1362 for (count
= 0; count
< max
; count
++)
1366 bfd
*cur_bfd
= bfd_asymbol_bfd(*current
);
1369 bfd_print_symbol (cur_bfd
,
1371 *current
, bfd_print_symbol_all
);
1389 for (a
= abfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1393 if (bfd_is_abs_section (a
))
1395 if (bfd_is_und_section (a
))
1397 if (bfd_is_com_section (a
))
1402 if (strcmp (only
, a
->name
))
1405 else if ((a
->flags
& SEC_RELOC
) == 0)
1408 printf ("RELOCATION RECORDS FOR [%s]:", a
->name
);
1410 relsize
= bfd_get_reloc_upper_bound (abfd
, a
);
1412 bfd_fatal (bfd_get_filename (abfd
));
1416 printf (" (none)\n\n");
1420 relpp
= (arelent
**) xmalloc (relsize
);
1421 relcount
= bfd_canonicalize_reloc (abfd
, a
, relpp
, syms
);
1423 bfd_fatal (bfd_get_filename (abfd
));
1424 else if (relcount
== 0)
1426 printf (" (none)\n\n");
1431 dump_reloc_set (abfd
, relpp
, relcount
);
1440 dump_dynamic_relocs (abfd
)
1447 printf ("DYNAMIC RELOCATION RECORDS");
1449 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
1451 bfd_fatal (bfd_get_filename (abfd
));
1455 printf (" (none)\n\n");
1459 relpp
= (arelent
**) xmalloc (relsize
);
1460 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
1462 bfd_fatal (bfd_get_filename (abfd
));
1463 else if (relcount
== 0)
1465 printf (" (none)\n\n");
1470 dump_reloc_set (abfd
, relpp
, relcount
);
1478 dump_reloc_set (abfd
, relpp
, relcount
)
1485 /* Get column headers lined up reasonably. */
1491 sprintf_vma (buf
, (bfd_vma
) -1);
1492 width
= strlen (buf
) - 7;
1494 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
1497 for (p
= relpp
; relcount
&& *p
!= (arelent
*) NULL
; p
++, relcount
--)
1500 CONST
char *sym_name
;
1501 CONST
char *section_name
;
1503 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
1505 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
1506 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
1511 section_name
= NULL
;
1515 printf_vma (q
->address
);
1516 printf (" %-16s %s",
1522 if (section_name
== (CONST
char *) NULL
)
1523 section_name
= "*unknown*";
1524 printf_vma (q
->address
);
1525 printf (" %-16s [%s]",
1532 printf_vma (q
->addend
);
1538 /* The length of the longest architecture name + 1. */
1539 #define LONGEST_ARCH sizeof("rs6000:6000")
1545 /* List the targets that BFD is configured to support, each followed
1546 by its endianness and the architectures it supports. */
1549 display_target_list ()
1551 extern char *tmpnam ();
1552 extern bfd_target
*bfd_target_vector
[];
1553 char tmparg
[L_tmpnam
];
1557 dummy_name
= tmpnam (tmparg
);
1558 for (t
= 0; bfd_target_vector
[t
]; t
++)
1560 bfd_target
*p
= bfd_target_vector
[t
];
1561 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1564 printf ("%s\n (header %s, data %s)\n", p
->name
,
1565 p
->header_byteorder_big_p
? "big endian" : "little endian",
1566 p
->byteorder_big_p
? "big endian" : "little endian");
1570 bfd_nonfatal (dummy_name
);
1574 if (! bfd_set_format (abfd
, bfd_object
))
1576 if (bfd_get_error () != bfd_error_invalid_operation
)
1577 bfd_nonfatal (p
->name
);
1581 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1582 if (bfd_set_arch_mach (abfd
, (enum bfd_architecture
) a
, 0))
1584 bfd_printable_arch_mach ((enum bfd_architecture
) a
, 0));
1586 unlink (dummy_name
);
1589 /* Print a table showing which architectures are supported for entries
1590 FIRST through LAST-1 of bfd_target_vector (targets across,
1591 architectures down). */
1594 display_info_table (first
, last
)
1598 extern bfd_target
*bfd_target_vector
[];
1599 extern char *tmpnam ();
1600 char tmparg
[L_tmpnam
];
1604 /* Print heading of target names. */
1605 printf ("\n%*s", (int) LONGEST_ARCH
, " ");
1606 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1607 printf ("%s ", bfd_target_vector
[t
]->name
);
1610 dummy_name
= tmpnam (tmparg
);
1611 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1612 if (strcmp (bfd_printable_arch_mach (a
, 0), "UNKNOWN!") != 0)
1614 printf ("%*s ", (int) LONGEST_ARCH
- 1,
1615 bfd_printable_arch_mach (a
, 0));
1616 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1618 bfd_target
*p
= bfd_target_vector
[t
];
1620 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1624 bfd_nonfatal (p
->name
);
1630 if (! bfd_set_format (abfd
, bfd_object
))
1632 if (bfd_get_error () != bfd_error_invalid_operation
)
1633 bfd_nonfatal (p
->name
);
1640 if (! bfd_set_arch_mach (abfd
, a
, 0))
1645 printf ("%s ", p
->name
);
1648 int l
= strlen (p
->name
);
1656 unlink (dummy_name
);
1659 /* Print tables of all the target-architecture combinations that
1660 BFD has been configured to support. */
1663 display_target_tables ()
1666 extern bfd_target
*bfd_target_vector
[];
1668 extern char *getenv ();
1671 colum
= getenv ("COLUMNS");
1673 columns
= atoi (colum
);
1678 while (bfd_target_vector
[t
] != NULL
)
1682 wid
= LONGEST_ARCH
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1684 while (wid
< columns
&& bfd_target_vector
[t
] != NULL
)
1688 newwid
= wid
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1689 if (newwid
>= columns
)
1694 display_info_table (oldt
, t
);
1701 printf ("BFD header file version %s\n", BFD_VERSION
);
1702 display_target_list ();
1703 display_target_tables ();
1712 char *target
= default_target
;
1713 boolean seenflag
= false;
1715 program_name
= *argv
;
1716 xmalloc_set_program_name (program_name
);
1718 START_PROGRESS (program_name
, 0);
1722 while ((c
= getopt_long (argc
, argv
, "pib:m:VdDlfahrRtTxsSj:w", long_options
,
1730 break; /* we've been given a long option */
1738 with_line_numbers
= 1;
1744 dump_file_header
= true;
1747 formats_info
= true;
1750 dump_private_headers
= 1;
1753 dump_private_headers
= 1;
1755 dump_reloc_info
= 1;
1756 dump_file_header
= true;
1758 dump_section_headers
= 1;
1764 dump_dynamic_symtab
= 1;
1770 disassemble
= disassemble_all
= true;
1774 with_source_code
= true;
1777 dump_section_contents
= 1;
1780 dump_reloc_info
= 1;
1783 dump_dynamic_reloc_info
= 1;
1789 dump_section_headers
= 1;
1806 printf ("GNU %s version %s\n", program_name
, program_version
);
1810 if (seenflag
== false)
1820 display_file ("a.out", target
);
1822 for (; optind
< argc
;)
1823 display_file (argv
[optind
++], target
);
1826 END_PROGRESS (program_name
);