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. */
26 #include "libiberty.h"
30 /* Internal headers for the ELF .stab-dump code - sorry. */
31 #define BYTES_IN_WORD 32
32 #include "aout/aout64.h"
34 #ifdef NEED_DECLARATION_FPRINTF
35 /* This is needed by INIT_DISASSEMBLE_INFO. */
36 extern int fprintf ();
39 char *default_target
= NULL
; /* default at runtime */
41 extern char *program_version
;
43 int show_version
= 0; /* show the version number */
44 int dump_section_contents
; /* -s */
45 int dump_section_headers
; /* -h */
46 boolean dump_file_header
; /* -f */
47 int dump_symtab
; /* -t */
48 int dump_dynamic_symtab
; /* -T */
49 int dump_reloc_info
; /* -r */
50 int dump_dynamic_reloc_info
; /* -R */
51 int dump_ar_hdrs
; /* -a */
52 int dump_private_headers
; /* -p */
53 int with_line_numbers
; /* -l */
54 boolean with_source_code
; /* -S */
55 int dump_stab_section_info
; /* --stabs */
56 boolean disassemble
; /* -d */
57 boolean disassemble_all
; /* -D */
58 boolean formats_info
; /* -i */
59 char *only
; /* -j secname */
60 int wide_output
; /* -w */
61 bfd_vma start_address
= (bfd_vma
) -1; /* --start-address */
62 bfd_vma stop_address
= (bfd_vma
) -1; /* --stop-address */
63 int dump_debugging
; /* --debugging */
65 /* Extra info to pass to the disassembler address printing function. */
66 struct objdump_disasm_info
{
72 /* Architecture to disassemble for, or default if NULL. */
73 char *machine
= (char *) NULL
;
75 /* The symbol table. */
78 /* Number of symbols in `syms'. */
81 /* The sorted symbol table. */
82 asymbol
**sorted_syms
;
84 /* Number of symbols in `sorted_syms'. */
85 long sorted_symcount
= 0;
87 /* The dynamic symbol table. */
90 /* Number of symbols in `dynsyms'. */
93 /* Forward declarations. */
96 display_file
PARAMS ((char *filename
, char *target
));
99 dump_data
PARAMS ((bfd
*abfd
));
102 dump_relocs
PARAMS ((bfd
*abfd
));
105 dump_dynamic_relocs
PARAMS ((bfd
* abfd
));
108 dump_reloc_set
PARAMS ((bfd
*, arelent
**, long));
111 dump_symbols
PARAMS ((bfd
*abfd
, boolean dynamic
));
114 display_bfd
PARAMS ((bfd
*abfd
));
117 objdump_print_value
PARAMS ((bfd_vma
, FILE *));
120 objdump_print_address
PARAMS ((bfd_vma
, struct disassemble_info
*));
123 show_line
PARAMS ((bfd
*, asection
*, bfd_vma
));
126 endian_string
PARAMS ((enum bfd_endian
));
129 usage (stream
, status
)
134 Usage: %s [-ahifdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
135 [--archive-headers] [--target=bfdname] [--debugging] [--disassemble]\n\
136 [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
137 [--info] [--section=section-name] [--line-numbers] [--source]\n",
140 [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
141 [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
142 [--wide] [--version] [--help] [--private-headers]\n\
143 [--start-address=addr] [--stop-address=addr] objfile...\n\
144 at least one option besides -l (--line-numbers) must be given\n");
145 list_supported_targets (program_name
, stream
);
149 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
151 #define OPTION_START_ADDRESS (150)
152 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
154 static struct option long_options
[]=
156 {"all-headers", no_argument
, NULL
, 'x'},
157 {"private-headers", no_argument
, NULL
, 'p'},
158 {"architecture", required_argument
, NULL
, 'm'},
159 {"archive-headers", no_argument
, NULL
, 'a'},
160 {"debugging", no_argument
, &dump_debugging
, 1},
161 {"disassemble", no_argument
, NULL
, 'd'},
162 {"disassemble-all", no_argument
, NULL
, 'D'},
163 {"dynamic-reloc", no_argument
, NULL
, 'R'},
164 {"dynamic-syms", no_argument
, NULL
, 'T'},
165 {"file-headers", no_argument
, NULL
, 'f'},
166 {"full-contents", no_argument
, NULL
, 's'},
167 {"headers", no_argument
, NULL
, 'h'},
168 {"help", no_argument
, NULL
, 'H'},
169 {"info", no_argument
, NULL
, 'i'},
170 {"line-numbers", no_argument
, NULL
, 'l'},
171 {"reloc", no_argument
, NULL
, 'r'},
172 {"section", required_argument
, NULL
, 'j'},
173 {"section-headers", no_argument
, NULL
, 'h'},
174 {"source", no_argument
, NULL
, 'S'},
175 {"stabs", no_argument
, &dump_stab_section_info
, 1},
176 {"start-address", required_argument
, NULL
, OPTION_START_ADDRESS
},
177 {"stop-address", required_argument
, NULL
, OPTION_STOP_ADDRESS
},
178 {"syms", no_argument
, NULL
, 't'},
179 {"target", required_argument
, NULL
, 'b'},
180 {"version", no_argument
, &show_version
, 1},
181 {"wide", no_argument
, &wide_output
, 'w'},
182 {0, no_argument
, 0, 0}
186 dump_section_header (abfd
, section
, ignored
)
194 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
197 printf ("SECTION %d [%s]\t: size %08x",
200 (unsigned) bfd_get_section_size_before_reloc (section
));
202 printf_vma (section
->vma
);
204 printf_vma (section
->lma
);
205 printf (" align 2**%u%s ",
206 section
->alignment_power
, (wide_output
) ? "" : "\n");
207 PF (SEC_ALLOC
, "ALLOC");
208 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
209 PF (SEC_CONSTRUCTOR_TEXT
, "CONSTRUCTOR TEXT");
210 PF (SEC_CONSTRUCTOR_DATA
, "CONSTRUCTOR DATA");
211 PF (SEC_CONSTRUCTOR_BSS
, "CONSTRUCTOR BSS");
212 PF (SEC_LOAD
, "LOAD");
213 PF (SEC_RELOC
, "RELOC");
215 PF (SEC_BALIGN
, "BALIGN");
217 PF (SEC_READONLY
, "READONLY");
218 PF (SEC_CODE
, "CODE");
219 PF (SEC_DATA
, "DATA");
221 PF (SEC_DEBUGGING
, "DEBUGGING");
222 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
223 PF (SEC_EXCLUDE
, "EXCLUDE");
224 PF (SEC_SORT_ENTRIES
, "SORT ENTRIES");
233 bfd_map_over_sections (abfd
, dump_section_header
, (PTR
) NULL
);
240 asymbol
**sy
= (asymbol
**) NULL
;
243 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
245 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd
));
250 storage
= bfd_get_symtab_upper_bound (abfd
);
252 bfd_fatal (bfd_get_filename (abfd
));
256 sy
= (asymbol
**) xmalloc (storage
);
258 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
260 bfd_fatal (bfd_get_filename (abfd
));
262 fprintf (stderr
, "%s: %s: No symbols\n",
263 program_name
, bfd_get_filename (abfd
));
267 /* Read in the dynamic symbols. */
270 slurp_dynamic_symtab (abfd
)
273 asymbol
**sy
= (asymbol
**) NULL
;
276 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
279 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
281 fprintf (stderr
, "%s: %s: not a dynamic object\n",
282 program_name
, bfd_get_filename (abfd
));
287 bfd_fatal (bfd_get_filename (abfd
));
292 sy
= (asymbol
**) xmalloc (storage
);
294 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
296 bfd_fatal (bfd_get_filename (abfd
));
297 if (dynsymcount
== 0)
298 fprintf (stderr
, "%s: %s: No dynamic symbols\n",
299 program_name
, bfd_get_filename (abfd
));
303 /* Filter out (in place) symbols that are useless for disassembly.
304 COUNT is the number of elements in SYMBOLS.
305 Return the number of useful symbols. */
308 remove_useless_symbols (symbols
, count
)
312 register asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
316 asymbol
*sym
= *in_ptr
++;
318 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
320 if (sym
->flags
& (BSF_DEBUGGING
))
322 if (bfd_is_und_section (sym
->section
)
323 || bfd_is_com_section (sym
->section
))
328 return out_ptr
- symbols
;
331 /* Sort symbols into value order. */
334 compare_symbols (ap
, bp
)
338 const asymbol
*a
= *(const asymbol
**)ap
;
339 const asymbol
*b
= *(const asymbol
**)bp
;
343 flagword aflags
, bflags
;
345 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
347 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
350 if (a
->section
> b
->section
)
352 else if (a
->section
< b
->section
)
355 an
= bfd_asymbol_name (a
);
356 bn
= bfd_asymbol_name (b
);
360 /* The symbols gnu_compiled and gcc2_compiled convey no real
361 information, so put them after other symbols with the same value. */
363 af
= (strstr (an
, "gnu_compiled") != NULL
364 || strstr (an
, "gcc2_compiled") != NULL
);
365 bf
= (strstr (bn
, "gnu_compiled") != NULL
366 || strstr (bn
, "gcc2_compiled") != NULL
);
373 /* We use a heuristic for the file name, to try to sort it after
374 more useful symbols. It may not work on non Unix systems, but it
375 doesn't really matter; the only difference is precisely which
376 symbol names get printed. */
378 #define file_symbol(s, sn, snl) \
379 (((s)->flags & BSF_FILE) != 0 \
380 || ((sn)[(snl) - 2] == '.' \
381 && ((sn)[(snl) - 1] == 'o' \
382 || (sn)[(snl) - 1] == 'a')))
384 af
= file_symbol (a
, an
, anl
);
385 bf
= file_symbol (b
, bn
, bnl
);
392 /* Finally, try to sort global symbols before local symbols before
393 debugging symbols. */
398 if ((aflags
& BSF_DEBUGGING
) != (bflags
& BSF_DEBUGGING
))
400 if ((aflags
& BSF_DEBUGGING
) != 0)
405 if ((aflags
& BSF_LOCAL
) != (bflags
& BSF_LOCAL
))
407 if ((aflags
& BSF_LOCAL
) != 0)
416 /* Sort relocs into address order. */
419 compare_relocs (ap
, bp
)
423 const arelent
*a
= *(const arelent
**)ap
;
424 const arelent
*b
= *(const arelent
**)bp
;
426 if (a
->address
> b
->address
)
428 else if (a
->address
< b
->address
)
431 /* So that associated relocations tied to the same address show up
432 in the correct order, we don't do any further sorting. */
441 /* Print VMA to STREAM with no leading zeroes. */
444 objdump_print_value (vma
, stream
)
451 sprintf_vma (buf
, vma
);
452 for (p
= buf
; *p
== '0'; ++p
)
454 fprintf (stream
, "%s", p
);
457 /* Print VMA symbolically to INFO if possible. */
460 objdump_print_address (vma
, info
)
462 struct disassemble_info
*info
;
464 /* @@ Would it speed things up to cache the last two symbols returned,
465 and maybe their address ranges? For many processors, only one memory
466 operand can be present at a time, so the 2-entry cache wouldn't be
467 constantly churned by code doing heavy memory accesses. */
469 /* Indices in `sorted_syms'. */
471 long max
= sorted_symcount
;
474 fprintf_vma (info
->stream
, vma
);
476 if (sorted_symcount
< 1)
479 /* Perform a binary search looking for the closest symbol to the
480 required value. We are searching the range (min, max]. */
481 while (min
+ 1 < max
)
485 thisplace
= (max
+ min
) / 2;
486 sym
= sorted_syms
[thisplace
];
488 if (bfd_asymbol_value (sym
) > vma
)
490 else if (bfd_asymbol_value (sym
) < vma
)
499 /* The symbol we want is now in min, the low end of the range we
500 were searching. If there are several symbols with the same
501 value, we want the first one. */
504 && (bfd_asymbol_value (sorted_syms
[thisplace
])
505 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
509 /* If the file is relocateable, and the symbol could be from this
510 section, prefer a symbol from this section over symbols from
511 others, even if the other symbol's value might be closer.
513 Note that this may be wrong for some symbol references if the
514 sections have overlapping memory ranges, but in that case there's
515 no way to tell what's desired without looking at the relocation
517 struct objdump_disasm_info
*aux
;
520 aux
= (struct objdump_disasm_info
*) info
->application_data
;
521 if (sorted_syms
[thisplace
]->section
!= aux
->sec
523 || ((aux
->abfd
->flags
& HAS_RELOC
) != 0
524 && vma
>= bfd_get_section_vma (aux
->abfd
, aux
->sec
)
525 && vma
< (bfd_get_section_vma (aux
->abfd
, aux
->sec
)
526 + bfd_section_size (aux
->abfd
, aux
->sec
)))))
528 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
530 if (bfd_asymbol_value (sorted_syms
[i
])
531 != bfd_asymbol_value (sorted_syms
[thisplace
]))
537 if (sorted_syms
[i
]->section
== aux
->sec
539 || sorted_syms
[i
- 1]->section
!= aux
->sec
540 || (bfd_asymbol_value (sorted_syms
[i
])
541 != bfd_asymbol_value (sorted_syms
[i
- 1]))))
548 if (sorted_syms
[thisplace
]->section
!= aux
->sec
)
550 /* We didn't find a good symbol with a smaller value.
551 Look for one with a larger value. */
552 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
554 if (sorted_syms
[i
]->section
== aux
->sec
)
562 if (sorted_syms
[thisplace
]->section
!= aux
->sec
564 || ((aux
->abfd
->flags
& HAS_RELOC
) != 0
565 && vma
>= bfd_get_section_vma (aux
->abfd
, aux
->sec
)
566 && vma
< (bfd_get_section_vma (aux
->abfd
, aux
->sec
)
567 + bfd_section_size (aux
->abfd
, aux
->sec
)))))
571 fprintf (info
->stream
, " <%s",
572 bfd_get_section_name (aux
->abfd
, aux
->sec
));
573 secaddr
= bfd_get_section_vma (aux
->abfd
, aux
->sec
);
576 fprintf (info
->stream
, "-");
577 objdump_print_value (secaddr
- vma
, info
->stream
);
579 else if (vma
> secaddr
)
581 fprintf (info
->stream
, "+");
582 objdump_print_value (vma
- secaddr
, info
->stream
);
584 fprintf (info
->stream
, ">");
590 fprintf (info
->stream
, " <%s", sorted_syms
[thisplace
]->name
);
591 if (bfd_asymbol_value (sorted_syms
[thisplace
]) > vma
)
593 fprintf (info
->stream
, "-");
594 objdump_print_value (bfd_asymbol_value (sorted_syms
[thisplace
]) - vma
,
597 else if (vma
> bfd_asymbol_value (sorted_syms
[thisplace
]))
599 fprintf (info
->stream
, "+");
600 objdump_print_value (vma
- bfd_asymbol_value (sorted_syms
[thisplace
]),
603 fprintf (info
->stream
, ">");
606 /* Hold the last function name and the last line number we displayed
609 static char *prev_functionname
;
610 static unsigned int prev_line
;
612 /* We keep a list of all files that we have seen when doing a
613 dissassembly with source, so that we know how much of the file to
614 display. This can be important for inlined functions. */
616 struct print_file_list
618 struct print_file_list
*next
;
624 static struct print_file_list
*print_files
;
626 /* The number of preceding context lines to show when we start
627 displaying a file for the first time. */
629 #define SHOW_PRECEDING_CONTEXT_LINES (5)
631 /* Skip ahead to a given line in a file, optionally printing each
635 skip_to_line
PARAMS ((struct print_file_list
*, unsigned int, boolean
));
638 skip_to_line (p
, line
, show
)
639 struct print_file_list
*p
;
643 while (p
->line
< line
)
647 if (fgets (buf
, sizeof buf
, p
->f
) == NULL
)
657 if (strchr (buf
, '\n') != NULL
)
662 /* Show the line number, or the source line, in a dissassembly
666 show_line (abfd
, section
, off
)
671 CONST
char *filename
;
672 CONST
char *functionname
;
675 if (! with_line_numbers
&& ! with_source_code
)
678 if (! bfd_find_nearest_line (abfd
, section
, syms
, off
, &filename
,
679 &functionname
, &line
))
682 if (filename
!= NULL
&& *filename
== '\0')
684 if (functionname
!= NULL
&& *functionname
== '\0')
687 if (with_line_numbers
)
689 if (functionname
!= NULL
690 && (prev_functionname
== NULL
691 || strcmp (functionname
, prev_functionname
) != 0))
692 printf ("%s():\n", functionname
);
693 if (line
> 0 && line
!= prev_line
)
694 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
701 struct print_file_list
**pp
, *p
;
703 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
704 if (strcmp ((*pp
)->filename
, filename
) == 0)
710 if (p
!= print_files
)
714 /* We have reencountered a file name which we saw
715 earlier. This implies that either we are dumping out
716 code from an included file, or the same file was
717 linked in more than once. There are two common cases
718 of an included file: inline functions in a header
719 file, and a bison or flex skeleton file. In the
720 former case we want to just start printing (but we
721 back up a few lines to give context); in the latter
722 case we want to continue from where we left off. I
723 can't think of a good way to distinguish the cases,
724 so I used a heuristic based on the file name. */
725 if (strcmp (p
->filename
+ strlen (p
->filename
) - 2, ".h") != 0)
729 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
736 p
->f
= fopen (p
->filename
, "r");
740 skip_to_line (p
, l
, false);
742 if (print_files
->f
!= NULL
)
744 fclose (print_files
->f
);
745 print_files
->f
= NULL
;
751 skip_to_line (p
, line
, true);
753 p
->next
= print_files
;
761 f
= fopen (filename
, "r");
766 p
= ((struct print_file_list
*)
767 xmalloc (sizeof (struct print_file_list
)));
768 p
->filename
= xmalloc (strlen (filename
) + 1);
769 strcpy (p
->filename
, filename
);
773 if (print_files
!= NULL
&& print_files
->f
!= NULL
)
775 fclose (print_files
->f
);
776 print_files
->f
= NULL
;
778 p
->next
= print_files
;
781 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
784 skip_to_line (p
, l
, false);
786 skip_to_line (p
, line
, true);
791 if (functionname
!= NULL
792 && (prev_functionname
== NULL
793 || strcmp (functionname
, prev_functionname
) != 0))
795 if (prev_functionname
!= NULL
)
796 free (prev_functionname
);
797 prev_functionname
= xmalloc (strlen (functionname
) + 1);
798 strcpy (prev_functionname
, functionname
);
801 if (line
> 0 && line
!= prev_line
)
806 disassemble_data (abfd
)
810 disassembler_ftype disassemble_fn
= 0; /* New style */
811 struct disassemble_info disasm_info
;
812 struct objdump_disasm_info aux
;
814 boolean done_dot
= false;
817 prev_functionname
= NULL
;
820 /* We make a copy of syms to sort. We don't want to sort syms
821 because that will screw up the relocs. */
822 sorted_syms
= (asymbol
**) xmalloc (symcount
* sizeof (asymbol
*));
823 memcpy (sorted_syms
, syms
, symcount
* sizeof (asymbol
*));
825 sorted_symcount
= remove_useless_symbols (sorted_syms
, symcount
);
827 /* Sort the symbols into section and symbol order */
828 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
830 INIT_DISASSEMBLE_INFO(disasm_info
, stdout
);
831 disasm_info
.application_data
= (PTR
) &aux
;
833 disasm_info
.print_address_func
= objdump_print_address
;
835 if (machine
!= (char *) NULL
)
837 const bfd_arch_info_type
*info
= bfd_scan_arch (machine
);
840 fprintf (stderr
, "%s: Can't use supplied machine %s\n",
845 abfd
->arch_info
= info
;
848 disassemble_fn
= disassembler (abfd
);
851 fprintf (stderr
, "%s: Can't disassemble for architecture %s\n",
853 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
857 for (section
= abfd
->sections
;
858 section
!= (asection
*) NULL
;
859 section
= section
->next
)
861 bfd_byte
*data
= NULL
;
862 bfd_size_type datasize
= 0;
863 arelent
**relbuf
= NULL
;
864 arelent
**relpp
= NULL
;
865 arelent
**relppend
= NULL
;
868 if ((section
->flags
& SEC_LOAD
) == 0
869 || (! disassemble_all
871 && (section
->flags
& SEC_CODE
) == 0))
873 if (only
!= (char *) NULL
&& strcmp (only
, section
->name
) != 0)
877 && (section
->flags
& SEC_RELOC
) != 0)
881 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
883 bfd_fatal (bfd_get_filename (abfd
));
889 relbuf
= (arelent
**) xmalloc (relsize
);
890 relcount
= bfd_canonicalize_reloc (abfd
, section
, relbuf
, syms
);
892 bfd_fatal (bfd_get_filename (abfd
));
894 /* Sort the relocs by address. */
895 qsort (relbuf
, relcount
, sizeof (arelent
*), compare_relocs
);
898 relppend
= relpp
+ relcount
;
902 printf ("Disassembly of section %s:\n", section
->name
);
904 datasize
= bfd_get_section_size_before_reloc (section
);
908 data
= (bfd_byte
*) xmalloc ((size_t) datasize
);
910 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
913 disasm_info
.buffer
= data
;
914 disasm_info
.buffer_vma
= section
->vma
;
915 disasm_info
.buffer_length
= datasize
;
916 if (start_address
== (bfd_vma
) -1
917 || start_address
< disasm_info
.buffer_vma
)
920 i
= start_address
- disasm_info
.buffer_vma
;
921 if (stop_address
== (bfd_vma
) -1)
925 if (stop_address
< disasm_info
.buffer_vma
)
928 stop
= stop_address
- disasm_info
.buffer_vma
;
929 if (stop
> disasm_info
.buffer_length
)
930 stop
= disasm_info
.buffer_length
;
935 boolean need_nl
= false;
937 if (data
[i
] == 0 && data
[i
+ 1] == 0 && data
[i
+ 2] == 0 &&
940 if (done_dot
== false)
950 if (with_line_numbers
|| with_source_code
)
951 show_line (abfd
, section
, i
);
952 aux
.require_sec
= true;
953 objdump_print_address (section
->vma
+ i
, &disasm_info
);
954 aux
.require_sec
= false;
957 bytes
= (*disassemble_fn
) (section
->vma
+ i
, &disasm_info
);
968 && (section
->flags
& SEC_RELOC
) != 0)
970 while (relpp
< relppend
971 && ((*relpp
)->address
>= (bfd_vma
) i
972 && (*relpp
)->address
< (bfd_vma
) i
+ bytes
))
975 const char *sym_name
;
979 printf ("\t\tRELOC: ");
981 printf_vma (section
->vma
+ q
->address
);
983 printf (" %s ", q
->howto
->name
);
985 if (q
->sym_ptr_ptr
!= NULL
986 && *q
->sym_ptr_ptr
!= NULL
)
988 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
989 if (sym_name
== NULL
|| *sym_name
== '\0')
993 sym_sec
= bfd_get_section (*q
->sym_ptr_ptr
);
994 sym_name
= bfd_get_section_name (abfd
, sym_sec
);
995 if (sym_name
== NULL
|| *sym_name
== '\0')
996 sym_name
= "*unknown*";
1000 printf ("%s", sym_name
);
1005 printf_vma (q
->addend
);
1028 /* Define a table of stab values and print-strings. We wish the initializer
1029 could be a direct-mapped table, but instead we build one the first
1039 struct stab_print stab_print
[] = {
1040 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
1041 #include "aout/stab.def"
1042 #undef __define_stab
1046 void dump_section_stabs
PARAMS ((bfd
*abfd
, char *stabsect_name
,
1047 char *strsect_name
));
1049 /* Dump the stabs sections from an object file that has a section that
1050 uses Sun stabs encoding. It has to use some hooks into BFD because
1051 string table sections are not normally visible to BFD callers. */
1057 /* Allocate and initialize stab name array if first time. */
1058 if (stab_name
== NULL
)
1062 stab_name
= (char **) xmalloc (256 * sizeof(char *));
1063 /* Clear the array. */
1064 for (i
= 0; i
< 256; i
++)
1065 stab_name
[i
] = NULL
;
1066 /* Fill in the defined stabs. */
1067 for (i
= 0; *stab_print
[i
].string
; i
++)
1068 stab_name
[stab_print
[i
].value
] = stab_print
[i
].string
;
1071 dump_section_stabs (abfd
, ".stab", ".stabstr");
1072 dump_section_stabs (abfd
, ".stab.excl", ".stab.exclstr");
1073 dump_section_stabs (abfd
, ".stab.index", ".stab.indexstr");
1074 dump_section_stabs (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1077 static struct internal_nlist
*stabs
;
1078 static bfd_size_type stab_size
;
1080 static char *strtab
;
1081 static bfd_size_type stabstr_size
;
1083 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1084 and string table section STRSECT_NAME into `strtab'.
1085 If the section exists and was read, allocate the space and return true.
1086 Otherwise return false. */
1089 read_section_stabs (abfd
, stabsect_name
, strsect_name
)
1091 char *stabsect_name
;
1094 asection
*stabsect
, *stabstrsect
;
1096 stabsect
= bfd_get_section_by_name (abfd
, stabsect_name
);
1099 printf ("No %s section present\n\n", stabsect_name
);
1103 stabstrsect
= bfd_get_section_by_name (abfd
, strsect_name
);
1104 if (0 == stabstrsect
)
1106 fprintf (stderr
, "%s: %s has no %s section\n", program_name
,
1107 bfd_get_filename (abfd
), strsect_name
);
1111 stab_size
= bfd_section_size (abfd
, stabsect
);
1112 stabstr_size
= bfd_section_size (abfd
, stabstrsect
);
1114 stabs
= (struct internal_nlist
*) xmalloc (stab_size
);
1115 strtab
= (char *) xmalloc (stabstr_size
);
1117 if (! bfd_get_section_contents (abfd
, stabsect
, (PTR
) stabs
, 0, stab_size
))
1119 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1120 program_name
, stabsect_name
, bfd_get_filename (abfd
),
1121 bfd_errmsg (bfd_get_error ()));
1127 if (! bfd_get_section_contents (abfd
, stabstrsect
, (PTR
) strtab
, 0,
1130 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1131 program_name
, strsect_name
, bfd_get_filename (abfd
),
1132 bfd_errmsg (bfd_get_error ()));
1141 #define SWAP_SYMBOL(symp, abfd) \
1143 (symp)->n_strx = bfd_h_get_32(abfd, \
1144 (unsigned char *)&(symp)->n_strx); \
1145 (symp)->n_desc = bfd_h_get_16 (abfd, \
1146 (unsigned char *)&(symp)->n_desc); \
1147 (symp)->n_value = bfd_h_get_32 (abfd, \
1148 (unsigned char *)&(symp)->n_value); \
1151 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1152 using string table section STRSECT_NAME (in `strtab'). */
1155 print_section_stabs (abfd
, stabsect_name
, strsect_name
)
1157 char *stabsect_name
;
1161 unsigned file_string_table_offset
= 0, next_file_string_table_offset
= 0;
1162 struct internal_nlist
*stabp
= stabs
,
1163 *stabs_end
= (struct internal_nlist
*) (stab_size
+ (char *) stabs
);
1165 printf ("Contents of %s section:\n\n", stabsect_name
);
1166 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1168 /* Loop through all symbols and print them.
1170 We start the index at -1 because there is a dummy symbol on
1171 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1173 for (i
= -1; stabp
< stabs_end
; stabp
++, i
++)
1175 SWAP_SYMBOL (stabp
, abfd
);
1176 printf ("\n%-6d ", i
);
1177 /* Either print the stab name, or, if unnamed, print its number
1178 again (makes consistent formatting for tools like awk). */
1179 if (stab_name
[stabp
->n_type
])
1180 printf ("%-6s", stab_name
[stabp
->n_type
]);
1181 else if (stabp
->n_type
== N_UNDF
)
1184 printf ("%-6d", stabp
->n_type
);
1185 printf (" %-6d %-6d ", stabp
->n_other
, stabp
->n_desc
);
1186 printf_vma (stabp
->n_value
);
1187 printf (" %-6lu", stabp
->n_strx
);
1189 /* Symbols with type == 0 (N_UNDF) specify the length of the
1190 string table associated with this file. We use that info
1191 to know how to relocate the *next* file's string table indices. */
1193 if (stabp
->n_type
== N_UNDF
)
1195 file_string_table_offset
= next_file_string_table_offset
;
1196 next_file_string_table_offset
+= stabp
->n_value
;
1200 /* Using the (possibly updated) string table offset, print the
1201 string (if any) associated with this symbol. */
1203 if ((stabp
->n_strx
+ file_string_table_offset
) < stabstr_size
)
1204 printf (" %s", &strtab
[stabp
->n_strx
+ file_string_table_offset
]);
1213 dump_section_stabs (abfd
, stabsect_name
, strsect_name
)
1215 char *stabsect_name
;
1220 /* Check for section names for which stabsect_name is a prefix, to
1221 handle .stab0, etc. */
1222 for (s
= abfd
->sections
;
1226 if (strncmp (stabsect_name
, s
->name
, strlen (stabsect_name
)) == 0
1227 && strncmp (strsect_name
, s
->name
, strlen (strsect_name
)) != 0)
1229 if (read_section_stabs (abfd
, s
->name
, strsect_name
))
1231 print_section_stabs (abfd
, s
->name
, strsect_name
);
1240 dump_bfd_header (abfd
)
1245 printf ("architecture: %s, ",
1246 bfd_printable_arch_mach (bfd_get_arch (abfd
),
1247 bfd_get_mach (abfd
)));
1248 printf ("flags 0x%08x:\n", abfd
->flags
);
1250 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1251 PF (HAS_RELOC
, "HAS_RELOC");
1252 PF (EXEC_P
, "EXEC_P");
1253 PF (HAS_LINENO
, "HAS_LINENO");
1254 PF (HAS_DEBUG
, "HAS_DEBUG");
1255 PF (HAS_SYMS
, "HAS_SYMS");
1256 PF (HAS_LOCALS
, "HAS_LOCALS");
1257 PF (DYNAMIC
, "DYNAMIC");
1258 PF (WP_TEXT
, "WP_TEXT");
1259 PF (D_PAGED
, "D_PAGED");
1260 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
1261 printf ("\nstart address 0x");
1262 printf_vma (abfd
->start_address
);
1266 dump_bfd_private_header (abfd
)
1269 bfd_print_private_bfd_data (abfd
, stdout
);
1277 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
1279 bfd_nonfatal (bfd_get_filename (abfd
));
1280 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1282 list_matching_formats (matching
);
1288 printf ("\n%s: file format %s\n", bfd_get_filename (abfd
),
1291 print_arelt_descr (stdout
, abfd
, true);
1292 if (dump_file_header
)
1293 dump_bfd_header (abfd
);
1294 if (dump_private_headers
)
1295 dump_bfd_private_header (abfd
);
1297 if (dump_section_headers
)
1298 dump_headers (abfd
);
1299 if (dump_symtab
|| dump_reloc_info
|| disassemble
|| dump_debugging
)
1301 syms
= slurp_symtab (abfd
);
1303 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
)
1305 dynsyms
= slurp_dynamic_symtab (abfd
);
1308 dump_symbols (abfd
, false);
1309 if (dump_dynamic_symtab
)
1310 dump_symbols (abfd
, true);
1311 if (dump_stab_section_info
)
1313 if (dump_reloc_info
&& ! disassemble
)
1315 if (dump_dynamic_reloc_info
)
1316 dump_dynamic_relocs (abfd
);
1317 if (dump_section_contents
)
1320 disassemble_data (abfd
);
1325 dhandle
= read_debugging_info (abfd
, syms
, symcount
);
1326 if (dhandle
!= NULL
)
1328 if (! print_debugging_info (stdout
, dhandle
))
1329 fprintf (stderr
, "%s: printing debugging information failed\n",
1330 bfd_get_filename (abfd
));
1346 display_file (filename
, target
)
1350 bfd
*file
, *arfile
= (bfd
*) NULL
;
1352 file
= bfd_openr (filename
, target
);
1355 bfd_nonfatal (filename
);
1359 if (bfd_check_format (file
, bfd_archive
) == true)
1361 bfd
*last_arfile
= NULL
;
1363 printf ("In archive %s:\n", bfd_get_filename (file
));
1366 bfd_set_error (bfd_error_no_error
);
1368 arfile
= bfd_openr_next_archived_file (file
, arfile
);
1371 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1373 bfd_nonfatal (bfd_get_filename (file
));
1378 display_bfd (arfile
);
1380 if (last_arfile
!= NULL
)
1381 bfd_close (last_arfile
);
1382 last_arfile
= arfile
;
1385 if (last_arfile
!= NULL
)
1386 bfd_close (last_arfile
);
1394 /* Actually display the various requested regions */
1402 bfd_size_type datasize
= 0;
1404 bfd_size_type start
, stop
;
1406 for (section
= abfd
->sections
; section
!= NULL
; section
=
1411 if (only
== (char *) NULL
||
1412 strcmp (only
, section
->name
) == 0)
1414 if (section
->flags
& SEC_HAS_CONTENTS
)
1416 printf ("Contents of section %s:\n", section
->name
);
1418 if (bfd_section_size (abfd
, section
) == 0)
1420 data
= (bfd_byte
*) xmalloc ((size_t) bfd_section_size (abfd
, section
));
1421 datasize
= bfd_section_size (abfd
, section
);
1424 bfd_get_section_contents (abfd
, section
, (PTR
) data
, 0, bfd_section_size (abfd
, section
));
1426 if (start_address
== (bfd_vma
) -1
1427 || start_address
< section
->vma
)
1430 start
= start_address
- section
->vma
;
1431 if (stop_address
== (bfd_vma
) -1)
1432 stop
= bfd_section_size (abfd
, section
);
1435 if (stop_address
< section
->vma
)
1438 stop
= stop_address
- section
->vma
;
1439 if (stop
> bfd_section_size (abfd
, section
))
1440 stop
= bfd_section_size (abfd
, section
);
1442 for (i
= start
; i
< stop
; i
+= onaline
)
1446 printf (" %04lx ", (unsigned long int) (i
+ section
->vma
));
1447 for (j
= i
; j
< i
+ onaline
; j
++)
1450 printf ("%02x", (unsigned) (data
[j
]));
1458 for (j
= i
; j
< i
+ onaline
; j
++)
1463 printf ("%c", isprint (data
[j
]) ? data
[j
] : '.');
1473 /* Should perhaps share code and display with nm? */
1475 dump_symbols (abfd
, dynamic
)
1489 printf ("DYNAMIC SYMBOL TABLE:\n");
1497 printf ("SYMBOL TABLE:\n");
1500 for (count
= 0; count
< max
; count
++)
1504 bfd
*cur_bfd
= bfd_asymbol_bfd(*current
);
1507 bfd_print_symbol (cur_bfd
,
1509 *current
, bfd_print_symbol_all
);
1527 for (a
= abfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1531 if (bfd_is_abs_section (a
))
1533 if (bfd_is_und_section (a
))
1535 if (bfd_is_com_section (a
))
1540 if (strcmp (only
, a
->name
))
1543 else if ((a
->flags
& SEC_RELOC
) == 0)
1546 relsize
= bfd_get_reloc_upper_bound (abfd
, a
);
1548 bfd_fatal (bfd_get_filename (abfd
));
1550 printf ("RELOCATION RECORDS FOR [%s]:", a
->name
);
1554 printf (" (none)\n\n");
1558 relpp
= (arelent
**) xmalloc (relsize
);
1559 relcount
= bfd_canonicalize_reloc (abfd
, a
, relpp
, syms
);
1561 bfd_fatal (bfd_get_filename (abfd
));
1562 else if (relcount
== 0)
1564 printf (" (none)\n\n");
1569 dump_reloc_set (abfd
, relpp
, relcount
);
1578 dump_dynamic_relocs (abfd
)
1585 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
1587 bfd_fatal (bfd_get_filename (abfd
));
1589 printf ("DYNAMIC RELOCATION RECORDS");
1593 printf (" (none)\n\n");
1597 relpp
= (arelent
**) xmalloc (relsize
);
1598 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
1600 bfd_fatal (bfd_get_filename (abfd
));
1601 else if (relcount
== 0)
1603 printf (" (none)\n\n");
1608 dump_reloc_set (abfd
, relpp
, relcount
);
1616 dump_reloc_set (abfd
, relpp
, relcount
)
1623 /* Get column headers lined up reasonably. */
1629 sprintf_vma (buf
, (bfd_vma
) -1);
1630 width
= strlen (buf
) - 7;
1632 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
1635 for (p
= relpp
; relcount
&& *p
!= (arelent
*) NULL
; p
++, relcount
--)
1638 CONST
char *sym_name
;
1639 CONST
char *section_name
;
1641 if (start_address
!= (bfd_vma
) -1
1642 && q
->address
< start_address
)
1644 if (stop_address
!= (bfd_vma
) -1
1645 && q
->address
> stop_address
)
1648 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
1650 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
1651 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
1656 section_name
= NULL
;
1660 printf_vma (q
->address
);
1661 printf (" %-16s %s",
1667 if (section_name
== (CONST
char *) NULL
)
1668 section_name
= "*unknown*";
1669 printf_vma (q
->address
);
1670 printf (" %-16s [%s]",
1677 printf_vma (q
->addend
);
1683 /* The length of the longest architecture name + 1. */
1684 #define LONGEST_ARCH sizeof("rs6000:6000")
1691 endian_string (endian
)
1692 enum bfd_endian endian
;
1694 if (endian
== BFD_ENDIAN_BIG
)
1695 return "big endian";
1696 else if (endian
== BFD_ENDIAN_LITTLE
)
1697 return "little endian";
1699 return "endianness unknown";
1702 /* List the targets that BFD is configured to support, each followed
1703 by its endianness and the architectures it supports. */
1706 display_target_list ()
1708 extern char *tmpnam ();
1709 extern bfd_target
*bfd_target_vector
[];
1710 char tmparg
[L_tmpnam
];
1714 dummy_name
= tmpnam (tmparg
);
1715 for (t
= 0; bfd_target_vector
[t
]; t
++)
1717 bfd_target
*p
= bfd_target_vector
[t
];
1718 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1721 printf ("%s\n (header %s, data %s)\n", p
->name
,
1722 endian_string (p
->header_byteorder
),
1723 endian_string (p
->byteorder
));
1727 bfd_nonfatal (dummy_name
);
1731 if (! bfd_set_format (abfd
, bfd_object
))
1733 if (bfd_get_error () != bfd_error_invalid_operation
)
1734 bfd_nonfatal (p
->name
);
1738 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1739 if (bfd_set_arch_mach (abfd
, (enum bfd_architecture
) a
, 0))
1741 bfd_printable_arch_mach ((enum bfd_architecture
) a
, 0));
1743 unlink (dummy_name
);
1746 /* Print a table showing which architectures are supported for entries
1747 FIRST through LAST-1 of bfd_target_vector (targets across,
1748 architectures down). */
1751 display_info_table (first
, last
)
1755 extern bfd_target
*bfd_target_vector
[];
1756 extern char *tmpnam ();
1757 char tmparg
[L_tmpnam
];
1761 /* Print heading of target names. */
1762 printf ("\n%*s", (int) LONGEST_ARCH
, " ");
1763 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1764 printf ("%s ", bfd_target_vector
[t
]->name
);
1767 dummy_name
= tmpnam (tmparg
);
1768 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1769 if (strcmp (bfd_printable_arch_mach (a
, 0), "UNKNOWN!") != 0)
1771 printf ("%*s ", (int) LONGEST_ARCH
- 1,
1772 bfd_printable_arch_mach (a
, 0));
1773 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1775 bfd_target
*p
= bfd_target_vector
[t
];
1777 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1781 bfd_nonfatal (p
->name
);
1787 if (! bfd_set_format (abfd
, bfd_object
))
1789 if (bfd_get_error () != bfd_error_invalid_operation
)
1790 bfd_nonfatal (p
->name
);
1797 if (! bfd_set_arch_mach (abfd
, a
, 0))
1802 printf ("%s ", p
->name
);
1805 int l
= strlen (p
->name
);
1813 unlink (dummy_name
);
1816 /* Print tables of all the target-architecture combinations that
1817 BFD has been configured to support. */
1820 display_target_tables ()
1823 extern bfd_target
*bfd_target_vector
[];
1825 extern char *getenv ();
1828 colum
= getenv ("COLUMNS");
1830 columns
= atoi (colum
);
1835 while (bfd_target_vector
[t
] != NULL
)
1839 wid
= LONGEST_ARCH
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1841 while (wid
< columns
&& bfd_target_vector
[t
] != NULL
)
1845 newwid
= wid
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1846 if (newwid
>= columns
)
1851 display_info_table (oldt
, t
);
1858 printf ("BFD header file version %s\n", BFD_VERSION
);
1859 display_target_list ();
1860 display_target_tables ();
1869 char *target
= default_target
;
1870 boolean seenflag
= false;
1872 program_name
= *argv
;
1873 xmalloc_set_program_name (program_name
);
1875 START_PROGRESS (program_name
, 0);
1879 while ((c
= getopt_long (argc
, argv
, "pib:m:VdDlfahrRtTxsSj:w", long_options
,
1883 if (c
!= 'l' && c
!= OPTION_START_ADDRESS
&& c
!= OPTION_STOP_ADDRESS
)
1888 break; /* we've been given a long option */
1896 with_line_numbers
= 1;
1902 dump_file_header
= true;
1905 formats_info
= true;
1908 dump_private_headers
= 1;
1911 dump_private_headers
= 1;
1913 dump_reloc_info
= 1;
1914 dump_file_header
= true;
1916 dump_section_headers
= 1;
1922 dump_dynamic_symtab
= 1;
1928 disassemble
= disassemble_all
= true;
1932 with_source_code
= true;
1935 dump_section_contents
= 1;
1938 dump_reloc_info
= 1;
1941 dump_dynamic_reloc_info
= 1;
1947 dump_section_headers
= 1;
1957 case OPTION_START_ADDRESS
:
1958 start_address
= parse_vma (optarg
, "--start-address");
1960 case OPTION_STOP_ADDRESS
:
1961 stop_address
= parse_vma (optarg
, "--stop-address");
1970 printf ("GNU %s version %s\n", program_name
, program_version
);
1974 if (seenflag
== false)
1984 display_file ("a.out", target
);
1986 for (; optind
< argc
;)
1987 display_file (argv
[optind
++], target
);
1990 END_PROGRESS (program_name
);