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