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