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