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 */
60 bfd_vma start_address
= (bfd_vma
) -1; /* --start-address */
61 bfd_vma stop_address
= (bfd_vma
) -1; /* --stop-address */
63 /* Extra info to pass to the disassembler address printing function. */
64 struct objdump_disasm_info
{
70 /* Architecture to disassemble for, or default if NULL. */
71 char *machine
= (char *) NULL
;
73 /* The symbol table. */
76 /* Number of symbols in `syms'. */
79 /* The sorted symbol table. */
80 asymbol
**sorted_syms
;
82 /* Number of symbols in `sorted_syms'. */
83 long sorted_symcount
= 0;
85 /* The dynamic symbol table. */
88 /* Number of symbols in `dynsyms'. */
91 /* Forward declarations. */
94 display_file
PARAMS ((char *filename
, char *target
));
97 dump_data
PARAMS ((bfd
*abfd
));
100 dump_relocs
PARAMS ((bfd
*abfd
));
103 dump_dynamic_relocs
PARAMS ((bfd
* abfd
));
106 dump_reloc_set
PARAMS ((bfd
*, arelent
**, long));
109 dump_symbols
PARAMS ((bfd
*abfd
, boolean dynamic
));
112 display_bfd
PARAMS ((bfd
*abfd
));
115 objdump_print_address
PARAMS ((bfd_vma
, struct disassemble_info
*));
118 show_line
PARAMS ((bfd
*, asection
*, bfd_vma
));
121 usage (stream
, status
)
126 Usage: %s [-ahifdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
127 [--archive-headers] [--target=bfdname] [--disassemble]\n\
128 [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
129 [--info] [--section=section-name] [--line-numbers] [--source]\n",
132 [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
133 [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
134 [--wide] [--version] [--help] [--private-headers]\n\
135 [--start-address=addr] [--stop-address=addr] objfile...\n\
136 at least one option besides -l (--line-numbers) must be given\n");
137 list_supported_targets (program_name
, stream
);
141 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
143 #define OPTION_START_ADDRESS (150)
144 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
146 static struct option long_options
[]=
148 {"all-headers", no_argument
, NULL
, 'x'},
149 {"private-headers", no_argument
, NULL
, 'p'},
150 {"architecture", required_argument
, NULL
, 'm'},
151 {"archive-headers", no_argument
, NULL
, 'a'},
152 {"disassemble", no_argument
, NULL
, 'd'},
153 {"disassemble-all", no_argument
, NULL
, 'D'},
154 {"dynamic-reloc", no_argument
, NULL
, 'R'},
155 {"dynamic-syms", no_argument
, NULL
, 'T'},
156 {"file-headers", no_argument
, NULL
, 'f'},
157 {"full-contents", no_argument
, NULL
, 's'},
158 {"headers", no_argument
, NULL
, 'h'},
159 {"help", no_argument
, NULL
, 'H'},
160 {"info", no_argument
, NULL
, 'i'},
161 {"line-numbers", no_argument
, NULL
, 'l'},
162 {"reloc", no_argument
, NULL
, 'r'},
163 {"section", required_argument
, NULL
, 'j'},
164 {"section-headers", no_argument
, NULL
, 'h'},
165 {"source", no_argument
, NULL
, 'S'},
166 {"stabs", no_argument
, &dump_stab_section_info
, 1},
167 {"start-address", required_argument
, NULL
, OPTION_START_ADDRESS
},
168 {"stop-address", required_argument
, NULL
, OPTION_STOP_ADDRESS
},
169 {"syms", no_argument
, NULL
, 't'},
170 {"target", required_argument
, NULL
, 'b'},
171 {"version", no_argument
, &show_version
, 1},
172 {"wide", no_argument
, &wide_output
, 'w'},
173 {0, no_argument
, 0, 0}
177 dump_section_header (abfd
, section
, ignored
)
185 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
188 printf ("SECTION %d [%s]\t: size %08x",
191 (unsigned) bfd_get_section_size_before_reloc (section
));
193 printf_vma (section
->vma
);
195 printf_vma (section
->lma
);
196 printf (" align 2**%u%s ",
197 section
->alignment_power
, (wide_output
) ? "" : "\n");
198 PF (SEC_ALLOC
, "ALLOC");
199 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
200 PF (SEC_CONSTRUCTOR_TEXT
, "CONSTRUCTOR TEXT");
201 PF (SEC_CONSTRUCTOR_DATA
, "CONSTRUCTOR DATA");
202 PF (SEC_CONSTRUCTOR_BSS
, "CONSTRUCTOR BSS");
203 PF (SEC_LOAD
, "LOAD");
204 PF (SEC_RELOC
, "RELOC");
206 PF (SEC_BALIGN
, "BALIGN");
208 PF (SEC_READONLY
, "READONLY");
209 PF (SEC_CODE
, "CODE");
210 PF (SEC_DATA
, "DATA");
212 PF (SEC_DEBUGGING
, "DEBUGGING");
213 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
222 bfd_map_over_sections (abfd
, dump_section_header
, (PTR
) NULL
);
229 asymbol
**sy
= (asymbol
**) NULL
;
232 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
234 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd
));
238 storage
= bfd_get_symtab_upper_bound (abfd
);
240 bfd_fatal (bfd_get_filename (abfd
));
244 sy
= (asymbol
**) xmalloc (storage
);
246 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
248 bfd_fatal (bfd_get_filename (abfd
));
250 fprintf (stderr
, "%s: %s: No symbols\n",
251 program_name
, bfd_get_filename (abfd
));
255 /* Read in the dynamic symbols. */
258 slurp_dynamic_symtab (abfd
)
261 asymbol
**sy
= (asymbol
**) NULL
;
264 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
267 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
269 fprintf (stderr
, "%s: %s: not a dynamic object\n",
270 program_name
, bfd_get_filename (abfd
));
274 bfd_fatal (bfd_get_filename (abfd
));
279 sy
= (asymbol
**) xmalloc (storage
);
281 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
283 bfd_fatal (bfd_get_filename (abfd
));
284 if (dynsymcount
== 0)
285 fprintf (stderr
, "%s: %s: No dynamic symbols\n",
286 program_name
, bfd_get_filename (abfd
));
290 /* Filter out (in place) symbols that are useless for disassembly.
291 COUNT is the number of elements in SYMBOLS.
292 Return the number of useful symbols. */
295 remove_useless_symbols (symbols
, count
)
299 register asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
303 asymbol
*sym
= *in_ptr
++;
305 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
307 if (sym
->flags
& (BSF_DEBUGGING
))
309 if (bfd_is_und_section (sym
->section
)
310 || bfd_is_com_section (sym
->section
))
315 return out_ptr
- symbols
;
318 /* Sort symbols into value order. */
321 compare_symbols (ap
, bp
)
325 const asymbol
*a
= *(const asymbol
**)ap
;
326 const asymbol
*b
= *(const asymbol
**)bp
;
328 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
330 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
333 if (a
->section
> b
->section
)
335 else if (a
->section
< b
->section
)
340 /* Sort relocs into address order. */
343 compare_relocs (ap
, bp
)
347 const arelent
*a
= *(const arelent
**)ap
;
348 const arelent
*b
= *(const arelent
**)bp
;
350 if (a
->address
> b
->address
)
352 else if (a
->address
< b
->address
)
355 /* So that associated relocations tied to the same address show up
356 in the correct order, we don't do any further sorting. */
365 /* Print VMA symbolically to INFO if possible. */
368 objdump_print_address (vma
, info
)
370 struct disassemble_info
*info
;
372 /* @@ For relocateable files, should filter out symbols belonging to
373 the wrong section. Unfortunately, not enough information is supplied
374 to this routine to determine the correct section in all cases. */
375 /* @@ Would it speed things up to cache the last two symbols returned,
376 and maybe their address ranges? For many processors, only one memory
377 operand can be present at a time, so the 2-entry cache wouldn't be
378 constantly churned by code doing heavy memory accesses. */
380 /* Indices in `sorted_syms'. */
382 long max
= sorted_symcount
;
385 fprintf_vma (info
->stream
, vma
);
387 if (sorted_symcount
< 1)
390 /* Perform a binary search looking for the closest symbol to the
391 required value. We are searching the range (min, max]. */
392 while (min
+ 1 < max
)
396 thisplace
= (max
+ min
) / 2;
397 sym
= sorted_syms
[thisplace
];
399 if (bfd_asymbol_value (sym
) > vma
)
401 else if (bfd_asymbol_value (sym
) < vma
)
410 /* The symbol we want is now in min, the low end of the range we
415 /* If this symbol isn't global, search for one with the same value
417 bfd_vma val
= bfd_asymbol_value (sorted_syms
[thisplace
]);
419 if (sorted_syms
[thisplace
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
420 for (i
= thisplace
- 1; i
>= 0; i
--)
422 if (bfd_asymbol_value (sorted_syms
[i
]) == val
423 && (!(sorted_syms
[i
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
424 || ((sorted_syms
[thisplace
]->flags
& BSF_DEBUGGING
)
425 && !(sorted_syms
[i
]->flags
& BSF_DEBUGGING
))))
431 if (sorted_syms
[thisplace
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
432 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
434 if (bfd_asymbol_value (sorted_syms
[i
]) == val
435 && (!(sorted_syms
[i
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
436 || ((sorted_syms
[thisplace
]->flags
& BSF_DEBUGGING
)
437 && !(sorted_syms
[i
]->flags
& BSF_DEBUGGING
))))
445 /* If the file is relocateable, and the symbol could be from this
446 section, prefer a symbol from this section over symbols from
447 others, even if the other symbol's value might be closer.
449 Note that this may be wrong for some symbol references if the
450 sections have overlapping memory ranges, but in that case there's
451 no way to tell what's desired without looking at the relocation
453 struct objdump_disasm_info
*aux
;
456 aux
= (struct objdump_disasm_info
*) info
->application_data
;
457 if (sorted_syms
[thisplace
]->section
!= aux
->sec
459 || ((aux
->abfd
->flags
& HAS_RELOC
) != 0
460 && vma
>= bfd_get_section_vma (aux
->abfd
, aux
->sec
)
461 && vma
< (bfd_get_section_vma (aux
->abfd
, aux
->sec
)
462 + bfd_get_section_size_before_reloc (aux
->sec
)))))
464 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
466 if (bfd_asymbol_value (sorted_syms
[i
])
467 != bfd_asymbol_value (sorted_syms
[thisplace
]))
473 if (sorted_syms
[i
]->section
== aux
->sec
)
480 if (sorted_syms
[thisplace
]->section
!= aux
->sec
)
482 /* We didn't find a good symbol with a smaller value.
483 Look for one with a larger value. */
484 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
486 if (sorted_syms
[i
]->section
== aux
->sec
)
496 fprintf (info
->stream
, " <%s", sorted_syms
[thisplace
]->name
);
497 if (bfd_asymbol_value (sorted_syms
[thisplace
]) > vma
)
499 char buf
[30], *p
= buf
;
500 sprintf_vma (buf
, bfd_asymbol_value (sorted_syms
[thisplace
]) - vma
);
503 fprintf (info
->stream
, "-%s", p
);
505 else if (vma
> bfd_asymbol_value (sorted_syms
[thisplace
]))
507 char buf
[30], *p
= buf
;
508 sprintf_vma (buf
, vma
- bfd_asymbol_value (sorted_syms
[thisplace
]));
511 fprintf (info
->stream
, "+%s", p
);
513 fprintf (info
->stream
, ">");
516 /* Hold the last function name and the last line number we displayed
519 static char *prev_functionname
;
520 static unsigned int prev_line
;
522 /* We keep a list of all files that we have seen when doing a
523 dissassembly with source, so that we know how much of the file to
524 display. This can be important for inlined functions. */
526 struct print_file_list
528 struct print_file_list
*next
;
534 static struct print_file_list
*print_files
;
536 /* The number of preceding context lines to show when we start
537 displaying a file for the first time. */
539 #define SHOW_PRECEDING_CONTEXT_LINES (5)
541 /* Skip ahead to a given line in a file, optionally printing each
545 skip_to_line
PARAMS ((struct print_file_list
*, unsigned int, boolean
));
548 skip_to_line (p
, line
, show
)
549 struct print_file_list
*p
;
553 while (p
->line
< line
)
557 if (fgets (buf
, sizeof buf
, p
->f
) == NULL
)
567 if (strchr (buf
, '\n') != NULL
)
572 /* Show the line number, or the source line, in a dissassembly
576 show_line (abfd
, section
, off
)
581 CONST
char *filename
;
582 CONST
char *functionname
;
585 if (! with_line_numbers
&& ! with_source_code
)
588 if (! bfd_find_nearest_line (abfd
, section
, syms
, off
, &filename
,
589 &functionname
, &line
))
592 if (filename
!= NULL
&& *filename
== '\0')
594 if (functionname
!= NULL
&& *functionname
== '\0')
597 if (with_line_numbers
)
599 if (functionname
!= NULL
600 && (prev_functionname
== NULL
601 || strcmp (functionname
, prev_functionname
) != 0))
602 printf ("%s():\n", functionname
);
603 if (line
> 0 && line
!= prev_line
)
604 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
611 struct print_file_list
**pp
, *p
;
613 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
614 if (strcmp ((*pp
)->filename
, filename
) == 0)
620 if (p
!= print_files
)
624 /* We have reencountered a file name which we saw
625 earlier. This implies that either we are dumping out
626 code from an included file, or the same file was
627 linked in more than once. There are two common cases
628 of an included file: inline functions in a header
629 file, and a bison or flex skeleton file. In the
630 former case we want to just start printing (but we
631 back up a few lines to give context); in the latter
632 case we want to continue from where we left off. I
633 can't think of a good way to distinguish the cases,
634 so I used a heuristic based on the file name. */
635 if (strcmp (p
->filename
+ strlen (p
->filename
) - 2, ".h") != 0)
639 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
646 p
->f
= fopen (p
->filename
, "r");
650 skip_to_line (p
, l
, false);
652 if (print_files
->f
!= NULL
)
654 fclose (print_files
->f
);
655 print_files
->f
= NULL
;
661 skip_to_line (p
, line
, true);
663 p
->next
= print_files
;
671 f
= fopen (filename
, "r");
676 p
= ((struct print_file_list
*)
677 xmalloc (sizeof (struct print_file_list
)));
678 p
->filename
= xmalloc (strlen (filename
) + 1);
679 strcpy (p
->filename
, filename
);
683 if (print_files
!= NULL
&& print_files
->f
!= NULL
)
685 fclose (print_files
->f
);
686 print_files
->f
= NULL
;
688 p
->next
= print_files
;
691 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
694 skip_to_line (p
, l
, false);
696 skip_to_line (p
, line
, true);
701 if (functionname
!= NULL
702 && (prev_functionname
== NULL
703 || strcmp (functionname
, prev_functionname
) != 0))
705 if (prev_functionname
!= NULL
)
706 free (prev_functionname
);
707 prev_functionname
= xmalloc (strlen (functionname
) + 1);
708 strcpy (prev_functionname
, functionname
);
711 if (line
> 0 && line
!= prev_line
)
716 disassemble_data (abfd
)
720 unsigned int (*print
) () = 0; /* Old style */
721 disassembler_ftype disassemble_fn
= 0; /* New style */
722 struct disassemble_info disasm_info
;
723 struct objdump_disasm_info aux
;
725 boolean done_dot
= false;
728 prev_functionname
= NULL
;
731 /* We make a copy of syms to sort. We don't want to sort syms
732 because that will screw up the relocs. */
733 sorted_syms
= (asymbol
**) xmalloc (symcount
* sizeof (asymbol
*));
734 memcpy (sorted_syms
, syms
, symcount
* sizeof (asymbol
*));
736 sorted_symcount
= remove_useless_symbols (sorted_syms
, symcount
);
738 /* Sort the symbols into section and symbol order */
739 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
741 INIT_DISASSEMBLE_INFO(disasm_info
, stdout
);
742 disasm_info
.application_data
= (PTR
) &aux
;
744 disasm_info
.print_address_func
= objdump_print_address
;
746 if (machine
!= (char *) NULL
)
748 bfd_arch_info_type
*info
= bfd_scan_arch (machine
);
751 fprintf (stderr
, "%s: Can't use supplied machine %s\n",
756 abfd
->arch_info
= info
;
759 /* See if we can disassemble using bfd. */
761 if (abfd
->arch_info
->disassemble
)
763 print
= abfd
->arch_info
->disassemble
;
767 disassemble_fn
= disassembler (abfd
);
770 fprintf (stderr
, "%s: Can't disassemble for architecture %s\n",
772 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
777 for (section
= abfd
->sections
;
778 section
!= (asection
*) NULL
;
779 section
= section
->next
)
781 bfd_byte
*data
= NULL
;
782 bfd_size_type datasize
= 0;
783 arelent
**relbuf
= NULL
;
784 arelent
**relpp
= NULL
;
785 arelent
**relppend
= NULL
;
788 if ((section
->flags
& SEC_LOAD
) == 0
789 || (! disassemble_all
791 && (section
->flags
& SEC_CODE
) == 0))
793 if (only
!= (char *) NULL
&& strcmp (only
, section
->name
) != 0)
797 && (section
->flags
& SEC_RELOC
) != 0)
801 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
803 bfd_fatal (bfd_get_filename (abfd
));
809 relbuf
= (arelent
**) xmalloc (relsize
);
810 relcount
= bfd_canonicalize_reloc (abfd
, section
, relbuf
, syms
);
812 bfd_fatal (bfd_get_filename (abfd
));
814 /* Sort the relocs by address. */
815 qsort (relbuf
, relcount
, sizeof (arelent
*), compare_relocs
);
818 relppend
= relpp
+ relcount
;
822 printf ("Disassembly of section %s:\n", section
->name
);
824 datasize
= bfd_get_section_size_before_reloc (section
);
828 data
= (bfd_byte
*) xmalloc ((size_t) datasize
);
830 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
833 disasm_info
.buffer
= data
;
834 disasm_info
.buffer_vma
= section
->vma
;
835 disasm_info
.buffer_length
= datasize
;
836 if (start_address
== (bfd_vma
) -1
837 || start_address
< disasm_info
.buffer_vma
)
840 i
= start_address
- disasm_info
.buffer_vma
;
841 if (stop_address
== (bfd_vma
) -1)
845 if (stop_address
< disasm_info
.buffer_vma
)
848 stop
= stop_address
- disasm_info
.buffer_vma
;
849 if (stop
> disasm_info
.buffer_length
)
850 stop
= disasm_info
.buffer_length
;
855 boolean need_nl
= false;
857 if (data
[i
] == 0 && data
[i
+ 1] == 0 && data
[i
+ 2] == 0 &&
860 if (done_dot
== false)
870 if (with_line_numbers
|| with_source_code
)
871 show_line (abfd
, section
, i
);
872 aux
.require_sec
= true;
873 objdump_print_address (section
->vma
+ i
, &disasm_info
);
874 aux
.require_sec
= false;
880 bytes
= (*disassemble_fn
) (section
->vma
+ i
, &disasm_info
);
887 bytes
= print (section
->vma
+ i
, data
+ i
, stdout
);
896 && (section
->flags
& SEC_RELOC
) != 0)
898 while (relpp
< relppend
899 && ((*relpp
)->address
>= (bfd_vma
) i
900 && (*relpp
)->address
< (bfd_vma
) i
+ bytes
))
903 const char *sym_name
;
907 printf ("\t\tRELOC: ");
909 printf_vma (section
->vma
+ q
->address
);
911 printf (" %s ", q
->howto
->name
);
913 if (q
->sym_ptr_ptr
!= NULL
914 && *q
->sym_ptr_ptr
!= NULL
)
916 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
917 if (sym_name
== NULL
|| *sym_name
== '\0')
921 sym_sec
= bfd_get_section (*q
->sym_ptr_ptr
);
922 sym_name
= bfd_get_section_name (abfd
, sym_sec
);
923 if (sym_name
== NULL
|| *sym_name
== '\0')
924 sym_name
= "*unknown*";
928 printf ("%s", sym_name
);
933 printf_vma (q
->addend
);
955 /* Define a table of stab values and print-strings. We wish the initializer
956 could be a direct-mapped table, but instead we build one the first
966 struct stab_print stab_print
[] = {
967 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
968 #include "aout/stab.def"
973 void dump_section_stabs
PARAMS ((bfd
*abfd
, char *stabsect_name
,
974 char *strsect_name
));
976 /* Dump the stabs sections from an object file that has a section that
977 uses Sun stabs encoding. It has to use some hooks into BFD because
978 string table sections are not normally visible to BFD callers. */
984 /* Allocate and initialize stab name array if first time. */
985 if (stab_name
== NULL
)
989 stab_name
= (char **) xmalloc (256 * sizeof(char *));
990 /* Clear the array. */
991 for (i
= 0; i
< 256; i
++)
993 /* Fill in the defined stabs. */
994 for (i
= 0; *stab_print
[i
].string
; i
++)
995 stab_name
[stab_print
[i
].value
] = stab_print
[i
].string
;
998 dump_section_stabs (abfd
, ".stab", ".stabstr");
999 dump_section_stabs (abfd
, ".stab.excl", ".stab.exclstr");
1000 dump_section_stabs (abfd
, ".stab.index", ".stab.indexstr");
1001 dump_section_stabs (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1004 static struct internal_nlist
*stabs
;
1005 static bfd_size_type stab_size
;
1007 static char *strtab
;
1008 static bfd_size_type stabstr_size
;
1010 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1011 and string table section STRSECT_NAME into `strtab'.
1012 If the section exists and was read, allocate the space and return true.
1013 Otherwise return false. */
1016 read_section_stabs (abfd
, stabsect_name
, strsect_name
)
1018 char *stabsect_name
;
1021 asection
*stabsect
, *stabstrsect
;
1023 stabsect
= bfd_get_section_by_name (abfd
, stabsect_name
);
1026 printf ("No %s section present\n\n", stabsect_name
);
1030 stabstrsect
= bfd_get_section_by_name (abfd
, strsect_name
);
1031 if (0 == stabstrsect
)
1033 fprintf (stderr
, "%s: %s has no %s section\n", program_name
,
1034 bfd_get_filename (abfd
), strsect_name
);
1038 stab_size
= bfd_section_size (abfd
, stabsect
);
1039 stabstr_size
= bfd_section_size (abfd
, stabstrsect
);
1041 stabs
= (struct internal_nlist
*) xmalloc (stab_size
);
1042 strtab
= (char *) xmalloc (stabstr_size
);
1044 if (! bfd_get_section_contents (abfd
, stabsect
, (PTR
) stabs
, 0, stab_size
))
1046 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1047 program_name
, stabsect_name
, bfd_get_filename (abfd
),
1048 bfd_errmsg (bfd_get_error ()));
1054 if (! bfd_get_section_contents (abfd
, stabstrsect
, (PTR
) strtab
, 0,
1057 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1058 program_name
, strsect_name
, bfd_get_filename (abfd
),
1059 bfd_errmsg (bfd_get_error ()));
1068 #define SWAP_SYMBOL(symp, abfd) \
1070 (symp)->n_strx = bfd_h_get_32(abfd, \
1071 (unsigned char *)&(symp)->n_strx); \
1072 (symp)->n_desc = bfd_h_get_16 (abfd, \
1073 (unsigned char *)&(symp)->n_desc); \
1074 (symp)->n_value = bfd_h_get_32 (abfd, \
1075 (unsigned char *)&(symp)->n_value); \
1078 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1079 using string table section STRSECT_NAME (in `strtab'). */
1082 print_section_stabs (abfd
, stabsect_name
, strsect_name
)
1084 char *stabsect_name
;
1088 unsigned file_string_table_offset
= 0, next_file_string_table_offset
= 0;
1089 struct internal_nlist
*stabp
= stabs
,
1090 *stabs_end
= (struct internal_nlist
*) (stab_size
+ (char *) stabs
);
1092 printf ("Contents of %s section:\n\n", stabsect_name
);
1093 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1095 /* Loop through all symbols and print them.
1097 We start the index at -1 because there is a dummy symbol on
1098 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1100 for (i
= -1; stabp
< stabs_end
; stabp
++, i
++)
1102 SWAP_SYMBOL (stabp
, abfd
);
1103 printf ("\n%-6d ", i
);
1104 /* Either print the stab name, or, if unnamed, print its number
1105 again (makes consistent formatting for tools like awk). */
1106 if (stab_name
[stabp
->n_type
])
1107 printf ("%-6s", stab_name
[stabp
->n_type
]);
1108 else if (stabp
->n_type
== N_UNDF
)
1111 printf ("%-6d", stabp
->n_type
);
1112 printf (" %-6d %-6d ", stabp
->n_other
, stabp
->n_desc
);
1113 printf_vma (stabp
->n_value
);
1114 printf (" %-6lu", stabp
->n_strx
);
1116 /* Symbols with type == 0 (N_UNDF) specify the length of the
1117 string table associated with this file. We use that info
1118 to know how to relocate the *next* file's string table indices. */
1120 if (stabp
->n_type
== N_UNDF
)
1122 file_string_table_offset
= next_file_string_table_offset
;
1123 next_file_string_table_offset
+= stabp
->n_value
;
1127 /* Using the (possibly updated) string table offset, print the
1128 string (if any) associated with this symbol. */
1130 if ((stabp
->n_strx
+ file_string_table_offset
) < stabstr_size
)
1131 printf (" %s", &strtab
[stabp
->n_strx
+ file_string_table_offset
]);
1140 dump_section_stabs (abfd
, stabsect_name
, strsect_name
)
1142 char *stabsect_name
;
1147 /* Check for section names for which stabsect_name is a prefix, to
1148 handle .stab0, etc. */
1149 for (s
= abfd
->sections
;
1153 if (strncmp (stabsect_name
, s
->name
, strlen (stabsect_name
)) == 0
1154 && strncmp (strsect_name
, s
->name
, strlen (strsect_name
)) != 0)
1156 if (read_section_stabs (abfd
, s
->name
, strsect_name
))
1158 print_section_stabs (abfd
, s
->name
, strsect_name
);
1167 dump_bfd_header (abfd
)
1172 printf ("architecture: %s, ",
1173 bfd_printable_arch_mach (bfd_get_arch (abfd
),
1174 bfd_get_mach (abfd
)));
1175 printf ("flags 0x%08x:\n", abfd
->flags
);
1177 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1178 PF (HAS_RELOC
, "HAS_RELOC");
1179 PF (EXEC_P
, "EXEC_P");
1180 PF (HAS_LINENO
, "HAS_LINENO");
1181 PF (HAS_DEBUG
, "HAS_DEBUG");
1182 PF (HAS_SYMS
, "HAS_SYMS");
1183 PF (HAS_LOCALS
, "HAS_LOCALS");
1184 PF (DYNAMIC
, "DYNAMIC");
1185 PF (WP_TEXT
, "WP_TEXT");
1186 PF (D_PAGED
, "D_PAGED");
1187 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
1188 printf ("\nstart address 0x");
1189 printf_vma (abfd
->start_address
);
1193 dump_bfd_private_header (abfd
)
1196 bfd_print_private_bfd_data (abfd
, stdout
);
1204 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
1206 bfd_nonfatal (bfd_get_filename (abfd
));
1207 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1209 list_matching_formats (matching
);
1215 printf ("\n%s: file format %s\n", bfd_get_filename (abfd
),
1218 print_arelt_descr (stdout
, abfd
, true);
1219 if (dump_file_header
)
1220 dump_bfd_header (abfd
);
1221 if (dump_private_headers
)
1222 dump_bfd_private_header (abfd
);
1224 if (dump_section_headers
)
1225 dump_headers (abfd
);
1226 if (dump_symtab
|| dump_reloc_info
|| disassemble
)
1228 syms
= slurp_symtab (abfd
);
1230 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
)
1232 dynsyms
= slurp_dynamic_symtab (abfd
);
1235 dump_symbols (abfd
, false);
1236 if (dump_dynamic_symtab
)
1237 dump_symbols (abfd
, true);
1238 if (dump_stab_section_info
)
1240 if (dump_reloc_info
&& ! disassemble
)
1242 if (dump_dynamic_reloc_info
)
1243 dump_dynamic_relocs (abfd
);
1244 if (dump_section_contents
)
1247 disassemble_data (abfd
);
1251 display_file (filename
, target
)
1255 bfd
*file
, *arfile
= (bfd
*) NULL
;
1257 file
= bfd_openr (filename
, target
);
1260 bfd_nonfatal (filename
);
1264 if (bfd_check_format (file
, bfd_archive
) == true)
1266 bfd
*last_arfile
= NULL
;
1268 printf ("In archive %s:\n", bfd_get_filename (file
));
1271 bfd_set_error (bfd_error_no_error
);
1273 arfile
= bfd_openr_next_archived_file (file
, arfile
);
1276 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1278 bfd_nonfatal (bfd_get_filename (file
));
1283 display_bfd (arfile
);
1285 if (last_arfile
!= NULL
)
1286 bfd_close (last_arfile
);
1287 last_arfile
= arfile
;
1290 if (last_arfile
!= NULL
)
1291 bfd_close (last_arfile
);
1299 /* Actually display the various requested regions */
1307 bfd_size_type datasize
= 0;
1309 bfd_size_type start
, stop
;
1311 for (section
= abfd
->sections
; section
!= NULL
; section
=
1316 if (only
== (char *) NULL
||
1317 strcmp (only
, section
->name
) == 0)
1319 if (section
->flags
& SEC_HAS_CONTENTS
)
1321 printf ("Contents of section %s:\n", section
->name
);
1323 if (bfd_section_size (abfd
, section
) == 0)
1325 data
= (bfd_byte
*) xmalloc ((size_t) bfd_section_size (abfd
, section
));
1326 datasize
= bfd_section_size (abfd
, section
);
1329 bfd_get_section_contents (abfd
, section
, (PTR
) data
, 0, bfd_section_size (abfd
, section
));
1331 if (start_address
== (bfd_vma
) -1
1332 || start_address
< section
->vma
)
1335 start
= start_address
- section
->vma
;
1336 if (stop_address
== (bfd_vma
) -1)
1337 stop
= bfd_section_size (abfd
, section
);
1340 if (stop_address
< section
->vma
)
1343 stop
= stop_address
- section
->vma
;
1344 if (stop
> bfd_section_size (abfd
, section
))
1345 stop
= bfd_section_size (abfd
, section
);
1347 for (i
= start
; i
< stop
; i
+= onaline
)
1351 printf (" %04lx ", (unsigned long int) (i
+ section
->vma
));
1352 for (j
= i
; j
< i
+ onaline
; j
++)
1355 printf ("%02x", (unsigned) (data
[j
]));
1363 for (j
= i
; j
< i
+ onaline
; j
++)
1368 printf ("%c", isprint (data
[j
]) ? data
[j
] : '.');
1378 /* Should perhaps share code and display with nm? */
1380 dump_symbols (abfd
, dynamic
)
1394 printf ("DYNAMIC SYMBOL TABLE:\n");
1402 printf ("SYMBOL TABLE:\n");
1405 for (count
= 0; count
< max
; count
++)
1409 bfd
*cur_bfd
= bfd_asymbol_bfd(*current
);
1412 bfd_print_symbol (cur_bfd
,
1414 *current
, bfd_print_symbol_all
);
1432 for (a
= abfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1436 if (bfd_is_abs_section (a
))
1438 if (bfd_is_und_section (a
))
1440 if (bfd_is_com_section (a
))
1445 if (strcmp (only
, a
->name
))
1448 else if ((a
->flags
& SEC_RELOC
) == 0)
1451 printf ("RELOCATION RECORDS FOR [%s]:", a
->name
);
1453 relsize
= bfd_get_reloc_upper_bound (abfd
, a
);
1455 bfd_fatal (bfd_get_filename (abfd
));
1459 printf (" (none)\n\n");
1463 relpp
= (arelent
**) xmalloc (relsize
);
1464 relcount
= bfd_canonicalize_reloc (abfd
, a
, relpp
, syms
);
1466 bfd_fatal (bfd_get_filename (abfd
));
1467 else if (relcount
== 0)
1469 printf (" (none)\n\n");
1474 dump_reloc_set (abfd
, relpp
, relcount
);
1483 dump_dynamic_relocs (abfd
)
1490 printf ("DYNAMIC RELOCATION RECORDS");
1492 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
1494 bfd_fatal (bfd_get_filename (abfd
));
1498 printf (" (none)\n\n");
1502 relpp
= (arelent
**) xmalloc (relsize
);
1503 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
1505 bfd_fatal (bfd_get_filename (abfd
));
1506 else if (relcount
== 0)
1508 printf (" (none)\n\n");
1513 dump_reloc_set (abfd
, relpp
, relcount
);
1521 dump_reloc_set (abfd
, relpp
, relcount
)
1528 /* Get column headers lined up reasonably. */
1534 sprintf_vma (buf
, (bfd_vma
) -1);
1535 width
= strlen (buf
) - 7;
1537 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
1540 for (p
= relpp
; relcount
&& *p
!= (arelent
*) NULL
; p
++, relcount
--)
1543 CONST
char *sym_name
;
1544 CONST
char *section_name
;
1546 if (start_address
!= (bfd_vma
) -1
1547 && q
->address
< start_address
)
1549 if (stop_address
!= (bfd_vma
) -1
1550 && q
->address
> stop_address
)
1553 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
1555 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
1556 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
1561 section_name
= NULL
;
1565 printf_vma (q
->address
);
1566 printf (" %-16s %s",
1572 if (section_name
== (CONST
char *) NULL
)
1573 section_name
= "*unknown*";
1574 printf_vma (q
->address
);
1575 printf (" %-16s [%s]",
1582 printf_vma (q
->addend
);
1588 /* The length of the longest architecture name + 1. */
1589 #define LONGEST_ARCH sizeof("rs6000:6000")
1595 /* List the targets that BFD is configured to support, each followed
1596 by its endianness and the architectures it supports. */
1599 display_target_list ()
1601 extern char *tmpnam ();
1602 extern bfd_target
*bfd_target_vector
[];
1603 char tmparg
[L_tmpnam
];
1607 dummy_name
= tmpnam (tmparg
);
1608 for (t
= 0; bfd_target_vector
[t
]; t
++)
1610 bfd_target
*p
= bfd_target_vector
[t
];
1611 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1614 printf ("%s\n (header %s, data %s)\n", p
->name
,
1615 p
->header_byteorder_big_p
? "big endian" : "little endian",
1616 p
->byteorder_big_p
? "big endian" : "little endian");
1620 bfd_nonfatal (dummy_name
);
1624 if (! bfd_set_format (abfd
, bfd_object
))
1626 if (bfd_get_error () != bfd_error_invalid_operation
)
1627 bfd_nonfatal (p
->name
);
1631 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1632 if (bfd_set_arch_mach (abfd
, (enum bfd_architecture
) a
, 0))
1634 bfd_printable_arch_mach ((enum bfd_architecture
) a
, 0));
1636 unlink (dummy_name
);
1639 /* Print a table showing which architectures are supported for entries
1640 FIRST through LAST-1 of bfd_target_vector (targets across,
1641 architectures down). */
1644 display_info_table (first
, last
)
1648 extern bfd_target
*bfd_target_vector
[];
1649 extern char *tmpnam ();
1650 char tmparg
[L_tmpnam
];
1654 /* Print heading of target names. */
1655 printf ("\n%*s", (int) LONGEST_ARCH
, " ");
1656 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1657 printf ("%s ", bfd_target_vector
[t
]->name
);
1660 dummy_name
= tmpnam (tmparg
);
1661 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1662 if (strcmp (bfd_printable_arch_mach (a
, 0), "UNKNOWN!") != 0)
1664 printf ("%*s ", (int) LONGEST_ARCH
- 1,
1665 bfd_printable_arch_mach (a
, 0));
1666 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1668 bfd_target
*p
= bfd_target_vector
[t
];
1670 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1674 bfd_nonfatal (p
->name
);
1680 if (! bfd_set_format (abfd
, bfd_object
))
1682 if (bfd_get_error () != bfd_error_invalid_operation
)
1683 bfd_nonfatal (p
->name
);
1690 if (! bfd_set_arch_mach (abfd
, a
, 0))
1695 printf ("%s ", p
->name
);
1698 int l
= strlen (p
->name
);
1706 unlink (dummy_name
);
1709 /* Print tables of all the target-architecture combinations that
1710 BFD has been configured to support. */
1713 display_target_tables ()
1716 extern bfd_target
*bfd_target_vector
[];
1718 extern char *getenv ();
1721 colum
= getenv ("COLUMNS");
1723 columns
= atoi (colum
);
1728 while (bfd_target_vector
[t
] != NULL
)
1732 wid
= LONGEST_ARCH
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1734 while (wid
< columns
&& bfd_target_vector
[t
] != NULL
)
1738 newwid
= wid
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1739 if (newwid
>= columns
)
1744 display_info_table (oldt
, t
);
1751 printf ("BFD header file version %s\n", BFD_VERSION
);
1752 display_target_list ();
1753 display_target_tables ();
1762 char *target
= default_target
;
1763 boolean seenflag
= false;
1765 program_name
= *argv
;
1766 xmalloc_set_program_name (program_name
);
1768 START_PROGRESS (program_name
, 0);
1772 while ((c
= getopt_long (argc
, argv
, "pib:m:VdDlfahrRtTxsSj:w", long_options
,
1776 if (c
!= 'l' && c
!= OPTION_START_ADDRESS
&& c
!= OPTION_STOP_ADDRESS
)
1781 break; /* we've been given a long option */
1789 with_line_numbers
= 1;
1795 dump_file_header
= true;
1798 formats_info
= true;
1801 dump_private_headers
= 1;
1804 dump_private_headers
= 1;
1806 dump_reloc_info
= 1;
1807 dump_file_header
= true;
1809 dump_section_headers
= 1;
1815 dump_dynamic_symtab
= 1;
1821 disassemble
= disassemble_all
= true;
1825 with_source_code
= true;
1828 dump_section_contents
= 1;
1831 dump_reloc_info
= 1;
1834 dump_dynamic_reloc_info
= 1;
1840 dump_section_headers
= 1;
1850 case OPTION_START_ADDRESS
:
1851 start_address
= parse_vma (optarg
, "--start-address");
1853 case OPTION_STOP_ADDRESS
:
1854 stop_address
= parse_vma (optarg
, "--stop-address");
1863 printf ("GNU %s version %s\n", program_name
, program_version
);
1867 if (seenflag
== false)
1877 display_file ("a.out", target
);
1879 for (; optind
< argc
;)
1880 display_file (argv
[optind
++], target
);
1883 END_PROGRESS (program_name
);