2 * Acess2 Dynamic Linker
3 * - By John Hodge (thePowersGang)
6 * - ELF32/ELF64 relocation
8 #ifndef DEBUG // This code is #include'd from the kernel, so DEBUG may already be defined
13 # define PAGE_SIZE 4096
22 # define DEBUGS(v...) SysDebug("ld-acess - " v)
28 # define SUPPORT_ELF64
33 //static const char *csaDT_NAMES[] = {"DT_NULL", "DT_NEEDED", "DT_PLTRELSZ", "DT_PLTGOT", "DT_HASH", "DT_STRTAB", "DT_SYMTAB", "DT_RELA", "DT_RELASZ", "DT_RELAENT", "DT_STRSZ", "DT_SYMENT", "DT_INIT", "DT_FINI", "DT_SONAME", "DT_RPATH", "DT_SYMBOLIC", "DT_REL", "DT_RELSZ", "DT_RELENT", "DT_PLTREL", "DT_DEBUG", "DT_TEXTREL", "DT_JMPREL"};
34 static const char *csaR_NAMES[] = {"R_386_NONE", "R_386_32", "R_386_PC32", "R_386_GOT32", "R_386_PLT32", "R_386_COPY", "R_386_GLOB_DAT", "R_386_JMP_SLOT", "R_386_RELATIVE", "R_386_GOTOFF", "R_386_GOTPC", "R_386_LAST"};
38 void *ElfRelocate(void *Base, char **envp, const char *Filename);
39 int ElfGetSymbol(void *Base, const char *Name, void **Ret, size_t *Size);
40 void *Elf32Relocate(void *Base, char **envp, const char *Filename);
41 int Elf32GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size);
42 int elf_doRelocate_386(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff);
43 int elf_doRelocate_arm(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff);
44 int elf_doRelocate_unk(uint32_t , uint32_t *, Elf32_Addr , int , int , const char *, intptr_t);
46 void *Elf64Relocate(void *Base, char **envp, const char *Filename);
47 int Elf64GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size);
49 uint32_t ElfHashString(const char *name);
53 * \fn int ElfRelocate(void *Base, char **envp, const char *Filename)
54 * \brief Relocates a loaded ELF Executable
56 void *ElfRelocate(void *Base, char **envp, const char *Filename)
58 Elf32_Ehdr *hdr = Base;
60 switch(hdr->e_ident[4])
63 return Elf32Relocate(Base, envp, Filename);
66 return Elf64Relocate(Base, envp, Filename);
69 SysDebug("ld-acess - ElfRelocate: Unknown file class %i", hdr->e_ident[4]);
75 * \fn int ElfGetSymbol(Uint Base, const char *name, void **ret)
77 int ElfGetSymbol(void *Base, const char *Name, void **ret, size_t *Size)
79 Elf32_Ehdr *hdr = Base;
81 switch(hdr->e_ident[4])
84 return Elf32GetSymbol(Base, Name, ret, Size);
87 return Elf64GetSymbol(Base, Name, ret, Size);
90 SysDebug("ld-acess - ElfRelocate: Unknown file class %i", hdr->e_ident[4]);
95 int elf_doRelocate_386(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type,
96 int bRela, const char *Sym, intptr_t iBaseDiff)
101 // Standard 32 Bit Relocation (S+A)
103 if( !GetSymbol(Sym, &symval, NULL) )
105 DEBUGS(" elf_doRelocate: R_386_32 *0x%x += %p('%s')",
107 *ptr = (intptr_t)symval + addend;
110 // 32 Bit Relocation wrt. Offset (S+A-P)
112 DEBUGS(" elf_doRelocate: '%s'", Sym);
113 if( !GetSymbol(Sym, &symval, NULL) ) return 1;
114 DEBUGS(" elf_doRelocate: R_386_PC32 *0x%x = 0x%x + 0x%p - 0x%x",
115 ptr, *ptr, symval, (intptr_t)ptr );
116 *ptr = (intptr_t)symval + addend - (intptr_t)ptr;
117 //*ptr = val + addend - ((Uint)ptr - iBaseDiff);
120 // Absolute Value of a symbol (S)
123 DEBUGS(" elf_doRelocate: '%s'", Sym);
124 if( !GetSymbol(Sym, &symval, NULL) ) return 1;
125 DEBUGS(" elf_doRelocate: %s *0x%x = %p", csaR_NAMES[type], ptr, symval);
126 *ptr = (intptr_t)symval;
129 // Base Address (B+A)
131 DEBUGS(" elf_doRelocate: R_386_RELATIVE *0x%x = 0x%x + 0x%x", ptr, iBaseDiff, addend);
132 *ptr = iBaseDiff + addend;
137 if( !GetSymbol(Sym, &symval, &size) ) return 1;
138 DEBUGS(" elf_doRelocate_386: R_386_COPY (%p, %p, %i)", ptr, symval, size);
139 memcpy(ptr, symval, size);
143 SysDebug("elf_doRelocate_386: Unknown relocation %i", type);
149 int elf_doRelocate_arm(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff)
156 DEBUGS(" elf_doRelocate_arm: R_ARM_ABS32 %p (%s + %x)", ptr, Sym, addend);
157 if( !GetSymbol(Sym, (void**)&val, NULL) ) return 1;
161 DEBUGS(" elf_doRelocate_arm: R_ARM_GLOB_DAT %p (%s + %x)", ptr, Sym, addend);
162 if( !GetSymbol(Sym, (void**)&val, NULL) ) return 1;
165 case R_ARM_JUMP_SLOT:
166 if(!bRela) addend = 0;
167 DEBUGS(" elf_doRelocate_arm: R_ARM_JUMP_SLOT %p (%s + %x)", ptr, Sym, addend);
168 if( !GetSymbol(Sym, (void**)&val, NULL) ) return 1;
175 if( !GetSymbol(Sym, &src, &size) ) return 1;
176 DEBUGS(" elf_doRelocate_arm: R_ARM_COPY (%p, %p, %i)", ptr, src, size);
177 memcpy(ptr, src, size);
179 // Delta between link and runtime locations + A
181 DEBUGS(" elf_doRelocate_arm: R_ARM_RELATIVE %p (0x%x + 0x%x)", ptr, iBaseDiff, addend);
183 // TODO: Get delta for a symbol
184 SysDebug("elf_doRelocate_arm: TODO - Implment R_ARM_RELATIVE for symbols");
188 *ptr = iBaseDiff + addend;
192 SysDebug("elf_doRelocate_arm: Unknown Relocation, %i", type);
198 int elf_doRelocate_unk(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff)
203 void *Elf32Relocate(void *Base, char **envp, const char *Filename)
205 Elf32_Ehdr *hdr = Base;
207 int i, j; // Counters
209 intptr_t iRealBase = -1;
213 Elf32_Rel *rel = NULL;
214 Elf32_Rela *rela = NULL;
216 int relSz=0, relEntSz=8;
217 int relaSz=0, relaEntSz=8;
218 int pltSz=0, pltType=0;
219 Elf32_Dyn *dynamicTab = NULL; // Dynamic Table Pointer
220 char *dynstrtab = NULL; // .dynamic String Table
221 Elf32_Sym *dynsymtab;
222 int (*do_relocate)(uint32_t t_info, uint32_t *ptr, Elf32_Addr addend, int Type, int bRela, const char *Sym, intptr_t iBaseDiff);
224 DEBUGS("ElfRelocate: (Base=0x%x)", Base);
226 // Check magic header
229 // Parse Program Header to get Dynamic Table
230 phtab = (void*)( (uintptr_t)Base + hdr->phoff );
231 iSegmentCount = hdr->phentcount;
232 for(i=0;i<iSegmentCount;i++)
234 // Determine linked base address
235 if(phtab[i].Type == PT_LOAD && iRealBase > phtab[i].VAddr)
236 iRealBase = phtab[i].VAddr;
238 // Find Dynamic Section
239 if(phtab[i].Type == PT_DYNAMIC) {
241 DEBUGS(" WARNING - elf_relocate: Multiple PT_DYNAMIC segments");
244 dynamicTab = (void *) (intptr_t) phtab[i].VAddr;
245 j = i; // Save Dynamic Table ID
249 // Page Align real base
251 DEBUGS(" elf_relocate: True Base = 0x%x, Compiled Base = 0x%x", Base, iRealBase);
253 // Adjust "Real" Base
254 iBaseDiff = (intptr_t)Base - iRealBase;
256 // hdr->entrypoint += iBaseDiff; // Adjust Entrypoint
258 // Check if a PT_DYNAMIC segement was found
260 SysDebug(" elf_relocate: No PT_DYNAMIC segment in image %p, returning", Base);
261 return (void *)(intptr_t)(hdr->entrypoint + iBaseDiff);
264 // Allow writing to read-only segments, just in case they need to be relocated
265 // - Will be reversed at the end of the function
266 for( i = 0; i < iSegmentCount; i ++ )
268 if(phtab[i].Type == PT_LOAD && !(phtab[i].Flags & PF_W) ) {
269 uintptr_t addr = phtab[i].VAddr + iBaseDiff;
270 uintptr_t end = addr + phtab[i].MemSize;
271 for( ; addr < end; addr += PAGE_SIZE )
272 _SysSetMemFlags(addr, 0, 1); // Unset RO
276 // Adjust Dynamic Table
277 dynamicTab = (void *)( (intptr_t)dynamicTab + iBaseDiff );
279 // === Get Symbol table and String Table ===
281 for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)
283 switch(dynamicTab[j].d_tag)
285 // --- Symbol Table ---
287 DEBUGS(" elf_relocate: DYNAMIC Symbol Table 0x%x (0x%x)",
288 dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff);
289 if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
290 dynsymtab = (void*)(intptr_t)dynamicTab[j].d_val;
292 // --- String Table ---
294 DEBUGS(" elf_relocate: DYNAMIC String Table 0x%x (0x%x)",
295 dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff);
296 if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
297 dynstrtab = (void*)(intptr_t)dynamicTab[j].d_val;
301 if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
302 // iSymCount = ((Elf32_Word*)(intptr_t)dynamicTab[j].d_val)[1];
307 if(dynsymtab == NULL) {
308 SysDebug("ld-acess.so - WARNING: No Dynamic Symbol table in %p, returning", hdr);
309 return (void *)(intptr_t) (hdr->entrypoint + iBaseDiff);
312 // === Add to loaded list (can be imported now) ===
313 AddLoaded( Filename, Base );
315 // === Parse Relocation Data ===
316 DEBUGS(" elf_relocate: dynamicTab = 0x%x", dynamicTab);
317 for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)
319 switch(dynamicTab[j].d_tag)
321 // --- Shared Library Name ---
323 DEBUGS(" elf_relocate: .so Name '%s'", dynstrtab+dynamicTab[j].d_val);
325 // --- Needed Library ---
327 libPath = dynstrtab + dynamicTab[j].d_val;
328 DEBUGS(" Required Library '%s'", libPath);
329 if(LoadLibrary(libPath, NULL, envp) == 0) {
331 DEBUGS(" elf_relocate: Unable to load '%s'", libPath);
333 SysDebug("Unable to load required library '%s'", libPath);
337 DEBUGS(" Lib loaded");
340 // case DT_PLTGOT: pltgot = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
341 case DT_JMPREL: plt = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
342 case DT_PLTREL: pltType = dynamicTab[j].d_val; break;
343 case DT_PLTRELSZ: pltSz = dynamicTab[j].d_val; break;
345 // --- Relocation ---
346 case DT_REL: rel = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
347 case DT_RELSZ: relSz = dynamicTab[j].d_val; break;
348 case DT_RELENT: relEntSz = dynamicTab[j].d_val; break;
349 case DT_RELA: rela = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
350 case DT_RELASZ: relaSz = dynamicTab[j].d_val; break;
351 case DT_RELAENT: relaEntSz = dynamicTab[j].d_val; break;
353 // --- Symbol Table ---
355 // --- Hash Table ---
357 // --- String Table ---
363 if(dynamicTab[j].d_tag > DT_JMPREL) continue;
364 //DEBUGS(" elf_relocate: %i-%i = %s,0x%x",
365 // i,j, csaDT_NAMES[dynamicTab[j].d_tag],dynamicTab[j].d_val);
370 DEBUGS(" elf_relocate: Beginning Relocation");
377 do_relocate = elf_doRelocate_386;
380 do_relocate = elf_doRelocate_arm;
383 SysDebug("Elf32Relocate: Unknown machine type %i", hdr->machine);
384 do_relocate = elf_doRelocate_unk;
389 DEBUGS("do_relocate = %p (%p or %p)", do_relocate, &elf_doRelocate_386, &elf_doRelocate_arm);
391 #define _doRelocate(r_info, ptr, bRela, addend) \
392 do_relocate(r_info, ptr, addend, ELF32_R_TYPE(r_info), bRela, \
393 dynstrtab + dynsymtab[ELF32_R_SYM(r_info)].nameOfs, iBaseDiff);
395 // Parse Relocation Entries
399 DEBUGS(" elf_relocate: rel=0x%x, relSz=0x%x, relEntSz=0x%x", rel, relSz, relEntSz);
400 j = relSz / relEntSz;
401 for( i = 0; i < j; i++ )
403 //DEBUGS(" Rel %i: 0x%x+0x%x", i, iBaseDiff, rel[i].r_offset);
404 ptr = (void*)(iBaseDiff + rel[i].r_offset);
405 fail |= _doRelocate(rel[i].r_info, ptr, 0, *ptr);
408 // Parse Relocation Entries
412 DEBUGS(" elf_relocate: rela=0x%x, relaSz=0x%x, relaEntSz=0x%x", rela, relaSz, relaEntSz);
413 j = relaSz / relaEntSz;
414 for( i = 0; i < j; i++ )
416 ptr = (void*)(iBaseDiff + rela[i].r_offset);
417 fail |= _doRelocate(rel[i].r_info, ptr, 1, rela[i].r_addend);
421 // === Process PLT (Procedure Linkage Table) ===
425 DEBUGS(" elf_relocate: Relocate PLT, plt=0x%x", plt);
426 if(pltType == DT_REL)
428 Elf32_Rel *pltRel = plt;
429 j = pltSz / sizeof(Elf32_Rel);
430 DEBUGS(" elf_relocate: PLT Reloc Type = Rel, %i entries", j);
433 ptr = (void*)(iBaseDiff + pltRel[i].r_offset);
434 fail |= _doRelocate(pltRel[i].r_info, ptr, 0, *ptr);
439 Elf32_Rela *pltRela = plt;
440 j = pltSz / sizeof(Elf32_Rela);
441 DEBUGS(" elf_relocate: PLT Reloc Type = Rela, %i entries", j);
444 ptr = (void*)(iRealBase + pltRela[i].r_offset);
445 fail |= _doRelocate(pltRela[i].r_info, ptr, 1, pltRela[i].r_addend);
451 for( i = 0; i < iSegmentCount; i ++ )
453 // If load and not writable
454 if(phtab[i].Type == PT_LOAD && !(phtab[i].Flags & PF_W) ) {
455 uintptr_t addr = phtab[i].VAddr + iBaseDiff;
456 uintptr_t end = addr + phtab[i].MemSize;
457 for( ; addr < end; addr += PAGE_SIZE )
458 _SysSetMemFlags(addr, 1, 1); // Unset RO
463 DEBUGS("ElfRelocate: Failure");
469 DEBUGS("ElfRelocate: RETURN 0x%x to %p", hdr->entrypoint + iBaseDiff, __builtin_return_address(0));
470 return (void*)(intptr_t)( hdr->entrypoint + iBaseDiff );
473 int Elf32GetSymbol(void *Base, const char *Name, void **ret, size_t *Size)
475 Elf32_Ehdr *hdr = Base;
476 Elf32_Sym *symtab = NULL;
478 Elf32_Word *pBuckets = NULL;
481 const char *dynstrtab = NULL;
482 uintptr_t iBaseDiff = -1;
484 Elf32_Dyn *dynTab = NULL;
488 phtab = (void*)( (uintptr_t)Base + hdr->phoff );
489 for( i = 0; i < hdr->phentcount; i ++ )
491 if(phtab[i].Type == PT_LOAD && iBaseDiff > phtab[i].VAddr)
492 iBaseDiff = phtab[i].VAddr;
493 if( phtab[i].Type == PT_DYNAMIC ) {
494 dynTab = (void*)(intptr_t)phtab[i].VAddr;
498 SysDebug("ERROR - Unable to find DYNAMIC segment in %p", Base);
501 iBaseDiff = (intptr_t)Base - iBaseDiff; // Make iBaseDiff actually the diff
502 dynTab = (void*)( (intptr_t)dynTab + iBaseDiff );
503 for( i = 0; dynTab[i].d_tag != DT_NULL; i++)
505 switch(dynTab[i].d_tag)
507 // --- Symbol Table ---
509 symtab = (void*)(intptr_t) dynTab[i].d_val; // Rebased in Relocate
512 dynstrtab = (void*)(intptr_t) dynTab[i].d_val;
516 pBuckets = (void*)(intptr_t) dynTab[i].d_val;
522 SysDebug("ERRO - No DT_SYMTAB in %p", Base);
526 SysDebug("ERRO - No DT_HASH in %p", Base);
530 SysDebug("ERRO - No DT_STRTAB in %p", Base);
534 // ... ok... maybe they haven't been relocated
535 if( (uintptr_t)symtab < (uintptr_t)Base )
537 symtab = (void*)( (uintptr_t)symtab + iBaseDiff );
538 pBuckets = (void*)( (uintptr_t)pBuckets + iBaseDiff );
539 dynstrtab = (void*)( (uintptr_t)dynstrtab + iBaseDiff );
540 SysDebug("Executable not yet relocated");
543 nbuckets = pBuckets[0];
544 // iSymCount = pBuckets[1];
545 pBuckets = &pBuckets[2];
546 pChains = &pBuckets[ nbuckets ];
549 iNameHash = ElfHashString(Name);
550 iNameHash %= nbuckets;
553 i = pBuckets[ iNameHash ];
554 if(symtab[i].shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].nameOfs, Name) == 0) {
555 *ret = (void*)( (uintptr_t) symtab[ i ].value + iBaseDiff );
556 if(Size) *Size = symtab[i].size;
560 while(pChains[i] != STN_UNDEF)
563 if(symtab[i].shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[ i ].nameOfs, Name) == 0) {
564 *ret = (void*)( (uintptr_t)symtab[ i ].value + iBaseDiff );
565 if(Size) *Size = symtab[i].size;
574 typedef int (*t_elf64_doreloc)(void *Base, const char *strtab, Elf64_Sym *symtab, Elf64_Xword r_info, void *ptr, Elf64_Sxword addend);
576 int _Elf64DoReloc_X86_64(void *Base, const char *strtab, Elf64_Sym *symtab, Elf64_Xword r_info, void *ptr, Elf64_Sxword addend)
578 int sym = ELF64_R_SYM(r_info);
579 int type = ELF64_R_TYPE(r_info);
580 const char *symname = strtab + symtab[sym].st_name;
582 //DEBUGS("_Elf64DoReloc: %s", symname);
588 if( !GetSymbol(symname, &symval, NULL) ) return 1;
589 *(uint64_t*)ptr = (uintptr_t)symval + addend;
591 case R_X86_64_COPY: {
593 if( !GetSymbol(symname, &symval, &size) ) return 1;
594 memcpy(ptr, symval, size);
596 case R_X86_64_GLOB_DAT:
597 if( !GetSymbol(symname, &symval, NULL) ) return 1;
598 *(uint64_t*)ptr = (uintptr_t)symval;
600 case R_X86_64_JUMP_SLOT:
601 if( !GetSymbol(symname, &symval, NULL) ) return 1;
602 *(uint64_t*)ptr = (uintptr_t)symval;
604 case R_X86_64_RELATIVE:
605 *(uint64_t*)ptr = (uintptr_t)Base + addend;
608 SysDebug("ld-acess - _Elf64DoReloc: Unknown relocation type %i", type);
611 //DEBUGS("_Elf64DoReloc: - Good");
615 void *Elf64Relocate(void *Base, char **envp, const char *Filename)
618 Elf64_Ehdr *hdr = Base;
620 Elf64_Dyn *dyntab = NULL;
621 Elf64_Addr compiledBase = -1, baseDiff;
622 Elf64_Sym *symtab = NULL;
624 Elf64_Word *hashtab = NULL;
625 Elf64_Rel *rel = NULL;
627 Elf64_Rela *rela = NULL;
630 int plt_size = 0, plt_type = 0;
632 DEBUGS("Elf64Relocate: hdr = {");
633 DEBUGS("Elf64Relocate: e_ident = '%.16s'", hdr->e_ident);
634 DEBUGS("Elf64Relocate: e_type = 0x%x", hdr->e_type);
635 DEBUGS("Elf64Relocate: e_machine = 0x%x", hdr->e_machine);
636 DEBUGS("Elf64Relocate: e_version = 0x%x", hdr->e_version);
637 DEBUGS("Elf64Relocate: e_entry = %p", hdr->e_entry);
638 DEBUGS("Elf64Relocate: e_phoff = 0x%llx", hdr->e_phoff);
639 DEBUGS("Elf64Relocate: e_shoff = 0x%llx", hdr->e_shoff);
640 DEBUGS("Elf64Relocate: e_flags = 0x%x", hdr->e_flags);
641 DEBUGS("Elf64Relocate: e_ehsize = 0x%x", hdr->e_ehsize);
642 DEBUGS("Elf64Relocate: e_phentsize = 0x%x", hdr->e_phentsize);
643 DEBUGS("Elf64Relocate: e_phnum = %i", hdr->e_phnum);
645 // Scan for the dynamic table (and find the compiled base)
646 phtab = (void*)((uintptr_t)Base + (uintptr_t)hdr->e_phoff);
647 for( i = 0; i < hdr->e_phnum; i ++ )
649 if(phtab[i].p_type == PT_DYNAMIC)
650 dyntab = (void *)(intptr_t)phtab[i].p_vaddr;
651 if(phtab[i].p_type == PT_LOAD && compiledBase > phtab[i].p_vaddr)
652 compiledBase = phtab[i].p_vaddr;
655 baseDiff = (uintptr_t)Base - compiledBase;
657 DEBUGS("baseDiff = %p", baseDiff);
660 SysDebug(" Elf64Relocate: No PT_DYNAMIC segment in image %p, returning", Base);
661 return (void *)(uintptr_t)(hdr->e_entry + baseDiff);
664 dyntab = (void *)(uintptr_t)((uintptr_t)dyntab + baseDiff);
666 // Parse the dynamic table (first pass)
667 // - Search for String, Symbol and Hash tables
668 for(i = 0; dyntab[i].d_tag != DT_NULL; i ++)
670 switch(dyntab[i].d_tag)
673 dyntab[i].d_un.d_ptr += baseDiff;
674 symtab = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
677 dyntab[i].d_un.d_ptr += baseDiff;
678 strtab = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
681 dyntab[i].d_un.d_ptr += baseDiff;
682 hashtab = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
687 if( !symtab || !strtab || !hashtab ) {
688 SysDebug("ld-acess - Elf64Relocate: Missing Symbol, string or hash table");
692 // Ready for symbol use
693 AddLoaded( Filename, Base );
695 // Second pass on dynamic table
696 for(i = 0; dyntab[i].d_tag != DT_NULL; i ++)
698 DEBUGS("dyntab[%i].d_tag = %i", i, dyntab[i].d_tag);
699 switch(dyntab[i].d_tag)
701 case DT_SONAME: break;
704 char *libPath = strtab + dyntab[i].d_un.d_val;
705 DEBUGS("Elf64Relocate: libPath = '%s'", libPath);
706 if(LoadLibrary(libPath, NULL, envp) == 0) {
707 SysDebug("ld-acess - Elf64Relocate: Unable to load '%s'", libPath);
712 // Relocation entries
714 dyntab[i].d_un.d_ptr += baseDiff;
715 rel = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
718 rel_count = dyntab[i].d_un.d_val / sizeof(Elf64_Rel);
721 if( dyntab[i].d_un.d_val != sizeof(Elf64_Rel) ) {
722 SysDebug("ld-acess - Elf64Relocate: DT_RELENT(%i) != sizeof(Elf64_Rel)(%i)",
723 dyntab[i].d_un.d_val, sizeof(Elf64_Rel));
728 dyntab[i].d_un.d_ptr += baseDiff;
729 rela = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
732 rela_count = dyntab[i].d_un.d_val / sizeof(Elf64_Rela);
735 if( dyntab[i].d_un.d_val != sizeof(Elf64_Rela) ) {
736 SysDebug("ld-acess - Elf64Relocate: DT_RELAENT(%i) != sizeof(Elf64_Rela)(%i)",
737 dyntab[i].d_un.d_val, sizeof(Elf64_Rela));
742 dyntab[i].d_un.d_ptr += baseDiff;
743 pltrel = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
746 plt_type = dyntab[i].d_un.d_val;
749 plt_size = dyntab[i].d_un.d_val;
754 // Relocation function
755 t_elf64_doreloc fpElf64DoReloc = &_Elf64DoReloc_X86_64;
756 #define _Elf64DoReloc(info, ptr, addend) fpElf64DoReloc(Base, strtab, symtab, info, ptr, addend)
761 DEBUGS("rel_count = %i", rel_count);
762 for( i = 0; i < rel_count; i ++ )
764 uint64_t *ptr = (void *)(uintptr_t)( rel[i].r_offset + baseDiff );
765 fail |= _Elf64DoReloc( rel[i].r_info, ptr, *ptr);
771 DEBUGS("rela_count = %i", rela_count);
772 for( i = 0; i < rela_count; i ++ )
774 uint64_t *ptr = (void *)(uintptr_t)( rela[i].r_offset + baseDiff );
775 fail |= _Elf64DoReloc( rela[i].r_info, ptr, rela[i].r_addend );
779 if( pltrel && plt_type )
781 if( plt_type == DT_REL ) {
782 Elf64_Rel *plt = pltrel;
783 int count = plt_size / sizeof(Elf64_Rel);
784 DEBUGS("plt rel count = %i", count);
785 for( i = 0; i < count; i ++ )
787 uint64_t *ptr = (void *)(uintptr_t)( plt[i].r_offset + baseDiff );
788 fail |= _Elf64DoReloc( plt[i].r_info, ptr, *ptr);
792 Elf64_Rela *plt = pltrel;
793 int count = plt_size / sizeof(Elf64_Rela);
794 DEBUGS("plt rela count = %i", count);
795 for( i = 0; i < count; i ++ )
797 uint64_t *ptr = (void *)(uintptr_t)( plt[i].r_offset + baseDiff );
798 fail |= _Elf64DoReloc( plt[i].r_info, ptr, plt[i].r_addend);
804 DEBUGS("Elf64Relocate: Failure");
809 void *ret = (void *)(uintptr_t)(hdr->e_entry + baseDiff);
810 DEBUGS("Elf64Relocate: Relocations done, return %p", ret);
815 int Elf64GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size)
817 Elf64_Ehdr *hdr = Base;
820 // int iSymCount = 0;
822 Elf64_Word *pBuckets;
825 const char *dynstrtab;
826 uintptr_t iBaseDiff = -1;
832 // Catch the current executable
836 Elf64_Dyn *dynTab = NULL;
840 phtab = (void*)( (intptr_t)Base + (uintptr_t)hdr->e_phoff );
841 for( i = 0; i < hdr->e_phnum; i ++ )
843 if(phtab[i].p_type == PT_LOAD && iBaseDiff > phtab[i].p_vaddr)
844 iBaseDiff = phtab[i].p_vaddr;
845 if( phtab[i].p_type == PT_DYNAMIC ) {
846 dynTab = (void*)(intptr_t)phtab[i].p_vaddr;
850 SysDebug("ERROR - Unable to find DYNAMIC segment in %p", Base);
853 iBaseDiff = (intptr_t)Base - iBaseDiff; // Make iBaseDiff actually the diff
854 dynTab = (void*)( (intptr_t)dynTab + iBaseDiff );
856 for( j = 0; dynTab[j].d_tag != DT_NULL; j++)
858 switch(dynTab[j].d_tag)
860 // --- Symbol Table ---
862 symtab = (void*)(intptr_t) dynTab[j].d_un.d_val; // Rebased in Relocate
865 dynstrtab = (void*)(intptr_t) dynTab[j].d_un.d_val;
869 pBuckets = (void*)(intptr_t) dynTab[j].d_un.d_val;
875 nbuckets = pBuckets[0];
876 // iSymCount = pBuckets[1];
877 pBuckets = &pBuckets[2];
878 pChains = &pBuckets[ nbuckets ];
881 iNameHash = ElfHashString(Name);
882 iNameHash %= nbuckets;
885 i = pBuckets[ iNameHash ];
886 if(symtab[i].st_shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].st_name, Name) == 0) {
887 *Ret = (void*)( (intptr_t)symtab[i].st_value + iBaseDiff );
888 if(Size) *Size = symtab[i].st_size;
889 DEBUGS("%s = %p", Name, *Ret);
893 while(pChains[i] != STN_UNDEF)
896 if(symtab[i].st_shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].st_name, Name) == 0) {
897 *Ret = (void*)((intptr_t)symtab[i].st_value + iBaseDiff);
898 if(Size) *Size = symtab[i].st_size;
899 DEBUGS("%s = %p", Name, *Ret);
909 uint32_t ElfHashString(const char *name)
914 h = (h << 4) + *(uint8_t*)name++;
915 if( (g = h & 0xf0000000) )