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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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