1 /*** objdump.c -- dump information about an object file. */
3 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Diddler.
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)
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.
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. */
25 * Until there is other documentation, refer to the manual page dump(1) in
26 * the system 5 program's reference manual
39 char *default_target = NULL; /* default at runtime */
41 char *program_name = NULL;
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 */
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));
66 char *machine = (char *)NULL;
73 unsigned int symcount = 0;
79 "usage: %s [-ahifdrtxsl] [-m machine] [-j section_name] obj ...\n",
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},
97 for (section = abfd->sections;
98 section != (asection *) NULL;
99 section = section->next)
103 if (section->flags & x) { printf("%s%s",comma,y); comma = ", "; }
106 printf("SECTION %d [%s]\t: size %08x",
109 (unsigned) section->size);
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");
120 PF(SEC_RELOC,"RELOC");
121 PF(SEC_BALIGN,"BALIGN");
122 PF(SEC_READONLY,"READONLY");
136 if (!(bfd_get_file_flags (abfd) & HAS_SYMS)) {
137 (void) printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
141 storage = get_symtab_upper_bound (abfd);
143 sy = (asymbol **) malloc (storage);
145 fprintf (stderr, "%s: out of memory.\n", program_name);
149 symcount = bfd_canonicalize_symtab (abfd, sy);
152 /* Sort symbols into value order */
153 static int comp(ap,bp)
161 if ( a->name== (char *)NULL || (a->flags &( BSF_DEBUGGING| BSF_UNDEFINED) ))
163 if ( b->name== (char *)NULL || (b->flags &( BSF_DEBUGGING|BSF_UNDEFINED)))
166 diff = a->the_bfd - b->the_bfd;
170 diff = a->value - b->value;
174 return a->section - b->section;
177 /* Print the supplied address symbolically if possible */
179 print_address(vma, stream)
183 /* Perform a binary search looking for the closest symbol to
184 the required value */
186 unsigned int min = 0;
187 unsigned int max = symcount;
189 unsigned int thisplace = 1;
190 unsigned int oldthisplace ;
194 fprintf_vma(stream, vma);
198 oldthisplace = thisplace;
199 thisplace = (max + min )/2 ;
200 if (thisplace == oldthisplace) break;
201 vardiff = syms[thisplace]->value - vma;
212 /* Totally awesome! the exact right symbol */
213 CONST char *match_name = syms[thisplace]->name;
214 int sym_len = strlen(match_name);
215 /* Avoid "filename.o" as a match */
217 && match_name[sym_len - 2] == '.'
218 && match_name[sym_len - 1] == 'o'
219 && thisplace + 1 < symcount
220 && syms[thisplace+1]->value == vma)
221 match_name = syms[thisplace+1]->name;
222 /* Totally awesome! the exact right symbol */
223 fprintf_vma(stream, vma);
224 fprintf(stream," (%s)", syms[thisplace]->name);
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 */
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 */
239 fprintf_vma(stream, vma);
240 if (syms[thisplace]->value > vma) {
241 fprintf(stream," (%s-)", syms[thisplace]->name);
242 fprintf_vma(stream, syms[thisplace]->value - vma);
246 fprintf(stream," (%s+)", syms[thisplace]->name);
247 fprintf_vma(stream, vma - syms[thisplace]->value);
255 disassemble_data(abfd)
258 bfd_byte *data = NULL;
259 bfd_arch_info_type *info ;
260 bfd_size_type datasize = 0;
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();
268 enum bfd_architecture a;
271 /* Replace symbol section relative values with abs values */
272 boolean done_dot = false;
274 for (i = 0; i < symcount; i++) {
275 if (syms[i]->section != (asection *)NULL) {
276 syms[i]->value += syms[i]->section->vma;
280 /* We keep a copy of the symbols in the original order */
281 syms2 = slurp_symtab(abfd);
283 /* Sort the symbols into section and symbol order */
284 (void) qsort(syms, symcount, sizeof(asymbol *), comp);
286 /* Find the first useless symbol */
288 for (i =0; i < symcount; i++) {
289 if (syms[i]->the_bfd == 0) {
299 if (machine!= (char *)NULL) {
300 info = bfd_scan_arch(machine);
302 fprintf(stderr,"%s: Can't use supplied machine %s\n",
307 abfd->arch_info = info;
310 /* See if we can disassemble using bfd */
312 if(abfd->arch_info->disassemble) {
313 print = abfd->arch_info->disassemble;
316 a = bfd_get_arch(abfd);
319 print = print_insn_sparc;
322 print = print_insn_m68k;
325 print = print_insn_a29k;
328 print = print_insn_i960;
331 fprintf(stderr,"%s: Can't disassemble for architecture %s\n",
333 bfd_printable_arch_mach(bfd_get_arch(abfd),0));
339 for (section = abfd->sections;
340 section != (asection *)NULL;
341 section = section->next) {
343 if (only == (char *)NULL || strcmp(only,section->name) == 0){
344 printf("Disassembly of section %s:\n", section->name);
346 if (section->size == 0) continue;
348 data = (bfd_byte *)malloc(section->size);
350 if (data == (bfd_byte *)NULL) {
351 fprintf (stderr, "%s: memory exhausted.\n", program_name);
354 datasize = section->size;
357 bfd_get_section_contents (abfd, section, data, 0, section->size);
360 while (i <section->size) {
361 if (data[i] ==0 && data[i+1] == 0 && data[i+2] == 0 &&
363 if (done_dot == false) {
371 if (with_line_numbers) {
373 CONST char *filename;
374 CONST char *functionname;
376 bfd_find_nearest_line(abfd,
384 if (filename && functionname && line && line != prevline) {
385 printf("%s:%u\n", filename, line);
389 print_address(section->vma + i, stdout);
392 i += print(section->vma + i,
411 if (!bfd_check_format (abfd, bfd_object)) {
412 fprintf (stderr,"%s: %s not an object file\n", program_name,
416 printf ("\n%s: file format %s\n", abfd->filename, abfd->xvec->name);
417 if (dump_ar_hdrs) print_arelt_descr (abfd, true);
419 if (dump_file_header) {
422 printf("architecture: %s, ",
423 bfd_printable_arch_mach (bfd_get_arch (abfd),
424 bfd_get_mach (abfd)));
425 printf("flags 0x%08x:\n", abfd->flags);
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");
437 printf("\nstart address 0x");
438 printf_vma(abfd->start_address);
442 if (dump_section_headers)
444 if (dump_symtab || dump_reloc_info || disassemble) {
445 syms = slurp_symtab(abfd);
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);
454 display_file (filename, target)
458 bfd *file, *arfile = (bfd *) NULL;
460 file = bfd_openr (filename, target);
462 bfd_perror (filename);
466 if (bfd_check_format (file, bfd_archive) == true) {
467 printf ("In archive %s:\n", bfd_get_filename (file));
469 bfd_error = no_error;
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));
478 display_bfd (arfile);
479 /* Don't close the archive elements; we need them for next_archive */
488 /* Actually display the various requested regions */
505 bfd_size_type datasize = 0;
508 for (section = abfd->sections; section != NULL; section =
512 if (only == (char *)NULL ||
513 strcmp(only,section->name) == 0){
517 printf("Contents of section %s:\n", section->name);
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);
525 datasize = section->size;
528 bfd_get_section_contents (abfd, section, (PTR)data, 0, section->size);
530 for (i= 0; i < section->size; i += onaline) {
532 printf(" %04lx ", (unsigned long int)(i + section->vma));
533 for (j = i; j < i+ onaline; j++) {
534 if (j < section->size)
535 printf("%02x", (unsigned)(data[j]));
538 if ((j & 3 ) == 3) printf(" ");
542 for (j = i; j < i+onaline ; j++) {
543 if (j >= section->size)
546 printf("%c", isprint(data[j]) ?data[j] : '.');
558 /* Should perhaps share code and display with nm? */
565 asymbol **current = syms;
566 printf("SYMBOL TABLE:\n");
568 for (count = 0; count < symcount; count++) {
570 if (*current && (*current)->the_bfd) {
571 bfd_print_symbol((*current)->the_bfd,
573 *current, bfd_print_symbol_all);
590 unsigned int relcount;
592 for (a = abfd->sections; a != (asection *)NULL; a = a->next) {
593 printf("RELOCATION RECORDS FOR [%s]:",a->name);
595 if (get_reloc_upper_bound(abfd, a) == 0) {
596 printf(" (none)\n\n");
601 relpp = (arelent **) xmalloc( get_reloc_upper_bound(abfd,a) );
602 relcount = bfd_canonicalize_reloc(abfd,a,relpp, syms);
604 printf(" (none)\n\n");
608 printf("OFFSET TYPE VALUE \n");
610 for (p =relpp; relcount && *p != (arelent *)NULL; p++,
613 CONST char *sym_name;
614 CONST char *section_name = q->section == (asection *)NULL ? "*abs" :
616 if (q->sym_ptr_ptr && *q->sym_ptr_ptr) {
617 sym_name = (*(q->sym_ptr_ptr))->name ;
623 printf_vma(q->address);
629 printf_vma(q->address);
636 printf_vma(q->addend);
649 DEFUN_VOID(display_info)
652 extern bfd_target *target_vector[];
654 printf("BFD header file version %s\n", BFD_VERSION);
655 for (i = 0; target_vector[i] != (bfd_target *)NULL; i++)
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" );
663 for (j = (int)bfd_arch_obscure +1; j < (int)bfd_arch_last; j++)
666 if (bfd_set_arch_mach(abfd, (enum bfd_architecture)j, 0))
668 bfd_printable_arch_mach((enum bfd_architecture)j,0));
674 /* Again as a table */
676 for (i = 0; target_vector[i]; i++) {
677 printf("%s ",target_vector[i]->name);
682 for (j = (int)bfd_arch_obscure +1; (int)j <(int) bfd_arch_last; j++)
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++) {
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);
693 printf("%s ", p->name);
697 printf("%c",ok?'*':'-');
709 /** main and like trivia */
718 char *target = default_target;
719 boolean seenflag = false;
723 program_name = *argv;
725 while ((c = getopt_long (argc, argv, "ib:m:dlfahrtxsj:", long_options, &ind))
736 with_line_numbers = 1;
742 dump_file_header = true;
750 dump_file_header = true;
752 dump_section_headers = 1;
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;
766 if (seenflag == false)
774 display_file ("a.out", target);
776 for (; optind < argc;)
777 display_file (argv[optind++], target);