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. */
24 #include <sys/types.h>
28 #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 */
64 /* Extra info to pass to the disassembler address printing function. */
65 struct objdump_disasm_info {
71 /* Architecture to disassemble for, or default if NULL. */
72 char *machine = (char *) NULL;
74 /* The symbol table. */
77 /* Number of symbols in `syms'. */
80 /* The sorted symbol table. */
81 asymbol **sorted_syms;
83 /* Number of symbols in `sorted_syms'. */
84 long sorted_symcount = 0;
86 /* The dynamic symbol table. */
89 /* Number of symbols in `dynsyms'. */
92 /* Forward declarations. */
95 display_file PARAMS ((char *filename, char *target));
98 dump_data PARAMS ((bfd *abfd));
101 dump_relocs PARAMS ((bfd *abfd));
104 dump_dynamic_relocs PARAMS ((bfd * abfd));
107 dump_reloc_set PARAMS ((bfd *, arelent **, long));
110 dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
113 display_bfd PARAMS ((bfd *abfd));
116 objdump_print_value PARAMS ((bfd_vma, FILE *));
119 objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *));
122 show_line PARAMS ((bfd *, asection *, bfd_vma));
125 usage (stream, status)
130 Usage: %s [-ahifdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
131 [--archive-headers] [--target=bfdname] [--disassemble]\n\
132 [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
133 [--info] [--section=section-name] [--line-numbers] [--source]\n",
136 [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
137 [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
138 [--wide] [--version] [--help] [--private-headers]\n\
139 [--start-address=addr] [--stop-address=addr] objfile...\n\
140 at least one option besides -l (--line-numbers) must be given\n");
141 list_supported_targets (program_name, stream);
145 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
147 #define OPTION_START_ADDRESS (150)
148 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
150 static struct option long_options[]=
152 {"all-headers", no_argument, NULL, 'x'},
153 {"private-headers", no_argument, NULL, 'p'},
154 {"architecture", required_argument, NULL, 'm'},
155 {"archive-headers", no_argument, NULL, 'a'},
156 {"disassemble", no_argument, NULL, 'd'},
157 {"disassemble-all", no_argument, NULL, 'D'},
158 {"dynamic-reloc", no_argument, NULL, 'R'},
159 {"dynamic-syms", no_argument, NULL, 'T'},
160 {"file-headers", no_argument, NULL, 'f'},
161 {"full-contents", no_argument, NULL, 's'},
162 {"headers", no_argument, NULL, 'h'},
163 {"help", no_argument, NULL, 'H'},
164 {"info", no_argument, NULL, 'i'},
165 {"line-numbers", no_argument, NULL, 'l'},
166 {"reloc", no_argument, NULL, 'r'},
167 {"section", required_argument, NULL, 'j'},
168 {"section-headers", no_argument, NULL, 'h'},
169 {"source", no_argument, NULL, 'S'},
170 {"stabs", no_argument, &dump_stab_section_info, 1},
171 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
172 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
173 {"syms", no_argument, NULL, 't'},
174 {"target", required_argument, NULL, 'b'},
175 {"version", no_argument, &show_version, 1},
176 {"wide", no_argument, &wide_output, 'w'},
177 {0, no_argument, 0, 0}
181 dump_section_header (abfd, section, ignored)
189 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
192 printf ("SECTION %d [%s]\t: size %08x",
195 (unsigned) bfd_get_section_size_before_reloc (section));
197 printf_vma (section->vma);
199 printf_vma (section->lma);
200 printf (" align 2**%u%s ",
201 section->alignment_power, (wide_output) ? "" : "\n");
202 PF (SEC_ALLOC, "ALLOC");
203 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
204 PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
205 PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA");
206 PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS");
207 PF (SEC_LOAD, "LOAD");
208 PF (SEC_RELOC, "RELOC");
210 PF (SEC_BALIGN, "BALIGN");
212 PF (SEC_READONLY, "READONLY");
213 PF (SEC_CODE, "CODE");
214 PF (SEC_DATA, "DATA");
216 PF (SEC_DEBUGGING, "DEBUGGING");
217 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
226 bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
233 asymbol **sy = (asymbol **) NULL;
236 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
238 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
243 storage = bfd_get_symtab_upper_bound (abfd);
245 bfd_fatal (bfd_get_filename (abfd));
249 sy = (asymbol **) xmalloc (storage);
251 symcount = bfd_canonicalize_symtab (abfd, sy);
253 bfd_fatal (bfd_get_filename (abfd));
255 fprintf (stderr, "%s: %s: No symbols\n",
256 program_name, bfd_get_filename (abfd));
260 /* Read in the dynamic symbols. */
263 slurp_dynamic_symtab (abfd)
266 asymbol **sy = (asymbol **) NULL;
269 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
272 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
274 fprintf (stderr, "%s: %s: not a dynamic object\n",
275 program_name, bfd_get_filename (abfd));
280 bfd_fatal (bfd_get_filename (abfd));
285 sy = (asymbol **) xmalloc (storage);
287 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
289 bfd_fatal (bfd_get_filename (abfd));
290 if (dynsymcount == 0)
291 fprintf (stderr, "%s: %s: No dynamic symbols\n",
292 program_name, bfd_get_filename (abfd));
296 /* Filter out (in place) symbols that are useless for disassembly.
297 COUNT is the number of elements in SYMBOLS.
298 Return the number of useful symbols. */
301 remove_useless_symbols (symbols, count)
305 register asymbol **in_ptr = symbols, **out_ptr = symbols;
309 asymbol *sym = *in_ptr++;
311 if (sym->name == NULL || sym->name[0] == '\0')
313 if (sym->flags & (BSF_DEBUGGING))
315 if (bfd_is_und_section (sym->section)
316 || bfd_is_com_section (sym->section))
321 return out_ptr - symbols;
324 /* Sort symbols into value order. */
327 compare_symbols (ap, bp)
331 const asymbol *a = *(const asymbol **)ap;
332 const asymbol *b = *(const asymbol **)bp;
337 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
339 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
342 if (a->section > b->section)
344 else if (a->section < b->section)
347 an = bfd_asymbol_name (a);
348 bn = bfd_asymbol_name (b);
352 /* The symbols gnu_compiled and gcc2_compiled convey no real
353 information, so put them after other symbols with the same value. */
355 af = (strstr (an, "gnu_compiled") != NULL
356 || strstr (an, "gcc2_compiled") != NULL);
357 bf = (strstr (bn, "gnu_compiled") != NULL
358 || strstr (bn, "gcc2_compiled") != NULL);
365 /* We use a heuristic for the file name, to try to sort it after
366 more useful symbols. It may not work on non Unix systems, but it
367 doesn't really matter; the only difference is precisely which
368 symbol names get printed. */
370 #define file_symbol(s, sn, snl) \
371 (((s)->flags & BSF_FILE) != 0 \
372 || ((sn)[(snl) - 2] == '.' \
373 && ((sn)[(snl) - 1] == 'o' \
374 || (sn)[(snl) - 1] == 'a')))
376 af = file_symbol (a, an, anl);
377 bf = file_symbol (b, bn, bnl);
387 /* Sort relocs into address order. */
390 compare_relocs (ap, bp)
394 const arelent *a = *(const arelent **)ap;
395 const arelent *b = *(const arelent **)bp;
397 if (a->address > b->address)
399 else if (a->address < b->address)
402 /* So that associated relocations tied to the same address show up
403 in the correct order, we don't do any further sorting. */
412 /* Print VMA to STREAM with no leading zeroes. */
415 objdump_print_value (vma, stream)
422 sprintf_vma (buf, vma);
423 for (p = buf; *p == '0'; ++p)
425 fprintf (stream, "%s", p);
428 /* Print VMA symbolically to INFO if possible. */
431 objdump_print_address (vma, info)
433 struct disassemble_info *info;
435 /* @@ For relocateable files, should filter out symbols belonging to
436 the wrong section. Unfortunately, not enough information is supplied
437 to this routine to determine the correct section in all cases. */
438 /* @@ Would it speed things up to cache the last two symbols returned,
439 and maybe their address ranges? For many processors, only one memory
440 operand can be present at a time, so the 2-entry cache wouldn't be
441 constantly churned by code doing heavy memory accesses. */
443 /* Indices in `sorted_syms'. */
445 long max = sorted_symcount;
448 fprintf_vma (info->stream, vma);
450 if (sorted_symcount < 1)
453 /* Perform a binary search looking for the closest symbol to the
454 required value. We are searching the range (min, max]. */
455 while (min + 1 < max)
459 thisplace = (max + min) / 2;
460 sym = sorted_syms[thisplace];
462 if (bfd_asymbol_value (sym) > vma)
464 else if (bfd_asymbol_value (sym) < vma)
473 /* The symbol we want is now in min, the low end of the range we
477 && (bfd_asymbol_value (sorted_syms[thisplace])
478 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
482 /* If this symbol isn't global, search for one with the same value
484 bfd_vma val = bfd_asymbol_value (sorted_syms[thisplace]);
486 if (sorted_syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING))
487 for (i = thisplace - 1; i >= 0; i--)
489 if (bfd_asymbol_value (sorted_syms[i]) == val
490 && (!(sorted_syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING))
491 || ((sorted_syms[thisplace]->flags & BSF_DEBUGGING)
492 && !(sorted_syms[i]->flags & BSF_DEBUGGING))))
498 if (sorted_syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING))
499 for (i = thisplace + 1; i < sorted_symcount; i++)
501 if (bfd_asymbol_value (sorted_syms[i]) == val
502 && (!(sorted_syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING))
503 || ((sorted_syms[thisplace]->flags & BSF_DEBUGGING)
504 && !(sorted_syms[i]->flags & BSF_DEBUGGING))))
512 /* If the file is relocateable, and the symbol could be from this
513 section, prefer a symbol from this section over symbols from
514 others, even if the other symbol's value might be closer.
516 Note that this may be wrong for some symbol references if the
517 sections have overlapping memory ranges, but in that case there's
518 no way to tell what's desired without looking at the relocation
520 struct objdump_disasm_info *aux;
523 aux = (struct objdump_disasm_info *) info->application_data;
524 if (sorted_syms[thisplace]->section != aux->sec
526 || ((aux->abfd->flags & HAS_RELOC) != 0
527 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
528 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
529 + bfd_section_size (aux->abfd, aux->sec)))))
531 for (i = thisplace + 1; i < sorted_symcount; i++)
533 if (bfd_asymbol_value (sorted_syms[i])
534 != bfd_asymbol_value (sorted_syms[thisplace]))
540 if (sorted_syms[i]->section == aux->sec
542 || sorted_syms[i - 1]->section != aux->sec
543 || (bfd_asymbol_value (sorted_syms[i])
544 != bfd_asymbol_value (sorted_syms[i - 1]))))
551 if (sorted_syms[thisplace]->section != aux->sec)
553 /* We didn't find a good symbol with a smaller value.
554 Look for one with a larger value. */
555 for (i = thisplace + 1; i < sorted_symcount; i++)
557 if (sorted_syms[i]->section == aux->sec)
565 if (sorted_syms[thisplace]->section != aux->sec
567 || ((aux->abfd->flags & HAS_RELOC) != 0
568 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
569 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
570 + bfd_section_size (aux->abfd, aux->sec)))))
574 fprintf (info->stream, " <%s",
575 bfd_get_section_name (aux->abfd, aux->sec));
576 secaddr = bfd_get_section_vma (aux->abfd, aux->sec);
579 fprintf (info->stream, "-");
580 objdump_print_value (secaddr - vma, info->stream);
582 else if (vma > secaddr)
584 fprintf (info->stream, "+");
585 objdump_print_value (vma - secaddr, info->stream);
587 fprintf (info->stream, ">");
593 fprintf (info->stream, " <%s", sorted_syms[thisplace]->name);
594 if (bfd_asymbol_value (sorted_syms[thisplace]) > vma)
596 fprintf (info->stream, "-");
597 objdump_print_value (bfd_asymbol_value (sorted_syms[thisplace]) - vma,
600 else if (vma > bfd_asymbol_value (sorted_syms[thisplace]))
602 fprintf (info->stream, "+");
603 objdump_print_value (vma - bfd_asymbol_value (sorted_syms[thisplace]),
606 fprintf (info->stream, ">");
609 /* Hold the last function name and the last line number we displayed
612 static char *prev_functionname;
613 static unsigned int prev_line;
615 /* We keep a list of all files that we have seen when doing a
616 dissassembly with source, so that we know how much of the file to
617 display. This can be important for inlined functions. */
619 struct print_file_list
621 struct print_file_list *next;
627 static struct print_file_list *print_files;
629 /* The number of preceding context lines to show when we start
630 displaying a file for the first time. */
632 #define SHOW_PRECEDING_CONTEXT_LINES (5)
634 /* Skip ahead to a given line in a file, optionally printing each
638 skip_to_line PARAMS ((struct print_file_list *, unsigned int, boolean));
641 skip_to_line (p, line, show)
642 struct print_file_list *p;
646 while (p->line < line)
650 if (fgets (buf, sizeof buf, p->f) == NULL)
660 if (strchr (buf, '\n') != NULL)
665 /* Show the line number, or the source line, in a dissassembly
669 show_line (abfd, section, off)
674 CONST char *filename;
675 CONST char *functionname;
678 if (! with_line_numbers && ! with_source_code)
681 if (! bfd_find_nearest_line (abfd, section, syms, off, &filename,
682 &functionname, &line))
685 if (filename != NULL && *filename == '\0')
687 if (functionname != NULL && *functionname == '\0')
690 if (with_line_numbers)
692 if (functionname != NULL
693 && (prev_functionname == NULL
694 || strcmp (functionname, prev_functionname) != 0))
695 printf ("%s():\n", functionname);
696 if (line > 0 && line != prev_line)
697 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
704 struct print_file_list **pp, *p;
706 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
707 if (strcmp ((*pp)->filename, filename) == 0)
713 if (p != print_files)
717 /* We have reencountered a file name which we saw
718 earlier. This implies that either we are dumping out
719 code from an included file, or the same file was
720 linked in more than once. There are two common cases
721 of an included file: inline functions in a header
722 file, and a bison or flex skeleton file. In the
723 former case we want to just start printing (but we
724 back up a few lines to give context); in the latter
725 case we want to continue from where we left off. I
726 can't think of a good way to distinguish the cases,
727 so I used a heuristic based on the file name. */
728 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
732 l = line - SHOW_PRECEDING_CONTEXT_LINES;
739 p->f = fopen (p->filename, "r");
743 skip_to_line (p, l, false);
745 if (print_files->f != NULL)
747 fclose (print_files->f);
748 print_files->f = NULL;
754 skip_to_line (p, line, true);
756 p->next = print_files;
764 f = fopen (filename, "r");
769 p = ((struct print_file_list *)
770 xmalloc (sizeof (struct print_file_list)));
771 p->filename = xmalloc (strlen (filename) + 1);
772 strcpy (p->filename, filename);
776 if (print_files != NULL && print_files->f != NULL)
778 fclose (print_files->f);
779 print_files->f = NULL;
781 p->next = print_files;
784 l = line - SHOW_PRECEDING_CONTEXT_LINES;
787 skip_to_line (p, l, false);
789 skip_to_line (p, line, true);
794 if (functionname != NULL
795 && (prev_functionname == NULL
796 || strcmp (functionname, prev_functionname) != 0))
798 if (prev_functionname != NULL)
799 free (prev_functionname);
800 prev_functionname = xmalloc (strlen (functionname) + 1);
801 strcpy (prev_functionname, functionname);
804 if (line > 0 && line != prev_line)
809 disassemble_data (abfd)
813 disassembler_ftype disassemble_fn = 0; /* New style */
814 struct disassemble_info disasm_info;
815 struct objdump_disasm_info aux;
817 boolean done_dot = false;
820 prev_functionname = NULL;
823 /* We make a copy of syms to sort. We don't want to sort syms
824 because that will screw up the relocs. */
825 sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
826 memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
828 sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
830 /* Sort the symbols into section and symbol order */
831 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
833 INIT_DISASSEMBLE_INFO(disasm_info, stdout);
834 disasm_info.application_data = (PTR) &aux;
836 disasm_info.print_address_func = objdump_print_address;
838 if (machine != (char *) NULL)
840 const bfd_arch_info_type *info = bfd_scan_arch (machine);
843 fprintf (stderr, "%s: Can't use supplied machine %s\n",
848 abfd->arch_info = info;
851 disassemble_fn = disassembler (abfd);
854 fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
856 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
860 for (section = abfd->sections;
861 section != (asection *) NULL;
862 section = section->next)
864 bfd_byte *data = NULL;
865 bfd_size_type datasize = 0;
866 arelent **relbuf = NULL;
867 arelent **relpp = NULL;
868 arelent **relppend = NULL;
871 if ((section->flags & SEC_LOAD) == 0
872 || (! disassemble_all
874 && (section->flags & SEC_CODE) == 0))
876 if (only != (char *) NULL && strcmp (only, section->name) != 0)
880 && (section->flags & SEC_RELOC) != 0)
884 relsize = bfd_get_reloc_upper_bound (abfd, section);
886 bfd_fatal (bfd_get_filename (abfd));
892 relbuf = (arelent **) xmalloc (relsize);
893 relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
895 bfd_fatal (bfd_get_filename (abfd));
897 /* Sort the relocs by address. */
898 qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
901 relppend = relpp + relcount;
905 printf ("Disassembly of section %s:\n", section->name);
907 datasize = bfd_get_section_size_before_reloc (section);
911 data = (bfd_byte *) xmalloc ((size_t) datasize);
913 bfd_get_section_contents (abfd, section, data, 0, datasize);
916 disasm_info.buffer = data;
917 disasm_info.buffer_vma = section->vma;
918 disasm_info.buffer_length = datasize;
919 if (start_address == (bfd_vma) -1
920 || start_address < disasm_info.buffer_vma)
923 i = start_address - disasm_info.buffer_vma;
924 if (stop_address == (bfd_vma) -1)
928 if (stop_address < disasm_info.buffer_vma)
931 stop = stop_address - disasm_info.buffer_vma;
932 if (stop > disasm_info.buffer_length)
933 stop = disasm_info.buffer_length;
938 boolean need_nl = false;
940 if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 &&
943 if (done_dot == false)
953 if (with_line_numbers || with_source_code)
954 show_line (abfd, section, i);
955 aux.require_sec = true;
956 objdump_print_address (section->vma + i, &disasm_info);
957 aux.require_sec = false;
960 bytes = (*disassemble_fn) (section->vma + i, &disasm_info);
971 && (section->flags & SEC_RELOC) != 0)
973 while (relpp < relppend
974 && ((*relpp)->address >= (bfd_vma) i
975 && (*relpp)->address < (bfd_vma) i + bytes))
978 const char *sym_name;
982 printf ("\t\tRELOC: ");
984 printf_vma (section->vma + q->address);
986 printf (" %s ", q->howto->name);
988 if (q->sym_ptr_ptr != NULL
989 && *q->sym_ptr_ptr != NULL)
991 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
992 if (sym_name == NULL || *sym_name == '\0')
996 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
997 sym_name = bfd_get_section_name (abfd, sym_sec);
998 if (sym_name == NULL || *sym_name == '\0')
999 sym_name = "*unknown*";
1003 printf ("%s", sym_name);
1008 printf_vma (q->addend);
1031 /* Define a table of stab values and print-strings. We wish the initializer
1032 could be a direct-mapped table, but instead we build one the first
1042 struct stab_print stab_print[] = {
1043 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
1044 #include "aout/stab.def"
1045 #undef __define_stab
1049 void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
1050 char *strsect_name));
1052 /* Dump the stabs sections from an object file that has a section that
1053 uses Sun stabs encoding. It has to use some hooks into BFD because
1054 string table sections are not normally visible to BFD callers. */
1060 /* Allocate and initialize stab name array if first time. */
1061 if (stab_name == NULL)
1065 stab_name = (char **) xmalloc (256 * sizeof(char *));
1066 /* Clear the array. */
1067 for (i = 0; i < 256; i++)
1068 stab_name[i] = NULL;
1069 /* Fill in the defined stabs. */
1070 for (i = 0; *stab_print[i].string; i++)
1071 stab_name[stab_print[i].value] = stab_print[i].string;
1074 dump_section_stabs (abfd, ".stab", ".stabstr");
1075 dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
1076 dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
1077 dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1080 static struct internal_nlist *stabs;
1081 static bfd_size_type stab_size;
1083 static char *strtab;
1084 static bfd_size_type stabstr_size;
1086 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1087 and string table section STRSECT_NAME into `strtab'.
1088 If the section exists and was read, allocate the space and return true.
1089 Otherwise return false. */
1092 read_section_stabs (abfd, stabsect_name, strsect_name)
1094 char *stabsect_name;
1097 asection *stabsect, *stabstrsect;
1099 stabsect = bfd_get_section_by_name (abfd, stabsect_name);
1102 printf ("No %s section present\n\n", stabsect_name);
1106 stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
1107 if (0 == stabstrsect)
1109 fprintf (stderr, "%s: %s has no %s section\n", program_name,
1110 bfd_get_filename (abfd), strsect_name);
1114 stab_size = bfd_section_size (abfd, stabsect);
1115 stabstr_size = bfd_section_size (abfd, stabstrsect);
1117 stabs = (struct internal_nlist *) xmalloc (stab_size);
1118 strtab = (char *) xmalloc (stabstr_size);
1120 if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
1122 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1123 program_name, stabsect_name, bfd_get_filename (abfd),
1124 bfd_errmsg (bfd_get_error ()));
1130 if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
1133 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1134 program_name, strsect_name, bfd_get_filename (abfd),
1135 bfd_errmsg (bfd_get_error ()));
1144 #define SWAP_SYMBOL(symp, abfd) \
1146 (symp)->n_strx = bfd_h_get_32(abfd, \
1147 (unsigned char *)&(symp)->n_strx); \
1148 (symp)->n_desc = bfd_h_get_16 (abfd, \
1149 (unsigned char *)&(symp)->n_desc); \
1150 (symp)->n_value = bfd_h_get_32 (abfd, \
1151 (unsigned char *)&(symp)->n_value); \
1154 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1155 using string table section STRSECT_NAME (in `strtab'). */
1158 print_section_stabs (abfd, stabsect_name, strsect_name)
1160 char *stabsect_name;
1164 unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
1165 struct internal_nlist *stabp = stabs,
1166 *stabs_end = (struct internal_nlist *) (stab_size + (char *) stabs);
1168 printf ("Contents of %s section:\n\n", stabsect_name);
1169 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1171 /* Loop through all symbols and print them.
1173 We start the index at -1 because there is a dummy symbol on
1174 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1176 for (i = -1; stabp < stabs_end; stabp++, i++)
1178 SWAP_SYMBOL (stabp, abfd);
1179 printf ("\n%-6d ", i);
1180 /* Either print the stab name, or, if unnamed, print its number
1181 again (makes consistent formatting for tools like awk). */
1182 if (stab_name[stabp->n_type])
1183 printf ("%-6s", stab_name[stabp->n_type]);
1184 else if (stabp->n_type == N_UNDF)
1187 printf ("%-6d", stabp->n_type);
1188 printf (" %-6d %-6d ", stabp->n_other, stabp->n_desc);
1189 printf_vma (stabp->n_value);
1190 printf (" %-6lu", stabp->n_strx);
1192 /* Symbols with type == 0 (N_UNDF) specify the length of the
1193 string table associated with this file. We use that info
1194 to know how to relocate the *next* file's string table indices. */
1196 if (stabp->n_type == N_UNDF)
1198 file_string_table_offset = next_file_string_table_offset;
1199 next_file_string_table_offset += stabp->n_value;
1203 /* Using the (possibly updated) string table offset, print the
1204 string (if any) associated with this symbol. */
1206 if ((stabp->n_strx + file_string_table_offset) < stabstr_size)
1207 printf (" %s", &strtab[stabp->n_strx + file_string_table_offset]);
1216 dump_section_stabs (abfd, stabsect_name, strsect_name)
1218 char *stabsect_name;
1223 /* Check for section names for which stabsect_name is a prefix, to
1224 handle .stab0, etc. */
1225 for (s = abfd->sections;
1229 if (strncmp (stabsect_name, s->name, strlen (stabsect_name)) == 0
1230 && strncmp (strsect_name, s->name, strlen (strsect_name)) != 0)
1232 if (read_section_stabs (abfd, s->name, strsect_name))
1234 print_section_stabs (abfd, s->name, strsect_name);
1243 dump_bfd_header (abfd)
1248 printf ("architecture: %s, ",
1249 bfd_printable_arch_mach (bfd_get_arch (abfd),
1250 bfd_get_mach (abfd)));
1251 printf ("flags 0x%08x:\n", abfd->flags);
1253 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1254 PF (HAS_RELOC, "HAS_RELOC");
1255 PF (EXEC_P, "EXEC_P");
1256 PF (HAS_LINENO, "HAS_LINENO");
1257 PF (HAS_DEBUG, "HAS_DEBUG");
1258 PF (HAS_SYMS, "HAS_SYMS");
1259 PF (HAS_LOCALS, "HAS_LOCALS");
1260 PF (DYNAMIC, "DYNAMIC");
1261 PF (WP_TEXT, "WP_TEXT");
1262 PF (D_PAGED, "D_PAGED");
1263 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
1264 printf ("\nstart address 0x");
1265 printf_vma (abfd->start_address);
1269 dump_bfd_private_header (abfd)
1272 bfd_print_private_bfd_data (abfd, stdout);
1280 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
1282 bfd_nonfatal (bfd_get_filename (abfd));
1283 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1285 list_matching_formats (matching);
1291 printf ("\n%s: file format %s\n", bfd_get_filename (abfd),
1294 print_arelt_descr (stdout, abfd, true);
1295 if (dump_file_header)
1296 dump_bfd_header (abfd);
1297 if (dump_private_headers)
1298 dump_bfd_private_header (abfd);
1300 if (dump_section_headers)
1301 dump_headers (abfd);
1302 if (dump_symtab || dump_reloc_info || disassemble)
1304 syms = slurp_symtab (abfd);
1306 if (dump_dynamic_symtab || dump_dynamic_reloc_info)
1308 dynsyms = slurp_dynamic_symtab (abfd);
1311 dump_symbols (abfd, false);
1312 if (dump_dynamic_symtab)
1313 dump_symbols (abfd, true);
1314 if (dump_stab_section_info)
1316 if (dump_reloc_info && ! disassemble)
1318 if (dump_dynamic_reloc_info)
1319 dump_dynamic_relocs (abfd);
1320 if (dump_section_contents)
1323 disassemble_data (abfd);
1337 display_file (filename, target)
1341 bfd *file, *arfile = (bfd *) NULL;
1343 file = bfd_openr (filename, target);
1346 bfd_nonfatal (filename);
1350 if (bfd_check_format (file, bfd_archive) == true)
1352 bfd *last_arfile = NULL;
1354 printf ("In archive %s:\n", bfd_get_filename (file));
1357 bfd_set_error (bfd_error_no_error);
1359 arfile = bfd_openr_next_archived_file (file, arfile);
1362 if (bfd_get_error () != bfd_error_no_more_archived_files)
1364 bfd_nonfatal (bfd_get_filename (file));
1369 display_bfd (arfile);
1371 if (last_arfile != NULL)
1372 bfd_close (last_arfile);
1373 last_arfile = arfile;
1376 if (last_arfile != NULL)
1377 bfd_close (last_arfile);
1385 /* Actually display the various requested regions */
1393 bfd_size_type datasize = 0;
1395 bfd_size_type start, stop;
1397 for (section = abfd->sections; section != NULL; section =
1402 if (only == (char *) NULL ||
1403 strcmp (only, section->name) == 0)
1405 if (section->flags & SEC_HAS_CONTENTS)
1407 printf ("Contents of section %s:\n", section->name);
1409 if (bfd_section_size (abfd, section) == 0)
1411 data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
1412 datasize = bfd_section_size (abfd, section);
1415 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
1417 if (start_address == (bfd_vma) -1
1418 || start_address < section->vma)
1421 start = start_address - section->vma;
1422 if (stop_address == (bfd_vma) -1)
1423 stop = bfd_section_size (abfd, section);
1426 if (stop_address < section->vma)
1429 stop = stop_address - section->vma;
1430 if (stop > bfd_section_size (abfd, section))
1431 stop = bfd_section_size (abfd, section);
1433 for (i = start; i < stop; i += onaline)
1437 printf (" %04lx ", (unsigned long int) (i + section->vma));
1438 for (j = i; j < i + onaline; j++)
1441 printf ("%02x", (unsigned) (data[j]));
1449 for (j = i; j < i + onaline; j++)
1454 printf ("%c", isprint (data[j]) ? data[j] : '.');
1464 /* Should perhaps share code and display with nm? */
1466 dump_symbols (abfd, dynamic)
1480 printf ("DYNAMIC SYMBOL TABLE:\n");
1488 printf ("SYMBOL TABLE:\n");
1491 for (count = 0; count < max; count++)
1495 bfd *cur_bfd = bfd_asymbol_bfd(*current);
1498 bfd_print_symbol (cur_bfd,
1500 *current, bfd_print_symbol_all);
1518 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
1522 if (bfd_is_abs_section (a))
1524 if (bfd_is_und_section (a))
1526 if (bfd_is_com_section (a))
1531 if (strcmp (only, a->name))
1534 else if ((a->flags & SEC_RELOC) == 0)
1537 relsize = bfd_get_reloc_upper_bound (abfd, a);
1539 bfd_fatal (bfd_get_filename (abfd));
1541 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
1545 printf (" (none)\n\n");
1549 relpp = (arelent **) xmalloc (relsize);
1550 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
1552 bfd_fatal (bfd_get_filename (abfd));
1553 else if (relcount == 0)
1555 printf (" (none)\n\n");
1560 dump_reloc_set (abfd, relpp, relcount);
1569 dump_dynamic_relocs (abfd)
1576 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
1578 bfd_fatal (bfd_get_filename (abfd));
1580 printf ("DYNAMIC RELOCATION RECORDS");
1584 printf (" (none)\n\n");
1588 relpp = (arelent **) xmalloc (relsize);
1589 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
1591 bfd_fatal (bfd_get_filename (abfd));
1592 else if (relcount == 0)
1594 printf (" (none)\n\n");
1599 dump_reloc_set (abfd, relpp, relcount);
1607 dump_reloc_set (abfd, relpp, relcount)
1614 /* Get column headers lined up reasonably. */
1620 sprintf_vma (buf, (bfd_vma) -1);
1621 width = strlen (buf) - 7;
1623 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
1626 for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
1629 CONST char *sym_name;
1630 CONST char *section_name;
1632 if (start_address != (bfd_vma) -1
1633 && q->address < start_address)
1635 if (stop_address != (bfd_vma) -1
1636 && q->address > stop_address)
1639 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
1641 sym_name = (*(q->sym_ptr_ptr))->name;
1642 section_name = (*(q->sym_ptr_ptr))->section->name;
1647 section_name = NULL;
1651 printf_vma (q->address);
1652 printf (" %-16s %s",
1658 if (section_name == (CONST char *) NULL)
1659 section_name = "*unknown*";
1660 printf_vma (q->address);
1661 printf (" %-16s [%s]",
1668 printf_vma (q->addend);
1674 /* The length of the longest architecture name + 1. */
1675 #define LONGEST_ARCH sizeof("rs6000:6000")
1681 /* List the targets that BFD is configured to support, each followed
1682 by its endianness and the architectures it supports. */
1685 display_target_list ()
1687 extern char *tmpnam ();
1688 extern bfd_target *bfd_target_vector[];
1689 char tmparg[L_tmpnam];
1693 dummy_name = tmpnam (tmparg);
1694 for (t = 0; bfd_target_vector[t]; t++)
1696 bfd_target *p = bfd_target_vector[t];
1697 bfd *abfd = bfd_openw (dummy_name, p->name);
1700 printf ("%s\n (header %s, data %s)\n", p->name,
1701 p->header_byteorder_big_p ? "big endian" : "little endian",
1702 p->byteorder_big_p ? "big endian" : "little endian");
1706 bfd_nonfatal (dummy_name);
1710 if (! bfd_set_format (abfd, bfd_object))
1712 if (bfd_get_error () != bfd_error_invalid_operation)
1713 bfd_nonfatal (p->name);
1717 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1718 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
1720 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
1722 unlink (dummy_name);
1725 /* Print a table showing which architectures are supported for entries
1726 FIRST through LAST-1 of bfd_target_vector (targets across,
1727 architectures down). */
1730 display_info_table (first, last)
1734 extern bfd_target *bfd_target_vector[];
1735 extern char *tmpnam ();
1736 char tmparg[L_tmpnam];
1740 /* Print heading of target names. */
1741 printf ("\n%*s", (int) LONGEST_ARCH, " ");
1742 for (t = first; t < last && bfd_target_vector[t]; t++)
1743 printf ("%s ", bfd_target_vector[t]->name);
1746 dummy_name = tmpnam (tmparg);
1747 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1748 if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
1750 printf ("%*s ", (int) LONGEST_ARCH - 1,
1751 bfd_printable_arch_mach (a, 0));
1752 for (t = first; t < last && bfd_target_vector[t]; t++)
1754 bfd_target *p = bfd_target_vector[t];
1756 bfd *abfd = bfd_openw (dummy_name, p->name);
1760 bfd_nonfatal (p->name);
1766 if (! bfd_set_format (abfd, bfd_object))
1768 if (bfd_get_error () != bfd_error_invalid_operation)
1769 bfd_nonfatal (p->name);
1776 if (! bfd_set_arch_mach (abfd, a, 0))
1781 printf ("%s ", p->name);
1784 int l = strlen (p->name);
1792 unlink (dummy_name);
1795 /* Print tables of all the target-architecture combinations that
1796 BFD has been configured to support. */
1799 display_target_tables ()
1802 extern bfd_target *bfd_target_vector[];
1804 extern char *getenv ();
1807 colum = getenv ("COLUMNS");
1809 columns = atoi (colum);
1814 while (bfd_target_vector[t] != NULL)
1818 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
1820 while (wid < columns && bfd_target_vector[t] != NULL)
1824 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
1825 if (newwid >= columns)
1830 display_info_table (oldt, t);
1837 printf ("BFD header file version %s\n", BFD_VERSION);
1838 display_target_list ();
1839 display_target_tables ();
1848 char *target = default_target;
1849 boolean seenflag = false;
1851 program_name = *argv;
1852 xmalloc_set_program_name (program_name);
1854 START_PROGRESS (program_name, 0);
1858 while ((c = getopt_long (argc, argv, "pib:m:VdDlfahrRtTxsSj:w", long_options,
1862 if (c != 'l' && c != OPTION_START_ADDRESS && c != OPTION_STOP_ADDRESS)
1867 break; /* we've been given a long option */
1875 with_line_numbers = 1;
1881 dump_file_header = true;
1884 formats_info = true;
1887 dump_private_headers = 1;
1890 dump_private_headers = 1;
1892 dump_reloc_info = 1;
1893 dump_file_header = true;
1895 dump_section_headers = 1;
1901 dump_dynamic_symtab = 1;
1907 disassemble = disassemble_all = true;
1911 with_source_code = true;
1914 dump_section_contents = 1;
1917 dump_reloc_info = 1;
1920 dump_dynamic_reloc_info = 1;
1926 dump_section_headers = 1;
1936 case OPTION_START_ADDRESS:
1937 start_address = parse_vma (optarg, "--start-address");
1939 case OPTION_STOP_ADDRESS:
1940 stop_address = parse_vma (optarg, "--stop-address");
1949 printf ("GNU %s version %s\n", program_name, program_version);
1953 if (seenflag == false)
1963 display_file ("a.out", target);
1965 for (; optind < argc;)
1966 display_file (argv[optind++], target);
1969 END_PROGRESS (program_name);