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