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
;
331 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
333 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
336 if (a
->section
> b
->section
)
338 else if (a
->section
< b
->section
)
341 an
= bfd_asymbol_name (a
);
342 bn
= bfd_asymbol_name (b
);
346 /* The symbols gnu_compiled and gcc2_compiled convey no real
347 information, so put them after other symbols with the same value. */
349 af
= (strstr (an
, "gnu_compiled") != NULL
350 || strstr (an
, "gcc2_compiled") != NULL
);
351 bf
= (strstr (bn
, "gnu_compiled") != NULL
352 || strstr (bn
, "gcc2_compiled") != NULL
);
359 /* We use a heuristic for the file name, to try to sort it after
360 more useful symbols. It may not work on non Unix systems, but it
361 doesn't really matter; the only difference is precisely which
362 symbol names get printed. */
364 #define file_symbol(s, sn, snl) \
365 (((s)->flags & BSF_FILE) != 0 \
366 || ((sn)[(snl) - 2] == '.' \
367 && ((sn)[(snl) - 1] == 'o' \
368 || (sn)[(snl) - 1] == 'a')))
370 af
= file_symbol (a
, an
, anl
);
371 bf
= file_symbol (b
, bn
, bnl
);
381 /* Sort relocs into address order. */
384 compare_relocs (ap
, bp
)
388 const arelent
*a
= *(const arelent
**)ap
;
389 const arelent
*b
= *(const arelent
**)bp
;
391 if (a
->address
> b
->address
)
393 else if (a
->address
< b
->address
)
396 /* So that associated relocations tied to the same address show up
397 in the correct order, we don't do any further sorting. */
406 /* Print VMA symbolically to INFO if possible. */
409 objdump_print_address (vma
, info
)
411 struct disassemble_info
*info
;
413 /* @@ For relocateable files, should filter out symbols belonging to
414 the wrong section. Unfortunately, not enough information is supplied
415 to this routine to determine the correct section in all cases. */
416 /* @@ Would it speed things up to cache the last two symbols returned,
417 and maybe their address ranges? For many processors, only one memory
418 operand can be present at a time, so the 2-entry cache wouldn't be
419 constantly churned by code doing heavy memory accesses. */
421 /* Indices in `sorted_syms'. */
423 long max
= sorted_symcount
;
426 fprintf_vma (info
->stream
, vma
);
428 if (sorted_symcount
< 1)
431 /* Perform a binary search looking for the closest symbol to the
432 required value. We are searching the range (min, max]. */
433 while (min
+ 1 < max
)
437 thisplace
= (max
+ min
) / 2;
438 sym
= sorted_syms
[thisplace
];
440 if (bfd_asymbol_value (sym
) > vma
)
442 else if (bfd_asymbol_value (sym
) < vma
)
451 /* The symbol we want is now in min, the low end of the range we
455 && (bfd_asymbol_value (sorted_syms
[thisplace
])
456 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
460 /* If this symbol isn't global, search for one with the same value
462 bfd_vma val
= bfd_asymbol_value (sorted_syms
[thisplace
]);
464 if (sorted_syms
[thisplace
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
465 for (i
= thisplace
- 1; i
>= 0; i
--)
467 if (bfd_asymbol_value (sorted_syms
[i
]) == val
468 && (!(sorted_syms
[i
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
469 || ((sorted_syms
[thisplace
]->flags
& BSF_DEBUGGING
)
470 && !(sorted_syms
[i
]->flags
& BSF_DEBUGGING
))))
476 if (sorted_syms
[thisplace
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
477 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
479 if (bfd_asymbol_value (sorted_syms
[i
]) == val
480 && (!(sorted_syms
[i
]->flags
& (BSF_LOCAL
|BSF_DEBUGGING
))
481 || ((sorted_syms
[thisplace
]->flags
& BSF_DEBUGGING
)
482 && !(sorted_syms
[i
]->flags
& BSF_DEBUGGING
))))
490 /* If the file is relocateable, and the symbol could be from this
491 section, prefer a symbol from this section over symbols from
492 others, even if the other symbol's value might be closer.
494 Note that this may be wrong for some symbol references if the
495 sections have overlapping memory ranges, but in that case there's
496 no way to tell what's desired without looking at the relocation
498 struct objdump_disasm_info
*aux
;
501 aux
= (struct objdump_disasm_info
*) info
->application_data
;
502 if (sorted_syms
[thisplace
]->section
!= aux
->sec
504 || ((aux
->abfd
->flags
& HAS_RELOC
) != 0
505 && vma
>= bfd_get_section_vma (aux
->abfd
, aux
->sec
)
506 && vma
< (bfd_get_section_vma (aux
->abfd
, aux
->sec
)
507 + bfd_get_section_size_before_reloc (aux
->sec
)))))
509 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
511 if (bfd_asymbol_value (sorted_syms
[i
])
512 != bfd_asymbol_value (sorted_syms
[thisplace
]))
518 if (sorted_syms
[i
]->section
== aux
->sec
520 || sorted_syms
[i
- 1]->section
!= aux
->sec
521 || (bfd_asymbol_value (sorted_syms
[i
])
522 != bfd_asymbol_value (sorted_syms
[i
- 1]))))
529 if (sorted_syms
[thisplace
]->section
!= aux
->sec
)
531 /* We didn't find a good symbol with a smaller value.
532 Look for one with a larger value. */
533 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
535 if (sorted_syms
[i
]->section
== aux
->sec
)
545 fprintf (info
->stream
, " <%s", sorted_syms
[thisplace
]->name
);
546 if (bfd_asymbol_value (sorted_syms
[thisplace
]) > vma
)
548 char buf
[30], *p
= buf
;
549 sprintf_vma (buf
, bfd_asymbol_value (sorted_syms
[thisplace
]) - vma
);
552 fprintf (info
->stream
, "-%s", p
);
554 else if (vma
> bfd_asymbol_value (sorted_syms
[thisplace
]))
556 char buf
[30], *p
= buf
;
557 sprintf_vma (buf
, vma
- bfd_asymbol_value (sorted_syms
[thisplace
]));
560 fprintf (info
->stream
, "+%s", p
);
562 fprintf (info
->stream
, ">");
565 /* Hold the last function name and the last line number we displayed
568 static char *prev_functionname
;
569 static unsigned int prev_line
;
571 /* We keep a list of all files that we have seen when doing a
572 dissassembly with source, so that we know how much of the file to
573 display. This can be important for inlined functions. */
575 struct print_file_list
577 struct print_file_list
*next
;
583 static struct print_file_list
*print_files
;
585 /* The number of preceding context lines to show when we start
586 displaying a file for the first time. */
588 #define SHOW_PRECEDING_CONTEXT_LINES (5)
590 /* Skip ahead to a given line in a file, optionally printing each
594 skip_to_line
PARAMS ((struct print_file_list
*, unsigned int, boolean
));
597 skip_to_line (p
, line
, show
)
598 struct print_file_list
*p
;
602 while (p
->line
< line
)
606 if (fgets (buf
, sizeof buf
, p
->f
) == NULL
)
616 if (strchr (buf
, '\n') != NULL
)
621 /* Show the line number, or the source line, in a dissassembly
625 show_line (abfd
, section
, off
)
630 CONST
char *filename
;
631 CONST
char *functionname
;
634 if (! with_line_numbers
&& ! with_source_code
)
637 if (! bfd_find_nearest_line (abfd
, section
, syms
, off
, &filename
,
638 &functionname
, &line
))
641 if (filename
!= NULL
&& *filename
== '\0')
643 if (functionname
!= NULL
&& *functionname
== '\0')
646 if (with_line_numbers
)
648 if (functionname
!= NULL
649 && (prev_functionname
== NULL
650 || strcmp (functionname
, prev_functionname
) != 0))
651 printf ("%s():\n", functionname
);
652 if (line
> 0 && line
!= prev_line
)
653 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
660 struct print_file_list
**pp
, *p
;
662 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
663 if (strcmp ((*pp
)->filename
, filename
) == 0)
669 if (p
!= print_files
)
673 /* We have reencountered a file name which we saw
674 earlier. This implies that either we are dumping out
675 code from an included file, or the same file was
676 linked in more than once. There are two common cases
677 of an included file: inline functions in a header
678 file, and a bison or flex skeleton file. In the
679 former case we want to just start printing (but we
680 back up a few lines to give context); in the latter
681 case we want to continue from where we left off. I
682 can't think of a good way to distinguish the cases,
683 so I used a heuristic based on the file name. */
684 if (strcmp (p
->filename
+ strlen (p
->filename
) - 2, ".h") != 0)
688 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
695 p
->f
= fopen (p
->filename
, "r");
699 skip_to_line (p
, l
, false);
701 if (print_files
->f
!= NULL
)
703 fclose (print_files
->f
);
704 print_files
->f
= NULL
;
710 skip_to_line (p
, line
, true);
712 p
->next
= print_files
;
720 f
= fopen (filename
, "r");
725 p
= ((struct print_file_list
*)
726 xmalloc (sizeof (struct print_file_list
)));
727 p
->filename
= xmalloc (strlen (filename
) + 1);
728 strcpy (p
->filename
, filename
);
732 if (print_files
!= NULL
&& print_files
->f
!= NULL
)
734 fclose (print_files
->f
);
735 print_files
->f
= NULL
;
737 p
->next
= print_files
;
740 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
743 skip_to_line (p
, l
, false);
745 skip_to_line (p
, line
, true);
750 if (functionname
!= NULL
751 && (prev_functionname
== NULL
752 || strcmp (functionname
, prev_functionname
) != 0))
754 if (prev_functionname
!= NULL
)
755 free (prev_functionname
);
756 prev_functionname
= xmalloc (strlen (functionname
) + 1);
757 strcpy (prev_functionname
, functionname
);
760 if (line
> 0 && line
!= prev_line
)
765 disassemble_data (abfd
)
769 disassembler_ftype disassemble_fn
= 0; /* New style */
770 struct disassemble_info disasm_info
;
771 struct objdump_disasm_info aux
;
773 boolean done_dot
= false;
776 prev_functionname
= NULL
;
779 /* We make a copy of syms to sort. We don't want to sort syms
780 because that will screw up the relocs. */
781 sorted_syms
= (asymbol
**) xmalloc (symcount
* sizeof (asymbol
*));
782 memcpy (sorted_syms
, syms
, symcount
* sizeof (asymbol
*));
784 sorted_symcount
= remove_useless_symbols (sorted_syms
, symcount
);
786 /* Sort the symbols into section and symbol order */
787 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
789 INIT_DISASSEMBLE_INFO(disasm_info
, stdout
);
790 disasm_info
.application_data
= (PTR
) &aux
;
792 disasm_info
.print_address_func
= objdump_print_address
;
794 if (machine
!= (char *) NULL
)
796 const bfd_arch_info_type
*info
= bfd_scan_arch (machine
);
799 fprintf (stderr
, "%s: Can't use supplied machine %s\n",
804 abfd
->arch_info
= info
;
807 disassemble_fn
= disassembler (abfd
);
810 fprintf (stderr
, "%s: Can't disassemble for architecture %s\n",
812 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
816 for (section
= abfd
->sections
;
817 section
!= (asection
*) NULL
;
818 section
= section
->next
)
820 bfd_byte
*data
= NULL
;
821 bfd_size_type datasize
= 0;
822 arelent
**relbuf
= NULL
;
823 arelent
**relpp
= NULL
;
824 arelent
**relppend
= NULL
;
827 if ((section
->flags
& SEC_LOAD
) == 0
828 || (! disassemble_all
830 && (section
->flags
& SEC_CODE
) == 0))
832 if (only
!= (char *) NULL
&& strcmp (only
, section
->name
) != 0)
836 && (section
->flags
& SEC_RELOC
) != 0)
840 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
842 bfd_fatal (bfd_get_filename (abfd
));
848 relbuf
= (arelent
**) xmalloc (relsize
);
849 relcount
= bfd_canonicalize_reloc (abfd
, section
, relbuf
, syms
);
851 bfd_fatal (bfd_get_filename (abfd
));
853 /* Sort the relocs by address. */
854 qsort (relbuf
, relcount
, sizeof (arelent
*), compare_relocs
);
857 relppend
= relpp
+ relcount
;
861 printf ("Disassembly of section %s:\n", section
->name
);
863 datasize
= bfd_get_section_size_before_reloc (section
);
867 data
= (bfd_byte
*) xmalloc ((size_t) datasize
);
869 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
872 disasm_info
.buffer
= data
;
873 disasm_info
.buffer_vma
= section
->vma
;
874 disasm_info
.buffer_length
= datasize
;
875 if (start_address
== (bfd_vma
) -1
876 || start_address
< disasm_info
.buffer_vma
)
879 i
= start_address
- disasm_info
.buffer_vma
;
880 if (stop_address
== (bfd_vma
) -1)
884 if (stop_address
< disasm_info
.buffer_vma
)
887 stop
= stop_address
- disasm_info
.buffer_vma
;
888 if (stop
> disasm_info
.buffer_length
)
889 stop
= disasm_info
.buffer_length
;
894 boolean need_nl
= false;
896 if (data
[i
] == 0 && data
[i
+ 1] == 0 && data
[i
+ 2] == 0 &&
899 if (done_dot
== false)
909 if (with_line_numbers
|| with_source_code
)
910 show_line (abfd
, section
, i
);
911 aux
.require_sec
= true;
912 objdump_print_address (section
->vma
+ i
, &disasm_info
);
913 aux
.require_sec
= false;
916 bytes
= (*disassemble_fn
) (section
->vma
+ i
, &disasm_info
);
927 && (section
->flags
& SEC_RELOC
) != 0)
929 while (relpp
< relppend
930 && ((*relpp
)->address
>= (bfd_vma
) i
931 && (*relpp
)->address
< (bfd_vma
) i
+ bytes
))
934 const char *sym_name
;
938 printf ("\t\tRELOC: ");
940 printf_vma (section
->vma
+ q
->address
);
942 printf (" %s ", q
->howto
->name
);
944 if (q
->sym_ptr_ptr
!= NULL
945 && *q
->sym_ptr_ptr
!= NULL
)
947 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
948 if (sym_name
== NULL
|| *sym_name
== '\0')
952 sym_sec
= bfd_get_section (*q
->sym_ptr_ptr
);
953 sym_name
= bfd_get_section_name (abfd
, sym_sec
);
954 if (sym_name
== NULL
|| *sym_name
== '\0')
955 sym_name
= "*unknown*";
959 printf ("%s", sym_name
);
964 printf_vma (q
->addend
);
986 /* Define a table of stab values and print-strings. We wish the initializer
987 could be a direct-mapped table, but instead we build one the first
997 struct stab_print stab_print
[] = {
998 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
999 #include "aout/stab.def"
1000 #undef __define_stab
1004 void dump_section_stabs
PARAMS ((bfd
*abfd
, char *stabsect_name
,
1005 char *strsect_name
));
1007 /* Dump the stabs sections from an object file that has a section that
1008 uses Sun stabs encoding. It has to use some hooks into BFD because
1009 string table sections are not normally visible to BFD callers. */
1015 /* Allocate and initialize stab name array if first time. */
1016 if (stab_name
== NULL
)
1020 stab_name
= (char **) xmalloc (256 * sizeof(char *));
1021 /* Clear the array. */
1022 for (i
= 0; i
< 256; i
++)
1023 stab_name
[i
] = NULL
;
1024 /* Fill in the defined stabs. */
1025 for (i
= 0; *stab_print
[i
].string
; i
++)
1026 stab_name
[stab_print
[i
].value
] = stab_print
[i
].string
;
1029 dump_section_stabs (abfd
, ".stab", ".stabstr");
1030 dump_section_stabs (abfd
, ".stab.excl", ".stab.exclstr");
1031 dump_section_stabs (abfd
, ".stab.index", ".stab.indexstr");
1032 dump_section_stabs (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1035 static struct internal_nlist
*stabs
;
1036 static bfd_size_type stab_size
;
1038 static char *strtab
;
1039 static bfd_size_type stabstr_size
;
1041 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1042 and string table section STRSECT_NAME into `strtab'.
1043 If the section exists and was read, allocate the space and return true.
1044 Otherwise return false. */
1047 read_section_stabs (abfd
, stabsect_name
, strsect_name
)
1049 char *stabsect_name
;
1052 asection
*stabsect
, *stabstrsect
;
1054 stabsect
= bfd_get_section_by_name (abfd
, stabsect_name
);
1057 printf ("No %s section present\n\n", stabsect_name
);
1061 stabstrsect
= bfd_get_section_by_name (abfd
, strsect_name
);
1062 if (0 == stabstrsect
)
1064 fprintf (stderr
, "%s: %s has no %s section\n", program_name
,
1065 bfd_get_filename (abfd
), strsect_name
);
1069 stab_size
= bfd_section_size (abfd
, stabsect
);
1070 stabstr_size
= bfd_section_size (abfd
, stabstrsect
);
1072 stabs
= (struct internal_nlist
*) xmalloc (stab_size
);
1073 strtab
= (char *) xmalloc (stabstr_size
);
1075 if (! bfd_get_section_contents (abfd
, stabsect
, (PTR
) stabs
, 0, stab_size
))
1077 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1078 program_name
, stabsect_name
, bfd_get_filename (abfd
),
1079 bfd_errmsg (bfd_get_error ()));
1085 if (! bfd_get_section_contents (abfd
, stabstrsect
, (PTR
) strtab
, 0,
1088 fprintf (stderr
, "%s: Reading %s section of %s failed: %s\n",
1089 program_name
, strsect_name
, bfd_get_filename (abfd
),
1090 bfd_errmsg (bfd_get_error ()));
1099 #define SWAP_SYMBOL(symp, abfd) \
1101 (symp)->n_strx = bfd_h_get_32(abfd, \
1102 (unsigned char *)&(symp)->n_strx); \
1103 (symp)->n_desc = bfd_h_get_16 (abfd, \
1104 (unsigned char *)&(symp)->n_desc); \
1105 (symp)->n_value = bfd_h_get_32 (abfd, \
1106 (unsigned char *)&(symp)->n_value); \
1109 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1110 using string table section STRSECT_NAME (in `strtab'). */
1113 print_section_stabs (abfd
, stabsect_name
, strsect_name
)
1115 char *stabsect_name
;
1119 unsigned file_string_table_offset
= 0, next_file_string_table_offset
= 0;
1120 struct internal_nlist
*stabp
= stabs
,
1121 *stabs_end
= (struct internal_nlist
*) (stab_size
+ (char *) stabs
);
1123 printf ("Contents of %s section:\n\n", stabsect_name
);
1124 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1126 /* Loop through all symbols and print them.
1128 We start the index at -1 because there is a dummy symbol on
1129 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1131 for (i
= -1; stabp
< stabs_end
; stabp
++, i
++)
1133 SWAP_SYMBOL (stabp
, abfd
);
1134 printf ("\n%-6d ", i
);
1135 /* Either print the stab name, or, if unnamed, print its number
1136 again (makes consistent formatting for tools like awk). */
1137 if (stab_name
[stabp
->n_type
])
1138 printf ("%-6s", stab_name
[stabp
->n_type
]);
1139 else if (stabp
->n_type
== N_UNDF
)
1142 printf ("%-6d", stabp
->n_type
);
1143 printf (" %-6d %-6d ", stabp
->n_other
, stabp
->n_desc
);
1144 printf_vma (stabp
->n_value
);
1145 printf (" %-6lu", stabp
->n_strx
);
1147 /* Symbols with type == 0 (N_UNDF) specify the length of the
1148 string table associated with this file. We use that info
1149 to know how to relocate the *next* file's string table indices. */
1151 if (stabp
->n_type
== N_UNDF
)
1153 file_string_table_offset
= next_file_string_table_offset
;
1154 next_file_string_table_offset
+= stabp
->n_value
;
1158 /* Using the (possibly updated) string table offset, print the
1159 string (if any) associated with this symbol. */
1161 if ((stabp
->n_strx
+ file_string_table_offset
) < stabstr_size
)
1162 printf (" %s", &strtab
[stabp
->n_strx
+ file_string_table_offset
]);
1171 dump_section_stabs (abfd
, stabsect_name
, strsect_name
)
1173 char *stabsect_name
;
1178 /* Check for section names for which stabsect_name is a prefix, to
1179 handle .stab0, etc. */
1180 for (s
= abfd
->sections
;
1184 if (strncmp (stabsect_name
, s
->name
, strlen (stabsect_name
)) == 0
1185 && strncmp (strsect_name
, s
->name
, strlen (strsect_name
)) != 0)
1187 if (read_section_stabs (abfd
, s
->name
, strsect_name
))
1189 print_section_stabs (abfd
, s
->name
, strsect_name
);
1198 dump_bfd_header (abfd
)
1203 printf ("architecture: %s, ",
1204 bfd_printable_arch_mach (bfd_get_arch (abfd
),
1205 bfd_get_mach (abfd
)));
1206 printf ("flags 0x%08x:\n", abfd
->flags
);
1208 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1209 PF (HAS_RELOC
, "HAS_RELOC");
1210 PF (EXEC_P
, "EXEC_P");
1211 PF (HAS_LINENO
, "HAS_LINENO");
1212 PF (HAS_DEBUG
, "HAS_DEBUG");
1213 PF (HAS_SYMS
, "HAS_SYMS");
1214 PF (HAS_LOCALS
, "HAS_LOCALS");
1215 PF (DYNAMIC
, "DYNAMIC");
1216 PF (WP_TEXT
, "WP_TEXT");
1217 PF (D_PAGED
, "D_PAGED");
1218 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
1219 printf ("\nstart address 0x");
1220 printf_vma (abfd
->start_address
);
1224 dump_bfd_private_header (abfd
)
1227 bfd_print_private_bfd_data (abfd
, stdout
);
1235 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
1237 bfd_nonfatal (bfd_get_filename (abfd
));
1238 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1240 list_matching_formats (matching
);
1246 printf ("\n%s: file format %s\n", bfd_get_filename (abfd
),
1249 print_arelt_descr (stdout
, abfd
, true);
1250 if (dump_file_header
)
1251 dump_bfd_header (abfd
);
1252 if (dump_private_headers
)
1253 dump_bfd_private_header (abfd
);
1255 if (dump_section_headers
)
1256 dump_headers (abfd
);
1257 if (dump_symtab
|| dump_reloc_info
|| disassemble
)
1259 syms
= slurp_symtab (abfd
);
1261 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
)
1263 dynsyms
= slurp_dynamic_symtab (abfd
);
1266 dump_symbols (abfd
, false);
1267 if (dump_dynamic_symtab
)
1268 dump_symbols (abfd
, true);
1269 if (dump_stab_section_info
)
1271 if (dump_reloc_info
&& ! disassemble
)
1273 if (dump_dynamic_reloc_info
)
1274 dump_dynamic_relocs (abfd
);
1275 if (dump_section_contents
)
1278 disassemble_data (abfd
);
1282 display_file (filename
, target
)
1286 bfd
*file
, *arfile
= (bfd
*) NULL
;
1288 file
= bfd_openr (filename
, target
);
1291 bfd_nonfatal (filename
);
1295 if (bfd_check_format (file
, bfd_archive
) == true)
1297 bfd
*last_arfile
= NULL
;
1299 printf ("In archive %s:\n", bfd_get_filename (file
));
1302 bfd_set_error (bfd_error_no_error
);
1304 arfile
= bfd_openr_next_archived_file (file
, arfile
);
1307 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1309 bfd_nonfatal (bfd_get_filename (file
));
1314 display_bfd (arfile
);
1316 if (last_arfile
!= NULL
)
1317 bfd_close (last_arfile
);
1318 last_arfile
= arfile
;
1321 if (last_arfile
!= NULL
)
1322 bfd_close (last_arfile
);
1330 /* Actually display the various requested regions */
1338 bfd_size_type datasize
= 0;
1340 bfd_size_type start
, stop
;
1342 for (section
= abfd
->sections
; section
!= NULL
; section
=
1347 if (only
== (char *) NULL
||
1348 strcmp (only
, section
->name
) == 0)
1350 if (section
->flags
& SEC_HAS_CONTENTS
)
1352 printf ("Contents of section %s:\n", section
->name
);
1354 if (bfd_section_size (abfd
, section
) == 0)
1356 data
= (bfd_byte
*) xmalloc ((size_t) bfd_section_size (abfd
, section
));
1357 datasize
= bfd_section_size (abfd
, section
);
1360 bfd_get_section_contents (abfd
, section
, (PTR
) data
, 0, bfd_section_size (abfd
, section
));
1362 if (start_address
== (bfd_vma
) -1
1363 || start_address
< section
->vma
)
1366 start
= start_address
- section
->vma
;
1367 if (stop_address
== (bfd_vma
) -1)
1368 stop
= bfd_section_size (abfd
, section
);
1371 if (stop_address
< section
->vma
)
1374 stop
= stop_address
- section
->vma
;
1375 if (stop
> bfd_section_size (abfd
, section
))
1376 stop
= bfd_section_size (abfd
, section
);
1378 for (i
= start
; i
< stop
; i
+= onaline
)
1382 printf (" %04lx ", (unsigned long int) (i
+ section
->vma
));
1383 for (j
= i
; j
< i
+ onaline
; j
++)
1386 printf ("%02x", (unsigned) (data
[j
]));
1394 for (j
= i
; j
< i
+ onaline
; j
++)
1399 printf ("%c", isprint (data
[j
]) ? data
[j
] : '.');
1409 /* Should perhaps share code and display with nm? */
1411 dump_symbols (abfd
, dynamic
)
1425 printf ("DYNAMIC SYMBOL TABLE:\n");
1433 printf ("SYMBOL TABLE:\n");
1436 for (count
= 0; count
< max
; count
++)
1440 bfd
*cur_bfd
= bfd_asymbol_bfd(*current
);
1443 bfd_print_symbol (cur_bfd
,
1445 *current
, bfd_print_symbol_all
);
1463 for (a
= abfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1467 if (bfd_is_abs_section (a
))
1469 if (bfd_is_und_section (a
))
1471 if (bfd_is_com_section (a
))
1476 if (strcmp (only
, a
->name
))
1479 else if ((a
->flags
& SEC_RELOC
) == 0)
1482 printf ("RELOCATION RECORDS FOR [%s]:", a
->name
);
1484 relsize
= bfd_get_reloc_upper_bound (abfd
, a
);
1486 bfd_fatal (bfd_get_filename (abfd
));
1490 printf (" (none)\n\n");
1494 relpp
= (arelent
**) xmalloc (relsize
);
1495 relcount
= bfd_canonicalize_reloc (abfd
, a
, relpp
, syms
);
1497 bfd_fatal (bfd_get_filename (abfd
));
1498 else if (relcount
== 0)
1500 printf (" (none)\n\n");
1505 dump_reloc_set (abfd
, relpp
, relcount
);
1514 dump_dynamic_relocs (abfd
)
1521 printf ("DYNAMIC RELOCATION RECORDS");
1523 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
1525 bfd_fatal (bfd_get_filename (abfd
));
1529 printf (" (none)\n\n");
1533 relpp
= (arelent
**) xmalloc (relsize
);
1534 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
1536 bfd_fatal (bfd_get_filename (abfd
));
1537 else if (relcount
== 0)
1539 printf (" (none)\n\n");
1544 dump_reloc_set (abfd
, relpp
, relcount
);
1552 dump_reloc_set (abfd
, relpp
, relcount
)
1559 /* Get column headers lined up reasonably. */
1565 sprintf_vma (buf
, (bfd_vma
) -1);
1566 width
= strlen (buf
) - 7;
1568 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
1571 for (p
= relpp
; relcount
&& *p
!= (arelent
*) NULL
; p
++, relcount
--)
1574 CONST
char *sym_name
;
1575 CONST
char *section_name
;
1577 if (start_address
!= (bfd_vma
) -1
1578 && q
->address
< start_address
)
1580 if (stop_address
!= (bfd_vma
) -1
1581 && q
->address
> stop_address
)
1584 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
1586 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
1587 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
1592 section_name
= NULL
;
1596 printf_vma (q
->address
);
1597 printf (" %-16s %s",
1603 if (section_name
== (CONST
char *) NULL
)
1604 section_name
= "*unknown*";
1605 printf_vma (q
->address
);
1606 printf (" %-16s [%s]",
1613 printf_vma (q
->addend
);
1619 /* The length of the longest architecture name + 1. */
1620 #define LONGEST_ARCH sizeof("rs6000:6000")
1626 /* List the targets that BFD is configured to support, each followed
1627 by its endianness and the architectures it supports. */
1630 display_target_list ()
1632 extern char *tmpnam ();
1633 extern bfd_target
*bfd_target_vector
[];
1634 char tmparg
[L_tmpnam
];
1638 dummy_name
= tmpnam (tmparg
);
1639 for (t
= 0; bfd_target_vector
[t
]; t
++)
1641 bfd_target
*p
= bfd_target_vector
[t
];
1642 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1645 printf ("%s\n (header %s, data %s)\n", p
->name
,
1646 p
->header_byteorder_big_p
? "big endian" : "little endian",
1647 p
->byteorder_big_p
? "big endian" : "little endian");
1651 bfd_nonfatal (dummy_name
);
1655 if (! bfd_set_format (abfd
, bfd_object
))
1657 if (bfd_get_error () != bfd_error_invalid_operation
)
1658 bfd_nonfatal (p
->name
);
1662 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1663 if (bfd_set_arch_mach (abfd
, (enum bfd_architecture
) a
, 0))
1665 bfd_printable_arch_mach ((enum bfd_architecture
) a
, 0));
1667 unlink (dummy_name
);
1670 /* Print a table showing which architectures are supported for entries
1671 FIRST through LAST-1 of bfd_target_vector (targets across,
1672 architectures down). */
1675 display_info_table (first
, last
)
1679 extern bfd_target
*bfd_target_vector
[];
1680 extern char *tmpnam ();
1681 char tmparg
[L_tmpnam
];
1685 /* Print heading of target names. */
1686 printf ("\n%*s", (int) LONGEST_ARCH
, " ");
1687 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1688 printf ("%s ", bfd_target_vector
[t
]->name
);
1691 dummy_name
= tmpnam (tmparg
);
1692 for (a
= (int) bfd_arch_obscure
+ 1; a
< (int) bfd_arch_last
; a
++)
1693 if (strcmp (bfd_printable_arch_mach (a
, 0), "UNKNOWN!") != 0)
1695 printf ("%*s ", (int) LONGEST_ARCH
- 1,
1696 bfd_printable_arch_mach (a
, 0));
1697 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
1699 bfd_target
*p
= bfd_target_vector
[t
];
1701 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
1705 bfd_nonfatal (p
->name
);
1711 if (! bfd_set_format (abfd
, bfd_object
))
1713 if (bfd_get_error () != bfd_error_invalid_operation
)
1714 bfd_nonfatal (p
->name
);
1721 if (! bfd_set_arch_mach (abfd
, a
, 0))
1726 printf ("%s ", p
->name
);
1729 int l
= strlen (p
->name
);
1737 unlink (dummy_name
);
1740 /* Print tables of all the target-architecture combinations that
1741 BFD has been configured to support. */
1744 display_target_tables ()
1747 extern bfd_target
*bfd_target_vector
[];
1749 extern char *getenv ();
1752 colum
= getenv ("COLUMNS");
1754 columns
= atoi (colum
);
1759 while (bfd_target_vector
[t
] != NULL
)
1763 wid
= LONGEST_ARCH
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1765 while (wid
< columns
&& bfd_target_vector
[t
] != NULL
)
1769 newwid
= wid
+ strlen (bfd_target_vector
[t
]->name
) + 1;
1770 if (newwid
>= columns
)
1775 display_info_table (oldt
, t
);
1782 printf ("BFD header file version %s\n", BFD_VERSION
);
1783 display_target_list ();
1784 display_target_tables ();
1793 char *target
= default_target
;
1794 boolean seenflag
= false;
1796 program_name
= *argv
;
1797 xmalloc_set_program_name (program_name
);
1799 START_PROGRESS (program_name
, 0);
1803 while ((c
= getopt_long (argc
, argv
, "pib:m:VdDlfahrRtTxsSj:w", long_options
,
1807 if (c
!= 'l' && c
!= OPTION_START_ADDRESS
&& c
!= OPTION_STOP_ADDRESS
)
1812 break; /* we've been given a long option */
1820 with_line_numbers
= 1;
1826 dump_file_header
= true;
1829 formats_info
= true;
1832 dump_private_headers
= 1;
1835 dump_private_headers
= 1;
1837 dump_reloc_info
= 1;
1838 dump_file_header
= true;
1840 dump_section_headers
= 1;
1846 dump_dynamic_symtab
= 1;
1852 disassemble
= disassemble_all
= true;
1856 with_source_code
= true;
1859 dump_section_contents
= 1;
1862 dump_reloc_info
= 1;
1865 dump_dynamic_reloc_info
= 1;
1871 dump_section_headers
= 1;
1881 case OPTION_START_ADDRESS
:
1882 start_address
= parse_vma (optarg
, "--start-address");
1884 case OPTION_STOP_ADDRESS
:
1885 stop_address
= parse_vma (optarg
, "--stop-address");
1894 printf ("GNU %s version %s\n", program_name
, program_version
);
1898 if (seenflag
== false)
1908 display_file ("a.out", target
);
1910 for (; optind
< argc
;)
1911 display_file (argv
[optind
++], target
);
1914 END_PROGRESS (program_name
);