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 */
264 boolean done_dot
= false;
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 (data
[i
] ==0 && data
[i
+1] == 0 && data
[i
+2] == 0 &&
343 if (done_dot
== false) {
351 if (with_line_numbers
) {
353 CONST
char *filename
;
354 CONST
char *functionname
;
356 bfd_find_nearest_line(abfd
,
364 if (filename
&& functionname
&& line
&& line
!= prevline
) {
365 printf("%s:%u\n", filename
, line
);
369 print_address(section
->vma
+ i
, stdout
);
372 i
+= print(section
->vma
+ i
,
391 if (!bfd_check_format (abfd
, bfd_object
)) {
392 fprintf (stderr
,"%s: %s not an object file\n", program_name
,
396 printf ("\n%s: file format %s\n", abfd
->filename
, abfd
->xvec
->name
);
397 if (dump_ar_hdrs
) print_arelt_descr (abfd
, true);
399 if (dump_file_header
) {
402 printf("architecture: %s, ",
403 bfd_printable_arch_mach (bfd_get_architecture (abfd
),
404 bfd_get_machine (abfd
)));
405 printf("flags 0x%08x:\n", abfd
->flags
);
407 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
408 PF(HAS_RELOC
, "HAS_RELOC");
409 PF(EXEC_P
, "EXEC_P");
410 PF(HAS_LINENO
, "HAS_LINENO");
411 PF(HAS_DEBUG
, "HAS_DEBUG");
412 PF(HAS_SYMS
, "HAS_SYMS");
413 PF(HAS_LOCALS
, "HAS_LOCALS");
414 PF(DYNAMIC
, "DYNAMIC");
415 PF(WP_TEXT
, "WP_TEXT");
416 PF(D_PAGED
, "D_PAGED");
417 printf("\nstart address 0x");
418 printf_vma(abfd
->start_address
);
422 if (dump_section_headers
)
424 if (dump_symtab
|| dump_reloc_info
|| disassemble
) {
425 syms
= slurp_symtab(abfd
);
427 if (dump_symtab
) dump_symbols (abfd
);
428 if (dump_reloc_info
) dump_relocs(abfd
);
429 if (dump_section_contents
) dump_data (abfd
);
430 if (disassemble
) disassemble_data(abfd
);
434 display_file (filename
, target
)
438 bfd
*file
, *arfile
= (bfd
*) NULL
;
440 file
= bfd_openr (filename
, target
);
442 bfd_perror (filename
);
446 if (bfd_check_format (file
, bfd_archive
) == true) {
447 printf ("In archive %s:\n", bfd_get_filename (file
));
449 bfd_error
= no_error
;
451 arfile
= bfd_openr_next_archived_file (file
, arfile
);
452 if (arfile
== NULL
) {
453 if (bfd_error
!= no_more_archived_files
)
454 bfd_perror (bfd_get_filename(file
));
458 display_bfd (arfile
);
459 /* Don't close the archive elements; we need them for next_archive */
468 /* Actually display the various requested regions */
485 bfd_size_type datasize
= 0;
488 for (section
= abfd
->sections
; section
!= NULL
; section
=
492 if (only
== (char *)NULL
||
493 strcmp(only
,section
->name
) == 0){
497 printf("Contents of section %s:\n", section
->name
);
499 if (section
->size
== 0) continue;
500 data
= (bfd_byte
*)malloc(section
->size
);
501 if (data
== (bfd_byte
*)NULL
) {
502 fprintf (stderr
, "%s: memory exhausted.\n", program_name
);
505 datasize
= section
->size
;
508 bfd_get_section_contents (abfd
, section
, (PTR
)data
, 0, section
->size
);
510 for (i
= 0; i
< section
->size
; i
+= onaline
) {
512 printf(" %04lx ", (unsigned long int)(i
+ section
->vma
));
513 for (j
= i
; j
< i
+ onaline
; j
++) {
514 if (j
< section
->size
)
515 printf("%02x", (unsigned)(data
[j
]));
518 if ((j
& 3 ) == 3) printf(" ");
522 for (j
= i
; j
< i
+onaline
; j
++) {
523 if (j
>= section
->size
)
526 printf("%c", isprint(data
[j
]) ?data
[j
] : '.');
538 /* Should perhaps share code and display with nm? */
545 asymbol
**current
= syms
;
546 printf("SYMBOL TABLE:\n");
548 for (count
= 0; count
< symcount
; count
++) {
549 if ((*current
)->the_bfd
) {
550 bfd_print_symbol((*current
)->the_bfd
,
552 *current
, bfd_print_symbol_all_enum
);
568 unsigned int relcount
;
570 for (a
= abfd
->sections
; a
!= (asection
*)NULL
; a
= a
->next
) {
571 printf("RELOCATION RECORDS FOR [%s]:",a
->name
);
573 if (get_reloc_upper_bound(abfd
, a
) == 0) {
574 printf(" (none)\n\n");
579 relpp
= (arelent
**) xmalloc( get_reloc_upper_bound(abfd
,a
) );
580 relcount
= bfd_canonicalize_reloc(abfd
,a
,relpp
, syms
);
582 printf(" (none)\n\n");
586 printf("OFFSET TYPE VALUE \n");
588 for (p
=relpp
; relcount
&& *p
!= (arelent
*)NULL
; p
++,
591 CONST
char *sym_name
;
592 CONST
char *section_name
= q
->section
== (asection
*)NULL
? "*abs" :
594 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
) {
595 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
601 printf_vma(q
->address
);
607 printf_vma(q
->address
);
614 printf_vma(q
->addend
);
627 DEFUN_VOID(display_info
)
630 extern bfd_target
*target_vector
[];
632 enum bfd_architecture j
;
634 printf("BFD header file version %s\n", BFD_VERSION
);
635 while (target_vector
[i
] != (bfd_target
*)NULL
)
637 bfd_target
*p
= target_vector
[i
];
638 bfd
*abfd
= bfd_openw("##dummy",p
->name
);
639 printf("%s\n (header %s, data %s)\n", p
->name
,
640 p
->header_byteorder_big_p
? "big endian" : "little endian",
641 p
->byteorder_big_p
? "big endian" : "little endian" );
643 enum bfd_architecture j
;
644 for (j
= (int)bfd_arch_obscure
+1; j
<(int) bfd_arch_last
; j
++)
646 if (bfd_set_arch_mach(abfd
, j
, 0))
648 printf(" %s\n", bfd_printable_arch_mach(j
,0));
655 /* Again as a table */
657 for (i
= 0; target_vector
[i
]; i
++) {
658 printf("%s ",target_vector
[i
]->name
);
661 for (j
= (int)bfd_arch_obscure
+1; (int)j
<(int) bfd_arch_last
; j
++) {
662 printf("%11s ", bfd_printable_arch_mach(j
,0));
663 for (i
= 0; target_vector
[i
]; i
++) {
665 bfd_target
*p
= target_vector
[i
];
666 bfd
*abfd
= bfd_openw("##dummy",p
->name
);
667 int l
= strlen(p
->name
);
668 int ok
= bfd_set_arch_mach(abfd
, j
, 0);
670 printf("%s ", p
->name
);
674 printf("%c",ok
?'*':'-');
685 /** main and like trivia */
694 char *target
= default_target
;
695 boolean seenflag
= false;
698 program_name
= *argv
;
700 while ((c
= getopt_long (argc
, argv
, "Aib:m:dlfahrtxsj:", long_options
, &ind
))
711 with_line_numbers
= 1;
717 dump_file_header
= true;
725 dump_file_header
= true;
727 dump_section_headers
= 1;
732 dump_file_header
= true;
734 dump_section_headers
= 1;
738 case 0 : break; /* we've been given a long option */
739 case 't': dump_symtab
= 1; break;
740 case 'd': disassemble
= true ; break;
741 case 's': dump_section_contents
= 1; break;
742 case 'r': dump_reloc_info
= 1; break;
743 case 'a': dump_ar_hdrs
= 1; break;
744 case 'h': dump_section_headers
= 1; break;
750 if (seenflag
== false)
758 display_file ("a.out", target
);
760 for (; optind
< argc
;)
761 display_file (argv
[optind
++], target
);