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 %-14s%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");
244 printf ("Sections:\n");
246 printf ("Idx Name Size VMA LMA File off Algn\n");
248 printf ("Idx Name Size VMA LMA File off Algn\n");
250 bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
257 asymbol **sy = (asymbol **) NULL;
260 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
262 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
267 storage = bfd_get_symtab_upper_bound (abfd);
269 bfd_fatal (bfd_get_filename (abfd));
273 sy = (asymbol **) xmalloc (storage);
275 symcount = bfd_canonicalize_symtab (abfd, sy);
277 bfd_fatal (bfd_get_filename (abfd));
279 fprintf (stderr, "%s: %s: No symbols\n",
280 program_name, bfd_get_filename (abfd));
284 /* Read in the dynamic symbols. */
287 slurp_dynamic_symtab (abfd)
290 asymbol **sy = (asymbol **) NULL;
293 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
296 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
298 fprintf (stderr, "%s: %s: not a dynamic object\n",
299 program_name, bfd_get_filename (abfd));
304 bfd_fatal (bfd_get_filename (abfd));
309 sy = (asymbol **) xmalloc (storage);
311 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
313 bfd_fatal (bfd_get_filename (abfd));
314 if (dynsymcount == 0)
315 fprintf (stderr, "%s: %s: No dynamic symbols\n",
316 program_name, bfd_get_filename (abfd));
320 /* Filter out (in place) symbols that are useless for disassembly.
321 COUNT is the number of elements in SYMBOLS.
322 Return the number of useful symbols. */
325 remove_useless_symbols (symbols, count)
329 register asymbol **in_ptr = symbols, **out_ptr = symbols;
333 asymbol *sym = *in_ptr++;
335 if (sym->name == NULL || sym->name[0] == '\0')
337 if (sym->flags & (BSF_DEBUGGING))
339 if (bfd_is_und_section (sym->section)
340 || bfd_is_com_section (sym->section))
345 return out_ptr - symbols;
348 /* Sort symbols into value order. */
351 compare_symbols (ap, bp)
355 const asymbol *a = *(const asymbol **)ap;
356 const asymbol *b = *(const asymbol **)bp;
360 flagword aflags, bflags;
362 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
364 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
367 if (a->section > b->section)
369 else if (a->section < b->section)
372 an = bfd_asymbol_name (a);
373 bn = bfd_asymbol_name (b);
377 /* The symbols gnu_compiled and gcc2_compiled convey no real
378 information, so put them after other symbols with the same value. */
380 af = (strstr (an, "gnu_compiled") != NULL
381 || strstr (an, "gcc2_compiled") != NULL);
382 bf = (strstr (bn, "gnu_compiled") != NULL
383 || strstr (bn, "gcc2_compiled") != NULL);
390 /* We use a heuristic for the file name, to try to sort it after
391 more useful symbols. It may not work on non Unix systems, but it
392 doesn't really matter; the only difference is precisely which
393 symbol names get printed. */
395 #define file_symbol(s, sn, snl) \
396 (((s)->flags & BSF_FILE) != 0 \
397 || ((sn)[(snl) - 2] == '.' \
398 && ((sn)[(snl) - 1] == 'o' \
399 || (sn)[(snl) - 1] == 'a')))
401 af = file_symbol (a, an, anl);
402 bf = file_symbol (b, bn, bnl);
409 /* Finally, try to sort global symbols before local symbols before
410 debugging symbols. */
415 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
417 if ((aflags & BSF_DEBUGGING) != 0)
422 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
424 if ((aflags & BSF_LOCAL) != 0)
433 /* Sort relocs into address order. */
436 compare_relocs (ap, bp)
440 const arelent *a = *(const arelent **)ap;
441 const arelent *b = *(const arelent **)bp;
443 if (a->address > b->address)
445 else if (a->address < b->address)
448 /* So that associated relocations tied to the same address show up
449 in the correct order, we don't do any further sorting. */
458 /* Print VMA to STREAM with no leading zeroes. */
461 objdump_print_value (vma, info)
463 struct disassemble_info *info;
468 sprintf_vma (buf, vma);
469 for (p = buf; *p == '0'; ++p)
471 (*info->fprintf_func) (info->stream, "%s", p);
474 /* Print VMA symbolically to INFO if possible. */
477 objdump_print_address (vma, info)
479 struct disassemble_info *info;
483 /* @@ Would it speed things up to cache the last two symbols returned,
484 and maybe their address ranges? For many processors, only one memory
485 operand can be present at a time, so the 2-entry cache wouldn't be
486 constantly churned by code doing heavy memory accesses. */
488 /* Indices in `sorted_syms'. */
490 long max = sorted_symcount;
493 sprintf_vma (buf, vma);
494 (*info->fprintf_func) (info->stream, "%s", buf);
496 if (sorted_symcount < 1)
499 /* Perform a binary search looking for the closest symbol to the
500 required value. We are searching the range (min, max]. */
501 while (min + 1 < max)
505 thisplace = (max + min) / 2;
506 sym = sorted_syms[thisplace];
508 if (bfd_asymbol_value (sym) > vma)
510 else if (bfd_asymbol_value (sym) < vma)
519 /* The symbol we want is now in min, the low end of the range we
520 were searching. If there are several symbols with the same
521 value, we want the first one. */
524 && (bfd_asymbol_value (sorted_syms[thisplace])
525 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
529 /* If the file is relocateable, and the symbol could be from this
530 section, prefer a symbol from this section over symbols from
531 others, even if the other symbol's value might be closer.
533 Note that this may be wrong for some symbol references if the
534 sections have overlapping memory ranges, but in that case there's
535 no way to tell what's desired without looking at the relocation
537 struct objdump_disasm_info *aux;
540 aux = (struct objdump_disasm_info *) info->application_data;
541 if (sorted_syms[thisplace]->section != aux->sec
543 || ((aux->abfd->flags & HAS_RELOC) != 0
544 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
545 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
546 + bfd_section_size (aux->abfd, aux->sec)))))
548 for (i = thisplace + 1; i < sorted_symcount; i++)
550 if (bfd_asymbol_value (sorted_syms[i])
551 != bfd_asymbol_value (sorted_syms[thisplace]))
557 if (sorted_syms[i]->section == aux->sec
559 || sorted_syms[i - 1]->section != aux->sec
560 || (bfd_asymbol_value (sorted_syms[i])
561 != bfd_asymbol_value (sorted_syms[i - 1]))))
568 if (sorted_syms[thisplace]->section != aux->sec)
570 /* We didn't find a good symbol with a smaller value.
571 Look for one with a larger value. */
572 for (i = thisplace + 1; i < sorted_symcount; i++)
574 if (sorted_syms[i]->section == aux->sec)
582 if (sorted_syms[thisplace]->section != aux->sec
584 || ((aux->abfd->flags & HAS_RELOC) != 0
585 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
586 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
587 + bfd_section_size (aux->abfd, aux->sec)))))
591 (*info->fprintf_func) (info->stream, " <%s",
592 bfd_get_section_name (aux->abfd, aux->sec));
593 secaddr = bfd_get_section_vma (aux->abfd, aux->sec);
596 (*info->fprintf_func) (info->stream, "-");
597 objdump_print_value (secaddr - vma, info);
599 else if (vma > secaddr)
601 (*info->fprintf_func) (info->stream, "+");
602 objdump_print_value (vma - secaddr, info);
604 (*info->fprintf_func) (info->stream, ">");
610 (*info->fprintf_func) (info->stream, " <%s", sorted_syms[thisplace]->name);
611 if (bfd_asymbol_value (sorted_syms[thisplace]) > vma)
613 (*info->fprintf_func) (info->stream, "-");
614 objdump_print_value (bfd_asymbol_value (sorted_syms[thisplace]) - vma,
617 else if (vma > bfd_asymbol_value (sorted_syms[thisplace]))
619 (*info->fprintf_func) (info->stream, "+");
620 objdump_print_value (vma - bfd_asymbol_value (sorted_syms[thisplace]),
623 (*info->fprintf_func) (info->stream, ">");
626 /* Hold the last function name and the last line number we displayed
629 static char *prev_functionname;
630 static unsigned int prev_line;
632 /* We keep a list of all files that we have seen when doing a
633 dissassembly with source, so that we know how much of the file to
634 display. This can be important for inlined functions. */
636 struct print_file_list
638 struct print_file_list *next;
644 static struct print_file_list *print_files;
646 /* The number of preceding context lines to show when we start
647 displaying a file for the first time. */
649 #define SHOW_PRECEDING_CONTEXT_LINES (5)
651 /* Skip ahead to a given line in a file, optionally printing each
655 skip_to_line PARAMS ((struct print_file_list *, unsigned int, boolean));
658 skip_to_line (p, line, show)
659 struct print_file_list *p;
663 while (p->line < line)
667 if (fgets (buf, sizeof buf, p->f) == NULL)
677 if (strchr (buf, '\n') != NULL)
682 /* Show the line number, or the source line, in a dissassembly
686 show_line (abfd, section, off)
691 CONST char *filename;
692 CONST char *functionname;
695 if (! with_line_numbers && ! with_source_code)
698 if (! bfd_find_nearest_line (abfd, section, syms, off, &filename,
699 &functionname, &line))
702 if (filename != NULL && *filename == '\0')
704 if (functionname != NULL && *functionname == '\0')
707 if (with_line_numbers)
709 if (functionname != NULL
710 && (prev_functionname == NULL
711 || strcmp (functionname, prev_functionname) != 0))
712 printf ("%s():\n", functionname);
713 if (line > 0 && line != prev_line)
714 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
721 struct print_file_list **pp, *p;
723 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
724 if (strcmp ((*pp)->filename, filename) == 0)
730 if (p != print_files)
734 /* We have reencountered a file name which we saw
735 earlier. This implies that either we are dumping out
736 code from an included file, or the same file was
737 linked in more than once. There are two common cases
738 of an included file: inline functions in a header
739 file, and a bison or flex skeleton file. In the
740 former case we want to just start printing (but we
741 back up a few lines to give context); in the latter
742 case we want to continue from where we left off. I
743 can't think of a good way to distinguish the cases,
744 so I used a heuristic based on the file name. */
745 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
749 l = line - SHOW_PRECEDING_CONTEXT_LINES;
756 p->f = fopen (p->filename, "r");
760 skip_to_line (p, l, false);
762 if (print_files->f != NULL)
764 fclose (print_files->f);
765 print_files->f = NULL;
771 skip_to_line (p, line, true);
773 p->next = print_files;
781 f = fopen (filename, "r");
786 p = ((struct print_file_list *)
787 xmalloc (sizeof (struct print_file_list)));
788 p->filename = xmalloc (strlen (filename) + 1);
789 strcpy (p->filename, filename);
793 if (print_files != NULL && print_files->f != NULL)
795 fclose (print_files->f);
796 print_files->f = NULL;
798 p->next = print_files;
801 l = line - SHOW_PRECEDING_CONTEXT_LINES;
804 skip_to_line (p, l, false);
806 skip_to_line (p, line, true);
811 if (functionname != NULL
812 && (prev_functionname == NULL
813 || strcmp (functionname, prev_functionname) != 0))
815 if (prev_functionname != NULL)
816 free (prev_functionname);
817 prev_functionname = xmalloc (strlen (functionname) + 1);
818 strcpy (prev_functionname, functionname);
821 if (line > 0 && line != prev_line)
825 /* Pseudo FILE object for strings. */
831 /* sprintf to a "stream" */
833 #ifdef ANSI_PROTOTYPES
835 objdump_sprintf (SFILE *f, const char *format, ...)
840 va_start (args, format);
841 vsprintf (f->current, format, args);
842 f->current += n = strlen (f->current);
848 objdump_sprintf (va_alist)
857 f = va_arg (args, SFILE *);
858 format = va_arg (args, const char *);
859 vsprintf (f->current, format, args);
860 f->current += n = strlen (f->current);
867 disassemble_data (abfd)
871 disassembler_ftype disassemble_fn = 0; /* New style */
872 struct disassemble_info disasm_info;
873 struct objdump_disasm_info aux;
875 boolean done_dot = false;
880 prev_functionname = NULL;
883 /* We make a copy of syms to sort. We don't want to sort syms
884 because that will screw up the relocs. */
885 sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
886 memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
888 sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
890 /* Sort the symbols into section and symbol order */
891 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
893 INIT_DISASSEMBLE_INFO(disasm_info, stdout, fprintf);
894 disasm_info.application_data = (PTR) &aux;
896 disasm_info.print_address_func = objdump_print_address;
898 if (machine != (char *) NULL)
900 const bfd_arch_info_type *info = bfd_scan_arch (machine);
903 fprintf (stderr, "%s: Can't use supplied machine %s\n",
908 abfd->arch_info = info;
911 disassemble_fn = disassembler (abfd);
914 fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
916 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
920 disasm_info.arch = bfd_get_arch (abfd);
921 disasm_info.mach = bfd_get_mach (abfd);
922 if (bfd_big_endian (abfd))
923 disasm_info.endian = BFD_ENDIAN_BIG;
924 else if (bfd_little_endian (abfd))
925 disasm_info.endian = BFD_ENDIAN_LITTLE;
927 /* ??? Aborting here seems too drastic. We could default to big or little
929 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
931 for (section = abfd->sections;
932 section != (asection *) NULL;
933 section = section->next)
935 bfd_byte *data = NULL;
936 bfd_size_type datasize = 0;
937 arelent **relbuf = NULL;
938 arelent **relpp = NULL;
939 arelent **relppend = NULL;
942 if ((section->flags & SEC_LOAD) == 0
943 || (! disassemble_all
945 && (section->flags & SEC_CODE) == 0))
947 if (only != (char *) NULL && strcmp (only, section->name) != 0)
951 && (section->flags & SEC_RELOC) != 0)
955 relsize = bfd_get_reloc_upper_bound (abfd, section);
957 bfd_fatal (bfd_get_filename (abfd));
963 relbuf = (arelent **) xmalloc (relsize);
964 relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
966 bfd_fatal (bfd_get_filename (abfd));
968 /* Sort the relocs by address. */
969 qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
972 relppend = relpp + relcount;
976 printf ("Disassembly of section %s:\n", section->name);
978 datasize = bfd_get_section_size_before_reloc (section);
982 data = (bfd_byte *) xmalloc ((size_t) datasize);
984 bfd_get_section_contents (abfd, section, data, 0, datasize);
987 disasm_info.buffer = data;
988 disasm_info.buffer_vma = section->vma;
989 disasm_info.buffer_length = datasize;
990 if (start_address == (bfd_vma) -1
991 || start_address < disasm_info.buffer_vma)
994 i = start_address - disasm_info.buffer_vma;
995 if (stop_address == (bfd_vma) -1)
999 if (stop_address < disasm_info.buffer_vma)
1002 stop = stop_address - disasm_info.buffer_vma;
1003 if (stop > disasm_info.buffer_length)
1004 stop = disasm_info.buffer_length;
1009 boolean need_nl = false;
1011 if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 &&
1014 if (done_dot == false)
1024 if (with_line_numbers || with_source_code)
1025 show_line (abfd, section, i);
1026 aux.require_sec = true;
1027 objdump_print_address (section->vma + i, &disasm_info);
1028 aux.require_sec = false;
1031 sfile.buffer = sfile.current = buf;
1032 disasm_info.fprintf_func = (fprintf_ftype) objdump_sprintf;
1033 disasm_info.stream = (FILE *) &sfile;
1034 bytes = (*disassemble_fn) (section->vma + i, &disasm_info);
1035 disasm_info.fprintf_func = (fprintf_ftype) fprintf;
1036 disasm_info.stream = stdout;
1043 for (j = i; j < i + bytes; ++j)
1045 printf ("%02x", (unsigned) data[j]);
1048 /* Separate raw data from instruction by extra space. */
1052 printf ("%s", sfile.buffer);
1061 && (section->flags & SEC_RELOC) != 0)
1063 while (relpp < relppend
1064 && ((*relpp)->address >= (bfd_vma) i
1065 && (*relpp)->address < (bfd_vma) i + bytes))
1068 const char *sym_name;
1072 printf ("\t\tRELOC: ");
1074 printf_vma (section->vma + q->address);
1076 printf (" %s ", q->howto->name);
1078 if (q->sym_ptr_ptr != NULL
1079 && *q->sym_ptr_ptr != NULL)
1081 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1082 if (sym_name == NULL || *sym_name == '\0')
1086 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1087 sym_name = bfd_get_section_name (abfd, sym_sec);
1088 if (sym_name == NULL || *sym_name == '\0')
1089 sym_name = "*unknown*";
1093 printf ("%s", sym_name);
1098 printf_vma (q->addend);
1121 /* Define a table of stab values and print-strings. We wish the initializer
1122 could be a direct-mapped table, but instead we build one the first
1125 void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
1126 char *strsect_name));
1128 /* Dump the stabs sections from an object file that has a section that
1129 uses Sun stabs encoding. It has to use some hooks into BFD because
1130 string table sections are not normally visible to BFD callers. */
1136 dump_section_stabs (abfd, ".stab", ".stabstr");
1137 dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
1138 dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
1139 dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1142 static struct internal_nlist *stabs;
1143 static bfd_size_type stab_size;
1145 static char *strtab;
1146 static bfd_size_type stabstr_size;
1148 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1149 and string table section STRSECT_NAME into `strtab'.
1150 If the section exists and was read, allocate the space and return true.
1151 Otherwise return false. */
1154 read_section_stabs (abfd, stabsect_name, strsect_name)
1156 char *stabsect_name;
1159 asection *stabsect, *stabstrsect;
1161 stabsect = bfd_get_section_by_name (abfd, stabsect_name);
1164 printf ("No %s section present\n\n", stabsect_name);
1168 stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
1169 if (0 == stabstrsect)
1171 fprintf (stderr, "%s: %s has no %s section\n", program_name,
1172 bfd_get_filename (abfd), strsect_name);
1176 stab_size = bfd_section_size (abfd, stabsect);
1177 stabstr_size = bfd_section_size (abfd, stabstrsect);
1179 stabs = (struct internal_nlist *) xmalloc (stab_size);
1180 strtab = (char *) xmalloc (stabstr_size);
1182 if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
1184 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1185 program_name, stabsect_name, bfd_get_filename (abfd),
1186 bfd_errmsg (bfd_get_error ()));
1192 if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
1195 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1196 program_name, strsect_name, bfd_get_filename (abfd),
1197 bfd_errmsg (bfd_get_error ()));
1206 #define SWAP_SYMBOL(symp, abfd) \
1208 (symp)->n_strx = bfd_h_get_32(abfd, \
1209 (unsigned char *)&(symp)->n_strx); \
1210 (symp)->n_desc = bfd_h_get_16 (abfd, \
1211 (unsigned char *)&(symp)->n_desc); \
1212 (symp)->n_value = bfd_h_get_32 (abfd, \
1213 (unsigned char *)&(symp)->n_value); \
1216 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1217 using string table section STRSECT_NAME (in `strtab'). */
1220 print_section_stabs (abfd, stabsect_name, strsect_name)
1222 char *stabsect_name;
1226 unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
1227 struct internal_nlist *stabp = stabs,
1228 *stabs_end = (struct internal_nlist *) (stab_size + (char *) stabs);
1230 printf ("Contents of %s section:\n\n", stabsect_name);
1231 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1233 /* Loop through all symbols and print them.
1235 We start the index at -1 because there is a dummy symbol on
1236 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1238 for (i = -1; stabp < stabs_end; stabp++, i++)
1242 SWAP_SYMBOL (stabp, abfd);
1243 printf ("\n%-6d ", i);
1244 /* Either print the stab name, or, if unnamed, print its number
1245 again (makes consistent formatting for tools like awk). */
1246 name = bfd_get_stab_name (stabp->n_type);
1248 printf ("%-6s", name);
1249 else if (stabp->n_type == N_UNDF)
1252 printf ("%-6d", stabp->n_type);
1253 printf (" %-6d %-6d ", stabp->n_other, stabp->n_desc);
1254 printf_vma (stabp->n_value);
1255 printf (" %-6lu", stabp->n_strx);
1257 /* Symbols with type == 0 (N_UNDF) specify the length of the
1258 string table associated with this file. We use that info
1259 to know how to relocate the *next* file's string table indices. */
1261 if (stabp->n_type == N_UNDF)
1263 file_string_table_offset = next_file_string_table_offset;
1264 next_file_string_table_offset += stabp->n_value;
1268 /* Using the (possibly updated) string table offset, print the
1269 string (if any) associated with this symbol. */
1271 if ((stabp->n_strx + file_string_table_offset) < stabstr_size)
1272 printf (" %s", &strtab[stabp->n_strx + file_string_table_offset]);
1281 dump_section_stabs (abfd, stabsect_name, strsect_name)
1283 char *stabsect_name;
1288 /* Check for section names for which stabsect_name is a prefix, to
1289 handle .stab0, etc. */
1290 for (s = abfd->sections;
1294 if (strncmp (stabsect_name, s->name, strlen (stabsect_name)) == 0
1295 && strncmp (strsect_name, s->name, strlen (strsect_name)) != 0)
1297 if (read_section_stabs (abfd, s->name, strsect_name))
1299 print_section_stabs (abfd, s->name, strsect_name);
1308 dump_bfd_header (abfd)
1313 printf ("architecture: %s, ",
1314 bfd_printable_arch_mach (bfd_get_arch (abfd),
1315 bfd_get_mach (abfd)));
1316 printf ("flags 0x%08x:\n", abfd->flags);
1318 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1319 PF (HAS_RELOC, "HAS_RELOC");
1320 PF (EXEC_P, "EXEC_P");
1321 PF (HAS_LINENO, "HAS_LINENO");
1322 PF (HAS_DEBUG, "HAS_DEBUG");
1323 PF (HAS_SYMS, "HAS_SYMS");
1324 PF (HAS_LOCALS, "HAS_LOCALS");
1325 PF (DYNAMIC, "DYNAMIC");
1326 PF (WP_TEXT, "WP_TEXT");
1327 PF (D_PAGED, "D_PAGED");
1328 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
1329 printf ("\nstart address 0x");
1330 printf_vma (abfd->start_address);
1334 dump_bfd_private_header (abfd)
1337 bfd_print_private_bfd_data (abfd, stdout);
1345 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
1347 bfd_nonfatal (bfd_get_filename (abfd));
1348 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1350 list_matching_formats (matching);
1356 printf ("\n%s: file format %s\n", bfd_get_filename (abfd),
1359 print_arelt_descr (stdout, abfd, true);
1360 if (dump_file_header)
1361 dump_bfd_header (abfd);
1362 if (dump_private_headers)
1363 dump_bfd_private_header (abfd);
1365 if (dump_section_headers)
1366 dump_headers (abfd);
1367 if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
1369 syms = slurp_symtab (abfd);
1371 if (dump_dynamic_symtab || dump_dynamic_reloc_info)
1373 dynsyms = slurp_dynamic_symtab (abfd);
1376 dump_symbols (abfd, false);
1377 if (dump_dynamic_symtab)
1378 dump_symbols (abfd, true);
1379 if (dump_stab_section_info)
1381 if (dump_reloc_info && ! disassemble)
1383 if (dump_dynamic_reloc_info)
1384 dump_dynamic_relocs (abfd);
1385 if (dump_section_contents)
1388 disassemble_data (abfd);
1393 dhandle = read_debugging_info (abfd, syms, symcount);
1394 if (dhandle != NULL)
1396 if (! print_debugging_info (stdout, dhandle))
1397 fprintf (stderr, "%s: printing debugging information failed\n",
1398 bfd_get_filename (abfd));
1414 display_file (filename, target)
1418 bfd *file, *arfile = (bfd *) NULL;
1420 file = bfd_openr (filename, target);
1423 bfd_nonfatal (filename);
1427 if (bfd_check_format (file, bfd_archive) == true)
1429 bfd *last_arfile = NULL;
1431 printf ("In archive %s:\n", bfd_get_filename (file));
1434 bfd_set_error (bfd_error_no_error);
1436 arfile = bfd_openr_next_archived_file (file, arfile);
1439 if (bfd_get_error () != bfd_error_no_more_archived_files)
1441 bfd_nonfatal (bfd_get_filename (file));
1446 display_bfd (arfile);
1448 if (last_arfile != NULL)
1449 bfd_close (last_arfile);
1450 last_arfile = arfile;
1453 if (last_arfile != NULL)
1454 bfd_close (last_arfile);
1462 /* Actually display the various requested regions */
1470 bfd_size_type datasize = 0;
1472 bfd_size_type start, stop;
1474 for (section = abfd->sections; section != NULL; section =
1479 if (only == (char *) NULL ||
1480 strcmp (only, section->name) == 0)
1482 if (section->flags & SEC_HAS_CONTENTS)
1484 printf ("Contents of section %s:\n", section->name);
1486 if (bfd_section_size (abfd, section) == 0)
1488 data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
1489 datasize = bfd_section_size (abfd, section);
1492 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
1494 if (start_address == (bfd_vma) -1
1495 || start_address < section->vma)
1498 start = start_address - section->vma;
1499 if (stop_address == (bfd_vma) -1)
1500 stop = bfd_section_size (abfd, section);
1503 if (stop_address < section->vma)
1506 stop = stop_address - section->vma;
1507 if (stop > bfd_section_size (abfd, section))
1508 stop = bfd_section_size (abfd, section);
1510 for (i = start; i < stop; i += onaline)
1514 printf (" %04lx ", (unsigned long int) (i + section->vma));
1515 for (j = i; j < i + onaline; j++)
1518 printf ("%02x", (unsigned) (data[j]));
1526 for (j = i; j < i + onaline; j++)
1531 printf ("%c", isprint (data[j]) ? data[j] : '.');
1541 /* Should perhaps share code and display with nm? */
1543 dump_symbols (abfd, dynamic)
1557 printf ("DYNAMIC SYMBOL TABLE:\n");
1565 printf ("SYMBOL TABLE:\n");
1568 for (count = 0; count < max; count++)
1572 bfd *cur_bfd = bfd_asymbol_bfd(*current);
1575 bfd_print_symbol (cur_bfd,
1577 *current, bfd_print_symbol_all);
1595 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
1599 if (bfd_is_abs_section (a))
1601 if (bfd_is_und_section (a))
1603 if (bfd_is_com_section (a))
1608 if (strcmp (only, a->name))
1611 else if ((a->flags & SEC_RELOC) == 0)
1614 relsize = bfd_get_reloc_upper_bound (abfd, a);
1616 bfd_fatal (bfd_get_filename (abfd));
1618 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
1622 printf (" (none)\n\n");
1626 relpp = (arelent **) xmalloc (relsize);
1627 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
1629 bfd_fatal (bfd_get_filename (abfd));
1630 else if (relcount == 0)
1632 printf (" (none)\n\n");
1637 dump_reloc_set (abfd, relpp, relcount);
1646 dump_dynamic_relocs (abfd)
1653 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
1655 bfd_fatal (bfd_get_filename (abfd));
1657 printf ("DYNAMIC RELOCATION RECORDS");
1661 printf (" (none)\n\n");
1665 relpp = (arelent **) xmalloc (relsize);
1666 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
1668 bfd_fatal (bfd_get_filename (abfd));
1669 else if (relcount == 0)
1671 printf (" (none)\n\n");
1676 dump_reloc_set (abfd, relpp, relcount);
1684 dump_reloc_set (abfd, relpp, relcount)
1691 /* Get column headers lined up reasonably. */
1697 sprintf_vma (buf, (bfd_vma) -1);
1698 width = strlen (buf) - 7;
1700 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
1703 for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
1706 CONST char *sym_name;
1707 CONST char *section_name;
1709 if (start_address != (bfd_vma) -1
1710 && q->address < start_address)
1712 if (stop_address != (bfd_vma) -1
1713 && q->address > stop_address)
1716 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
1718 sym_name = (*(q->sym_ptr_ptr))->name;
1719 section_name = (*(q->sym_ptr_ptr))->section->name;
1724 section_name = NULL;
1728 printf_vma (q->address);
1729 printf (" %-16s %s",
1735 if (section_name == (CONST char *) NULL)
1736 section_name = "*unknown*";
1737 printf_vma (q->address);
1738 printf (" %-16s [%s]",
1745 printf_vma (q->addend);
1751 /* The length of the longest architecture name + 1. */
1752 #define LONGEST_ARCH sizeof("rs6000:6000")
1759 endian_string (endian)
1760 enum bfd_endian endian;
1762 if (endian == BFD_ENDIAN_BIG)
1763 return "big endian";
1764 else if (endian == BFD_ENDIAN_LITTLE)
1765 return "little endian";
1767 return "endianness unknown";
1770 /* List the targets that BFD is configured to support, each followed
1771 by its endianness and the architectures it supports. */
1774 display_target_list ()
1776 extern char *tmpnam ();
1777 extern bfd_target *bfd_target_vector[];
1778 char tmparg[L_tmpnam];
1782 dummy_name = tmpnam (tmparg);
1783 for (t = 0; bfd_target_vector[t]; t++)
1785 bfd_target *p = bfd_target_vector[t];
1786 bfd *abfd = bfd_openw (dummy_name, p->name);
1789 printf ("%s\n (header %s, data %s)\n", p->name,
1790 endian_string (p->header_byteorder),
1791 endian_string (p->byteorder));
1795 bfd_nonfatal (dummy_name);
1799 if (! bfd_set_format (abfd, bfd_object))
1801 if (bfd_get_error () != bfd_error_invalid_operation)
1802 bfd_nonfatal (p->name);
1806 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1807 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
1809 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
1811 unlink (dummy_name);
1814 /* Print a table showing which architectures are supported for entries
1815 FIRST through LAST-1 of bfd_target_vector (targets across,
1816 architectures down). */
1819 display_info_table (first, last)
1823 extern bfd_target *bfd_target_vector[];
1824 extern char *tmpnam ();
1825 char tmparg[L_tmpnam];
1829 /* Print heading of target names. */
1830 printf ("\n%*s", (int) LONGEST_ARCH, " ");
1831 for (t = first; t < last && bfd_target_vector[t]; t++)
1832 printf ("%s ", bfd_target_vector[t]->name);
1835 dummy_name = tmpnam (tmparg);
1836 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1837 if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
1839 printf ("%*s ", (int) LONGEST_ARCH - 1,
1840 bfd_printable_arch_mach (a, 0));
1841 for (t = first; t < last && bfd_target_vector[t]; t++)
1843 bfd_target *p = bfd_target_vector[t];
1845 bfd *abfd = bfd_openw (dummy_name, p->name);
1849 bfd_nonfatal (p->name);
1855 if (! bfd_set_format (abfd, bfd_object))
1857 if (bfd_get_error () != bfd_error_invalid_operation)
1858 bfd_nonfatal (p->name);
1865 if (! bfd_set_arch_mach (abfd, a, 0))
1870 printf ("%s ", p->name);
1873 int l = strlen (p->name);
1881 unlink (dummy_name);
1884 /* Print tables of all the target-architecture combinations that
1885 BFD has been configured to support. */
1888 display_target_tables ()
1891 extern bfd_target *bfd_target_vector[];
1893 extern char *getenv ();
1896 colum = getenv ("COLUMNS");
1898 columns = atoi (colum);
1903 while (bfd_target_vector[t] != NULL)
1907 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
1909 while (wid < columns && bfd_target_vector[t] != NULL)
1913 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
1914 if (newwid >= columns)
1919 display_info_table (oldt, t);
1926 printf ("BFD header file version %s\n", BFD_VERSION);
1927 display_target_list ();
1928 display_target_tables ();
1937 char *target = default_target;
1938 boolean seenflag = false;
1940 program_name = *argv;
1941 xmalloc_set_program_name (program_name);
1943 START_PROGRESS (program_name, 0);
1947 while ((c = getopt_long (argc, argv, "pib:m:VdDlfahrRtTxsSj:w", long_options,
1951 if (c != 'l' && c != OPTION_START_ADDRESS && c != OPTION_STOP_ADDRESS)
1956 break; /* we've been given a long option */
1964 with_line_numbers = 1;
1970 dump_file_header = true;
1973 formats_info = true;
1976 dump_private_headers = 1;
1979 dump_private_headers = 1;
1981 dump_reloc_info = 1;
1982 dump_file_header = true;
1984 dump_section_headers = 1;
1990 dump_dynamic_symtab = 1;
1996 disassemble = disassemble_all = true;
2000 with_source_code = true;
2003 dump_section_contents = 1;
2006 dump_reloc_info = 1;
2009 dump_dynamic_reloc_info = 1;
2015 dump_section_headers = 1;
2025 case OPTION_START_ADDRESS:
2026 start_address = parse_vma (optarg, "--start-address");
2028 case OPTION_STOP_ADDRESS:
2029 stop_address = parse_vma (optarg, "--stop-address");
2038 printf ("GNU %s version %s\n", program_name, program_version);
2042 if (seenflag == false)
2052 display_file ("a.out", target);
2054 for (; optind < argc;)
2055 display_file (argv[optind++], target);
2058 END_PROGRESS (program_name);