1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
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. */
28 #include "safe-ctype.h"
30 #include "libiberty.h"
35 /* Internal headers for the ELF .stab-dump code - sorry. */
36 #define BYTES_IN_WORD 32
37 #include "aout/aout64.h"
39 #ifdef NEED_DECLARATION_FPRINTF
40 /* This is needed by INIT_DISASSEMBLE_INFO. */
42 PARAMS ((FILE *, const char *, ...));
46 static int exit_status
= 0;
48 static char *default_target
= NULL
; /* Default at runtime. */
50 static int show_version
= 0; /* Show the version number. */
51 static int dump_section_contents
; /* -s */
52 static int dump_section_headers
; /* -h */
53 static bfd_boolean dump_file_header
; /* -f */
54 static int dump_symtab
; /* -t */
55 static int dump_dynamic_symtab
; /* -T */
56 static int dump_reloc_info
; /* -r */
57 static int dump_dynamic_reloc_info
; /* -R */
58 static int dump_ar_hdrs
; /* -a */
59 static int dump_private_headers
; /* -p */
60 static int prefix_addresses
; /* --prefix-addresses */
61 static int with_line_numbers
; /* -l */
62 static bfd_boolean with_source_code
; /* -S */
63 static int show_raw_insn
; /* --show-raw-insn */
64 static int dump_stab_section_info
; /* --stabs */
65 static int do_demangle
; /* -C, --demangle */
66 static bfd_boolean disassemble
; /* -d */
67 static bfd_boolean disassemble_all
; /* -D */
68 static int disassemble_zeroes
; /* --disassemble-zeroes */
69 static bfd_boolean formats_info
; /* -i */
70 static char **only
; /* -j secname */
71 static size_t only_size
= 0;
72 static size_t only_used
= 0;
73 static int wide_output
; /* -w */
74 static bfd_vma start_address
= (bfd_vma
) -1; /* --start-address */
75 static bfd_vma stop_address
= (bfd_vma
) -1; /* --stop-address */
76 static int dump_debugging
; /* --debugging */
77 static int dump_debugging_tags
; /* --debugging-tags */
78 static bfd_vma adjust_section_vma
= 0; /* --adjust-vma */
79 static int file_start_context
= 0; /* --file-start-context */
81 /* Extra info to pass to the disassembler address printing function. */
82 struct objdump_disasm_info
86 bfd_boolean require_sec
;
89 /* Architecture to disassemble for, or default if NULL. */
90 static char *machine
= (char *) NULL
;
92 /* Target specific options to the disassembler. */
93 static char *disassembler_options
= (char *) NULL
;
95 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
96 static enum bfd_endian endian
= BFD_ENDIAN_UNKNOWN
;
98 /* The symbol table. */
99 static asymbol
**syms
;
101 /* Number of symbols in `syms'. */
102 static long symcount
= 0;
104 /* The sorted symbol table. */
105 static asymbol
**sorted_syms
;
107 /* Number of symbols in `sorted_syms'. */
108 static long sorted_symcount
= 0;
110 /* The dynamic symbol table. */
111 static asymbol
**dynsyms
;
113 /* Number of symbols in `dynsyms'. */
114 static long dynsymcount
= 0;
116 static bfd_byte
*stabs
;
117 static bfd_size_type stab_size
;
120 static bfd_size_type stabstr_size
;
122 /* Static declarations. */
125 PARAMS ((FILE *, int));
127 PARAMS ((const char *));
128 static void display_file
129 PARAMS ((char *, char *));
130 static void dump_section_header
131 PARAMS ((bfd
*, asection
*, PTR
));
132 static void dump_headers
134 static void dump_data
136 static void dump_relocs
138 static void dump_dynamic_relocs
140 static void dump_reloc_set
141 PARAMS ((bfd
*, asection
*, arelent
**, long));
142 static void dump_symbols
143 PARAMS ((bfd
*, bfd_boolean
));
144 static void dump_bfd_header
146 static void dump_bfd_private_header
150 static void display_bfd
152 static void objdump_print_value
153 PARAMS ((bfd_vma
, struct disassemble_info
*, bfd_boolean
));
154 static void objdump_print_symname
155 PARAMS ((bfd
*, struct disassemble_info
*, asymbol
*));
156 static asymbol
*find_symbol_for_address
157 PARAMS ((bfd
*, asection
*, bfd_vma
, bfd_boolean
, long *));
158 static void objdump_print_addr_with_sym
159 PARAMS ((bfd
*, asection
*, asymbol
*, bfd_vma
,
160 struct disassemble_info
*, bfd_boolean
));
161 static void objdump_print_addr
162 PARAMS ((bfd_vma
, struct disassemble_info
*, bfd_boolean
));
163 static void objdump_print_address
164 PARAMS ((bfd_vma
, struct disassemble_info
*));
165 static int objdump_symbol_at_address
166 PARAMS ((bfd_vma
, struct disassemble_info
*));
167 static void show_line
168 PARAMS ((bfd
*, asection
*, bfd_vma
));
169 static void disassemble_bytes
170 PARAMS ((struct disassemble_info
*, disassembler_ftype
, bfd_boolean
,
171 bfd_byte
*, bfd_vma
, bfd_vma
, arelent
***, arelent
**));
172 static void disassemble_data
174 static asymbol
** slurp_symtab
176 static asymbol
** slurp_dynamic_symtab
178 static long remove_useless_symbols
179 PARAMS ((asymbol
**, long));
180 static int compare_symbols
181 PARAMS ((const PTR
, const PTR
));
182 static int compare_relocs
183 PARAMS ((const PTR
, const PTR
));
184 static void dump_stabs
186 static bfd_boolean read_section_stabs
187 PARAMS ((bfd
*, const char *, const char *));
188 static void print_section_stabs
189 PARAMS ((bfd
*, const char *, const char *));
190 static void dump_section_stabs
191 PARAMS ((bfd
*, char *, char *));
194 usage (stream
, status
)
198 fprintf (stream
, _("Usage: %s <option(s)> <file(s)>\n"), program_name
);
199 fprintf (stream
, _(" Display information from object <file(s)>.\n"));
200 fprintf (stream
, _(" At least one of the following switches must be given:\n"));
201 fprintf (stream
, _("\
202 -a, --archive-headers Display archive header information\n\
203 -f, --file-headers Display the contents of the overall file header\n\
204 -p, --private-headers Display object format specific file header contents\n\
205 -h, --[section-]headers Display the contents of the section headers\n\
206 -x, --all-headers Display the contents of all headers\n\
207 -d, --disassemble Display assembler contents of executable sections\n\
208 -D, --disassemble-all Display assembler contents of all sections\n\
209 -S, --source Intermix source code with disassembly\n\
210 -s, --full-contents Display the full contents of all sections requested\n\
211 -g, --debugging Display debug information in object file\n\
212 -e, --debugging-tags Display debug information using ctags style\n\
213 -G, --stabs Display (in raw form) any STABS info in the file\n\
214 -t, --syms Display the contents of the symbol table(s)\n\
215 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
216 -r, --reloc Display the relocation entries in the file\n\
217 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
218 -v, --version Display this program's version number\n\
219 -i, --info List object formats and architectures supported\n\
220 -H, --help Display this information\n\
224 fprintf (stream
, _("\n The following switches are optional:\n"));
225 fprintf (stream
, _("\
226 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
227 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
228 -j, --section=NAME Only display information for section NAME\n\
229 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
230 -EB --endian=big Assume big endian format when disassembling\n\
231 -EL --endian=little Assume little endian format when disassembling\n\
232 --file-start-context Include context from start of file (with -S)\n\
233 -l, --line-numbers Include line numbers and filenames in output\n\
234 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
235 The STYLE, if specified, can be `auto', `gnu',\n\
236 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
238 -w, --wide Format output for more than 80 columns\n\
239 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
240 --start-address=ADDR Only process data whose address is >= ADDR\n\
241 --stop-address=ADDR Only process data whose address is <= ADDR\n\
242 --prefix-addresses Print complete address alongside disassembly\n\
243 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
244 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
246 list_supported_targets (program_name
, stream
);
247 list_supported_architectures (program_name
, stream
);
249 disassembler_usage (stream
);
252 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
256 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
258 #define OPTION_ENDIAN (150)
259 #define OPTION_START_ADDRESS (OPTION_ENDIAN + 1)
260 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
261 #define OPTION_ADJUST_VMA (OPTION_STOP_ADDRESS + 1)
263 static struct option long_options
[]=
265 {"adjust-vma", required_argument
, NULL
, OPTION_ADJUST_VMA
},
266 {"all-headers", no_argument
, NULL
, 'x'},
267 {"private-headers", no_argument
, NULL
, 'p'},
268 {"architecture", required_argument
, NULL
, 'm'},
269 {"archive-headers", no_argument
, NULL
, 'a'},
270 {"debugging", no_argument
, NULL
, 'g'},
271 {"debugging-tags", no_argument
, NULL
, 'e'},
272 {"demangle", optional_argument
, NULL
, 'C'},
273 {"disassemble", no_argument
, NULL
, 'd'},
274 {"disassemble-all", no_argument
, NULL
, 'D'},
275 {"disassembler-options", required_argument
, NULL
, 'M'},
276 {"disassemble-zeroes", no_argument
, NULL
, 'z'},
277 {"dynamic-reloc", no_argument
, NULL
, 'R'},
278 {"dynamic-syms", no_argument
, NULL
, 'T'},
279 {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
280 {"file-headers", no_argument
, NULL
, 'f'},
281 {"file-start-context", no_argument
, &file_start_context
, 1},
282 {"full-contents", no_argument
, NULL
, 's'},
283 {"headers", no_argument
, NULL
, 'h'},
284 {"help", no_argument
, NULL
, 'H'},
285 {"info", no_argument
, NULL
, 'i'},
286 {"line-numbers", no_argument
, NULL
, 'l'},
287 {"no-show-raw-insn", no_argument
, &show_raw_insn
, -1},
288 {"prefix-addresses", no_argument
, &prefix_addresses
, 1},
289 {"reloc", no_argument
, NULL
, 'r'},
290 {"section", required_argument
, NULL
, 'j'},
291 {"section-headers", no_argument
, NULL
, 'h'},
292 {"show-raw-insn", no_argument
, &show_raw_insn
, 1},
293 {"source", no_argument
, NULL
, 'S'},
294 {"stabs", no_argument
, NULL
, 'G'},
295 {"start-address", required_argument
, NULL
, OPTION_START_ADDRESS
},
296 {"stop-address", required_argument
, NULL
, OPTION_STOP_ADDRESS
},
297 {"syms", no_argument
, NULL
, 't'},
298 {"target", required_argument
, NULL
, 'b'},
299 {"version", no_argument
, NULL
, 'V'},
300 {"wide", no_argument
, NULL
, 'w'},
301 {0, no_argument
, 0, 0}
313 dump_section_header (abfd
, section
, ignored
)
314 bfd
*abfd ATTRIBUTE_UNUSED
;
316 PTR ignored ATTRIBUTE_UNUSED
;
319 unsigned int opb
= bfd_octets_per_byte (abfd
);
321 printf ("%3d %-13s %08lx ", section
->index
,
322 bfd_get_section_name (abfd
, section
),
323 (unsigned long) bfd_section_size (abfd
, section
) / opb
);
324 bfd_printf_vma (abfd
, bfd_get_section_vma (abfd
, section
));
326 bfd_printf_vma (abfd
, section
->lma
);
327 printf (" %08lx 2**%u", (unsigned long) section
->filepos
,
328 bfd_get_section_alignment (abfd
, section
));
334 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
336 PF (SEC_HAS_CONTENTS
, "CONTENTS");
337 PF (SEC_ALLOC
, "ALLOC");
338 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
339 PF (SEC_LOAD
, "LOAD");
340 PF (SEC_RELOC
, "RELOC");
341 PF (SEC_READONLY
, "READONLY");
342 PF (SEC_CODE
, "CODE");
343 PF (SEC_DATA
, "DATA");
345 PF (SEC_DEBUGGING
, "DEBUGGING");
346 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
347 PF (SEC_EXCLUDE
, "EXCLUDE");
348 PF (SEC_SORT_ENTRIES
, "SORT_ENTRIES");
349 PF (SEC_BLOCK
, "BLOCK");
350 PF (SEC_CLINK
, "CLINK");
351 PF (SEC_SMALL_DATA
, "SMALL_DATA");
352 PF (SEC_SHARED
, "SHARED");
353 PF (SEC_ARCH_BIT_0
, "ARCH_BIT_0");
354 PF (SEC_THREAD_LOCAL
, "THREAD_LOCAL");
356 if ((section
->flags
& SEC_LINK_ONCE
) != 0)
360 switch (section
->flags
& SEC_LINK_DUPLICATES
)
364 case SEC_LINK_DUPLICATES_DISCARD
:
365 ls
= "LINK_ONCE_DISCARD";
367 case SEC_LINK_DUPLICATES_ONE_ONLY
:
368 ls
= "LINK_ONCE_ONE_ONLY";
370 case SEC_LINK_DUPLICATES_SAME_SIZE
:
371 ls
= "LINK_ONCE_SAME_SIZE";
373 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
374 ls
= "LINK_ONCE_SAME_CONTENTS";
377 printf ("%s%s", comma
, ls
);
379 if (section
->comdat
!= NULL
)
380 printf (" (COMDAT %s %ld)", section
->comdat
->name
,
381 section
->comdat
->symbol
);
394 printf (_("Sections:\n"));
397 printf (_("Idx Name Size VMA LMA File off Algn"));
399 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
400 if (bfd_get_arch_size (abfd
) == 32)
401 printf (_("Idx Name Size VMA LMA File off Algn"));
403 printf (_("Idx Name Size VMA LMA File off Algn"));
407 printf (_(" Flags"));
408 if (abfd
->flags
& HAS_LOAD_PAGE
)
412 bfd_map_over_sections (abfd
, dump_section_header
, (PTR
) NULL
);
419 asymbol
**sy
= (asymbol
**) NULL
;
422 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
428 storage
= bfd_get_symtab_upper_bound (abfd
);
430 bfd_fatal (bfd_get_filename (abfd
));
432 sy
= (asymbol
**) xmalloc (storage
);
434 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
436 bfd_fatal (bfd_get_filename (abfd
));
440 /* Read in the dynamic symbols. */
443 slurp_dynamic_symtab (abfd
)
446 asymbol
**sy
= (asymbol
**) NULL
;
449 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
452 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
454 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd
));
459 bfd_fatal (bfd_get_filename (abfd
));
462 sy
= (asymbol
**) xmalloc (storage
);
464 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
466 bfd_fatal (bfd_get_filename (abfd
));
470 /* Filter out (in place) symbols that are useless for disassembly.
471 COUNT is the number of elements in SYMBOLS.
472 Return the number of useful symbols. */
475 remove_useless_symbols (symbols
, count
)
479 register asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
483 asymbol
*sym
= *in_ptr
++;
485 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
487 if (sym
->flags
& (BSF_DEBUGGING
))
489 if (bfd_is_und_section (sym
->section
)
490 || bfd_is_com_section (sym
->section
))
495 return out_ptr
- symbols
;
498 /* Sort symbols into value order. */
501 compare_symbols (ap
, bp
)
505 const asymbol
*a
= *(const asymbol
**)ap
;
506 const asymbol
*b
= *(const asymbol
**)bp
;
510 flagword aflags
, bflags
;
512 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
514 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
517 if (a
->section
> b
->section
)
519 else if (a
->section
< b
->section
)
522 an
= bfd_asymbol_name (a
);
523 bn
= bfd_asymbol_name (b
);
527 /* The symbols gnu_compiled and gcc2_compiled convey no real
528 information, so put them after other symbols with the same value. */
529 af
= (strstr (an
, "gnu_compiled") != NULL
530 || strstr (an
, "gcc2_compiled") != NULL
);
531 bf
= (strstr (bn
, "gnu_compiled") != NULL
532 || strstr (bn
, "gcc2_compiled") != NULL
);
539 /* We use a heuristic for the file name, to try to sort it after
540 more useful symbols. It may not work on non Unix systems, but it
541 doesn't really matter; the only difference is precisely which
542 symbol names get printed. */
544 #define file_symbol(s, sn, snl) \
545 (((s)->flags & BSF_FILE) != 0 \
546 || ((sn)[(snl) - 2] == '.' \
547 && ((sn)[(snl) - 1] == 'o' \
548 || (sn)[(snl) - 1] == 'a')))
550 af
= file_symbol (a
, an
, anl
);
551 bf
= file_symbol (b
, bn
, bnl
);
558 /* Try to sort global symbols before local symbols before function
559 symbols before debugging symbols. */
564 if ((aflags
& BSF_DEBUGGING
) != (bflags
& BSF_DEBUGGING
))
566 if ((aflags
& BSF_DEBUGGING
) != 0)
571 if ((aflags
& BSF_FUNCTION
) != (bflags
& BSF_FUNCTION
))
573 if ((aflags
& BSF_FUNCTION
) != 0)
578 if ((aflags
& BSF_LOCAL
) != (bflags
& BSF_LOCAL
))
580 if ((aflags
& BSF_LOCAL
) != 0)
585 if ((aflags
& BSF_GLOBAL
) != (bflags
& BSF_GLOBAL
))
587 if ((aflags
& BSF_GLOBAL
) != 0)
593 /* Symbols that start with '.' might be section names, so sort them
594 after symbols that don't start with '.'. */
595 if (an
[0] == '.' && bn
[0] != '.')
597 if (an
[0] != '.' && bn
[0] == '.')
600 /* Finally, if we can't distinguish them in any other way, try to
601 get consistent results by sorting the symbols by name. */
602 return strcmp (an
, bn
);
605 /* Sort relocs into address order. */
608 compare_relocs (ap
, bp
)
612 const arelent
*a
= *(const arelent
**)ap
;
613 const arelent
*b
= *(const arelent
**)bp
;
615 if (a
->address
> b
->address
)
617 else if (a
->address
< b
->address
)
620 /* So that associated relocations tied to the same address show up
621 in the correct order, we don't do any further sorting. */
630 /* Print VMA to STREAM. If SKIP_ZEROES is TRUE, omit leading zeroes. */
633 objdump_print_value (vma
, info
, skip_zeroes
)
635 struct disassemble_info
*info
;
636 bfd_boolean skip_zeroes
;
640 struct objdump_disasm_info
*aux
641 = (struct objdump_disasm_info
*) info
->application_data
;
643 bfd_sprintf_vma (aux
->abfd
, buf
, vma
);
648 for (p
= buf
; *p
== '0'; ++p
)
653 (*info
->fprintf_func
) (info
->stream
, "%s", p
);
656 /* Print the name of a symbol. */
659 objdump_print_symname (abfd
, info
, sym
)
661 struct disassemble_info
*info
;
668 name
= bfd_asymbol_name (sym
);
669 if (do_demangle
&& name
[0] != '\0')
671 /* Demangle the name. */
672 alloc
= demangle (abfd
, name
);
677 (*info
->fprintf_func
) (info
->stream
, "%s", name
);
685 /* Locate a symbol given a bfd, a section, and a VMA. If REQUIRE_SEC
686 is TRUE, then always require the symbol to be in the section. This
687 returns NULL if there is no suitable symbol. If PLACE is not NULL,
688 then *PLACE is set to the index of the symbol in sorted_syms. */
691 find_symbol_for_address (abfd
, sec
, vma
, require_sec
, place
)
695 bfd_boolean require_sec
;
698 /* @@ Would it speed things up to cache the last two symbols returned,
699 and maybe their address ranges? For many processors, only one memory
700 operand can be present at a time, so the 2-entry cache wouldn't be
701 constantly churned by code doing heavy memory accesses. */
703 /* Indices in `sorted_syms'. */
705 long max
= sorted_symcount
;
707 unsigned int opb
= bfd_octets_per_byte (abfd
);
709 if (sorted_symcount
< 1)
712 /* Perform a binary search looking for the closest symbol to the
713 required value. We are searching the range (min, max]. */
714 while (min
+ 1 < max
)
718 thisplace
= (max
+ min
) / 2;
719 sym
= sorted_syms
[thisplace
];
721 if (bfd_asymbol_value (sym
) > vma
)
723 else if (bfd_asymbol_value (sym
) < vma
)
732 /* The symbol we want is now in min, the low end of the range we
733 were searching. If there are several symbols with the same
734 value, we want the first one. */
737 && (bfd_asymbol_value (sorted_syms
[thisplace
])
738 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
741 /* If the file is relocatable, and the symbol could be from this
742 section, prefer a symbol from this section over symbols from
743 others, even if the other symbol's value might be closer.
745 Note that this may be wrong for some symbol references if the
746 sections have overlapping memory ranges, but in that case there's
747 no way to tell what's desired without looking at the relocation
749 if (sorted_syms
[thisplace
]->section
!= sec
751 || ((abfd
->flags
& HAS_RELOC
) != 0
752 && vma
>= bfd_get_section_vma (abfd
, sec
)
753 && vma
< (bfd_get_section_vma (abfd
, sec
)
754 + bfd_section_size (abfd
, sec
) / opb
))))
758 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
760 if (bfd_asymbol_value (sorted_syms
[i
])
761 != bfd_asymbol_value (sorted_syms
[thisplace
]))
769 if (sorted_syms
[i
]->section
== sec
771 || sorted_syms
[i
- 1]->section
!= sec
772 || (bfd_asymbol_value (sorted_syms
[i
])
773 != bfd_asymbol_value (sorted_syms
[i
- 1]))))
780 if (sorted_syms
[thisplace
]->section
!= sec
)
782 /* We didn't find a good symbol with a smaller value.
783 Look for one with a larger value. */
784 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
786 if (sorted_syms
[i
]->section
== sec
)
794 if (sorted_syms
[thisplace
]->section
!= sec
796 || ((abfd
->flags
& HAS_RELOC
) != 0
797 && vma
>= bfd_get_section_vma (abfd
, sec
)
798 && vma
< (bfd_get_section_vma (abfd
, sec
)
799 + bfd_section_size (abfd
, sec
)))))
801 /* There is no suitable symbol. */
809 return sorted_syms
[thisplace
];
812 /* Print an address to INFO symbolically. */
815 objdump_print_addr_with_sym (abfd
, sec
, sym
, vma
, info
, skip_zeroes
)
820 struct disassemble_info
*info
;
821 bfd_boolean skip_zeroes
;
823 objdump_print_value (vma
, info
, skip_zeroes
);
829 (*info
->fprintf_func
) (info
->stream
, " <%s",
830 bfd_get_section_name (abfd
, sec
));
831 secaddr
= bfd_get_section_vma (abfd
, sec
);
834 (*info
->fprintf_func
) (info
->stream
, "-0x");
835 objdump_print_value (secaddr
- vma
, info
, TRUE
);
837 else if (vma
> secaddr
)
839 (*info
->fprintf_func
) (info
->stream
, "+0x");
840 objdump_print_value (vma
- secaddr
, info
, TRUE
);
842 (*info
->fprintf_func
) (info
->stream
, ">");
846 (*info
->fprintf_func
) (info
->stream
, " <");
847 objdump_print_symname (abfd
, info
, sym
);
848 if (bfd_asymbol_value (sym
) > vma
)
850 (*info
->fprintf_func
) (info
->stream
, "-0x");
851 objdump_print_value (bfd_asymbol_value (sym
) - vma
, info
, TRUE
);
853 else if (vma
> bfd_asymbol_value (sym
))
855 (*info
->fprintf_func
) (info
->stream
, "+0x");
856 objdump_print_value (vma
- bfd_asymbol_value (sym
), info
, TRUE
);
858 (*info
->fprintf_func
) (info
->stream
, ">");
862 /* Print VMA to INFO, symbolically if possible. If SKIP_ZEROES is
863 TRUE, don't output leading zeroes. */
866 objdump_print_addr (vma
, info
, skip_zeroes
)
868 struct disassemble_info
*info
;
869 bfd_boolean skip_zeroes
;
871 struct objdump_disasm_info
*aux
;
874 if (sorted_symcount
< 1)
876 (*info
->fprintf_func
) (info
->stream
, "0x");
877 objdump_print_value (vma
, info
, skip_zeroes
);
881 aux
= (struct objdump_disasm_info
*) info
->application_data
;
882 sym
= find_symbol_for_address (aux
->abfd
, aux
->sec
, vma
, aux
->require_sec
,
884 objdump_print_addr_with_sym (aux
->abfd
, aux
->sec
, sym
, vma
, info
,
888 /* Print VMA to INFO. This function is passed to the disassembler
892 objdump_print_address (vma
, info
)
894 struct disassemble_info
*info
;
896 objdump_print_addr (vma
, info
, ! prefix_addresses
);
899 /* Determine of the given address has a symbol associated with it. */
902 objdump_symbol_at_address (vma
, info
)
904 struct disassemble_info
* info
;
906 struct objdump_disasm_info
* aux
;
909 /* No symbols - do not bother checking. */
910 if (sorted_symcount
< 1)
913 aux
= (struct objdump_disasm_info
*) info
->application_data
;
914 sym
= find_symbol_for_address (aux
->abfd
, aux
->sec
, vma
, aux
->require_sec
,
917 return (sym
!= NULL
&& (bfd_asymbol_value (sym
) == vma
));
920 /* Hold the last function name and the last line number we displayed
923 static char *prev_functionname
;
924 static unsigned int prev_line
;
926 /* We keep a list of all files that we have seen when doing a
927 dissassembly with source, so that we know how much of the file to
928 display. This can be important for inlined functions. */
930 struct print_file_list
932 struct print_file_list
*next
;
938 static struct print_file_list
*print_files
;
940 /* The number of preceding context lines to show when we start
941 displaying a file for the first time. */
943 #define SHOW_PRECEDING_CONTEXT_LINES (5)
945 /* Skip ahead to a given line in a file, optionally printing each
948 static void skip_to_line
949 PARAMS ((struct print_file_list
*, unsigned int, bfd_boolean
));
952 skip_to_line (p
, line
, show
)
953 struct print_file_list
*p
;
957 while (p
->line
< line
)
961 if (fgets (buf
, sizeof buf
, p
->f
) == NULL
)
971 if (strchr (buf
, '\n') != NULL
)
976 /* Show the line number, or the source line, in a dissassembly
980 show_line (abfd
, section
, addr_offset
)
985 const char *filename
;
986 const char *functionname
;
989 if (! with_line_numbers
&& ! with_source_code
)
992 if (! bfd_find_nearest_line (abfd
, section
, syms
, addr_offset
, &filename
,
993 &functionname
, &line
))
996 if (filename
!= NULL
&& *filename
== '\0')
998 if (functionname
!= NULL
&& *functionname
== '\0')
1001 if (with_line_numbers
)
1003 if (functionname
!= NULL
1004 && (prev_functionname
== NULL
1005 || strcmp (functionname
, prev_functionname
) != 0))
1006 printf ("%s():\n", functionname
);
1007 if (line
> 0 && line
!= prev_line
)
1008 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
1011 if (with_source_code
1015 struct print_file_list
**pp
, *p
;
1017 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1018 if (strcmp ((*pp
)->filename
, filename
) == 0)
1024 if (p
!= print_files
)
1028 /* We have reencountered a file name which we saw
1029 earlier. This implies that either we are dumping out
1030 code from an included file, or the same file was
1031 linked in more than once. There are two common cases
1032 of an included file: inline functions in a header
1033 file, and a bison or flex skeleton file. In the
1034 former case we want to just start printing (but we
1035 back up a few lines to give context); in the latter
1036 case we want to continue from where we left off. I
1037 can't think of a good way to distinguish the cases,
1038 so I used a heuristic based on the file name. */
1039 if (strcmp (p
->filename
+ strlen (p
->filename
) - 2, ".h") != 0)
1043 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
1050 p
->f
= fopen (p
->filename
, "r");
1054 skip_to_line (p
, l
, FALSE
);
1056 if (print_files
->f
!= NULL
)
1058 fclose (print_files
->f
);
1059 print_files
->f
= NULL
;
1065 skip_to_line (p
, line
, TRUE
);
1067 p
->next
= print_files
;
1075 f
= fopen (filename
, "r");
1080 p
= ((struct print_file_list
*)
1081 xmalloc (sizeof (struct print_file_list
)));
1082 p
->filename
= xmalloc (strlen (filename
) + 1);
1083 strcpy (p
->filename
, filename
);
1087 if (print_files
!= NULL
&& print_files
->f
!= NULL
)
1089 fclose (print_files
->f
);
1090 print_files
->f
= NULL
;
1092 p
->next
= print_files
;
1095 if (file_start_context
)
1098 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
1101 skip_to_line (p
, l
, FALSE
);
1103 skip_to_line (p
, line
, TRUE
);
1108 if (functionname
!= NULL
1109 && (prev_functionname
== NULL
1110 || strcmp (functionname
, prev_functionname
) != 0))
1112 if (prev_functionname
!= NULL
)
1113 free (prev_functionname
);
1114 prev_functionname
= xmalloc (strlen (functionname
) + 1);
1115 strcpy (prev_functionname
, functionname
);
1118 if (line
> 0 && line
!= prev_line
)
1122 /* Pseudo FILE object for strings. */
1130 /* sprintf to a "stream" */
1133 objdump_sprintf
VPARAMS ((SFILE
*f
, const char *format
, ...))
1138 VA_OPEN (args
, format
);
1139 VA_FIXEDARG (args
, SFILE
*, f
);
1140 VA_FIXEDARG (args
, const char *, format
);
1142 vasprintf (&buf
, format
, args
);
1147 fatal (_("Out of virtual memory"));
1152 while ((size_t) ((f
->buffer
+ f
->size
) - f
->current
) < n
+ 1)
1156 curroff
= f
->current
- f
->buffer
;
1158 f
->buffer
= xrealloc (f
->buffer
, f
->size
);
1159 f
->current
= f
->buffer
+ curroff
;
1162 memcpy (f
->current
, buf
, n
);
1164 f
->current
[0] = '\0';
1172 /* The number of zeroes we want to see before we start skipping them.
1173 The number is arbitrarily chosen. */
1176 #define SKIP_ZEROES (8)
1179 /* The number of zeroes to skip at the end of a section. If the
1180 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1181 SKIP_ZEROES, they will be disassembled. If there are fewer than
1182 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1183 attempt to avoid disassembling zeroes inserted by section
1186 #ifndef SKIP_ZEROES_AT_END
1187 #define SKIP_ZEROES_AT_END (3)
1190 /* Disassemble some data in memory between given values. */
1193 disassemble_bytes (info
, disassemble_fn
, insns
, data
,
1194 start_offset
, stop_offset
, relppp
,
1196 struct disassemble_info
*info
;
1197 disassembler_ftype disassemble_fn
;
1200 bfd_vma start_offset
;
1201 bfd_vma stop_offset
;
1205 struct objdump_disasm_info
*aux
;
1207 int octets_per_line
;
1208 bfd_boolean done_dot
;
1209 int skip_addr_chars
;
1210 bfd_vma addr_offset
;
1211 int opb
= info
->octets_per_byte
;
1213 aux
= (struct objdump_disasm_info
*) info
->application_data
;
1217 octets_per_line
= 4;
1219 octets_per_line
= 16;
1221 /* Figure out how many characters to skip at the start of an
1222 address, to make the disassembly look nicer. We discard leading
1223 zeroes in chunks of 4, ensuring that there is always a leading
1225 skip_addr_chars
= 0;
1226 if (! prefix_addresses
)
1234 + bfd_section_size (section
->owner
, section
) / opb
));
1236 while (s
[0] == '0' && s
[1] == '0' && s
[2] == '0' && s
[3] == '0'
1239 skip_addr_chars
+= 4;
1244 info
->insn_info_valid
= 0;
1247 addr_offset
= start_offset
;
1248 while (addr_offset
< stop_offset
)
1252 bfd_boolean need_nl
= FALSE
;
1254 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1256 for (z
= addr_offset
* opb
; z
< stop_offset
* opb
; z
++)
1259 if (! disassemble_zeroes
1260 && (info
->insn_info_valid
== 0
1261 || info
->branch_delay_insns
== 0)
1262 && (z
- addr_offset
* opb
>= SKIP_ZEROES
1263 || (z
== stop_offset
* opb
&&
1264 z
- addr_offset
* opb
< SKIP_ZEROES_AT_END
)))
1268 /* If there are more nonzero octets to follow, we only skip
1269 zeroes in multiples of 4, to try to avoid running over
1270 the start of an instruction which happens to start with
1272 if (z
!= stop_offset
* opb
)
1273 z
= addr_offset
* opb
+ ((z
- addr_offset
* opb
) &~ 3);
1275 octets
= z
- addr_offset
* opb
;
1286 if (with_line_numbers
|| with_source_code
)
1287 /* The line number tables will refer to unadjusted
1288 section VMAs, so we must undo any VMA modifications
1289 when calling show_line. */
1290 show_line (aux
->abfd
, section
, addr_offset
- adjust_section_vma
);
1292 if (! prefix_addresses
)
1296 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ addr_offset
);
1297 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
1301 printf ("%s:\t", buf
+ skip_addr_chars
);
1305 aux
->require_sec
= TRUE
;
1306 objdump_print_address (section
->vma
+ addr_offset
, info
);
1307 aux
->require_sec
= FALSE
;
1314 sfile
.buffer
= xmalloc (sfile
.size
);
1315 sfile
.current
= sfile
.buffer
;
1316 info
->fprintf_func
= (fprintf_ftype
) objdump_sprintf
;
1317 info
->stream
= (FILE *) &sfile
;
1318 info
->bytes_per_line
= 0;
1319 info
->bytes_per_chunk
= 0;
1321 #ifdef DISASSEMBLER_NEEDS_RELOCS
1322 /* FIXME: This is wrong. It tests the number of octets
1323 in the last instruction, not the current one. */
1324 if (*relppp
< relppend
1325 && (**relppp
)->address
>= addr_offset
1326 && (**relppp
)->address
<= addr_offset
+ octets
/ opb
)
1327 info
->flags
= INSN_HAS_RELOC
;
1332 octets
= (*disassemble_fn
) (section
->vma
+ addr_offset
, info
);
1333 info
->fprintf_func
= (fprintf_ftype
) fprintf
;
1334 info
->stream
= stdout
;
1335 if (info
->bytes_per_line
!= 0)
1336 octets_per_line
= info
->bytes_per_line
;
1339 if (sfile
.current
!= sfile
.buffer
)
1340 printf ("%s\n", sfile
.buffer
);
1341 free (sfile
.buffer
);
1349 octets
= octets_per_line
;
1350 if (addr_offset
+ octets
/ opb
> stop_offset
)
1351 octets
= (stop_offset
- addr_offset
) * opb
;
1353 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ octets
; ++j
)
1355 if (ISPRINT (data
[j
]))
1356 buf
[j
- addr_offset
* opb
] = data
[j
];
1358 buf
[j
- addr_offset
* opb
] = '.';
1360 buf
[j
- addr_offset
* opb
] = '\0';
1363 if (prefix_addresses
1365 : show_raw_insn
>= 0)
1369 /* If ! prefix_addresses and ! wide_output, we print
1370 octets_per_line octets per line. */
1372 if (pb
> octets_per_line
&& ! prefix_addresses
&& ! wide_output
)
1373 pb
= octets_per_line
;
1375 if (info
->bytes_per_chunk
)
1376 bpc
= info
->bytes_per_chunk
;
1380 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
1383 if (bpc
> 1 && info
->display_endian
== BFD_ENDIAN_LITTLE
)
1385 for (k
= bpc
- 1; k
>= 0; k
--)
1386 printf ("%02x", (unsigned) data
[j
+ k
]);
1391 for (k
= 0; k
< bpc
; k
++)
1392 printf ("%02x", (unsigned) data
[j
+ k
]);
1397 for (; pb
< octets_per_line
; pb
+= bpc
)
1401 for (k
= 0; k
< bpc
; k
++)
1406 /* Separate raw data from instruction by extra space. */
1417 printf ("%s", sfile
.buffer
);
1418 free (sfile
.buffer
);
1421 if (prefix_addresses
1423 : show_raw_insn
>= 0)
1431 j
= addr_offset
* opb
+ pb
;
1433 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ j
/ opb
);
1434 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
1438 printf ("%s:\t", buf
+ skip_addr_chars
);
1440 pb
+= octets_per_line
;
1443 for (; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
1447 if (bpc
> 1 && info
->display_endian
== BFD_ENDIAN_LITTLE
)
1449 for (k
= bpc
- 1; k
>= 0; k
--)
1450 printf ("%02x", (unsigned) data
[j
+ k
]);
1455 for (k
= 0; k
< bpc
; k
++)
1456 printf ("%02x", (unsigned) data
[j
+ k
]);
1469 if ((section
->flags
& SEC_RELOC
) != 0
1470 #ifndef DISASSEMBLER_NEEDS_RELOCS
1475 while ((*relppp
) < relppend
1476 && ((**relppp
)->address
>= (bfd_vma
) addr_offset
1477 && (**relppp
)->address
< (bfd_vma
) addr_offset
+ octets
/ opb
))
1478 #ifdef DISASSEMBLER_NEEDS_RELOCS
1479 if (! dump_reloc_info
)
1493 objdump_print_value (section
->vma
+ q
->address
, info
, TRUE
);
1495 printf (": %s\t", q
->howto
->name
);
1497 if (q
->sym_ptr_ptr
== NULL
|| *q
->sym_ptr_ptr
== NULL
)
1498 printf ("*unknown*");
1501 const char *sym_name
;
1503 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
1504 if (sym_name
!= NULL
&& *sym_name
!= '\0')
1505 objdump_print_symname (aux
->abfd
, info
, *q
->sym_ptr_ptr
);
1510 sym_sec
= bfd_get_section (*q
->sym_ptr_ptr
);
1511 sym_name
= bfd_get_section_name (aux
->abfd
, sym_sec
);
1512 if (sym_name
== NULL
|| *sym_name
== '\0')
1513 sym_name
= "*unknown*";
1514 printf ("%s", sym_name
);
1521 objdump_print_value (q
->addend
, info
, TRUE
);
1533 addr_offset
+= octets
/ opb
;
1537 /* Disassemble the contents of an object file. */
1540 disassemble_data (abfd
)
1543 unsigned long addr_offset
;
1544 disassembler_ftype disassemble_fn
;
1545 struct disassemble_info disasm_info
;
1546 struct objdump_disasm_info aux
;
1551 prev_functionname
= NULL
;
1554 /* We make a copy of syms to sort. We don't want to sort syms
1555 because that will screw up the relocs. */
1556 sorted_syms
= (asymbol
**) xmalloc (symcount
* sizeof (asymbol
*));
1557 memcpy (sorted_syms
, syms
, symcount
* sizeof (asymbol
*));
1559 sorted_symcount
= remove_useless_symbols (sorted_syms
, symcount
);
1561 /* Sort the symbols into section and symbol order. */
1562 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
1564 INIT_DISASSEMBLE_INFO (disasm_info
, stdout
, fprintf
);
1566 disasm_info
.application_data
= (PTR
) &aux
;
1568 aux
.require_sec
= FALSE
;
1569 disasm_info
.print_address_func
= objdump_print_address
;
1570 disasm_info
.symbol_at_address_func
= objdump_symbol_at_address
;
1572 if (machine
!= (char *) NULL
)
1574 const bfd_arch_info_type
*info
= bfd_scan_arch (machine
);
1577 fatal (_("Can't use supplied machine %s"), machine
);
1579 abfd
->arch_info
= info
;
1582 if (endian
!= BFD_ENDIAN_UNKNOWN
)
1584 struct bfd_target
*xvec
;
1586 xvec
= (struct bfd_target
*) xmalloc (sizeof (struct bfd_target
));
1587 memcpy (xvec
, abfd
->xvec
, sizeof (struct bfd_target
));
1588 xvec
->byteorder
= endian
;
1592 disassemble_fn
= disassembler (abfd
);
1593 if (!disassemble_fn
)
1595 non_fatal (_("Can't disassemble for architecture %s\n"),
1596 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
1601 opb
= bfd_octets_per_byte (abfd
);
1603 disasm_info
.flavour
= bfd_get_flavour (abfd
);
1604 disasm_info
.arch
= bfd_get_arch (abfd
);
1605 disasm_info
.mach
= bfd_get_mach (abfd
);
1606 disasm_info
.disassembler_options
= disassembler_options
;
1607 disasm_info
.octets_per_byte
= opb
;
1609 if (bfd_big_endian (abfd
))
1610 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_BIG
;
1611 else if (bfd_little_endian (abfd
))
1612 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
1614 /* ??? Aborting here seems too drastic. We could default to big or little
1616 disasm_info
.endian
= BFD_ENDIAN_UNKNOWN
;
1618 for (section
= abfd
->sections
;
1619 section
!= (asection
*) NULL
;
1620 section
= section
->next
)
1622 bfd_byte
*data
= NULL
;
1623 bfd_size_type datasize
= 0;
1624 arelent
**relbuf
= NULL
;
1625 arelent
**relpp
= NULL
;
1626 arelent
**relppend
= NULL
;
1627 unsigned long stop_offset
;
1628 asymbol
*sym
= NULL
;
1631 /* Sections that do not contain machine code are not normally disassembled. */
1632 if (! disassemble_all
1634 && (section
->flags
& SEC_CODE
) == 0)
1641 for (i
= 0; i
< only_used
; i
++)
1642 if (strcmp (only
[i
], section
->name
) == 0)
1649 if ((section
->flags
& SEC_RELOC
) != 0
1650 #ifndef DISASSEMBLER_NEEDS_RELOCS
1657 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
1659 bfd_fatal (bfd_get_filename (abfd
));
1665 relbuf
= (arelent
**) xmalloc (relsize
);
1666 relcount
= bfd_canonicalize_reloc (abfd
, section
, relbuf
, syms
);
1668 bfd_fatal (bfd_get_filename (abfd
));
1670 /* Sort the relocs by address. */
1671 qsort (relbuf
, relcount
, sizeof (arelent
*), compare_relocs
);
1674 relppend
= relpp
+ relcount
;
1676 /* Skip over the relocs belonging to addresses below the
1678 if (start_address
!= (bfd_vma
) -1)
1679 while (relpp
< relppend
1680 && (*relpp
)->address
< start_address
)
1685 datasize
= bfd_get_section_size_before_reloc (section
);
1689 printf (_("Disassembly of section %s:\n"), section
->name
);
1691 data
= (bfd_byte
*) xmalloc ((size_t) datasize
);
1693 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
1696 disasm_info
.buffer
= data
;
1697 disasm_info
.buffer_vma
= section
->vma
;
1698 disasm_info
.buffer_length
= datasize
;
1699 disasm_info
.section
= section
;
1701 if (start_address
== (bfd_vma
) -1
1702 || start_address
< disasm_info
.buffer_vma
)
1705 addr_offset
= start_address
- disasm_info
.buffer_vma
;
1707 if (stop_address
== (bfd_vma
) -1)
1708 stop_offset
= datasize
/ opb
;
1711 if (stop_address
< disasm_info
.buffer_vma
)
1714 stop_offset
= stop_address
- disasm_info
.buffer_vma
;
1715 if (stop_offset
> disasm_info
.buffer_length
/ opb
)
1716 stop_offset
= disasm_info
.buffer_length
/ opb
;
1719 sym
= find_symbol_for_address (abfd
, section
, section
->vma
+ addr_offset
,
1722 while (addr_offset
< stop_offset
)
1725 unsigned long nextstop_offset
;
1728 if (sym
!= NULL
&& bfd_asymbol_value (sym
) <= section
->vma
+ addr_offset
)
1733 (x
< sorted_symcount
1734 && bfd_asymbol_value (sorted_syms
[x
]) <= section
->vma
+ addr_offset
);
1738 disasm_info
.symbols
= & sorted_syms
[place
];
1739 disasm_info
.num_symbols
= x
- place
;
1742 disasm_info
.symbols
= NULL
;
1744 if (! prefix_addresses
)
1746 (* disasm_info
.fprintf_func
) (disasm_info
.stream
, "\n");
1747 objdump_print_addr_with_sym (abfd
, section
, sym
,
1748 section
->vma
+ addr_offset
,
1751 (* disasm_info
.fprintf_func
) (disasm_info
.stream
, ":\n");
1754 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > section
->vma
+ addr_offset
)
1756 else if (sym
== NULL
)
1760 /* Search forward for the next appropriate symbol in
1761 SECTION. Note that all the symbols are sorted
1762 together into one big array, and that some sections
1763 may have overlapping addresses. */
1764 while (place
< sorted_symcount
1765 && (sorted_syms
[place
]->section
!= section
1766 || (bfd_asymbol_value (sorted_syms
[place
])
1767 <= bfd_asymbol_value (sym
))))
1769 if (place
>= sorted_symcount
)
1772 nextsym
= sorted_syms
[place
];
1775 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > section
->vma
+ addr_offset
)
1777 nextstop_offset
= bfd_asymbol_value (sym
) - section
->vma
;
1778 if (nextstop_offset
> stop_offset
)
1779 nextstop_offset
= stop_offset
;
1781 else if (nextsym
== NULL
)
1782 nextstop_offset
= stop_offset
;
1785 nextstop_offset
= bfd_asymbol_value (nextsym
) - section
->vma
;
1786 if (nextstop_offset
> stop_offset
)
1787 nextstop_offset
= stop_offset
;
1790 /* If a symbol is explicitly marked as being an object
1791 rather than a function, just dump the bytes without
1792 disassembling them. */
1795 || bfd_asymbol_value (sym
) > section
->vma
+ addr_offset
1796 || ((sym
->flags
& BSF_OBJECT
) == 0
1797 && (strstr (bfd_asymbol_name (sym
), "gnu_compiled")
1799 && (strstr (bfd_asymbol_name (sym
), "gcc2_compiled")
1801 || (sym
->flags
& BSF_FUNCTION
) != 0)
1806 disassemble_bytes (&disasm_info
, disassemble_fn
, insns
, data
,
1807 addr_offset
, nextstop_offset
, &relpp
, relppend
);
1809 addr_offset
= nextstop_offset
;
1821 /* Dump the stabs sections from an object file that has a section that
1822 uses Sun stabs encoding. */
1828 dump_section_stabs (abfd
, ".stab", ".stabstr");
1829 dump_section_stabs (abfd
, ".stab.excl", ".stab.exclstr");
1830 dump_section_stabs (abfd
, ".stab.index", ".stab.indexstr");
1831 dump_section_stabs (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1834 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1835 and string table section STRSECT_NAME into `strtab'.
1836 If the section exists and was read, allocate the space and return TRUE.
1837 Otherwise return FALSE. */
1840 read_section_stabs (abfd
, stabsect_name
, strsect_name
)
1842 const char *stabsect_name
;
1843 const char *strsect_name
;
1845 asection
*stabsect
, *stabstrsect
;
1847 stabsect
= bfd_get_section_by_name (abfd
, stabsect_name
);
1850 printf (_("No %s section present\n\n"), stabsect_name
);
1854 stabstrsect
= bfd_get_section_by_name (abfd
, strsect_name
);
1855 if (0 == stabstrsect
)
1857 non_fatal (_("%s has no %s section"),
1858 bfd_get_filename (abfd
), strsect_name
);
1863 stab_size
= bfd_section_size (abfd
, stabsect
);
1864 stabstr_size
= bfd_section_size (abfd
, stabstrsect
);
1866 stabs
= (bfd_byte
*) xmalloc (stab_size
);
1867 strtab
= (char *) xmalloc (stabstr_size
);
1869 if (! bfd_get_section_contents (abfd
, stabsect
, (PTR
) stabs
, 0, stab_size
))
1871 non_fatal (_("Reading %s section of %s failed: %s"),
1872 stabsect_name
, bfd_get_filename (abfd
),
1873 bfd_errmsg (bfd_get_error ()));
1880 if (! bfd_get_section_contents (abfd
, stabstrsect
, (PTR
) strtab
, 0,
1883 non_fatal (_("Reading %s section of %s failed: %s\n"),
1884 strsect_name
, bfd_get_filename (abfd
),
1885 bfd_errmsg (bfd_get_error ()));
1895 /* Stabs entries use a 12 byte format:
1896 4 byte string table index
1898 1 byte stab other field
1899 2 byte stab desc field
1901 FIXME: This will have to change for a 64 bit object format. */
1903 #define STRDXOFF (0)
1905 #define OTHEROFF (5)
1908 #define STABSIZE (12)
1910 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1911 using string table section STRSECT_NAME (in `strtab'). */
1914 print_section_stabs (abfd
, stabsect_name
, strsect_name
)
1916 const char *stabsect_name
;
1917 const char *strsect_name ATTRIBUTE_UNUSED
;
1920 unsigned file_string_table_offset
= 0, next_file_string_table_offset
= 0;
1921 bfd_byte
*stabp
, *stabs_end
;
1924 stabs_end
= stabp
+ stab_size
;
1926 printf (_("Contents of %s section:\n\n"), stabsect_name
);
1927 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1929 /* Loop through all symbols and print them.
1931 We start the index at -1 because there is a dummy symbol on
1932 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1933 for (i
= -1; stabp
< stabs_end
; stabp
+= STABSIZE
, i
++)
1937 unsigned char type
, other
;
1938 unsigned short desc
;
1941 strx
= bfd_h_get_32 (abfd
, stabp
+ STRDXOFF
);
1942 type
= bfd_h_get_8 (abfd
, stabp
+ TYPEOFF
);
1943 other
= bfd_h_get_8 (abfd
, stabp
+ OTHEROFF
);
1944 desc
= bfd_h_get_16 (abfd
, stabp
+ DESCOFF
);
1945 value
= bfd_h_get_32 (abfd
, stabp
+ VALOFF
);
1947 printf ("\n%-6d ", i
);
1948 /* Either print the stab name, or, if unnamed, print its number
1949 again (makes consistent formatting for tools like awk). */
1950 name
= bfd_get_stab_name (type
);
1952 printf ("%-6s", name
);
1953 else if (type
== N_UNDF
)
1956 printf ("%-6d", type
);
1957 printf (" %-6d %-6d ", other
, desc
);
1958 bfd_printf_vma (abfd
, value
);
1959 printf (" %-6lu", strx
);
1961 /* Symbols with type == 0 (N_UNDF) specify the length of the
1962 string table associated with this file. We use that info
1963 to know how to relocate the *next* file's string table indices. */
1966 file_string_table_offset
= next_file_string_table_offset
;
1967 next_file_string_table_offset
+= value
;
1971 /* Using the (possibly updated) string table offset, print the
1972 string (if any) associated with this symbol. */
1973 if ((strx
+ file_string_table_offset
) < stabstr_size
)
1974 printf (" %s", &strtab
[strx
+ file_string_table_offset
]);
1983 dump_section_stabs (abfd
, stabsect_name
, strsect_name
)
1985 char *stabsect_name
;
1990 /* Check for section names for which stabsect_name is a prefix, to
1991 handle .stab0, etc. */
1992 for (s
= abfd
->sections
;
1998 len
= strlen (stabsect_name
);
2000 /* If the prefix matches, and the files section name ends with a
2001 nul or a digit, then we match. I.e., we want either an exact
2002 match or a section followed by a number. */
2003 if (strncmp (stabsect_name
, s
->name
, len
) == 0
2004 && (s
->name
[len
] == '\000'
2005 || ISDIGIT (s
->name
[len
])))
2007 if (read_section_stabs (abfd
, s
->name
, strsect_name
))
2009 print_section_stabs (abfd
, s
->name
, strsect_name
);
2019 dump_bfd_header (abfd
)
2024 printf (_("architecture: %s, "),
2025 bfd_printable_arch_mach (bfd_get_arch (abfd
),
2026 bfd_get_mach (abfd
)));
2027 printf (_("flags 0x%08x:\n"), abfd
->flags
);
2029 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2030 PF (HAS_RELOC
, "HAS_RELOC");
2031 PF (EXEC_P
, "EXEC_P");
2032 PF (HAS_LINENO
, "HAS_LINENO");
2033 PF (HAS_DEBUG
, "HAS_DEBUG");
2034 PF (HAS_SYMS
, "HAS_SYMS");
2035 PF (HAS_LOCALS
, "HAS_LOCALS");
2036 PF (DYNAMIC
, "DYNAMIC");
2037 PF (WP_TEXT
, "WP_TEXT");
2038 PF (D_PAGED
, "D_PAGED");
2039 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
2040 PF (HAS_LOAD_PAGE
, "HAS_LOAD_PAGE");
2041 printf (_("\nstart address 0x"));
2042 bfd_printf_vma (abfd
, abfd
->start_address
);
2048 dump_bfd_private_header (abfd
)
2051 bfd_print_private_bfd_data (abfd
, stdout
);
2054 /* Dump selected contents of ABFD. */
2060 /* If we are adjusting section VMA's, change them all now. Changing
2061 the BFD information is a hack. However, we must do it, or
2062 bfd_find_nearest_line will not do the right thing. */
2063 if (adjust_section_vma
!= 0)
2067 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2069 s
->vma
+= adjust_section_vma
;
2070 s
->lma
+= adjust_section_vma
;
2074 if (! dump_debugging_tags
)
2075 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd
),
2078 print_arelt_descr (stdout
, abfd
, TRUE
);
2079 if (dump_file_header
)
2080 dump_bfd_header (abfd
);
2081 if (dump_private_headers
)
2082 dump_bfd_private_header (abfd
);
2083 if (! dump_debugging_tags
)
2085 if (dump_section_headers
)
2086 dump_headers (abfd
);
2088 if (dump_symtab
|| dump_reloc_info
|| disassemble
|| dump_debugging
)
2089 syms
= slurp_symtab (abfd
);
2090 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
)
2091 dynsyms
= slurp_dynamic_symtab (abfd
);
2094 dump_symbols (abfd
, FALSE
);
2095 if (dump_dynamic_symtab
)
2096 dump_symbols (abfd
, TRUE
);
2097 if (dump_stab_section_info
)
2099 if (dump_reloc_info
&& ! disassemble
)
2101 if (dump_dynamic_reloc_info
)
2102 dump_dynamic_relocs (abfd
);
2103 if (dump_section_contents
)
2106 disassemble_data (abfd
);
2111 dhandle
= read_debugging_info (abfd
, syms
, symcount
);
2112 if (dhandle
!= NULL
)
2114 if (! print_debugging_info (stdout
, dhandle
, abfd
, syms
, demangle
,
2115 dump_debugging_tags
? TRUE
: FALSE
))
2117 non_fatal (_("%s: printing debugging information failed"),
2118 bfd_get_filename (abfd
));
2143 if (bfd_check_format_matches (abfd
, bfd_object
, &matching
))
2149 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
2151 nonfatal (bfd_get_filename (abfd
));
2152 list_matching_formats (matching
);
2157 if (bfd_get_error () != bfd_error_file_not_recognized
)
2159 nonfatal (bfd_get_filename (abfd
));
2163 if (bfd_check_format_matches (abfd
, bfd_core
, &matching
))
2169 nonfatal (bfd_get_filename (abfd
));
2171 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
2173 list_matching_formats (matching
);
2179 display_file (filename
, target
)
2183 bfd
*file
, *arfile
= (bfd
*) NULL
;
2185 file
= bfd_openr (filename
, target
);
2188 nonfatal (filename
);
2192 if (bfd_check_format (file
, bfd_archive
))
2194 bfd
*last_arfile
= NULL
;
2196 printf (_("In archive %s:\n"), bfd_get_filename (file
));
2199 bfd_set_error (bfd_error_no_error
);
2201 arfile
= bfd_openr_next_archived_file (file
, arfile
);
2204 if (bfd_get_error () != bfd_error_no_more_archived_files
)
2205 nonfatal (bfd_get_filename (file
));
2209 display_bfd (arfile
);
2211 if (last_arfile
!= NULL
)
2212 bfd_close (last_arfile
);
2213 last_arfile
= arfile
;
2216 if (last_arfile
!= NULL
)
2217 bfd_close (last_arfile
);
2225 /* Actually display the various requested regions. */
2233 bfd_size_type datasize
= 0;
2234 bfd_size_type addr_offset
;
2235 bfd_size_type start_offset
, stop_offset
;
2236 unsigned int opb
= bfd_octets_per_byte (abfd
);
2238 for (section
= abfd
->sections
; section
!= NULL
; section
=
2244 for (i
= 0; i
< only_used
; i
++)
2245 if (strcmp (only
[i
], section
->name
) == 0)
2248 if (only_used
== 0 || i
!= only_used
)
2250 if (section
->flags
& SEC_HAS_CONTENTS
)
2255 printf (_("Contents of section %s:\n"), section
->name
);
2257 if (bfd_section_size (abfd
, section
) == 0)
2259 data
= (bfd_byte
*) xmalloc ((size_t) bfd_section_size (abfd
, section
));
2260 datasize
= bfd_section_size (abfd
, section
);
2263 bfd_get_section_contents (abfd
, section
, (PTR
) data
, 0, bfd_section_size (abfd
, section
));
2265 if (start_address
== (bfd_vma
) -1
2266 || start_address
< section
->vma
)
2269 start_offset
= start_address
- section
->vma
;
2270 if (stop_address
== (bfd_vma
) -1)
2271 stop_offset
= bfd_section_size (abfd
, section
) / opb
;
2274 if (stop_address
< section
->vma
)
2277 stop_offset
= stop_address
- section
->vma
;
2278 if (stop_offset
> bfd_section_size (abfd
, section
) / opb
)
2279 stop_offset
= bfd_section_size (abfd
, section
) / opb
;
2284 bfd_sprintf_vma (abfd
, buf
, start_offset
+ section
->vma
);
2285 if (strlen (buf
) >= sizeof (buf
))
2288 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
2290 count
= strlen (buf
) - count
;
2294 bfd_sprintf_vma (abfd
, buf
, stop_offset
+ section
->vma
- 1);
2295 if (strlen (buf
) >= sizeof (buf
))
2298 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
2300 count
= strlen (buf
) - count
;
2304 for (addr_offset
= start_offset
;
2305 addr_offset
< stop_offset
; addr_offset
+= onaline
/ opb
)
2309 bfd_sprintf_vma (abfd
, buf
, (addr_offset
+ section
->vma
));
2310 count
= strlen (buf
);
2311 if ((size_t) count
>= sizeof (buf
))
2314 while (count
< width
)
2319 fputs (buf
+ count
- width
, stdout
);
2322 for (j
= addr_offset
* opb
;
2323 j
< addr_offset
* opb
+ onaline
; j
++)
2325 if (j
< stop_offset
* opb
)
2326 printf ("%02x", (unsigned) (data
[j
]));
2334 for (j
= addr_offset
* opb
;
2335 j
< addr_offset
* opb
+ onaline
; j
++)
2337 if (j
>= stop_offset
* opb
)
2340 printf ("%c", ISPRINT (data
[j
]) ? data
[j
] : '.');
2350 /* Should perhaps share code and display with nm? */
2353 dump_symbols (abfd
, dynamic
)
2354 bfd
*abfd ATTRIBUTE_UNUSED
;
2355 bfd_boolean dynamic
;
2365 printf ("DYNAMIC SYMBOL TABLE:\n");
2371 printf ("SYMBOL TABLE:\n");
2375 printf (_("no symbols\n"));
2377 for (count
= 0; count
< max
; count
++)
2381 bfd
*cur_bfd
= bfd_asymbol_bfd (*current
);
2383 if (cur_bfd
!= NULL
)
2388 name
= (*current
)->name
;
2390 if (do_demangle
&& name
!= NULL
&& *name
!= '\0')
2392 /* If we want to demangle the name, we demangle it
2393 here, and temporarily clobber it while calling
2394 bfd_print_symbol. FIXME: This is a gross hack. */
2395 alloc
= demangle (cur_bfd
, name
);
2396 (*current
)->name
= alloc
;
2399 bfd_print_symbol (cur_bfd
, stdout
, *current
,
2400 bfd_print_symbol_all
);
2402 (*current
)->name
= name
;
2423 for (a
= abfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
2427 if (bfd_is_abs_section (a
))
2429 if (bfd_is_und_section (a
))
2431 if (bfd_is_com_section (a
))
2438 for (i
= 0; i
< only_used
; i
++)
2439 if (strcmp (only
[i
], a
->name
) == 0)
2445 else if ((a
->flags
& SEC_RELOC
) == 0)
2448 relsize
= bfd_get_reloc_upper_bound (abfd
, a
);
2450 bfd_fatal (bfd_get_filename (abfd
));
2452 printf ("RELOCATION RECORDS FOR [%s]:", a
->name
);
2456 printf (" (none)\n\n");
2460 relpp
= (arelent
**) xmalloc (relsize
);
2461 relcount
= bfd_canonicalize_reloc (abfd
, a
, relpp
, syms
);
2464 bfd_fatal (bfd_get_filename (abfd
));
2465 else if (relcount
== 0)
2466 printf (" (none)\n\n");
2470 dump_reloc_set (abfd
, a
, relpp
, relcount
);
2479 dump_dynamic_relocs (abfd
)
2486 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
2488 bfd_fatal (bfd_get_filename (abfd
));
2490 printf ("DYNAMIC RELOCATION RECORDS");
2493 printf (" (none)\n\n");
2496 relpp
= (arelent
**) xmalloc (relsize
);
2497 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
2500 bfd_fatal (bfd_get_filename (abfd
));
2501 else if (relcount
== 0)
2502 printf (" (none)\n\n");
2506 dump_reloc_set (abfd
, (asection
*) NULL
, relpp
, relcount
);
2514 dump_reloc_set (abfd
, sec
, relpp
, relcount
)
2521 char *last_filename
, *last_functionname
;
2522 unsigned int last_line
;
2524 /* Get column headers lined up reasonably. */
2531 bfd_sprintf_vma (abfd
, buf
, (bfd_vma
) -1);
2532 width
= strlen (buf
) - 7;
2534 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
2537 last_filename
= NULL
;
2538 last_functionname
= NULL
;
2541 for (p
= relpp
; relcount
&& *p
!= (arelent
*) NULL
; p
++, relcount
--)
2544 const char *filename
, *functionname
;
2546 const char *sym_name
;
2547 const char *section_name
;
2549 if (start_address
!= (bfd_vma
) -1
2550 && q
->address
< start_address
)
2552 if (stop_address
!= (bfd_vma
) -1
2553 && q
->address
> stop_address
)
2556 if (with_line_numbers
2558 && bfd_find_nearest_line (abfd
, sec
, syms
, q
->address
,
2559 &filename
, &functionname
, &line
))
2561 if (functionname
!= NULL
2562 && (last_functionname
== NULL
2563 || strcmp (functionname
, last_functionname
) != 0))
2565 printf ("%s():\n", functionname
);
2566 if (last_functionname
!= NULL
)
2567 free (last_functionname
);
2568 last_functionname
= xstrdup (functionname
);
2572 && (line
!= last_line
2573 || (filename
!= NULL
2574 && last_filename
!= NULL
2575 && strcmp (filename
, last_filename
) != 0)))
2577 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
2579 if (last_filename
!= NULL
)
2580 free (last_filename
);
2581 if (filename
== NULL
)
2582 last_filename
= NULL
;
2584 last_filename
= xstrdup (filename
);
2588 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
2590 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
2591 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
2596 section_name
= NULL
;
2601 bfd_printf_vma (abfd
, q
->address
);
2603 printf (" %-16s ", q
->howto
->name
);
2605 printf (" %-16d ", q
->howto
->type
);
2606 objdump_print_symname (abfd
, (struct disassemble_info
*) NULL
,
2611 if (section_name
== (const char *) NULL
)
2612 section_name
= "*unknown*";
2613 bfd_printf_vma (abfd
, q
->address
);
2614 printf (" %-16s [%s]",
2622 bfd_printf_vma (abfd
, q
->addend
);
2629 int main
PARAMS ((int, char **));
2637 char *target
= default_target
;
2638 bfd_boolean seenflag
= FALSE
;
2640 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2641 setlocale (LC_MESSAGES
, "");
2643 #if defined (HAVE_SETLOCALE)
2644 setlocale (LC_CTYPE
, "");
2646 bindtextdomain (PACKAGE
, LOCALEDIR
);
2647 textdomain (PACKAGE
);
2649 program_name
= *argv
;
2650 xmalloc_set_program_name (program_name
);
2652 START_PROGRESS (program_name
, 0);
2655 set_default_bfd_target ();
2657 while ((c
= getopt_long (argc
, argv
, "pib:m:M:VvCdDlfaHhrRtTxsSj:wE:zgeG",
2658 long_options
, (int *) 0))
2664 break; /* We've been given a long option. */
2669 if (disassembler_options
)
2670 /* Ignore potential memory leak for now. */
2671 disassembler_options
= concat (disassembler_options
, ",", optarg
, NULL
);
2673 disassembler_options
= optarg
;
2679 only
= (char **) xmalloc (only_size
* sizeof (char *));
2681 else if (only_used
== only_size
)
2684 only
= (char **) xrealloc (only
,
2685 only_size
* sizeof (char *));
2687 only
[only_used
++] = optarg
;
2690 with_line_numbers
= TRUE
;
2699 enum demangling_styles style
;
2701 style
= cplus_demangle_name_to_style (optarg
);
2702 if (style
== unknown_demangling
)
2703 fatal (_("unknown demangling style `%s'"),
2706 cplus_demangle_set_style (style
);
2712 case OPTION_ADJUST_VMA
:
2713 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
2715 case OPTION_START_ADDRESS
:
2716 start_address
= parse_vma (optarg
, "--start-address");
2718 case OPTION_STOP_ADDRESS
:
2719 stop_address
= parse_vma (optarg
, "--stop-address");
2722 if (strcmp (optarg
, "B") == 0)
2723 endian
= BFD_ENDIAN_BIG
;
2724 else if (strcmp (optarg
, "L") == 0)
2725 endian
= BFD_ENDIAN_LITTLE
;
2728 non_fatal (_("unrecognized -E option"));
2733 if (strncmp (optarg
, "big", strlen (optarg
)) == 0)
2734 endian
= BFD_ENDIAN_BIG
;
2735 else if (strncmp (optarg
, "little", strlen (optarg
)) == 0)
2736 endian
= BFD_ENDIAN_LITTLE
;
2739 non_fatal (_("unrecognized --endian type `%s'"), optarg
);
2745 dump_file_header
= TRUE
;
2749 formats_info
= TRUE
;
2753 dump_private_headers
= TRUE
;
2757 dump_private_headers
= TRUE
;
2759 dump_reloc_info
= TRUE
;
2760 dump_file_header
= TRUE
;
2761 dump_ar_hdrs
= TRUE
;
2762 dump_section_headers
= TRUE
;
2770 dump_dynamic_symtab
= TRUE
;
2778 disassemble_zeroes
= TRUE
;
2782 disassemble_all
= TRUE
;
2787 with_source_code
= TRUE
;
2796 dump_debugging_tags
= 1;
2801 dump_stab_section_info
= TRUE
;
2805 dump_section_contents
= TRUE
;
2809 dump_reloc_info
= TRUE
;
2813 dump_dynamic_reloc_info
= TRUE
;
2817 dump_ar_hdrs
= TRUE
;
2821 dump_section_headers
= TRUE
;
2829 show_version
= TRUE
;
2839 print_version ("objdump");
2845 exit_status
= display_info ();
2849 display_file ("a.out", target
);
2851 for (; optind
< argc
;)
2852 display_file (argv
[optind
++], target
);
2855 END_PROGRESS (program_name
);