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
18 # define DEBUGS(v...) SysDebug("ld-acess - " v)
24 # define SUPPORT_ELF64
29 //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"};
30 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"};
34 void *ElfRelocate(void *Base, char **envp, const char *Filename);
35 int ElfGetSymbol(void *Base, const char *Name, void **Ret, size_t *Size);
36 void *Elf32Relocate(void *Base, char **envp, const char *Filename);
37 int Elf32GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size);
38 void elf_doRelocate_386(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff);
39 void elf_doRelocate_arm(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff);
41 void *Elf64Relocate(void *Base, char **envp, const char *Filename);
42 int Elf64GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size);
44 uint32_t ElfHashString(const char *name);
48 * \fn int ElfRelocate(void *Base, char **envp, const char *Filename)
49 * \brief Relocates a loaded ELF Executable
51 void *ElfRelocate(void *Base, char **envp, const char *Filename)
53 Elf32_Ehdr *hdr = Base;
55 switch(hdr->e_ident[4])
58 return Elf32Relocate(Base, envp, Filename);
61 return Elf64Relocate(Base, envp, Filename);
64 SysDebug("ld-acess - ElfRelocate: Unknown file class %i", hdr->e_ident[4]);
70 * \fn int ElfGetSymbol(Uint Base, const char *name, void **ret)
72 int ElfGetSymbol(void *Base, const char *Name, void **ret, size_t *Size)
74 Elf32_Ehdr *hdr = Base;
76 switch(hdr->e_ident[4])
79 return Elf32GetSymbol(Base, Name, ret, Size);
82 return Elf64GetSymbol(Base, Name, ret, Size);
85 SysDebug("ld-acess - ElfRelocate: Unknown file class %i", hdr->e_ident[4]);
90 void elf_doRelocate_386(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff)
95 // Standard 32 Bit Relocation (S+A)
97 val = (intptr_t) GetSymbol(Sym, NULL);
98 DEBUGS(" elf_doRelocate: R_386_32 *0x%x += 0x%x('%s')",
103 // 32 Bit Relocation wrt. Offset (S+A-P)
105 DEBUGS(" elf_doRelocate: '%s'", Sym);
106 val = (intptr_t) GetSymbol(Sym, NULL);
107 DEBUGS(" elf_doRelocate: R_386_PC32 *0x%x = 0x%x + 0x%x - 0x%x",
108 ptr, *ptr, val, (intptr_t)ptr );
109 *ptr = val + addend - (intptr_t)ptr;
110 //*ptr = val + addend - ((Uint)ptr - iBaseDiff);
113 // Absolute Value of a symbol (S)
116 DEBUGS(" elf_doRelocate: '%s'", Sym);
117 val = (intptr_t) GetSymbol( Sym, NULL );
118 DEBUGS(" elf_doRelocate: %s *0x%x = 0x%x", csaR_NAMES[type], ptr, val);
122 // Base Address (B+A)
124 DEBUGS(" elf_doRelocate: R_386_RELATIVE *0x%x = 0x%x + 0x%x", ptr, iBaseDiff, addend);
125 *ptr = iBaseDiff + addend;
130 void *src = GetSymbol(Sym, &size);
131 DEBUGS(" elf_doRelocate_386: R_386_COPY (%p, %p, %i)", ptr, src, size);
132 memcpy(ptr, src, size);
136 SysDebug("elf_doRelocate_386: Unknown relocation %i", type);
141 void elf_doRelocate_arm(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff)
148 DEBUGS(" elf_doRelocate_arm: R_ARM_ABS32 %p (%s + %x)", ptr, Sym, addend);
149 val = (intptr_t)GetSymbol(Sym, NULL);
153 DEBUGS(" elf_doRelocate_arm: R_ARM_GLOB_DAT %p (%s + %x)", ptr, Sym, addend);
154 val = (intptr_t)GetSymbol(Sym, NULL);
157 case R_ARM_JUMP_SLOT:
158 if(!bRela) addend = 0;
159 DEBUGS(" elf_doRelocate_arm: R_ARM_JUMP_SLOT %p (%s + %x)", ptr, Sym, addend);
160 val = (intptr_t)GetSymbol(Sym, NULL);
166 void *src = GetSymbol(Sym, &size);
167 DEBUGS(" elf_doRelocate_arm: R_ARM_COPY (%p, %p, %i)", ptr, src, size);
168 memcpy(ptr, src, size);
170 // Delta between link and runtime locations + A
173 // TODO: Get delta for a symbol
174 SysDebug("elf_doRelocate_arm: TODO - Implment R_ARM_RELATIVE for symbols");
177 *ptr = iBaseDiff + addend;
181 SysDebug("elf_doRelocate_arm: Unknown Relocation, %i", type);
186 void *Elf32Relocate(void *Base, char **envp, const char *Filename)
188 Elf32_Ehdr *hdr = Base;
190 int i, j; // Counters
192 intptr_t iRealBase = -1;
196 Elf32_Rel *rel = NULL;
197 Elf32_Rela *rela = NULL;
199 int relSz=0, relEntSz=8;
200 int relaSz=0, relaEntSz=8;
201 int pltSz=0, pltType=0;
202 Elf32_Dyn *dynamicTab = NULL; // Dynamic Table Pointer
203 char *dynstrtab = NULL; // .dynamic String Table
204 Elf32_Sym *dynsymtab;
205 void (*do_relocate)(uint32_t t_info, uint32_t *ptr, Elf32_Addr addend, int Type, int bRela, const char *Sym, intptr_t iBaseDiff);
206 auto void _doRelocate(uint32_t r_info, uint32_t *ptr, int bRela, Elf32_Addr addend);
208 DEBUGS("ElfRelocate: (Base=0x%x)", Base);
210 // Check magic header
213 // Parse Program Header to get Dynamic Table
214 phtab = (void*)( (uintptr_t)Base + hdr->phoff );
215 iSegmentCount = hdr->phentcount;
216 for(i=0;i<iSegmentCount;i++)
218 // Determine linked base address
219 if(phtab[i].Type == PT_LOAD && iRealBase > phtab[i].VAddr)
220 iRealBase = phtab[i].VAddr;
222 // Find Dynamic Section
223 if(phtab[i].Type == PT_DYNAMIC) {
225 DEBUGS(" WARNING - elf_relocate: Multiple PT_DYNAMIC segments");
228 dynamicTab = (void *) (intptr_t) phtab[i].VAddr;
229 j = i; // Save Dynamic Table ID
233 // Page Align real base
235 DEBUGS(" elf_relocate: True Base = 0x%x, Compiled Base = 0x%x", Base, iRealBase);
237 // Adjust "Real" Base
238 iBaseDiff = (intptr_t)Base - iRealBase;
240 // hdr->entrypoint += iBaseDiff; // Adjust Entrypoint
242 // Check if a PT_DYNAMIC segement was found
244 SysDebug(" elf_relocate: No PT_DYNAMIC segment in image %p, returning", Base);
245 return (void *)(intptr_t)(hdr->entrypoint + iBaseDiff);
248 // Adjust Dynamic Table
249 dynamicTab = (void *)( (intptr_t)dynamicTab + iBaseDiff );
251 // === Get Symbol table and String Table ===
252 for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)
254 switch(dynamicTab[j].d_tag)
256 // --- Symbol Table ---
258 DEBUGS(" elf_relocate: DYNAMIC Symbol Table 0x%x (0x%x)",
259 dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff);
260 if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
261 dynsymtab = (void*)(intptr_t)dynamicTab[j].d_val;
263 // --- String Table ---
265 DEBUGS(" elf_relocate: DYNAMIC String Table 0x%x (0x%x)",
266 dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff);
267 if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
268 dynstrtab = (void*)(intptr_t)dynamicTab[j].d_val;
272 if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
273 // iSymCount = ((Elf32_Word*)(intptr_t)dynamicTab[j].d_val)[1];
278 if(dynsymtab == NULL) {
279 SysDebug("ld-acess.so - WARNING: No Dynamic Symbol table in %p, returning", hdr);
280 return (void *)(intptr_t) (hdr->entrypoint + iBaseDiff);
283 // === Add to loaded list (can be imported now) ===
284 AddLoaded( Filename, Base );
286 // === Parse Relocation Data ===
287 DEBUGS(" elf_relocate: dynamicTab = 0x%x", dynamicTab);
288 for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)
290 switch(dynamicTab[j].d_tag)
292 // --- Shared Library Name ---
294 DEBUGS(" elf_relocate: .so Name '%s'", dynstrtab+dynamicTab[j].d_val);
296 // --- Needed Library ---
298 libPath = dynstrtab + dynamicTab[j].d_val;
299 DEBUGS(" Required Library '%s'", libPath);
300 if(LoadLibrary(libPath, NULL, envp) == 0) {
302 DEBUGS(" elf_relocate: Unable to load '%s'", libPath);
304 SysDebug("Unable to load required library '%s'", libPath);
308 DEBUGS(" Lib loaded");
311 // case DT_PLTGOT: pltgot = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
312 case DT_JMPREL: plt = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
313 case DT_PLTREL: pltType = dynamicTab[j].d_val; break;
314 case DT_PLTRELSZ: pltSz = dynamicTab[j].d_val; break;
316 // --- Relocation ---
317 case DT_REL: rel = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
318 case DT_RELSZ: relSz = dynamicTab[j].d_val; break;
319 case DT_RELENT: relEntSz = dynamicTab[j].d_val; break;
320 case DT_RELA: rela = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
321 case DT_RELASZ: relaSz = dynamicTab[j].d_val; break;
322 case DT_RELAENT: relaEntSz = dynamicTab[j].d_val; break;
324 // --- Symbol Table ---
326 // --- Hash Table ---
328 // --- String Table ---
334 if(dynamicTab[j].d_tag > DT_JMPREL) continue;
335 //DEBUGS(" elf_relocate: %i-%i = %s,0x%x",
336 // i,j, csaDT_NAMES[dynamicTab[j].d_tag],dynamicTab[j].d_val);
341 DEBUGS(" elf_relocate: Beginning Relocation");
343 void _doRelocate(uint32_t r_info, uint32_t *ptr, int bRela, Elf32_Addr addend)
345 int type = ELF32_R_TYPE(r_info);
346 int sym = ELF32_R_SYM(r_info);
347 const char *symname = dynstrtab + dynsymtab[sym].nameOfs;
348 do_relocate(r_info, ptr, addend, type, bRela, symname, iBaseDiff);
354 do_relocate = elf_doRelocate_386;
357 do_relocate = elf_doRelocate_arm;
360 SysDebug("Elf32Relocate: Unknown machine type %i", hdr->machine);
364 DEBUGS("do_relocate = %p (%p or %p)", do_relocate, &elf_doRelocate_386, &elf_doRelocate_arm);
366 // Parse Relocation Entries
370 DEBUGS(" elf_relocate: rel=0x%x, relSz=0x%x, relEntSz=0x%x", rel, relSz, relEntSz);
371 j = relSz / relEntSz;
372 for( i = 0; i < j; i++ )
374 //DEBUGS(" Rel %i: 0x%x+0x%x", i, iBaseDiff, rel[i].r_offset);
375 ptr = (void*)(iBaseDiff + rel[i].r_offset);
376 _doRelocate(rel[i].r_info, ptr, 0, *ptr);
379 // Parse Relocation Entries
383 DEBUGS(" elf_relocate: rela=0x%x, relaSz=0x%x, relaEntSz=0x%x", rela, relaSz, relaEntSz);
384 j = relaSz / relaEntSz;
385 for( i = 0; i < j; i++ )
387 ptr = (void*)(iBaseDiff + rela[i].r_offset);
388 _doRelocate(rel[i].r_info, ptr, 1, rela[i].r_addend);
392 // === Process PLT (Procedure Linkage Table) ===
396 DEBUGS(" elf_relocate: Relocate PLT, plt=0x%x", plt);
397 if(pltType == DT_REL)
399 Elf32_Rel *pltRel = plt;
400 j = pltSz / sizeof(Elf32_Rel);
401 DEBUGS(" elf_relocate: PLT Reloc Type = Rel, %i entries", j);
404 ptr = (void*)(iBaseDiff + pltRel[i].r_offset);
405 _doRelocate(pltRel[i].r_info, ptr, 0, *ptr);
410 Elf32_Rela *pltRela = plt;
411 j = pltSz / sizeof(Elf32_Rela);
412 DEBUGS(" elf_relocate: PLT Reloc Type = Rela, %i entries", j);
415 ptr = (void*)(iRealBase + pltRela[i].r_offset);
416 _doRelocate(pltRela[i].r_info, ptr, 1, pltRela[i].r_addend);
421 DEBUGS("ElfRelocate: RETURN 0x%x to %p", hdr->entrypoint + iBaseDiff, __builtin_return_address(0));
422 return (void*)(intptr_t)( hdr->entrypoint + iBaseDiff );
425 int Elf32GetSymbol(void *Base, const char *Name, void **ret, size_t *Size)
427 Elf32_Ehdr *hdr = Base;
428 Elf32_Sym *symtab = NULL;
430 Elf32_Word *pBuckets = NULL;
433 const char *dynstrtab = NULL;
434 uintptr_t iBaseDiff = -1;
436 Elf32_Dyn *dynTab = NULL;
440 phtab = (void*)( (uintptr_t)Base + hdr->phoff );
441 for( i = 0; i < hdr->phentcount; i ++ )
443 if(phtab[i].Type == PT_LOAD && iBaseDiff > phtab[i].VAddr)
444 iBaseDiff = phtab[i].VAddr;
445 if( phtab[i].Type == PT_DYNAMIC ) {
446 dynTab = (void*)(intptr_t)phtab[i].VAddr;
450 SysDebug("ERROR - Unable to find DYNAMIC segment in %p", Base);
453 iBaseDiff = (intptr_t)Base - iBaseDiff; // Make iBaseDiff actually the diff
454 dynTab = (void*)( (intptr_t)dynTab + iBaseDiff );
455 for( i = 0; dynTab[i].d_tag != DT_NULL; i++)
457 switch(dynTab[i].d_tag)
459 // --- Symbol Table ---
461 symtab = (void*)(intptr_t) dynTab[i].d_val; // Rebased in Relocate
464 dynstrtab = (void*)(intptr_t) dynTab[i].d_val;
468 pBuckets = (void*)(intptr_t) dynTab[i].d_val;
474 SysDebug("ERRO - No DT_SYMTAB in %p", Base);
478 SysDebug("ERRO - No DT_HASH in %p", Base);
482 SysDebug("ERRO - No DT_STRTAB in %p", Base);
486 // ... ok... maybe they haven't been relocated
487 if( (uintptr_t)symtab < (uintptr_t)Base )
489 symtab = (void*)( (uintptr_t)symtab + iBaseDiff );
490 pBuckets = (void*)( (uintptr_t)pBuckets + iBaseDiff );
491 dynstrtab = (void*)( (uintptr_t)dynstrtab + iBaseDiff );
492 SysDebug("Executable not yet relocated");
495 nbuckets = pBuckets[0];
496 // iSymCount = pBuckets[1];
497 pBuckets = &pBuckets[2];
498 pChains = &pBuckets[ nbuckets ];
501 iNameHash = ElfHashString(Name);
502 iNameHash %= nbuckets;
505 i = pBuckets[ iNameHash ];
506 if(symtab[i].shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].nameOfs, Name) == 0) {
507 *ret = (void*)( (uintptr_t) symtab[ i ].value + iBaseDiff );
508 if(Size) *Size = symtab[i].size;
512 while(pChains[i] != STN_UNDEF)
515 if(symtab[i].shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[ i ].nameOfs, Name) == 0) {
516 *ret = (void*)( (uintptr_t)symtab[ i ].value + iBaseDiff );
517 if(Size) *Size = symtab[i].size;
526 void *Elf64Relocate(void *Base, char **envp, const char *Filename)
529 Elf64_Ehdr *hdr = Base;
531 Elf64_Dyn *dyntab = NULL;
532 Elf64_Addr compiledBase = -1, baseDiff;
533 Elf64_Sym *symtab = NULL;
535 Elf64_Word *hashtab = NULL;
536 Elf64_Rel *rel = NULL;
538 Elf64_Rela *rela = NULL;
541 int plt_size = 0, plt_type = 0;
543 DEBUGS("Elf64Relocate: hdr = {");
544 DEBUGS("Elf64Relocate: e_ident = '%.16s'", hdr->e_ident);
545 DEBUGS("Elf64Relocate: e_type = 0x%x", hdr->e_type);
546 DEBUGS("Elf64Relocate: e_machine = 0x%x", hdr->e_machine);
547 DEBUGS("Elf64Relocate: e_version = 0x%x", hdr->e_version);
548 DEBUGS("Elf64Relocate: e_entry = %p", hdr->e_entry);
549 DEBUGS("Elf64Relocate: e_phoff = 0x%llx", hdr->e_phoff);
550 DEBUGS("Elf64Relocate: e_shoff = 0x%llx", hdr->e_shoff);
551 DEBUGS("Elf64Relocate: e_flags = 0x%x", hdr->e_flags);
552 DEBUGS("Elf64Relocate: e_ehsize = 0x%x", hdr->e_ehsize);
553 DEBUGS("Elf64Relocate: e_phentsize = 0x%x", hdr->e_phentsize);
554 DEBUGS("Elf64Relocate: e_phnum = %i", hdr->e_phnum);
556 // Scan for the dynamic table (and find the compiled base)
557 phtab = (void*)((uintptr_t)Base + hdr->e_phoff);
558 for( i = 0; i < hdr->e_phnum; i ++ )
560 if(phtab[i].p_type == PT_DYNAMIC)
561 dyntab = (void *)(intptr_t)phtab[i].p_vaddr;
562 if(phtab[i].p_type == PT_LOAD && compiledBase > phtab[i].p_vaddr)
563 compiledBase = phtab[i].p_vaddr;
566 baseDiff = (uintptr_t)Base - compiledBase;
568 DEBUGS("baseDiff = %p", baseDiff);
571 SysDebug(" Elf64Relocate: No PT_DYNAMIC segment in image %p, returning", Base);
572 return (void *)(uintptr_t)(hdr->e_entry + baseDiff);
575 dyntab = (void *)(uintptr_t)((uintptr_t)dyntab + baseDiff);
577 // Parse the dynamic table (first pass)
578 // - Search for String, Symbol and Hash tables
579 for(i = 0; dyntab[i].d_tag != DT_NULL; i ++)
581 switch(dyntab[i].d_tag)
584 dyntab[i].d_un.d_ptr += baseDiff;
585 symtab = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
588 dyntab[i].d_un.d_ptr += baseDiff;
589 strtab = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
592 dyntab[i].d_un.d_ptr += baseDiff;
593 hashtab = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
598 if( !symtab || !strtab || !hashtab ) {
599 SysDebug("ld-acess - Elf64Relocate: Missing Symbol, string or hash table");
603 // Ready for symbol use
604 AddLoaded( Filename, Base );
606 // Second pass on dynamic table
607 for(i = 0; dyntab[i].d_tag != DT_NULL; i ++)
609 DEBUGS("dyntab[%i].d_tag = %i", i, dyntab[i].d_tag);
610 switch(dyntab[i].d_tag)
612 case DT_SONAME: break;
615 char *libPath = strtab + dyntab[i].d_un.d_val;
616 DEBUGS("Elf64Relocate: libPath = '%s'", libPath);
617 if(LoadLibrary(libPath, NULL, envp) == 0) {
618 SysDebug("ld-acess - Elf64Relocate: Unable to load '%s'", libPath);
623 // Relocation entries
625 dyntab[i].d_un.d_ptr += baseDiff;
626 rel = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
629 rel_count = dyntab[i].d_un.d_val / sizeof(Elf64_Rel);
632 if( dyntab[i].d_un.d_val != sizeof(Elf64_Rel) ) {
633 SysDebug("ld-acess - Elf64Relocate: DT_RELENT(%i) != sizeof(Elf64_Rel)(%i)",
634 dyntab[i].d_un.d_val, sizeof(Elf64_Rel));
639 dyntab[i].d_un.d_ptr += baseDiff;
640 rela = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
643 rela_count = dyntab[i].d_un.d_val / sizeof(Elf64_Rela);
646 if( dyntab[i].d_un.d_val != sizeof(Elf64_Rela) ) {
647 SysDebug("ld-acess - Elf64Relocate: DT_RELAENT(%i) != sizeof(Elf64_Rela)(%i)",
648 dyntab[i].d_un.d_val, sizeof(Elf64_Rela));
653 dyntab[i].d_un.d_ptr += baseDiff;
654 pltrel = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
657 plt_type = dyntab[i].d_un.d_val;
660 plt_size = dyntab[i].d_un.d_val;
665 // Relocation function
666 void _Elf64DoReloc(Elf64_Xword r_info, void *ptr, Elf64_Sxword addend)
668 int sym = ELF64_R_SYM(r_info);
669 int type = ELF64_R_TYPE(r_info);
670 const char *symname = strtab + symtab[sym].st_name;
676 *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL) + addend;
678 case R_X86_64_COPY: {
680 void *symptr = GetSymbol(symname, &size);
681 memcpy(ptr, symptr, size);
683 case R_X86_64_GLOB_DAT:
684 *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL);
686 case R_X86_64_JUMP_SLOT:
687 *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL);
689 case R_X86_64_RELATIVE:
690 *(uint64_t*)ptr = (uintptr_t)Base + addend;
693 SysDebug("ld-acess - _Elf64DoReloc: Unknown relocation type %i", type);
700 DEBUGS("rel_count = %i", rel_count);
701 for( i = 0; i < rel_count; i ++ )
703 uint64_t *ptr = (void *)(uintptr_t)( rel[i].r_offset + baseDiff );
704 _Elf64DoReloc( rel[i].r_info, ptr, *ptr);
710 DEBUGS("rela_count = %i", rela_count);
711 for( i = 0; i < rela_count; i ++ )
713 uint64_t *ptr = (void *)(uintptr_t)( rela[i].r_offset + baseDiff );
714 _Elf64DoReloc( rela[i].r_info, ptr, rela[i].r_addend );
718 if( pltrel && plt_type )
720 if( plt_type == DT_REL ) {
721 Elf64_Rel *plt = pltrel;
722 int count = plt_size / sizeof(Elf64_Rel);
723 DEBUGS("plt rel count = %i", count);
724 for( i = 0; i < count; i ++ )
726 uint64_t *ptr = (void *)(uintptr_t)( plt[i].r_offset + baseDiff );
727 _Elf64DoReloc( plt[i].r_info, ptr, *ptr);
731 Elf64_Rela *plt = pltrel;
732 int count = plt_size / sizeof(Elf64_Rela);
733 DEBUGS("plt rela count = %i", count);
734 for( i = 0; i < count; i ++ )
736 uint64_t *ptr = (void *)(uintptr_t)( plt[i].r_offset + baseDiff );
737 _Elf64DoReloc( plt[i].r_info, ptr, plt[i].r_addend);
743 void *ret = (void *)(uintptr_t)(hdr->e_entry + baseDiff);
744 DEBUGS("Elf64Relocate: Relocations done, return %p", ret);
749 int Elf64GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size)
751 Elf64_Ehdr *hdr = Base;
754 // int iSymCount = 0;
756 Elf64_Word *pBuckets;
759 const char *dynstrtab;
760 uintptr_t iBaseDiff = -1;
766 // Catch the current executable
770 Elf64_Dyn *dynTab = NULL;
774 phtab = (void*)( (intptr_t)Base + hdr->e_phoff );
775 for( i = 0; i < hdr->e_phnum; i ++ )
777 if(phtab[i].p_type == PT_LOAD && iBaseDiff > phtab[i].p_vaddr)
778 iBaseDiff = phtab[i].p_vaddr;
779 if( phtab[i].p_type == PT_DYNAMIC ) {
780 dynTab = (void*)(intptr_t)phtab[i].p_vaddr;
784 SysDebug("ERROR - Unable to find DYNAMIC segment in %p", Base);
787 iBaseDiff = (intptr_t)Base - iBaseDiff; // Make iBaseDiff actually the diff
788 dynTab = (void*)( (intptr_t)dynTab + iBaseDiff );
790 for( j = 0; dynTab[j].d_tag != DT_NULL; j++)
792 switch(dynTab[j].d_tag)
794 // --- Symbol Table ---
796 symtab = (void*)(intptr_t) dynTab[j].d_un.d_val; // Rebased in Relocate
799 dynstrtab = (void*)(intptr_t) dynTab[j].d_un.d_val;
803 pBuckets = (void*)(intptr_t) dynTab[j].d_un.d_val;
809 nbuckets = pBuckets[0];
810 // iSymCount = pBuckets[1];
811 pBuckets = &pBuckets[2];
812 pChains = &pBuckets[ nbuckets ];
815 iNameHash = ElfHashString(Name);
816 iNameHash %= nbuckets;
819 i = pBuckets[ iNameHash ];
820 if(symtab[i].st_shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].st_name, Name) == 0) {
821 *Ret = (void*)( (intptr_t)symtab[i].st_value + iBaseDiff );
822 if(Size) *Size = symtab[i].st_size;
823 DEBUGS("%s = %p", Name, *Ret);
827 while(pChains[i] != STN_UNDEF)
830 if(symtab[i].st_shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].st_name, Name) == 0) {
831 *Ret = (void*)((intptr_t)symtab[i].st_value + iBaseDiff);
832 if(Size) *Size = symtab[i].st_size;
833 DEBUGS("%s = %p", Name, *Ret);
843 uint32_t ElfHashString(const char *name)
848 h = (h << 4) + *(uint8_t*)name++;
849 if( (g = h & 0xf0000000) )