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