1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 Objdump displays information about one or more object files, either on
25 their own, or inside libraries. It is commonly used as a disassembler,
26 but it can also display information about file headers, symbol tables,
27 relocations, debugging directives and more.
29 The flow of execution is as follows:
31 1. Command line arguments are checked for control switches and the
32 information to be displayed is selected.
34 2. Any remaining arguments are assumed to be object files, and they are
35 processed in order by display_bfd(). If the file is an archive each
36 of its elements is processed in turn.
38 3. The file's target architecture and binary file format are determined
39 by bfd_check_format(). If they are recognised, then dump_bfd() is
42 4. dump_bfd() in turn calls separate functions to display the requested
43 item(s) of information(s). For example disassemble_data() is called if
44 a disassembly has been requested.
46 When disassembling the code loops through blocks of instructions bounded
47 by symbols, calling disassemble_bytes() on each block. The actual
48 disassembling is done by the libopcodes library, via a function pointer
49 supplied by the disassembler() function. */
57 #include "safe-ctype.h"
59 #include "libiberty.h"
64 /* Internal headers for the ELF .stab-dump code - sorry. */
65 #define BYTES_IN_WORD 32
66 #include "aout/aout64.h"
68 #ifdef NEED_DECLARATION_FPRINTF
69 /* This is needed by init_disassemble_info(). */
70 extern int fprintf (FILE *, const char *, ...);
74 static int exit_status = 0;
76 static char *default_target = NULL; /* Default at runtime. */
78 /* The following variables are set based on arguments passed on the
80 static int show_version = 0; /* Show the version number. */
81 static int dump_section_contents; /* -s */
82 static int dump_section_headers; /* -h */
83 static bfd_boolean dump_file_header; /* -f */
84 static int dump_symtab; /* -t */
85 static int dump_dynamic_symtab; /* -T */
86 static int dump_reloc_info; /* -r */
87 static int dump_dynamic_reloc_info; /* -R */
88 static int dump_ar_hdrs; /* -a */
89 static int dump_private_headers; /* -p */
90 static int prefix_addresses; /* --prefix-addresses */
91 static int with_line_numbers; /* -l */
92 static bfd_boolean with_source_code; /* -S */
93 static int show_raw_insn; /* --show-raw-insn */
94 static int dump_stab_section_info; /* --stabs */
95 static int do_demangle; /* -C, --demangle */
96 static bfd_boolean disassemble; /* -d */
97 static bfd_boolean disassemble_all; /* -D */
98 static int disassemble_zeroes; /* --disassemble-zeroes */
99 static bfd_boolean formats_info; /* -i */
100 static int wide_output; /* -w */
101 static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
102 static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
103 static int dump_debugging; /* --debugging */
104 static int dump_debugging_tags; /* --debugging-tags */
105 static int dump_special_syms = 0; /* --special-syms */
106 static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
107 static int file_start_context = 0; /* --file-start-context */
109 /* Pointer to an array of section names provided by
110 one or more "-j secname" command line options. */
112 /* The total number of slots in the only[] array. */
113 static size_t only_size = 0;
114 /* The number of occupied slots in the only[] array. */
115 static size_t only_used = 0;
117 /* Variables for handling include file path table. */
118 static const char **include_paths;
119 static int include_path_count;
121 /* Extra info to pass to the section disassembler and address printing
123 struct objdump_disasm_info
127 bfd_boolean require_sec;
128 arelent ** dynrelbuf;
130 disassembler_ftype disassemble_fn;
133 /* Architecture to disassemble for, or default if NULL. */
134 static char *machine = NULL;
136 /* Target specific options to the disassembler. */
137 static char *disassembler_options = NULL;
139 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
140 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
142 /* The symbol table. */
143 static asymbol **syms;
145 /* Number of symbols in `syms'. */
146 static long symcount = 0;
148 /* The sorted symbol table. */
149 static asymbol **sorted_syms;
151 /* Number of symbols in `sorted_syms'. */
152 static long sorted_symcount = 0;
154 /* The dynamic symbol table. */
155 static asymbol **dynsyms;
157 /* The synthetic symbol table. */
158 static asymbol *synthsyms;
159 static long synthcount = 0;
161 /* Number of symbols in `dynsyms'. */
162 static long dynsymcount = 0;
164 static bfd_byte *stabs;
165 static bfd_size_type stab_size;
168 static bfd_size_type stabstr_size;
171 usage (FILE *stream, int status)
173 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
174 fprintf (stream, _(" Display information from object <file(s)>.\n"));
175 fprintf (stream, _(" At least one of the following switches must be given:\n"));
176 fprintf (stream, _("\
177 -a, --archive-headers Display archive header information\n\
178 -f, --file-headers Display the contents of the overall file header\n\
179 -p, --private-headers Display object format specific file header contents\n\
180 -h, --[section-]headers Display the contents of the section headers\n\
181 -x, --all-headers Display the contents of all headers\n\
182 -d, --disassemble Display assembler contents of executable sections\n\
183 -D, --disassemble-all Display assembler contents of all sections\n\
184 -S, --source Intermix source code with disassembly\n\
185 -s, --full-contents Display the full contents of all sections requested\n\
186 -g, --debugging Display debug information in object file\n\
187 -e, --debugging-tags Display debug information using ctags style\n\
188 -G, --stabs Display (in raw form) any STABS info in the file\n\
189 -t, --syms Display the contents of the symbol table(s)\n\
190 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
191 -r, --reloc Display the relocation entries in the file\n\
192 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
193 -v, --version Display this program's version number\n\
194 -i, --info List object formats and architectures supported\n\
195 -H, --help Display this information\n\
199 fprintf (stream, _("\n The following switches are optional:\n"));
200 fprintf (stream, _("\
201 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
202 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
203 -j, --section=NAME Only display information for section NAME\n\
204 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
205 -EB --endian=big Assume big endian format when disassembling\n\
206 -EL --endian=little Assume little endian format when disassembling\n\
207 --file-start-context Include context from start of file (with -S)\n\
208 -I, --include=DIR Add DIR to search list for source files\n\
209 -l, --line-numbers Include line numbers and filenames in output\n\
210 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
211 The STYLE, if specified, can be `auto', `gnu',\n\
212 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
214 -w, --wide Format output for more than 80 columns\n\
215 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
216 --start-address=ADDR Only process data whose address is >= ADDR\n\
217 --stop-address=ADDR Only process data whose address is <= ADDR\n\
218 --prefix-addresses Print complete address alongside disassembly\n\
219 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
220 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
221 --special-syms Include special symbols in symbol dumps\n\
223 list_supported_targets (program_name, stream);
224 list_supported_architectures (program_name, stream);
226 disassembler_usage (stream);
229 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
233 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
237 OPTION_START_ADDRESS,
242 static struct option long_options[]=
244 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
245 {"all-headers", no_argument, NULL, 'x'},
246 {"private-headers", no_argument, NULL, 'p'},
247 {"architecture", required_argument, NULL, 'm'},
248 {"archive-headers", no_argument, NULL, 'a'},
249 {"debugging", no_argument, NULL, 'g'},
250 {"debugging-tags", no_argument, NULL, 'e'},
251 {"demangle", optional_argument, NULL, 'C'},
252 {"disassemble", no_argument, NULL, 'd'},
253 {"disassemble-all", no_argument, NULL, 'D'},
254 {"disassembler-options", required_argument, NULL, 'M'},
255 {"disassemble-zeroes", no_argument, NULL, 'z'},
256 {"dynamic-reloc", no_argument, NULL, 'R'},
257 {"dynamic-syms", no_argument, NULL, 'T'},
258 {"endian", required_argument, NULL, OPTION_ENDIAN},
259 {"file-headers", no_argument, NULL, 'f'},
260 {"file-start-context", no_argument, &file_start_context, 1},
261 {"full-contents", no_argument, NULL, 's'},
262 {"headers", no_argument, NULL, 'h'},
263 {"help", no_argument, NULL, 'H'},
264 {"info", no_argument, NULL, 'i'},
265 {"line-numbers", no_argument, NULL, 'l'},
266 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
267 {"prefix-addresses", no_argument, &prefix_addresses, 1},
268 {"reloc", no_argument, NULL, 'r'},
269 {"section", required_argument, NULL, 'j'},
270 {"section-headers", no_argument, NULL, 'h'},
271 {"show-raw-insn", no_argument, &show_raw_insn, 1},
272 {"source", no_argument, NULL, 'S'},
273 {"special-syms", no_argument, &dump_special_syms, 1},
274 {"include", required_argument, NULL, 'I'},
275 {"stabs", no_argument, NULL, 'G'},
276 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
277 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
278 {"syms", no_argument, NULL, 't'},
279 {"target", required_argument, NULL, 'b'},
280 {"version", no_argument, NULL, 'V'},
281 {"wide", no_argument, NULL, 'w'},
282 {0, no_argument, 0, 0}
286 nonfatal (const char *msg)
293 dump_section_header (bfd *abfd, asection *section,
294 void *ignored ATTRIBUTE_UNUSED)
297 unsigned int opb = bfd_octets_per_byte (abfd);
299 printf ("%3d %-13s %08lx ", section->index,
300 bfd_get_section_name (abfd, section),
301 (unsigned long) bfd_section_size (abfd, section) / opb);
302 bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
304 bfd_printf_vma (abfd, section->lma);
305 printf (" %08lx 2**%u", (unsigned long) section->filepos,
306 bfd_get_section_alignment (abfd, section));
312 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
314 PF (SEC_HAS_CONTENTS, "CONTENTS");
315 PF (SEC_ALLOC, "ALLOC");
316 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
317 PF (SEC_LOAD, "LOAD");
318 PF (SEC_RELOC, "RELOC");
319 PF (SEC_READONLY, "READONLY");
320 PF (SEC_CODE, "CODE");
321 PF (SEC_DATA, "DATA");
323 PF (SEC_DEBUGGING, "DEBUGGING");
324 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
325 PF (SEC_EXCLUDE, "EXCLUDE");
326 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
327 if (bfd_get_arch (abfd) == bfd_arch_tic54x)
329 PF (SEC_TIC54X_BLOCK, "BLOCK");
330 PF (SEC_TIC54X_CLINK, "CLINK");
332 PF (SEC_SMALL_DATA, "SMALL_DATA");
333 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
334 PF (SEC_COFF_SHARED, "SHARED");
335 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
337 if ((section->flags & SEC_LINK_ONCE) != 0)
340 struct coff_comdat_info *comdat;
342 switch (section->flags & SEC_LINK_DUPLICATES)
346 case SEC_LINK_DUPLICATES_DISCARD:
347 ls = "LINK_ONCE_DISCARD";
349 case SEC_LINK_DUPLICATES_ONE_ONLY:
350 ls = "LINK_ONCE_ONE_ONLY";
352 case SEC_LINK_DUPLICATES_SAME_SIZE:
353 ls = "LINK_ONCE_SAME_SIZE";
355 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
356 ls = "LINK_ONCE_SAME_CONTENTS";
359 printf ("%s%s", comma, ls);
361 comdat = bfd_coff_get_comdat_section (abfd, section);
363 printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
373 dump_headers (bfd *abfd)
375 printf (_("Sections:\n"));
378 printf (_("Idx Name Size VMA LMA File off Algn"));
380 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
381 if (bfd_get_arch_size (abfd) == 32)
382 printf (_("Idx Name Size VMA LMA File off Algn"));
384 printf (_("Idx Name Size VMA LMA File off Algn"));
388 printf (_(" Flags"));
389 if (abfd->flags & HAS_LOAD_PAGE)
393 bfd_map_over_sections (abfd, dump_section_header, NULL);
397 slurp_symtab (bfd *abfd)
402 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
408 storage = bfd_get_symtab_upper_bound (abfd);
410 bfd_fatal (bfd_get_filename (abfd));
412 sy = xmalloc (storage);
414 symcount = bfd_canonicalize_symtab (abfd, sy);
416 bfd_fatal (bfd_get_filename (abfd));
420 /* Read in the dynamic symbols. */
423 slurp_dynamic_symtab (bfd *abfd)
428 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
431 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
433 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
438 bfd_fatal (bfd_get_filename (abfd));
441 sy = xmalloc (storage);
443 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
445 bfd_fatal (bfd_get_filename (abfd));
449 /* Filter out (in place) symbols that are useless for disassembly.
450 COUNT is the number of elements in SYMBOLS.
451 Return the number of useful symbols. */
454 remove_useless_symbols (asymbol **symbols, long count)
456 asymbol **in_ptr = symbols, **out_ptr = symbols;
460 asymbol *sym = *in_ptr++;
462 if (sym->name == NULL || sym->name[0] == '\0')
464 if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
466 if (bfd_is_und_section (sym->section)
467 || bfd_is_com_section (sym->section))
472 return out_ptr - symbols;
475 /* Sort symbols into value order. */
478 compare_symbols (const void *ap, const void *bp)
480 const asymbol *a = * (const asymbol **) ap;
481 const asymbol *b = * (const asymbol **) bp;
491 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
493 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
496 if (a->section > b->section)
498 else if (a->section < b->section)
501 an = bfd_asymbol_name (a);
502 bn = bfd_asymbol_name (b);
506 /* The symbols gnu_compiled and gcc2_compiled convey no real
507 information, so put them after other symbols with the same value. */
508 af = (strstr (an, "gnu_compiled") != NULL
509 || strstr (an, "gcc2_compiled") != NULL);
510 bf = (strstr (bn, "gnu_compiled") != NULL
511 || strstr (bn, "gcc2_compiled") != NULL);
518 /* We use a heuristic for the file name, to try to sort it after
519 more useful symbols. It may not work on non Unix systems, but it
520 doesn't really matter; the only difference is precisely which
521 symbol names get printed. */
523 #define file_symbol(s, sn, snl) \
524 (((s)->flags & BSF_FILE) != 0 \
525 || ((sn)[(snl) - 2] == '.' \
526 && ((sn)[(snl) - 1] == 'o' \
527 || (sn)[(snl) - 1] == 'a')))
529 af = file_symbol (a, an, anl);
530 bf = file_symbol (b, bn, bnl);
537 /* Try to sort global symbols before local symbols before function
538 symbols before debugging symbols. */
543 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
545 if ((aflags & BSF_DEBUGGING) != 0)
550 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
552 if ((aflags & BSF_FUNCTION) != 0)
557 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
559 if ((aflags & BSF_LOCAL) != 0)
564 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
566 if ((aflags & BSF_GLOBAL) != 0)
572 /* Symbols that start with '.' might be section names, so sort them
573 after symbols that don't start with '.'. */
574 if (an[0] == '.' && bn[0] != '.')
576 if (an[0] != '.' && bn[0] == '.')
579 /* Finally, if we can't distinguish them in any other way, try to
580 get consistent results by sorting the symbols by name. */
581 return strcmp (an, bn);
584 /* Sort relocs into address order. */
587 compare_relocs (const void *ap, const void *bp)
589 const arelent *a = * (const arelent **) ap;
590 const arelent *b = * (const arelent **) bp;
592 if (a->address > b->address)
594 else if (a->address < b->address)
597 /* So that associated relocations tied to the same address show up
598 in the correct order, we don't do any further sorting. */
607 /* Print an address (VMA) to the output stream in INFO.
608 If SKIP_ZEROES is TRUE, omit leading zeroes. */
611 objdump_print_value (bfd_vma vma, struct disassemble_info *info,
612 bfd_boolean skip_zeroes)
616 struct objdump_disasm_info *aux;
618 aux = (struct objdump_disasm_info *) info->application_data;
619 bfd_sprintf_vma (aux->abfd, buf, vma);
624 for (p = buf; *p == '0'; ++p)
629 (*info->fprintf_func) (info->stream, "%s", p);
632 /* Print the name of a symbol. */
635 objdump_print_symname (bfd *abfd, struct disassemble_info *info,
642 name = bfd_asymbol_name (sym);
643 if (do_demangle && name[0] != '\0')
645 /* Demangle the name. */
646 alloc = demangle (abfd, name);
651 (*info->fprintf_func) (info->stream, "%s", name);
659 /* Locate a symbol given a bfd and a section (from INFO->application_data),
660 and a VMA. If INFO->application_data->require_sec is TRUE, then always
661 require the symbol to be in the section. Returns NULL if there is no
662 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
663 of the symbol in sorted_syms. */
666 find_symbol_for_address (bfd_vma vma,
667 struct disassemble_info *info,
670 /* @@ Would it speed things up to cache the last two symbols returned,
671 and maybe their address ranges? For many processors, only one memory
672 operand can be present at a time, so the 2-entry cache wouldn't be
673 constantly churned by code doing heavy memory accesses. */
675 /* Indices in `sorted_syms'. */
677 long max = sorted_symcount;
679 struct objdump_disasm_info *aux;
684 if (sorted_symcount < 1)
687 aux = (struct objdump_disasm_info *) info->application_data;
690 opb = bfd_octets_per_byte (abfd);
692 /* Perform a binary search looking for the closest symbol to the
693 required value. We are searching the range (min, max]. */
694 while (min + 1 < max)
698 thisplace = (max + min) / 2;
699 sym = sorted_syms[thisplace];
701 if (bfd_asymbol_value (sym) > vma)
703 else if (bfd_asymbol_value (sym) < vma)
712 /* The symbol we want is now in min, the low end of the range we
713 were searching. If there are several symbols with the same
714 value, we want the first one. */
717 && (bfd_asymbol_value (sorted_syms[thisplace])
718 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
721 /* If the file is relocatable, and the symbol could be from this
722 section, prefer a symbol from this section over symbols from
723 others, even if the other symbol's value might be closer.
725 Note that this may be wrong for some symbol references if the
726 sections have overlapping memory ranges, but in that case there's
727 no way to tell what's desired without looking at the relocation
729 if (sorted_syms[thisplace]->section != sec
731 || ((abfd->flags & HAS_RELOC) != 0
732 && vma >= bfd_get_section_vma (abfd, sec)
733 && vma < (bfd_get_section_vma (abfd, sec)
734 + bfd_section_size (abfd, sec) / opb))))
738 for (i = thisplace + 1; i < sorted_symcount; i++)
740 if (bfd_asymbol_value (sorted_syms[i])
741 != bfd_asymbol_value (sorted_syms[thisplace]))
749 if (sorted_syms[i]->section == sec
751 || sorted_syms[i - 1]->section != sec
752 || (bfd_asymbol_value (sorted_syms[i])
753 != bfd_asymbol_value (sorted_syms[i - 1]))))
760 if (sorted_syms[thisplace]->section != sec)
762 /* We didn't find a good symbol with a smaller value.
763 Look for one with a larger value. */
764 for (i = thisplace + 1; i < sorted_symcount; i++)
766 if (sorted_syms[i]->section == sec)
774 if (sorted_syms[thisplace]->section != sec
776 || ((abfd->flags & HAS_RELOC) != 0
777 && vma >= bfd_get_section_vma (abfd, sec)
778 && vma < (bfd_get_section_vma (abfd, sec)
779 + bfd_section_size (abfd, sec)))))
780 /* There is no suitable symbol. */
784 /* Give the target a chance to reject the symbol. */
785 while (! info->symbol_is_valid (sorted_syms [thisplace], info))
788 if (thisplace >= sorted_symcount
789 || bfd_asymbol_value (sorted_syms [thisplace]) > vma)
796 return sorted_syms[thisplace];
799 /* Print an address and the offset to the nearest symbol. */
802 objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
803 bfd_vma vma, struct disassemble_info *info,
804 bfd_boolean skip_zeroes)
806 objdump_print_value (vma, info, skip_zeroes);
812 (*info->fprintf_func) (info->stream, " <%s",
813 bfd_get_section_name (abfd, sec));
814 secaddr = bfd_get_section_vma (abfd, sec);
817 (*info->fprintf_func) (info->stream, "-0x");
818 objdump_print_value (secaddr - vma, info, TRUE);
820 else if (vma > secaddr)
822 (*info->fprintf_func) (info->stream, "+0x");
823 objdump_print_value (vma - secaddr, info, TRUE);
825 (*info->fprintf_func) (info->stream, ">");
829 (*info->fprintf_func) (info->stream, " <");
830 objdump_print_symname (abfd, info, sym);
831 if (bfd_asymbol_value (sym) > vma)
833 (*info->fprintf_func) (info->stream, "-0x");
834 objdump_print_value (bfd_asymbol_value (sym) - vma, info, TRUE);
836 else if (vma > bfd_asymbol_value (sym))
838 (*info->fprintf_func) (info->stream, "+0x");
839 objdump_print_value (vma - bfd_asymbol_value (sym), info, TRUE);
841 (*info->fprintf_func) (info->stream, ">");
845 /* Print an address (VMA), symbolically if possible.
846 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
849 objdump_print_addr (bfd_vma vma,
850 struct disassemble_info *info,
851 bfd_boolean skip_zeroes)
853 struct objdump_disasm_info *aux;
856 if (sorted_symcount < 1)
858 (*info->fprintf_func) (info->stream, "0x");
859 objdump_print_value (vma, info, skip_zeroes);
863 aux = (struct objdump_disasm_info *) info->application_data;
864 sym = find_symbol_for_address (vma, info, NULL);
865 objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, info,
869 /* Print VMA to INFO. This function is passed to the disassembler
873 objdump_print_address (bfd_vma vma, struct disassemble_info *info)
875 objdump_print_addr (vma, info, ! prefix_addresses);
878 /* Determine of the given address has a symbol associated with it. */
881 objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * info)
885 sym = find_symbol_for_address (vma, info, NULL);
887 return (sym != NULL && (bfd_asymbol_value (sym) == vma));
890 /* Hold the last function name and the last line number we displayed
893 static char *prev_functionname;
894 static unsigned int prev_line;
896 /* We keep a list of all files that we have seen when doing a
897 disassembly with source, so that we know how much of the file to
898 display. This can be important for inlined functions. */
900 struct print_file_list
902 struct print_file_list *next;
903 const char *filename;
909 static struct print_file_list *print_files;
911 /* The number of preceding context lines to show when we start
912 displaying a file for the first time. */
914 #define SHOW_PRECEDING_CONTEXT_LINES (5)
916 /* Tries to open MODNAME, and if successful adds a node to print_files
917 linked list and returns that node. Returns NULL on failure. */
919 static struct print_file_list *
920 try_print_file_open (const char *origname, const char *modname)
922 struct print_file_list *p;
925 f = fopen (modname, "r");
929 if (print_files != NULL && print_files->f != NULL)
931 fclose (print_files->f);
932 print_files->f = NULL;
935 p = xmalloc (sizeof (struct print_file_list));
936 p->filename = origname;
937 p->modname = modname;
940 p->next = print_files;
945 /* If the the source file, as described in the symtab, is not found
946 try to locate it in one of the paths specified with -I
947 If found, add location to print_files linked list. */
949 static struct print_file_list *
950 update_source_path (const char *filename)
952 struct print_file_list *p;
956 if (filename == NULL)
959 p = try_print_file_open (filename, filename);
963 if (include_path_count == 0)
966 /* Get the name of the file. */
967 fname = strrchr (filename, '/');
968 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
970 /* We could have a mixed forward/back slash case. */
971 char *backslash = strrchr (filename, '\\');
972 if (fname == NULL || (backslash != NULL && backslash > fname))
974 if (fname == NULL && filename[0] != '\0' && filename[1] == ':')
975 fname = filename + 1;
983 /* If file exists under a new path, we need to add it to the list
984 so that show_line knows about it. */
985 for (i = 0; i < include_path_count; i++)
987 char *modname = concat (include_paths[i], "/", fname, (const char *) 0);
989 p = try_print_file_open (filename, modname);
999 /* Skip ahead to a given line in a file, optionally printing each
1003 skip_to_line (struct print_file_list *p, unsigned int line,
1006 while (p->line < line)
1010 if (fgets (buf, sizeof buf, p->f) == NULL)
1020 if (strchr (buf, '\n') != NULL)
1025 /* Show the line number, or the source line, in a disassembly
1029 show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
1031 const char *filename;
1032 const char *functionname;
1035 if (! with_line_numbers && ! with_source_code)
1038 if (! bfd_find_nearest_line (abfd, section, syms, addr_offset, &filename,
1039 &functionname, &line))
1042 if (filename != NULL && *filename == '\0')
1044 if (functionname != NULL && *functionname == '\0')
1045 functionname = NULL;
1047 if (with_line_numbers)
1049 if (functionname != NULL
1050 && (prev_functionname == NULL
1051 || strcmp (functionname, prev_functionname) != 0))
1052 printf ("%s():\n", functionname);
1053 if (line > 0 && line != prev_line)
1054 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
1057 if (with_source_code
1061 struct print_file_list **pp, *p;
1063 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
1064 if (strcmp ((*pp)->filename, filename) == 0)
1070 if (p != print_files)
1074 /* We have reencountered a file name which we saw
1075 earlier. This implies that either we are dumping out
1076 code from an included file, or the same file was
1077 linked in more than once. There are two common cases
1078 of an included file: inline functions in a header
1079 file, and a bison or flex skeleton file. In the
1080 former case we want to just start printing (but we
1081 back up a few lines to give context); in the latter
1082 case we want to continue from where we left off. I
1083 can't think of a good way to distinguish the cases,
1084 so I used a heuristic based on the file name. */
1085 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
1089 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1096 p->f = fopen (p->modname, "r");
1100 skip_to_line (p, l, FALSE);
1102 if (print_files->f != NULL)
1104 fclose (print_files->f);
1105 print_files->f = NULL;
1111 skip_to_line (p, line, TRUE);
1113 p->next = print_files;
1119 p = update_source_path (filename);
1125 if (file_start_context)
1128 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1131 skip_to_line (p, l, FALSE);
1133 skip_to_line (p, line, TRUE);
1138 if (functionname != NULL
1139 && (prev_functionname == NULL
1140 || strcmp (functionname, prev_functionname) != 0))
1142 if (prev_functionname != NULL)
1143 free (prev_functionname);
1144 prev_functionname = xmalloc (strlen (functionname) + 1);
1145 strcpy (prev_functionname, functionname);
1148 if (line > 0 && line != prev_line)
1152 /* Pseudo FILE object for strings. */
1160 /* sprintf to a "stream". */
1163 objdump_sprintf (SFILE *f, const char *format, ...)
1170 size_t space = f->alloc - f->pos;
1172 va_start (args, format);
1173 n = vsnprintf (f->buffer + f->pos, space, format, args);
1179 f->alloc = (f->alloc + n) * 2;
1180 f->buffer = xrealloc (f->buffer, f->alloc);
1187 /* Returns TRUE if the specified section should be dumped. */
1190 process_section_p (asection * section)
1197 for (i = 0; i < only_used; i++)
1198 if (strcmp (only [i], section->name) == 0)
1205 /* The number of zeroes we want to see before we start skipping them.
1206 The number is arbitrarily chosen. */
1208 #define DEFAULT_SKIP_ZEROES 8
1210 /* The number of zeroes to skip at the end of a section. If the
1211 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1212 SKIP_ZEROES, they will be disassembled. If there are fewer than
1213 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1214 attempt to avoid disassembling zeroes inserted by section
1217 #define DEFAULT_SKIP_ZEROES_AT_END 3
1219 /* Disassemble some data in memory between given values. */
1222 disassemble_bytes (struct disassemble_info * info,
1223 disassembler_ftype disassemble_fn,
1226 bfd_vma start_offset,
1227 bfd_vma stop_offset,
1230 arelent ** relppend)
1232 struct objdump_disasm_info *aux;
1234 int octets_per_line;
1235 bfd_boolean done_dot;
1236 int skip_addr_chars;
1237 bfd_vma addr_offset;
1238 unsigned int opb = info->octets_per_byte;
1239 unsigned int skip_zeroes = info->skip_zeroes;
1240 unsigned int skip_zeroes_at_end = info->skip_zeroes_at_end;
1243 aux = (struct objdump_disasm_info *) info->application_data;
1247 sfile.buffer = xmalloc (sfile.alloc);
1251 octets_per_line = 4;
1253 octets_per_line = 16;
1255 /* Figure out how many characters to skip at the start of an
1256 address, to make the disassembly look nicer. We discard leading
1257 zeroes in chunks of 4, ensuring that there is always a leading
1259 skip_addr_chars = 0;
1260 if (! prefix_addresses)
1268 + bfd_section_size (section->owner, section) / opb));
1270 while (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0'
1273 skip_addr_chars += 4;
1278 info->insn_info_valid = 0;
1281 addr_offset = start_offset;
1282 while (addr_offset < stop_offset)
1286 bfd_boolean need_nl = FALSE;
1288 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1290 for (z = addr_offset * opb; z < stop_offset * opb; z++)
1293 if (! disassemble_zeroes
1294 && (info->insn_info_valid == 0
1295 || info->branch_delay_insns == 0)
1296 && (z - addr_offset * opb >= skip_zeroes
1297 || (z == stop_offset * opb &&
1298 z - addr_offset * opb < skip_zeroes_at_end)))
1302 /* If there are more nonzero octets to follow, we only skip
1303 zeroes in multiples of 4, to try to avoid running over
1304 the start of an instruction which happens to start with
1306 if (z != stop_offset * opb)
1307 z = addr_offset * opb + ((z - addr_offset * opb) &~ 3);
1309 octets = z - addr_offset * opb;
1319 if (with_line_numbers || with_source_code)
1320 /* The line number tables will refer to unadjusted
1321 section VMAs, so we must undo any VMA modifications
1322 when calling show_line. */
1323 show_line (aux->abfd, section, addr_offset - adjust_section_vma);
1325 if (! prefix_addresses)
1329 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
1330 for (s = buf + skip_addr_chars; *s == '0'; s++)
1334 printf ("%s:\t", buf + skip_addr_chars);
1338 aux->require_sec = TRUE;
1339 objdump_print_address (section->vma + addr_offset, info);
1340 aux->require_sec = FALSE;
1347 info->fprintf_func = (fprintf_ftype) objdump_sprintf;
1348 info->stream = (FILE *) &sfile;
1349 info->bytes_per_line = 0;
1350 info->bytes_per_chunk = 0;
1352 #ifdef DISASSEMBLER_NEEDS_RELOCS
1353 /* FIXME: This is wrong. It tests the number of octets
1354 in the last instruction, not the current one. */
1355 if (*relppp < relppend
1356 && (**relppp)->address >= rel_offset + addr_offset
1357 && ((**relppp)->address
1358 < rel_offset + addr_offset + octets / opb))
1359 info->flags = INSN_HAS_RELOC;
1364 octets = (*disassemble_fn) (section->vma + addr_offset, info);
1365 info->fprintf_func = (fprintf_ftype) fprintf;
1366 info->stream = stdout;
1367 if (info->bytes_per_line != 0)
1368 octets_per_line = info->bytes_per_line;
1372 printf ("%s\n", sfile.buffer);
1380 octets = octets_per_line;
1381 if (addr_offset + octets / opb > stop_offset)
1382 octets = (stop_offset - addr_offset) * opb;
1384 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
1386 if (ISPRINT (data[j]))
1387 buf[j - addr_offset * opb] = data[j];
1389 buf[j - addr_offset * opb] = '.';
1391 buf[j - addr_offset * opb] = '\0';
1394 if (prefix_addresses
1396 : show_raw_insn >= 0)
1400 /* If ! prefix_addresses and ! wide_output, we print
1401 octets_per_line octets per line. */
1403 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
1404 pb = octets_per_line;
1406 if (info->bytes_per_chunk)
1407 bpc = info->bytes_per_chunk;
1411 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
1415 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1417 for (k = bpc - 1; k >= 0; k--)
1418 printf ("%02x", (unsigned) data[j + k]);
1423 for (k = 0; k < bpc; k++)
1424 printf ("%02x", (unsigned) data[j + k]);
1429 for (; pb < octets_per_line; pb += bpc)
1433 for (k = 0; k < bpc; k++)
1438 /* Separate raw data from instruction by extra space. */
1448 printf ("%s", sfile.buffer);
1450 if (prefix_addresses
1452 : show_raw_insn >= 0)
1460 j = addr_offset * opb + pb;
1462 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
1463 for (s = buf + skip_addr_chars; *s == '0'; s++)
1467 printf ("%s:\t", buf + skip_addr_chars);
1469 pb += octets_per_line;
1472 for (; j < addr_offset * opb + pb; j += bpc)
1476 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1478 for (k = bpc - 1; k >= 0; k--)
1479 printf ("%02x", (unsigned) data[j + k]);
1484 for (k = 0; k < bpc; k++)
1485 printf ("%02x", (unsigned) data[j + k]);
1498 while ((*relppp) < relppend
1499 && (**relppp)->address < rel_offset + addr_offset + octets / opb)
1501 if (dump_reloc_info || dump_dynamic_reloc_info)
1512 objdump_print_value (section->vma - rel_offset + q->address,
1515 printf (": %s\t", q->howto->name);
1517 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
1518 printf ("*unknown*");
1521 const char *sym_name;
1523 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1524 if (sym_name != NULL && *sym_name != '\0')
1525 objdump_print_symname (aux->abfd, info, *q->sym_ptr_ptr);
1530 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1531 sym_name = bfd_get_section_name (aux->abfd, sym_sec);
1532 if (sym_name == NULL || *sym_name == '\0')
1533 sym_name = "*unknown*";
1534 printf ("%s", sym_name);
1541 objdump_print_value (q->addend, info, TRUE);
1553 addr_offset += octets / opb;
1556 free (sfile.buffer);
1560 disassemble_section (bfd *abfd, asection *section, void *info)
1562 struct disassemble_info * pinfo = (struct disassemble_info *) info;
1563 struct objdump_disasm_info * paux;
1564 unsigned int opb = pinfo->octets_per_byte;
1565 bfd_byte * data = NULL;
1566 bfd_size_type datasize = 0;
1567 arelent ** rel_pp = NULL;
1568 arelent ** rel_ppstart = NULL;
1569 arelent ** rel_ppend;
1570 unsigned long stop_offset;
1571 asymbol * sym = NULL;
1575 unsigned long addr_offset;
1577 /* Sections that do not contain machine
1578 code are not normally disassembled. */
1579 if (! disassemble_all
1581 && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
1582 != (SEC_CODE | SEC_HAS_CONTENTS)))
1585 if (! process_section_p (section))
1588 datasize = bfd_get_section_size (section);
1592 /* Decide which set of relocs to use. Load them if necessary. */
1593 paux = (struct objdump_disasm_info *) pinfo->application_data;
1594 if (paux->dynrelbuf)
1596 rel_pp = paux->dynrelbuf;
1597 rel_count = paux->dynrelcount;
1598 /* Dynamic reloc addresses are absolute, non-dynamic are section
1599 relative. REL_OFFSET specifies the reloc address corresponding
1600 to the start of this section. */
1601 rel_offset = section->vma;
1609 if ((section->flags & SEC_RELOC) != 0
1610 #ifndef DISASSEMBLER_NEEDS_RELOCS
1617 relsize = bfd_get_reloc_upper_bound (abfd, section);
1619 bfd_fatal (bfd_get_filename (abfd));
1623 rel_ppstart = rel_pp = xmalloc (relsize);
1624 rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
1626 bfd_fatal (bfd_get_filename (abfd));
1628 /* Sort the relocs by address. */
1629 qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
1634 rel_ppend = rel_pp + rel_count;
1636 data = xmalloc (datasize);
1638 bfd_get_section_contents (abfd, section, data, 0, datasize);
1640 paux->sec = section;
1641 pinfo->buffer = data;
1642 pinfo->buffer_vma = section->vma;
1643 pinfo->buffer_length = datasize;
1644 pinfo->section = section;
1646 if (start_address == (bfd_vma) -1
1647 || start_address < pinfo->buffer_vma)
1650 addr_offset = start_address - pinfo->buffer_vma;
1652 if (stop_address == (bfd_vma) -1)
1653 stop_offset = datasize / opb;
1656 if (stop_address < pinfo->buffer_vma)
1659 stop_offset = stop_address - pinfo->buffer_vma;
1660 if (stop_offset > pinfo->buffer_length / opb)
1661 stop_offset = pinfo->buffer_length / opb;
1664 /* Skip over the relocs belonging to addresses below the
1666 while (rel_pp < rel_ppend
1667 && (*rel_pp)->address < rel_offset + addr_offset)
1670 printf (_("Disassembly of section %s:\n"), section->name);
1672 /* Find the nearest symbol forwards from our current position. */
1673 paux->require_sec = TRUE;
1674 sym = find_symbol_for_address (section->vma + addr_offset, info, &place);
1675 paux->require_sec = FALSE;
1677 /* Disassemble a block of instructions up to the address associated with
1678 the symbol we have just found. Then print the symbol and find the
1679 next symbol on. Repeat until we have disassembled the entire section
1680 or we have reached the end of the address range we are interested in. */
1681 while (addr_offset < stop_offset)
1685 unsigned long nextstop_offset;
1688 addr = section->vma + addr_offset;
1690 if (sym != NULL && bfd_asymbol_value (sym) <= addr)
1695 (x < sorted_symcount
1696 && (bfd_asymbol_value (sorted_syms[x]) <= addr));
1700 pinfo->symbols = sorted_syms + place;
1701 pinfo->num_symbols = x - place;
1705 pinfo->symbols = NULL;
1706 pinfo->num_symbols = 0;
1709 if (! prefix_addresses)
1711 pinfo->fprintf_func (pinfo->stream, "\n");
1712 objdump_print_addr_with_sym (abfd, section, sym, addr,
1714 pinfo->fprintf_func (pinfo->stream, ":\n");
1717 if (sym != NULL && bfd_asymbol_value (sym) > addr)
1719 else if (sym == NULL)
1723 #define is_valid_next_sym(SYM) \
1724 ((SYM)->section == section \
1725 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
1726 && pinfo->symbol_is_valid (SYM, pinfo))
1728 /* Search forward for the next appropriate symbol in
1729 SECTION. Note that all the symbols are sorted
1730 together into one big array, and that some sections
1731 may have overlapping addresses. */
1732 while (place < sorted_symcount
1733 && ! is_valid_next_sym (sorted_syms [place]))
1736 if (place >= sorted_symcount)
1739 nextsym = sorted_syms[place];
1742 if (sym != NULL && bfd_asymbol_value (sym) > addr)
1743 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
1744 else if (nextsym == NULL)
1745 nextstop_offset = stop_offset;
1747 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
1749 if (nextstop_offset > stop_offset)
1750 nextstop_offset = stop_offset;
1752 /* If a symbol is explicitly marked as being an object
1753 rather than a function, just dump the bytes without
1754 disassembling them. */
1757 || bfd_asymbol_value (sym) > addr
1758 || ((sym->flags & BSF_OBJECT) == 0
1759 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
1761 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
1763 || (sym->flags & BSF_FUNCTION) != 0)
1768 disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
1769 addr_offset, nextstop_offset,
1770 rel_offset, &rel_pp, rel_ppend);
1772 addr_offset = nextstop_offset;
1778 if (rel_ppstart != NULL)
1782 /* Disassemble the contents of an object file. */
1785 disassemble_data (bfd *abfd)
1787 struct disassemble_info disasm_info;
1788 struct objdump_disasm_info aux;
1792 prev_functionname = NULL;
1795 /* We make a copy of syms to sort. We don't want to sort syms
1796 because that will screw up the relocs. */
1797 sorted_symcount = symcount ? symcount : dynsymcount;
1798 sorted_syms = xmalloc ((sorted_symcount + synthcount) * sizeof (asymbol *));
1799 memcpy (sorted_syms, symcount ? syms : dynsyms,
1800 sorted_symcount * sizeof (asymbol *));
1802 sorted_symcount = remove_useless_symbols (sorted_syms, sorted_symcount);
1804 for (i = 0; i < synthcount; ++i)
1806 sorted_syms[sorted_symcount] = synthsyms + i;
1810 /* Sort the symbols into section and symbol order. */
1811 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
1813 init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
1815 disasm_info.application_data = (void *) &aux;
1817 aux.require_sec = FALSE;
1818 aux.dynrelbuf = NULL;
1819 aux.dynrelcount = 0;
1821 disasm_info.print_address_func = objdump_print_address;
1822 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
1824 if (machine != NULL)
1826 const bfd_arch_info_type *info = bfd_scan_arch (machine);
1829 fatal (_("Can't use supplied machine %s"), machine);
1831 abfd->arch_info = info;
1834 if (endian != BFD_ENDIAN_UNKNOWN)
1836 struct bfd_target *xvec;
1838 xvec = xmalloc (sizeof (struct bfd_target));
1839 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
1840 xvec->byteorder = endian;
1844 /* Use libopcodes to locate a suitable disassembler. */
1845 aux.disassemble_fn = disassembler (abfd);
1846 if (!aux.disassemble_fn)
1848 non_fatal (_("Can't disassemble for architecture %s\n"),
1849 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
1854 disasm_info.flavour = bfd_get_flavour (abfd);
1855 disasm_info.arch = bfd_get_arch (abfd);
1856 disasm_info.mach = bfd_get_mach (abfd);
1857 disasm_info.disassembler_options = disassembler_options;
1858 disasm_info.octets_per_byte = bfd_octets_per_byte (abfd);
1859 disasm_info.skip_zeroes = DEFAULT_SKIP_ZEROES;
1860 disasm_info.skip_zeroes_at_end = DEFAULT_SKIP_ZEROES_AT_END;
1862 if (bfd_big_endian (abfd))
1863 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
1864 else if (bfd_little_endian (abfd))
1865 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
1867 /* ??? Aborting here seems too drastic. We could default to big or little
1869 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
1871 /* Allow the target to customize the info structure. */
1872 disassemble_init_for_target (& disasm_info);
1874 /* Pre-load the dynamic relocs if we are going
1875 to be dumping them along with the disassembly. */
1876 if (dump_dynamic_reloc_info)
1878 long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
1881 bfd_fatal (bfd_get_filename (abfd));
1885 aux.dynrelbuf = xmalloc (relsize);
1886 aux.dynrelcount = bfd_canonicalize_dynamic_reloc (abfd,
1889 if (aux.dynrelcount < 0)
1890 bfd_fatal (bfd_get_filename (abfd));
1892 /* Sort the relocs by address. */
1893 qsort (aux.dynrelbuf, aux.dynrelcount, sizeof (arelent *),
1898 bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
1900 if (aux.dynrelbuf != NULL)
1901 free (aux.dynrelbuf);
1905 /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
1906 it. Return NULL on failure. */
1909 read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr)
1915 stabsect = bfd_get_section_by_name (abfd, sect_name);
1916 if (stabsect == NULL)
1918 printf (_("No %s section present\n\n"), sect_name);
1922 size = bfd_section_size (abfd, stabsect);
1923 contents = xmalloc (size);
1925 if (! bfd_get_section_contents (abfd, stabsect, contents, 0, size))
1927 non_fatal (_("Reading %s section of %s failed: %s"),
1928 sect_name, bfd_get_filename (abfd),
1929 bfd_errmsg (bfd_get_error ()));
1940 /* Stabs entries use a 12 byte format:
1941 4 byte string table index
1943 1 byte stab other field
1944 2 byte stab desc field
1946 FIXME: This will have to change for a 64 bit object format. */
1948 #define STRDXOFF (0)
1950 #define OTHEROFF (5)
1953 #define STABSIZE (12)
1955 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1956 using string table section STRSECT_NAME (in `strtab'). */
1959 print_section_stabs (bfd *abfd,
1960 const char *stabsect_name,
1961 unsigned *string_offset_ptr)
1964 unsigned file_string_table_offset = 0;
1965 unsigned next_file_string_table_offset = *string_offset_ptr;
1966 bfd_byte *stabp, *stabs_end;
1969 stabs_end = stabp + stab_size;
1971 printf (_("Contents of %s section:\n\n"), stabsect_name);
1972 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1974 /* Loop through all symbols and print them.
1976 We start the index at -1 because there is a dummy symbol on
1977 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1978 for (i = -1; stabp < stabs_end; stabp += STABSIZE, i++)
1982 unsigned char type, other;
1983 unsigned short desc;
1986 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
1987 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
1988 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
1989 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
1990 value = bfd_h_get_32 (abfd, stabp + VALOFF);
1992 printf ("\n%-6d ", i);
1993 /* Either print the stab name, or, if unnamed, print its number
1994 again (makes consistent formatting for tools like awk). */
1995 name = bfd_get_stab_name (type);
1997 printf ("%-6s", name);
1998 else if (type == N_UNDF)
2001 printf ("%-6d", type);
2002 printf (" %-6d %-6d ", other, desc);
2003 bfd_printf_vma (abfd, value);
2004 printf (" %-6lu", strx);
2006 /* Symbols with type == 0 (N_UNDF) specify the length of the
2007 string table associated with this file. We use that info
2008 to know how to relocate the *next* file's string table indices. */
2011 file_string_table_offset = next_file_string_table_offset;
2012 next_file_string_table_offset += value;
2016 /* Using the (possibly updated) string table offset, print the
2017 string (if any) associated with this symbol. */
2018 if ((strx + file_string_table_offset) < stabstr_size)
2019 printf (" %s", &strtab[strx + file_string_table_offset]);
2025 *string_offset_ptr = next_file_string_table_offset;
2030 const char * section_name;
2031 const char * string_section_name;
2032 unsigned string_offset;
2037 find_stabs_section (bfd *abfd, asection *section, void *names)
2040 stab_section_names * sought = (stab_section_names *) names;
2042 /* Check for section names for which stabsect_name is a prefix, to
2043 handle .stab.N, etc. */
2044 len = strlen (sought->section_name);
2046 /* If the prefix matches, and the files section name ends with a
2047 nul or a digit, then we match. I.e., we want either an exact
2048 match or a section followed by a number. */
2049 if (strncmp (sought->section_name, section->name, len) == 0
2050 && (section->name[len] == 0
2051 || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
2054 strtab = read_section_stabs (abfd, sought->string_section_name,
2059 stabs = (bfd_byte *) read_section_stabs (abfd, section->name,
2062 print_section_stabs (abfd, section->name, &sought->string_offset);
2068 dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
2070 stab_section_names s;
2072 s.section_name = stabsect_name;
2073 s.string_section_name = strsect_name;
2074 s.string_offset = 0;
2076 bfd_map_over_sections (abfd, find_stabs_section, & s);
2082 /* Dump the any sections containing stabs debugging information. */
2085 dump_stabs (bfd *abfd)
2087 dump_stabs_section (abfd, ".stab", ".stabstr");
2088 dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
2089 dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
2090 dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
2094 dump_bfd_header (bfd *abfd)
2098 printf (_("architecture: %s, "),
2099 bfd_printable_arch_mach (bfd_get_arch (abfd),
2100 bfd_get_mach (abfd)));
2101 printf (_("flags 0x%08x:\n"), abfd->flags);
2103 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2104 PF (HAS_RELOC, "HAS_RELOC");
2105 PF (EXEC_P, "EXEC_P");
2106 PF (HAS_LINENO, "HAS_LINENO");
2107 PF (HAS_DEBUG, "HAS_DEBUG");
2108 PF (HAS_SYMS, "HAS_SYMS");
2109 PF (HAS_LOCALS, "HAS_LOCALS");
2110 PF (DYNAMIC, "DYNAMIC");
2111 PF (WP_TEXT, "WP_TEXT");
2112 PF (D_PAGED, "D_PAGED");
2113 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
2114 PF (HAS_LOAD_PAGE, "HAS_LOAD_PAGE");
2115 printf (_("\nstart address 0x"));
2116 bfd_printf_vma (abfd, abfd->start_address);
2122 dump_bfd_private_header (bfd *abfd)
2124 bfd_print_private_bfd_data (abfd, stdout);
2128 /* Display a section in hexadecimal format with associated characters.
2129 Each line prefixed by the zero padded address. */
2132 dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
2135 bfd_size_type datasize;
2136 bfd_size_type addr_offset;
2137 bfd_size_type start_offset;
2138 bfd_size_type stop_offset;
2139 unsigned int opb = bfd_octets_per_byte (abfd);
2140 /* Bytes per line. */
2141 const int onaline = 16;
2146 if ((section->flags & SEC_HAS_CONTENTS) == 0)
2149 if (! process_section_p (section))
2152 if ((datasize = bfd_section_size (abfd, section)) == 0)
2155 printf (_("Contents of section %s:\n"), section->name);
2157 data = xmalloc (datasize);
2159 bfd_get_section_contents (abfd, section, data, 0, datasize);
2161 /* Compute the address range to display. */
2162 if (start_address == (bfd_vma) -1
2163 || start_address < section->vma)
2166 start_offset = start_address - section->vma;
2168 if (stop_address == (bfd_vma) -1)
2169 stop_offset = datasize / opb;
2172 if (stop_address < section->vma)
2175 stop_offset = stop_address - section->vma;
2177 if (stop_offset > datasize / opb)
2178 stop_offset = datasize / opb;
2183 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
2184 if (strlen (buf) >= sizeof (buf))
2188 while (buf[count] == '0' && buf[count+1] != '\0')
2190 count = strlen (buf) - count;
2194 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
2195 if (strlen (buf) >= sizeof (buf))
2199 while (buf[count] == '0' && buf[count+1] != '\0')
2201 count = strlen (buf) - count;
2205 for (addr_offset = start_offset;
2206 addr_offset < stop_offset; addr_offset += onaline / opb)
2210 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
2211 count = strlen (buf);
2212 if ((size_t) count >= sizeof (buf))
2216 while (count < width)
2221 fputs (buf + count - width, stdout);
2224 for (j = addr_offset * opb;
2225 j < addr_offset * opb + onaline; j++)
2227 if (j < stop_offset * opb)
2228 printf ("%02x", (unsigned) (data[j]));
2236 for (j = addr_offset * opb;
2237 j < addr_offset * opb + onaline; j++)
2239 if (j >= stop_offset * opb)
2242 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
2249 /* Actually display the various requested regions. */
2252 dump_data (bfd *abfd)
2254 bfd_map_over_sections (abfd, dump_section, NULL);
2257 /* Should perhaps share code and display with nm? */
2260 dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean dynamic)
2270 printf ("DYNAMIC SYMBOL TABLE:\n");
2276 printf ("SYMBOL TABLE:\n");
2280 printf (_("no symbols\n"));
2282 for (count = 0; count < max; count++)
2286 if (*current == NULL)
2287 printf (_("no information for symbol number %ld\n"), count);
2289 else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
2290 printf (_("could not determine the type of symbol number %ld\n"),
2293 else if (dump_special_syms
2294 || !bfd_is_target_special_symbol (cur_bfd, *current))
2296 const char *name = (*current)->name;
2298 if (do_demangle && name != NULL && *name != '\0')
2302 /* If we want to demangle the name, we demangle it
2303 here, and temporarily clobber it while calling
2304 bfd_print_symbol. FIXME: This is a gross hack. */
2305 alloc = demangle (cur_bfd, name);
2306 (*current)->name = alloc;
2307 bfd_print_symbol (cur_bfd, stdout, *current,
2308 bfd_print_symbol_all);
2309 (*current)->name = name;
2313 bfd_print_symbol (cur_bfd, stdout, *current,
2314 bfd_print_symbol_all);
2323 dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
2326 char *last_filename, *last_functionname;
2327 unsigned int last_line;
2329 /* Get column headers lined up reasonably. */
2337 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
2338 width = strlen (buf) - 7;
2340 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
2343 last_filename = NULL;
2344 last_functionname = NULL;
2347 for (p = relpp; relcount && *p != NULL; p++, relcount--)
2350 const char *filename, *functionname;
2352 const char *sym_name;
2353 const char *section_name;
2355 if (start_address != (bfd_vma) -1
2356 && q->address < start_address)
2358 if (stop_address != (bfd_vma) -1
2359 && q->address > stop_address)
2362 if (with_line_numbers
2364 && bfd_find_nearest_line (abfd, sec, syms, q->address,
2365 &filename, &functionname, &line))
2367 if (functionname != NULL
2368 && (last_functionname == NULL
2369 || strcmp (functionname, last_functionname) != 0))
2371 printf ("%s():\n", functionname);
2372 if (last_functionname != NULL)
2373 free (last_functionname);
2374 last_functionname = xstrdup (functionname);
2378 && (line != last_line
2379 || (filename != NULL
2380 && last_filename != NULL
2381 && strcmp (filename, last_filename) != 0)))
2383 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
2385 if (last_filename != NULL)
2386 free (last_filename);
2387 if (filename == NULL)
2388 last_filename = NULL;
2390 last_filename = xstrdup (filename);
2394 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
2396 sym_name = (*(q->sym_ptr_ptr))->name;
2397 section_name = (*(q->sym_ptr_ptr))->section->name;
2402 section_name = NULL;
2407 bfd_printf_vma (abfd, q->address);
2409 printf (" %-16s ", q->howto->name);
2411 printf (" %-16d ", q->howto->type);
2412 objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
2416 if (section_name == NULL)
2417 section_name = "*unknown*";
2418 bfd_printf_vma (abfd, q->address);
2419 printf (" %-16s [%s]",
2427 bfd_printf_vma (abfd, q->addend);
2435 dump_relocs_in_section (bfd *abfd,
2437 void *dummy ATTRIBUTE_UNUSED)
2443 if ( bfd_is_abs_section (section)
2444 || bfd_is_und_section (section)
2445 || bfd_is_com_section (section)
2446 || (! process_section_p (section))
2447 || ((section->flags & SEC_RELOC) == 0))
2450 relsize = bfd_get_reloc_upper_bound (abfd, section);
2452 bfd_fatal (bfd_get_filename (abfd));
2454 printf ("RELOCATION RECORDS FOR [%s]:", section->name);
2458 printf (" (none)\n\n");
2462 relpp = xmalloc (relsize);
2463 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
2466 bfd_fatal (bfd_get_filename (abfd));
2467 else if (relcount == 0)
2468 printf (" (none)\n\n");
2472 dump_reloc_set (abfd, section, relpp, relcount);
2479 dump_relocs (bfd *abfd)
2481 bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
2485 dump_dynamic_relocs (bfd *abfd)
2491 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2493 bfd_fatal (bfd_get_filename (abfd));
2495 printf ("DYNAMIC RELOCATION RECORDS");
2498 printf (" (none)\n\n");
2501 relpp = xmalloc (relsize);
2502 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
2505 bfd_fatal (bfd_get_filename (abfd));
2506 else if (relcount == 0)
2507 printf (" (none)\n\n");
2511 dump_reloc_set (abfd, NULL, relpp, relcount);
2518 /* Creates a table of paths, to search for source files. */
2521 add_include_path (const char *path)
2525 include_path_count++;
2526 include_paths = xrealloc (include_paths,
2527 include_path_count * sizeof (*include_paths));
2528 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2529 if (path[1] == ':' && path[2] == 0)
2530 path = concat (path, ".", (const char *) 0);
2532 include_paths[include_path_count - 1] = path;
2536 adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
2538 void *dummy ATTRIBUTE_UNUSED)
2540 section->vma += adjust_section_vma;
2541 section->lma += adjust_section_vma;
2544 /* Dump selected contents of ABFD. */
2547 dump_bfd (bfd *abfd)
2549 /* If we are adjusting section VMA's, change them all now. Changing
2550 the BFD information is a hack. However, we must do it, or
2551 bfd_find_nearest_line will not do the right thing. */
2552 if (adjust_section_vma != 0)
2553 bfd_map_over_sections (abfd, adjust_addresses, NULL);
2555 if (! dump_debugging_tags)
2556 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd),
2559 print_arelt_descr (stdout, abfd, TRUE);
2560 if (dump_file_header)
2561 dump_bfd_header (abfd);
2562 if (dump_private_headers)
2563 dump_bfd_private_header (abfd);
2564 if (! dump_debugging_tags)
2566 if (dump_section_headers)
2567 dump_headers (abfd);
2569 if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
2570 syms = slurp_symtab (abfd);
2571 if (dump_dynamic_symtab || dump_dynamic_reloc_info
2572 || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
2573 dynsyms = slurp_dynamic_symtab (abfd);
2576 synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
2577 dynsymcount, dynsyms, &synthsyms);
2583 dump_symbols (abfd, FALSE);
2584 if (dump_dynamic_symtab)
2585 dump_symbols (abfd, TRUE);
2586 if (dump_stab_section_info)
2588 if (dump_reloc_info && ! disassemble)
2590 if (dump_dynamic_reloc_info && ! disassemble)
2591 dump_dynamic_relocs (abfd);
2592 if (dump_section_contents)
2595 disassemble_data (abfd);
2601 dhandle = read_debugging_info (abfd, syms, symcount);
2602 if (dhandle != NULL)
2604 if (! print_debugging_info (stdout, dhandle, abfd, syms, demangle,
2605 dump_debugging_tags ? TRUE : FALSE))
2607 non_fatal (_("%s: printing debugging information failed"),
2608 bfd_get_filename (abfd));
2638 display_bfd (bfd *abfd)
2642 if (bfd_check_format_matches (abfd, bfd_object, &matching))
2648 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2650 nonfatal (bfd_get_filename (abfd));
2651 list_matching_formats (matching);
2656 if (bfd_get_error () != bfd_error_file_not_recognized)
2658 nonfatal (bfd_get_filename (abfd));
2662 if (bfd_check_format_matches (abfd, bfd_core, &matching))
2668 nonfatal (bfd_get_filename (abfd));
2670 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2672 list_matching_formats (matching);
2678 display_file (char *filename, char *target)
2683 if (get_file_size (filename) < 1)
2686 file = bfd_openr (filename, target);
2689 nonfatal (filename);
2693 /* If the file is an archive, process all of its elements. */
2694 if (bfd_check_format (file, bfd_archive))
2696 bfd *last_arfile = NULL;
2698 printf (_("In archive %s:\n"), bfd_get_filename (file));
2701 bfd_set_error (bfd_error_no_error);
2703 arfile = bfd_openr_next_archived_file (file, arfile);
2706 if (bfd_get_error () != bfd_error_no_more_archived_files)
2707 nonfatal (bfd_get_filename (file));
2711 display_bfd (arfile);
2713 if (last_arfile != NULL)
2714 bfd_close (last_arfile);
2715 last_arfile = arfile;
2718 if (last_arfile != NULL)
2719 bfd_close (last_arfile);
2728 main (int argc, char **argv)
2731 char *target = default_target;
2732 bfd_boolean seenflag = FALSE;
2734 #if defined (HAVE_SETLOCALE)
2735 #if defined (HAVE_LC_MESSAGES)
2736 setlocale (LC_MESSAGES, "");
2738 setlocale (LC_CTYPE, "");
2741 bindtextdomain (PACKAGE, LOCALEDIR);
2742 textdomain (PACKAGE);
2744 program_name = *argv;
2745 xmalloc_set_program_name (program_name);
2747 START_PROGRESS (program_name, 0);
2750 set_default_bfd_target ();
2752 while ((c = getopt_long (argc, argv, "pib:m:M:VvCdDlfaHhrRtTxsSI:j:wE:zgeG",
2753 long_options, (int *) 0))
2759 break; /* We've been given a long option. */
2764 if (disassembler_options)
2765 /* Ignore potential memory leak for now. */
2766 disassembler_options = concat (disassembler_options, ",",
2769 disassembler_options = optarg;
2772 if (only_used == only_size)
2775 only = xrealloc (only, only_size * sizeof (char *));
2777 only [only_used++] = optarg;
2780 with_line_numbers = TRUE;
2789 enum demangling_styles style;
2791 style = cplus_demangle_name_to_style (optarg);
2792 if (style == unknown_demangling)
2793 fatal (_("unknown demangling style `%s'"),
2796 cplus_demangle_set_style (style);
2802 case OPTION_ADJUST_VMA:
2803 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
2805 case OPTION_START_ADDRESS:
2806 start_address = parse_vma (optarg, "--start-address");
2808 case OPTION_STOP_ADDRESS:
2809 stop_address = parse_vma (optarg, "--stop-address");
2812 if (strcmp (optarg, "B") == 0)
2813 endian = BFD_ENDIAN_BIG;
2814 else if (strcmp (optarg, "L") == 0)
2815 endian = BFD_ENDIAN_LITTLE;
2818 non_fatal (_("unrecognized -E option"));
2823 if (strncmp (optarg, "big", strlen (optarg)) == 0)
2824 endian = BFD_ENDIAN_BIG;
2825 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
2826 endian = BFD_ENDIAN_LITTLE;
2829 non_fatal (_("unrecognized --endian type `%s'"), optarg);
2835 dump_file_header = TRUE;
2839 formats_info = TRUE;
2843 add_include_path (optarg);
2846 dump_private_headers = TRUE;
2850 dump_private_headers = TRUE;
2852 dump_reloc_info = TRUE;
2853 dump_file_header = TRUE;
2854 dump_ar_hdrs = TRUE;
2855 dump_section_headers = TRUE;
2863 dump_dynamic_symtab = TRUE;
2871 disassemble_zeroes = TRUE;
2875 disassemble_all = TRUE;
2880 with_source_code = TRUE;
2889 dump_debugging_tags = 1;
2894 dump_stab_section_info = TRUE;
2898 dump_section_contents = TRUE;
2902 dump_reloc_info = TRUE;
2906 dump_dynamic_reloc_info = TRUE;
2910 dump_ar_hdrs = TRUE;
2914 dump_section_headers = TRUE;
2922 show_version = TRUE;
2932 print_version ("objdump");
2938 exit_status = display_info ();
2942 display_file ("a.out", target);
2944 for (; optind < argc;)
2945 display_file (argv[optind++], target);
2948 END_PROGRESS (program_name);