1 /* objdump.c -- dump information about an object file.
2 Copyright (C) 1990, 1991 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Diddler.
6 BFD 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 BFD 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 BFD; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 * Until there is other documentation, refer to the manual page dump(1) in
22 * the system 5 program's reference manual
31 #define ELF_STAB_DISPLAY /* This code works, but uses internal
32 bfd and elf stuff. Flip this define
33 off if you need to just use generic
36 #ifdef ELF_STAB_DISPLAY
37 /* Internal headers for the ELF .stab-dump code - sorry. */
38 #define BYTES_IN_WORD 32
39 #include "aout/aout64.h"
40 #include "elf/internal.h"
41 extern Elf_Internal_Shdr
*bfd_elf_find_section();
42 #endif /* ELF_STAB_DISPLAY */
46 char *default_target
= NULL
; /* default at runtime */
48 extern *program_version
;
49 char *program_name
= NULL
;
51 int show_version
= 0; /* show the version number */
52 int dump_section_contents
; /* -s */
53 int dump_section_headers
; /* -h */
54 boolean dump_file_header
; /* -f */
55 int dump_symtab
; /* -t */
56 int dump_reloc_info
; /* -r */
57 int dump_ar_hdrs
; /* -a */
58 int with_line_numbers
; /* -l */
59 int dump_stab_section_info
; /* -stabs */
60 boolean disassemble
; /* -d */
61 boolean info
; /* -i */
64 PROTO (void, display_file
, (char *filename
, char *target
));
65 PROTO (void, dump_data
, (bfd
* abfd
));
66 PROTO (void, dump_relocs
, (bfd
* abfd
));
67 PROTO (void, dump_symbols
, (bfd
* abfd
));
68 PROTO (void, print_arelt_descr
, (FILE *, bfd
* abfd
, boolean verbose
));
76 char *machine
= (char *) NULL
;
83 unsigned int symcount
= 0;
89 "usage: %s [-ahifdrtxsl] [-m machine] [-j section_name] obj ...\n",
94 static struct option long_options
[]=
96 {"syms", no_argument
, &dump_symtab
, 1},
97 {"reloc", no_argument
, &dump_reloc_info
, 1},
98 {"header", no_argument
, &dump_section_headers
, 1},
99 {"version", no_argument
, &show_version
, 1},
100 #ifdef ELF_STAB_DISPLAY
101 {"stabs", no_argument
, &dump_stab_section_info
, 1},
103 {0, no_argument
, 0, 0}};
112 for (section
= abfd
->sections
;
113 section
!= (asection
*) NULL
;
114 section
= section
->next
)
119 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
122 printf ("SECTION %d [%s]\t: size %08x",
125 (unsigned) bfd_get_section_size_before_reloc (section
));
127 printf_vma (section
->vma
);
128 printf (" align 2**%u\n ",
129 section
->alignment_power
);
130 PF (SEC_ALLOC
, "ALLOC");
131 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
132 PF (SEC_CONSTRUCTOR_TEXT
, "CONSTRUCTOR TEXT");
133 PF (SEC_CONSTRUCTOR_DATA
, "CONSTRUCTOR DATA");
134 PF (SEC_CONSTRUCTOR_BSS
, "CONSTRUCTOR BSS");
135 PF (SEC_LOAD
, "LOAD");
136 PF (SEC_RELOC
, "RELOC");
137 PF (SEC_BALIGN
, "BALIGN");
138 PF (SEC_READONLY
, "READONLY");
139 PF (SEC_CODE
, "CODE");
140 PF (SEC_DATA
, "DATA");
148 DEFUN (slurp_symtab
, (abfd
),
151 asymbol
**sy
= (asymbol
**) NULL
;
153 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
155 (void) printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd
));
159 storage
= get_symtab_upper_bound (abfd
);
162 sy
= (asymbol
**) malloc (storage
);
165 fprintf (stderr
, "%s: out of memory.\n", program_name
);
169 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
173 /* Sort symbols into value order */
179 asymbol
*a
= *(asymbol
**)ap
;
180 asymbol
*b
= *(asymbol
**)bp
;
183 if (a
->name
== (char *) NULL
|| (a
->flags
& (BSF_DEBUGGING
)))
185 if (b
->name
== (char *) NULL
|| (b
->flags
& (BSF_DEBUGGING
)))
188 diff
= a
->the_bfd
- b
->the_bfd
;
193 diff
= a
->value
- b
->value
;
198 return a
->section
- b
->section
;
201 /* Print the supplied address symbolically if possible */
203 print_address (vma
, stream
)
207 /* Perform a binary search looking for the closest symbol to
208 the required value */
210 unsigned int min
= 0;
211 unsigned int max
= symcount
;
213 unsigned int thisplace
= 1;
214 unsigned int oldthisplace
;
220 fprintf_vma (stream
, vma
);
226 oldthisplace
= thisplace
;
227 thisplace
= (max
+ min
) / 2;
228 if (thisplace
== oldthisplace
)
230 vardiff
= syms
[thisplace
]->value
- vma
;
245 /* Totally awesome! the exact right symbol */
246 CONST
char *match_name
= syms
[thisplace
]->name
;
247 int sym_len
= strlen (match_name
);
249 /* Avoid "filename.o" as a match */
251 && match_name
[sym_len
- 2] == '.'
252 && match_name
[sym_len
- 1] == 'o'
253 && thisplace
+ 1 < symcount
254 && syms
[thisplace
+ 1]->value
== vma
)
255 match_name
= syms
[thisplace
+ 1]->name
;
256 /* Totally awesome! the exact right symbol */
257 fprintf_vma (stream
, vma
);
258 fprintf (stream
, " (%s+)0000", syms
[thisplace
]->name
);
262 /* We've run out of places to look, print the symbol before this one
263 see if this or the symbol before describes this location the best */
267 if (syms
[thisplace
- 1]->value
- vma
>
268 syms
[thisplace
]->value
- vma
)
270 /* Previous symbol is in correct section and is closer */
275 fprintf_vma (stream
, vma
);
276 if (syms
[thisplace
]->value
> vma
)
278 fprintf (stream
, " (%s-)", syms
[thisplace
]->name
);
279 fprintf (stream
, "%04x", syms
[thisplace
]->value
- vma
);
283 fprintf (stream
, " (%s+)", syms
[thisplace
]->name
);
284 fprintf (stream
, "%04x", vma
- syms
[thisplace
]->value
);
290 disassemble_data (abfd
)
293 bfd_byte
*data
= NULL
;
294 bfd_arch_info_type
*info
;
295 bfd_size_type datasize
= 0;
297 unsigned int (*print
) ()= 0;
298 unsigned int print_insn_m68k ();
299 unsigned int print_insn_a29k ();
300 unsigned int print_insn_i960 ();
301 unsigned int print_insn_sparc ();
302 unsigned int print_insn_i386 ();
303 unsigned int print_insn_h8300 ();
304 enum bfd_architecture a
;
308 /* Replace symbol section relative values with abs values */
309 boolean done_dot
= false;
311 for (i
= 0; i
< symcount
; i
++)
313 syms
[i
]->value
+= syms
[i
]->section
->vma
;
316 /* We keep a copy of the symbols in the original order */
317 syms2
= slurp_symtab (abfd
);
319 /* Sort the symbols into section and symbol order */
320 (void) qsort (syms
, symcount
, sizeof (asymbol
*), comp
);
322 /* Find the first useless symbol */
326 for (i
= 0; i
< symcount
; i
++)
328 if (syms
[i
]->the_bfd
== 0)
337 if (machine
!= (char *) NULL
)
339 info
= bfd_scan_arch (machine
);
342 fprintf (stderr
, "%s: Can't use supplied machine %s\n",
347 abfd
->arch_info
= info
;
350 /* See if we can disassemble using bfd */
352 if (abfd
->arch_info
->disassemble
)
354 print
= abfd
->arch_info
->disassemble
;
358 a
= bfd_get_arch (abfd
);
362 print
= print_insn_sparc
;
365 print
= print_insn_i386
;
368 print
= print_insn_m68k
;
371 print
= print_insn_a29k
;
374 print
= print_insn_i960
;
377 fprintf (stderr
, "%s: Can't disassemble for architecture %s\n",
379 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
385 for (section
= abfd
->sections
;
386 section
!= (asection
*) NULL
;
387 section
= section
->next
)
390 if ((section
->flags
& SEC_LOAD
)
391 && (only
== (char *) NULL
|| strcmp (only
, section
->name
) == 0))
393 printf ("Disassembly of section %s:\n", section
->name
);
395 if (bfd_get_section_size_before_reloc (section
) == 0)
398 data
= (bfd_byte
*) malloc (bfd_get_section_size_before_reloc (section
));
400 if (data
== (bfd_byte
*) NULL
)
402 fprintf (stderr
, "%s: memory exhausted.\n", program_name
);
405 datasize
= bfd_get_section_size_before_reloc (section
);
407 bfd_get_section_contents (abfd
, section
, data
, 0, bfd_get_section_size_before_reloc (section
));
410 while (i
< bfd_get_section_size_before_reloc (section
))
412 if (data
[i
] == 0 && data
[i
+ 1] == 0 && data
[i
+ 2] == 0 &&
415 if (done_dot
== false)
425 if (with_line_numbers
)
428 CONST
char *filename
;
429 CONST
char *functionname
;
432 bfd_find_nearest_line (abfd
,
440 if (filename
&& functionname
&& line
&& line
!= prevline
)
442 printf ("%s:%u\n", filename
, line
);
446 print_address (section
->vma
+ i
, stdout
);
449 i
+= print (section
->vma
+ i
,
460 #ifdef ELF_STAB_DISPLAY
462 /* Define a table of stab values and print-strings. We wish the initializer
463 could be a direct-mapped table, but instead we build one the first
466 #define STAB_STRING_LENGTH 6
468 char stab_name
[256][STAB_STRING_LENGTH
];
472 char string
[STAB_STRING_LENGTH
];
475 struct stab_print stab_print
[] = {
476 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
477 #include "aout/stab.def"
482 void dump_elf_stabs_1 ();
484 /* This is a kludge for dumping the stabs section from an ELF file that
485 uses Sun stabs encoding. It has to use some hooks into BFD because
486 string table sections are not normally visible to BFD callers. */
489 dump_elf_stabs (abfd
)
494 /* Initialize stab name array if first time. */
495 if (stab_name
[0][0] == 0)
497 /* Fill in numeric values for all possible strings. */
498 for (i
= 0; i
< 256; i
++)
500 sprintf (stab_name
[i
], "%d", i
);
502 for (i
= 0; stab_print
[i
].string
[0]; i
++)
503 strcpy (stab_name
[stab_print
[i
].value
], stab_print
[i
].string
);
506 if (0 != strncmp ("elf", abfd
->xvec
->name
, 3))
508 fprintf (stderr
, "%s: %s is not in ELF format.\n", program_name
,
513 dump_elf_stabs_1 (abfd
, ".stab", ".stabstr");
514 dump_elf_stabs_1 (abfd
, ".stab.excl", ".stab.exclstr");
515 dump_elf_stabs_1 (abfd
, ".stab.index", ".stab.indexstr");
519 dump_elf_stabs_1 (abfd
, name1
, name2
)
521 char *name1
; /* Section name of .stab */
522 char *name2
; /* Section name of its string section */
524 Elf_Internal_Shdr
*stab_hdr
, *stabstr_hdr
;
526 struct internal_nlist
*stabs
, *stabs_end
;
528 unsigned file_string_table_offset
, next_file_string_table_offset
;
530 stab_hdr
= bfd_elf_find_section (abfd
, name1
);
533 printf ("Contents of %s section: none.\n\n", name1
);
537 stabstr_hdr
= bfd_elf_find_section (abfd
, name2
);
538 if (0 == stabstr_hdr
)
540 fprintf (stderr
, "%s: %s has no %s section.\n", program_name
,
541 abfd
->filename
, name2
);
545 stabs
= (struct internal_nlist
*) xmalloc (stab_hdr
->sh_size
);
546 strtab
= (char *) xmalloc (stabstr_hdr
->sh_size
);
547 stabs_end
= (struct internal_nlist
*) (stab_hdr
->sh_size
+ (char *)stabs
);
549 if (bfd_seek (abfd
, stab_hdr
->sh_offset
, L_SET
) < 0 ||
550 stab_hdr
->sh_size
!= bfd_read ((PTR
)stabs
, stab_hdr
->sh_size
, 1, abfd
))
552 fprintf (stderr
, "%s: reading %s section of %s failed.\n",
558 if (bfd_seek (abfd
, stabstr_hdr
->sh_offset
, L_SET
) < 0 ||
559 stabstr_hdr
->sh_size
!= bfd_read ((PTR
)strtab
, stabstr_hdr
->sh_size
,
562 fprintf (stderr
, "%s: reading %s section of %s failed.\n",
568 #define SWAP_SYMBOL(symp, abfd) \
570 (symp)->n_strx = bfd_h_get_32(abfd, \
571 (unsigned char *)&(symp)->n_strx); \
572 (symp)->n_desc = bfd_h_get_16 (abfd, \
573 (unsigned char *)&(symp)->n_desc); \
574 (symp)->n_value = bfd_h_get_32 (abfd, \
575 (unsigned char *)&(symp)->n_value); \
578 printf ("Contents of %s section:\n\n", name1
);
579 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
581 file_string_table_offset
= 0;
582 next_file_string_table_offset
= 0;
584 /* Loop through all symbols and print them.
586 We start the index at -1 because there is a dummy symbol on
587 the front of Sun's stabs-in-elf sections. */
589 for (i
= -1; stabs
< stabs_end
; stabs
++, i
++)
591 SWAP_SYMBOL (stabs
, abfd
);
592 printf ("\n%-6d %-6s %-6d %-6d %08x %-6d", i
,
593 stab_name
[stabs
->n_type
],
594 stabs
->n_other
, stabs
->n_desc
, stabs
->n_value
,
597 /* Symbols with type == 0 (N_UNDF) specify the length of the
598 string table associated with this file. We use that info
599 to know how to relocate the *next* file's string table indices. */
601 if (stabs
->n_type
== N_UNDF
)
603 file_string_table_offset
= next_file_string_table_offset
;
604 next_file_string_table_offset
+= stabs
->n_value
;
607 /* Now, using the possibly updated string table offset, print the
608 string (if any) associated with this symbol. */
610 if ((stabs
->n_strx
+ file_string_table_offset
) < stabstr_hdr
->sh_size
)
611 printf (" %s", &strtab
[stabs
->n_strx
+ file_string_table_offset
]);
617 #endif /* ELF_STAB_DISPLAY */
623 if (!bfd_check_format (abfd
, bfd_object
))
625 fprintf (stderr
, "%s: %s not an object file\n", program_name
,
629 printf ("\n%s: file format %s\n", abfd
->filename
, abfd
->xvec
->name
);
631 print_arelt_descr (stdout
, abfd
, true);
633 if (dump_file_header
)
637 printf ("architecture: %s, ",
638 bfd_printable_arch_mach (bfd_get_arch (abfd
),
639 bfd_get_mach (abfd
)));
640 printf ("flags 0x%08x:\n", abfd
->flags
);
642 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
643 PF (HAS_RELOC
, "HAS_RELOC");
644 PF (EXEC_P
, "EXEC_P");
645 PF (HAS_LINENO
, "HAS_LINENO");
646 PF (HAS_DEBUG
, "HAS_DEBUG");
647 PF (HAS_SYMS
, "HAS_SYMS");
648 PF (HAS_LOCALS
, "HAS_LOCALS");
649 PF (DYNAMIC
, "DYNAMIC");
650 PF (WP_TEXT
, "WP_TEXT");
651 PF (D_PAGED
, "D_PAGED");
652 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
653 printf ("\nstart address 0x");
654 printf_vma (abfd
->start_address
);
658 if (dump_section_headers
)
660 if (dump_symtab
|| dump_reloc_info
|| disassemble
)
662 syms
= slurp_symtab (abfd
);
666 #ifdef ELF_STAB_DISPLAY
667 if (dump_stab_section_info
)
668 dump_elf_stabs (abfd
);
672 if (dump_section_contents
)
675 disassemble_data (abfd
);
679 display_file (filename
, target
)
683 bfd
*file
, *arfile
= (bfd
*) NULL
;
685 file
= bfd_openr (filename
, target
);
688 bfd_perror (filename
);
692 if (bfd_check_format (file
, bfd_archive
) == true)
694 printf ("In archive %s:\n", bfd_get_filename (file
));
697 bfd_error
= no_error
;
699 arfile
= bfd_openr_next_archived_file (file
, arfile
);
702 if (bfd_error
!= no_more_archived_files
)
703 bfd_perror (bfd_get_filename (file
));
707 display_bfd (arfile
);
708 /* Don't close the archive elements; we need them for next_archive */
717 /* Actually display the various requested regions */
725 bfd_size_type datasize
= 0;
728 for (section
= abfd
->sections
; section
!= NULL
; section
=
733 if (only
== (char *) NULL
||
734 strcmp (only
, section
->name
) == 0)
736 if (section
->flags
& SEC_HAS_CONTENTS
)
738 printf ("Contents of section %s:\n", section
->name
);
740 if (bfd_get_section_size_before_reloc (section
) == 0)
742 data
= (bfd_byte
*) malloc (bfd_get_section_size_before_reloc (section
));
743 if (data
== (bfd_byte
*) NULL
)
745 fprintf (stderr
, "%s: memory exhausted.\n", program_name
);
748 datasize
= bfd_get_section_size_before_reloc (section
);
751 bfd_get_section_contents (abfd
, section
, (PTR
) data
, 0, bfd_get_section_size_before_reloc (section
));
753 for (i
= 0; i
< bfd_get_section_size_before_reloc (section
); i
+= onaline
)
757 printf (" %04lx ", (unsigned long int) (i
+ section
->vma
));
758 for (j
= i
; j
< i
+ onaline
; j
++)
760 if (j
< bfd_get_section_size_before_reloc (section
))
761 printf ("%02x", (unsigned) (data
[j
]));
769 for (j
= i
; j
< i
+ onaline
; j
++)
771 if (j
>= bfd_get_section_size_before_reloc (section
))
774 printf ("%c", isprint (data
[j
]) ? data
[j
] : '.');
784 /* Should perhaps share code and display with nm? */
791 asymbol
**current
= syms
;
793 printf ("SYMBOL TABLE:\n");
795 for (count
= 0; count
< symcount
; count
++)
798 if (*current
&& (*current
)->the_bfd
)
800 bfd_print_symbol ((*current
)->the_bfd
,
802 *current
, bfd_print_symbol_all
);
818 unsigned int relcount
;
821 for (a
= abfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
823 if (a
== &bfd_abs_section
)
825 if (a
== &bfd_und_section
)
827 if (a
== &bfd_com_section
)
830 printf ("RELOCATION RECORDS FOR [%s]:", a
->name
);
832 if (bfd_get_reloc_upper_bound (abfd
, a
) == 0)
834 printf (" (none)\n\n");
840 relpp
= (arelent
**) xmalloc (bfd_get_reloc_upper_bound (abfd
, a
));
841 relcount
= bfd_canonicalize_reloc (abfd
, a
, relpp
, syms
);
844 printf (" (none)\n\n");
849 printf ("OFFSET TYPE VALUE \n");
851 for (p
= relpp
; relcount
&& *p
!= (arelent
*) NULL
; p
++,
855 CONST
char *sym_name
;
857 /* CONST char *section_name = q->section == (asection *)NULL ? "*abs" :*/
858 /* q->section->name;*/
859 CONST
char *section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
861 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
863 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
871 printf_vma (q
->address
);
878 printf_vma (q
->address
);
879 printf (" %-8s [%s]",
886 printf_vma (q
->addend
);
899 #define _DUMMY_NAME_ "/dev/null"
901 #define _DUMMY_NAME_ "##dummy"
904 DEFUN (display_info_table
, (first
, last
),
905 int first AND
int last
)
908 extern bfd_target
*target_vector
[];
910 printf ("\n%12s", " ");
911 for (i
= first
; i
++ < last
&& target_vector
[i
];)
912 printf ("%s ", target_vector
[i
]->name
);
915 for (j
= (int) bfd_arch_obscure
+ 1; (int) j
< (int) bfd_arch_last
; j
++)
916 if (strcmp (bfd_printable_arch_mach (j
, 0), "UNKNOWN!") != 0)
918 printf ("%11s ", bfd_printable_arch_mach (j
, 0));
919 for (i
= first
; i
++ < last
&& target_vector
[i
];)
921 bfd_target
*p
= target_vector
[i
];
922 bfd
*abfd
= bfd_openw (_DUMMY_NAME_
, p
->name
);
923 int l
= strlen (p
->name
);
925 bfd_set_format (abfd
, bfd_object
);
926 ok
= bfd_set_arch_mach (abfd
, j
, 0);
929 printf ("%s ", p
->name
);
933 printf ("%c", ok
? '*' : '-');
942 DEFUN_VOID (display_info
)
945 unsigned int i
, j
, columns
;
946 extern bfd_target
*target_vector
[];
947 extern char *getenv ();
949 printf ("BFD header file version %s\n", BFD_VERSION
);
950 for (i
= 0; target_vector
[i
]; i
++)
952 bfd_target
*p
= target_vector
[i
];
953 bfd
*abfd
= bfd_openw (_DUMMY_NAME_
, p
->name
);
954 bfd_set_format (abfd
, bfd_object
);
955 printf ("%s\n (header %s, data %s)\n", p
->name
,
956 p
->header_byteorder_big_p
? "big endian" : "little endian",
957 p
->byteorder_big_p
? "big endian" : "little endian");
958 for (j
= (int) bfd_arch_obscure
+ 1; j
< (int) bfd_arch_last
; j
++)
959 if (bfd_set_arch_mach (abfd
, (enum bfd_architecture
) j
, 0))
961 bfd_printable_arch_mach ((enum bfd_architecture
) j
, 0));
964 if (colum
= getenv ("COLUMNS"))
965 columns
= atoi (colum
);
968 for (i
= 0; target_vector
[i
];)
972 for (j
= 12; target_vector
[i
] && j
< columns
; i
++)
973 j
+= strlen (target_vector
[i
]->name
) + 1;
977 display_info_table (old
, i
);
981 /** main and like trivia */
990 char *target
= default_target
;
991 boolean seenflag
= false;
995 program_name
= *argv
;
997 while ((c
= getopt_long (argc
, argv
, "ib:m:Vdlfahrtxsj:", long_options
, &ind
))
1010 with_line_numbers
= 1;
1016 dump_file_header
= true;
1023 dump_reloc_info
= 1;
1024 dump_file_header
= true;
1026 dump_section_headers
= 1;
1029 break; /* we've been given a long option */
1037 dump_section_contents
= 1;
1040 dump_reloc_info
= 1;
1046 dump_section_headers
= 1;
1057 printf ("%s version %s\n", program_name
, program_version
);
1059 if (seenflag
== false)
1069 display_file ("a.out", target
);
1071 for (; optind
< argc
;)
1072 display_file (argv
[optind
++], target
);