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