]>
Commit | Line | Data |
---|---|---|
d20f480f | 1 | /* objdump.c -- dump information about an object file. |
37853673 | 2 | Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. |
2fa0b342 | 3 | |
b3a2b497 | 4 | This file is part of GNU Binutils. |
2fa0b342 | 5 | |
b3a2b497 | 6 | This program is free software; you can redistribute it and/or modify |
2fa0b342 | 7 | it under the terms of the GNU General Public License as published by |
d20f480f | 8 | the Free Software Foundation; either version 2, or (at your option) |
2fa0b342 DHW |
9 | any later version. |
10 | ||
b3a2b497 | 11 | This program is distributed in the hope that it will be useful, |
2fa0b342 DHW |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
b3a2b497 ILT |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
2fa0b342 | 19 | |
2fa0b342 | 20 | #include "bfd.h" |
d20f480f | 21 | #include "sysdep.h" |
2fa0b342 | 22 | #include "getopt.h" |
e1ec9f07 | 23 | #include "bucomm.h" |
2fa0b342 DHW |
24 | #include <stdio.h> |
25 | #include <ctype.h> | |
2e8adbd7 | 26 | #include "dis-asm.h" |
2fa0b342 | 27 | |
73b8f102 JG |
28 | /* Internal headers for the ELF .stab-dump code - sorry. */ |
29 | #define BYTES_IN_WORD 32 | |
30 | #include "aout/aout64.h" | |
31 | #include "elf/internal.h" | |
d086adf8 | 32 | extern Elf_Internal_Shdr *bfd_elf_find_section(); |
bf661056 | 33 | |
80d19ec1 | 34 | #ifndef FPRINTF_ALREADY_DECLARED |
6f575704 | 35 | extern int fprintf PARAMS ((FILE *, CONST char *, ...)); |
80d19ec1 | 36 | #endif |
2fa0b342 DHW |
37 | |
38 | char *default_target = NULL; /* default at runtime */ | |
39 | ||
e1ec9f07 | 40 | extern char *program_version; |
2fa0b342 | 41 | |
249c6fc0 | 42 | int show_version = 0; /* show the version number */ |
2fa0b342 DHW |
43 | int dump_section_contents; /* -s */ |
44 | int dump_section_headers; /* -h */ | |
45 | boolean dump_file_header; /* -f */ | |
46 | int dump_symtab; /* -t */ | |
de3b08ac | 47 | int dump_dynamic_symtab; /* -T */ |
2fa0b342 | 48 | int dump_reloc_info; /* -r */ |
de3b08ac | 49 | int dump_dynamic_reloc_info; /* -R */ |
2fa0b342 | 50 | int dump_ar_hdrs; /* -a */ |
aa0a709a | 51 | int with_line_numbers; /* -l */ |
9b018ecd | 52 | int dump_stab_section_info; /* --stabs */ |
aa0a709a | 53 | boolean disassemble; /* -d */ |
e1ec9f07 | 54 | boolean formats_info; /* -i */ |
195d1adf KR |
55 | char *only; /* -j secname */ |
56 | ||
cef35d48 | 57 | /* Extra info to pass to the disassembler address printing function. */ |
195d1adf KR |
58 | struct objdump_disasm_info { |
59 | bfd *abfd; | |
60 | asection *sec; | |
61 | }; | |
2fa0b342 | 62 | |
cef35d48 | 63 | /* Architecture to disassemble for, or default if NULL. */ |
aa0a709a | 64 | char *machine = (char *) NULL; |
f7b839f7 DM |
65 | |
66 | /* The symbol table. */ | |
aa0a709a | 67 | asymbol **syms; |
2fa0b342 | 68 | |
f7b839f7 | 69 | /* Number of symbols in `syms'. */ |
ae5d2ff5 | 70 | long symcount = 0; |
2fa0b342 | 71 | |
de3b08ac ILT |
72 | /* The dynamic symbol table. */ |
73 | asymbol **dynsyms; | |
74 | ||
75 | /* Number of symbols in `dynsyms'. */ | |
76 | long dynsymcount = 0; | |
77 | ||
d9971b83 KR |
78 | /* Forward declarations. */ |
79 | ||
80 | static void | |
81 | display_file PARAMS ((char *filename, char *target)); | |
82 | ||
83 | static void | |
84 | dump_data PARAMS ((bfd *abfd)); | |
85 | ||
86 | static void | |
87 | dump_relocs PARAMS ((bfd *abfd)); | |
88 | ||
89 | static void | |
de3b08ac ILT |
90 | dump_dynamic_relocs PARAMS ((bfd * abfd)); |
91 | ||
92 | static void | |
93 | dump_reloc_set PARAMS ((bfd *, arelent **, long)); | |
94 | ||
95 | static void | |
96 | dump_symbols PARAMS ((bfd *abfd, boolean dynamic)); | |
02a68547 ILT |
97 | |
98 | static void | |
99 | display_bfd PARAMS ((bfd *abfd)); | |
8f197c94 ILT |
100 | |
101 | static void | |
102 | objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *)); | |
d9971b83 | 103 | \f |
2fa0b342 | 104 | void |
b3a2b497 ILT |
105 | usage (stream, status) |
106 | FILE *stream; | |
107 | int status; | |
2fa0b342 | 108 | { |
b3a2b497 | 109 | fprintf (stream, "\ |
de3b08ac | 110 | Usage: %s [-ahifdrRtTxsl] [-b bfdname] [-m machine] [-j section-name]\n\ |
02a68547 ILT |
111 | [--archive-headers] [--target=bfdname] [--disassemble] [--file-headers]\n\ |
112 | [--section-headers] [--headers] [--info] [--section=section-name]\n\ | |
113 | [--line-numbers] [--architecture=machine] [--reloc] [--full-contents]\n\ | |
de3b08ac ILT |
114 | [--stabs] [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\ |
115 | [--version] [--help] objfile...\n\ | |
02a68547 | 116 | at least one option besides -l (--line-numbers) must be given\n", |
b3a2b497 ILT |
117 | program_name); |
118 | exit (status); | |
2fa0b342 DHW |
119 | } |
120 | ||
aa0a709a SC |
121 | static struct option long_options[]= |
122 | { | |
02a68547 ILT |
123 | {"all-headers", no_argument, NULL, 'x'}, |
124 | {"architecture", required_argument, NULL, 'm'}, | |
125 | {"archive-headers", no_argument, NULL, 'a'}, | |
126 | {"disassemble", no_argument, NULL, 'd'}, | |
de3b08ac ILT |
127 | {"dynamic-reloc", no_argument, NULL, 'R'}, |
128 | {"dynamic-syms", no_argument, NULL, 'T'}, | |
02a68547 ILT |
129 | {"file-headers", no_argument, NULL, 'f'}, |
130 | {"full-contents", no_argument, NULL, 's'}, | |
131 | {"headers", no_argument, NULL, 'h'}, | |
132 | {"help", no_argument, NULL, 'H'}, | |
133 | {"info", no_argument, NULL, 'i'}, | |
134 | {"line-numbers", no_argument, NULL, 'l'}, | |
135 | {"reloc", no_argument, NULL, 'r'}, | |
136 | {"section", required_argument, NULL, 'j'}, | |
137 | {"section-headers", no_argument, NULL, 'h'}, | |
73b8f102 | 138 | {"stabs", no_argument, &dump_stab_section_info, 1}, |
02a68547 ILT |
139 | {"syms", no_argument, NULL, 't'}, |
140 | {"target", required_argument, NULL, 'b'}, | |
141 | {"version", no_argument, &show_version, 1}, | |
d2442698 DM |
142 | {0, no_argument, 0, 0} |
143 | }; | |
f7b839f7 | 144 | \f |
2fa0b342 | 145 | static void |
f7b839f7 | 146 | dump_section_header (abfd, section, ignored) |
aa0a709a | 147 | bfd *abfd; |
f7b839f7 DM |
148 | asection *section; |
149 | PTR ignored; | |
2fa0b342 | 150 | { |
f7b839f7 | 151 | char *comma = ""; |
aa0a709a | 152 | |
2fa0b342 | 153 | #define PF(x,y) \ |
f7b839f7 DM |
154 | if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; } |
155 | ||
156 | ||
157 | printf ("SECTION %d [%s]\t: size %08x", | |
158 | section->index, | |
159 | section->name, | |
160 | (unsigned) bfd_get_section_size_before_reloc (section)); | |
161 | printf (" vma "); | |
162 | printf_vma (section->vma); | |
163 | printf (" align 2**%u\n ", | |
164 | section->alignment_power); | |
165 | PF (SEC_ALLOC, "ALLOC"); | |
166 | PF (SEC_CONSTRUCTOR, "CONSTRUCTOR"); | |
167 | PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT"); | |
168 | PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA"); | |
169 | PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS"); | |
170 | PF (SEC_LOAD, "LOAD"); | |
171 | PF (SEC_RELOC, "RELOC"); | |
195d1adf | 172 | #ifdef SEC_BALIGN |
f7b839f7 | 173 | PF (SEC_BALIGN, "BALIGN"); |
195d1adf | 174 | #endif |
f7b839f7 DM |
175 | PF (SEC_READONLY, "READONLY"); |
176 | PF (SEC_CODE, "CODE"); | |
177 | PF (SEC_DATA, "DATA"); | |
178 | PF (SEC_ROM, "ROM"); | |
179 | PF (SEC_DEBUGGING, "DEBUGGING"); | |
180 | printf ("\n"); | |
2fa0b342 | 181 | #undef PF |
2fa0b342 DHW |
182 | } |
183 | ||
f7b839f7 DM |
184 | static void |
185 | dump_headers (abfd) | |
186 | bfd *abfd; | |
187 | { | |
188 | bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL); | |
189 | } | |
190 | \f | |
2fa0b342 | 191 | static asymbol ** |
abdcac0f DM |
192 | slurp_symtab (abfd) |
193 | bfd *abfd; | |
2fa0b342 | 194 | { |
aa0a709a | 195 | asymbol **sy = (asymbol **) NULL; |
ae5d2ff5 | 196 | long storage; |
2fa0b342 | 197 | |
aa0a709a SC |
198 | if (!(bfd_get_file_flags (abfd) & HAS_SYMS)) |
199 | { | |
f7b839f7 DM |
200 | printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd)); |
201 | return NULL; | |
aa0a709a SC |
202 | } |
203 | ||
ae5d2ff5 ILT |
204 | storage = bfd_get_symtab_upper_bound (abfd); |
205 | if (storage < 0) | |
206 | bfd_fatal (bfd_get_filename (abfd)); | |
207 | ||
aa0a709a SC |
208 | if (storage) |
209 | { | |
02a68547 | 210 | sy = (asymbol **) xmalloc (storage); |
aa0a709a SC |
211 | } |
212 | symcount = bfd_canonicalize_symtab (abfd, sy); | |
ae5d2ff5 ILT |
213 | if (symcount < 0) |
214 | bfd_fatal (bfd_get_filename (abfd)); | |
215 | if (symcount == 0) | |
de3b08ac ILT |
216 | fprintf (stderr, "%s: %s: No symbols\n", |
217 | program_name, bfd_get_filename (abfd)); | |
218 | return sy; | |
219 | } | |
220 | ||
221 | /* Read in the dynamic symbols. */ | |
222 | ||
223 | static asymbol ** | |
224 | slurp_dynamic_symtab (abfd) | |
225 | bfd *abfd; | |
226 | { | |
227 | asymbol **sy = (asymbol **) NULL; | |
228 | long storage; | |
229 | ||
230 | if (!(bfd_get_file_flags (abfd) & DYNAMIC)) | |
f50af42b | 231 | { |
de3b08ac | 232 | fprintf (stderr, "%s: %s: not a dynamic object\n", |
f50af42b | 233 | program_name, bfd_get_filename (abfd)); |
de3b08ac | 234 | return NULL; |
f50af42b | 235 | } |
de3b08ac ILT |
236 | |
237 | storage = bfd_get_dynamic_symtab_upper_bound (abfd); | |
238 | if (storage < 0) | |
239 | bfd_fatal (bfd_get_filename (abfd)); | |
240 | ||
241 | if (storage) | |
242 | { | |
243 | sy = (asymbol **) xmalloc (storage); | |
244 | } | |
245 | dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy); | |
246 | if (dynsymcount < 0) | |
247 | bfd_fatal (bfd_get_filename (abfd)); | |
248 | if (dynsymcount == 0) | |
249 | fprintf (stderr, "%s: %s: No dynamic symbols\n", | |
250 | program_name, bfd_get_filename (abfd)); | |
aa0a709a | 251 | return sy; |
2fa0b342 | 252 | } |
aa0a709a | 253 | |
f7b839f7 DM |
254 | /* Filter out (in place) symbols that are useless for disassembly. |
255 | COUNT is the number of elements in SYMBOLS. | |
256 | Return the number of useful symbols. */ | |
3ae36cb6 | 257 | |
ae5d2ff5 | 258 | long |
f7b839f7 DM |
259 | remove_useless_symbols (symbols, count) |
260 | asymbol **symbols; | |
ae5d2ff5 | 261 | long count; |
3ae36cb6 | 262 | { |
f7b839f7 | 263 | register asymbol **in_ptr = symbols, **out_ptr = symbols; |
3ae36cb6 | 264 | |
f7b839f7 | 265 | while (--count >= 0) |
3ae36cb6 PB |
266 | { |
267 | asymbol *sym = *in_ptr++; | |
268 | ||
269 | if (sym->name == NULL || sym->name[0] == '\0') | |
270 | continue; | |
271 | if (sym->flags & (BSF_DEBUGGING)) | |
272 | continue; | |
273 | if (sym->section == &bfd_und_section | |
274 | || bfd_is_com_section (sym->section)) | |
275 | continue; | |
276 | ||
277 | *out_ptr++ = sym; | |
278 | } | |
f7b839f7 | 279 | return out_ptr - symbols; |
3ae36cb6 PB |
280 | } |
281 | ||
37853673 SS |
282 | /* Sort symbols into value order. */ |
283 | ||
aa0a709a | 284 | static int |
37853673 | 285 | compare_symbols (ap, bp) |
770cde30 JG |
286 | PTR ap; |
287 | PTR bp; | |
2fa0b342 | 288 | { |
770cde30 JG |
289 | asymbol *a = *(asymbol **)ap; |
290 | asymbol *b = *(asymbol **)bp; | |
2fa0b342 | 291 | |
3ae36cb6 PB |
292 | if (a->value > b->value) |
293 | return 1; | |
294 | else if (a->value < b->value) | |
295 | return -1; | |
2fa0b342 | 296 | |
3ae36cb6 PB |
297 | if (a->section > b->section) |
298 | return 1; | |
299 | else if (a->section < b->section) | |
300 | return -1; | |
301 | return 0; | |
2fa0b342 DHW |
302 | } |
303 | ||
f7b839f7 DM |
304 | /* Print VMA symbolically to INFO if possible. */ |
305 | ||
8f197c94 | 306 | static void |
545a2768 | 307 | objdump_print_address (vma, info) |
aa0a709a | 308 | bfd_vma vma; |
545a2768 | 309 | struct disassemble_info *info; |
2fa0b342 | 310 | { |
195d1adf KR |
311 | /* @@ For relocateable files, should filter out symbols belonging to |
312 | the wrong section. Unfortunately, not enough information is supplied | |
313 | to this routine to determine the correct section in all cases. */ | |
314 | /* @@ Would it speed things up to cache the last two symbols returned, | |
315 | and maybe their address ranges? For many processors, only one memory | |
316 | operand can be present at a time, so the 2-entry cache wouldn't be | |
317 | constantly churned by code doing heavy memory accesses. */ | |
2fa0b342 | 318 | |
f7b839f7 | 319 | /* Indices in `syms'. */ |
ae5d2ff5 ILT |
320 | long min = 0; |
321 | long max = symcount; | |
322 | long thisplace; | |
2fa0b342 | 323 | |
8f197c94 | 324 | bfd_signed_vma vardiff; |
2fa0b342 | 325 | |
3ae36cb6 PB |
326 | fprintf_vma (info->stream, vma); |
327 | ||
f7b839f7 DM |
328 | if (symcount < 1) |
329 | return; | |
330 | ||
8f197c94 ILT |
331 | /* Perform a binary search looking for the closest symbol to the |
332 | required value. We are searching the range (min, max]. */ | |
333 | while (min + 1 < max) | |
aa0a709a | 334 | { |
f7b839f7 | 335 | asymbol *sym; |
8f197c94 | 336 | |
f7b839f7 | 337 | thisplace = (max + min) / 2; |
f7b839f7 | 338 | sym = syms[thisplace]; |
8f197c94 | 339 | |
f7b839f7 | 340 | vardiff = sym->value - vma; |
f7b839f7 DM |
341 | |
342 | if (vardiff > 0) | |
343 | max = thisplace; | |
344 | else if (vardiff < 0) | |
345 | min = thisplace; | |
346 | else | |
aa0a709a | 347 | { |
8f197c94 ILT |
348 | min = thisplace; |
349 | break; | |
aa0a709a | 350 | } |
f7b839f7 | 351 | } |
fc5d6074 | 352 | |
8f197c94 ILT |
353 | /* The symbol we want is now in min, the low end of the range we |
354 | were searching. */ | |
355 | thisplace = min; | |
356 | ||
f7b839f7 DM |
357 | { |
358 | /* If this symbol isn't global, search for one with the same value | |
359 | that is. */ | |
360 | bfd_vma val = syms[thisplace]->value; | |
ae5d2ff5 | 361 | long i; |
f7b839f7 DM |
362 | if (syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING)) |
363 | for (i = thisplace - 1; i >= 0; i--) | |
aa0a709a | 364 | { |
f7b839f7 DM |
365 | if (syms[i]->value == val |
366 | && (!(syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING)) | |
367 | || ((syms[thisplace]->flags & BSF_DEBUGGING) | |
368 | && !(syms[i]->flags & BSF_DEBUGGING)))) | |
aa0a709a | 369 | { |
f7b839f7 DM |
370 | thisplace = i; |
371 | break; | |
aa0a709a SC |
372 | } |
373 | } | |
f7b839f7 DM |
374 | if (syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING)) |
375 | for (i = thisplace + 1; i < symcount; i++) | |
376 | { | |
377 | if (syms[i]->value == val | |
378 | && (!(syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING)) | |
379 | || ((syms[thisplace]->flags & BSF_DEBUGGING) | |
380 | && !(syms[i]->flags & BSF_DEBUGGING)))) | |
195d1adf | 381 | { |
f7b839f7 DM |
382 | thisplace = i; |
383 | break; | |
195d1adf | 384 | } |
f7b839f7 DM |
385 | } |
386 | } | |
387 | { | |
388 | /* If the file is relocateable, and the symbol could be from this | |
389 | section, prefer a symbol from this section over symbols from | |
390 | others, even if the other symbol's value might be closer. | |
391 | ||
392 | Note that this may be wrong for some symbol references if the | |
393 | sections have overlapping memory ranges, but in that case there's | |
394 | no way to tell what's desired without looking at the relocation | |
395 | table. */ | |
396 | struct objdump_disasm_info *aux; | |
ae5d2ff5 | 397 | long i; |
f7b839f7 DM |
398 | |
399 | aux = (struct objdump_disasm_info *) info->application_data; | |
400 | if ((aux->abfd->flags & HAS_RELOC) | |
401 | && vma >= bfd_get_section_vma (aux->abfd, aux->sec) | |
402 | && vma < (bfd_get_section_vma (aux->abfd, aux->sec) | |
403 | + bfd_get_section_size_before_reloc (aux->sec)) | |
404 | && syms[thisplace]->section != aux->sec) | |
405 | { | |
406 | for (i = thisplace + 1; i < symcount; i++) | |
8f197c94 ILT |
407 | { |
408 | if (syms[i]->value != syms[thisplace]->value) | |
409 | { | |
410 | i--; | |
411 | break; | |
412 | } | |
413 | } | |
414 | for (; i >= 0; i--) | |
415 | { | |
416 | if (syms[i]->section == aux->sec) | |
417 | { | |
418 | thisplace = i; | |
419 | break; | |
420 | } | |
421 | } | |
195d1adf | 422 | } |
f7b839f7 DM |
423 | } |
424 | fprintf (info->stream, " <%s", syms[thisplace]->name); | |
425 | if (syms[thisplace]->value > vma) | |
426 | { | |
427 | char buf[30], *p = buf; | |
428 | sprintf_vma (buf, syms[thisplace]->value - vma); | |
429 | while (*p == '0') | |
430 | p++; | |
431 | fprintf (info->stream, "-%s", p); | |
432 | } | |
433 | else if (vma > syms[thisplace]->value) | |
434 | { | |
435 | char buf[30], *p = buf; | |
436 | sprintf_vma (buf, vma - syms[thisplace]->value); | |
437 | while (*p == '0') | |
438 | p++; | |
439 | fprintf (info->stream, "+%s", p); | |
2fa0b342 | 440 | } |
f7b839f7 | 441 | fprintf (info->stream, ">"); |
2fa0b342 DHW |
442 | } |
443 | ||
195d1adf KR |
444 | #ifdef ARCH_all |
445 | #define ARCH_a29k | |
446 | #define ARCH_alpha | |
447 | #define ARCH_h8300 | |
448 | #define ARCH_h8500 | |
449 | #define ARCH_hppa | |
450 | #define ARCH_i386 | |
451 | #define ARCH_i960 | |
452 | #define ARCH_m68k | |
453 | #define ARCH_m88k | |
454 | #define ARCH_mips | |
8f197c94 | 455 | #define ARCH_powerpc |
722087ec | 456 | #define ARCH_rs6000 |
195d1adf KR |
457 | #define ARCH_sh |
458 | #define ARCH_sparc | |
459 | #define ARCH_z8k | |
460 | #endif | |
461 | ||
2fa0b342 | 462 | void |
aa0a709a SC |
463 | disassemble_data (abfd) |
464 | bfd *abfd; | |
2fa0b342 | 465 | { |
ae5d2ff5 | 466 | long i; |
cef35d48 | 467 | unsigned int (*print) () = 0; /* Old style */ |
2e8adbd7 | 468 | disassembler_ftype disassemble = 0; /* New style */ |
2e8adbd7 | 469 | struct disassemble_info disasm_info; |
195d1adf | 470 | struct objdump_disasm_info aux; |
2e8adbd7 PB |
471 | |
472 | int prevline; | |
473 | CONST char *prev_function = ""; | |
d20f480f | 474 | |
2fa0b342 | 475 | asection *section; |
aa0a709a | 476 | |
96d7950b | 477 | boolean done_dot = false; |
aa0a709a | 478 | |
cef35d48 | 479 | /* Replace symbol section relative values with abs values. */ |
aa0a709a SC |
480 | for (i = 0; i < symcount; i++) |
481 | { | |
2fa0b342 | 482 | syms[i]->value += syms[i]->section->vma; |
aa0a709a | 483 | } |
2fa0b342 | 484 | |
3ae36cb6 | 485 | symcount = remove_useless_symbols (syms, symcount); |
2fa0b342 DHW |
486 | |
487 | /* Sort the symbols into section and symbol order */ | |
f7b839f7 | 488 | qsort (syms, symcount, sizeof (asymbol *), compare_symbols); |
2fa0b342 | 489 | |
cef35d48 DM |
490 | INIT_DISASSEMBLE_INFO(disasm_info, stdout); |
491 | disasm_info.application_data = (PTR) &aux; | |
492 | aux.abfd = abfd; | |
493 | disasm_info.print_address_func = objdump_print_address; | |
494 | ||
aa0a709a SC |
495 | if (machine != (char *) NULL) |
496 | { | |
cef35d48 DM |
497 | bfd_arch_info_type *info = bfd_scan_arch (machine); |
498 | if (info == NULL) | |
aa0a709a SC |
499 | { |
500 | fprintf (stderr, "%s: Can't use supplied machine %s\n", | |
501 | program_name, | |
502 | machine); | |
503 | exit (1); | |
504 | } | |
505 | abfd->arch_info = info; | |
2fa0b342 | 506 | } |
e779a58c | 507 | |
cef35d48 | 508 | /* See if we can disassemble using bfd. */ |
e779a58c | 509 | |
aa0a709a SC |
510 | if (abfd->arch_info->disassemble) |
511 | { | |
512 | print = abfd->arch_info->disassemble; | |
e779a58c | 513 | } |
aa0a709a SC |
514 | else |
515 | { | |
cef35d48 | 516 | enum bfd_architecture a = bfd_get_arch (abfd); |
aa0a709a SC |
517 | switch (a) |
518 | { | |
195d1adf KR |
519 | /* If you add a case to this table, also add it to the |
520 | ARCH_all definition right above this function. */ | |
521 | #ifdef ARCH_a29k | |
522 | case bfd_arch_a29k: | |
523 | /* As far as I know we only handle big-endian 29k objects. */ | |
524 | disassemble = print_insn_big_a29k; | |
aa0a709a | 525 | break; |
195d1adf KR |
526 | #endif |
527 | #ifdef ARCH_alpha | |
528 | case bfd_arch_alpha: | |
529 | disassemble = print_insn_alpha; | |
12da1775 | 530 | break; |
195d1adf KR |
531 | #endif |
532 | #ifdef ARCH_h8300 | |
3ae36cb6 PB |
533 | case bfd_arch_h8300: |
534 | if (bfd_get_mach(abfd) == bfd_mach_h8300h) | |
535 | disassemble = print_insn_h8300h; | |
536 | else | |
537 | disassemble = print_insn_h8300; | |
538 | break; | |
195d1adf KR |
539 | #endif |
540 | #ifdef ARCH_h8500 | |
541 | case bfd_arch_h8500: | |
542 | disassemble = print_insn_h8500; | |
6f575704 | 543 | break; |
195d1adf KR |
544 | #endif |
545 | #ifdef ARCH_hppa | |
546 | case bfd_arch_hppa: | |
547 | disassemble = print_insn_hppa; | |
aa0a709a | 548 | break; |
195d1adf KR |
549 | #endif |
550 | #ifdef ARCH_i386 | |
551 | case bfd_arch_i386: | |
552 | disassemble = print_insn_i386; | |
aa0a709a | 553 | break; |
195d1adf KR |
554 | #endif |
555 | #ifdef ARCH_i960 | |
aa0a709a | 556 | case bfd_arch_i960: |
545a2768 | 557 | disassemble = print_insn_i960; |
aa0a709a | 558 | break; |
195d1adf KR |
559 | #endif |
560 | #ifdef ARCH_m68k | |
561 | case bfd_arch_m68k: | |
562 | disassemble = print_insn_m68k; | |
563 | break; | |
564 | #endif | |
565 | #ifdef ARCH_m88k | |
b3a2b497 ILT |
566 | case bfd_arch_m88k: |
567 | disassemble = print_insn_m88k; | |
568 | break; | |
195d1adf KR |
569 | #endif |
570 | #ifdef ARCH_mips | |
d9971b83 | 571 | case bfd_arch_mips: |
2e8adbd7 PB |
572 | if (abfd->xvec->byteorder_big_p) |
573 | disassemble = print_insn_big_mips; | |
574 | else | |
575 | disassemble = print_insn_little_mips; | |
d9971b83 | 576 | break; |
195d1adf | 577 | #endif |
8f197c94 ILT |
578 | #ifdef ARCH_powerpc |
579 | case bfd_arch_powerpc: | |
580 | if (abfd->xvec->byteorder_big_p) | |
581 | disassemble = print_insn_big_powerpc; | |
582 | else | |
583 | disassemble = print_insn_little_powerpc; | |
584 | break; | |
585 | #endif | |
eae82145 | 586 | #ifdef ARCH_rs6000 |
722087ec ILT |
587 | case bfd_arch_rs6000: |
588 | disassemble = print_insn_rs6000; | |
589 | break; | |
590 | #endif | |
195d1adf KR |
591 | #ifdef ARCH_sh |
592 | case bfd_arch_sh: | |
593 | disassemble = print_insn_sh; | |
594 | break; | |
595 | #endif | |
596 | #ifdef ARCH_sparc | |
597 | case bfd_arch_sparc: | |
598 | disassemble = print_insn_sparc; | |
599 | break; | |
600 | #endif | |
601 | #ifdef ARCH_z8k | |
602 | case bfd_arch_z8k: | |
603 | if (bfd_get_mach(abfd) == bfd_mach_z8001) | |
604 | disassemble = print_insn_z8001; | |
605 | else | |
606 | disassemble = print_insn_z8002; | |
607 | break; | |
608 | #endif | |
aa0a709a SC |
609 | default: |
610 | fprintf (stderr, "%s: Can't disassemble for architecture %s\n", | |
611 | program_name, | |
cef35d48 | 612 | bfd_printable_arch_mach (a, 0)); |
aa0a709a SC |
613 | exit (1); |
614 | } | |
2fa0b342 | 615 | |
aa0a709a | 616 | } |
2fa0b342 DHW |
617 | |
618 | for (section = abfd->sections; | |
aa0a709a SC |
619 | section != (asection *) NULL; |
620 | section = section->next) | |
65cceb78 | 621 | { |
cef35d48 DM |
622 | bfd_byte *data = NULL; |
623 | bfd_size_type datasize = 0; | |
2fa0b342 | 624 | |
cef35d48 DM |
625 | if (!(section->flags & SEC_LOAD)) |
626 | continue; | |
627 | if (only != (char *) NULL && strcmp (only, section->name) != 0) | |
628 | continue; | |
2fa0b342 | 629 | |
cef35d48 | 630 | printf ("Disassembly of section %s:\n", section->name); |
2fa0b342 | 631 | |
cef35d48 DM |
632 | datasize = bfd_get_section_size_before_reloc (section); |
633 | if (datasize == 0) | |
634 | continue; | |
2fa0b342 | 635 | |
cef35d48 | 636 | data = (bfd_byte *) xmalloc ((size_t) datasize); |
2fa0b342 | 637 | |
cef35d48 | 638 | bfd_get_section_contents (abfd, section, data, 0, datasize); |
2fa0b342 | 639 | |
cef35d48 DM |
640 | aux.sec = section; |
641 | disasm_info.buffer = data; | |
642 | disasm_info.buffer_vma = section->vma; | |
643 | disasm_info.buffer_length = datasize; | |
644 | i = 0; | |
645 | while (i < disasm_info.buffer_length) | |
646 | { | |
647 | if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 && | |
648 | data[i + 3] == 0) | |
aa0a709a | 649 | { |
cef35d48 | 650 | if (done_dot == false) |
aa0a709a | 651 | { |
cef35d48 DM |
652 | printf ("...\n"); |
653 | done_dot = true; | |
65cceb78 | 654 | } |
cef35d48 DM |
655 | i += 4; |
656 | } | |
657 | else | |
658 | { | |
659 | done_dot = false; | |
660 | if (with_line_numbers) | |
aa0a709a | 661 | { |
cef35d48 DM |
662 | CONST char *filename; |
663 | CONST char *functionname; | |
664 | unsigned int line; | |
665 | ||
666 | if (bfd_find_nearest_line (abfd, | |
667 | section, | |
668 | syms, | |
669 | section->vma + i, | |
670 | &filename, | |
671 | &functionname, | |
672 | &line)) | |
aa0a709a | 673 | { |
cef35d48 DM |
674 | if (functionname && *functionname |
675 | && strcmp(functionname, prev_function)) | |
676 | { | |
677 | printf ("%s():\n", functionname); | |
678 | prev_function = functionname; | |
679 | } | |
680 | if (!filename) | |
681 | filename = "???"; | |
682 | if (line && line != prevline) | |
aa0a709a | 683 | { |
cef35d48 DM |
684 | printf ("%s:%u\n", filename, line); |
685 | prevline = line; | |
aa0a709a SC |
686 | } |
687 | } | |
cef35d48 DM |
688 | } |
689 | objdump_print_address (section->vma + i, &disasm_info); | |
690 | putchar (' '); | |
65cceb78 | 691 | |
cef35d48 DM |
692 | if (disassemble) /* New style */ |
693 | { | |
694 | int bytes = (*disassemble)(section->vma + i, | |
695 | &disasm_info); | |
696 | if (bytes < 0) | |
697 | break; | |
698 | i += bytes; | |
aa0a709a | 699 | } |
cef35d48 DM |
700 | else /* Old style */ |
701 | i += print (section->vma + i, | |
702 | data + i, | |
703 | stdout); | |
704 | putchar ('\n'); | |
96d7950b | 705 | } |
96d7950b | 706 | } |
cef35d48 | 707 | free (data); |
2fa0b342 | 708 | } |
2fa0b342 | 709 | } |
73b8f102 | 710 | \f |
73b8f102 JG |
711 | |
712 | /* Define a table of stab values and print-strings. We wish the initializer | |
713 | could be a direct-mapped table, but instead we build one the first | |
714 | time we need it. */ | |
715 | ||
fe2750e1 | 716 | char **stab_name; |
73b8f102 JG |
717 | |
718 | struct stab_print { | |
719 | int value; | |
fe2750e1 | 720 | char *string; |
73b8f102 JG |
721 | }; |
722 | ||
723 | struct stab_print stab_print[] = { | |
724 | #define __define_stab(NAME, CODE, STRING) {CODE, STRING}, | |
725 | #include "aout/stab.def" | |
726 | #undef __define_stab | |
02a68547 | 727 | {0, ""} |
73b8f102 JG |
728 | }; |
729 | ||
250e36fe DM |
730 | void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name, |
731 | char *strsect_name)); | |
249c6fc0 | 732 | |
250e36fe | 733 | /* Dump the stabs sections from an object file that has a section that |
73b8f102 JG |
734 | uses Sun stabs encoding. It has to use some hooks into BFD because |
735 | string table sections are not normally visible to BFD callers. */ | |
736 | ||
737 | void | |
9b018ecd | 738 | dump_stabs (abfd) |
73b8f102 JG |
739 | bfd *abfd; |
740 | { | |
fe2750e1 SS |
741 | /* Allocate and initialize stab name array if first time. */ |
742 | if (stab_name == NULL) | |
73b8f102 | 743 | { |
250e36fe DM |
744 | int i; |
745 | ||
fe2750e1 SS |
746 | stab_name = (char **) xmalloc (256 * sizeof(char *)); |
747 | /* Clear the array. */ | |
73b8f102 | 748 | for (i = 0; i < 256; i++) |
fe2750e1 SS |
749 | stab_name[i] = NULL; |
750 | /* Fill in the defined stabs. */ | |
751 | for (i = 0; *stab_print[i].string; i++) | |
752 | stab_name[stab_print[i].value] = stab_print[i].string; | |
73b8f102 JG |
753 | } |
754 | ||
250e36fe DM |
755 | dump_section_stabs (abfd, ".stab", ".stabstr"); |
756 | dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr"); | |
757 | dump_section_stabs (abfd, ".stab.index", ".stab.indexstr"); | |
758 | dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$"); | |
249c6fc0 RS |
759 | } |
760 | ||
250e36fe DM |
761 | static struct internal_nlist *stabs; |
762 | static bfd_size_type stab_size; | |
763 | ||
764 | static char *strtab; | |
765 | static bfd_size_type stabstr_size; | |
766 | ||
767 | /* Read ABFD's stabs section STABSECT_NAME into `stabs' | |
768 | and string table section STRSECT_NAME into `strtab'. | |
769 | If the section exists and was read, allocate the space and return true. | |
770 | Otherwise return false. */ | |
771 | ||
772 | boolean | |
773 | read_section_stabs (abfd, stabsect_name, strsect_name) | |
249c6fc0 | 774 | bfd *abfd; |
250e36fe DM |
775 | char *stabsect_name; |
776 | char *strsect_name; | |
249c6fc0 | 777 | { |
d086adf8 | 778 | Elf_Internal_Shdr *stab_hdr, *stabstr_hdr; |
9b018ecd | 779 | asection *stabsect, *stabstrsect; |
9b018ecd ILT |
780 | int is_elf = (0 == strncmp ("elf", abfd->xvec->name, 3)); |
781 | ||
782 | if (is_elf) | |
250e36fe | 783 | stab_hdr = bfd_elf_find_section (abfd, stabsect_name); |
9b018ecd | 784 | else |
250e36fe | 785 | stabsect = bfd_get_section_by_name (abfd, stabsect_name); |
249c6fc0 | 786 | |
9b018ecd | 787 | if (is_elf ? (0 == stab_hdr) : (0 == stabsect)) |
73b8f102 | 788 | { |
250e36fe DM |
789 | printf ("No %s section present\n\n", stabsect_name); |
790 | return false; | |
73b8f102 JG |
791 | } |
792 | ||
9b018ecd | 793 | if (is_elf) |
250e36fe | 794 | stabstr_hdr = bfd_elf_find_section (abfd, strsect_name); |
9b018ecd | 795 | else |
250e36fe | 796 | stabstrsect = bfd_get_section_by_name (abfd, strsect_name); |
9b018ecd ILT |
797 | |
798 | if (is_elf ? (0 == stabstr_hdr) : (0 == stabstrsect)) | |
73b8f102 | 799 | { |
eae82145 | 800 | fprintf (stderr, "%s: %s has no %s section\n", program_name, |
250e36fe DM |
801 | bfd_get_filename (abfd), strsect_name); |
802 | return false; | |
73b8f102 | 803 | } |
9b018ecd ILT |
804 | |
805 | stab_size = (is_elf ? stab_hdr ->sh_size : bfd_section_size (abfd, stabsect)); | |
806 | stabstr_size = (is_elf ? stabstr_hdr->sh_size : bfd_section_size (abfd, stabstrsect)); | |
73b8f102 | 807 | |
9b018ecd ILT |
808 | stabs = (struct internal_nlist *) xmalloc (stab_size); |
809 | strtab = (char *) xmalloc (stabstr_size); | |
73b8f102 | 810 | |
9b018ecd | 811 | if (is_elf) |
73b8f102 | 812 | { |
9b018ecd ILT |
813 | if (bfd_seek (abfd, stab_hdr->sh_offset, SEEK_SET) < 0 || |
814 | stab_size != bfd_read ((PTR) stabs, stab_size, 1, abfd)) | |
815 | { | |
eae82145 | 816 | fprintf (stderr, "%s: Reading %s section of %s failed\n", |
250e36fe | 817 | program_name, stabsect_name, |
cef35d48 | 818 | bfd_get_filename (abfd)); |
250e36fe DM |
819 | free (stabs); |
820 | free (strtab); | |
821 | return false; | |
9b018ecd ILT |
822 | } |
823 | } | |
824 | else | |
825 | { | |
826 | bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size); | |
73b8f102 | 827 | } |
2fa0b342 | 828 | |
9b018ecd | 829 | if (is_elf) |
73b8f102 | 830 | { |
9b018ecd ILT |
831 | if (bfd_seek (abfd, stabstr_hdr->sh_offset, SEEK_SET) < 0 || |
832 | stabstr_size != bfd_read ((PTR) strtab, stabstr_size, 1, abfd)) | |
833 | { | |
eae82145 | 834 | fprintf (stderr, "%s: Reading %s section of %s failed\n", |
250e36fe | 835 | program_name, strsect_name, |
cef35d48 | 836 | bfd_get_filename (abfd)); |
250e36fe DM |
837 | free (stabs); |
838 | free (strtab); | |
839 | return false; | |
9b018ecd ILT |
840 | } |
841 | } | |
842 | else | |
843 | { | |
844 | bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0, stabstr_size); | |
73b8f102 | 845 | } |
250e36fe DM |
846 | return true; |
847 | } | |
73b8f102 JG |
848 | |
849 | #define SWAP_SYMBOL(symp, abfd) \ | |
250e36fe | 850 | { \ |
73b8f102 | 851 | (symp)->n_strx = bfd_h_get_32(abfd, \ |
250e36fe | 852 | (unsigned char *)&(symp)->n_strx); \ |
73b8f102 | 853 | (symp)->n_desc = bfd_h_get_16 (abfd, \ |
250e36fe | 854 | (unsigned char *)&(symp)->n_desc); \ |
73b8f102 | 855 | (symp)->n_value = bfd_h_get_32 (abfd, \ |
250e36fe DM |
856 | (unsigned char *)&(symp)->n_value); \ |
857 | } | |
73b8f102 | 858 | |
250e36fe DM |
859 | /* Print ABFD's stabs section STABSECT_NAME (in `stabs'), |
860 | using string table section STRSECT_NAME (in `strtab'). */ | |
861 | ||
862 | void | |
863 | print_section_stabs (abfd, stabsect_name, strsect_name) | |
864 | bfd *abfd; | |
865 | char *stabsect_name; | |
866 | char *strsect_name; | |
867 | { | |
868 | int i; | |
869 | unsigned file_string_table_offset = 0, next_file_string_table_offset = 0; | |
870 | struct internal_nlist *stabp = stabs, | |
871 | *stabs_end = (struct internal_nlist *) (stab_size + (char *) stabs); | |
73b8f102 | 872 | |
250e36fe DM |
873 | printf ("Contents of %s section:\n\n", stabsect_name); |
874 | printf ("Symnum n_type n_othr n_desc n_value n_strx String\n"); | |
249c6fc0 RS |
875 | |
876 | /* Loop through all symbols and print them. | |
877 | ||
878 | We start the index at -1 because there is a dummy symbol on | |
250e36fe | 879 | the front of stabs-in-{coff,elf} sections that supplies sizes. */ |
249c6fc0 | 880 | |
250e36fe | 881 | for (i = -1; stabp < stabs_end; stabp++, i++) |
73b8f102 | 882 | { |
250e36fe | 883 | SWAP_SYMBOL (stabp, abfd); |
fe2750e1 | 884 | printf ("\n%-6d ", i); |
250e36fe | 885 | /* Either print the stab name, or, if unnamed, print its number |
fe2750e1 | 886 | again (makes consistent formatting for tools like awk). */ |
250e36fe DM |
887 | if (stab_name[stabp->n_type]) |
888 | printf ("%-6s", stab_name[stabp->n_type]); | |
fe2750e1 SS |
889 | else |
890 | printf ("%-6d", i); | |
250e36fe DM |
891 | printf (" %-6d %-6d ", stabp->n_other, stabp->n_desc); |
892 | printf_vma (stabp->n_value); | |
893 | printf (" %-6lu", stabp->n_strx); | |
249c6fc0 RS |
894 | |
895 | /* Symbols with type == 0 (N_UNDF) specify the length of the | |
896 | string table associated with this file. We use that info | |
897 | to know how to relocate the *next* file's string table indices. */ | |
898 | ||
250e36fe | 899 | if (stabp->n_type == N_UNDF) |
249c6fc0 RS |
900 | { |
901 | file_string_table_offset = next_file_string_table_offset; | |
250e36fe | 902 | next_file_string_table_offset += stabp->n_value; |
249c6fc0 | 903 | } |
249c6fc0 | 904 | else |
e1ec9f07 | 905 | { |
250e36fe | 906 | /* Using the (possibly updated) string table offset, print the |
e1ec9f07 SS |
907 | string (if any) associated with this symbol. */ |
908 | ||
250e36fe DM |
909 | if ((stabp->n_strx + file_string_table_offset) < stabstr_size) |
910 | printf (" %s", &strtab[stabp->n_strx + file_string_table_offset]); | |
e1ec9f07 SS |
911 | else |
912 | printf (" *"); | |
913 | } | |
73b8f102 JG |
914 | } |
915 | printf ("\n\n"); | |
916 | } | |
249c6fc0 | 917 | |
250e36fe DM |
918 | void |
919 | dump_section_stabs (abfd, stabsect_name, strsect_name) | |
920 | bfd *abfd; | |
921 | char *stabsect_name; | |
922 | char *strsect_name; | |
923 | { | |
924 | if (read_section_stabs (abfd, stabsect_name, strsect_name)) | |
925 | { | |
926 | print_section_stabs (abfd, stabsect_name, strsect_name); | |
927 | free (stabs); | |
928 | free (strtab); | |
929 | } | |
930 | } | |
931 | \f | |
eae82145 | 932 | static void |
f7b839f7 DM |
933 | dump_bfd_header (abfd) |
934 | bfd *abfd; | |
935 | { | |
936 | char *comma = ""; | |
937 | ||
938 | printf ("architecture: %s, ", | |
939 | bfd_printable_arch_mach (bfd_get_arch (abfd), | |
940 | bfd_get_mach (abfd))); | |
941 | printf ("flags 0x%08x:\n", abfd->flags); | |
942 | ||
943 | #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";} | |
944 | PF (HAS_RELOC, "HAS_RELOC"); | |
945 | PF (EXEC_P, "EXEC_P"); | |
946 | PF (HAS_LINENO, "HAS_LINENO"); | |
947 | PF (HAS_DEBUG, "HAS_DEBUG"); | |
948 | PF (HAS_SYMS, "HAS_SYMS"); | |
949 | PF (HAS_LOCALS, "HAS_LOCALS"); | |
950 | PF (DYNAMIC, "DYNAMIC"); | |
951 | PF (WP_TEXT, "WP_TEXT"); | |
952 | PF (D_PAGED, "D_PAGED"); | |
953 | PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE"); | |
954 | printf ("\nstart address 0x"); | |
955 | printf_vma (abfd->start_address); | |
956 | } | |
957 | ||
02a68547 | 958 | static void |
2fa0b342 DHW |
959 | display_bfd (abfd) |
960 | bfd *abfd; | |
961 | { | |
209e5610 DM |
962 | char **matching; |
963 | ||
964 | if (!bfd_check_format_matches (abfd, bfd_object, &matching)) | |
aa0a709a | 965 | { |
cef35d48 | 966 | bfd_nonfatal (bfd_get_filename (abfd)); |
8f197c94 | 967 | if (bfd_get_error () == bfd_error_file_ambiguously_recognized) |
209e5610 DM |
968 | { |
969 | list_matching_formats (matching); | |
970 | free (matching); | |
971 | } | |
aa0a709a SC |
972 | return; |
973 | } | |
f7b839f7 | 974 | |
cef35d48 DM |
975 | printf ("\n%s: file format %s\n", bfd_get_filename (abfd), |
976 | abfd->xvec->name); | |
aa0a709a SC |
977 | if (dump_ar_hdrs) |
978 | print_arelt_descr (stdout, abfd, true); | |
aa0a709a | 979 | if (dump_file_header) |
f7b839f7 DM |
980 | dump_bfd_header (abfd); |
981 | putchar ('\n'); | |
2fa0b342 | 982 | if (dump_section_headers) |
aa0a709a SC |
983 | dump_headers (abfd); |
984 | if (dump_symtab || dump_reloc_info || disassemble) | |
985 | { | |
986 | syms = slurp_symtab (abfd); | |
987 | } | |
de3b08ac ILT |
988 | if (dump_dynamic_symtab || dump_dynamic_reloc_info) |
989 | { | |
990 | dynsyms = slurp_dynamic_symtab (abfd); | |
991 | } | |
aa0a709a | 992 | if (dump_symtab) |
de3b08ac ILT |
993 | dump_symbols (abfd, false); |
994 | if (dump_dynamic_symtab) | |
995 | dump_symbols (abfd, true); | |
73b8f102 | 996 | if (dump_stab_section_info) |
9b018ecd | 997 | dump_stabs (abfd); |
aa0a709a SC |
998 | if (dump_reloc_info) |
999 | dump_relocs (abfd); | |
de3b08ac ILT |
1000 | if (dump_dynamic_reloc_info) |
1001 | dump_dynamic_relocs (abfd); | |
aa0a709a SC |
1002 | if (dump_section_contents) |
1003 | dump_data (abfd); | |
3ae36cb6 PB |
1004 | /* Note that disassemble_data re-orders the syms table, but that is |
1005 | safe - as long as it is done last! */ | |
aa0a709a SC |
1006 | if (disassemble) |
1007 | disassemble_data (abfd); | |
2fa0b342 DHW |
1008 | } |
1009 | ||
d9971b83 | 1010 | static void |
2fa0b342 DHW |
1011 | display_file (filename, target) |
1012 | char *filename; | |
1013 | char *target; | |
1014 | { | |
1015 | bfd *file, *arfile = (bfd *) NULL; | |
1016 | ||
1017 | file = bfd_openr (filename, target); | |
aa0a709a SC |
1018 | if (file == NULL) |
1019 | { | |
cef35d48 | 1020 | bfd_nonfatal (filename); |
aa0a709a SC |
1021 | return; |
1022 | } | |
2fa0b342 | 1023 | |
aa0a709a SC |
1024 | if (bfd_check_format (file, bfd_archive) == true) |
1025 | { | |
8f197c94 ILT |
1026 | bfd *last_arfile = NULL; |
1027 | ||
aa0a709a SC |
1028 | printf ("In archive %s:\n", bfd_get_filename (file)); |
1029 | for (;;) | |
1030 | { | |
8f197c94 | 1031 | bfd_set_error (bfd_error_no_error); |
aa0a709a SC |
1032 | |
1033 | arfile = bfd_openr_next_archived_file (file, arfile); | |
1034 | if (arfile == NULL) | |
1035 | { | |
8f197c94 | 1036 | if (bfd_get_error () != bfd_error_no_more_archived_files) |
d2442698 | 1037 | { |
cef35d48 | 1038 | bfd_nonfatal (bfd_get_filename (file)); |
d2442698 | 1039 | } |
8f197c94 | 1040 | break; |
aa0a709a | 1041 | } |
2fa0b342 | 1042 | |
aa0a709a | 1043 | display_bfd (arfile); |
8f197c94 ILT |
1044 | |
1045 | if (last_arfile != NULL) | |
1046 | bfd_close (last_arfile); | |
1047 | last_arfile = arfile; | |
aa0a709a | 1048 | } |
8f197c94 ILT |
1049 | |
1050 | if (last_arfile != NULL) | |
1051 | bfd_close (last_arfile); | |
2fa0b342 | 1052 | } |
2fa0b342 | 1053 | else |
aa0a709a | 1054 | display_bfd (file); |
2fa0b342 | 1055 | |
aa0a709a | 1056 | bfd_close (file); |
2fa0b342 DHW |
1057 | } |
1058 | \f | |
1059 | /* Actually display the various requested regions */ | |
1060 | ||
d9971b83 | 1061 | static void |
2fa0b342 DHW |
1062 | dump_data (abfd) |
1063 | bfd *abfd; | |
1064 | { | |
1065 | asection *section; | |
aa0a709a | 1066 | bfd_byte *data = 0; |
fc5d6074 SC |
1067 | bfd_size_type datasize = 0; |
1068 | bfd_size_type i; | |
2fa0b342 DHW |
1069 | |
1070 | for (section = abfd->sections; section != NULL; section = | |
aa0a709a SC |
1071 | section->next) |
1072 | { | |
1073 | int onaline = 16; | |
2fa0b342 | 1074 | |
aa0a709a SC |
1075 | if (only == (char *) NULL || |
1076 | strcmp (only, section->name) == 0) | |
60c80016 | 1077 | { |
aa0a709a SC |
1078 | if (section->flags & SEC_HAS_CONTENTS) |
1079 | { | |
1080 | printf ("Contents of section %s:\n", section->name); | |
1081 | ||
9b018ecd | 1082 | if (bfd_section_size (abfd, section) == 0) |
aa0a709a | 1083 | continue; |
02a68547 | 1084 | data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section)); |
9b018ecd | 1085 | datasize = bfd_section_size (abfd, section); |
2fa0b342 | 1086 | |
2fa0b342 | 1087 | |
9b018ecd | 1088 | bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section)); |
2fa0b342 | 1089 | |
9b018ecd | 1090 | for (i = 0; i < bfd_section_size (abfd, section); i += onaline) |
aa0a709a SC |
1091 | { |
1092 | bfd_size_type j; | |
1093 | ||
1094 | printf (" %04lx ", (unsigned long int) (i + section->vma)); | |
1095 | for (j = i; j < i + onaline; j++) | |
1096 | { | |
9b018ecd | 1097 | if (j < bfd_section_size (abfd, section)) |
aa0a709a SC |
1098 | printf ("%02x", (unsigned) (data[j])); |
1099 | else | |
1100 | printf (" "); | |
1101 | if ((j & 3) == 3) | |
1102 | printf (" "); | |
1103 | } | |
2fa0b342 | 1104 | |
aa0a709a SC |
1105 | printf (" "); |
1106 | for (j = i; j < i + onaline; j++) | |
1107 | { | |
9b018ecd | 1108 | if (j >= bfd_section_size (abfd, section)) |
aa0a709a SC |
1109 | printf (" "); |
1110 | else | |
1111 | printf ("%c", isprint (data[j]) ? data[j] : '.'); | |
1112 | } | |
1113 | putchar ('\n'); | |
1114 | } | |
d9971b83 | 1115 | free (data); |
60c80016 | 1116 | } |
2fa0b342 | 1117 | } |
2fa0b342 | 1118 | } |
2fa0b342 DHW |
1119 | } |
1120 | ||
2fa0b342 | 1121 | /* Should perhaps share code and display with nm? */ |
d9971b83 | 1122 | static void |
de3b08ac | 1123 | dump_symbols (abfd, dynamic) |
2fa0b342 | 1124 | bfd *abfd; |
de3b08ac | 1125 | boolean dynamic; |
2fa0b342 | 1126 | { |
de3b08ac ILT |
1127 | asymbol **current; |
1128 | long max; | |
ae5d2ff5 | 1129 | long count; |
2fa0b342 | 1130 | |
de3b08ac | 1131 | if (dynamic) |
aa0a709a | 1132 | { |
de3b08ac ILT |
1133 | current = dynsyms; |
1134 | max = dynsymcount; | |
1135 | if (max == 0) | |
1136 | return; | |
1137 | printf ("DYNAMIC SYMBOL TABLE:\n"); | |
1138 | } | |
1139 | else | |
1140 | { | |
1141 | current = syms; | |
1142 | max = symcount; | |
1143 | if (max == 0) | |
1144 | return; | |
1145 | printf ("SYMBOL TABLE:\n"); | |
1146 | } | |
2fa0b342 | 1147 | |
de3b08ac ILT |
1148 | for (count = 0; count < max; count++) |
1149 | { | |
d9971b83 | 1150 | if (*current) |
aa0a709a | 1151 | { |
d9971b83 KR |
1152 | bfd *cur_bfd = bfd_asymbol_bfd(*current); |
1153 | if (cur_bfd) | |
1154 | { | |
1155 | bfd_print_symbol (cur_bfd, | |
1156 | stdout, | |
1157 | *current, bfd_print_symbol_all); | |
1158 | printf ("\n"); | |
1159 | } | |
aa0a709a SC |
1160 | } |
1161 | current++; | |
2fa0b342 | 1162 | } |
aa0a709a SC |
1163 | printf ("\n"); |
1164 | printf ("\n"); | |
2fa0b342 DHW |
1165 | } |
1166 | ||
d9971b83 | 1167 | static void |
aa0a709a SC |
1168 | dump_relocs (abfd) |
1169 | bfd *abfd; | |
2fa0b342 DHW |
1170 | { |
1171 | arelent **relpp; | |
ae5d2ff5 | 1172 | long relcount; |
2fa0b342 | 1173 | asection *a; |
aa0a709a SC |
1174 | |
1175 | for (a = abfd->sections; a != (asection *) NULL; a = a->next) | |
1176 | { | |
ae5d2ff5 ILT |
1177 | long relsize; |
1178 | ||
aa0a709a SC |
1179 | if (a == &bfd_abs_section) |
1180 | continue; | |
1181 | if (a == &bfd_und_section) | |
1182 | continue; | |
d9971b83 | 1183 | if (bfd_is_com_section (a)) |
aa0a709a SC |
1184 | continue; |
1185 | ||
195d1adf KR |
1186 | if (only) |
1187 | { | |
1188 | if (strcmp (only, a->name)) | |
1189 | continue; | |
1190 | } | |
1191 | else if ((a->flags & SEC_RELOC) == 0) | |
1192 | continue; | |
1193 | ||
aa0a709a SC |
1194 | printf ("RELOCATION RECORDS FOR [%s]:", a->name); |
1195 | ||
ae5d2ff5 ILT |
1196 | relsize = bfd_get_reloc_upper_bound (abfd, a); |
1197 | if (relsize < 0) | |
1198 | bfd_fatal (bfd_get_filename (abfd)); | |
1199 | ||
1200 | if (relsize == 0) | |
aa0a709a SC |
1201 | { |
1202 | printf (" (none)\n\n"); | |
d20f480f | 1203 | } |
aa0a709a SC |
1204 | else |
1205 | { | |
ae5d2ff5 | 1206 | relpp = (arelent **) xmalloc (relsize); |
3ae36cb6 | 1207 | /* Note that this must be done *before* we sort the syms table. */ |
aa0a709a | 1208 | relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms); |
ae5d2ff5 ILT |
1209 | if (relcount < 0) |
1210 | bfd_fatal (bfd_get_filename (abfd)); | |
1211 | else if (relcount == 0) | |
aa0a709a SC |
1212 | { |
1213 | printf (" (none)\n\n"); | |
d20f480f | 1214 | } |
aa0a709a SC |
1215 | else |
1216 | { | |
1217 | printf ("\n"); | |
de3b08ac | 1218 | dump_reloc_set (abfd, relpp, relcount); |
aa0a709a | 1219 | printf ("\n\n"); |
d20f480f | 1220 | } |
de3b08ac | 1221 | free (relpp); |
2fa0b342 | 1222 | } |
de3b08ac ILT |
1223 | } |
1224 | } | |
2fa0b342 | 1225 | |
de3b08ac ILT |
1226 | static void |
1227 | dump_dynamic_relocs (abfd) | |
1228 | bfd *abfd; | |
1229 | { | |
1230 | long relsize; | |
1231 | arelent **relpp; | |
1232 | long relcount; | |
1233 | ||
1234 | printf ("DYNAMIC RELOCATION RECORDS"); | |
1235 | ||
1236 | relsize = bfd_get_dynamic_reloc_upper_bound (abfd); | |
1237 | if (relsize < 0) | |
1238 | bfd_fatal (bfd_get_filename (abfd)); | |
1239 | ||
1240 | if (relsize == 0) | |
1241 | { | |
1242 | printf (" (none)\n\n"); | |
1243 | } | |
1244 | else | |
1245 | { | |
1246 | relpp = (arelent **) xmalloc (relsize); | |
1247 | relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms); | |
1248 | if (relcount < 0) | |
1249 | bfd_fatal (bfd_get_filename (abfd)); | |
1250 | else if (relcount == 0) | |
1251 | { | |
1252 | printf (" (none)\n\n"); | |
1253 | } | |
1254 | else | |
1255 | { | |
1256 | printf ("\n"); | |
1257 | dump_reloc_set (abfd, relpp, relcount); | |
1258 | printf ("\n\n"); | |
1259 | } | |
1260 | free (relpp); | |
1261 | } | |
1262 | } | |
1263 | ||
1264 | static void | |
1265 | dump_reloc_set (abfd, relpp, relcount) | |
1266 | bfd *abfd; | |
1267 | arelent **relpp; | |
1268 | long relcount; | |
1269 | { | |
1270 | arelent **p; | |
1271 | ||
1272 | /* Get column headers lined up reasonably. */ | |
1273 | { | |
1274 | static int width; | |
1275 | if (width == 0) | |
1276 | { | |
1277 | char buf[30]; | |
1278 | sprintf_vma (buf, (bfd_vma) -1); | |
1279 | width = strlen (buf) - 7; | |
1280 | } | |
1281 | printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, ""); | |
1282 | } | |
1283 | ||
1284 | for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--) | |
1285 | { | |
1286 | arelent *q = *p; | |
1287 | CONST char *sym_name; | |
1288 | CONST char *section_name; | |
1289 | ||
1290 | if (q->sym_ptr_ptr && *q->sym_ptr_ptr) | |
1291 | { | |
1292 | sym_name = (*(q->sym_ptr_ptr))->name; | |
1293 | section_name = (*(q->sym_ptr_ptr))->section->name; | |
1294 | } | |
1295 | else | |
1296 | { | |
1297 | sym_name = NULL; | |
1298 | section_name = NULL; | |
1299 | } | |
1300 | if (sym_name) | |
1301 | { | |
1302 | printf_vma (q->address); | |
1303 | printf (" %-16s %s", | |
1304 | q->howto->name, | |
1305 | sym_name); | |
1306 | } | |
1307 | else | |
1308 | { | |
1309 | if (section_name == (CONST char *) NULL) | |
1310 | section_name = "*unknown*"; | |
1311 | printf_vma (q->address); | |
1312 | printf (" %-16s [%s]", | |
1313 | q->howto->name, | |
1314 | section_name); | |
1315 | } | |
1316 | if (q->addend) | |
1317 | { | |
1318 | printf ("+0x"); | |
1319 | printf_vma (q->addend); | |
1320 | } | |
1321 | printf ("\n"); | |
d20f480f | 1322 | } |
2fa0b342 | 1323 | } |
f7b839f7 DM |
1324 | \f |
1325 | /* A file to open each BFD on. It will never actually be written to. */ | |
ebc9eeba | 1326 | #ifdef __GO32__ |
aa0a709a | 1327 | #define _DUMMY_NAME_ "##dummy" |
ebc9eeba ILT |
1328 | #else |
1329 | #define _DUMMY_NAME_ "/dev/null" | |
aa0a709a | 1330 | #endif |
f7b839f7 DM |
1331 | |
1332 | /* The length of the longest architecture name + 1. */ | |
1333 | #define LONGEST_ARCH sizeof("rs6000:6000") | |
1334 | ||
1335 | /* List the targets that BFD is configured to support, each followed | |
1336 | by its endianness and the architectures it supports. */ | |
1337 | ||
1338 | static void | |
1339 | display_target_list () | |
1340 | { | |
1341 | extern bfd_target *bfd_target_vector[]; | |
1342 | int t; | |
1343 | ||
1344 | for (t = 0; bfd_target_vector[t]; t++) | |
1345 | { | |
1346 | int a; | |
1347 | bfd_target *p = bfd_target_vector[t]; | |
1348 | bfd *abfd = bfd_openw (_DUMMY_NAME_, p->name); | |
1349 | ||
334d6e76 SS |
1350 | /* It *is* possible that bfd_openw might fail; avoid the |
1351 | tragic consequences that would otherwise ensue. */ | |
1352 | if (abfd == NULL) | |
1353 | { | |
cef35d48 | 1354 | bfd_nonfatal (_DUMMY_NAME_); |
334d6e76 SS |
1355 | return; |
1356 | } | |
f7b839f7 DM |
1357 | bfd_set_format (abfd, bfd_object); |
1358 | printf ("%s\n (header %s, data %s)\n", p->name, | |
1359 | p->header_byteorder_big_p ? "big endian" : "little endian", | |
1360 | p->byteorder_big_p ? "big endian" : "little endian"); | |
1361 | for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++) | |
1362 | if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0)) | |
1363 | printf (" %s\n", | |
1364 | bfd_printable_arch_mach ((enum bfd_architecture) a, 0)); | |
1365 | } | |
1366 | } | |
1367 | ||
1368 | /* Print a table showing which architectures are supported for entries | |
1369 | FIRST through LAST-1 of bfd_target_vector (targets across, | |
1370 | architectures down). */ | |
1371 | ||
9872a49c | 1372 | static void |
abdcac0f DM |
1373 | display_info_table (first, last) |
1374 | int first; | |
1375 | int last; | |
9872a49c | 1376 | { |
f7b839f7 | 1377 | int t, a; |
abdcac0f | 1378 | extern bfd_target *bfd_target_vector[]; |
9872a49c | 1379 | |
f7b839f7 | 1380 | /* Print heading of target names. */ |
ae5d2ff5 | 1381 | printf ("\n%*s", (int) LONGEST_ARCH, " "); |
f7b839f7 DM |
1382 | for (t = first; t++ < last && bfd_target_vector[t];) |
1383 | printf ("%s ", bfd_target_vector[t]->name); | |
1384 | putchar ('\n'); | |
9872a49c | 1385 | |
f7b839f7 DM |
1386 | for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++) |
1387 | if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0) | |
e779a58c | 1388 | { |
ae5d2ff5 ILT |
1389 | printf ("%*s ", (int) LONGEST_ARCH - 1, |
1390 | bfd_printable_arch_mach (a, 0)); | |
f7b839f7 | 1391 | for (t = first; t++ < last && bfd_target_vector[t];) |
aa0a709a | 1392 | { |
f7b839f7 | 1393 | bfd_target *p = bfd_target_vector[t]; |
aa0a709a | 1394 | bfd *abfd = bfd_openw (_DUMMY_NAME_, p->name); |
aa0a709a | 1395 | |
334d6e76 SS |
1396 | /* Just in case the open failed somehow. */ |
1397 | if (abfd == NULL) | |
1398 | { | |
cef35d48 | 1399 | bfd_nonfatal (_DUMMY_NAME_); |
334d6e76 SS |
1400 | return; |
1401 | } | |
f7b839f7 DM |
1402 | bfd_set_format (abfd, bfd_object); |
1403 | if (bfd_set_arch_mach (abfd, a, 0)) | |
aa0a709a SC |
1404 | printf ("%s ", p->name); |
1405 | else | |
e779a58c | 1406 | { |
f7b839f7 | 1407 | int l = strlen (p->name); |
aa0a709a | 1408 | while (l--) |
f7b839f7 DM |
1409 | putchar ('-'); |
1410 | putchar (' '); | |
e779a58c | 1411 | } |
e779a58c | 1412 | } |
f7b839f7 | 1413 | putchar ('\n'); |
e779a58c | 1414 | } |
9872a49c | 1415 | } |
aa0a709a | 1416 | |
f7b839f7 DM |
1417 | /* Print tables of all the target-architecture combinations that |
1418 | BFD has been configured to support. */ | |
1419 | ||
aa0a709a | 1420 | static void |
f7b839f7 | 1421 | display_target_tables () |
aa0a709a | 1422 | { |
f7b839f7 | 1423 | int t, columns; |
abdcac0f | 1424 | extern bfd_target *bfd_target_vector[]; |
f7b839f7 | 1425 | char *colum; |
aa0a709a SC |
1426 | extern char *getenv (); |
1427 | ||
aa0a709a | 1428 | columns = 0; |
f7b839f7 DM |
1429 | colum = getenv ("COLUMNS"); |
1430 | if (colum != NULL) | |
aa0a709a | 1431 | columns = atoi (colum); |
f7b839f7 | 1432 | if (columns == 0) |
aa0a709a | 1433 | columns = 80; |
f7b839f7 DM |
1434 | |
1435 | for (t = 0; bfd_target_vector[t];) | |
aa0a709a | 1436 | { |
f7b839f7 DM |
1437 | int oldt = t, wid; |
1438 | ||
1439 | for (wid = LONGEST_ARCH; bfd_target_vector[t] && wid < columns; t++) | |
1440 | wid += strlen (bfd_target_vector[t]->name) + 1; | |
1441 | t--; | |
1442 | if (oldt == t) | |
aa0a709a | 1443 | break; |
f7b839f7 | 1444 | display_info_table (oldt, t); |
aa0a709a SC |
1445 | } |
1446 | } | |
1447 | ||
f7b839f7 DM |
1448 | static void |
1449 | display_info () | |
1450 | { | |
1451 | printf ("BFD header file version %s\n", BFD_VERSION); | |
1452 | display_target_list (); | |
1453 | display_target_tables (); | |
1454 | } | |
1455 | ||
2fa0b342 DHW |
1456 | int |
1457 | main (argc, argv) | |
1458 | int argc; | |
1459 | char **argv; | |
1460 | { | |
1461 | int c; | |
2fa0b342 DHW |
1462 | char *target = default_target; |
1463 | boolean seenflag = false; | |
2fa0b342 DHW |
1464 | |
1465 | program_name = *argv; | |
8f197c94 ILT |
1466 | xmalloc_set_program_name (program_name); |
1467 | ||
1468 | bfd_init (); | |
2fa0b342 | 1469 | |
de3b08ac | 1470 | while ((c = getopt_long (argc, argv, "ib:m:VdlfahrRtTxsj:", long_options, |
d2442698 | 1471 | (int *) 0)) |
aa0a709a SC |
1472 | != EOF) |
1473 | { | |
1474 | seenflag = true; | |
1475 | switch (c) | |
1476 | { | |
b3a2b497 ILT |
1477 | case 0: |
1478 | break; /* we've been given a long option */ | |
aa0a709a SC |
1479 | case 'm': |
1480 | machine = optarg; | |
1481 | break; | |
1482 | case 'j': | |
1483 | only = optarg; | |
1484 | break; | |
1485 | case 'l': | |
1486 | with_line_numbers = 1; | |
1487 | break; | |
1488 | case 'b': | |
1489 | target = optarg; | |
1490 | break; | |
1491 | case 'f': | |
1492 | dump_file_header = true; | |
1493 | break; | |
1494 | case 'i': | |
e1ec9f07 | 1495 | formats_info = true; |
aa0a709a SC |
1496 | break; |
1497 | case 'x': | |
1498 | dump_symtab = 1; | |
1499 | dump_reloc_info = 1; | |
1500 | dump_file_header = true; | |
1501 | dump_ar_hdrs = 1; | |
1502 | dump_section_headers = 1; | |
1503 | break; | |
aa0a709a SC |
1504 | case 't': |
1505 | dump_symtab = 1; | |
1506 | break; | |
de3b08ac ILT |
1507 | case 'T': |
1508 | dump_dynamic_symtab = 1; | |
1509 | break; | |
aa0a709a SC |
1510 | case 'd': |
1511 | disassemble = true; | |
1512 | break; | |
1513 | case 's': | |
1514 | dump_section_contents = 1; | |
1515 | break; | |
1516 | case 'r': | |
1517 | dump_reloc_info = 1; | |
1518 | break; | |
de3b08ac ILT |
1519 | case 'R': |
1520 | dump_dynamic_reloc_info = 1; | |
1521 | break; | |
aa0a709a SC |
1522 | case 'a': |
1523 | dump_ar_hdrs = 1; | |
1524 | break; | |
1525 | case 'h': | |
1526 | dump_section_headers = 1; | |
1527 | break; | |
b3a2b497 ILT |
1528 | case 'H': |
1529 | usage (stdout, 0); | |
249c6fc0 RS |
1530 | case 'V': |
1531 | show_version = 1; | |
1532 | break; | |
aa0a709a | 1533 | default: |
b3a2b497 | 1534 | usage (stderr, 1); |
aa0a709a | 1535 | } |
2fa0b342 | 1536 | } |
2fa0b342 | 1537 | |
249c6fc0 | 1538 | if (show_version) |
b3a2b497 ILT |
1539 | { |
1540 | printf ("GNU %s version %s\n", program_name, program_version); | |
1541 | exit (0); | |
1542 | } | |
249c6fc0 | 1543 | |
2fa0b342 | 1544 | if (seenflag == false) |
b3a2b497 | 1545 | usage (stderr, 1); |
2fa0b342 | 1546 | |
e1ec9f07 | 1547 | if (formats_info) |
aa0a709a SC |
1548 | { |
1549 | display_info (); | |
1550 | } | |
1551 | else | |
1552 | { | |
1553 | if (optind == argc) | |
1554 | display_file ("a.out", target); | |
1555 | else | |
1556 | for (; optind < argc;) | |
1557 | display_file (argv[optind++], target); | |
1558 | } | |
2fa0b342 DHW |
1559 | return 0; |
1560 | } |