3 * ELF Executable Loader Code
\r
10 #define SysDebug(v...) LOG(v)
\r
11 #define DISABLE_ELF64
\r
12 void *GetSymbol(const char *Name, size_t *Size);
\r
13 void *GetSymbol(const char *Name, size_t *Size) { Uint val; Binary_GetSymbol(Name, &val); if(Size)*Size=0; return (void*)val; };
\r
14 #define AddLoaded(a,b) do{}while(0)
\r
15 #define LoadLibrary(a,b,c) (Log_Debug("ELF", "Module requested lib '%s'",a),0)
\r
16 #include "../../../Usermode/Libraries/ld-acess.so_src/elf.c"
\r
18 #define DEBUG_WARN 1
\r
20 // === PROTOTYPES ===
\r
21 tBinary *Elf_Load(int fp);
\r
22 tBinary *Elf_Load64(int fp, Elf64_Ehdr *hdr);
\r
23 tBinary *Elf_Load32(int fp, Elf32_Ehdr *hdr);
\r
24 int Elf_Relocate(void *Base);
\r
25 int Elf_GetSymbol(void *Base, const char *Name, Uint *Ret);
\r
26 Uint Elf_Int_HashString(const char *str);
\r
29 tBinaryType gELF_Info = {
\r
31 0x464C457F, 0xFFFFFFFF, // '\x7FELF'
\r
33 Elf_Load, Elf_Relocate, Elf_GetSymbol
\r
37 tBinary *Elf_Load(int fp)
\r
42 VFS_Read(fp, sizeof(hdr), &hdr);
\r
44 // Check the file type
\r
45 if(hdr.e_ident[0] != 0x7F || hdr.e_ident[1] != 'E' || hdr.e_ident[2] != 'L' || hdr.e_ident[3] != 'F') {
\r
46 Log_Warning("ELF", "Non-ELF File was passed to the ELF loader");
\r
50 switch(hdr.e_ident[4]) // EI_CLASS
\r
53 return Elf_Load32(fp, (void*)&hdr);
\r
55 return Elf_Load64(fp, &hdr);
\r
57 Log_Warning("ELF", "Unknown EI_CLASS value %i", hdr.e_ident[4]);
\r
62 tBinary *Elf_Load64(int FD, Elf64_Ehdr *Header)
\r
65 Elf64_Phdr phtab[Header->e_phnum];
\r
70 if( Header->e_phoff == 0 )
\r
72 Log_Warning("ELF", "No program header, panic!");
\r
75 if( Header->e_shentsize != sizeof(Elf64_Shdr) ) {
\r
76 Log_Warning("ELF", "Header gives shentsize as %i, my type is %i",
\r
77 Header->e_shentsize, sizeof(Elf64_Shdr) );
\r
79 if( Header->e_phentsize != sizeof(Elf64_Phdr) ) {
\r
80 Log_Warning("ELF", "Header gives phentsize as %i, my type is %i",
\r
81 Header->e_phentsize, sizeof(Elf64_Phdr) );
\r
85 LOG(" e_ident = %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
\r
86 Header->e_ident[0], Header->e_ident[1], Header->e_ident[2], Header->e_ident[3],
\r
87 Header->e_ident[4], Header->e_ident[5], Header->e_ident[6], Header->e_ident[7],
\r
88 Header->e_ident[8], Header->e_ident[9], Header->e_ident[10], Header->e_ident[11],
\r
89 Header->e_ident[12], Header->e_ident[13], Header->e_ident[14], Header->e_ident[15]
\r
91 LOG(" e_type = %i", Header->e_type);
\r
92 LOG(" e_machine = %i", Header->e_machine);
\r
93 LOG(" e_version = %i", Header->e_version);
\r
94 LOG(" e_entry = 0x%llx", Header->e_entry);
\r
95 LOG(" e_phoff = 0x%llx", Header->e_phoff);
\r
96 LOG(" e_shoff = 0x%llx", Header->e_shoff);
\r
97 LOG(" e_flags = 0x%x", Header->e_flags);
\r
98 LOG(" e_ehsize = %i", Header->e_ehsize);
\r
99 LOG(" e_phentsize = %i", Header->e_phentsize);
\r
100 LOG(" e_phnum = %i", Header->e_phnum);
\r
101 LOG(" e_shentsize = %i", Header->e_shentsize);
\r
102 LOG(" e_shnum = %i", Header->e_shnum);
\r
103 LOG(" e_shstrndx = %i", Header->e_shstrndx);
\r
106 // Load Program Header table
\r
107 VFS_Seek(FD, Header->e_phoff, SEEK_SET);
\r
108 VFS_Read(FD, sizeof(Elf64_Phdr)*Header->e_phnum, phtab);
\r
110 // Count load segments
\r
112 for( i = 0; i < Header->e_phnum; i ++ )
\r
114 if( phtab[i].p_type != PT_LOAD ) continue ;
\r
118 // Allocate Information Structure
\r
119 ret = malloc( sizeof(tBinary) + sizeof(tBinarySection)*nLoadSegments );
\r
120 // Fill Info Struct
\r
121 ret->Entry = Header->e_entry;
\r
122 ret->Base = -1; // Set Base to maximum value
\r
123 ret->NumSections = nLoadSegments;
\r
124 ret->Interpreter = NULL;
\r
126 j = 0; // LoadSections[] index
\r
127 for( i = 0; i < Header->e_phnum; i ++ )
\r
129 LOG("phtab[%i] = {", i);
\r
130 LOG(" .p_type = %i", phtab[i].p_type);
\r
131 LOG(" .p_flags = 0x%x", phtab[i].p_flags);
\r
132 LOG(" .p_offset = 0x%llx", phtab[i].p_offset);
\r
133 LOG(" .p_vaddr = 0x%llx", phtab[i].p_vaddr);
\r
134 LOG(" .p_paddr = 0x%llx", phtab[i].p_paddr);
\r
135 LOG(" .p_filesz = 0x%llx", phtab[i].p_filesz);
\r
136 LOG(" .p_memsz = 0x%llx", phtab[i].p_memsz);
\r
137 LOG(" .p_align = 0x%llx", phtab[i].p_align);
\r
140 // Get Interpreter Name
\r
141 if( phtab[i].p_type == PT_INTERP )
\r
144 if(ret->Interpreter) continue;
\r
145 tmp = malloc(phtab[i].p_filesz);
\r
146 VFS_Seek(FD, phtab[i].p_offset, 1);
\r
147 VFS_Read(FD, phtab[i].p_filesz, tmp);
\r
148 ret->Interpreter = Binary_RegInterp(tmp);
\r
149 LOG("Interpreter '%s'", tmp);
\r
154 if( phtab[i].p_type != PT_LOAD ) continue ;
\r
156 // Find the executable base
\r
157 if( phtab[i].p_vaddr < ret->Base ) ret->Base = phtab[i].p_vaddr;
\r
159 ret->LoadSections[j].Offset = phtab[i].p_offset;
\r
160 ret->LoadSections[j].Virtual = phtab[i].p_vaddr;
\r
161 ret->LoadSections[j].FileSize = phtab[i].p_filesz;
\r
162 ret->LoadSections[j].MemSize = phtab[i].p_memsz;
\r
164 ret->LoadSections[j].Flags = 0;
\r
165 if( !(phtab[i].p_flags & PF_W) )
\r
166 ret->LoadSections[j].Flags |= BIN_SECTFLAG_RO;
\r
167 if( phtab[i].p_flags & PF_X )
\r
168 ret->LoadSections[j].Flags |= BIN_SECTFLAG_EXEC;
\r
175 tBinary *Elf_Load32(int FD, Elf32_Ehdr *Header)
\r
184 // Check architecture with current CPU
\r
185 // - TODO: Support kernel level emulation
\r
187 if( Header->machine != EM_386 )
\r
189 Log_Warning("ELF", "Unknown architecure on ELF-32");
\r
195 // Check for a program header
\r
196 if(Header->phoff == 0) {
\r
198 Log_Warning("ELF", "File does not contain a program header (phoff == 0)");
\r
204 // Read Program Header Table
\r
205 phtab = malloc( sizeof(Elf32_Phdr) * Header->phentcount );
\r
210 LOG("hdr.phoff = 0x%08x", Header->phoff);
\r
211 VFS_Seek(FD, Header->phoff, SEEK_SET);
\r
212 VFS_Read(FD, sizeof(Elf32_Phdr)*Header->phentcount, phtab);
\r
216 LOG("Header->phentcount = %i", Header->phentcount);
\r
217 for( i = 0; i < Header->phentcount; i++ )
\r
219 // Ignore Non-LOAD types
\r
220 if(phtab[i].Type != PT_LOAD)
\r
223 LOG("phtab[%i] = {VAddr:0x%x, MemSize:0x%x}", i, phtab[i].VAddr, phtab[i].MemSize);
\r
226 LOG("iLoadCount = %i", iLoadCount);
\r
228 // Allocate Information Structure
\r
229 ret = malloc( sizeof(tBinary) + sizeof(tBinarySection)*iLoadCount );
\r
230 // Fill Info Struct
\r
231 ret->Entry = Header->entrypoint;
\r
232 ret->Base = -1; // Set Base to maximum value
\r
233 ret->NumSections = iLoadCount;
\r
234 ret->Interpreter = NULL;
\r
238 for( i = 0; i < Header->phentcount; i++ )
\r
240 //LOG("phtab[%i].Type = 0x%x", i, phtab[i].Type);
\r
241 LOG("phtab[%i] = {", i);
\r
242 LOG(" .Type = 0x%08x", phtab[i].Type);
\r
243 LOG(" .Offset = 0x%08x", phtab[i].Offset);
\r
244 LOG(" .VAddr = 0x%08x", phtab[i].VAddr);
\r
245 LOG(" .PAddr = 0x%08x", phtab[i].PAddr);
\r
246 LOG(" .FileSize = 0x%08x", phtab[i].FileSize);
\r
247 LOG(" .MemSize = 0x%08x", phtab[i].MemSize);
\r
248 LOG(" .Flags = 0x%08x", phtab[i].Flags);
\r
249 LOG(" .Align = 0x%08x", phtab[i].Align);
\r
251 // Get Interpreter Name
\r
252 if( phtab[i].Type == PT_INTERP )
\r
255 if(ret->Interpreter) continue;
\r
256 tmp = malloc(phtab[i].FileSize);
\r
257 VFS_Seek(FD, phtab[i].Offset, 1);
\r
258 VFS_Read(FD, phtab[i].FileSize, tmp);
\r
259 ret->Interpreter = Binary_RegInterp(tmp);
\r
260 LOG("Interpreter '%s'", tmp);
\r
264 // Ignore non-LOAD types
\r
265 if(phtab[i].Type != PT_LOAD) continue;
\r
268 if(phtab[i].VAddr < ret->Base) ret->Base = phtab[i].VAddr;
\r
270 LOG("phtab[%i] = {VAddr:0x%x,Offset:0x%x,FileSize:0x%x}",
\r
271 i, phtab[i].VAddr, phtab[i].Offset, phtab[i].FileSize);
\r
273 ret->LoadSections[j].Offset = phtab[i].Offset;
\r
274 ret->LoadSections[j].FileSize = phtab[i].FileSize;
\r
275 ret->LoadSections[j].Virtual = phtab[i].VAddr;
\r
276 ret->LoadSections[j].MemSize = phtab[i].MemSize;
\r
277 ret->LoadSections[j].Flags = 0;
\r
278 if( !(phtab[i].Flags & PF_W) )
\r
279 ret->LoadSections[j].Flags |= BIN_SECTFLAG_RO;
\r
280 if( phtab[i].Flags & PF_X )
\r
281 ret->LoadSections[j].Flags |= BIN_SECTFLAG_EXEC;
\r
292 int Elf_Relocate(void *Base)
\r
294 return ElfRelocate(Base, (char**){NULL}, "") != NULL;
\r
296 int Elf_GetSymbol(void *Base, const char *Name, Uint *ret)
\r
298 return ElfGetSymbol(Base, Name, (void**)ret, NULL);
\r