* symtab.c (lookup_method_type): Arguments were swapped in all
[deliverable/binutils-gdb.git] / binutils / objdump.c
CommitLineData
2fa0b342
DHW
1/*** objdump.c -- dump information about an object file. */
2
3/* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
4
5This file is part of BFD, the Binary File Diddler.
6
7BFD is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 1, or (at your option)
10any later version.
11
12BFD is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with BFD; see the file COPYING. If not, write to
19the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21/*
22 $Id$
2fa0b342
DHW
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
bf661056
SC
35
36
2fa0b342
DHW
37char *xmalloc();
38
39char *default_target = NULL; /* default at runtime */
40
41char *program_name = NULL;
42
43int dump_section_contents; /* -s */
44int dump_section_headers; /* -h */
45boolean dump_file_header; /* -f */
46int dump_symtab; /* -t */
47int dump_reloc_info; /* -r */
48int dump_ar_hdrs; /* -a */
49int with_line_numbers; /* -l */
50boolean disassemble; /* -d */
9872a49c 51boolean info; /* -i */
2fa0b342
DHW
52char *only;
53
54PROTO (void, display_file, (char *filename, char *target));
55PROTO (void, dump_data, (bfd *abfd));
56PROTO (void, dump_relocs, (bfd *abfd));
57PROTO (void, dump_symbols, (bfd *abfd));
58PROTO (void, print_arelt_descr, (bfd *abfd, boolean verbose));
59
60
61
62
63
64
65\f
66char *machine = (char *)NULL;
67 asymbol **syms;
68 asymbol **syms2;
69
70
71unsigned int storage;
72
73unsigned int symcount = 0;
74
75void
76usage ()
77{
78 fprintf (stderr,
9872a49c 79 "usage: %s [-ahifdrtxsl] [-m machine] [-j section_name] obj ...\n",
2fa0b342
DHW
80 program_name);
81 exit (1);
82}
83
84static 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
92static void
93dump_headers(abfd)
94bfd *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);
fc5d6074
SC
109 printf(" vma ");
110printf_vma(section->vma);
111printf(" align 2**%2u\n ",
2fa0b342
DHW
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
126static asymbol **
127slurp_symtab(abfd)
128bfd *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);
145return sy;
146}
147/* Sort symbols into value order */
148static int comp(ap,bp)
149asymbol **ap;
150asymbol **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 */
173void
174print_address(vma, stream)
175bfd_vma vma;
176FILE *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;
fc5d6074
SC
188 if (symcount == 0) {
189 fprintf_vma(stream, vma);
190 }
2fa0b342
DHW
191 else {
192 while (true) {
193 oldthisplace = thisplace;
194 thisplace = (max + min )/2 ;
195 if (thisplace == oldthisplace) break;
2fa0b342
DHW
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 {
fc5d6074
SC
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;
2fa0b342 217 /* Totally awesome! the exact right symbol */
fc5d6074
SC
218 fprintf_vma(stream, vma);
219 fprintf(stream," (%s)", syms[thisplace]->name);
2fa0b342
DHW
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
fc5d6074 234 fprintf_vma(stream, vma);
2fa0b342 235 if (syms[thisplace]->value > vma) {
fc5d6074
SC
236 fprintf(stream," (%s-)", syms[thisplace]->name);
237 fprintf_vma(stream, syms[thisplace]->value - vma);
2fa0b342
DHW
238
239 }
240 else {
fc5d6074
SC
241 fprintf(stream," (%s+)", syms[thisplace]->name);
242 fprintf_vma(stream, vma - syms[thisplace]->value);
243
244
2fa0b342
DHW
245 }
246 }
247}
248
249void
250disassemble_data(abfd)
251bfd *abfd;
252{
253 bfd_byte *data = NULL;
fc5d6074
SC
254 bfd_size_type datasize = 0;
255 bfd_size_type i;
2fa0b342
DHW
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 */
96d7950b 264 boolean done_dot = false;
2fa0b342
DHW
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 */
fc5d6074
SC
279 { unsigned int i;
280 for (i =0; i < symcount; i++) {
281 if (syms[i]->the_bfd == 0) {
282 symcount =i;
283 break;
284 }
2fa0b342
DHW
285 }
286 }
2fa0b342
DHW
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;
fc5d6074 340 while (i <section->size) {
96d7950b
SC
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;
2fa0b342 346 }
96d7950b 347 i+=4;
2fa0b342 348 }
96d7950b
SC
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(" ");
2fa0b342 371
96d7950b
SC
372 i += print(section->vma + i,
373 data + i,
374 stdout);
375 putchar ('\n') ;
376 }
2fa0b342
DHW
377 }
378
379
380
2fa0b342
DHW
381 free(data);
382 }
383 }
384}
385
386void
387display_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");
fc5d6074
SC
417 printf("\nstart address 0x");
418 printf_vma(abfd->start_address);
2fa0b342
DHW
419 }
420 printf("\n");
421
422 if (dump_section_headers)
423 dump_headers(abfd);
424 if (dump_symtab || dump_reloc_info || disassemble) {
425syms = 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
433void
434display_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
479void
480dump_data (abfd)
481 bfd *abfd;
482{
483 asection *section;
484 bfd_byte *data ;
fc5d6074
SC
485 bfd_size_type datasize = 0;
486 bfd_size_type i;
2fa0b342
DHW
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
fc5d6074 508 bfd_get_section_contents (abfd, section, (PTR)data, 0, section->size);
2fa0b342
DHW
509
510 for (i= 0; i < section->size; i += onaline) {
fc5d6074
SC
511 bfd_size_type j;
512 printf(" %04lx ", (unsigned long int)(i + section->vma));
2fa0b342
DHW
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? */
539void
540dump_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
563void
564dump_relocs(abfd)
565bfd *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
fc5d6074
SC
588 for (p =relpp; relcount && *p != (arelent *)NULL; p++,
589 relcount --) {
2fa0b342 590 arelent *q = *p;
bf661056
SC
591 CONST char *sym_name;
592 CONST char *section_name = q->section == (asection *)NULL ? "*abs" :
2fa0b342
DHW
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) {
fc5d6074
SC
601 printf_vma(q->address);
602 printf(" %-8s %s",
2fa0b342
DHW
603 q->howto->name,
604 sym_name);
605 }
606 else {
fc5d6074
SC
607 printf_vma(q->address);
608 printf(" %-8s [%s]",
2fa0b342
DHW
609 q->howto->name,
610 section_name);
611 }
612 if (q->addend) {
fc5d6074
SC
613 printf("+0x");
614 printf_vma(q->addend);
2fa0b342
DHW
615 }
616 printf("\n");
617 }
618 printf("\n\n");
619 free(relpp);
620 }
621 }
622
623 }
624}
625
9872a49c
SC
626static void
627DEFUN_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;
fc5d6074 644 for (j = (int)bfd_arch_obscure +1; j <(int) bfd_arch_last; j++)
9872a49c
SC
645 {
646 if (bfd_set_arch_mach(abfd, j, 0))
647 {
648 printf(" %s\n", bfd_printable_arch_mach(j,0));
649 }
650 }
2fa0b342 651
9872a49c
SC
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");
fc5d6074 661 for (j = (int)bfd_arch_obscure +1; (int)j <(int) bfd_arch_last; j++) {
9872a49c
SC
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}
2fa0b342
DHW
685/** main and like trivia */
686int
687main (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
7cffe88a 700 while ((c = getopt_long (argc, argv, "Aib:m:dlfahrtxsj:", long_options, &ind))
2fa0b342
DHW
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;
9872a49c
SC
719 case 'i':
720 info = true;
721 break;
2fa0b342
DHW
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;
7cffe88a
RP
729 case 'A':
730 disassemble = true;
731 dump_ar_hdrs = 1;
732 dump_file_header = true;
733 dump_reloc_info = 1;
734 dump_section_headers = 1;
735 dump_symtab = 1;
736 break;
737
2fa0b342
DHW
738 case 0 : break; /* we've been given a long option */
739 case 't': dump_symtab = 1; break;
740 case 'd': disassemble = true ; break;
741 case 's': dump_section_contents = 1; break;
742 case 'r': dump_reloc_info = 1; break;
743 case 'a': dump_ar_hdrs = 1; break;
744 case 'h': dump_section_headers = 1; break;
745 default:
746 usage ();
747 }
748 }
749
750 if (seenflag == false)
751 usage ();
752
9872a49c
SC
753 if (info) {
754 display_info();
755 }
756 else {
2fa0b342
DHW
757 if (optind == argc)
758 display_file ("a.out", target);
759 else
760 for (; optind < argc;)
761 display_file (argv[optind++], target);
9872a49c 762}
2fa0b342
DHW
763 return 0;
764}
This page took 0.06122 seconds and 4 git commands to generate.