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. */
21 * Until there is other documentation, refer to the manual page dump(1) in
22 * the system 5 program's reference manual
35 char *default_target
= NULL
; /* default at runtime */
37 char *program_name
= NULL
;
39 int dump_section_contents
; /* -s */
40 int dump_section_headers
; /* -h */
41 boolean dump_file_header
; /* -f */
42 int dump_symtab
; /* -t */
43 int dump_reloc_info
; /* -r */
44 int dump_ar_hdrs
; /* -a */
45 int with_line_numbers
; /* -l */
46 boolean disassemble
; /* -d */
47 boolean info
; /* -i */
50 PROTO (void, display_file
, (char *filename
, char *target
));
51 PROTO (void, dump_data
, (bfd
* abfd
));
52 PROTO (void, dump_relocs
, (bfd
* abfd
));
53 PROTO (void, dump_symbols
, (bfd
* abfd
));
54 PROTO (void, print_arelt_descr
, (FILE *, bfd
* abfd
, boolean verbose
));
62 char *machine
= (char *) NULL
;
69 unsigned int symcount
= 0;
75 "usage: %s [-ahifdrtxsl] [-m machine] [-j section_name] obj ...\n",
80 static struct option long_options
[]=
82 {"syms", no_argument
, &dump_symtab
, 1},
83 {"reloc", no_argument
, &dump_reloc_info
, 1},
84 {"header", no_argument
, &dump_section_headers
, 1},
85 {0, no_argument
, 0, 0}};
95 for (section
= abfd
->sections
;
96 section
!= (asection
*) NULL
;
97 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");
118 PF (SEC_LOAD
, "LOAD");
119 PF (SEC_RELOC
, "RELOC");
120 PF (SEC_BALIGN
, "BALIGN");
121 PF (SEC_READONLY
, "READONLY");
122 PF (SEC_CODE
, "CODE");
123 PF (SEC_DATA
, "DATA");
131 DEFUN (slurp_symtab
, (abfd
),
134 asymbol
**sy
= (asymbol
**) NULL
;
136 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
138 (void) printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd
));
142 storage
= get_symtab_upper_bound (abfd
);
145 sy
= (asymbol
**) malloc (storage
);
148 fprintf (stderr
, "%s: out of memory.\n", program_name
);
152 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
156 /* Sort symbols into value order */
166 if (a
->name
== (char *) NULL
|| (a
->flags
& (BSF_DEBUGGING
)))
168 if (b
->name
== (char *) NULL
|| (b
->flags
& (BSF_DEBUGGING
)))
171 diff
= a
->the_bfd
- b
->the_bfd
;
176 diff
= a
->value
- b
->value
;
181 return a
->section
- b
->section
;
184 /* Print the supplied address symbolically if possible */
186 print_address (vma
, stream
)
190 /* Perform a binary search looking for the closest symbol to
191 the required value */
193 unsigned int min
= 0;
194 unsigned int max
= symcount
;
196 unsigned int thisplace
= 1;
197 unsigned int oldthisplace
;
203 fprintf_vma (stream
, vma
);
209 oldthisplace
= thisplace
;
210 thisplace
= (max
+ min
) / 2;
211 if (thisplace
== oldthisplace
)
213 vardiff
= syms
[thisplace
]->value
- vma
;
228 /* Totally awesome! the exact right symbol */
229 CONST
char *match_name
= syms
[thisplace
]->name
;
230 int sym_len
= strlen (match_name
);
232 /* Avoid "filename.o" as a match */
234 && match_name
[sym_len
- 2] == '.'
235 && match_name
[sym_len
- 1] == 'o'
236 && thisplace
+ 1 < symcount
237 && syms
[thisplace
+ 1]->value
== vma
)
238 match_name
= syms
[thisplace
+ 1]->name
;
239 /* Totally awesome! the exact right symbol */
240 fprintf_vma (stream
, vma
);
241 fprintf (stream
, " (%s+)0000", syms
[thisplace
]->name
);
245 /* We've run out of places to look, print the symbol before this one
246 see if this or the symbol before describes this location the best */
250 if (syms
[thisplace
- 1]->value
- vma
>
251 syms
[thisplace
]->value
- vma
)
253 /* Previous symbol is in correct section and is closer */
258 fprintf_vma (stream
, vma
);
259 if (syms
[thisplace
]->value
> vma
)
261 fprintf (stream
, " (%s-)", syms
[thisplace
]->name
);
262 fprintf (stream
, "%04x", syms
[thisplace
]->value
- vma
);
266 fprintf (stream
, " (%s+)", syms
[thisplace
]->name
);
267 fprintf (stream
, "%04x", vma
- syms
[thisplace
]->value
);
273 disassemble_data (abfd
)
276 bfd_byte
*data
= NULL
;
277 bfd_arch_info_type
*info
;
278 bfd_size_type datasize
= 0;
280 unsigned int (*print
) ()= 0;
281 unsigned int print_insn_m68k ();
282 unsigned int print_insn_a29k ();
283 unsigned int print_insn_i960 ();
284 unsigned int print_insn_sparc ();
285 unsigned int print_insn_i386 ();
286 unsigned int print_insn_h8300 ();
287 enum bfd_architecture a
;
291 /* Replace symbol section relative values with abs values */
292 boolean done_dot
= false;
294 for (i
= 0; i
< symcount
; i
++)
296 syms
[i
]->value
+= syms
[i
]->section
->vma
;
299 /* We keep a copy of the symbols in the original order */
300 syms2
= slurp_symtab (abfd
);
302 /* Sort the symbols into section and symbol order */
303 (void) qsort (syms
, symcount
, sizeof (asymbol
*), comp
);
305 /* Find the first useless symbol */
309 for (i
= 0; i
< symcount
; i
++)
311 if (syms
[i
]->the_bfd
== 0)
320 if (machine
!= (char *) NULL
)
322 info
= bfd_scan_arch (machine
);
325 fprintf (stderr
, "%s: Can't use supplied machine %s\n",
330 abfd
->arch_info
= info
;
333 /* See if we can disassemble using bfd */
335 if (abfd
->arch_info
->disassemble
)
337 print
= abfd
->arch_info
->disassemble
;
341 a
= bfd_get_arch (abfd
);
345 print
= print_insn_sparc
;
348 print
= print_insn_i386
;
351 print
= print_insn_m68k
;
354 print
= print_insn_a29k
;
357 print
= print_insn_i960
;
360 fprintf (stderr
, "%s: Can't disassemble for architecture %s\n",
362 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
368 for (section
= abfd
->sections
;
369 section
!= (asection
*) NULL
;
370 section
= section
->next
)
373 if ((section
->flags
& SEC_LOAD
)
374 && (only
== (char *) NULL
|| strcmp (only
, section
->name
) == 0))
376 printf ("Disassembly of section %s:\n", section
->name
);
378 if (bfd_get_section_size_before_reloc (section
) == 0)
381 data
= (bfd_byte
*) malloc (bfd_get_section_size_before_reloc (section
));
383 if (data
== (bfd_byte
*) NULL
)
385 fprintf (stderr
, "%s: memory exhausted.\n", program_name
);
388 datasize
= bfd_get_section_size_before_reloc (section
);
390 bfd_get_section_contents (abfd
, section
, data
, 0, bfd_get_section_size_before_reloc (section
));
393 while (i
< bfd_get_section_size_before_reloc (section
))
395 if (data
[i
] == 0 && data
[i
+ 1] == 0 && data
[i
+ 2] == 0 &&
398 if (done_dot
== false)
408 if (with_line_numbers
)
411 CONST
char *filename
;
412 CONST
char *functionname
;
415 bfd_find_nearest_line (abfd
,
423 if (filename
&& functionname
&& line
&& line
!= prevline
)
425 printf ("%s:%u\n", filename
, line
);
429 print_address (section
->vma
+ i
, stdout
);
432 i
+= print (section
->vma
+ i
,
448 if (!bfd_check_format (abfd
, bfd_object
))
450 fprintf (stderr
, "%s: %s not an object file\n", program_name
,
454 printf ("\n%s: file format %s\n", abfd
->filename
, abfd
->xvec
->name
);
456 print_arelt_descr (stdout
, abfd
, true);
458 if (dump_file_header
)
462 printf ("architecture: %s, ",
463 bfd_printable_arch_mach (bfd_get_arch (abfd
),
464 bfd_get_mach (abfd
)));
465 printf ("flags 0x%08x:\n", abfd
->flags
);
467 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
468 PF (HAS_RELOC
, "HAS_RELOC");
469 PF (EXEC_P
, "EXEC_P");
470 PF (HAS_LINENO
, "HAS_LINENO");
471 PF (HAS_DEBUG
, "HAS_DEBUG");
472 PF (HAS_SYMS
, "HAS_SYMS");
473 PF (HAS_LOCALS
, "HAS_LOCALS");
474 PF (DYNAMIC
, "DYNAMIC");
475 PF (WP_TEXT
, "WP_TEXT");
476 PF (D_PAGED
, "D_PAGED");
477 printf ("\nstart address 0x");
478 printf_vma (abfd
->start_address
);
482 if (dump_section_headers
)
484 if (dump_symtab
|| dump_reloc_info
|| disassemble
)
486 syms
= slurp_symtab (abfd
);
492 if (dump_section_contents
)
495 disassemble_data (abfd
);
499 display_file (filename
, target
)
503 bfd
*file
, *arfile
= (bfd
*) NULL
;
505 file
= bfd_openr (filename
, target
);
508 bfd_perror (filename
);
512 if (bfd_check_format (file
, bfd_archive
) == true)
514 printf ("In archive %s:\n", bfd_get_filename (file
));
517 bfd_error
= no_error
;
519 arfile
= bfd_openr_next_archived_file (file
, arfile
);
522 if (bfd_error
!= no_more_archived_files
)
523 bfd_perror (bfd_get_filename (file
));
527 display_bfd (arfile
);
528 /* Don't close the archive elements; we need them for next_archive */
537 /* Actually display the various requested regions */
545 bfd_size_type datasize
= 0;
548 for (section
= abfd
->sections
; section
!= NULL
; section
=
553 if (only
== (char *) NULL
||
554 strcmp (only
, section
->name
) == 0)
556 if (section
->flags
& SEC_HAS_CONTENTS
)
558 printf ("Contents of section %s:\n", section
->name
);
560 if (bfd_get_section_size_before_reloc (section
) == 0)
562 data
= (bfd_byte
*) malloc (bfd_get_section_size_before_reloc (section
));
563 if (data
== (bfd_byte
*) NULL
)
565 fprintf (stderr
, "%s: memory exhausted.\n", program_name
);
568 datasize
= bfd_get_section_size_before_reloc (section
);
571 bfd_get_section_contents (abfd
, section
, (PTR
) data
, 0, bfd_get_section_size_before_reloc (section
));
573 for (i
= 0; i
< bfd_get_section_size_before_reloc (section
); i
+= onaline
)
577 printf (" %04lx ", (unsigned long int) (i
+ section
->vma
));
578 for (j
= i
; j
< i
+ onaline
; j
++)
580 if (j
< bfd_get_section_size_before_reloc (section
))
581 printf ("%02x", (unsigned) (data
[j
]));
589 for (j
= i
; j
< i
+ onaline
; j
++)
591 if (j
>= bfd_get_section_size_before_reloc (section
))
594 printf ("%c", isprint (data
[j
]) ? data
[j
] : '.');
604 /* Should perhaps share code and display with nm? */
611 asymbol
**current
= syms
;
613 printf ("SYMBOL TABLE:\n");
615 for (count
= 0; count
< symcount
; count
++)
618 if (*current
&& (*current
)->the_bfd
)
620 bfd_print_symbol ((*current
)->the_bfd
,
622 *current
, bfd_print_symbol_all
);
638 unsigned int relcount
;
641 for (a
= abfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
643 if (a
== &bfd_abs_section
)
645 if (a
== &bfd_und_section
)
647 if (a
== &bfd_com_section
)
650 printf ("RELOCATION RECORDS FOR [%s]:", a
->name
);
652 if (bfd_get_reloc_upper_bound (abfd
, a
) == 0)
654 printf (" (none)\n\n");
660 relpp
= (arelent
**) xmalloc (bfd_get_reloc_upper_bound (abfd
, a
));
661 relcount
= bfd_canonicalize_reloc (abfd
, a
, relpp
, syms
);
664 printf (" (none)\n\n");
669 printf ("OFFSET TYPE VALUE \n");
671 for (p
= relpp
; relcount
&& *p
!= (arelent
*) NULL
; p
++,
675 CONST
char *sym_name
;
677 /* CONST char *section_name = q->section == (asection *)NULL ? "*abs" :*/
678 /* q->section->name;*/
679 CONST
char *section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
681 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
683 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
691 printf_vma (q
->address
);
698 printf_vma (q
->address
);
699 printf (" %-8s [%s]",
706 printf_vma (q
->addend
);
719 #define _DUMMY_NAME_ "/dev/null"
721 #define _DUMMY_NAME_ "##dummy"
724 DEFUN (display_info_table
, (first
, last
),
725 int first AND
int last
)
728 extern bfd_target
*target_vector
[];
730 printf ("\n%12s", " ");
731 for (i
= first
; i
++ < last
&& target_vector
[i
];)
732 printf ("%s ", target_vector
[i
]->name
);
735 for (j
= (int) bfd_arch_obscure
+ 1; (int) j
< (int) bfd_arch_last
; j
++)
736 if (strcmp (bfd_printable_arch_mach (j
, 0), "UNKNOWN!") != 0)
738 printf ("%11s ", bfd_printable_arch_mach (j
, 0));
739 for (i
= first
; i
++ < last
&& target_vector
[i
];)
741 bfd_target
*p
= target_vector
[i
];
742 bfd
*abfd
= bfd_openw (_DUMMY_NAME_
, p
->name
);
743 int l
= strlen (p
->name
);
744 int ok
= bfd_set_arch_mach (abfd
, j
, 0);
747 printf ("%s ", p
->name
);
751 printf ("%c", ok
? '*' : '-');
760 DEFUN_VOID (display_info
)
763 unsigned int i
, j
, columns
;
764 extern bfd_target
*target_vector
[];
765 extern char *getenv ();
767 printf ("BFD header file version %s\n", BFD_VERSION
);
768 for (i
= 0; target_vector
[i
]; i
++)
770 bfd_target
*p
= target_vector
[i
];
771 bfd
*abfd
= bfd_openw (_DUMMY_NAME_
, p
->name
);
773 printf ("%s\n (header %s, data %s)\n", p
->name
,
774 p
->header_byteorder_big_p
? "big endian" : "little endian",
775 p
->byteorder_big_p
? "big endian" : "little endian");
776 for (j
= (int) bfd_arch_obscure
+ 1; j
< (int) bfd_arch_last
; j
++)
777 if (bfd_set_arch_mach (abfd
, (enum bfd_architecture
) j
, 0))
779 bfd_printable_arch_mach ((enum bfd_architecture
) j
, 0));
782 if (colum
= getenv ("COLUMNS"))
783 columns
= atoi (colum
);
786 for (i
= 0; target_vector
[i
];)
790 for (j
= 12; target_vector
[i
] && j
< columns
; i
++)
791 j
+= strlen (target_vector
[i
]->name
) + 1;
795 display_info_table (old
, i
);
799 /** main and like trivia */
808 char *target
= default_target
;
809 boolean seenflag
= false;
813 program_name
= *argv
;
815 while ((c
= getopt_long (argc
, argv
, "ib:m:dlfahrtxsj:", long_options
, &ind
))
828 with_line_numbers
= 1;
834 dump_file_header
= true;
842 dump_file_header
= true;
844 dump_section_headers
= 1;
847 break; /* we've been given a long option */
855 dump_section_contents
= 1;
864 dump_section_headers
= 1;
871 if (seenflag
== false)
881 display_file ("a.out", target
);
883 for (; optind
< argc
;)
884 display_file (argv
[optind
++], target
);