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