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 int wide_output
; /* -w */
72 static bfd_vma start_address
= (bfd_vma
) -1; /* --start-address */
73 static bfd_vma stop_address
= (bfd_vma
) -1; /* --stop-address */
74 static int dump_debugging
; /* --debugging */
75 static bfd_vma adjust_section_vma
= 0; /* --adjust-vma */
76 static int file_start_context
= 0; /* --file-start-context */
78 /* Extra info to pass to the disassembler address printing function. */
79 struct objdump_disasm_info
83 bfd_boolean require_sec
;
86 /* Architecture to disassemble for, or default if NULL. */
87 static char *machine
= (char *) NULL
;
89 /* Target specific options to the disassembler. */
90 static char *disassembler_options
= (char *) NULL
;
92 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
93 static enum bfd_endian endian
= BFD_ENDIAN_UNKNOWN
;
95 /* The symbol table. */
96 static asymbol
**syms
;
98 /* Number of symbols in `syms'. */
99 static long symcount
= 0;
101 /* The sorted symbol table. */
102 static asymbol
**sorted_syms
;
104 /* Number of symbols in `sorted_syms'. */
105 static long sorted_symcount
= 0;
107 /* The dynamic symbol table. */
108 static asymbol
**dynsyms
;
110 /* Number of symbols in `dynsyms'. */
111 static long dynsymcount
= 0;
113 static bfd_byte
*stabs
;
114 static bfd_size_type stab_size
;
117 static bfd_size_type stabstr_size
;
119 /* Static declarations. */
122 PARAMS ((FILE *, int));
124 PARAMS ((const char *));
125 static void display_file
126 PARAMS ((char *, char *));
127 static void dump_section_header
128 PARAMS ((bfd
*, asection
*, PTR
));
129 static void dump_headers
131 static void dump_data
133 static void dump_relocs
135 static void dump_dynamic_relocs
137 static void dump_reloc_set
138 PARAMS ((bfd
*, asection
*, arelent
**, long));
139 static void dump_symbols
140 PARAMS ((bfd
*, bfd_boolean
));
141 static void dump_bfd_header
143 static void dump_bfd_private_header
147 static void display_bfd
149 static void objdump_print_value
150 PARAMS ((bfd_vma
, struct disassemble_info
*, bfd_boolean
));
151 static void objdump_print_symname
152 PARAMS ((bfd
*, struct disassemble_info
*, asymbol
*));
153 static asymbol
*find_symbol_for_address
154 PARAMS ((bfd
*, asection
*, bfd_vma
, bfd_boolean
, long *));
155 static void objdump_print_addr_with_sym
156 PARAMS ((bfd
*, asection
*, asymbol
*, bfd_vma
,
157 struct disassemble_info
*, bfd_boolean
));
158 static void objdump_print_addr
159 PARAMS ((bfd_vma
, struct disassemble_info
*, bfd_boolean
));
160 static void objdump_print_address
161 PARAMS ((bfd_vma
, struct disassemble_info
*));
162 static int objdump_symbol_at_address
163 PARAMS ((bfd_vma
, struct disassemble_info
*));
164 static void show_line
165 PARAMS ((bfd
*, asection
*, bfd_vma
));
166 static void disassemble_bytes
167 PARAMS ((struct disassemble_info
*, disassembler_ftype
, bfd_boolean
,
168 bfd_byte
*, bfd_vma
, bfd_vma
, arelent
***, arelent
**));
169 static void disassemble_data
171 static asymbol
** slurp_symtab
173 static asymbol
** slurp_dynamic_symtab
175 static long remove_useless_symbols
176 PARAMS ((asymbol
**, long));
177 static int compare_symbols
178 PARAMS ((const PTR
, const PTR
));
179 static int compare_relocs
180 PARAMS ((const PTR
, const PTR
));
181 static void dump_stabs
183 static bfd_boolean read_section_stabs
184 PARAMS ((bfd
*, const char *, const char *));
185 static void print_section_stabs
186 PARAMS ((bfd
*, const char *, const char *));
187 static void dump_section_stabs
188 PARAMS ((bfd
*, char *, char *));
191 usage (stream
, status
)
195 fprintf (stream
, _("Usage: %s <option(s)> <file(s)>\n"), program_name
);
196 fprintf (stream
, _(" Display information from object <file(s)>.\n"));
197 fprintf (stream
, _(" At least one of the following switches must be given:\n"));
198 fprintf (stream
, _("\
199 -a, --archive-headers Display archive header information\n\
200 -f, --file-headers Display the contents of the overall file header\n\
201 -p, --private-headers Display object format specific file header contents\n\
202 -h, --[section-]headers Display the contents of the section headers\n\
203 -x, --all-headers Display the contents of all headers\n\
204 -d, --disassemble Display assembler contents of executable sections\n\
205 -D, --disassemble-all Display assembler contents of all sections\n\
206 -S, --source Intermix source code with disassembly\n\
207 -s, --full-contents Display the full contents of all sections requested\n\
208 -g, --debugging Display debug information in object file\n\
209 -G, --stabs Display (in raw form) any STABS info in the file\n\
210 -t, --syms Display the contents of the symbol table(s)\n\
211 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
212 -r, --reloc Display the relocation entries in the file\n\
213 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
214 -v, --version Display this program's version number\n\
215 -i, --info List object formats and architectures supported\n\
216 -H, --help Display this information\n\
220 fprintf (stream
, _("\n The following switches are optional:\n"));
221 fprintf (stream
, _("\
222 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
223 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
224 -j, --section=NAME Only display information for section NAME\n\
225 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
226 -EB --endian=big Assume big endian format when disassembling\n\
227 -EL --endian=little Assume little endian format when disassembling\n\
228 --file-start-context Include context from start of file (with -S)\n\
229 -l, --line-numbers Include line numbers and filenames in output\n\
230 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
231 The STYLE, if specified, can be `auto', `gnu',\n\
232 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
234 -w, --wide Format output for more than 80 columns\n\
235 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
236 --start-address=ADDR Only process data whose address is >= ADDR\n\
237 --stop-address=ADDR Only process data whose address is <= ADDR\n\
238 --prefix-addresses Print complete address alongside disassembly\n\
239 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
240 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
242 list_supported_targets (program_name
, stream
);
243 list_supported_architectures (program_name
, stream
);
245 disassembler_usage (stream
);
248 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
252 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
254 #define OPTION_ENDIAN (150)
255 #define OPTION_START_ADDRESS (OPTION_ENDIAN + 1)
256 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
257 #define OPTION_ADJUST_VMA (OPTION_STOP_ADDRESS + 1)
259 static struct option long_options
[]=
261 {"adjust-vma", required_argument
, NULL
, OPTION_ADJUST_VMA
},
262 {"all-headers", no_argument
, NULL
, 'x'},
263 {"private-headers", no_argument
, NULL
, 'p'},
264 {"architecture", required_argument
, NULL
, 'm'},
265 {"archive-headers", no_argument
, NULL
, 'a'},
266 {"debugging", no_argument
, NULL
, 'g'},
267 {"demangle", optional_argument
, NULL
, 'C'},
268 {"disassemble", no_argument
, NULL
, 'd'},
269 {"disassemble-all", no_argument
, NULL
, 'D'},
270 {"disassembler-options", required_argument
, NULL
, 'M'},
271 {"disassemble-zeroes", no_argument
, NULL
, 'z'},
272 {"dynamic-reloc", no_argument
, NULL
, 'R'},
273 {"dynamic-syms", no_argument
, NULL
, 'T'},
274 {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
275 {"file-headers", no_argument
, NULL
, 'f'},
276 {"file-start-context", no_argument
, &file_start_context
, 1},
277 {"full-contents", no_argument
, NULL
, 's'},
278 {"headers", no_argument
, NULL
, 'h'},
279 {"help", no_argument
, NULL
, 'H'},
280 {"info", no_argument
, NULL
, 'i'},
281 {"line-numbers", no_argument
, NULL
, 'l'},
282 {"no-show-raw-insn", no_argument
, &show_raw_insn
, -1},
283 {"prefix-addresses", no_argument
, &prefix_addresses
, 1},
284 {"reloc", no_argument
, NULL
, 'r'},
285 {"section", required_argument
, NULL
, 'j'},
286 {"section-headers", no_argument
, NULL
, 'h'},
287 {"show-raw-insn", no_argument
, &show_raw_insn
, 1},
288 {"source", no_argument
, NULL
, 'S'},
289 {"stabs", no_argument
, NULL
, 'G'},
290 {"start-address", required_argument
, NULL
, OPTION_START_ADDRESS
},
291 {"stop-address", required_argument
, NULL
, OPTION_STOP_ADDRESS
},
292 {"syms", no_argument
, NULL
, 't'},
293 {"target", required_argument
, NULL
, 'b'},
294 {"version", no_argument
, NULL
, 'V'},
295 {"wide", no_argument
, NULL
, 'w'},
296 {0, no_argument
, 0, 0}
308 dump_section_header (abfd
, section
, ignored
)
309 bfd
*abfd ATTRIBUTE_UNUSED
;
311 PTR ignored ATTRIBUTE_UNUSED
;
314 unsigned int opb
= bfd_octets_per_byte (abfd
);
316 printf ("%3d %-13s %08lx ", section
->index
,
317 bfd_get_section_name (abfd
, section
),
318 (unsigned long) bfd_section_size (abfd
, section
) / opb
);
319 bfd_printf_vma (abfd
, bfd_get_section_vma (abfd
, section
));
321 bfd_printf_vma (abfd
, section
->lma
);
322 printf (" %08lx 2**%u", (unsigned long) section
->filepos
,
323 bfd_get_section_alignment (abfd
, section
));
329 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
331 PF (SEC_HAS_CONTENTS
, "CONTENTS");
332 PF (SEC_ALLOC
, "ALLOC");
333 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
334 PF (SEC_LOAD
, "LOAD");
335 PF (SEC_RELOC
, "RELOC");
336 PF (SEC_READONLY
, "READONLY");
337 PF (SEC_CODE
, "CODE");
338 PF (SEC_DATA
, "DATA");
340 PF (SEC_DEBUGGING
, "DEBUGGING");
341 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
342 PF (SEC_EXCLUDE
, "EXCLUDE");
343 PF (SEC_SORT_ENTRIES
, "SORT_ENTRIES");
344 PF (SEC_BLOCK
, "BLOCK");
345 PF (SEC_CLINK
, "CLINK");
346 PF (SEC_SMALL_DATA
, "SMALL_DATA");
347 PF (SEC_SHARED
, "SHARED");
348 PF (SEC_ARCH_BIT_0
, "ARCH_BIT_0");
349 PF (SEC_THREAD_LOCAL
, "THREAD_LOCAL");
351 if ((section
->flags
& SEC_LINK_ONCE
) != 0)
355 switch (section
->flags
& SEC_LINK_DUPLICATES
)
359 case SEC_LINK_DUPLICATES_DISCARD
:
360 ls
= "LINK_ONCE_DISCARD";
362 case SEC_LINK_DUPLICATES_ONE_ONLY
:
363 ls
= "LINK_ONCE_ONE_ONLY";
365 case SEC_LINK_DUPLICATES_SAME_SIZE
:
366 ls
= "LINK_ONCE_SAME_SIZE";
368 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
369 ls
= "LINK_ONCE_SAME_CONTENTS";
372 printf ("%s%s", comma
, ls
);
374 if (section
->comdat
!= NULL
)
375 printf (" (COMDAT %s %ld)", section
->comdat
->name
,
376 section
->comdat
->symbol
);
389 printf (_("Sections:\n"));
392 printf (_("Idx Name Size VMA LMA File off Algn"));
394 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
395 if (bfd_get_arch_size (abfd
) == 32)
396 printf (_("Idx Name Size VMA LMA File off Algn"));
398 printf (_("Idx Name Size VMA LMA File off Algn"));
402 printf (_(" Flags"));
403 if (abfd
->flags
& HAS_LOAD_PAGE
)
407 bfd_map_over_sections (abfd
, dump_section_header
, (PTR
) NULL
);
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 (abfd
)
441 asymbol
**sy
= (asymbol
**) NULL
;
444 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
447 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
449 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd
));
454 bfd_fatal (bfd_get_filename (abfd
));
457 sy
= (asymbol
**) xmalloc (storage
);
459 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
461 bfd_fatal (bfd_get_filename (abfd
));
465 /* Filter out (in place) symbols that are useless for disassembly.
466 COUNT is the number of elements in SYMBOLS.
467 Return the number of useful symbols. */
470 remove_useless_symbols (symbols
, count
)
474 register asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
478 asymbol
*sym
= *in_ptr
++;
480 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
482 if (sym
->flags
& (BSF_DEBUGGING
))
484 if (bfd_is_und_section (sym
->section
)
485 || bfd_is_com_section (sym
->section
))
490 return out_ptr
- symbols
;
493 /* Sort symbols into value order. */
496 compare_symbols (ap
, bp
)
500 const asymbol
*a
= *(const asymbol
**)ap
;
501 const asymbol
*b
= *(const asymbol
**)bp
;
505 flagword aflags
, bflags
;
507 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
509 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
512 if (a
->section
> b
->section
)
514 else if (a
->section
< b
->section
)
517 an
= bfd_asymbol_name (a
);
518 bn
= bfd_asymbol_name (b
);
522 /* The symbols gnu_compiled and gcc2_compiled convey no real
523 information, so put them after other symbols with the same value. */
524 af
= (strstr (an
, "gnu_compiled") != NULL
525 || strstr (an
, "gcc2_compiled") != NULL
);
526 bf
= (strstr (bn
, "gnu_compiled") != NULL
527 || strstr (bn
, "gcc2_compiled") != NULL
);
534 /* We use a heuristic for the file name, to try to sort it after
535 more useful symbols. It may not work on non Unix systems, but it
536 doesn't really matter; the only difference is precisely which
537 symbol names get printed. */
539 #define file_symbol(s, sn, snl) \
540 (((s)->flags & BSF_FILE) != 0 \
541 || ((sn)[(snl) - 2] == '.' \
542 && ((sn)[(snl) - 1] == 'o' \
543 || (sn)[(snl) - 1] == 'a')))
545 af
= file_symbol (a
, an
, anl
);
546 bf
= file_symbol (b
, bn
, bnl
);
553 /* Try to sort global symbols before local symbols before function
554 symbols before debugging symbols. */
559 if ((aflags
& BSF_DEBUGGING
) != (bflags
& BSF_DEBUGGING
))
561 if ((aflags
& BSF_DEBUGGING
) != 0)
566 if ((aflags
& BSF_FUNCTION
) != (bflags
& BSF_FUNCTION
))
568 if ((aflags
& BSF_FUNCTION
) != 0)
573 if ((aflags
& BSF_LOCAL
) != (bflags
& BSF_LOCAL
))
575 if ((aflags
& BSF_LOCAL
) != 0)
580 if ((aflags
& BSF_GLOBAL
) != (bflags
& BSF_GLOBAL
))
582 if ((aflags
& BSF_GLOBAL
) != 0)
588 /* Symbols that start with '.' might be section names, so sort them
589 after symbols that don't start with '.'. */
590 if (an
[0] == '.' && bn
[0] != '.')
592 if (an
[0] != '.' && bn
[0] == '.')
595 /* Finally, if we can't distinguish them in any other way, try to
596 get consistent results by sorting the symbols by name. */
597 return strcmp (an
, bn
);
600 /* Sort relocs into address order. */
603 compare_relocs (ap
, bp
)
607 const arelent
*a
= *(const arelent
**)ap
;
608 const arelent
*b
= *(const arelent
**)bp
;
610 if (a
->address
> b
->address
)
612 else if (a
->address
< b
->address
)
615 /* So that associated relocations tied to the same address show up
616 in the correct order, we don't do any further sorting. */
625 /* Print VMA to STREAM. If SKIP_ZEROES is TRUE, omit leading zeroes. */
628 objdump_print_value (vma
, info
, skip_zeroes
)
630 struct disassemble_info
*info
;
631 bfd_boolean skip_zeroes
;
635 struct objdump_disasm_info
*aux
636 = (struct objdump_disasm_info
*) info
->application_data
;
638 bfd_sprintf_vma (aux
->abfd
, buf
, vma
);
643 for (p
= buf
; *p
== '0'; ++p
)
648 (*info
->fprintf_func
) (info
->stream
, "%s", p
);
651 /* Print the name of a symbol. */
654 objdump_print_symname (abfd
, info
, sym
)
656 struct disassemble_info
*info
;
663 name
= bfd_asymbol_name (sym
);
664 if (do_demangle
&& name
[0] != '\0')
666 /* Demangle the name. */
667 alloc
= demangle (abfd
, name
);
672 (*info
->fprintf_func
) (info
->stream
, "%s", name
);
680 /* Locate a symbol given a bfd, a section, and a VMA. If REQUIRE_SEC
681 is TRUE, then always require the symbol to be in the section. This
682 returns NULL if there is no suitable symbol. If PLACE is not NULL,
683 then *PLACE is set to the index of the symbol in sorted_syms. */
686 find_symbol_for_address (abfd
, sec
, vma
, require_sec
, place
)
690 bfd_boolean require_sec
;
693 /* @@ Would it speed things up to cache the last two symbols returned,
694 and maybe their address ranges? For many processors, only one memory
695 operand can be present at a time, so the 2-entry cache wouldn't be
696 constantly churned by code doing heavy memory accesses. */
698 /* Indices in `sorted_syms'. */
700 long max
= sorted_symcount
;
702 unsigned int opb
= bfd_octets_per_byte (abfd
);
704 if (sorted_symcount
< 1)
707 /* Perform a binary search looking for the closest symbol to the
708 required value. We are searching the range (min, max]. */
709 while (min
+ 1 < max
)
713 thisplace
= (max
+ min
) / 2;
714 sym
= sorted_syms
[thisplace
];
716 if (bfd_asymbol_value (sym
) > vma
)
718 else if (bfd_asymbol_value (sym
) < vma
)
727 /* The symbol we want is now in min, the low end of the range we
728 were searching. If there are several symbols with the same
729 value, we want the first one. */
732 && (bfd_asymbol_value (sorted_syms
[thisplace
])
733 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
736 /* If the file is relocateable, and the symbol could be from this
737 section, prefer a symbol from this section over symbols from
738 others, even if the other symbol's value might be closer.
740 Note that this may be wrong for some symbol references if the
741 sections have overlapping memory ranges, but in that case there's
742 no way to tell what's desired without looking at the relocation
744 if (sorted_syms
[thisplace
]->section
!= sec
746 || ((abfd
->flags
& HAS_RELOC
) != 0
747 && vma
>= bfd_get_section_vma (abfd
, sec
)
748 && vma
< (bfd_get_section_vma (abfd
, sec
)
749 + bfd_section_size (abfd
, sec
) / opb
))))
753 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
755 if (bfd_asymbol_value (sorted_syms
[i
])
756 != bfd_asymbol_value (sorted_syms
[thisplace
]))
764 if (sorted_syms
[i
]->section
== sec
766 || sorted_syms
[i
- 1]->section
!= sec
767 || (bfd_asymbol_value (sorted_syms
[i
])
768 != bfd_asymbol_value (sorted_syms
[i
- 1]))))
775 if (sorted_syms
[thisplace
]->section
!= sec
)
777 /* We didn't find a good symbol with a smaller value.
778 Look for one with a larger value. */
779 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
781 if (sorted_syms
[i
]->section
== sec
)
789 if (sorted_syms
[thisplace
]->section
!= sec
791 || ((abfd
->flags
& HAS_RELOC
) != 0
792 && vma
>= bfd_get_section_vma (abfd
, sec
)
793 && vma
< (bfd_get_section_vma (abfd
, sec
)
794 + bfd_section_size (abfd
, sec
)))))
796 /* There is no suitable symbol. */
804 return sorted_syms
[thisplace
];
807 /* Print an address to INFO symbolically. */
810 objdump_print_addr_with_sym (abfd
, sec
, sym
, vma
, info
, skip_zeroes
)
815 struct disassemble_info
*info
;
816 bfd_boolean skip_zeroes
;
818 objdump_print_value (vma
, info
, skip_zeroes
);
824 (*info
->fprintf_func
) (info
->stream
, " <%s",
825 bfd_get_section_name (abfd
, sec
));
826 secaddr
= bfd_get_section_vma (abfd
, sec
);
829 (*info
->fprintf_func
) (info
->stream
, "-0x");
830 objdump_print_value (secaddr
- vma
, info
, TRUE
);
832 else if (vma
> secaddr
)
834 (*info
->fprintf_func
) (info
->stream
, "+0x");
835 objdump_print_value (vma
- secaddr
, info
, TRUE
);
837 (*info
->fprintf_func
) (info
->stream
, ">");
841 (*info
->fprintf_func
) (info
->stream
, " <");
842 objdump_print_symname (abfd
, info
, sym
);
843 if (bfd_asymbol_value (sym
) > vma
)
845 (*info
->fprintf_func
) (info
->stream
, "-0x");
846 objdump_print_value (bfd_asymbol_value (sym
) - vma
, info
, TRUE
);
848 else if (vma
> bfd_asymbol_value (sym
))
850 (*info
->fprintf_func
) (info
->stream
, "+0x");
851 objdump_print_value (vma
- bfd_asymbol_value (sym
), info
, TRUE
);
853 (*info
->fprintf_func
) (info
->stream
, ">");
857 /* Print VMA to INFO, symbolically if possible. If SKIP_ZEROES is
858 TRUE, don't output leading zeroes. */
861 objdump_print_addr (vma
, info
, skip_zeroes
)
863 struct disassemble_info
*info
;
864 bfd_boolean skip_zeroes
;
866 struct objdump_disasm_info
*aux
;
869 if (sorted_symcount
< 1)
871 (*info
->fprintf_func
) (info
->stream
, "0x");
872 objdump_print_value (vma
, info
, skip_zeroes
);
876 aux
= (struct objdump_disasm_info
*) info
->application_data
;
877 sym
= find_symbol_for_address (aux
->abfd
, aux
->sec
, vma
, aux
->require_sec
,
879 objdump_print_addr_with_sym (aux
->abfd
, aux
->sec
, sym
, vma
, info
,
883 /* Print VMA to INFO. This function is passed to the disassembler
887 objdump_print_address (vma
, info
)
889 struct disassemble_info
*info
;
891 objdump_print_addr (vma
, info
, ! prefix_addresses
);
894 /* Determine of the given address has a symbol associated with it. */
897 objdump_symbol_at_address (vma
, info
)
899 struct disassemble_info
* info
;
901 struct objdump_disasm_info
* aux
;
904 /* No symbols - do not bother checking. */
905 if (sorted_symcount
< 1)
908 aux
= (struct objdump_disasm_info
*) info
->application_data
;
909 sym
= find_symbol_for_address (aux
->abfd
, aux
->sec
, vma
, aux
->require_sec
,
912 return (sym
!= NULL
&& (bfd_asymbol_value (sym
) == vma
));
915 /* Hold the last function name and the last line number we displayed
918 static char *prev_functionname
;
919 static unsigned int prev_line
;
921 /* We keep a list of all files that we have seen when doing a
922 dissassembly with source, so that we know how much of the file to
923 display. This can be important for inlined functions. */
925 struct print_file_list
927 struct print_file_list
*next
;
933 static struct print_file_list
*print_files
;
935 /* The number of preceding context lines to show when we start
936 displaying a file for the first time. */
938 #define SHOW_PRECEDING_CONTEXT_LINES (5)
940 /* Skip ahead to a given line in a file, optionally printing each
943 static void skip_to_line
944 PARAMS ((struct print_file_list
*, unsigned int, bfd_boolean
));
947 skip_to_line (p
, line
, show
)
948 struct print_file_list
*p
;
952 while (p
->line
< line
)
956 if (fgets (buf
, sizeof buf
, p
->f
) == NULL
)
966 if (strchr (buf
, '\n') != NULL
)
971 /* Show the line number, or the source line, in a dissassembly
975 show_line (abfd
, section
, addr_offset
)
980 const char *filename
;
981 const char *functionname
;
984 if (! with_line_numbers
&& ! with_source_code
)
987 if (! bfd_find_nearest_line (abfd
, section
, syms
, addr_offset
, &filename
,
988 &functionname
, &line
))
991 if (filename
!= NULL
&& *filename
== '\0')
993 if (functionname
!= NULL
&& *functionname
== '\0')
996 if (with_line_numbers
)
998 if (functionname
!= NULL
999 && (prev_functionname
== NULL
1000 || strcmp (functionname
, prev_functionname
) != 0))
1001 printf ("%s():\n", functionname
);
1002 if (line
> 0 && line
!= prev_line
)
1003 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
1006 if (with_source_code
1010 struct print_file_list
**pp
, *p
;
1012 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1013 if (strcmp ((*pp
)->filename
, filename
) == 0)
1019 if (p
!= print_files
)
1023 /* We have reencountered a file name which we saw
1024 earlier. This implies that either we are dumping out
1025 code from an included file, or the same file was
1026 linked in more than once. There are two common cases
1027 of an included file: inline functions in a header
1028 file, and a bison or flex skeleton file. In the
1029 former case we want to just start printing (but we
1030 back up a few lines to give context); in the latter
1031 case we want to continue from where we left off. I
1032 can't think of a good way to distinguish the cases,
1033 so I used a heuristic based on the file name. */
1034 if (strcmp (p
->filename
+ strlen (p
->filename
) - 2, ".h") != 0)
1038 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
1045 p
->f
= fopen (p
->filename
, "r");
1049 skip_to_line (p
, l
, FALSE
);
1051 if (print_files
->f
!= NULL
)
1053 fclose (print_files
->f
);
1054 print_files
->f
= NULL
;
1060 skip_to_line (p
, line
, TRUE
);
1062 p
->next
= print_files
;
1070 f
= fopen (filename
, "r");
1075 p
= ((struct print_file_list
*)
1076 xmalloc (sizeof (struct print_file_list
)));
1077 p
->filename
= xmalloc (strlen (filename
) + 1);
1078 strcpy (p
->filename
, filename
);
1082 if (print_files
!= NULL
&& print_files
->f
!= NULL
)
1084 fclose (print_files
->f
);
1085 print_files
->f
= NULL
;
1087 p
->next
= print_files
;
1090 if (file_start_context
)
1093 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
1096 skip_to_line (p
, l
, FALSE
);
1098 skip_to_line (p
, line
, TRUE
);
1103 if (functionname
!= NULL
1104 && (prev_functionname
== NULL
1105 || strcmp (functionname
, prev_functionname
) != 0))
1107 if (prev_functionname
!= NULL
)
1108 free (prev_functionname
);
1109 prev_functionname
= xmalloc (strlen (functionname
) + 1);
1110 strcpy (prev_functionname
, functionname
);
1113 if (line
> 0 && line
!= prev_line
)
1117 /* Pseudo FILE object for strings. */
1125 /* sprintf to a "stream" */
1128 objdump_sprintf
VPARAMS ((SFILE
*f
, const char *format
, ...))
1133 VA_OPEN (args
, format
);
1134 VA_FIXEDARG (args
, SFILE
*, f
);
1135 VA_FIXEDARG (args
, const char *, format
);
1137 vasprintf (&buf
, format
, args
);
1142 fatal (_("Out of virtual memory"));
1147 while ((size_t) ((f
->buffer
+ f
->size
) - f
->current
) < n
+ 1)
1151 curroff
= f
->current
- f
->buffer
;
1153 f
->buffer
= xrealloc (f
->buffer
, f
->size
);
1154 f
->current
= f
->buffer
+ curroff
;
1157 memcpy (f
->current
, buf
, n
);
1159 f
->current
[0] = '\0';
1167 /* The number of zeroes we want to see before we start skipping them.
1168 The number is arbitrarily chosen. */
1171 #define SKIP_ZEROES (8)
1174 /* The number of zeroes to skip at the end of a section. If the
1175 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1176 SKIP_ZEROES, they will be disassembled. If there are fewer than
1177 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1178 attempt to avoid disassembling zeroes inserted by section
1181 #ifndef SKIP_ZEROES_AT_END
1182 #define SKIP_ZEROES_AT_END (3)
1185 /* Disassemble some data in memory between given values. */
1188 disassemble_bytes (info
, disassemble_fn
, insns
, data
,
1189 start_offset
, stop_offset
, relppp
,
1191 struct disassemble_info
*info
;
1192 disassembler_ftype disassemble_fn
;
1195 bfd_vma start_offset
;
1196 bfd_vma stop_offset
;
1200 struct objdump_disasm_info
*aux
;
1202 int octets_per_line
;
1203 bfd_boolean done_dot
;
1204 int skip_addr_chars
;
1205 bfd_vma addr_offset
;
1206 int opb
= info
->octets_per_byte
;
1208 aux
= (struct objdump_disasm_info
*) info
->application_data
;
1212 octets_per_line
= 4;
1214 octets_per_line
= 16;
1216 /* Figure out how many characters to skip at the start of an
1217 address, to make the disassembly look nicer. We discard leading
1218 zeroes in chunks of 4, ensuring that there is always a leading
1220 skip_addr_chars
= 0;
1221 if (! prefix_addresses
)
1229 + bfd_section_size (section
->owner
, section
) / opb
));
1231 while (s
[0] == '0' && s
[1] == '0' && s
[2] == '0' && s
[3] == '0'
1234 skip_addr_chars
+= 4;
1239 info
->insn_info_valid
= 0;
1242 addr_offset
= start_offset
;
1243 while (addr_offset
< stop_offset
)
1247 bfd_boolean need_nl
= FALSE
;
1249 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1251 for (z
= addr_offset
* opb
; z
< stop_offset
* opb
; z
++)
1254 if (! disassemble_zeroes
1255 && (info
->insn_info_valid
== 0
1256 || info
->branch_delay_insns
== 0)
1257 && (z
- addr_offset
* opb
>= SKIP_ZEROES
1258 || (z
== stop_offset
* opb
&&
1259 z
- addr_offset
* opb
< SKIP_ZEROES_AT_END
)))
1263 /* If there are more nonzero octets to follow, we only skip
1264 zeroes in multiples of 4, to try to avoid running over
1265 the start of an instruction which happens to start with
1267 if (z
!= stop_offset
* opb
)
1268 z
= addr_offset
* opb
+ ((z
- addr_offset
* opb
) &~ 3);
1270 octets
= z
- addr_offset
* opb
;
1281 if (with_line_numbers
|| with_source_code
)
1282 /* The line number tables will refer to unadjusted
1283 section VMAs, so we must undo any VMA modifications
1284 when calling show_line. */
1285 show_line (aux
->abfd
, section
, addr_offset
- adjust_section_vma
);
1287 if (! prefix_addresses
)
1291 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ addr_offset
);
1292 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
1296 printf ("%s:\t", buf
+ skip_addr_chars
);
1300 aux
->require_sec
= TRUE
;
1301 objdump_print_address (section
->vma
+ addr_offset
, info
);
1302 aux
->require_sec
= FALSE
;
1309 sfile
.buffer
= xmalloc (sfile
.size
);
1310 sfile
.current
= sfile
.buffer
;
1311 info
->fprintf_func
= (fprintf_ftype
) objdump_sprintf
;
1312 info
->stream
= (FILE *) &sfile
;
1313 info
->bytes_per_line
= 0;
1314 info
->bytes_per_chunk
= 0;
1316 #ifdef DISASSEMBLER_NEEDS_RELOCS
1317 /* FIXME: This is wrong. It tests the number of octets
1318 in the last instruction, not the current one. */
1319 if (*relppp
< relppend
1320 && (**relppp
)->address
>= addr_offset
1321 && (**relppp
)->address
<= addr_offset
+ octets
/ opb
)
1322 info
->flags
= INSN_HAS_RELOC
;
1327 octets
= (*disassemble_fn
) (section
->vma
+ addr_offset
, info
);
1328 info
->fprintf_func
= (fprintf_ftype
) fprintf
;
1329 info
->stream
= stdout
;
1330 if (info
->bytes_per_line
!= 0)
1331 octets_per_line
= info
->bytes_per_line
;
1334 if (sfile
.current
!= sfile
.buffer
)
1335 printf ("%s\n", sfile
.buffer
);
1336 free (sfile
.buffer
);
1344 octets
= octets_per_line
;
1345 if (addr_offset
+ octets
/ opb
> stop_offset
)
1346 octets
= (stop_offset
- addr_offset
) * opb
;
1348 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ octets
; ++j
)
1350 if (ISPRINT (data
[j
]))
1351 buf
[j
- addr_offset
* opb
] = data
[j
];
1353 buf
[j
- addr_offset
* opb
] = '.';
1355 buf
[j
- addr_offset
* opb
] = '\0';
1358 if (prefix_addresses
1360 : show_raw_insn
>= 0)
1364 /* If ! prefix_addresses and ! wide_output, we print
1365 octets_per_line octets per line. */
1367 if (pb
> octets_per_line
&& ! prefix_addresses
&& ! wide_output
)
1368 pb
= octets_per_line
;
1370 if (info
->bytes_per_chunk
)
1371 bpc
= info
->bytes_per_chunk
;
1375 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
1378 if (bpc
> 1 && info
->display_endian
== BFD_ENDIAN_LITTLE
)
1380 for (k
= bpc
- 1; k
>= 0; k
--)
1381 printf ("%02x", (unsigned) data
[j
+ k
]);
1386 for (k
= 0; k
< bpc
; k
++)
1387 printf ("%02x", (unsigned) data
[j
+ k
]);
1392 for (; pb
< octets_per_line
; pb
+= bpc
)
1396 for (k
= 0; k
< bpc
; k
++)
1401 /* Separate raw data from instruction by extra space. */
1412 printf ("%s", sfile
.buffer
);
1413 free (sfile
.buffer
);
1416 if (prefix_addresses
1418 : show_raw_insn
>= 0)
1426 j
= addr_offset
* opb
+ pb
;
1428 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ j
/ opb
);
1429 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
1433 printf ("%s:\t", buf
+ skip_addr_chars
);
1435 pb
+= octets_per_line
;
1438 for (; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
1442 if (bpc
> 1 && info
->display_endian
== BFD_ENDIAN_LITTLE
)
1444 for (k
= bpc
- 1; k
>= 0; k
--)
1445 printf ("%02x", (unsigned) data
[j
+ k
]);
1450 for (k
= 0; k
< bpc
; k
++)
1451 printf ("%02x", (unsigned) data
[j
+ k
]);
1464 if ((section
->flags
& SEC_RELOC
) != 0
1465 #ifndef DISASSEMBLER_NEEDS_RELOCS
1470 while ((*relppp
) < relppend
1471 && ((**relppp
)->address
>= (bfd_vma
) addr_offset
1472 && (**relppp
)->address
< (bfd_vma
) addr_offset
+ octets
/ opb
))
1473 #ifdef DISASSEMBLER_NEEDS_RELOCS
1474 if (! dump_reloc_info
)
1488 objdump_print_value (section
->vma
+ q
->address
, info
, TRUE
);
1490 printf (": %s\t", q
->howto
->name
);
1492 if (q
->sym_ptr_ptr
== NULL
|| *q
->sym_ptr_ptr
== NULL
)
1493 printf ("*unknown*");
1496 const char *sym_name
;
1498 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
1499 if (sym_name
!= NULL
&& *sym_name
!= '\0')
1500 objdump_print_symname (aux
->abfd
, info
, *q
->sym_ptr_ptr
);
1505 sym_sec
= bfd_get_section (*q
->sym_ptr_ptr
);
1506 sym_name
= bfd_get_section_name (aux
->abfd
, sym_sec
);
1507 if (sym_name
== NULL
|| *sym_name
== '\0')
1508 sym_name
= "*unknown*";
1509 printf ("%s", sym_name
);
1516 objdump_print_value (q
->addend
, info
, TRUE
);
1528 addr_offset
+= octets
/ opb
;
1532 /* Disassemble the contents of an object file. */
1535 disassemble_data (abfd
)
1538 unsigned long addr_offset
;
1539 disassembler_ftype disassemble_fn
;
1540 struct disassemble_info disasm_info
;
1541 struct objdump_disasm_info aux
;
1546 prev_functionname
= NULL
;
1549 /* We make a copy of syms to sort. We don't want to sort syms
1550 because that will screw up the relocs. */
1551 sorted_syms
= (asymbol
**) xmalloc (symcount
* sizeof (asymbol
*));
1552 memcpy (sorted_syms
, syms
, symcount
* sizeof (asymbol
*));
1554 sorted_symcount
= remove_useless_symbols (sorted_syms
, symcount
);
1556 /* Sort the symbols into section and symbol order. */
1557 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
1559 INIT_DISASSEMBLE_INFO (disasm_info
, stdout
, fprintf
);
1561 disasm_info
.application_data
= (PTR
) &aux
;
1563 aux
.require_sec
= FALSE
;
1564 disasm_info
.print_address_func
= objdump_print_address
;
1565 disasm_info
.symbol_at_address_func
= objdump_symbol_at_address
;
1567 if (machine
!= (char *) NULL
)
1569 const bfd_arch_info_type
*info
= bfd_scan_arch (machine
);
1572 fatal (_("Can't use supplied machine %s"), machine
);
1574 abfd
->arch_info
= info
;
1577 if (endian
!= BFD_ENDIAN_UNKNOWN
)
1579 struct bfd_target
*xvec
;
1581 xvec
= (struct bfd_target
*) xmalloc (sizeof (struct bfd_target
));
1582 memcpy (xvec
, abfd
->xvec
, sizeof (struct bfd_target
));
1583 xvec
->byteorder
= endian
;
1587 disassemble_fn
= disassembler (abfd
);
1588 if (!disassemble_fn
)
1590 non_fatal (_("Can't disassemble for architecture %s\n"),
1591 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
1596 opb
= bfd_octets_per_byte (abfd
);
1598 disasm_info
.flavour
= bfd_get_flavour (abfd
);
1599 disasm_info
.arch
= bfd_get_arch (abfd
);
1600 disasm_info
.mach
= bfd_get_mach (abfd
);
1601 disasm_info
.disassembler_options
= disassembler_options
;
1602 disasm_info
.octets_per_byte
= opb
;
1604 if (bfd_big_endian (abfd
))
1605 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_BIG
;
1606 else if (bfd_little_endian (abfd
))
1607 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
1609 /* ??? Aborting here seems too drastic. We could default to big or little
1611 disasm_info
.endian
= BFD_ENDIAN_UNKNOWN
;
1613 for (section
= abfd
->sections
;
1614 section
!= (asection
*) NULL
;
1615 section
= section
->next
)
1617 bfd_byte
*data
= NULL
;
1618 bfd_size_type datasize
= 0;
1619 arelent
**relbuf
= NULL
;
1620 arelent
**relpp
= NULL
;
1621 arelent
**relppend
= NULL
;
1622 unsigned long stop_offset
;
1623 asymbol
*sym
= NULL
;
1626 if ((section
->flags
& SEC_LOAD
) == 0
1627 || (! disassemble_all
1629 && (section
->flags
& SEC_CODE
) == 0))
1631 if (only
!= (char *) NULL
&& strcmp (only
, section
->name
) != 0)
1634 if ((section
->flags
& SEC_RELOC
) != 0
1635 #ifndef DISASSEMBLER_NEEDS_RELOCS
1642 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
1644 bfd_fatal (bfd_get_filename (abfd
));
1650 relbuf
= (arelent
**) xmalloc (relsize
);
1651 relcount
= bfd_canonicalize_reloc (abfd
, section
, relbuf
, syms
);
1653 bfd_fatal (bfd_get_filename (abfd
));
1655 /* Sort the relocs by address. */
1656 qsort (relbuf
, relcount
, sizeof (arelent
*), compare_relocs
);
1659 relppend
= relpp
+ relcount
;
1661 /* Skip over the relocs belonging to addresses below the
1663 if (start_address
!= (bfd_vma
) -1)
1664 while (relpp
< relppend
1665 && (*relpp
)->address
< start_address
)
1670 printf (_("Disassembly of section %s:\n"), section
->name
);
1672 datasize
= bfd_get_section_size_before_reloc (section
);
1676 data
= (bfd_byte
*) xmalloc ((size_t) datasize
);
1678 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
1681 disasm_info
.buffer
= data
;
1682 disasm_info
.buffer_vma
= section
->vma
;
1683 disasm_info
.buffer_length
= datasize
;
1684 disasm_info
.section
= section
;
1686 if (start_address
== (bfd_vma
) -1
1687 || start_address
< disasm_info
.buffer_vma
)
1690 addr_offset
= start_address
- disasm_info
.buffer_vma
;
1692 if (stop_address
== (bfd_vma
) -1)
1693 stop_offset
= datasize
/ opb
;
1696 if (stop_address
< disasm_info
.buffer_vma
)
1699 stop_offset
= stop_address
- disasm_info
.buffer_vma
;
1700 if (stop_offset
> disasm_info
.buffer_length
/ opb
)
1701 stop_offset
= disasm_info
.buffer_length
/ opb
;
1704 sym
= find_symbol_for_address (abfd
, section
, section
->vma
+ addr_offset
,
1707 while (addr_offset
< stop_offset
)
1710 unsigned long nextstop_offset
;
1713 if (sym
!= NULL
&& bfd_asymbol_value (sym
) <= section
->vma
+ addr_offset
)
1718 (x
< sorted_symcount
1719 && bfd_asymbol_value (sorted_syms
[x
]) <= section
->vma
+ addr_offset
);
1723 disasm_info
.symbols
= & sorted_syms
[place
];
1724 disasm_info
.num_symbols
= x
- place
;
1727 disasm_info
.symbols
= NULL
;
1729 if (! prefix_addresses
)
1731 (* disasm_info
.fprintf_func
) (disasm_info
.stream
, "\n");
1732 objdump_print_addr_with_sym (abfd
, section
, sym
,
1733 section
->vma
+ addr_offset
,
1736 (* disasm_info
.fprintf_func
) (disasm_info
.stream
, ":\n");
1739 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > section
->vma
+ addr_offset
)
1741 else if (sym
== NULL
)
1745 /* Search forward for the next appropriate symbol in
1746 SECTION. Note that all the symbols are sorted
1747 together into one big array, and that some sections
1748 may have overlapping addresses. */
1749 while (place
< sorted_symcount
1750 && (sorted_syms
[place
]->section
!= section
1751 || (bfd_asymbol_value (sorted_syms
[place
])
1752 <= bfd_asymbol_value (sym
))))
1754 if (place
>= sorted_symcount
)
1757 nextsym
= sorted_syms
[place
];
1760 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > section
->vma
+ addr_offset
)
1762 nextstop_offset
= bfd_asymbol_value (sym
) - section
->vma
;
1763 if (nextstop_offset
> stop_offset
)
1764 nextstop_offset
= stop_offset
;
1766 else if (nextsym
== NULL
)
1767 nextstop_offset
= stop_offset
;
1770 nextstop_offset
= bfd_asymbol_value (nextsym
) - section
->vma
;
1771 if (nextstop_offset
> stop_offset
)
1772 nextstop_offset
= stop_offset
;
1775 /* If a symbol is explicitly marked as being an object
1776 rather than a function, just dump the bytes without
1777 disassembling them. */
1780 || bfd_asymbol_value (sym
) > section
->vma
+ addr_offset
1781 || ((sym
->flags
& BSF_OBJECT
) == 0
1782 && (strstr (bfd_asymbol_name (sym
), "gnu_compiled")
1784 && (strstr (bfd_asymbol_name (sym
), "gcc2_compiled")
1786 || (sym
->flags
& BSF_FUNCTION
) != 0)
1791 disassemble_bytes (&disasm_info
, disassemble_fn
, insns
, data
,
1792 addr_offset
, nextstop_offset
, &relpp
, relppend
);
1794 addr_offset
= nextstop_offset
;
1806 /* Dump the stabs sections from an object file that has a section that
1807 uses Sun stabs encoding. */
1813 dump_section_stabs (abfd
, ".stab", ".stabstr");
1814 dump_section_stabs (abfd
, ".stab.excl", ".stab.exclstr");
1815 dump_section_stabs (abfd
, ".stab.index", ".stab.indexstr");
1816 dump_section_stabs (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1819 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1820 and string table section STRSECT_NAME into `strtab'.
1821 If the section exists and was read, allocate the space and return TRUE.
1822 Otherwise return FALSE. */
1825 read_section_stabs (abfd
, stabsect_name
, strsect_name
)
1827 const char *stabsect_name
;
1828 const char *strsect_name
;
1830 asection
*stabsect
, *stabstrsect
;
1832 stabsect
= bfd_get_section_by_name (abfd
, stabsect_name
);
1835 printf (_("No %s section present\n\n"), stabsect_name
);
1839 stabstrsect
= bfd_get_section_by_name (abfd
, strsect_name
);
1840 if (0 == stabstrsect
)
1842 non_fatal (_("%s has no %s section"),
1843 bfd_get_filename (abfd
), strsect_name
);
1848 stab_size
= bfd_section_size (abfd
, stabsect
);
1849 stabstr_size
= bfd_section_size (abfd
, stabstrsect
);
1851 stabs
= (bfd_byte
*) xmalloc (stab_size
);
1852 strtab
= (char *) xmalloc (stabstr_size
);
1854 if (! bfd_get_section_contents (abfd
, stabsect
, (PTR
) stabs
, 0, stab_size
))
1856 non_fatal (_("Reading %s section of %s failed: %s"),
1857 stabsect_name
, bfd_get_filename (abfd
),
1858 bfd_errmsg (bfd_get_error ()));
1865 if (! bfd_get_section_contents (abfd
, stabstrsect
, (PTR
) strtab
, 0,
1868 non_fatal (_("Reading %s section of %s failed: %s\n"),
1869 strsect_name
, bfd_get_filename (abfd
),
1870 bfd_errmsg (bfd_get_error ()));
1880 /* Stabs entries use a 12 byte format:
1881 4 byte string table index
1883 1 byte stab other field
1884 2 byte stab desc field
1886 FIXME: This will have to change for a 64 bit object format. */
1888 #define STRDXOFF (0)
1890 #define OTHEROFF (5)
1893 #define STABSIZE (12)
1895 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1896 using string table section STRSECT_NAME (in `strtab'). */
1899 print_section_stabs (abfd
, stabsect_name
, strsect_name
)
1901 const char *stabsect_name
;
1902 const char *strsect_name ATTRIBUTE_UNUSED
;
1905 unsigned file_string_table_offset
= 0, next_file_string_table_offset
= 0;
1906 bfd_byte
*stabp
, *stabs_end
;
1909 stabs_end
= stabp
+ stab_size
;
1911 printf (_("Contents of %s section:\n\n"), stabsect_name
);
1912 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1914 /* Loop through all symbols and print them.
1916 We start the index at -1 because there is a dummy symbol on
1917 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1918 for (i
= -1; stabp
< stabs_end
; stabp
+= STABSIZE
, i
++)
1922 unsigned char type
, other
;
1923 unsigned short desc
;
1926 strx
= bfd_h_get_32 (abfd
, stabp
+ STRDXOFF
);
1927 type
= bfd_h_get_8 (abfd
, stabp
+ TYPEOFF
);
1928 other
= bfd_h_get_8 (abfd
, stabp
+ OTHEROFF
);
1929 desc
= bfd_h_get_16 (abfd
, stabp
+ DESCOFF
);
1930 value
= bfd_h_get_32 (abfd
, stabp
+ VALOFF
);
1932 printf ("\n%-6d ", i
);
1933 /* Either print the stab name, or, if unnamed, print its number
1934 again (makes consistent formatting for tools like awk). */
1935 name
= bfd_get_stab_name (type
);
1937 printf ("%-6s", name
);
1938 else if (type
== N_UNDF
)
1941 printf ("%-6d", type
);
1942 printf (" %-6d %-6d ", other
, desc
);
1943 bfd_printf_vma (abfd
, value
);
1944 printf (" %-6lu", strx
);
1946 /* Symbols with type == 0 (N_UNDF) specify the length of the
1947 string table associated with this file. We use that info
1948 to know how to relocate the *next* file's string table indices. */
1951 file_string_table_offset
= next_file_string_table_offset
;
1952 next_file_string_table_offset
+= value
;
1956 /* Using the (possibly updated) string table offset, print the
1957 string (if any) associated with this symbol. */
1958 if ((strx
+ file_string_table_offset
) < stabstr_size
)
1959 printf (" %s", &strtab
[strx
+ file_string_table_offset
]);
1968 dump_section_stabs (abfd
, stabsect_name
, strsect_name
)
1970 char *stabsect_name
;
1975 /* Check for section names for which stabsect_name is a prefix, to
1976 handle .stab0, etc. */
1977 for (s
= abfd
->sections
;
1983 len
= strlen (stabsect_name
);
1985 /* If the prefix matches, and the files section name ends with a
1986 nul or a digit, then we match. I.e., we want either an exact
1987 match or a section followed by a number. */
1988 if (strncmp (stabsect_name
, s
->name
, len
) == 0
1989 && (s
->name
[len
] == '\000'
1990 || ISDIGIT (s
->name
[len
])))
1992 if (read_section_stabs (abfd
, s
->name
, strsect_name
))
1994 print_section_stabs (abfd
, s
->name
, strsect_name
);
2004 dump_bfd_header (abfd
)
2009 printf (_("architecture: %s, "),
2010 bfd_printable_arch_mach (bfd_get_arch (abfd
),
2011 bfd_get_mach (abfd
)));
2012 printf (_("flags 0x%08x:\n"), abfd
->flags
);
2014 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2015 PF (HAS_RELOC
, "HAS_RELOC");
2016 PF (EXEC_P
, "EXEC_P");
2017 PF (HAS_LINENO
, "HAS_LINENO");
2018 PF (HAS_DEBUG
, "HAS_DEBUG");
2019 PF (HAS_SYMS
, "HAS_SYMS");
2020 PF (HAS_LOCALS
, "HAS_LOCALS");
2021 PF (DYNAMIC
, "DYNAMIC");
2022 PF (WP_TEXT
, "WP_TEXT");
2023 PF (D_PAGED
, "D_PAGED");
2024 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
2025 PF (HAS_LOAD_PAGE
, "HAS_LOAD_PAGE");
2026 printf (_("\nstart address 0x"));
2027 bfd_printf_vma (abfd
, abfd
->start_address
);
2033 dump_bfd_private_header (abfd
)
2036 bfd_print_private_bfd_data (abfd
, stdout
);
2039 /* Dump selected contents of ABFD. */
2045 /* If we are adjusting section VMA's, change them all now. Changing
2046 the BFD information is a hack. However, we must do it, or
2047 bfd_find_nearest_line will not do the right thing. */
2048 if (adjust_section_vma
!= 0)
2052 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2054 s
->vma
+= adjust_section_vma
;
2055 s
->lma
+= adjust_section_vma
;
2059 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd
),
2062 print_arelt_descr (stdout
, abfd
, TRUE
);
2063 if (dump_file_header
)
2064 dump_bfd_header (abfd
);
2065 if (dump_private_headers
)
2066 dump_bfd_private_header (abfd
);
2068 if (dump_section_headers
)
2069 dump_headers (abfd
);
2071 if (dump_symtab
|| dump_reloc_info
|| disassemble
|| dump_debugging
)
2072 syms
= slurp_symtab (abfd
);
2073 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
)
2074 dynsyms
= slurp_dynamic_symtab (abfd
);
2077 dump_symbols (abfd
, FALSE
);
2078 if (dump_dynamic_symtab
)
2079 dump_symbols (abfd
, TRUE
);
2080 if (dump_stab_section_info
)
2082 if (dump_reloc_info
&& ! disassemble
)
2084 if (dump_dynamic_reloc_info
)
2085 dump_dynamic_relocs (abfd
);
2086 if (dump_section_contents
)
2089 disassemble_data (abfd
);
2094 dhandle
= read_debugging_info (abfd
, syms
, symcount
);
2095 if (dhandle
!= NULL
)
2097 if (! print_debugging_info (stdout
, dhandle
))
2099 non_fatal (_("%s: printing debugging information failed"),
2100 bfd_get_filename (abfd
));
2125 if (bfd_check_format_matches (abfd
, bfd_object
, &matching
))
2131 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
2133 nonfatal (bfd_get_filename (abfd
));
2134 list_matching_formats (matching
);
2139 if (bfd_get_error () != bfd_error_file_not_recognized
)
2141 nonfatal (bfd_get_filename (abfd
));
2145 if (bfd_check_format_matches (abfd
, bfd_core
, &matching
))
2151 nonfatal (bfd_get_filename (abfd
));
2153 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
2155 list_matching_formats (matching
);
2161 display_file (filename
, target
)
2165 bfd
*file
, *arfile
= (bfd
*) NULL
;
2167 file
= bfd_openr (filename
, target
);
2170 nonfatal (filename
);
2174 if (bfd_check_format (file
, bfd_archive
))
2176 bfd
*last_arfile
= NULL
;
2178 printf (_("In archive %s:\n"), bfd_get_filename (file
));
2181 bfd_set_error (bfd_error_no_error
);
2183 arfile
= bfd_openr_next_archived_file (file
, arfile
);
2186 if (bfd_get_error () != bfd_error_no_more_archived_files
)
2187 nonfatal (bfd_get_filename (file
));
2191 display_bfd (arfile
);
2193 if (last_arfile
!= NULL
)
2194 bfd_close (last_arfile
);
2195 last_arfile
= arfile
;
2198 if (last_arfile
!= NULL
)
2199 bfd_close (last_arfile
);
2207 /* Actually display the various requested regions. */
2215 bfd_size_type datasize
= 0;
2216 bfd_size_type addr_offset
;
2217 bfd_size_type start_offset
, stop_offset
;
2218 unsigned int opb
= bfd_octets_per_byte (abfd
);
2220 for (section
= abfd
->sections
; section
!= NULL
; section
=
2225 if (only
== (char *) NULL
||
2226 strcmp (only
, section
->name
) == 0)
2228 if (section
->flags
& SEC_HAS_CONTENTS
)
2233 printf (_("Contents of section %s:\n"), section
->name
);
2235 if (bfd_section_size (abfd
, section
) == 0)
2237 data
= (bfd_byte
*) xmalloc ((size_t) bfd_section_size (abfd
, section
));
2238 datasize
= bfd_section_size (abfd
, section
);
2241 bfd_get_section_contents (abfd
, section
, (PTR
) data
, 0, bfd_section_size (abfd
, section
));
2243 if (start_address
== (bfd_vma
) -1
2244 || start_address
< section
->vma
)
2247 start_offset
= start_address
- section
->vma
;
2248 if (stop_address
== (bfd_vma
) -1)
2249 stop_offset
= bfd_section_size (abfd
, section
) / opb
;
2252 if (stop_address
< section
->vma
)
2255 stop_offset
= stop_address
- section
->vma
;
2256 if (stop_offset
> bfd_section_size (abfd
, section
) / opb
)
2257 stop_offset
= bfd_section_size (abfd
, section
) / opb
;
2262 bfd_sprintf_vma (abfd
, buf
, start_offset
+ section
->vma
);
2263 if (strlen (buf
) >= sizeof (buf
))
2266 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
2268 count
= strlen (buf
) - count
;
2272 bfd_sprintf_vma (abfd
, buf
, stop_offset
+ section
->vma
- 1);
2273 if (strlen (buf
) >= sizeof (buf
))
2276 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
2278 count
= strlen (buf
) - count
;
2282 for (addr_offset
= start_offset
;
2283 addr_offset
< stop_offset
; addr_offset
+= onaline
/ opb
)
2287 bfd_sprintf_vma (abfd
, buf
, (addr_offset
+ section
->vma
));
2288 count
= strlen (buf
);
2289 if (count
>= sizeof (buf
))
2292 while (count
< width
)
2297 fputs (buf
+ count
- width
, stdout
);
2300 for (j
= addr_offset
* opb
;
2301 j
< addr_offset
* opb
+ onaline
; j
++)
2303 if (j
< stop_offset
* opb
)
2304 printf ("%02x", (unsigned) (data
[j
]));
2312 for (j
= addr_offset
* opb
;
2313 j
< addr_offset
* opb
+ onaline
; j
++)
2315 if (j
>= stop_offset
* opb
)
2318 printf ("%c", ISPRINT (data
[j
]) ? data
[j
] : '.');
2328 /* Should perhaps share code and display with nm? */
2331 dump_symbols (abfd
, dynamic
)
2332 bfd
*abfd ATTRIBUTE_UNUSED
;
2333 bfd_boolean dynamic
;
2343 printf ("DYNAMIC SYMBOL TABLE:\n");
2349 printf ("SYMBOL TABLE:\n");
2353 printf (_("no symbols\n"));
2355 for (count
= 0; count
< max
; count
++)
2359 bfd
*cur_bfd
= bfd_asymbol_bfd (*current
);
2361 if (cur_bfd
!= NULL
)
2366 name
= (*current
)->name
;
2368 if (do_demangle
&& name
!= NULL
&& *name
!= '\0')
2370 /* If we want to demangle the name, we demangle it
2371 here, and temporarily clobber it while calling
2372 bfd_print_symbol. FIXME: This is a gross hack. */
2373 alloc
= demangle (cur_bfd
, name
);
2374 (*current
)->name
= alloc
;
2377 bfd_print_symbol (cur_bfd
, stdout
, *current
,
2378 bfd_print_symbol_all
);
2380 (*current
)->name
= name
;
2401 for (a
= abfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
2405 if (bfd_is_abs_section (a
))
2407 if (bfd_is_und_section (a
))
2409 if (bfd_is_com_section (a
))
2414 if (strcmp (only
, a
->name
))
2417 else if ((a
->flags
& SEC_RELOC
) == 0)
2420 relsize
= bfd_get_reloc_upper_bound (abfd
, a
);
2422 bfd_fatal (bfd_get_filename (abfd
));
2424 printf ("RELOCATION RECORDS FOR [%s]:", a
->name
);
2428 printf (" (none)\n\n");
2432 relpp
= (arelent
**) xmalloc (relsize
);
2433 relcount
= bfd_canonicalize_reloc (abfd
, a
, relpp
, syms
);
2436 bfd_fatal (bfd_get_filename (abfd
));
2437 else if (relcount
== 0)
2438 printf (" (none)\n\n");
2442 dump_reloc_set (abfd
, a
, relpp
, relcount
);
2451 dump_dynamic_relocs (abfd
)
2458 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
2460 bfd_fatal (bfd_get_filename (abfd
));
2462 printf ("DYNAMIC RELOCATION RECORDS");
2465 printf (" (none)\n\n");
2468 relpp
= (arelent
**) xmalloc (relsize
);
2469 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
2472 bfd_fatal (bfd_get_filename (abfd
));
2473 else if (relcount
== 0)
2474 printf (" (none)\n\n");
2478 dump_reloc_set (abfd
, (asection
*) NULL
, relpp
, relcount
);
2486 dump_reloc_set (abfd
, sec
, relpp
, relcount
)
2493 char *last_filename
, *last_functionname
;
2494 unsigned int last_line
;
2496 /* Get column headers lined up reasonably. */
2503 bfd_sprintf_vma (abfd
, buf
, (bfd_vma
) -1);
2504 width
= strlen (buf
) - 7;
2506 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
2509 last_filename
= NULL
;
2510 last_functionname
= NULL
;
2513 for (p
= relpp
; relcount
&& *p
!= (arelent
*) NULL
; p
++, relcount
--)
2516 const char *filename
, *functionname
;
2518 const char *sym_name
;
2519 const char *section_name
;
2521 if (start_address
!= (bfd_vma
) -1
2522 && q
->address
< start_address
)
2524 if (stop_address
!= (bfd_vma
) -1
2525 && q
->address
> stop_address
)
2528 if (with_line_numbers
2530 && bfd_find_nearest_line (abfd
, sec
, syms
, q
->address
,
2531 &filename
, &functionname
, &line
))
2533 if (functionname
!= NULL
2534 && (last_functionname
== NULL
2535 || strcmp (functionname
, last_functionname
) != 0))
2537 printf ("%s():\n", functionname
);
2538 if (last_functionname
!= NULL
)
2539 free (last_functionname
);
2540 last_functionname
= xstrdup (functionname
);
2544 && (line
!= last_line
2545 || (filename
!= NULL
2546 && last_filename
!= NULL
2547 && strcmp (filename
, last_filename
) != 0)))
2549 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
2551 if (last_filename
!= NULL
)
2552 free (last_filename
);
2553 if (filename
== NULL
)
2554 last_filename
= NULL
;
2556 last_filename
= xstrdup (filename
);
2560 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
2562 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
2563 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
2568 section_name
= NULL
;
2573 bfd_printf_vma (abfd
, q
->address
);
2575 printf (" %-16s ", q
->howto
->name
);
2577 printf (" %-16d ", q
->howto
->type
);
2578 objdump_print_symname (abfd
, (struct disassemble_info
*) NULL
,
2583 if (section_name
== (const char *) NULL
)
2584 section_name
= "*unknown*";
2585 bfd_printf_vma (abfd
, q
->address
);
2586 printf (" %-16s [%s]",
2594 bfd_printf_vma (abfd
, q
->addend
);
2601 int main
PARAMS ((int, char **));
2609 char *target
= default_target
;
2610 bfd_boolean seenflag
= FALSE
;
2612 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2613 setlocale (LC_MESSAGES
, "");
2615 #if defined (HAVE_SETLOCALE)
2616 setlocale (LC_CTYPE
, "");
2618 bindtextdomain (PACKAGE
, LOCALEDIR
);
2619 textdomain (PACKAGE
);
2621 program_name
= *argv
;
2622 xmalloc_set_program_name (program_name
);
2624 START_PROGRESS (program_name
, 0);
2627 set_default_bfd_target ();
2629 while ((c
= getopt_long (argc
, argv
, "pib:m:M:VvCdDlfaHhrRtTxsSj:wE:zgG",
2630 long_options
, (int *) 0))
2636 break; /* We've been given a long option. */
2641 disassembler_options
= optarg
;
2647 with_line_numbers
= TRUE
;
2656 enum demangling_styles style
;
2658 style
= cplus_demangle_name_to_style (optarg
);
2659 if (style
== unknown_demangling
)
2660 fatal (_("unknown demangling style `%s'"),
2663 cplus_demangle_set_style (style
);
2669 case OPTION_ADJUST_VMA
:
2670 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
2672 case OPTION_START_ADDRESS
:
2673 start_address
= parse_vma (optarg
, "--start-address");
2675 case OPTION_STOP_ADDRESS
:
2676 stop_address
= parse_vma (optarg
, "--stop-address");
2679 if (strcmp (optarg
, "B") == 0)
2680 endian
= BFD_ENDIAN_BIG
;
2681 else if (strcmp (optarg
, "L") == 0)
2682 endian
= BFD_ENDIAN_LITTLE
;
2685 non_fatal (_("unrecognized -E option"));
2690 if (strncmp (optarg
, "big", strlen (optarg
)) == 0)
2691 endian
= BFD_ENDIAN_BIG
;
2692 else if (strncmp (optarg
, "little", strlen (optarg
)) == 0)
2693 endian
= BFD_ENDIAN_LITTLE
;
2696 non_fatal (_("unrecognized --endian type `%s'"), optarg
);
2702 dump_file_header
= TRUE
;
2706 formats_info
= TRUE
;
2710 dump_private_headers
= TRUE
;
2714 dump_private_headers
= TRUE
;
2716 dump_reloc_info
= TRUE
;
2717 dump_file_header
= TRUE
;
2718 dump_ar_hdrs
= TRUE
;
2719 dump_section_headers
= TRUE
;
2727 dump_dynamic_symtab
= TRUE
;
2735 disassemble_zeroes
= TRUE
;
2739 disassemble_all
= TRUE
;
2744 with_source_code
= TRUE
;
2752 dump_stab_section_info
= TRUE
;
2756 dump_section_contents
= TRUE
;
2760 dump_reloc_info
= TRUE
;
2764 dump_dynamic_reloc_info
= TRUE
;
2768 dump_ar_hdrs
= TRUE
;
2772 dump_section_headers
= TRUE
;
2780 show_version
= TRUE
;
2790 print_version ("objdump");
2796 exit_status
= display_info ();
2800 display_file ("a.out", target
);
2802 for (; optind
< argc
;)
2803 display_file (argv
[optind
++], target
);
2806 END_PROGRESS (program_name
);