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. */
24 * Until there is other documentation, refer to the manual page dump(1) in
25 * the system 5 program's reference manual
38 char *default_target
= NULL
; /* default at runtime */
40 char *program_name
= NULL
;
42 int dump_section_contents
; /* -s */
43 int dump_section_headers
; /* -h */
44 boolean dump_file_header
; /* -f */
45 int dump_symtab
; /* -t */
46 int dump_reloc_info
; /* -r */
47 int dump_ar_hdrs
; /* -a */
48 int with_line_numbers
; /* -l */
49 boolean disassemble
; /* -d */
50 boolean info
; /* -i */
53 PROTO (void, display_file
, (char *filename
, char *target
));
54 PROTO (void, dump_data
, (bfd
*abfd
));
55 PROTO (void, dump_relocs
, (bfd
*abfd
));
56 PROTO (void, dump_symbols
, (bfd
*abfd
));
57 PROTO (void, print_arelt_descr
, (bfd
*abfd
, boolean verbose
));
65 char *machine
= (char *)NULL
;
72 unsigned int symcount
= 0;
78 "usage: %s [-ahifdrtxsl] [-m machine] [-j section_name] obj ...\n",
83 static struct option long_options
[] =
84 {{"syms", 0, &dump_symtab
, 1},
85 {"reloc", 0, &dump_reloc_info
, 1},
86 {"header", 0, &dump_section_headers
, 1},
96 for (section
= abfd
->sections
;
97 section
!= (asection
*) NULL
;
98 section
= section
->next
)
102 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
105 printf("SECTION %d [%s]\t: size %08x",
108 (unsigned) bfd_get_section_size_before_reloc(section
));
110 printf_vma(section
->vma
);
111 printf(" align 2**%u\n ",
112 section
->alignment_power
);
113 PF(SEC_ALLOC
,"ALLOC");
114 PF(SEC_CONSTRUCTOR
,"CONSTRUCTOR");
115 PF(SEC_CONSTRUCTOR_TEXT
,"CONSTRUCTOR TEXT");
116 PF(SEC_CONSTRUCTOR_DATA
,"CONSTRUCTOR DATA");
117 PF(SEC_CONSTRUCTOR_BSS
,"CONSTRUCTOR BSS");
119 PF(SEC_RELOC
,"RELOC");
120 PF(SEC_BALIGN
,"BALIGN");
121 PF(SEC_READONLY
,"READONLY");
131 DEFUN(slurp_symtab
,(abfd
),
134 asymbol
**sy
= (asymbol
**)NULL
;
136 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
)) {
137 (void) printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd
));
141 storage
= get_symtab_upper_bound (abfd
);
143 sy
= (asymbol
**) malloc (storage
);
145 fprintf (stderr
, "%s: out of memory.\n", program_name
);
149 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
152 /* Sort symbols into value order */
153 static int comp(ap
,bp
)
161 if ( a
->name
== (char *)NULL
|| (a
->flags
&( BSF_DEBUGGING
| BSF_UNDEFINED
) ))
163 if ( b
->name
== (char *)NULL
|| (b
->flags
&( BSF_DEBUGGING
|BSF_UNDEFINED
)))
166 diff
= a
->the_bfd
- b
->the_bfd
;
170 diff
= a
->value
- b
->value
;
174 return a
->section
- b
->section
;
177 /* Print the supplied address symbolically if possible */
179 print_address(vma
, stream
)
183 /* Perform a binary search looking for the closest symbol to
184 the required value */
186 unsigned int min
= 0;
187 unsigned int max
= symcount
;
189 unsigned int thisplace
= 1;
190 unsigned int oldthisplace
;
194 fprintf_vma(stream
, vma
);
198 oldthisplace
= thisplace
;
199 thisplace
= (max
+ min
)/2 ;
200 if (thisplace
== oldthisplace
) break;
201 vardiff
= syms
[thisplace
]->value
- vma
;
212 /* Totally awesome! the exact right symbol */
213 CONST
char *match_name
= syms
[thisplace
]->name
;
214 int sym_len
= strlen(match_name
);
215 /* Avoid "filename.o" as a match */
217 && match_name
[sym_len
- 2] == '.'
218 && match_name
[sym_len
- 1] == 'o'
219 && thisplace
+ 1 < symcount
220 && syms
[thisplace
+1]->value
== vma
)
221 match_name
= syms
[thisplace
+1]->name
;
222 /* Totally awesome! the exact right symbol */
223 fprintf_vma(stream
, vma
);
224 fprintf(stream
," (%s+)0000", syms
[thisplace
]->name
);
228 /* We've run out of places to look, print the symbol before this one
229 see if this or the symbol before describes this location the best */
231 if (thisplace
!= 0) {
232 if (syms
[thisplace
-1]->value
- vma
>
233 syms
[thisplace
]->value
-vma
) {
234 /* Previous symbol is in correct section and is closer */
239 fprintf_vma(stream
, vma
);
240 if (syms
[thisplace
]->value
> vma
) {
241 fprintf(stream
," (%s-)", syms
[thisplace
]->name
);
242 fprintf(stream
,"%04x", syms
[thisplace
]->value
- vma
);
246 fprintf(stream
," (%s+)", syms
[thisplace
]->name
);
247 fprintf(stream
, "%04x", vma
- syms
[thisplace
]->value
);
255 disassemble_data(abfd
)
258 bfd_byte
*data
= NULL
;
259 bfd_arch_info_type
*info
;
260 bfd_size_type datasize
= 0;
262 unsigned int (*print
)() =0;
263 unsigned int print_insn_m68k();
264 unsigned int print_insn_a29k();
265 unsigned int print_insn_i960();
266 unsigned int print_insn_sparc();
267 unsigned int print_insn_h8300();
268 enum bfd_architecture a
;
271 /* Replace symbol section relative values with abs values */
272 boolean done_dot
= false;
274 for (i
= 0; i
< symcount
; i
++) {
275 syms
[i
]->value
+= syms
[i
]->section
->vma
;
278 /* We keep a copy of the symbols in the original order */
279 syms2
= slurp_symtab(abfd
);
281 /* Sort the symbols into section and symbol order */
282 (void) qsort(syms
, symcount
, sizeof(asymbol
*), comp
);
284 /* Find the first useless symbol */
286 for (i
=0; i
< symcount
; i
++) {
287 if (syms
[i
]->the_bfd
== 0) {
297 if (machine
!= (char *)NULL
) {
298 info
= bfd_scan_arch(machine
);
300 fprintf(stderr
,"%s: Can't use supplied machine %s\n",
305 abfd
->arch_info
= info
;
308 /* See if we can disassemble using bfd */
310 if(abfd
->arch_info
->disassemble
) {
311 print
= abfd
->arch_info
->disassemble
;
314 a
= bfd_get_arch(abfd
);
317 print
= print_insn_sparc
;
320 print
= print_insn_m68k
;
323 print
= print_insn_a29k
;
326 print
= print_insn_i960
;
329 fprintf(stderr
,"%s: Can't disassemble for architecture %s\n",
331 bfd_printable_arch_mach(bfd_get_arch(abfd
),0));
337 for (section
= abfd
->sections
;
338 section
!= (asection
*)NULL
;
339 section
= section
->next
) {
341 if (only
== (char *)NULL
|| strcmp(only
,section
->name
) == 0){
342 printf("Disassembly of section %s:\n", section
->name
);
344 if (bfd_get_section_size_before_reloc(section
) == 0) continue;
346 data
= (bfd_byte
*)malloc(bfd_get_section_size_before_reloc(section
));
348 if (data
== (bfd_byte
*)NULL
) {
349 fprintf (stderr
, "%s: memory exhausted.\n", program_name
);
352 datasize
= bfd_get_section_size_before_reloc(section
);
355 bfd_get_section_contents (abfd
, section
, data
, 0, bfd_get_section_size_before_reloc(section
));
358 while (i
<bfd_get_section_size_before_reloc(section
)) {
359 if (data
[i
] ==0 && data
[i
+1] == 0 && data
[i
+2] == 0 &&
361 if (done_dot
== false) {
369 if (with_line_numbers
) {
371 CONST
char *filename
;
372 CONST
char *functionname
;
374 bfd_find_nearest_line(abfd
,
382 if (filename
&& functionname
&& line
&& line
!= prevline
) {
383 printf("%s:%u\n", filename
, line
);
387 print_address(section
->vma
+ i
, stdout
);
390 i
+= print(section
->vma
+ i
,
409 if (!bfd_check_format (abfd
, bfd_object
)) {
410 fprintf (stderr
,"%s: %s not an object file\n", program_name
,
414 printf ("\n%s: file format %s\n", abfd
->filename
, abfd
->xvec
->name
);
415 if (dump_ar_hdrs
) print_arelt_descr (abfd
, true);
417 if (dump_file_header
) {
420 printf("architecture: %s, ",
421 bfd_printable_arch_mach (bfd_get_arch (abfd
),
422 bfd_get_mach (abfd
)));
423 printf("flags 0x%08x:\n", abfd
->flags
);
425 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
426 PF(HAS_RELOC
, "HAS_RELOC");
427 PF(EXEC_P
, "EXEC_P");
428 PF(HAS_LINENO
, "HAS_LINENO");
429 PF(HAS_DEBUG
, "HAS_DEBUG");
430 PF(HAS_SYMS
, "HAS_SYMS");
431 PF(HAS_LOCALS
, "HAS_LOCALS");
432 PF(DYNAMIC
, "DYNAMIC");
433 PF(WP_TEXT
, "WP_TEXT");
434 PF(D_PAGED
, "D_PAGED");
435 printf("\nstart address 0x");
436 printf_vma(abfd
->start_address
);
440 if (dump_section_headers
)
442 if (dump_symtab
|| dump_reloc_info
|| disassemble
) {
443 syms
= slurp_symtab(abfd
);
445 if (dump_symtab
) dump_symbols (abfd
);
446 if (dump_reloc_info
) dump_relocs(abfd
);
447 if (dump_section_contents
) dump_data (abfd
);
448 if (disassemble
) disassemble_data(abfd
);
452 display_file (filename
, target
)
456 bfd
*file
, *arfile
= (bfd
*) NULL
;
458 file
= bfd_openr (filename
, target
);
460 bfd_perror (filename
);
464 if (bfd_check_format (file
, bfd_archive
) == true) {
465 printf ("In archive %s:\n", bfd_get_filename (file
));
467 bfd_error
= no_error
;
469 arfile
= bfd_openr_next_archived_file (file
, arfile
);
470 if (arfile
== NULL
) {
471 if (bfd_error
!= no_more_archived_files
)
472 bfd_perror (bfd_get_filename(file
));
476 display_bfd (arfile
);
477 /* Don't close the archive elements; we need them for next_archive */
486 /* Actually display the various requested regions */
503 bfd_size_type datasize
= 0;
506 for (section
= abfd
->sections
; section
!= NULL
; section
=
510 if (only
== (char *)NULL
||
511 strcmp(only
,section
->name
) == 0){
515 printf("Contents of section %s:\n", section
->name
);
517 if (bfd_get_section_size_before_reloc(section
) == 0) continue;
518 data
= (bfd_byte
*)malloc(bfd_get_section_size_before_reloc(section
));
519 if (data
== (bfd_byte
*)NULL
) {
520 fprintf (stderr
, "%s: memory exhausted.\n", program_name
);
523 datasize
= bfd_get_section_size_before_reloc(section
);
526 bfd_get_section_contents (abfd
, section
, (PTR
)data
, 0, bfd_get_section_size_before_reloc(section
));
528 for (i
= 0; i
< bfd_get_section_size_before_reloc(section
); i
+= onaline
) {
530 printf(" %04lx ", (unsigned long int)(i
+ section
->vma
));
531 for (j
= i
; j
< i
+ onaline
; j
++) {
532 if (j
< bfd_get_section_size_before_reloc(section
))
533 printf("%02x", (unsigned)(data
[j
]));
536 if ((j
& 3 ) == 3) printf(" ");
540 for (j
= i
; j
< i
+onaline
; j
++) {
541 if (j
>= bfd_get_section_size_before_reloc(section
))
544 printf("%c", isprint(data
[j
]) ?data
[j
] : '.');
556 /* Should perhaps share code and display with nm? */
563 asymbol
**current
= syms
;
564 printf("SYMBOL TABLE:\n");
566 for (count
= 0; count
< symcount
; count
++) {
568 if (*current
&& (*current
)->the_bfd
) {
569 bfd_print_symbol((*current
)->the_bfd
,
571 *current
, bfd_print_symbol_all
);
588 unsigned int relcount
;
590 for (a
= abfd
->sections
; a
!= (asection
*)NULL
; a
= a
->next
) {
591 if (a
== &bfd_abs_section
) continue;
592 if (a
== &bfd_und_section
) continue;
593 if (a
== &bfd_com_section
) continue;
595 printf("RELOCATION RECORDS FOR [%s]:",a
->name
);
597 if (bfd_get_reloc_upper_bound(abfd
, a
) == 0) {
598 printf(" (none)\n\n");
603 relpp
= (arelent
**) xmalloc( bfd_get_reloc_upper_bound(abfd
,a
) );
604 relcount
= bfd_canonicalize_reloc(abfd
,a
,relpp
, syms
);
606 printf(" (none)\n\n");
610 printf("OFFSET TYPE VALUE \n");
612 for (p
=relpp
; relcount
&& *p
!= (arelent
*)NULL
; p
++,
615 CONST
char *sym_name
;
616 /* CONST char *section_name = q->section == (asection *)NULL ? "*abs" :*/
617 /* q->section->name;*/
618 CONST
char *section_name
= (*( q
->sym_ptr_ptr
))->section
->name
;
620 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
) {
621 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
627 printf_vma(q
->address
);
633 printf_vma(q
->address
);
640 printf_vma(q
->addend
);
653 DEFUN_VOID(display_info
)
656 extern bfd_target
*target_vector
[];
658 printf("BFD header file version %s\n", BFD_VERSION
);
659 for (i
= 0; target_vector
[i
] != (bfd_target
*)NULL
; i
++)
661 bfd_target
*p
= target_vector
[i
];
662 bfd
*abfd
= bfd_openw("##dummy",p
->name
);
663 printf("%s\n (header %s, data %s)\n", p
->name
,
664 p
->header_byteorder_big_p
? "big endian" : "little endian",
665 p
->byteorder_big_p
? "big endian" : "little endian" );
667 for (j
= (int)bfd_arch_obscure
+1; j
< (int)bfd_arch_last
; j
++)
670 if (bfd_set_arch_mach(abfd
, (enum bfd_architecture
)j
, 0))
672 bfd_printable_arch_mach((enum bfd_architecture
)j
,0));
678 /* Again as a table */
680 for (i
= 0; target_vector
[i
]; i
++) {
681 printf("%s ",target_vector
[i
]->name
);
686 for (j
= (int)bfd_arch_obscure
+1; (int)j
<(int) bfd_arch_last
; j
++)
688 if (strcmp(bfd_printable_arch_mach(j
,0),"UNKNOWN!") != 0) {
689 printf("%11s ", bfd_printable_arch_mach(j
,0));
690 for (i
= 0; target_vector
[i
]; i
++) {
692 bfd_target
*p
= target_vector
[i
];
693 bfd
*abfd
= bfd_openw("##dummy",p
->name
);
694 int l
= strlen(p
->name
);
695 int ok
= bfd_set_arch_mach(abfd
, j
, 0);
697 printf("%s ", p
->name
);
701 printf("%c",ok
?'*':'-');
713 /** main and like trivia */
722 char *target
= default_target
;
723 boolean seenflag
= false;
727 program_name
= *argv
;
729 while ((c
= getopt_long (argc
, argv
, "ib:m:dlfahrtxsj:", long_options
, &ind
))
740 with_line_numbers
= 1;
746 dump_file_header
= true;
754 dump_file_header
= true;
756 dump_section_headers
= 1;
758 case 0 : break; /* we've been given a long option */
759 case 't': dump_symtab
= 1; break;
760 case 'd': disassemble
= true ; break;
761 case 's': dump_section_contents
= 1; break;
762 case 'r': dump_reloc_info
= 1; break;
763 case 'a': dump_ar_hdrs
= 1; break;
764 case 'h': dump_section_headers
= 1; break;
770 if (seenflag
== false)
778 display_file ("a.out", target
);
780 for (; optind
< argc
;)
781 display_file (argv
[optind
++], target
);
This page took 0.049491 seconds and 4 git commands to generate.