1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 1991, 1992, 1993, 1994 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 /* Internal headers for the ELF .stab-dump code - sorry. */
31 #define BYTES_IN_WORD 32
32 #include "aout/aout64.h"
34 #ifdef NEED_DECLARATION_FPRINTF
35 /* This is needed by INIT_DISASSEMBLE_INFO. */
36 extern int fprintf ();
39 char *default_target = NULL; /* default at runtime */
41 extern char *program_version;
43 int show_version = 0; /* show the version number */
44 int dump_section_contents; /* -s */
45 int dump_section_headers; /* -h */
46 boolean dump_file_header; /* -f */
47 int dump_symtab; /* -t */
48 int dump_dynamic_symtab; /* -T */
49 int dump_reloc_info; /* -r */
50 int dump_dynamic_reloc_info; /* -R */
51 int dump_ar_hdrs; /* -a */
52 int dump_private_headers; /* -p */
53 int with_line_numbers; /* -l */
54 boolean with_source_code; /* -S */
55 int dump_stab_section_info; /* --stabs */
56 boolean disassemble; /* -d */
57 boolean disassemble_all; /* -D */
58 boolean formats_info; /* -i */
59 char *only; /* -j secname */
60 int wide_output; /* -w */
61 bfd_vma start_address = (bfd_vma) -1; /* --start-address */
62 bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
63 int dump_debugging; /* --debugging */
65 /* Extra info to pass to the disassembler address printing function. */
66 struct objdump_disasm_info {
72 /* Architecture to disassemble for, or default if NULL. */
73 char *machine = (char *) NULL;
75 /* The symbol table. */
78 /* Number of symbols in `syms'. */
81 /* The sorted symbol table. */
82 asymbol **sorted_syms;
84 /* Number of symbols in `sorted_syms'. */
85 long sorted_symcount = 0;
87 /* The dynamic symbol table. */
90 /* Number of symbols in `dynsyms'. */
93 /* Forward declarations. */
96 display_file PARAMS ((char *filename, char *target));
99 dump_data PARAMS ((bfd *abfd));
102 dump_relocs PARAMS ((bfd *abfd));
105 dump_dynamic_relocs PARAMS ((bfd * abfd));
108 dump_reloc_set PARAMS ((bfd *, arelent **, long));
111 dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
114 display_bfd PARAMS ((bfd *abfd));
117 objdump_print_value PARAMS ((bfd_vma, FILE *));
120 objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *));
123 show_line PARAMS ((bfd *, asection *, bfd_vma));
126 endian_string PARAMS ((enum bfd_endian));
129 usage (stream, status)
134 Usage: %s [-ahifdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
135 [--archive-headers] [--target=bfdname] [--debugging] [--disassemble]\n\
136 [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
137 [--info] [--section=section-name] [--line-numbers] [--source]\n",
140 [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
141 [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
142 [--wide] [--version] [--help] [--private-headers]\n\
143 [--start-address=addr] [--stop-address=addr] objfile...\n\
144 at least one option besides -l (--line-numbers) must be given\n");
145 list_supported_targets (program_name, stream);
149 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
151 #define OPTION_START_ADDRESS (150)
152 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
154 static struct option long_options[]=
156 {"all-headers", no_argument, NULL, 'x'},
157 {"private-headers", no_argument, NULL, 'p'},
158 {"architecture", required_argument, NULL, 'm'},
159 {"archive-headers", no_argument, NULL, 'a'},
160 {"debugging", no_argument, &dump_debugging, 1},
161 {"disassemble", no_argument, NULL, 'd'},
162 {"disassemble-all", no_argument, NULL, 'D'},
163 {"dynamic-reloc", no_argument, NULL, 'R'},
164 {"dynamic-syms", no_argument, NULL, 'T'},
165 {"file-headers", no_argument, NULL, 'f'},
166 {"full-contents", no_argument, NULL, 's'},
167 {"headers", no_argument, NULL, 'h'},
168 {"help", no_argument, NULL, 'H'},
169 {"info", no_argument, NULL, 'i'},
170 {"line-numbers", no_argument, NULL, 'l'},
171 {"reloc", no_argument, NULL, 'r'},
172 {"section", required_argument, NULL, 'j'},
173 {"section-headers", no_argument, NULL, 'h'},
174 {"source", no_argument, NULL, 'S'},
175 {"stabs", no_argument, &dump_stab_section_info, 1},
176 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
177 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
178 {"syms", no_argument, NULL, 't'},
179 {"target", required_argument, NULL, 'b'},
180 {"version", no_argument, &show_version, 1},
181 {"wide", no_argument, &wide_output, 'w'},
182 {0, no_argument, 0, 0}
186 dump_section_header (abfd, section, ignored)
194 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
197 printf ("SECTION %d [%s]\t: size %08x",
200 (unsigned) bfd_get_section_size_before_reloc (section));
202 printf_vma (section->vma);
204 printf_vma (section->lma);
205 printf (" align 2**%u%s ",
206 section->alignment_power, (wide_output) ? "" : "\n");
207 PF (SEC_ALLOC, "ALLOC");
208 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
209 PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
210 PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA");
211 PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS");
212 PF (SEC_LOAD, "LOAD");
213 PF (SEC_RELOC, "RELOC");
215 PF (SEC_BALIGN, "BALIGN");
217 PF (SEC_READONLY, "READONLY");
218 PF (SEC_CODE, "CODE");
219 PF (SEC_DATA, "DATA");
221 PF (SEC_DEBUGGING, "DEBUGGING");
222 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
231 bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
238 asymbol **sy = (asymbol **) NULL;
241 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
243 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
248 storage = bfd_get_symtab_upper_bound (abfd);
250 bfd_fatal (bfd_get_filename (abfd));
254 sy = (asymbol **) xmalloc (storage);
256 symcount = bfd_canonicalize_symtab (abfd, sy);
258 bfd_fatal (bfd_get_filename (abfd));
260 fprintf (stderr, "%s: %s: No symbols\n",
261 program_name, bfd_get_filename (abfd));
265 /* Read in the dynamic symbols. */
268 slurp_dynamic_symtab (abfd)
271 asymbol **sy = (asymbol **) NULL;
274 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
277 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
279 fprintf (stderr, "%s: %s: not a dynamic object\n",
280 program_name, bfd_get_filename (abfd));
285 bfd_fatal (bfd_get_filename (abfd));
290 sy = (asymbol **) xmalloc (storage);
292 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
294 bfd_fatal (bfd_get_filename (abfd));
295 if (dynsymcount == 0)
296 fprintf (stderr, "%s: %s: No dynamic symbols\n",
297 program_name, bfd_get_filename (abfd));
301 /* Filter out (in place) symbols that are useless for disassembly.
302 COUNT is the number of elements in SYMBOLS.
303 Return the number of useful symbols. */
306 remove_useless_symbols (symbols, count)
310 register asymbol **in_ptr = symbols, **out_ptr = symbols;
314 asymbol *sym = *in_ptr++;
316 if (sym->name == NULL || sym->name[0] == '\0')
318 if (sym->flags & (BSF_DEBUGGING))
320 if (bfd_is_und_section (sym->section)
321 || bfd_is_com_section (sym->section))
326 return out_ptr - symbols;
329 /* Sort symbols into value order. */
332 compare_symbols (ap, bp)
336 const asymbol *a = *(const asymbol **)ap;
337 const asymbol *b = *(const asymbol **)bp;
341 flagword aflags, bflags;
343 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
345 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
348 if (a->section > b->section)
350 else if (a->section < b->section)
353 an = bfd_asymbol_name (a);
354 bn = bfd_asymbol_name (b);
358 /* The symbols gnu_compiled and gcc2_compiled convey no real
359 information, so put them after other symbols with the same value. */
361 af = (strstr (an, "gnu_compiled") != NULL
362 || strstr (an, "gcc2_compiled") != NULL);
363 bf = (strstr (bn, "gnu_compiled") != NULL
364 || strstr (bn, "gcc2_compiled") != NULL);
371 /* We use a heuristic for the file name, to try to sort it after
372 more useful symbols. It may not work on non Unix systems, but it
373 doesn't really matter; the only difference is precisely which
374 symbol names get printed. */
376 #define file_symbol(s, sn, snl) \
377 (((s)->flags & BSF_FILE) != 0 \
378 || ((sn)[(snl) - 2] == '.' \
379 && ((sn)[(snl) - 1] == 'o' \
380 || (sn)[(snl) - 1] == 'a')))
382 af = file_symbol (a, an, anl);
383 bf = file_symbol (b, bn, bnl);
390 /* Finally, try to sort global symbols before local symbols before
391 debugging symbols. */
396 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
398 if ((aflags & BSF_DEBUGGING) != 0)
403 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
405 if ((aflags & BSF_LOCAL) != 0)
414 /* Sort relocs into address order. */
417 compare_relocs (ap, bp)
421 const arelent *a = *(const arelent **)ap;
422 const arelent *b = *(const arelent **)bp;
424 if (a->address > b->address)
426 else if (a->address < b->address)
429 /* So that associated relocations tied to the same address show up
430 in the correct order, we don't do any further sorting. */
439 /* Print VMA to STREAM with no leading zeroes. */
442 objdump_print_value (vma, stream)
449 sprintf_vma (buf, vma);
450 for (p = buf; *p == '0'; ++p)
452 fprintf (stream, "%s", p);
455 /* Print VMA symbolically to INFO if possible. */
458 objdump_print_address (vma, info)
460 struct disassemble_info *info;
462 /* @@ Would it speed things up to cache the last two symbols returned,
463 and maybe their address ranges? For many processors, only one memory
464 operand can be present at a time, so the 2-entry cache wouldn't be
465 constantly churned by code doing heavy memory accesses. */
467 /* Indices in `sorted_syms'. */
469 long max = sorted_symcount;
472 fprintf_vma (info->stream, vma);
474 if (sorted_symcount < 1)
477 /* Perform a binary search looking for the closest symbol to the
478 required value. We are searching the range (min, max]. */
479 while (min + 1 < max)
483 thisplace = (max + min) / 2;
484 sym = sorted_syms[thisplace];
486 if (bfd_asymbol_value (sym) > vma)
488 else if (bfd_asymbol_value (sym) < vma)
497 /* The symbol we want is now in min, the low end of the range we
498 were searching. If there are several symbols with the same
499 value, we want the first one. */
502 && (bfd_asymbol_value (sorted_syms[thisplace])
503 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
507 /* If the file is relocateable, and the symbol could be from this
508 section, prefer a symbol from this section over symbols from
509 others, even if the other symbol's value might be closer.
511 Note that this may be wrong for some symbol references if the
512 sections have overlapping memory ranges, but in that case there's
513 no way to tell what's desired without looking at the relocation
515 struct objdump_disasm_info *aux;
518 aux = (struct objdump_disasm_info *) info->application_data;
519 if (sorted_syms[thisplace]->section != aux->sec
521 || ((aux->abfd->flags & HAS_RELOC) != 0
522 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
523 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
524 + bfd_section_size (aux->abfd, aux->sec)))))
526 for (i = thisplace + 1; i < sorted_symcount; i++)
528 if (bfd_asymbol_value (sorted_syms[i])
529 != bfd_asymbol_value (sorted_syms[thisplace]))
535 if (sorted_syms[i]->section == aux->sec
537 || sorted_syms[i - 1]->section != aux->sec
538 || (bfd_asymbol_value (sorted_syms[i])
539 != bfd_asymbol_value (sorted_syms[i - 1]))))
546 if (sorted_syms[thisplace]->section != aux->sec)
548 /* We didn't find a good symbol with a smaller value.
549 Look for one with a larger value. */
550 for (i = thisplace + 1; i < sorted_symcount; i++)
552 if (sorted_syms[i]->section == aux->sec)
560 if (sorted_syms[thisplace]->section != aux->sec
562 || ((aux->abfd->flags & HAS_RELOC) != 0
563 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
564 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
565 + bfd_section_size (aux->abfd, aux->sec)))))
569 fprintf (info->stream, " <%s",
570 bfd_get_section_name (aux->abfd, aux->sec));
571 secaddr = bfd_get_section_vma (aux->abfd, aux->sec);
574 fprintf (info->stream, "-");
575 objdump_print_value (secaddr - vma, info->stream);
577 else if (vma > secaddr)
579 fprintf (info->stream, "+");
580 objdump_print_value (vma - secaddr, info->stream);
582 fprintf (info->stream, ">");
588 fprintf (info->stream, " <%s", sorted_syms[thisplace]->name);
589 if (bfd_asymbol_value (sorted_syms[thisplace]) > vma)
591 fprintf (info->stream, "-");
592 objdump_print_value (bfd_asymbol_value (sorted_syms[thisplace]) - vma,
595 else if (vma > bfd_asymbol_value (sorted_syms[thisplace]))
597 fprintf (info->stream, "+");
598 objdump_print_value (vma - bfd_asymbol_value (sorted_syms[thisplace]),
601 fprintf (info->stream, ">");
604 /* Hold the last function name and the last line number we displayed
607 static char *prev_functionname;
608 static unsigned int prev_line;
610 /* We keep a list of all files that we have seen when doing a
611 dissassembly with source, so that we know how much of the file to
612 display. This can be important for inlined functions. */
614 struct print_file_list
616 struct print_file_list *next;
622 static struct print_file_list *print_files;
624 /* The number of preceding context lines to show when we start
625 displaying a file for the first time. */
627 #define SHOW_PRECEDING_CONTEXT_LINES (5)
629 /* Skip ahead to a given line in a file, optionally printing each
633 skip_to_line PARAMS ((struct print_file_list *, unsigned int, boolean));
636 skip_to_line (p, line, show)
637 struct print_file_list *p;
641 while (p->line < line)
645 if (fgets (buf, sizeof buf, p->f) == NULL)
655 if (strchr (buf, '\n') != NULL)
660 /* Show the line number, or the source line, in a dissassembly
664 show_line (abfd, section, off)
669 CONST char *filename;
670 CONST char *functionname;
673 if (! with_line_numbers && ! with_source_code)
676 if (! bfd_find_nearest_line (abfd, section, syms, off, &filename,
677 &functionname, &line))
680 if (filename != NULL && *filename == '\0')
682 if (functionname != NULL && *functionname == '\0')
685 if (with_line_numbers)
687 if (functionname != NULL
688 && (prev_functionname == NULL
689 || strcmp (functionname, prev_functionname) != 0))
690 printf ("%s():\n", functionname);
691 if (line > 0 && line != prev_line)
692 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
699 struct print_file_list **pp, *p;
701 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
702 if (strcmp ((*pp)->filename, filename) == 0)
708 if (p != print_files)
712 /* We have reencountered a file name which we saw
713 earlier. This implies that either we are dumping out
714 code from an included file, or the same file was
715 linked in more than once. There are two common cases
716 of an included file: inline functions in a header
717 file, and a bison or flex skeleton file. In the
718 former case we want to just start printing (but we
719 back up a few lines to give context); in the latter
720 case we want to continue from where we left off. I
721 can't think of a good way to distinguish the cases,
722 so I used a heuristic based on the file name. */
723 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
727 l = line - SHOW_PRECEDING_CONTEXT_LINES;
734 p->f = fopen (p->filename, "r");
738 skip_to_line (p, l, false);
740 if (print_files->f != NULL)
742 fclose (print_files->f);
743 print_files->f = NULL;
749 skip_to_line (p, line, true);
751 p->next = print_files;
759 f = fopen (filename, "r");
764 p = ((struct print_file_list *)
765 xmalloc (sizeof (struct print_file_list)));
766 p->filename = xmalloc (strlen (filename) + 1);
767 strcpy (p->filename, filename);
771 if (print_files != NULL && print_files->f != NULL)
773 fclose (print_files->f);
774 print_files->f = NULL;
776 p->next = print_files;
779 l = line - SHOW_PRECEDING_CONTEXT_LINES;
782 skip_to_line (p, l, false);
784 skip_to_line (p, line, true);
789 if (functionname != NULL
790 && (prev_functionname == NULL
791 || strcmp (functionname, prev_functionname) != 0))
793 if (prev_functionname != NULL)
794 free (prev_functionname);
795 prev_functionname = xmalloc (strlen (functionname) + 1);
796 strcpy (prev_functionname, functionname);
799 if (line > 0 && line != prev_line)
804 disassemble_data (abfd)
808 disassembler_ftype disassemble_fn = 0; /* New style */
809 struct disassemble_info disasm_info;
810 struct objdump_disasm_info aux;
812 boolean done_dot = false;
815 prev_functionname = NULL;
818 /* We make a copy of syms to sort. We don't want to sort syms
819 because that will screw up the relocs. */
820 sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
821 memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
823 sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
825 /* Sort the symbols into section and symbol order */
826 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
828 INIT_DISASSEMBLE_INFO(disasm_info, stdout);
829 disasm_info.application_data = (PTR) &aux;
831 disasm_info.print_address_func = objdump_print_address;
833 if (machine != (char *) NULL)
835 const bfd_arch_info_type *info = bfd_scan_arch (machine);
838 fprintf (stderr, "%s: Can't use supplied machine %s\n",
843 abfd->arch_info = info;
846 disassemble_fn = disassembler (abfd);
849 fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
851 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
855 for (section = abfd->sections;
856 section != (asection *) NULL;
857 section = section->next)
859 bfd_byte *data = NULL;
860 bfd_size_type datasize = 0;
861 arelent **relbuf = NULL;
862 arelent **relpp = NULL;
863 arelent **relppend = NULL;
866 if ((section->flags & SEC_LOAD) == 0
867 || (! disassemble_all
869 && (section->flags & SEC_CODE) == 0))
871 if (only != (char *) NULL && strcmp (only, section->name) != 0)
875 && (section->flags & SEC_RELOC) != 0)
879 relsize = bfd_get_reloc_upper_bound (abfd, section);
881 bfd_fatal (bfd_get_filename (abfd));
887 relbuf = (arelent **) xmalloc (relsize);
888 relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
890 bfd_fatal (bfd_get_filename (abfd));
892 /* Sort the relocs by address. */
893 qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
896 relppend = relpp + relcount;
900 printf ("Disassembly of section %s:\n", section->name);
902 datasize = bfd_get_section_size_before_reloc (section);
906 data = (bfd_byte *) xmalloc ((size_t) datasize);
908 bfd_get_section_contents (abfd, section, data, 0, datasize);
911 disasm_info.buffer = data;
912 disasm_info.buffer_vma = section->vma;
913 disasm_info.buffer_length = datasize;
914 if (start_address == (bfd_vma) -1
915 || start_address < disasm_info.buffer_vma)
918 i = start_address - disasm_info.buffer_vma;
919 if (stop_address == (bfd_vma) -1)
923 if (stop_address < disasm_info.buffer_vma)
926 stop = stop_address - disasm_info.buffer_vma;
927 if (stop > disasm_info.buffer_length)
928 stop = disasm_info.buffer_length;
933 boolean need_nl = false;
935 if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 &&
938 if (done_dot == false)
948 if (with_line_numbers || with_source_code)
949 show_line (abfd, section, i);
950 aux.require_sec = true;
951 objdump_print_address (section->vma + i, &disasm_info);
952 aux.require_sec = false;
955 bytes = (*disassemble_fn) (section->vma + i, &disasm_info);
966 && (section->flags & SEC_RELOC) != 0)
968 while (relpp < relppend
969 && ((*relpp)->address >= (bfd_vma) i
970 && (*relpp)->address < (bfd_vma) i + bytes))
973 const char *sym_name;
977 printf ("\t\tRELOC: ");
979 printf_vma (section->vma + q->address);
981 printf (" %s ", q->howto->name);
983 if (q->sym_ptr_ptr != NULL
984 && *q->sym_ptr_ptr != NULL)
986 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
987 if (sym_name == NULL || *sym_name == '\0')
991 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
992 sym_name = bfd_get_section_name (abfd, sym_sec);
993 if (sym_name == NULL || *sym_name == '\0')
994 sym_name = "*unknown*";
998 printf ("%s", sym_name);
1003 printf_vma (q->addend);
1026 /* Define a table of stab values and print-strings. We wish the initializer
1027 could be a direct-mapped table, but instead we build one the first
1037 struct stab_print stab_print[] = {
1038 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
1039 #include "aout/stab.def"
1040 #undef __define_stab
1044 void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
1045 char *strsect_name));
1047 /* Dump the stabs sections from an object file that has a section that
1048 uses Sun stabs encoding. It has to use some hooks into BFD because
1049 string table sections are not normally visible to BFD callers. */
1055 /* Allocate and initialize stab name array if first time. */
1056 if (stab_name == NULL)
1060 stab_name = (char **) xmalloc (256 * sizeof(char *));
1061 /* Clear the array. */
1062 for (i = 0; i < 256; i++)
1063 stab_name[i] = NULL;
1064 /* Fill in the defined stabs. */
1065 for (i = 0; *stab_print[i].string; i++)
1066 stab_name[stab_print[i].value] = stab_print[i].string;
1069 dump_section_stabs (abfd, ".stab", ".stabstr");
1070 dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
1071 dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
1072 dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1075 static struct internal_nlist *stabs;
1076 static bfd_size_type stab_size;
1078 static char *strtab;
1079 static bfd_size_type stabstr_size;
1081 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1082 and string table section STRSECT_NAME into `strtab'.
1083 If the section exists and was read, allocate the space and return true.
1084 Otherwise return false. */
1087 read_section_stabs (abfd, stabsect_name, strsect_name)
1089 char *stabsect_name;
1092 asection *stabsect, *stabstrsect;
1094 stabsect = bfd_get_section_by_name (abfd, stabsect_name);
1097 printf ("No %s section present\n\n", stabsect_name);
1101 stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
1102 if (0 == stabstrsect)
1104 fprintf (stderr, "%s: %s has no %s section\n", program_name,
1105 bfd_get_filename (abfd), strsect_name);
1109 stab_size = bfd_section_size (abfd, stabsect);
1110 stabstr_size = bfd_section_size (abfd, stabstrsect);
1112 stabs = (struct internal_nlist *) xmalloc (stab_size);
1113 strtab = (char *) xmalloc (stabstr_size);
1115 if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
1117 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1118 program_name, stabsect_name, bfd_get_filename (abfd),
1119 bfd_errmsg (bfd_get_error ()));
1125 if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
1128 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1129 program_name, strsect_name, bfd_get_filename (abfd),
1130 bfd_errmsg (bfd_get_error ()));
1139 #define SWAP_SYMBOL(symp, abfd) \
1141 (symp)->n_strx = bfd_h_get_32(abfd, \
1142 (unsigned char *)&(symp)->n_strx); \
1143 (symp)->n_desc = bfd_h_get_16 (abfd, \
1144 (unsigned char *)&(symp)->n_desc); \
1145 (symp)->n_value = bfd_h_get_32 (abfd, \
1146 (unsigned char *)&(symp)->n_value); \
1149 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1150 using string table section STRSECT_NAME (in `strtab'). */
1153 print_section_stabs (abfd, stabsect_name, strsect_name)
1155 char *stabsect_name;
1159 unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
1160 struct internal_nlist *stabp = stabs,
1161 *stabs_end = (struct internal_nlist *) (stab_size + (char *) stabs);
1163 printf ("Contents of %s section:\n\n", stabsect_name);
1164 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1166 /* Loop through all symbols and print them.
1168 We start the index at -1 because there is a dummy symbol on
1169 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1171 for (i = -1; stabp < stabs_end; stabp++, i++)
1173 SWAP_SYMBOL (stabp, abfd);
1174 printf ("\n%-6d ", i);
1175 /* Either print the stab name, or, if unnamed, print its number
1176 again (makes consistent formatting for tools like awk). */
1177 if (stab_name[stabp->n_type])
1178 printf ("%-6s", stab_name[stabp->n_type]);
1179 else if (stabp->n_type == N_UNDF)
1182 printf ("%-6d", stabp->n_type);
1183 printf (" %-6d %-6d ", stabp->n_other, stabp->n_desc);
1184 printf_vma (stabp->n_value);
1185 printf (" %-6lu", stabp->n_strx);
1187 /* Symbols with type == 0 (N_UNDF) specify the length of the
1188 string table associated with this file. We use that info
1189 to know how to relocate the *next* file's string table indices. */
1191 if (stabp->n_type == N_UNDF)
1193 file_string_table_offset = next_file_string_table_offset;
1194 next_file_string_table_offset += stabp->n_value;
1198 /* Using the (possibly updated) string table offset, print the
1199 string (if any) associated with this symbol. */
1201 if ((stabp->n_strx + file_string_table_offset) < stabstr_size)
1202 printf (" %s", &strtab[stabp->n_strx + file_string_table_offset]);
1211 dump_section_stabs (abfd, stabsect_name, strsect_name)
1213 char *stabsect_name;
1218 /* Check for section names for which stabsect_name is a prefix, to
1219 handle .stab0, etc. */
1220 for (s = abfd->sections;
1224 if (strncmp (stabsect_name, s->name, strlen (stabsect_name)) == 0
1225 && strncmp (strsect_name, s->name, strlen (strsect_name)) != 0)
1227 if (read_section_stabs (abfd, s->name, strsect_name))
1229 print_section_stabs (abfd, s->name, strsect_name);
1238 dump_bfd_header (abfd)
1243 printf ("architecture: %s, ",
1244 bfd_printable_arch_mach (bfd_get_arch (abfd),
1245 bfd_get_mach (abfd)));
1246 printf ("flags 0x%08x:\n", abfd->flags);
1248 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1249 PF (HAS_RELOC, "HAS_RELOC");
1250 PF (EXEC_P, "EXEC_P");
1251 PF (HAS_LINENO, "HAS_LINENO");
1252 PF (HAS_DEBUG, "HAS_DEBUG");
1253 PF (HAS_SYMS, "HAS_SYMS");
1254 PF (HAS_LOCALS, "HAS_LOCALS");
1255 PF (DYNAMIC, "DYNAMIC");
1256 PF (WP_TEXT, "WP_TEXT");
1257 PF (D_PAGED, "D_PAGED");
1258 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
1259 printf ("\nstart address 0x");
1260 printf_vma (abfd->start_address);
1264 dump_bfd_private_header (abfd)
1267 bfd_print_private_bfd_data (abfd, stdout);
1275 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
1277 bfd_nonfatal (bfd_get_filename (abfd));
1278 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1280 list_matching_formats (matching);
1286 printf ("\n%s: file format %s\n", bfd_get_filename (abfd),
1289 print_arelt_descr (stdout, abfd, true);
1290 if (dump_file_header)
1291 dump_bfd_header (abfd);
1292 if (dump_private_headers)
1293 dump_bfd_private_header (abfd);
1295 if (dump_section_headers)
1296 dump_headers (abfd);
1297 if (dump_symtab || dump_reloc_info || disassemble)
1299 syms = slurp_symtab (abfd);
1301 if (dump_dynamic_symtab || dump_dynamic_reloc_info)
1303 dynsyms = slurp_dynamic_symtab (abfd);
1306 dump_symbols (abfd, false);
1307 if (dump_dynamic_symtab)
1308 dump_symbols (abfd, true);
1309 if (dump_stab_section_info)
1311 if (dump_reloc_info && ! disassemble)
1313 if (dump_dynamic_reloc_info)
1314 dump_dynamic_relocs (abfd);
1315 if (dump_section_contents)
1318 disassemble_data (abfd);
1323 dhandle = read_debugging_info (abfd);
1324 if (dhandle != NULL)
1326 if (! print_debugging_info (stdout, dhandle))
1327 fprintf (stderr, "%s: printing debugging information failed\n",
1328 bfd_get_filename (abfd));
1344 display_file (filename, target)
1348 bfd *file, *arfile = (bfd *) NULL;
1350 file = bfd_openr (filename, target);
1353 bfd_nonfatal (filename);
1357 if (bfd_check_format (file, bfd_archive) == true)
1359 bfd *last_arfile = NULL;
1361 printf ("In archive %s:\n", bfd_get_filename (file));
1364 bfd_set_error (bfd_error_no_error);
1366 arfile = bfd_openr_next_archived_file (file, arfile);
1369 if (bfd_get_error () != bfd_error_no_more_archived_files)
1371 bfd_nonfatal (bfd_get_filename (file));
1376 display_bfd (arfile);
1378 if (last_arfile != NULL)
1379 bfd_close (last_arfile);
1380 last_arfile = arfile;
1383 if (last_arfile != NULL)
1384 bfd_close (last_arfile);
1392 /* Actually display the various requested regions */
1400 bfd_size_type datasize = 0;
1402 bfd_size_type start, stop;
1404 for (section = abfd->sections; section != NULL; section =
1409 if (only == (char *) NULL ||
1410 strcmp (only, section->name) == 0)
1412 if (section->flags & SEC_HAS_CONTENTS)
1414 printf ("Contents of section %s:\n", section->name);
1416 if (bfd_section_size (abfd, section) == 0)
1418 data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
1419 datasize = bfd_section_size (abfd, section);
1422 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
1424 if (start_address == (bfd_vma) -1
1425 || start_address < section->vma)
1428 start = start_address - section->vma;
1429 if (stop_address == (bfd_vma) -1)
1430 stop = bfd_section_size (abfd, section);
1433 if (stop_address < section->vma)
1436 stop = stop_address - section->vma;
1437 if (stop > bfd_section_size (abfd, section))
1438 stop = bfd_section_size (abfd, section);
1440 for (i = start; i < stop; i += onaline)
1444 printf (" %04lx ", (unsigned long int) (i + section->vma));
1445 for (j = i; j < i + onaline; j++)
1448 printf ("%02x", (unsigned) (data[j]));
1456 for (j = i; j < i + onaline; j++)
1461 printf ("%c", isprint (data[j]) ? data[j] : '.');
1471 /* Should perhaps share code and display with nm? */
1473 dump_symbols (abfd, dynamic)
1487 printf ("DYNAMIC SYMBOL TABLE:\n");
1495 printf ("SYMBOL TABLE:\n");
1498 for (count = 0; count < max; count++)
1502 bfd *cur_bfd = bfd_asymbol_bfd(*current);
1505 bfd_print_symbol (cur_bfd,
1507 *current, bfd_print_symbol_all);
1525 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
1529 if (bfd_is_abs_section (a))
1531 if (bfd_is_und_section (a))
1533 if (bfd_is_com_section (a))
1538 if (strcmp (only, a->name))
1541 else if ((a->flags & SEC_RELOC) == 0)
1544 relsize = bfd_get_reloc_upper_bound (abfd, a);
1546 bfd_fatal (bfd_get_filename (abfd));
1548 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
1552 printf (" (none)\n\n");
1556 relpp = (arelent **) xmalloc (relsize);
1557 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
1559 bfd_fatal (bfd_get_filename (abfd));
1560 else if (relcount == 0)
1562 printf (" (none)\n\n");
1567 dump_reloc_set (abfd, relpp, relcount);
1576 dump_dynamic_relocs (abfd)
1583 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
1585 bfd_fatal (bfd_get_filename (abfd));
1587 printf ("DYNAMIC RELOCATION RECORDS");
1591 printf (" (none)\n\n");
1595 relpp = (arelent **) xmalloc (relsize);
1596 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
1598 bfd_fatal (bfd_get_filename (abfd));
1599 else if (relcount == 0)
1601 printf (" (none)\n\n");
1606 dump_reloc_set (abfd, relpp, relcount);
1614 dump_reloc_set (abfd, relpp, relcount)
1621 /* Get column headers lined up reasonably. */
1627 sprintf_vma (buf, (bfd_vma) -1);
1628 width = strlen (buf) - 7;
1630 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
1633 for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
1636 CONST char *sym_name;
1637 CONST char *section_name;
1639 if (start_address != (bfd_vma) -1
1640 && q->address < start_address)
1642 if (stop_address != (bfd_vma) -1
1643 && q->address > stop_address)
1646 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
1648 sym_name = (*(q->sym_ptr_ptr))->name;
1649 section_name = (*(q->sym_ptr_ptr))->section->name;
1654 section_name = NULL;
1658 printf_vma (q->address);
1659 printf (" %-16s %s",
1665 if (section_name == (CONST char *) NULL)
1666 section_name = "*unknown*";
1667 printf_vma (q->address);
1668 printf (" %-16s [%s]",
1675 printf_vma (q->addend);
1681 /* The length of the longest architecture name + 1. */
1682 #define LONGEST_ARCH sizeof("rs6000:6000")
1689 endian_string (endian)
1690 enum bfd_endian endian;
1692 if (endian == BFD_ENDIAN_BIG)
1693 return "big endian";
1694 else if (endian == BFD_ENDIAN_LITTLE)
1695 return "little endian";
1697 return "endianness unknown";
1700 /* List the targets that BFD is configured to support, each followed
1701 by its endianness and the architectures it supports. */
1704 display_target_list ()
1706 extern char *tmpnam ();
1707 extern bfd_target *bfd_target_vector[];
1708 char tmparg[L_tmpnam];
1712 dummy_name = tmpnam (tmparg);
1713 for (t = 0; bfd_target_vector[t]; t++)
1715 bfd_target *p = bfd_target_vector[t];
1716 bfd *abfd = bfd_openw (dummy_name, p->name);
1719 printf ("%s\n (header %s, data %s)\n", p->name,
1720 endian_string (p->header_byteorder),
1721 endian_string (p->byteorder));
1725 bfd_nonfatal (dummy_name);
1729 if (! bfd_set_format (abfd, bfd_object))
1731 if (bfd_get_error () != bfd_error_invalid_operation)
1732 bfd_nonfatal (p->name);
1736 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1737 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
1739 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
1741 unlink (dummy_name);
1744 /* Print a table showing which architectures are supported for entries
1745 FIRST through LAST-1 of bfd_target_vector (targets across,
1746 architectures down). */
1749 display_info_table (first, last)
1753 extern bfd_target *bfd_target_vector[];
1754 extern char *tmpnam ();
1755 char tmparg[L_tmpnam];
1759 /* Print heading of target names. */
1760 printf ("\n%*s", (int) LONGEST_ARCH, " ");
1761 for (t = first; t < last && bfd_target_vector[t]; t++)
1762 printf ("%s ", bfd_target_vector[t]->name);
1765 dummy_name = tmpnam (tmparg);
1766 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1767 if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
1769 printf ("%*s ", (int) LONGEST_ARCH - 1,
1770 bfd_printable_arch_mach (a, 0));
1771 for (t = first; t < last && bfd_target_vector[t]; t++)
1773 bfd_target *p = bfd_target_vector[t];
1775 bfd *abfd = bfd_openw (dummy_name, p->name);
1779 bfd_nonfatal (p->name);
1785 if (! bfd_set_format (abfd, bfd_object))
1787 if (bfd_get_error () != bfd_error_invalid_operation)
1788 bfd_nonfatal (p->name);
1795 if (! bfd_set_arch_mach (abfd, a, 0))
1800 printf ("%s ", p->name);
1803 int l = strlen (p->name);
1811 unlink (dummy_name);
1814 /* Print tables of all the target-architecture combinations that
1815 BFD has been configured to support. */
1818 display_target_tables ()
1821 extern bfd_target *bfd_target_vector[];
1823 extern char *getenv ();
1826 colum = getenv ("COLUMNS");
1828 columns = atoi (colum);
1833 while (bfd_target_vector[t] != NULL)
1837 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
1839 while (wid < columns && bfd_target_vector[t] != NULL)
1843 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
1844 if (newwid >= columns)
1849 display_info_table (oldt, t);
1856 printf ("BFD header file version %s\n", BFD_VERSION);
1857 display_target_list ();
1858 display_target_tables ();
1867 char *target = default_target;
1868 boolean seenflag = false;
1870 program_name = *argv;
1871 xmalloc_set_program_name (program_name);
1873 START_PROGRESS (program_name, 0);
1877 while ((c = getopt_long (argc, argv, "pib:m:VdDlfahrRtTxsSj:w", long_options,
1881 if (c != 'l' && c != OPTION_START_ADDRESS && c != OPTION_STOP_ADDRESS)
1886 break; /* we've been given a long option */
1894 with_line_numbers = 1;
1900 dump_file_header = true;
1903 formats_info = true;
1906 dump_private_headers = 1;
1909 dump_private_headers = 1;
1911 dump_reloc_info = 1;
1912 dump_file_header = true;
1914 dump_section_headers = 1;
1920 dump_dynamic_symtab = 1;
1926 disassemble = disassemble_all = true;
1930 with_source_code = true;
1933 dump_section_contents = 1;
1936 dump_reloc_info = 1;
1939 dump_dynamic_reloc_info = 1;
1945 dump_section_headers = 1;
1955 case OPTION_START_ADDRESS:
1956 start_address = parse_vma (optarg, "--start-address");
1958 case OPTION_STOP_ADDRESS:
1959 stop_address = parse_vma (optarg, "--stop-address");
1968 printf ("GNU %s version %s\n", program_name, program_version);
1972 if (seenflag == false)
1982 display_file ("a.out", target);
1984 for (; optind < argc;)
1985 display_file (argv[optind++], target);
1988 END_PROGRESS (program_name);