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
31 #define ELF_STAB_DISPLAY /* This code works, but uses internal
32 bfd and elf stuff. Flip this define
33 off if you need to just use generic
36 #ifdef ELF_STAB_DISPLAY
37 /* Internal headers for the ELF .stab-dump code - sorry. */
38 #define BYTES_IN_WORD 32
39 #include "aout/aout64.h"
40 #include "elf/internal.h"
41 extern Elf_Internal_Shdr *bfd_elf_find_section();
42 #endif /* ELF_STAB_DISPLAY */
46 char *default_target = NULL; /* default at runtime */
48 char *program_name = NULL;
50 int dump_section_contents; /* -s */
51 int dump_section_headers; /* -h */
52 boolean dump_file_header; /* -f */
53 int dump_symtab; /* -t */
54 int dump_reloc_info; /* -r */
55 int dump_ar_hdrs; /* -a */
56 int with_line_numbers; /* -l */
57 int dump_stab_section_info; /* -stabs */
58 boolean disassemble; /* -d */
59 boolean info; /* -i */
62 PROTO (void, display_file, (char *filename, char *target));
63 PROTO (void, dump_data, (bfd * abfd));
64 PROTO (void, dump_relocs, (bfd * abfd));
65 PROTO (void, dump_symbols, (bfd * abfd));
66 PROTO (void, print_arelt_descr, (FILE *, bfd * abfd, boolean verbose));
74 char *machine = (char *) NULL;
81 unsigned int symcount = 0;
87 "usage: %s [-ahifdrtxsl] [-m machine] [-j section_name] obj ...\n",
92 static struct option long_options[]=
94 {"syms", no_argument, &dump_symtab, 1},
95 {"reloc", no_argument, &dump_reloc_info, 1},
96 {"header", no_argument, &dump_section_headers, 1},
97 #ifdef ELF_STAB_DISPLAY
98 {"stabs", no_argument, &dump_stab_section_info, 1},
100 {0, no_argument, 0, 0}};
109 for (section = abfd->sections;
110 section != (asection *) NULL;
111 section = section->next)
116 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
119 printf ("SECTION %d [%s]\t: size %08x",
122 (unsigned) bfd_get_section_size_before_reloc (section));
124 printf_vma (section->vma);
125 printf (" align 2**%u\n ",
126 section->alignment_power);
127 PF (SEC_ALLOC, "ALLOC");
128 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
129 PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
130 PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA");
131 PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS");
132 PF (SEC_LOAD, "LOAD");
133 PF (SEC_RELOC, "RELOC");
134 PF (SEC_BALIGN, "BALIGN");
135 PF (SEC_READONLY, "READONLY");
136 PF (SEC_CODE, "CODE");
137 PF (SEC_DATA, "DATA");
145 DEFUN (slurp_symtab, (abfd),
148 asymbol **sy = (asymbol **) NULL;
150 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
152 (void) printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
156 storage = get_symtab_upper_bound (abfd);
159 sy = (asymbol **) malloc (storage);
162 fprintf (stderr, "%s: out of memory.\n", program_name);
166 symcount = bfd_canonicalize_symtab (abfd, sy);
170 /* Sort symbols into value order */
176 asymbol *a = *(asymbol **)ap;
177 asymbol *b = *(asymbol **)bp;
180 if (a->name == (char *) NULL || (a->flags & (BSF_DEBUGGING)))
182 if (b->name == (char *) NULL || (b->flags & (BSF_DEBUGGING)))
185 diff = a->the_bfd - b->the_bfd;
190 diff = a->value - b->value;
195 return a->section - b->section;
198 /* Print the supplied address symbolically if possible */
200 print_address (vma, stream)
204 /* Perform a binary search looking for the closest symbol to
205 the required value */
207 unsigned int min = 0;
208 unsigned int max = symcount;
210 unsigned int thisplace = 1;
211 unsigned int oldthisplace;
217 fprintf_vma (stream, vma);
223 oldthisplace = thisplace;
224 thisplace = (max + min) / 2;
225 if (thisplace == oldthisplace)
227 vardiff = syms[thisplace]->value - vma;
242 /* Totally awesome! the exact right symbol */
243 CONST char *match_name = syms[thisplace]->name;
244 int sym_len = strlen (match_name);
246 /* Avoid "filename.o" as a match */
248 && match_name[sym_len - 2] == '.'
249 && match_name[sym_len - 1] == 'o'
250 && thisplace + 1 < symcount
251 && syms[thisplace + 1]->value == vma)
252 match_name = syms[thisplace + 1]->name;
253 /* Totally awesome! the exact right symbol */
254 fprintf_vma (stream, vma);
255 fprintf (stream, " (%s+)0000", syms[thisplace]->name);
259 /* We've run out of places to look, print the symbol before this one
260 see if this or the symbol before describes this location the best */
264 if (syms[thisplace - 1]->value - vma >
265 syms[thisplace]->value - vma)
267 /* Previous symbol is in correct section and is closer */
272 fprintf_vma (stream, vma);
273 if (syms[thisplace]->value > vma)
275 fprintf (stream, " (%s-)", syms[thisplace]->name);
276 fprintf (stream, "%04x", syms[thisplace]->value - vma);
280 fprintf (stream, " (%s+)", syms[thisplace]->name);
281 fprintf (stream, "%04x", vma - syms[thisplace]->value);
287 disassemble_data (abfd)
290 bfd_byte *data = NULL;
291 bfd_arch_info_type *info;
292 bfd_size_type datasize = 0;
294 unsigned int (*print) ()= 0;
295 unsigned int print_insn_m68k ();
296 unsigned int print_insn_a29k ();
297 unsigned int print_insn_i960 ();
298 unsigned int print_insn_sparc ();
299 unsigned int print_insn_i386 ();
300 unsigned int print_insn_h8300 ();
301 enum bfd_architecture a;
305 /* Replace symbol section relative values with abs values */
306 boolean done_dot = false;
308 for (i = 0; i < symcount; i++)
310 syms[i]->value += syms[i]->section->vma;
313 /* We keep a copy of the symbols in the original order */
314 syms2 = slurp_symtab (abfd);
316 /* Sort the symbols into section and symbol order */
317 (void) qsort (syms, symcount, sizeof (asymbol *), comp);
319 /* Find the first useless symbol */
323 for (i = 0; i < symcount; i++)
325 if (syms[i]->the_bfd == 0)
334 if (machine != (char *) NULL)
336 info = bfd_scan_arch (machine);
339 fprintf (stderr, "%s: Can't use supplied machine %s\n",
344 abfd->arch_info = info;
347 /* See if we can disassemble using bfd */
349 if (abfd->arch_info->disassemble)
351 print = abfd->arch_info->disassemble;
355 a = bfd_get_arch (abfd);
359 print = print_insn_sparc;
362 print = print_insn_i386;
365 print = print_insn_m68k;
368 print = print_insn_a29k;
371 print = print_insn_i960;
374 fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
376 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
382 for (section = abfd->sections;
383 section != (asection *) NULL;
384 section = section->next)
387 if ((section->flags & SEC_LOAD)
388 && (only == (char *) NULL || strcmp (only, section->name) == 0))
390 printf ("Disassembly of section %s:\n", section->name);
392 if (bfd_get_section_size_before_reloc (section) == 0)
395 data = (bfd_byte *) malloc (bfd_get_section_size_before_reloc (section));
397 if (data == (bfd_byte *) NULL)
399 fprintf (stderr, "%s: memory exhausted.\n", program_name);
402 datasize = bfd_get_section_size_before_reloc (section);
404 bfd_get_section_contents (abfd, section, data, 0, bfd_get_section_size_before_reloc (section));
407 while (i < bfd_get_section_size_before_reloc (section))
409 if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 &&
412 if (done_dot == false)
422 if (with_line_numbers)
425 CONST char *filename;
426 CONST char *functionname;
429 bfd_find_nearest_line (abfd,
437 if (filename && functionname && line && line != prevline)
439 printf ("%s:%u\n", filename, line);
443 print_address (section->vma + i, stdout);
446 i += print (section->vma + i,
457 #ifdef ELF_STAB_DISPLAY
459 /* Define a table of stab values and print-strings. We wish the initializer
460 could be a direct-mapped table, but instead we build one the first
463 #define STAB_STRING_LENGTH 6
465 char stab_name[256][STAB_STRING_LENGTH];
469 char string[STAB_STRING_LENGTH];
472 struct stab_print stab_print[] = {
473 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
474 #include "aout/stab.def"
479 /* This is a kludge for dumping the stabs section from an ELF file that
480 uses Sun stabs encoding. It has to use some hooks into BFD because
481 string table sections are not normally visible to BFD callers. */
484 dump_elf_stabs (abfd)
487 Elf_Internal_Shdr *stab_hdr, *stabstr_hdr;
489 struct internal_nlist *stabs, *stabs_end;
492 /* Initialize stab name array if first time. */
493 if (stab_name[0][0] == 0)
495 /* Fill in numeric values for all possible strings. */
496 for (i = 0; i < 256; i++)
498 sprintf (stab_name[i], "%d", i);
500 for (i = 0; stab_print[i].string[0]; i++)
501 strcpy (stab_name[stab_print[i].value], stab_print[i].string);
504 if (0 != strncmp ("elf", abfd->xvec->name, 3))
506 fprintf (stderr, "%s: %s is not in ELF format.\n", program_name,
511 stab_hdr = bfd_elf_find_section (abfd, ".stab");
514 fprintf (stderr, "%s: %s has no .stab section.\n", program_name,
519 stabstr_hdr = bfd_elf_find_section (abfd, ".stabstr");
520 if (0 == stabstr_hdr)
522 fprintf (stderr, "%s: %s has no .stabstr section.\n", program_name,
527 stabs = (struct internal_nlist *) xmalloc (stab_hdr ->sh_size);
528 strtab = (char *) xmalloc (stabstr_hdr->sh_size);
529 stabs_end = (struct internal_nlist *) (stab_hdr->sh_size + (char *)stabs);
531 if (bfd_seek (abfd, stab_hdr->sh_offset, L_SET) < 0 ||
532 stab_hdr->sh_size != bfd_read ((PTR)stabs, stab_hdr->sh_size, 1, abfd))
534 fprintf (stderr, "%s: reading .stab section of %s failed.\n",
540 if (bfd_seek (abfd, stabstr_hdr->sh_offset, L_SET) < 0 ||
541 stabstr_hdr->sh_size != bfd_read ((PTR)strtab, stabstr_hdr->sh_size,
544 fprintf (stderr, "%s: reading .stabstr section of %s failed.\n",
550 #define SWAP_SYMBOL(symp, abfd) \
552 (symp)->n_strx = bfd_h_get_32(abfd, \
553 (unsigned char *)&(symp)->n_strx); \
554 (symp)->n_desc = bfd_h_get_16 (abfd, \
555 (unsigned char *)&(symp)->n_desc); \
556 (symp)->n_value = bfd_h_get_32 (abfd, \
557 (unsigned char *)&(symp)->n_value); \
560 printf ("Contents of .stab section:\n\n");
561 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
563 /* We start the index at -1 because there is a dummy symbol on
564 the front of Sun's stabs-in-elf sections. */
565 for (i = -1; stabs < stabs_end; stabs++, i++)
567 SWAP_SYMBOL (stabs, abfd);
568 printf ("\n%-6d %-6s %-6d %-6d %08x %-6d", i,
569 stab_name [stabs->n_type],
570 stabs->n_other, stabs->n_desc, stabs->n_value,
572 if (stabs->n_strx < stabstr_hdr->sh_size)
573 printf (" %s", &strtab[stabs->n_strx]);
577 #endif /* ELF_STAB_DISPLAY */
584 if (!bfd_check_format (abfd, bfd_object))
586 fprintf (stderr, "%s: %s not an object file\n", program_name,
590 printf ("\n%s: file format %s\n", abfd->filename, abfd->xvec->name);
592 print_arelt_descr (stdout, abfd, true);
594 if (dump_file_header)
598 printf ("architecture: %s, ",
599 bfd_printable_arch_mach (bfd_get_arch (abfd),
600 bfd_get_mach (abfd)));
601 printf ("flags 0x%08x:\n", abfd->flags);
603 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
604 PF (HAS_RELOC, "HAS_RELOC");
605 PF (EXEC_P, "EXEC_P");
606 PF (HAS_LINENO, "HAS_LINENO");
607 PF (HAS_DEBUG, "HAS_DEBUG");
608 PF (HAS_SYMS, "HAS_SYMS");
609 PF (HAS_LOCALS, "HAS_LOCALS");
610 PF (DYNAMIC, "DYNAMIC");
611 PF (WP_TEXT, "WP_TEXT");
612 PF (D_PAGED, "D_PAGED");
613 printf ("\nstart address 0x");
614 printf_vma (abfd->start_address);
618 if (dump_section_headers)
620 if (dump_symtab || dump_reloc_info || disassemble)
622 syms = slurp_symtab (abfd);
626 #ifdef ELF_STAB_DISPLAY
627 if (dump_stab_section_info)
628 dump_elf_stabs (abfd);
632 if (dump_section_contents)
635 disassemble_data (abfd);
639 display_file (filename, target)
643 bfd *file, *arfile = (bfd *) NULL;
645 file = bfd_openr (filename, target);
648 bfd_perror (filename);
652 if (bfd_check_format (file, bfd_archive) == true)
654 printf ("In archive %s:\n", bfd_get_filename (file));
657 bfd_error = no_error;
659 arfile = bfd_openr_next_archived_file (file, arfile);
662 if (bfd_error != no_more_archived_files)
663 bfd_perror (bfd_get_filename (file));
667 display_bfd (arfile);
668 /* Don't close the archive elements; we need them for next_archive */
677 /* Actually display the various requested regions */
685 bfd_size_type datasize = 0;
688 for (section = abfd->sections; section != NULL; section =
693 if (only == (char *) NULL ||
694 strcmp (only, section->name) == 0)
696 if (section->flags & SEC_HAS_CONTENTS)
698 printf ("Contents of section %s:\n", section->name);
700 if (bfd_get_section_size_before_reloc (section) == 0)
702 data = (bfd_byte *) malloc (bfd_get_section_size_before_reloc (section));
703 if (data == (bfd_byte *) NULL)
705 fprintf (stderr, "%s: memory exhausted.\n", program_name);
708 datasize = bfd_get_section_size_before_reloc (section);
711 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_get_section_size_before_reloc (section));
713 for (i = 0; i < bfd_get_section_size_before_reloc (section); i += onaline)
717 printf (" %04lx ", (unsigned long int) (i + section->vma));
718 for (j = i; j < i + onaline; j++)
720 if (j < bfd_get_section_size_before_reloc (section))
721 printf ("%02x", (unsigned) (data[j]));
729 for (j = i; j < i + onaline; j++)
731 if (j >= bfd_get_section_size_before_reloc (section))
734 printf ("%c", isprint (data[j]) ? data[j] : '.');
744 /* Should perhaps share code and display with nm? */
751 asymbol **current = syms;
753 printf ("SYMBOL TABLE:\n");
755 for (count = 0; count < symcount; count++)
758 if (*current && (*current)->the_bfd)
760 bfd_print_symbol ((*current)->the_bfd,
762 *current, bfd_print_symbol_all);
778 unsigned int relcount;
781 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
783 if (a == &bfd_abs_section)
785 if (a == &bfd_und_section)
787 if (a == &bfd_com_section)
790 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
792 if (bfd_get_reloc_upper_bound (abfd, a) == 0)
794 printf (" (none)\n\n");
800 relpp = (arelent **) xmalloc (bfd_get_reloc_upper_bound (abfd, a));
801 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
804 printf (" (none)\n\n");
809 printf ("OFFSET TYPE VALUE \n");
811 for (p = relpp; relcount && *p != (arelent *) NULL; p++,
815 CONST char *sym_name;
817 /* CONST char *section_name = q->section == (asection *)NULL ? "*abs" :*/
818 /* q->section->name;*/
819 CONST char *section_name = (*(q->sym_ptr_ptr))->section->name;
821 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
823 sym_name = (*(q->sym_ptr_ptr))->name;
831 printf_vma (q->address);
838 printf_vma (q->address);
839 printf (" %-8s [%s]",
846 printf_vma (q->addend);
859 #define _DUMMY_NAME_ "/dev/null"
861 #define _DUMMY_NAME_ "##dummy"
864 DEFUN (display_info_table, (first, last),
865 int first AND int last)
868 extern bfd_target *target_vector[];
870 printf ("\n%12s", " ");
871 for (i = first; i++ < last && target_vector[i];)
872 printf ("%s ", target_vector[i]->name);
875 for (j = (int) bfd_arch_obscure + 1; (int) j < (int) bfd_arch_last; j++)
876 if (strcmp (bfd_printable_arch_mach (j, 0), "UNKNOWN!") != 0)
878 printf ("%11s ", bfd_printable_arch_mach (j, 0));
879 for (i = first; i++ < last && target_vector[i];)
881 bfd_target *p = target_vector[i];
882 bfd *abfd = bfd_openw (_DUMMY_NAME_, p->name);
883 int l = strlen (p->name);
884 int ok = bfd_set_arch_mach (abfd, j, 0);
887 printf ("%s ", p->name);
891 printf ("%c", ok ? '*' : '-');
900 DEFUN_VOID (display_info)
903 unsigned int i, j, columns;
904 extern bfd_target *target_vector[];
905 extern char *getenv ();
907 printf ("BFD header file version %s\n", BFD_VERSION);
908 for (i = 0; target_vector[i]; i++)
910 bfd_target *p = target_vector[i];
911 bfd *abfd = bfd_openw (_DUMMY_NAME_, p->name);
913 printf ("%s\n (header %s, data %s)\n", p->name,
914 p->header_byteorder_big_p ? "big endian" : "little endian",
915 p->byteorder_big_p ? "big endian" : "little endian");
916 for (j = (int) bfd_arch_obscure + 1; j < (int) bfd_arch_last; j++)
917 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) j, 0))
919 bfd_printable_arch_mach ((enum bfd_architecture) j, 0));
922 if (colum = getenv ("COLUMNS"))
923 columns = atoi (colum);
926 for (i = 0; target_vector[i];)
930 for (j = 12; target_vector[i] && j < columns; i++)
931 j += strlen (target_vector[i]->name) + 1;
935 display_info_table (old, i);
939 /** main and like trivia */
948 char *target = default_target;
949 boolean seenflag = false;
953 program_name = *argv;
955 while ((c = getopt_long (argc, argv, "ib:m:dlfahrtxsj:", long_options, &ind))
968 with_line_numbers = 1;
974 dump_file_header = true;
982 dump_file_header = true;
984 dump_section_headers = 1;
987 break; /* we've been given a long option */
995 dump_section_contents = 1;
1004 dump_section_headers = 1;
1011 if (seenflag == false)
1021 display_file ("a.out", target);
1023 for (; optind < argc;)
1024 display_file (argv[optind++], target);