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