1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 91, 92, 93, 94, 95, 1996 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "libiberty.h"
30 #ifdef ANSI_PROTOTYPES
36 /* Internal headers for the ELF .stab-dump code - sorry. */
37 #define BYTES_IN_WORD 32
38 #include "aout/aout64.h"
40 #ifdef NEED_DECLARATION_FPRINTF
41 /* This is needed by INIT_DISASSEMBLE_INFO. */
42 extern int fprintf PARAMS ((FILE *, const char *, ...));
45 static char *default_target = NULL; /* default at runtime */
47 static int show_version = 0; /* show the version number */
48 static int dump_section_contents; /* -s */
49 static int dump_section_headers; /* -h */
50 static boolean dump_file_header; /* -f */
51 static int dump_symtab; /* -t */
52 static int dump_dynamic_symtab; /* -T */
53 static int dump_reloc_info; /* -r */
54 static int dump_dynamic_reloc_info; /* -R */
55 static int dump_ar_hdrs; /* -a */
56 static int dump_private_headers; /* -p */
57 static int with_line_numbers; /* -l */
58 static boolean with_source_code; /* -S */
59 static int show_raw_insn; /* --show-raw-insn */
60 static int dump_stab_section_info; /* --stabs */
61 static boolean disassemble; /* -d */
62 static boolean disassemble_all; /* -D */
63 static boolean formats_info; /* -i */
64 static char *only; /* -j secname */
65 static int wide_output; /* -w */
66 static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
67 static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
68 static int dump_debugging; /* --debugging */
70 /* Extra info to pass to the disassembler address printing function. */
71 struct objdump_disasm_info {
77 /* Architecture to disassemble for, or default if NULL. */
78 static char *machine = (char *) NULL;
80 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
81 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
83 /* The symbol table. */
84 static asymbol **syms;
86 /* Number of symbols in `syms'. */
87 static long symcount = 0;
89 /* The sorted symbol table. */
90 static asymbol **sorted_syms;
92 /* Number of symbols in `sorted_syms'. */
93 static long sorted_symcount = 0;
95 /* The dynamic symbol table. */
96 static asymbol **dynsyms;
98 /* Number of symbols in `dynsyms'. */
99 static long dynsymcount = 0;
101 /* Forward declarations. */
104 display_file PARAMS ((char *filename, char *target));
107 dump_data PARAMS ((bfd *abfd));
110 dump_relocs PARAMS ((bfd *abfd));
113 dump_dynamic_relocs PARAMS ((bfd * abfd));
116 dump_reloc_set PARAMS ((bfd *, asection *, arelent **, long));
119 dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
122 display_bfd PARAMS ((bfd *abfd));
125 objdump_print_value PARAMS ((bfd_vma, struct disassemble_info *));
128 objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *));
131 show_line PARAMS ((bfd *, asection *, bfd_vma));
134 endian_string PARAMS ((enum bfd_endian));
137 usage (stream, status)
142 Usage: %s [-ahifdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
143 [--archive-headers] [--target=bfdname] [--debugging] [--disassemble]\n\
144 [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
145 [--info] [--section=section-name] [--line-numbers] [--source]\n",
148 [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
149 [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
150 [--wide] [--version] [--help] [--private-headers]\n\
151 [--start-address=addr] [--stop-address=addr]\n\
152 [--show-raw-insn] [-EB|-EL] [--endian={big|little}] objfile...\n\
153 at least one option besides -l (--line-numbers) must be given\n");
154 list_supported_targets (program_name, stream);
160 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
162 #define OPTION_ENDIAN (150)
163 #define OPTION_START_ADDRESS (OPTION_ENDIAN + 1)
164 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
166 static struct option long_options[]=
168 {"all-headers", no_argument, NULL, 'x'},
169 {"private-headers", no_argument, NULL, 'p'},
170 {"architecture", required_argument, NULL, 'm'},
171 {"archive-headers", no_argument, NULL, 'a'},
172 {"debugging", no_argument, &dump_debugging, 1},
173 {"disassemble", no_argument, NULL, 'd'},
174 {"disassemble-all", no_argument, NULL, 'D'},
175 {"dynamic-reloc", no_argument, NULL, 'R'},
176 {"dynamic-syms", no_argument, NULL, 'T'},
177 {"endian", required_argument, NULL, OPTION_ENDIAN},
178 {"file-headers", no_argument, NULL, 'f'},
179 {"full-contents", no_argument, NULL, 's'},
180 {"headers", no_argument, NULL, 'h'},
181 {"help", no_argument, NULL, 'H'},
182 {"info", no_argument, NULL, 'i'},
183 {"line-numbers", no_argument, NULL, 'l'},
184 {"reloc", no_argument, NULL, 'r'},
185 {"section", required_argument, NULL, 'j'},
186 {"section-headers", no_argument, NULL, 'h'},
187 {"show-raw-insn", no_argument, &show_raw_insn, 1},
188 {"source", no_argument, NULL, 'S'},
189 {"stabs", no_argument, &dump_stab_section_info, 1},
190 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
191 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
192 {"syms", no_argument, NULL, 't'},
193 {"target", required_argument, NULL, 'b'},
194 {"version", no_argument, &show_version, 1},
195 {"wide", no_argument, &wide_output, 'w'},
196 {0, no_argument, 0, 0}
200 dump_section_header (abfd, section, ignored)
207 printf ("%3d %-13s %08lx ", section->index,
208 bfd_get_section_name (abfd, section),
209 (unsigned long) bfd_section_size (abfd, section));
210 printf_vma (bfd_get_section_vma (abfd, section));
212 printf_vma (section->lma);
213 printf (" %08lx 2**%u", section->filepos,
214 bfd_get_section_alignment (abfd, section));
220 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
222 PF (SEC_HAS_CONTENTS, "CONTENTS");
223 PF (SEC_ALLOC, "ALLOC");
224 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
225 PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
226 PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA");
227 PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS");
228 PF (SEC_LOAD, "LOAD");
229 PF (SEC_RELOC, "RELOC");
231 PF (SEC_BALIGN, "BALIGN");
233 PF (SEC_READONLY, "READONLY");
234 PF (SEC_CODE, "CODE");
235 PF (SEC_DATA, "DATA");
237 PF (SEC_DEBUGGING, "DEBUGGING");
238 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
239 PF (SEC_EXCLUDE, "EXCLUDE");
240 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
242 if ((section->flags & SEC_LINK_ONCE) != 0)
246 switch (section->flags & SEC_LINK_DUPLICATES)
250 case SEC_LINK_DUPLICATES_DISCARD:
251 ls = "LINK_ONCE_DISCARD";
253 case SEC_LINK_DUPLICATES_ONE_ONLY:
254 ls = "LINK_ONCE_ONE_ONLY";
256 case SEC_LINK_DUPLICATES_SAME_SIZE:
257 ls = "LINK_ONCE_SAME_SIZE";
259 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
260 ls = "LINK_ONCE_SAME_CONTENTS";
263 printf ("%s%s", comma, ls);
275 printf ("Sections:\n");
277 printf ("Idx Name Size VMA LMA File off Algn\n");
279 printf ("Idx Name Size VMA LMA File off Algn\n");
281 bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
288 asymbol **sy = (asymbol **) NULL;
291 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
293 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
298 storage = bfd_get_symtab_upper_bound (abfd);
300 bfd_fatal (bfd_get_filename (abfd));
304 sy = (asymbol **) xmalloc (storage);
306 symcount = bfd_canonicalize_symtab (abfd, sy);
308 bfd_fatal (bfd_get_filename (abfd));
310 fprintf (stderr, "%s: %s: No symbols\n",
311 program_name, bfd_get_filename (abfd));
315 /* Read in the dynamic symbols. */
318 slurp_dynamic_symtab (abfd)
321 asymbol **sy = (asymbol **) NULL;
324 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
327 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
329 fprintf (stderr, "%s: %s: not a dynamic object\n",
330 program_name, bfd_get_filename (abfd));
335 bfd_fatal (bfd_get_filename (abfd));
340 sy = (asymbol **) xmalloc (storage);
342 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
344 bfd_fatal (bfd_get_filename (abfd));
345 if (dynsymcount == 0)
346 fprintf (stderr, "%s: %s: No dynamic symbols\n",
347 program_name, bfd_get_filename (abfd));
351 /* Filter out (in place) symbols that are useless for disassembly.
352 COUNT is the number of elements in SYMBOLS.
353 Return the number of useful symbols. */
356 remove_useless_symbols (symbols, count)
360 register asymbol **in_ptr = symbols, **out_ptr = symbols;
364 asymbol *sym = *in_ptr++;
366 if (sym->name == NULL || sym->name[0] == '\0')
368 if (sym->flags & (BSF_DEBUGGING))
370 if (bfd_is_und_section (sym->section)
371 || bfd_is_com_section (sym->section))
376 return out_ptr - symbols;
379 /* Sort symbols into value order. */
382 compare_symbols (ap, bp)
386 const asymbol *a = *(const asymbol **)ap;
387 const asymbol *b = *(const asymbol **)bp;
391 flagword aflags, bflags;
393 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
395 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
398 if (a->section > b->section)
400 else if (a->section < b->section)
403 an = bfd_asymbol_name (a);
404 bn = bfd_asymbol_name (b);
408 /* The symbols gnu_compiled and gcc2_compiled convey no real
409 information, so put them after other symbols with the same value. */
411 af = (strstr (an, "gnu_compiled") != NULL
412 || strstr (an, "gcc2_compiled") != NULL);
413 bf = (strstr (bn, "gnu_compiled") != NULL
414 || strstr (bn, "gcc2_compiled") != NULL);
421 /* We use a heuristic for the file name, to try to sort it after
422 more useful symbols. It may not work on non Unix systems, but it
423 doesn't really matter; the only difference is precisely which
424 symbol names get printed. */
426 #define file_symbol(s, sn, snl) \
427 (((s)->flags & BSF_FILE) != 0 \
428 || ((sn)[(snl) - 2] == '.' \
429 && ((sn)[(snl) - 1] == 'o' \
430 || (sn)[(snl) - 1] == 'a')))
432 af = file_symbol (a, an, anl);
433 bf = file_symbol (b, bn, bnl);
440 /* Try to sort global symbols before local symbols before debugging
446 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
448 if ((aflags & BSF_DEBUGGING) != 0)
453 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
455 if ((aflags & BSF_LOCAL) != 0)
460 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
462 if ((aflags & BSF_GLOBAL) != 0)
468 /* Symbols that start with '.' might be section names, so sort them
469 after symbols that don't start with '.'. */
470 if (an[0] == '.' && bn[0] != '.')
472 if (an[0] != '.' && bn[0] == '.')
475 /* Finally, if we can't distinguish them in any other way, try to
476 get consistent results by sorting the symbols by name. */
477 return strcmp (an, bn);
480 /* Sort relocs into address order. */
483 compare_relocs (ap, bp)
487 const arelent *a = *(const arelent **)ap;
488 const arelent *b = *(const arelent **)bp;
490 if (a->address > b->address)
492 else if (a->address < b->address)
495 /* So that associated relocations tied to the same address show up
496 in the correct order, we don't do any further sorting. */
505 /* Print VMA to STREAM with no leading zeroes. */
508 objdump_print_value (vma, info)
510 struct disassemble_info *info;
515 sprintf_vma (buf, vma);
516 for (p = buf; *p == '0'; ++p)
518 (*info->fprintf_func) (info->stream, "%s", p);
521 /* Print VMA symbolically to INFO if possible. */
524 objdump_print_address (vma, info)
526 struct disassemble_info *info;
530 /* @@ Would it speed things up to cache the last two symbols returned,
531 and maybe their address ranges? For many processors, only one memory
532 operand can be present at a time, so the 2-entry cache wouldn't be
533 constantly churned by code doing heavy memory accesses. */
535 /* Indices in `sorted_syms'. */
537 long max = sorted_symcount;
540 sprintf_vma (buf, vma);
541 (*info->fprintf_func) (info->stream, "%s", buf);
543 if (sorted_symcount < 1)
546 /* Perform a binary search looking for the closest symbol to the
547 required value. We are searching the range (min, max]. */
548 while (min + 1 < max)
552 thisplace = (max + min) / 2;
553 sym = sorted_syms[thisplace];
555 if (bfd_asymbol_value (sym) > vma)
557 else if (bfd_asymbol_value (sym) < vma)
566 /* The symbol we want is now in min, the low end of the range we
567 were searching. If there are several symbols with the same
568 value, we want the first one. */
571 && (bfd_asymbol_value (sorted_syms[thisplace])
572 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
576 /* If the file is relocateable, and the symbol could be from this
577 section, prefer a symbol from this section over symbols from
578 others, even if the other symbol's value might be closer.
580 Note that this may be wrong for some symbol references if the
581 sections have overlapping memory ranges, but in that case there's
582 no way to tell what's desired without looking at the relocation
584 struct objdump_disasm_info *aux;
587 aux = (struct objdump_disasm_info *) info->application_data;
588 if (sorted_syms[thisplace]->section != aux->sec
590 || ((aux->abfd->flags & HAS_RELOC) != 0
591 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
592 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
593 + bfd_section_size (aux->abfd, aux->sec)))))
595 for (i = thisplace + 1; i < sorted_symcount; i++)
597 if (bfd_asymbol_value (sorted_syms[i])
598 != bfd_asymbol_value (sorted_syms[thisplace]))
604 if (sorted_syms[i]->section == aux->sec
606 || sorted_syms[i - 1]->section != aux->sec
607 || (bfd_asymbol_value (sorted_syms[i])
608 != bfd_asymbol_value (sorted_syms[i - 1]))))
615 if (sorted_syms[thisplace]->section != aux->sec)
617 /* We didn't find a good symbol with a smaller value.
618 Look for one with a larger value. */
619 for (i = thisplace + 1; i < sorted_symcount; i++)
621 if (sorted_syms[i]->section == aux->sec)
629 if (sorted_syms[thisplace]->section != aux->sec
631 || ((aux->abfd->flags & HAS_RELOC) != 0
632 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
633 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
634 + bfd_section_size (aux->abfd, aux->sec)))))
638 (*info->fprintf_func) (info->stream, " <%s",
639 bfd_get_section_name (aux->abfd, aux->sec));
640 secaddr = bfd_get_section_vma (aux->abfd, aux->sec);
643 (*info->fprintf_func) (info->stream, "-");
644 objdump_print_value (secaddr - vma, info);
646 else if (vma > secaddr)
648 (*info->fprintf_func) (info->stream, "+");
649 objdump_print_value (vma - secaddr, info);
651 (*info->fprintf_func) (info->stream, ">");
657 (*info->fprintf_func) (info->stream, " <%s", sorted_syms[thisplace]->name);
658 if (bfd_asymbol_value (sorted_syms[thisplace]) > vma)
660 (*info->fprintf_func) (info->stream, "-");
661 objdump_print_value (bfd_asymbol_value (sorted_syms[thisplace]) - vma,
664 else if (vma > bfd_asymbol_value (sorted_syms[thisplace]))
666 (*info->fprintf_func) (info->stream, "+");
667 objdump_print_value (vma - bfd_asymbol_value (sorted_syms[thisplace]),
670 (*info->fprintf_func) (info->stream, ">");
673 /* Hold the last function name and the last line number we displayed
676 static char *prev_functionname;
677 static unsigned int prev_line;
679 /* We keep a list of all files that we have seen when doing a
680 dissassembly with source, so that we know how much of the file to
681 display. This can be important for inlined functions. */
683 struct print_file_list
685 struct print_file_list *next;
691 static struct print_file_list *print_files;
693 /* The number of preceding context lines to show when we start
694 displaying a file for the first time. */
696 #define SHOW_PRECEDING_CONTEXT_LINES (5)
698 /* Skip ahead to a given line in a file, optionally printing each
702 skip_to_line PARAMS ((struct print_file_list *, unsigned int, boolean));
705 skip_to_line (p, line, show)
706 struct print_file_list *p;
710 while (p->line < line)
714 if (fgets (buf, sizeof buf, p->f) == NULL)
724 if (strchr (buf, '\n') != NULL)
729 /* Show the line number, or the source line, in a dissassembly
733 show_line (abfd, section, off)
738 CONST char *filename;
739 CONST char *functionname;
742 if (! with_line_numbers && ! with_source_code)
745 if (! bfd_find_nearest_line (abfd, section, syms, off, &filename,
746 &functionname, &line))
749 if (filename != NULL && *filename == '\0')
751 if (functionname != NULL && *functionname == '\0')
754 if (with_line_numbers)
756 if (functionname != NULL
757 && (prev_functionname == NULL
758 || strcmp (functionname, prev_functionname) != 0))
759 printf ("%s():\n", functionname);
760 if (line > 0 && line != prev_line)
761 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
768 struct print_file_list **pp, *p;
770 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
771 if (strcmp ((*pp)->filename, filename) == 0)
777 if (p != print_files)
781 /* We have reencountered a file name which we saw
782 earlier. This implies that either we are dumping out
783 code from an included file, or the same file was
784 linked in more than once. There are two common cases
785 of an included file: inline functions in a header
786 file, and a bison or flex skeleton file. In the
787 former case we want to just start printing (but we
788 back up a few lines to give context); in the latter
789 case we want to continue from where we left off. I
790 can't think of a good way to distinguish the cases,
791 so I used a heuristic based on the file name. */
792 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
796 l = line - SHOW_PRECEDING_CONTEXT_LINES;
803 p->f = fopen (p->filename, "r");
807 skip_to_line (p, l, false);
809 if (print_files->f != NULL)
811 fclose (print_files->f);
812 print_files->f = NULL;
818 skip_to_line (p, line, true);
820 p->next = print_files;
828 f = fopen (filename, "r");
833 p = ((struct print_file_list *)
834 xmalloc (sizeof (struct print_file_list)));
835 p->filename = xmalloc (strlen (filename) + 1);
836 strcpy (p->filename, filename);
840 if (print_files != NULL && print_files->f != NULL)
842 fclose (print_files->f);
843 print_files->f = NULL;
845 p->next = print_files;
848 l = line - SHOW_PRECEDING_CONTEXT_LINES;
851 skip_to_line (p, l, false);
853 skip_to_line (p, line, true);
858 if (functionname != NULL
859 && (prev_functionname == NULL
860 || strcmp (functionname, prev_functionname) != 0))
862 if (prev_functionname != NULL)
863 free (prev_functionname);
864 prev_functionname = xmalloc (strlen (functionname) + 1);
865 strcpy (prev_functionname, functionname);
868 if (line > 0 && line != prev_line)
872 /* Pseudo FILE object for strings. */
878 /* sprintf to a "stream" */
880 #ifdef ANSI_PROTOTYPES
882 objdump_sprintf (SFILE *f, const char *format, ...)
887 va_start (args, format);
888 vsprintf (f->current, format, args);
889 f->current += n = strlen (f->current);
895 objdump_sprintf (va_alist)
904 f = va_arg (args, SFILE *);
905 format = va_arg (args, const char *);
906 vsprintf (f->current, format, args);
907 f->current += n = strlen (f->current);
914 disassemble_data (abfd)
918 disassembler_ftype disassemble_fn = 0; /* New style */
919 struct disassemble_info disasm_info;
920 struct objdump_disasm_info aux;
922 boolean done_dot = false;
927 prev_functionname = NULL;
930 /* We make a copy of syms to sort. We don't want to sort syms
931 because that will screw up the relocs. */
932 sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
933 memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
935 sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
937 /* Sort the symbols into section and symbol order */
938 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
940 INIT_DISASSEMBLE_INFO(disasm_info, stdout, fprintf);
941 disasm_info.application_data = (PTR) &aux;
943 disasm_info.print_address_func = objdump_print_address;
945 if (machine != (char *) NULL)
947 const bfd_arch_info_type *info = bfd_scan_arch (machine);
950 fprintf (stderr, "%s: Can't use supplied machine %s\n",
955 abfd->arch_info = info;
958 if (endian != BFD_ENDIAN_UNKNOWN)
960 struct bfd_target *xvec;
962 xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
963 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
964 xvec->byteorder = endian;
968 disassemble_fn = disassembler (abfd);
971 fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
973 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
977 disasm_info.flavour = bfd_get_flavour (abfd);
978 disasm_info.arch = bfd_get_arch (abfd);
979 disasm_info.mach = bfd_get_mach (abfd);
980 if (bfd_big_endian (abfd))
981 disasm_info.endian = BFD_ENDIAN_BIG;
982 else if (bfd_little_endian (abfd))
983 disasm_info.endian = BFD_ENDIAN_LITTLE;
985 /* ??? Aborting here seems too drastic. We could default to big or little
987 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
989 for (section = abfd->sections;
990 section != (asection *) NULL;
991 section = section->next)
993 bfd_byte *data = NULL;
994 bfd_size_type datasize = 0;
995 arelent **relbuf = NULL;
996 arelent **relpp = NULL;
997 arelent **relppend = NULL;
1000 if ((section->flags & SEC_LOAD) == 0
1001 || (! disassemble_all
1003 && (section->flags & SEC_CODE) == 0))
1005 if (only != (char *) NULL && strcmp (only, section->name) != 0)
1009 && (section->flags & SEC_RELOC) != 0)
1013 relsize = bfd_get_reloc_upper_bound (abfd, section);
1015 bfd_fatal (bfd_get_filename (abfd));
1021 relbuf = (arelent **) xmalloc (relsize);
1022 relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
1024 bfd_fatal (bfd_get_filename (abfd));
1026 /* Sort the relocs by address. */
1027 qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
1030 relppend = relpp + relcount;
1034 printf ("Disassembly of section %s:\n", section->name);
1036 datasize = bfd_get_section_size_before_reloc (section);
1040 data = (bfd_byte *) xmalloc ((size_t) datasize);
1042 bfd_get_section_contents (abfd, section, data, 0, datasize);
1045 disasm_info.buffer = data;
1046 disasm_info.buffer_vma = section->vma;
1047 disasm_info.buffer_length = datasize;
1048 if (start_address == (bfd_vma) -1
1049 || start_address < disasm_info.buffer_vma)
1052 i = start_address - disasm_info.buffer_vma;
1053 if (stop_address == (bfd_vma) -1)
1057 if (stop_address < disasm_info.buffer_vma)
1060 stop = stop_address - disasm_info.buffer_vma;
1061 if (stop > disasm_info.buffer_length)
1062 stop = disasm_info.buffer_length;
1067 boolean need_nl = false;
1071 || (data[i + 1] == 0
1073 || (data[i + 2] == 0
1075 || data[i + 3] == 0))))))
1077 if (done_dot == false)
1087 if (with_line_numbers || with_source_code)
1088 show_line (abfd, section, i);
1089 aux.require_sec = true;
1090 objdump_print_address (section->vma + i, &disasm_info);
1091 aux.require_sec = false;
1094 sfile.buffer = sfile.current = buf;
1095 disasm_info.fprintf_func = (fprintf_ftype) objdump_sprintf;
1096 disasm_info.stream = (FILE *) &sfile;
1097 bytes = (*disassemble_fn) (section->vma + i, &disasm_info);
1098 disasm_info.fprintf_func = (fprintf_ftype) fprintf;
1099 disasm_info.stream = stdout;
1106 for (j = i; j < i + bytes; ++j)
1108 printf ("%02x", (unsigned) data[j]);
1111 /* Separate raw data from instruction by extra space. */
1115 printf ("%s", sfile.buffer);
1124 && (section->flags & SEC_RELOC) != 0)
1126 while (relpp < relppend
1127 && ((*relpp)->address >= (bfd_vma) i
1128 && (*relpp)->address < (bfd_vma) i + bytes))
1131 const char *sym_name;
1135 printf ("\t\tRELOC: ");
1137 printf_vma (section->vma + q->address);
1139 printf (" %s ", q->howto->name);
1141 if (q->sym_ptr_ptr != NULL
1142 && *q->sym_ptr_ptr != NULL)
1144 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1145 if (sym_name == NULL || *sym_name == '\0')
1149 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1150 sym_name = bfd_get_section_name (abfd, sym_sec);
1151 if (sym_name == NULL || *sym_name == '\0')
1152 sym_name = "*unknown*";
1156 sym_name = "*unknown*";
1158 printf ("%s", sym_name);
1163 printf_vma (q->addend);
1186 /* Define a table of stab values and print-strings. We wish the initializer
1187 could be a direct-mapped table, but instead we build one the first
1190 static void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
1191 char *strsect_name));
1193 /* Dump the stabs sections from an object file that has a section that
1194 uses Sun stabs encoding. */
1200 dump_section_stabs (abfd, ".stab", ".stabstr");
1201 dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
1202 dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
1203 dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1206 static bfd_byte *stabs;
1207 static bfd_size_type stab_size;
1209 static char *strtab;
1210 static bfd_size_type stabstr_size;
1212 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1213 and string table section STRSECT_NAME into `strtab'.
1214 If the section exists and was read, allocate the space and return true.
1215 Otherwise return false. */
1218 read_section_stabs (abfd, stabsect_name, strsect_name)
1220 char *stabsect_name;
1223 asection *stabsect, *stabstrsect;
1225 stabsect = bfd_get_section_by_name (abfd, stabsect_name);
1228 printf ("No %s section present\n\n", stabsect_name);
1232 stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
1233 if (0 == stabstrsect)
1235 fprintf (stderr, "%s: %s has no %s section\n", program_name,
1236 bfd_get_filename (abfd), strsect_name);
1240 stab_size = bfd_section_size (abfd, stabsect);
1241 stabstr_size = bfd_section_size (abfd, stabstrsect);
1243 stabs = (bfd_byte *) xmalloc (stab_size);
1244 strtab = (char *) xmalloc (stabstr_size);
1246 if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
1248 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1249 program_name, stabsect_name, bfd_get_filename (abfd),
1250 bfd_errmsg (bfd_get_error ()));
1256 if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
1259 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1260 program_name, strsect_name, bfd_get_filename (abfd),
1261 bfd_errmsg (bfd_get_error ()));
1270 /* Stabs entries use a 12 byte format:
1271 4 byte string table index
1273 1 byte stab other field
1274 2 byte stab desc field
1276 FIXME: This will have to change for a 64 bit object format. */
1278 #define STRDXOFF (0)
1280 #define OTHEROFF (5)
1283 #define STABSIZE (12)
1285 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1286 using string table section STRSECT_NAME (in `strtab'). */
1289 print_section_stabs (abfd, stabsect_name, strsect_name)
1291 char *stabsect_name;
1295 unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
1296 bfd_byte *stabp, *stabs_end;
1299 stabs_end = stabp + stab_size;
1301 printf ("Contents of %s section:\n\n", stabsect_name);
1302 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1304 /* Loop through all symbols and print them.
1306 We start the index at -1 because there is a dummy symbol on
1307 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1309 for (i = -1; stabp < stabs_end; stabp += STABSIZE, i++)
1313 unsigned char type, other;
1314 unsigned short desc;
1317 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
1318 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
1319 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
1320 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
1321 value = bfd_h_get_32 (abfd, stabp + VALOFF);
1323 printf ("\n%-6d ", i);
1324 /* Either print the stab name, or, if unnamed, print its number
1325 again (makes consistent formatting for tools like awk). */
1326 name = bfd_get_stab_name (type);
1328 printf ("%-6s", name);
1329 else if (type == N_UNDF)
1332 printf ("%-6d", type);
1333 printf (" %-6d %-6d ", other, desc);
1335 printf (" %-6lu", strx);
1337 /* Symbols with type == 0 (N_UNDF) specify the length of the
1338 string table associated with this file. We use that info
1339 to know how to relocate the *next* file's string table indices. */
1343 file_string_table_offset = next_file_string_table_offset;
1344 next_file_string_table_offset += value;
1348 /* Using the (possibly updated) string table offset, print the
1349 string (if any) associated with this symbol. */
1351 if ((strx + file_string_table_offset) < stabstr_size)
1352 printf (" %s", &strtab[strx + file_string_table_offset]);
1361 dump_section_stabs (abfd, stabsect_name, strsect_name)
1363 char *stabsect_name;
1368 /* Check for section names for which stabsect_name is a prefix, to
1369 handle .stab0, etc. */
1370 for (s = abfd->sections;
1376 len = strlen (stabsect_name);
1378 /* If the prefix matches, and the files section name ends with a nul or a digit,
1379 then we match. Ie: we want either an exact match or a a section followed by
1381 if (strncmp (stabsect_name, s->name, len) == 0
1382 && (s->name[len] == '\000' || isdigit (s->name[len])))
1384 if (read_section_stabs (abfd, s->name, strsect_name))
1386 print_section_stabs (abfd, s->name, strsect_name);
1395 dump_bfd_header (abfd)
1400 printf ("architecture: %s, ",
1401 bfd_printable_arch_mach (bfd_get_arch (abfd),
1402 bfd_get_mach (abfd)));
1403 printf ("flags 0x%08x:\n", abfd->flags);
1405 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1406 PF (HAS_RELOC, "HAS_RELOC");
1407 PF (EXEC_P, "EXEC_P");
1408 PF (HAS_LINENO, "HAS_LINENO");
1409 PF (HAS_DEBUG, "HAS_DEBUG");
1410 PF (HAS_SYMS, "HAS_SYMS");
1411 PF (HAS_LOCALS, "HAS_LOCALS");
1412 PF (DYNAMIC, "DYNAMIC");
1413 PF (WP_TEXT, "WP_TEXT");
1414 PF (D_PAGED, "D_PAGED");
1415 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
1416 printf ("\nstart address 0x");
1417 printf_vma (abfd->start_address);
1422 dump_bfd_private_header (abfd)
1425 bfd_print_private_bfd_data (abfd, stdout);
1434 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
1436 bfd_nonfatal (bfd_get_filename (abfd));
1437 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1439 list_matching_formats (matching);
1445 printf ("\n%s: file format %s\n", bfd_get_filename (abfd),
1448 print_arelt_descr (stdout, abfd, true);
1449 if (dump_file_header)
1450 dump_bfd_header (abfd);
1451 if (dump_private_headers)
1452 dump_bfd_private_header (abfd);
1454 if (dump_section_headers)
1455 dump_headers (abfd);
1456 if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
1458 syms = slurp_symtab (abfd);
1460 if (dump_dynamic_symtab || dump_dynamic_reloc_info)
1462 dynsyms = slurp_dynamic_symtab (abfd);
1465 dump_symbols (abfd, false);
1466 if (dump_dynamic_symtab)
1467 dump_symbols (abfd, true);
1468 if (dump_stab_section_info)
1470 if (dump_reloc_info && ! disassemble)
1472 if (dump_dynamic_reloc_info)
1473 dump_dynamic_relocs (abfd);
1474 if (dump_section_contents)
1477 disassemble_data (abfd);
1482 dhandle = read_debugging_info (abfd, syms, symcount);
1483 if (dhandle != NULL)
1485 if (! print_debugging_info (stdout, dhandle))
1486 fprintf (stderr, "%s: printing debugging information failed\n",
1487 bfd_get_filename (abfd));
1503 display_file (filename, target)
1507 bfd *file, *arfile = (bfd *) NULL;
1509 file = bfd_openr (filename, target);
1512 bfd_nonfatal (filename);
1516 if (bfd_check_format (file, bfd_archive) == true)
1518 bfd *last_arfile = NULL;
1520 printf ("In archive %s:\n", bfd_get_filename (file));
1523 bfd_set_error (bfd_error_no_error);
1525 arfile = bfd_openr_next_archived_file (file, arfile);
1528 if (bfd_get_error () != bfd_error_no_more_archived_files)
1530 bfd_nonfatal (bfd_get_filename (file));
1535 display_bfd (arfile);
1537 if (last_arfile != NULL)
1538 bfd_close (last_arfile);
1539 last_arfile = arfile;
1542 if (last_arfile != NULL)
1543 bfd_close (last_arfile);
1551 /* Actually display the various requested regions */
1559 bfd_size_type datasize = 0;
1561 bfd_size_type start, stop;
1563 for (section = abfd->sections; section != NULL; section =
1568 if (only == (char *) NULL ||
1569 strcmp (only, section->name) == 0)
1571 if (section->flags & SEC_HAS_CONTENTS)
1573 printf ("Contents of section %s:\n", section->name);
1575 if (bfd_section_size (abfd, section) == 0)
1577 data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
1578 datasize = bfd_section_size (abfd, section);
1581 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
1583 if (start_address == (bfd_vma) -1
1584 || start_address < section->vma)
1587 start = start_address - section->vma;
1588 if (stop_address == (bfd_vma) -1)
1589 stop = bfd_section_size (abfd, section);
1592 if (stop_address < section->vma)
1595 stop = stop_address - section->vma;
1596 if (stop > bfd_section_size (abfd, section))
1597 stop = bfd_section_size (abfd, section);
1599 for (i = start; i < stop; i += onaline)
1603 printf (" %04lx ", (unsigned long int) (i + section->vma));
1604 for (j = i; j < i + onaline; j++)
1607 printf ("%02x", (unsigned) (data[j]));
1615 for (j = i; j < i + onaline; j++)
1620 printf ("%c", isprint (data[j]) ? data[j] : '.');
1630 /* Should perhaps share code and display with nm? */
1632 dump_symbols (abfd, dynamic)
1646 printf ("DYNAMIC SYMBOL TABLE:\n");
1654 printf ("SYMBOL TABLE:\n");
1657 for (count = 0; count < max; count++)
1661 bfd *cur_bfd = bfd_asymbol_bfd(*current);
1664 bfd_print_symbol (cur_bfd,
1666 *current, bfd_print_symbol_all);
1684 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
1688 if (bfd_is_abs_section (a))
1690 if (bfd_is_und_section (a))
1692 if (bfd_is_com_section (a))
1697 if (strcmp (only, a->name))
1700 else if ((a->flags & SEC_RELOC) == 0)
1703 relsize = bfd_get_reloc_upper_bound (abfd, a);
1705 bfd_fatal (bfd_get_filename (abfd));
1707 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
1711 printf (" (none)\n\n");
1715 relpp = (arelent **) xmalloc (relsize);
1716 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
1718 bfd_fatal (bfd_get_filename (abfd));
1719 else if (relcount == 0)
1721 printf (" (none)\n\n");
1726 dump_reloc_set (abfd, a, relpp, relcount);
1735 dump_dynamic_relocs (abfd)
1742 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
1744 bfd_fatal (bfd_get_filename (abfd));
1746 printf ("DYNAMIC RELOCATION RECORDS");
1750 printf (" (none)\n\n");
1754 relpp = (arelent **) xmalloc (relsize);
1755 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
1757 bfd_fatal (bfd_get_filename (abfd));
1758 else if (relcount == 0)
1760 printf (" (none)\n\n");
1765 dump_reloc_set (abfd, (asection *) NULL, relpp, relcount);
1773 dump_reloc_set (abfd, sec, relpp, relcount)
1780 char *last_filename, *last_functionname;
1781 unsigned int last_line;
1783 /* Get column headers lined up reasonably. */
1789 sprintf_vma (buf, (bfd_vma) -1);
1790 width = strlen (buf) - 7;
1792 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
1795 last_filename = NULL;
1796 last_functionname = NULL;
1799 for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
1802 const char *filename, *functionname;
1804 const char *sym_name;
1805 const char *section_name;
1807 if (start_address != (bfd_vma) -1
1808 && q->address < start_address)
1810 if (stop_address != (bfd_vma) -1
1811 && q->address > stop_address)
1814 if (with_line_numbers
1816 && bfd_find_nearest_line (abfd, sec, syms, q->address,
1817 &filename, &functionname, &line))
1819 if (functionname != NULL
1820 && (last_functionname == NULL
1821 || strcmp (functionname, last_functionname) != 0))
1823 printf ("%s():\n", functionname);
1824 if (last_functionname != NULL)
1825 free (last_functionname);
1826 last_functionname = xstrdup (functionname);
1829 && (line != last_line
1830 || (filename != NULL
1831 && last_filename != NULL
1832 && strcmp (filename, last_filename) != 0)))
1834 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
1836 if (last_filename != NULL)
1837 free (last_filename);
1838 if (filename == NULL)
1839 last_filename = NULL;
1841 last_filename = xstrdup (filename);
1845 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
1847 sym_name = (*(q->sym_ptr_ptr))->name;
1848 section_name = (*(q->sym_ptr_ptr))->section->name;
1853 section_name = NULL;
1857 printf_vma (q->address);
1858 printf (" %-16s %s",
1864 if (section_name == (CONST char *) NULL)
1865 section_name = "*unknown*";
1866 printf_vma (q->address);
1867 printf (" %-16s [%s]",
1874 printf_vma (q->addend);
1880 /* The length of the longest architecture name + 1. */
1881 #define LONGEST_ARCH sizeof("rs6000:6000")
1884 endian_string (endian)
1885 enum bfd_endian endian;
1887 if (endian == BFD_ENDIAN_BIG)
1888 return "big endian";
1889 else if (endian == BFD_ENDIAN_LITTLE)
1890 return "little endian";
1892 return "endianness unknown";
1895 /* List the targets that BFD is configured to support, each followed
1896 by its endianness and the architectures it supports. */
1899 display_target_list ()
1901 extern bfd_target *bfd_target_vector[];
1905 dummy_name = choose_temp_base ();
1906 for (t = 0; bfd_target_vector[t]; t++)
1908 bfd_target *p = bfd_target_vector[t];
1909 bfd *abfd = bfd_openw (dummy_name, p->name);
1912 printf ("%s\n (header %s, data %s)\n", p->name,
1913 endian_string (p->header_byteorder),
1914 endian_string (p->byteorder));
1918 bfd_nonfatal (dummy_name);
1922 if (! bfd_set_format (abfd, bfd_object))
1924 if (bfd_get_error () != bfd_error_invalid_operation)
1925 bfd_nonfatal (p->name);
1929 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1930 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
1932 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
1934 unlink (dummy_name);
1938 /* Print a table showing which architectures are supported for entries
1939 FIRST through LAST-1 of bfd_target_vector (targets across,
1940 architectures down). */
1943 display_info_table (first, last)
1947 extern bfd_target *bfd_target_vector[];
1951 /* Print heading of target names. */
1952 printf ("\n%*s", (int) LONGEST_ARCH, " ");
1953 for (t = first; t < last && bfd_target_vector[t]; t++)
1954 printf ("%s ", bfd_target_vector[t]->name);
1957 dummy_name = choose_temp_base ();
1958 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1959 if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
1961 printf ("%*s ", (int) LONGEST_ARCH - 1,
1962 bfd_printable_arch_mach (a, 0));
1963 for (t = first; t < last && bfd_target_vector[t]; t++)
1965 bfd_target *p = bfd_target_vector[t];
1967 bfd *abfd = bfd_openw (dummy_name, p->name);
1971 bfd_nonfatal (p->name);
1977 if (! bfd_set_format (abfd, bfd_object))
1979 if (bfd_get_error () != bfd_error_invalid_operation)
1980 bfd_nonfatal (p->name);
1987 if (! bfd_set_arch_mach (abfd, a, 0))
1992 printf ("%s ", p->name);
1995 int l = strlen (p->name);
2003 unlink (dummy_name);
2007 /* Print tables of all the target-architecture combinations that
2008 BFD has been configured to support. */
2011 display_target_tables ()
2014 extern bfd_target *bfd_target_vector[];
2016 extern char *getenv ();
2019 colum = getenv ("COLUMNS");
2021 columns = atoi (colum);
2026 while (bfd_target_vector[t] != NULL)
2030 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
2032 while (wid < columns && bfd_target_vector[t] != NULL)
2036 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
2037 if (newwid >= columns)
2042 display_info_table (oldt, t);
2049 printf ("BFD header file version %s\n", BFD_VERSION);
2050 display_target_list ();
2051 display_target_tables ();
2060 char *target = default_target;
2061 boolean seenflag = false;
2063 program_name = *argv;
2064 xmalloc_set_program_name (program_name);
2066 START_PROGRESS (program_name, 0);
2070 while ((c = getopt_long (argc, argv, "pib:m:VdDlfahrRtTxsSj:wE:",
2071 long_options, (int *) 0))
2074 if (c != 'l' && c != OPTION_START_ADDRESS && c != OPTION_STOP_ADDRESS)
2079 break; /* we've been given a long option */
2087 with_line_numbers = 1;
2093 dump_file_header = true;
2096 formats_info = true;
2099 dump_private_headers = 1;
2102 dump_private_headers = 1;
2104 dump_reloc_info = 1;
2105 dump_file_header = true;
2107 dump_section_headers = 1;
2113 dump_dynamic_symtab = 1;
2119 disassemble = disassemble_all = true;
2123 with_source_code = true;
2126 dump_section_contents = 1;
2129 dump_reloc_info = 1;
2132 dump_dynamic_reloc_info = 1;
2138 dump_section_headers = 1;
2148 case OPTION_START_ADDRESS:
2149 start_address = parse_vma (optarg, "--start-address");
2151 case OPTION_STOP_ADDRESS:
2152 stop_address = parse_vma (optarg, "--stop-address");
2155 if (strcmp (optarg, "B") == 0)
2156 endian = BFD_ENDIAN_BIG;
2157 else if (strcmp (optarg, "L") == 0)
2158 endian = BFD_ENDIAN_LITTLE;
2161 fprintf (stderr, "%s: unrecognized -E option\n", program_name);
2166 if (strncmp (optarg, "big", strlen (optarg)) == 0)
2167 endian = BFD_ENDIAN_BIG;
2168 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
2169 endian = BFD_ENDIAN_LITTLE;
2172 fprintf (stderr, "%s: unrecognized --endian type `%s'\n",
2173 program_name, optarg);
2183 print_version ("objdump");
2185 if (seenflag == false)
2195 display_file ("a.out", target);
2197 for (; optind < argc;)
2198 display_file (argv[optind++], target);
2201 END_PROGRESS (program_name);