When disassembling, now prints ... instead of pages of whatever the
[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 boolean done_dot = false;
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 (data[i] ==0 && data[i+1] == 0 && data[i+2] == 0 &&
342 data[i+3] == 0) {
343 if (done_dot == false) {
344 printf("...\n");
345 done_dot=true;
346 }
347 i+=4;
348 }
349 else {
350 done_dot = false;
351 if (with_line_numbers) {
352 static prevline;
353 CONST char *filename;
354 CONST char *functionname;
355 unsigned int line;
356 bfd_find_nearest_line(abfd,
357 section,
358 syms,
359 section->vma + i,
360 &filename,
361 &functionname,
362 &line);
363
364 if (filename && functionname && line && line != prevline) {
365 printf("%s:%u\n", filename, line);
366 prevline = line;
367 }
368 }
369 print_address(section->vma + i, stdout);
370 printf(" ");
371
372 i += print(section->vma + i,
373 data + i,
374 stdout);
375 putchar ('\n') ;
376 }
377 }
378
379
380
381 free(data);
382 }
383 }
384 }
385
386 void
387 display_bfd (abfd)
388 bfd *abfd;
389 {
390
391 if (!bfd_check_format (abfd, bfd_object)) {
392 fprintf (stderr,"%s: %s not an object file\n", program_name,
393 abfd->filename);
394 return;
395 }
396 printf ("\n%s: file format %s\n", abfd->filename, abfd->xvec->name);
397 if (dump_ar_hdrs) print_arelt_descr (abfd, true);
398
399 if (dump_file_header) {
400 char *comma = "";
401
402 printf("architecture: %s, ",
403 bfd_printable_arch_mach (bfd_get_architecture (abfd),
404 bfd_get_machine (abfd)));
405 printf("flags 0x%08x:\n", abfd->flags);
406
407 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
408 PF(HAS_RELOC, "HAS_RELOC");
409 PF(EXEC_P, "EXEC_P");
410 PF(HAS_LINENO, "HAS_LINENO");
411 PF(HAS_DEBUG, "HAS_DEBUG");
412 PF(HAS_SYMS, "HAS_SYMS");
413 PF(HAS_LOCALS, "HAS_LOCALS");
414 PF(DYNAMIC, "DYNAMIC");
415 PF(WP_TEXT, "WP_TEXT");
416 PF(D_PAGED, "D_PAGED");
417 printf("\nstart address 0x");
418 printf_vma(abfd->start_address);
419 }
420 printf("\n");
421
422 if (dump_section_headers)
423 dump_headers(abfd);
424 if (dump_symtab || dump_reloc_info || disassemble) {
425 syms = slurp_symtab(abfd);
426 }
427 if (dump_symtab) dump_symbols (abfd);
428 if (dump_reloc_info) dump_relocs(abfd);
429 if (dump_section_contents) dump_data (abfd);
430 if (disassemble) disassemble_data(abfd);
431 }
432
433 void
434 display_file (filename, target)
435 char *filename;
436 char *target;
437 {
438 bfd *file, *arfile = (bfd *) NULL;
439
440 file = bfd_openr (filename, target);
441 if (file == NULL) {
442 bfd_perror (filename);
443 return;
444 }
445
446 if (bfd_check_format (file, bfd_archive) == true) {
447 printf ("In archive %s:\n", bfd_get_filename (file));
448 for(;;) {
449 bfd_error = no_error;
450
451 arfile = bfd_openr_next_archived_file (file, arfile);
452 if (arfile == NULL) {
453 if (bfd_error != no_more_archived_files)
454 bfd_perror (bfd_get_filename(file));
455 return;
456 }
457
458 display_bfd (arfile);
459 /* Don't close the archive elements; we need them for next_archive */
460 }
461 }
462 else
463 display_bfd(file);
464
465 bfd_close(file);
466 }
467 \f
468 /* Actually display the various requested regions */
469
470
471
472
473
474
475
476
477
478
479 void
480 dump_data (abfd)
481 bfd *abfd;
482 {
483 asection *section;
484 bfd_byte *data ;
485 bfd_size_type datasize = 0;
486 bfd_size_type i;
487
488 for (section = abfd->sections; section != NULL; section =
489 section->next) {
490 int onaline = 16;
491
492 if (only == (char *)NULL ||
493 strcmp(only,section->name) == 0){
494
495
496
497 printf("Contents of section %s:\n", section->name);
498
499 if (section->size == 0) continue;
500 data = (bfd_byte *)malloc(section->size);
501 if (data == (bfd_byte *)NULL) {
502 fprintf (stderr, "%s: memory exhausted.\n", program_name);
503 exit (1);
504 }
505 datasize = section->size;
506
507
508 bfd_get_section_contents (abfd, section, (PTR)data, 0, section->size);
509
510 for (i= 0; i < section->size; i += onaline) {
511 bfd_size_type j;
512 printf(" %04lx ", (unsigned long int)(i + section->vma));
513 for (j = i; j < i+ onaline; j++) {
514 if (j < section->size)
515 printf("%02x", (unsigned)(data[j]));
516 else
517 printf(" ");
518 if ((j & 3 ) == 3) printf(" ");
519 }
520
521 printf(" ");
522 for (j = i; j < i+onaline ; j++) {
523 if (j >= section->size)
524 printf(" ");
525 else
526 printf("%c", isprint(data[j]) ?data[j] : '.');
527 }
528 putchar ('\n');
529 }
530 }
531
532 free (data);
533 }
534 }
535
536
537
538 /* Should perhaps share code and display with nm? */
539 void
540 dump_symbols (abfd)
541 bfd *abfd;
542 {
543
544 unsigned int count;
545 asymbol **current = syms;
546 printf("SYMBOL TABLE:\n");
547
548 for (count = 0; count < symcount; count++) {
549 if ((*current)->the_bfd) {
550 bfd_print_symbol((*current)->the_bfd,
551 stdout,
552 *current, bfd_print_symbol_all_enum);
553
554 printf("\n");
555 }
556 current++;
557 }
558 printf("\n");
559 printf("\n");
560 }
561
562
563 void
564 dump_relocs(abfd)
565 bfd *abfd;
566 {
567 arelent **relpp;
568 unsigned int relcount;
569 asection *a;
570 for (a = abfd->sections; a != (asection *)NULL; a = a->next) {
571 printf("RELOCATION RECORDS FOR [%s]:",a->name);
572
573 if (get_reloc_upper_bound(abfd, a) == 0) {
574 printf(" (none)\n\n");
575 }
576 else {
577 arelent **p;
578
579 relpp = (arelent **) xmalloc( get_reloc_upper_bound(abfd,a) );
580 relcount = bfd_canonicalize_reloc(abfd,a,relpp, syms);
581 if (relcount == 0) {
582 printf(" (none)\n\n");
583 }
584 else {
585 printf("\n");
586 printf("OFFSET TYPE VALUE \n");
587
588 for (p =relpp; relcount && *p != (arelent *)NULL; p++,
589 relcount --) {
590 arelent *q = *p;
591 CONST char *sym_name;
592 CONST char *section_name = q->section == (asection *)NULL ? "*abs" :
593 q->section->name;
594 if (q->sym_ptr_ptr && *q->sym_ptr_ptr) {
595 sym_name = (*(q->sym_ptr_ptr))->name ;
596 }
597 else {
598 sym_name = 0;
599 }
600 if (sym_name) {
601 printf_vma(q->address);
602 printf(" %-8s %s",
603 q->howto->name,
604 sym_name);
605 }
606 else {
607 printf_vma(q->address);
608 printf(" %-8s [%s]",
609 q->howto->name,
610 section_name);
611 }
612 if (q->addend) {
613 printf("+0x");
614 printf_vma(q->addend);
615 }
616 printf("\n");
617 }
618 printf("\n\n");
619 free(relpp);
620 }
621 }
622
623 }
624 }
625
626 static void
627 DEFUN_VOID(display_info)
628 {
629 unsigned int i;
630 extern bfd_target *target_vector[];
631
632 enum bfd_architecture j;
633 i = 0;
634 printf("BFD header file version %s\n", BFD_VERSION);
635 while (target_vector[i] != (bfd_target *)NULL)
636 {
637 bfd_target *p = target_vector[i];
638 bfd *abfd = bfd_openw("##dummy",p->name);
639 printf("%s\n (header %s, data %s)\n", p->name,
640 p->header_byteorder_big_p ? "big endian" : "little endian",
641 p->byteorder_big_p ? "big endian" : "little endian" );
642 {
643 enum bfd_architecture j;
644 for (j = (int)bfd_arch_obscure +1; j <(int) bfd_arch_last; j++)
645 {
646 if (bfd_set_arch_mach(abfd, j, 0))
647 {
648 printf(" %s\n", bfd_printable_arch_mach(j,0));
649 }
650 }
651
652 }
653 i++;
654 }
655 /* Again as a table */
656 printf("%12s"," ");
657 for (i = 0; target_vector[i]; i++) {
658 printf("%s ",target_vector[i]->name);
659 }
660 printf("\n");
661 for (j = (int)bfd_arch_obscure +1; (int)j <(int) bfd_arch_last; j++) {
662 printf("%11s ", bfd_printable_arch_mach(j,0));
663 for (i = 0; target_vector[i]; i++) {
664 {
665 bfd_target *p = target_vector[i];
666 bfd *abfd = bfd_openw("##dummy",p->name);
667 int l = strlen(p->name);
668 int ok = bfd_set_arch_mach(abfd, j, 0);
669 if (ok) {
670 printf("%s ", p->name);
671 }
672 else {
673 while (l--) {
674 printf("%c",ok?'*':'-');
675 }
676 printf(" ");
677 }
678
679 }
680 }
681
682 printf("\n");
683 }
684 }
685 /** main and like trivia */
686 int
687 main (argc, argv)
688 int argc;
689 char **argv;
690 {
691 int c;
692 extern int optind;
693 extern char *optarg;
694 char *target = default_target;
695 boolean seenflag = false;
696 int ind = 0;
697
698 program_name = *argv;
699
700 while ((c = getopt_long (argc, argv, "ib:m:dlfahrtxsj:", long_options, &ind))
701 != EOF) {
702 seenflag = true;
703 switch (c) {
704 case 'm':
705 machine = optarg;
706 break;
707 case 'j':
708 only = optarg;
709 break;
710 case 'l':
711 with_line_numbers = 1;
712 break;
713 case 'b':
714 target = optarg;
715 break;
716 case 'f':
717 dump_file_header = true;
718 break;
719 case 'i':
720 info = true;
721 break;
722 case 'x':
723 dump_symtab = 1;
724 dump_reloc_info = 1;
725 dump_file_header = true;
726 dump_ar_hdrs = 1;
727 dump_section_headers = 1;
728 break;
729 case 0 : break; /* we've been given a long option */
730 case 't': dump_symtab = 1; break;
731 case 'd': disassemble = true ; break;
732 case 's': dump_section_contents = 1; break;
733 case 'r': dump_reloc_info = 1; break;
734 case 'a': dump_ar_hdrs = 1; break;
735 case 'h': dump_section_headers = 1; break;
736 default:
737 usage ();
738 }
739 }
740
741 if (seenflag == false)
742 usage ();
743
744 if (info) {
745 display_info();
746 }
747 else {
748 if (optind == argc)
749 display_file ("a.out", target);
750 else
751 for (; optind < argc;)
752 display_file (argv[optind++], target);
753 }
754 return 0;
755 }
This page took 0.045636 seconds and 5 git commands to generate.