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. */
27 #include "libiberty.h"
29 /* Internal headers for the ELF .stab-dump code - sorry. */
30 #define BYTES_IN_WORD 32
31 #include "aout/aout64.h"
33 #ifdef NEED_DECLARATION_FPRINTF
34 /* This is needed by INIT_DISASSEMBLE_INFO. */
35 extern int fprintf ();
38 char *default_target = NULL; /* default at runtime */
40 extern char *program_version;
42 int show_version = 0; /* show the version number */
43 int dump_section_contents; /* -s */
44 int dump_section_headers; /* -h */
45 boolean dump_file_header; /* -f */
46 int dump_symtab; /* -t */
47 int dump_dynamic_symtab; /* -T */
48 int dump_reloc_info; /* -r */
49 int dump_dynamic_reloc_info; /* -R */
50 int dump_ar_hdrs; /* -a */
51 int dump_private_headers; /* -p */
52 int with_line_numbers; /* -l */
53 boolean with_source_code; /* -S */
54 int dump_stab_section_info; /* --stabs */
55 boolean disassemble; /* -d */
56 boolean disassemble_all; /* -D */
57 boolean formats_info; /* -i */
58 char *only; /* -j secname */
59 int wide_output; /* -w */
60 bfd_vma start_address = (bfd_vma) -1; /* --start-address */
61 bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
63 /* Extra info to pass to the disassembler address printing function. */
64 struct objdump_disasm_info {
70 /* Architecture to disassemble for, or default if NULL. */
71 char *machine = (char *) NULL;
73 /* The symbol table. */
76 /* Number of symbols in `syms'. */
79 /* The sorted symbol table. */
80 asymbol **sorted_syms;
82 /* Number of symbols in `sorted_syms'. */
83 long sorted_symcount = 0;
85 /* The dynamic symbol table. */
88 /* Number of symbols in `dynsyms'. */
91 /* Forward declarations. */
94 display_file PARAMS ((char *filename, char *target));
97 dump_data PARAMS ((bfd *abfd));
100 dump_relocs PARAMS ((bfd *abfd));
103 dump_dynamic_relocs PARAMS ((bfd * abfd));
106 dump_reloc_set PARAMS ((bfd *, arelent **, long));
109 dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
112 display_bfd PARAMS ((bfd *abfd));
115 objdump_print_value PARAMS ((bfd_vma, FILE *));
118 objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *));
121 show_line PARAMS ((bfd *, asection *, bfd_vma));
124 usage (stream, status)
129 Usage: %s [-ahifdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
130 [--archive-headers] [--target=bfdname] [--disassemble]\n\
131 [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
132 [--info] [--section=section-name] [--line-numbers] [--source]\n",
135 [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
136 [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
137 [--wide] [--version] [--help] [--private-headers]\n\
138 [--start-address=addr] [--stop-address=addr] objfile...\n\
139 at least one option besides -l (--line-numbers) must be given\n");
140 list_supported_targets (program_name, stream);
144 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
146 #define OPTION_START_ADDRESS (150)
147 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
149 static struct option long_options[]=
151 {"all-headers", no_argument, NULL, 'x'},
152 {"private-headers", no_argument, NULL, 'p'},
153 {"architecture", required_argument, NULL, 'm'},
154 {"archive-headers", no_argument, NULL, 'a'},
155 {"disassemble", no_argument, NULL, 'd'},
156 {"disassemble-all", no_argument, NULL, 'D'},
157 {"dynamic-reloc", no_argument, NULL, 'R'},
158 {"dynamic-syms", no_argument, NULL, 'T'},
159 {"file-headers", no_argument, NULL, 'f'},
160 {"full-contents", no_argument, NULL, 's'},
161 {"headers", no_argument, NULL, 'h'},
162 {"help", no_argument, NULL, 'H'},
163 {"info", no_argument, NULL, 'i'},
164 {"line-numbers", no_argument, NULL, 'l'},
165 {"reloc", no_argument, NULL, 'r'},
166 {"section", required_argument, NULL, 'j'},
167 {"section-headers", no_argument, NULL, 'h'},
168 {"source", no_argument, NULL, 'S'},
169 {"stabs", no_argument, &dump_stab_section_info, 1},
170 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
171 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
172 {"syms", no_argument, NULL, 't'},
173 {"target", required_argument, NULL, 'b'},
174 {"version", no_argument, &show_version, 1},
175 {"wide", no_argument, &wide_output, 'w'},
176 {0, no_argument, 0, 0}
180 dump_section_header (abfd, section, ignored)
188 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
191 printf ("SECTION %d [%s]\t: size %08x",
194 (unsigned) bfd_get_section_size_before_reloc (section));
196 printf_vma (section->vma);
198 printf_vma (section->lma);
199 printf (" align 2**%u%s ",
200 section->alignment_power, (wide_output) ? "" : "\n");
201 PF (SEC_ALLOC, "ALLOC");
202 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
203 PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
204 PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA");
205 PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS");
206 PF (SEC_LOAD, "LOAD");
207 PF (SEC_RELOC, "RELOC");
209 PF (SEC_BALIGN, "BALIGN");
211 PF (SEC_READONLY, "READONLY");
212 PF (SEC_CODE, "CODE");
213 PF (SEC_DATA, "DATA");
215 PF (SEC_DEBUGGING, "DEBUGGING");
216 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
225 bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
232 asymbol **sy = (asymbol **) NULL;
235 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
237 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
242 storage = bfd_get_symtab_upper_bound (abfd);
244 bfd_fatal (bfd_get_filename (abfd));
248 sy = (asymbol **) xmalloc (storage);
250 symcount = bfd_canonicalize_symtab (abfd, sy);
252 bfd_fatal (bfd_get_filename (abfd));
254 fprintf (stderr, "%s: %s: No symbols\n",
255 program_name, bfd_get_filename (abfd));
259 /* Read in the dynamic symbols. */
262 slurp_dynamic_symtab (abfd)
265 asymbol **sy = (asymbol **) NULL;
268 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
271 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
273 fprintf (stderr, "%s: %s: not a dynamic object\n",
274 program_name, bfd_get_filename (abfd));
279 bfd_fatal (bfd_get_filename (abfd));
284 sy = (asymbol **) xmalloc (storage);
286 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
288 bfd_fatal (bfd_get_filename (abfd));
289 if (dynsymcount == 0)
290 fprintf (stderr, "%s: %s: No dynamic symbols\n",
291 program_name, bfd_get_filename (abfd));
295 /* Filter out (in place) symbols that are useless for disassembly.
296 COUNT is the number of elements in SYMBOLS.
297 Return the number of useful symbols. */
300 remove_useless_symbols (symbols, count)
304 register asymbol **in_ptr = symbols, **out_ptr = symbols;
308 asymbol *sym = *in_ptr++;
310 if (sym->name == NULL || sym->name[0] == '\0')
312 if (sym->flags & (BSF_DEBUGGING))
314 if (bfd_is_und_section (sym->section)
315 || bfd_is_com_section (sym->section))
320 return out_ptr - symbols;
323 /* Sort symbols into value order. */
326 compare_symbols (ap, bp)
330 const asymbol *a = *(const asymbol **)ap;
331 const asymbol *b = *(const asymbol **)bp;
336 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
338 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
341 if (a->section > b->section)
343 else if (a->section < b->section)
346 an = bfd_asymbol_name (a);
347 bn = bfd_asymbol_name (b);
351 /* The symbols gnu_compiled and gcc2_compiled convey no real
352 information, so put them after other symbols with the same value. */
354 af = (strstr (an, "gnu_compiled") != NULL
355 || strstr (an, "gcc2_compiled") != NULL);
356 bf = (strstr (bn, "gnu_compiled") != NULL
357 || strstr (bn, "gcc2_compiled") != NULL);
364 /* We use a heuristic for the file name, to try to sort it after
365 more useful symbols. It may not work on non Unix systems, but it
366 doesn't really matter; the only difference is precisely which
367 symbol names get printed. */
369 #define file_symbol(s, sn, snl) \
370 (((s)->flags & BSF_FILE) != 0 \
371 || ((sn)[(snl) - 2] == '.' \
372 && ((sn)[(snl) - 1] == 'o' \
373 || (sn)[(snl) - 1] == 'a')))
375 af = file_symbol (a, an, anl);
376 bf = file_symbol (b, bn, bnl);
386 /* Sort relocs into address order. */
389 compare_relocs (ap, bp)
393 const arelent *a = *(const arelent **)ap;
394 const arelent *b = *(const arelent **)bp;
396 if (a->address > b->address)
398 else if (a->address < b->address)
401 /* So that associated relocations tied to the same address show up
402 in the correct order, we don't do any further sorting. */
411 /* Print VMA to STREAM with no leading zeroes. */
414 objdump_print_value (vma, stream)
421 sprintf_vma (buf, vma);
422 for (p = buf; *p == '0'; ++p)
424 fprintf (stream, "%s", p);
427 /* Print VMA symbolically to INFO if possible. */
430 objdump_print_address (vma, info)
432 struct disassemble_info *info;
434 /* @@ For relocateable files, should filter out symbols belonging to
435 the wrong section. Unfortunately, not enough information is supplied
436 to this routine to determine the correct section in all cases. */
437 /* @@ Would it speed things up to cache the last two symbols returned,
438 and maybe their address ranges? For many processors, only one memory
439 operand can be present at a time, so the 2-entry cache wouldn't be
440 constantly churned by code doing heavy memory accesses. */
442 /* Indices in `sorted_syms'. */
444 long max = sorted_symcount;
447 fprintf_vma (info->stream, vma);
449 if (sorted_symcount < 1)
452 /* Perform a binary search looking for the closest symbol to the
453 required value. We are searching the range (min, max]. */
454 while (min + 1 < max)
458 thisplace = (max + min) / 2;
459 sym = sorted_syms[thisplace];
461 if (bfd_asymbol_value (sym) > vma)
463 else if (bfd_asymbol_value (sym) < vma)
472 /* The symbol we want is now in min, the low end of the range we
476 && (bfd_asymbol_value (sorted_syms[thisplace])
477 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
481 /* If this symbol isn't global, search for one with the same value
483 bfd_vma val = bfd_asymbol_value (sorted_syms[thisplace]);
485 if (sorted_syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING))
486 for (i = thisplace - 1; i >= 0; i--)
488 if (bfd_asymbol_value (sorted_syms[i]) == val
489 && (!(sorted_syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING))
490 || ((sorted_syms[thisplace]->flags & BSF_DEBUGGING)
491 && !(sorted_syms[i]->flags & BSF_DEBUGGING))))
497 if (sorted_syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING))
498 for (i = thisplace + 1; i < sorted_symcount; i++)
500 if (bfd_asymbol_value (sorted_syms[i]) == val
501 && (!(sorted_syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING))
502 || ((sorted_syms[thisplace]->flags & BSF_DEBUGGING)
503 && !(sorted_syms[i]->flags & BSF_DEBUGGING))))
511 /* If the file is relocateable, and the symbol could be from this
512 section, prefer a symbol from this section over symbols from
513 others, even if the other symbol's value might be closer.
515 Note that this may be wrong for some symbol references if the
516 sections have overlapping memory ranges, but in that case there's
517 no way to tell what's desired without looking at the relocation
519 struct objdump_disasm_info *aux;
522 aux = (struct objdump_disasm_info *) info->application_data;
523 if (sorted_syms[thisplace]->section != aux->sec
525 || ((aux->abfd->flags & HAS_RELOC) != 0
526 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
527 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
528 + bfd_section_size (aux->abfd, aux->sec)))))
530 for (i = thisplace + 1; i < sorted_symcount; i++)
532 if (bfd_asymbol_value (sorted_syms[i])
533 != bfd_asymbol_value (sorted_syms[thisplace]))
539 if (sorted_syms[i]->section == aux->sec
541 || sorted_syms[i - 1]->section != aux->sec
542 || (bfd_asymbol_value (sorted_syms[i])
543 != bfd_asymbol_value (sorted_syms[i - 1]))))
550 if (sorted_syms[thisplace]->section != aux->sec)
552 /* We didn't find a good symbol with a smaller value.
553 Look for one with a larger value. */
554 for (i = thisplace + 1; i < sorted_symcount; i++)
556 if (sorted_syms[i]->section == aux->sec)
564 if (sorted_syms[thisplace]->section != aux->sec
566 || ((aux->abfd->flags & HAS_RELOC) != 0
567 && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
568 && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
569 + bfd_section_size (aux->abfd, aux->sec)))))
573 fprintf (info->stream, " <%s",
574 bfd_get_section_name (aux->abfd, aux->sec));
575 secaddr = bfd_get_section_vma (aux->abfd, aux->sec);
578 fprintf (info->stream, "-");
579 objdump_print_value (secaddr - vma, info->stream);
581 else if (vma > secaddr)
583 fprintf (info->stream, "+");
584 objdump_print_value (vma - secaddr, info->stream);
586 fprintf (info->stream, ">");
592 fprintf (info->stream, " <%s", sorted_syms[thisplace]->name);
593 if (bfd_asymbol_value (sorted_syms[thisplace]) > vma)
595 fprintf (info->stream, "-");
596 objdump_print_value (bfd_asymbol_value (sorted_syms[thisplace]) - vma,
599 else if (vma > bfd_asymbol_value (sorted_syms[thisplace]))
601 fprintf (info->stream, "+");
602 objdump_print_value (vma - bfd_asymbol_value (sorted_syms[thisplace]),
605 fprintf (info->stream, ">");
608 /* Hold the last function name and the last line number we displayed
611 static char *prev_functionname;
612 static unsigned int prev_line;
614 /* We keep a list of all files that we have seen when doing a
615 dissassembly with source, so that we know how much of the file to
616 display. This can be important for inlined functions. */
618 struct print_file_list
620 struct print_file_list *next;
626 static struct print_file_list *print_files;
628 /* The number of preceding context lines to show when we start
629 displaying a file for the first time. */
631 #define SHOW_PRECEDING_CONTEXT_LINES (5)
633 /* Skip ahead to a given line in a file, optionally printing each
637 skip_to_line PARAMS ((struct print_file_list *, unsigned int, boolean));
640 skip_to_line (p, line, show)
641 struct print_file_list *p;
645 while (p->line < line)
649 if (fgets (buf, sizeof buf, p->f) == NULL)
659 if (strchr (buf, '\n') != NULL)
664 /* Show the line number, or the source line, in a dissassembly
668 show_line (abfd, section, off)
673 CONST char *filename;
674 CONST char *functionname;
677 if (! with_line_numbers && ! with_source_code)
680 if (! bfd_find_nearest_line (abfd, section, syms, off, &filename,
681 &functionname, &line))
684 if (filename != NULL && *filename == '\0')
686 if (functionname != NULL && *functionname == '\0')
689 if (with_line_numbers)
691 if (functionname != NULL
692 && (prev_functionname == NULL
693 || strcmp (functionname, prev_functionname) != 0))
694 printf ("%s():\n", functionname);
695 if (line > 0 && line != prev_line)
696 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
703 struct print_file_list **pp, *p;
705 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
706 if (strcmp ((*pp)->filename, filename) == 0)
712 if (p != print_files)
716 /* We have reencountered a file name which we saw
717 earlier. This implies that either we are dumping out
718 code from an included file, or the same file was
719 linked in more than once. There are two common cases
720 of an included file: inline functions in a header
721 file, and a bison or flex skeleton file. In the
722 former case we want to just start printing (but we
723 back up a few lines to give context); in the latter
724 case we want to continue from where we left off. I
725 can't think of a good way to distinguish the cases,
726 so I used a heuristic based on the file name. */
727 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
731 l = line - SHOW_PRECEDING_CONTEXT_LINES;
738 p->f = fopen (p->filename, "r");
742 skip_to_line (p, l, false);
744 if (print_files->f != NULL)
746 fclose (print_files->f);
747 print_files->f = NULL;
753 skip_to_line (p, line, true);
755 p->next = print_files;
763 f = fopen (filename, "r");
768 p = ((struct print_file_list *)
769 xmalloc (sizeof (struct print_file_list)));
770 p->filename = xmalloc (strlen (filename) + 1);
771 strcpy (p->filename, filename);
775 if (print_files != NULL && print_files->f != NULL)
777 fclose (print_files->f);
778 print_files->f = NULL;
780 p->next = print_files;
783 l = line - SHOW_PRECEDING_CONTEXT_LINES;
786 skip_to_line (p, l, false);
788 skip_to_line (p, line, true);
793 if (functionname != NULL
794 && (prev_functionname == NULL
795 || strcmp (functionname, prev_functionname) != 0))
797 if (prev_functionname != NULL)
798 free (prev_functionname);
799 prev_functionname = xmalloc (strlen (functionname) + 1);
800 strcpy (prev_functionname, functionname);
803 if (line > 0 && line != prev_line)
808 disassemble_data (abfd)
812 disassembler_ftype disassemble_fn = 0; /* New style */
813 struct disassemble_info disasm_info;
814 struct objdump_disasm_info aux;
816 boolean done_dot = false;
819 prev_functionname = NULL;
822 /* We make a copy of syms to sort. We don't want to sort syms
823 because that will screw up the relocs. */
824 sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
825 memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
827 sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
829 /* Sort the symbols into section and symbol order */
830 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
832 INIT_DISASSEMBLE_INFO(disasm_info, stdout);
833 disasm_info.application_data = (PTR) &aux;
835 disasm_info.print_address_func = objdump_print_address;
837 if (machine != (char *) NULL)
839 const bfd_arch_info_type *info = bfd_scan_arch (machine);
842 fprintf (stderr, "%s: Can't use supplied machine %s\n",
847 abfd->arch_info = info;
850 disassemble_fn = disassembler (abfd);
853 fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
855 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
859 for (section = abfd->sections;
860 section != (asection *) NULL;
861 section = section->next)
863 bfd_byte *data = NULL;
864 bfd_size_type datasize = 0;
865 arelent **relbuf = NULL;
866 arelent **relpp = NULL;
867 arelent **relppend = NULL;
870 if ((section->flags & SEC_LOAD) == 0
871 || (! disassemble_all
873 && (section->flags & SEC_CODE) == 0))
875 if (only != (char *) NULL && strcmp (only, section->name) != 0)
879 && (section->flags & SEC_RELOC) != 0)
883 relsize = bfd_get_reloc_upper_bound (abfd, section);
885 bfd_fatal (bfd_get_filename (abfd));
891 relbuf = (arelent **) xmalloc (relsize);
892 relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
894 bfd_fatal (bfd_get_filename (abfd));
896 /* Sort the relocs by address. */
897 qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
900 relppend = relpp + relcount;
904 printf ("Disassembly of section %s:\n", section->name);
906 datasize = bfd_get_section_size_before_reloc (section);
910 data = (bfd_byte *) xmalloc ((size_t) datasize);
912 bfd_get_section_contents (abfd, section, data, 0, datasize);
915 disasm_info.buffer = data;
916 disasm_info.buffer_vma = section->vma;
917 disasm_info.buffer_length = datasize;
918 if (start_address == (bfd_vma) -1
919 || start_address < disasm_info.buffer_vma)
922 i = start_address - disasm_info.buffer_vma;
923 if (stop_address == (bfd_vma) -1)
927 if (stop_address < disasm_info.buffer_vma)
930 stop = stop_address - disasm_info.buffer_vma;
931 if (stop > disasm_info.buffer_length)
932 stop = disasm_info.buffer_length;
937 boolean need_nl = false;
939 if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 &&
942 if (done_dot == false)
952 if (with_line_numbers || with_source_code)
953 show_line (abfd, section, i);
954 aux.require_sec = true;
955 objdump_print_address (section->vma + i, &disasm_info);
956 aux.require_sec = false;
959 bytes = (*disassemble_fn) (section->vma + i, &disasm_info);
970 && (section->flags & SEC_RELOC) != 0)
972 while (relpp < relppend
973 && ((*relpp)->address >= (bfd_vma) i
974 && (*relpp)->address < (bfd_vma) i + bytes))
977 const char *sym_name;
981 printf ("\t\tRELOC: ");
983 printf_vma (section->vma + q->address);
985 printf (" %s ", q->howto->name);
987 if (q->sym_ptr_ptr != NULL
988 && *q->sym_ptr_ptr != NULL)
990 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
991 if (sym_name == NULL || *sym_name == '\0')
995 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
996 sym_name = bfd_get_section_name (abfd, sym_sec);
997 if (sym_name == NULL || *sym_name == '\0')
998 sym_name = "*unknown*";
1002 printf ("%s", sym_name);
1007 printf_vma (q->addend);
1030 /* Define a table of stab values and print-strings. We wish the initializer
1031 could be a direct-mapped table, but instead we build one the first
1041 struct stab_print stab_print[] = {
1042 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
1043 #include "aout/stab.def"
1044 #undef __define_stab
1048 void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
1049 char *strsect_name));
1051 /* Dump the stabs sections from an object file that has a section that
1052 uses Sun stabs encoding. It has to use some hooks into BFD because
1053 string table sections are not normally visible to BFD callers. */
1059 /* Allocate and initialize stab name array if first time. */
1060 if (stab_name == NULL)
1064 stab_name = (char **) xmalloc (256 * sizeof(char *));
1065 /* Clear the array. */
1066 for (i = 0; i < 256; i++)
1067 stab_name[i] = NULL;
1068 /* Fill in the defined stabs. */
1069 for (i = 0; *stab_print[i].string; i++)
1070 stab_name[stab_print[i].value] = stab_print[i].string;
1073 dump_section_stabs (abfd, ".stab", ".stabstr");
1074 dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
1075 dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
1076 dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1079 static struct internal_nlist *stabs;
1080 static bfd_size_type stab_size;
1082 static char *strtab;
1083 static bfd_size_type stabstr_size;
1085 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1086 and string table section STRSECT_NAME into `strtab'.
1087 If the section exists and was read, allocate the space and return true.
1088 Otherwise return false. */
1091 read_section_stabs (abfd, stabsect_name, strsect_name)
1093 char *stabsect_name;
1096 asection *stabsect, *stabstrsect;
1098 stabsect = bfd_get_section_by_name (abfd, stabsect_name);
1101 printf ("No %s section present\n\n", stabsect_name);
1105 stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
1106 if (0 == stabstrsect)
1108 fprintf (stderr, "%s: %s has no %s section\n", program_name,
1109 bfd_get_filename (abfd), strsect_name);
1113 stab_size = bfd_section_size (abfd, stabsect);
1114 stabstr_size = bfd_section_size (abfd, stabstrsect);
1116 stabs = (struct internal_nlist *) xmalloc (stab_size);
1117 strtab = (char *) xmalloc (stabstr_size);
1119 if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
1121 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1122 program_name, stabsect_name, bfd_get_filename (abfd),
1123 bfd_errmsg (bfd_get_error ()));
1129 if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
1132 fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1133 program_name, strsect_name, bfd_get_filename (abfd),
1134 bfd_errmsg (bfd_get_error ()));
1143 #define SWAP_SYMBOL(symp, abfd) \
1145 (symp)->n_strx = bfd_h_get_32(abfd, \
1146 (unsigned char *)&(symp)->n_strx); \
1147 (symp)->n_desc = bfd_h_get_16 (abfd, \
1148 (unsigned char *)&(symp)->n_desc); \
1149 (symp)->n_value = bfd_h_get_32 (abfd, \
1150 (unsigned char *)&(symp)->n_value); \
1153 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1154 using string table section STRSECT_NAME (in `strtab'). */
1157 print_section_stabs (abfd, stabsect_name, strsect_name)
1159 char *stabsect_name;
1163 unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
1164 struct internal_nlist *stabp = stabs,
1165 *stabs_end = (struct internal_nlist *) (stab_size + (char *) stabs);
1167 printf ("Contents of %s section:\n\n", stabsect_name);
1168 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1170 /* Loop through all symbols and print them.
1172 We start the index at -1 because there is a dummy symbol on
1173 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1175 for (i = -1; stabp < stabs_end; stabp++, i++)
1177 SWAP_SYMBOL (stabp, abfd);
1178 printf ("\n%-6d ", i);
1179 /* Either print the stab name, or, if unnamed, print its number
1180 again (makes consistent formatting for tools like awk). */
1181 if (stab_name[stabp->n_type])
1182 printf ("%-6s", stab_name[stabp->n_type]);
1183 else if (stabp->n_type == N_UNDF)
1186 printf ("%-6d", stabp->n_type);
1187 printf (" %-6d %-6d ", stabp->n_other, stabp->n_desc);
1188 printf_vma (stabp->n_value);
1189 printf (" %-6lu", stabp->n_strx);
1191 /* Symbols with type == 0 (N_UNDF) specify the length of the
1192 string table associated with this file. We use that info
1193 to know how to relocate the *next* file's string table indices. */
1195 if (stabp->n_type == N_UNDF)
1197 file_string_table_offset = next_file_string_table_offset;
1198 next_file_string_table_offset += stabp->n_value;
1202 /* Using the (possibly updated) string table offset, print the
1203 string (if any) associated with this symbol. */
1205 if ((stabp->n_strx + file_string_table_offset) < stabstr_size)
1206 printf (" %s", &strtab[stabp->n_strx + file_string_table_offset]);
1215 dump_section_stabs (abfd, stabsect_name, strsect_name)
1217 char *stabsect_name;
1222 /* Check for section names for which stabsect_name is a prefix, to
1223 handle .stab0, etc. */
1224 for (s = abfd->sections;
1228 if (strncmp (stabsect_name, s->name, strlen (stabsect_name)) == 0
1229 && strncmp (strsect_name, s->name, strlen (strsect_name)) != 0)
1231 if (read_section_stabs (abfd, s->name, strsect_name))
1233 print_section_stabs (abfd, s->name, strsect_name);
1242 dump_bfd_header (abfd)
1247 printf ("architecture: %s, ",
1248 bfd_printable_arch_mach (bfd_get_arch (abfd),
1249 bfd_get_mach (abfd)));
1250 printf ("flags 0x%08x:\n", abfd->flags);
1252 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1253 PF (HAS_RELOC, "HAS_RELOC");
1254 PF (EXEC_P, "EXEC_P");
1255 PF (HAS_LINENO, "HAS_LINENO");
1256 PF (HAS_DEBUG, "HAS_DEBUG");
1257 PF (HAS_SYMS, "HAS_SYMS");
1258 PF (HAS_LOCALS, "HAS_LOCALS");
1259 PF (DYNAMIC, "DYNAMIC");
1260 PF (WP_TEXT, "WP_TEXT");
1261 PF (D_PAGED, "D_PAGED");
1262 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
1263 printf ("\nstart address 0x");
1264 printf_vma (abfd->start_address);
1268 dump_bfd_private_header (abfd)
1271 bfd_print_private_bfd_data (abfd, stdout);
1279 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
1281 bfd_nonfatal (bfd_get_filename (abfd));
1282 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1284 list_matching_formats (matching);
1290 printf ("\n%s: file format %s\n", bfd_get_filename (abfd),
1293 print_arelt_descr (stdout, abfd, true);
1294 if (dump_file_header)
1295 dump_bfd_header (abfd);
1296 if (dump_private_headers)
1297 dump_bfd_private_header (abfd);
1299 if (dump_section_headers)
1300 dump_headers (abfd);
1301 if (dump_symtab || dump_reloc_info || disassemble)
1303 syms = slurp_symtab (abfd);
1305 if (dump_dynamic_symtab || dump_dynamic_reloc_info)
1307 dynsyms = slurp_dynamic_symtab (abfd);
1310 dump_symbols (abfd, false);
1311 if (dump_dynamic_symtab)
1312 dump_symbols (abfd, true);
1313 if (dump_stab_section_info)
1315 if (dump_reloc_info && ! disassemble)
1317 if (dump_dynamic_reloc_info)
1318 dump_dynamic_relocs (abfd);
1319 if (dump_section_contents)
1322 disassemble_data (abfd);
1336 display_file (filename, target)
1340 bfd *file, *arfile = (bfd *) NULL;
1342 file = bfd_openr (filename, target);
1345 bfd_nonfatal (filename);
1349 if (bfd_check_format (file, bfd_archive) == true)
1351 bfd *last_arfile = NULL;
1353 printf ("In archive %s:\n", bfd_get_filename (file));
1356 bfd_set_error (bfd_error_no_error);
1358 arfile = bfd_openr_next_archived_file (file, arfile);
1361 if (bfd_get_error () != bfd_error_no_more_archived_files)
1363 bfd_nonfatal (bfd_get_filename (file));
1368 display_bfd (arfile);
1370 if (last_arfile != NULL)
1371 bfd_close (last_arfile);
1372 last_arfile = arfile;
1375 if (last_arfile != NULL)
1376 bfd_close (last_arfile);
1384 /* Actually display the various requested regions */
1392 bfd_size_type datasize = 0;
1394 bfd_size_type start, stop;
1396 for (section = abfd->sections; section != NULL; section =
1401 if (only == (char *) NULL ||
1402 strcmp (only, section->name) == 0)
1404 if (section->flags & SEC_HAS_CONTENTS)
1406 printf ("Contents of section %s:\n", section->name);
1408 if (bfd_section_size (abfd, section) == 0)
1410 data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
1411 datasize = bfd_section_size (abfd, section);
1414 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
1416 if (start_address == (bfd_vma) -1
1417 || start_address < section->vma)
1420 start = start_address - section->vma;
1421 if (stop_address == (bfd_vma) -1)
1422 stop = bfd_section_size (abfd, section);
1425 if (stop_address < section->vma)
1428 stop = stop_address - section->vma;
1429 if (stop > bfd_section_size (abfd, section))
1430 stop = bfd_section_size (abfd, section);
1432 for (i = start; i < stop; i += onaline)
1436 printf (" %04lx ", (unsigned long int) (i + section->vma));
1437 for (j = i; j < i + onaline; j++)
1440 printf ("%02x", (unsigned) (data[j]));
1448 for (j = i; j < i + onaline; j++)
1453 printf ("%c", isprint (data[j]) ? data[j] : '.');
1463 /* Should perhaps share code and display with nm? */
1465 dump_symbols (abfd, dynamic)
1479 printf ("DYNAMIC SYMBOL TABLE:\n");
1487 printf ("SYMBOL TABLE:\n");
1490 for (count = 0; count < max; count++)
1494 bfd *cur_bfd = bfd_asymbol_bfd(*current);
1497 bfd_print_symbol (cur_bfd,
1499 *current, bfd_print_symbol_all);
1517 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
1521 if (bfd_is_abs_section (a))
1523 if (bfd_is_und_section (a))
1525 if (bfd_is_com_section (a))
1530 if (strcmp (only, a->name))
1533 else if ((a->flags & SEC_RELOC) == 0)
1536 relsize = bfd_get_reloc_upper_bound (abfd, a);
1538 bfd_fatal (bfd_get_filename (abfd));
1540 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
1544 printf (" (none)\n\n");
1548 relpp = (arelent **) xmalloc (relsize);
1549 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
1551 bfd_fatal (bfd_get_filename (abfd));
1552 else if (relcount == 0)
1554 printf (" (none)\n\n");
1559 dump_reloc_set (abfd, relpp, relcount);
1568 dump_dynamic_relocs (abfd)
1575 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
1577 bfd_fatal (bfd_get_filename (abfd));
1579 printf ("DYNAMIC RELOCATION RECORDS");
1583 printf (" (none)\n\n");
1587 relpp = (arelent **) xmalloc (relsize);
1588 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
1590 bfd_fatal (bfd_get_filename (abfd));
1591 else if (relcount == 0)
1593 printf (" (none)\n\n");
1598 dump_reloc_set (abfd, relpp, relcount);
1606 dump_reloc_set (abfd, relpp, relcount)
1613 /* Get column headers lined up reasonably. */
1619 sprintf_vma (buf, (bfd_vma) -1);
1620 width = strlen (buf) - 7;
1622 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
1625 for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
1628 CONST char *sym_name;
1629 CONST char *section_name;
1631 if (start_address != (bfd_vma) -1
1632 && q->address < start_address)
1634 if (stop_address != (bfd_vma) -1
1635 && q->address > stop_address)
1638 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
1640 sym_name = (*(q->sym_ptr_ptr))->name;
1641 section_name = (*(q->sym_ptr_ptr))->section->name;
1646 section_name = NULL;
1650 printf_vma (q->address);
1651 printf (" %-16s %s",
1657 if (section_name == (CONST char *) NULL)
1658 section_name = "*unknown*";
1659 printf_vma (q->address);
1660 printf (" %-16s [%s]",
1667 printf_vma (q->addend);
1673 /* The length of the longest architecture name + 1. */
1674 #define LONGEST_ARCH sizeof("rs6000:6000")
1680 /* List the targets that BFD is configured to support, each followed
1681 by its endianness and the architectures it supports. */
1684 display_target_list ()
1686 extern char *tmpnam ();
1687 extern bfd_target *bfd_target_vector[];
1688 char tmparg[L_tmpnam];
1692 dummy_name = tmpnam (tmparg);
1693 for (t = 0; bfd_target_vector[t]; t++)
1695 bfd_target *p = bfd_target_vector[t];
1696 bfd *abfd = bfd_openw (dummy_name, p->name);
1699 printf ("%s\n (header %s, data %s)\n", p->name,
1700 p->header_byteorder_big_p ? "big endian" : "little endian",
1701 p->byteorder_big_p ? "big endian" : "little endian");
1705 bfd_nonfatal (dummy_name);
1709 if (! bfd_set_format (abfd, bfd_object))
1711 if (bfd_get_error () != bfd_error_invalid_operation)
1712 bfd_nonfatal (p->name);
1716 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1717 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
1719 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
1721 unlink (dummy_name);
1724 /* Print a table showing which architectures are supported for entries
1725 FIRST through LAST-1 of bfd_target_vector (targets across,
1726 architectures down). */
1729 display_info_table (first, last)
1733 extern bfd_target *bfd_target_vector[];
1734 extern char *tmpnam ();
1735 char tmparg[L_tmpnam];
1739 /* Print heading of target names. */
1740 printf ("\n%*s", (int) LONGEST_ARCH, " ");
1741 for (t = first; t < last && bfd_target_vector[t]; t++)
1742 printf ("%s ", bfd_target_vector[t]->name);
1745 dummy_name = tmpnam (tmparg);
1746 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1747 if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
1749 printf ("%*s ", (int) LONGEST_ARCH - 1,
1750 bfd_printable_arch_mach (a, 0));
1751 for (t = first; t < last && bfd_target_vector[t]; t++)
1753 bfd_target *p = bfd_target_vector[t];
1755 bfd *abfd = bfd_openw (dummy_name, p->name);
1759 bfd_nonfatal (p->name);
1765 if (! bfd_set_format (abfd, bfd_object))
1767 if (bfd_get_error () != bfd_error_invalid_operation)
1768 bfd_nonfatal (p->name);
1775 if (! bfd_set_arch_mach (abfd, a, 0))
1780 printf ("%s ", p->name);
1783 int l = strlen (p->name);
1791 unlink (dummy_name);
1794 /* Print tables of all the target-architecture combinations that
1795 BFD has been configured to support. */
1798 display_target_tables ()
1801 extern bfd_target *bfd_target_vector[];
1803 extern char *getenv ();
1806 colum = getenv ("COLUMNS");
1808 columns = atoi (colum);
1813 while (bfd_target_vector[t] != NULL)
1817 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
1819 while (wid < columns && bfd_target_vector[t] != NULL)
1823 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
1824 if (newwid >= columns)
1829 display_info_table (oldt, t);
1836 printf ("BFD header file version %s\n", BFD_VERSION);
1837 display_target_list ();
1838 display_target_tables ();
1847 char *target = default_target;
1848 boolean seenflag = false;
1850 program_name = *argv;
1851 xmalloc_set_program_name (program_name);
1853 START_PROGRESS (program_name, 0);
1857 while ((c = getopt_long (argc, argv, "pib:m:VdDlfahrRtTxsSj:w", long_options,
1861 if (c != 'l' && c != OPTION_START_ADDRESS && c != OPTION_STOP_ADDRESS)
1866 break; /* we've been given a long option */
1874 with_line_numbers = 1;
1880 dump_file_header = true;
1883 formats_info = true;
1886 dump_private_headers = 1;
1889 dump_private_headers = 1;
1891 dump_reloc_info = 1;
1892 dump_file_header = true;
1894 dump_section_headers = 1;
1900 dump_dynamic_symtab = 1;
1906 disassemble = disassemble_all = true;
1910 with_source_code = true;
1913 dump_section_contents = 1;
1916 dump_reloc_info = 1;
1919 dump_dynamic_reloc_info = 1;
1925 dump_section_headers = 1;
1935 case OPTION_START_ADDRESS:
1936 start_address = parse_vma (optarg, "--start-address");
1938 case OPTION_STOP_ADDRESS:
1939 stop_address = parse_vma (optarg, "--stop-address");
1948 printf ("GNU %s version %s\n", program_name, program_version);
1952 if (seenflag == false)
1962 display_file ("a.out", target);
1964 for (; optind < argc;)
1965 display_file (argv[optind++], target);
1968 END_PROGRESS (program_name);