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