/*
- * AcessOS 1 - Dynamic Loader
- * By thePowersGang
+ * Acess2 Dynamic Linker
+ * - By John Hodge (thePowersGang)
+ *
+ * elf.c
+ * - ELF32/ELF64 relocation
*/
+#ifndef DEBUG // This code is #include'd from the kernel, so DEBUG may already be defined
+# define DEBUG 0
+#endif
+
+#ifndef PAGE_SIZE
+# define PAGE_SIZE 4096
+#endif
+
#include "common.h"
#include <stdint.h>
#include "elf32.h"
#include "elf64.h"
-#define DEBUG 0
-
#if DEBUG
# define DEBUGS(v...) SysDebug("ld-acess - " v)
#else
# define DEBUGS(...)
#endif
-//#if BITS > 32
+#ifndef DISABLE_ELF64
# define SUPPORT_ELF64
-//#endif
+#endif
// === CONSTANTS ===
#if DEBUG
// === PROTOTYPES ===
void *ElfRelocate(void *Base, char **envp, const char *Filename);
- int ElfGetSymbol(void *Base, const char *Name, void **Ret);
+ int ElfGetSymbol(void *Base, const char *Name, void **Ret, size_t *Size);
void *Elf32Relocate(void *Base, char **envp, const char *Filename);
- int Elf32GetSymbol(void *Base, const char *Name, void **Ret);
+ int Elf32GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size);
+ int elf_doRelocate_386(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff);
+ int elf_doRelocate_arm(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff);
+ int elf_doRelocate_unk(uint32_t , uint32_t *, Elf32_Addr , int , int , const char *, intptr_t);
#ifdef SUPPORT_ELF64
+int _Elf64DoReloc_X86_64(void *Base, const char *strtab, Elf64_Sym *symtab, Elf64_Xword r_info, void *ptr, Elf64_Sxword addend);
void *Elf64Relocate(void *Base, char **envp, const char *Filename);
- int Elf64GetSymbol(void *Base, const char *Name, void **Ret);
+ int Elf64GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size);
#endif
-Uint32 ElfHashString(const char *name);
+uint32_t ElfHashString(const char *name);
// === CODE ===
/**
/**
* \fn int ElfGetSymbol(Uint Base, const char *name, void **ret)
*/
-int ElfGetSymbol(void *Base, const char *Name, void **ret)
+int ElfGetSymbol(void *Base, const char *Name, void **ret, size_t *Size)
{
Elf32_Ehdr *hdr = Base;
switch(hdr->e_ident[4])
{
case ELFCLASS32:
- return Elf32GetSymbol(Base, Name, ret);
+ return Elf32GetSymbol(Base, Name, ret, Size);
#ifdef SUPPORT_ELF64
case ELFCLASS64:
- return Elf64GetSymbol(Base, Name, ret);
+ return Elf64GetSymbol(Base, Name, ret, Size);
#endif
default:
SysDebug("ld-acess - ElfRelocate: Unknown file class %i", hdr->e_ident[4]);
}
}
+int elf_doRelocate_386(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type,
+ int bRela, const char *Sym, intptr_t iBaseDiff)
+{
+ void *symval;
+ switch( type )
+ {
+ // Standard 32 Bit Relocation (S+A)
+ case R_386_32:
+ if( !GetSymbol(Sym, &symval, NULL) )
+ return 1;
+ DEBUGS(" elf_doRelocate: R_386_32 *0x%x += %p('%s')",
+ ptr, symval, Sym);
+ *ptr = (intptr_t)symval + addend;
+ break;
+
+ // 32 Bit Relocation wrt. Offset (S+A-P)
+ case R_386_PC32:
+ DEBUGS(" elf_doRelocate: '%s'", Sym);
+ if( !GetSymbol(Sym, &symval, NULL) ) return 1;
+ DEBUGS(" elf_doRelocate: R_386_PC32 *0x%x = 0x%x + 0x%p - 0x%x",
+ ptr, *ptr, symval, (intptr_t)ptr );
+ *ptr = (intptr_t)symval + addend - (intptr_t)ptr;
+ //*ptr = val + addend - ((Uint)ptr - iBaseDiff);
+ break;
+
+ // Absolute Value of a symbol (S)
+ case R_386_GLOB_DAT:
+ case R_386_JMP_SLOT:
+ DEBUGS(" elf_doRelocate: '%s'", Sym);
+ if( !GetSymbol(Sym, &symval, NULL) ) return 1;
+ DEBUGS(" elf_doRelocate: %s *0x%x = %p", csaR_NAMES[type], ptr, symval);
+ *ptr = (intptr_t)symval;
+ break;
+
+ // Base Address (B+A)
+ case R_386_RELATIVE:
+ DEBUGS(" elf_doRelocate: R_386_RELATIVE *0x%x = 0x%x + 0x%x", ptr, iBaseDiff, addend);
+ *ptr = iBaseDiff + addend;
+ break;
+
+ case R_386_COPY: {
+ size_t size;
+ if( !GetSymbol(Sym, &symval, &size) ) return 1;
+ DEBUGS(" elf_doRelocate_386: R_386_COPY (%p, %p, %i)", ptr, symval, size);
+ memcpy(ptr, symval, size);
+ break; }
+
+ default:
+ SysDebug("elf_doRelocate_386: Unknown relocation %i", type);
+ return 2;
+ }
+ return 0;
+}
+
+int elf_doRelocate_arm(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff)
+{
+ uint32_t val;
+ switch(type)
+ {
+ // (S + A) | T
+ case R_ARM_ABS32:
+ DEBUGS(" elf_doRelocate_arm: R_ARM_ABS32 %p (%s + %x)", ptr, Sym, addend);
+ if( !GetSymbol(Sym, (void**)&val, NULL) ) return 1;
+ *ptr = val + addend;
+ break;
+ case R_ARM_GLOB_DAT:
+ DEBUGS(" elf_doRelocate_arm: R_ARM_GLOB_DAT %p (%s + %x)", ptr, Sym, addend);
+ if( !GetSymbol(Sym, (void**)&val, NULL) ) return 1;
+ *ptr = val + addend;
+ break;
+ case R_ARM_JUMP_SLOT:
+ if(!bRela) addend = 0;
+ DEBUGS(" elf_doRelocate_arm: R_ARM_JUMP_SLOT %p (%s + %x)", ptr, Sym, addend);
+ if( !GetSymbol(Sym, (void**)&val, NULL) ) return 1;
+ *ptr = val + addend;
+ break;
+ // Copy
+ case R_ARM_COPY: {
+ size_t size;
+ void *src;
+ if( !GetSymbol(Sym, &src, &size) ) return 1;
+ DEBUGS(" elf_doRelocate_arm: R_ARM_COPY (%p, %p, %i)", ptr, src, size);
+ memcpy(ptr, src, size);
+ break; }
+ // Delta between link and runtime locations + A
+ case R_ARM_RELATIVE:
+ DEBUGS(" elf_doRelocate_arm: R_ARM_RELATIVE %p (0x%x + 0x%x)", ptr, iBaseDiff, addend);
+ if(Sym[0] != '\0') {
+ // TODO: Get delta for a symbol
+ SysDebug("elf_doRelocate_arm: TODO - Implment R_ARM_RELATIVE for symbols");
+ return 2;
+ }
+ else {
+ *ptr = iBaseDiff + addend;
+ }
+ break;
+ default:
+ SysDebug("elf_doRelocate_arm: Unknown Relocation, %i", type);
+ return 2;
+ }
+ return 0;
+}
+
+int elf_doRelocate_unk(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym, intptr_t iBaseDiff)
+{
+ return 1;
+}
+
void *Elf32Relocate(void *Base, char **envp, const char *Filename)
{
Elf32_Ehdr *hdr = Base;
Elf32_Phdr *phtab;
- int i, j; // Counters
char *libPath;
intptr_t iRealBase = -1;
intptr_t iBaseDiff;
int iSegmentCount;
- int iSymCount;
Elf32_Rel *rel = NULL;
Elf32_Rela *rela = NULL;
- Uint32 *pltgot = NULL;
void *plt = NULL;
int relSz=0, relEntSz=8;
int relaSz=0, relaEntSz=8;
Elf32_Dyn *dynamicTab = NULL; // Dynamic Table Pointer
char *dynstrtab = NULL; // .dynamic String Table
Elf32_Sym *dynsymtab;
- void (*do_relocate)(uint32_t t_info, uint32_t *ptr, Elf32_Addr addend, int Type, const char *Sym);
+ int (*do_relocate)(uint32_t t_info, uint32_t *ptr, Elf32_Addr addend, int Type, int bRela, const char *Sym, intptr_t iBaseDiff);
DEBUGS("ElfRelocate: (Base=0x%x)", Base);
// Parse Program Header to get Dynamic Table
- phtab = Base + hdr->phoff;
+ phtab = (void*)( (uintptr_t)Base + hdr->phoff );
iSegmentCount = hdr->phentcount;
- for(i=0;i<iSegmentCount;i++)
+ for(int i = 0; i < iSegmentCount; i ++)
{
- // Determine linked base address
- if(phtab[i].Type == PT_LOAD && iRealBase > phtab[i].VAddr)
- iRealBase = phtab[i].VAddr;
-
- // Find Dynamic Section
- if(phtab[i].Type == PT_DYNAMIC) {
- if(dynamicTab) {
+ switch(phtab[i].Type)
+ {
+ case PT_LOAD:
+ // Determine linked base address
+ if( iRealBase > phtab[i].VAddr)
+ iRealBase = phtab[i].VAddr;
+ break;
+ case PT_DYNAMIC:
+ // Find Dynamic Section
+ if(!dynamicTab) {
+ dynamicTab = (void *) (intptr_t) phtab[i].VAddr;
+ }
+ else {
DEBUGS(" WARNING - elf_relocate: Multiple PT_DYNAMIC segments");
- continue;
}
- dynamicTab = (void *) (intptr_t) phtab[i].VAddr;
- j = i; // Save Dynamic Table ID
+ break;
}
}
// Adjust "Real" Base
iBaseDiff = (intptr_t)Base - iRealBase;
-
+
// hdr->entrypoint += iBaseDiff; // Adjust Entrypoint
// Check if a PT_DYNAMIC segement was found
SysDebug(" elf_relocate: No PT_DYNAMIC segment in image %p, returning", Base);
return (void *)(intptr_t)(hdr->entrypoint + iBaseDiff);
}
-
+
+ // Allow writing to read-only segments, just in case they need to be relocated
+ // - Will be reversed at the end of the function
+ for( int i = 0; i < iSegmentCount; i ++ )
+ {
+ if(phtab[i].Type == PT_LOAD && !(phtab[i].Flags & PF_W) ) {
+ uintptr_t addr = phtab[i].VAddr + iBaseDiff;
+ uintptr_t end = addr + phtab[i].MemSize;
+ for( ; addr < end; addr += PAGE_SIZE )
+ _SysSetMemFlags(addr, 0, 1); // Unset RO
+ }
+ }
+
// Adjust Dynamic Table
dynamicTab = (void *)( (intptr_t)dynamicTab + iBaseDiff );
// === Get Symbol table and String Table ===
- for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)
+ dynsymtab = NULL;
+ for( int j = 0; dynamicTab[j].d_tag != DT_NULL; j++)
{
switch(dynamicTab[j].d_tag)
{
case DT_SYMTAB:
DEBUGS(" elf_relocate: DYNAMIC Symbol Table 0x%x (0x%x)",
dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff);
- if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
- dynsymtab = (void*)(dynamicTab[j].d_val);
+ dynsymtab = (void*)((intptr_t)dynamicTab[j].d_val + iBaseDiff);
+ //if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
break;
// --- String Table ---
case DT_STRTAB:
DEBUGS(" elf_relocate: DYNAMIC String Table 0x%x (0x%x)",
dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff);
- if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
- dynstrtab = (void*)(dynamicTab[j].d_val);
+ dynstrtab = (void*)((intptr_t)dynamicTab[j].d_val + iBaseDiff);
+ //if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
break;
// --- Hash Table --
case DT_HASH:
- if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
- iSymCount = ((Elf32_Word*)(dynamicTab[j].d_val))[1];
+ //if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff;
+// iSymCount = ((Elf32_Word*)(intptr_t)dynamicTab[j].d_val)[1];
break;
}
}
if(dynsymtab == NULL) {
SysDebug("ld-acess.so - WARNING: No Dynamic Symbol table in %p, returning", hdr);
- return (void *) hdr->entrypoint + iBaseDiff;
+ return (void *)(intptr_t) (hdr->entrypoint + iBaseDiff);
}
// === Add to loaded list (can be imported now) ===
// === Parse Relocation Data ===
DEBUGS(" elf_relocate: dynamicTab = 0x%x", dynamicTab);
- for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)
+ for( int j = 0; dynamicTab[j].d_tag != DT_NULL; j++)
{
switch(dynamicTab[j].d_tag)
{
// --- Needed Library ---
case DT_NEEDED:
libPath = dynstrtab + dynamicTab[j].d_val;
+ DEBUGS(" dynstrtab = %p, d_val = 0x%x", dynstrtab, dynamicTab[j].d_val);
DEBUGS(" Required Library '%s'", libPath);
if(LoadLibrary(libPath, NULL, envp) == 0) {
#if DEBUG
DEBUGS(" Lib loaded");
break;
// --- PLT/GOT ---
- case DT_PLTGOT: pltgot = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
+// case DT_PLTGOT: pltgot = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
case DT_JMPREL: plt = (void*)(iBaseDiff + dynamicTab[j].d_val); break;
case DT_PLTREL: pltType = dynamicTab[j].d_val; break;
case DT_PLTRELSZ: pltSz = dynamicTab[j].d_val; break;
}
DEBUGS(" elf_relocate: Beginning Relocation");
-
- void elf_doRelocate_386(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, const char *Sym)
- {
- Uint32 val;
- switch( type )
- {
- // Standard 32 Bit Relocation (S+A)
- case R_386_32:
- val = (intptr_t) GetSymbol( Sym );
- DEBUGS(" elf_doRelocate: R_386_32 *0x%x += 0x%x('%s')",
- ptr, val, Sym);
- *ptr = val + addend;
- break;
-
- // 32 Bit Relocation wrt. Offset (S+A-P)
- case R_386_PC32:
- DEBUGS(" elf_doRelocate: '%s'", Sym);
- val = (intptr_t) GetSymbol( Sym );
- DEBUGS(" elf_doRelocate: R_386_PC32 *0x%x = 0x%x + 0x%x - 0x%x",
- ptr, *ptr, val, (intptr_t)ptr );
- *ptr = val + addend - (intptr_t)ptr;
- //*ptr = val + addend - ((Uint)ptr - iBaseDiff);
- break;
-
- // Absolute Value of a symbol (S)
- case R_386_GLOB_DAT:
- case R_386_JMP_SLOT:
- DEBUGS(" elf_doRelocate: '%s'", Sym);
- val = (intptr_t) GetSymbol( Sym );
- DEBUGS(" elf_doRelocate: %s *0x%x = 0x%x", csaR_NAMES[type], ptr, val);
- *ptr = val;
- break;
-
- // Base Address (B+A)
- case R_386_RELATIVE:
- DEBUGS(" elf_doRelocate: R_386_RELATIVE *0x%x = 0x%x + 0x%x", ptr, iBaseDiff, addend);
- *ptr = iBaseDiff + addend;
- break;
-
- default:
- SysDebug("elf_doRelocate_386: Unknown relocation %i", type);
- break;
- }
- }
-
- void elf_doRelocate_arm(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, const char *Sym)
- {
- uint32_t val;
- switch(type)
- {
- // (S + A) | T
- case R_ARM_GLOB_DAT:
- case R_ARM_JUMP_SLOT:
- val = (intptr_t)GetSymbol(Sym);
- *ptr = val + addend;
- break;
- default:
- SysDebug("elf_doRelocate_arm: Unknown Relocation, %i", type);
- break;
- }
- }
- void _doRelocate(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend)
- {
- int type = ELF32_R_TYPE(r_info);
- int sym = ELF32_R_SYM(r_info);
- const char *symname = dynstrtab + dynsymtab[sym].nameOfs;
- do_relocate(r_info, ptr, addend, type, symname);
- }
+ int fail = 0;
switch(hdr->machine)
{
break;
default:
SysDebug("Elf32Relocate: Unknown machine type %i", hdr->machine);
+ do_relocate = elf_doRelocate_unk;
+ fail = 1;
break;
}
+ DEBUGS("do_relocate = %p (%p or %p)", do_relocate, &elf_doRelocate_386, &elf_doRelocate_arm);
+
+ #define _doRelocate(r_info, ptr, bRela, addend) \
+ do_relocate(r_info, ptr, addend, ELF32_R_TYPE(r_info), bRela, \
+ dynstrtab + dynsymtab[ELF32_R_SYM(r_info)].nameOfs, iBaseDiff);
+
// Parse Relocation Entries
if(rel && relSz)
{
- Uint32 *ptr;
+ Elf32_Word *ptr;
DEBUGS(" elf_relocate: rel=0x%x, relSz=0x%x, relEntSz=0x%x", rel, relSz, relEntSz);
- j = relSz / relEntSz;
- for( i = 0; i < j; i++ )
+ int max = relSz / relEntSz;
+ for( int i = 0; i < max; i++ )
{
//DEBUGS(" Rel %i: 0x%x+0x%x", i, iBaseDiff, rel[i].r_offset);
ptr = (void*)(iBaseDiff + rel[i].r_offset);
- _doRelocate(rel[i].r_info, ptr, *ptr);
+ fail |= _doRelocate(rel[i].r_info, ptr, 0, *ptr);
}
}
// Parse Relocation Entries
if(rela && relaSz)
{
- Uint32 *ptr;
+ Elf32_Word *ptr;
DEBUGS(" elf_relocate: rela=0x%x, relaSz=0x%x, relaEntSz=0x%x", rela, relaSz, relaEntSz);
- j = relaSz / relaEntSz;
- for( i = 0; i < j; i++ )
+ int count = relaSz / relaEntSz;
+ for( int i = 0; i < count; i++ )
{
ptr = (void*)(iBaseDiff + rela[i].r_offset);
- _doRelocate(rel[i].r_info, ptr, rela[i].r_addend);
+ fail |= _doRelocate(rel[i].r_info, ptr, 1, rela[i].r_addend);
}
}
// === Process PLT (Procedure Linkage Table) ===
if(plt && pltSz)
{
- Uint32 *ptr;
+ Elf32_Word *ptr;
DEBUGS(" elf_relocate: Relocate PLT, plt=0x%x", plt);
if(pltType == DT_REL)
{
Elf32_Rel *pltRel = plt;
- j = pltSz / sizeof(Elf32_Rel);
- DEBUGS(" elf_relocate: PLT Reloc Type = Rel, %i entries", j);
- for(i=0;i<j;i++)
+ int count = pltSz / sizeof(Elf32_Rel);
+ DEBUGS(" elf_relocate: PLT Reloc Type = Rel, %i entries", count);
+ for(int i = 0; i < count; i ++)
{
ptr = (void*)(iBaseDiff + pltRel[i].r_offset);
- _doRelocate(pltRel[i].r_info, ptr, *ptr);
+ fail |= _doRelocate(pltRel[i].r_info, ptr, 0, *ptr);
}
}
else
{
Elf32_Rela *pltRela = plt;
- j = pltSz / sizeof(Elf32_Rela);
- DEBUGS(" elf_relocate: PLT Reloc Type = Rela, %i entries", j);
- for(i=0;i<j;i++)
+ int count = pltSz / sizeof(Elf32_Rela);
+ DEBUGS(" elf_relocate: PLT Reloc Type = Rela, %i entries", count);
+ for(int i=0;i<count;i++)
{
ptr = (void*)(iRealBase + pltRela[i].r_offset);
- _doRelocate(pltRela[i].r_info, ptr, pltRela[i].r_addend);
+ fail |= _doRelocate(pltRela[i].r_info, ptr, 1, pltRela[i].r_addend);
}
}
}
-
- DEBUGS("ElfRelocate: RETURN 0x%x", hdr->entrypoint + iBaseDiff);
- return (void*)hdr->entrypoint + iBaseDiff;
+
+ // Re-set readonly
+ for( int i = 0; i < iSegmentCount; i ++ )
+ {
+ // If load and not writable
+ if(phtab[i].Type == PT_LOAD && !(phtab[i].Flags & PF_W) ) {
+ uintptr_t addr = phtab[i].VAddr + iBaseDiff;
+ uintptr_t end = addr + phtab[i].MemSize;
+ for( ; addr < end; addr += PAGE_SIZE )
+ _SysSetMemFlags(addr, 1, 1); // Unset RO
+ }
+ }
+
+ if( fail ) {
+ DEBUGS("ElfRelocate: Failure");
+ return NULL;
+ }
+
+ #undef _doRelocate
+
+ DEBUGS("ElfRelocate: RETURN 0x%x to %p", hdr->entrypoint + iBaseDiff, __builtin_return_address(0));
+ return (void*)(intptr_t)( hdr->entrypoint + iBaseDiff );
}
-int Elf32GetSymbol(void *Base, const char *Name, void **ret)
+int Elf32GetSymbol(void *Base, const char *Name, void **ret, size_t *Size)
{
Elf32_Ehdr *hdr = Base;
- Elf32_Sym *symtab;
+ Elf32_Sym *symtab = NULL;
int nbuckets = 0;
- int iSymCount = 0;
- int i;
- Uint32 *pBuckets;
- Uint32 *pChains;
+ Elf32_Word *pBuckets = NULL;
+ Elf32_Word *pChains;
uint32_t iNameHash;
- const char *dynstrtab;
+ const char *dynstrtab = NULL;
uintptr_t iBaseDiff = -1;
Elf32_Phdr *phtab;
Elf32_Dyn *dynTab = NULL;
// Locate the tables
- phtab = (void*)( Base + hdr->phoff );
- for( i = 0; i < hdr->phentcount; i ++ )
+ phtab = (void*)( (uintptr_t)Base + hdr->phoff );
+ for( int i = 0; i < hdr->phentcount; i ++ )
{
if(phtab[i].Type == PT_LOAD && iBaseDiff > phtab[i].VAddr)
iBaseDiff = phtab[i].VAddr;
}
iBaseDiff = (intptr_t)Base - iBaseDiff; // Make iBaseDiff actually the diff
dynTab = (void*)( (intptr_t)dynTab + iBaseDiff );
- for( i = 0; dynTab[i].d_tag != DT_NULL; i++)
+ for( int i = 0; dynTab[i].d_tag != DT_NULL; i++)
{
switch(dynTab[i].d_tag)
{
// --- Symbol Table ---
case DT_SYMTAB:
- symtab = (void*)(intptr_t) dynTab[i].d_val; // Rebased in Relocate
+ symtab = (void*)((intptr_t)dynTab[i].d_val + iBaseDiff); // Rebased in Relocate
break;
case DT_STRTAB:
- dynstrtab = (void*)(intptr_t) dynTab[i].d_val;
+ dynstrtab = (void*)((intptr_t)dynTab[i].d_val + iBaseDiff);
break;
// --- Hash Table --
case DT_HASH:
- pBuckets = (void*)(intptr_t) dynTab[i].d_val;
+ pBuckets = (void*)((intptr_t)dynTab[i].d_val + iBaseDiff);
break;
}
}
+ if( !symtab ) {
+ SysDebug("ERRO - No DT_SYMTAB in %p", Base);
+ return 0;
+ }
+ if( !pBuckets ) {
+ SysDebug("ERRO - No DT_HASH in %p", Base);
+ return 0;
+ }
+ if( !dynstrtab ) {
+ SysDebug("ERRO - No DT_STRTAB in %p", Base);
+ return 0;
+ }
+
+ // ... ok... maybe they haven't been relocated
+ if( (uintptr_t)symtab < (uintptr_t)Base )
+ {
+ symtab = (void*)( (uintptr_t)symtab + iBaseDiff );
+ pBuckets = (void*)( (uintptr_t)pBuckets + iBaseDiff );
+ dynstrtab = (void*)( (uintptr_t)dynstrtab + iBaseDiff );
+ SysDebug("Executable not yet relocated");
+ }
+
nbuckets = pBuckets[0];
- iSymCount = pBuckets[1];
+// iSymCount = pBuckets[1];
pBuckets = &pBuckets[2];
pChains = &pBuckets[ nbuckets ];
-
+ assert(pChains);
+
// Get hash
iNameHash = ElfHashString(Name);
iNameHash %= nbuckets;
// Walk Chain
- i = pBuckets[ iNameHash ];
- if(symtab[i].shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].nameOfs, Name) == 0) {
- *ret = (void*) (intptr_t) symtab[ i ].value + iBaseDiff;
- return 1;
- }
-
- while(pChains[i] != STN_UNDEF)
- {
- i = pChains[i];
- if(symtab[i].shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[ i ].nameOfs, Name) == 0) {
- *ret = (void*)(intptr_t)symtab[ i ].value + iBaseDiff;
+ int idx = pBuckets[ iNameHash ];
+ do {
+ Elf32_Sym *sym = &symtab[idx];
+ assert(sym);
+ if(sym->shndx != SHN_UNDEF && strcmp(dynstrtab + sym->nameOfs, Name) == 0) {
+ *ret = (void*)( (uintptr_t)sym->value + iBaseDiff );
+ if(Size) *Size = sym->size;
return 1;
}
- }
+ } while( (idx = pChains[idx]) != STN_UNDEF && idx != pBuckets[iNameHash] );
return 0;
}
#ifdef SUPPORT_ELF64
+typedef int (*t_elf64_doreloc)(void *Base, const char *strtab, Elf64_Sym *symtab, Elf64_Xword r_info, void *ptr, Elf64_Sxword addend);
+
+int _Elf64DoReloc_X86_64(void *Base, const char *strtab, Elf64_Sym *symtab, Elf64_Xword r_info, void *ptr, Elf64_Sxword addend)
+{
+ int sym = ELF64_R_SYM(r_info);
+ int type = ELF64_R_TYPE(r_info);
+ const char *symname = strtab + symtab[sym].st_name;
+ void *symval;
+ //DEBUGS("_Elf64DoReloc: %s", symname);
+ switch( type )
+ {
+ case R_X86_64_NONE:
+ break;
+ case R_X86_64_64:
+ if( !GetSymbol(symname, &symval, NULL) ) return 1;
+ *(uint64_t*)ptr = (uintptr_t)symval + addend;
+ break;
+ case R_X86_64_COPY: {
+ size_t size;
+ if( !GetSymbol(symname, &symval, &size) ) return 1;
+ memcpy(ptr, symval, size);
+ } break;
+ case R_X86_64_GLOB_DAT:
+ if( !GetSymbol(symname, &symval, NULL) ) return 1;
+ *(uint64_t*)ptr = (uintptr_t)symval;
+ break;
+ case R_X86_64_JUMP_SLOT:
+ if( !GetSymbol(symname, &symval, NULL) ) return 1;
+ *(uint64_t*)ptr = (uintptr_t)symval;
+ break;
+ case R_X86_64_RELATIVE:
+ *(uint64_t*)ptr = (uintptr_t)Base + addend;
+ break;
+ default:
+ SysDebug("ld-acess - _Elf64DoReloc: Unknown relocation type %i", type);
+ return 2;
+ }
+ //DEBUGS("_Elf64DoReloc: - Good");
+ return 0;
+}
+
void *Elf64Relocate(void *Base, char **envp, const char *Filename)
{
int i;
Elf64_Ehdr *hdr = Base;
Elf64_Phdr *phtab;
- Elf64_Dyn *dyntab;
+ Elf64_Dyn *dyntab = NULL;
Elf64_Addr compiledBase = -1, baseDiff;
Elf64_Sym *symtab = NULL;
char *strtab = NULL;
DEBUGS("Elf64Relocate: e_phnum = %i", hdr->e_phnum);
// Scan for the dynamic table (and find the compiled base)
- phtab = Base + hdr->e_phoff;
+ phtab = (void*)((uintptr_t)Base + (uintptr_t)hdr->e_phoff);
for( i = 0; i < hdr->e_phnum; i ++ )
{
if(phtab[i].p_type == PT_DYNAMIC)
compiledBase = phtab[i].p_vaddr;
}
- baseDiff = (Elf64_Addr)Base - compiledBase;
+ baseDiff = (uintptr_t)Base - compiledBase;
DEBUGS("baseDiff = %p", baseDiff);
if(dyntab == NULL) {
SysDebug(" Elf64Relocate: No PT_DYNAMIC segment in image %p, returning", Base);
- return (void *)(hdr->e_entry + baseDiff);
+ return (void *)(uintptr_t)(hdr->e_entry + baseDiff);
}
- dyntab = (void *)((Elf64_Addr)dyntab + baseDiff);
+ dyntab = (void *)(uintptr_t)((uintptr_t)dyntab + baseDiff);
// Parse the dynamic table (first pass)
// - Search for String, Symbol and Hash tables
{
case DT_SYMTAB:
dyntab[i].d_un.d_ptr += baseDiff;
- symtab = (void *)dyntab[i].d_un.d_ptr;
+ symtab = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
break;
case DT_STRTAB:
dyntab[i].d_un.d_ptr += baseDiff;
- strtab = (void *)dyntab[i].d_un.d_ptr;
+ strtab = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
break;
case DT_HASH:
dyntab[i].d_un.d_ptr += baseDiff;
- hashtab = (void *)dyntab[i].d_un.d_ptr;
+ hashtab = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
break;
}
}
// Relocation entries
case DT_REL:
dyntab[i].d_un.d_ptr += baseDiff;
- rel = (void *)dyntab[i].d_un.d_ptr;
+ rel = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
break;
case DT_RELSZ:
rel_count = dyntab[i].d_un.d_val / sizeof(Elf64_Rel);
break;
case DT_RELA:
dyntab[i].d_un.d_ptr += baseDiff;
- rela = (void *)dyntab[i].d_un.d_ptr;
+ rela = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
break;
case DT_RELASZ:
rela_count = dyntab[i].d_un.d_val / sizeof(Elf64_Rela);
break;
case DT_JMPREL:
dyntab[i].d_un.d_ptr += baseDiff;
- pltrel = (void *)dyntab[i].d_un.d_ptr;
+ pltrel = (void *)(uintptr_t)dyntab[i].d_un.d_ptr;
break;
case DT_PLTREL:
plt_type = dyntab[i].d_un.d_val;
}
// Relocation function
- void _Elf64DoReloc(Elf64_Xword r_info, void *ptr, Elf64_Sxword addend)
- {
- int sym = ELF64_R_SYM(r_info);
- int type = ELF64_R_TYPE(r_info);
- const char *symname = strtab + symtab[sym].st_name;
- switch( type )
- {
- case R_X86_64_NONE:
- break;
- case R_X86_64_64:
- *(uint64_t*)ptr = (uint64_t)GetSymbol(symname) + addend;
- break;
- case R_X86_64_COPY:
- break;
- case R_X86_64_GLOB_DAT:
- *(uint64_t*)ptr = (uint64_t)GetSymbol(symname);
- break;
- case R_X86_64_JUMP_SLOT:
- *(uint64_t*)ptr = (uint64_t)GetSymbol(symname);
- break;
- case R_X86_64_RELATIVE:
- *(uint64_t*)ptr = (intptr_t)Base + addend;
- break;
- default:
- SysDebug("ld-acess - _Elf64DoReloc: Unknown relocation type %i", type);
- break;
- }
- }
+ t_elf64_doreloc fpElf64DoReloc = &_Elf64DoReloc_X86_64;
+ #define _Elf64DoReloc(info, ptr, addend) fpElf64DoReloc(Base, strtab, symtab, info, ptr, addend)
+ int fail = 0;
if( rel )
{
DEBUGS("rel_count = %i", rel_count);
for( i = 0; i < rel_count; i ++ )
{
- uint64_t *ptr = (void *)( rel[i].r_offset + baseDiff );
- _Elf64DoReloc( rel[i].r_info, ptr, *ptr);
+ uint64_t *ptr = (void *)(uintptr_t)( rel[i].r_offset + baseDiff );
+ fail |= _Elf64DoReloc( rel[i].r_info, ptr, *ptr);
}
}
DEBUGS("rela_count = %i", rela_count);
for( i = 0; i < rela_count; i ++ )
{
- _Elf64DoReloc( rela[i].r_info, (void *)( rela[i].r_offset + baseDiff ), rela[i].r_addend );
+ uint64_t *ptr = (void *)(uintptr_t)( rela[i].r_offset + baseDiff );
+ fail |= _Elf64DoReloc( rela[i].r_info, ptr, rela[i].r_addend );
}
}
DEBUGS("plt rel count = %i", count);
for( i = 0; i < count; i ++ )
{
- uint64_t *ptr = (void *)( plt[i].r_offset + baseDiff );
- _Elf64DoReloc( plt[i].r_info, ptr, *ptr);
+ uint64_t *ptr = (void *)(uintptr_t)( plt[i].r_offset + baseDiff );
+ fail |= _Elf64DoReloc( plt[i].r_info, ptr, *ptr);
}
}
else {
DEBUGS("plt rela count = %i", count);
for( i = 0; i < count; i ++ )
{
- _Elf64DoReloc( plt[i].r_info, (void *)(plt[i].r_offset + baseDiff), plt[i].r_addend);
+ uint64_t *ptr = (void *)(uintptr_t)( plt[i].r_offset + baseDiff );
+ fail |= _Elf64DoReloc( plt[i].r_info, ptr, plt[i].r_addend);
}
}
}
- DEBUGS("Elf64Relocate: Relocations done, return %p", (void *)(hdr->e_entry + baseDiff));
- return (void *)(hdr->e_entry + baseDiff);
+ if( fail ) {
+ DEBUGS("Elf64Relocate: Failure");
+ return NULL;
+ }
+
+ {
+ void *ret = (void *)(uintptr_t)(hdr->e_entry + baseDiff);
+ DEBUGS("Elf64Relocate: Relocations done, return %p", ret);
+ return ret;
+ }
}
-int Elf64GetSymbol(void *Base, const char *Name, void **Ret)
+int Elf64GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size)
{
Elf64_Ehdr *hdr = Base;
Elf64_Sym *symtab;
int nbuckets = 0;
- int iSymCount = 0;
+// int iSymCount = 0;
int i;
Elf64_Word *pBuckets;
Elf64_Word *pChains;
int j;
// Locate the tables
- phtab = (void*)( Base + hdr->e_phoff );
+ phtab = (void*)( (intptr_t)Base + (uintptr_t)hdr->e_phoff );
for( i = 0; i < hdr->e_phnum; i ++ )
{
if(phtab[i].p_type == PT_LOAD && iBaseDiff > phtab[i].p_vaddr)
}
nbuckets = pBuckets[0];
- iSymCount = pBuckets[1];
+// iSymCount = pBuckets[1];
pBuckets = &pBuckets[2];
pChains = &pBuckets[ nbuckets ];
// Walk Chain
i = pBuckets[ iNameHash ];
if(symtab[i].st_shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].st_name, Name) == 0) {
- *Ret = (void*) (intptr_t) symtab[i].st_value + iBaseDiff;
+ *Ret = (void*)( (intptr_t)symtab[i].st_value + iBaseDiff );
+ if(Size) *Size = symtab[i].st_size;
DEBUGS("%s = %p", Name, *Ret);
return 1;
}
{
i = pChains[i];
if(symtab[i].st_shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].st_name, Name) == 0) {
- *Ret = (void*)(intptr_t)symtab[i].st_value + iBaseDiff;
+ *Ret = (void*)((intptr_t)symtab[i].st_value + iBaseDiff);
+ if(Size) *Size = symtab[i].st_size;
DEBUGS("%s = %p", Name, *Ret);
return 1;
}
#endif
-Uint32 ElfHashString(const char *name)
+uint32_t ElfHashString(const char *name)
{
- Uint32 h = 0, g;
+ uint32_t h = 0, g;
while(*name)
{
- h = (h << 4) + *(Uint8*)name++;
+ h = (h << 4) + *(uint8_t*)name++;
if( (g = h & 0xf0000000) )
h ^= g >> 24;
h &= ~g;