861cc18103f353c002f3e4c7ecfd07a9636fa666
[deliverable/binutils-gdb.git] / binutils / objdump.c
1 /* objdump.c -- dump information about an object file.
2 Copyright (C) 1990, 1991 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Diddler.
5
6 BFD 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 BFD 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 BFD; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /*
21 $Id$
22 */
23 /*
24 * Until there is other documentation, refer to the manual page dump(1) in
25 * the system 5 program's reference manual
26 */
27
28 #include "bfd.h"
29 #include "sysdep.h"
30 #include "getopt.h"
31 #include <stdio.h>
32 #include <ctype.h>
33
34
35
36 char *xmalloc();
37
38 char *default_target = NULL; /* default at runtime */
39
40 char *program_name = NULL;
41
42 int dump_section_contents; /* -s */
43 int dump_section_headers; /* -h */
44 boolean dump_file_header; /* -f */
45 int dump_symtab; /* -t */
46 int dump_reloc_info; /* -r */
47 int dump_ar_hdrs; /* -a */
48 int with_line_numbers; /* -l */
49 boolean disassemble; /* -d */
50 boolean info; /* -i */
51 char *only;
52
53 PROTO (void, display_file, (char *filename, char *target));
54 PROTO (void, dump_data, (bfd *abfd));
55 PROTO (void, dump_relocs, (bfd *abfd));
56 PROTO (void, dump_symbols, (bfd *abfd));
57 PROTO (void, print_arelt_descr, (bfd *abfd, boolean verbose));
58
59
60
61
62
63
64 \f
65 char *machine = (char *)NULL;
66 asymbol **syms;
67 asymbol **syms2;
68
69
70 unsigned int storage;
71
72 unsigned int symcount = 0;
73
74 void
75 usage ()
76 {
77 fprintf (stderr,
78 "usage: %s [-ahifdrtxsl] [-m machine] [-j section_name] obj ...\n",
79 program_name);
80 exit (1);
81 }
82
83 static struct option long_options[] =
84 {{"syms", 0, &dump_symtab, 1},
85 {"reloc", 0, &dump_reloc_info, 1},
86 {"header", 0, &dump_section_headers, 1},
87 {0, 0, 0, 0}};
88
89
90
91 static void
92 dump_headers(abfd)
93 bfd *abfd;
94 {
95 asection *section;
96 for (section = abfd->sections;
97 section != (asection *) NULL;
98 section = section->next)
99 {
100 char *comma = "";
101 #define PF(x,y) \
102 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
103
104
105 printf("SECTION %d [%s]\t: size %08x",
106 section->index,
107 section->name,
108 (unsigned) bfd_get_section_size_before_reloc(section));
109 printf(" vma ");
110 printf_vma(section->vma);
111 printf(" align 2**%u\n ",
112 section->alignment_power);
113 PF(SEC_ALLOC,"ALLOC");
114 PF(SEC_CONSTRUCTOR,"CONSTRUCTOR");
115 PF(SEC_CONSTRUCTOR_TEXT,"CONSTRUCTOR TEXT");
116 PF(SEC_CONSTRUCTOR_DATA,"CONSTRUCTOR DATA");
117 PF(SEC_CONSTRUCTOR_BSS,"CONSTRUCTOR BSS");
118 PF(SEC_LOAD,"LOAD");
119 PF(SEC_RELOC,"RELOC");
120 PF(SEC_BALIGN,"BALIGN");
121 PF(SEC_READONLY,"READONLY");
122 PF(SEC_CODE,"CODE");
123 PF(SEC_DATA,"DATA");
124 PF(SEC_ROM,"ROM");
125 printf("\n");
126 #undef PF
127 }
128 }
129
130 static asymbol **
131 DEFUN(slurp_symtab,(abfd),
132 bfd *abfd)
133 {
134 asymbol **sy = (asymbol **)NULL;
135
136 if (!(bfd_get_file_flags (abfd) & HAS_SYMS)) {
137 (void) printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
138 return(NULL);
139 }
140
141 storage = get_symtab_upper_bound (abfd);
142 if (storage) {
143 sy = (asymbol **) malloc (storage);
144 if (sy == NULL) {
145 fprintf (stderr, "%s: out of memory.\n", program_name);
146 exit (1);
147 }
148 }
149 symcount = bfd_canonicalize_symtab (abfd, sy);
150 return sy;
151 }
152 /* Sort symbols into value order */
153 static int comp(ap,bp)
154 asymbol **ap;
155 asymbol **bp;
156 {
157 asymbol *a = *ap;
158 asymbol *b = *bp;
159 int diff;
160
161 if ( a->name== (char *)NULL || (a->flags &( BSF_DEBUGGING) ))
162 a->the_bfd = 0;
163 if ( b->name== (char *)NULL || (b->flags &( BSF_DEBUGGING)))
164 b->the_bfd =0;
165
166 diff = a->the_bfd - b->the_bfd;
167 if (diff) {
168 return -diff;
169 }
170 diff = a->value - b->value;
171 if (diff) {
172 return diff;
173 }
174 return a->section - b->section;
175 }
176
177 /* Print the supplied address symbolically if possible */
178 void
179 print_address(vma, stream)
180 bfd_vma vma;
181 FILE *stream;
182 {
183 /* Perform a binary search looking for the closest symbol to
184 the required value */
185
186 unsigned int min = 0;
187 unsigned int max = symcount;
188
189 unsigned int thisplace = 1;
190 unsigned int oldthisplace ;
191
192 int vardiff;
193 if (symcount == 0) {
194 fprintf_vma(stream, vma);
195 }
196 else {
197 while (true) {
198 oldthisplace = thisplace;
199 thisplace = (max + min )/2 ;
200 if (thisplace == oldthisplace) break;
201 vardiff = syms[thisplace]->value - vma;
202
203 if (vardiff) {
204 if (vardiff > 0) {
205 max = thisplace;
206 }
207 else {
208 min = thisplace;
209 }
210 }
211 else {
212 /* Totally awesome! the exact right symbol */
213 CONST char *match_name = syms[thisplace]->name;
214 int sym_len = strlen(match_name);
215 /* Avoid "filename.o" as a match */
216 if (sym_len > 2
217 && match_name[sym_len - 2] == '.'
218 && match_name[sym_len - 1] == 'o'
219 && thisplace + 1 < symcount
220 && syms[thisplace+1]->value == vma)
221 match_name = syms[thisplace+1]->name;
222 /* Totally awesome! the exact right symbol */
223 fprintf_vma(stream, vma);
224 fprintf(stream," (%s+)0000", syms[thisplace]->name);
225 return;
226 }
227 }
228 /* We've run out of places to look, print the symbol before this one
229 see if this or the symbol before describes this location the best */
230
231 if (thisplace != 0) {
232 if (syms[thisplace-1]->value - vma >
233 syms[thisplace]->value-vma) {
234 /* Previous symbol is in correct section and is closer */
235 thisplace --;
236 }
237 }
238
239 fprintf_vma(stream, vma);
240 if (syms[thisplace]->value > vma) {
241 fprintf(stream," (%s-)", syms[thisplace]->name);
242 fprintf(stream,"%04x", syms[thisplace]->value - vma);
243
244 }
245 else {
246 fprintf(stream," (%s+)", syms[thisplace]->name);
247 fprintf(stream, "%04x", vma - syms[thisplace]->value);
248
249
250 }
251 }
252 }
253
254 void
255 disassemble_data(abfd)
256 bfd *abfd;
257 {
258 bfd_byte *data = NULL;
259 bfd_arch_info_type *info ;
260 bfd_size_type datasize = 0;
261 bfd_size_type i;
262 unsigned int (*print)() =0;
263 unsigned int print_insn_m68k();
264 unsigned int print_insn_a29k();
265 unsigned int print_insn_i960();
266 unsigned int print_insn_sparc();
267 unsigned int print_insn_h8300();
268 enum bfd_architecture a;
269
270 asection *section;
271 /* Replace symbol section relative values with abs values */
272 boolean done_dot = false;
273
274 for (i = 0; i < symcount; i++) {
275 syms[i]->value += syms[i]->section->vma;
276 }
277
278 /* We keep a copy of the symbols in the original order */
279 syms2 = slurp_symtab(abfd);
280
281 /* Sort the symbols into section and symbol order */
282 (void) qsort(syms, symcount, sizeof(asymbol *), comp);
283
284 /* Find the first useless symbol */
285 { unsigned int i;
286 for (i =0; i < symcount; i++) {
287 if (syms[i]->the_bfd == 0) {
288 symcount =i;
289 break;
290 }
291 }
292 }
293
294
295
296
297 if (machine!= (char *)NULL) {
298 info = bfd_scan_arch(machine);
299 if (info == 0) {
300 fprintf(stderr,"%s: Can't use supplied machine %s\n",
301 program_name,
302 machine);
303 exit(1);
304 }
305 abfd->arch_info = info;
306 }
307
308 /* See if we can disassemble using bfd */
309
310 if(abfd->arch_info->disassemble) {
311 print = abfd->arch_info->disassemble;
312 }
313 else {
314 a = bfd_get_arch(abfd);
315 switch (a) {
316 case bfd_arch_sparc:
317 print = print_insn_sparc;
318 break;
319 case bfd_arch_m68k:
320 print = print_insn_m68k;
321 break;
322 case bfd_arch_a29k:
323 print = print_insn_a29k;
324 break;
325 case bfd_arch_i960:
326 print = print_insn_i960;
327 break;
328 default:
329 fprintf(stderr,"%s: Can't disassemble for architecture %s\n",
330 program_name,
331 bfd_printable_arch_mach(bfd_get_arch(abfd),0));
332 exit(1);
333 }
334
335 }
336
337 for (section = abfd->sections;
338 section != (asection *)NULL;
339 section = section->next)
340 {
341
342 if ((section->flags & SEC_LOAD)
343 &&(only == (char *)NULL ||strcmp(only,section->name) == 0))
344 {
345 printf("Disassembly of section %s:\n", section->name);
346
347 if (bfd_get_section_size_before_reloc(section) == 0) continue;
348
349 data = (bfd_byte *)malloc(bfd_get_section_size_before_reloc(section));
350
351 if (data == (bfd_byte *)NULL) {
352 fprintf (stderr, "%s: memory exhausted.\n", program_name);
353 exit (1);
354 }
355 datasize = bfd_get_section_size_before_reloc(section);
356
357
358 bfd_get_section_contents (abfd, section, data, 0, bfd_get_section_size_before_reloc(section));
359
360 i = 0;
361 while (i <bfd_get_section_size_before_reloc(section)) {
362 if (data[i] ==0 && data[i+1] == 0 && data[i+2] == 0 &&
363 data[i+3] == 0) {
364 if (done_dot == false) {
365 printf("...\n");
366 done_dot=true;
367 }
368 i+=4;
369 }
370 else {
371 done_dot = false;
372 if (with_line_numbers) {
373 static prevline;
374 CONST char *filename;
375 CONST char *functionname;
376 unsigned int line;
377 bfd_find_nearest_line(abfd,
378 section,
379 syms,
380 section->vma + i,
381 &filename,
382 &functionname,
383 &line);
384
385 if (filename && functionname && line && line != prevline) {
386 printf("%s:%u\n", filename, line);
387 prevline = line;
388 }
389 }
390 print_address(section->vma + i, stdout);
391 printf(" ");
392
393 i += print(section->vma + i,
394 data + i,
395 stdout);
396 putchar ('\n') ;
397 }
398 }
399
400
401
402 free(data);
403 }
404 }
405 }
406
407 void
408 display_bfd (abfd)
409 bfd *abfd;
410 {
411
412 if (!bfd_check_format (abfd, bfd_object)) {
413 fprintf (stderr,"%s: %s not an object file\n", program_name,
414 abfd->filename);
415 return;
416 }
417 printf ("\n%s: file format %s\n", abfd->filename, abfd->xvec->name);
418 if (dump_ar_hdrs) print_arelt_descr (abfd, true);
419
420 if (dump_file_header) {
421 char *comma = "";
422
423 printf("architecture: %s, ",
424 bfd_printable_arch_mach (bfd_get_arch (abfd),
425 bfd_get_mach (abfd)));
426 printf("flags 0x%08x:\n", abfd->flags);
427
428 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
429 PF(HAS_RELOC, "HAS_RELOC");
430 PF(EXEC_P, "EXEC_P");
431 PF(HAS_LINENO, "HAS_LINENO");
432 PF(HAS_DEBUG, "HAS_DEBUG");
433 PF(HAS_SYMS, "HAS_SYMS");
434 PF(HAS_LOCALS, "HAS_LOCALS");
435 PF(DYNAMIC, "DYNAMIC");
436 PF(WP_TEXT, "WP_TEXT");
437 PF(D_PAGED, "D_PAGED");
438 printf("\nstart address 0x");
439 printf_vma(abfd->start_address);
440 }
441 printf("\n");
442
443 if (dump_section_headers)
444 dump_headers(abfd);
445 if (dump_symtab || dump_reloc_info || disassemble) {
446 syms = slurp_symtab(abfd);
447 }
448 if (dump_symtab) dump_symbols (abfd);
449 if (dump_reloc_info) dump_relocs(abfd);
450 if (dump_section_contents) dump_data (abfd);
451 if (disassemble) disassemble_data(abfd);
452 }
453
454 void
455 display_file (filename, target)
456 char *filename;
457 char *target;
458 {
459 bfd *file, *arfile = (bfd *) NULL;
460
461 file = bfd_openr (filename, target);
462 if (file == NULL) {
463 bfd_perror (filename);
464 return;
465 }
466
467 if (bfd_check_format (file, bfd_archive) == true) {
468 printf ("In archive %s:\n", bfd_get_filename (file));
469 for(;;) {
470 bfd_error = no_error;
471
472 arfile = bfd_openr_next_archived_file (file, arfile);
473 if (arfile == NULL) {
474 if (bfd_error != no_more_archived_files)
475 bfd_perror (bfd_get_filename(file));
476 return;
477 }
478
479 display_bfd (arfile);
480 /* Don't close the archive elements; we need them for next_archive */
481 }
482 }
483 else
484 display_bfd(file);
485
486 bfd_close(file);
487 }
488 \f
489 /* Actually display the various requested regions */
490
491
492
493
494
495
496
497
498
499
500 void
501 dump_data (abfd)
502 bfd *abfd;
503 {
504 asection *section;
505 bfd_byte *data = 0;
506 bfd_size_type datasize = 0;
507 bfd_size_type i;
508
509 for (section = abfd->sections; section != NULL; section =
510 section->next) {
511 int onaline = 16;
512
513 if (only == (char *)NULL ||
514 strcmp(only,section->name) == 0){
515
516
517
518 printf("Contents of section %s:\n", section->name);
519
520 if (bfd_get_section_size_before_reloc(section) == 0) continue;
521 data = (bfd_byte *)malloc(bfd_get_section_size_before_reloc(section));
522 if (data == (bfd_byte *)NULL) {
523 fprintf (stderr, "%s: memory exhausted.\n", program_name);
524 exit (1);
525 }
526 datasize = bfd_get_section_size_before_reloc(section);
527
528
529 bfd_get_section_contents (abfd, section, (PTR)data, 0, bfd_get_section_size_before_reloc(section));
530
531 for (i= 0; i < bfd_get_section_size_before_reloc(section); i += onaline) {
532 bfd_size_type j;
533 printf(" %04lx ", (unsigned long int)(i + section->vma));
534 for (j = i; j < i+ onaline; j++) {
535 if (j < bfd_get_section_size_before_reloc(section))
536 printf("%02x", (unsigned)(data[j]));
537 else
538 printf(" ");
539 if ((j & 3 ) == 3) printf(" ");
540 }
541
542 printf(" ");
543 for (j = i; j < i+onaline ; j++) {
544 if (j >= bfd_get_section_size_before_reloc(section))
545 printf(" ");
546 else
547 printf("%c", isprint(data[j]) ?data[j] : '.');
548 }
549 putchar ('\n');
550 }
551 }
552
553 free (data);
554 }
555 }
556
557
558
559 /* Should perhaps share code and display with nm? */
560 void
561 dump_symbols (abfd)
562 bfd *abfd;
563 {
564
565 unsigned int count;
566 asymbol **current = syms;
567 printf("SYMBOL TABLE:\n");
568
569 for (count = 0; count < symcount; count++) {
570
571 if (*current && (*current)->the_bfd) {
572 bfd_print_symbol((*current)->the_bfd,
573 stdout,
574 *current, bfd_print_symbol_all);
575
576 printf("\n");
577
578 }
579 current++;
580 }
581 printf("\n");
582 printf("\n");
583 }
584
585
586 void
587 dump_relocs(abfd)
588 bfd *abfd;
589 {
590 arelent **relpp;
591 unsigned int relcount;
592 asection *a;
593 for (a = abfd->sections; a != (asection *)NULL; a = a->next) {
594 if (a == &bfd_abs_section) continue;
595 if (a == &bfd_und_section) continue;
596 if (a == &bfd_com_section) continue;
597
598 printf("RELOCATION RECORDS FOR [%s]:",a->name);
599
600 if (bfd_get_reloc_upper_bound(abfd, a) == 0) {
601 printf(" (none)\n\n");
602 }
603 else {
604 arelent **p;
605
606 relpp = (arelent **) xmalloc( bfd_get_reloc_upper_bound(abfd,a) );
607 relcount = bfd_canonicalize_reloc(abfd,a,relpp, syms);
608 if (relcount == 0) {
609 printf(" (none)\n\n");
610 }
611 else {
612 printf("\n");
613 printf("OFFSET TYPE VALUE \n");
614
615 for (p =relpp; relcount && *p != (arelent *)NULL; p++,
616 relcount --) {
617 arelent *q = *p;
618 CONST char *sym_name;
619 /* CONST char *section_name = q->section == (asection *)NULL ? "*abs" :*/
620 /* q->section->name;*/
621 CONST char *section_name = (*( q->sym_ptr_ptr))->section->name;
622
623 if (q->sym_ptr_ptr && *q->sym_ptr_ptr) {
624 sym_name = (*(q->sym_ptr_ptr))->name ;
625 }
626 else {
627 sym_name = 0;
628 }
629 if (sym_name) {
630 printf_vma(q->address);
631 printf(" %-8s %s",
632 q->howto->name,
633 sym_name);
634 }
635 else {
636 printf_vma(q->address);
637 printf(" %-8s [%s]",
638 q->howto->name,
639 section_name);
640 }
641 if (q->addend) {
642 printf("+0x");
643 printf_vma(q->addend);
644 }
645 printf("\n");
646 }
647 printf("\n\n");
648 free(relpp);
649 }
650 }
651
652 }
653 }
654
655 static void
656 DEFUN_VOID(display_info)
657 {
658 unsigned int i, j;
659 extern bfd_target *target_vector[];
660
661 printf("BFD header file version %s\n", BFD_VERSION);
662 for (i = 0; target_vector[i] != (bfd_target *)NULL; i++)
663 {
664 bfd_target *p = target_vector[i];
665 bfd *abfd = bfd_openw("##dummy",p->name);
666 printf("%s\n (header %s, data %s)\n", p->name,
667 p->header_byteorder_big_p ? "big endian" : "little endian",
668 p->byteorder_big_p ? "big endian" : "little endian" );
669 {
670 for (j = (int)bfd_arch_obscure +1; j < (int)bfd_arch_last; j++)
671 {
672
673 if (bfd_set_arch_mach(abfd, (enum bfd_architecture)j, 0))
674 printf(" %s\n",
675 bfd_printable_arch_mach((enum bfd_architecture)j,0));
676
677 }
678
679 }
680 }
681 /* Again as a table */
682 printf("%12s"," ");
683 for (i = 0; target_vector[i]; i++) {
684 printf("%s ",target_vector[i]->name);
685 }
686 printf("\n");
687
688
689 for (j = (int)bfd_arch_obscure +1; (int)j <(int) bfd_arch_last; j++)
690 {
691 if (strcmp(bfd_printable_arch_mach(j,0),"UNKNOWN!") != 0) {
692 printf("%11s ", bfd_printable_arch_mach(j,0));
693 for (i = 0; target_vector[i]; i++) {
694 {
695 bfd_target *p = target_vector[i];
696 bfd *abfd = bfd_openw("##dummy",p->name);
697 int l = strlen(p->name);
698 int ok = bfd_set_arch_mach(abfd, j, 0);
699 if (ok) {
700 printf("%s ", p->name);
701 }
702 else {
703 while (l--) {
704 printf("%c",ok?'*':'-');
705 }
706 printf(" ");
707 }
708
709 }
710
711 }
712 printf("\n");
713 }
714 }
715 }
716 /** main and like trivia */
717 int
718 main (argc, argv)
719 int argc;
720 char **argv;
721 {
722 int c;
723 extern int optind;
724 extern char *optarg;
725 char *target = default_target;
726 boolean seenflag = false;
727 int ind = 0;
728
729 bfd_init();
730 program_name = *argv;
731
732 while ((c = getopt_long (argc, argv, "ib:m:dlfahrtxsj:", long_options, &ind))
733 != EOF) {
734 seenflag = true;
735 switch (c) {
736 case 'm':
737 machine = optarg;
738 break;
739 case 'j':
740 only = optarg;
741 break;
742 case 'l':
743 with_line_numbers = 1;
744 break;
745 case 'b':
746 target = optarg;
747 break;
748 case 'f':
749 dump_file_header = true;
750 break;
751 case 'i':
752 info = true;
753 break;
754 case 'x':
755 dump_symtab = 1;
756 dump_reloc_info = 1;
757 dump_file_header = true;
758 dump_ar_hdrs = 1;
759 dump_section_headers = 1;
760 break;
761 case 0 : break; /* we've been given a long option */
762 case 't': dump_symtab = 1; break;
763 case 'd': disassemble = true ; break;
764 case 's': dump_section_contents = 1; break;
765 case 'r': dump_reloc_info = 1; break;
766 case 'a': dump_ar_hdrs = 1; break;
767 case 'h': dump_section_headers = 1; break;
768 default:
769 usage ();
770 }
771 }
772
773 if (seenflag == false)
774 usage ();
775
776 if (info) {
777 display_info();
778 }
779 else {
780 if (optind == argc)
781 display_file ("a.out", target);
782 else
783 for (; optind < argc;)
784 display_file (argv[optind++], target);
785 }
786 return 0;
787 }
This page took 0.046529 seconds and 4 git commands to generate.