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 */
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 [-ahfdrtxsl] [-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 = ", "; }
104 printf("SECTION %d [%s]\t: size %08x",
107 (unsigned) section
->size
);
108 printf(" vma %08lx align 2**%2u\n ",
110 section
->alignment_power
);
111 PF(SEC_ALLOC
,"ALLOC");
113 PF(SEC_RELOC
,"RELOC");
114 PF(SEC_BALIGN
,"BALIGN");
115 PF(SEC_READONLY
,"READONLY");
129 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
)) {
130 (void) printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd
));
134 storage
= get_symtab_upper_bound (abfd
);
136 sy
= (asymbol
**) malloc (storage
);
138 fprintf (stderr
, "%s: out of memory.\n", program_name
);
142 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
145 /* Sort symbols into value order */
146 static int comp(ap
,bp
)
154 if ( a
->name
== (char *)NULL
|| (a
->flags
&( BSF_DEBUGGING
| BSF_UNDEFINED
) ))
156 if ( b
->name
== (char *)NULL
|| (b
->flags
&( BSF_DEBUGGING
|BSF_UNDEFINED
)))
159 diff
= a
->the_bfd
- b
->the_bfd
;
163 diff
= a
->value
- b
->value
;
167 return a
->section
- b
->section
;
170 /* Print the supplied address symbolically if possible */
172 print_address(vma
, stream
)
176 /* Perform a binary search looking for the closest symbol to
177 the required value */
179 unsigned int min
= 0;
180 unsigned int max
= symcount
;
182 unsigned int thisplace
= 1;
183 unsigned int oldthisplace
;
187 fprintf(stream
,"%08lx", vma
);
190 oldthisplace
= thisplace
;
191 thisplace
= (max
+ min
)/2 ;
192 if (thisplace
== oldthisplace
) break;
195 vardiff
= syms
[thisplace
]->value
- vma
;
206 /* Totally awesome! the exact right symbol */
207 fprintf(stream
,"%08lx (%s)", vma
, syms
[thisplace
]->name
);
211 /* We've run out of places to look, print the symbol before this one */
212 /* see if this or the symbol before describes this location the best */
214 if (thisplace
!= 0) {
215 if (syms
[thisplace
-1]->value
- vma
>
216 syms
[thisplace
]->value
-vma
) {
217 /* Previous symbol is in correct section and is closer */
223 if (syms
[thisplace
]->value
> vma
) {
224 fprintf(stream
,"%08lx (%s-%lx)", vma
, syms
[thisplace
]->name
,
225 syms
[thisplace
]->value
- vma
);
229 fprintf(stream
,"%08lx (%s+%lx)", vma
,
230 syms
[thisplace
]->name
,
231 vma
- syms
[thisplace
]->value
);
237 disassemble_data(abfd
)
240 bfd_byte
*data
= NULL
;
241 unsigned int datasize
= 0;
244 int print_insn_m68k();
245 int print_insn_i960();
246 int print_insn_sparc();
247 enum bfd_architecture a
;
250 /* Replace symbol section relative values with abs values */
253 for (i
= 0; i
< symcount
; i
++) {
254 if (syms
[i
]->section
!= (asection
*)NULL
) {
255 syms
[i
]->value
+= syms
[i
]->section
->vma
;
259 /* We keep a copy of the symbols in the original order */
260 syms2
= slurp_symtab(abfd
);
262 /* Sort the symbols into section and symbol order */
263 (void) qsort(syms
, symcount
, sizeof(asymbol
*), comp
);
265 /* Find the first useless symbol */
267 for (i
=0; i
< symcount
; i
++) {
268 if (syms
[i
]->the_bfd
== 0) {
276 if (machine
!= (char *)NULL
) {
277 if (bfd_scan_arch_mach(machine
, &a
, &m
) == false) {
278 fprintf(stderr
,"%s: Can't use supplied machine %s\n",
285 a
= bfd_get_architecture(abfd
);
290 print
= print_insn_sparc
;
293 print
= print_insn_m68k
;
296 print
= print_insn_i960
;
299 fprintf(stderr
,"%s: Can't disassemble for architecture %s\n",
301 bfd_printable_arch_mach(bfd_get_architecture(abfd
),0));
306 for (section
= abfd
->sections
;
307 section
!= (asection
*)NULL
;
308 section
= section
->next
) {
310 if (only
== (char *)NULL
|| strcmp(only
,section
->name
) == 0){
311 printf("Disassembly of section %s:\n", section
->name
);
313 if (section
->size
== 0) continue;
315 data
= (bfd_byte
*)malloc(section
->size
);
317 if (data
== (bfd_byte
*)NULL
) {
318 fprintf (stderr
, "%s: memory exhausted.\n", program_name
);
321 datasize
= section
->size
;
324 bfd_get_section_contents (abfd
, section
, data
, 0, section
->size
);
327 while ((size_t)i
<section
->size
) {
328 if (with_line_numbers
) {
333 bfd_find_nearest_line(abfd
,
341 if (filename
&& functionname
&& line
&& line
!= prevline
) {
342 printf("%s:%d\n", filename
, line
);
346 print_address(section
->vma
+ i
, stdout
);
349 i
+= print(section
->vma
+ i
,
368 if (!bfd_check_format (abfd
, bfd_object
)) {
369 fprintf (stderr
,"%s: %s not an object file\n", program_name
,
373 printf ("\n%s: file format %s\n", abfd
->filename
, abfd
->xvec
->name
);
374 if (dump_ar_hdrs
) print_arelt_descr (abfd
, true);
376 if (dump_file_header
) {
379 printf("architecture: %s, ",
380 bfd_printable_arch_mach (bfd_get_architecture (abfd
),
381 bfd_get_machine (abfd
)));
382 printf("flags 0x%08x:\n", abfd
->flags
);
384 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
385 PF(HAS_RELOC
, "HAS_RELOC");
386 PF(EXEC_P
, "EXEC_P");
387 PF(HAS_LINENO
, "HAS_LINENO");
388 PF(HAS_DEBUG
, "HAS_DEBUG");
389 PF(HAS_SYMS
, "HAS_SYMS");
390 PF(HAS_LOCALS
, "HAS_LOCALS");
391 PF(DYNAMIC
, "DYNAMIC");
392 PF(WP_TEXT
, "WP_TEXT");
393 PF(D_PAGED
, "D_PAGED");
394 printf("\nstart address 0x%08lx", abfd
->start_address
);
398 if (dump_section_headers
)
400 if (dump_symtab
|| dump_reloc_info
|| disassemble
) {
401 syms
= slurp_symtab(abfd
);
403 if (dump_symtab
) dump_symbols (abfd
);
404 if (dump_reloc_info
) dump_relocs(abfd
);
405 if (dump_section_contents
) dump_data (abfd
);
406 if (disassemble
) disassemble_data(abfd
);
410 display_file (filename
, target
)
414 bfd
*file
, *arfile
= (bfd
*) NULL
;
416 file
= bfd_openr (filename
, target
);
418 bfd_perror (filename
);
422 if (bfd_check_format (file
, bfd_archive
) == true) {
423 printf ("In archive %s:\n", bfd_get_filename (file
));
425 bfd_error
= no_error
;
427 arfile
= bfd_openr_next_archived_file (file
, arfile
);
428 if (arfile
== NULL
) {
429 if (bfd_error
!= no_more_archived_files
)
430 bfd_perror (bfd_get_filename(file
));
434 display_bfd (arfile
);
435 /* Don't close the archive elements; we need them for next_archive */
444 /* Actually display the various requested regions */
461 unsigned int datasize
= 0;
464 for (section
= abfd
->sections
; section
!= NULL
; section
=
468 if (only
== (char *)NULL
||
469 strcmp(only
,section
->name
) == 0){
473 printf("Contents of section %s:\n", section
->name
);
475 if (section
->size
== 0) continue;
476 data
= (bfd_byte
*)malloc(section
->size
);
477 if (data
== (bfd_byte
*)NULL
) {
478 fprintf (stderr
, "%s: memory exhausted.\n", program_name
);
481 datasize
= section
->size
;
484 bfd_get_section_contents (abfd
, section
, data
, 0, section
->size
);
486 for (i
= 0; i
< section
->size
; i
+= onaline
) {
488 printf(" %04lx ", i
+ section
->vma
);
489 for (j
= i
; j
< i
+ onaline
; j
++) {
490 if (j
< section
->size
)
491 printf("%02x", (unsigned)(data
[j
]));
494 if ((j
& 3 ) == 3) printf(" ");
498 for (j
= i
; j
< i
+onaline
; j
++) {
499 if (j
>= section
->size
)
502 printf("%c", isprint(data
[j
]) ?data
[j
] : '.');
514 /* Should perhaps share code and display with nm? */
521 asymbol
**current
= syms
;
522 printf("SYMBOL TABLE:\n");
524 for (count
= 0; count
< symcount
; count
++) {
525 if ((*current
)->the_bfd
) {
526 bfd_print_symbol((*current
)->the_bfd
,
528 *current
, bfd_print_symbol_all_enum
);
544 unsigned int relcount
;
546 for (a
= abfd
->sections
; a
!= (asection
*)NULL
; a
= a
->next
) {
547 printf("RELOCATION RECORDS FOR [%s]:",a
->name
);
549 if (get_reloc_upper_bound(abfd
, a
) == 0) {
550 printf(" (none)\n\n");
555 relpp
= (arelent
**) xmalloc( get_reloc_upper_bound(abfd
,a
) );
556 relcount
= bfd_canonicalize_reloc(abfd
,a
,relpp
, syms
);
558 printf(" (none)\n\n");
562 printf("OFFSET TYPE VALUE \n");
564 for (p
=relpp
; *p
!= (arelent
*)NULL
; p
++) {
566 CONST
char *sym_name
;
567 CONST
char *section_name
= q
->section
== (asection
*)NULL
? "*abs" :
569 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
) {
570 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
576 printf("%08lx %-8s %s",
582 printf("%08lx %-8s [%s]",
588 printf("+0x%lx(%ld)", q
->addend
, (long) q
->addend
);
601 /** main and like trivia */
610 char *target
= default_target
;
611 boolean seenflag
= false;
614 program_name
= *argv
;
616 while ((c
= getopt_long (argc
, argv
, "b:m:dlfahrtxsj:", long_options
, &ind
))
627 with_line_numbers
= 1;
633 dump_file_header
= true;
638 dump_file_header
= true;
640 dump_section_headers
= 1;
642 case 0 : break; /* we've been given a long option */
643 case 't': dump_symtab
= 1; break;
644 case 'd': disassemble
= true ; break;
645 case 's': dump_section_contents
= 1; break;
646 case 'r': dump_reloc_info
= 1; break;
647 case 'a': dump_ar_hdrs
= 1; break;
648 case 'h': dump_section_headers
= 1; break;
654 if (seenflag
== false)
658 display_file ("a.out", target
);
660 for (; optind
< argc
;)
661 display_file (argv
[optind
++], target
);