Commit | Line | Data |
---|---|---|
d20f480f SC |
1 | /* objdump.c -- dump information about an object file. |
2 | Copyright (C) 1990, 1991 Free Software Foundation, Inc. | |
2fa0b342 DHW |
3 | |
4 | This file is part of BFD, the Binary File Diddler. | |
5 | ||
6 | BFD is free software; you can redistribute it and/or modify | |
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 | ||
11 | BFD is distributed in the hope that it will be useful, | |
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 | |
17 | along with BFD; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
2fa0b342 DHW |
20 | /* |
21 | * Until there is other documentation, refer to the manual page dump(1) in | |
22 | * the system 5 program's reference manual | |
23 | */ | |
24 | ||
2fa0b342 | 25 | #include "bfd.h" |
d20f480f | 26 | #include "sysdep.h" |
2fa0b342 DHW |
27 | #include "getopt.h" |
28 | #include <stdio.h> | |
29 | #include <ctype.h> | |
30 | ||
73b8f102 JG |
31 | #define ELF_STAB_DISPLAY /* This code works, but uses internal |
32 | bfd and elf stuff. Flip this define | |
33 | off if you need to just use generic | |
34 | BFD interfaces. */ | |
35 | ||
36 | #ifdef ELF_STAB_DISPLAY | |
37 | /* Internal headers for the ELF .stab-dump code - sorry. */ | |
38 | #define BYTES_IN_WORD 32 | |
39 | #include "aout/aout64.h" | |
40 | #include "elf/internal.h" | |
41 | extern Elf_Internal_Shdr *bfd_elf_find_section(); | |
42 | #endif /* ELF_STAB_DISPLAY */ | |
bf661056 | 43 | |
aa0a709a | 44 | char *xmalloc (); |
2fa0b342 DHW |
45 | |
46 | char *default_target = NULL; /* default at runtime */ | |
47 | ||
48 | char *program_name = NULL; | |
49 | ||
50 | int dump_section_contents; /* -s */ | |
51 | int dump_section_headers; /* -h */ | |
52 | boolean dump_file_header; /* -f */ | |
53 | int dump_symtab; /* -t */ | |
54 | int dump_reloc_info; /* -r */ | |
55 | int dump_ar_hdrs; /* -a */ | |
aa0a709a | 56 | int with_line_numbers; /* -l */ |
73b8f102 | 57 | int dump_stab_section_info; /* -stabs */ |
aa0a709a SC |
58 | boolean disassemble; /* -d */ |
59 | boolean info; /* -i */ | |
2fa0b342 DHW |
60 | char *only; |
61 | ||
62 | PROTO (void, display_file, (char *filename, char *target)); | |
aa0a709a SC |
63 | PROTO (void, dump_data, (bfd * abfd)); |
64 | PROTO (void, dump_relocs, (bfd * abfd)); | |
65 | PROTO (void, dump_symbols, (bfd * abfd)); | |
66 | PROTO (void, print_arelt_descr, (FILE *, bfd * abfd, boolean verbose)); | |
67 | \f | |
2fa0b342 DHW |
68 | |
69 | ||
70 | ||
71 | ||
72 | ||
73 | ||
aa0a709a SC |
74 | char *machine = (char *) NULL; |
75 | asymbol **syms; | |
76 | asymbol **syms2; | |
2fa0b342 DHW |
77 | |
78 | ||
79 | unsigned int storage; | |
80 | ||
81 | unsigned int symcount = 0; | |
82 | ||
83 | void | |
84 | usage () | |
85 | { | |
86 | fprintf (stderr, | |
aa0a709a | 87 | "usage: %s [-ahifdrtxsl] [-m machine] [-j section_name] obj ...\n", |
2fa0b342 DHW |
88 | program_name); |
89 | exit (1); | |
90 | } | |
91 | ||
aa0a709a SC |
92 | static struct option long_options[]= |
93 | { | |
94 | {"syms", no_argument, &dump_symtab, 1}, | |
95 | {"reloc", no_argument, &dump_reloc_info, 1}, | |
96 | {"header", no_argument, &dump_section_headers, 1}, | |
73b8f102 JG |
97 | #ifdef ELF_STAB_DISPLAY |
98 | {"stabs", no_argument, &dump_stab_section_info, 1}, | |
99 | #endif | |
aa0a709a | 100 | {0, no_argument, 0, 0}}; |
2fa0b342 DHW |
101 | |
102 | ||
2fa0b342 | 103 | static void |
aa0a709a SC |
104 | dump_headers (abfd) |
105 | bfd *abfd; | |
2fa0b342 DHW |
106 | { |
107 | asection *section; | |
aa0a709a | 108 | |
2fa0b342 DHW |
109 | for (section = abfd->sections; |
110 | section != (asection *) NULL; | |
aa0a709a SC |
111 | section = section->next) |
112 | { | |
113 | char *comma = ""; | |
114 | ||
2fa0b342 | 115 | #define PF(x,y) \ |
e779a58c JG |
116 | if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; } |
117 | ||
118 | ||
aa0a709a SC |
119 | printf ("SECTION %d [%s]\t: size %08x", |
120 | section->index, | |
121 | section->name, | |
122 | (unsigned) bfd_get_section_size_before_reloc (section)); | |
123 | printf (" vma "); | |
124 | printf_vma (section->vma); | |
125 | printf (" align 2**%u\n ", | |
126 | section->alignment_power); | |
127 | PF (SEC_ALLOC, "ALLOC"); | |
128 | PF (SEC_CONSTRUCTOR, "CONSTRUCTOR"); | |
129 | PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT"); | |
130 | PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA"); | |
131 | PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS"); | |
132 | PF (SEC_LOAD, "LOAD"); | |
133 | PF (SEC_RELOC, "RELOC"); | |
134 | PF (SEC_BALIGN, "BALIGN"); | |
135 | PF (SEC_READONLY, "READONLY"); | |
136 | PF (SEC_CODE, "CODE"); | |
137 | PF (SEC_DATA, "DATA"); | |
138 | PF (SEC_ROM, "ROM"); | |
139 | printf ("\n"); | |
2fa0b342 | 140 | #undef PF |
aa0a709a | 141 | } |
2fa0b342 DHW |
142 | } |
143 | ||
144 | static asymbol ** | |
aa0a709a SC |
145 | DEFUN (slurp_symtab, (abfd), |
146 | bfd * abfd) | |
2fa0b342 | 147 | { |
aa0a709a | 148 | asymbol **sy = (asymbol **) NULL; |
2fa0b342 | 149 | |
aa0a709a SC |
150 | if (!(bfd_get_file_flags (abfd) & HAS_SYMS)) |
151 | { | |
152 | (void) printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd)); | |
153 | return (NULL); | |
154 | } | |
155 | ||
156 | storage = get_symtab_upper_bound (abfd); | |
157 | if (storage) | |
158 | { | |
159 | sy = (asymbol **) malloc (storage); | |
160 | if (sy == NULL) | |
161 | { | |
162 | fprintf (stderr, "%s: out of memory.\n", program_name); | |
163 | exit (1); | |
d20f480f | 164 | } |
aa0a709a SC |
165 | } |
166 | symcount = bfd_canonicalize_symtab (abfd, sy); | |
167 | return sy; | |
2fa0b342 | 168 | } |
aa0a709a | 169 | |
2fa0b342 | 170 | /* Sort symbols into value order */ |
aa0a709a SC |
171 | static int |
172 | comp (ap, bp) | |
770cde30 JG |
173 | PTR ap; |
174 | PTR bp; | |
2fa0b342 | 175 | { |
770cde30 JG |
176 | asymbol *a = *(asymbol **)ap; |
177 | asymbol *b = *(asymbol **)bp; | |
2fa0b342 DHW |
178 | int diff; |
179 | ||
aa0a709a | 180 | if (a->name == (char *) NULL || (a->flags & (BSF_DEBUGGING))) |
2fa0b342 | 181 | a->the_bfd = 0; |
aa0a709a SC |
182 | if (b->name == (char *) NULL || (b->flags & (BSF_DEBUGGING))) |
183 | b->the_bfd = 0; | |
2fa0b342 DHW |
184 | |
185 | diff = a->the_bfd - b->the_bfd; | |
aa0a709a SC |
186 | if (diff) |
187 | { | |
188 | return -diff; | |
189 | } | |
2fa0b342 | 190 | diff = a->value - b->value; |
aa0a709a SC |
191 | if (diff) |
192 | { | |
193 | return diff; | |
194 | } | |
195 | return a->section - b->section; | |
2fa0b342 DHW |
196 | } |
197 | ||
198 | /* Print the supplied address symbolically if possible */ | |
199 | void | |
aa0a709a SC |
200 | print_address (vma, stream) |
201 | bfd_vma vma; | |
202 | FILE *stream; | |
2fa0b342 DHW |
203 | { |
204 | /* Perform a binary search looking for the closest symbol to | |
205 | the required value */ | |
206 | ||
207 | unsigned int min = 0; | |
208 | unsigned int max = symcount; | |
209 | ||
210 | unsigned int thisplace = 1; | |
aa0a709a | 211 | unsigned int oldthisplace; |
2fa0b342 DHW |
212 | |
213 | int vardiff; | |
2fa0b342 | 214 | |
aa0a709a SC |
215 | if (symcount == 0) |
216 | { | |
217 | fprintf_vma (stream, vma); | |
2fa0b342 | 218 | } |
aa0a709a SC |
219 | else |
220 | { | |
221 | while (true) | |
222 | { | |
223 | oldthisplace = thisplace; | |
224 | thisplace = (max + min) / 2; | |
225 | if (thisplace == oldthisplace) | |
226 | break; | |
227 | vardiff = syms[thisplace]->value - vma; | |
228 | ||
229 | if (vardiff) | |
230 | { | |
231 | if (vardiff > 0) | |
232 | { | |
233 | max = thisplace; | |
234 | } | |
235 | else | |
236 | { | |
237 | min = thisplace; | |
238 | } | |
239 | } | |
240 | else | |
241 | { | |
242 | /* Totally awesome! the exact right symbol */ | |
243 | CONST char *match_name = syms[thisplace]->name; | |
244 | int sym_len = strlen (match_name); | |
245 | ||
246 | /* Avoid "filename.o" as a match */ | |
247 | if (sym_len > 2 | |
248 | && match_name[sym_len - 2] == '.' | |
249 | && match_name[sym_len - 1] == 'o' | |
250 | && thisplace + 1 < symcount | |
251 | && syms[thisplace + 1]->value == vma) | |
252 | match_name = syms[thisplace + 1]->name; | |
253 | /* Totally awesome! the exact right symbol */ | |
254 | fprintf_vma (stream, vma); | |
255 | fprintf (stream, " (%s+)0000", syms[thisplace]->name); | |
256 | return; | |
257 | } | |
258 | } | |
259 | /* We've run out of places to look, print the symbol before this one | |
260 | see if this or the symbol before describes this location the best */ | |
fc5d6074 | 261 | |
aa0a709a SC |
262 | if (thisplace != 0) |
263 | { | |
264 | if (syms[thisplace - 1]->value - vma > | |
265 | syms[thisplace]->value - vma) | |
266 | { | |
267 | /* Previous symbol is in correct section and is closer */ | |
268 | thisplace--; | |
269 | } | |
270 | } | |
fc5d6074 | 271 | |
aa0a709a SC |
272 | fprintf_vma (stream, vma); |
273 | if (syms[thisplace]->value > vma) | |
274 | { | |
275 | fprintf (stream, " (%s-)", syms[thisplace]->name); | |
276 | fprintf (stream, "%04x", syms[thisplace]->value - vma); | |
277 | } | |
278 | else | |
279 | { | |
280 | fprintf (stream, " (%s+)", syms[thisplace]->name); | |
281 | fprintf (stream, "%04x", vma - syms[thisplace]->value); | |
282 | } | |
2fa0b342 | 283 | } |
2fa0b342 DHW |
284 | } |
285 | ||
286 | void | |
aa0a709a SC |
287 | disassemble_data (abfd) |
288 | bfd *abfd; | |
2fa0b342 DHW |
289 | { |
290 | bfd_byte *data = NULL; | |
aa0a709a | 291 | bfd_arch_info_type *info; |
fc5d6074 SC |
292 | bfd_size_type datasize = 0; |
293 | bfd_size_type i; | |
aa0a709a SC |
294 | unsigned int (*print) ()= 0; |
295 | unsigned int print_insn_m68k (); | |
296 | unsigned int print_insn_a29k (); | |
297 | unsigned int print_insn_i960 (); | |
298 | unsigned int print_insn_sparc (); | |
299 | unsigned int print_insn_i386 (); | |
300 | unsigned int print_insn_h8300 (); | |
2fa0b342 | 301 | enum bfd_architecture a; |
d20f480f | 302 | |
2fa0b342 | 303 | asection *section; |
aa0a709a | 304 | |
2fa0b342 | 305 | /* Replace symbol section relative values with abs values */ |
96d7950b | 306 | boolean done_dot = false; |
aa0a709a SC |
307 | |
308 | for (i = 0; i < symcount; i++) | |
309 | { | |
2fa0b342 | 310 | syms[i]->value += syms[i]->section->vma; |
aa0a709a | 311 | } |
2fa0b342 DHW |
312 | |
313 | /* We keep a copy of the symbols in the original order */ | |
aa0a709a | 314 | syms2 = slurp_symtab (abfd); |
2fa0b342 DHW |
315 | |
316 | /* Sort the symbols into section and symbol order */ | |
aa0a709a | 317 | (void) qsort (syms, symcount, sizeof (asymbol *), comp); |
2fa0b342 DHW |
318 | |
319 | /* Find the first useless symbol */ | |
aa0a709a SC |
320 | { |
321 | unsigned int i; | |
2fa0b342 | 322 | |
aa0a709a SC |
323 | for (i = 0; i < symcount; i++) |
324 | { | |
325 | if (syms[i]->the_bfd == 0) | |
326 | { | |
327 | symcount = i; | |
328 | break; | |
329 | } | |
330 | } | |
331 | } | |
e779a58c JG |
332 | |
333 | ||
aa0a709a SC |
334 | if (machine != (char *) NULL) |
335 | { | |
336 | info = bfd_scan_arch (machine); | |
337 | if (info == 0) | |
338 | { | |
339 | fprintf (stderr, "%s: Can't use supplied machine %s\n", | |
340 | program_name, | |
341 | machine); | |
342 | exit (1); | |
343 | } | |
344 | abfd->arch_info = info; | |
2fa0b342 | 345 | } |
e779a58c JG |
346 | |
347 | /* See if we can disassemble using bfd */ | |
348 | ||
aa0a709a SC |
349 | if (abfd->arch_info->disassemble) |
350 | { | |
351 | print = abfd->arch_info->disassemble; | |
e779a58c | 352 | } |
aa0a709a SC |
353 | else |
354 | { | |
355 | a = bfd_get_arch (abfd); | |
356 | switch (a) | |
357 | { | |
358 | case bfd_arch_sparc: | |
359 | print = print_insn_sparc; | |
360 | break; | |
361 | case bfd_arch_i386: | |
362 | print = print_insn_i386; | |
363 | break; | |
364 | case bfd_arch_m68k: | |
365 | print = print_insn_m68k; | |
366 | break; | |
367 | case bfd_arch_a29k: | |
368 | print = print_insn_a29k; | |
369 | break; | |
370 | case bfd_arch_i960: | |
371 | print = print_insn_i960; | |
372 | break; | |
373 | default: | |
374 | fprintf (stderr, "%s: Can't disassemble for architecture %s\n", | |
375 | program_name, | |
376 | bfd_printable_arch_mach (bfd_get_arch (abfd), 0)); | |
377 | exit (1); | |
378 | } | |
2fa0b342 | 379 | |
aa0a709a | 380 | } |
2fa0b342 DHW |
381 | |
382 | for (section = abfd->sections; | |
aa0a709a SC |
383 | section != (asection *) NULL; |
384 | section = section->next) | |
65cceb78 | 385 | { |
2fa0b342 | 386 | |
aa0a709a SC |
387 | if ((section->flags & SEC_LOAD) |
388 | && (only == (char *) NULL || strcmp (only, section->name) == 0)) | |
389 | { | |
390 | printf ("Disassembly of section %s:\n", section->name); | |
2fa0b342 | 391 | |
aa0a709a SC |
392 | if (bfd_get_section_size_before_reloc (section) == 0) |
393 | continue; | |
2fa0b342 | 394 | |
aa0a709a | 395 | data = (bfd_byte *) malloc (bfd_get_section_size_before_reloc (section)); |
2fa0b342 | 396 | |
aa0a709a SC |
397 | if (data == (bfd_byte *) NULL) |
398 | { | |
399 | fprintf (stderr, "%s: memory exhausted.\n", program_name); | |
400 | exit (1); | |
401 | } | |
402 | datasize = bfd_get_section_size_before_reloc (section); | |
2fa0b342 | 403 | |
aa0a709a | 404 | bfd_get_section_contents (abfd, section, data, 0, bfd_get_section_size_before_reloc (section)); |
2fa0b342 | 405 | |
aa0a709a SC |
406 | i = 0; |
407 | while (i < bfd_get_section_size_before_reloc (section)) | |
408 | { | |
409 | if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 && | |
410 | data[i + 3] == 0) | |
411 | { | |
412 | if (done_dot == false) | |
413 | { | |
414 | printf ("...\n"); | |
415 | done_dot = true; | |
65cceb78 | 416 | } |
aa0a709a | 417 | i += 4; |
65cceb78 | 418 | } |
aa0a709a SC |
419 | else |
420 | { | |
421 | done_dot = false; | |
422 | if (with_line_numbers) | |
423 | { | |
424 | static prevline; | |
425 | CONST char *filename; | |
426 | CONST char *functionname; | |
427 | unsigned int line; | |
428 | ||
429 | bfd_find_nearest_line (abfd, | |
430 | section, | |
431 | syms, | |
432 | section->vma + i, | |
433 | &filename, | |
434 | &functionname, | |
435 | &line); | |
436 | ||
437 | if (filename && functionname && line && line != prevline) | |
438 | { | |
439 | printf ("%s:%u\n", filename, line); | |
440 | prevline = line; | |
441 | } | |
442 | } | |
443 | print_address (section->vma + i, stdout); | |
444 | printf (" "); | |
65cceb78 | 445 | |
aa0a709a SC |
446 | i += print (section->vma + i, |
447 | data + i, | |
448 | stdout); | |
449 | putchar ('\n'); | |
450 | } | |
96d7950b | 451 | } |
aa0a709a | 452 | free (data); |
96d7950b | 453 | } |
2fa0b342 | 454 | } |
2fa0b342 | 455 | } |
73b8f102 JG |
456 | \f |
457 | #ifdef ELF_STAB_DISPLAY | |
458 | ||
459 | /* Define a table of stab values and print-strings. We wish the initializer | |
460 | could be a direct-mapped table, but instead we build one the first | |
461 | time we need it. */ | |
462 | ||
463 | #define STAB_STRING_LENGTH 6 | |
464 | ||
465 | char stab_name[256][STAB_STRING_LENGTH]; | |
466 | ||
467 | struct stab_print { | |
468 | int value; | |
469 | char string[STAB_STRING_LENGTH]; | |
470 | }; | |
471 | ||
472 | struct stab_print stab_print[] = { | |
473 | #define __define_stab(NAME, CODE, STRING) {CODE, STRING}, | |
474 | #include "aout/stab.def" | |
475 | #undef __define_stab | |
476 | {0, 0} | |
477 | }; | |
478 | ||
479 | /* This is a kludge for dumping the stabs section from an ELF file that | |
480 | uses Sun stabs encoding. It has to use some hooks into BFD because | |
481 | string table sections are not normally visible to BFD callers. */ | |
482 | ||
483 | void | |
484 | dump_elf_stabs (abfd) | |
485 | bfd *abfd; | |
486 | { | |
487 | Elf_Internal_Shdr *stab_hdr, *stabstr_hdr; | |
488 | char *strtab; | |
489 | struct internal_nlist *stabs, *stabs_end; | |
490 | int i, j; | |
491 | ||
492 | /* Initialize stab name array if first time. */ | |
493 | if (stab_name[0][0] == 0) | |
494 | { | |
495 | /* Fill in numeric values for all possible strings. */ | |
496 | for (i = 0; i < 256; i++) | |
497 | { | |
498 | sprintf (stab_name[i], "%d", i); | |
499 | } | |
500 | for (i = 0; stab_print[i].string[0]; i++) | |
501 | strcpy (stab_name[stab_print[i].value], stab_print[i].string); | |
502 | } | |
503 | ||
504 | if (0 != strncmp ("elf", abfd->xvec->name, 3)) | |
505 | { | |
506 | fprintf (stderr, "%s: %s is not in ELF format.\n", program_name, | |
507 | abfd->filename); | |
508 | return; | |
509 | } | |
510 | ||
511 | stab_hdr = bfd_elf_find_section (abfd, ".stab"); | |
512 | if (0 == stab_hdr) | |
513 | { | |
514 | fprintf (stderr, "%s: %s has no .stab section.\n", program_name, | |
515 | abfd->filename); | |
516 | return; | |
517 | } | |
518 | ||
519 | stabstr_hdr = bfd_elf_find_section (abfd, ".stabstr"); | |
520 | if (0 == stabstr_hdr) | |
521 | { | |
522 | fprintf (stderr, "%s: %s has no .stabstr section.\n", program_name, | |
523 | abfd->filename); | |
524 | return; | |
525 | } | |
526 | ||
527 | stabs = (struct internal_nlist *) xmalloc (stab_hdr ->sh_size); | |
528 | strtab = (char *) xmalloc (stabstr_hdr->sh_size); | |
529 | stabs_end = (struct internal_nlist *) (stab_hdr->sh_size + (char *)stabs); | |
530 | ||
531 | if (bfd_seek (abfd, stab_hdr->sh_offset, L_SET) < 0 || | |
532 | stab_hdr->sh_size != bfd_read ((PTR)stabs, stab_hdr->sh_size, 1, abfd)) | |
533 | { | |
534 | fprintf (stderr, "%s: reading .stab section of %s failed.\n", | |
535 | program_name, | |
536 | abfd->filename); | |
537 | return; | |
538 | } | |
2fa0b342 | 539 | |
73b8f102 JG |
540 | if (bfd_seek (abfd, stabstr_hdr->sh_offset, L_SET) < 0 || |
541 | stabstr_hdr->sh_size != bfd_read ((PTR)strtab, stabstr_hdr->sh_size, | |
542 | 1, abfd)) | |
543 | { | |
544 | fprintf (stderr, "%s: reading .stabstr section of %s failed.\n", | |
545 | program_name, | |
546 | abfd->filename); | |
547 | return; | |
548 | } | |
549 | ||
550 | #define SWAP_SYMBOL(symp, abfd) \ | |
551 | { \ | |
552 | (symp)->n_strx = bfd_h_get_32(abfd, \ | |
553 | (unsigned char *)&(symp)->n_strx); \ | |
554 | (symp)->n_desc = bfd_h_get_16 (abfd, \ | |
555 | (unsigned char *)&(symp)->n_desc); \ | |
556 | (symp)->n_value = bfd_h_get_32 (abfd, \ | |
557 | (unsigned char *)&(symp)->n_value); \ | |
558 | } | |
559 | ||
560 | printf ("Contents of .stab section:\n\n"); | |
561 | printf ("Symnum n_type n_othr n_desc n_value n_strx String\n"); | |
562 | ||
563 | /* We start the index at -1 because there is a dummy symbol on | |
564 | the front of Sun's stabs-in-elf sections. */ | |
565 | for (i = -1; stabs < stabs_end; stabs++, i++) | |
566 | { | |
567 | SWAP_SYMBOL (stabs, abfd); | |
568 | printf ("\n%-6d %-6s %-6d %-6d %08x %-6d", i, | |
569 | stab_name [stabs->n_type], | |
570 | stabs->n_other, stabs->n_desc, stabs->n_value, | |
571 | stabs->n_strx); | |
572 | if (stabs->n_strx < stabstr_hdr->sh_size) | |
573 | printf (" %s", &strtab[stabs->n_strx]); | |
574 | } | |
575 | printf ("\n\n"); | |
576 | } | |
577 | #endif /* ELF_STAB_DISPLAY */ | |
578 | \f | |
2fa0b342 DHW |
579 | void |
580 | display_bfd (abfd) | |
581 | bfd *abfd; | |
582 | { | |
583 | ||
aa0a709a SC |
584 | if (!bfd_check_format (abfd, bfd_object)) |
585 | { | |
586 | fprintf (stderr, "%s: %s not an object file\n", program_name, | |
587 | abfd->filename); | |
588 | return; | |
589 | } | |
2fa0b342 | 590 | printf ("\n%s: file format %s\n", abfd->filename, abfd->xvec->name); |
aa0a709a SC |
591 | if (dump_ar_hdrs) |
592 | print_arelt_descr (stdout, abfd, true); | |
2fa0b342 | 593 | |
aa0a709a SC |
594 | if (dump_file_header) |
595 | { | |
596 | char *comma = ""; | |
597 | ||
598 | printf ("architecture: %s, ", | |
599 | bfd_printable_arch_mach (bfd_get_arch (abfd), | |
600 | bfd_get_mach (abfd))); | |
601 | printf ("flags 0x%08x:\n", abfd->flags); | |
2fa0b342 | 602 | |
2fa0b342 | 603 | #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";} |
aa0a709a SC |
604 | PF (HAS_RELOC, "HAS_RELOC"); |
605 | PF (EXEC_P, "EXEC_P"); | |
606 | PF (HAS_LINENO, "HAS_LINENO"); | |
607 | PF (HAS_DEBUG, "HAS_DEBUG"); | |
608 | PF (HAS_SYMS, "HAS_SYMS"); | |
609 | PF (HAS_LOCALS, "HAS_LOCALS"); | |
610 | PF (DYNAMIC, "DYNAMIC"); | |
611 | PF (WP_TEXT, "WP_TEXT"); | |
612 | PF (D_PAGED, "D_PAGED"); | |
613 | printf ("\nstart address 0x"); | |
614 | printf_vma (abfd->start_address); | |
615 | } | |
616 | printf ("\n"); | |
2fa0b342 DHW |
617 | |
618 | if (dump_section_headers) | |
aa0a709a SC |
619 | dump_headers (abfd); |
620 | if (dump_symtab || dump_reloc_info || disassemble) | |
621 | { | |
622 | syms = slurp_symtab (abfd); | |
623 | } | |
624 | if (dump_symtab) | |
625 | dump_symbols (abfd); | |
73b8f102 JG |
626 | #ifdef ELF_STAB_DISPLAY |
627 | if (dump_stab_section_info) | |
628 | dump_elf_stabs (abfd); | |
629 | #endif | |
aa0a709a SC |
630 | if (dump_reloc_info) |
631 | dump_relocs (abfd); | |
632 | if (dump_section_contents) | |
633 | dump_data (abfd); | |
634 | if (disassemble) | |
635 | disassemble_data (abfd); | |
2fa0b342 DHW |
636 | } |
637 | ||
638 | void | |
639 | display_file (filename, target) | |
640 | char *filename; | |
641 | char *target; | |
642 | { | |
643 | bfd *file, *arfile = (bfd *) NULL; | |
644 | ||
645 | file = bfd_openr (filename, target); | |
aa0a709a SC |
646 | if (file == NULL) |
647 | { | |
648 | bfd_perror (filename); | |
649 | return; | |
650 | } | |
2fa0b342 | 651 | |
aa0a709a SC |
652 | if (bfd_check_format (file, bfd_archive) == true) |
653 | { | |
654 | printf ("In archive %s:\n", bfd_get_filename (file)); | |
655 | for (;;) | |
656 | { | |
657 | bfd_error = no_error; | |
658 | ||
659 | arfile = bfd_openr_next_archived_file (file, arfile); | |
660 | if (arfile == NULL) | |
661 | { | |
662 | if (bfd_error != no_more_archived_files) | |
663 | bfd_perror (bfd_get_filename (file)); | |
664 | return; | |
665 | } | |
2fa0b342 | 666 | |
aa0a709a SC |
667 | display_bfd (arfile); |
668 | /* Don't close the archive elements; we need them for next_archive */ | |
669 | } | |
2fa0b342 | 670 | } |
2fa0b342 | 671 | else |
aa0a709a | 672 | display_bfd (file); |
2fa0b342 | 673 | |
aa0a709a | 674 | bfd_close (file); |
2fa0b342 DHW |
675 | } |
676 | \f | |
677 | /* Actually display the various requested regions */ | |
678 | ||
2fa0b342 DHW |
679 | void |
680 | dump_data (abfd) | |
681 | bfd *abfd; | |
682 | { | |
683 | asection *section; | |
aa0a709a | 684 | bfd_byte *data = 0; |
fc5d6074 SC |
685 | bfd_size_type datasize = 0; |
686 | bfd_size_type i; | |
2fa0b342 DHW |
687 | |
688 | for (section = abfd->sections; section != NULL; section = | |
aa0a709a SC |
689 | section->next) |
690 | { | |
691 | int onaline = 16; | |
2fa0b342 | 692 | |
aa0a709a SC |
693 | if (only == (char *) NULL || |
694 | strcmp (only, section->name) == 0) | |
60c80016 | 695 | { |
aa0a709a SC |
696 | if (section->flags & SEC_HAS_CONTENTS) |
697 | { | |
698 | printf ("Contents of section %s:\n", section->name); | |
699 | ||
700 | if (bfd_get_section_size_before_reloc (section) == 0) | |
701 | continue; | |
702 | data = (bfd_byte *) malloc (bfd_get_section_size_before_reloc (section)); | |
703 | if (data == (bfd_byte *) NULL) | |
704 | { | |
705 | fprintf (stderr, "%s: memory exhausted.\n", program_name); | |
706 | exit (1); | |
707 | } | |
708 | datasize = bfd_get_section_size_before_reloc (section); | |
2fa0b342 | 709 | |
2fa0b342 | 710 | |
aa0a709a | 711 | bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_get_section_size_before_reloc (section)); |
2fa0b342 | 712 | |
aa0a709a SC |
713 | for (i = 0; i < bfd_get_section_size_before_reloc (section); i += onaline) |
714 | { | |
715 | bfd_size_type j; | |
716 | ||
717 | printf (" %04lx ", (unsigned long int) (i + section->vma)); | |
718 | for (j = i; j < i + onaline; j++) | |
719 | { | |
720 | if (j < bfd_get_section_size_before_reloc (section)) | |
721 | printf ("%02x", (unsigned) (data[j])); | |
722 | else | |
723 | printf (" "); | |
724 | if ((j & 3) == 3) | |
725 | printf (" "); | |
726 | } | |
2fa0b342 | 727 | |
aa0a709a SC |
728 | printf (" "); |
729 | for (j = i; j < i + onaline; j++) | |
730 | { | |
731 | if (j >= bfd_get_section_size_before_reloc (section)) | |
732 | printf (" "); | |
733 | else | |
734 | printf ("%c", isprint (data[j]) ? data[j] : '.'); | |
735 | } | |
736 | putchar ('\n'); | |
737 | } | |
60c80016 | 738 | } |
2fa0b342 | 739 | } |
aa0a709a | 740 | free (data); |
2fa0b342 | 741 | } |
2fa0b342 DHW |
742 | } |
743 | ||
2fa0b342 DHW |
744 | /* Should perhaps share code and display with nm? */ |
745 | void | |
746 | dump_symbols (abfd) | |
747 | bfd *abfd; | |
748 | { | |
749 | ||
750 | unsigned int count; | |
751 | asymbol **current = syms; | |
2fa0b342 | 752 | |
aa0a709a | 753 | printf ("SYMBOL TABLE:\n"); |
e779a58c | 754 | |
aa0a709a SC |
755 | for (count = 0; count < symcount; count++) |
756 | { | |
2fa0b342 | 757 | |
aa0a709a SC |
758 | if (*current && (*current)->the_bfd) |
759 | { | |
760 | bfd_print_symbol ((*current)->the_bfd, | |
761 | stdout, | |
762 | *current, bfd_print_symbol_all); | |
e779a58c | 763 | |
aa0a709a SC |
764 | printf ("\n"); |
765 | ||
766 | } | |
767 | current++; | |
2fa0b342 | 768 | } |
aa0a709a SC |
769 | printf ("\n"); |
770 | printf ("\n"); | |
2fa0b342 DHW |
771 | } |
772 | ||
2fa0b342 | 773 | void |
aa0a709a SC |
774 | dump_relocs (abfd) |
775 | bfd *abfd; | |
2fa0b342 DHW |
776 | { |
777 | arelent **relpp; | |
778 | unsigned int relcount; | |
779 | asection *a; | |
aa0a709a SC |
780 | |
781 | for (a = abfd->sections; a != (asection *) NULL; a = a->next) | |
782 | { | |
783 | if (a == &bfd_abs_section) | |
784 | continue; | |
785 | if (a == &bfd_und_section) | |
786 | continue; | |
787 | if (a == &bfd_com_section) | |
788 | continue; | |
789 | ||
790 | printf ("RELOCATION RECORDS FOR [%s]:", a->name); | |
791 | ||
792 | if (bfd_get_reloc_upper_bound (abfd, a) == 0) | |
793 | { | |
794 | printf (" (none)\n\n"); | |
d20f480f | 795 | } |
aa0a709a SC |
796 | else |
797 | { | |
d20f480f SC |
798 | arelent **p; |
799 | ||
aa0a709a SC |
800 | relpp = (arelent **) xmalloc (bfd_get_reloc_upper_bound (abfd, a)); |
801 | relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms); | |
802 | if (relcount == 0) | |
803 | { | |
804 | printf (" (none)\n\n"); | |
d20f480f | 805 | } |
aa0a709a SC |
806 | else |
807 | { | |
808 | printf ("\n"); | |
809 | printf ("OFFSET TYPE VALUE \n"); | |
d20f480f | 810 | |
aa0a709a SC |
811 | for (p = relpp; relcount && *p != (arelent *) NULL; p++, |
812 | relcount--) | |
813 | { | |
d20f480f SC |
814 | arelent *q = *p; |
815 | CONST char *sym_name; | |
aa0a709a | 816 | |
d20f480f SC |
817 | /* CONST char *section_name = q->section == (asection *)NULL ? "*abs" :*/ |
818 | /* q->section->name;*/ | |
aa0a709a SC |
819 | CONST char *section_name = (*(q->sym_ptr_ptr))->section->name; |
820 | ||
821 | if (q->sym_ptr_ptr && *q->sym_ptr_ptr) | |
822 | { | |
823 | sym_name = (*(q->sym_ptr_ptr))->name; | |
d20f480f | 824 | } |
aa0a709a SC |
825 | else |
826 | { | |
d20f480f SC |
827 | sym_name = 0; |
828 | } | |
aa0a709a SC |
829 | if (sym_name) |
830 | { | |
831 | printf_vma (q->address); | |
832 | printf (" %-8s %s", | |
833 | q->howto->name, | |
834 | sym_name); | |
d20f480f | 835 | } |
aa0a709a SC |
836 | else |
837 | { | |
838 | printf_vma (q->address); | |
839 | printf (" %-8s [%s]", | |
840 | q->howto->name, | |
841 | section_name); | |
d20f480f | 842 | } |
aa0a709a SC |
843 | if (q->addend) |
844 | { | |
845 | printf ("+0x"); | |
846 | printf_vma (q->addend); | |
d20f480f | 847 | } |
aa0a709a | 848 | printf ("\n"); |
d20f480f | 849 | } |
aa0a709a SC |
850 | printf ("\n\n"); |
851 | free (relpp); | |
d20f480f | 852 | } |
2fa0b342 | 853 | } |
2fa0b342 | 854 | |
d20f480f | 855 | } |
2fa0b342 DHW |
856 | } |
857 | ||
aa0a709a SC |
858 | #ifdef unix |
859 | #define _DUMMY_NAME_ "/dev/null" | |
860 | #else | |
861 | #define _DUMMY_NAME_ "##dummy" | |
862 | #endif | |
9872a49c | 863 | static void |
aa0a709a SC |
864 | DEFUN (display_info_table, (first, last), |
865 | int first AND int last) | |
9872a49c | 866 | { |
3fdbfe8d | 867 | unsigned int i, j; |
9872a49c SC |
868 | extern bfd_target *target_vector[]; |
869 | ||
aa0a709a SC |
870 | printf ("\n%12s", " "); |
871 | for (i = first; i++ < last && target_vector[i];) | |
872 | printf ("%s ", target_vector[i]->name); | |
873 | printf ("\n"); | |
9872a49c | 874 | |
aa0a709a SC |
875 | for (j = (int) bfd_arch_obscure + 1; (int) j < (int) bfd_arch_last; j++) |
876 | if (strcmp (bfd_printable_arch_mach (j, 0), "UNKNOWN!") != 0) | |
e779a58c | 877 | { |
aa0a709a SC |
878 | printf ("%11s ", bfd_printable_arch_mach (j, 0)); |
879 | for (i = first; i++ < last && target_vector[i];) | |
880 | { | |
881 | bfd_target *p = target_vector[i]; | |
882 | bfd *abfd = bfd_openw (_DUMMY_NAME_, p->name); | |
883 | int l = strlen (p->name); | |
884 | int ok = bfd_set_arch_mach (abfd, j, 0); | |
885 | ||
886 | if (ok) | |
887 | printf ("%s ", p->name); | |
888 | else | |
e779a58c | 889 | { |
aa0a709a SC |
890 | while (l--) |
891 | printf ("%c", ok ? '*' : '-'); | |
892 | printf (" "); | |
e779a58c | 893 | } |
e779a58c | 894 | } |
aa0a709a | 895 | printf ("\n"); |
e779a58c | 896 | } |
9872a49c | 897 | } |
aa0a709a SC |
898 | |
899 | static void | |
900 | DEFUN_VOID (display_info) | |
901 | { | |
902 | char *colum; | |
903 | unsigned int i, j, columns; | |
904 | extern bfd_target *target_vector[]; | |
905 | extern char *getenv (); | |
906 | ||
907 | printf ("BFD header file version %s\n", BFD_VERSION); | |
908 | for (i = 0; target_vector[i]; i++) | |
909 | { | |
910 | bfd_target *p = target_vector[i]; | |
911 | bfd *abfd = bfd_openw (_DUMMY_NAME_, p->name); | |
912 | ||
913 | printf ("%s\n (header %s, data %s)\n", p->name, | |
914 | p->header_byteorder_big_p ? "big endian" : "little endian", | |
915 | p->byteorder_big_p ? "big endian" : "little endian"); | |
916 | for (j = (int) bfd_arch_obscure + 1; j < (int) bfd_arch_last; j++) | |
917 | if (bfd_set_arch_mach (abfd, (enum bfd_architecture) j, 0)) | |
918 | printf (" %s\n", | |
919 | bfd_printable_arch_mach ((enum bfd_architecture) j, 0)); | |
920 | } | |
921 | columns = 0; | |
922 | if (colum = getenv ("COLUMNS")) | |
923 | columns = atoi (colum); | |
924 | if (!columns) | |
925 | columns = 80; | |
926 | for (i = 0; target_vector[i];) | |
927 | { | |
928 | int old; | |
929 | old = i; | |
930 | for (j = 12; target_vector[i] && j < columns; i++) | |
931 | j += strlen (target_vector[i]->name) + 1; | |
932 | i--; | |
933 | if (old == i) | |
934 | break; | |
935 | display_info_table (old, i); | |
936 | } | |
937 | } | |
938 | ||
2fa0b342 DHW |
939 | /** main and like trivia */ |
940 | int | |
941 | main (argc, argv) | |
942 | int argc; | |
943 | char **argv; | |
944 | { | |
945 | int c; | |
946 | extern int optind; | |
947 | extern char *optarg; | |
948 | char *target = default_target; | |
949 | boolean seenflag = false; | |
950 | int ind = 0; | |
951 | ||
aa0a709a | 952 | bfd_init (); |
2fa0b342 DHW |
953 | program_name = *argv; |
954 | ||
3fdbfe8d | 955 | while ((c = getopt_long (argc, argv, "ib:m:dlfahrtxsj:", long_options, &ind)) |
aa0a709a SC |
956 | != EOF) |
957 | { | |
958 | seenflag = true; | |
959 | switch (c) | |
960 | { | |
961 | case 'm': | |
962 | machine = optarg; | |
963 | break; | |
964 | case 'j': | |
965 | only = optarg; | |
966 | break; | |
967 | case 'l': | |
968 | with_line_numbers = 1; | |
969 | break; | |
970 | case 'b': | |
971 | target = optarg; | |
972 | break; | |
973 | case 'f': | |
974 | dump_file_header = true; | |
975 | break; | |
976 | case 'i': | |
977 | info = true; | |
978 | break; | |
979 | case 'x': | |
980 | dump_symtab = 1; | |
981 | dump_reloc_info = 1; | |
982 | dump_file_header = true; | |
983 | dump_ar_hdrs = 1; | |
984 | dump_section_headers = 1; | |
985 | break; | |
986 | case 0: | |
987 | break; /* we've been given a long option */ | |
988 | case 't': | |
989 | dump_symtab = 1; | |
990 | break; | |
991 | case 'd': | |
992 | disassemble = true; | |
993 | break; | |
994 | case 's': | |
995 | dump_section_contents = 1; | |
996 | break; | |
997 | case 'r': | |
998 | dump_reloc_info = 1; | |
999 | break; | |
1000 | case 'a': | |
1001 | dump_ar_hdrs = 1; | |
1002 | break; | |
1003 | case 'h': | |
1004 | dump_section_headers = 1; | |
1005 | break; | |
1006 | default: | |
1007 | usage (); | |
1008 | } | |
2fa0b342 | 1009 | } |
2fa0b342 DHW |
1010 | |
1011 | if (seenflag == false) | |
1012 | usage (); | |
1013 | ||
aa0a709a SC |
1014 | if (info) |
1015 | { | |
1016 | display_info (); | |
1017 | } | |
1018 | else | |
1019 | { | |
1020 | if (optind == argc) | |
1021 | display_file ("a.out", target); | |
1022 | else | |
1023 | for (; optind < argc;) | |
1024 | display_file (argv[optind++], target); | |
1025 | } | |
2fa0b342 DHW |
1026 | return 0; |
1027 | } |