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