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

Subversion Repositories plasma

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

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 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 133 rhoads
      if(elfProgram->p_vaddr < BUF_SIZE)
159
      {
160 136 rhoads
         //printf("[0x%x,0x%x,0x%x,0x%x,0x%x]\n", elfProgram->p_vaddr,
161
         //   elfProgram->p_offset, elfProgram->p_filesz, elfProgram->p_memsz,
162
         //   elfProgram->p_flags);
163 133 rhoads
         memcpy(code + elfProgram->p_vaddr, buf + elfProgram->p_offset,
164
                 elfProgram->p_filesz);
165 136 rhoads
         length = elfProgram->p_vaddr + elfProgram->p_filesz;
166
         //printf("length = %d 0x%x\n", length, length);
167 133 rhoads
      }
168 65 rhoads
   }
169
 
170 133 rhoads
   for(i = 0; i < elfHeader->e_shnum; ++i)
171
   {
172
      elfSection = (Elf32_Shdr *)(buf + elfHeader->e_shoff +
173
                         elfHeader->e_shentsize * i);
174
      elfSection->sh_name = ntohl(elfSection->sh_name);
175
      elfSection->sh_type = ntohl(elfSection->sh_type);
176
      elfSection->sh_addr = ntohl(elfSection->sh_addr);
177
      elfSection->sh_offset = ntohl(elfSection->sh_offset);
178
      elfSection->sh_size = ntohl(elfSection->sh_size);
179
 
180 136 rhoads
      if(elfSection->sh_type == SHT_MIPS_REGINFO)
181
      {
182
         elfRegInfo = (ELF_RegInfo*)(buf + elfSection->sh_offset);
183
         gp_ptr = ntohl(elfRegInfo->ri_gp_value);
184
      }
185 133 rhoads
      if(elfSection->sh_type == SHT_PROGBITS)
186
      {
187 136 rhoads
         //printf("elfSection->sh_addr=0x%x\n", elfSection->sh_addr);
188
         if(elfSection->sh_addr > gp_ptr_backup)
189
            gp_ptr_backup = elfSection->sh_addr;
190 27 rhoads
      }
191 133 rhoads
      if(elfSection->sh_type == SHT_NOBITS)
192
      {
193
         if(bss_start == 0)
194
         {
195
            bss_start = elfSection->sh_addr;
196 65 rhoads
         }
197 133 rhoads
         bss_end = elfSection->sh_addr + elfSection->sh_size;
198 65 rhoads
      }
199 27 rhoads
   }
200
 
201 136 rhoads
   if(length > bss_start - elfHeader->e_entry)
202
   {
203
      length = bss_start - elfHeader->e_entry;
204
   }
205 133 rhoads
   if(bss_start == length)
206
   {
207
      bss_start = length;
208
      bss_end = length + 4;
209 65 rhoads
   }
210 136 rhoads
   if(gp_ptr == 0)
211
      gp_ptr = gp_ptr_backup + 0x7ff0;
212 65 rhoads
 
213 200 rhoads
#if 0
214 133 rhoads
   /*Initialize the $gp register for sdata and sbss */
215
   printf("gp_ptr=0x%x ", gp_ptr);
216
   /*modify the first opcodes in boot.asm */
217
   /*modify the lui opcode */
218
   set_low(code, 0, gp_ptr >> 16);
219
   /*modify the ori opcode */
220
   set_low(code, 4, gp_ptr & 0xffff);
221 27 rhoads
 
222 133 rhoads
   /*Clear .sbss and .bss */
223 136 rhoads
   printf("sbss=0x%x bss_end=0x%x\nlength=0x%x ", bss_start, bss_end, length);
224 133 rhoads
   set_low(code, 8, bss_start >> 16);
225
   set_low(code, 12, bss_start & 0xffff);
226
   set_low(code, 16, bss_end >> 16);
227
   set_low(code, 20, bss_end & 0xffff);
228 27 rhoads
 
229 133 rhoads
   /*Set stack pointer */
230 136 rhoads
   if(elfHeader->e_entry < 0x10000000)
231
      stack_pointer = bss_end + 512;
232
   else
233
      stack_pointer = bss_end + 1024 * 4;
234
   stack_pointer &= ~7;
235
   printf("SP=0x%x\n", stack_pointer);
236 133 rhoads
   set_low(code, 24, stack_pointer >> 16);
237
   set_low(code, 28, stack_pointer & 0xffff);
238 200 rhoads
#endif
239 47 rhoads
 
240 136 rhoads
   /*write out test.bin */
241
   outfile = fopen("test.bin", "wb");
242 133 rhoads
   fwrite(code, length, 1, outfile);
243 65 rhoads
   fclose(outfile);
244
 
245 136 rhoads
   /*write out code.txt */
246 133 rhoads
   txtfile = fopen("code.txt", "w");
247
   for(i = 0; i <= length; i += 4)
248
   {
249 311 rhoads
      d = ntohl(*(uint32 *)(code + i));
250 133 rhoads
      fprintf(txtfile, "%8.8x\n", d);
251 2 rhoads
   }
252 26 rhoads
   fclose(txtfile);
253 2 rhoads
   free(buf);
254 266 rhoads
   printf("length=%d=0x%x\n", length, length);
255 27 rhoads
 
256 2 rhoads
   return 0;
257
}
258
 

powered by: WebSVN 2.1.0

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