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. */
24 Revision 1.1 1991/03/21 21:26:48 gumby
27 * Revision 1.2 1991/03/15 18:34:14 rich
30 * Revision 1.1 1991/03/13 00:34:19 chrisb
33 * Revision 1.9 1991/03/09 04:36:33 rich
35 * sparc-pinsn.c ostrip.c objdump.c m68k-pinsn.c i960-pinsn.c
38 * Pulled sysdep.h out of bfd.h.
40 * Revision 1.8 1991/03/09 03:42:01 rich
42 * Makefile alloca.c ar.c i960-pinsn.c nm.c objdump.c ostrip.c
45 * Ports for intel960 group Portland.
47 * Revision 1.7 1991/03/08 21:54:47 rich
49 * Makefile ar.c binutils.h bucomm.c copy.c cplus-dem.c getopt.c
50 * i960-pinsn.c m68k-pinsn.c nm.c objdump.c sparc-opcode.h
51 * sparc-pinsn.c strip.c
53 * Verifying Portland tree with steve's last changes. Also, some partial
56 * Revision 1.6 1991/03/08 07:46:26 sac
57 * Added -l option to disassembly - prints line numbers too.
59 * Revision 1.5 1991/03/07 21:50:24 sac
60 * More intelligent reloc printing
62 * Revision 1.4 1991/03/05 16:36:54 sac
63 * Fixed bug where empty symbols would print (null) on suns and crash elsewhere.
67 * Until there is other documentation, refer to the manual page dump(1) in
68 * the system 5 program's reference manual
81 char *default_target
= NULL
; /* default at runtime */
83 char *program_name
= NULL
;
85 int dump_section_contents
; /* -s */
86 int dump_section_headers
; /* -h */
87 boolean dump_file_header
; /* -f */
88 int dump_symtab
; /* -t */
89 int dump_reloc_info
; /* -r */
90 int dump_ar_hdrs
; /* -a */
91 int with_line_numbers
; /* -l */
92 boolean disassemble
; /* -d */
95 PROTO (void, display_file
, (char *filename
, char *target
));
96 PROTO (void, dump_data
, (bfd
*abfd
));
97 PROTO (void, dump_relocs
, (bfd
*abfd
));
98 PROTO (void, dump_symbols
, (bfd
*abfd
));
99 PROTO (void, print_arelt_descr
, (bfd
*abfd
, boolean verbose
));
107 char *machine
= (char *)NULL
;
112 unsigned int storage
;
114 unsigned int symcount
= 0;
120 "usage: %s [-ahfdrtxsl] [-m machine] [-j section_name] obj ...\n",
125 static struct option long_options
[] =
126 {{"syms", 0, &dump_symtab
, 1},
127 {"reloc", 0, &dump_reloc_info
, 1},
128 {"header", 0, &dump_section_headers
, 1},
138 for (section
= abfd
->sections
;
139 section
!= (asection
*) NULL
;
140 section
= section
->next
)
144 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
146 printf("SECTION %d [%s]\t: size %08x",
149 (unsigned) section
->size
);
150 printf(" vma %08lx align 2**%2u\n ",
152 section
->alignment_power
);
153 PF(SEC_ALLOC
,"ALLOC");
155 PF(SEC_RELOC
,"RELOC");
156 PF(SEC_BALIGN
,"BALIGN");
157 PF(SEC_READONLY
,"READONLY");
171 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
)) {
172 (void) printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd
));
176 storage
= get_symtab_upper_bound (abfd
);
178 sy
= (asymbol
**) malloc (storage
);
180 fprintf (stderr
, "%s: out of memory.\n", program_name
);
184 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
187 /* Sort symbols into value order */
188 static int comp(ap
,bp
)
196 if ( a
->name
== (char *)NULL
|| (a
->flags
&( BSF_DEBUGGING
| BSF_UNDEFINED
) ))
198 if ( b
->name
== (char *)NULL
|| (b
->flags
&( BSF_DEBUGGING
|BSF_UNDEFINED
)))
201 diff
= a
->the_bfd
- b
->the_bfd
;
205 diff
= a
->value
- b
->value
;
209 return a
->section
- b
->section
;
212 /* Print the supplied address symbolically if possible */
214 print_address(vma
, stream
)
218 /* Perform a binary search looking for the closest symbol to
219 the required value */
221 unsigned int min
= 0;
222 unsigned int max
= symcount
;
224 unsigned int thisplace
= 1;
225 unsigned int oldthisplace
;
229 fprintf(stream
,"%08lx", vma
);
232 oldthisplace
= thisplace
;
233 thisplace
= (max
+ min
)/2 ;
234 if (thisplace
== oldthisplace
) break;
237 vardiff
= syms
[thisplace
]->value
- vma
;
248 /* Totally awesome! the exact right symbol */
249 fprintf(stream
,"%08lx (%s)", vma
, syms
[thisplace
]->name
);
253 /* We've run out of places to look, print the symbol before this one */
254 /* see if this or the symbol before describes this location the best */
256 if (thisplace
!= 0) {
257 if (syms
[thisplace
-1]->value
- vma
>
258 syms
[thisplace
]->value
-vma
) {
259 /* Previous symbol is in correct section and is closer */
266 asection
*sec
= syms
[thisplace
]->section
;
268 section_name
= sec
->name
;
271 section_name
= "abs";
274 if (syms
[thisplace
]->value
> vma
) {
275 fprintf(stream
,"%08lx (%s-%lx)", vma
, syms
[thisplace
]->name
,
276 syms
[thisplace
]->value
- vma
);
280 fprintf(stream
,"%08lx (%s+%lx)", vma
,
281 syms
[thisplace
]->name
,
282 vma
- syms
[thisplace
]->value
);
288 disassemble_data(abfd
)
291 bfd_byte
*data
= NULL
;
292 unsigned int datasize
= 0;
295 int print_insn_m68k();
296 int print_insn_i960();
297 int print_insn_sparc();
298 enum bfd_architecture a
;
301 /* Replace symbol section relative values with abs values */
304 for (i
= 0; i
< symcount
; i
++) {
305 if (syms
[i
]->section
!= (asection
*)NULL
) {
306 syms
[i
]->value
+= syms
[i
]->section
->vma
;
310 /* We keep a copy of the symbols in the original order */
311 syms2
= slurp_symtab(abfd
);
313 /* Sort the symbols into section and symbol order */
314 (void) qsort(syms
, symcount
, sizeof(asymbol
*), comp
);
316 /* Find the first useless symbol */
318 for (i
=0; i
< symcount
; i
++) {
319 if (syms
[i
]->the_bfd
== 0) {
327 if (machine
!= (char *)NULL
) {
328 if (bfd_scan_arch_mach(machine
, &a
, &m
) == false) {
329 fprintf(stderr
,"%s: Can't use supplied machine %s\n",
336 a
= bfd_get_architecture(abfd
);
341 print
= print_insn_sparc
;
344 print
= print_insn_m68k
;
347 print
= print_insn_i960
;
350 fprintf(stderr
,"%s: Can't disassemble for architecture %s\n",
352 bfd_printable_arch_mach(bfd_get_architecture(abfd
),0));
357 for (section
= abfd
->sections
;
358 section
!= (asection
*)NULL
;
359 section
= section
->next
) {
361 if (only
== (char *)NULL
|| strcmp(only
,section
->name
) == 0){
362 printf("Disassembly of section %s:\n", section
->name
);
364 if (section
->size
== 0) continue;
366 data
= (bfd_byte
*)malloc(section
->size
);
368 if (data
== (bfd_byte
*)NULL
) {
369 fprintf (stderr
, "%s: memory exhausted.\n", program_name
);
372 datasize
= section
->size
;
375 bfd_get_section_contents (abfd
, section
, data
, 0, section
->size
);
378 while ((size_t)i
<section
->size
) {
379 if (with_line_numbers
) {
384 bfd_find_nearest_line(abfd
,
392 if (filename
&& functionname
&& line
&& line
!= prevline
) {
393 printf("%s:%d\n", filename
, line
);
397 print_address(section
->vma
+ i
, stdout
);
400 i
+= print(section
->vma
+ i
,
419 if (!bfd_check_format (abfd
, bfd_object
)) {
420 fprintf (stderr
,"%s: %s not an object file\n", program_name
,
424 printf ("\n%s: file format %s\n", abfd
->filename
, abfd
->xvec
->name
);
425 if (dump_ar_hdrs
) print_arelt_descr (abfd
, true);
427 if (dump_file_header
) {
430 printf("architecture: %s, ",
431 bfd_printable_arch_mach (bfd_get_architecture (abfd
),
432 bfd_get_machine (abfd
)));
433 printf("flags 0x%08x:\n", abfd
->flags
);
435 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
436 PF(HAS_RELOC
, "HAS_RELOC");
437 PF(EXEC_P
, "EXEC_P");
438 PF(HAS_LINENO
, "HAS_LINENO");
439 PF(HAS_DEBUG
, "HAS_DEBUG");
440 PF(HAS_SYMS
, "HAS_SYMS");
441 PF(HAS_LOCALS
, "HAS_LOCALS");
442 PF(DYNAMIC
, "DYNAMIC");
443 PF(WP_TEXT
, "WP_TEXT");
444 PF(D_PAGED
, "D_PAGED");
445 printf("\nstart address 0x%08lx", abfd
->start_address
);
449 if (dump_section_headers
)
451 if (dump_symtab
|| dump_reloc_info
|| disassemble
) {
452 syms
= slurp_symtab(abfd
);
454 if (dump_symtab
) dump_symbols (abfd
);
455 if (dump_reloc_info
) dump_relocs(abfd
);
456 if (dump_section_contents
) dump_data (abfd
);
457 if (disassemble
) disassemble_data(abfd
);
461 display_file (filename
, target
)
465 bfd
*file
, *arfile
= (bfd
*) NULL
;
467 file
= bfd_openr (filename
, target
);
469 bfd_perror (filename
);
473 if (bfd_check_format (file
, bfd_archive
) == true) {
474 printf ("In archive %s:\n", bfd_get_filename (file
));
476 bfd_error
= no_error
;
478 arfile
= bfd_openr_next_archived_file (file
, arfile
);
479 if (arfile
== NULL
) {
480 if (bfd_error
!= no_more_archived_files
)
481 bfd_perror (bfd_get_filename(file
));
485 display_bfd (arfile
);
486 /* Don't close the archive elements; we need them for next_archive */
495 /* Actually display the various requested regions */
512 unsigned int datasize
= 0;
515 for (section
= abfd
->sections
; section
!= NULL
; section
=
519 if (only
== (char *)NULL
||
520 strcmp(only
,section
->name
) == 0){
524 printf("Contents of section %s:\n", section
->name
);
526 if (section
->size
== 0) continue;
527 data
= (bfd_byte
*)malloc(section
->size
);
528 if (data
== (bfd_byte
*)NULL
) {
529 fprintf (stderr
, "%s: memory exhausted.\n", program_name
);
532 datasize
= section
->size
;
535 bfd_get_section_contents (abfd
, section
, data
, 0, section
->size
);
537 for (i
= 0; i
< section
->size
; i
+= onaline
) {
539 printf(" %04lx ", i
+ section
->vma
);
540 for (j
= i
; j
< i
+ onaline
; j
++) {
541 if (j
< section
->size
)
542 printf("%02x", (unsigned)(data
[j
]));
545 if ((j
& 3 ) == 3) printf(" ");
549 for (j
= i
; j
< i
+onaline
; j
++) {
550 if (j
>= section
->size
)
553 printf("%c", isprint(data
[j
]) ?data
[j
] : '.');
565 /* Should perhaps share code and display with nm? */
572 asymbol
**current
= syms
;
573 printf("SYMBOL TABLE:\n");
575 for (count
= 0; count
< symcount
; count
++) {
576 if ((*current
)->the_bfd
) {
577 bfd_print_symbol((*current
)->the_bfd
,
579 *current
, bfd_print_symbol_all_enum
);
595 unsigned int relcount
;
597 for (a
= abfd
->sections
; a
!= (asection
*)NULL
; a
= a
->next
) {
598 printf("RELOCATION RECORDS FOR [%s]:",a
->name
);
600 if (get_reloc_upper_bound(abfd
, a
) == 0) {
601 printf(" (none)\n\n");
606 relpp
= (arelent
**) xmalloc( get_reloc_upper_bound(abfd
,a
) );
607 relcount
= bfd_canonicalize_reloc(abfd
,a
,relpp
, syms
);
609 printf(" (none)\n\n");
613 printf("OFFSET TYPE VALUE \n");
615 for (p
=relpp
; *p
!= (arelent
*)NULL
; p
++) {
618 char *section_name
= q
->section
== (asection
*)NULL
? "*abs" :
620 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
) {
621 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
627 printf("%08lx %-8s %s",
633 printf("%08lx %-8s [%s]",
639 printf("+0x%lx(%ld)", q
->addend
, (long) q
->addend
);
652 /** main and like trivia */
661 char *target
= default_target
;
662 boolean seenflag
= false;
665 program_name
= *argv
;
667 while ((c
= getopt_long (argc
, argv
, "b:m:dlfahrtxsj:", long_options
, &ind
))
678 with_line_numbers
= 1;
684 dump_file_header
= true;
689 dump_file_header
= true;
691 dump_section_headers
= 1;
693 case 0 : break; /* we've been given a long option */
694 case 't': dump_symtab
= 1; break;
695 case 'd': disassemble
= true ; break;
696 case 's': dump_section_contents
= 1; break;
697 case 'r': dump_reloc_info
= 1; break;
698 case 'a': dump_ar_hdrs
= 1; break;
699 case 'h': dump_section_headers
= 1; break;
705 if (seenflag
== false)
709 display_file ("a.out", target
);
711 for (; optind
< argc
;)
712 display_file (argv
[optind
++], target
);