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