1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996 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 show_raw_insn
; /* --show-raw-insn */
56 int dump_stab_section_info
; /* --stabs */
57 boolean disassemble
; /* -d */
58 boolean disassemble_all
; /* -D */
59 boolean formats_info
; /* -i */
60 char *only
; /* -j secname */
61 int wide_output
; /* -w */
62 bfd_vma start_address
= (bfd_vma
) -1; /* --start-address */
63 bfd_vma stop_address
= (bfd_vma
) -1; /* --stop-address */
64 int dump_debugging
; /* --debugging */
66 /* Extra info to pass to the disassembler address printing function. */
67 struct objdump_disasm_info
{
73 /* Architecture to disassemble for, or default if NULL. */
74 char *machine
= (char *) NULL
;
76 /* The symbol table. */
79 /* Number of symbols in `syms'. */
82 /* The sorted symbol table. */
83 asymbol
**sorted_syms
;
85 /* Number of symbols in `sorted_syms'. */
86 long sorted_symcount
= 0;
88 /* The dynamic symbol table. */
91 /* Number of symbols in `dynsyms'. */
94 /* Forward declarations. */
97 display_file
PARAMS ((char *filename
, char *target
));
100 dump_data
PARAMS ((bfd
*abfd
));
103 dump_relocs
PARAMS ((bfd
*abfd
));
106 dump_dynamic_relocs
PARAMS ((bfd
* abfd
));
109 dump_reloc_set
PARAMS ((bfd
*, arelent
**, long));
112 dump_symbols
PARAMS ((bfd
*abfd
, boolean dynamic
));
115 display_bfd
PARAMS ((bfd
*abfd
));
118 objdump_print_value
PARAMS ((bfd_vma
, FILE *));
121 objdump_print_address
PARAMS ((bfd_vma
, struct disassemble_info
*));
124 show_line
PARAMS ((bfd
*, asection
*, bfd_vma
));
127 endian_string
PARAMS ((enum bfd_endian
));
130 usage (stream
, status
)
135 Usage: %s [-ahifdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
136 [--archive-headers] [--target=bfdname] [--debugging] [--disassemble]\n\
137 [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
138 [--info] [--section=section-name] [--line-numbers] [--source]\n",
141 [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
142 [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
143 [--wide] [--version] [--help] [--private-headers]\n\
144 [--start-address=addr] [--stop-address=addr]\n\
145 [--show-raw-insn] objfile...\n\
146 at least one option besides -l (--line-numbers) must be given\n");
147 list_supported_targets (program_name
, stream
);
151 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
153 #define OPTION_START_ADDRESS (150)
154 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
156 static struct option long_options
[]=
158 {"all-headers", no_argument
, NULL
, 'x'},
159 {"private-headers", no_argument
, NULL
, 'p'},
160 {"architecture", required_argument
, NULL
, 'm'},
161 {"archive-headers", no_argument
, NULL
, 'a'},
162 {"debugging", no_argument
, &dump_debugging
, 1},
163 {"disassemble", no_argument
, NULL
, 'd'},
164 {"disassemble-all", no_argument
, NULL
, 'D'},
165 {"dynamic-reloc", no_argument
, NULL
, 'R'},
166 {"dynamic-syms", no_argument
, NULL
, 'T'},
167 {"file-headers", no_argument
, NULL
, 'f'},
168 {"full-contents", no_argument
, NULL
, 's'},
169 {"headers", no_argument
, NULL
, 'h'},
170 {"help", no_argument
, NULL
, 'H'},
171 {"info", no_argument
, NULL
, 'i'},
172 {"line-numbers", no_argument
, NULL
, 'l'},
173 {"reloc", no_argument
, NULL
, 'r'},
174 {"section", required_argument
, NULL
, 'j'},
175 {"section-headers", no_argument
, NULL
, 'h'},
176 {"show-raw-insn", no_argument
, &show_raw_insn
, 1},
177 {"source", no_argument
, NULL
, 'S'},
178 {"stabs", no_argument
, &dump_stab_section_info
, 1},
179 {"start-address", required_argument
, NULL
, OPTION_START_ADDRESS
},
180 {"stop-address", required_argument
, NULL
, OPTION_STOP_ADDRESS
},
181 {"syms", no_argument
, NULL
, 't'},
182 {"target", required_argument
, NULL
, 'b'},
183 {"version", no_argument
, &show_version
, 1},
184 {"wide", no_argument
, &wide_output
, 'w'},
185 {0, no_argument
, 0, 0}
189 dump_section_header (abfd
, section
, ignored
)
197 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
200 printf ("SECTION %d [%s]\t: size %08x",
203 (unsigned) bfd_get_section_size_before_reloc (section
));
205 printf_vma (section
->vma
);
207 printf_vma (section
->lma
);
208 printf (" align 2**%u%s ",
209 section
->alignment_power
, (wide_output
) ? "" : "\n");
210 PF (SEC_ALLOC
, "ALLOC");
211 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
212 PF (SEC_CONSTRUCTOR_TEXT
, "CONSTRUCTOR TEXT");
213 PF (SEC_CONSTRUCTOR_DATA
, "CONSTRUCTOR DATA");
214 PF (SEC_CONSTRUCTOR_BSS
, "CONSTRUCTOR BSS");
215 PF (SEC_LOAD
, "LOAD");
216 PF (SEC_RELOC
, "RELOC");
218 PF (SEC_BALIGN
, "BALIGN");
220 PF (SEC_READONLY
, "READONLY");
221 PF (SEC_CODE
, "CODE");
222 PF (SEC_DATA
, "DATA");
224 PF (SEC_DEBUGGING
, "DEBUGGING");
225 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
226 PF (SEC_EXCLUDE
, "EXCLUDE");
227 PF (SEC_SORT_ENTRIES
, "SORT ENTRIES");
236 bfd_map_over_sections (abfd
, dump_section_header
, (PTR
) NULL
);
243 asymbol
**sy
= (asymbol
**) NULL
;
246 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
248 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd
));
253 storage
= bfd_get_symtab_upper_bound (abfd
);
255 bfd_fatal (bfd_get_filename (abfd
));
259 sy
= (asymbol
**) xmalloc (storage
);
261 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
263 bfd_fatal (bfd_get_filename (abfd
));
265 fprintf (stderr
, "%s: %s: No symbols\n",
266 program_name
, bfd_get_filename (abfd
));
270 /* Read in the dynamic symbols. */
273 slurp_dynamic_symtab (abfd
)
276 asymbol
**sy
= (asymbol
**) NULL
;
279 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
282 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
284 fprintf (stderr
, "%s: %s: not a dynamic object\n",
285 program_name
, bfd_get_filename (abfd
));
290 bfd_fatal (bfd_get_filename (abfd
));
295 sy
= (asymbol
**) xmalloc (storage
);
297 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
299 bfd_fatal (bfd_get_filename (abfd
));
300 if (dynsymcount
== 0)
301 fprintf (stderr
, "%s: %s: No dynamic symbols\n",
302 program_name
, bfd_get_filename (abfd
));
306 /* Filter out (in place) symbols that are useless for disassembly.
307 COUNT is the number of elements in SYMBOLS.
308 Return the number of useful symbols. */
311 remove_useless_symbols (symbols
, count
)
315 register asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
319 asymbol
*sym
= *in_ptr
++;
321 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
323 if (sym
->flags
& (BSF_DEBUGGING
))
325 if (bfd_is_und_section (sym
->section
)
326 || bfd_is_com_section (sym
->section
))
331 return out_ptr
- symbols
;
334 /* Sort symbols into value order. */
337 compare_symbols (ap
, bp
)
341 const asymbol
*a
= *(const asymbol
**)ap
;
342 const asymbol
*b
= *(const asymbol
**)bp
;
346 flagword aflags
, bflags
;
348 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
350 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
353 if (a
->section
> b
->section
)
355 else if (a
->section
< b
->section
)
358 an
= bfd_asymbol_name (a
);
359 bn
= bfd_asymbol_name (b
);
363 /* The symbols gnu_compiled and gcc2_compiled convey no real
364 information, so put them after other symbols with the same value. */
366 af
= (strstr (an
, "gnu_compiled") != NULL
367 || strstr (an
, "gcc2_compiled") != NULL
);
368 bf
= (strstr (bn
, "gnu_compiled") != NULL
369 || strstr (bn
, "gcc2_compiled") != NULL
);
376 /* We use a heuristic for the file name, to try to sort it after
377 more useful symbols. It may not work on non Unix systems, but it
378 doesn't really matter; the only difference is precisely which
379 symbol names get printed. */
381 #define file_symbol(s, sn, snl) \
382 (((s)->flags & BSF_FILE) != 0 \
383 || ((sn)[(snl) - 2] == '.' \
384 && ((sn)[(snl) - 1] == 'o' \
385 || (sn)[(snl) - 1] == 'a')))
387 af
= file_symbol (a
, an
, anl
);
388 bf
= file_symbol (b
, bn
, bnl
);
395 /* Finally, try to sort global symbols before local symbols before
396 debugging symbols. */
401 if ((aflags
& BSF_DEBUGGING
) != (bflags
& BSF_DEBUGGING
))
403 if ((aflags
& BSF_DEBUGGING
) != 0)
408 if ((aflags
& BSF_LOCAL
) != (bflags
& BSF_LOCAL
))
410 if ((aflags
& BSF_LOCAL
) != 0)
419 /* Sort relocs into address order. */
422 compare_relocs (ap
, bp
)
426 const arelent
*a
= *(const arelent
**)ap
;
427 const arelent
*b
= *(const arelent
**)bp
;
429 if (a
->address
> b
->address
)
431 else if (a
->address
< b
->address
)
434 /* So that associated relocations tied to the same address show up
435 in the correct order, we don't do any further sorting. */
444 /* Print VMA to STREAM with no leading zeroes. */
447 objdump_print_value (vma
, stream
)
454 sprintf_vma (buf
, vma
);
455 for (p
= buf
; *p
== '0'; ++p
)
457 fprintf (stream
, "%s", p
);
460 /* Print VMA symbolically to INFO if possible. */
463 objdump_print_address (vma
, info
)
465 struct disassemble_info
*info
;
467 /* @@ Would it speed things up to cache the last two symbols returned,
468 and maybe their address ranges? For many processors, only one memory
469 operand can be present at a time, so the 2-entry cache wouldn't be
470 constantly churned by code doing heavy memory accesses. */
472 /* Indices in `sorted_syms'. */
474 long max
= sorted_symcount
;
477 fprintf_vma (info
->stream
, vma
);
479 if (sorted_symcount
< 1)
482 /* Perform a binary search looking for the closest symbol to the
483 required value. We are searching the range (min, max]. */
484 while (min
+ 1 < max
)
488 thisplace
= (max
+ min
) / 2;
489 sym
= sorted_syms
[thisplace
];
491 if (bfd_asymbol_value (sym
) > vma
)
493 else if (bfd_asymbol_value (sym
) < vma
)
502 /* The symbol we want is now in min, the low end of the range we
503 were searching. If there are several symbols with the same
504 value, we want the first one. */
507 && (bfd_asymbol_value (sorted_syms
[thisplace
])
508 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
512 /* If the file is relocateable, and the symbol could be from this
513 section, prefer a symbol from this section over symbols from
514 others, even if the other symbol's value might be closer.
516 Note that this may be wrong for some symbol references if the
517 sections have overlapping memory ranges, but in that case there's
518 no way to tell what's desired without looking at the relocation
520 struct objdump_disasm_info
*aux
;
523 aux
= (struct objdump_disasm_info
*) info
->application_data
;
524 if (sorted_syms
[thisplace
]->section
!= aux
->sec
526 || ((aux
->abfd
->flags
& HAS_RELOC
) != 0
527 && vma
>= bfd_get_section_vma (aux
->abfd
, aux
->sec
)
528 && vma
< (bfd_get_section_vma (aux
->abfd
, aux
->sec
)
529 + bfd_section_size (aux
->abfd
, aux
->sec
)))))
531 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
533 if (bfd_asymbol_value (sorted_syms
[i
])
534 != bfd_asymbol_value (sorted_syms
[thisplace
]))
540 if (sorted_syms
[i
]->section
== aux
->sec
542 || sorted_syms
[i
- 1]->section
!= aux
->sec
543 || (bfd_asymbol_value (sorted_syms
[i
])
544 != bfd_asymbol_value (sorted_syms
[i
- 1]))))
551 if (sorted_syms
[thisplace
]->section
!= aux
->sec
)
553 /* We didn't find a good symbol with a smaller value.
554 Look for one with a larger value. */
555 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
557 if (sorted_syms
[i
]->section
== aux
->sec
)
565 if (sorted_syms
[thisplace
]->section
!= aux
->sec
567 || ((aux
->abfd
->flags
& HAS_RELOC
) != 0
568 && vma
>= bfd_get_section_vma (aux
->abfd
, aux
->sec
)
569 && vma
< (bfd_get_section_vma (aux
->abfd
, aux
->sec
)
570 + bfd_section_size (aux
->abfd
, aux
->sec
)))))
574 fprintf (info
->stream
, " <%s",
575 bfd_get_section_name (aux
->abfd
, aux
->sec
));
576 secaddr
= bfd_get_section_vma (aux
->abfd
, aux
->sec
);
579 fprintf (info
->stream
, "-");
580 objdump_print_value (secaddr
- vma
, info
->stream
);
582 else if (vma
> secaddr
)
584 fprintf (info
->stream
, "+");
585 objdump_print_value (vma
- secaddr
, info
->stream
);
587 fprintf (info
->stream
, ">");
593 fprintf (info
->stream
, " <%s", sorted_syms
[thisplace
]->name
);
594 if (bfd_asymbol_value (sorted_syms
[thisplace
]) > vma
)
596 fprintf (info
->stream
, "-");
597 objdump_print_value (bfd_asymbol_value (sorted_syms
[thisplace
]) - vma
,
600 else if (vma
> bfd_asymbol_value (sorted_syms
[thisplace
]))
602 fprintf (info
->stream
, "+");
603 objdump_print_value (vma
- bfd_asymbol_value (sorted_syms
[thisplace
]),
606 fprintf (info
->stream
, ">");
609 /* Hold the last function name and the last line number we displayed
612 static char *prev_functionname
;
613 static unsigned int prev_line
;
615 /* We keep a list of all files that we have seen when doing a
616 dissassembly with source, so that we know how much of the file to
617 display. This can be important for inlined functions. */
619 struct print_file_list
621 struct print_file_list
*next
;
627 static struct print_file_list
*print_files
;
629 /* The number of preceding context lines to show when we start
630 displaying a file for the first time. */
632 #define SHOW_PRECEDING_CONTEXT_LINES (5)
634 /* Skip ahead to a given line in a file, optionally printing each
638 skip_to_line
PARAMS ((struct print_file_list
*, unsigned int, boolean
));
641 skip_to_line (p
, line
, show
)
642 struct print_file_list
*p
;
646 while (p
->line
< line
)
650 if (fgets (buf
, sizeof buf
, p
->f
) == NULL
)
660 if (strchr (buf
, '\n') != NULL
)
665 /* Show the line number, or the source line, in a dissassembly
669 show_line (abfd
, section
, off
)
674 CONST
char *filename
;
675 CONST
char *functionname
;
678 if (! with_line_numbers
&& ! with_source_code
)
681 if (! bfd_find_nearest_line (abfd
, section
, syms
, off
, &filename
,
682 &functionname
, &line
))
685 if (filename
!= NULL
&& *filename
== '\0')
687 if (functionname
!= NULL
&& *functionname
== '\0')
690 if (with_line_numbers
)
692 if (functionname
!= NULL
693 && (prev_functionname
== NULL
694 || strcmp (functionname
, prev_functionname
) != 0))
695 printf ("%s():\n", functionname
);
696 if (line
> 0 && line
!= prev_line
)
697 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
704 struct print_file_list
**pp
, *p
;
706 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
707 if (strcmp ((*pp
)->filename
, filename
) == 0)
713 if (p
!= print_files
)
717 /* We have reencountered a file name which we saw
718 earlier. This implies that either we are dumping out
719 code from an included file, or the same file was
720 linked in more than once. There are two common cases
721 of an included file: inline functions in a header
722 file, and a bison or flex skeleton file. In the
723 former case we want to just start printing (but we
724 back up a few lines to give context); in the latter
725 case we want to continue from where we left off. I
726 can't think of a good way to distinguish the cases,
727 so I used a heuristic based on the file name. */
728 if (strcmp (p
->filename
+ strlen (p
->filename
) - 2, ".h") != 0)
732 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
739 p
->f
= fopen (p
->filename
, "r");
743 skip_to_line (p
, l
, false);
745 if (print_files
->f
!= NULL
)
747 fclose (print_files
->f
);
748 print_files
->f
= NULL
;
754 skip_to_line (p
, line
, true);
756 p
->next
= print_files
;
764 f
= fopen (filename
, "r");
769 p
= ((struct print_file_list
*)
770 xmalloc (sizeof (struct print_file_list
)));
771 p
->filename
= xmalloc (strlen (filename
) + 1);
772 strcpy (p
->filename
, filename
);
776 if (print_files
!= NULL
&& print_files
->f
!= NULL
)
778 fclose (print_files
->f
);
779 print_files
->f
= NULL
;
781 p
->next
= print_files
;
784 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
787 skip_to_line (p
, l
, false);
789 skip_to_line (p
, line
, true);
794 if (functionname
!= NULL
795 && (prev_functionname
== NULL
796 || strcmp (functionname
, prev_functionname
) != 0))
798 if (prev_functionname
!= NULL
)
799 free (prev_functionname
);
800 prev_functionname
= xmalloc (strlen (functionname
) + 1);
801 strcpy (prev_functionname
, functionname
);
804 if (line
> 0 && line
!= prev_line
)
809 disassemble_data (abfd
)
813 disassembler_ftype disassemble_fn
= 0; /* New style */
814 struct disassemble_info disasm_info
;
815 struct objdump_disasm_info aux
;
817 boolean done_dot
= false;
820 prev_functionname
= NULL
;
823 /* We make a copy of syms to sort. We don't want to sort syms
824 because that will screw up the relocs. */
825 sorted_syms
= (asymbol
**) xmalloc (symcount
* sizeof (asymbol
*));
826 memcpy (sorted_syms
, syms
, symcount
* sizeof (asymbol
*));
828 sorted_symcount
= remove_useless_symbols (sorted_syms
, symcount
);
830 /* Sort the symbols into section and symbol order */
831 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
833 INIT_DISASSEMBLE_INFO(disasm_info
, stdout
);
834 disasm_info
.application_data
= (PTR
) &aux
;
836 disasm_info
.print_address_func
= objdump_print_address
;
838 disasm_info
.flags
|= DISASM_RAW_INSN_FLAG
;
840 if (machine
!= (char *) NULL
)
842 const bfd_arch_info_type
*info
= bfd_scan_arch (machine
);
845 fprintf (stderr
, "%s: Can't use supplied machine %s\n",
850 abfd
->arch_info
= info
;
853 disassemble_fn
= disassembler (abfd
);
856 fprintf (stderr
, "%s: Can't disassemble for architecture %s\n",
858 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
862 disasm_info
.arch
= bfd_get_arch (abfd
);
863 disasm_info
.mach
= bfd_get_mach (abfd
);
864 if (bfd_big_endian (abfd
))
865 disasm_info
.endian
= BFD_ENDIAN_BIG
;
866 else if (bfd_little_endian (abfd
))
867 disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
869 /* ??? Aborting here seems too drastic. We could default to big or little
871 disasm_info
.endian
= BFD_ENDIAN_UNKNOWN
;
873 for (section
= abfd
->sections
;
874 section
!= (asection
*) NULL
;
875 section
= section
->next
)
877 bfd_byte
*data
= NULL
;
878 bfd_size_type datasize
= 0;
879 arelent
**relbuf
= NULL
;
880 arelent
**relpp
= NULL
;
881 arelent
**relppend
= NULL
;
884 if ((section
->flags
& SEC_LOAD
) == 0
885 || (! disassemble_all
887 && (section
->flags
& SEC_CODE
) == 0))
889 if (only
!= (char *) NULL
&& strcmp (only
, section
->name
) != 0)
893 && (section
->flags
& SEC_RELOC
) != 0)
897 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
899 bfd_fatal (bfd_get_filename (abfd
));
905 relbuf
= (arelent
**) xmalloc (relsize
);
906 relcount
= bfd_canonicalize_reloc (abfd
, section
, relbuf
, syms
);
908 bfd_fatal (bfd_get_filename (abfd
));
910 /* Sort the relocs by address. */
911 qsort (relbuf
, relcount
, sizeof (arelent
*), compare_relocs
);
914 relppend
= relpp
+ relcount
;
918 printf ("Disassembly of section %s:\n", section
->name
);
920 datasize
= bfd_get_section_size_before_reloc (section
);
924 data
= (bfd_byte
*) xmalloc ((size_t) datasize
);
926 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
929 disasm_info
.buffer
= data
;
930 disasm_info
.buffer_vma
= section
->vma
;
931 disasm_info
.buffer_length
= datasize
;
932 if (start_address
== (bfd_vma
) -1
933 || start_address
< disasm_info
.buffer_vma
)
936 i
= start_address
- disasm_info
.buffer_vma
;
937 if (stop_address
== (bfd_vma
) -1)
941 if (stop_address
< disasm_info
.buffer_vma
)
944 stop
= stop_address
- disasm_info
.buffer_vma
;
945 if (stop
> disasm_info
.buffer_length
)
946 stop
= disasm_info
.buffer_length
;
951 boolean need_nl
= false;
953 if (data
[i
] == 0 && data
[i
+ 1] == 0 && data
[i
+ 2] == 0 &&
956 if (done_dot
== false)
966 if (with_line_numbers
|| with_source_code
)
967 show_line (abfd
, section
, i
);
968 aux
.require_sec
= true;
969 objdump_print_address (section
->vma
+ i
, &disasm_info
);
970 aux
.require_sec
= false;
973 bytes
= (*disassemble_fn
) (section
->vma
+ i
, &disasm_info
);
984 && (section
->flags
& SEC_RELOC
) != 0)
986 while (relpp
< relppend
987 && ((*relpp
)->address
>= (bfd_vma
) i
988 && (*relpp
)->address
< (bfd_vma
) i
+ bytes
))
991 const char *sym_name
;
995 printf ("\t\tRELOC: ");
997 printf_vma (section
->vma
+ q
->address
);
999 printf (" %s ", q
->howto
->name
);
1001 if (q
->sym_ptr_ptr
!= NULL
1002 && *q
->sym_ptr_ptr
!= NULL
)
1004 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
1005 if (sym_name
== NULL
|| *sym_name
== '\0')
1009 sym_sec
= bfd_get_section (*q
->sym_ptr_ptr
);
1010 sym_name
= bfd_get_section_name (abfd
, sym_sec
);
1011 if (sym_name
== NULL
|| *sym_name
== '\0')
1012 sym_name
= "*unknown*";
1016 printf ("%s", sym_name
);
1021 printf_vma (q
->addend
);
1044 /* Define a table of stab values and print-strings. We wish the initializer
1045 could be a direct-mapped table, but instead we build one the first
1055 struct stab_print stab_print
[] = {
1056 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
1057 #include "aout/stab.def"
1058 #undef __define_stab
1062 void dump_section_stabs
PARAMS ((bfd
*abfd
, char *stabsect_name
,
1063 char *strsect_name
));
1065 /* Dump the stabs sections from an object file that has a section that
1066 uses Sun stabs encoding. It has to use some hooks into BFD because
1067 string table sections are not normally visible to BFD callers. */
1073 /* Allocate and initialize stab name array if first time. */
1074 if (stab_name
== NULL
)
1078 stab_name
= (char **) xmalloc (256 * sizeof(char *));
1079 /* Clear the array. */
1080 for (i
= 0; i
< 256; i
++)
1081 stab_name
[i
] = NULL
;
1082 /* Fill in the defined stabs. */
1083 for (i
= 0; *stab_print
[i
].string
; i
++)
1084 stab_name
[stab_print
[i
].value
] = stab_print
[i
].string
;
1087 dump_section_stabs (abfd
, ".stab", ".stabstr");
1088 dump_section_stabs (abfd
, ".stab.excl", ".stab.exclstr");
1089 dump_section_stabs (abfd
, ".stab.index", ".stab.indexstr");
1090 dump_section_stabs (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1093 static struct internal_nlist
*stabs
;
1094 static bfd_size_type stab_size
;
1096 static char *strtab
;
1097 static bfd_size_type stabstr_size
;
1099 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1100 and string table section STRSECT_NAME into `strtab'.
1101 If the section exists and was read, allocate the space and return true.
1102 Otherwise return false. */
1105 read_section_stabs (abfd
, stabsect_name
, strsect_name
)
1107 char *stabsect_name
;
1110 asection
*stabsect
, *stabstrsect
;
1112 stabsect
= bfd_get_section_by_name (abfd
, stabsect_name
);
1115 printf ("No %s section present\n\n", stabsect_name
);
1119 stabstrsect
= bfd_get_section_by_name (abfd
, strsect_name
);
1120 if (0 == stabstrsect
)
1122 fprintf (stderr
, "%s: %s has no %s section\n", program_name
,
1123 bfd_get_filename (abfd
), strsect_name
);
1127 stab_size
= bfd_section_size (abfd
, stabsect
);
1128 stabstr_size
= bfd_section_size (abfd
, stabstrsect
);
1130 stabs
= (struct internal_nlist
*) xmalloc (stab_size
);
1131 strtab
= (char *) xmalloc (stabstr_size
);
1133 if (! bfd_get_section_contents (abfd
, stabsect
, (PTR
) stabs
, 0, stab_size
))
1135 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1136 program_name
, stabsect_name
, bfd_get_filename (abfd
),
1137 bfd_errmsg (bfd_get_error ()));
1143 if (! bfd_get_section_contents (abfd
, stabstrsect
, (PTR
) strtab
, 0,
1146 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1147 program_name
, strsect_name
, bfd_get_filename (abfd
),
1148 bfd_errmsg (bfd_get_error ()));
1157 #define SWAP_SYMBOL(symp, abfd) \
1159 (symp)->n_strx = bfd_h_get_32(abfd, \
1160 (unsigned char *)&(symp)->n_strx); \
1161 (symp)->n_desc = bfd_h_get_16 (abfd, \
1162 (unsigned char *)&(symp)->n_desc); \
1163 (symp)->n_value = bfd_h_get_32 (abfd, \
1164 (unsigned char *)&(symp)->n_value); \
1167 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1168 using string table section STRSECT_NAME (in `strtab'). */
1171 print_section_stabs (abfd
, stabsect_name
, strsect_name
)
1173 char *stabsect_name
;
1177 unsigned file_string_table_offset
= 0, next_file_string_table_offset
= 0;
1178 struct internal_nlist
*stabp
= stabs
,
1179 *stabs_end
= (struct internal_nlist
*) (stab_size
+ (char *) stabs
);
1181 printf ("Contents of %s section:\n\n", stabsect_name
);
1182 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1184 /* Loop through all symbols and print them.
1186 We start the index at -1 because there is a dummy symbol on
1187 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1189 for (i
= -1; stabp
< stabs_end
; stabp
++, i
++)
1191 SWAP_SYMBOL (stabp
, abfd
);
1192 printf ("\n%-6d ", i
);
1193 /* Either print the stab name, or, if unnamed, print its number
1194 again (makes consistent formatting for tools like awk). */
1195 if (stab_name
[stabp
->n_type
])
1196 printf ("%-6s", stab_name
[stabp
->n_type
]);
1197 else if (stabp
->n_type
== N_UNDF
)
1200 printf ("%-6d", stabp
->n_type
);
1201 printf (" %-6d %-6d ", stabp
->n_other
, stabp
->n_desc
);
1202 printf_vma (stabp
->n_value
);
1203 printf (" %-6lu", stabp
->n_strx
);
1205 /* Symbols with type == 0 (N_UNDF) specify the length of the
1206 string table associated with this file. We use that info
1207 to know how to relocate the *next* file's string table indices. */
1209 if (stabp
->n_type
== N_UNDF
)
1211 file_string_table_offset
= next_file_string_table_offset
;
1212 next_file_string_table_offset
+= stabp
->n_value
;
1216 /* Using the (possibly updated) string table offset, print the
1217 string (if any) associated with this symbol. */
1219 if ((stabp
->n_strx
+ file_string_table_offset
) < stabstr_size
)
1220 printf (" %s", &strtab
[stabp
->n_strx
+ file_string_table_offset
]);
1229 dump_section_stabs (abfd
, stabsect_name
, strsect_name
)
1231 char *stabsect_name
;
1236 /* Check for section names for which stabsect_name is a prefix, to
1237 handle .stab0, etc. */
1238 for (s
= abfd
->sections
;
1242 if (strncmp (stabsect_name
, s
->name
, strlen (stabsect_name
)) == 0
1243 && strncmp (strsect_name
, s
->name
, strlen (strsect_name
)) != 0)
1245 if (read_section_stabs (abfd
, s
->name
, strsect_name
))
1247 print_section_stabs (abfd
, s
->name
, strsect_name
);
1256 dump_bfd_header (abfd
)
1261 printf ("architecture: %s, ",
1262 bfd_printable_arch_mach (bfd_get_arch (abfd
),
1263 bfd_get_mach (abfd
)));
1264 printf ("flags 0x%08x:\n", abfd
->flags
);
1266 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1267 PF (HAS_RELOC
, "HAS_RELOC");
1268 PF (EXEC_P
, "EXEC_P");
1269 PF (HAS_LINENO
, "HAS_LINENO");
1270 PF (HAS_DEBUG
, "HAS_DEBUG");
1271 PF (HAS_SYMS
, "HAS_SYMS");
1272 PF (HAS_LOCALS
, "HAS_LOCALS");
1273 PF (DYNAMIC
, "DYNAMIC");
1274 PF (WP_TEXT
, "WP_TEXT");
1275 PF (D_PAGED
, "D_PAGED");
1276 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
1277 printf ("\nstart address 0x");
1278 printf_vma (abfd
->start_address
);
1282 dump_bfd_private_header (abfd
)
1285 bfd_print_private_bfd_data (abfd
, stdout
);
1293 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
1295 bfd_nonfatal (bfd_get_filename (abfd
));
1296 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1298 list_matching_formats (matching
);
1304 printf ("\n%s: file format %s\n", bfd_get_filename (abfd
),
1307 print_arelt_descr (stdout
, abfd
, true);
1308 if (dump_file_header
)
1309 dump_bfd_header (abfd
);
1310 if (dump_private_headers
)
1311 dump_bfd_private_header (abfd
);
1313 if (dump_section_headers
)
1314 dump_headers (abfd
);
1315 if (dump_symtab
|| dump_reloc_info
|| disassemble
|| dump_debugging
)
1317 syms
= slurp_symtab (abfd
);
1319 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
)
1321 dynsyms
= slurp_dynamic_symtab (abfd
);
1324 dump_symbols (abfd
, false);
1325 if (dump_dynamic_symtab
)
1326 dump_symbols (abfd
, true);
1327 if (dump_stab_section_info
)
1329 if (dump_reloc_info
&& ! disassemble
)
1331 if (dump_dynamic_reloc_info
)
1332 dump_dynamic_relocs (abfd
);
1333 if (dump_section_contents
)
1336 disassemble_data (abfd
);
1341 dhandle
= read_debugging_info (abfd
, syms
, symcount
);
1342 if (dhandle
!= NULL
)
1344 if (! print_debugging_info (stdout
, dhandle
))
1345 fprintf (stderr
, "%s: printing debugging information failed\n",
1346 bfd_get_filename (abfd
));
1362 display_file (filename
, target
)
1366 bfd
*file
, *arfile
= (bfd
*) NULL
;
1368 file
= bfd_openr (filename
, target
);
1371 bfd_nonfatal (filename
);
1375 if (bfd_check_format (file
, bfd_archive
) == true)
1377 bfd
*last_arfile
= NULL
;
1379 printf ("In archive %s:\n", bfd_get_filename (file
));
1382 bfd_set_error (bfd_error_no_error
);
1384 arfile
= bfd_openr_next_archived_file (file
, arfile
);
1387 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1389 bfd_nonfatal (bfd_get_filename (file
));
1394 display_bfd (arfile
);
1396 if (last_arfile
!= NULL
)
1397 bfd_close (last_arfile
);
1398 last_arfile
= arfile
;
1401 if (last_arfile
!= NULL
)
1402 bfd_close (last_arfile
);
1410 /* Actually display the various requested regions */
1418 bfd_size_type datasize
= 0;
1420 bfd_size_type start
, stop
;
1422 for (section
= abfd
->sections
; section
!= NULL
; section
=
1427 if (only
== (char *) NULL
||
1428 strcmp (only
, section
->name
) == 0)
1430 if (section
->flags
& SEC_HAS_CONTENTS
)
1432 printf ("Contents of section %s:\n", section
->name
);
1434 if (bfd_section_size (abfd
, section
) == 0)
1436 data
= (bfd_byte
*) xmalloc ((size_t) bfd_section_size (abfd
, section
));
1437 datasize
= bfd_section_size (abfd
, section
);
1440 bfd_get_section_contents (abfd
, section
, (PTR
) data
, 0, bfd_section_size (abfd
, section
));
1442 if (start_address
== (bfd_vma
) -1
1443 || start_address
< section
->vma
)
1446 start
= start_address
- section
->vma
;
1447 if (stop_address
== (bfd_vma
) -1)
1448 stop
= bfd_section_size (abfd
, section
);
1451 if (stop_address
< section
->vma
)
1454 stop
= stop_address
- section
->vma
;
1455 if (stop
> bfd_section_size (abfd
, section
))
1456 stop
= bfd_section_size (abfd
, section
);
1458 for (i
= start
; i
< stop
; i
+= onaline
)
1462 printf (" %04lx ", (unsigned long int) (i
+ section
->vma
));
1463 for (j
= i
; j
< i
+ onaline
; j
++)
1466 printf ("%02x", (unsigned) (data
[j
]));
1474 for (j
= i
; j
< i
+ onaline
; j
++)
1479 printf ("%c", isprint (data
[j
]) ? data
[j
] : '.');
1489 /* Should perhaps share code and display with nm? */
1491 dump_symbols (abfd
, dynamic
)
1505 printf ("DYNAMIC SYMBOL TABLE:\n");
1513 printf ("SYMBOL TABLE:\n");
1516 for (count
= 0; count
< max
; count
++)
1520 bfd
*cur_bfd
= bfd_asymbol_bfd(*current
);
1523 bfd_print_symbol (cur_bfd
,
1525 *current
, bfd_print_symbol_all
);
1543 for (a
= abfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1547 if (bfd_is_abs_section (a
))
1549 if (bfd_is_und_section (a
))
1551 if (bfd_is_com_section (a
))
1556 if (strcmp (only
, a
->name
))
1559 else if ((a
->flags
& SEC_RELOC
) == 0)
1562 relsize
= bfd_get_reloc_upper_bound (abfd
, a
);
1564 bfd_fatal (bfd_get_filename (abfd
));
1566 printf ("RELOCATION RECORDS FOR [%s]:", a
->name
);
1570 printf (" (none)\n\n");
1574 relpp
= (arelent
**) xmalloc (relsize
);
1575 relcount
= bfd_canonicalize_reloc (abfd
, a
, relpp
, syms
);
1577 bfd_fatal (bfd_get_filename (abfd
));
1578 else if (relcount
== 0)
1580 printf (" (none)\n\n");
1585 dump_reloc_set (abfd
, relpp
, relcount
);
1594 dump_dynamic_relocs (abfd
)
1601 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
1603 bfd_fatal (bfd_get_filename (abfd
));
1605 printf ("DYNAMIC RELOCATION RECORDS");
1609 printf (" (none)\n\n");
1613 relpp
= (arelent
**) xmalloc (relsize
);
1614 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
1616 bfd_fatal (bfd_get_filename (abfd
));
1617 else if (relcount
== 0)
1619 printf (" (none)\n\n");
1624 dump_reloc_set (abfd
, relpp
, relcount
);
1632 dump_reloc_set (abfd
, relpp
, relcount
)
1639 /* Get column headers lined up reasonably. */
1645 sprintf_vma (buf
, (bfd_vma
) -1);
1646 width
= strlen (buf
) - 7;
1648 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
1651 for (p
= relpp
; relcount
&& *p
!= (arelent
*) NULL
; p
++, relcount
--)
1654 CONST
char *sym_name
;
1655 CONST
char *section_name
;
1657 if (start_address
!= (bfd_vma
) -1
1658 && q
->address
< start_address
)
1660 if (stop_address
!= (bfd_vma
) -1
1661 && q
->address
> stop_address
)
1664 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
1666 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
1667 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
1672 section_name
= NULL
;
1676 printf_vma (q
->address
);
1677 printf (" %-16s %s",
1683 if (section_name
== (CONST
char *) NULL
)
1684 section_name
= "*unknown*";
1685 printf_vma (q
->address
);
1686 printf (" %-16s [%s]",
1693 printf_vma (q
->addend
);
1699 /* The length of the longest architecture name + 1. */
1700 #define LONGEST_ARCH sizeof("rs6000:6000")
1707 endian_string (endian
)
1708 enum bfd_endian endian
;
1710 if (endian
== BFD_ENDIAN_BIG
)
1711 return "big endian";
1712 else if (endian
== BFD_ENDIAN_LITTLE
)
1713 return "little endian";
1715 return "endianness unknown";
1718 /* List the targets that BFD is configured to support, each followed
1719 by its endianness and the architectures it supports. */
1722 display_target_list ()
1724 extern char *tmpnam ();
1725 extern bfd_target
*bfd_target_vector
[];
1726 char tmparg
[L_tmpnam
];
1730 dummy_name
= tmpnam (tmparg
);
1731 for (t
= 0; bfd_target_vector
[t
]; t
++)
1733 bfd_target
*p
= bfd_target_vector
[t
];
1734 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1737 printf ("%s\n (header %s, data %s)\n", p
->name
,
1738 endian_string (p
->header_byteorder
),
1739 endian_string (p
->byteorder
));
1743 bfd_nonfatal (dummy_name
);
1747 if (! bfd_set_format (abfd
, bfd_object
))
1749 if (bfd_get_error () != bfd_error_invalid_operation
)
1750 bfd_nonfatal (p
->name
);
1754 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1755 if (bfd_set_arch_mach (abfd
, (enum bfd_architecture
) a
, 0))
1757 bfd_printable_arch_mach ((enum bfd_architecture
) a
, 0));
1759 unlink (dummy_name
);
1762 /* Print a table showing which architectures are supported for entries
1763 FIRST through LAST-1 of bfd_target_vector (targets across,
1764 architectures down). */
1767 display_info_table (first
, last
)
1771 extern bfd_target
*bfd_target_vector
[];
1772 extern char *tmpnam ();
1773 char tmparg
[L_tmpnam
];
1777 /* Print heading of target names. */
1778 printf ("\n%*s", (int) LONGEST_ARCH
, " ");
1779 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1780 printf ("%s ", bfd_target_vector
[t
]->name
);
1783 dummy_name
= tmpnam (tmparg
);
1784 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1785 if (strcmp (bfd_printable_arch_mach (a
, 0), "UNKNOWN!") != 0)
1787 printf ("%*s ", (int) LONGEST_ARCH
- 1,
1788 bfd_printable_arch_mach (a
, 0));
1789 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1791 bfd_target
*p
= bfd_target_vector
[t
];
1793 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1797 bfd_nonfatal (p
->name
);
1803 if (! bfd_set_format (abfd
, bfd_object
))
1805 if (bfd_get_error () != bfd_error_invalid_operation
)
1806 bfd_nonfatal (p
->name
);
1813 if (! bfd_set_arch_mach (abfd
, a
, 0))
1818 printf ("%s ", p
->name
);
1821 int l
= strlen (p
->name
);
1829 unlink (dummy_name
);
1832 /* Print tables of all the target-architecture combinations that
1833 BFD has been configured to support. */
1836 display_target_tables ()
1839 extern bfd_target
*bfd_target_vector
[];
1841 extern char *getenv ();
1844 colum
= getenv ("COLUMNS");
1846 columns
= atoi (colum
);
1851 while (bfd_target_vector
[t
] != NULL
)
1855 wid
= LONGEST_ARCH
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1857 while (wid
< columns
&& bfd_target_vector
[t
] != NULL
)
1861 newwid
= wid
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1862 if (newwid
>= columns
)
1867 display_info_table (oldt
, t
);
1874 printf ("BFD header file version %s\n", BFD_VERSION
);
1875 display_target_list ();
1876 display_target_tables ();
1885 char *target
= default_target
;
1886 boolean seenflag
= false;
1888 program_name
= *argv
;
1889 xmalloc_set_program_name (program_name
);
1891 START_PROGRESS (program_name
, 0);
1895 while ((c
= getopt_long (argc
, argv
, "pib:m:VdDlfahrRtTxsSj:w", long_options
,
1899 if (c
!= 'l' && c
!= OPTION_START_ADDRESS
&& c
!= OPTION_STOP_ADDRESS
)
1904 break; /* we've been given a long option */
1912 with_line_numbers
= 1;
1918 dump_file_header
= true;
1921 formats_info
= true;
1924 dump_private_headers
= 1;
1927 dump_private_headers
= 1;
1929 dump_reloc_info
= 1;
1930 dump_file_header
= true;
1932 dump_section_headers
= 1;
1938 dump_dynamic_symtab
= 1;
1944 disassemble
= disassemble_all
= true;
1948 with_source_code
= true;
1951 dump_section_contents
= 1;
1954 dump_reloc_info
= 1;
1957 dump_dynamic_reloc_info
= 1;
1963 dump_section_headers
= 1;
1973 case OPTION_START_ADDRESS
:
1974 start_address
= parse_vma (optarg
, "--start-address");
1976 case OPTION_STOP_ADDRESS
:
1977 stop_address
= parse_vma (optarg
, "--stop-address");
1986 printf ("GNU %s version %s\n", program_name
, program_version
);
1990 if (seenflag
== false)
2000 display_file ("a.out", target
);
2002 for (; optind
< argc
;)
2003 display_file (argv
[optind
++], target
);
2006 END_PROGRESS (program_name
);