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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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