3 * - By John Hodge (thePowersGang)
\r
6 * - ELF Executable Loader Code
\r
12 // ---- Import linking code from userland
\r
14 #define SysDebug(v...) LOG(v)
\r
16 # define DISABLE_ELF64
\r
18 static int GetSymbol(const char *Name, void **Value, size_t *Size);
\r
19 static int GetSymbol(const char *Name, void **Value, size_t *Size) {
\r
21 if(!Binary_GetSymbol(Name, &val)) {
\r
22 Log_Notice("ELF", "Lookup of '%s' failed", Name);
\r
27 *Value = (void*)val;
\r
30 #define AddLoaded(a,b) do{}while(0)
\r
31 #define LoadLibrary(a,b,c) (Log_Debug("ELF", "Module requested lib '%s'",a),0)
\r
32 #define _SysSetMemFlags(ad,f,m) do{}while(0)
\r
33 #include "../../../Usermode/Libraries/ld-acess.so_src/elf.c"
\r
36 #define DEBUG_WARN 1
\r
38 // === PROTOTYPES ===
\r
39 tBinary *Elf_Load(int fp);
\r
40 tBinary *Elf_Load64(int fp, Elf64_Ehdr *hdr);
\r
41 tBinary *Elf_Load32(int fp, Elf32_Ehdr *hdr);
\r
42 int Elf_Relocate(void *Base);
\r
43 int Elf_GetSymbol(void *Base, const char *Name, Uint *Ret);
\r
44 Uint Elf_Int_HashString(const char *str);
\r
47 tBinaryType gELF_Info = {
\r
49 0x464C457F, 0xFFFFFFFF, // '\x7FELF'
\r
51 Elf_Load, Elf_Relocate, Elf_GetSymbol
\r
55 tBinary *Elf_Load(int fp)
\r
60 VFS_Read(fp, sizeof(hdr), &hdr);
\r
62 // Check the file type
\r
63 if(hdr.e_ident[0] != 0x7F || hdr.e_ident[1] != 'E' || hdr.e_ident[2] != 'L' || hdr.e_ident[3] != 'F') {
\r
64 Log_Warning("ELF", "Non-ELF File was passed to the ELF loader");
\r
68 switch(hdr.e_ident[4]) // EI_CLASS
\r
71 return Elf_Load32(fp, (void*)&hdr);
\r
73 return Elf_Load64(fp, &hdr);
\r
75 Log_Warning("ELF", "Unknown EI_CLASS value %i", hdr.e_ident[4]);
\r
80 tBinary *Elf_Load64(int FD, Elf64_Ehdr *Header)
\r
83 Elf64_Phdr phtab[Header->e_phnum];
\r
88 if( Header->e_phoff == 0 )
\r
90 Log_Warning("ELF", "No program header, panic!");
\r
93 if( Header->e_shentsize != sizeof(Elf64_Shdr) ) {
\r
94 Log_Warning("ELF", "Header gives shentsize as %i, my type is %i",
\r
95 Header->e_shentsize, sizeof(Elf64_Shdr) );
\r
97 if( Header->e_phentsize != sizeof(Elf64_Phdr) ) {
\r
98 Log_Warning("ELF", "Header gives phentsize as %i, my type is %i",
\r
99 Header->e_phentsize, sizeof(Elf64_Phdr) );
\r
103 LOG(" e_ident = %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
\r
104 Header->e_ident[0], Header->e_ident[1], Header->e_ident[2], Header->e_ident[3],
\r
105 Header->e_ident[4], Header->e_ident[5], Header->e_ident[6], Header->e_ident[7],
\r
106 Header->e_ident[8], Header->e_ident[9], Header->e_ident[10], Header->e_ident[11],
\r
107 Header->e_ident[12], Header->e_ident[13], Header->e_ident[14], Header->e_ident[15]
\r
109 LOG(" e_type = %i", Header->e_type);
\r
110 LOG(" e_machine = %i", Header->e_machine);
\r
111 LOG(" e_version = %i", Header->e_version);
\r
112 LOG(" e_entry = 0x%llx", Header->e_entry);
\r
113 LOG(" e_phoff = 0x%llx", Header->e_phoff);
\r
114 LOG(" e_shoff = 0x%llx", Header->e_shoff);
\r
115 LOG(" e_flags = 0x%x", Header->e_flags);
\r
116 LOG(" e_ehsize = %i", Header->e_ehsize);
\r
117 LOG(" e_phentsize = %i", Header->e_phentsize);
\r
118 LOG(" e_phnum = %i", Header->e_phnum);
\r
119 LOG(" e_shentsize = %i", Header->e_shentsize);
\r
120 LOG(" e_shnum = %i", Header->e_shnum);
\r
121 LOG(" e_shstrndx = %i", Header->e_shstrndx);
\r
124 // Load Program Header table
\r
125 VFS_Seek(FD, Header->e_phoff, SEEK_SET);
\r
126 VFS_Read(FD, sizeof(Elf64_Phdr)*Header->e_phnum, phtab);
\r
128 // Count load segments
\r
130 for( i = 0; i < Header->e_phnum; i ++ )
\r
132 if( phtab[i].p_type != PT_LOAD ) continue ;
\r
136 // Allocate Information Structure
\r
137 ret = malloc( sizeof(tBinary) + sizeof(tBinarySection)*nLoadSegments );
\r
138 // Fill Info Struct
\r
139 ret->Entry = Header->e_entry;
\r
140 ret->Base = -1; // Set Base to maximum value
\r
141 ret->NumSections = nLoadSegments;
\r
142 ret->Interpreter = NULL;
\r
144 j = 0; // LoadSections[] index
\r
145 for( i = 0; i < Header->e_phnum; i ++ )
\r
147 LOG("phtab[%i] = {", i);
\r
148 LOG(" .p_type = %i", phtab[i].p_type);
\r
149 LOG(" .p_flags = 0x%x", phtab[i].p_flags);
\r
150 LOG(" .p_offset = 0x%llx", phtab[i].p_offset);
\r
151 LOG(" .p_vaddr = 0x%llx", phtab[i].p_vaddr);
\r
152 LOG(" .p_paddr = 0x%llx", phtab[i].p_paddr);
\r
153 LOG(" .p_filesz = 0x%llx", phtab[i].p_filesz);
\r
154 LOG(" .p_memsz = 0x%llx", phtab[i].p_memsz);
\r
155 LOG(" .p_align = 0x%llx", phtab[i].p_align);
\r
158 // Get Interpreter Name
\r
159 if( phtab[i].p_type == PT_INTERP )
\r
162 if(ret->Interpreter) continue;
\r
163 tmp = malloc(phtab[i].p_filesz);
\r
164 VFS_Seek(FD, phtab[i].p_offset, 1);
\r
165 VFS_Read(FD, phtab[i].p_filesz, tmp);
\r
166 ret->Interpreter = Binary_RegInterp(tmp);
\r
167 LOG("Interpreter '%s'", tmp);
\r
172 if( phtab[i].p_type != PT_LOAD ) continue ;
\r
174 // Find the executable base
\r
175 if( phtab[i].p_vaddr < ret->Base ) ret->Base = phtab[i].p_vaddr;
\r
177 ret->LoadSections[j].Offset = phtab[i].p_offset;
\r
178 ret->LoadSections[j].Virtual = phtab[i].p_vaddr;
\r
179 ret->LoadSections[j].FileSize = phtab[i].p_filesz;
\r
180 ret->LoadSections[j].MemSize = phtab[i].p_memsz;
\r
182 ret->LoadSections[j].Flags = 0;
\r
183 if( !(phtab[i].p_flags & PF_W) )
\r
184 ret->LoadSections[j].Flags |= BIN_SECTFLAG_RO;
\r
185 if( phtab[i].p_flags & PF_X )
\r
186 ret->LoadSections[j].Flags |= BIN_SECTFLAG_EXEC;
\r
193 tBinary *Elf_Load32(int FD, Elf32_Ehdr *Header)
\r
202 // Check architecture with current CPU
\r
203 // - TODO: Support kernel level emulation
\r
205 if( Header->machine != EM_386 )
\r
207 Log_Warning("ELF", "Unknown architecure on ELF-32");
\r
213 // Check for a program header
\r
214 if(Header->phoff == 0) {
\r
216 Log_Warning("ELF", "File does not contain a program header (phoff == 0)");
\r
222 // Read Program Header Table
\r
223 phtab = malloc( sizeof(Elf32_Phdr) * Header->phentcount );
\r
228 LOG("hdr.phoff = 0x%08x", Header->phoff);
\r
229 VFS_Seek(FD, Header->phoff, SEEK_SET);
\r
230 VFS_Read(FD, sizeof(Elf32_Phdr)*Header->phentcount, phtab);
\r
234 LOG("Header->phentcount = %i", Header->phentcount);
\r
235 for( i = 0; i < Header->phentcount; i++ )
\r
237 // Ignore Non-LOAD types
\r
238 if(phtab[i].Type != PT_LOAD)
\r
241 LOG("phtab[%i] = {VAddr:0x%x, MemSize:0x%x}", i, phtab[i].VAddr, phtab[i].MemSize);
\r
244 LOG("iLoadCount = %i", iLoadCount);
\r
246 // Allocate Information Structure
\r
247 ret = malloc( sizeof(tBinary) + sizeof(tBinarySection)*iLoadCount );
\r
248 // Fill Info Struct
\r
249 ret->Entry = Header->entrypoint;
\r
250 ret->Base = -1; // Set Base to maximum value
\r
251 ret->NumSections = iLoadCount;
\r
252 ret->Interpreter = NULL;
\r
256 for( i = 0; i < Header->phentcount; i++ )
\r
258 //LOG("phtab[%i].Type = 0x%x", i, phtab[i].Type);
\r
259 LOG("phtab[%i] = {", i);
\r
260 LOG(" .Type = 0x%08x", phtab[i].Type);
\r
261 LOG(" .Offset = 0x%08x", phtab[i].Offset);
\r
262 LOG(" .VAddr = 0x%08x", phtab[i].VAddr);
\r
263 LOG(" .PAddr = 0x%08x", phtab[i].PAddr);
\r
264 LOG(" .FileSize = 0x%08x", phtab[i].FileSize);
\r
265 LOG(" .MemSize = 0x%08x", phtab[i].MemSize);
\r
266 LOG(" .Flags = 0x%08x", phtab[i].Flags);
\r
267 LOG(" .Align = 0x%08x", phtab[i].Align);
\r
269 // Get Interpreter Name
\r
270 if( phtab[i].Type == PT_INTERP )
\r
273 if(ret->Interpreter) continue;
\r
274 tmp = malloc(phtab[i].FileSize);
\r
275 VFS_Seek(FD, phtab[i].Offset, 1);
\r
276 VFS_Read(FD, phtab[i].FileSize, tmp);
\r
277 ret->Interpreter = Binary_RegInterp(tmp);
\r
278 LOG("Interpreter '%s'", tmp);
\r
282 // Ignore non-LOAD types
\r
283 if(phtab[i].Type != PT_LOAD) continue;
\r
286 if(phtab[i].VAddr < ret->Base) ret->Base = phtab[i].VAddr;
\r
288 LOG("phtab[%i] = {VAddr:0x%x,Offset:0x%x,FileSize:0x%x}",
\r
289 i, phtab[i].VAddr, phtab[i].Offset, phtab[i].FileSize);
\r
291 ret->LoadSections[j].Offset = phtab[i].Offset;
\r
292 ret->LoadSections[j].FileSize = phtab[i].FileSize;
\r
293 ret->LoadSections[j].Virtual = phtab[i].VAddr;
\r
294 ret->LoadSections[j].MemSize = phtab[i].MemSize;
\r
295 ret->LoadSections[j].Flags = 0;
\r
296 if( !(phtab[i].Flags & PF_W) )
\r
297 ret->LoadSections[j].Flags |= BIN_SECTFLAG_RO;
\r
298 if( phtab[i].Flags & PF_X )
\r
299 ret->LoadSections[j].Flags |= BIN_SECTFLAG_EXEC;
\r
310 int Elf_Relocate(void *Base)
\r
312 return ElfRelocate(Base, (char**){NULL}, "") != NULL;
\r
314 int Elf_GetSymbol(void *Base, const char *Name, Uint *ret)
\r
316 return ElfGetSymbol(Base, Name, (void**)ret, NULL);
\r