Subversion Repositories freemyipod

Rev

Rev 449 | Rev 466 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 449 Rev 450
Line 1... Line 1...
1
/*
1
/*
-
 
2
 * elf2emcoreapp.c: Convert ELF (or any BFD format) to emCORE executable format
-
 
3
 *
-
 
4
 * (c) 2011, Michael Sparmann <theseven@gmx.net>
-
 
5
 *
-
 
6
 * Based on:
2
 * elf2flt.c: Convert ELF (or any BFD format) to FLAT binary format
7
 * elf2flt.c: Convert ELF (or any BFD format) to FLAT binary format
3
 *
8
 *
4
 * (c) 1999-2002, Greg Ungerer <gerg@snapgear.com>
9
 * (c) 1999-2002, Greg Ungerer <gerg@snapgear.com>
5
 * Created elf2flt from coff2flt (see copyrights below). Added all the
10
 * Created elf2flt from coff2flt (see copyrights below). Added all the
6
 * ELF format file handling. Extended relocation support for all of
11
 * ELF format file handling. Extended relocation support for all of
Line 71... Line 76...
71
 
76
 
72
#if defined(__MINGW32__)
77
#if defined(__MINGW32__)
73
#include <getopt.h>
78
#include <getopt.h>
74
#endif
79
#endif
75
 
80
 
76
/* from uClinux-x.x.x/include/linux */
81
#define _TOOL
77
#include "flat.h"     /* Binary flat header description                      */
82
#include "../../emcore/trunk/execimage.h"
78
#include "compress.h"
83
#include "compress.h"
79
 
84
 
80
#ifdef TARGET_e1
85
#ifdef TARGET_e1
81
#include <e1.h>
86
#include <e1.h>
82
#endif
87
#endif
Line 109... Line 114...
109
#define ARCH	"nios2"
114
#define ARCH	"nios2"
110
#else
115
#else
111
#error "Don't know how to support your CPU architecture??"
116
#error "Don't know how to support your CPU architecture??"
112
#endif
117
#endif
113
 
118
 
-
 
119
#ifndef R_ARM_V4BX
-
 
120
#define R_ARM_V4BX 40
-
 
121
#endif
-
 
122
#ifndef R_ARM_JUMP24
-
 
123
#define R_ARM_JUMP24 29
-
 
124
#endif
-
 
125
 
114
#if defined(TARGET_m68k) || defined(TARGET_h8300) || defined(TARGET_bfin)
126
#if defined(TARGET_m68k) || defined(TARGET_h8300) || defined(TARGET_bfin)
115
/*
127
/*
116
 * Define a maximum number of bytes allowed in the offset table.
128
 * Define a maximum number of bytes allowed in the offset table.
117
 * We'll fail if the table is larger than this.
129
 * We'll fail if the table is larger than this.
118
 *
130
 *
Line 782... Line 794...
782
#endif
794
#endif
783
 
795
 
784
#if defined(TARGET_arm)
796
#if defined(TARGET_arm)
785
				case R_ARM_ABS32:
797
				case R_ARM_ABS32:
786
					relocation_needed = 1;
798
					relocation_needed = 1;
-
 
799
					sym_vma = bfd_section_vma(abs_bfd, sym_section);
-
 
800
					sym_addr += sym_vma + q->addend;
787
					if (verbose)
801
					if (verbose)
788
						fprintf(stderr,
802
						fprintf(stderr,
789
							"%s vma=0x%x, value=0x%x, address=0x%x "
803
							"%s vma=0x%x, value=0x%x, address=0x%x "
790
							"sym_addr=0x%x rs=0x%x, opcode=0x%x\n",
804
							"sym_addr=0x%x rs=0x%x, opcode=0x%x\n",
791
							"ABS32",
805
							"ABS32",
792
							sym_vma, (*(q->sym_ptr_ptr))->value,
806
							sym_vma, (*(q->sym_ptr_ptr))->value,
793
							q->address, sym_addr,
807
							q->address, sym_addr,
794
							(*p)->howto->rightshift,
808
							(*p)->howto->rightshift,
795
							*(uint32_t *)r_mem);
809
							*(uint32_t *)r_mem);
796
					sym_vma = bfd_section_vma(abs_bfd, sym_section);
-
 
797
					sym_addr += sym_vma + q->addend;
-
 
798
					break;
810
					break;
799
				case R_ARM_GOT32:
811
				case R_ARM_GOT32:
800
				case R_ARM_GOTPC:
812
				case R_ARM_GOTPC:
801
                case R_ARM_V4BX:
813
                case R_ARM_V4BX:
802
					/* Should be fine as is */
814
					/* Should be fine as is */
Line 805... Line 817...
805
					if (verbose)
817
					if (verbose)
806
						fprintf(stderr,
818
						fprintf(stderr,
807
							"%s vma=0x%x, value=0x%x, address=0x%x "
819
							"%s vma=0x%x, value=0x%x, address=0x%x "
808
							"sym_addr=0x%x rs=0x%x, opcode=0x%x\n",
820
							"sym_addr=0x%x rs=0x%x, opcode=0x%x\n",
809
							"PLT32",
821
							"PLT32",
810
							sym_vma, (*(q->sym_ptr_ptr))->value,
822
							0, (*(q->sym_ptr_ptr))->value,
811
							q->address, sym_addr,
823
							q->address, (sym_addr-q->address)>>(*p)->howto->rightshift,
812
							(*p)->howto->rightshift,
824
							(*p)->howto->rightshift,
813
							*(uint32_t *)r_mem);
825
							*(uint32_t *)r_mem);
814
				case R_ARM_PC24:
826
				case R_ARM_PC24:
815
                case R_ARM_JUMP24:
827
                case R_ARM_JUMP24:
816
					sym_vma = 0;
828
					sym_vma = 0;
Line 1583... Line 1595...
1583
	"       -z              : compress code/data/relocs\n"
1595
	"       -z              : compress code/data/relocs\n"
1584
	"       -d              : compress data/relocs\n"
1596
	"       -d              : compress data/relocs\n"
1585
	"       -a              : use existing symbol references\n"
1597
	"       -a              : use existing symbol references\n"
1586
	"                         instead of recalculating from\n"
1598
	"                         instead of recalculating from\n"
1587
	"                         relocation info\n"
1599
	"                         relocation info\n"
1588
        "       -R reloc-file   : read relocations from a separate file\n"
1600
    "       -R reloc-file   : read relocations from a separate file\n"
1589
	"       -p abs-pic-file : GOT/PIC processing with files\n"
1601
	"       -p abs-pic-file : GOT/PIC processing with files\n"
1590
	"       -s stacksize    : set application stack size\n"
1602
	"       -s stacksize    : set application stack size\n"
1591
	"       -o output-file  : output file name\n\n",
1603
	"       -o output-file  : output file name\n\n",
1592
	elf2flt_progname);
1604
	elf2flt_progname);
1593
	fprintf(stderr, "Compiled for " ARCH " architecture\n\n");
1605
	fprintf(stderr, "Compiled for " ARCH " architecture\n\n");
Line 1641... Line 1653...
1641
 
1653
 
1642
  void *text;
1654
  void *text;
1643
  void *data;
1655
  void *data;
1644
  uint32_t *reloc;
1656
  uint32_t *reloc;
1645
 
1657
 
1646
  struct flat_hdr hdr;
1658
  struct emcoreapp_header hdr;
1647
 
1659
 
1648
  elf2flt_progname = argv[0];
1660
  elf2flt_progname = argv[0];
1649
  xmalloc_set_program_name(elf2flt_progname);
1661
  xmalloc_set_program_name(elf2flt_progname);
1650
 
1662
 
1651
  if (argc < 2)
1663
  if (argc < 2)
1652
  	usage();
1664
  	usage();
1653
 
1665
 
1654
  if (sizeof(hdr) != 64)
-
 
1655
    fatal(
-
 
1656
	    "Potential flat header incompatibility detected\n"
-
 
1657
	    "header size should be 64 but is %d",
-
 
1658
	    sizeof(hdr));
-
 
1659
 
-
 
1660
#ifndef TARGET_e1
1666
#ifndef TARGET_e1
1661
  stack = 4096;
1667
  stack = 4096;
1662
#else /* We need plenty of stack for both of them (Aggregate and Register) */
1668
#else /* We need plenty of stack for both of them (Aggregate and Register) */
1663
  stack = 0x2020;
1669
  stack = 0x2020;
1664
#endif
1670
#endif
Line 1856... Line 1862...
1856
    printf("No relocations in code!\n");
1862
    printf("No relocations in code!\n");
1857
 
1863
 
1858
  text_offs = real_address_bits(text_vma);
1864
  text_offs = real_address_bits(text_vma);
1859
 
1865
 
1860
  /* Fill in the binflt_flat header */
1866
  /* Fill in the binflt_flat header */
1861
  memcpy(hdr.magic,"bFLT",4);
1867
  memcpy(hdr.signature, "emCOexec", 8);
1862
  hdr.rev         = FLAT_VERSION;
1868
  hdr.version = EMCOREAPP_HEADER_VERSION;
1863
  hdr.entry       = sizeof(hdr) + bfd_get_start_address(abs_bfd);
-
 
1864
  hdr.data_start  = sizeof(hdr) + text_offs + text_len;
1869
  hdr.textstart = sizeof(hdr);
1865
  hdr.data_end    = sizeof(hdr) + text_offs + text_len +data_len;
1870
  hdr.textsize = text_len + data_len;
1866
  hdr.bss_end     = sizeof(hdr) + text_offs + text_len +data_len+bss_len;
1871
  hdr.bsssize = bss_len;
1867
  hdr.stack_size  = stack; /* FIXME */
1872
  hdr.stacksize = stack;
-
 
1873
  hdr.entrypoint = bfd_get_start_address(abs_bfd);
1868
  hdr.reloc_start = sizeof(hdr) + text_offs + text_len +data_len;
1874
  hdr.relocstart = sizeof(hdr) + text_len + data_len;
1869
  hdr.reloc_count = reloc_len;
1875
  hdr.reloccount = reloc_len;
1870
  hdr.flags       = 0
-
 
1871
	  | (load_to_ram || text_has_relocs ? FLAT_FLAG_RAM : 0)
-
 
1872
	  | (ktrace ? FLAT_FLAG_KTRACE : 0)
-
 
1873
	  | (pic_with_got ? FLAT_FLAG_GOTPIC : 0)
-
 
1874
	  | (docompress ? (docompress == 2 ? FLAT_FLAG_GZDATA : FLAT_FLAG_GZIP) : 0);
1876
  hdr.flags = docompress ? EMCOREAPP_FLAG_COMPRESSED : 0;
1875
  hdr.build_date = (uint32_t)time(NULL);
1877
  hdr.creationtime = (uint32_t)time(NULL);
1876
  memset(hdr.filler, 0x00, sizeof(hdr.filler));
-
 
1877
 
1878
 
1878
  for (i=0; i<reloc_len; i++) reloc[i] = reloc[i];
1879
  for (i=0; i<reloc_len; i++) reloc[i] = reloc[i];
1879
 
1880
 
1880
  if (verbose) {
1881
  if (verbose) {
1881
    printf("SIZE: .text=0x%04x, .data=0x%04x, .bss=0x%04x",
1882
    printf("SIZE: .text=0x%04x, .data=0x%04x, .bss=0x%04x",
Line 1884... Line 1885...
1884
      printf(", relocs=0x%04x", reloc_len);
1885
      printf(", relocs=0x%04x", reloc_len);
1885
    printf("\n");
1886
    printf("\n");
1886
  }
1887
  }
1887
  
1888
  
1888
  if (!ofile) {
1889
  if (!ofile) {
1889
    ofile = xmalloc(strlen(fname) + 5 + 1); /* 5 to add suffix */
1890
    ofile = xmalloc(strlen(fname) + 10 + 1); /* 10 to add suffix */
1890
    strcpy(ofile, fname);
1891
    strcpy(ofile, fname);
1891
    strcat(ofile, ".bflt");
1892
    strcat(ofile, ".emcoreapp");
1892
  }
1893
  }
1893
 
1894
 
1894
  if ((fd = open (ofile, O_WRONLY|O_BINARY|O_CREAT|O_TRUNC, 0744)) < 0)
1895
  if ((fd = open (ofile, O_WRONLY|O_BINARY|O_CREAT|O_TRUNC, 0744)) < 0)
1895
    fatal_perror("Can't open output file %s", ofile);
1896
    fatal_perror("Can't open output file %s", ofile);
1896
 
1897