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