1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 3, 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, 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
26 Objdump displays information about one or more object files, either on
27 their own, or inside libraries. It is commonly used as a disassembler,
28 but it can also display information about file headers, symbol tables,
29 relocations, debugging directives and more.
31 The flow of execution is as follows:
33 1. Command line arguments are checked for control switches and the
34 information to be displayed is selected.
36 2. Any remaining arguments are assumed to be object files, and they are
37 processed in order by display_bfd(). If the file is an archive each
38 of its elements is processed in turn.
40 3. The file's target architecture and binary file format are determined
41 by bfd_check_format(). If they are recognised, then dump_bfd() is
44 4. dump_bfd() in turn calls separate functions to display the requested
45 item(s) of information(s). For example disassemble_data() is called if
46 a disassembly has been requested.
48 When disassembling the code loops through blocks of instructions bounded
49 by symbols, calling disassemble_bytes() on each block. The actual
50 disassembling is done by the libopcodes library, via a function pointer
51 supplied by the disassembler() function. */
60 #include "safe-ctype.h"
62 #include "libiberty.h"
64 #include "filenames.h"
74 /* Internal headers for the ELF .stab-dump code - sorry. */
75 #define BYTES_IN_WORD 32
76 #include "aout/aout64.h"
79 static int exit_status
= 0;
81 static char *default_target
= NULL
; /* Default at runtime. */
83 /* The following variables are set based on arguments passed on the
85 static int show_version
= 0; /* Show the version number. */
86 static int dump_section_contents
; /* -s */
87 static int dump_section_headers
; /* -h */
88 static bfd_boolean dump_file_header
; /* -f */
89 static int dump_symtab
; /* -t */
90 static int dump_dynamic_symtab
; /* -T */
91 static int dump_reloc_info
; /* -r */
92 static int dump_dynamic_reloc_info
; /* -R */
93 static int dump_ar_hdrs
; /* -a */
94 static int dump_private_headers
; /* -p */
95 static int prefix_addresses
; /* --prefix-addresses */
96 static int with_line_numbers
; /* -l */
97 static bfd_boolean with_source_code
; /* -S */
98 static int show_raw_insn
; /* --show-raw-insn */
99 static int dump_dwarf_section_info
; /* --dwarf */
100 static int dump_stab_section_info
; /* --stabs */
101 static int do_demangle
; /* -C, --demangle */
102 static bfd_boolean disassemble
; /* -d */
103 static bfd_boolean disassemble_all
; /* -D */
104 static int disassemble_zeroes
; /* --disassemble-zeroes */
105 static bfd_boolean formats_info
; /* -i */
106 static int wide_output
; /* -w */
107 static bfd_vma start_address
= (bfd_vma
) -1; /* --start-address */
108 static bfd_vma stop_address
= (bfd_vma
) -1; /* --stop-address */
109 static int dump_debugging
; /* --debugging */
110 static int dump_debugging_tags
; /* --debugging-tags */
111 static int dump_special_syms
= 0; /* --special-syms */
112 static bfd_vma adjust_section_vma
= 0; /* --adjust-vma */
113 static int file_start_context
= 0; /* --file-start-context */
114 static bfd_boolean display_file_offsets
;/* -F */
115 static const char *prefix
; /* --prefix */
116 static int prefix_strip
; /* --prefix-strip */
117 static size_t prefix_length
;
119 /* Pointer to an array of section names provided by
120 one or more "-j secname" command line options. */
122 /* The total number of slots in the only[] array. */
123 static size_t only_size
= 0;
124 /* The number of occupied slots in the only[] array. */
125 static size_t only_used
= 0;
127 /* Variables for handling include file path table. */
128 static const char **include_paths
;
129 static int include_path_count
;
131 /* Extra info to pass to the section disassembler and address printing
133 struct objdump_disasm_info
137 bfd_boolean require_sec
;
138 arelent
** dynrelbuf
;
140 disassembler_ftype disassemble_fn
;
144 /* Architecture to disassemble for, or default if NULL. */
145 static char *machine
= NULL
;
147 /* Target specific options to the disassembler. */
148 static char *disassembler_options
= NULL
;
150 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
151 static enum bfd_endian endian
= BFD_ENDIAN_UNKNOWN
;
153 /* The symbol table. */
154 static asymbol
**syms
;
156 /* Number of symbols in `syms'. */
157 static long symcount
= 0;
159 /* The sorted symbol table. */
160 static asymbol
**sorted_syms
;
162 /* Number of symbols in `sorted_syms'. */
163 static long sorted_symcount
= 0;
165 /* The dynamic symbol table. */
166 static asymbol
**dynsyms
;
168 /* The synthetic symbol table. */
169 static asymbol
*synthsyms
;
170 static long synthcount
= 0;
172 /* Number of symbols in `dynsyms'. */
173 static long dynsymcount
= 0;
175 static bfd_byte
*stabs
;
176 static bfd_size_type stab_size
;
179 static bfd_size_type stabstr_size
;
181 static bfd_boolean is_relocatable
= FALSE
;
184 usage (FILE *stream
, int status
)
186 fprintf (stream
, _("Usage: %s <option(s)> <file(s)>\n"), program_name
);
187 fprintf (stream
, _(" Display information from object <file(s)>.\n"));
188 fprintf (stream
, _(" At least one of the following switches must be given:\n"));
189 fprintf (stream
, _("\
190 -a, --archive-headers Display archive header information\n\
191 -f, --file-headers Display the contents of the overall file header\n\
192 -p, --private-headers Display object format specific file header contents\n\
193 -h, --[section-]headers Display the contents of the section headers\n\
194 -x, --all-headers Display the contents of all headers\n\
195 -d, --disassemble Display assembler contents of executable sections\n\
196 -D, --disassemble-all Display assembler contents of all sections\n\
197 -S, --source Intermix source code with disassembly\n\
198 -s, --full-contents Display the full contents of all sections requested\n\
199 -g, --debugging Display debug information in object file\n\
200 -e, --debugging-tags Display debug information using ctags style\n\
201 -G, --stabs Display (in raw form) any STABS info in the file\n\
202 -W, --dwarf Display DWARF info in the file\n\
203 -t, --syms Display the contents of the symbol table(s)\n\
204 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
205 -r, --reloc Display the relocation entries in the file\n\
206 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
207 @<file> Read options from <file>\n\
208 -v, --version Display this program's version number\n\
209 -i, --info List object formats and architectures supported\n\
210 -H, --help Display this information\n\
214 fprintf (stream
, _("\n The following switches are optional:\n"));
215 fprintf (stream
, _("\
216 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
217 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
218 -j, --section=NAME Only display information for section NAME\n\
219 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
220 -EB --endian=big Assume big endian format when disassembling\n\
221 -EL --endian=little Assume little endian format when disassembling\n\
222 --file-start-context Include context from start of file (with -S)\n\
223 -I, --include=DIR Add DIR to search list for source files\n\
224 -l, --line-numbers Include line numbers and filenames in output\n\
225 -F, --file-offsets Include file offsets when displaying information\n\
226 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
227 The STYLE, if specified, can be `auto', `gnu',\n\
228 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
230 -w, --wide Format output for more than 80 columns\n\
231 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
232 --start-address=ADDR Only process data whose address is >= ADDR\n\
233 --stop-address=ADDR Only process data whose address is <= ADDR\n\
234 --prefix-addresses Print complete address alongside disassembly\n\
235 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
236 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
237 --special-syms Include special symbols in symbol dumps\n\
238 --prefix=PREFIX Add PREFIX to absolute paths for -S\n\
239 --prefix-strip=LEVEL Strip initial directory names for -S\n\
241 list_supported_targets (program_name
, stream
);
242 list_supported_architectures (program_name
, stream
);
244 disassembler_usage (stream
);
246 if (REPORT_BUGS_TO
[0] && status
== 0)
247 fprintf (stream
, _("Report bugs to %s.\n"), REPORT_BUGS_TO
);
251 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
255 OPTION_START_ADDRESS
,
262 static struct option long_options
[]=
264 {"adjust-vma", required_argument
, NULL
, OPTION_ADJUST_VMA
},
265 {"all-headers", no_argument
, NULL
, 'x'},
266 {"private-headers", no_argument
, NULL
, 'p'},
267 {"architecture", required_argument
, NULL
, 'm'},
268 {"archive-headers", no_argument
, NULL
, 'a'},
269 {"debugging", no_argument
, NULL
, 'g'},
270 {"debugging-tags", no_argument
, NULL
, 'e'},
271 {"demangle", optional_argument
, NULL
, 'C'},
272 {"disassemble", no_argument
, NULL
, 'd'},
273 {"disassemble-all", no_argument
, NULL
, 'D'},
274 {"disassembler-options", required_argument
, NULL
, 'M'},
275 {"disassemble-zeroes", no_argument
, NULL
, 'z'},
276 {"dynamic-reloc", no_argument
, NULL
, 'R'},
277 {"dynamic-syms", no_argument
, NULL
, 'T'},
278 {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
279 {"file-headers", no_argument
, NULL
, 'f'},
280 {"file-offsets", 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 {"special-syms", no_argument
, &dump_special_syms
, 1},
295 {"include", required_argument
, NULL
, 'I'},
296 {"dwarf", no_argument
, NULL
, 'W'},
297 {"stabs", no_argument
, NULL
, 'G'},
298 {"start-address", required_argument
, NULL
, OPTION_START_ADDRESS
},
299 {"stop-address", required_argument
, NULL
, OPTION_STOP_ADDRESS
},
300 {"syms", no_argument
, NULL
, 't'},
301 {"target", required_argument
, NULL
, 'b'},
302 {"version", no_argument
, NULL
, 'V'},
303 {"wide", no_argument
, NULL
, 'w'},
304 {"prefix", required_argument
, NULL
, OPTION_PREFIX
},
305 {"prefix-strip", required_argument
, NULL
, OPTION_PREFIX_STRIP
},
306 {0, no_argument
, 0, 0}
310 nonfatal (const char *msg
)
317 dump_section_header (bfd
*abfd
, asection
*section
,
318 void *ignored ATTRIBUTE_UNUSED
)
321 unsigned int opb
= bfd_octets_per_byte (abfd
);
323 /* Ignore linker created section. See elfNN_ia64_object_p in
325 if (section
->flags
& SEC_LINKER_CREATED
)
328 printf ("%3d %-13s %08lx ", section
->index
,
329 bfd_get_section_name (abfd
, section
),
330 (unsigned long) bfd_section_size (abfd
, section
) / opb
);
331 bfd_printf_vma (abfd
, bfd_get_section_vma (abfd
, section
));
333 bfd_printf_vma (abfd
, section
->lma
);
334 printf (" %08lx 2**%u", (unsigned long) section
->filepos
,
335 bfd_get_section_alignment (abfd
, section
));
341 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
343 PF (SEC_HAS_CONTENTS
, "CONTENTS");
344 PF (SEC_ALLOC
, "ALLOC");
345 PF (SEC_CONSTRUCTOR
, "CONSTRUCTOR");
346 PF (SEC_LOAD
, "LOAD");
347 PF (SEC_RELOC
, "RELOC");
348 PF (SEC_READONLY
, "READONLY");
349 PF (SEC_CODE
, "CODE");
350 PF (SEC_DATA
, "DATA");
352 PF (SEC_DEBUGGING
, "DEBUGGING");
353 PF (SEC_NEVER_LOAD
, "NEVER_LOAD");
354 PF (SEC_EXCLUDE
, "EXCLUDE");
355 PF (SEC_SORT_ENTRIES
, "SORT_ENTRIES");
356 if (bfd_get_arch (abfd
) == bfd_arch_tic54x
)
358 PF (SEC_TIC54X_BLOCK
, "BLOCK");
359 PF (SEC_TIC54X_CLINK
, "CLINK");
361 PF (SEC_SMALL_DATA
, "SMALL_DATA");
362 if (bfd_get_flavour (abfd
) == bfd_target_coff_flavour
)
363 PF (SEC_COFF_SHARED
, "SHARED");
364 PF (SEC_THREAD_LOCAL
, "THREAD_LOCAL");
365 PF (SEC_GROUP
, "GROUP");
367 if ((section
->flags
& SEC_LINK_ONCE
) != 0)
370 struct coff_comdat_info
*comdat
;
372 switch (section
->flags
& SEC_LINK_DUPLICATES
)
376 case SEC_LINK_DUPLICATES_DISCARD
:
377 ls
= "LINK_ONCE_DISCARD";
379 case SEC_LINK_DUPLICATES_ONE_ONLY
:
380 ls
= "LINK_ONCE_ONE_ONLY";
382 case SEC_LINK_DUPLICATES_SAME_SIZE
:
383 ls
= "LINK_ONCE_SAME_SIZE";
385 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
386 ls
= "LINK_ONCE_SAME_CONTENTS";
389 printf ("%s%s", comma
, ls
);
391 comdat
= bfd_coff_get_comdat_section (abfd
, section
);
393 printf (" (COMDAT %s %ld)", comdat
->name
, comdat
->symbol
);
403 dump_headers (bfd
*abfd
)
405 printf (_("Sections:\n"));
408 printf (_("Idx Name Size VMA LMA File off Algn"));
410 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
411 if (bfd_get_arch_size (abfd
) == 32)
412 printf (_("Idx Name Size VMA LMA File off Algn"));
414 printf (_("Idx Name Size VMA LMA File off Algn"));
418 printf (_(" Flags"));
419 if (abfd
->flags
& HAS_LOAD_PAGE
)
423 bfd_map_over_sections (abfd
, dump_section_header
, NULL
);
427 slurp_symtab (bfd
*abfd
)
432 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
438 storage
= bfd_get_symtab_upper_bound (abfd
);
440 bfd_fatal (bfd_get_filename (abfd
));
442 sy
= xmalloc (storage
);
444 symcount
= bfd_canonicalize_symtab (abfd
, sy
);
446 bfd_fatal (bfd_get_filename (abfd
));
450 /* Read in the dynamic symbols. */
453 slurp_dynamic_symtab (bfd
*abfd
)
458 storage
= bfd_get_dynamic_symtab_upper_bound (abfd
);
461 if (!(bfd_get_file_flags (abfd
) & DYNAMIC
))
463 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd
));
468 bfd_fatal (bfd_get_filename (abfd
));
471 sy
= xmalloc (storage
);
473 dynsymcount
= bfd_canonicalize_dynamic_symtab (abfd
, sy
);
475 bfd_fatal (bfd_get_filename (abfd
));
479 /* Filter out (in place) symbols that are useless for disassembly.
480 COUNT is the number of elements in SYMBOLS.
481 Return the number of useful symbols. */
484 remove_useless_symbols (asymbol
**symbols
, long count
)
486 asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
490 asymbol
*sym
= *in_ptr
++;
492 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
494 if (sym
->flags
& (BSF_DEBUGGING
| BSF_SECTION_SYM
))
496 if (bfd_is_und_section (sym
->section
)
497 || bfd_is_com_section (sym
->section
))
502 return out_ptr
- symbols
;
505 /* Sort symbols into value order. */
508 compare_symbols (const void *ap
, const void *bp
)
510 const asymbol
*a
= * (const asymbol
**) ap
;
511 const asymbol
*b
= * (const asymbol
**) bp
;
521 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
523 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
526 if (a
->section
> b
->section
)
528 else if (a
->section
< b
->section
)
531 an
= bfd_asymbol_name (a
);
532 bn
= bfd_asymbol_name (b
);
536 /* The symbols gnu_compiled and gcc2_compiled convey no real
537 information, so put them after other symbols with the same value. */
538 af
= (strstr (an
, "gnu_compiled") != NULL
539 || strstr (an
, "gcc2_compiled") != NULL
);
540 bf
= (strstr (bn
, "gnu_compiled") != NULL
541 || strstr (bn
, "gcc2_compiled") != NULL
);
548 /* We use a heuristic for the file name, to try to sort it after
549 more useful symbols. It may not work on non Unix systems, but it
550 doesn't really matter; the only difference is precisely which
551 symbol names get printed. */
553 #define file_symbol(s, sn, snl) \
554 (((s)->flags & BSF_FILE) != 0 \
555 || ((sn)[(snl) - 2] == '.' \
556 && ((sn)[(snl) - 1] == 'o' \
557 || (sn)[(snl) - 1] == 'a')))
559 af
= file_symbol (a
, an
, anl
);
560 bf
= file_symbol (b
, bn
, bnl
);
567 /* Try to sort global symbols before local symbols before function
568 symbols before debugging symbols. */
573 if ((aflags
& BSF_DEBUGGING
) != (bflags
& BSF_DEBUGGING
))
575 if ((aflags
& BSF_DEBUGGING
) != 0)
580 if ((aflags
& BSF_FUNCTION
) != (bflags
& BSF_FUNCTION
))
582 if ((aflags
& BSF_FUNCTION
) != 0)
587 if ((aflags
& BSF_LOCAL
) != (bflags
& BSF_LOCAL
))
589 if ((aflags
& BSF_LOCAL
) != 0)
594 if ((aflags
& BSF_GLOBAL
) != (bflags
& BSF_GLOBAL
))
596 if ((aflags
& BSF_GLOBAL
) != 0)
602 /* Symbols that start with '.' might be section names, so sort them
603 after symbols that don't start with '.'. */
604 if (an
[0] == '.' && bn
[0] != '.')
606 if (an
[0] != '.' && bn
[0] == '.')
609 /* Finally, if we can't distinguish them in any other way, try to
610 get consistent results by sorting the symbols by name. */
611 return strcmp (an
, bn
);
614 /* Sort relocs into address order. */
617 compare_relocs (const void *ap
, const void *bp
)
619 const arelent
*a
= * (const arelent
**) ap
;
620 const arelent
*b
= * (const arelent
**) bp
;
622 if (a
->address
> b
->address
)
624 else if (a
->address
< b
->address
)
627 /* So that associated relocations tied to the same address show up
628 in the correct order, we don't do any further sorting. */
637 /* Print an address (VMA) to the output stream in INFO.
638 If SKIP_ZEROES is TRUE, omit leading zeroes. */
641 objdump_print_value (bfd_vma vma
, struct disassemble_info
*info
,
642 bfd_boolean skip_zeroes
)
646 struct objdump_disasm_info
*aux
;
648 aux
= (struct objdump_disasm_info
*) info
->application_data
;
649 bfd_sprintf_vma (aux
->abfd
, buf
, vma
);
654 for (p
= buf
; *p
== '0'; ++p
)
659 (*info
->fprintf_func
) (info
->stream
, "%s", p
);
662 /* Print the name of a symbol. */
665 objdump_print_symname (bfd
*abfd
, struct disassemble_info
*info
,
672 name
= bfd_asymbol_name (sym
);
673 if (do_demangle
&& name
[0] != '\0')
675 /* Demangle the name. */
676 alloc
= bfd_demangle (abfd
, name
, DMGL_ANSI
| DMGL_PARAMS
);
682 (*info
->fprintf_func
) (info
->stream
, "%s", name
);
690 /* Locate a symbol given a bfd and a section (from INFO->application_data),
691 and a VMA. If INFO->application_data->require_sec is TRUE, then always
692 require the symbol to be in the section. Returns NULL if there is no
693 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
694 of the symbol in sorted_syms. */
697 find_symbol_for_address (bfd_vma vma
,
698 struct disassemble_info
*info
,
701 /* @@ Would it speed things up to cache the last two symbols returned,
702 and maybe their address ranges? For many processors, only one memory
703 operand can be present at a time, so the 2-entry cache wouldn't be
704 constantly churned by code doing heavy memory accesses. */
706 /* Indices in `sorted_syms'. */
708 long max
= sorted_symcount
;
710 struct objdump_disasm_info
*aux
;
714 bfd_boolean want_section
;
716 if (sorted_symcount
< 1)
719 aux
= (struct objdump_disasm_info
*) info
->application_data
;
722 opb
= bfd_octets_per_byte (abfd
);
724 /* Perform a binary search looking for the closest symbol to the
725 required value. We are searching the range (min, max]. */
726 while (min
+ 1 < max
)
730 thisplace
= (max
+ min
) / 2;
731 sym
= sorted_syms
[thisplace
];
733 if (bfd_asymbol_value (sym
) > vma
)
735 else if (bfd_asymbol_value (sym
) < vma
)
744 /* The symbol we want is now in min, the low end of the range we
745 were searching. If there are several symbols with the same
746 value, we want the first one. */
749 && (bfd_asymbol_value (sorted_syms
[thisplace
])
750 == bfd_asymbol_value (sorted_syms
[thisplace
- 1])))
753 /* Prefer a symbol in the current section if we have multple symbols
754 with the same value, as can occur with overlays or zero size
758 && (bfd_asymbol_value (sorted_syms
[min
])
759 == bfd_asymbol_value (sorted_syms
[thisplace
])))
761 if (sorted_syms
[min
]->section
== sec
762 && info
->symbol_is_valid (sorted_syms
[min
], info
))
769 return sorted_syms
[thisplace
];
774 /* If the file is relocatable, and the symbol could be from this
775 section, prefer a symbol from this section over symbols from
776 others, even if the other symbol's value might be closer.
778 Note that this may be wrong for some symbol references if the
779 sections have overlapping memory ranges, but in that case there's
780 no way to tell what's desired without looking at the relocation
783 Also give the target a chance to reject symbols. */
784 want_section
= (aux
->require_sec
785 || ((abfd
->flags
& HAS_RELOC
) != 0
786 && vma
>= bfd_get_section_vma (abfd
, sec
)
787 && vma
< (bfd_get_section_vma (abfd
, sec
)
788 + bfd_section_size (abfd
, sec
) / opb
)));
789 if ((sorted_syms
[thisplace
]->section
!= sec
&& want_section
)
790 || !info
->symbol_is_valid (sorted_syms
[thisplace
], info
))
793 long newplace
= sorted_symcount
;
795 for (i
= min
- 1; i
>= 0; i
--)
797 if ((sorted_syms
[i
]->section
== sec
|| !want_section
)
798 && info
->symbol_is_valid (sorted_syms
[i
], info
))
800 if (newplace
== sorted_symcount
)
803 if (bfd_asymbol_value (sorted_syms
[i
])
804 != bfd_asymbol_value (sorted_syms
[newplace
]))
807 /* Remember this symbol and keep searching until we reach
808 an earlier address. */
813 if (newplace
!= sorted_symcount
)
814 thisplace
= newplace
;
817 /* We didn't find a good symbol with a smaller value.
818 Look for one with a larger value. */
819 for (i
= thisplace
+ 1; i
< sorted_symcount
; i
++)
821 if ((sorted_syms
[i
]->section
== sec
|| !want_section
)
822 && info
->symbol_is_valid (sorted_syms
[i
], info
))
830 if ((sorted_syms
[thisplace
]->section
!= sec
&& want_section
)
831 || !info
->symbol_is_valid (sorted_syms
[thisplace
], info
))
832 /* There is no suitable symbol. */
839 return sorted_syms
[thisplace
];
842 /* Print an address and the offset to the nearest symbol. */
845 objdump_print_addr_with_sym (bfd
*abfd
, asection
*sec
, asymbol
*sym
,
846 bfd_vma vma
, struct disassemble_info
*info
,
847 bfd_boolean skip_zeroes
)
849 objdump_print_value (vma
, info
, skip_zeroes
);
855 (*info
->fprintf_func
) (info
->stream
, " <%s",
856 bfd_get_section_name (abfd
, sec
));
857 secaddr
= bfd_get_section_vma (abfd
, sec
);
860 (*info
->fprintf_func
) (info
->stream
, "-0x");
861 objdump_print_value (secaddr
- vma
, info
, TRUE
);
863 else if (vma
> secaddr
)
865 (*info
->fprintf_func
) (info
->stream
, "+0x");
866 objdump_print_value (vma
- secaddr
, info
, TRUE
);
868 (*info
->fprintf_func
) (info
->stream
, ">");
872 (*info
->fprintf_func
) (info
->stream
, " <");
873 objdump_print_symname (abfd
, info
, sym
);
874 if (bfd_asymbol_value (sym
) > vma
)
876 (*info
->fprintf_func
) (info
->stream
, "-0x");
877 objdump_print_value (bfd_asymbol_value (sym
) - vma
, info
, TRUE
);
879 else if (vma
> bfd_asymbol_value (sym
))
881 (*info
->fprintf_func
) (info
->stream
, "+0x");
882 objdump_print_value (vma
- bfd_asymbol_value (sym
), info
, TRUE
);
884 (*info
->fprintf_func
) (info
->stream
, ">");
887 if (display_file_offsets
)
888 info
->fprintf_func (info
->stream
, _(" (File Offset: 0x%lx)"),
889 (long int)(sec
->filepos
+ (vma
- sec
->vma
)));
892 /* Print an address (VMA), symbolically if possible.
893 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
896 objdump_print_addr (bfd_vma vma
,
897 struct disassemble_info
*info
,
898 bfd_boolean skip_zeroes
)
900 struct objdump_disasm_info
*aux
;
902 bfd_boolean skip_find
= FALSE
;
904 aux
= (struct objdump_disasm_info
*) info
->application_data
;
906 if (sorted_symcount
< 1)
908 (*info
->fprintf_func
) (info
->stream
, "0x");
909 objdump_print_value (vma
, info
, skip_zeroes
);
911 if (display_file_offsets
)
912 info
->fprintf_func (info
->stream
, _(" (File Offset: 0x%lx)"),
913 (long int)(aux
->sec
->filepos
+ (vma
- aux
->sec
->vma
)));
917 if (aux
->reloc
!= NULL
918 && aux
->reloc
->sym_ptr_ptr
!= NULL
919 && * aux
->reloc
->sym_ptr_ptr
!= NULL
)
921 sym
= * aux
->reloc
->sym_ptr_ptr
;
923 /* Adjust the vma to the reloc. */
924 vma
+= bfd_asymbol_value (sym
);
926 if (bfd_is_und_section (bfd_get_section (sym
)))
931 sym
= find_symbol_for_address (vma
, info
, NULL
);
933 objdump_print_addr_with_sym (aux
->abfd
, aux
->sec
, sym
, vma
, info
,
937 /* Print VMA to INFO. This function is passed to the disassembler
941 objdump_print_address (bfd_vma vma
, struct disassemble_info
*info
)
943 objdump_print_addr (vma
, info
, ! prefix_addresses
);
946 /* Determine if the given address has a symbol associated with it. */
949 objdump_symbol_at_address (bfd_vma vma
, struct disassemble_info
* info
)
953 sym
= find_symbol_for_address (vma
, info
, NULL
);
955 return (sym
!= NULL
&& (bfd_asymbol_value (sym
) == vma
));
958 /* Hold the last function name and the last line number we displayed
961 static char *prev_functionname
;
962 static unsigned int prev_line
;
964 /* We keep a list of all files that we have seen when doing a
965 disassembly with source, so that we know how much of the file to
966 display. This can be important for inlined functions. */
968 struct print_file_list
970 struct print_file_list
*next
;
971 const char *filename
;
975 const char **linemap
;
981 static struct print_file_list
*print_files
;
983 /* The number of preceding context lines to show when we start
984 displaying a file for the first time. */
986 #define SHOW_PRECEDING_CONTEXT_LINES (5)
988 /* Read a complete file into memory. */
991 slurp_file (const char *fn
, size_t *size
)
994 int ps
= getpagesize ();
999 int fd
= open (fn
, O_RDONLY
| O_BINARY
);
1003 if (fstat (fd
, &st
) < 0)
1007 msize
= (*size
+ ps
- 1) & ~(ps
- 1);
1008 map
= mmap (NULL
, msize
, PROT_READ
, MAP_SHARED
, fd
, 0);
1009 if (map
!= (char *)-1L)
1015 map
= malloc (*size
);
1016 if (!map
|| (size_t) read (fd
, (char *)map
, *size
) != *size
)
1025 #define line_map_decrease 5
1027 /* Precompute array of lines for a mapped file. */
1029 static const char **
1030 index_file (const char *map
, size_t size
, unsigned int *maxline
)
1032 const char *p
, *lstart
, *end
;
1033 int chars_per_line
= 45; /* First iteration will use 40. */
1034 unsigned int lineno
;
1035 const char **linemap
= NULL
;
1036 unsigned long line_map_size
= 0;
1042 for (p
= map
; p
< end
; p
++)
1046 if (p
+ 1 < end
&& p
[1] == '\r')
1049 else if (*p
== '\r')
1051 if (p
+ 1 < end
&& p
[1] == '\n')
1057 /* End of line found. */
1059 if (linemap
== NULL
|| line_map_size
< lineno
+ 1)
1061 unsigned long newsize
;
1063 chars_per_line
-= line_map_decrease
;
1064 if (chars_per_line
<= 1)
1066 line_map_size
= size
/ chars_per_line
+ 1;
1067 if (line_map_size
< lineno
+ 1)
1068 line_map_size
= lineno
+ 1;
1069 newsize
= line_map_size
* sizeof (char *);
1070 linemap
= xrealloc (linemap
, newsize
);
1073 linemap
[lineno
++] = lstart
;
1081 /* Tries to open MODNAME, and if successful adds a node to print_files
1082 linked list and returns that node. Returns NULL on failure. */
1084 static struct print_file_list
*
1085 try_print_file_open (const char *origname
, const char *modname
)
1087 struct print_file_list
*p
;
1089 p
= xmalloc (sizeof (struct print_file_list
));
1091 p
->map
= slurp_file (modname
, &p
->mapsize
);
1098 p
->linemap
= index_file (p
->map
, p
->mapsize
, &p
->maxline
);
1100 p
->filename
= origname
;
1101 p
->modname
= modname
;
1102 p
->next
= print_files
;
1108 /* If the the source file, as described in the symtab, is not found
1109 try to locate it in one of the paths specified with -I
1110 If found, add location to print_files linked list. */
1112 static struct print_file_list
*
1113 update_source_path (const char *filename
)
1115 struct print_file_list
*p
;
1119 if (filename
== NULL
)
1122 p
= try_print_file_open (filename
, filename
);
1126 if (include_path_count
== 0)
1129 /* Get the name of the file. */
1130 fname
= strrchr (filename
, '/');
1131 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
1133 /* We could have a mixed forward/back slash case. */
1134 char *backslash
= strrchr (filename
, '\\');
1135 if (fname
== NULL
|| (backslash
!= NULL
&& backslash
> fname
))
1137 if (fname
== NULL
&& filename
[0] != '\0' && filename
[1] == ':')
1138 fname
= filename
+ 1;
1146 /* If file exists under a new path, we need to add it to the list
1147 so that show_line knows about it. */
1148 for (i
= 0; i
< include_path_count
; i
++)
1150 char *modname
= concat (include_paths
[i
], "/", fname
, (const char *) 0);
1152 p
= try_print_file_open (filename
, modname
);
1162 /* Print a source file line. */
1165 print_line (struct print_file_list
*p
, unsigned int line
)
1171 if (line
>= p
->maxline
)
1173 l
= p
->linemap
[line
];
1174 /* Test fwrite return value to quiet glibc warning. */
1175 len
= strcspn (l
, "\n\r");
1176 if (len
== 0 || fwrite (l
, len
, 1, stdout
) == 1)
1180 /* Print a range of source code lines. */
1183 dump_lines (struct print_file_list
*p
, unsigned int start
, unsigned int end
)
1187 while (start
<= end
)
1189 print_line (p
, start
);
1194 /* Show the line number, or the source line, in a disassembly
1198 show_line (bfd
*abfd
, asection
*section
, bfd_vma addr_offset
)
1200 const char *filename
;
1201 const char *functionname
;
1205 if (! with_line_numbers
&& ! with_source_code
)
1208 if (! bfd_find_nearest_line (abfd
, section
, syms
, addr_offset
, &filename
,
1209 &functionname
, &line
))
1212 if (filename
!= NULL
&& *filename
== '\0')
1214 if (functionname
!= NULL
&& *functionname
== '\0')
1215 functionname
= NULL
;
1218 && IS_ABSOLUTE_PATH (filename
)
1222 const char *fname
= filename
;
1223 char *path
= (char *) alloca (prefix_length
+ PATH_MAX
+ 1);
1226 memcpy (path
, prefix
, prefix_length
);
1227 path_up
= path
+ prefix_length
;
1229 /* Build relocated filename, stripping off leading directories
1230 from the initial filename if requested. */
1231 if (prefix_strip
> 0)
1236 /* Skip selected directory levels. */
1237 for (s
= fname
+ 1; *s
!= '\0' && level
< prefix_strip
; s
++)
1238 if (IS_DIR_SEPARATOR(*s
))
1245 /* Update complete filename. */
1246 strncpy (path_up
, fname
, PATH_MAX
);
1247 path_up
[PATH_MAX
] = '\0';
1255 if (with_line_numbers
)
1257 if (functionname
!= NULL
1258 && (prev_functionname
== NULL
1259 || strcmp (functionname
, prev_functionname
) != 0))
1260 printf ("%s():\n", functionname
);
1261 if (line
> 0 && line
!= prev_line
)
1262 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
1265 if (with_source_code
1269 struct print_file_list
**pp
, *p
;
1272 for (pp
= &print_files
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1273 if (strcmp ((*pp
)->filename
, filename
) == 0)
1280 filename
= xstrdup (filename
);
1281 p
= update_source_path (filename
);
1284 if (p
!= NULL
&& line
!= p
->last_line
)
1286 if (file_start_context
&& p
->first
)
1290 l
= line
- SHOW_PRECEDING_CONTEXT_LINES
;
1293 if (p
->last_line
>= l
&& p
->last_line
<= line
)
1294 l
= p
->last_line
+ 1;
1296 dump_lines (p
, l
, line
);
1297 p
->last_line
= line
;
1302 if (functionname
!= NULL
1303 && (prev_functionname
== NULL
1304 || strcmp (functionname
, prev_functionname
) != 0))
1306 if (prev_functionname
!= NULL
)
1307 free (prev_functionname
);
1308 prev_functionname
= xmalloc (strlen (functionname
) + 1);
1309 strcpy (prev_functionname
, functionname
);
1312 if (line
> 0 && line
!= prev_line
)
1316 /* Pseudo FILE object for strings. */
1324 /* sprintf to a "stream". */
1326 static int ATTRIBUTE_PRINTF_2
1327 objdump_sprintf (SFILE
*f
, const char *format
, ...)
1334 size_t space
= f
->alloc
- f
->pos
;
1336 va_start (args
, format
);
1337 n
= vsnprintf (f
->buffer
+ f
->pos
, space
, format
, args
);
1343 f
->alloc
= (f
->alloc
+ n
) * 2;
1344 f
->buffer
= xrealloc (f
->buffer
, f
->alloc
);
1351 /* Returns TRUE if the specified section should be dumped. */
1354 process_section_p (asection
* section
)
1361 for (i
= 0; i
< only_used
; i
++)
1362 if (strcmp (only
[i
], section
->name
) == 0)
1369 /* The number of zeroes we want to see before we start skipping them.
1370 The number is arbitrarily chosen. */
1372 #define DEFAULT_SKIP_ZEROES 8
1374 /* The number of zeroes to skip at the end of a section. If the
1375 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1376 SKIP_ZEROES, they will be disassembled. If there are fewer than
1377 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1378 attempt to avoid disassembling zeroes inserted by section
1381 #define DEFAULT_SKIP_ZEROES_AT_END 3
1383 /* Disassemble some data in memory between given values. */
1386 disassemble_bytes (struct disassemble_info
* info
,
1387 disassembler_ftype disassemble_fn
,
1390 bfd_vma start_offset
,
1391 bfd_vma stop_offset
,
1394 arelent
** relppend
)
1396 struct objdump_disasm_info
*aux
;
1398 int octets_per_line
;
1399 bfd_boolean done_dot
;
1400 int skip_addr_chars
;
1401 bfd_vma addr_offset
;
1402 unsigned int opb
= info
->octets_per_byte
;
1403 unsigned int skip_zeroes
= info
->skip_zeroes
;
1404 unsigned int skip_zeroes_at_end
= info
->skip_zeroes_at_end
;
1408 aux
= (struct objdump_disasm_info
*) info
->application_data
;
1412 sfile
.buffer
= xmalloc (sfile
.alloc
);
1416 octets_per_line
= 4;
1418 octets_per_line
= 16;
1420 /* Figure out how many characters to skip at the start of an
1421 address, to make the disassembly look nicer. We discard leading
1422 zeroes in chunks of 4, ensuring that there is always a leading
1424 skip_addr_chars
= 0;
1425 if (! prefix_addresses
)
1429 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ section
->size
/ opb
);
1431 while (buf
[skip_addr_chars
] == '0')
1434 /* Don't discard zeros on overflow. */
1435 if (buf
[skip_addr_chars
] == '\0' && section
->vma
!= 0)
1436 skip_addr_chars
= 0;
1438 if (skip_addr_chars
!= 0)
1439 skip_addr_chars
= (skip_addr_chars
- 1) & -4;
1442 info
->insn_info_valid
= 0;
1445 addr_offset
= start_offset
;
1446 while (addr_offset
< stop_offset
)
1449 bfd_boolean need_nl
= FALSE
;
1450 int previous_octets
;
1452 /* Remember the length of the previous instruction. */
1453 previous_octets
= octets
;
1456 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1458 for (z
= addr_offset
* opb
; z
< stop_offset
* opb
; z
++)
1461 if (! disassemble_zeroes
1462 && (info
->insn_info_valid
== 0
1463 || info
->branch_delay_insns
== 0)
1464 && (z
- addr_offset
* opb
>= skip_zeroes
1465 || (z
== stop_offset
* opb
&&
1466 z
- addr_offset
* opb
< skip_zeroes_at_end
)))
1468 /* If there are more nonzero octets to follow, we only skip
1469 zeroes in multiples of 4, to try to avoid running over
1470 the start of an instruction which happens to start with
1472 if (z
!= stop_offset
* opb
)
1473 z
= addr_offset
* opb
+ ((z
- addr_offset
* opb
) &~ 3);
1475 octets
= z
- addr_offset
* opb
;
1477 /* If we are going to display more data, and we are displaying
1478 file offsets, then tell the user how many zeroes we skip
1479 and the file offset from where we resume dumping. */
1480 if (display_file_offsets
&& ((addr_offset
+ (octets
/ opb
)) < stop_offset
))
1481 printf ("\t... (skipping %d zeroes, resuming at file offset: 0x%lx)\n",
1483 (unsigned long) (section
->filepos
1484 + (addr_offset
+ (octets
/ opb
))));
1496 if (with_line_numbers
|| with_source_code
)
1497 show_line (aux
->abfd
, section
, addr_offset
);
1499 if (! prefix_addresses
)
1503 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ addr_offset
);
1504 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
1508 printf ("%s:\t", buf
+ skip_addr_chars
);
1512 aux
->require_sec
= TRUE
;
1513 objdump_print_address (section
->vma
+ addr_offset
, info
);
1514 aux
->require_sec
= FALSE
;
1521 info
->fprintf_func
= (fprintf_ftype
) objdump_sprintf
;
1522 info
->stream
= &sfile
;
1523 info
->bytes_per_line
= 0;
1524 info
->bytes_per_chunk
= 0;
1527 if (info
->disassembler_needs_relocs
1528 && (bfd_get_file_flags (aux
->abfd
) & EXEC_P
) == 0
1529 && (bfd_get_file_flags (aux
->abfd
) & DYNAMIC
) == 0
1530 && *relppp
< relppend
)
1532 bfd_signed_vma distance_to_rel
;
1534 distance_to_rel
= (**relppp
)->address
1535 - (rel_offset
+ addr_offset
);
1537 /* Check to see if the current reloc is associated with
1538 the instruction that we are about to disassemble. */
1539 if (distance_to_rel
== 0
1540 /* FIXME: This is wrong. We are trying to catch
1541 relocs that are addressed part way through the
1542 current instruction, as might happen with a packed
1543 VLIW instruction. Unfortunately we do not know the
1544 length of the current instruction since we have not
1545 disassembled it yet. Instead we take a guess based
1546 upon the length of the previous instruction. The
1547 proper solution is to have a new target-specific
1548 disassembler function which just returns the length
1549 of an instruction at a given address without trying
1550 to display its disassembly. */
1551 || (distance_to_rel
> 0
1552 && distance_to_rel
< (bfd_signed_vma
) (previous_octets
/ opb
)))
1554 info
->flags
= INSN_HAS_RELOC
;
1555 aux
->reloc
= **relppp
;
1561 octets
= (*disassemble_fn
) (section
->vma
+ addr_offset
, info
);
1562 info
->fprintf_func
= (fprintf_ftype
) fprintf
;
1563 info
->stream
= stdout
;
1564 if (info
->bytes_per_line
!= 0)
1565 octets_per_line
= info
->bytes_per_line
;
1569 printf ("%s\n", sfile
.buffer
);
1577 octets
= octets_per_line
;
1578 if (addr_offset
+ octets
/ opb
> stop_offset
)
1579 octets
= (stop_offset
- addr_offset
) * opb
;
1581 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ octets
; ++j
)
1583 if (ISPRINT (data
[j
]))
1584 buf
[j
- addr_offset
* opb
] = data
[j
];
1586 buf
[j
- addr_offset
* opb
] = '.';
1588 buf
[j
- addr_offset
* opb
] = '\0';
1591 if (prefix_addresses
1593 : show_raw_insn
>= 0)
1597 /* If ! prefix_addresses and ! wide_output, we print
1598 octets_per_line octets per line. */
1600 if (pb
> octets_per_line
&& ! prefix_addresses
&& ! wide_output
)
1601 pb
= octets_per_line
;
1603 if (info
->bytes_per_chunk
)
1604 bpc
= info
->bytes_per_chunk
;
1608 for (j
= addr_offset
* opb
; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
1612 if (bpc
> 1 && info
->display_endian
== BFD_ENDIAN_LITTLE
)
1614 for (k
= bpc
- 1; k
>= 0; k
--)
1615 printf ("%02x", (unsigned) data
[j
+ k
]);
1620 for (k
= 0; k
< bpc
; k
++)
1621 printf ("%02x", (unsigned) data
[j
+ k
]);
1626 for (; pb
< octets_per_line
; pb
+= bpc
)
1630 for (k
= 0; k
< bpc
; k
++)
1635 /* Separate raw data from instruction by extra space. */
1645 printf ("%s", sfile
.buffer
);
1647 if (prefix_addresses
1649 : show_raw_insn
>= 0)
1657 j
= addr_offset
* opb
+ pb
;
1659 bfd_sprintf_vma (aux
->abfd
, buf
, section
->vma
+ j
/ opb
);
1660 for (s
= buf
+ skip_addr_chars
; *s
== '0'; s
++)
1664 printf ("%s:\t", buf
+ skip_addr_chars
);
1666 pb
+= octets_per_line
;
1669 for (; j
< addr_offset
* opb
+ pb
; j
+= bpc
)
1673 if (bpc
> 1 && info
->display_endian
== BFD_ENDIAN_LITTLE
)
1675 for (k
= bpc
- 1; k
>= 0; k
--)
1676 printf ("%02x", (unsigned) data
[j
+ k
]);
1681 for (k
= 0; k
< bpc
; k
++)
1682 printf ("%02x", (unsigned) data
[j
+ k
]);
1695 while ((*relppp
) < relppend
1696 && (**relppp
)->address
< rel_offset
+ addr_offset
+ octets
/ opb
)
1698 if (dump_reloc_info
|| dump_dynamic_reloc_info
)
1709 objdump_print_value (section
->vma
- rel_offset
+ q
->address
,
1712 if (q
->howto
== NULL
)
1713 printf (": *unknown*\t");
1714 else if (q
->howto
->name
)
1715 printf (": %s\t", q
->howto
->name
);
1717 printf (": %d\t", q
->howto
->type
);
1719 if (q
->sym_ptr_ptr
== NULL
|| *q
->sym_ptr_ptr
== NULL
)
1720 printf ("*unknown*");
1723 const char *sym_name
;
1725 sym_name
= bfd_asymbol_name (*q
->sym_ptr_ptr
);
1726 if (sym_name
!= NULL
&& *sym_name
!= '\0')
1727 objdump_print_symname (aux
->abfd
, info
, *q
->sym_ptr_ptr
);
1732 sym_sec
= bfd_get_section (*q
->sym_ptr_ptr
);
1733 sym_name
= bfd_get_section_name (aux
->abfd
, sym_sec
);
1734 if (sym_name
== NULL
|| *sym_name
== '\0')
1735 sym_name
= "*unknown*";
1736 printf ("%s", sym_name
);
1743 objdump_print_value (q
->addend
, info
, TRUE
);
1755 addr_offset
+= octets
/ opb
;
1758 free (sfile
.buffer
);
1762 disassemble_section (bfd
*abfd
, asection
*section
, void *info
)
1764 const struct elf_backend_data
* bed
;
1765 bfd_vma sign_adjust
= 0;
1766 struct disassemble_info
* pinfo
= (struct disassemble_info
*) info
;
1767 struct objdump_disasm_info
* paux
;
1768 unsigned int opb
= pinfo
->octets_per_byte
;
1769 bfd_byte
* data
= NULL
;
1770 bfd_size_type datasize
= 0;
1771 arelent
** rel_pp
= NULL
;
1772 arelent
** rel_ppstart
= NULL
;
1773 arelent
** rel_ppend
;
1774 unsigned long stop_offset
;
1775 asymbol
* sym
= NULL
;
1779 unsigned long addr_offset
;
1781 /* Sections that do not contain machine
1782 code are not normally disassembled. */
1783 if (! disassemble_all
1785 && ((section
->flags
& (SEC_CODE
| SEC_HAS_CONTENTS
))
1786 != (SEC_CODE
| SEC_HAS_CONTENTS
)))
1789 if (! process_section_p (section
))
1792 datasize
= bfd_get_section_size (section
);
1796 /* Decide which set of relocs to use. Load them if necessary. */
1797 paux
= (struct objdump_disasm_info
*) pinfo
->application_data
;
1798 if (paux
->dynrelbuf
)
1800 rel_pp
= paux
->dynrelbuf
;
1801 rel_count
= paux
->dynrelcount
;
1802 /* Dynamic reloc addresses are absolute, non-dynamic are section
1803 relative. REL_OFFSET specifies the reloc address corresponding
1804 to the start of this section. */
1805 rel_offset
= section
->vma
;
1813 if ((section
->flags
& SEC_RELOC
) != 0
1814 && (dump_reloc_info
|| pinfo
->disassembler_needs_relocs
))
1818 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
1820 bfd_fatal (bfd_get_filename (abfd
));
1824 rel_ppstart
= rel_pp
= xmalloc (relsize
);
1825 rel_count
= bfd_canonicalize_reloc (abfd
, section
, rel_pp
, syms
);
1827 bfd_fatal (bfd_get_filename (abfd
));
1829 /* Sort the relocs by address. */
1830 qsort (rel_pp
, rel_count
, sizeof (arelent
*), compare_relocs
);
1834 rel_ppend
= rel_pp
+ rel_count
;
1836 data
= xmalloc (datasize
);
1838 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
1840 paux
->sec
= section
;
1841 pinfo
->buffer
= data
;
1842 pinfo
->buffer_vma
= section
->vma
;
1843 pinfo
->buffer_length
= datasize
;
1844 pinfo
->section
= section
;
1846 if (start_address
== (bfd_vma
) -1
1847 || start_address
< pinfo
->buffer_vma
)
1850 addr_offset
= start_address
- pinfo
->buffer_vma
;
1852 if (stop_address
== (bfd_vma
) -1)
1853 stop_offset
= datasize
/ opb
;
1856 if (stop_address
< pinfo
->buffer_vma
)
1859 stop_offset
= stop_address
- pinfo
->buffer_vma
;
1860 if (stop_offset
> pinfo
->buffer_length
/ opb
)
1861 stop_offset
= pinfo
->buffer_length
/ opb
;
1864 /* Skip over the relocs belonging to addresses below the
1866 while (rel_pp
< rel_ppend
1867 && (*rel_pp
)->address
< rel_offset
+ addr_offset
)
1870 if (addr_offset
< stop_offset
)
1871 printf (_("\nDisassembly of section %s:\n"), section
->name
);
1873 /* Find the nearest symbol forwards from our current position. */
1874 paux
->require_sec
= TRUE
;
1875 sym
= find_symbol_for_address (section
->vma
+ addr_offset
, info
, &place
);
1876 paux
->require_sec
= FALSE
;
1878 /* PR 9774: If the target used signed 32-bit addresses then we must make
1879 sure that we sign extend the value that we calculate for 'addr' in the
1881 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1882 && (bed
= get_elf_backend_data (abfd
)) != NULL
1883 && bed
->sign_extend_vma
)
1884 sign_adjust
= 0x80000000;
1886 /* Disassemble a block of instructions up to the address associated with
1887 the symbol we have just found. Then print the symbol and find the
1888 next symbol on. Repeat until we have disassembled the entire section
1889 or we have reached the end of the address range we are interested in. */
1890 while (addr_offset
< stop_offset
)
1894 unsigned long nextstop_offset
;
1897 addr
= section
->vma
+ addr_offset
;
1898 addr
= (addr
^ sign_adjust
) - sign_adjust
;
1900 if (sym
!= NULL
&& bfd_asymbol_value (sym
) <= addr
)
1905 (x
< sorted_symcount
1906 && (bfd_asymbol_value (sorted_syms
[x
]) <= addr
));
1910 pinfo
->symbols
= sorted_syms
+ place
;
1911 pinfo
->num_symbols
= x
- place
;
1912 pinfo
->symtab_pos
= place
;
1916 pinfo
->symbols
= NULL
;
1917 pinfo
->num_symbols
= 0;
1918 pinfo
->symtab_pos
= -1;
1921 if (! prefix_addresses
)
1923 pinfo
->fprintf_func (pinfo
->stream
, "\n");
1924 objdump_print_addr_with_sym (abfd
, section
, sym
, addr
,
1926 pinfo
->fprintf_func (pinfo
->stream
, ":\n");
1929 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > addr
)
1931 else if (sym
== NULL
)
1935 #define is_valid_next_sym(SYM) \
1936 ((SYM)->section == section \
1937 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
1938 && pinfo->symbol_is_valid (SYM, pinfo))
1940 /* Search forward for the next appropriate symbol in
1941 SECTION. Note that all the symbols are sorted
1942 together into one big array, and that some sections
1943 may have overlapping addresses. */
1944 while (place
< sorted_symcount
1945 && ! is_valid_next_sym (sorted_syms
[place
]))
1948 if (place
>= sorted_symcount
)
1951 nextsym
= sorted_syms
[place
];
1954 if (sym
!= NULL
&& bfd_asymbol_value (sym
) > addr
)
1955 nextstop_offset
= bfd_asymbol_value (sym
) - section
->vma
;
1956 else if (nextsym
== NULL
)
1957 nextstop_offset
= stop_offset
;
1959 nextstop_offset
= bfd_asymbol_value (nextsym
) - section
->vma
;
1961 if (nextstop_offset
> stop_offset
)
1962 nextstop_offset
= stop_offset
;
1964 /* If a symbol is explicitly marked as being an object
1965 rather than a function, just dump the bytes without
1966 disassembling them. */
1969 || sym
->section
!= section
1970 || bfd_asymbol_value (sym
) > addr
1971 || ((sym
->flags
& BSF_OBJECT
) == 0
1972 && (strstr (bfd_asymbol_name (sym
), "gnu_compiled")
1974 && (strstr (bfd_asymbol_name (sym
), "gcc2_compiled")
1976 || (sym
->flags
& BSF_FUNCTION
) != 0)
1981 disassemble_bytes (pinfo
, paux
->disassemble_fn
, insns
, data
,
1982 addr_offset
, nextstop_offset
,
1983 rel_offset
, &rel_pp
, rel_ppend
);
1985 addr_offset
= nextstop_offset
;
1991 if (rel_ppstart
!= NULL
)
1995 /* Disassemble the contents of an object file. */
1998 disassemble_data (bfd
*abfd
)
2000 struct disassemble_info disasm_info
;
2001 struct objdump_disasm_info aux
;
2005 prev_functionname
= NULL
;
2008 /* We make a copy of syms to sort. We don't want to sort syms
2009 because that will screw up the relocs. */
2010 sorted_symcount
= symcount
? symcount
: dynsymcount
;
2011 sorted_syms
= xmalloc ((sorted_symcount
+ synthcount
) * sizeof (asymbol
*));
2012 memcpy (sorted_syms
, symcount
? syms
: dynsyms
,
2013 sorted_symcount
* sizeof (asymbol
*));
2015 sorted_symcount
= remove_useless_symbols (sorted_syms
, sorted_symcount
);
2017 for (i
= 0; i
< synthcount
; ++i
)
2019 sorted_syms
[sorted_symcount
] = synthsyms
+ i
;
2023 /* Sort the symbols into section and symbol order. */
2024 qsort (sorted_syms
, sorted_symcount
, sizeof (asymbol
*), compare_symbols
);
2026 init_disassemble_info (&disasm_info
, stdout
, (fprintf_ftype
) fprintf
);
2028 disasm_info
.application_data
= (void *) &aux
;
2030 aux
.require_sec
= FALSE
;
2031 aux
.dynrelbuf
= NULL
;
2032 aux
.dynrelcount
= 0;
2035 disasm_info
.print_address_func
= objdump_print_address
;
2036 disasm_info
.symbol_at_address_func
= objdump_symbol_at_address
;
2038 if (machine
!= NULL
)
2040 const bfd_arch_info_type
*info
= bfd_scan_arch (machine
);
2043 fatal (_("Can't use supplied machine %s"), machine
);
2045 abfd
->arch_info
= info
;
2048 if (endian
!= BFD_ENDIAN_UNKNOWN
)
2050 struct bfd_target
*xvec
;
2052 xvec
= xmalloc (sizeof (struct bfd_target
));
2053 memcpy (xvec
, abfd
->xvec
, sizeof (struct bfd_target
));
2054 xvec
->byteorder
= endian
;
2058 /* Use libopcodes to locate a suitable disassembler. */
2059 aux
.disassemble_fn
= disassembler (abfd
);
2060 if (!aux
.disassemble_fn
)
2062 non_fatal (_("Can't disassemble for architecture %s\n"),
2063 bfd_printable_arch_mach (bfd_get_arch (abfd
), 0));
2068 disasm_info
.flavour
= bfd_get_flavour (abfd
);
2069 disasm_info
.arch
= bfd_get_arch (abfd
);
2070 disasm_info
.mach
= bfd_get_mach (abfd
);
2071 disasm_info
.disassembler_options
= disassembler_options
;
2072 disasm_info
.octets_per_byte
= bfd_octets_per_byte (abfd
);
2073 disasm_info
.skip_zeroes
= DEFAULT_SKIP_ZEROES
;
2074 disasm_info
.skip_zeroes_at_end
= DEFAULT_SKIP_ZEROES_AT_END
;
2075 disasm_info
.disassembler_needs_relocs
= FALSE
;
2077 if (bfd_big_endian (abfd
))
2078 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_BIG
;
2079 else if (bfd_little_endian (abfd
))
2080 disasm_info
.display_endian
= disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
2082 /* ??? Aborting here seems too drastic. We could default to big or little
2084 disasm_info
.endian
= BFD_ENDIAN_UNKNOWN
;
2086 /* Allow the target to customize the info structure. */
2087 disassemble_init_for_target (& disasm_info
);
2089 /* Pre-load the dynamic relocs if we are going
2090 to be dumping them along with the disassembly. */
2091 if (dump_dynamic_reloc_info
)
2093 long relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
2096 bfd_fatal (bfd_get_filename (abfd
));
2100 aux
.dynrelbuf
= xmalloc (relsize
);
2101 aux
.dynrelcount
= bfd_canonicalize_dynamic_reloc (abfd
,
2104 if (aux
.dynrelcount
< 0)
2105 bfd_fatal (bfd_get_filename (abfd
));
2107 /* Sort the relocs by address. */
2108 qsort (aux
.dynrelbuf
, aux
.dynrelcount
, sizeof (arelent
*),
2112 disasm_info
.symtab
= sorted_syms
;
2113 disasm_info
.symtab_size
= sorted_symcount
;
2115 bfd_map_over_sections (abfd
, disassemble_section
, & disasm_info
);
2117 if (aux
.dynrelbuf
!= NULL
)
2118 free (aux
.dynrelbuf
);
2123 load_debug_section (enum dwarf_section_display_enum debug
, void *file
)
2125 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
2129 int section_is_compressed
;
2131 /* If it is already loaded, do nothing. */
2132 if (section
->start
!= NULL
)
2135 /* Locate the debug section. */
2136 sec
= bfd_get_section_by_name (abfd
, section
->uncompressed_name
);
2138 section
->name
= section
->uncompressed_name
;
2141 sec
= bfd_get_section_by_name (abfd
, section
->compressed_name
);
2143 section
->name
= section
->compressed_name
;
2147 section_is_compressed
= section
->name
== section
->compressed_name
;
2149 section
->address
= 0;
2150 section
->size
= bfd_get_section_size (sec
);
2151 section
->start
= xmalloc (section
->size
);
2153 if (is_relocatable
&& debug_displays
[debug
].relocate
)
2154 ret
= bfd_simple_get_relocated_section_contents (abfd
,
2159 ret
= bfd_get_section_contents (abfd
, sec
, section
->start
, 0,
2164 free_debug_section (debug
);
2165 printf (_("\nCan't get contents for section '%s'.\n"),
2170 if (section_is_compressed
)
2172 bfd_size_type size
= section
->size
;
2173 if (! bfd_uncompress_section_contents (§ion
->start
, &size
))
2175 free_debug_section (debug
);
2176 printf (_("\nCan't uncompress section '%s'.\n"), section
->name
);
2179 section
->size
= size
;
2186 free_debug_section (enum dwarf_section_display_enum debug
)
2188 struct dwarf_section
*section
= &debug_displays
[debug
].section
;
2190 if (section
->start
== NULL
)
2193 free ((char *) section
->start
);
2194 section
->start
= NULL
;
2195 section
->address
= 0;
2200 dump_dwarf_section (bfd
*abfd
, asection
*section
,
2201 void *arg ATTRIBUTE_UNUSED
)
2203 const char *name
= bfd_get_section_name (abfd
, section
);
2205 enum dwarf_section_display_enum i
;
2207 if (CONST_STRNEQ (name
, ".gnu.linkonce.wi."))
2208 match
= ".debug_info";
2212 for (i
= 0; i
< max
; i
++)
2213 if (strcmp (debug_displays
[i
].section
.uncompressed_name
, match
) == 0
2214 || strcmp (debug_displays
[i
].section
.compressed_name
, match
) == 0)
2216 if (!debug_displays
[i
].eh_frame
)
2218 struct dwarf_section
*sec
= &debug_displays
[i
].section
;
2220 if (load_debug_section (i
, abfd
))
2222 debug_displays
[i
].display (sec
, abfd
);
2224 if (i
!= info
&& i
!= abbrev
)
2225 free_debug_section (i
);
2232 /* Dump the dwarf debugging information. */
2235 dump_dwarf (bfd
*abfd
)
2237 is_relocatable
= (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0;
2239 /* FIXME: bfd_get_arch_size may return -1. We assume that 64bit
2240 targets will return 64. */
2241 eh_addr_size
= bfd_get_arch_size (abfd
) == 64 ? 8 : 4;
2243 if (bfd_big_endian (abfd
))
2244 byte_get
= byte_get_big_endian
;
2245 else if (bfd_little_endian (abfd
))
2246 byte_get
= byte_get_little_endian
;
2250 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
2252 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2253 init_dwarf_regnames (bed
->elf_machine_code
);
2256 bfd_map_over_sections (abfd
, dump_dwarf_section
, NULL
);
2258 free_debug_memory ();
2261 /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
2262 it. Return NULL on failure. */
2265 read_section_stabs (bfd
*abfd
, const char *sect_name
, bfd_size_type
*size_ptr
)
2271 stabsect
= bfd_get_section_by_name (abfd
, sect_name
);
2272 if (stabsect
== NULL
)
2274 printf (_("No %s section present\n\n"), sect_name
);
2278 size
= bfd_section_size (abfd
, stabsect
);
2279 contents
= xmalloc (size
);
2281 if (! bfd_get_section_contents (abfd
, stabsect
, contents
, 0, size
))
2283 non_fatal (_("Reading %s section of %s failed: %s"),
2284 sect_name
, bfd_get_filename (abfd
),
2285 bfd_errmsg (bfd_get_error ()));
2296 /* Stabs entries use a 12 byte format:
2297 4 byte string table index
2299 1 byte stab other field
2300 2 byte stab desc field
2302 FIXME: This will have to change for a 64 bit object format. */
2304 #define STRDXOFF (0)
2306 #define OTHEROFF (5)
2309 #define STABSIZE (12)
2311 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
2312 using string table section STRSECT_NAME (in `strtab'). */
2315 print_section_stabs (bfd
*abfd
,
2316 const char *stabsect_name
,
2317 unsigned *string_offset_ptr
)
2320 unsigned file_string_table_offset
= 0;
2321 unsigned next_file_string_table_offset
= *string_offset_ptr
;
2322 bfd_byte
*stabp
, *stabs_end
;
2325 stabs_end
= stabp
+ stab_size
;
2327 printf (_("Contents of %s section:\n\n"), stabsect_name
);
2328 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
2330 /* Loop through all symbols and print them.
2332 We start the index at -1 because there is a dummy symbol on
2333 the front of stabs-in-{coff,elf} sections that supplies sizes. */
2334 for (i
= -1; stabp
< stabs_end
; stabp
+= STABSIZE
, i
++)
2338 unsigned char type
, other
;
2339 unsigned short desc
;
2342 strx
= bfd_h_get_32 (abfd
, stabp
+ STRDXOFF
);
2343 type
= bfd_h_get_8 (abfd
, stabp
+ TYPEOFF
);
2344 other
= bfd_h_get_8 (abfd
, stabp
+ OTHEROFF
);
2345 desc
= bfd_h_get_16 (abfd
, stabp
+ DESCOFF
);
2346 value
= bfd_h_get_32 (abfd
, stabp
+ VALOFF
);
2348 printf ("\n%-6d ", i
);
2349 /* Either print the stab name, or, if unnamed, print its number
2350 again (makes consistent formatting for tools like awk). */
2351 name
= bfd_get_stab_name (type
);
2353 printf ("%-6s", name
);
2354 else if (type
== N_UNDF
)
2357 printf ("%-6d", type
);
2358 printf (" %-6d %-6d ", other
, desc
);
2359 bfd_printf_vma (abfd
, value
);
2360 printf (" %-6lu", strx
);
2362 /* Symbols with type == 0 (N_UNDF) specify the length of the
2363 string table associated with this file. We use that info
2364 to know how to relocate the *next* file's string table indices. */
2367 file_string_table_offset
= next_file_string_table_offset
;
2368 next_file_string_table_offset
+= value
;
2372 /* Using the (possibly updated) string table offset, print the
2373 string (if any) associated with this symbol. */
2374 if ((strx
+ file_string_table_offset
) < stabstr_size
)
2375 printf (" %s", &strtab
[strx
+ file_string_table_offset
]);
2381 *string_offset_ptr
= next_file_string_table_offset
;
2386 const char * section_name
;
2387 const char * string_section_name
;
2388 unsigned string_offset
;
2393 find_stabs_section (bfd
*abfd
, asection
*section
, void *names
)
2396 stab_section_names
* sought
= (stab_section_names
*) names
;
2398 /* Check for section names for which stabsect_name is a prefix, to
2399 handle .stab.N, etc. */
2400 len
= strlen (sought
->section_name
);
2402 /* If the prefix matches, and the files section name ends with a
2403 nul or a digit, then we match. I.e., we want either an exact
2404 match or a section followed by a number. */
2405 if (strncmp (sought
->section_name
, section
->name
, len
) == 0
2406 && (section
->name
[len
] == 0
2407 || (section
->name
[len
] == '.' && ISDIGIT (section
->name
[len
+ 1]))))
2410 strtab
= read_section_stabs (abfd
, sought
->string_section_name
,
2415 stabs
= (bfd_byte
*) read_section_stabs (abfd
, section
->name
,
2418 print_section_stabs (abfd
, section
->name
, &sought
->string_offset
);
2424 dump_stabs_section (bfd
*abfd
, char *stabsect_name
, char *strsect_name
)
2426 stab_section_names s
;
2428 s
.section_name
= stabsect_name
;
2429 s
.string_section_name
= strsect_name
;
2430 s
.string_offset
= 0;
2432 bfd_map_over_sections (abfd
, find_stabs_section
, & s
);
2438 /* Dump the any sections containing stabs debugging information. */
2441 dump_stabs (bfd
*abfd
)
2443 dump_stabs_section (abfd
, ".stab", ".stabstr");
2444 dump_stabs_section (abfd
, ".stab.excl", ".stab.exclstr");
2445 dump_stabs_section (abfd
, ".stab.index", ".stab.indexstr");
2448 dump_stabs_section (abfd
, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
2450 dump_stabs_section (abfd
, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
2454 dump_bfd_header (bfd
*abfd
)
2458 printf (_("architecture: %s, "),
2459 bfd_printable_arch_mach (bfd_get_arch (abfd
),
2460 bfd_get_mach (abfd
)));
2461 printf (_("flags 0x%08x:\n"), abfd
->flags
);
2463 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2464 PF (HAS_RELOC
, "HAS_RELOC");
2465 PF (EXEC_P
, "EXEC_P");
2466 PF (HAS_LINENO
, "HAS_LINENO");
2467 PF (HAS_DEBUG
, "HAS_DEBUG");
2468 PF (HAS_SYMS
, "HAS_SYMS");
2469 PF (HAS_LOCALS
, "HAS_LOCALS");
2470 PF (DYNAMIC
, "DYNAMIC");
2471 PF (WP_TEXT
, "WP_TEXT");
2472 PF (D_PAGED
, "D_PAGED");
2473 PF (BFD_IS_RELAXABLE
, "BFD_IS_RELAXABLE");
2474 PF (HAS_LOAD_PAGE
, "HAS_LOAD_PAGE");
2475 printf (_("\nstart address 0x"));
2476 bfd_printf_vma (abfd
, abfd
->start_address
);
2482 dump_bfd_private_header (bfd
*abfd
)
2484 bfd_print_private_bfd_data (abfd
, stdout
);
2488 /* Display a section in hexadecimal format with associated characters.
2489 Each line prefixed by the zero padded address. */
2492 dump_section (bfd
*abfd
, asection
*section
, void *dummy ATTRIBUTE_UNUSED
)
2495 bfd_size_type datasize
;
2496 bfd_size_type addr_offset
;
2497 bfd_size_type start_offset
;
2498 bfd_size_type stop_offset
;
2499 unsigned int opb
= bfd_octets_per_byte (abfd
);
2500 /* Bytes per line. */
2501 const int onaline
= 16;
2506 if ((section
->flags
& SEC_HAS_CONTENTS
) == 0)
2509 if (! process_section_p (section
))
2512 if ((datasize
= bfd_section_size (abfd
, section
)) == 0)
2515 /* Compute the address range to display. */
2516 if (start_address
== (bfd_vma
) -1
2517 || start_address
< section
->vma
)
2520 start_offset
= start_address
- section
->vma
;
2522 if (stop_address
== (bfd_vma
) -1)
2523 stop_offset
= datasize
/ opb
;
2526 if (stop_address
< section
->vma
)
2529 stop_offset
= stop_address
- section
->vma
;
2531 if (stop_offset
> datasize
/ opb
)
2532 stop_offset
= datasize
/ opb
;
2535 if (start_offset
>= stop_offset
)
2538 printf (_("Contents of section %s:"), section
->name
);
2539 if (display_file_offsets
)
2540 printf (_(" (Starting at file offset: 0x%lx)"),
2541 (unsigned long) (section
->filepos
+ start_offset
));
2544 data
= xmalloc (datasize
);
2546 bfd_get_section_contents (abfd
, section
, data
, 0, datasize
);
2550 bfd_sprintf_vma (abfd
, buf
, start_offset
+ section
->vma
);
2551 if (strlen (buf
) >= sizeof (buf
))
2555 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
2557 count
= strlen (buf
) - count
;
2561 bfd_sprintf_vma (abfd
, buf
, stop_offset
+ section
->vma
- 1);
2562 if (strlen (buf
) >= sizeof (buf
))
2566 while (buf
[count
] == '0' && buf
[count
+1] != '\0')
2568 count
= strlen (buf
) - count
;
2572 for (addr_offset
= start_offset
;
2573 addr_offset
< stop_offset
; addr_offset
+= onaline
/ opb
)
2577 bfd_sprintf_vma (abfd
, buf
, (addr_offset
+ section
->vma
));
2578 count
= strlen (buf
);
2579 if ((size_t) count
>= sizeof (buf
))
2583 while (count
< width
)
2588 fputs (buf
+ count
- width
, stdout
);
2591 for (j
= addr_offset
* opb
;
2592 j
< addr_offset
* opb
+ onaline
; j
++)
2594 if (j
< stop_offset
* opb
)
2595 printf ("%02x", (unsigned) (data
[j
]));
2603 for (j
= addr_offset
* opb
;
2604 j
< addr_offset
* opb
+ onaline
; j
++)
2606 if (j
>= stop_offset
* opb
)
2609 printf ("%c", ISPRINT (data
[j
]) ? data
[j
] : '.');
2616 /* Actually display the various requested regions. */
2619 dump_data (bfd
*abfd
)
2621 bfd_map_over_sections (abfd
, dump_section
, NULL
);
2624 /* Should perhaps share code and display with nm? */
2627 dump_symbols (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_boolean dynamic
)
2637 printf ("DYNAMIC SYMBOL TABLE:\n");
2643 printf ("SYMBOL TABLE:\n");
2647 printf (_("no symbols\n"));
2649 for (count
= 0; count
< max
; count
++)
2653 if (*current
== NULL
)
2654 printf (_("no information for symbol number %ld\n"), count
);
2656 else if ((cur_bfd
= bfd_asymbol_bfd (*current
)) == NULL
)
2657 printf (_("could not determine the type of symbol number %ld\n"),
2660 else if (process_section_p ((* current
)->section
)
2661 && (dump_special_syms
2662 || !bfd_is_target_special_symbol (cur_bfd
, *current
)))
2664 const char *name
= (*current
)->name
;
2666 if (do_demangle
&& name
!= NULL
&& *name
!= '\0')
2670 /* If we want to demangle the name, we demangle it
2671 here, and temporarily clobber it while calling
2672 bfd_print_symbol. FIXME: This is a gross hack. */
2673 alloc
= bfd_demangle (cur_bfd
, name
, DMGL_ANSI
| DMGL_PARAMS
);
2675 (*current
)->name
= alloc
;
2676 bfd_print_symbol (cur_bfd
, stdout
, *current
,
2677 bfd_print_symbol_all
);
2680 (*current
)->name
= name
;
2685 bfd_print_symbol (cur_bfd
, stdout
, *current
,
2686 bfd_print_symbol_all
);
2696 dump_reloc_set (bfd
*abfd
, asection
*sec
, arelent
**relpp
, long relcount
)
2699 char *last_filename
, *last_functionname
;
2700 unsigned int last_line
;
2702 /* Get column headers lined up reasonably. */
2710 bfd_sprintf_vma (abfd
, buf
, (bfd_vma
) -1);
2711 width
= strlen (buf
) - 7;
2713 printf ("OFFSET %*s TYPE %*s VALUE \n", width
, "", 12, "");
2716 last_filename
= NULL
;
2717 last_functionname
= NULL
;
2720 for (p
= relpp
; relcount
&& *p
!= NULL
; p
++, relcount
--)
2723 const char *filename
, *functionname
;
2725 const char *sym_name
;
2726 const char *section_name
;
2728 if (start_address
!= (bfd_vma
) -1
2729 && q
->address
< start_address
)
2731 if (stop_address
!= (bfd_vma
) -1
2732 && q
->address
> stop_address
)
2735 if (with_line_numbers
2737 && bfd_find_nearest_line (abfd
, sec
, syms
, q
->address
,
2738 &filename
, &functionname
, &line
))
2740 if (functionname
!= NULL
2741 && (last_functionname
== NULL
2742 || strcmp (functionname
, last_functionname
) != 0))
2744 printf ("%s():\n", functionname
);
2745 if (last_functionname
!= NULL
)
2746 free (last_functionname
);
2747 last_functionname
= xstrdup (functionname
);
2751 && (line
!= last_line
2752 || (filename
!= NULL
2753 && last_filename
!= NULL
2754 && strcmp (filename
, last_filename
) != 0)))
2756 printf ("%s:%u\n", filename
== NULL
? "???" : filename
, line
);
2758 if (last_filename
!= NULL
)
2759 free (last_filename
);
2760 if (filename
== NULL
)
2761 last_filename
= NULL
;
2763 last_filename
= xstrdup (filename
);
2767 if (q
->sym_ptr_ptr
&& *q
->sym_ptr_ptr
)
2769 sym_name
= (*(q
->sym_ptr_ptr
))->name
;
2770 section_name
= (*(q
->sym_ptr_ptr
))->section
->name
;
2775 section_name
= NULL
;
2778 bfd_printf_vma (abfd
, q
->address
);
2779 if (q
->howto
== NULL
)
2780 printf (" *unknown* ");
2781 else if (q
->howto
->name
)
2782 printf (" %-16s ", q
->howto
->name
);
2784 printf (" %-16d ", q
->howto
->type
);
2788 objdump_print_symname (abfd
, NULL
, *q
->sym_ptr_ptr
);
2792 if (section_name
== NULL
)
2793 section_name
= "*unknown*";
2794 printf ("[%s]", section_name
);
2800 bfd_printf_vma (abfd
, q
->addend
);
2808 dump_relocs_in_section (bfd
*abfd
,
2810 void *dummy ATTRIBUTE_UNUSED
)
2816 if ( bfd_is_abs_section (section
)
2817 || bfd_is_und_section (section
)
2818 || bfd_is_com_section (section
)
2819 || (! process_section_p (section
))
2820 || ((section
->flags
& SEC_RELOC
) == 0))
2823 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
2825 bfd_fatal (bfd_get_filename (abfd
));
2827 printf ("RELOCATION RECORDS FOR [%s]:", section
->name
);
2831 printf (" (none)\n\n");
2835 relpp
= xmalloc (relsize
);
2836 relcount
= bfd_canonicalize_reloc (abfd
, section
, relpp
, syms
);
2839 bfd_fatal (bfd_get_filename (abfd
));
2840 else if (relcount
== 0)
2841 printf (" (none)\n\n");
2845 dump_reloc_set (abfd
, section
, relpp
, relcount
);
2852 dump_relocs (bfd
*abfd
)
2854 bfd_map_over_sections (abfd
, dump_relocs_in_section
, NULL
);
2858 dump_dynamic_relocs (bfd
*abfd
)
2864 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
2866 bfd_fatal (bfd_get_filename (abfd
));
2868 printf ("DYNAMIC RELOCATION RECORDS");
2871 printf (" (none)\n\n");
2874 relpp
= xmalloc (relsize
);
2875 relcount
= bfd_canonicalize_dynamic_reloc (abfd
, relpp
, dynsyms
);
2878 bfd_fatal (bfd_get_filename (abfd
));
2879 else if (relcount
== 0)
2880 printf (" (none)\n\n");
2884 dump_reloc_set (abfd
, NULL
, relpp
, relcount
);
2891 /* Creates a table of paths, to search for source files. */
2894 add_include_path (const char *path
)
2898 include_path_count
++;
2899 include_paths
= xrealloc (include_paths
,
2900 include_path_count
* sizeof (*include_paths
));
2901 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2902 if (path
[1] == ':' && path
[2] == 0)
2903 path
= concat (path
, ".", (const char *) 0);
2905 include_paths
[include_path_count
- 1] = path
;
2909 adjust_addresses (bfd
*abfd ATTRIBUTE_UNUSED
,
2913 if ((section
->flags
& SEC_DEBUGGING
) == 0)
2915 bfd_boolean
*has_reloc_p
= (bfd_boolean
*) arg
;
2916 section
->vma
+= adjust_section_vma
;
2918 section
->lma
+= adjust_section_vma
;
2922 /* Dump selected contents of ABFD. */
2925 dump_bfd (bfd
*abfd
)
2927 /* If we are adjusting section VMA's, change them all now. Changing
2928 the BFD information is a hack. However, we must do it, or
2929 bfd_find_nearest_line will not do the right thing. */
2930 if (adjust_section_vma
!= 0)
2932 bfd_boolean has_reloc
= (abfd
->flags
& HAS_RELOC
);
2933 bfd_map_over_sections (abfd
, adjust_addresses
, &has_reloc
);
2936 if (! dump_debugging_tags
)
2937 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd
),
2940 print_arelt_descr (stdout
, abfd
, TRUE
);
2941 if (dump_file_header
)
2942 dump_bfd_header (abfd
);
2943 if (dump_private_headers
)
2944 dump_bfd_private_header (abfd
);
2945 if (! dump_debugging_tags
)
2947 if (dump_section_headers
)
2948 dump_headers (abfd
);
2954 || dump_dwarf_section_info
)
2955 syms
= slurp_symtab (abfd
);
2956 if (dump_dynamic_symtab
|| dump_dynamic_reloc_info
2957 || (disassemble
&& bfd_get_dynamic_symtab_upper_bound (abfd
) > 0))
2958 dynsyms
= slurp_dynamic_symtab (abfd
);
2961 synthcount
= bfd_get_synthetic_symtab (abfd
, symcount
, syms
,
2962 dynsymcount
, dynsyms
, &synthsyms
);
2968 dump_symbols (abfd
, FALSE
);
2969 if (dump_dynamic_symtab
)
2970 dump_symbols (abfd
, TRUE
);
2971 if (dump_dwarf_section_info
)
2973 if (dump_stab_section_info
)
2975 if (dump_reloc_info
&& ! disassemble
)
2977 if (dump_dynamic_reloc_info
&& ! disassemble
)
2978 dump_dynamic_relocs (abfd
);
2979 if (dump_section_contents
)
2982 disassemble_data (abfd
);
2988 dhandle
= read_debugging_info (abfd
, syms
, symcount
, TRUE
);
2989 if (dhandle
!= NULL
)
2991 if (!print_debugging_info (stdout
, dhandle
, abfd
, syms
,
2993 dump_debugging_tags
? TRUE
: FALSE
))
2995 non_fatal (_("%s: printing debugging information failed"),
2996 bfd_get_filename (abfd
));
3000 /* PR 6483: If there was no STABS or IEEE debug
3001 info in the file, try DWARF instead. */
3002 else if (! dump_dwarf_section_info
)
3032 display_bfd (bfd
*abfd
)
3036 if (bfd_check_format_matches (abfd
, bfd_object
, &matching
))
3042 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
3044 nonfatal (bfd_get_filename (abfd
));
3045 list_matching_formats (matching
);
3050 if (bfd_get_error () != bfd_error_file_not_recognized
)
3052 nonfatal (bfd_get_filename (abfd
));
3056 if (bfd_check_format_matches (abfd
, bfd_core
, &matching
))
3062 nonfatal (bfd_get_filename (abfd
));
3064 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
3066 list_matching_formats (matching
);
3072 display_file (char *filename
, char *target
)
3077 if (get_file_size (filename
) < 1)
3083 file
= bfd_openr (filename
, target
);
3086 nonfatal (filename
);
3090 /* If the file is an archive, process all of its elements. */
3091 if (bfd_check_format (file
, bfd_archive
))
3093 bfd
*last_arfile
= NULL
;
3095 printf (_("In archive %s:\n"), bfd_get_filename (file
));
3098 bfd_set_error (bfd_error_no_error
);
3100 arfile
= bfd_openr_next_archived_file (file
, arfile
);
3103 if (bfd_get_error () != bfd_error_no_more_archived_files
)
3104 nonfatal (bfd_get_filename (file
));
3108 display_bfd (arfile
);
3110 if (last_arfile
!= NULL
)
3111 bfd_close (last_arfile
);
3112 last_arfile
= arfile
;
3115 if (last_arfile
!= NULL
)
3116 bfd_close (last_arfile
);
3125 main (int argc
, char **argv
)
3128 char *target
= default_target
;
3129 bfd_boolean seenflag
= FALSE
;
3131 #if defined (HAVE_SETLOCALE)
3132 #if defined (HAVE_LC_MESSAGES)
3133 setlocale (LC_MESSAGES
, "");
3135 setlocale (LC_CTYPE
, "");
3138 bindtextdomain (PACKAGE
, LOCALEDIR
);
3139 textdomain (PACKAGE
);
3141 program_name
= *argv
;
3142 xmalloc_set_program_name (program_name
);
3144 START_PROGRESS (program_name
, 0);
3146 expandargv (&argc
, &argv
);
3149 set_default_bfd_target ();
3151 while ((c
= getopt_long (argc
, argv
, "pib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW",
3152 long_options
, (int *) 0))
3158 break; /* We've been given a long option. */
3163 if (disassembler_options
)
3164 /* Ignore potential memory leak for now. */
3165 disassembler_options
= concat (disassembler_options
, ",",
3166 optarg
, (const char *) NULL
);
3168 disassembler_options
= optarg
;
3171 if (only_used
== only_size
)
3174 only
= xrealloc (only
, only_size
* sizeof (char *));
3176 only
[only_used
++] = optarg
;
3179 display_file_offsets
= TRUE
;
3182 with_line_numbers
= TRUE
;
3191 enum demangling_styles style
;
3193 style
= cplus_demangle_name_to_style (optarg
);
3194 if (style
== unknown_demangling
)
3195 fatal (_("unknown demangling style `%s'"),
3198 cplus_demangle_set_style (style
);
3204 case OPTION_ADJUST_VMA
:
3205 adjust_section_vma
= parse_vma (optarg
, "--adjust-vma");
3207 case OPTION_START_ADDRESS
:
3208 start_address
= parse_vma (optarg
, "--start-address");
3209 if ((stop_address
!= (bfd_vma
) -1) && stop_address
<= start_address
)
3210 fatal (_("error: the start address should be before the end address"));
3212 case OPTION_STOP_ADDRESS
:
3213 stop_address
= parse_vma (optarg
, "--stop-address");
3214 if ((start_address
!= (bfd_vma
) -1) && stop_address
<= start_address
)
3215 fatal (_("error: the stop address should be after the start address"));
3219 prefix_length
= strlen (prefix
);
3220 /* Remove an unnecessary trailing '/' */
3221 while (IS_DIR_SEPARATOR (prefix
[prefix_length
- 1]))
3224 case OPTION_PREFIX_STRIP
:
3225 prefix_strip
= atoi (optarg
);
3226 if (prefix_strip
< 0)
3227 fatal (_("error: prefix strip must be non-negative"));
3230 if (strcmp (optarg
, "B") == 0)
3231 endian
= BFD_ENDIAN_BIG
;
3232 else if (strcmp (optarg
, "L") == 0)
3233 endian
= BFD_ENDIAN_LITTLE
;
3236 non_fatal (_("unrecognized -E option"));
3241 if (strncmp (optarg
, "big", strlen (optarg
)) == 0)
3242 endian
= BFD_ENDIAN_BIG
;
3243 else if (strncmp (optarg
, "little", strlen (optarg
)) == 0)
3244 endian
= BFD_ENDIAN_LITTLE
;
3247 non_fatal (_("unrecognized --endian type `%s'"), optarg
);
3253 dump_file_header
= TRUE
;
3257 formats_info
= TRUE
;
3261 add_include_path (optarg
);
3264 dump_private_headers
= TRUE
;
3268 dump_private_headers
= TRUE
;
3270 dump_reloc_info
= TRUE
;
3271 dump_file_header
= TRUE
;
3272 dump_ar_hdrs
= TRUE
;
3273 dump_section_headers
= TRUE
;
3281 dump_dynamic_symtab
= TRUE
;
3289 disassemble_zeroes
= TRUE
;
3293 disassemble_all
= TRUE
;
3298 with_source_code
= TRUE
;
3307 dump_debugging_tags
= 1;
3312 dump_dwarf_section_info
= TRUE
;
3315 do_debug_abbrevs
= 1;
3317 do_debug_pubnames
= 1;
3318 do_debug_aranges
= 1;
3319 do_debug_ranges
= 1;
3320 do_debug_frames
= 1;
3321 do_debug_macinfo
= 1;
3326 dump_stab_section_info
= TRUE
;
3330 dump_section_contents
= TRUE
;
3334 dump_reloc_info
= TRUE
;
3338 dump_dynamic_reloc_info
= TRUE
;
3342 dump_ar_hdrs
= TRUE
;
3346 dump_section_headers
= TRUE
;
3354 show_version
= TRUE
;
3364 print_version ("objdump");
3370 exit_status
= display_info ();
3374 display_file ("a.out", target
);
3376 for (; optind
< argc
;)
3377 display_file (argv
[optind
++], target
);
3380 END_PROGRESS (program_name
);