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 char *default_target = NULL; /* default at runtime */
47 extern char *program_version;
49 int show_version = 0; /* show the version number */
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_dynamic_symtab; /* -T */
55 int dump_reloc_info; /* -r */
56 int dump_dynamic_reloc_info; /* -R */
57 int dump_ar_hdrs; /* -a */
58 int dump_private_headers; /* -p */
59 int with_line_numbers; /* -l */
60 boolean with_source_code; /* -S */
61 int show_raw_insn; /* --show-raw-insn */
62 int dump_stab_section_info; /* --stabs */
63 boolean disassemble; /* -d */
64 boolean disassemble_all; /* -D */
65 boolean formats_info; /* -i */
66 char *only; /* -j secname */
67 int wide_output; /* -w */
68 bfd_vma start_address = (bfd_vma) -1; /* --start-address */
69 bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
70 int dump_debugging; /* --debugging */
72 /* Extra info to pass to the disassembler address printing function. */
73 struct objdump_disasm_info {
79 /* Architecture to disassemble for, or default if NULL. */
80 char *machine = (char *) NULL;
82 /* The symbol table. */
85 /* Number of symbols in `syms'. */
88 /* The sorted symbol table. */
89 asymbol **sorted_syms;
91 /* Number of symbols in `sorted_syms'. */
92 long sorted_symcount = 0;
94 /* The dynamic symbol table. */
97 /* Number of symbols in `dynsyms'. */
100 /* Forward declarations. */
103 display_file PARAMS ((char *filename, char *target));
106 dump_data PARAMS ((bfd *abfd));
109 dump_relocs PARAMS ((bfd *abfd));
112 dump_dynamic_relocs PARAMS ((bfd * abfd));
115 dump_reloc_set PARAMS ((bfd *, arelent **, long));
118 dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
121 display_bfd PARAMS ((bfd *abfd));
124 objdump_print_value PARAMS ((bfd_vma, struct disassemble_info *));
127 objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *));
130 show_line PARAMS ((bfd *, asection *, bfd_vma));
133 endian_string PARAMS ((enum bfd_endian));
136 usage (stream, status)
141 Usage: %s [-ahifdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
142 [--archive-headers] [--target=bfdname] [--debugging] [--disassemble]\n\
143 [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
144 [--info] [--section=section-name] [--line-numbers] [--source]\n",
147 [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
148 [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
149 [--wide] [--version] [--help] [--private-headers]\n\
150 [--start-address=addr] [--stop-address=addr]\n\
151 [--show-raw-insn] objfile...\n\
152 at least one option besides -l (--line-numbers) must be given\n");
153 list_supported_targets (program_name, stream);
157 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
159 #define OPTION_START_ADDRESS (150)
160 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
162 static struct option long_options[]=
164 {"all-headers", no_argument, NULL, 'x'},
165 {"private-headers", no_argument, NULL, 'p'},
166 {"architecture", required_argument, NULL, 'm'},
167 {"archive-headers", no_argument, NULL, 'a'},
168 {"debugging", no_argument, &dump_debugging, 1},
169 {"disassemble", no_argument, NULL, 'd'},
170 {"disassemble-all", no_argument, NULL, 'D'},
171 {"dynamic-reloc", no_argument, NULL, 'R'},
172 {"dynamic-syms", no_argument, NULL, 'T'},
173 {"file-headers", no_argument, NULL, 'f'},
174 {"full-contents", no_argument, NULL, 's'},
175 {"headers", no_argument, NULL, 'h'},
176 {"help", no_argument, NULL, 'H'},
177 {"info", no_argument, NULL, 'i'},
178 {"line-numbers", no_argument, NULL, 'l'},
179 {"reloc", no_argument, NULL, 'r'},
180 {"section", required_argument, NULL, 'j'},
181 {"section-headers", no_argument, NULL, 'h'},
182 {"show-raw-insn", no_argument, &show_raw_insn, 1},
183 {"source", no_argument, NULL, 'S'},
184 {"stabs", no_argument, &dump_stab_section_info, 1},
185 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
186 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
187 {"syms", no_argument, NULL, 't'},
188 {"target", required_argument, NULL, 'b'},
189 {"version", no_argument, &show_version, 1},
190 {"wide", no_argument, &wide_output, 'w'},
191 {0, no_argument, 0, 0}
195 dump_section_header (abfd, section, ignored)
202 printf ("%3d %-13s %08lx ", section->index,
203 bfd_get_section_name (abfd, section),
204 (unsigned long) bfd_section_size (abfd, section));
205 printf_vma (bfd_get_section_vma (abfd, section));
207 printf_vma (section->lma);
208 printf (" %08lx 2**%u", section->filepos,
209 bfd_get_section_alignment (abfd, section));
215 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
217 PF (SEC_HAS_CONTENTS, "CONTENTS");
218 PF (SEC_ALLOC, "ALLOC");
219 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
220 PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
221 PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA");
222 PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS");
223 PF (SEC_LOAD, "LOAD");
224 PF (SEC_RELOC, "RELOC");
226 PF (SEC_BALIGN, "BALIGN");
228 PF (SEC_READONLY, "READONLY");
229 PF (SEC_CODE, "CODE");
230 PF (SEC_DATA, "DATA");
232 PF (SEC_DEBUGGING, "DEBUGGING");
233 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
234 PF (SEC_EXCLUDE, "EXCLUDE");
235 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
237 if ((section->flags & SEC_LINK_ONCE) != 0)
241 switch (section->flags & SEC_LINK_DUPLICATES)
245 case SEC_LINK_DUPLICATES_DISCARD:
246 ls = "LINK_ONCE_DISCARD";
248 case SEC_LINK_DUPLICATES_ONE_ONLY:
249 ls = "LINK_ONCE_ONE_ONLY";
251 case SEC_LINK_DUPLICATES_SAME_SIZE:
252 ls = "LINK_ONCE_SAME_SIZE";
254 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
255 ls = "LINK_ONCE_SAME_CONTENTS";
258 printf ("%s%s", comma, ls);
270 printf ("Sections:\n");
272 printf ("Idx Name Size VMA LMA File off Algn\n");
274 printf ("Idx Name Size VMA LMA File off Algn\n");
276 bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
283 asymbol **sy = (asymbol **) NULL;
286 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
288 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
293 storage = bfd_get_symtab_upper_bound (abfd);
295 bfd_fatal (bfd_get_filename (abfd));
299 sy = (asymbol **) xmalloc (storage);
301 symcount = bfd_canonicalize_symtab (abfd, sy);
303 bfd_fatal (bfd_get_filename (abfd));
305 fprintf (stderr, "%s: %s: No symbols\n",
306 program_name, bfd_get_filename (abfd));
310 /* Read in the dynamic symbols. */
313 slurp_dynamic_symtab (abfd)
316 asymbol **sy = (asymbol **) NULL;
319 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
322 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
324 fprintf (stderr, "%s: %s: not a dynamic object\n",
325 program_name, bfd_get_filename (abfd));
330 bfd_fatal (bfd_get_filename (abfd));
335 sy = (asymbol **) xmalloc (storage);
337 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
339 bfd_fatal (bfd_get_filename (abfd));
340 if (dynsymcount == 0)
341 fprintf (stderr, "%s: %s: No dynamic symbols\n",
342 program_name, bfd_get_filename (abfd));
346 /* Filter out (in place) symbols that are useless for disassembly.
347 COUNT is the number of elements in SYMBOLS.
348 Return the number of useful symbols. */
351 remove_useless_symbols (symbols, count)
355 register asymbol **in_ptr = symbols, **out_ptr = symbols;
359 asymbol *sym = *in_ptr++;
361 if (sym->name == NULL || sym->name[0] == '\0')
363 if (sym->flags & (BSF_DEBUGGING))
365 if (bfd_is_und_section (sym->section)
366 || bfd_is_com_section (sym->section))
371 return out_ptr - symbols;
374 /* Sort symbols into value order. */
377 compare_symbols (ap, bp)
381 const asymbol *a = *(const asymbol **)ap;
382 const asymbol *b = *(const asymbol **)bp;
386 flagword aflags, bflags;
388 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
390 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
393 if (a->section > b->section)
395 else if (a->section < b->section)
398 an = bfd_asymbol_name (a);
399 bn = bfd_asymbol_name (b);
403 /* The symbols gnu_compiled and gcc2_compiled convey no real
404 information, so put them after other symbols with the same value. */
406 af = (strstr (an, "gnu_compiled") != NULL
407 || strstr (an, "gcc2_compiled") != NULL);
408 bf = (strstr (bn, "gnu_compiled") != NULL
409 || strstr (bn, "gcc2_compiled") != NULL);
416 /* We use a heuristic for the file name, to try to sort it after
417 more useful symbols. It may not work on non Unix systems, but it
418 doesn't really matter; the only difference is precisely which
419 symbol names get printed. */
421 #define file_symbol(s, sn, snl) \
422 (((s)->flags & BSF_FILE) != 0 \
423 || ((sn)[(snl) - 2] == '.' \
424 && ((sn)[(snl) - 1] == 'o' \
425 || (sn)[(snl) - 1] == 'a')))
427 af = file_symbol (a, an, anl);
428 bf = file_symbol (b, bn, bnl);
435 /* Try to sort global symbols before local symbols before debugging
441 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
443 if ((aflags & BSF_DEBUGGING) != 0)
448 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
450 if ((aflags & BSF_LOCAL) != 0)
455 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
457 if ((aflags & BSF_GLOBAL) != 0)
463 /* Symbols that start with '.' might be section names, so sort them
464 after symbols that don't start with '.'. */
465 if (an[0] == '.' && bn[0] != '.')
467 if (an[0] != '.' && bn[0] == '.')
470 /* Finally, if we can't distinguish them in any other way, try to
471 get consistent results by sorting the symbols by name. */
472 return strcmp (an, bn);
475 /* Sort relocs into address order. */
478 compare_relocs (ap, bp)
482 const arelent *a = *(const arelent **)ap;
483 const arelent *b = *(const arelent **)bp;
485 if (a->address > b->address)
487 else if (a->address < b->address)
490 /* So that associated relocations tied to the same address show up
491 in the correct order, we don't do any further sorting. */
500 /* Print VMA to STREAM with no leading zeroes. */
503 objdump_print_value (vma, info)
505 struct disassemble_info *info;
510 sprintf_vma (buf, vma);
511 for (p = buf; *p == '0'; ++p)
513 (*info->fprintf_func) (info->stream, "%s", p);
516 /* Print VMA symbolically to INFO if possible. */
519 objdump_print_address (vma, info)
521 struct disassemble_info *info;
525 /* @@ Would it speed things up to cache the last two symbols returned,
526 and maybe their address ranges? For many processors, only one memory
527 operand can be present at a time, so the 2-entry cache wouldn't be
528 constantly churned by code doing heavy memory accesses. */
530 /* Indices in `sorted_syms'. */
532 long max = sorted_symcount;
535 sprintf_vma (buf, vma);
536 (*info->fprintf_func) (info->stream, "%s", buf);
538 if (sorted_symcount < 1)
541 /* Perform a binary search looking for the closest symbol to the
542 required value. We are searching the range (min, max]. */
543 while (min + 1 < max)
547 thisplace = (max + min) / 2;
548 sym = sorted_syms[thisplace];
550 if (bfd_asymbol_value (sym) > vma)
552 else if (bfd_asymbol_value (sym) < vma)
561 /* The symbol we want is now in min, the low end of the range we
562 were searching. If there are several symbols with the same
563 value, we want the first one. */
566 && (bfd_asymbol_value (sorted_syms[thisplace])
567 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
571 /* If the file is relocateable, and the symbol could be from this
572 section, prefer a symbol from this section over symbols from
573 others, even if the other symbol's value might be closer.
575 Note that this may be wrong for some symbol references if the
576 sections have overlapping memory ranges, but in that case there's
577 no way to tell what's desired without looking at the relocation
579 struct objdump_disasm_info *aux;
582 aux = (struct objdump_disasm_info *) info->application_data;
583 if (sorted_syms[thisplace]->section != aux->sec
585 || ((aux->abfd->flags & HAS_RELOC) != 0
586 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
587 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
588 + bfd_section_size (aux->abfd, aux->sec)))))
590 for (i = thisplace + 1; i < sorted_symcount; i++)
592 if (bfd_asymbol_value (sorted_syms[i])
593 != bfd_asymbol_value (sorted_syms[thisplace]))
599 if (sorted_syms[i]->section == aux->sec
601 || sorted_syms[i - 1]->section != aux->sec
602 || (bfd_asymbol_value (sorted_syms[i])
603 != bfd_asymbol_value (sorted_syms[i - 1]))))
610 if (sorted_syms[thisplace]->section != aux->sec)
612 /* We didn't find a good symbol with a smaller value.
613 Look for one with a larger value. */
614 for (i = thisplace + 1; i < sorted_symcount; i++)
616 if (sorted_syms[i]->section == aux->sec)
624 if (sorted_syms[thisplace]->section != aux->sec
626 || ((aux->abfd->flags & HAS_RELOC) != 0
627 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
628 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
629 + bfd_section_size (aux->abfd, aux->sec)))))
633 (*info->fprintf_func) (info->stream, " <%s",
634 bfd_get_section_name (aux->abfd, aux->sec));
635 secaddr = bfd_get_section_vma (aux->abfd, aux->sec);
638 (*info->fprintf_func) (info->stream, "-");
639 objdump_print_value (secaddr - vma, info);
641 else if (vma > secaddr)
643 (*info->fprintf_func) (info->stream, "+");
644 objdump_print_value (vma - secaddr, info);
646 (*info->fprintf_func) (info->stream, ">");
652 (*info->fprintf_func) (info->stream, " <%s", sorted_syms[thisplace]->name);
653 if (bfd_asymbol_value (sorted_syms[thisplace]) > vma)
655 (*info->fprintf_func) (info->stream, "-");
656 objdump_print_value (bfd_asymbol_value (sorted_syms[thisplace]) - vma,
659 else if (vma > bfd_asymbol_value (sorted_syms[thisplace]))
661 (*info->fprintf_func) (info->stream, "+");
662 objdump_print_value (vma - bfd_asymbol_value (sorted_syms[thisplace]),
665 (*info->fprintf_func) (info->stream, ">");
668 /* Hold the last function name and the last line number we displayed
671 static char *prev_functionname;
672 static unsigned int prev_line;
674 /* We keep a list of all files that we have seen when doing a
675 dissassembly with source, so that we know how much of the file to
676 display. This can be important for inlined functions. */
678 struct print_file_list
680 struct print_file_list *next;
686 static struct print_file_list *print_files;
688 /* The number of preceding context lines to show when we start
689 displaying a file for the first time. */
691 #define SHOW_PRECEDING_CONTEXT_LINES (5)
693 /* Skip ahead to a given line in a file, optionally printing each
697 skip_to_line PARAMS ((struct print_file_list *, unsigned int, boolean));
700 skip_to_line (p, line, show)
701 struct print_file_list *p;
705 while (p->line < line)
709 if (fgets (buf, sizeof buf, p->f) == NULL)
719 if (strchr (buf, '\n') != NULL)
724 /* Show the line number, or the source line, in a dissassembly
728 show_line (abfd, section, off)
733 CONST char *filename;
734 CONST char *functionname;
737 if (! with_line_numbers && ! with_source_code)
740 if (! bfd_find_nearest_line (abfd, section, syms, off, &filename,
741 &functionname, &line))
744 if (filename != NULL && *filename == '\0')
746 if (functionname != NULL && *functionname == '\0')
749 if (with_line_numbers)
751 if (functionname != NULL
752 && (prev_functionname == NULL
753 || strcmp (functionname, prev_functionname) != 0))
754 printf ("%s():\n", functionname);
755 if (line > 0 && line != prev_line)
756 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
763 struct print_file_list **pp, *p;
765 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
766 if (strcmp ((*pp)->filename, filename) == 0)
772 if (p != print_files)
776 /* We have reencountered a file name which we saw
777 earlier. This implies that either we are dumping out
778 code from an included file, or the same file was
779 linked in more than once. There are two common cases
780 of an included file: inline functions in a header
781 file, and a bison or flex skeleton file. In the
782 former case we want to just start printing (but we
783 back up a few lines to give context); in the latter
784 case we want to continue from where we left off. I
785 can't think of a good way to distinguish the cases,
786 so I used a heuristic based on the file name. */
787 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
791 l = line - SHOW_PRECEDING_CONTEXT_LINES;
798 p->f = fopen (p->filename, "r");
802 skip_to_line (p, l, false);
804 if (print_files->f != NULL)
806 fclose (print_files->f);
807 print_files->f = NULL;
813 skip_to_line (p, line, true);
815 p->next = print_files;
823 f = fopen (filename, "r");
828 p = ((struct print_file_list *)
829 xmalloc (sizeof (struct print_file_list)));
830 p->filename = xmalloc (strlen (filename) + 1);
831 strcpy (p->filename, filename);
835 if (print_files != NULL && print_files->f != NULL)
837 fclose (print_files->f);
838 print_files->f = NULL;
840 p->next = print_files;
843 l = line - SHOW_PRECEDING_CONTEXT_LINES;
846 skip_to_line (p, l, false);
848 skip_to_line (p, line, true);
853 if (functionname != NULL
854 && (prev_functionname == NULL
855 || strcmp (functionname, prev_functionname) != 0))
857 if (prev_functionname != NULL)
858 free (prev_functionname);
859 prev_functionname = xmalloc (strlen (functionname) + 1);
860 strcpy (prev_functionname, functionname);
863 if (line > 0 && line != prev_line)
867 /* Pseudo FILE object for strings. */
873 /* sprintf to a "stream" */
875 #ifdef ANSI_PROTOTYPES
877 objdump_sprintf (SFILE *f, const char *format, ...)
882 va_start (args, format);
883 vsprintf (f->current, format, args);
884 f->current += n = strlen (f->current);
890 objdump_sprintf (va_alist)
899 f = va_arg (args, SFILE *);
900 format = va_arg (args, const char *);
901 vsprintf (f->current, format, args);
902 f->current += n = strlen (f->current);
909 disassemble_data (abfd)
913 disassembler_ftype disassemble_fn = 0; /* New style */
914 struct disassemble_info disasm_info;
915 struct objdump_disasm_info aux;
917 boolean done_dot = false;
922 prev_functionname = NULL;
925 /* We make a copy of syms to sort. We don't want to sort syms
926 because that will screw up the relocs. */
927 sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
928 memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
930 sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
932 /* Sort the symbols into section and symbol order */
933 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
935 INIT_DISASSEMBLE_INFO(disasm_info, stdout, fprintf);
936 disasm_info.application_data = (PTR) &aux;
938 disasm_info.print_address_func = objdump_print_address;
940 if (machine != (char *) NULL)
942 const bfd_arch_info_type *info = bfd_scan_arch (machine);
945 fprintf (stderr, "%s: Can't use supplied machine %s\n",
950 abfd->arch_info = info;
953 disassemble_fn = disassembler (abfd);
956 fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
958 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
962 disasm_info.arch = bfd_get_arch (abfd);
963 disasm_info.mach = bfd_get_mach (abfd);
964 if (bfd_big_endian (abfd))
965 disasm_info.endian = BFD_ENDIAN_BIG;
966 else if (bfd_little_endian (abfd))
967 disasm_info.endian = BFD_ENDIAN_LITTLE;
969 /* ??? Aborting here seems too drastic. We could default to big or little
971 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
973 for (section = abfd->sections;
974 section != (asection *) NULL;
975 section = section->next)
977 bfd_byte *data = NULL;
978 bfd_size_type datasize = 0;
979 arelent **relbuf = NULL;
980 arelent **relpp = NULL;
981 arelent **relppend = NULL;
984 if ((section->flags & SEC_LOAD) == 0
985 || (! disassemble_all
987 && (section->flags & SEC_CODE) == 0))
989 if (only != (char *) NULL && strcmp (only, section->name) != 0)
993 && (section->flags & SEC_RELOC) != 0)
997 relsize = bfd_get_reloc_upper_bound (abfd, section);
999 bfd_fatal (bfd_get_filename (abfd));
1005 relbuf = (arelent **) xmalloc (relsize);
1006 relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
1008 bfd_fatal (bfd_get_filename (abfd));
1010 /* Sort the relocs by address. */
1011 qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
1014 relppend = relpp + relcount;
1018 printf ("Disassembly of section %s:\n", section->name);
1020 datasize = bfd_get_section_size_before_reloc (section);
1024 data = (bfd_byte *) xmalloc ((size_t) datasize);
1026 bfd_get_section_contents (abfd, section, data, 0, datasize);
1029 disasm_info.buffer = data;
1030 disasm_info.buffer_vma = section->vma;
1031 disasm_info.buffer_length = datasize;
1032 if (start_address == (bfd_vma) -1
1033 || start_address < disasm_info.buffer_vma)
1036 i = start_address - disasm_info.buffer_vma;
1037 if (stop_address == (bfd_vma) -1)
1041 if (stop_address < disasm_info.buffer_vma)
1044 stop = stop_address - disasm_info.buffer_vma;
1045 if (stop > disasm_info.buffer_length)
1046 stop = disasm_info.buffer_length;
1051 boolean need_nl = false;
1053 if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 &&
1056 if (done_dot == false)
1066 if (with_line_numbers || with_source_code)
1067 show_line (abfd, section, i);
1068 aux.require_sec = true;
1069 objdump_print_address (section->vma + i, &disasm_info);
1070 aux.require_sec = false;
1073 sfile.buffer = sfile.current = buf;
1074 disasm_info.fprintf_func = (fprintf_ftype) objdump_sprintf;
1075 disasm_info.stream = (FILE *) &sfile;
1076 bytes = (*disassemble_fn) (section->vma + i, &disasm_info);
1077 disasm_info.fprintf_func = (fprintf_ftype) fprintf;
1078 disasm_info.stream = stdout;
1085 for (j = i; j < i + bytes; ++j)
1087 printf ("%02x", (unsigned) data[j]);
1090 /* Separate raw data from instruction by extra space. */
1094 printf ("%s", sfile.buffer);
1103 && (section->flags & SEC_RELOC) != 0)
1105 while (relpp < relppend
1106 && ((*relpp)->address >= (bfd_vma) i
1107 && (*relpp)->address < (bfd_vma) i + bytes))
1110 const char *sym_name;
1114 printf ("\t\tRELOC: ");
1116 printf_vma (section->vma + q->address);
1118 printf (" %s ", q->howto->name);
1120 if (q->sym_ptr_ptr != NULL
1121 && *q->sym_ptr_ptr != NULL)
1123 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1124 if (sym_name == NULL || *sym_name == '\0')
1128 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1129 sym_name = bfd_get_section_name (abfd, sym_sec);
1130 if (sym_name == NULL || *sym_name == '\0')
1131 sym_name = "*unknown*";
1135 sym_name = "*unknown*";
1137 printf ("%s", sym_name);
1142 printf_vma (q->addend);
1165 /* Define a table of stab values and print-strings. We wish the initializer
1166 could be a direct-mapped table, but instead we build one the first
1169 void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
1170 char *strsect_name));
1172 /* Dump the stabs sections from an object file that has a section that
1173 uses Sun stabs encoding. It has to use some hooks into BFD because
1174 string table sections are not normally visible to BFD callers. */
1180 dump_section_stabs (abfd, ".stab", ".stabstr");
1181 dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
1182 dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
1183 dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1186 static struct internal_nlist *stabs;
1187 static bfd_size_type stab_size;
1189 static char *strtab;
1190 static bfd_size_type stabstr_size;
1192 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1193 and string table section STRSECT_NAME into `strtab'.
1194 If the section exists and was read, allocate the space and return true.
1195 Otherwise return false. */
1198 read_section_stabs (abfd, stabsect_name, strsect_name)
1200 char *stabsect_name;
1203 asection *stabsect, *stabstrsect;
1205 stabsect = bfd_get_section_by_name (abfd, stabsect_name);
1208 printf ("No %s section present\n\n", stabsect_name);
1212 stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
1213 if (0 == stabstrsect)
1215 fprintf (stderr, "%s: %s has no %s section\n", program_name,
1216 bfd_get_filename (abfd), strsect_name);
1220 stab_size = bfd_section_size (abfd, stabsect);
1221 stabstr_size = bfd_section_size (abfd, stabstrsect);
1223 stabs = (struct internal_nlist *) xmalloc (stab_size);
1224 strtab = (char *) xmalloc (stabstr_size);
1226 if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
1228 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1229 program_name, stabsect_name, bfd_get_filename (abfd),
1230 bfd_errmsg (bfd_get_error ()));
1236 if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
1239 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1240 program_name, strsect_name, bfd_get_filename (abfd),
1241 bfd_errmsg (bfd_get_error ()));
1250 #define SWAP_SYMBOL(symp, abfd) \
1252 (symp)->n_strx = bfd_h_get_32(abfd, \
1253 (unsigned char *)&(symp)->n_strx); \
1254 (symp)->n_desc = bfd_h_get_16 (abfd, \
1255 (unsigned char *)&(symp)->n_desc); \
1256 (symp)->n_value = bfd_h_get_32 (abfd, \
1257 (unsigned char *)&(symp)->n_value); \
1260 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1261 using string table section STRSECT_NAME (in `strtab'). */
1264 print_section_stabs (abfd, stabsect_name, strsect_name)
1266 char *stabsect_name;
1270 unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
1271 struct internal_nlist *stabp = stabs,
1272 *stabs_end = (struct internal_nlist *) (stab_size + (char *) stabs);
1274 printf ("Contents of %s section:\n\n", stabsect_name);
1275 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1277 /* Loop through all symbols and print them.
1279 We start the index at -1 because there is a dummy symbol on
1280 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1282 for (i = -1; stabp < stabs_end; stabp++, i++)
1286 SWAP_SYMBOL (stabp, abfd);
1287 printf ("\n%-6d ", i);
1288 /* Either print the stab name, or, if unnamed, print its number
1289 again (makes consistent formatting for tools like awk). */
1290 name = bfd_get_stab_name (stabp->n_type);
1292 printf ("%-6s", name);
1293 else if (stabp->n_type == N_UNDF)
1296 printf ("%-6d", stabp->n_type);
1297 printf (" %-6d %-6d ", stabp->n_other, stabp->n_desc);
1298 printf_vma (stabp->n_value);
1299 printf (" %-6lu", stabp->n_strx);
1301 /* Symbols with type == 0 (N_UNDF) specify the length of the
1302 string table associated with this file. We use that info
1303 to know how to relocate the *next* file's string table indices. */
1305 if (stabp->n_type == N_UNDF)
1307 file_string_table_offset = next_file_string_table_offset;
1308 next_file_string_table_offset += stabp->n_value;
1312 /* Using the (possibly updated) string table offset, print the
1313 string (if any) associated with this symbol. */
1315 if ((stabp->n_strx + file_string_table_offset) < stabstr_size)
1316 printf (" %s", &strtab[stabp->n_strx + file_string_table_offset]);
1325 dump_section_stabs (abfd, stabsect_name, strsect_name)
1327 char *stabsect_name;
1332 /* Check for section names for which stabsect_name is a prefix, to
1333 handle .stab0, etc. */
1334 for (s = abfd->sections;
1338 if (strncmp (stabsect_name, s->name, strlen (stabsect_name)) == 0
1339 && strncmp (strsect_name, s->name, strlen (strsect_name)) != 0)
1341 if (read_section_stabs (abfd, s->name, strsect_name))
1343 print_section_stabs (abfd, s->name, strsect_name);
1352 dump_bfd_header (abfd)
1357 printf ("architecture: %s, ",
1358 bfd_printable_arch_mach (bfd_get_arch (abfd),
1359 bfd_get_mach (abfd)));
1360 printf ("flags 0x%08x:\n", abfd->flags);
1362 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1363 PF (HAS_RELOC, "HAS_RELOC");
1364 PF (EXEC_P, "EXEC_P");
1365 PF (HAS_LINENO, "HAS_LINENO");
1366 PF (HAS_DEBUG, "HAS_DEBUG");
1367 PF (HAS_SYMS, "HAS_SYMS");
1368 PF (HAS_LOCALS, "HAS_LOCALS");
1369 PF (DYNAMIC, "DYNAMIC");
1370 PF (WP_TEXT, "WP_TEXT");
1371 PF (D_PAGED, "D_PAGED");
1372 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
1373 printf ("\nstart address 0x");
1374 printf_vma (abfd->start_address);
1379 dump_bfd_private_header (abfd)
1382 bfd_print_private_bfd_data (abfd, stdout);
1390 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
1392 bfd_nonfatal (bfd_get_filename (abfd));
1393 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1395 list_matching_formats (matching);
1401 printf ("\n%s: file format %s\n", bfd_get_filename (abfd),
1404 print_arelt_descr (stdout, abfd, true);
1405 if (dump_file_header)
1406 dump_bfd_header (abfd);
1407 if (dump_private_headers)
1408 dump_bfd_private_header (abfd);
1410 if (dump_section_headers)
1411 dump_headers (abfd);
1412 if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
1414 syms = slurp_symtab (abfd);
1416 if (dump_dynamic_symtab || dump_dynamic_reloc_info)
1418 dynsyms = slurp_dynamic_symtab (abfd);
1421 dump_symbols (abfd, false);
1422 if (dump_dynamic_symtab)
1423 dump_symbols (abfd, true);
1424 if (dump_stab_section_info)
1426 if (dump_reloc_info && ! disassemble)
1428 if (dump_dynamic_reloc_info)
1429 dump_dynamic_relocs (abfd);
1430 if (dump_section_contents)
1433 disassemble_data (abfd);
1438 dhandle = read_debugging_info (abfd, syms, symcount);
1439 if (dhandle != NULL)
1441 if (! print_debugging_info (stdout, dhandle))
1442 fprintf (stderr, "%s: printing debugging information failed\n",
1443 bfd_get_filename (abfd));
1459 display_file (filename, target)
1463 bfd *file, *arfile = (bfd *) NULL;
1465 file = bfd_openr (filename, target);
1468 bfd_nonfatal (filename);
1472 if (bfd_check_format (file, bfd_archive) == true)
1474 bfd *last_arfile = NULL;
1476 printf ("In archive %s:\n", bfd_get_filename (file));
1479 bfd_set_error (bfd_error_no_error);
1481 arfile = bfd_openr_next_archived_file (file, arfile);
1484 if (bfd_get_error () != bfd_error_no_more_archived_files)
1486 bfd_nonfatal (bfd_get_filename (file));
1491 display_bfd (arfile);
1493 if (last_arfile != NULL)
1494 bfd_close (last_arfile);
1495 last_arfile = arfile;
1498 if (last_arfile != NULL)
1499 bfd_close (last_arfile);
1507 /* Actually display the various requested regions */
1515 bfd_size_type datasize = 0;
1517 bfd_size_type start, stop;
1519 for (section = abfd->sections; section != NULL; section =
1524 if (only == (char *) NULL ||
1525 strcmp (only, section->name) == 0)
1527 if (section->flags & SEC_HAS_CONTENTS)
1529 printf ("Contents of section %s:\n", section->name);
1531 if (bfd_section_size (abfd, section) == 0)
1533 data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
1534 datasize = bfd_section_size (abfd, section);
1537 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
1539 if (start_address == (bfd_vma) -1
1540 || start_address < section->vma)
1543 start = start_address - section->vma;
1544 if (stop_address == (bfd_vma) -1)
1545 stop = bfd_section_size (abfd, section);
1548 if (stop_address < section->vma)
1551 stop = stop_address - section->vma;
1552 if (stop > bfd_section_size (abfd, section))
1553 stop = bfd_section_size (abfd, section);
1555 for (i = start; i < stop; i += onaline)
1559 printf (" %04lx ", (unsigned long int) (i + section->vma));
1560 for (j = i; j < i + onaline; j++)
1563 printf ("%02x", (unsigned) (data[j]));
1571 for (j = i; j < i + onaline; j++)
1576 printf ("%c", isprint (data[j]) ? data[j] : '.');
1586 /* Should perhaps share code and display with nm? */
1588 dump_symbols (abfd, dynamic)
1602 printf ("DYNAMIC SYMBOL TABLE:\n");
1610 printf ("SYMBOL TABLE:\n");
1613 for (count = 0; count < max; count++)
1617 bfd *cur_bfd = bfd_asymbol_bfd(*current);
1620 bfd_print_symbol (cur_bfd,
1622 *current, bfd_print_symbol_all);
1640 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
1644 if (bfd_is_abs_section (a))
1646 if (bfd_is_und_section (a))
1648 if (bfd_is_com_section (a))
1653 if (strcmp (only, a->name))
1656 else if ((a->flags & SEC_RELOC) == 0)
1659 relsize = bfd_get_reloc_upper_bound (abfd, a);
1661 bfd_fatal (bfd_get_filename (abfd));
1663 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
1667 printf (" (none)\n\n");
1671 relpp = (arelent **) xmalloc (relsize);
1672 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
1674 bfd_fatal (bfd_get_filename (abfd));
1675 else if (relcount == 0)
1677 printf (" (none)\n\n");
1682 dump_reloc_set (abfd, relpp, relcount);
1691 dump_dynamic_relocs (abfd)
1698 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
1700 bfd_fatal (bfd_get_filename (abfd));
1702 printf ("DYNAMIC RELOCATION RECORDS");
1706 printf (" (none)\n\n");
1710 relpp = (arelent **) xmalloc (relsize);
1711 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
1713 bfd_fatal (bfd_get_filename (abfd));
1714 else if (relcount == 0)
1716 printf (" (none)\n\n");
1721 dump_reloc_set (abfd, relpp, relcount);
1729 dump_reloc_set (abfd, relpp, relcount)
1736 /* Get column headers lined up reasonably. */
1742 sprintf_vma (buf, (bfd_vma) -1);
1743 width = strlen (buf) - 7;
1745 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
1748 for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
1751 CONST char *sym_name;
1752 CONST char *section_name;
1754 if (start_address != (bfd_vma) -1
1755 && q->address < start_address)
1757 if (stop_address != (bfd_vma) -1
1758 && q->address > stop_address)
1761 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
1763 sym_name = (*(q->sym_ptr_ptr))->name;
1764 section_name = (*(q->sym_ptr_ptr))->section->name;
1769 section_name = NULL;
1773 printf_vma (q->address);
1774 printf (" %-16s %s",
1780 if (section_name == (CONST char *) NULL)
1781 section_name = "*unknown*";
1782 printf_vma (q->address);
1783 printf (" %-16s [%s]",
1790 printf_vma (q->addend);
1796 /* The length of the longest architecture name + 1. */
1797 #define LONGEST_ARCH sizeof("rs6000:6000")
1804 endian_string (endian)
1805 enum bfd_endian endian;
1807 if (endian == BFD_ENDIAN_BIG)
1808 return "big endian";
1809 else if (endian == BFD_ENDIAN_LITTLE)
1810 return "little endian";
1812 return "endianness unknown";
1815 /* List the targets that BFD is configured to support, each followed
1816 by its endianness and the architectures it supports. */
1819 display_target_list ()
1821 extern char *tmpnam ();
1822 extern bfd_target *bfd_target_vector[];
1823 char tmparg[L_tmpnam];
1827 dummy_name = tmpnam (tmparg);
1828 for (t = 0; bfd_target_vector[t]; t++)
1830 bfd_target *p = bfd_target_vector[t];
1831 bfd *abfd = bfd_openw (dummy_name, p->name);
1834 printf ("%s\n (header %s, data %s)\n", p->name,
1835 endian_string (p->header_byteorder),
1836 endian_string (p->byteorder));
1840 bfd_nonfatal (dummy_name);
1844 if (! bfd_set_format (abfd, bfd_object))
1846 if (bfd_get_error () != bfd_error_invalid_operation)
1847 bfd_nonfatal (p->name);
1851 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1852 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
1854 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
1856 unlink (dummy_name);
1859 /* Print a table showing which architectures are supported for entries
1860 FIRST through LAST-1 of bfd_target_vector (targets across,
1861 architectures down). */
1864 display_info_table (first, last)
1868 extern bfd_target *bfd_target_vector[];
1869 extern char *tmpnam ();
1870 char tmparg[L_tmpnam];
1874 /* Print heading of target names. */
1875 printf ("\n%*s", (int) LONGEST_ARCH, " ");
1876 for (t = first; t < last && bfd_target_vector[t]; t++)
1877 printf ("%s ", bfd_target_vector[t]->name);
1880 dummy_name = tmpnam (tmparg);
1881 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1882 if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
1884 printf ("%*s ", (int) LONGEST_ARCH - 1,
1885 bfd_printable_arch_mach (a, 0));
1886 for (t = first; t < last && bfd_target_vector[t]; t++)
1888 bfd_target *p = bfd_target_vector[t];
1890 bfd *abfd = bfd_openw (dummy_name, p->name);
1894 bfd_nonfatal (p->name);
1900 if (! bfd_set_format (abfd, bfd_object))
1902 if (bfd_get_error () != bfd_error_invalid_operation)
1903 bfd_nonfatal (p->name);
1910 if (! bfd_set_arch_mach (abfd, a, 0))
1915 printf ("%s ", p->name);
1918 int l = strlen (p->name);
1926 unlink (dummy_name);
1929 /* Print tables of all the target-architecture combinations that
1930 BFD has been configured to support. */
1933 display_target_tables ()
1936 extern bfd_target *bfd_target_vector[];
1938 extern char *getenv ();
1941 colum = getenv ("COLUMNS");
1943 columns = atoi (colum);
1948 while (bfd_target_vector[t] != NULL)
1952 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
1954 while (wid < columns && bfd_target_vector[t] != NULL)
1958 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
1959 if (newwid >= columns)
1964 display_info_table (oldt, t);
1971 printf ("BFD header file version %s\n", BFD_VERSION);
1972 display_target_list ();
1973 display_target_tables ();
1982 char *target = default_target;
1983 boolean seenflag = false;
1985 program_name = *argv;
1986 xmalloc_set_program_name (program_name);
1988 START_PROGRESS (program_name, 0);
1992 while ((c = getopt_long (argc, argv, "pib:m:VdDlfahrRtTxsSj:w", long_options,
1996 if (c != 'l' && c != OPTION_START_ADDRESS && c != OPTION_STOP_ADDRESS)
2001 break; /* we've been given a long option */
2009 with_line_numbers = 1;
2015 dump_file_header = true;
2018 formats_info = true;
2021 dump_private_headers = 1;
2024 dump_private_headers = 1;
2026 dump_reloc_info = 1;
2027 dump_file_header = true;
2029 dump_section_headers = 1;
2035 dump_dynamic_symtab = 1;
2041 disassemble = disassemble_all = true;
2045 with_source_code = true;
2048 dump_section_contents = 1;
2051 dump_reloc_info = 1;
2054 dump_dynamic_reloc_info = 1;
2060 dump_section_headers = 1;
2070 case OPTION_START_ADDRESS:
2071 start_address = parse_vma (optarg, "--start-address");
2073 case OPTION_STOP_ADDRESS:
2074 stop_address = parse_vma (optarg, "--stop-address");
2083 printf ("GNU %s version %s\n", program_name, program_version);
2087 if (seenflag == false)
2097 display_file ("a.out", target);
2099 for (; optind < argc;)
2100 display_file (argv[optind++], target);
2103 END_PROGRESS (program_name);