2 * Acess2 Dynamic Linker
3 * - By John Hodge (thePowersGang)
6 * - ELF32/ELF64 relocation
16 # define DEBUGS(v...) SysDebug("ld-acess - " v)
22 # define SUPPORT_ELF64
27 //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"};
28 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"};
32 void *ElfRelocate(void *Base, char **envp, const char *Filename);
33 int ElfGetSymbol(void *Base, const char *Name, void **Ret, size_t *Size);
34 void *Elf32Relocate(void *Base, char **envp, const char *Filename);
35 int Elf32GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size);
37 void *Elf64Relocate(void *Base, char **envp, const char *Filename);
38 int Elf64GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size);
40 uint32_t ElfHashString(const char *name);
44 * \fn int ElfRelocate(void *Base, char **envp, const char *Filename)
45 * \brief Relocates a loaded ELF Executable
47 void *ElfRelocate(void *Base, char **envp, const char *Filename)
49 Elf32_Ehdr *hdr = Base;
51 switch(hdr->e_ident[4])
54 return Elf32Relocate(Base, envp, Filename);
57 return Elf64Relocate(Base, envp, Filename);
60 SysDebug("ld-acess - ElfRelocate: Unknown file class %i", hdr->e_ident[4]);
66 * \fn int ElfGetSymbol(Uint Base, const char *name, void **ret)
68 int ElfGetSymbol(void *Base, const char *Name, void **ret, size_t *Size)
70 Elf32_Ehdr *hdr = Base;
72 switch(hdr->e_ident[4])
75 return Elf32GetSymbol(Base, Name, ret, Size);
78 return Elf64GetSymbol(Base, Name, ret, Size);
81 SysDebug("ld-acess - ElfRelocate: Unknown file class %i", hdr->e_ident[4]);
86 void elf_doRelocate_386(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff)
91 // Standard 32 Bit Relocation (S+A)
93 val = (intptr_t) GetSymbol(Sym, NULL);
94 DEBUGS(" elf_doRelocate: R_386_32 *0x%x += 0x%x('%s')",
99 // 32 Bit Relocation wrt. Offset (S+A-P)
101 DEBUGS(" elf_doRelocate: '%s'", Sym);
102 val = (intptr_t) GetSymbol(Sym, NULL);
103 DEBUGS(" elf_doRelocate: R_386_PC32 *0x%x = 0x%x + 0x%x - 0x%x",
104 ptr, *ptr, val, (intptr_t)ptr );
105 *ptr = val + addend - (intptr_t)ptr;
106 //*ptr = val + addend - ((Uint)ptr - iBaseDiff);
109 // Absolute Value of a symbol (S)
112 DEBUGS(" elf_doRelocate: '%s'", Sym);
113 val = (intptr_t) GetSymbol( Sym, NULL );
114 DEBUGS(" elf_doRelocate: %s *0x%x = 0x%x", csaR_NAMES[type], ptr, val);
118 // Base Address (B+A)
120 DEBUGS(" elf_doRelocate: R_386_RELATIVE *0x%x = 0x%x + 0x%x", ptr, iBaseDiff, addend);
121 *ptr = iBaseDiff + addend;
126 void *src = GetSymbol(Sym, &size);
127 DEBUGS(" elf_doRelocate_386: R_386_COPY (%p, %p, %i)", ptr, src, size);
128 memcpy(ptr, src, size);
132 SysDebug("elf_doRelocate_386: Unknown relocation %i", type);
137 void elf_doRelocate_arm(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff)
144 DEBUGS(" elf_doRelocate_arm: R_ARM_ABS32 %p (%s + %x)", ptr, Sym, addend);
145 val = (intptr_t)GetSymbol(Sym, NULL);
149 DEBUGS(" elf_doRelocate_arm: R_ARM_GLOB_DAT %p (%s + %x)", ptr, Sym, addend);
150 val = (intptr_t)GetSymbol(Sym, NULL);
153 case R_ARM_JUMP_SLOT:
154 if(!bRela) addend = 0;
155 DEBUGS(" elf_doRelocate_arm: R_ARM_JUMP_SLOT %p (%s + %x)", ptr, Sym, addend);
156 val = (intptr_t)GetSymbol(Sym, NULL);
162 void *src = GetSymbol(Sym, &size);
163 DEBUGS(" elf_doRelocate_arm: R_ARM_COPY (%p, %p, %i)", ptr, src, size);
164 memcpy(ptr, src, size);
166 // Delta between link and runtime locations + A
169 // TODO: Get delta for a symbol
170 SysDebug("elf_doRelocate_arm: TODO - Implment R_ARM_RELATIVE for symbols");
173 *ptr = iBaseDiff + addend;
177 SysDebug("elf_doRelocate_arm: Unknown Relocation, %i", type);
182 void *Elf32Relocate(void *Base, char **envp, const char *Filename)
184 Elf32_Ehdr *hdr = Base;
186 int i, j; // Counters
188 intptr_t iRealBase = -1;
192 Elf32_Rel *rel = NULL;
193 Elf32_Rela *rela = NULL;
195 int relSz=0, relEntSz=8;
196 int relaSz=0, relaEntSz=8;
197 int pltSz=0, pltType=0;
198 Elf32_Dyn *dynamicTab = NULL; // Dynamic Table Pointer
199 char *dynstrtab = NULL; // .dynamic String Table
200 Elf32_Sym *dynsymtab;
201 void (*do_relocate)(uint32_t t_info, uint32_t *ptr, Elf32_Addr addend, int Type, int bRela, const char *Sym, intptr_t iBaseDiff);
203 DEBUGS("ElfRelocate: (Base=0x%x)", Base);
205 // Check magic header
208 // Parse Program Header to get Dynamic Table
209 phtab = Base + hdr->phoff;
210 iSegmentCount = hdr->phentcount;
211 for(i=0;i<iSegmentCount;i++)
213 // Determine linked base address
214 if(phtab[i].Type == PT_LOAD && iRealBase > phtab[i].VAddr)
215 iRealBase = phtab[i].VAddr;
217 // Find Dynamic Section
218 if(phtab[i].Type == PT_DYNAMIC) {
220 DEBUGS(" WARNING - elf_relocate: Multiple PT_DYNAMIC segments");
223 dynamicTab = (void *) (intptr_t) phtab[i].VAddr;
224 j = i; // Save Dynamic Table ID
228 // Page Align real base
230 DEBUGS(" elf_relocate: True Base = 0x%x, Compiled Base = 0x%x", Base, iRealBase);
232 // Adjust "Real" Base
233 iBaseDiff = (intptr_t)Base - iRealBase;
235 // hdr->entrypoint += iBaseDiff; // Adjust Entrypoint
237 // Check if a PT_DYNAMIC segement was found
239 SysDebug(" elf_relocate: No PT_DYNAMIC segment in image %p, returning", Base);
240 return (void *)(intptr_t)(hdr->entrypoint + iBaseDiff);
243 // Adjust Dynamic Table
244 dynamicTab = (void *)( (intptr_t)dynamicTab + iBaseDiff );
246 // === Get Symbol table and String Table ===
247 for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)
249 switch(dynamicTab[j].d_tag)
251 // --- Symbol Table ---
253 DEBUGS(" elf_relocate: DYNAMIC Symbol Table 0x%x (0x%x)",
254 dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff);
255 if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
256 dynsymtab = (void*)(intptr_t)dynamicTab[j].d_val;
258 // --- String Table ---
260 DEBUGS(" elf_relocate: DYNAMIC String Table 0x%x (0x%x)",
261 dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff);
262 if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
263 dynstrtab = (void*)(intptr_t)dynamicTab[j].d_val;
267 if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
268 // iSymCount = ((Elf32_Word*)(intptr_t)dynamicTab[j].d_val)[1];
273 if(dynsymtab == NULL) {
274 SysDebug("ld-acess.so - WARNING: No Dynamic Symbol table in %p, returning", hdr);
275 return (void *)(intptr_t) (hdr->entrypoint + iBaseDiff);
278 // === Add to loaded list (can be imported now) ===
279 AddLoaded( Filename, Base );
281 // === Parse Relocation Data ===
282 DEBUGS(" elf_relocate: dynamicTab = 0x%x", dynamicTab);
283 for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)
285 switch(dynamicTab[j].d_tag)
287 // --- Shared Library Name ---
289 DEBUGS(" elf_relocate: .so Name '%s'", dynstrtab+dynamicTab[j].d_val);
291 // --- Needed Library ---
293 libPath = dynstrtab + dynamicTab[j].d_val;
294 DEBUGS(" Required Library '%s'", libPath);
295 if(LoadLibrary(libPath, NULL, envp) == 0) {
297 DEBUGS(" elf_relocate: Unable to load '%s'", libPath);
299 SysDebug("Unable to load required library '%s'", libPath);
303 DEBUGS(" Lib loaded");
306 // case DT_PLTGOT: pltgot = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
307 case DT_JMPREL: plt = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
308 case DT_PLTREL: pltType = dynamicTab[j].d_val; break;
309 case DT_PLTRELSZ: pltSz = dynamicTab[j].d_val; break;
311 // --- Relocation ---
312 case DT_REL: rel = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
313 case DT_RELSZ: relSz = dynamicTab[j].d_val; break;
314 case DT_RELENT: relEntSz = dynamicTab[j].d_val; break;
315 case DT_RELA: rela = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
316 case DT_RELASZ: relaSz = dynamicTab[j].d_val; break;
317 case DT_RELAENT: relaEntSz = dynamicTab[j].d_val; break;
319 // --- Symbol Table ---
321 // --- Hash Table ---
323 // --- String Table ---
329 if(dynamicTab[j].d_tag > DT_JMPREL) continue;
330 //DEBUGS(" elf_relocate: %i-%i = %s,0x%x",
331 // i,j, csaDT_NAMES[dynamicTab[j].d_tag],dynamicTab[j].d_val);
336 DEBUGS(" elf_relocate: Beginning Relocation");
338 void _doRelocate(uint32_t r_info, uint32_t *ptr, int bRela, Elf32_Addr addend)
340 int type = ELF32_R_TYPE(r_info);
341 int sym = ELF32_R_SYM(r_info);
342 const char *symname = dynstrtab + dynsymtab[sym].nameOfs;
343 do_relocate(r_info, ptr, addend, type, bRela, symname, iBaseDiff);
349 do_relocate = elf_doRelocate_386;
352 do_relocate = elf_doRelocate_arm;
355 SysDebug("Elf32Relocate: Unknown machine type %i", hdr->machine);
359 DEBUGS("do_relocate = %p (%p or %p)", do_relocate, &elf_doRelocate_386, &elf_doRelocate_arm);
361 // Parse Relocation Entries
365 DEBUGS(" elf_relocate: rel=0x%x, relSz=0x%x, relEntSz=0x%x", rel, relSz, relEntSz);
366 j = relSz / relEntSz;
367 for( i = 0; i < j; i++ )
369 //DEBUGS(" Rel %i: 0x%x+0x%x", i, iBaseDiff, rel[i].r_offset);
370 ptr = (void*)(iBaseDiff + rel[i].r_offset);
371 _doRelocate(rel[i].r_info, ptr, 0, *ptr);
374 // Parse Relocation Entries
378 DEBUGS(" elf_relocate: rela=0x%x, relaSz=0x%x, relaEntSz=0x%x", rela, relaSz, relaEntSz);
379 j = relaSz / relaEntSz;
380 for( i = 0; i < j; i++ )
382 ptr = (void*)(iBaseDiff + rela[i].r_offset);
383 _doRelocate(rel[i].r_info, ptr, 1, rela[i].r_addend);
387 // === Process PLT (Procedure Linkage Table) ===
391 DEBUGS(" elf_relocate: Relocate PLT, plt=0x%x", plt);
392 if(pltType == DT_REL)
394 Elf32_Rel *pltRel = plt;
395 j = pltSz / sizeof(Elf32_Rel);
396 DEBUGS(" elf_relocate: PLT Reloc Type = Rel, %i entries", j);
399 ptr = (void*)(iBaseDiff + pltRel[i].r_offset);
400 _doRelocate(pltRel[i].r_info, ptr, 0, *ptr);
405 Elf32_Rela *pltRela = plt;
406 j = pltSz / sizeof(Elf32_Rela);
407 DEBUGS(" elf_relocate: PLT Reloc Type = Rela, %i entries", j);
410 ptr = (void*)(iRealBase + pltRela[i].r_offset);
411 _doRelocate(pltRela[i].r_info, ptr, 1, pltRela[i].r_addend);
416 DEBUGS("ElfRelocate: RETURN 0x%x to %p", hdr->entrypoint + iBaseDiff, __builtin_return_address(0));
417 return (void*)(intptr_t)( hdr->entrypoint + iBaseDiff );
420 int Elf32GetSymbol(void *Base, const char *Name, void **ret, size_t *Size)
422 Elf32_Ehdr *hdr = Base;
425 // int iSymCount = 0;
427 Elf32_Word *pBuckets;
430 const char *dynstrtab;
431 uintptr_t iBaseDiff = -1;
433 Elf32_Dyn *dynTab = NULL;
436 phtab = (void*)( Base + hdr->phoff );
437 for( i = 0; i < hdr->phentcount; i ++ )
439 if(phtab[i].Type == PT_LOAD && iBaseDiff > phtab[i].VAddr)
440 iBaseDiff = phtab[i].VAddr;
441 if( phtab[i].Type == PT_DYNAMIC ) {
442 dynTab = (void*)(intptr_t)phtab[i].VAddr;
446 SysDebug("ERROR - Unable to find DYNAMIC segment in %p", Base);
449 iBaseDiff = (intptr_t)Base - iBaseDiff; // Make iBaseDiff actually the diff
450 dynTab = (void*)( (intptr_t)dynTab + iBaseDiff );
451 for( i = 0; dynTab[i].d_tag != DT_NULL; i++)
453 switch(dynTab[i].d_tag)
455 // --- Symbol Table ---
457 symtab = (void*)(intptr_t) dynTab[i].d_val; // Rebased in Relocate
460 dynstrtab = (void*)(intptr_t) dynTab[i].d_val;
464 pBuckets = (void*)(intptr_t) dynTab[i].d_val;
469 nbuckets = pBuckets[0];
470 // iSymCount = pBuckets[1];
471 pBuckets = &pBuckets[2];
472 pChains = &pBuckets[ nbuckets ];
475 iNameHash = ElfHashString(Name);
476 iNameHash %= nbuckets;
479 i = pBuckets[ iNameHash ];
480 if(symtab[i].shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].nameOfs, Name) == 0) {
481 *ret = (void*) (intptr_t) symtab[ i ].value + iBaseDiff;
482 if(Size) *Size = symtab[i].size;
486 while(pChains[i] != STN_UNDEF)
489 if(symtab[i].shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[ i ].nameOfs, Name) == 0) {
490 *ret = (void*)(intptr_t)symtab[ i ].value + iBaseDiff;
491 if(Size) *Size = symtab[i].size;
500 void *Elf64Relocate(void *Base, char **envp, const char *Filename)
503 Elf64_Ehdr *hdr = Base;
506 Elf64_Addr compiledBase = -1, baseDiff;
507 Elf64_Sym *symtab = NULL;
509 Elf64_Word *hashtab = NULL;
510 Elf64_Rel *rel = NULL;
512 Elf64_Rela *rela = NULL;
515 int plt_size = 0, plt_type = 0;
517 DEBUGS("Elf64Relocate: hdr = {");
518 DEBUGS("Elf64Relocate: e_ident = '%.16s'", hdr->e_ident);
519 DEBUGS("Elf64Relocate: e_type = 0x%x", hdr->e_type);
520 DEBUGS("Elf64Relocate: e_machine = 0x%x", hdr->e_machine);
521 DEBUGS("Elf64Relocate: e_version = 0x%x", hdr->e_version);
522 DEBUGS("Elf64Relocate: e_entry = %p", hdr->e_entry);
523 DEBUGS("Elf64Relocate: e_phoff = 0x%llx", hdr->e_phoff);
524 DEBUGS("Elf64Relocate: e_shoff = 0x%llx", hdr->e_shoff);
525 DEBUGS("Elf64Relocate: e_flags = 0x%x", hdr->e_flags);
526 DEBUGS("Elf64Relocate: e_ehsize = 0x%x", hdr->e_ehsize);
527 DEBUGS("Elf64Relocate: e_phentsize = 0x%x", hdr->e_phentsize);
528 DEBUGS("Elf64Relocate: e_phnum = %i", hdr->e_phnum);
530 // Scan for the dynamic table (and find the compiled base)
531 phtab = Base + hdr->e_phoff;
532 for( i = 0; i < hdr->e_phnum; i ++ )
534 if(phtab[i].p_type == PT_DYNAMIC)
535 dyntab = (void *)(intptr_t)phtab[i].p_vaddr;
536 if(phtab[i].p_type == PT_LOAD && compiledBase > phtab[i].p_vaddr)
537 compiledBase = phtab[i].p_vaddr;
540 baseDiff = (uintptr_t)Base - compiledBase;
542 DEBUGS("baseDiff = %p", baseDiff);
545 SysDebug(" Elf64Relocate: No PT_DYNAMIC segment in image %p, returning", Base);
546 return (void *)(uintptr_t)(hdr->e_entry + baseDiff);
549 dyntab = (void *)(uintptr_t)((uintptr_t)dyntab + baseDiff);
551 // Parse the dynamic table (first pass)
552 // - Search for String, Symbol and Hash tables
553 for(i = 0; dyntab[i].d_tag != DT_NULL; i ++)
555 switch(dyntab[i].d_tag)
558 dyntab[i].d_un.d_ptr += baseDiff;
559 symtab = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
562 dyntab[i].d_un.d_ptr += baseDiff;
563 strtab = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
566 dyntab[i].d_un.d_ptr += baseDiff;
567 hashtab = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
572 if( !symtab || !strtab || !hashtab ) {
573 SysDebug("ld-acess - Elf64Relocate: Missing Symbol, string or hash table");
577 // Ready for symbol use
578 AddLoaded( Filename, Base );
580 // Second pass on dynamic table
581 for(i = 0; dyntab[i].d_tag != DT_NULL; i ++)
583 DEBUGS("dyntab[%i].d_tag = %i", i, dyntab[i].d_tag);
584 switch(dyntab[i].d_tag)
586 case DT_SONAME: break;
589 char *libPath = strtab + dyntab[i].d_un.d_val;
590 DEBUGS("Elf64Relocate: libPath = '%s'", libPath);
591 if(LoadLibrary(libPath, NULL, envp) == 0) {
592 SysDebug("ld-acess - Elf64Relocate: Unable to load '%s'", libPath);
597 // Relocation entries
599 dyntab[i].d_un.d_ptr += baseDiff;
600 rel = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
603 rel_count = dyntab[i].d_un.d_val / sizeof(Elf64_Rel);
606 if( dyntab[i].d_un.d_val != sizeof(Elf64_Rel) ) {
607 SysDebug("ld-acess - Elf64Relocate: DT_RELENT(%i) != sizeof(Elf64_Rel)(%i)",
608 dyntab[i].d_un.d_val, sizeof(Elf64_Rel));
613 dyntab[i].d_un.d_ptr += baseDiff;
614 rela = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
617 rela_count = dyntab[i].d_un.d_val / sizeof(Elf64_Rela);
620 if( dyntab[i].d_un.d_val != sizeof(Elf64_Rela) ) {
621 SysDebug("ld-acess - Elf64Relocate: DT_RELAENT(%i) != sizeof(Elf64_Rela)(%i)",
622 dyntab[i].d_un.d_val, sizeof(Elf64_Rela));
627 dyntab[i].d_un.d_ptr += baseDiff;
628 pltrel = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
631 plt_type = dyntab[i].d_un.d_val;
634 plt_size = dyntab[i].d_un.d_val;
639 // Relocation function
640 void _Elf64DoReloc(Elf64_Xword r_info, void *ptr, Elf64_Sxword addend)
642 int sym = ELF64_R_SYM(r_info);
643 int type = ELF64_R_TYPE(r_info);
644 const char *symname = strtab + symtab[sym].st_name;
650 *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL) + addend;
652 case R_X86_64_COPY: {
654 void *sym = GetSymbol(symname, &size);
655 memcpy(ptr, sym, size);
657 case R_X86_64_GLOB_DAT:
658 *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL);
660 case R_X86_64_JUMP_SLOT:
661 *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL);
663 case R_X86_64_RELATIVE:
664 *(uint64_t*)ptr = (uintptr_t)Base + addend;
667 SysDebug("ld-acess - _Elf64DoReloc: Unknown relocation type %i", type);
674 DEBUGS("rel_count = %i", rel_count);
675 for( i = 0; i < rel_count; i ++ )
677 uint64_t *ptr = (void *)(uintptr_t)( rel[i].r_offset + baseDiff );
678 _Elf64DoReloc( rel[i].r_info, ptr, *ptr);
684 DEBUGS("rela_count = %i", rela_count);
685 for( i = 0; i < rela_count; i ++ )
687 uint64_t *ptr = (void *)(uintptr_t)( rela[i].r_offset + baseDiff );
688 _Elf64DoReloc( rela[i].r_info, ptr, rela[i].r_addend );
692 if( pltrel && plt_type )
694 if( plt_type == DT_REL ) {
695 Elf64_Rel *plt = pltrel;
696 int count = plt_size / sizeof(Elf64_Rel);
697 DEBUGS("plt rel count = %i", count);
698 for( i = 0; i < count; i ++ )
700 uint64_t *ptr = (void *)(uintptr_t)( plt[i].r_offset + baseDiff );
701 _Elf64DoReloc( plt[i].r_info, ptr, *ptr);
705 Elf64_Rela *plt = pltrel;
706 int count = plt_size / sizeof(Elf64_Rela);
707 DEBUGS("plt rela count = %i", count);
708 for( i = 0; i < count; i ++ )
710 uint64_t *ptr = (void *)(uintptr_t)( plt[i].r_offset + baseDiff );
711 _Elf64DoReloc( plt[i].r_info, ptr, plt[i].r_addend);
717 void *ret = (void *)(uintptr_t)(hdr->e_entry + baseDiff);
718 DEBUGS("Elf64Relocate: Relocations done, return %p", ret);
723 int Elf64GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size)
725 Elf64_Ehdr *hdr = Base;
728 // int iSymCount = 0;
730 Elf64_Word *pBuckets;
733 const char *dynstrtab;
734 uintptr_t iBaseDiff = -1;
740 // Catch the current executable
744 Elf64_Dyn *dynTab = NULL;
748 phtab = (void*)( Base + hdr->e_phoff );
749 for( i = 0; i < hdr->e_phnum; i ++ )
751 if(phtab[i].p_type == PT_LOAD && iBaseDiff > phtab[i].p_vaddr)
752 iBaseDiff = phtab[i].p_vaddr;
753 if( phtab[i].p_type == PT_DYNAMIC ) {
754 dynTab = (void*)(intptr_t)phtab[i].p_vaddr;
758 SysDebug("ERROR - Unable to find DYNAMIC segment in %p", Base);
761 iBaseDiff = (intptr_t)Base - iBaseDiff; // Make iBaseDiff actually the diff
762 dynTab = (void*)( (intptr_t)dynTab + iBaseDiff );
764 for( j = 0; dynTab[j].d_tag != DT_NULL; j++)
766 switch(dynTab[j].d_tag)
768 // --- Symbol Table ---
770 symtab = (void*)(intptr_t) dynTab[j].d_un.d_val; // Rebased in Relocate
773 dynstrtab = (void*)(intptr_t) dynTab[j].d_un.d_val;
777 pBuckets = (void*)(intptr_t) dynTab[j].d_un.d_val;
783 nbuckets = pBuckets[0];
784 // iSymCount = pBuckets[1];
785 pBuckets = &pBuckets[2];
786 pChains = &pBuckets[ nbuckets ];
789 iNameHash = ElfHashString(Name);
790 iNameHash %= nbuckets;
793 i = pBuckets[ iNameHash ];
794 if(symtab[i].st_shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].st_name, Name) == 0) {
795 *Ret = (void*) (intptr_t) symtab[i].st_value + iBaseDiff;
796 if(Size) *Size = symtab[i].st_size;
797 DEBUGS("%s = %p", Name, *Ret);
801 while(pChains[i] != STN_UNDEF)
804 if(symtab[i].st_shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].st_name, Name) == 0) {
805 *Ret = (void*)(intptr_t)symtab[i].st_value + iBaseDiff;
806 if(Size) *Size = symtab[i].st_size;
807 DEBUGS("%s = %p", Name, *Ret);
817 uint32_t ElfHashString(const char *name)
822 h = (h << 4) + *(uint8_t*)name++;
823 if( (g = h & 0xf0000000) )