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