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