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