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

Subversion Repositories plasma

[/] [plasma/] [tags/] [V3_0/] [tools/] [convert.c] - Blame information for rev 352

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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