Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* objdump.c -- dump information about an object file. |
8c2bc687 | 2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
f0c8c24a | 3 | 2000, 2001, 2002, 2003 |
252b5132 RH |
4 | Free Software Foundation, Inc. |
5 | ||
b5e2a4f3 | 6 | This file is part of GNU Binutils. |
252b5132 | 7 | |
b5e2a4f3 NC |
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) | |
11 | any later version. | |
252b5132 | 12 | |
b5e2a4f3 NC |
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. | |
252b5132 | 17 | |
b5e2a4f3 NC |
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. */ | |
252b5132 RH |
21 | |
22 | #include "bfd.h" | |
6e1a7e9a | 23 | #include "bfdver.h" |
252b5132 RH |
24 | #include "progress.h" |
25 | #include "bucomm.h" | |
a6637ec0 | 26 | #include "budemang.h" |
d7a283d4 | 27 | #include "getopt.h" |
3882b010 | 28 | #include "safe-ctype.h" |
252b5132 RH |
29 | #include "dis-asm.h" |
30 | #include "libiberty.h" | |
31 | #include "demangle.h" | |
32 | #include "debug.h" | |
33 | #include "budbg.h" | |
34 | ||
252b5132 RH |
35 | /* Internal headers for the ELF .stab-dump code - sorry. */ |
36 | #define BYTES_IN_WORD 32 | |
37 | #include "aout/aout64.h" | |
38 | ||
39 | #ifdef NEED_DECLARATION_FPRINTF | |
40 | /* This is needed by INIT_DISASSEMBLE_INFO. */ | |
b34976b6 AM |
41 | extern int fprintf |
42 | PARAMS ((FILE *, const char *, ...)); | |
252b5132 RH |
43 | #endif |
44 | ||
75cd796a ILT |
45 | /* Exit status. */ |
46 | static int exit_status = 0; | |
47 | ||
98a91d6a | 48 | static char *default_target = NULL; /* Default at runtime. */ |
252b5132 | 49 | |
98a91d6a | 50 | static int show_version = 0; /* Show the version number. */ |
252b5132 RH |
51 | static int dump_section_contents; /* -s */ |
52 | static int dump_section_headers; /* -h */ | |
b34976b6 | 53 | static bfd_boolean dump_file_header; /* -f */ |
252b5132 RH |
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 */ | |
b34976b6 | 62 | static bfd_boolean with_source_code; /* -S */ |
252b5132 RH |
63 | static int show_raw_insn; /* --show-raw-insn */ |
64 | static int dump_stab_section_info; /* --stabs */ | |
65 | static int do_demangle; /* -C, --demangle */ | |
b34976b6 AM |
66 | static bfd_boolean disassemble; /* -d */ |
67 | static bfd_boolean disassemble_all; /* -D */ | |
252b5132 | 68 | static int disassemble_zeroes; /* --disassemble-zeroes */ |
b34976b6 | 69 | static bfd_boolean formats_info; /* -i */ |
6e50c90c L |
70 | static char **only; /* -j secname */ |
71 | static size_t only_size = 0; | |
72 | static size_t only_used = 0; | |
252b5132 RH |
73 | static int wide_output; /* -w */ |
74 | static bfd_vma start_address = (bfd_vma) -1; /* --start-address */ | |
75 | static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */ | |
76 | static int dump_debugging; /* --debugging */ | |
51cdc6e0 | 77 | static int dump_debugging_tags; /* --debugging-tags */ |
252b5132 | 78 | static bfd_vma adjust_section_vma = 0; /* --adjust-vma */ |
f1563258 | 79 | static int file_start_context = 0; /* --file-start-context */ |
252b5132 RH |
80 | |
81 | /* Extra info to pass to the disassembler address printing function. */ | |
026df7c5 NC |
82 | struct objdump_disasm_info |
83 | { | |
252b5132 RH |
84 | bfd *abfd; |
85 | asection *sec; | |
b34976b6 | 86 | bfd_boolean require_sec; |
252b5132 RH |
87 | }; |
88 | ||
89 | /* Architecture to disassemble for, or default if NULL. */ | |
90 | static char *machine = (char *) NULL; | |
91 | ||
dd92f639 NC |
92 | /* Target specific options to the disassembler. */ |
93 | static char *disassembler_options = (char *) NULL; | |
94 | ||
252b5132 RH |
95 | /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */ |
96 | static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN; | |
97 | ||
98 | /* The symbol table. */ | |
99 | static asymbol **syms; | |
100 | ||
101 | /* Number of symbols in `syms'. */ | |
102 | static long symcount = 0; | |
103 | ||
104 | /* The sorted symbol table. */ | |
105 | static asymbol **sorted_syms; | |
106 | ||
107 | /* Number of symbols in `sorted_syms'. */ | |
108 | static long sorted_symcount = 0; | |
109 | ||
110 | /* The dynamic symbol table. */ | |
111 | static asymbol **dynsyms; | |
112 | ||
113 | /* Number of symbols in `dynsyms'. */ | |
114 | static long dynsymcount = 0; | |
115 | ||
98a91d6a NC |
116 | static bfd_byte *stabs; |
117 | static bfd_size_type stab_size; | |
118 | ||
119 | static char *strtab; | |
120 | static bfd_size_type stabstr_size; | |
121 | ||
252b5132 RH |
122 | /* Static declarations. */ |
123 | ||
b34976b6 AM |
124 | static void usage |
125 | PARAMS ((FILE *, int)); | |
126 | static void nonfatal | |
127 | PARAMS ((const char *)); | |
128 | static void display_file | |
06d86cf7 | 129 | PARAMS ((char *, char *)); |
b34976b6 AM |
130 | static void dump_section_header |
131 | PARAMS ((bfd *, asection *, PTR)); | |
132 | static void dump_headers | |
133 | PARAMS ((bfd *)); | |
134 | static void dump_data | |
06d86cf7 | 135 | PARAMS ((bfd *)); |
b34976b6 | 136 | static void dump_relocs |
06d86cf7 | 137 | PARAMS ((bfd *)); |
b34976b6 | 138 | static void dump_dynamic_relocs |
06d86cf7 | 139 | PARAMS ((bfd *)); |
b34976b6 AM |
140 | static void dump_reloc_set |
141 | PARAMS ((bfd *, asection *, arelent **, long)); | |
142 | static void dump_symbols | |
06d86cf7 | 143 | PARAMS ((bfd *, bfd_boolean)); |
b34976b6 AM |
144 | static void dump_bfd_header |
145 | PARAMS ((bfd *)); | |
146 | static void dump_bfd_private_header | |
147 | PARAMS ((bfd *)); | |
148 | static void dump_bfd | |
149 | PARAMS ((bfd *)); | |
150 | static void display_bfd | |
06d86cf7 | 151 | PARAMS ((bfd *)); |
2863d58a | 152 | static void objdump_print_value |
b34976b6 | 153 | PARAMS ((bfd_vma, struct disassemble_info *, bfd_boolean)); |
2863d58a AM |
154 | static void objdump_print_symname |
155 | PARAMS ((bfd *, struct disassemble_info *, asymbol *)); | |
156 | static asymbol *find_symbol_for_address | |
b34976b6 | 157 | PARAMS ((bfd *, asection *, bfd_vma, bfd_boolean, long *)); |
2863d58a AM |
158 | static void objdump_print_addr_with_sym |
159 | PARAMS ((bfd *, asection *, asymbol *, bfd_vma, | |
b34976b6 | 160 | struct disassemble_info *, bfd_boolean)); |
2863d58a | 161 | static void objdump_print_addr |
b34976b6 | 162 | PARAMS ((bfd_vma, struct disassemble_info *, bfd_boolean)); |
2863d58a AM |
163 | static void objdump_print_address |
164 | PARAMS ((bfd_vma, struct disassemble_info *)); | |
165 | static int objdump_symbol_at_address | |
166 | PARAMS ((bfd_vma, struct disassemble_info *)); | |
b34976b6 AM |
167 | static void show_line |
168 | PARAMS ((bfd *, asection *, bfd_vma)); | |
2863d58a | 169 | static void disassemble_bytes |
b34976b6 | 170 | PARAMS ((struct disassemble_info *, disassembler_ftype, bfd_boolean, |
2863d58a | 171 | bfd_byte *, bfd_vma, bfd_vma, arelent ***, arelent **)); |
b34976b6 AM |
172 | static void disassemble_data |
173 | PARAMS ((bfd *)); | |
b34976b6 AM |
174 | static asymbol ** slurp_symtab |
175 | PARAMS ((bfd *)); | |
176 | static asymbol ** slurp_dynamic_symtab | |
177 | PARAMS ((bfd *)); | |
178 | static long remove_useless_symbols | |
179 | PARAMS ((asymbol **, long)); | |
180 | static int compare_symbols | |
181 | PARAMS ((const PTR, const PTR)); | |
182 | static int compare_relocs | |
183 | PARAMS ((const PTR, const PTR)); | |
184 | static void dump_stabs | |
185 | PARAMS ((bfd *)); | |
186 | static bfd_boolean read_section_stabs | |
187 | PARAMS ((bfd *, const char *, const char *)); | |
188 | static void print_section_stabs | |
189 | PARAMS ((bfd *, const char *, const char *)); | |
98a91d6a NC |
190 | static void dump_section_stabs |
191 | PARAMS ((bfd *, char *, char *)); | |
252b5132 RH |
192 | \f |
193 | static void | |
194 | usage (stream, status) | |
195 | FILE *stream; | |
196 | int status; | |
197 | { | |
8b53311e NC |
198 | fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name); |
199 | fprintf (stream, _(" Display information from object <file(s)>.\n")); | |
200 | fprintf (stream, _(" At least one of the following switches must be given:\n")); | |
252b5132 | 201 | fprintf (stream, _("\ |
86d65c94 MK |
202 | -a, --archive-headers Display archive header information\n\ |
203 | -f, --file-headers Display the contents of the overall file header\n\ | |
204 | -p, --private-headers Display object format specific file header contents\n\ | |
205 | -h, --[section-]headers Display the contents of the section headers\n\ | |
206 | -x, --all-headers Display the contents of all headers\n\ | |
207 | -d, --disassemble Display assembler contents of executable sections\n\ | |
208 | -D, --disassemble-all Display assembler contents of all sections\n\ | |
209 | -S, --source Intermix source code with disassembly\n\ | |
210 | -s, --full-contents Display the full contents of all sections requested\n\ | |
211 | -g, --debugging Display debug information in object file\n\ | |
51cdc6e0 | 212 | -e, --debugging-tags Display debug information using ctags style\n\ |
86d65c94 MK |
213 | -G, --stabs Display (in raw form) any STABS info in the file\n\ |
214 | -t, --syms Display the contents of the symbol table(s)\n\ | |
215 | -T, --dynamic-syms Display the contents of the dynamic symbol table\n\ | |
216 | -r, --reloc Display the relocation entries in the file\n\ | |
217 | -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\ | |
8b53311e | 218 | -v, --version Display this program's version number\n\ |
86d65c94 MK |
219 | -i, --info List object formats and architectures supported\n\ |
220 | -H, --help Display this information\n\ | |
1dada9c5 NC |
221 | ")); |
222 | if (status != 2) | |
223 | { | |
224 | fprintf (stream, _("\n The following switches are optional:\n")); | |
225 | fprintf (stream, _("\ | |
86d65c94 MK |
226 | -b, --target=BFDNAME Specify the target object format as BFDNAME\n\ |
227 | -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\ | |
228 | -j, --section=NAME Only display information for section NAME\n\ | |
229 | -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\ | |
1dada9c5 NC |
230 | -EB --endian=big Assume big endian format when disassembling\n\ |
231 | -EL --endian=little Assume little endian format when disassembling\n\ | |
f1563258 | 232 | --file-start-context Include context from start of file (with -S)\n\ |
86d65c94 | 233 | -l, --line-numbers Include line numbers and filenames in output\n\ |
28c309a2 | 234 | -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\ |
f0c8c24a NC |
235 | The STYLE, if specified, can be `auto', `gnu',\n\ |
236 | `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\ | |
237 | or `gnat'\n\ | |
86d65c94 MK |
238 | -w, --wide Format output for more than 80 columns\n\ |
239 | -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\ | |
f0c8c24a NC |
240 | --start-address=ADDR Only process data whose address is >= ADDR\n\ |
241 | --stop-address=ADDR Only process data whose address is <= ADDR\n\ | |
1dada9c5 NC |
242 | --prefix-addresses Print complete address alongside disassembly\n\ |
243 | --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\ | |
86d65c94 | 244 | --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\ |
1dada9c5 NC |
245 | \n")); |
246 | list_supported_targets (program_name, stream); | |
2f83960e | 247 | list_supported_architectures (program_name, stream); |
86d65c94 | 248 | |
94470b23 | 249 | disassembler_usage (stream); |
1dada9c5 | 250 | } |
252b5132 | 251 | if (status == 0) |
86d65c94 | 252 | fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO); |
252b5132 RH |
253 | exit (status); |
254 | } | |
255 | ||
256 | /* 150 isn't special; it's just an arbitrary non-ASCII char value. */ | |
257 | ||
258 | #define OPTION_ENDIAN (150) | |
259 | #define OPTION_START_ADDRESS (OPTION_ENDIAN + 1) | |
260 | #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1) | |
261 | #define OPTION_ADJUST_VMA (OPTION_STOP_ADDRESS + 1) | |
262 | ||
263 | static struct option long_options[]= | |
264 | { | |
265 | {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA}, | |
266 | {"all-headers", no_argument, NULL, 'x'}, | |
267 | {"private-headers", no_argument, NULL, 'p'}, | |
268 | {"architecture", required_argument, NULL, 'm'}, | |
269 | {"archive-headers", no_argument, NULL, 'a'}, | |
1dada9c5 | 270 | {"debugging", no_argument, NULL, 'g'}, |
51cdc6e0 | 271 | {"debugging-tags", no_argument, NULL, 'e'}, |
28c309a2 | 272 | {"demangle", optional_argument, NULL, 'C'}, |
252b5132 RH |
273 | {"disassemble", no_argument, NULL, 'd'}, |
274 | {"disassemble-all", no_argument, NULL, 'D'}, | |
dd92f639 | 275 | {"disassembler-options", required_argument, NULL, 'M'}, |
1dada9c5 | 276 | {"disassemble-zeroes", no_argument, NULL, 'z'}, |
252b5132 RH |
277 | {"dynamic-reloc", no_argument, NULL, 'R'}, |
278 | {"dynamic-syms", no_argument, NULL, 'T'}, | |
279 | {"endian", required_argument, NULL, OPTION_ENDIAN}, | |
280 | {"file-headers", no_argument, NULL, 'f'}, | |
f1563258 | 281 | {"file-start-context", no_argument, &file_start_context, 1}, |
252b5132 RH |
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'}, | |
1dada9c5 | 294 | {"stabs", no_argument, NULL, 'G'}, |
252b5132 RH |
295 | {"start-address", required_argument, NULL, OPTION_START_ADDRESS}, |
296 | {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS}, | |
297 | {"syms", no_argument, NULL, 't'}, | |
298 | {"target", required_argument, NULL, 'b'}, | |
1dada9c5 NC |
299 | {"version", no_argument, NULL, 'V'}, |
300 | {"wide", no_argument, NULL, 'w'}, | |
252b5132 RH |
301 | {0, no_argument, 0, 0} |
302 | }; | |
303 | \f | |
304 | static void | |
75cd796a ILT |
305 | nonfatal (msg) |
306 | const char *msg; | |
307 | { | |
308 | bfd_nonfatal (msg); | |
309 | exit_status = 1; | |
310 | } | |
311 | \f | |
312 | static void | |
252b5132 | 313 | dump_section_header (abfd, section, ignored) |
b4c96d0d | 314 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 | 315 | asection *section; |
b4c96d0d | 316 | PTR ignored ATTRIBUTE_UNUSED; |
252b5132 RH |
317 | { |
318 | char *comma = ""; | |
f6af82bd | 319 | unsigned int opb = bfd_octets_per_byte (abfd); |
252b5132 RH |
320 | |
321 | printf ("%3d %-13s %08lx ", section->index, | |
322 | bfd_get_section_name (abfd, section), | |
940b2b78 | 323 | (unsigned long) bfd_section_size (abfd, section) / opb); |
d8180c76 | 324 | bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section)); |
252b5132 | 325 | printf (" "); |
d8180c76 | 326 | bfd_printf_vma (abfd, section->lma); |
e59b4dfb | 327 | printf (" %08lx 2**%u", (unsigned long) section->filepos, |
252b5132 RH |
328 | bfd_get_section_alignment (abfd, section)); |
329 | if (! wide_output) | |
330 | printf ("\n "); | |
331 | printf (" "); | |
332 | ||
333 | #define PF(x, y) \ | |
334 | if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; } | |
335 | ||
336 | PF (SEC_HAS_CONTENTS, "CONTENTS"); | |
337 | PF (SEC_ALLOC, "ALLOC"); | |
338 | PF (SEC_CONSTRUCTOR, "CONSTRUCTOR"); | |
252b5132 RH |
339 | PF (SEC_LOAD, "LOAD"); |
340 | PF (SEC_RELOC, "RELOC"); | |
252b5132 RH |
341 | PF (SEC_READONLY, "READONLY"); |
342 | PF (SEC_CODE, "CODE"); | |
343 | PF (SEC_DATA, "DATA"); | |
344 | PF (SEC_ROM, "ROM"); | |
345 | PF (SEC_DEBUGGING, "DEBUGGING"); | |
346 | PF (SEC_NEVER_LOAD, "NEVER_LOAD"); | |
347 | PF (SEC_EXCLUDE, "EXCLUDE"); | |
348 | PF (SEC_SORT_ENTRIES, "SORT_ENTRIES"); | |
34cbe64e TW |
349 | PF (SEC_BLOCK, "BLOCK"); |
350 | PF (SEC_CLINK, "CLINK"); | |
24c411ed ILT |
351 | PF (SEC_SMALL_DATA, "SMALL_DATA"); |
352 | PF (SEC_SHARED, "SHARED"); | |
9e85c2a0 | 353 | PF (SEC_ARCH_BIT_0, "ARCH_BIT_0"); |
13ae64f3 | 354 | PF (SEC_THREAD_LOCAL, "THREAD_LOCAL"); |
252b5132 RH |
355 | |
356 | if ((section->flags & SEC_LINK_ONCE) != 0) | |
357 | { | |
358 | const char *ls; | |
359 | ||
360 | switch (section->flags & SEC_LINK_DUPLICATES) | |
361 | { | |
362 | default: | |
363 | abort (); | |
364 | case SEC_LINK_DUPLICATES_DISCARD: | |
365 | ls = "LINK_ONCE_DISCARD"; | |
366 | break; | |
367 | case SEC_LINK_DUPLICATES_ONE_ONLY: | |
368 | ls = "LINK_ONCE_ONE_ONLY"; | |
369 | break; | |
370 | case SEC_LINK_DUPLICATES_SAME_SIZE: | |
371 | ls = "LINK_ONCE_SAME_SIZE"; | |
372 | break; | |
373 | case SEC_LINK_DUPLICATES_SAME_CONTENTS: | |
374 | ls = "LINK_ONCE_SAME_CONTENTS"; | |
375 | break; | |
376 | } | |
377 | printf ("%s%s", comma, ls); | |
deecf979 ILT |
378 | |
379 | if (section->comdat != NULL) | |
380 | printf (" (COMDAT %s %ld)", section->comdat->name, | |
381 | section->comdat->symbol); | |
382 | ||
252b5132 RH |
383 | comma = ", "; |
384 | } | |
385 | ||
386 | printf ("\n"); | |
387 | #undef PF | |
388 | } | |
389 | ||
390 | static void | |
391 | dump_headers (abfd) | |
392 | bfd *abfd; | |
393 | { | |
394 | printf (_("Sections:\n")); | |
8bea4d5c | 395 | |
252b5132 | 396 | #ifndef BFD64 |
8bea4d5c | 397 | printf (_("Idx Name Size VMA LMA File off Algn")); |
252b5132 | 398 | #else |
21611032 TS |
399 | /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */ |
400 | if (bfd_get_arch_size (abfd) == 32) | |
401 | printf (_("Idx Name Size VMA LMA File off Algn")); | |
402 | else | |
403 | printf (_("Idx Name Size VMA LMA File off Algn")); | |
252b5132 | 404 | #endif |
8bea4d5c ILT |
405 | |
406 | if (wide_output) | |
407 | printf (_(" Flags")); | |
026df7c5 NC |
408 | if (abfd->flags & HAS_LOAD_PAGE) |
409 | printf (_(" Pg")); | |
8bea4d5c ILT |
410 | printf ("\n"); |
411 | ||
252b5132 RH |
412 | bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL); |
413 | } | |
414 | \f | |
415 | static asymbol ** | |
416 | slurp_symtab (abfd) | |
417 | bfd *abfd; | |
418 | { | |
419 | asymbol **sy = (asymbol **) NULL; | |
420 | long storage; | |
421 | ||
422 | if (!(bfd_get_file_flags (abfd) & HAS_SYMS)) | |
423 | { | |
252b5132 RH |
424 | symcount = 0; |
425 | return NULL; | |
426 | } | |
427 | ||
428 | storage = bfd_get_symtab_upper_bound (abfd); | |
429 | if (storage < 0) | |
430 | bfd_fatal (bfd_get_filename (abfd)); | |
252b5132 | 431 | if (storage) |
28b18af1 AM |
432 | sy = (asymbol **) xmalloc (storage); |
433 | ||
252b5132 RH |
434 | symcount = bfd_canonicalize_symtab (abfd, sy); |
435 | if (symcount < 0) | |
436 | bfd_fatal (bfd_get_filename (abfd)); | |
252b5132 RH |
437 | return sy; |
438 | } | |
439 | ||
440 | /* Read in the dynamic symbols. */ | |
441 | ||
442 | static asymbol ** | |
443 | slurp_dynamic_symtab (abfd) | |
444 | bfd *abfd; | |
445 | { | |
446 | asymbol **sy = (asymbol **) NULL; | |
447 | long storage; | |
448 | ||
449 | storage = bfd_get_dynamic_symtab_upper_bound (abfd); | |
450 | if (storage < 0) | |
451 | { | |
452 | if (!(bfd_get_file_flags (abfd) & DYNAMIC)) | |
453 | { | |
37cc8ec1 | 454 | non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd)); |
252b5132 RH |
455 | dynsymcount = 0; |
456 | return NULL; | |
457 | } | |
458 | ||
459 | bfd_fatal (bfd_get_filename (abfd)); | |
460 | } | |
252b5132 | 461 | if (storage) |
28b18af1 AM |
462 | sy = (asymbol **) xmalloc (storage); |
463 | ||
252b5132 RH |
464 | dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy); |
465 | if (dynsymcount < 0) | |
466 | bfd_fatal (bfd_get_filename (abfd)); | |
252b5132 RH |
467 | return sy; |
468 | } | |
469 | ||
470 | /* Filter out (in place) symbols that are useless for disassembly. | |
471 | COUNT is the number of elements in SYMBOLS. | |
0af11b59 | 472 | Return the number of useful symbols. */ |
252b5132 RH |
473 | |
474 | static long | |
475 | remove_useless_symbols (symbols, count) | |
476 | asymbol **symbols; | |
477 | long count; | |
478 | { | |
479 | register asymbol **in_ptr = symbols, **out_ptr = symbols; | |
480 | ||
481 | while (--count >= 0) | |
482 | { | |
483 | asymbol *sym = *in_ptr++; | |
484 | ||
485 | if (sym->name == NULL || sym->name[0] == '\0') | |
486 | continue; | |
487 | if (sym->flags & (BSF_DEBUGGING)) | |
488 | continue; | |
489 | if (bfd_is_und_section (sym->section) | |
490 | || bfd_is_com_section (sym->section)) | |
491 | continue; | |
492 | ||
493 | *out_ptr++ = sym; | |
494 | } | |
495 | return out_ptr - symbols; | |
496 | } | |
497 | ||
498 | /* Sort symbols into value order. */ | |
499 | ||
0af11b59 | 500 | static int |
252b5132 RH |
501 | compare_symbols (ap, bp) |
502 | const PTR ap; | |
503 | const PTR bp; | |
504 | { | |
505 | const asymbol *a = *(const asymbol **)ap; | |
506 | const asymbol *b = *(const asymbol **)bp; | |
507 | const char *an, *bn; | |
508 | size_t anl, bnl; | |
b34976b6 | 509 | bfd_boolean af, bf; |
252b5132 RH |
510 | flagword aflags, bflags; |
511 | ||
512 | if (bfd_asymbol_value (a) > bfd_asymbol_value (b)) | |
513 | return 1; | |
514 | else if (bfd_asymbol_value (a) < bfd_asymbol_value (b)) | |
515 | return -1; | |
516 | ||
517 | if (a->section > b->section) | |
518 | return 1; | |
519 | else if (a->section < b->section) | |
520 | return -1; | |
521 | ||
522 | an = bfd_asymbol_name (a); | |
523 | bn = bfd_asymbol_name (b); | |
524 | anl = strlen (an); | |
525 | bnl = strlen (bn); | |
526 | ||
527 | /* The symbols gnu_compiled and gcc2_compiled convey no real | |
528 | information, so put them after other symbols with the same value. */ | |
252b5132 RH |
529 | af = (strstr (an, "gnu_compiled") != NULL |
530 | || strstr (an, "gcc2_compiled") != NULL); | |
531 | bf = (strstr (bn, "gnu_compiled") != NULL | |
532 | || strstr (bn, "gcc2_compiled") != NULL); | |
533 | ||
534 | if (af && ! bf) | |
535 | return 1; | |
536 | if (! af && bf) | |
537 | return -1; | |
538 | ||
539 | /* We use a heuristic for the file name, to try to sort it after | |
540 | more useful symbols. It may not work on non Unix systems, but it | |
541 | doesn't really matter; the only difference is precisely which | |
542 | symbol names get printed. */ | |
543 | ||
544 | #define file_symbol(s, sn, snl) \ | |
545 | (((s)->flags & BSF_FILE) != 0 \ | |
546 | || ((sn)[(snl) - 2] == '.' \ | |
547 | && ((sn)[(snl) - 1] == 'o' \ | |
548 | || (sn)[(snl) - 1] == 'a'))) | |
549 | ||
550 | af = file_symbol (a, an, anl); | |
551 | bf = file_symbol (b, bn, bnl); | |
552 | ||
553 | if (af && ! bf) | |
554 | return 1; | |
555 | if (! af && bf) | |
556 | return -1; | |
557 | ||
558 | /* Try to sort global symbols before local symbols before function | |
559 | symbols before debugging symbols. */ | |
560 | ||
561 | aflags = a->flags; | |
562 | bflags = b->flags; | |
563 | ||
564 | if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING)) | |
565 | { | |
566 | if ((aflags & BSF_DEBUGGING) != 0) | |
567 | return 1; | |
568 | else | |
569 | return -1; | |
570 | } | |
571 | if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION)) | |
572 | { | |
573 | if ((aflags & BSF_FUNCTION) != 0) | |
574 | return -1; | |
575 | else | |
576 | return 1; | |
577 | } | |
578 | if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL)) | |
579 | { | |
580 | if ((aflags & BSF_LOCAL) != 0) | |
581 | return 1; | |
582 | else | |
583 | return -1; | |
584 | } | |
585 | if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL)) | |
586 | { | |
587 | if ((aflags & BSF_GLOBAL) != 0) | |
588 | return -1; | |
589 | else | |
590 | return 1; | |
591 | } | |
592 | ||
593 | /* Symbols that start with '.' might be section names, so sort them | |
594 | after symbols that don't start with '.'. */ | |
595 | if (an[0] == '.' && bn[0] != '.') | |
596 | return 1; | |
597 | if (an[0] != '.' && bn[0] == '.') | |
598 | return -1; | |
599 | ||
600 | /* Finally, if we can't distinguish them in any other way, try to | |
601 | get consistent results by sorting the symbols by name. */ | |
602 | return strcmp (an, bn); | |
603 | } | |
604 | ||
605 | /* Sort relocs into address order. */ | |
606 | ||
607 | static int | |
608 | compare_relocs (ap, bp) | |
609 | const PTR ap; | |
610 | const PTR bp; | |
611 | { | |
612 | const arelent *a = *(const arelent **)ap; | |
613 | const arelent *b = *(const arelent **)bp; | |
614 | ||
615 | if (a->address > b->address) | |
616 | return 1; | |
617 | else if (a->address < b->address) | |
618 | return -1; | |
619 | ||
620 | /* So that associated relocations tied to the same address show up | |
621 | in the correct order, we don't do any further sorting. */ | |
622 | if (a > b) | |
623 | return 1; | |
624 | else if (a < b) | |
625 | return -1; | |
626 | else | |
627 | return 0; | |
628 | } | |
629 | ||
b34976b6 | 630 | /* Print VMA to STREAM. If SKIP_ZEROES is TRUE, omit leading zeroes. */ |
252b5132 RH |
631 | |
632 | static void | |
633 | objdump_print_value (vma, info, skip_zeroes) | |
634 | bfd_vma vma; | |
635 | struct disassemble_info *info; | |
b34976b6 | 636 | bfd_boolean skip_zeroes; |
252b5132 RH |
637 | { |
638 | char buf[30]; | |
639 | char *p; | |
d8180c76 L |
640 | struct objdump_disasm_info *aux |
641 | = (struct objdump_disasm_info *) info->application_data; | |
252b5132 | 642 | |
d8180c76 | 643 | bfd_sprintf_vma (aux->abfd, buf, vma); |
252b5132 RH |
644 | if (! skip_zeroes) |
645 | p = buf; | |
646 | else | |
647 | { | |
648 | for (p = buf; *p == '0'; ++p) | |
649 | ; | |
650 | if (*p == '\0') | |
651 | --p; | |
652 | } | |
653 | (*info->fprintf_func) (info->stream, "%s", p); | |
654 | } | |
655 | ||
656 | /* Print the name of a symbol. */ | |
657 | ||
658 | static void | |
659 | objdump_print_symname (abfd, info, sym) | |
660 | bfd *abfd; | |
661 | struct disassemble_info *info; | |
662 | asymbol *sym; | |
663 | { | |
664 | char *alloc; | |
665 | const char *name; | |
252b5132 RH |
666 | |
667 | alloc = NULL; | |
668 | name = bfd_asymbol_name (sym); | |
a6637ec0 | 669 | if (do_demangle && name[0] != '\0') |
252b5132 RH |
670 | { |
671 | /* Demangle the name. */ | |
a6637ec0 AM |
672 | alloc = demangle (abfd, name); |
673 | name = alloc; | |
252b5132 RH |
674 | } |
675 | ||
676 | if (info != NULL) | |
a6637ec0 | 677 | (*info->fprintf_func) (info->stream, "%s", name); |
252b5132 | 678 | else |
a6637ec0 | 679 | printf ("%s", name); |
252b5132 RH |
680 | |
681 | if (alloc != NULL) | |
682 | free (alloc); | |
683 | } | |
684 | ||
685 | /* Locate a symbol given a bfd, a section, and a VMA. If REQUIRE_SEC | |
b34976b6 | 686 | is TRUE, then always require the symbol to be in the section. This |
252b5132 RH |
687 | returns NULL if there is no suitable symbol. If PLACE is not NULL, |
688 | then *PLACE is set to the index of the symbol in sorted_syms. */ | |
689 | ||
690 | static asymbol * | |
691 | find_symbol_for_address (abfd, sec, vma, require_sec, place) | |
692 | bfd *abfd; | |
693 | asection *sec; | |
694 | bfd_vma vma; | |
b34976b6 | 695 | bfd_boolean require_sec; |
252b5132 RH |
696 | long *place; |
697 | { | |
698 | /* @@ Would it speed things up to cache the last two symbols returned, | |
699 | and maybe their address ranges? For many processors, only one memory | |
700 | operand can be present at a time, so the 2-entry cache wouldn't be | |
701 | constantly churned by code doing heavy memory accesses. */ | |
702 | ||
703 | /* Indices in `sorted_syms'. */ | |
704 | long min = 0; | |
705 | long max = sorted_symcount; | |
706 | long thisplace; | |
0af11b59 | 707 | unsigned int opb = bfd_octets_per_byte (abfd); |
252b5132 RH |
708 | |
709 | if (sorted_symcount < 1) | |
710 | return NULL; | |
711 | ||
712 | /* Perform a binary search looking for the closest symbol to the | |
713 | required value. We are searching the range (min, max]. */ | |
714 | while (min + 1 < max) | |
715 | { | |
716 | asymbol *sym; | |
717 | ||
718 | thisplace = (max + min) / 2; | |
719 | sym = sorted_syms[thisplace]; | |
720 | ||
721 | if (bfd_asymbol_value (sym) > vma) | |
722 | max = thisplace; | |
723 | else if (bfd_asymbol_value (sym) < vma) | |
724 | min = thisplace; | |
725 | else | |
726 | { | |
727 | min = thisplace; | |
728 | break; | |
729 | } | |
730 | } | |
731 | ||
732 | /* The symbol we want is now in min, the low end of the range we | |
733 | were searching. If there are several symbols with the same | |
734 | value, we want the first one. */ | |
735 | thisplace = min; | |
736 | while (thisplace > 0 | |
737 | && (bfd_asymbol_value (sorted_syms[thisplace]) | |
738 | == bfd_asymbol_value (sorted_syms[thisplace - 1]))) | |
739 | --thisplace; | |
740 | ||
1049f94e | 741 | /* If the file is relocatable, and the symbol could be from this |
252b5132 RH |
742 | section, prefer a symbol from this section over symbols from |
743 | others, even if the other symbol's value might be closer. | |
0af11b59 | 744 | |
252b5132 RH |
745 | Note that this may be wrong for some symbol references if the |
746 | sections have overlapping memory ranges, but in that case there's | |
747 | no way to tell what's desired without looking at the relocation | |
748 | table. */ | |
252b5132 RH |
749 | if (sorted_syms[thisplace]->section != sec |
750 | && (require_sec | |
751 | || ((abfd->flags & HAS_RELOC) != 0 | |
752 | && vma >= bfd_get_section_vma (abfd, sec) | |
753 | && vma < (bfd_get_section_vma (abfd, sec) | |
940b2b78 | 754 | + bfd_section_size (abfd, sec) / opb)))) |
252b5132 RH |
755 | { |
756 | long i; | |
757 | ||
758 | for (i = thisplace + 1; i < sorted_symcount; i++) | |
759 | { | |
760 | if (bfd_asymbol_value (sorted_syms[i]) | |
761 | != bfd_asymbol_value (sorted_syms[thisplace])) | |
762 | break; | |
763 | } | |
98a91d6a | 764 | |
252b5132 | 765 | --i; |
98a91d6a | 766 | |
252b5132 RH |
767 | for (; i >= 0; i--) |
768 | { | |
769 | if (sorted_syms[i]->section == sec | |
770 | && (i == 0 | |
771 | || sorted_syms[i - 1]->section != sec | |
772 | || (bfd_asymbol_value (sorted_syms[i]) | |
773 | != bfd_asymbol_value (sorted_syms[i - 1])))) | |
774 | { | |
775 | thisplace = i; | |
776 | break; | |
777 | } | |
778 | } | |
779 | ||
780 | if (sorted_syms[thisplace]->section != sec) | |
781 | { | |
782 | /* We didn't find a good symbol with a smaller value. | |
783 | Look for one with a larger value. */ | |
784 | for (i = thisplace + 1; i < sorted_symcount; i++) | |
785 | { | |
786 | if (sorted_syms[i]->section == sec) | |
787 | { | |
788 | thisplace = i; | |
789 | break; | |
790 | } | |
791 | } | |
792 | } | |
793 | ||
794 | if (sorted_syms[thisplace]->section != sec | |
795 | && (require_sec | |
796 | || ((abfd->flags & HAS_RELOC) != 0 | |
797 | && vma >= bfd_get_section_vma (abfd, sec) | |
798 | && vma < (bfd_get_section_vma (abfd, sec) | |
799 | + bfd_section_size (abfd, sec))))) | |
800 | { | |
801 | /* There is no suitable symbol. */ | |
802 | return NULL; | |
803 | } | |
804 | } | |
805 | ||
806 | if (place != NULL) | |
807 | *place = thisplace; | |
808 | ||
809 | return sorted_syms[thisplace]; | |
810 | } | |
811 | ||
812 | /* Print an address to INFO symbolically. */ | |
813 | ||
814 | static void | |
815 | objdump_print_addr_with_sym (abfd, sec, sym, vma, info, skip_zeroes) | |
816 | bfd *abfd; | |
817 | asection *sec; | |
818 | asymbol *sym; | |
819 | bfd_vma vma; | |
820 | struct disassemble_info *info; | |
b34976b6 | 821 | bfd_boolean skip_zeroes; |
252b5132 RH |
822 | { |
823 | objdump_print_value (vma, info, skip_zeroes); | |
824 | ||
825 | if (sym == NULL) | |
826 | { | |
827 | bfd_vma secaddr; | |
828 | ||
829 | (*info->fprintf_func) (info->stream, " <%s", | |
830 | bfd_get_section_name (abfd, sec)); | |
831 | secaddr = bfd_get_section_vma (abfd, sec); | |
832 | if (vma < secaddr) | |
833 | { | |
834 | (*info->fprintf_func) (info->stream, "-0x"); | |
b34976b6 | 835 | objdump_print_value (secaddr - vma, info, TRUE); |
252b5132 RH |
836 | } |
837 | else if (vma > secaddr) | |
838 | { | |
839 | (*info->fprintf_func) (info->stream, "+0x"); | |
b34976b6 | 840 | objdump_print_value (vma - secaddr, info, TRUE); |
252b5132 RH |
841 | } |
842 | (*info->fprintf_func) (info->stream, ">"); | |
843 | } | |
844 | else | |
845 | { | |
846 | (*info->fprintf_func) (info->stream, " <"); | |
847 | objdump_print_symname (abfd, info, sym); | |
848 | if (bfd_asymbol_value (sym) > vma) | |
849 | { | |
850 | (*info->fprintf_func) (info->stream, "-0x"); | |
b34976b6 | 851 | objdump_print_value (bfd_asymbol_value (sym) - vma, info, TRUE); |
252b5132 RH |
852 | } |
853 | else if (vma > bfd_asymbol_value (sym)) | |
854 | { | |
855 | (*info->fprintf_func) (info->stream, "+0x"); | |
b34976b6 | 856 | objdump_print_value (vma - bfd_asymbol_value (sym), info, TRUE); |
252b5132 RH |
857 | } |
858 | (*info->fprintf_func) (info->stream, ">"); | |
859 | } | |
860 | } | |
861 | ||
862 | /* Print VMA to INFO, symbolically if possible. If SKIP_ZEROES is | |
b34976b6 | 863 | TRUE, don't output leading zeroes. */ |
252b5132 RH |
864 | |
865 | static void | |
866 | objdump_print_addr (vma, info, skip_zeroes) | |
867 | bfd_vma vma; | |
868 | struct disassemble_info *info; | |
b34976b6 | 869 | bfd_boolean skip_zeroes; |
252b5132 RH |
870 | { |
871 | struct objdump_disasm_info *aux; | |
872 | asymbol *sym; | |
873 | ||
874 | if (sorted_symcount < 1) | |
875 | { | |
876 | (*info->fprintf_func) (info->stream, "0x"); | |
877 | objdump_print_value (vma, info, skip_zeroes); | |
878 | return; | |
879 | } | |
880 | ||
881 | aux = (struct objdump_disasm_info *) info->application_data; | |
882 | sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec, | |
883 | (long *) NULL); | |
884 | objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, info, | |
885 | skip_zeroes); | |
886 | } | |
887 | ||
888 | /* Print VMA to INFO. This function is passed to the disassembler | |
889 | routine. */ | |
890 | ||
891 | static void | |
892 | objdump_print_address (vma, info) | |
893 | bfd_vma vma; | |
894 | struct disassemble_info *info; | |
895 | { | |
896 | objdump_print_addr (vma, info, ! prefix_addresses); | |
897 | } | |
898 | ||
899 | /* Determine of the given address has a symbol associated with it. */ | |
900 | ||
901 | static int | |
902 | objdump_symbol_at_address (vma, info) | |
903 | bfd_vma vma; | |
904 | struct disassemble_info * info; | |
905 | { | |
906 | struct objdump_disasm_info * aux; | |
907 | asymbol * sym; | |
908 | ||
909 | /* No symbols - do not bother checking. */ | |
910 | if (sorted_symcount < 1) | |
911 | return 0; | |
912 | ||
913 | aux = (struct objdump_disasm_info *) info->application_data; | |
914 | sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec, | |
915 | (long *) NULL); | |
916 | ||
917 | return (sym != NULL && (bfd_asymbol_value (sym) == vma)); | |
918 | } | |
919 | ||
920 | /* Hold the last function name and the last line number we displayed | |
921 | in a disassembly. */ | |
922 | ||
923 | static char *prev_functionname; | |
924 | static unsigned int prev_line; | |
925 | ||
926 | /* We keep a list of all files that we have seen when doing a | |
927 | dissassembly with source, so that we know how much of the file to | |
928 | display. This can be important for inlined functions. */ | |
929 | ||
930 | struct print_file_list | |
931 | { | |
932 | struct print_file_list *next; | |
933 | char *filename; | |
934 | unsigned int line; | |
935 | FILE *f; | |
936 | }; | |
937 | ||
938 | static struct print_file_list *print_files; | |
939 | ||
940 | /* The number of preceding context lines to show when we start | |
941 | displaying a file for the first time. */ | |
942 | ||
943 | #define SHOW_PRECEDING_CONTEXT_LINES (5) | |
944 | ||
945 | /* Skip ahead to a given line in a file, optionally printing each | |
946 | line. */ | |
947 | ||
b34976b6 AM |
948 | static void skip_to_line |
949 | PARAMS ((struct print_file_list *, unsigned int, bfd_boolean)); | |
252b5132 RH |
950 | |
951 | static void | |
952 | skip_to_line (p, line, show) | |
953 | struct print_file_list *p; | |
954 | unsigned int line; | |
b34976b6 | 955 | bfd_boolean show; |
252b5132 RH |
956 | { |
957 | while (p->line < line) | |
958 | { | |
959 | char buf[100]; | |
960 | ||
961 | if (fgets (buf, sizeof buf, p->f) == NULL) | |
962 | { | |
963 | fclose (p->f); | |
964 | p->f = NULL; | |
965 | break; | |
966 | } | |
967 | ||
968 | if (show) | |
969 | printf ("%s", buf); | |
970 | ||
971 | if (strchr (buf, '\n') != NULL) | |
972 | ++p->line; | |
973 | } | |
0af11b59 | 974 | } |
252b5132 RH |
975 | |
976 | /* Show the line number, or the source line, in a dissassembly | |
977 | listing. */ | |
978 | ||
979 | static void | |
940b2b78 | 980 | show_line (abfd, section, addr_offset) |
252b5132 RH |
981 | bfd *abfd; |
982 | asection *section; | |
940b2b78 | 983 | bfd_vma addr_offset; |
252b5132 | 984 | { |
b1f88ebe AM |
985 | const char *filename; |
986 | const char *functionname; | |
252b5132 RH |
987 | unsigned int line; |
988 | ||
989 | if (! with_line_numbers && ! with_source_code) | |
990 | return; | |
991 | ||
940b2b78 | 992 | if (! bfd_find_nearest_line (abfd, section, syms, addr_offset, &filename, |
252b5132 RH |
993 | &functionname, &line)) |
994 | return; | |
995 | ||
996 | if (filename != NULL && *filename == '\0') | |
997 | filename = NULL; | |
998 | if (functionname != NULL && *functionname == '\0') | |
999 | functionname = NULL; | |
1000 | ||
1001 | if (with_line_numbers) | |
1002 | { | |
1003 | if (functionname != NULL | |
1004 | && (prev_functionname == NULL | |
1005 | || strcmp (functionname, prev_functionname) != 0)) | |
1006 | printf ("%s():\n", functionname); | |
1007 | if (line > 0 && line != prev_line) | |
1008 | printf ("%s:%u\n", filename == NULL ? "???" : filename, line); | |
1009 | } | |
1010 | ||
1011 | if (with_source_code | |
1012 | && filename != NULL | |
1013 | && line > 0) | |
1014 | { | |
1015 | struct print_file_list **pp, *p; | |
1016 | ||
1017 | for (pp = &print_files; *pp != NULL; pp = &(*pp)->next) | |
1018 | if (strcmp ((*pp)->filename, filename) == 0) | |
1019 | break; | |
1020 | p = *pp; | |
1021 | ||
1022 | if (p != NULL) | |
1023 | { | |
1024 | if (p != print_files) | |
1025 | { | |
1026 | int l; | |
1027 | ||
1028 | /* We have reencountered a file name which we saw | |
1029 | earlier. This implies that either we are dumping out | |
1030 | code from an included file, or the same file was | |
1031 | linked in more than once. There are two common cases | |
1032 | of an included file: inline functions in a header | |
1033 | file, and a bison or flex skeleton file. In the | |
1034 | former case we want to just start printing (but we | |
1035 | back up a few lines to give context); in the latter | |
1036 | case we want to continue from where we left off. I | |
1037 | can't think of a good way to distinguish the cases, | |
1038 | so I used a heuristic based on the file name. */ | |
1039 | if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0) | |
1040 | l = p->line; | |
1041 | else | |
1042 | { | |
1043 | l = line - SHOW_PRECEDING_CONTEXT_LINES; | |
f1563258 TW |
1044 | if (l < 0) |
1045 | l = 0; | |
252b5132 RH |
1046 | } |
1047 | ||
1048 | if (p->f == NULL) | |
1049 | { | |
1050 | p->f = fopen (p->filename, "r"); | |
1051 | p->line = 0; | |
1052 | } | |
1053 | if (p->f != NULL) | |
b34976b6 | 1054 | skip_to_line (p, l, FALSE); |
252b5132 RH |
1055 | |
1056 | if (print_files->f != NULL) | |
1057 | { | |
1058 | fclose (print_files->f); | |
1059 | print_files->f = NULL; | |
1060 | } | |
1061 | } | |
1062 | ||
1063 | if (p->f != NULL) | |
1064 | { | |
b34976b6 | 1065 | skip_to_line (p, line, TRUE); |
252b5132 RH |
1066 | *pp = p->next; |
1067 | p->next = print_files; | |
1068 | print_files = p; | |
1069 | } | |
1070 | } | |
1071 | else | |
1072 | { | |
1073 | FILE *f; | |
1074 | ||
1075 | f = fopen (filename, "r"); | |
1076 | if (f != NULL) | |
1077 | { | |
1078 | int l; | |
1079 | ||
1080 | p = ((struct print_file_list *) | |
1081 | xmalloc (sizeof (struct print_file_list))); | |
1082 | p->filename = xmalloc (strlen (filename) + 1); | |
1083 | strcpy (p->filename, filename); | |
1084 | p->line = 0; | |
1085 | p->f = f; | |
1086 | ||
1087 | if (print_files != NULL && print_files->f != NULL) | |
1088 | { | |
1089 | fclose (print_files->f); | |
1090 | print_files->f = NULL; | |
1091 | } | |
1092 | p->next = print_files; | |
1093 | print_files = p; | |
1094 | ||
0af11b59 KH |
1095 | if (file_start_context) |
1096 | l = 0; | |
1097 | else | |
1098 | l = line - SHOW_PRECEDING_CONTEXT_LINES; | |
f1563258 TW |
1099 | if (l < 0) |
1100 | l = 0; | |
b34976b6 | 1101 | skip_to_line (p, l, FALSE); |
252b5132 | 1102 | if (p->f != NULL) |
b34976b6 | 1103 | skip_to_line (p, line, TRUE); |
252b5132 RH |
1104 | } |
1105 | } | |
1106 | } | |
1107 | ||
1108 | if (functionname != NULL | |
1109 | && (prev_functionname == NULL | |
1110 | || strcmp (functionname, prev_functionname) != 0)) | |
1111 | { | |
1112 | if (prev_functionname != NULL) | |
1113 | free (prev_functionname); | |
1114 | prev_functionname = xmalloc (strlen (functionname) + 1); | |
1115 | strcpy (prev_functionname, functionname); | |
1116 | } | |
1117 | ||
1118 | if (line > 0 && line != prev_line) | |
1119 | prev_line = line; | |
1120 | } | |
1121 | ||
1122 | /* Pseudo FILE object for strings. */ | |
1123 | typedef struct | |
1124 | { | |
1125 | char *buffer; | |
1126 | size_t size; | |
1127 | char *current; | |
1128 | } SFILE; | |
1129 | ||
1130 | /* sprintf to a "stream" */ | |
1131 | ||
1132 | static int | |
451dad9c | 1133 | objdump_sprintf VPARAMS ((SFILE *f, const char *format, ...)) |
252b5132 | 1134 | { |
252b5132 | 1135 | char *buf; |
252b5132 RH |
1136 | size_t n; |
1137 | ||
451dad9c AM |
1138 | VA_OPEN (args, format); |
1139 | VA_FIXEDARG (args, SFILE *, f); | |
1140 | VA_FIXEDARG (args, const char *, format); | |
252b5132 RH |
1141 | |
1142 | vasprintf (&buf, format, args); | |
1143 | ||
252b5132 RH |
1144 | if (buf == NULL) |
1145 | { | |
451dad9c | 1146 | va_end (args); |
37cc8ec1 | 1147 | fatal (_("Out of virtual memory")); |
252b5132 RH |
1148 | } |
1149 | ||
1150 | n = strlen (buf); | |
1151 | ||
b4c96d0d | 1152 | while ((size_t) ((f->buffer + f->size) - f->current) < n + 1) |
252b5132 RH |
1153 | { |
1154 | size_t curroff; | |
1155 | ||
1156 | curroff = f->current - f->buffer; | |
1157 | f->size *= 2; | |
1158 | f->buffer = xrealloc (f->buffer, f->size); | |
1159 | f->current = f->buffer + curroff; | |
1160 | } | |
1161 | ||
1162 | memcpy (f->current, buf, n); | |
1163 | f->current += n; | |
1164 | f->current[0] = '\0'; | |
1165 | ||
1166 | free (buf); | |
1167 | ||
451dad9c | 1168 | VA_CLOSE (args); |
252b5132 RH |
1169 | return n; |
1170 | } | |
1171 | ||
1172 | /* The number of zeroes we want to see before we start skipping them. | |
1173 | The number is arbitrarily chosen. */ | |
1174 | ||
20fe0649 | 1175 | #ifndef SKIP_ZEROES |
252b5132 | 1176 | #define SKIP_ZEROES (8) |
20fe0649 | 1177 | #endif |
252b5132 RH |
1178 | |
1179 | /* The number of zeroes to skip at the end of a section. If the | |
1180 | number of zeroes at the end is between SKIP_ZEROES_AT_END and | |
1181 | SKIP_ZEROES, they will be disassembled. If there are fewer than | |
1182 | SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic | |
1183 | attempt to avoid disassembling zeroes inserted by section | |
1184 | alignment. */ | |
1185 | ||
20fe0649 | 1186 | #ifndef SKIP_ZEROES_AT_END |
252b5132 | 1187 | #define SKIP_ZEROES_AT_END (3) |
20fe0649 | 1188 | #endif |
252b5132 RH |
1189 | |
1190 | /* Disassemble some data in memory between given values. */ | |
1191 | ||
1192 | static void | |
0af11b59 KH |
1193 | disassemble_bytes (info, disassemble_fn, insns, data, |
1194 | start_offset, stop_offset, relppp, | |
252b5132 RH |
1195 | relppend) |
1196 | struct disassemble_info *info; | |
1197 | disassembler_ftype disassemble_fn; | |
b34976b6 | 1198 | bfd_boolean insns; |
252b5132 | 1199 | bfd_byte *data; |
940b2b78 TW |
1200 | bfd_vma start_offset; |
1201 | bfd_vma stop_offset; | |
252b5132 RH |
1202 | arelent ***relppp; |
1203 | arelent **relppend; | |
1204 | { | |
1205 | struct objdump_disasm_info *aux; | |
1206 | asection *section; | |
940b2b78 | 1207 | int octets_per_line; |
b34976b6 | 1208 | bfd_boolean done_dot; |
252b5132 | 1209 | int skip_addr_chars; |
940b2b78 TW |
1210 | bfd_vma addr_offset; |
1211 | int opb = info->octets_per_byte; | |
252b5132 RH |
1212 | |
1213 | aux = (struct objdump_disasm_info *) info->application_data; | |
1214 | section = aux->sec; | |
1215 | ||
1216 | if (insns) | |
940b2b78 | 1217 | octets_per_line = 4; |
252b5132 | 1218 | else |
940b2b78 | 1219 | octets_per_line = 16; |
252b5132 RH |
1220 | |
1221 | /* Figure out how many characters to skip at the start of an | |
1222 | address, to make the disassembly look nicer. We discard leading | |
1223 | zeroes in chunks of 4, ensuring that there is always a leading | |
1224 | zero remaining. */ | |
1225 | skip_addr_chars = 0; | |
1226 | if (! prefix_addresses) | |
1227 | { | |
1228 | char buf[30]; | |
1229 | char *s; | |
1230 | ||
d8180c76 L |
1231 | bfd_sprintf_vma |
1232 | (aux->abfd, buf, | |
1233 | (section->vma | |
1234 | + bfd_section_size (section->owner, section) / opb)); | |
252b5132 RH |
1235 | s = buf; |
1236 | while (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0' | |
1237 | && s[4] == '0') | |
1238 | { | |
1239 | skip_addr_chars += 4; | |
1240 | s += 4; | |
1241 | } | |
1242 | } | |
1243 | ||
1244 | info->insn_info_valid = 0; | |
1245 | ||
b34976b6 | 1246 | done_dot = FALSE; |
940b2b78 TW |
1247 | addr_offset = start_offset; |
1248 | while (addr_offset < stop_offset) | |
252b5132 RH |
1249 | { |
1250 | bfd_vma z; | |
940b2b78 | 1251 | int octets = 0; |
b34976b6 | 1252 | bfd_boolean need_nl = FALSE; |
252b5132 | 1253 | |
940b2b78 | 1254 | /* If we see more than SKIP_ZEROES octets of zeroes, we just |
252b5132 | 1255 | print `...'. */ |
940b2b78 | 1256 | for (z = addr_offset * opb; z < stop_offset * opb; z++) |
252b5132 RH |
1257 | if (data[z] != 0) |
1258 | break; | |
1259 | if (! disassemble_zeroes | |
1260 | && (info->insn_info_valid == 0 | |
1261 | || info->branch_delay_insns == 0) | |
940b2b78 | 1262 | && (z - addr_offset * opb >= SKIP_ZEROES |
0af11b59 KH |
1263 | || (z == stop_offset * opb && |
1264 | z - addr_offset * opb < SKIP_ZEROES_AT_END))) | |
252b5132 RH |
1265 | { |
1266 | printf ("\t...\n"); | |
1267 | ||
940b2b78 | 1268 | /* If there are more nonzero octets to follow, we only skip |
252b5132 RH |
1269 | zeroes in multiples of 4, to try to avoid running over |
1270 | the start of an instruction which happens to start with | |
1271 | zero. */ | |
940b2b78 TW |
1272 | if (z != stop_offset * opb) |
1273 | z = addr_offset * opb + ((z - addr_offset * opb) &~ 3); | |
252b5132 | 1274 | |
940b2b78 | 1275 | octets = z - addr_offset * opb; |
252b5132 RH |
1276 | } |
1277 | else | |
1278 | { | |
1279 | char buf[50]; | |
1280 | SFILE sfile; | |
1281 | int bpc = 0; | |
1282 | int pb = 0; | |
1283 | ||
b34976b6 | 1284 | done_dot = FALSE; |
252b5132 RH |
1285 | |
1286 | if (with_line_numbers || with_source_code) | |
76a406e5 NC |
1287 | /* The line number tables will refer to unadjusted |
1288 | section VMAs, so we must undo any VMA modifications | |
1289 | when calling show_line. */ | |
1290 | show_line (aux->abfd, section, addr_offset - adjust_section_vma); | |
252b5132 RH |
1291 | |
1292 | if (! prefix_addresses) | |
1293 | { | |
1294 | char *s; | |
1295 | ||
d8180c76 | 1296 | bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset); |
252b5132 RH |
1297 | for (s = buf + skip_addr_chars; *s == '0'; s++) |
1298 | *s = ' '; | |
1299 | if (*s == '\0') | |
1300 | *--s = '0'; | |
1301 | printf ("%s:\t", buf + skip_addr_chars); | |
1302 | } | |
1303 | else | |
1304 | { | |
b34976b6 | 1305 | aux->require_sec = TRUE; |
940b2b78 | 1306 | objdump_print_address (section->vma + addr_offset, info); |
b34976b6 | 1307 | aux->require_sec = FALSE; |
252b5132 RH |
1308 | putchar (' '); |
1309 | } | |
1310 | ||
1311 | if (insns) | |
1312 | { | |
1313 | sfile.size = 120; | |
1314 | sfile.buffer = xmalloc (sfile.size); | |
1315 | sfile.current = sfile.buffer; | |
1316 | info->fprintf_func = (fprintf_ftype) objdump_sprintf; | |
1317 | info->stream = (FILE *) &sfile; | |
1318 | info->bytes_per_line = 0; | |
1319 | info->bytes_per_chunk = 0; | |
1320 | ||
8b1e6df3 | 1321 | #ifdef DISASSEMBLER_NEEDS_RELOCS |
940b2b78 | 1322 | /* FIXME: This is wrong. It tests the number of octets |
252b5132 RH |
1323 | in the last instruction, not the current one. */ |
1324 | if (*relppp < relppend | |
940b2b78 | 1325 | && (**relppp)->address >= addr_offset |
8b1e6df3 | 1326 | && (**relppp)->address <= addr_offset + octets / opb) |
252b5132 RH |
1327 | info->flags = INSN_HAS_RELOC; |
1328 | else | |
8b1e6df3 | 1329 | #endif |
252b5132 RH |
1330 | info->flags = 0; |
1331 | ||
940b2b78 | 1332 | octets = (*disassemble_fn) (section->vma + addr_offset, info); |
252b5132 RH |
1333 | info->fprintf_func = (fprintf_ftype) fprintf; |
1334 | info->stream = stdout; | |
1335 | if (info->bytes_per_line != 0) | |
940b2b78 TW |
1336 | octets_per_line = info->bytes_per_line; |
1337 | if (octets < 0) | |
e07bf1ac ILT |
1338 | { |
1339 | if (sfile.current != sfile.buffer) | |
1340 | printf ("%s\n", sfile.buffer); | |
1341 | free (sfile.buffer); | |
1342 | break; | |
1343 | } | |
252b5132 RH |
1344 | } |
1345 | else | |
1346 | { | |
b4c96d0d | 1347 | bfd_vma j; |
252b5132 | 1348 | |
940b2b78 TW |
1349 | octets = octets_per_line; |
1350 | if (addr_offset + octets / opb > stop_offset) | |
1351 | octets = (stop_offset - addr_offset) * opb; | |
252b5132 | 1352 | |
940b2b78 | 1353 | for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j) |
252b5132 | 1354 | { |
3882b010 | 1355 | if (ISPRINT (data[j])) |
940b2b78 | 1356 | buf[j - addr_offset * opb] = data[j]; |
252b5132 | 1357 | else |
940b2b78 | 1358 | buf[j - addr_offset * opb] = '.'; |
252b5132 | 1359 | } |
940b2b78 | 1360 | buf[j - addr_offset * opb] = '\0'; |
252b5132 RH |
1361 | } |
1362 | ||
1363 | if (prefix_addresses | |
1364 | ? show_raw_insn > 0 | |
1365 | : show_raw_insn >= 0) | |
1366 | { | |
b4c96d0d | 1367 | bfd_vma j; |
252b5132 RH |
1368 | |
1369 | /* If ! prefix_addresses and ! wide_output, we print | |
940b2b78 TW |
1370 | octets_per_line octets per line. */ |
1371 | pb = octets; | |
1372 | if (pb > octets_per_line && ! prefix_addresses && ! wide_output) | |
1373 | pb = octets_per_line; | |
252b5132 RH |
1374 | |
1375 | if (info->bytes_per_chunk) | |
1376 | bpc = info->bytes_per_chunk; | |
1377 | else | |
1378 | bpc = 1; | |
1379 | ||
940b2b78 | 1380 | for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc) |
252b5132 RH |
1381 | { |
1382 | int k; | |
1383 | if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE) | |
1384 | { | |
1385 | for (k = bpc - 1; k >= 0; k--) | |
1386 | printf ("%02x", (unsigned) data[j + k]); | |
1387 | putchar (' '); | |
1388 | } | |
1389 | else | |
1390 | { | |
1391 | for (k = 0; k < bpc; k++) | |
1392 | printf ("%02x", (unsigned) data[j + k]); | |
1393 | putchar (' '); | |
1394 | } | |
1395 | } | |
1396 | ||
940b2b78 | 1397 | for (; pb < octets_per_line; pb += bpc) |
252b5132 RH |
1398 | { |
1399 | int k; | |
1400 | ||
1401 | for (k = 0; k < bpc; k++) | |
1402 | printf (" "); | |
1403 | putchar (' '); | |
1404 | } | |
1405 | ||
1406 | /* Separate raw data from instruction by extra space. */ | |
1407 | if (insns) | |
1408 | putchar ('\t'); | |
1409 | else | |
1410 | printf (" "); | |
1411 | } | |
1412 | ||
1413 | if (! insns) | |
1414 | printf ("%s", buf); | |
1415 | else | |
1416 | { | |
1417 | printf ("%s", sfile.buffer); | |
1418 | free (sfile.buffer); | |
1419 | } | |
1420 | ||
1421 | if (prefix_addresses | |
1422 | ? show_raw_insn > 0 | |
1423 | : show_raw_insn >= 0) | |
1424 | { | |
940b2b78 | 1425 | while (pb < octets) |
252b5132 | 1426 | { |
b4c96d0d | 1427 | bfd_vma j; |
252b5132 RH |
1428 | char *s; |
1429 | ||
1430 | putchar ('\n'); | |
940b2b78 | 1431 | j = addr_offset * opb + pb; |
252b5132 | 1432 | |
d8180c76 | 1433 | bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb); |
252b5132 RH |
1434 | for (s = buf + skip_addr_chars; *s == '0'; s++) |
1435 | *s = ' '; | |
1436 | if (*s == '\0') | |
1437 | *--s = '0'; | |
1438 | printf ("%s:\t", buf + skip_addr_chars); | |
1439 | ||
940b2b78 TW |
1440 | pb += octets_per_line; |
1441 | if (pb > octets) | |
1442 | pb = octets; | |
1443 | for (; j < addr_offset * opb + pb; j += bpc) | |
252b5132 RH |
1444 | { |
1445 | int k; | |
1446 | ||
1447 | if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE) | |
1448 | { | |
1449 | for (k = bpc - 1; k >= 0; k--) | |
1450 | printf ("%02x", (unsigned) data[j + k]); | |
1451 | putchar (' '); | |
1452 | } | |
1453 | else | |
1454 | { | |
1455 | for (k = 0; k < bpc; k++) | |
1456 | printf ("%02x", (unsigned) data[j + k]); | |
1457 | putchar (' '); | |
1458 | } | |
1459 | } | |
1460 | } | |
1461 | } | |
1462 | ||
1463 | if (!wide_output) | |
1464 | putchar ('\n'); | |
1465 | else | |
b34976b6 | 1466 | need_nl = TRUE; |
252b5132 RH |
1467 | } |
1468 | ||
8b1e6df3 | 1469 | if ((section->flags & SEC_RELOC) != 0 |
0af11b59 KH |
1470 | #ifndef DISASSEMBLER_NEEDS_RELOCS |
1471 | && dump_reloc_info | |
8b1e6df3 NC |
1472 | #endif |
1473 | ) | |
252b5132 RH |
1474 | { |
1475 | while ((*relppp) < relppend | |
940b2b78 TW |
1476 | && ((**relppp)->address >= (bfd_vma) addr_offset |
1477 | && (**relppp)->address < (bfd_vma) addr_offset + octets / opb)) | |
8b1e6df3 NC |
1478 | #ifdef DISASSEMBLER_NEEDS_RELOCS |
1479 | if (! dump_reloc_info) | |
1480 | ++(*relppp); | |
1481 | else | |
1482 | #endif | |
252b5132 RH |
1483 | { |
1484 | arelent *q; | |
1485 | ||
1486 | q = **relppp; | |
1487 | ||
1488 | if (wide_output) | |
1489 | putchar ('\t'); | |
1490 | else | |
1491 | printf ("\t\t\t"); | |
1492 | ||
b34976b6 | 1493 | objdump_print_value (section->vma + q->address, info, TRUE); |
252b5132 RH |
1494 | |
1495 | printf (": %s\t", q->howto->name); | |
1496 | ||
1497 | if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL) | |
1498 | printf ("*unknown*"); | |
1499 | else | |
1500 | { | |
1501 | const char *sym_name; | |
1502 | ||
1503 | sym_name = bfd_asymbol_name (*q->sym_ptr_ptr); | |
1504 | if (sym_name != NULL && *sym_name != '\0') | |
1505 | objdump_print_symname (aux->abfd, info, *q->sym_ptr_ptr); | |
1506 | else | |
1507 | { | |
1508 | asection *sym_sec; | |
1509 | ||
1510 | sym_sec = bfd_get_section (*q->sym_ptr_ptr); | |
1511 | sym_name = bfd_get_section_name (aux->abfd, sym_sec); | |
1512 | if (sym_name == NULL || *sym_name == '\0') | |
1513 | sym_name = "*unknown*"; | |
1514 | printf ("%s", sym_name); | |
1515 | } | |
1516 | } | |
1517 | ||
1518 | if (q->addend) | |
1519 | { | |
1520 | printf ("+0x"); | |
b34976b6 | 1521 | objdump_print_value (q->addend, info, TRUE); |
252b5132 RH |
1522 | } |
1523 | ||
1524 | printf ("\n"); | |
b34976b6 | 1525 | need_nl = FALSE; |
252b5132 RH |
1526 | ++(*relppp); |
1527 | } | |
1528 | } | |
1529 | ||
1530 | if (need_nl) | |
1531 | printf ("\n"); | |
1532 | ||
940b2b78 | 1533 | addr_offset += octets / opb; |
252b5132 RH |
1534 | } |
1535 | } | |
1536 | ||
1537 | /* Disassemble the contents of an object file. */ | |
1538 | ||
1539 | static void | |
1540 | disassemble_data (abfd) | |
1541 | bfd *abfd; | |
1542 | { | |
f6af82bd | 1543 | unsigned long addr_offset; |
252b5132 RH |
1544 | disassembler_ftype disassemble_fn; |
1545 | struct disassemble_info disasm_info; | |
1546 | struct objdump_disasm_info aux; | |
1547 | asection *section; | |
ea584125 | 1548 | unsigned int opb; |
252b5132 RH |
1549 | |
1550 | print_files = NULL; | |
1551 | prev_functionname = NULL; | |
1552 | prev_line = -1; | |
1553 | ||
1554 | /* We make a copy of syms to sort. We don't want to sort syms | |
1555 | because that will screw up the relocs. */ | |
1556 | sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *)); | |
1557 | memcpy (sorted_syms, syms, symcount * sizeof (asymbol *)); | |
1558 | ||
1559 | sorted_symcount = remove_useless_symbols (sorted_syms, symcount); | |
1560 | ||
98a91d6a | 1561 | /* Sort the symbols into section and symbol order. */ |
252b5132 RH |
1562 | qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols); |
1563 | ||
98a91d6a NC |
1564 | INIT_DISASSEMBLE_INFO (disasm_info, stdout, fprintf); |
1565 | ||
252b5132 RH |
1566 | disasm_info.application_data = (PTR) &aux; |
1567 | aux.abfd = abfd; | |
b34976b6 | 1568 | aux.require_sec = FALSE; |
252b5132 RH |
1569 | disasm_info.print_address_func = objdump_print_address; |
1570 | disasm_info.symbol_at_address_func = objdump_symbol_at_address; | |
1571 | ||
1572 | if (machine != (char *) NULL) | |
1573 | { | |
1574 | const bfd_arch_info_type *info = bfd_scan_arch (machine); | |
98a91d6a | 1575 | |
252b5132 | 1576 | if (info == NULL) |
98a91d6a NC |
1577 | fatal (_("Can't use supplied machine %s"), machine); |
1578 | ||
252b5132 RH |
1579 | abfd->arch_info = info; |
1580 | } | |
1581 | ||
1582 | if (endian != BFD_ENDIAN_UNKNOWN) | |
1583 | { | |
1584 | struct bfd_target *xvec; | |
1585 | ||
1586 | xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target)); | |
1587 | memcpy (xvec, abfd->xvec, sizeof (struct bfd_target)); | |
1588 | xvec->byteorder = endian; | |
1589 | abfd->xvec = xvec; | |
1590 | } | |
1591 | ||
1592 | disassemble_fn = disassembler (abfd); | |
1593 | if (!disassemble_fn) | |
1594 | { | |
37cc8ec1 AM |
1595 | non_fatal (_("Can't disassemble for architecture %s\n"), |
1596 | bfd_printable_arch_mach (bfd_get_arch (abfd), 0)); | |
75cd796a | 1597 | exit_status = 1; |
252b5132 RH |
1598 | return; |
1599 | } | |
1600 | ||
ea584125 TW |
1601 | opb = bfd_octets_per_byte (abfd); |
1602 | ||
252b5132 RH |
1603 | disasm_info.flavour = bfd_get_flavour (abfd); |
1604 | disasm_info.arch = bfd_get_arch (abfd); | |
1605 | disasm_info.mach = bfd_get_mach (abfd); | |
dd92f639 | 1606 | disasm_info.disassembler_options = disassembler_options; |
ea584125 | 1607 | disasm_info.octets_per_byte = opb; |
0af11b59 | 1608 | |
252b5132 | 1609 | if (bfd_big_endian (abfd)) |
a8a9050d | 1610 | disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG; |
252b5132 | 1611 | else if (bfd_little_endian (abfd)) |
a8a9050d | 1612 | disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE; |
252b5132 RH |
1613 | else |
1614 | /* ??? Aborting here seems too drastic. We could default to big or little | |
1615 | instead. */ | |
1616 | disasm_info.endian = BFD_ENDIAN_UNKNOWN; | |
1617 | ||
1618 | for (section = abfd->sections; | |
1619 | section != (asection *) NULL; | |
1620 | section = section->next) | |
1621 | { | |
1622 | bfd_byte *data = NULL; | |
1623 | bfd_size_type datasize = 0; | |
1624 | arelent **relbuf = NULL; | |
1625 | arelent **relpp = NULL; | |
1626 | arelent **relppend = NULL; | |
f6af82bd | 1627 | unsigned long stop_offset; |
252b5132 RH |
1628 | asymbol *sym = NULL; |
1629 | long place = 0; | |
1630 | ||
1631 | if ((section->flags & SEC_LOAD) == 0 | |
1632 | || (! disassemble_all | |
1633 | && only == NULL | |
1634 | && (section->flags & SEC_CODE) == 0)) | |
1635 | continue; | |
6e50c90c L |
1636 | |
1637 | if (only != NULL) | |
1638 | { | |
1639 | size_t i; | |
1640 | ||
1641 | for (i = 0; i < only_used; i++) | |
1642 | if (strcmp (only [i], section->name) == 0) | |
1643 | break; | |
1644 | ||
1645 | if (i == only_used) | |
1646 | continue; | |
1647 | } | |
252b5132 | 1648 | |
8b1e6df3 | 1649 | if ((section->flags & SEC_RELOC) != 0 |
0af11b59 | 1650 | #ifndef DISASSEMBLER_NEEDS_RELOCS |
8b1e6df3 NC |
1651 | && dump_reloc_info |
1652 | #endif | |
0af11b59 | 1653 | ) |
252b5132 RH |
1654 | { |
1655 | long relsize; | |
1656 | ||
1657 | relsize = bfd_get_reloc_upper_bound (abfd, section); | |
1658 | if (relsize < 0) | |
1659 | bfd_fatal (bfd_get_filename (abfd)); | |
1660 | ||
1661 | if (relsize > 0) | |
1662 | { | |
1663 | long relcount; | |
1664 | ||
1665 | relbuf = (arelent **) xmalloc (relsize); | |
1666 | relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms); | |
1667 | if (relcount < 0) | |
1668 | bfd_fatal (bfd_get_filename (abfd)); | |
1669 | ||
1670 | /* Sort the relocs by address. */ | |
1671 | qsort (relbuf, relcount, sizeof (arelent *), compare_relocs); | |
1672 | ||
1673 | relpp = relbuf; | |
1674 | relppend = relpp + relcount; | |
1675 | ||
1676 | /* Skip over the relocs belonging to addresses below the | |
1677 | start address. */ | |
1678 | if (start_address != (bfd_vma) -1) | |
98a91d6a NC |
1679 | while (relpp < relppend |
1680 | && (*relpp)->address < start_address) | |
1681 | ++relpp; | |
252b5132 RH |
1682 | } |
1683 | } | |
1684 | ||
1685 | printf (_("Disassembly of section %s:\n"), section->name); | |
1686 | ||
1687 | datasize = bfd_get_section_size_before_reloc (section); | |
1688 | if (datasize == 0) | |
1689 | continue; | |
1690 | ||
1691 | data = (bfd_byte *) xmalloc ((size_t) datasize); | |
1692 | ||
1693 | bfd_get_section_contents (abfd, section, data, 0, datasize); | |
1694 | ||
1695 | aux.sec = section; | |
1696 | disasm_info.buffer = data; | |
1697 | disasm_info.buffer_vma = section->vma; | |
1698 | disasm_info.buffer_length = datasize; | |
8f9d9bd9 | 1699 | disasm_info.section = section; |
98a91d6a | 1700 | |
252b5132 RH |
1701 | if (start_address == (bfd_vma) -1 |
1702 | || start_address < disasm_info.buffer_vma) | |
940b2b78 | 1703 | addr_offset = 0; |
252b5132 | 1704 | else |
940b2b78 | 1705 | addr_offset = start_address - disasm_info.buffer_vma; |
98a91d6a | 1706 | |
252b5132 | 1707 | if (stop_address == (bfd_vma) -1) |
940b2b78 | 1708 | stop_offset = datasize / opb; |
252b5132 RH |
1709 | else |
1710 | { | |
1711 | if (stop_address < disasm_info.buffer_vma) | |
940b2b78 | 1712 | stop_offset = 0; |
252b5132 | 1713 | else |
940b2b78 TW |
1714 | stop_offset = stop_address - disasm_info.buffer_vma; |
1715 | if (stop_offset > disasm_info.buffer_length / opb) | |
1716 | stop_offset = disasm_info.buffer_length / opb; | |
252b5132 RH |
1717 | } |
1718 | ||
940b2b78 | 1719 | sym = find_symbol_for_address (abfd, section, section->vma + addr_offset, |
b34976b6 | 1720 | TRUE, &place); |
252b5132 | 1721 | |
940b2b78 | 1722 | while (addr_offset < stop_offset) |
252b5132 RH |
1723 | { |
1724 | asymbol *nextsym; | |
f6af82bd | 1725 | unsigned long nextstop_offset; |
b34976b6 | 1726 | bfd_boolean insns; |
0af11b59 | 1727 | |
940b2b78 | 1728 | if (sym != NULL && bfd_asymbol_value (sym) <= section->vma + addr_offset) |
252b5132 RH |
1729 | { |
1730 | int x; | |
1731 | ||
1732 | for (x = place; | |
1733 | (x < sorted_symcount | |
940b2b78 | 1734 | && bfd_asymbol_value (sorted_syms[x]) <= section->vma + addr_offset); |
252b5132 RH |
1735 | ++x) |
1736 | continue; | |
98a91d6a | 1737 | |
252b5132 RH |
1738 | disasm_info.symbols = & sorted_syms[place]; |
1739 | disasm_info.num_symbols = x - place; | |
1740 | } | |
1741 | else | |
1742 | disasm_info.symbols = NULL; | |
1743 | ||
1744 | if (! prefix_addresses) | |
1745 | { | |
98a91d6a | 1746 | (* disasm_info.fprintf_func) (disasm_info.stream, "\n"); |
252b5132 | 1747 | objdump_print_addr_with_sym (abfd, section, sym, |
940b2b78 | 1748 | section->vma + addr_offset, |
252b5132 | 1749 | &disasm_info, |
b34976b6 | 1750 | FALSE); |
98a91d6a | 1751 | (* disasm_info.fprintf_func) (disasm_info.stream, ":\n"); |
252b5132 | 1752 | } |
0af11b59 | 1753 | |
940b2b78 | 1754 | if (sym != NULL && bfd_asymbol_value (sym) > section->vma + addr_offset) |
252b5132 RH |
1755 | nextsym = sym; |
1756 | else if (sym == NULL) | |
1757 | nextsym = NULL; | |
1758 | else | |
1759 | { | |
f1f8ce07 ILT |
1760 | /* Search forward for the next appropriate symbol in |
1761 | SECTION. Note that all the symbols are sorted | |
1762 | together into one big array, and that some sections | |
1763 | may have overlapping addresses. */ | |
252b5132 | 1764 | while (place < sorted_symcount |
252b5132 RH |
1765 | && (sorted_syms[place]->section != section |
1766 | || (bfd_asymbol_value (sorted_syms[place]) | |
1767 | <= bfd_asymbol_value (sym)))) | |
1768 | ++place; | |
1769 | if (place >= sorted_symcount) | |
1770 | nextsym = NULL; | |
1771 | else | |
1772 | nextsym = sorted_syms[place]; | |
1773 | } | |
0af11b59 | 1774 | |
940b2b78 | 1775 | if (sym != NULL && bfd_asymbol_value (sym) > section->vma + addr_offset) |
252b5132 | 1776 | { |
940b2b78 TW |
1777 | nextstop_offset = bfd_asymbol_value (sym) - section->vma; |
1778 | if (nextstop_offset > stop_offset) | |
1779 | nextstop_offset = stop_offset; | |
252b5132 RH |
1780 | } |
1781 | else if (nextsym == NULL) | |
940b2b78 | 1782 | nextstop_offset = stop_offset; |
252b5132 RH |
1783 | else |
1784 | { | |
940b2b78 TW |
1785 | nextstop_offset = bfd_asymbol_value (nextsym) - section->vma; |
1786 | if (nextstop_offset > stop_offset) | |
1787 | nextstop_offset = stop_offset; | |
252b5132 | 1788 | } |
0af11b59 | 1789 | |
252b5132 RH |
1790 | /* If a symbol is explicitly marked as being an object |
1791 | rather than a function, just dump the bytes without | |
1792 | disassembling them. */ | |
1793 | if (disassemble_all | |
1794 | || sym == NULL | |
940b2b78 | 1795 | || bfd_asymbol_value (sym) > section->vma + addr_offset |
252b5132 RH |
1796 | || ((sym->flags & BSF_OBJECT) == 0 |
1797 | && (strstr (bfd_asymbol_name (sym), "gnu_compiled") | |
1798 | == NULL) | |
1799 | && (strstr (bfd_asymbol_name (sym), "gcc2_compiled") | |
1800 | == NULL)) | |
1801 | || (sym->flags & BSF_FUNCTION) != 0) | |
b34976b6 | 1802 | insns = TRUE; |
252b5132 | 1803 | else |
b34976b6 | 1804 | insns = FALSE; |
0af11b59 KH |
1805 | |
1806 | disassemble_bytes (&disasm_info, disassemble_fn, insns, data, | |
1807 | addr_offset, nextstop_offset, &relpp, relppend); | |
1808 | ||
940b2b78 | 1809 | addr_offset = nextstop_offset; |
252b5132 RH |
1810 | sym = nextsym; |
1811 | } | |
0af11b59 | 1812 | |
252b5132 | 1813 | free (data); |
98a91d6a | 1814 | |
252b5132 RH |
1815 | if (relbuf != NULL) |
1816 | free (relbuf); | |
1817 | } | |
1818 | free (sorted_syms); | |
1819 | } | |
1820 | \f | |
252b5132 RH |
1821 | /* Dump the stabs sections from an object file that has a section that |
1822 | uses Sun stabs encoding. */ | |
1823 | ||
1824 | static void | |
1825 | dump_stabs (abfd) | |
1826 | bfd *abfd; | |
1827 | { | |
1828 | dump_section_stabs (abfd, ".stab", ".stabstr"); | |
1829 | dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr"); | |
1830 | dump_section_stabs (abfd, ".stab.index", ".stab.indexstr"); | |
1831 | dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$"); | |
1832 | } | |
1833 | ||
252b5132 RH |
1834 | /* Read ABFD's stabs section STABSECT_NAME into `stabs' |
1835 | and string table section STRSECT_NAME into `strtab'. | |
b34976b6 AM |
1836 | If the section exists and was read, allocate the space and return TRUE. |
1837 | Otherwise return FALSE. */ | |
252b5132 | 1838 | |
b34976b6 | 1839 | static bfd_boolean |
252b5132 RH |
1840 | read_section_stabs (abfd, stabsect_name, strsect_name) |
1841 | bfd *abfd; | |
1842 | const char *stabsect_name; | |
1843 | const char *strsect_name; | |
1844 | { | |
1845 | asection *stabsect, *stabstrsect; | |
1846 | ||
1847 | stabsect = bfd_get_section_by_name (abfd, stabsect_name); | |
1848 | if (0 == stabsect) | |
1849 | { | |
1850 | printf (_("No %s section present\n\n"), stabsect_name); | |
b34976b6 | 1851 | return FALSE; |
252b5132 RH |
1852 | } |
1853 | ||
1854 | stabstrsect = bfd_get_section_by_name (abfd, strsect_name); | |
1855 | if (0 == stabstrsect) | |
1856 | { | |
37cc8ec1 AM |
1857 | non_fatal (_("%s has no %s section"), |
1858 | bfd_get_filename (abfd), strsect_name); | |
75cd796a | 1859 | exit_status = 1; |
b34976b6 | 1860 | return FALSE; |
252b5132 | 1861 | } |
0af11b59 | 1862 | |
252b5132 RH |
1863 | stab_size = bfd_section_size (abfd, stabsect); |
1864 | stabstr_size = bfd_section_size (abfd, stabstrsect); | |
1865 | ||
1866 | stabs = (bfd_byte *) xmalloc (stab_size); | |
1867 | strtab = (char *) xmalloc (stabstr_size); | |
0af11b59 | 1868 | |
252b5132 RH |
1869 | if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size)) |
1870 | { | |
37cc8ec1 AM |
1871 | non_fatal (_("Reading %s section of %s failed: %s"), |
1872 | stabsect_name, bfd_get_filename (abfd), | |
1873 | bfd_errmsg (bfd_get_error ())); | |
252b5132 RH |
1874 | free (stabs); |
1875 | free (strtab); | |
75cd796a | 1876 | exit_status = 1; |
b34976b6 | 1877 | return FALSE; |
252b5132 RH |
1878 | } |
1879 | ||
1880 | if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0, | |
1881 | stabstr_size)) | |
1882 | { | |
37cc8ec1 AM |
1883 | non_fatal (_("Reading %s section of %s failed: %s\n"), |
1884 | strsect_name, bfd_get_filename (abfd), | |
1885 | bfd_errmsg (bfd_get_error ())); | |
252b5132 RH |
1886 | free (stabs); |
1887 | free (strtab); | |
75cd796a | 1888 | exit_status = 1; |
b34976b6 | 1889 | return FALSE; |
252b5132 RH |
1890 | } |
1891 | ||
b34976b6 | 1892 | return TRUE; |
252b5132 RH |
1893 | } |
1894 | ||
1895 | /* Stabs entries use a 12 byte format: | |
1896 | 4 byte string table index | |
1897 | 1 byte stab type | |
1898 | 1 byte stab other field | |
1899 | 2 byte stab desc field | |
1900 | 4 byte stab value | |
1901 | FIXME: This will have to change for a 64 bit object format. */ | |
1902 | ||
1903 | #define STRDXOFF (0) | |
1904 | #define TYPEOFF (4) | |
1905 | #define OTHEROFF (5) | |
1906 | #define DESCOFF (6) | |
1907 | #define VALOFF (8) | |
1908 | #define STABSIZE (12) | |
1909 | ||
1910 | /* Print ABFD's stabs section STABSECT_NAME (in `stabs'), | |
1911 | using string table section STRSECT_NAME (in `strtab'). */ | |
1912 | ||
1913 | static void | |
1914 | print_section_stabs (abfd, stabsect_name, strsect_name) | |
1915 | bfd *abfd; | |
1916 | const char *stabsect_name; | |
b4c96d0d | 1917 | const char *strsect_name ATTRIBUTE_UNUSED; |
252b5132 RH |
1918 | { |
1919 | int i; | |
1920 | unsigned file_string_table_offset = 0, next_file_string_table_offset = 0; | |
1921 | bfd_byte *stabp, *stabs_end; | |
1922 | ||
1923 | stabp = stabs; | |
1924 | stabs_end = stabp + stab_size; | |
1925 | ||
1926 | printf (_("Contents of %s section:\n\n"), stabsect_name); | |
1927 | printf ("Symnum n_type n_othr n_desc n_value n_strx String\n"); | |
1928 | ||
1929 | /* Loop through all symbols and print them. | |
1930 | ||
1931 | We start the index at -1 because there is a dummy symbol on | |
1932 | the front of stabs-in-{coff,elf} sections that supplies sizes. */ | |
252b5132 RH |
1933 | for (i = -1; stabp < stabs_end; stabp += STABSIZE, i++) |
1934 | { | |
1935 | const char *name; | |
1936 | unsigned long strx; | |
1937 | unsigned char type, other; | |
1938 | unsigned short desc; | |
1939 | bfd_vma value; | |
1940 | ||
1941 | strx = bfd_h_get_32 (abfd, stabp + STRDXOFF); | |
1942 | type = bfd_h_get_8 (abfd, stabp + TYPEOFF); | |
1943 | other = bfd_h_get_8 (abfd, stabp + OTHEROFF); | |
1944 | desc = bfd_h_get_16 (abfd, stabp + DESCOFF); | |
1945 | value = bfd_h_get_32 (abfd, stabp + VALOFF); | |
1946 | ||
1947 | printf ("\n%-6d ", i); | |
1948 | /* Either print the stab name, or, if unnamed, print its number | |
0af11b59 | 1949 | again (makes consistent formatting for tools like awk). */ |
252b5132 RH |
1950 | name = bfd_get_stab_name (type); |
1951 | if (name != NULL) | |
1952 | printf ("%-6s", name); | |
1953 | else if (type == N_UNDF) | |
1954 | printf ("HdrSym"); | |
1955 | else | |
1956 | printf ("%-6d", type); | |
1957 | printf (" %-6d %-6d ", other, desc); | |
d8180c76 | 1958 | bfd_printf_vma (abfd, value); |
252b5132 RH |
1959 | printf (" %-6lu", strx); |
1960 | ||
1961 | /* Symbols with type == 0 (N_UNDF) specify the length of the | |
1962 | string table associated with this file. We use that info | |
1963 | to know how to relocate the *next* file's string table indices. */ | |
252b5132 RH |
1964 | if (type == N_UNDF) |
1965 | { | |
1966 | file_string_table_offset = next_file_string_table_offset; | |
1967 | next_file_string_table_offset += value; | |
1968 | } | |
1969 | else | |
1970 | { | |
1971 | /* Using the (possibly updated) string table offset, print the | |
1972 | string (if any) associated with this symbol. */ | |
252b5132 RH |
1973 | if ((strx + file_string_table_offset) < stabstr_size) |
1974 | printf (" %s", &strtab[strx + file_string_table_offset]); | |
1975 | else | |
1976 | printf (" *"); | |
1977 | } | |
1978 | } | |
1979 | printf ("\n\n"); | |
1980 | } | |
1981 | ||
1982 | static void | |
1983 | dump_section_stabs (abfd, stabsect_name, strsect_name) | |
1984 | bfd *abfd; | |
1985 | char *stabsect_name; | |
1986 | char *strsect_name; | |
1987 | { | |
1988 | asection *s; | |
1989 | ||
1990 | /* Check for section names for which stabsect_name is a prefix, to | |
1991 | handle .stab0, etc. */ | |
1992 | for (s = abfd->sections; | |
1993 | s != NULL; | |
1994 | s = s->next) | |
1995 | { | |
1996 | int len; | |
1997 | ||
1998 | len = strlen (stabsect_name); | |
1999 | ||
2000 | /* If the prefix matches, and the files section name ends with a | |
2001 | nul or a digit, then we match. I.e., we want either an exact | |
2002 | match or a section followed by a number. */ | |
2003 | if (strncmp (stabsect_name, s->name, len) == 0 | |
2004 | && (s->name[len] == '\000' | |
3882b010 | 2005 | || ISDIGIT (s->name[len]))) |
252b5132 RH |
2006 | { |
2007 | if (read_section_stabs (abfd, s->name, strsect_name)) | |
2008 | { | |
2009 | print_section_stabs (abfd, s->name, strsect_name); | |
2010 | free (stabs); | |
2011 | free (strtab); | |
2012 | } | |
2013 | } | |
2014 | } | |
2015 | } | |
98a91d6a | 2016 | |
252b5132 RH |
2017 | \f |
2018 | static void | |
2019 | dump_bfd_header (abfd) | |
2020 | bfd *abfd; | |
2021 | { | |
2022 | char *comma = ""; | |
2023 | ||
2024 | printf (_("architecture: %s, "), | |
2025 | bfd_printable_arch_mach (bfd_get_arch (abfd), | |
2026 | bfd_get_mach (abfd))); | |
2027 | printf (_("flags 0x%08x:\n"), abfd->flags); | |
2028 | ||
2029 | #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";} | |
2030 | PF (HAS_RELOC, "HAS_RELOC"); | |
2031 | PF (EXEC_P, "EXEC_P"); | |
2032 | PF (HAS_LINENO, "HAS_LINENO"); | |
2033 | PF (HAS_DEBUG, "HAS_DEBUG"); | |
2034 | PF (HAS_SYMS, "HAS_SYMS"); | |
2035 | PF (HAS_LOCALS, "HAS_LOCALS"); | |
2036 | PF (DYNAMIC, "DYNAMIC"); | |
2037 | PF (WP_TEXT, "WP_TEXT"); | |
2038 | PF (D_PAGED, "D_PAGED"); | |
2039 | PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE"); | |
026df7c5 | 2040 | PF (HAS_LOAD_PAGE, "HAS_LOAD_PAGE"); |
252b5132 | 2041 | printf (_("\nstart address 0x")); |
d8180c76 | 2042 | bfd_printf_vma (abfd, abfd->start_address); |
252b5132 RH |
2043 | printf ("\n"); |
2044 | } | |
98a91d6a | 2045 | |
252b5132 RH |
2046 | \f |
2047 | static void | |
2048 | dump_bfd_private_header (abfd) | |
2049 | bfd *abfd; | |
2050 | { | |
2051 | bfd_print_private_bfd_data (abfd, stdout); | |
2052 | } | |
2053 | ||
98a91d6a | 2054 | /* Dump selected contents of ABFD. */ |
d24de309 | 2055 | |
252b5132 | 2056 | static void |
d24de309 | 2057 | dump_bfd (abfd) |
252b5132 RH |
2058 | bfd *abfd; |
2059 | { | |
252b5132 RH |
2060 | /* If we are adjusting section VMA's, change them all now. Changing |
2061 | the BFD information is a hack. However, we must do it, or | |
2062 | bfd_find_nearest_line will not do the right thing. */ | |
2063 | if (adjust_section_vma != 0) | |
2064 | { | |
2065 | asection *s; | |
2066 | ||
2067 | for (s = abfd->sections; s != NULL; s = s->next) | |
2068 | { | |
2069 | s->vma += adjust_section_vma; | |
2070 | s->lma += adjust_section_vma; | |
2071 | } | |
2072 | } | |
2073 | ||
51cdc6e0 NC |
2074 | if (! dump_debugging_tags) |
2075 | printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd), | |
2076 | abfd->xvec->name); | |
252b5132 | 2077 | if (dump_ar_hdrs) |
b34976b6 | 2078 | print_arelt_descr (stdout, abfd, TRUE); |
252b5132 RH |
2079 | if (dump_file_header) |
2080 | dump_bfd_header (abfd); | |
2081 | if (dump_private_headers) | |
2082 | dump_bfd_private_header (abfd); | |
51cdc6e0 NC |
2083 | if (! dump_debugging_tags) |
2084 | putchar ('\n'); | |
252b5132 RH |
2085 | if (dump_section_headers) |
2086 | dump_headers (abfd); | |
026df7c5 | 2087 | |
252b5132 | 2088 | if (dump_symtab || dump_reloc_info || disassemble || dump_debugging) |
026df7c5 | 2089 | syms = slurp_symtab (abfd); |
252b5132 | 2090 | if (dump_dynamic_symtab || dump_dynamic_reloc_info) |
026df7c5 NC |
2091 | dynsyms = slurp_dynamic_symtab (abfd); |
2092 | ||
252b5132 | 2093 | if (dump_symtab) |
b34976b6 | 2094 | dump_symbols (abfd, FALSE); |
252b5132 | 2095 | if (dump_dynamic_symtab) |
b34976b6 | 2096 | dump_symbols (abfd, TRUE); |
252b5132 RH |
2097 | if (dump_stab_section_info) |
2098 | dump_stabs (abfd); | |
2099 | if (dump_reloc_info && ! disassemble) | |
2100 | dump_relocs (abfd); | |
2101 | if (dump_dynamic_reloc_info) | |
2102 | dump_dynamic_relocs (abfd); | |
2103 | if (dump_section_contents) | |
2104 | dump_data (abfd); | |
2105 | if (disassemble) | |
2106 | disassemble_data (abfd); | |
2107 | if (dump_debugging) | |
2108 | { | |
2109 | PTR dhandle; | |
2110 | ||
2111 | dhandle = read_debugging_info (abfd, syms, symcount); | |
2112 | if (dhandle != NULL) | |
2113 | { | |
51cdc6e0 NC |
2114 | if (! print_debugging_info (stdout, dhandle, abfd, syms, demangle, |
2115 | dump_debugging_tags ? TRUE : FALSE)) | |
75cd796a | 2116 | { |
37cc8ec1 AM |
2117 | non_fatal (_("%s: printing debugging information failed"), |
2118 | bfd_get_filename (abfd)); | |
75cd796a ILT |
2119 | exit_status = 1; |
2120 | } | |
252b5132 RH |
2121 | } |
2122 | } | |
026df7c5 | 2123 | |
252b5132 RH |
2124 | if (syms) |
2125 | { | |
2126 | free (syms); | |
2127 | syms = NULL; | |
2128 | } | |
026df7c5 | 2129 | |
252b5132 RH |
2130 | if (dynsyms) |
2131 | { | |
2132 | free (dynsyms); | |
2133 | dynsyms = NULL; | |
2134 | } | |
2135 | } | |
2136 | ||
d24de309 ILT |
2137 | static void |
2138 | display_bfd (abfd) | |
2139 | bfd *abfd; | |
2140 | { | |
2141 | char **matching; | |
2142 | ||
2143 | if (bfd_check_format_matches (abfd, bfd_object, &matching)) | |
2144 | { | |
2145 | dump_bfd (abfd); | |
2146 | return; | |
2147 | } | |
2148 | ||
2149 | if (bfd_get_error () == bfd_error_file_ambiguously_recognized) | |
2150 | { | |
2151 | nonfatal (bfd_get_filename (abfd)); | |
2152 | list_matching_formats (matching); | |
2153 | free (matching); | |
2154 | return; | |
2155 | } | |
2156 | ||
2157 | if (bfd_get_error () != bfd_error_file_not_recognized) | |
2158 | { | |
2159 | nonfatal (bfd_get_filename (abfd)); | |
2160 | return; | |
2161 | } | |
2162 | ||
2163 | if (bfd_check_format_matches (abfd, bfd_core, &matching)) | |
2164 | { | |
2165 | dump_bfd (abfd); | |
2166 | return; | |
2167 | } | |
2168 | ||
2169 | nonfatal (bfd_get_filename (abfd)); | |
2170 | ||
2171 | if (bfd_get_error () == bfd_error_file_ambiguously_recognized) | |
2172 | { | |
2173 | list_matching_formats (matching); | |
2174 | free (matching); | |
2175 | } | |
2176 | } | |
2177 | ||
252b5132 RH |
2178 | static void |
2179 | display_file (filename, target) | |
2180 | char *filename; | |
2181 | char *target; | |
2182 | { | |
2183 | bfd *file, *arfile = (bfd *) NULL; | |
2184 | ||
2185 | file = bfd_openr (filename, target); | |
2186 | if (file == NULL) | |
2187 | { | |
75cd796a | 2188 | nonfatal (filename); |
252b5132 RH |
2189 | return; |
2190 | } | |
2191 | ||
b34976b6 | 2192 | if (bfd_check_format (file, bfd_archive)) |
252b5132 RH |
2193 | { |
2194 | bfd *last_arfile = NULL; | |
2195 | ||
2196 | printf (_("In archive %s:\n"), bfd_get_filename (file)); | |
2197 | for (;;) | |
2198 | { | |
2199 | bfd_set_error (bfd_error_no_error); | |
2200 | ||
2201 | arfile = bfd_openr_next_archived_file (file, arfile); | |
2202 | if (arfile == NULL) | |
2203 | { | |
2204 | if (bfd_get_error () != bfd_error_no_more_archived_files) | |
75cd796a | 2205 | nonfatal (bfd_get_filename (file)); |
252b5132 RH |
2206 | break; |
2207 | } | |
2208 | ||
2209 | display_bfd (arfile); | |
2210 | ||
2211 | if (last_arfile != NULL) | |
2212 | bfd_close (last_arfile); | |
2213 | last_arfile = arfile; | |
2214 | } | |
2215 | ||
2216 | if (last_arfile != NULL) | |
2217 | bfd_close (last_arfile); | |
2218 | } | |
2219 | else | |
2220 | display_bfd (file); | |
2221 | ||
2222 | bfd_close (file); | |
2223 | } | |
2224 | \f | |
98a91d6a | 2225 | /* Actually display the various requested regions. */ |
252b5132 RH |
2226 | |
2227 | static void | |
2228 | dump_data (abfd) | |
2229 | bfd *abfd; | |
2230 | { | |
2231 | asection *section; | |
2232 | bfd_byte *data = 0; | |
2233 | bfd_size_type datasize = 0; | |
940b2b78 TW |
2234 | bfd_size_type addr_offset; |
2235 | bfd_size_type start_offset, stop_offset; | |
f6af82bd | 2236 | unsigned int opb = bfd_octets_per_byte (abfd); |
252b5132 RH |
2237 | |
2238 | for (section = abfd->sections; section != NULL; section = | |
2239 | section->next) | |
2240 | { | |
2241 | int onaline = 16; | |
6e50c90c | 2242 | size_t i; |
252b5132 | 2243 | |
6e50c90c L |
2244 | for (i = 0; i < only_used; i++) |
2245 | if (strcmp (only [i], section->name) == 0) | |
2246 | break; | |
2247 | ||
2248 | if (only_used == 0 || i != only_used) | |
252b5132 RH |
2249 | { |
2250 | if (section->flags & SEC_HAS_CONTENTS) | |
2251 | { | |
335a1869 AO |
2252 | char buf[64]; |
2253 | int count, width; | |
2254 | ||
252b5132 RH |
2255 | printf (_("Contents of section %s:\n"), section->name); |
2256 | ||
2257 | if (bfd_section_size (abfd, section) == 0) | |
2258 | continue; | |
2259 | data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section)); | |
2260 | datasize = bfd_section_size (abfd, section); | |
2261 | ||
2262 | ||
2263 | bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section)); | |
2264 | ||
2265 | if (start_address == (bfd_vma) -1 | |
2266 | || start_address < section->vma) | |
940b2b78 | 2267 | start_offset = 0; |
252b5132 | 2268 | else |
940b2b78 | 2269 | start_offset = start_address - section->vma; |
252b5132 | 2270 | if (stop_address == (bfd_vma) -1) |
940b2b78 | 2271 | stop_offset = bfd_section_size (abfd, section) / opb; |
252b5132 RH |
2272 | else |
2273 | { | |
2274 | if (stop_address < section->vma) | |
940b2b78 | 2275 | stop_offset = 0; |
252b5132 | 2276 | else |
940b2b78 TW |
2277 | stop_offset = stop_address - section->vma; |
2278 | if (stop_offset > bfd_section_size (abfd, section) / opb) | |
2279 | stop_offset = bfd_section_size (abfd, section) / opb; | |
252b5132 | 2280 | } |
335a1869 AO |
2281 | |
2282 | width = 4; | |
2283 | ||
2284 | bfd_sprintf_vma (abfd, buf, start_offset + section->vma); | |
2285 | if (strlen (buf) >= sizeof (buf)) | |
2286 | abort (); | |
2287 | count = 0; | |
2288 | while (buf[count] == '0' && buf[count+1] != '\0') | |
2289 | count++; | |
2290 | count = strlen (buf) - count; | |
2291 | if (count > width) | |
2292 | width = count; | |
2293 | ||
2294 | bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1); | |
2295 | if (strlen (buf) >= sizeof (buf)) | |
2296 | abort (); | |
2297 | count = 0; | |
2298 | while (buf[count] == '0' && buf[count+1] != '\0') | |
2299 | count++; | |
2300 | count = strlen (buf) - count; | |
2301 | if (count > width) | |
2302 | width = count; | |
2303 | ||
0af11b59 | 2304 | for (addr_offset = start_offset; |
8ab56b6f | 2305 | addr_offset < stop_offset; addr_offset += onaline / opb) |
252b5132 RH |
2306 | { |
2307 | bfd_size_type j; | |
2308 | ||
335a1869 AO |
2309 | bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma)); |
2310 | count = strlen (buf); | |
6e50c90c | 2311 | if ((size_t) count >= sizeof (buf)) |
335a1869 AO |
2312 | abort (); |
2313 | putchar (' '); | |
2314 | while (count < width) | |
2315 | { | |
2316 | putchar ('0'); | |
2317 | count++; | |
2318 | } | |
2319 | fputs (buf + count - width, stdout); | |
2320 | putchar (' '); | |
2321 | ||
0af11b59 KH |
2322 | for (j = addr_offset * opb; |
2323 | j < addr_offset * opb + onaline; j++) | |
252b5132 | 2324 | { |
940b2b78 | 2325 | if (j < stop_offset * opb) |
252b5132 RH |
2326 | printf ("%02x", (unsigned) (data[j])); |
2327 | else | |
2328 | printf (" "); | |
2329 | if ((j & 3) == 3) | |
2330 | printf (" "); | |
2331 | } | |
2332 | ||
2333 | printf (" "); | |
8ab56b6f AM |
2334 | for (j = addr_offset * opb; |
2335 | j < addr_offset * opb + onaline; j++) | |
252b5132 | 2336 | { |
940b2b78 | 2337 | if (j >= stop_offset * opb) |
252b5132 RH |
2338 | printf (" "); |
2339 | else | |
3882b010 | 2340 | printf ("%c", ISPRINT (data[j]) ? data[j] : '.'); |
252b5132 RH |
2341 | } |
2342 | putchar ('\n'); | |
2343 | } | |
2344 | free (data); | |
2345 | } | |
2346 | } | |
2347 | } | |
2348 | } | |
2349 | ||
98a91d6a NC |
2350 | /* Should perhaps share code and display with nm? */ |
2351 | ||
252b5132 RH |
2352 | static void |
2353 | dump_symbols (abfd, dynamic) | |
b4c96d0d | 2354 | bfd *abfd ATTRIBUTE_UNUSED; |
b34976b6 | 2355 | bfd_boolean dynamic; |
252b5132 RH |
2356 | { |
2357 | asymbol **current; | |
2358 | long max; | |
2359 | long count; | |
2360 | ||
2361 | if (dynamic) | |
2362 | { | |
2363 | current = dynsyms; | |
2364 | max = dynsymcount; | |
252b5132 RH |
2365 | printf ("DYNAMIC SYMBOL TABLE:\n"); |
2366 | } | |
2367 | else | |
2368 | { | |
2369 | current = syms; | |
2370 | max = symcount; | |
252b5132 RH |
2371 | printf ("SYMBOL TABLE:\n"); |
2372 | } | |
2373 | ||
a1df01d1 AM |
2374 | if (max == 0) |
2375 | printf (_("no symbols\n")); | |
2376 | ||
252b5132 RH |
2377 | for (count = 0; count < max; count++) |
2378 | { | |
2379 | if (*current) | |
2380 | { | |
2381 | bfd *cur_bfd = bfd_asymbol_bfd (*current); | |
2382 | ||
2383 | if (cur_bfd != NULL) | |
2384 | { | |
2385 | const char *name; | |
2386 | char *alloc; | |
2387 | ||
a6637ec0 | 2388 | name = (*current)->name; |
252b5132 RH |
2389 | alloc = NULL; |
2390 | if (do_demangle && name != NULL && *name != '\0') | |
2391 | { | |
252b5132 RH |
2392 | /* If we want to demangle the name, we demangle it |
2393 | here, and temporarily clobber it while calling | |
2394 | bfd_print_symbol. FIXME: This is a gross hack. */ | |
a6637ec0 AM |
2395 | alloc = demangle (cur_bfd, name); |
2396 | (*current)->name = alloc; | |
252b5132 RH |
2397 | } |
2398 | ||
2399 | bfd_print_symbol (cur_bfd, stdout, *current, | |
2400 | bfd_print_symbol_all); | |
2401 | ||
2402 | (*current)->name = name; | |
2403 | if (alloc != NULL) | |
2404 | free (alloc); | |
2405 | ||
2406 | printf ("\n"); | |
2407 | } | |
2408 | } | |
2409 | current++; | |
2410 | } | |
2411 | printf ("\n"); | |
2412 | printf ("\n"); | |
2413 | } | |
2414 | ||
2415 | static void | |
2416 | dump_relocs (abfd) | |
2417 | bfd *abfd; | |
2418 | { | |
2419 | arelent **relpp; | |
2420 | long relcount; | |
2421 | asection *a; | |
2422 | ||
2423 | for (a = abfd->sections; a != (asection *) NULL; a = a->next) | |
2424 | { | |
2425 | long relsize; | |
2426 | ||
2427 | if (bfd_is_abs_section (a)) | |
2428 | continue; | |
2429 | if (bfd_is_und_section (a)) | |
2430 | continue; | |
2431 | if (bfd_is_com_section (a)) | |
2432 | continue; | |
2433 | ||
2434 | if (only) | |
2435 | { | |
6e50c90c L |
2436 | size_t i; |
2437 | ||
2438 | for (i = 0; i < only_used; i++) | |
2439 | if (strcmp (only [i], a->name) == 0) | |
2440 | break; | |
2441 | ||
2442 | if (i == only_used) | |
252b5132 RH |
2443 | continue; |
2444 | } | |
2445 | else if ((a->flags & SEC_RELOC) == 0) | |
2446 | continue; | |
2447 | ||
2448 | relsize = bfd_get_reloc_upper_bound (abfd, a); | |
2449 | if (relsize < 0) | |
2450 | bfd_fatal (bfd_get_filename (abfd)); | |
2451 | ||
2452 | printf ("RELOCATION RECORDS FOR [%s]:", a->name); | |
2453 | ||
2454 | if (relsize == 0) | |
2455 | { | |
2456 | printf (" (none)\n\n"); | |
2457 | } | |
2458 | else | |
2459 | { | |
2460 | relpp = (arelent **) xmalloc (relsize); | |
2461 | relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms); | |
98a91d6a | 2462 | |
252b5132 RH |
2463 | if (relcount < 0) |
2464 | bfd_fatal (bfd_get_filename (abfd)); | |
2465 | else if (relcount == 0) | |
98a91d6a | 2466 | printf (" (none)\n\n"); |
252b5132 RH |
2467 | else |
2468 | { | |
2469 | printf ("\n"); | |
2470 | dump_reloc_set (abfd, a, relpp, relcount); | |
2471 | printf ("\n\n"); | |
2472 | } | |
2473 | free (relpp); | |
2474 | } | |
2475 | } | |
2476 | } | |
2477 | ||
2478 | static void | |
2479 | dump_dynamic_relocs (abfd) | |
2480 | bfd *abfd; | |
2481 | { | |
2482 | long relsize; | |
2483 | arelent **relpp; | |
2484 | long relcount; | |
2485 | ||
2486 | relsize = bfd_get_dynamic_reloc_upper_bound (abfd); | |
2487 | if (relsize < 0) | |
2488 | bfd_fatal (bfd_get_filename (abfd)); | |
2489 | ||
2490 | printf ("DYNAMIC RELOCATION RECORDS"); | |
2491 | ||
2492 | if (relsize == 0) | |
98a91d6a | 2493 | printf (" (none)\n\n"); |
252b5132 RH |
2494 | else |
2495 | { | |
2496 | relpp = (arelent **) xmalloc (relsize); | |
2497 | relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms); | |
98a91d6a | 2498 | |
252b5132 RH |
2499 | if (relcount < 0) |
2500 | bfd_fatal (bfd_get_filename (abfd)); | |
2501 | else if (relcount == 0) | |
98a91d6a | 2502 | printf (" (none)\n\n"); |
252b5132 RH |
2503 | else |
2504 | { | |
2505 | printf ("\n"); | |
2506 | dump_reloc_set (abfd, (asection *) NULL, relpp, relcount); | |
2507 | printf ("\n\n"); | |
2508 | } | |
2509 | free (relpp); | |
2510 | } | |
2511 | } | |
2512 | ||
2513 | static void | |
2514 | dump_reloc_set (abfd, sec, relpp, relcount) | |
2515 | bfd *abfd; | |
2516 | asection *sec; | |
2517 | arelent **relpp; | |
2518 | long relcount; | |
2519 | { | |
2520 | arelent **p; | |
2521 | char *last_filename, *last_functionname; | |
2522 | unsigned int last_line; | |
2523 | ||
2524 | /* Get column headers lined up reasonably. */ | |
2525 | { | |
2526 | static int width; | |
98a91d6a | 2527 | |
252b5132 RH |
2528 | if (width == 0) |
2529 | { | |
2530 | char buf[30]; | |
d8180c76 | 2531 | bfd_sprintf_vma (abfd, buf, (bfd_vma) -1); |
252b5132 RH |
2532 | width = strlen (buf) - 7; |
2533 | } | |
2534 | printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, ""); | |
2535 | } | |
2536 | ||
2537 | last_filename = NULL; | |
2538 | last_functionname = NULL; | |
2539 | last_line = 0; | |
2540 | ||
2541 | for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--) | |
2542 | { | |
2543 | arelent *q = *p; | |
2544 | const char *filename, *functionname; | |
2545 | unsigned int line; | |
2546 | const char *sym_name; | |
2547 | const char *section_name; | |
2548 | ||
2549 | if (start_address != (bfd_vma) -1 | |
2550 | && q->address < start_address) | |
2551 | continue; | |
2552 | if (stop_address != (bfd_vma) -1 | |
2553 | && q->address > stop_address) | |
2554 | continue; | |
2555 | ||
2556 | if (with_line_numbers | |
2557 | && sec != NULL | |
2558 | && bfd_find_nearest_line (abfd, sec, syms, q->address, | |
2559 | &filename, &functionname, &line)) | |
2560 | { | |
2561 | if (functionname != NULL | |
2562 | && (last_functionname == NULL | |
2563 | || strcmp (functionname, last_functionname) != 0)) | |
2564 | { | |
2565 | printf ("%s():\n", functionname); | |
2566 | if (last_functionname != NULL) | |
2567 | free (last_functionname); | |
2568 | last_functionname = xstrdup (functionname); | |
2569 | } | |
98a91d6a | 2570 | |
252b5132 RH |
2571 | if (line > 0 |
2572 | && (line != last_line | |
2573 | || (filename != NULL | |
2574 | && last_filename != NULL | |
2575 | && strcmp (filename, last_filename) != 0))) | |
2576 | { | |
2577 | printf ("%s:%u\n", filename == NULL ? "???" : filename, line); | |
2578 | last_line = line; | |
2579 | if (last_filename != NULL) | |
2580 | free (last_filename); | |
2581 | if (filename == NULL) | |
2582 | last_filename = NULL; | |
2583 | else | |
2584 | last_filename = xstrdup (filename); | |
2585 | } | |
2586 | } | |
2587 | ||
2588 | if (q->sym_ptr_ptr && *q->sym_ptr_ptr) | |
2589 | { | |
2590 | sym_name = (*(q->sym_ptr_ptr))->name; | |
2591 | section_name = (*(q->sym_ptr_ptr))->section->name; | |
2592 | } | |
2593 | else | |
2594 | { | |
2595 | sym_name = NULL; | |
2596 | section_name = NULL; | |
2597 | } | |
98a91d6a | 2598 | |
252b5132 RH |
2599 | if (sym_name) |
2600 | { | |
d8180c76 | 2601 | bfd_printf_vma (abfd, q->address); |
09cda596 DD |
2602 | if (q->howto->name) |
2603 | printf (" %-16s ", q->howto->name); | |
2604 | else | |
2605 | printf (" %-16d ", q->howto->type); | |
252b5132 RH |
2606 | objdump_print_symname (abfd, (struct disassemble_info *) NULL, |
2607 | *q->sym_ptr_ptr); | |
2608 | } | |
2609 | else | |
2610 | { | |
b1f88ebe | 2611 | if (section_name == (const char *) NULL) |
252b5132 | 2612 | section_name = "*unknown*"; |
d8180c76 | 2613 | bfd_printf_vma (abfd, q->address); |
252b5132 RH |
2614 | printf (" %-16s [%s]", |
2615 | q->howto->name, | |
2616 | section_name); | |
2617 | } | |
98a91d6a | 2618 | |
252b5132 RH |
2619 | if (q->addend) |
2620 | { | |
2621 | printf ("+0x"); | |
d8180c76 | 2622 | bfd_printf_vma (abfd, q->addend); |
252b5132 | 2623 | } |
98a91d6a | 2624 | |
252b5132 RH |
2625 | printf ("\n"); |
2626 | } | |
2627 | } | |
2628 | \f | |
65de42c0 TS |
2629 | int main PARAMS ((int, char **)); |
2630 | ||
252b5132 RH |
2631 | int |
2632 | main (argc, argv) | |
2633 | int argc; | |
2634 | char **argv; | |
2635 | { | |
2636 | int c; | |
2637 | char *target = default_target; | |
b34976b6 | 2638 | bfd_boolean seenflag = FALSE; |
252b5132 RH |
2639 | |
2640 | #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) | |
2641 | setlocale (LC_MESSAGES, ""); | |
3882b010 L |
2642 | #endif |
2643 | #if defined (HAVE_SETLOCALE) | |
2644 | setlocale (LC_CTYPE, ""); | |
252b5132 RH |
2645 | #endif |
2646 | bindtextdomain (PACKAGE, LOCALEDIR); | |
2647 | textdomain (PACKAGE); | |
2648 | ||
2649 | program_name = *argv; | |
2650 | xmalloc_set_program_name (program_name); | |
2651 | ||
2652 | START_PROGRESS (program_name, 0); | |
2653 | ||
2654 | bfd_init (); | |
2655 | set_default_bfd_target (); | |
2656 | ||
51cdc6e0 | 2657 | while ((c = getopt_long (argc, argv, "pib:m:M:VvCdDlfaHhrRtTxsSj:wE:zgeG", |
252b5132 RH |
2658 | long_options, (int *) 0)) |
2659 | != EOF) | |
2660 | { | |
252b5132 RH |
2661 | switch (c) |
2662 | { | |
2663 | case 0: | |
8b53311e | 2664 | break; /* We've been given a long option. */ |
252b5132 RH |
2665 | case 'm': |
2666 | machine = optarg; | |
2667 | break; | |
dd92f639 | 2668 | case 'M': |
073fbac6 | 2669 | if (disassembler_options) |
31e0f3cd NC |
2670 | /* Ignore potential memory leak for now. */ |
2671 | disassembler_options = concat (disassembler_options, ",", optarg, NULL); | |
2672 | else | |
2673 | disassembler_options = optarg; | |
dd92f639 | 2674 | break; |
252b5132 | 2675 | case 'j': |
6e50c90c L |
2676 | if (only == NULL) |
2677 | { | |
2678 | only_size = 8; | |
2679 | only = (char **) xmalloc (only_size * sizeof (char *)); | |
2680 | } | |
2681 | else if (only_used == only_size) | |
2682 | { | |
2683 | only_size += 8; | |
2684 | only = (char **) xrealloc (only, | |
2685 | only_size * sizeof (char *)); | |
2686 | } | |
2687 | only [only_used++] = optarg; | |
252b5132 RH |
2688 | break; |
2689 | case 'l': | |
b34976b6 | 2690 | with_line_numbers = TRUE; |
252b5132 RH |
2691 | break; |
2692 | case 'b': | |
2693 | target = optarg; | |
2694 | break; | |
1dada9c5 | 2695 | case 'C': |
b34976b6 | 2696 | do_demangle = TRUE; |
28c309a2 NC |
2697 | if (optarg != NULL) |
2698 | { | |
2699 | enum demangling_styles style; | |
8b53311e | 2700 | |
28c309a2 | 2701 | style = cplus_demangle_name_to_style (optarg); |
0af11b59 | 2702 | if (style == unknown_demangling) |
28c309a2 NC |
2703 | fatal (_("unknown demangling style `%s'"), |
2704 | optarg); | |
8b53311e | 2705 | |
28c309a2 | 2706 | cplus_demangle_set_style (style); |
0af11b59 | 2707 | } |
1dada9c5 NC |
2708 | break; |
2709 | case 'w': | |
b34976b6 | 2710 | wide_output = TRUE; |
1dada9c5 NC |
2711 | break; |
2712 | case OPTION_ADJUST_VMA: | |
2713 | adjust_section_vma = parse_vma (optarg, "--adjust-vma"); | |
2714 | break; | |
2715 | case OPTION_START_ADDRESS: | |
2716 | start_address = parse_vma (optarg, "--start-address"); | |
2717 | break; | |
2718 | case OPTION_STOP_ADDRESS: | |
2719 | stop_address = parse_vma (optarg, "--stop-address"); | |
2720 | break; | |
2721 | case 'E': | |
2722 | if (strcmp (optarg, "B") == 0) | |
2723 | endian = BFD_ENDIAN_BIG; | |
2724 | else if (strcmp (optarg, "L") == 0) | |
2725 | endian = BFD_ENDIAN_LITTLE; | |
2726 | else | |
2727 | { | |
37cc8ec1 | 2728 | non_fatal (_("unrecognized -E option")); |
1dada9c5 NC |
2729 | usage (stderr, 1); |
2730 | } | |
2731 | break; | |
2732 | case OPTION_ENDIAN: | |
2733 | if (strncmp (optarg, "big", strlen (optarg)) == 0) | |
2734 | endian = BFD_ENDIAN_BIG; | |
2735 | else if (strncmp (optarg, "little", strlen (optarg)) == 0) | |
2736 | endian = BFD_ENDIAN_LITTLE; | |
2737 | else | |
2738 | { | |
37cc8ec1 | 2739 | non_fatal (_("unrecognized --endian type `%s'"), optarg); |
1dada9c5 NC |
2740 | usage (stderr, 1); |
2741 | } | |
2742 | break; | |
8b53311e | 2743 | |
252b5132 | 2744 | case 'f': |
b34976b6 AM |
2745 | dump_file_header = TRUE; |
2746 | seenflag = TRUE; | |
252b5132 RH |
2747 | break; |
2748 | case 'i': | |
b34976b6 AM |
2749 | formats_info = TRUE; |
2750 | seenflag = TRUE; | |
252b5132 RH |
2751 | break; |
2752 | case 'p': | |
b34976b6 AM |
2753 | dump_private_headers = TRUE; |
2754 | seenflag = TRUE; | |
252b5132 RH |
2755 | break; |
2756 | case 'x': | |
b34976b6 AM |
2757 | dump_private_headers = TRUE; |
2758 | dump_symtab = TRUE; | |
2759 | dump_reloc_info = TRUE; | |
2760 | dump_file_header = TRUE; | |
2761 | dump_ar_hdrs = TRUE; | |
2762 | dump_section_headers = TRUE; | |
2763 | seenflag = TRUE; | |
252b5132 RH |
2764 | break; |
2765 | case 't': | |
b34976b6 AM |
2766 | dump_symtab = TRUE; |
2767 | seenflag = TRUE; | |
252b5132 RH |
2768 | break; |
2769 | case 'T': | |
b34976b6 AM |
2770 | dump_dynamic_symtab = TRUE; |
2771 | seenflag = TRUE; | |
252b5132 RH |
2772 | break; |
2773 | case 'd': | |
b34976b6 AM |
2774 | disassemble = TRUE; |
2775 | seenflag = TRUE; | |
1dada9c5 NC |
2776 | break; |
2777 | case 'z': | |
b34976b6 | 2778 | disassemble_zeroes = TRUE; |
252b5132 RH |
2779 | break; |
2780 | case 'D': | |
b34976b6 AM |
2781 | disassemble = TRUE; |
2782 | disassemble_all = TRUE; | |
2783 | seenflag = TRUE; | |
252b5132 RH |
2784 | break; |
2785 | case 'S': | |
b34976b6 AM |
2786 | disassemble = TRUE; |
2787 | with_source_code = TRUE; | |
2788 | seenflag = TRUE; | |
1dada9c5 NC |
2789 | break; |
2790 | case 'g': | |
2791 | dump_debugging = 1; | |
b34976b6 | 2792 | seenflag = TRUE; |
1dada9c5 | 2793 | break; |
51cdc6e0 NC |
2794 | case 'e': |
2795 | dump_debugging = 1; | |
2796 | dump_debugging_tags = 1; | |
2797 | do_demangle = TRUE; | |
2798 | seenflag = TRUE; | |
2799 | break; | |
1dada9c5 | 2800 | case 'G': |
b34976b6 AM |
2801 | dump_stab_section_info = TRUE; |
2802 | seenflag = TRUE; | |
252b5132 RH |
2803 | break; |
2804 | case 's': | |
b34976b6 AM |
2805 | dump_section_contents = TRUE; |
2806 | seenflag = TRUE; | |
252b5132 RH |
2807 | break; |
2808 | case 'r': | |
b34976b6 AM |
2809 | dump_reloc_info = TRUE; |
2810 | seenflag = TRUE; | |
252b5132 RH |
2811 | break; |
2812 | case 'R': | |
b34976b6 AM |
2813 | dump_dynamic_reloc_info = TRUE; |
2814 | seenflag = TRUE; | |
252b5132 RH |
2815 | break; |
2816 | case 'a': | |
b34976b6 AM |
2817 | dump_ar_hdrs = TRUE; |
2818 | seenflag = TRUE; | |
252b5132 RH |
2819 | break; |
2820 | case 'h': | |
b34976b6 AM |
2821 | dump_section_headers = TRUE; |
2822 | seenflag = TRUE; | |
252b5132 RH |
2823 | break; |
2824 | case 'H': | |
2825 | usage (stdout, 0); | |
b34976b6 | 2826 | seenflag = TRUE; |
8b53311e | 2827 | case 'v': |
252b5132 | 2828 | case 'V': |
b34976b6 AM |
2829 | show_version = TRUE; |
2830 | seenflag = TRUE; | |
252b5132 | 2831 | break; |
0af11b59 | 2832 | |
252b5132 RH |
2833 | default: |
2834 | usage (stderr, 1); | |
2835 | } | |
2836 | } | |
2837 | ||
2838 | if (show_version) | |
2839 | print_version ("objdump"); | |
2840 | ||
b34976b6 | 2841 | if (!seenflag) |
1dada9c5 | 2842 | usage (stderr, 2); |
252b5132 RH |
2843 | |
2844 | if (formats_info) | |
06d86cf7 | 2845 | exit_status = display_info (); |
252b5132 RH |
2846 | else |
2847 | { | |
2848 | if (optind == argc) | |
2849 | display_file ("a.out", target); | |
2850 | else | |
2851 | for (; optind < argc;) | |
2852 | display_file (argv[optind++], target); | |
2853 | } | |
2854 | ||
2855 | END_PROGRESS (program_name); | |
2856 | ||
75cd796a | 2857 | return exit_status; |
252b5132 | 2858 | } |