1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program 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 This program 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 this program; if not, write to the Free Software
18 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #define ELF_STAB_DISPLAY /* This code works, but uses internal
28 bfd and elf stuff. Flip this define
29 off if you need to just use generic
32 #ifdef ELF_STAB_DISPLAY
33 /* Internal headers for the ELF .stab-dump code - sorry. */
34 #define BYTES_IN_WORD 32
35 #include "aout/aout64.h"
36 #include "elf/internal.h"
37 extern Elf_Internal_Shdr *bfd_elf_find_section();
38 #endif /* ELF_STAB_DISPLAY */
40 extern char *xmalloc ();
41 extern int fprintf PARAMS ((FILE *, CONST char *, ...));
43 char *default_target = NULL; /* default at runtime */
45 extern *program_version;
46 char *program_name = NULL;
48 int show_version = 0; /* show the version number */
49 int dump_section_contents; /* -s */
50 int dump_section_headers; /* -h */
51 boolean dump_file_header; /* -f */
52 int dump_symtab; /* -t */
53 int dump_reloc_info; /* -r */
54 int dump_ar_hdrs; /* -a */
55 int with_line_numbers; /* -l */
56 int dump_stab_section_info; /* -stabs */
57 boolean disassemble; /* -d */
58 boolean info; /* -i */
61 char *machine = (char *) NULL;
66 unsigned int symcount = 0;
68 /* Forward declarations. */
71 display_file PARAMS ((char *filename, char *target));
74 dump_data PARAMS ((bfd *abfd));
77 dump_relocs PARAMS ((bfd *abfd));
80 dump_symbols PARAMS ((bfd *abfd));
83 usage (stream, status)
88 Usage: %s [-ahifdrtxsl] [-m machine] [-j section_name] [-b bfdname]\n\
89 [--syms] [--reloc] [--header] [--version] [--help] objfile...\n",
94 static struct option long_options[]=
96 {"syms", no_argument, &dump_symtab, 1},
97 {"reloc", no_argument, &dump_reloc_info, 1},
98 {"header", no_argument, &dump_section_headers, 1},
99 {"version", no_argument, &show_version, 1},
100 {"help", no_argument, 0, 'H'},
101 #ifdef ELF_STAB_DISPLAY
102 {"stabs", no_argument, &dump_stab_section_info, 1},
104 {0, no_argument, 0, 0}
114 for (section = abfd->sections;
115 section != (asection *) NULL;
116 section = section->next)
121 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
124 printf ("SECTION %d [%s]\t: size %08x",
127 (unsigned) bfd_get_section_size_before_reloc (section));
129 printf_vma (section->vma);
130 printf (" align 2**%u\n ",
131 section->alignment_power);
132 PF (SEC_ALLOC, "ALLOC");
133 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
134 PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
135 PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA");
136 PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS");
137 PF (SEC_LOAD, "LOAD");
138 PF (SEC_RELOC, "RELOC");
139 PF (SEC_BALIGN, "BALIGN");
140 PF (SEC_READONLY, "READONLY");
141 PF (SEC_CODE, "CODE");
142 PF (SEC_DATA, "DATA");
150 DEFUN (slurp_symtab, (abfd),
153 asymbol **sy = (asymbol **) NULL;
155 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
157 (void) printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
161 storage = get_symtab_upper_bound (abfd);
164 sy = (asymbol **) malloc (storage);
167 fprintf (stderr, "%s: out of memory.\n", program_name);
171 symcount = bfd_canonicalize_symtab (abfd, sy);
174 fprintf (stderr, "%s: Bad symbol table in \"%s\".\n",
175 program_name, bfd_get_filename (abfd));
181 /* Filter out (in place) symbols that are useless for dis-assemble.
182 Return count of useful symbols. */
184 int remove_useless_symbols (syms, count)
188 register asymbol **in_ptr = syms;
189 register asymbol **out_ptr = syms;
191 while ( --count >= 0 )
193 asymbol *sym = *in_ptr++;
195 if (sym->name == NULL || sym->name[0] == '\0')
197 if (sym->flags & (BSF_DEBUGGING))
199 if (sym->section == &bfd_und_section
200 || bfd_is_com_section (sym->section))
205 return out_ptr - syms;
209 /* Sort symbols into value order */
215 asymbol *a = *(asymbol **)ap;
216 asymbol *b = *(asymbol **)bp;
218 if (a->value > b->value)
220 else if (a->value < b->value)
223 if (a->section > b->section)
225 else if (a->section < b->section)
230 /* Print the supplied address symbolically if possible */
232 objdump_print_address (vma, info)
234 struct disassemble_info *info;
236 /* Perform a binary search looking for the closest symbol to
237 the required value */
239 unsigned int min = 0;
240 unsigned int max = symcount;
242 unsigned int thisplace = 1;
243 unsigned int oldthisplace;
247 fprintf_vma (info->stream, vma);
253 asymbol *sym; asection *sym_sec;
254 oldthisplace = thisplace;
255 thisplace = (max + min) / 2;
256 if (thisplace == oldthisplace)
258 sym = syms[thisplace];
259 vardiff = sym->value - vma;
260 sym_sec = sym->section;
264 else if (vardiff < 0)
269 /* We've run out of places to look, print the symbol before this one
270 see if this or the symbol before describes this location the best */
274 if (syms[thisplace - 1]->value - vma >
275 syms[thisplace]->value - vma)
277 /* Previous symbol is in correct section and is closer */
283 fprintf (info->stream, " <%s", syms[thisplace]->name);
284 if (syms[thisplace]->value > vma)
286 char buf[30], *p = buf;
287 sprintf_vma (buf, syms[thisplace]->value - vma);
290 fprintf (info->stream, "-%s", p);
292 else if (vma > syms[thisplace]->value)
294 char buf[30], *p = buf;
295 sprintf_vma (buf, vma - syms[thisplace]->value);
298 fprintf (info->stream, "+%s", p);
300 fprintf (info->stream, ">");
305 disassemble_data (abfd)
308 bfd_byte *data = NULL;
309 bfd_arch_info_type *info;
310 bfd_size_type datasize = 0;
312 unsigned int (*print) ()= 0; /* Old style */
313 disassembler_ftype disassemble = 0; /* New style */
314 enum bfd_architecture a;
315 struct disassemble_info disasm_info;
318 CONST char *prev_function = "";
322 /* Replace symbol section relative values with abs values */
323 boolean done_dot = false;
325 INIT_DISASSEMBLE_INFO(disasm_info, stdout);
326 disasm_info.print_address_func = objdump_print_address;
328 for (i = 0; i < symcount; i++)
330 syms[i]->value += syms[i]->section->vma;
333 symcount = remove_useless_symbols (syms, symcount);
335 /* Sort the symbols into section and symbol order */
336 (void) qsort (syms, symcount, sizeof (asymbol *), comp);
338 if (machine != (char *) NULL)
340 info = bfd_scan_arch (machine);
343 fprintf (stderr, "%s: Can't use supplied machine %s\n",
348 abfd->arch_info = info;
351 /* See if we can disassemble using bfd */
353 if (abfd->arch_info->disassemble)
355 print = abfd->arch_info->disassemble;
359 a = bfd_get_arch (abfd);
363 disassemble = print_insn_sparc;
366 if (bfd_get_mach(abfd) == bfd_mach_z8001)
367 disassemble = print_insn_z8001;
369 disassemble = print_insn_z8002;
372 disassemble = print_insn_i386;
375 disassemble = print_insn_h8500;
378 if (bfd_get_mach(abfd) == bfd_mach_h8300h)
379 disassemble = print_insn_h8300h;
381 disassemble = print_insn_h8300;
384 disassemble = print_insn_sh;
387 disassemble = print_insn_alpha;
390 disassemble = print_insn_m68k;
393 /* As far as I know we only handle big-endian 29k objects. */
394 disassemble = print_insn_big_a29k;
397 disassemble = print_insn_i960;
400 disassemble = print_insn_m88k;
403 if (abfd->xvec->byteorder_big_p)
404 disassemble = print_insn_big_mips;
406 disassemble = print_insn_little_mips;
409 fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
411 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
417 for (section = abfd->sections;
418 section != (asection *) NULL;
419 section = section->next)
422 if ((section->flags & SEC_LOAD)
423 && (only == (char *) NULL || strcmp (only, section->name) == 0))
425 printf ("Disassembly of section %s:\n", section->name);
427 if (bfd_get_section_size_before_reloc (section) == 0)
430 data = (bfd_byte *) malloc (bfd_get_section_size_before_reloc (section));
432 if (data == (bfd_byte *) NULL)
434 fprintf (stderr, "%s: memory exhausted.\n", program_name);
437 datasize = bfd_get_section_size_before_reloc (section);
439 bfd_get_section_contents (abfd, section, data, 0, bfd_get_section_size_before_reloc (section));
441 disasm_info.buffer = data;
442 disasm_info.buffer_vma = section->vma;
443 disasm_info.buffer_length =
444 bfd_get_section_size_before_reloc (section);
446 while (i < disasm_info.buffer_length)
448 if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 &&
451 if (done_dot == false)
461 if (with_line_numbers)
463 CONST char *filename;
464 CONST char *functionname;
467 if (bfd_find_nearest_line (abfd,
475 if (functionname && *functionname
476 && strcmp(functionname, prev_function))
478 printf ("%s():\n", functionname);
479 prev_function = functionname;
483 if (line && line != prevline)
485 printf ("%s:%u\n", filename, line);
490 objdump_print_address (section->vma + i, &disasm_info);
493 if (disassemble) /* New style */
495 int bytes = (*disassemble)(section->vma + i,
502 i += print (section->vma + i,
513 #ifdef ELF_STAB_DISPLAY
515 /* Define a table of stab values and print-strings. We wish the initializer
516 could be a direct-mapped table, but instead we build one the first
519 #define STAB_STRING_LENGTH 6
521 char stab_name[256][STAB_STRING_LENGTH];
525 char string[STAB_STRING_LENGTH];
528 struct stab_print stab_print[] = {
529 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
530 #include "aout/stab.def"
535 void dump_elf_stabs_1 ();
537 /* This is a kludge for dumping the stabs section from an ELF file that
538 uses Sun stabs encoding. It has to use some hooks into BFD because
539 string table sections are not normally visible to BFD callers. */
542 dump_elf_stabs (abfd)
547 /* Initialize stab name array if first time. */
548 if (stab_name[0][0] == 0)
550 /* Fill in numeric values for all possible strings. */
551 for (i = 0; i < 256; i++)
553 sprintf (stab_name[i], "%d", i);
555 for (i = 0; stab_print[i].string[0]; i++)
556 strcpy (stab_name[stab_print[i].value], stab_print[i].string);
559 if (0 != strncmp ("elf", abfd->xvec->name, 3))
561 fprintf (stderr, "%s: %s is not in ELF format.\n", program_name,
566 dump_elf_stabs_1 (abfd, ".stab", ".stabstr");
567 dump_elf_stabs_1 (abfd, ".stab.excl", ".stab.exclstr");
568 dump_elf_stabs_1 (abfd, ".stab.index", ".stab.indexstr");
572 dump_elf_stabs_1 (abfd, name1, name2)
574 char *name1; /* Section name of .stab */
575 char *name2; /* Section name of its string section */
577 Elf_Internal_Shdr *stab_hdr, *stabstr_hdr;
579 struct internal_nlist *stabs, *stabs_end;
581 unsigned file_string_table_offset, next_file_string_table_offset;
583 stab_hdr = bfd_elf_find_section (abfd, name1);
586 printf ("Contents of %s section: none.\n\n", name1);
590 stabstr_hdr = bfd_elf_find_section (abfd, name2);
591 if (0 == stabstr_hdr)
593 fprintf (stderr, "%s: %s has no %s section.\n", program_name,
594 abfd->filename, name2);
598 stabs = (struct internal_nlist *) xmalloc (stab_hdr ->sh_size);
599 strtab = (char *) xmalloc (stabstr_hdr->sh_size);
600 stabs_end = (struct internal_nlist *) (stab_hdr->sh_size + (char *)stabs);
602 if (bfd_seek (abfd, stab_hdr->sh_offset, SEEK_SET) < 0 ||
603 stab_hdr->sh_size != bfd_read ((PTR)stabs, stab_hdr->sh_size, 1, abfd))
605 fprintf (stderr, "%s: reading %s section of %s failed.\n",
611 if (bfd_seek (abfd, stabstr_hdr->sh_offset, SEEK_SET) < 0 ||
612 stabstr_hdr->sh_size != bfd_read ((PTR)strtab, stabstr_hdr->sh_size,
615 fprintf (stderr, "%s: reading %s section of %s failed.\n",
621 #define SWAP_SYMBOL(symp, abfd) \
623 (symp)->n_strx = bfd_h_get_32(abfd, \
624 (unsigned char *)&(symp)->n_strx); \
625 (symp)->n_desc = bfd_h_get_16 (abfd, \
626 (unsigned char *)&(symp)->n_desc); \
627 (symp)->n_value = bfd_h_get_32 (abfd, \
628 (unsigned char *)&(symp)->n_value); \
631 printf ("Contents of %s section:\n\n", name1);
632 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
634 file_string_table_offset = 0;
635 next_file_string_table_offset = 0;
637 /* Loop through all symbols and print them.
639 We start the index at -1 because there is a dummy symbol on
640 the front of Sun's stabs-in-elf sections. */
642 for (i = -1; stabs < stabs_end; stabs++, i++)
644 SWAP_SYMBOL (stabs, abfd);
645 printf ("\n%-6d %-6s %-6d %-6d %08x %-6d", i,
646 stab_name [stabs->n_type],
647 stabs->n_other, stabs->n_desc, stabs->n_value,
650 /* Symbols with type == 0 (N_UNDF) specify the length of the
651 string table associated with this file. We use that info
652 to know how to relocate the *next* file's string table indices. */
654 if (stabs->n_type == N_UNDF)
656 file_string_table_offset = next_file_string_table_offset;
657 next_file_string_table_offset += stabs->n_value;
660 /* Now, using the possibly updated string table offset, print the
661 string (if any) associated with this symbol. */
663 if ((stabs->n_strx + file_string_table_offset) < stabstr_hdr->sh_size)
664 printf (" %s", &strtab[stabs->n_strx + file_string_table_offset]);
670 #endif /* ELF_STAB_DISPLAY */
676 if (!bfd_check_format (abfd, bfd_object))
678 fprintf (stderr, "%s:%s: %s\n", program_name, abfd->filename,
679 bfd_errmsg (bfd_error));
682 printf ("\n%s: file format %s\n", abfd->filename, abfd->xvec->name);
684 print_arelt_descr (stdout, abfd, true);
686 if (dump_file_header)
690 printf ("architecture: %s, ",
691 bfd_printable_arch_mach (bfd_get_arch (abfd),
692 bfd_get_mach (abfd)));
693 printf ("flags 0x%08x:\n", abfd->flags);
695 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
696 PF (HAS_RELOC, "HAS_RELOC");
697 PF (EXEC_P, "EXEC_P");
698 PF (HAS_LINENO, "HAS_LINENO");
699 PF (HAS_DEBUG, "HAS_DEBUG");
700 PF (HAS_SYMS, "HAS_SYMS");
701 PF (HAS_LOCALS, "HAS_LOCALS");
702 PF (DYNAMIC, "DYNAMIC");
703 PF (WP_TEXT, "WP_TEXT");
704 PF (D_PAGED, "D_PAGED");
705 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
706 printf ("\nstart address 0x");
707 printf_vma (abfd->start_address);
711 if (dump_section_headers)
713 if (dump_symtab || dump_reloc_info || disassemble)
715 syms = slurp_symtab (abfd);
719 #ifdef ELF_STAB_DISPLAY
720 if (dump_stab_section_info)
721 dump_elf_stabs (abfd);
725 if (dump_section_contents)
727 /* Note that disassemble_data re-orders the syms table, but that is
728 safe - as long as it is done last! */
730 disassemble_data (abfd);
734 display_file (filename, target)
738 bfd *file, *arfile = (bfd *) NULL;
740 file = bfd_openr (filename, target);
743 fprintf (stderr, "%s: ", program_name);
744 bfd_perror (filename);
748 if (bfd_check_format (file, bfd_archive) == true)
750 printf ("In archive %s:\n", bfd_get_filename (file));
753 bfd_error = no_error;
755 arfile = bfd_openr_next_archived_file (file, arfile);
758 if (bfd_error != no_more_archived_files)
760 fprintf (stderr, "%s: ", program_name);
761 bfd_perror (bfd_get_filename (file));
766 display_bfd (arfile);
767 /* Don't close the archive elements; we need them for next_archive */
776 /* Actually display the various requested regions */
784 bfd_size_type datasize = 0;
787 for (section = abfd->sections; section != NULL; section =
792 if (only == (char *) NULL ||
793 strcmp (only, section->name) == 0)
795 if (section->flags & SEC_HAS_CONTENTS)
797 printf ("Contents of section %s:\n", section->name);
799 if (bfd_get_section_size_before_reloc (section) == 0)
801 data = (bfd_byte *) malloc (bfd_get_section_size_before_reloc (section));
802 if (data == (bfd_byte *) NULL)
804 fprintf (stderr, "%s: memory exhausted.\n", program_name);
807 datasize = bfd_get_section_size_before_reloc (section);
810 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_get_section_size_before_reloc (section));
812 for (i = 0; i < bfd_get_section_size_before_reloc (section); i += onaline)
816 printf (" %04lx ", (unsigned long int) (i + section->vma));
817 for (j = i; j < i + onaline; j++)
819 if (j < bfd_get_section_size_before_reloc (section))
820 printf ("%02x", (unsigned) (data[j]));
828 for (j = i; j < i + onaline; j++)
830 if (j >= bfd_get_section_size_before_reloc (section))
833 printf ("%c", isprint (data[j]) ? data[j] : '.');
843 /* Should perhaps share code and display with nm? */
850 asymbol **current = syms;
852 printf ("SYMBOL TABLE:\n");
854 for (count = 0; count < symcount; count++)
859 bfd *cur_bfd = bfd_asymbol_bfd(*current);
862 bfd_print_symbol (cur_bfd,
864 *current, bfd_print_symbol_all);
880 unsigned int relcount;
883 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
885 if (a == &bfd_abs_section)
887 if (a == &bfd_und_section)
889 if (bfd_is_com_section (a))
892 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
894 if (bfd_get_reloc_upper_bound (abfd, a) == 0)
896 printf (" (none)\n\n");
902 relpp = (arelent **) xmalloc (bfd_get_reloc_upper_bound (abfd, a));
903 /* Note that this must be done *before* we sort the syms table. */
904 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
907 printf (" (none)\n\n");
912 printf ("OFFSET TYPE VALUE \n");
914 for (p = relpp; relcount && *p != (arelent *) NULL; p++,
918 CONST char *sym_name;
920 /* CONST char *section_name = q->section == (asection *)NULL ? "*abs" :*/
921 /* q->section->name;*/
922 CONST char *section_name = (*(q->sym_ptr_ptr))->section->name;
924 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
926 sym_name = (*(q->sym_ptr_ptr))->name;
934 printf_vma (q->address);
941 printf_vma (q->address);
942 printf (" %-8s [%s]",
949 printf_vma (q->addend);
962 #define _DUMMY_NAME_ "/dev/null"
964 #define _DUMMY_NAME_ "##dummy"
967 DEFUN (display_info_table, (first, last),
968 int first AND int last)
971 extern bfd_target *target_vector[];
973 printf ("\n%12s", " ");
974 for (i = first; i++ < last && target_vector[i];)
975 printf ("%s ", target_vector[i]->name);
978 for (j = (int) bfd_arch_obscure + 1; (int) j < (int) bfd_arch_last; j++)
979 if (strcmp (bfd_printable_arch_mach (j, 0), "UNKNOWN!") != 0)
981 printf ("%11s ", bfd_printable_arch_mach (j, 0));
982 for (i = first; i++ < last && target_vector[i];)
984 bfd_target *p = target_vector[i];
985 bfd *abfd = bfd_openw (_DUMMY_NAME_, p->name);
986 int l = strlen (p->name);
988 bfd_set_format (abfd, bfd_object);
989 ok = bfd_set_arch_mach (abfd, j, 0);
992 printf ("%s ", p->name);
996 printf ("%c", ok ? '*' : '-');
1005 DEFUN_VOID (display_info)
1008 unsigned int i, j, columns;
1009 extern bfd_target *target_vector[];
1010 extern char *getenv ();
1012 printf ("BFD header file version %s\n", BFD_VERSION);
1013 for (i = 0; target_vector[i]; i++)
1015 bfd_target *p = target_vector[i];
1016 bfd *abfd = bfd_openw (_DUMMY_NAME_, p->name);
1017 bfd_set_format (abfd, bfd_object);
1018 printf ("%s\n (header %s, data %s)\n", p->name,
1019 p->header_byteorder_big_p ? "big endian" : "little endian",
1020 p->byteorder_big_p ? "big endian" : "little endian");
1021 for (j = (int) bfd_arch_obscure + 1; j < (int) bfd_arch_last; j++)
1022 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) j, 0))
1024 bfd_printable_arch_mach ((enum bfd_architecture) j, 0));
1027 if (colum = getenv ("COLUMNS"))
1028 columns = atoi (colum);
1031 for (i = 0; target_vector[i];)
1035 for (j = 12; target_vector[i] && j < columns; i++)
1036 j += strlen (target_vector[i]->name) + 1;
1040 display_info_table (old, i);
1044 /** main and like trivia */
1052 extern char *optarg;
1053 char *target = default_target;
1054 boolean seenflag = false;
1057 program_name = *argv;
1059 while ((c = getopt_long (argc, argv, "ib:m:Vdlfahrtxsj:", long_options,
1067 break; /* we've been given a long option */
1075 with_line_numbers = 1;
1081 dump_file_header = true;
1088 dump_reloc_info = 1;
1089 dump_file_header = true;
1091 dump_section_headers = 1;
1100 dump_section_contents = 1;
1103 dump_reloc_info = 1;
1109 dump_section_headers = 1;
1123 printf ("GNU %s version %s\n", program_name, program_version);
1127 if (seenflag == false)
1137 display_file ("a.out", target);
1139 for (; optind < argc;)
1140 display_file (argv[optind++], target);