/[pcsx2_0.9.7]/branch/r3113_0.9.7_beta/fps2bios/kernel/eeload/eeelf.c
ViewVC logotype

Contents of /branch/r3113_0.9.7_beta/fps2bios/kernel/eeload/eeelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (show annotations) (download)
Tue Sep 7 03:29:01 2010 UTC (10 years, 10 months ago) by william
File MIME type: text/plain
File size: 9762 byte(s)
branching from upstream revision (http://pcsx2.googlecode.com/svn/trunk
): r3113 to
https://svn.netsolutions.dnsalias.com/websvn/ps2/pcsx2/pcsx2_0.9.7/branch/r3113_0.9.7_beta
1 #include "romdir.h"
2 #include "eedebug.h"
3
4 typedef struct {
5 u8 e_ident[16]; //0x7f,"ELF" (ELF file identifier)
6 u16 e_type; //ELF type: 0=NONE, 1=REL, 2=EXEC, 3=SHARED, 4=CORE
7 u16 e_machine; //Processor: 8=MIPS R3000
8 u32 e_version; //Version: 1=current
9 u32 e_entry; //Entry point address
10 u32 e_phoff; //Start of program headers (offset from file start)
11 u32 e_shoff; //Start of section headers (offset from file start)
12 u32 e_flags; //Processor specific flags = 0x20924001 noreorder, mips
13 u16 e_ehsize; //ELF header size (0x34 = 52 bytes)
14 u16 e_phentsize; //Program headers entry size
15 u16 e_phnum; //Number of program headers
16 u16 e_shentsize; //Section headers entry size
17 u16 e_shnum; //Number of section headers
18 u16 e_shstrndx; //Section header stringtable index
19 } ELF_HEADER;
20
21 typedef struct {
22 u32 p_type; //see notes1
23 u32 p_offset; //Offset from file start to program segment.
24 u32 p_vaddr; //Virtual address of the segment
25 u32 p_paddr; //Physical address of the segment
26 u32 p_filesz; //Number of bytes in the file image of the segment
27 u32 p_memsz; //Number of bytes in the memory image of the segment
28 u32 p_flags; //Flags for segment
29 u32 p_align; //Alignment. The address of 0x08 and 0x0C must fit this alignment. 0=no alignment
30 } ELF_PHR;
31
32 /*
33 notes1
34 ------
35 0=Inactive
36 1=Load the segment into memory, no. of bytes specified by 0x10 and 0x14
37 2=Dynamic linking
38 3=Interpreter. The array element must specify a path name
39 4=Note. The array element must specify the location and size of aux. info
40 5=reserved
41 6=The array element must specify location and size of the program header table.
42 */
43
44 typedef struct {
45 u32 sh_name; //No. to the index of the Section header stringtable index
46 u32 sh_type; //See notes2
47 u32 sh_flags; //see notes3
48 u32 sh_addr; //Section start address
49 u32 sh_offset; //Offset from start of file to section
50 u32 sh_size; //Size of section
51 u32 sh_link; //Section header table index link
52 u32 sh_info; //Info
53 u32 sh_addralign; //Alignment. The adress of 0x0C must fit this alignment. 0=no alignment.
54 u32 sh_entsize; //Fixed size entries.
55 } ELF_SHR;
56 /*
57 notes 2
58 -------
59 Type:
60 0=Inactive
61 1=PROGBITS
62 2=SYMTAB symbol table
63 3=STRTAB string table
64 4=RELA relocation entries
65 5=HASH hash table
66 6=DYNAMIC dynamic linking information
67 7=NOTE
68 8=NOBITS
69 9=REL relocation entries
70 10=SHLIB
71 0x70000000=LOPROC processor specifc
72 0x7fffffff=HIPROC
73 0x80000000=LOUSER lower bound
74 0xffffffff=HIUSER upper bound
75
76 notes 3
77 -------
78 Section Flags: (1 bit, you may combine them like 3 = alloc & write permission)
79 1=Write section contains data the is be writeable during execution.
80 2=Alloc section occupies memory during execution
81 4=Exec section contains executable instructions
82 0xf0000000=Mask bits processor-specific
83 */
84
85 typedef struct {
86 u32 st_name;
87 u32 st_value;
88 u32 st_size;
89 u8 st_info;
90 u8 st_other;
91 u16 st_shndx;
92 } Elf32_Sym;
93
94 #define ELF32_ST_TYPE(i) ((i)&0xf)
95
96 typedef struct {
97 u32 r_offset;
98 u32 r_info;
99 } Elf32_Rel;
100
101 char *sections_names;
102
103 ELF_HEADER *elfHeader;
104 ELF_PHR *elfProgH;
105 ELF_SHR *elfSectH;
106 u8 *elfdata;
107 int elfsize;
108
109
110 static void __memcpy(void *dest, const void *src, int n) {
111 const u8 *s = (u8*)src;
112 u8 *d = (u8*)dest;
113
114 while (n) {
115 *d++ = *s++; n--;
116 }
117 }
118
119
120 int loadHeaders() {
121 elfHeader = (ELF_HEADER*)elfdata;
122
123 if ((elfHeader->e_shentsize != sizeof(ELF_SHR)) && (elfHeader->e_shnum > 0)) {
124 return -1;
125 }
126
127 #ifdef ELF_LOG
128 ELF_LOG( "type: " );
129 #endif
130 switch( elfHeader->e_type )
131 {
132 default:
133 #ifdef ELF_LOG
134 ELF_LOG( "unknown %x", elfHeader->e_type );
135 #endif
136 break;
137
138 case 0x0:
139 #ifdef ELF_LOG
140 ELF_LOG( "no file type" );
141 #endif
142 break;
143
144 case 0x1:
145 #ifdef ELF_LOG
146 ELF_LOG( "relocatable" );
147 #endif
148 break;
149
150 case 0x2:
151 #ifdef ELF_LOG
152 ELF_LOG( "executable" );
153 #endif
154 break;
155 }
156 #ifdef ELF_LOG
157 ELF_LOG( "\n" );
158 ELF_LOG( "machine: " );
159 #endif
160 switch ( elfHeader->e_machine )
161 {
162 default:
163 #ifdef ELF_LOG
164 ELF_LOG( "unknown" );
165 #endif
166 break;
167
168 case 0x8:
169 #ifdef ELF_LOG
170 ELF_LOG( "mips_rs3000" );
171 #endif
172 break;
173 }
174 #ifdef ELF_LOG
175 ELF_LOG("\n");
176 ELF_LOG("version: %d\n",elfHeader->e_version);
177 ELF_LOG("entry: %08x\n",elfHeader->e_entry);
178 ELF_LOG("flags: %08x\n",elfHeader->e_flags);
179 ELF_LOG("eh size: %08x\n",elfHeader->e_ehsize);
180 ELF_LOG("ph off: %08x\n",elfHeader->e_phoff);
181 ELF_LOG("ph entsiz: %08x\n",elfHeader->e_phentsize);
182 ELF_LOG("ph num: %08x\n",elfHeader->e_phnum);
183 ELF_LOG("sh off: %08x\n",elfHeader->e_shoff);
184 ELF_LOG("sh entsiz: %08x\n",elfHeader->e_shentsize);
185 ELF_LOG("sh num: %08x\n",elfHeader->e_shnum);
186 ELF_LOG("sh strndx: %08x\n",elfHeader->e_shstrndx);
187
188 ELF_LOG("\n");
189 #endif
190
191 return 0;
192 }
193
194
195 int loadProgramHeaders() {
196 int i;
197
198 if (elfHeader->e_phnum == 0) {
199 return 0;
200 }
201
202 if (elfHeader->e_phentsize != sizeof(ELF_PHR)) {
203 return -1;
204 }
205
206 elfProgH = (ELF_PHR*)&elfdata[elfHeader->e_phoff];
207
208 for ( i = 0 ; i < elfHeader->e_phnum ; i++ )
209 {
210 #ifdef ELF_LOG
211 ELF_LOG( "Elf32 Program Header\n" );
212 ELF_LOG( "type: " );
213 #endif
214 switch ( elfProgH[ i ].p_type )
215 {
216 default:
217 #ifdef ELF_LOG
218 ELF_LOG( "unknown %x", (int)elfProgH[ i ].p_type );
219 #endif
220 break;
221
222 case 0x1:
223 #ifdef ELF_LOG
224 ELF_LOG("load");
225 #endif
226 /* if ( elfHeader->e_shnum == 0 ) {*/
227 if (elfProgH[ i ].p_offset < elfsize) {
228 int size;
229
230 if ((elfProgH[ i ].p_filesz + elfProgH[ i ].p_offset) > elfsize) {
231 size = elfsize - elfProgH[ i ].p_offset;
232 } else {
233 size = elfProgH[ i ].p_filesz;
234 }
235 // __printf("loading program to %x\n", elfProgH[ i ].p_paddr + elfbase);
236 __memcpy(elfProgH[ i ].p_paddr,
237 &elfdata[elfProgH[ i ].p_offset],
238 size);
239 }
240 #ifdef ELF_LOG
241 ELF_LOG("\t*LOADED*");
242 #endif
243 // }
244 break;
245 }
246 #ifdef ELF_LOG
247 ELF_LOG("\n");
248 ELF_LOG("offset: %08x\n",(int)elfProgH[i].p_offset);
249 ELF_LOG("vaddr: %08x\n",(int)elfProgH[i].p_vaddr);
250 ELF_LOG("paddr: %08x\n",elfProgH[i].p_paddr);
251 ELF_LOG("file size: %08x\n",elfProgH[i].p_filesz);
252 ELF_LOG("mem size: %08x\n",elfProgH[i].p_memsz);
253 ELF_LOG("flags: %08x\n",elfProgH[i].p_flags);
254 ELF_LOG("palign: %08x\n",elfProgH[i].p_align);
255 ELF_LOG("\n");
256 #endif
257 }
258
259 return 0;
260 }
261
262 int loadSectionHeaders() {
263 int i;
264 int i_st = -1;
265 int i_dt = -1;
266
267 if (elfHeader->e_shnum == 0) {
268 return -1;
269 }
270
271 elfSectH = (ELF_SHR*)&elfdata[elfHeader->e_shoff];
272
273 if ( elfHeader->e_shstrndx < elfHeader->e_shnum ) {
274 sections_names = (char *)&elfdata[elfSectH[ elfHeader->e_shstrndx ].sh_offset];
275 }
276
277 for ( i = 0 ; i < elfHeader->e_shnum ; i++ )
278 {
279 #ifdef ELF_LOG
280 ELF_LOG( "Elf32 Section Header [%x] %s", i, &sections_names[ elfSectH[ i ].sh_name ] );
281 #endif
282 /* if ( elfSectH[i].sh_flags & 0x2 ) {
283 if (elfSectH[i].sh_offset < elfsize) {
284 int size;
285
286 if ((elfSectH[i].sh_size + elfSectH[i].sh_offset) > elfsize) {
287 size = elfsize - elfSectH[i].sh_offset;
288 } else {
289 size = elfSectH[i].sh_size;
290 }
291 memcpy(&psM[ elfSectH[ i ].sh_addr &0x1ffffff ],
292 &elfdata[elfSectH[i].sh_offset],
293 size);
294 }
295 #ifdef ELF_LOG
296 ELF_LOG( "\t*LOADED*" );
297 #endif
298 }*/
299 #ifdef ELF_LOG
300 ELF_LOG("\n");
301 ELF_LOG("type: ");
302 #endif
303 switch ( elfSectH[ i ].sh_type )
304 {
305 default:
306 #ifdef ELF_LOG
307 ELF_LOG("unknown %08x",elfSectH[i].sh_type);
308 #endif
309 break;
310
311 case 0x0:
312 #ifdef ELF_LOG
313 ELF_LOG("null");
314 #endif
315 break;
316
317 case 0x1:
318 #ifdef ELF_LOG
319 ELF_LOG("progbits");
320 #endif
321 break;
322
323 case 0x2:
324 #ifdef ELF_LOG
325 ELF_LOG("symtab");
326 #endif
327 break;
328
329 case 0x3:
330 #ifdef ELF_LOG
331 ELF_LOG("strtab");
332 #endif
333 break;
334
335 case 0x4:
336 #ifdef ELF_LOG
337 ELF_LOG("rela");
338 #endif
339 break;
340
341 case 0x8:
342 #ifdef ELF_LOG
343 ELF_LOG("no bits");
344 #endif
345 break;
346
347 case 0x9:
348 #ifdef ELF_LOG
349 ELF_LOG("rel");
350 #endif
351 break;
352 }
353 #ifdef ELF_LOG
354 ELF_LOG("\n");
355 ELF_LOG("flags: %08x\n", elfSectH[i].sh_flags);
356 ELF_LOG("addr: %08x\n", elfSectH[i].sh_addr);
357 ELF_LOG("offset: %08x\n", elfSectH[i].sh_offset);
358 ELF_LOG("size: %08x\n", elfSectH[i].sh_size);
359 ELF_LOG("link: %08x\n", elfSectH[i].sh_link);
360 ELF_LOG("info: %08x\n", elfSectH[i].sh_info);
361 ELF_LOG("addralign: %08x\n", elfSectH[i].sh_addralign);
362 ELF_LOG("entsize: %08x\n", elfSectH[i].sh_entsize);
363 #endif
364 // dump symbol table
365
366 if (elfSectH[i].sh_type == 0x02) {
367 i_st = i; i_dt = elfSectH[i].sh_link;
368 }
369 /*
370 if (elfSectH[i].sh_type == 0x01) {
371 int size = elfSectH[i].sh_size / 4;
372 u32 *ptr = (u32*)&psxM[(elfSectH[i].sh_addr + irx_addr) & 0x1fffff];
373
374 while (size) {
375
376 if (*ptr == 0x41e00000) { // import func
377 int ret = iopSetImportFunc(ptr+1);
378 size-= ret; ptr+= ret;
379 }
380
381 if (*ptr == 0x41c00000) { // export func
382 int ret = iopSetExportFunc(ptr+1);
383 size-= ret; ptr+= ret;
384 }
385
386 size--; ptr++;
387 }
388 }
389 */
390 /* if (!strcmp(".data", &sections_names[elfSectH[i].sh_name])) {
391 // seems so..
392
393 psxRegs.GPR.n.gp = 0x8000 + irx_addr + elfSectH[i].sh_addr;
394 }*/
395 }
396
397 return 0;
398 }
399
400
401 u32 loadElfFile(char *filename, struct elfinfo *info) {
402 struct rominfo ri;
403 char str[256];
404 char str2[256];
405 int i;
406
407 __printf("loadElfFile: %s\n", filename);
408
409 if (romdirGetFile(filename, &ri) == NULL) {
410 __printf("file %s not found!!\n", filename);
411 return -1;
412 }
413 elfdata = (u8*)(0xbfc00000 + ri.fileOffset);
414 elfsize = ri.fileSize;
415
416 loadHeaders();
417 loadProgramHeaders();
418 loadSectionHeaders();
419
420 // __printf("loadElfFile: e_entry=%x\n", elfHeader->e_entry);
421 return elfHeader->e_entry;
422 }
423

  ViewVC Help
Powered by ViewVC 1.1.22