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