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 *Elf32Relocate(void *Base, char **envp, const char *Filename)
88 Elf32_Ehdr *hdr = Base;
92 intptr_t iRealBase = -1;
96 Elf32_Rel *rel = NULL;
97 Elf32_Rela *rela = NULL;
99 int relSz=0, relEntSz=8;
100 int relaSz=0, relaEntSz=8;
101 int pltSz=0, pltType=0;
102 Elf32_Dyn *dynamicTab = NULL; // Dynamic Table Pointer
103 char *dynstrtab = NULL; // .dynamic String Table
104 Elf32_Sym *dynsymtab;
105 void (*do_relocate)(uint32_t t_info, uint32_t *ptr, Elf32_Addr addend, int Type, int bRela, const char *Sym);
107 DEBUGS("ElfRelocate: (Base=0x%x)", Base);
109 // Check magic header
112 // Parse Program Header to get Dynamic Table
113 phtab = Base + hdr->phoff;
114 iSegmentCount = hdr->phentcount;
115 for(i=0;i<iSegmentCount;i++)
117 // Determine linked base address
118 if(phtab[i].Type == PT_LOAD && iRealBase > phtab[i].VAddr)
119 iRealBase = phtab[i].VAddr;
121 // Find Dynamic Section
122 if(phtab[i].Type == PT_DYNAMIC) {
124 DEBUGS(" WARNING - elf_relocate: Multiple PT_DYNAMIC segments");
127 dynamicTab = (void *) (intptr_t) phtab[i].VAddr;
128 j = i; // Save Dynamic Table ID
132 // Page Align real base
134 DEBUGS(" elf_relocate: True Base = 0x%x, Compiled Base = 0x%x", Base, iRealBase);
136 // Adjust "Real" Base
137 iBaseDiff = (intptr_t)Base - iRealBase;
139 // hdr->entrypoint += iBaseDiff; // Adjust Entrypoint
141 // Check if a PT_DYNAMIC segement was found
143 SysDebug(" elf_relocate: No PT_DYNAMIC segment in image %p, returning", Base);
144 return (void *)(intptr_t)(hdr->entrypoint + iBaseDiff);
147 // Adjust Dynamic Table
148 dynamicTab = (void *)( (intptr_t)dynamicTab + iBaseDiff );
150 // === Get Symbol table and String Table ===
151 for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)
153 switch(dynamicTab[j].d_tag)
155 // --- Symbol Table ---
157 DEBUGS(" elf_relocate: DYNAMIC Symbol Table 0x%x (0x%x)",
158 dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff);
159 if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
160 dynsymtab = (void*)(intptr_t)dynamicTab[j].d_val;
162 // --- String Table ---
164 DEBUGS(" elf_relocate: DYNAMIC String Table 0x%x (0x%x)",
165 dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff);
166 if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
167 dynstrtab = (void*)(intptr_t)dynamicTab[j].d_val;
171 if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
172 // iSymCount = ((Elf32_Word*)(intptr_t)dynamicTab[j].d_val)[1];
177 if(dynsymtab == NULL) {
178 SysDebug("ld-acess.so - WARNING: No Dynamic Symbol table in %p, returning", hdr);
179 return (void *)(intptr_t) (hdr->entrypoint + iBaseDiff);
182 // === Add to loaded list (can be imported now) ===
183 AddLoaded( Filename, Base );
185 // === Parse Relocation Data ===
186 DEBUGS(" elf_relocate: dynamicTab = 0x%x", dynamicTab);
187 for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)
189 switch(dynamicTab[j].d_tag)
191 // --- Shared Library Name ---
193 DEBUGS(" elf_relocate: .so Name '%s'", dynstrtab+dynamicTab[j].d_val);
195 // --- Needed Library ---
197 libPath = dynstrtab + dynamicTab[j].d_val;
198 DEBUGS(" Required Library '%s'", libPath);
199 if(LoadLibrary(libPath, NULL, envp) == 0) {
201 DEBUGS(" elf_relocate: Unable to load '%s'", libPath);
203 SysDebug("Unable to load required library '%s'", libPath);
207 DEBUGS(" Lib loaded");
210 // case DT_PLTGOT: pltgot = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
211 case DT_JMPREL: plt = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
212 case DT_PLTREL: pltType = dynamicTab[j].d_val; break;
213 case DT_PLTRELSZ: pltSz = dynamicTab[j].d_val; break;
215 // --- Relocation ---
216 case DT_REL: rel = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
217 case DT_RELSZ: relSz = dynamicTab[j].d_val; break;
218 case DT_RELENT: relEntSz = dynamicTab[j].d_val; break;
219 case DT_RELA: rela = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
220 case DT_RELASZ: relaSz = dynamicTab[j].d_val; break;
221 case DT_RELAENT: relaEntSz = dynamicTab[j].d_val; break;
223 // --- Symbol Table ---
225 // --- Hash Table ---
227 // --- String Table ---
233 if(dynamicTab[j].d_tag > DT_JMPREL) continue;
234 //DEBUGS(" elf_relocate: %i-%i = %s,0x%x",
235 // i,j, csaDT_NAMES[dynamicTab[j].d_tag],dynamicTab[j].d_val);
240 DEBUGS(" elf_relocate: Beginning Relocation");
242 void elf_doRelocate_386(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym)
247 // Standard 32 Bit Relocation (S+A)
249 val = (intptr_t) GetSymbol(Sym, NULL);
250 DEBUGS(" elf_doRelocate: R_386_32 *0x%x += 0x%x('%s')",
255 // 32 Bit Relocation wrt. Offset (S+A-P)
257 DEBUGS(" elf_doRelocate: '%s'", Sym);
258 val = (intptr_t) GetSymbol(Sym, NULL);
259 DEBUGS(" elf_doRelocate: R_386_PC32 *0x%x = 0x%x + 0x%x - 0x%x",
260 ptr, *ptr, val, (intptr_t)ptr );
261 *ptr = val + addend - (intptr_t)ptr;
262 //*ptr = val + addend - ((Uint)ptr - iBaseDiff);
265 // Absolute Value of a symbol (S)
268 DEBUGS(" elf_doRelocate: '%s'", Sym);
269 val = (intptr_t) GetSymbol( Sym, NULL );
270 DEBUGS(" elf_doRelocate: %s *0x%x = 0x%x", csaR_NAMES[type], ptr, val);
274 // Base Address (B+A)
276 DEBUGS(" elf_doRelocate: R_386_RELATIVE *0x%x = 0x%x + 0x%x", ptr, iBaseDiff, addend);
277 *ptr = iBaseDiff + addend;
282 void *src = GetSymbol(Sym, &size);
283 DEBUGS(" elf_doRelocate_386: R_386_COPY (%p, %p, %i)", ptr, src, size);
284 memcpy(ptr, src, size);
288 SysDebug("elf_doRelocate_386: Unknown relocation %i", type);
293 void elf_doRelocate_arm(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym)
300 DEBUGS(" elf_doRelocate_arm: R_ARM_GLOB_DAT %p (%s + %x)", ptr, Sym, addend);
301 val = (intptr_t)GetSymbol(Sym, NULL);
304 case R_ARM_JUMP_SLOT:
305 if(!bRela) addend = 0;
306 DEBUGS(" elf_doRelocate_arm: R_ARM_JUMP_SLOT %p (%s + %x)", ptr, Sym, addend);
307 val = (intptr_t)GetSymbol(Sym, NULL);
311 SysDebug("elf_doRelocate_arm: Unknown Relocation, %i", type);
316 void _doRelocate(uint32_t r_info, uint32_t *ptr, int bRela, Elf32_Addr addend)
318 int type = ELF32_R_TYPE(r_info);
319 int sym = ELF32_R_SYM(r_info);
320 const char *symname = dynstrtab + dynsymtab[sym].nameOfs;
321 do_relocate(r_info, ptr, addend, type, bRela, symname);
327 do_relocate = elf_doRelocate_386;
330 do_relocate = elf_doRelocate_arm;
333 SysDebug("Elf32Relocate: Unknown machine type %i", hdr->machine);
337 // Parse Relocation Entries
341 DEBUGS(" elf_relocate: rel=0x%x, relSz=0x%x, relEntSz=0x%x", rel, relSz, relEntSz);
342 j = relSz / relEntSz;
343 for( i = 0; i < j; i++ )
345 //DEBUGS(" Rel %i: 0x%x+0x%x", i, iBaseDiff, rel[i].r_offset);
346 ptr = (void*)(iBaseDiff + rel[i].r_offset);
347 _doRelocate(rel[i].r_info, ptr, 0, *ptr);
350 // Parse Relocation Entries
354 DEBUGS(" elf_relocate: rela=0x%x, relaSz=0x%x, relaEntSz=0x%x", rela, relaSz, relaEntSz);
355 j = relaSz / relaEntSz;
356 for( i = 0; i < j; i++ )
358 ptr = (void*)(iBaseDiff + rela[i].r_offset);
359 _doRelocate(rel[i].r_info, ptr, 1, rela[i].r_addend);
363 // === Process PLT (Procedure Linkage Table) ===
367 DEBUGS(" elf_relocate: Relocate PLT, plt=0x%x", plt);
368 if(pltType == DT_REL)
370 Elf32_Rel *pltRel = plt;
371 j = pltSz / sizeof(Elf32_Rel);
372 DEBUGS(" elf_relocate: PLT Reloc Type = Rel, %i entries", j);
375 ptr = (void*)(iBaseDiff + pltRel[i].r_offset);
376 _doRelocate(pltRel[i].r_info, ptr, 0, *ptr);
381 Elf32_Rela *pltRela = plt;
382 j = pltSz / sizeof(Elf32_Rela);
383 DEBUGS(" elf_relocate: PLT Reloc Type = Rela, %i entries", j);
386 ptr = (void*)(iRealBase + pltRela[i].r_offset);
387 _doRelocate(pltRela[i].r_info, ptr, 1, pltRela[i].r_addend);
392 DEBUGS("ElfRelocate: RETURN 0x%x to %p", hdr->entrypoint + iBaseDiff, __builtin_return_address(0));
393 return (void*)(intptr_t)( hdr->entrypoint + iBaseDiff );
396 int Elf32GetSymbol(void *Base, const char *Name, void **ret, size_t *Size)
398 Elf32_Ehdr *hdr = Base;
401 // int iSymCount = 0;
403 Elf32_Word *pBuckets;
406 const char *dynstrtab;
407 uintptr_t iBaseDiff = -1;
409 Elf32_Dyn *dynTab = NULL;
412 phtab = (void*)( Base + hdr->phoff );
413 for( i = 0; i < hdr->phentcount; i ++ )
415 if(phtab[i].Type == PT_LOAD && iBaseDiff > phtab[i].VAddr)
416 iBaseDiff = phtab[i].VAddr;
417 if( phtab[i].Type == PT_DYNAMIC ) {
418 dynTab = (void*)(intptr_t)phtab[i].VAddr;
422 SysDebug("ERROR - Unable to find DYNAMIC segment in %p", Base);
425 iBaseDiff = (intptr_t)Base - iBaseDiff; // Make iBaseDiff actually the diff
426 dynTab = (void*)( (intptr_t)dynTab + iBaseDiff );
427 for( i = 0; dynTab[i].d_tag != DT_NULL; i++)
429 switch(dynTab[i].d_tag)
431 // --- Symbol Table ---
433 symtab = (void*)(intptr_t) dynTab[i].d_val; // Rebased in Relocate
436 dynstrtab = (void*)(intptr_t) dynTab[i].d_val;
440 pBuckets = (void*)(intptr_t) dynTab[i].d_val;
445 nbuckets = pBuckets[0];
446 // iSymCount = pBuckets[1];
447 pBuckets = &pBuckets[2];
448 pChains = &pBuckets[ nbuckets ];
451 iNameHash = ElfHashString(Name);
452 iNameHash %= nbuckets;
455 i = pBuckets[ iNameHash ];
456 if(symtab[i].shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].nameOfs, Name) == 0) {
457 *ret = (void*) (intptr_t) symtab[ i ].value + iBaseDiff;
458 if(Size) *Size = symtab[i].size;
462 while(pChains[i] != STN_UNDEF)
465 if(symtab[i].shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[ i ].nameOfs, Name) == 0) {
466 *ret = (void*)(intptr_t)symtab[ i ].value + iBaseDiff;
467 if(Size) *Size = symtab[i].size;
476 void *Elf64Relocate(void *Base, char **envp, const char *Filename)
479 Elf64_Ehdr *hdr = Base;
482 Elf64_Addr compiledBase = -1, baseDiff;
483 Elf64_Sym *symtab = NULL;
485 Elf64_Word *hashtab = NULL;
486 Elf64_Rel *rel = NULL;
488 Elf64_Rela *rela = NULL;
491 int plt_size = 0, plt_type = 0;
493 DEBUGS("Elf64Relocate: hdr = {");
494 DEBUGS("Elf64Relocate: e_ident = '%.16s'", hdr->e_ident);
495 DEBUGS("Elf64Relocate: e_type = 0x%x", hdr->e_type);
496 DEBUGS("Elf64Relocate: e_machine = 0x%x", hdr->e_machine);
497 DEBUGS("Elf64Relocate: e_version = 0x%x", hdr->e_version);
498 DEBUGS("Elf64Relocate: e_entry = %p", hdr->e_entry);
499 DEBUGS("Elf64Relocate: e_phoff = 0x%llx", hdr->e_phoff);
500 DEBUGS("Elf64Relocate: e_shoff = 0x%llx", hdr->e_shoff);
501 DEBUGS("Elf64Relocate: e_flags = 0x%x", hdr->e_flags);
502 DEBUGS("Elf64Relocate: e_ehsize = 0x%x", hdr->e_ehsize);
503 DEBUGS("Elf64Relocate: e_phentsize = 0x%x", hdr->e_phentsize);
504 DEBUGS("Elf64Relocate: e_phnum = %i", hdr->e_phnum);
506 // Scan for the dynamic table (and find the compiled base)
507 phtab = Base + hdr->e_phoff;
508 for( i = 0; i < hdr->e_phnum; i ++ )
510 if(phtab[i].p_type == PT_DYNAMIC)
511 dyntab = (void *)(intptr_t)phtab[i].p_vaddr;
512 if(phtab[i].p_type == PT_LOAD && compiledBase > phtab[i].p_vaddr)
513 compiledBase = phtab[i].p_vaddr;
516 baseDiff = (uintptr_t)Base - compiledBase;
518 DEBUGS("baseDiff = %p", baseDiff);
521 SysDebug(" Elf64Relocate: No PT_DYNAMIC segment in image %p, returning", Base);
522 return (void *)(uintptr_t)(hdr->e_entry + baseDiff);
525 dyntab = (void *)(uintptr_t)((uintptr_t)dyntab + baseDiff);
527 // Parse the dynamic table (first pass)
528 // - Search for String, Symbol and Hash tables
529 for(i = 0; dyntab[i].d_tag != DT_NULL; i ++)
531 switch(dyntab[i].d_tag)
534 dyntab[i].d_un.d_ptr += baseDiff;
535 symtab = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
538 dyntab[i].d_un.d_ptr += baseDiff;
539 strtab = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
542 dyntab[i].d_un.d_ptr += baseDiff;
543 hashtab = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
548 if( !symtab || !strtab || !hashtab ) {
549 SysDebug("ld-acess - Elf64Relocate: Missing Symbol, string or hash table");
553 // Ready for symbol use
554 AddLoaded( Filename, Base );
556 // Second pass on dynamic table
557 for(i = 0; dyntab[i].d_tag != DT_NULL; i ++)
559 DEBUGS("dyntab[%i].d_tag = %i", i, dyntab[i].d_tag);
560 switch(dyntab[i].d_tag)
562 case DT_SONAME: break;
565 char *libPath = strtab + dyntab[i].d_un.d_val;
566 DEBUGS("Elf64Relocate: libPath = '%s'", libPath);
567 if(LoadLibrary(libPath, NULL, envp) == 0) {
568 SysDebug("ld-acess - Elf64Relocate: Unable to load '%s'", libPath);
573 // Relocation entries
575 dyntab[i].d_un.d_ptr += baseDiff;
576 rel = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
579 rel_count = dyntab[i].d_un.d_val / sizeof(Elf64_Rel);
582 if( dyntab[i].d_un.d_val != sizeof(Elf64_Rel) ) {
583 SysDebug("ld-acess - Elf64Relocate: DT_RELENT(%i) != sizeof(Elf64_Rel)(%i)",
584 dyntab[i].d_un.d_val, sizeof(Elf64_Rel));
589 dyntab[i].d_un.d_ptr += baseDiff;
590 rela = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
593 rela_count = dyntab[i].d_un.d_val / sizeof(Elf64_Rela);
596 if( dyntab[i].d_un.d_val != sizeof(Elf64_Rela) ) {
597 SysDebug("ld-acess - Elf64Relocate: DT_RELAENT(%i) != sizeof(Elf64_Rela)(%i)",
598 dyntab[i].d_un.d_val, sizeof(Elf64_Rela));
603 dyntab[i].d_un.d_ptr += baseDiff;
604 pltrel = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
607 plt_type = dyntab[i].d_un.d_val;
610 plt_size = dyntab[i].d_un.d_val;
615 // Relocation function
616 void _Elf64DoReloc(Elf64_Xword r_info, void *ptr, Elf64_Sxword addend)
618 int sym = ELF64_R_SYM(r_info);
619 int type = ELF64_R_TYPE(r_info);
620 const char *symname = strtab + symtab[sym].st_name;
626 *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL) + addend;
628 case R_X86_64_COPY: {
630 void *sym = GetSymbol(symname, &size);
631 memcpy(ptr, sym, size);
633 case R_X86_64_GLOB_DAT:
634 *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL);
636 case R_X86_64_JUMP_SLOT:
637 *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL);
639 case R_X86_64_RELATIVE:
640 *(uint64_t*)ptr = (uintptr_t)Base + addend;
643 SysDebug("ld-acess - _Elf64DoReloc: Unknown relocation type %i", type);
650 DEBUGS("rel_count = %i", rel_count);
651 for( i = 0; i < rel_count; i ++ )
653 uint64_t *ptr = (void *)(uintptr_t)( rel[i].r_offset + baseDiff );
654 _Elf64DoReloc( rel[i].r_info, ptr, *ptr);
660 DEBUGS("rela_count = %i", rela_count);
661 for( i = 0; i < rela_count; i ++ )
663 uint64_t *ptr = (void *)(uintptr_t)( rela[i].r_offset + baseDiff );
664 _Elf64DoReloc( rela[i].r_info, ptr, rela[i].r_addend );
668 if( pltrel && plt_type )
670 if( plt_type == DT_REL ) {
671 Elf64_Rel *plt = pltrel;
672 int count = plt_size / sizeof(Elf64_Rel);
673 DEBUGS("plt rel count = %i", count);
674 for( i = 0; i < count; i ++ )
676 uint64_t *ptr = (void *)(uintptr_t)( plt[i].r_offset + baseDiff );
677 _Elf64DoReloc( plt[i].r_info, ptr, *ptr);
681 Elf64_Rela *plt = pltrel;
682 int count = plt_size / sizeof(Elf64_Rela);
683 DEBUGS("plt rela count = %i", count);
684 for( i = 0; i < count; i ++ )
686 uint64_t *ptr = (void *)(uintptr_t)( plt[i].r_offset + baseDiff );
687 _Elf64DoReloc( plt[i].r_info, ptr, plt[i].r_addend);
692 DEBUGS("Elf64Relocate: Relocations done, return %p", (void *)(hdr->e_entry + baseDiff));
693 return (void *)(uintptr_t)(hdr->e_entry + baseDiff);
696 int Elf64GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size)
698 Elf64_Ehdr *hdr = Base;
701 // int iSymCount = 0;
703 Elf64_Word *pBuckets;
706 const char *dynstrtab;
707 uintptr_t iBaseDiff = -1;
713 // Catch the current executable
717 Elf64_Dyn *dynTab = NULL;
721 phtab = (void*)( Base + hdr->e_phoff );
722 for( i = 0; i < hdr->e_phnum; i ++ )
724 if(phtab[i].p_type == PT_LOAD && iBaseDiff > phtab[i].p_vaddr)
725 iBaseDiff = phtab[i].p_vaddr;
726 if( phtab[i].p_type == PT_DYNAMIC ) {
727 dynTab = (void*)(intptr_t)phtab[i].p_vaddr;
731 SysDebug("ERROR - Unable to find DYNAMIC segment in %p", Base);
734 iBaseDiff = (intptr_t)Base - iBaseDiff; // Make iBaseDiff actually the diff
735 dynTab = (void*)( (intptr_t)dynTab + iBaseDiff );
737 for( j = 0; dynTab[j].d_tag != DT_NULL; j++)
739 switch(dynTab[j].d_tag)
741 // --- Symbol Table ---
743 symtab = (void*)(intptr_t) dynTab[j].d_un.d_val; // Rebased in Relocate
746 dynstrtab = (void*)(intptr_t) dynTab[j].d_un.d_val;
750 pBuckets = (void*)(intptr_t) dynTab[j].d_un.d_val;
756 nbuckets = pBuckets[0];
757 // iSymCount = pBuckets[1];
758 pBuckets = &pBuckets[2];
759 pChains = &pBuckets[ nbuckets ];
762 iNameHash = ElfHashString(Name);
763 iNameHash %= nbuckets;
766 i = pBuckets[ iNameHash ];
767 if(symtab[i].st_shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].st_name, Name) == 0) {
768 *Ret = (void*) (intptr_t) symtab[i].st_value + iBaseDiff;
769 if(Size) *Size = symtab[i].st_size;
770 DEBUGS("%s = %p", Name, *Ret);
774 while(pChains[i] != STN_UNDEF)
777 if(symtab[i].st_shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].st_name, Name) == 0) {
778 *Ret = (void*)(intptr_t)symtab[i].st_value + iBaseDiff;
779 if(Size) *Size = symtab[i].st_size;
780 DEBUGS("%s = %p", Name, *Ret);
790 uint32_t ElfHashString(const char *name)
795 h = (h << 4) + *(uint8_t*)name++;
796 if( (g = h & 0xf0000000) )