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