OpenCores
URL https://opencores.org/ocsvn/plasma/plasma/trunk

Subversion Repositories plasma

[/] [plasma/] [trunk/] [tools/] [convert.c] - Blame information for rev 67

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 rhoads
//convert.c by Steve Rhoads 4/26/01
2 67 rhoads
//Now uses the ELF format (get gccmips_elf.zip) 
3 27 rhoads
//set $gp and zero .sbss and .bss
4 2 rhoads
#include <stdio.h>
5
#include <stdlib.h>
6 65 rhoads
#include <string.h>
7 2 rhoads
 
8
#define BUF_SIZE (1024*1024)
9 34 rhoads
/*Assumes running on PC little endian*/
10 65 rhoads
#define ntohl(A) (((A)>>24)|(((A)&0x00ff0000)>>8)|(((A)&0xff00)<<8)|((A)<<24))
11
#define ntohs(A) ((((A)&0xff00)>>8)|((A)<<8))
12 2 rhoads
 
13 65 rhoads
#define EI_NIDENT 16
14
#define SHT_PROGBITS 1
15
#define SHT_STRTAB 3
16
#define SHT_NOBITS 8
17 27 rhoads
 
18 65 rhoads
typedef struct {
19
   unsigned char  e_ident[EI_NIDENT];
20
   unsigned short e_e_type;
21
   unsigned short e_machine;
22
   unsigned long  e_version;
23
   unsigned long  e_entry;
24
   unsigned long  e_phoff;
25
   unsigned long  e_shoff;
26
   unsigned long  e_flags;
27
   unsigned short e_ehsize;
28
   unsigned short e_phentsize;
29
   unsigned short e_phnum;
30
   unsigned short e_shentsize;
31
   unsigned short e_shnum;
32
   unsigned short e_shstrndx;
33
} ElfHeader;
34 27 rhoads
 
35 65 rhoads
typedef struct {
36
   unsigned long p_type;
37
   unsigned long p_offset;
38
   unsigned long p_vaddr;
39
   unsigned long p_paddr;
40
   unsigned long p_filesz;
41
   unsigned long p_memsz;
42
   unsigned long p_flags;
43
   unsigned long p_align;
44
} Elf32_Phdr;
45
 
46
typedef struct {
47
   unsigned long sh_name;
48
   unsigned long sh_type;
49
   unsigned long sh_flags;
50
   unsigned long sh_addr;
51
   unsigned long sh_offset;
52
   unsigned long sh_size;
53
   unsigned long sh_link;
54
   unsigned long sh_info;
55
   unsigned long sh_addralign;
56
   unsigned long sh_entsize;
57
} Elf32_Shdr;
58
 
59
#if 0
60
unsigned long load(unsigned char *ptr,unsigned long address)
61 27 rhoads
{
62
   unsigned long value;
63
   value=*(unsigned long*)(ptr+address);
64
   value=ntohl(value);
65
   return value;
66
}
67
 
68 65 rhoads
unsigned short load_short(unsigned char *ptr,unsigned long address)
69
{
70
   return (ptr[address]<<8)+ptr[address+1];
71
}
72
#endif
73
 
74 27 rhoads
void set_low(char *ptr,unsigned long address,unsigned long value)
75
{
76
   unsigned long opcode;
77
   opcode=*(unsigned long*)(ptr+address);
78
   opcode=ntohl(opcode);
79
   opcode=(opcode&0xffff0000)|(value&0xffff);
80
   opcode=ntohl(opcode);
81
   *(unsigned long*)(ptr+address)=opcode;
82
}
83
 
84 2 rhoads
int main(int argc,char *argv[])
85
{
86 26 rhoads
   FILE *infile,*outfile,*txtfile;
87 27 rhoads
   unsigned char *buf,*code;
88 47 rhoads
   long size,stack_pointer;
89 65 rhoads
   unsigned long length,d,i,gp_ptr=0;
90
   unsigned long bss_start=0,bss_end=0;
91 27 rhoads
 
92 65 rhoads
   ElfHeader *elfHeader;
93
   Elf32_Phdr *elfProgram;
94
   Elf32_Shdr *elfSection;
95
 
96 27 rhoads
   printf("test.exe -> code.txt & test2.exe\n");
97 2 rhoads
   infile=fopen("test.exe","rb");
98
   if(infile==NULL) {
99
      printf("Can't open test.exe");
100
      return 0;
101
   }
102
   buf=(unsigned char*)malloc(BUF_SIZE);
103
   size=fread(buf,1,BUF_SIZE,infile);
104
   fclose(infile);
105 65 rhoads
   code=(unsigned char*)malloc(BUF_SIZE);
106
   memset(code,0,BUF_SIZE);
107 27 rhoads
 
108 65 rhoads
   elfHeader=(ElfHeader*)buf;
109
   if(strncmp(elfHeader->e_ident+1,"ELF",3)) {
110
      printf("Error:  Not an ELF file!\n");
111
      printf("Use the gccmips_elf.zip from opencores/projects/plasma!\n");
112
      return -1;
113
   }
114 34 rhoads
 
115 65 rhoads
   elfHeader->e_entry=ntohl(elfHeader->e_entry);
116
   elfHeader->e_phoff=ntohl(elfHeader->e_phoff);
117
   elfHeader->e_shoff=ntohl(elfHeader->e_shoff);
118
   elfHeader->e_phentsize=ntohs(elfHeader->e_phentsize);
119
   elfHeader->e_phnum=ntohs(elfHeader->e_phnum);
120
   elfHeader->e_shentsize=ntohs(elfHeader->e_shentsize);
121
   elfHeader->e_shnum=ntohs(elfHeader->e_shnum);
122
   length=0;
123
 
124
   for(i=0;i<elfHeader->e_phnum;++i) {
125
      elfProgram=(Elf32_Phdr*)(buf+elfHeader->e_phoff+elfHeader->e_phentsize*i);
126
      elfProgram->p_offset=ntohl(elfProgram->p_offset);
127
      elfProgram->p_vaddr=ntohl(elfProgram->p_vaddr);
128
      elfProgram->p_filesz=ntohl(elfProgram->p_filesz);
129
      elfProgram->p_memsz=ntohl(elfProgram->p_memsz);
130
//      printf("[0x%x,0x%x,0x%x]\n",elfProgram->p_vaddr,elfProgram->p_offset,elfProgram->p_filesz);
131
      memcpy(code+elfProgram->p_vaddr,buf+elfProgram->p_offset,elfProgram->p_filesz);
132
      length=elfProgram->p_vaddr+elfProgram->p_memsz;
133
   }
134
 
135
   for(i=0;i<elfHeader->e_shnum;++i) {
136
      elfSection=(Elf32_Shdr*)(buf+elfHeader->e_shoff+elfHeader->e_shentsize*i);
137
      elfSection->sh_name=ntohl(elfSection->sh_name);
138
      elfSection->sh_type=ntohl(elfSection->sh_type);
139
      elfSection->sh_addr=ntohl(elfSection->sh_addr);
140
      elfSection->sh_offset=ntohl(elfSection->sh_offset);
141
      elfSection->sh_size=ntohl(elfSection->sh_size);
142
#if 0
143
      printf("{0x%x,0x%x:0x%x,0x%x,0x%x}\n",
144
         elfSection->sh_name,elfSection->sh_type,elfSection->sh_addr,
145
         elfSection->sh_offset,elfSection->sh_size);
146
#endif
147
#if 0
148
      if(elfSection->sh_type==SHT_PROGBITS||elfSection->sh_type==SHT_STRTAB) {
149
//         memcpy(code+elfSection->sh_addr,buf+elfSection->sh_offset,elfSection->sh_size);
150
         length=elfSection->sh_addr+elfSection->sh_size;
151
         bss_start=length;
152 27 rhoads
      }
153 65 rhoads
#endif
154
      if(elfSection->sh_type==SHT_PROGBITS) {
155
         gp_ptr=elfSection->sh_addr;
156
      }
157
      if(elfSection->sh_type==SHT_NOBITS) {
158
         if(bss_start==0) {
159
            bss_start=elfSection->sh_addr;
160
         }
161
         bss_end=elfSection->sh_addr+elfSection->sh_size;
162
      }
163 27 rhoads
   }
164
 
165 65 rhoads
   if(bss_start==length) {
166
      bss_start=length;
167
      bss_end=length+4;
168
   }
169
 
170 27 rhoads
   /*Initialize the $gp register for sdata and sbss*/
171 65 rhoads
   gp_ptr+=0x7ff0;
172 34 rhoads
   printf("gp_ptr=0x%x ",gp_ptr);
173 27 rhoads
   /*modify the first opcodes in boot.asm*/
174
   /*modify the lui opcode*/
175
   set_low(code,0,gp_ptr>>16);
176
   /*modify the ori opcode*/
177
   set_low(code,4,gp_ptr&0xffff);
178
 
179
   /*Clear .sbss and .bss*/
180 65 rhoads
   printf(".sbss=0x%x .bss_end=0x%x\n",bss_start,bss_end);
181
   set_low(code,8,bss_start);
182
   set_low(code,12,bss_end);
183 27 rhoads
 
184 47 rhoads
   /*Set stack pointer*/
185 65 rhoads
   stack_pointer=bss_end+512;
186 47 rhoads
   printf("Stack pointer=0x%x\n",stack_pointer);
187
   set_low(code,16,stack_pointer);
188
 
189 27 rhoads
   /*write out code.txt*/
190 26 rhoads
   outfile=fopen("test2.exe","wb");
191 65 rhoads
   fwrite(code,length,1,outfile);
192
   fclose(outfile);
193
 
194 26 rhoads
   txtfile=fopen("code.txt","w");
195 65 rhoads
   for(i=0;i<=length;i+=4) {
196
      d=ntohl(*(unsigned long*)(code+i));
197 26 rhoads
      fprintf(txtfile,"%8.8x\n",d);
198 2 rhoads
   }
199 26 rhoads
   fclose(txtfile);
200 2 rhoads
   free(buf);
201 27 rhoads
 
202 2 rhoads
   return 0;
203
}
204
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.