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
);
110 printf_vma(section
->vma
);
111 printf(" align 2**%2u\n ",
112 section
->alignment_power
);
113 PF(SEC_ALLOC
,"ALLOC");
115 PF(SEC_RELOC
,"RELOC");
116 PF(SEC_BALIGN
,"BALIGN");
117 PF(SEC_READONLY
,"READONLY");
131 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
)) {
132 (void) printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd
));
136 storage
= get_symtab_upper_bound (abfd
);
138 sy
= (asymbol
**) malloc (storage
);
140 fprintf (stderr
, "%s: out of memory.\n", program_name
);
144 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
147 /* Sort symbols into value order */
148 static int comp(ap
,bp
)
156 if ( a
->name
== (char *)NULL
|| (a
->flags
&( BSF_DEBUGGING
| BSF_UNDEFINED
) ))
158 if ( b
->name
== (char *)NULL
|| (b
->flags
&( BSF_DEBUGGING
|BSF_UNDEFINED
)))
161 diff
= a
->the_bfd
- b
->the_bfd
;
165 diff
= a
->value
- b
->value
;
169 return a
->section
- b
->section
;
172 /* Print the supplied address symbolically if possible */
174 print_address(vma
, stream
)
178 /* Perform a binary search looking for the closest symbol to
179 the required value */
181 unsigned int min
= 0;
182 unsigned int max
= symcount
;
184 unsigned int thisplace
= 1;
185 unsigned int oldthisplace
;
189 fprintf_vma(stream
, vma
);
193 oldthisplace
= thisplace
;
194 thisplace
= (max
+ min
)/2 ;
195 if (thisplace
== oldthisplace
) break;
196 vardiff
= syms
[thisplace
]->value
- vma
;
207 /* Totally awesome! the exact right symbol */
208 char *match_name
= syms
[thisplace
]->name
;
209 int sym_len
= strlen(match_name
);
210 /* Avoid "filename.o" as a match */
212 && match_name
[sym_len
- 2] == '.'
213 && match_name
[sym_len
- 1] == 'o'
214 && thisplace
+ 1 < symcount
215 && syms
[thisplace
+1]->value
== vma
)
216 match_name
= syms
[thisplace
+1]->name
;
217 /* Totally awesome! the exact right symbol */
218 fprintf_vma(stream
, vma
);
219 fprintf(stream
," (%s)", syms
[thisplace
]->name
);
223 /* We've run out of places to look, print the symbol before this one */
224 /* see if this or the symbol before describes this location the best */
226 if (thisplace
!= 0) {
227 if (syms
[thisplace
-1]->value
- vma
>
228 syms
[thisplace
]->value
-vma
) {
229 /* Previous symbol is in correct section and is closer */
234 fprintf_vma(stream
, vma
);
235 if (syms
[thisplace
]->value
> vma
) {
236 fprintf(stream
," (%s-)", syms
[thisplace
]->name
);
237 fprintf_vma(stream
, syms
[thisplace
]->value
- vma
);
241 fprintf(stream
," (%s+)", syms
[thisplace
]->name
);
242 fprintf_vma(stream
, vma
- syms
[thisplace
]->value
);
250 disassemble_data(abfd
)
253 bfd_byte
*data
= NULL
;
254 bfd_size_type datasize
= 0;
257 int print_insn_m68k();
258 int print_insn_i960();
259 int print_insn_sparc();
260 enum bfd_architecture a
;
263 /* Replace symbol section relative values with abs values */
266 for (i
= 0; i
< symcount
; i
++) {
267 if (syms
[i
]->section
!= (asection
*)NULL
) {
268 syms
[i
]->value
+= syms
[i
]->section
->vma
;
272 /* We keep a copy of the symbols in the original order */
273 syms2
= slurp_symtab(abfd
);
275 /* Sort the symbols into section and symbol order */
276 (void) qsort(syms
, symcount
, sizeof(asymbol
*), comp
);
278 /* Find the first useless symbol */
280 for (i
=0; i
< symcount
; i
++) {
281 if (syms
[i
]->the_bfd
== 0) {
289 if (machine
!= (char *)NULL
) {
290 if (bfd_scan_arch_mach(machine
, &a
, &m
) == false) {
291 fprintf(stderr
,"%s: Can't use supplied machine %s\n",
298 a
= bfd_get_architecture(abfd
);
303 print
= print_insn_sparc
;
306 print
= print_insn_m68k
;
309 print
= print_insn_i960
;
312 fprintf(stderr
,"%s: Can't disassemble for architecture %s\n",
314 bfd_printable_arch_mach(bfd_get_architecture(abfd
),0));
319 for (section
= abfd
->sections
;
320 section
!= (asection
*)NULL
;
321 section
= section
->next
) {
323 if (only
== (char *)NULL
|| strcmp(only
,section
->name
) == 0){
324 printf("Disassembly of section %s:\n", section
->name
);
326 if (section
->size
== 0) continue;
328 data
= (bfd_byte
*)malloc(section
->size
);
330 if (data
== (bfd_byte
*)NULL
) {
331 fprintf (stderr
, "%s: memory exhausted.\n", program_name
);
334 datasize
= section
->size
;
337 bfd_get_section_contents (abfd
, section
, data
, 0, section
->size
);
340 while (i
<section
->size
) {
341 if (with_line_numbers
) {
343 CONST
char *filename
;
344 CONST
char *functionname
;
346 bfd_find_nearest_line(abfd
,
354 if (filename
&& functionname
&& line
&& line
!= prevline
) {
355 printf("%s:%u\n", filename
, line
);
359 print_address(section
->vma
+ i
, stdout
);
362 i
+= print(section
->vma
+ i
,
381 if (!bfd_check_format (abfd
, bfd_object
)) {
382 fprintf (stderr
,"%s: %s not an object file\n", program_name
,
386 printf ("\n%s: file format %s\n", abfd
->filename
, abfd
->xvec
->name
);
387 if (dump_ar_hdrs
) print_arelt_descr (abfd
, true);
389 if (dump_file_header
) {
392 printf("architecture: %s, ",
393 bfd_printable_arch_mach (bfd_get_architecture (abfd
),
394 bfd_get_machine (abfd
)));
395 printf("flags 0x%08x:\n", abfd
->flags
);
397 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
398 PF(HAS_RELOC
, "HAS_RELOC");
399 PF(EXEC_P
, "EXEC_P");
400 PF(HAS_LINENO
, "HAS_LINENO");
401 PF(HAS_DEBUG
, "HAS_DEBUG");
402 PF(HAS_SYMS
, "HAS_SYMS");
403 PF(HAS_LOCALS
, "HAS_LOCALS");
404 PF(DYNAMIC
, "DYNAMIC");
405 PF(WP_TEXT
, "WP_TEXT");
406 PF(D_PAGED
, "D_PAGED");
407 printf("\nstart address 0x");
408 printf_vma(abfd
->start_address
);
412 if (dump_section_headers
)
414 if (dump_symtab
|| dump_reloc_info
|| disassemble
) {
415 syms
= slurp_symtab(abfd
);
417 if (dump_symtab
) dump_symbols (abfd
);
418 if (dump_reloc_info
) dump_relocs(abfd
);
419 if (dump_section_contents
) dump_data (abfd
);
420 if (disassemble
) disassemble_data(abfd
);
424 display_file (filename
, target
)
428 bfd
*file
, *arfile
= (bfd
*) NULL
;
430 file
= bfd_openr (filename
, target
);
432 bfd_perror (filename
);
436 if (bfd_check_format (file
, bfd_archive
) == true) {
437 printf ("In archive %s:\n", bfd_get_filename (file
));
439 bfd_error
= no_error
;
441 arfile
= bfd_openr_next_archived_file (file
, arfile
);
442 if (arfile
== NULL
) {
443 if (bfd_error
!= no_more_archived_files
)
444 bfd_perror (bfd_get_filename(file
));
448 display_bfd (arfile
);
449 /* Don't close the archive elements; we need them for next_archive */
458 /* Actually display the various requested regions */
475 bfd_size_type datasize
= 0;
478 for (section
= abfd
->sections
; section
!= NULL
; section
=
482 if (only
== (char *)NULL
||
483 strcmp(only
,section
->name
) == 0){
487 printf("Contents of section %s:\n", section
->name
);
489 if (section
->size
== 0) continue;
490 data
= (bfd_byte
*)malloc(section
->size
);
491 if (data
== (bfd_byte
*)NULL
) {
492 fprintf (stderr
, "%s: memory exhausted.\n", program_name
);
495 datasize
= section
->size
;
498 bfd_get_section_contents (abfd
, section
, (PTR
)data
, 0, section
->size
);
500 for (i
= 0; i
< section
->size
; i
+= onaline
) {
502 printf(" %04lx ", (unsigned long int)(i
+ section
->vma
));
503 for (j
= i
; j
< i
+ onaline
; j
++) {
504 if (j
< section
->size
)
505 printf("%02x", (unsigned)(data
[j
]));
508 if ((j
& 3 ) == 3) printf(" ");
512 for (j
= i
; j
< i
+onaline
; j
++) {
513 if (j
>= section
->size
)
516 printf("%c", isprint(data
[j
]) ?data
[j
] : '.');
528 /* Should perhaps share code and display with nm? */
535 asymbol
**current
= syms
;
536 printf("SYMBOL TABLE:\n");
538 for (count
= 0; count
< symcount
; count
++) {
539 if ((*current
)->the_bfd
) {
540 bfd_print_symbol((*current
)->the_bfd
,
542 *current
, bfd_print_symbol_all_enum
);
558 unsigned int relcount
;
560 for (a
= abfd
->sections
; a
!= (asection
*)NULL
; a
= a
->next
) {
561 printf("RELOCATION RECORDS FOR [%s]:",a
->name
);
563 if (get_reloc_upper_bound(abfd
, a
) == 0) {
564 printf(" (none)\n\n");
569 relpp
= (arelent
**) xmalloc( get_reloc_upper_bound(abfd
,a
) );
570 relcount
= bfd_canonicalize_reloc(abfd
,a
,relpp
, syms
);
572 printf(" (none)\n\n");
576 printf("OFFSET TYPE VALUE \n");
578 for (p
=relpp
; relcount
&& *p
!= (arelent
*)NULL
; p
++,
581 CONST
char *sym_name
;
582 CONST
char *section_name
= q
->section
== (asection
*)NULL
? "*abs" :
584 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
) {
585 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
591 printf_vma(q
->address
);
597 printf_vma(q
->address
);
604 printf_vma(q
->addend
);
617 DEFUN_VOID(display_info
)
620 extern bfd_target
*target_vector
[];
622 enum bfd_architecture j
;
624 printf("BFD header file version %s\n", BFD_VERSION
);
625 while (target_vector
[i
] != (bfd_target
*)NULL
)
627 bfd_target
*p
= target_vector
[i
];
628 bfd
*abfd
= bfd_openw("##dummy",p
->name
);
629 printf("%s\n (header %s, data %s)\n", p
->name
,
630 p
->header_byteorder_big_p
? "big endian" : "little endian",
631 p
->byteorder_big_p
? "big endian" : "little endian" );
633 enum bfd_architecture j
;
634 for (j
= (int)bfd_arch_obscure
+1; j
<(int) bfd_arch_last
; j
++)
636 if (bfd_set_arch_mach(abfd
, j
, 0))
638 printf(" %s\n", bfd_printable_arch_mach(j
,0));
645 /* Again as a table */
647 for (i
= 0; target_vector
[i
]; i
++) {
648 printf("%s ",target_vector
[i
]->name
);
651 for (j
= (int)bfd_arch_obscure
+1; (int)j
<(int) bfd_arch_last
; j
++) {
652 printf("%11s ", bfd_printable_arch_mach(j
,0));
653 for (i
= 0; target_vector
[i
]; i
++) {
655 bfd_target
*p
= target_vector
[i
];
656 bfd
*abfd
= bfd_openw("##dummy",p
->name
);
657 int l
= strlen(p
->name
);
658 int ok
= bfd_set_arch_mach(abfd
, j
, 0);
660 printf("%s ", p
->name
);
664 printf("%c",ok
?'*':'-');
675 /** main and like trivia */
684 char *target
= default_target
;
685 boolean seenflag
= false;
688 program_name
= *argv
;
690 while ((c
= getopt_long (argc
, argv
, "ib:m:dlfahrtxsj:", long_options
, &ind
))
701 with_line_numbers
= 1;
707 dump_file_header
= true;
715 dump_file_header
= true;
717 dump_section_headers
= 1;
719 case 0 : break; /* we've been given a long option */
720 case 't': dump_symtab
= 1; break;
721 case 'd': disassemble
= true ; break;
722 case 's': dump_section_contents
= 1; break;
723 case 'r': dump_reloc_info
= 1; break;
724 case 'a': dump_ar_hdrs
= 1; break;
725 case 'h': dump_section_headers
= 1; break;
731 if (seenflag
== false)
739 display_file ("a.out", target
);
741 for (; optind
< argc
;)
742 display_file (argv
[optind
++], target
);