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