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