1 /*** objdump.c -- dump information about an object file. */
3 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Diddler.
7 BFD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 1, or (at your option)
12 BFD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with BFD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
25 * Until there is other documentation, refer to the manual page dump(1) in
26 * the system 5 program's reference manual
39 char *default_target
= NULL
; /* default at runtime */
41 char *program_name
= NULL
;
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_reloc_info
; /* -r */
48 int dump_ar_hdrs
; /* -a */
49 int with_line_numbers
; /* -l */
50 boolean disassemble
; /* -d */
51 boolean info
; /* -i */
54 PROTO (void, display_file
, (char *filename
, char *target
));
55 PROTO (void, dump_data
, (bfd
*abfd
));
56 PROTO (void, dump_relocs
, (bfd
*abfd
));
57 PROTO (void, dump_symbols
, (bfd
*abfd
));
58 PROTO (void, print_arelt_descr
, (bfd
*abfd
, boolean verbose
));
66 char *machine
= (char *)NULL
;
73 unsigned int symcount
= 0;
79 "usage: %s [-ahifdrtxsl] [-m machine] [-j section_name] obj ...\n",
84 static struct option long_options
[] =
85 {{"syms", 0, &dump_symtab
, 1},
86 {"reloc", 0, &dump_reloc_info
, 1},
87 {"header", 0, &dump_section_headers
, 1},
97 for (section
= abfd
->sections
;
98 section
!= (asection
*) NULL
;
99 section
= section
->next
)
103 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
105 printf("SECTION %d [%s]\t: size %08x",
108 (unsigned) section
->size
);
109 printf(" vma %08lx align 2**%2u\n ",
111 section
->alignment_power
);
112 PF(SEC_ALLOC
,"ALLOC");
114 PF(SEC_RELOC
,"RELOC");
115 PF(SEC_BALIGN
,"BALIGN");
116 PF(SEC_READONLY
,"READONLY");
130 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
)) {
131 (void) printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd
));
135 storage
= get_symtab_upper_bound (abfd
);
137 sy
= (asymbol
**) malloc (storage
);
139 fprintf (stderr
, "%s: out of memory.\n", program_name
);
143 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
146 /* Sort symbols into value order */
147 static int comp(ap
,bp
)
155 if ( a
->name
== (char *)NULL
|| (a
->flags
&( BSF_DEBUGGING
| BSF_UNDEFINED
) ))
157 if ( b
->name
== (char *)NULL
|| (b
->flags
&( BSF_DEBUGGING
|BSF_UNDEFINED
)))
160 diff
= a
->the_bfd
- b
->the_bfd
;
164 diff
= a
->value
- b
->value
;
168 return a
->section
- b
->section
;
171 /* Print the supplied address symbolically if possible */
173 print_address(vma
, stream
)
177 /* Perform a binary search looking for the closest symbol to
178 the required value */
180 unsigned int min
= 0;
181 unsigned int max
= symcount
;
183 unsigned int thisplace
= 1;
184 unsigned int oldthisplace
;
188 fprintf(stream
,"%08lx", vma
);
191 oldthisplace
= thisplace
;
192 thisplace
= (max
+ min
)/2 ;
193 if (thisplace
== oldthisplace
) break;
196 vardiff
= syms
[thisplace
]->value
- vma
;
207 /* Totally awesome! the exact right symbol */
208 fprintf(stream
,"%08lx (%s)", vma
, syms
[thisplace
]->name
);
212 /* We've run out of places to look, print the symbol before this one */
213 /* see if this or the symbol before describes this location the best */
215 if (thisplace
!= 0) {
216 if (syms
[thisplace
-1]->value
- vma
>
217 syms
[thisplace
]->value
-vma
) {
218 /* Previous symbol is in correct section and is closer */
224 if (syms
[thisplace
]->value
> vma
) {
225 fprintf(stream
,"%08lx (%s-%lx)", vma
, syms
[thisplace
]->name
,
226 syms
[thisplace
]->value
- vma
);
230 fprintf(stream
,"%08lx (%s+%lx)", vma
,
231 syms
[thisplace
]->name
,
232 vma
- syms
[thisplace
]->value
);
238 disassemble_data(abfd
)
241 bfd_byte
*data
= NULL
;
242 unsigned int datasize
= 0;
245 int print_insn_m68k();
246 int print_insn_i960();
247 int print_insn_sparc();
248 enum bfd_architecture a
;
251 /* Replace symbol section relative values with abs values */
254 for (i
= 0; i
< symcount
; i
++) {
255 if (syms
[i
]->section
!= (asection
*)NULL
) {
256 syms
[i
]->value
+= syms
[i
]->section
->vma
;
260 /* We keep a copy of the symbols in the original order */
261 syms2
= slurp_symtab(abfd
);
263 /* Sort the symbols into section and symbol order */
264 (void) qsort(syms
, symcount
, sizeof(asymbol
*), comp
);
266 /* Find the first useless symbol */
268 for (i
=0; i
< symcount
; i
++) {
269 if (syms
[i
]->the_bfd
== 0) {
277 if (machine
!= (char *)NULL
) {
278 if (bfd_scan_arch_mach(machine
, &a
, &m
) == false) {
279 fprintf(stderr
,"%s: Can't use supplied machine %s\n",
286 a
= bfd_get_architecture(abfd
);
291 print
= print_insn_sparc
;
294 print
= print_insn_m68k
;
297 print
= print_insn_i960
;
300 fprintf(stderr
,"%s: Can't disassemble for architecture %s\n",
302 bfd_printable_arch_mach(bfd_get_architecture(abfd
),0));
307 for (section
= abfd
->sections
;
308 section
!= (asection
*)NULL
;
309 section
= section
->next
) {
311 if (only
== (char *)NULL
|| strcmp(only
,section
->name
) == 0){
312 printf("Disassembly of section %s:\n", section
->name
);
314 if (section
->size
== 0) continue;
316 data
= (bfd_byte
*)malloc(section
->size
);
318 if (data
== (bfd_byte
*)NULL
) {
319 fprintf (stderr
, "%s: memory exhausted.\n", program_name
);
322 datasize
= section
->size
;
325 bfd_get_section_contents (abfd
, section
, data
, 0, section
->size
);
328 while ((size_t)i
<section
->size
) {
329 if (with_line_numbers
) {
334 bfd_find_nearest_line(abfd
,
342 if (filename
&& functionname
&& line
&& line
!= prevline
) {
343 printf("%s:%d\n", filename
, line
);
347 print_address(section
->vma
+ i
, stdout
);
350 i
+= print(section
->vma
+ i
,
369 if (!bfd_check_format (abfd
, bfd_object
)) {
370 fprintf (stderr
,"%s: %s not an object file\n", program_name
,
374 printf ("\n%s: file format %s\n", abfd
->filename
, abfd
->xvec
->name
);
375 if (dump_ar_hdrs
) print_arelt_descr (abfd
, true);
377 if (dump_file_header
) {
380 printf("architecture: %s, ",
381 bfd_printable_arch_mach (bfd_get_architecture (abfd
),
382 bfd_get_machine (abfd
)));
383 printf("flags 0x%08x:\n", abfd
->flags
);
385 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
386 PF(HAS_RELOC
, "HAS_RELOC");
387 PF(EXEC_P
, "EXEC_P");
388 PF(HAS_LINENO
, "HAS_LINENO");
389 PF(HAS_DEBUG
, "HAS_DEBUG");
390 PF(HAS_SYMS
, "HAS_SYMS");
391 PF(HAS_LOCALS
, "HAS_LOCALS");
392 PF(DYNAMIC
, "DYNAMIC");
393 PF(WP_TEXT
, "WP_TEXT");
394 PF(D_PAGED
, "D_PAGED");
395 printf("\nstart address 0x%08lx", abfd
->start_address
);
399 if (dump_section_headers
)
401 if (dump_symtab
|| dump_reloc_info
|| disassemble
) {
402 syms
= slurp_symtab(abfd
);
404 if (dump_symtab
) dump_symbols (abfd
);
405 if (dump_reloc_info
) dump_relocs(abfd
);
406 if (dump_section_contents
) dump_data (abfd
);
407 if (disassemble
) disassemble_data(abfd
);
411 display_file (filename
, target
)
415 bfd
*file
, *arfile
= (bfd
*) NULL
;
417 file
= bfd_openr (filename
, target
);
419 bfd_perror (filename
);
423 if (bfd_check_format (file
, bfd_archive
) == true) {
424 printf ("In archive %s:\n", bfd_get_filename (file
));
426 bfd_error
= no_error
;
428 arfile
= bfd_openr_next_archived_file (file
, arfile
);
429 if (arfile
== NULL
) {
430 if (bfd_error
!= no_more_archived_files
)
431 bfd_perror (bfd_get_filename(file
));
435 display_bfd (arfile
);
436 /* Don't close the archive elements; we need them for next_archive */
445 /* Actually display the various requested regions */
462 unsigned int datasize
= 0;
465 for (section
= abfd
->sections
; section
!= NULL
; section
=
469 if (only
== (char *)NULL
||
470 strcmp(only
,section
->name
) == 0){
474 printf("Contents of section %s:\n", section
->name
);
476 if (section
->size
== 0) continue;
477 data
= (bfd_byte
*)malloc(section
->size
);
478 if (data
== (bfd_byte
*)NULL
) {
479 fprintf (stderr
, "%s: memory exhausted.\n", program_name
);
482 datasize
= section
->size
;
485 bfd_get_section_contents (abfd
, section
, data
, 0, section
->size
);
487 for (i
= 0; i
< section
->size
; i
+= onaline
) {
489 printf(" %04lx ", i
+ section
->vma
);
490 for (j
= i
; j
< i
+ onaline
; j
++) {
491 if (j
< section
->size
)
492 printf("%02x", (unsigned)(data
[j
]));
495 if ((j
& 3 ) == 3) printf(" ");
499 for (j
= i
; j
< i
+onaline
; j
++) {
500 if (j
>= section
->size
)
503 printf("%c", isprint(data
[j
]) ?data
[j
] : '.');
515 /* Should perhaps share code and display with nm? */
522 asymbol
**current
= syms
;
523 printf("SYMBOL TABLE:\n");
525 for (count
= 0; count
< symcount
; count
++) {
526 if ((*current
)->the_bfd
) {
527 bfd_print_symbol((*current
)->the_bfd
,
529 *current
, bfd_print_symbol_all_enum
);
545 unsigned int relcount
;
547 for (a
= abfd
->sections
; a
!= (asection
*)NULL
; a
= a
->next
) {
548 printf("RELOCATION RECORDS FOR [%s]:",a
->name
);
550 if (get_reloc_upper_bound(abfd
, a
) == 0) {
551 printf(" (none)\n\n");
556 relpp
= (arelent
**) xmalloc( get_reloc_upper_bound(abfd
,a
) );
557 relcount
= bfd_canonicalize_reloc(abfd
,a
,relpp
, syms
);
559 printf(" (none)\n\n");
563 printf("OFFSET TYPE VALUE \n");
565 for (p
=relpp
; *p
!= (arelent
*)NULL
; p
++) {
567 CONST
char *sym_name
;
568 CONST
char *section_name
= q
->section
== (asection
*)NULL
? "*abs" :
570 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
) {
571 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
577 printf("%08lx %-8s %s",
583 printf("%08lx %-8s [%s]",
589 printf("+0x%lx(%ld)", q
->addend
, (long) q
->addend
);
602 DEFUN_VOID(display_info
)
605 extern bfd_target
*target_vector
[];
607 enum bfd_architecture j
;
609 printf("BFD header file version %s\n", BFD_VERSION
);
610 while (target_vector
[i
] != (bfd_target
*)NULL
)
612 bfd_target
*p
= target_vector
[i
];
613 bfd
*abfd
= bfd_openw("##dummy",p
->name
);
614 printf("%s\n (header %s, data %s)\n", p
->name
,
615 p
->header_byteorder_big_p
? "big endian" : "little endian",
616 p
->byteorder_big_p
? "big endian" : "little endian" );
618 enum bfd_architecture j
;
619 for (j
= bfd_arch_obscure
+1; j
< bfd_arch_last
; j
++)
621 if (bfd_set_arch_mach(abfd
, j
, 0))
623 printf(" %s\n", bfd_printable_arch_mach(j
,0));
630 /* Again as a table */
632 for (i
= 0; target_vector
[i
]; i
++) {
633 printf("%s ",target_vector
[i
]->name
);
636 for (j
= bfd_arch_obscure
+1; j
< bfd_arch_last
; j
++) {
637 printf("%11s ", bfd_printable_arch_mach(j
,0));
638 for (i
= 0; target_vector
[i
]; i
++) {
640 bfd_target
*p
= target_vector
[i
];
641 bfd
*abfd
= bfd_openw("##dummy",p
->name
);
642 int l
= strlen(p
->name
);
643 int ok
= bfd_set_arch_mach(abfd
, j
, 0);
645 printf("%s ", p
->name
);
649 printf("%c",ok
?'*':'-');
660 /** main and like trivia */
669 char *target
= default_target
;
670 boolean seenflag
= false;
673 program_name
= *argv
;
675 while ((c
= getopt_long (argc
, argv
, "ib:m:dlfahrtxsj:", long_options
, &ind
))
686 with_line_numbers
= 1;
692 dump_file_header
= true;
700 dump_file_header
= true;
702 dump_section_headers
= 1;
704 case 0 : break; /* we've been given a long option */
705 case 't': dump_symtab
= 1; break;
706 case 'd': disassemble
= true ; break;
707 case 's': dump_section_contents
= 1; break;
708 case 'r': dump_reloc_info
= 1; break;
709 case 'a': dump_ar_hdrs
= 1; break;
710 case 'h': dump_section_headers
= 1; break;
716 if (seenflag
== false)
724 display_file ("a.out", target
);
726 for (; optind
< argc
;)
727 display_file (argv
[optind
++], target
);