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

Subversion Repositories mlite

[/] [mlite/] [trunk/] [tools/] [convert.c] - Blame information for rev 309

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 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 309 rhoads
   (void)stack_pointer;
103 65 rhoads
 
104 191 rhoads
   printf("test.axf -> code.txt & test.bin\n");
105
   infile = fopen("test.axf", "rb");
106 133 rhoads
   if(infile == NULL)
107
   {
108 191 rhoads
      printf("Can't open test.axf");
109 2 rhoads
      return 0;
110
   }
111 133 rhoads
   buf = (unsigned char *)malloc(BUF_SIZE);
112
   size = (int)fread(buf, 1, BUF_SIZE, infile);
113 2 rhoads
   fclose(infile);
114 133 rhoads
   code = (unsigned char *)malloc(BUF_SIZE);
115
   memset(code, 0, BUF_SIZE);
116 27 rhoads
 
117 133 rhoads
   elfHeader = (ElfHeader *)buf;
118 136 rhoads
   if(strncmp((char*)elfHeader->e_ident + 1, "ELF", 3))
119 133 rhoads
   {
120 65 rhoads
      printf("Error:  Not an ELF file!\n");
121
      printf("Use the gccmips_elf.zip from opencores/projects/plasma!\n");
122
      return -1;
123
   }
124 34 rhoads
 
125 133 rhoads
   elfHeader->e_entry = ntohl(elfHeader->e_entry);
126
   elfHeader->e_phoff = ntohl(elfHeader->e_phoff);
127
   elfHeader->e_shoff = ntohl(elfHeader->e_shoff);
128
   elfHeader->e_flags = ntohl(elfHeader->e_flags);
129
   elfHeader->e_phentsize = ntohs(elfHeader->e_phentsize);
130
   elfHeader->e_phnum = ntohs(elfHeader->e_phnum);
131
   elfHeader->e_shentsize = ntohs(elfHeader->e_shentsize);
132
   elfHeader->e_shnum = ntohs(elfHeader->e_shnum);
133 136 rhoads
   printf("Entry=0x%x ", elfHeader->e_entry);
134 133 rhoads
   length = 0;
135 65 rhoads
 
136 133 rhoads
   for(i = 0; i < elfHeader->e_phnum; ++i)
137
   {
138
      elfProgram = (Elf32_Phdr *)(buf + elfHeader->e_phoff +
139
                         elfHeader->e_phentsize * i);
140
      elfProgram->p_type = ntohl(elfProgram->p_type);
141
      elfProgram->p_offset = ntohl(elfProgram->p_offset);
142
      elfProgram->p_vaddr = ntohl(elfProgram->p_vaddr);
143
      elfProgram->p_filesz = ntohl(elfProgram->p_filesz);
144
      elfProgram->p_memsz = ntohl(elfProgram->p_memsz);
145
      elfProgram->p_flags = ntohl(elfProgram->p_flags);
146 136 rhoads
 
147
      elfProgram->p_vaddr -= elfHeader->e_entry;
148
 
149
      if(elfProgram->p_type == PT_MIPS_REGINFO)
150
      {
151
         elfRegInfo = (ELF_RegInfo*)(buf + elfProgram->p_offset);
152
         gp_ptr = ntohl(elfRegInfo->ri_gp_value);
153
      }
154 133 rhoads
      if(elfProgram->p_vaddr < BUF_SIZE)
155
      {
156 136 rhoads
         //printf("[0x%x,0x%x,0x%x,0x%x,0x%x]\n", elfProgram->p_vaddr,
157
         //   elfProgram->p_offset, elfProgram->p_filesz, elfProgram->p_memsz,
158
         //   elfProgram->p_flags);
159 133 rhoads
         memcpy(code + elfProgram->p_vaddr, buf + elfProgram->p_offset,
160
                 elfProgram->p_filesz);
161 136 rhoads
         length = elfProgram->p_vaddr + elfProgram->p_filesz;
162
         //printf("length = %d 0x%x\n", length, length);
163 133 rhoads
      }
164 65 rhoads
   }
165
 
166 133 rhoads
   for(i = 0; i < elfHeader->e_shnum; ++i)
167
   {
168
      elfSection = (Elf32_Shdr *)(buf + elfHeader->e_shoff +
169
                         elfHeader->e_shentsize * i);
170
      elfSection->sh_name = ntohl(elfSection->sh_name);
171
      elfSection->sh_type = ntohl(elfSection->sh_type);
172
      elfSection->sh_addr = ntohl(elfSection->sh_addr);
173
      elfSection->sh_offset = ntohl(elfSection->sh_offset);
174
      elfSection->sh_size = ntohl(elfSection->sh_size);
175
 
176 136 rhoads
      if(elfSection->sh_type == SHT_MIPS_REGINFO)
177
      {
178
         elfRegInfo = (ELF_RegInfo*)(buf + elfSection->sh_offset);
179
         gp_ptr = ntohl(elfRegInfo->ri_gp_value);
180
      }
181 133 rhoads
      if(elfSection->sh_type == SHT_PROGBITS)
182
      {
183 136 rhoads
         //printf("elfSection->sh_addr=0x%x\n", elfSection->sh_addr);
184
         if(elfSection->sh_addr > gp_ptr_backup)
185
            gp_ptr_backup = elfSection->sh_addr;
186 27 rhoads
      }
187 133 rhoads
      if(elfSection->sh_type == SHT_NOBITS)
188
      {
189
         if(bss_start == 0)
190
         {
191
            bss_start = elfSection->sh_addr;
192 65 rhoads
         }
193 133 rhoads
         bss_end = elfSection->sh_addr + elfSection->sh_size;
194 65 rhoads
      }
195 27 rhoads
   }
196
 
197 136 rhoads
   if(length > bss_start - elfHeader->e_entry)
198
   {
199
      length = bss_start - elfHeader->e_entry;
200
   }
201 133 rhoads
   if(bss_start == length)
202
   {
203
      bss_start = length;
204
      bss_end = length + 4;
205 65 rhoads
   }
206 136 rhoads
   if(gp_ptr == 0)
207
      gp_ptr = gp_ptr_backup + 0x7ff0;
208 65 rhoads
 
209 200 rhoads
#if 0
210 133 rhoads
   /*Initialize the $gp register for sdata and sbss */
211
   printf("gp_ptr=0x%x ", gp_ptr);
212
   /*modify the first opcodes in boot.asm */
213
   /*modify the lui opcode */
214
   set_low(code, 0, gp_ptr >> 16);
215
   /*modify the ori opcode */
216
   set_low(code, 4, gp_ptr & 0xffff);
217 27 rhoads
 
218 133 rhoads
   /*Clear .sbss and .bss */
219 136 rhoads
   printf("sbss=0x%x bss_end=0x%x\nlength=0x%x ", bss_start, bss_end, length);
220 133 rhoads
   set_low(code, 8, bss_start >> 16);
221
   set_low(code, 12, bss_start & 0xffff);
222
   set_low(code, 16, bss_end >> 16);
223
   set_low(code, 20, bss_end & 0xffff);
224 27 rhoads
 
225 133 rhoads
   /*Set stack pointer */
226 136 rhoads
   if(elfHeader->e_entry < 0x10000000)
227
      stack_pointer = bss_end + 512;
228
   else
229
      stack_pointer = bss_end + 1024 * 4;
230
   stack_pointer &= ~7;
231
   printf("SP=0x%x\n", stack_pointer);
232 133 rhoads
   set_low(code, 24, stack_pointer >> 16);
233
   set_low(code, 28, stack_pointer & 0xffff);
234 200 rhoads
#endif
235 47 rhoads
 
236 136 rhoads
   /*write out test.bin */
237
   outfile = fopen("test.bin", "wb");
238 133 rhoads
   fwrite(code, length, 1, outfile);
239 65 rhoads
   fclose(outfile);
240
 
241 136 rhoads
   /*write out code.txt */
242 133 rhoads
   txtfile = fopen("code.txt", "w");
243
   for(i = 0; i <= length; i += 4)
244
   {
245
      d = ntohl(*(unsigned long *)(code + i));
246
      fprintf(txtfile, "%8.8x\n", d);
247 2 rhoads
   }
248 26 rhoads
   fclose(txtfile);
249 2 rhoads
   free(buf);
250 266 rhoads
   printf("length=%d=0x%x\n", length, length);
251 27 rhoads
 
252 2 rhoads
   return 0;
253
}
254
 

powered by: WebSVN 2.1.0

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