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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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