3 Copyright (c) 2008 Daniel Mack <daniel@caiaq.de>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "wikireader.h"
35 u8 e_ident
[16]; /* ELF "magic number" */
36 u16 e_type
; /* Identifies object file type */
37 u16 e_machine
; /* Specifies required architecture */
38 u32 e_version
; /* Identifies object file version */
39 u32 e_entry
; /* Entry point virtual address */
40 u32 e_phoff
; /* Program header table file offset */
41 u32 e_shoff
; /* Section header table file offset */
42 u32 e_flags
; /* Processor-specific flags */
43 u16 e_ehsize
; /* ELF header size in bytes */
44 u16 e_phentsize
; /* Program header table entry size */
45 u16 e_phnum
; /* Program header table entry count */
46 u16 e_shentsize
; /* Section header table entry size */
47 u16 e_shnum
; /* Section header table entry count */
48 u16 e_shstrndx
; /* Section header string table index */
49 } __attribute__((packed
)) elf32_hdr
;
52 u32 sh_name
; /* Section name, index in string tbl */
53 u32 sh_type
; /* Type of section */
54 u32 sh_flags
; /* Miscellaneous section attributes */
55 u32 sh_addr
; /* Section virtual addr at execution */
56 u32 sh_offset
; /* Section file offset */
57 u32 sh_size
; /* Size of section in bytes */
58 u32 sh_link
; /* Index of another section */
59 u32 sh_info
; /* Additional section information */
60 u32 sh_addralign
; /* Section alignment */
61 u32 sh_entsize
; /* Entry size if section holds table */
62 } __attribute__((packed
)) elf32_sec
;
64 #define SHT_NULL 0 /* Section header table entry unused */
65 #define SHT_PROGBITS 1 /* Program specific (private) data */
66 #define SHT_SYMTAB 2 /* Link editing symbol table */
67 #define SHT_STRTAB 3 /* A string table */
68 #define SHT_RELA 4 /* Relocation entries with addends */
69 #define SHT_HASH 5 /* A symbol hash table */
70 #define SHT_DYNAMIC 6 /* Information for dynamic linking */
71 #define SHT_NOTE 7 /* Information that marks file */
72 #define SHT_NOBITS 8 /* Section occupies no space in file */
73 #define SHT_REL 9 /* Relocation entries, no addends */
74 #define SHT_SHLIB 10 /* Reserved, unspecified semantics */
75 #define SHT_DYNSYM 11 /* Dynamic linking symbol table */
78 int elf_exec(const u8
*filename
)
88 if (f_mount(0, &fatfs
) != FR_OK
) {
93 if (f_open(&file
, filename
, FA_READ
) != FR_OK
) {
98 if (f_read(&file
, &hdr
, sizeof(hdr
), &r
) || r
!= sizeof(hdr
)) {
103 if (hdr
.e_ident
[0] != ELFMAG0
||
104 hdr
.e_ident
[1] != ELFMAG1
||
105 hdr
.e_ident
[2] != ELFMAG2
||
106 hdr
.e_ident
[3] != ELFMAG3
) {
107 /* print("invalid ELF magic\n"); */
112 if (hdr
.e_type
!= ET_EXEC
) {
113 /* print("invalid ELF file type\n"); */
118 if (hdr
.e_machine
!= EM_C33
) {
119 print("FAIL: machine\n");
124 for (i
= 0; i
< hdr
.e_shnum
; i
++) {
125 f_lseek(&file
, hdr
.e_shoff
+ sizeof(sec
) * i
);
126 if (f_read(&file
, (u8
*) &sec
, sizeof(sec
), &r
) || r
!= sizeof(sec
))
129 switch (sec
.sh_type
) {
131 f_lseek(&file
, sec
.sh_offset
);
132 if (f_read(&file
, (u8
*) sec
.sh_addr
, sec
.sh_size
, &r
) || r
!= sec
.sh_size
) {
133 print("FAIL: load\n");
138 print_u32(sec
.sh_addr
);
143 /* clean .bss sections */
144 memset((u8
*) sec
.sh_addr
, 0, sec
.sh_size
);
153 print_u32(hdr
.e_entry
);
155 disable_card_power();
157 exec
= (void *) hdr
.e_entry
;
158 ((void (*) (void)) exec
) ();
161 // make sure every thing is cleaned up if the load fails fail
166 disable_card_power();