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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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