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