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 (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. */
128 static void display_file
130 static void dump_section_header
131 (bfd
*, asection
*, void *);
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 (bfd
*, asection
*, arelent
**, long);
142 static void dump_symbols
143 (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 (bfd_vma
, struct disassemble_info
*, bfd_boolean
);
154 static void objdump_print_symname
155 (bfd
*, struct disassemble_info
*, asymbol
*);
156 static asymbol
*find_symbol_for_address
157 (bfd
*, asection
*, bfd_vma
, bfd_boolean
, long *);
158 static void objdump_print_addr_with_sym
159 (bfd
*, asection
*, asymbol
*, bfd_vma
,
160 struct disassemble_info
*, bfd_boolean
);
161 static void objdump_print_addr
162 (bfd_vma
, struct disassemble_info
*, bfd_boolean
);
163 static void objdump_print_address
164 (bfd_vma
, struct disassemble_info
*);
165 static int objdump_symbol_at_address
166 (bfd_vma
, struct disassemble_info
*);
167 static void show_line
168 (bfd
*, asection
*, bfd_vma
);
169 static void disassemble_bytes
170 (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
180 static int compare_symbols
181 (const void *, const void *);
182 static int compare_relocs
183 (const void *, const void *);
184 static void dump_stabs
186 static bfd_boolean read_section_stabs
187 (bfd
*, const char *, const char *);
188 static void print_section_stabs
189 (bfd
*, const char *, const char *);
190 static void dump_section_stabs
191 (bfd
*, char *, char *);
194 usage (FILE *stream
, int status
)
196 fprintf (stream
, _("Usage: %s <option(s)> <file(s)>\n"), program_name
);
197 fprintf (stream
, _(" Display information from object <file(s)>.\n"));
198 fprintf (stream
, _(" At least one of the following switches must be given:\n"));
199 fprintf (stream
, _("\
200 -a, --archive-headers Display archive header information\n\
201 -f, --file-headers Display the contents of the overall file header\n\
202 -p, --private-headers Display object format specific file header contents\n\
203 -h, --[section-]headers Display the contents of the section headers\n\
204 -x, --all-headers Display the contents of all headers\n\
205 -d, --disassemble Display assembler contents of executable sections\n\
206 -D, --disassemble-all Display assembler contents of all sections\n\
207 -S, --source Intermix source code with disassembly\n\
208 -s, --full-contents Display the full contents of all sections requested\n\
209 -g, --debugging Display debug information in object file\n\
210 -e, --debugging-tags Display debug information using ctags style\n\
211 -G, --stabs Display (in raw form) any STABS info in the file\n\
212 -t, --syms Display the contents of the symbol table(s)\n\
213 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
214 -r, --reloc Display the relocation entries in the file\n\
215 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
216 -v, --version Display this program's version number\n\
217 -i, --info List object formats and architectures supported\n\
218 -H, --help Display this information\n\
222 fprintf (stream
, _("\n The following switches are optional:\n"));
223 fprintf (stream
, _("\
224 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
225 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
226 -j, --section=NAME Only display information for section NAME\n\
227 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
228 -EB --endian=big Assume big endian format when disassembling\n\
229 -EL --endian=little Assume little endian format when disassembling\n\
230 --file-start-context Include context from start of file (with -S)\n\
231 -l, --line-numbers Include line numbers and filenames in output\n\
232 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
233 The STYLE, if specified, can be `auto', `gnu',\n\
234 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
236 -w, --wide Format output for more than 80 columns\n\
237 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
238 --start-address=ADDR Only process data whose address is >= ADDR\n\
239 --stop-address=ADDR Only process data whose address is <= ADDR\n\
240 --prefix-addresses Print complete address alongside disassembly\n\
241 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
242 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
244 list_supported_targets (program_name
, stream
);
245 list_supported_architectures (program_name
, stream
);
247 disassembler_usage (stream
);
250 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
254 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
258 OPTION_START_ADDRESS
,
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}
305 nonfatal (const char *msg
)
312 dump_section_header (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*section
,
313 void *ignored ATTRIBUTE_UNUSED
)
316 unsigned int opb
= bfd_octets_per_byte (abfd
);
318 printf ("%3d %-13s %08lx ", section
->index
,
319 bfd_get_section_name (abfd
, section
),
320 (unsigned long) bfd_section_size (abfd
, section
) / opb
);
321 bfd_printf_vma (abfd
, bfd_get_section_vma (abfd
, section
));
323 bfd_printf_vma (abfd
, section
->lma
);
324 printf (" %08lx 2**%u", (unsigned long) section
->filepos
,
325 bfd_get_section_alignment (abfd
, section
));
331 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
333 PF (SEC_HAS_CONTENTS
, "CONTENTS");
334 PF (SEC_ALLOC
, "ALLOC");
335 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
336 PF (SEC_LOAD
, "LOAD");
337 PF (SEC_RELOC
, "RELOC");
338 PF (SEC_READONLY
, "READONLY");
339 PF (SEC_CODE
, "CODE");
340 PF (SEC_DATA
, "DATA");
342 PF (SEC_DEBUGGING
, "DEBUGGING");
343 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
344 PF (SEC_EXCLUDE
, "EXCLUDE");
345 PF (SEC_SORT_ENTRIES
, "SORT_ENTRIES");
346 PF (SEC_BLOCK
, "BLOCK");
347 PF (SEC_CLINK
, "CLINK");
348 PF (SEC_SMALL_DATA
, "SMALL_DATA");
349 PF (SEC_SHARED
, "SHARED");
350 PF (SEC_ARCH_BIT_0
, "ARCH_BIT_0");
351 PF (SEC_THREAD_LOCAL
, "THREAD_LOCAL");
353 if ((section
->flags
& SEC_LINK_ONCE
) != 0)
357 switch (section
->flags
& SEC_LINK_DUPLICATES
)
361 case SEC_LINK_DUPLICATES_DISCARD
:
362 ls
= "LINK_ONCE_DISCARD";
364 case SEC_LINK_DUPLICATES_ONE_ONLY
:
365 ls
= "LINK_ONCE_ONE_ONLY";
367 case SEC_LINK_DUPLICATES_SAME_SIZE
:
368 ls
= "LINK_ONCE_SAME_SIZE";
370 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
371 ls
= "LINK_ONCE_SAME_CONTENTS";
374 printf ("%s%s", comma
, ls
);
376 if (section
->comdat
!= NULL
)
377 printf (" (COMDAT %s %ld)", section
->comdat
->name
,
378 section
->comdat
->symbol
);
388 dump_headers (bfd
*abfd
)
390 printf (_("Sections:\n"));
393 printf (_("Idx Name Size VMA LMA File off Algn"));
395 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
396 if (bfd_get_arch_size (abfd
) == 32)
397 printf (_("Idx Name Size VMA LMA File off Algn"));
399 printf (_("Idx Name Size VMA LMA File off Algn"));
403 printf (_(" Flags"));
404 if (abfd
->flags
& HAS_LOAD_PAGE
)
408 bfd_map_over_sections (abfd
, dump_section_header
, NULL
);
412 slurp_symtab (bfd
*abfd
)
414 asymbol
**sy
= (asymbol
**) NULL
;
417 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
423 storage
= bfd_get_symtab_upper_bound (abfd
);
425 bfd_fatal (bfd_get_filename (abfd
));
427 sy
= (asymbol
**) xmalloc (storage
);
429 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
431 bfd_fatal (bfd_get_filename (abfd
));
435 /* Read in the dynamic symbols. */
438 slurp_dynamic_symtab (bfd
*abfd
)
440 asymbol
**sy
= (asymbol
**) NULL
;
443 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
446 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
448 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd
));
453 bfd_fatal (bfd_get_filename (abfd
));
456 sy
= (asymbol
**) xmalloc (storage
);
458 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
460 bfd_fatal (bfd_get_filename (abfd
));
464 /* Filter out (in place) symbols that are useless for disassembly.
465 COUNT is the number of elements in SYMBOLS.
466 Return the number of useful symbols. */
469 remove_useless_symbols (asymbol
**symbols
, long count
)
471 asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
475 asymbol
*sym
= *in_ptr
++;
477 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
479 if (sym
->flags
& (BSF_DEBUGGING
))
481 if (bfd_is_und_section (sym
->section
)
482 || bfd_is_com_section (sym
->section
))
487 return out_ptr
- symbols
;
490 /* Sort symbols into value order. */
493 compare_symbols (const void *ap
, const void *bp
)
495 const asymbol
*a
= * (const asymbol
**) ap
;
496 const asymbol
*b
= * (const asymbol
**) bp
;
506 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
508 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
511 if (a
->section
> b
->section
)
513 else if (a
->section
< b
->section
)
516 an
= bfd_asymbol_name (a
);
517 bn
= bfd_asymbol_name (b
);
521 /* The symbols gnu_compiled and gcc2_compiled convey no real
522 information, so put them after other symbols with the same value. */
523 af
= (strstr (an
, "gnu_compiled") != NULL
524 || strstr (an
, "gcc2_compiled") != NULL
);
525 bf
= (strstr (bn
, "gnu_compiled") != NULL
526 || strstr (bn
, "gcc2_compiled") != NULL
);
533 /* We use a heuristic for the file name, to try to sort it after
534 more useful symbols. It may not work on non Unix systems, but it
535 doesn't really matter; the only difference is precisely which
536 symbol names get printed. */
538 #define file_symbol(s, sn, snl) \
539 (((s)->flags & BSF_FILE) != 0 \
540 || ((sn)[(snl) - 2] == '.' \
541 && ((sn)[(snl) - 1] == 'o' \
542 || (sn)[(snl) - 1] == 'a')))
544 af
= file_symbol (a
, an
, anl
);
545 bf
= file_symbol (b
, bn
, bnl
);
552 /* Try to sort global symbols before local symbols before function
553 symbols before debugging symbols. */
558 if ((aflags
& BSF_DEBUGGING
) != (bflags
& BSF_DEBUGGING
))
560 if ((aflags
& BSF_DEBUGGING
) != 0)
565 if ((aflags
& BSF_FUNCTION
) != (bflags
& BSF_FUNCTION
))
567 if ((aflags
& BSF_FUNCTION
) != 0)
572 if ((aflags
& BSF_LOCAL
) != (bflags
& BSF_LOCAL
))
574 if ((aflags
& BSF_LOCAL
) != 0)
579 if ((aflags
& BSF_GLOBAL
) != (bflags
& BSF_GLOBAL
))
581 if ((aflags
& BSF_GLOBAL
) != 0)
587 /* Symbols that start with '.' might be section names, so sort them
588 after symbols that don't start with '.'. */
589 if (an
[0] == '.' && bn
[0] != '.')
591 if (an
[0] != '.' && bn
[0] == '.')
594 /* Finally, if we can't distinguish them in any other way, try to
595 get consistent results by sorting the symbols by name. */
596 return strcmp (an
, bn
);
599 /* Sort relocs into address order. */
602 compare_relocs (const void *ap
, const void *bp
)
604 const arelent
*a
= * (const arelent
**) ap
;
605 const arelent
*b
= * (const arelent
**) bp
;
607 if (a
->address
> b
->address
)
609 else if (a
->address
< b
->address
)
612 /* So that associated relocations tied to the same address show up
613 in the correct order, we don't do any further sorting. */
622 /* Print VMA to STREAM. If SKIP_ZEROES is TRUE, omit leading zeroes. */
625 objdump_print_value (bfd_vma vma
, struct disassemble_info
*info
,
626 bfd_boolean skip_zeroes
)
630 struct objdump_disasm_info
*aux
631 = (struct objdump_disasm_info
*) info
->application_data
;
633 bfd_sprintf_vma (aux
->abfd
, buf
, vma
);
638 for (p
= buf
; *p
== '0'; ++p
)
643 (*info
->fprintf_func
) (info
->stream
, "%s", p
);
646 /* Print the name of a symbol. */
649 objdump_print_symname (bfd
*abfd
, struct disassemble_info
*info
,
656 name
= bfd_asymbol_name (sym
);
657 if (do_demangle
&& name
[0] != '\0')
659 /* Demangle the name. */
660 alloc
= demangle (abfd
, name
);
665 (*info
->fprintf_func
) (info
->stream
, "%s", name
);
673 /* Locate a symbol given a bfd, a section, and a VMA. If REQUIRE_SEC
674 is TRUE, then always require the symbol to be in the section. This
675 returns NULL if there is no suitable symbol. If PLACE is not NULL,
676 then *PLACE is set to the index of the symbol in sorted_syms. */
679 find_symbol_for_address (bfd
*abfd
, asection
*sec
, bfd_vma vma
,
680 bfd_boolean require_sec
, long *place
)
682 /* @@ Would it speed things up to cache the last two symbols returned,
683 and maybe their address ranges? For many processors, only one memory
684 operand can be present at a time, so the 2-entry cache wouldn't be
685 constantly churned by code doing heavy memory accesses. */
687 /* Indices in `sorted_syms'. */
689 long max
= sorted_symcount
;
691 unsigned int opb
= bfd_octets_per_byte (abfd
);
693 if (sorted_symcount
< 1)
696 /* Perform a binary search looking for the closest symbol to the
697 required value. We are searching the range (min, max]. */
698 while (min
+ 1 < max
)
702 thisplace
= (max
+ min
) / 2;
703 sym
= sorted_syms
[thisplace
];
705 if (bfd_asymbol_value (sym
) > vma
)
707 else if (bfd_asymbol_value (sym
) < vma
)
716 /* The symbol we want is now in min, the low end of the range we
717 were searching. If there are several symbols with the same
718 value, we want the first one. */
721 && (bfd_asymbol_value (sorted_syms
[thisplace
])
722 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
725 /* If the file is relocatable, and the symbol could be from this
726 section, prefer a symbol from this section over symbols from
727 others, even if the other symbol's value might be closer.
729 Note that this may be wrong for some symbol references if the
730 sections have overlapping memory ranges, but in that case there's
731 no way to tell what's desired without looking at the relocation
733 if (sorted_syms
[thisplace
]->section
!= sec
735 || ((abfd
->flags
& HAS_RELOC
) != 0
736 && vma
>= bfd_get_section_vma (abfd
, sec
)
737 && vma
< (bfd_get_section_vma (abfd
, sec
)
738 + bfd_section_size (abfd
, sec
) / opb
))))
742 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
744 if (bfd_asymbol_value (sorted_syms
[i
])
745 != bfd_asymbol_value (sorted_syms
[thisplace
]))
753 if (sorted_syms
[i
]->section
== sec
755 || sorted_syms
[i
- 1]->section
!= sec
756 || (bfd_asymbol_value (sorted_syms
[i
])
757 != bfd_asymbol_value (sorted_syms
[i
- 1]))))
764 if (sorted_syms
[thisplace
]->section
!= sec
)
766 /* We didn't find a good symbol with a smaller value.
767 Look for one with a larger value. */
768 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
770 if (sorted_syms
[i
]->section
== sec
)
778 if (sorted_syms
[thisplace
]->section
!= sec
780 || ((abfd
->flags
& HAS_RELOC
) != 0
781 && vma
>= bfd_get_section_vma (abfd
, sec
)
782 && vma
< (bfd_get_section_vma (abfd
, sec
)
783 + bfd_section_size (abfd
, sec
)))))
785 /* There is no suitable symbol. */
793 return sorted_syms
[thisplace
];
796 /* Print an address to INFO symbolically. */
799 objdump_print_addr_with_sym (bfd
*abfd
, asection
*sec
, asymbol
*sym
,
800 bfd_vma vma
, struct disassemble_info
*info
,
801 bfd_boolean skip_zeroes
)
803 objdump_print_value (vma
, info
, skip_zeroes
);
809 (*info
->fprintf_func
) (info
->stream
, " <%s",
810 bfd_get_section_name (abfd
, sec
));
811 secaddr
= bfd_get_section_vma (abfd
, sec
);
814 (*info
->fprintf_func
) (info
->stream
, "-0x");
815 objdump_print_value (secaddr
- vma
, info
, TRUE
);
817 else if (vma
> secaddr
)
819 (*info
->fprintf_func
) (info
->stream
, "+0x");
820 objdump_print_value (vma
- secaddr
, info
, TRUE
);
822 (*info
->fprintf_func
) (info
->stream
, ">");
826 (*info
->fprintf_func
) (info
->stream
, " <");
827 objdump_print_symname (abfd
, info
, sym
);
828 if (bfd_asymbol_value (sym
) > vma
)
830 (*info
->fprintf_func
) (info
->stream
, "-0x");
831 objdump_print_value (bfd_asymbol_value (sym
) - vma
, info
, TRUE
);
833 else if (vma
> bfd_asymbol_value (sym
))
835 (*info
->fprintf_func
) (info
->stream
, "+0x");
836 objdump_print_value (vma
- bfd_asymbol_value (sym
), info
, TRUE
);
838 (*info
->fprintf_func
) (info
->stream
, ">");
842 /* Print VMA to INFO, symbolically if possible. If SKIP_ZEROES is
843 TRUE, don't output leading zeroes. */
846 objdump_print_addr (bfd_vma vma
, struct disassemble_info
*info
,
847 bfd_boolean skip_zeroes
)
849 struct objdump_disasm_info
*aux
;
852 if (sorted_symcount
< 1)
854 (*info
->fprintf_func
) (info
->stream
, "0x");
855 objdump_print_value (vma
, info
, skip_zeroes
);
859 aux
= (struct objdump_disasm_info
*) info
->application_data
;
860 sym
= find_symbol_for_address (aux
->abfd
, aux
->sec
, vma
, aux
->require_sec
,
862 objdump_print_addr_with_sym (aux
->abfd
, aux
->sec
, sym
, vma
, info
,
866 /* Print VMA to INFO. This function is passed to the disassembler
870 objdump_print_address (bfd_vma vma
, struct disassemble_info
*info
)
872 objdump_print_addr (vma
, info
, ! prefix_addresses
);
875 /* Determine of the given address has a symbol associated with it. */
878 objdump_symbol_at_address (bfd_vma vma
, struct disassemble_info
* info
)
880 struct objdump_disasm_info
* aux
;
883 /* No symbols - do not bother checking. */
884 if (sorted_symcount
< 1)
887 aux
= (struct objdump_disasm_info
*) info
->application_data
;
888 sym
= find_symbol_for_address (aux
->abfd
, aux
->sec
, vma
, aux
->require_sec
,
891 return (sym
!= NULL
&& (bfd_asymbol_value (sym
) == vma
));
894 /* Hold the last function name and the last line number we displayed
897 static char *prev_functionname
;
898 static unsigned int prev_line
;
900 /* We keep a list of all files that we have seen when doing a
901 dissassembly with source, so that we know how much of the file to
902 display. This can be important for inlined functions. */
904 struct print_file_list
906 struct print_file_list
*next
;
912 static struct print_file_list
*print_files
;
914 /* The number of preceding context lines to show when we start
915 displaying a file for the first time. */
917 #define SHOW_PRECEDING_CONTEXT_LINES (5)
919 /* Skip ahead to a given line in a file, optionally printing each
923 skip_to_line (struct print_file_list
*p
, unsigned int line
,
926 while (p
->line
< line
)
930 if (fgets (buf
, sizeof buf
, p
->f
) == NULL
)
940 if (strchr (buf
, '\n') != NULL
)
945 /* Show the line number, or the source line, in a dissassembly
949 show_line (bfd
*abfd
, asection
*section
, bfd_vma addr_offset
)
951 const char *filename
;
952 const char *functionname
;
955 if (! with_line_numbers
&& ! with_source_code
)
958 if (! bfd_find_nearest_line (abfd
, section
, syms
, addr_offset
, &filename
,
959 &functionname
, &line
))
962 if (filename
!= NULL
&& *filename
== '\0')
964 if (functionname
!= NULL
&& *functionname
== '\0')
967 if (with_line_numbers
)
969 if (functionname
!= NULL
970 && (prev_functionname
== NULL
971 || strcmp (functionname
, prev_functionname
) != 0))
972 printf ("%s():\n", functionname
);
973 if (line
> 0 && line
!= prev_line
)
974 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
981 struct print_file_list
**pp
, *p
;
983 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
984 if (strcmp ((*pp
)->filename
, filename
) == 0)
990 if (p
!= print_files
)
994 /* We have reencountered a file name which we saw
995 earlier. This implies that either we are dumping out
996 code from an included file, or the same file was
997 linked in more than once. There are two common cases
998 of an included file: inline functions in a header
999 file, and a bison or flex skeleton file. In the
1000 former case we want to just start printing (but we
1001 back up a few lines to give context); in the latter
1002 case we want to continue from where we left off. I
1003 can't think of a good way to distinguish the cases,
1004 so I used a heuristic based on the file name. */
1005 if (strcmp (p
->filename
+ strlen (p
->filename
) - 2, ".h") != 0)
1009 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
1016 p
->f
= fopen (p
->filename
, "r");
1020 skip_to_line (p
, l
, FALSE
);
1022 if (print_files
->f
!= NULL
)
1024 fclose (print_files
->f
);
1025 print_files
->f
= NULL
;
1031 skip_to_line (p
, line
, TRUE
);
1033 p
->next
= print_files
;
1041 f
= fopen (filename
, "r");
1046 p
= ((struct print_file_list
*)
1047 xmalloc (sizeof (struct print_file_list
)));
1048 p
->filename
= xmalloc (strlen (filename
) + 1);
1049 strcpy (p
->filename
, filename
);
1053 if (print_files
!= NULL
&& print_files
->f
!= NULL
)
1055 fclose (print_files
->f
);
1056 print_files
->f
= NULL
;
1058 p
->next
= print_files
;
1061 if (file_start_context
)
1064 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
1067 skip_to_line (p
, l
, FALSE
);
1069 skip_to_line (p
, line
, TRUE
);
1074 if (functionname
!= NULL
1075 && (prev_functionname
== NULL
1076 || strcmp (functionname
, prev_functionname
) != 0))
1078 if (prev_functionname
!= NULL
)
1079 free (prev_functionname
);
1080 prev_functionname
= xmalloc (strlen (functionname
) + 1);
1081 strcpy (prev_functionname
, functionname
);
1084 if (line
> 0 && line
!= prev_line
)
1088 /* Pseudo FILE object for strings. */
1096 /* sprintf to a "stream". */
1099 objdump_sprintf (SFILE
*f
, const char *format
, ...)
1105 va_start (args
, format
);
1107 vasprintf (&buf
, format
, args
);
1112 fatal (_("Out of virtual memory"));
1117 while ((size_t) ((f
->buffer
+ f
->size
) - f
->current
) < n
+ 1)
1121 curroff
= f
->current
- f
->buffer
;
1123 f
->buffer
= xrealloc (f
->buffer
, f
->size
);
1124 f
->current
= f
->buffer
+ curroff
;
1127 memcpy (f
->current
, buf
, n
);
1129 f
->current
[0] = '\0';
1137 /* The number of zeroes we want to see before we start skipping them.
1138 The number is arbitrarily chosen. */
1141 #define SKIP_ZEROES (8)
1144 /* The number of zeroes to skip at the end of a section. If the
1145 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1146 SKIP_ZEROES, they will be disassembled. If there are fewer than
1147 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1148 attempt to avoid disassembling zeroes inserted by section
1151 #ifndef SKIP_ZEROES_AT_END
1152 #define SKIP_ZEROES_AT_END (3)
1155 /* Disassemble some data in memory between given values. */
1158 disassemble_bytes (struct disassemble_info
* info
,
1159 disassembler_ftype disassemble_fn
,
1162 bfd_vma start_offset
,
1163 bfd_vma stop_offset
,
1165 arelent
** relppend
)
1167 struct objdump_disasm_info
*aux
;
1169 int octets_per_line
;
1170 bfd_boolean done_dot
;
1171 int skip_addr_chars
;
1172 bfd_vma addr_offset
;
1173 int opb
= info
->octets_per_byte
;
1175 aux
= (struct objdump_disasm_info
*) info
->application_data
;
1179 octets_per_line
= 4;
1181 octets_per_line
= 16;
1183 /* Figure out how many characters to skip at the start of an
1184 address, to make the disassembly look nicer. We discard leading
1185 zeroes in chunks of 4, ensuring that there is always a leading
1187 skip_addr_chars
= 0;
1188 if (! prefix_addresses
)
1196 + bfd_section_size (section
->owner
, section
) / opb
));
1198 while (s
[0] == '0' && s
[1] == '0' && s
[2] == '0' && s
[3] == '0'
1201 skip_addr_chars
+= 4;
1206 info
->insn_info_valid
= 0;
1209 addr_offset
= start_offset
;
1210 while (addr_offset
< stop_offset
)
1214 bfd_boolean need_nl
= FALSE
;
1216 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1218 for (z
= addr_offset
* opb
; z
< stop_offset
* opb
; z
++)
1221 if (! disassemble_zeroes
1222 && (info
->insn_info_valid
== 0
1223 || info
->branch_delay_insns
== 0)
1224 && (z
- addr_offset
* opb
>= SKIP_ZEROES
1225 || (z
== stop_offset
* opb
&&
1226 z
- addr_offset
* opb
< SKIP_ZEROES_AT_END
)))
1230 /* If there are more nonzero octets to follow, we only skip
1231 zeroes in multiples of 4, to try to avoid running over
1232 the start of an instruction which happens to start with
1234 if (z
!= stop_offset
* opb
)
1235 z
= addr_offset
* opb
+ ((z
- addr_offset
* opb
) &~ 3);
1237 octets
= z
- addr_offset
* opb
;
1248 if (with_line_numbers
|| with_source_code
)
1249 /* The line number tables will refer to unadjusted
1250 section VMAs, so we must undo any VMA modifications
1251 when calling show_line. */
1252 show_line (aux
->abfd
, section
, addr_offset
- adjust_section_vma
);
1254 if (! prefix_addresses
)
1258 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ addr_offset
);
1259 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
1263 printf ("%s:\t", buf
+ skip_addr_chars
);
1267 aux
->require_sec
= TRUE
;
1268 objdump_print_address (section
->vma
+ addr_offset
, info
);
1269 aux
->require_sec
= FALSE
;
1276 sfile
.buffer
= xmalloc (sfile
.size
);
1277 sfile
.current
= sfile
.buffer
;
1278 info
->fprintf_func
= (fprintf_ftype
) objdump_sprintf
;
1279 info
->stream
= (FILE *) &sfile
;
1280 info
->bytes_per_line
= 0;
1281 info
->bytes_per_chunk
= 0;
1283 #ifdef DISASSEMBLER_NEEDS_RELOCS
1284 /* FIXME: This is wrong. It tests the number of octets
1285 in the last instruction, not the current one. */
1286 if (*relppp
< relppend
1287 && (**relppp
)->address
>= addr_offset
1288 && (**relppp
)->address
<= addr_offset
+ octets
/ opb
)
1289 info
->flags
= INSN_HAS_RELOC
;
1294 octets
= (*disassemble_fn
) (section
->vma
+ addr_offset
, info
);
1295 info
->fprintf_func
= (fprintf_ftype
) fprintf
;
1296 info
->stream
= stdout
;
1297 if (info
->bytes_per_line
!= 0)
1298 octets_per_line
= info
->bytes_per_line
;
1301 if (sfile
.current
!= sfile
.buffer
)
1302 printf ("%s\n", sfile
.buffer
);
1303 free (sfile
.buffer
);
1311 octets
= octets_per_line
;
1312 if (addr_offset
+ octets
/ opb
> stop_offset
)
1313 octets
= (stop_offset
- addr_offset
) * opb
;
1315 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ octets
; ++j
)
1317 if (ISPRINT (data
[j
]))
1318 buf
[j
- addr_offset
* opb
] = data
[j
];
1320 buf
[j
- addr_offset
* opb
] = '.';
1322 buf
[j
- addr_offset
* opb
] = '\0';
1325 if (prefix_addresses
1327 : show_raw_insn
>= 0)
1331 /* If ! prefix_addresses and ! wide_output, we print
1332 octets_per_line octets per line. */
1334 if (pb
> octets_per_line
&& ! prefix_addresses
&& ! wide_output
)
1335 pb
= octets_per_line
;
1337 if (info
->bytes_per_chunk
)
1338 bpc
= info
->bytes_per_chunk
;
1342 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
1345 if (bpc
> 1 && info
->display_endian
== BFD_ENDIAN_LITTLE
)
1347 for (k
= bpc
- 1; k
>= 0; k
--)
1348 printf ("%02x", (unsigned) data
[j
+ k
]);
1353 for (k
= 0; k
< bpc
; k
++)
1354 printf ("%02x", (unsigned) data
[j
+ k
]);
1359 for (; pb
< octets_per_line
; pb
+= bpc
)
1363 for (k
= 0; k
< bpc
; k
++)
1368 /* Separate raw data from instruction by extra space. */
1379 printf ("%s", sfile
.buffer
);
1380 free (sfile
.buffer
);
1383 if (prefix_addresses
1385 : show_raw_insn
>= 0)
1393 j
= addr_offset
* opb
+ pb
;
1395 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ j
/ opb
);
1396 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
1400 printf ("%s:\t", buf
+ skip_addr_chars
);
1402 pb
+= octets_per_line
;
1405 for (; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
1409 if (bpc
> 1 && info
->display_endian
== BFD_ENDIAN_LITTLE
)
1411 for (k
= bpc
- 1; k
>= 0; k
--)
1412 printf ("%02x", (unsigned) data
[j
+ k
]);
1417 for (k
= 0; k
< bpc
; k
++)
1418 printf ("%02x", (unsigned) data
[j
+ k
]);
1431 if ((section
->flags
& SEC_RELOC
) != 0
1432 #ifndef DISASSEMBLER_NEEDS_RELOCS
1437 while ((*relppp
) < relppend
1438 && ((**relppp
)->address
>= (bfd_vma
) addr_offset
1439 && (**relppp
)->address
< (bfd_vma
) addr_offset
+ octets
/ opb
))
1440 #ifdef DISASSEMBLER_NEEDS_RELOCS
1441 if (! dump_reloc_info
)
1455 objdump_print_value (section
->vma
+ q
->address
, info
, TRUE
);
1457 printf (": %s\t", q
->howto
->name
);
1459 if (q
->sym_ptr_ptr
== NULL
|| *q
->sym_ptr_ptr
== NULL
)
1460 printf ("*unknown*");
1463 const char *sym_name
;
1465 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
1466 if (sym_name
!= NULL
&& *sym_name
!= '\0')
1467 objdump_print_symname (aux
->abfd
, info
, *q
->sym_ptr_ptr
);
1472 sym_sec
= bfd_get_section (*q
->sym_ptr_ptr
);
1473 sym_name
= bfd_get_section_name (aux
->abfd
, sym_sec
);
1474 if (sym_name
== NULL
|| *sym_name
== '\0')
1475 sym_name
= "*unknown*";
1476 printf ("%s", sym_name
);
1483 objdump_print_value (q
->addend
, info
, TRUE
);
1495 addr_offset
+= octets
/ opb
;
1499 /* Disassemble the contents of an object file. */
1502 disassemble_data (bfd
*abfd
)
1504 unsigned long addr_offset
;
1505 disassembler_ftype disassemble_fn
;
1506 struct disassemble_info disasm_info
;
1507 struct objdump_disasm_info aux
;
1512 prev_functionname
= NULL
;
1515 /* We make a copy of syms to sort. We don't want to sort syms
1516 because that will screw up the relocs. */
1517 sorted_syms
= (asymbol
**) xmalloc (symcount
* sizeof (asymbol
*));
1518 memcpy (sorted_syms
, syms
, symcount
* sizeof (asymbol
*));
1520 sorted_symcount
= remove_useless_symbols (sorted_syms
, symcount
);
1522 /* Sort the symbols into section and symbol order. */
1523 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
1525 INIT_DISASSEMBLE_INFO (disasm_info
, stdout
, fprintf
);
1527 disasm_info
.application_data
= (void *) &aux
;
1529 aux
.require_sec
= FALSE
;
1530 disasm_info
.print_address_func
= objdump_print_address
;
1531 disasm_info
.symbol_at_address_func
= objdump_symbol_at_address
;
1533 if (machine
!= (char *) NULL
)
1535 const bfd_arch_info_type
*info
= bfd_scan_arch (machine
);
1538 fatal (_("Can't use supplied machine %s"), machine
);
1540 abfd
->arch_info
= info
;
1543 if (endian
!= BFD_ENDIAN_UNKNOWN
)
1545 struct bfd_target
*xvec
;
1547 xvec
= (struct bfd_target
*) xmalloc (sizeof (struct bfd_target
));
1548 memcpy (xvec
, abfd
->xvec
, sizeof (struct bfd_target
));
1549 xvec
->byteorder
= endian
;
1553 disassemble_fn
= disassembler (abfd
);
1554 if (!disassemble_fn
)
1556 non_fatal (_("Can't disassemble for architecture %s\n"),
1557 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
1562 opb
= bfd_octets_per_byte (abfd
);
1564 disasm_info
.flavour
= bfd_get_flavour (abfd
);
1565 disasm_info
.arch
= bfd_get_arch (abfd
);
1566 disasm_info
.mach
= bfd_get_mach (abfd
);
1567 disasm_info
.disassembler_options
= disassembler_options
;
1568 disasm_info
.octets_per_byte
= opb
;
1570 if (bfd_big_endian (abfd
))
1571 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_BIG
;
1572 else if (bfd_little_endian (abfd
))
1573 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
1575 /* ??? Aborting here seems too drastic. We could default to big or little
1577 disasm_info
.endian
= BFD_ENDIAN_UNKNOWN
;
1579 for (section
= abfd
->sections
;
1580 section
!= (asection
*) NULL
;
1581 section
= section
->next
)
1583 bfd_byte
*data
= NULL
;
1584 bfd_size_type datasize
= 0;
1585 arelent
**relbuf
= NULL
;
1586 arelent
**relpp
= NULL
;
1587 arelent
**relppend
= NULL
;
1588 unsigned long stop_offset
;
1589 asymbol
*sym
= NULL
;
1592 /* Sections that do not contain machine
1593 code are not normally disassembled. */
1594 if (! disassemble_all
1596 && (section
->flags
& SEC_CODE
) == 0)
1603 for (i
= 0; i
< only_used
; i
++)
1604 if (strcmp (only
[i
], section
->name
) == 0)
1611 if ((section
->flags
& SEC_RELOC
) != 0
1612 #ifndef DISASSEMBLER_NEEDS_RELOCS
1619 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
1621 bfd_fatal (bfd_get_filename (abfd
));
1627 relbuf
= (arelent
**) xmalloc (relsize
);
1628 relcount
= bfd_canonicalize_reloc (abfd
, section
, relbuf
, syms
);
1630 bfd_fatal (bfd_get_filename (abfd
));
1632 /* Sort the relocs by address. */
1633 qsort (relbuf
, relcount
, sizeof (arelent
*), compare_relocs
);
1636 relppend
= relpp
+ relcount
;
1638 /* Skip over the relocs belonging to addresses below the
1640 if (start_address
!= (bfd_vma
) -1)
1641 while (relpp
< relppend
1642 && (*relpp
)->address
< start_address
)
1647 datasize
= bfd_get_section_size_before_reloc (section
);
1651 printf (_("Disassembly of section %s:\n"), section
->name
);
1653 data
= (bfd_byte
*) xmalloc ((size_t) datasize
);
1655 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
1658 disasm_info
.buffer
= data
;
1659 disasm_info
.buffer_vma
= section
->vma
;
1660 disasm_info
.buffer_length
= datasize
;
1661 disasm_info
.section
= section
;
1663 if (start_address
== (bfd_vma
) -1
1664 || start_address
< disasm_info
.buffer_vma
)
1667 addr_offset
= start_address
- disasm_info
.buffer_vma
;
1669 if (stop_address
== (bfd_vma
) -1)
1670 stop_offset
= datasize
/ opb
;
1673 if (stop_address
< disasm_info
.buffer_vma
)
1676 stop_offset
= stop_address
- disasm_info
.buffer_vma
;
1677 if (stop_offset
> disasm_info
.buffer_length
/ opb
)
1678 stop_offset
= disasm_info
.buffer_length
/ opb
;
1681 sym
= find_symbol_for_address (abfd
, section
, section
->vma
+ addr_offset
,
1684 while (addr_offset
< stop_offset
)
1687 unsigned long nextstop_offset
;
1690 if (sym
!= NULL
&& bfd_asymbol_value (sym
) <= section
->vma
+ addr_offset
)
1695 (x
< sorted_symcount
1696 && bfd_asymbol_value (sorted_syms
[x
])
1697 <= section
->vma
+ addr_offset
);
1701 disasm_info
.symbols
= & sorted_syms
[place
];
1702 disasm_info
.num_symbols
= x
- place
;
1705 disasm_info
.symbols
= NULL
;
1707 if (! prefix_addresses
)
1709 (* disasm_info
.fprintf_func
) (disasm_info
.stream
, "\n");
1710 objdump_print_addr_with_sym (abfd
, section
, sym
,
1711 section
->vma
+ addr_offset
,
1714 (* disasm_info
.fprintf_func
) (disasm_info
.stream
, ":\n");
1718 && bfd_asymbol_value (sym
) > section
->vma
+ addr_offset
)
1720 else if (sym
== NULL
)
1724 /* Search forward for the next appropriate symbol in
1725 SECTION. Note that all the symbols are sorted
1726 together into one big array, and that some sections
1727 may have overlapping addresses. */
1728 while (place
< sorted_symcount
1729 && (sorted_syms
[place
]->section
!= section
1730 || (bfd_asymbol_value (sorted_syms
[place
])
1731 <= bfd_asymbol_value (sym
))))
1733 if (place
>= sorted_symcount
)
1736 nextsym
= sorted_syms
[place
];
1739 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > section
->vma
+ addr_offset
)
1741 nextstop_offset
= bfd_asymbol_value (sym
) - section
->vma
;
1742 if (nextstop_offset
> stop_offset
)
1743 nextstop_offset
= stop_offset
;
1745 else if (nextsym
== NULL
)
1746 nextstop_offset
= stop_offset
;
1749 nextstop_offset
= bfd_asymbol_value (nextsym
) - section
->vma
;
1750 if (nextstop_offset
> stop_offset
)
1751 nextstop_offset
= stop_offset
;
1754 /* If a symbol is explicitly marked as being an object
1755 rather than a function, just dump the bytes without
1756 disassembling them. */
1759 || bfd_asymbol_value (sym
) > section
->vma
+ addr_offset
1760 || ((sym
->flags
& BSF_OBJECT
) == 0
1761 && (strstr (bfd_asymbol_name (sym
), "gnu_compiled")
1763 && (strstr (bfd_asymbol_name (sym
), "gcc2_compiled")
1765 || (sym
->flags
& BSF_FUNCTION
) != 0)
1770 disassemble_bytes (&disasm_info
, disassemble_fn
, insns
, data
,
1771 addr_offset
, nextstop_offset
, &relpp
, relppend
);
1773 addr_offset
= nextstop_offset
;
1785 /* Dump the stabs sections from an object file that has a section that
1786 uses Sun stabs encoding. */
1789 dump_stabs (bfd
*abfd
)
1791 dump_section_stabs (abfd
, ".stab", ".stabstr");
1792 dump_section_stabs (abfd
, ".stab.excl", ".stab.exclstr");
1793 dump_section_stabs (abfd
, ".stab.index", ".stab.indexstr");
1794 dump_section_stabs (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1797 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1798 and string table section STRSECT_NAME into `strtab'.
1799 If the section exists and was read, allocate the space and return TRUE.
1800 Otherwise return FALSE. */
1803 read_section_stabs (bfd
*abfd
, const char *stabsect_name
,
1804 const char *strsect_name
)
1806 asection
*stabsect
, *stabstrsect
;
1808 stabsect
= bfd_get_section_by_name (abfd
, stabsect_name
);
1811 printf (_("No %s section present\n\n"), stabsect_name
);
1815 stabstrsect
= bfd_get_section_by_name (abfd
, strsect_name
);
1816 if (0 == stabstrsect
)
1818 non_fatal (_("%s has no %s section"),
1819 bfd_get_filename (abfd
), strsect_name
);
1824 stab_size
= bfd_section_size (abfd
, stabsect
);
1825 stabstr_size
= bfd_section_size (abfd
, stabstrsect
);
1827 stabs
= (bfd_byte
*) xmalloc (stab_size
);
1828 strtab
= (char *) xmalloc (stabstr_size
);
1830 if (! bfd_get_section_contents (abfd
, stabsect
, (void *) stabs
, 0, stab_size
))
1832 non_fatal (_("Reading %s section of %s failed: %s"),
1833 stabsect_name
, bfd_get_filename (abfd
),
1834 bfd_errmsg (bfd_get_error ()));
1841 if (! bfd_get_section_contents (abfd
, stabstrsect
, (void *) strtab
, 0,
1844 non_fatal (_("Reading %s section of %s failed: %s\n"),
1845 strsect_name
, bfd_get_filename (abfd
),
1846 bfd_errmsg (bfd_get_error ()));
1856 /* Stabs entries use a 12 byte format:
1857 4 byte string table index
1859 1 byte stab other field
1860 2 byte stab desc field
1862 FIXME: This will have to change for a 64 bit object format. */
1864 #define STRDXOFF (0)
1866 #define OTHEROFF (5)
1869 #define STABSIZE (12)
1871 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1872 using string table section STRSECT_NAME (in `strtab'). */
1875 print_section_stabs (bfd
*abfd
, const char *stabsect_name
,
1876 const char *strsect_name ATTRIBUTE_UNUSED
)
1879 unsigned file_string_table_offset
= 0;
1880 unsigned next_file_string_table_offset
= 0;
1881 bfd_byte
*stabp
, *stabs_end
;
1884 stabs_end
= stabp
+ stab_size
;
1886 printf (_("Contents of %s section:\n\n"), stabsect_name
);
1887 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1889 /* Loop through all symbols and print them.
1891 We start the index at -1 because there is a dummy symbol on
1892 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1893 for (i
= -1; stabp
< stabs_end
; stabp
+= STABSIZE
, i
++)
1897 unsigned char type
, other
;
1898 unsigned short desc
;
1901 strx
= bfd_h_get_32 (abfd
, stabp
+ STRDXOFF
);
1902 type
= bfd_h_get_8 (abfd
, stabp
+ TYPEOFF
);
1903 other
= bfd_h_get_8 (abfd
, stabp
+ OTHEROFF
);
1904 desc
= bfd_h_get_16 (abfd
, stabp
+ DESCOFF
);
1905 value
= bfd_h_get_32 (abfd
, stabp
+ VALOFF
);
1907 printf ("\n%-6d ", i
);
1908 /* Either print the stab name, or, if unnamed, print its number
1909 again (makes consistent formatting for tools like awk). */
1910 name
= bfd_get_stab_name (type
);
1912 printf ("%-6s", name
);
1913 else if (type
== N_UNDF
)
1916 printf ("%-6d", type
);
1917 printf (" %-6d %-6d ", other
, desc
);
1918 bfd_printf_vma (abfd
, value
);
1919 printf (" %-6lu", strx
);
1921 /* Symbols with type == 0 (N_UNDF) specify the length of the
1922 string table associated with this file. We use that info
1923 to know how to relocate the *next* file's string table indices. */
1926 file_string_table_offset
= next_file_string_table_offset
;
1927 next_file_string_table_offset
+= value
;
1931 /* Using the (possibly updated) string table offset, print the
1932 string (if any) associated with this symbol. */
1933 if ((strx
+ file_string_table_offset
) < stabstr_size
)
1934 printf (" %s", &strtab
[strx
+ file_string_table_offset
]);
1943 dump_section_stabs (bfd
*abfd
, char *stabsect_name
, char *strsect_name
)
1947 /* Check for section names for which stabsect_name is a prefix, to
1948 handle .stab0, etc. */
1949 for (s
= abfd
->sections
;
1955 len
= strlen (stabsect_name
);
1957 /* If the prefix matches, and the files section name ends with a
1958 nul or a digit, then we match. I.e., we want either an exact
1959 match or a section followed by a number. */
1960 if (strncmp (stabsect_name
, s
->name
, len
) == 0
1961 && (s
->name
[len
] == '\000'
1962 || ISDIGIT (s
->name
[len
])))
1964 if (read_section_stabs (abfd
, s
->name
, strsect_name
))
1966 print_section_stabs (abfd
, s
->name
, strsect_name
);
1976 dump_bfd_header (bfd
*abfd
)
1980 printf (_("architecture: %s, "),
1981 bfd_printable_arch_mach (bfd_get_arch (abfd
),
1982 bfd_get_mach (abfd
)));
1983 printf (_("flags 0x%08x:\n"), abfd
->flags
);
1985 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1986 PF (HAS_RELOC
, "HAS_RELOC");
1987 PF (EXEC_P
, "EXEC_P");
1988 PF (HAS_LINENO
, "HAS_LINENO");
1989 PF (HAS_DEBUG
, "HAS_DEBUG");
1990 PF (HAS_SYMS
, "HAS_SYMS");
1991 PF (HAS_LOCALS
, "HAS_LOCALS");
1992 PF (DYNAMIC
, "DYNAMIC");
1993 PF (WP_TEXT
, "WP_TEXT");
1994 PF (D_PAGED
, "D_PAGED");
1995 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
1996 PF (HAS_LOAD_PAGE
, "HAS_LOAD_PAGE");
1997 printf (_("\nstart address 0x"));
1998 bfd_printf_vma (abfd
, abfd
->start_address
);
2004 dump_bfd_private_header (bfd
*abfd
)
2006 bfd_print_private_bfd_data (abfd
, stdout
);
2009 /* Dump selected contents of ABFD. */
2012 dump_bfd (bfd
*abfd
)
2014 /* If we are adjusting section VMA's, change them all now. Changing
2015 the BFD information is a hack. However, we must do it, or
2016 bfd_find_nearest_line will not do the right thing. */
2017 if (adjust_section_vma
!= 0)
2021 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2023 s
->vma
+= adjust_section_vma
;
2024 s
->lma
+= adjust_section_vma
;
2028 if (! dump_debugging_tags
)
2029 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd
),
2032 print_arelt_descr (stdout
, abfd
, TRUE
);
2033 if (dump_file_header
)
2034 dump_bfd_header (abfd
);
2035 if (dump_private_headers
)
2036 dump_bfd_private_header (abfd
);
2037 if (! dump_debugging_tags
)
2039 if (dump_section_headers
)
2040 dump_headers (abfd
);
2042 if (dump_symtab
|| dump_reloc_info
|| disassemble
|| dump_debugging
)
2043 syms
= slurp_symtab (abfd
);
2044 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
)
2045 dynsyms
= slurp_dynamic_symtab (abfd
);
2048 dump_symbols (abfd
, FALSE
);
2049 if (dump_dynamic_symtab
)
2050 dump_symbols (abfd
, TRUE
);
2051 if (dump_stab_section_info
)
2053 if (dump_reloc_info
&& ! disassemble
)
2055 if (dump_dynamic_reloc_info
)
2056 dump_dynamic_relocs (abfd
);
2057 if (dump_section_contents
)
2060 disassemble_data (abfd
);
2065 dhandle
= read_debugging_info (abfd
, syms
, symcount
);
2066 if (dhandle
!= NULL
)
2068 if (! print_debugging_info (stdout
, dhandle
, abfd
, syms
, demangle
,
2069 dump_debugging_tags
? TRUE
: FALSE
))
2071 non_fatal (_("%s: printing debugging information failed"),
2072 bfd_get_filename (abfd
));
2092 display_bfd (bfd
*abfd
)
2096 if (bfd_check_format_matches (abfd
, bfd_object
, &matching
))
2102 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
2104 nonfatal (bfd_get_filename (abfd
));
2105 list_matching_formats (matching
);
2110 if (bfd_get_error () != bfd_error_file_not_recognized
)
2112 nonfatal (bfd_get_filename (abfd
));
2116 if (bfd_check_format_matches (abfd
, bfd_core
, &matching
))
2122 nonfatal (bfd_get_filename (abfd
));
2124 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
2126 list_matching_formats (matching
);
2132 display_file (char *filename
, char *target
)
2134 bfd
*file
, *arfile
= (bfd
*) NULL
;
2136 file
= bfd_openr (filename
, target
);
2139 nonfatal (filename
);
2143 if (bfd_check_format (file
, bfd_archive
))
2145 bfd
*last_arfile
= NULL
;
2147 printf (_("In archive %s:\n"), bfd_get_filename (file
));
2150 bfd_set_error (bfd_error_no_error
);
2152 arfile
= bfd_openr_next_archived_file (file
, arfile
);
2155 if (bfd_get_error () != bfd_error_no_more_archived_files
)
2156 nonfatal (bfd_get_filename (file
));
2160 display_bfd (arfile
);
2162 if (last_arfile
!= NULL
)
2163 bfd_close (last_arfile
);
2164 last_arfile
= arfile
;
2167 if (last_arfile
!= NULL
)
2168 bfd_close (last_arfile
);
2176 /* Actually display the various requested regions. */
2179 dump_data (bfd
*abfd
)
2183 bfd_size_type datasize
= 0;
2184 bfd_size_type addr_offset
;
2185 bfd_size_type start_offset
, stop_offset
;
2186 unsigned int opb
= bfd_octets_per_byte (abfd
);
2188 for (section
= abfd
->sections
; section
!= NULL
; section
=
2194 for (i
= 0; i
< only_used
; i
++)
2195 if (strcmp (only
[i
], section
->name
) == 0)
2198 if (only_used
== 0 || i
!= only_used
)
2200 if (section
->flags
& SEC_HAS_CONTENTS
)
2205 printf (_("Contents of section %s:\n"), section
->name
);
2207 if (bfd_section_size (abfd
, section
) == 0)
2209 data
= (bfd_byte
*) xmalloc ((size_t) bfd_section_size
2211 datasize
= bfd_section_size (abfd
, section
);
2214 bfd_get_section_contents (abfd
, section
, (void *) data
, 0,
2215 bfd_section_size (abfd
, section
));
2217 if (start_address
== (bfd_vma
) -1
2218 || start_address
< section
->vma
)
2221 start_offset
= start_address
- section
->vma
;
2222 if (stop_address
== (bfd_vma
) -1)
2223 stop_offset
= bfd_section_size (abfd
, section
) / opb
;
2226 if (stop_address
< section
->vma
)
2229 stop_offset
= stop_address
- section
->vma
;
2230 if (stop_offset
> bfd_section_size (abfd
, section
) / opb
)
2231 stop_offset
= bfd_section_size (abfd
, section
) / opb
;
2236 bfd_sprintf_vma (abfd
, buf
, start_offset
+ section
->vma
);
2237 if (strlen (buf
) >= sizeof (buf
))
2240 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
2242 count
= strlen (buf
) - count
;
2246 bfd_sprintf_vma (abfd
, buf
, stop_offset
+ section
->vma
- 1);
2247 if (strlen (buf
) >= sizeof (buf
))
2250 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
2252 count
= strlen (buf
) - count
;
2256 for (addr_offset
= start_offset
;
2257 addr_offset
< stop_offset
; addr_offset
+= onaline
/ opb
)
2261 bfd_sprintf_vma (abfd
, buf
, (addr_offset
+ section
->vma
));
2262 count
= strlen (buf
);
2263 if ((size_t) count
>= sizeof (buf
))
2266 while (count
< width
)
2271 fputs (buf
+ count
- width
, stdout
);
2274 for (j
= addr_offset
* opb
;
2275 j
< addr_offset
* opb
+ onaline
; j
++)
2277 if (j
< stop_offset
* opb
)
2278 printf ("%02x", (unsigned) (data
[j
]));
2286 for (j
= addr_offset
* opb
;
2287 j
< addr_offset
* opb
+ onaline
; j
++)
2289 if (j
>= stop_offset
* opb
)
2292 printf ("%c", ISPRINT (data
[j
]) ? data
[j
] : '.');
2302 /* Should perhaps share code and display with nm? */
2305 dump_symbols (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_boolean dynamic
)
2315 printf ("DYNAMIC SYMBOL TABLE:\n");
2321 printf ("SYMBOL TABLE:\n");
2325 printf (_("no symbols\n"));
2327 for (count
= 0; count
< max
; count
++)
2331 bfd
*cur_bfd
= bfd_asymbol_bfd (*current
);
2333 if (cur_bfd
!= NULL
)
2338 name
= (*current
)->name
;
2340 if (do_demangle
&& name
!= NULL
&& *name
!= '\0')
2342 /* If we want to demangle the name, we demangle it
2343 here, and temporarily clobber it while calling
2344 bfd_print_symbol. FIXME: This is a gross hack. */
2345 alloc
= demangle (cur_bfd
, name
);
2346 (*current
)->name
= alloc
;
2349 bfd_print_symbol (cur_bfd
, stdout
, *current
,
2350 bfd_print_symbol_all
);
2352 (*current
)->name
= name
;
2366 dump_relocs (bfd
*abfd
)
2372 for (a
= abfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
2376 if (bfd_is_abs_section (a
))
2378 if (bfd_is_und_section (a
))
2380 if (bfd_is_com_section (a
))
2387 for (i
= 0; i
< only_used
; i
++)
2388 if (strcmp (only
[i
], a
->name
) == 0)
2394 else if ((a
->flags
& SEC_RELOC
) == 0)
2397 relsize
= bfd_get_reloc_upper_bound (abfd
, a
);
2399 bfd_fatal (bfd_get_filename (abfd
));
2401 printf ("RELOCATION RECORDS FOR [%s]:", a
->name
);
2405 printf (" (none)\n\n");
2409 relpp
= (arelent
**) xmalloc (relsize
);
2410 relcount
= bfd_canonicalize_reloc (abfd
, a
, relpp
, syms
);
2413 bfd_fatal (bfd_get_filename (abfd
));
2414 else if (relcount
== 0)
2415 printf (" (none)\n\n");
2419 dump_reloc_set (abfd
, a
, relpp
, relcount
);
2428 dump_dynamic_relocs (bfd
*abfd
)
2434 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
2436 bfd_fatal (bfd_get_filename (abfd
));
2438 printf ("DYNAMIC RELOCATION RECORDS");
2441 printf (" (none)\n\n");
2444 relpp
= (arelent
**) xmalloc (relsize
);
2445 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
2448 bfd_fatal (bfd_get_filename (abfd
));
2449 else if (relcount
== 0)
2450 printf (" (none)\n\n");
2454 dump_reloc_set (abfd
, (asection
*) NULL
, relpp
, relcount
);
2462 dump_reloc_set (bfd
*abfd
, asection
*sec
, arelent
**relpp
, long relcount
)
2465 char *last_filename
, *last_functionname
;
2466 unsigned int last_line
;
2468 /* Get column headers lined up reasonably. */
2475 bfd_sprintf_vma (abfd
, buf
, (bfd_vma
) -1);
2476 width
= strlen (buf
) - 7;
2478 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
2481 last_filename
= NULL
;
2482 last_functionname
= NULL
;
2485 for (p
= relpp
; relcount
&& *p
!= (arelent
*) NULL
; p
++, relcount
--)
2488 const char *filename
, *functionname
;
2490 const char *sym_name
;
2491 const char *section_name
;
2493 if (start_address
!= (bfd_vma
) -1
2494 && q
->address
< start_address
)
2496 if (stop_address
!= (bfd_vma
) -1
2497 && q
->address
> stop_address
)
2500 if (with_line_numbers
2502 && bfd_find_nearest_line (abfd
, sec
, syms
, q
->address
,
2503 &filename
, &functionname
, &line
))
2505 if (functionname
!= NULL
2506 && (last_functionname
== NULL
2507 || strcmp (functionname
, last_functionname
) != 0))
2509 printf ("%s():\n", functionname
);
2510 if (last_functionname
!= NULL
)
2511 free (last_functionname
);
2512 last_functionname
= xstrdup (functionname
);
2516 && (line
!= last_line
2517 || (filename
!= NULL
2518 && last_filename
!= NULL
2519 && strcmp (filename
, last_filename
) != 0)))
2521 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
2523 if (last_filename
!= NULL
)
2524 free (last_filename
);
2525 if (filename
== NULL
)
2526 last_filename
= NULL
;
2528 last_filename
= xstrdup (filename
);
2532 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
2534 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
2535 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
2540 section_name
= NULL
;
2545 bfd_printf_vma (abfd
, q
->address
);
2547 printf (" %-16s ", q
->howto
->name
);
2549 printf (" %-16d ", q
->howto
->type
);
2550 objdump_print_symname (abfd
, (struct disassemble_info
*) NULL
,
2555 if (section_name
== (const char *) NULL
)
2556 section_name
= "*unknown*";
2557 bfd_printf_vma (abfd
, q
->address
);
2558 printf (" %-16s [%s]",
2566 bfd_printf_vma (abfd
, q
->addend
);
2574 main (int argc
, char **argv
)
2577 char *target
= default_target
;
2578 bfd_boolean seenflag
= FALSE
;
2580 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2581 setlocale (LC_MESSAGES
, "");
2583 #if defined (HAVE_SETLOCALE)
2584 setlocale (LC_CTYPE
, "");
2586 bindtextdomain (PACKAGE
, LOCALEDIR
);
2587 textdomain (PACKAGE
);
2589 program_name
= *argv
;
2590 xmalloc_set_program_name (program_name
);
2592 START_PROGRESS (program_name
, 0);
2595 set_default_bfd_target ();
2597 while ((c
= getopt_long (argc
, argv
, "pib:m:M:VvCdDlfaHhrRtTxsSj:wE:zgeG",
2598 long_options
, (int *) 0))
2604 break; /* We've been given a long option. */
2609 if (disassembler_options
)
2610 /* Ignore potential memory leak for now. */
2611 disassembler_options
= concat (disassembler_options
, ",",
2614 disassembler_options
= optarg
;
2620 only
= (char **) xmalloc (only_size
* sizeof (char *));
2622 else if (only_used
== only_size
)
2625 only
= (char **) xrealloc (only
,
2626 only_size
* sizeof (char *));
2628 only
[only_used
++] = optarg
;
2631 with_line_numbers
= TRUE
;
2640 enum demangling_styles style
;
2642 style
= cplus_demangle_name_to_style (optarg
);
2643 if (style
== unknown_demangling
)
2644 fatal (_("unknown demangling style `%s'"),
2647 cplus_demangle_set_style (style
);
2653 case OPTION_ADJUST_VMA
:
2654 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
2656 case OPTION_START_ADDRESS
:
2657 start_address
= parse_vma (optarg
, "--start-address");
2659 case OPTION_STOP_ADDRESS
:
2660 stop_address
= parse_vma (optarg
, "--stop-address");
2663 if (strcmp (optarg
, "B") == 0)
2664 endian
= BFD_ENDIAN_BIG
;
2665 else if (strcmp (optarg
, "L") == 0)
2666 endian
= BFD_ENDIAN_LITTLE
;
2669 non_fatal (_("unrecognized -E option"));
2674 if (strncmp (optarg
, "big", strlen (optarg
)) == 0)
2675 endian
= BFD_ENDIAN_BIG
;
2676 else if (strncmp (optarg
, "little", strlen (optarg
)) == 0)
2677 endian
= BFD_ENDIAN_LITTLE
;
2680 non_fatal (_("unrecognized --endian type `%s'"), optarg
);
2686 dump_file_header
= TRUE
;
2690 formats_info
= TRUE
;
2694 dump_private_headers
= TRUE
;
2698 dump_private_headers
= TRUE
;
2700 dump_reloc_info
= TRUE
;
2701 dump_file_header
= TRUE
;
2702 dump_ar_hdrs
= TRUE
;
2703 dump_section_headers
= TRUE
;
2711 dump_dynamic_symtab
= TRUE
;
2719 disassemble_zeroes
= TRUE
;
2723 disassemble_all
= TRUE
;
2728 with_source_code
= TRUE
;
2737 dump_debugging_tags
= 1;
2742 dump_stab_section_info
= TRUE
;
2746 dump_section_contents
= TRUE
;
2750 dump_reloc_info
= TRUE
;
2754 dump_dynamic_reloc_info
= TRUE
;
2758 dump_ar_hdrs
= TRUE
;
2762 dump_section_headers
= TRUE
;
2770 show_version
= TRUE
;
2780 print_version ("objdump");
2786 exit_status
= display_info ();
2790 display_file ("a.out", target
);
2792 for (; optind
< argc
;)
2793 display_file (argv
[optind
++], target
);
2796 END_PROGRESS (program_name
);