From: John Hodge Date: Sat, 15 Oct 2011 02:21:49 +0000 (+0800) Subject: Usermode/ld-acess - Sorting and improvements to ld-acess X-Git-Tag: rel0.13~63 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=c1771fb9d6e85a8453a0f4553b3878959901b613;p=tpg%2Facess2.git Usermode/ld-acess - Sorting and improvements to ld-acess - Removed ELF64 support if compiling for 32-bit (I think) - Sorted the ELF code a little differently - Adding ARM support --- diff --git a/Usermode/Libraries/ld-acess.so_src/arch/armv7.S.h b/Usermode/Libraries/ld-acess.so_src/arch/armv7.S.h index 85a3ac69..97dad215 100644 --- a/Usermode/Libraries/ld-acess.so_src/arch/armv7.S.h +++ b/Usermode/Libraries/ld-acess.so_src/arch/armv7.S.h @@ -33,6 +33,13 @@ __ucmpdi2: mov r0, #1 mov pc, lr +@ Well, can't blame it +@ - Clear the instruction cache +.globl __clear_cache +__clear_cache: + svc #0x1001 + mov pc, lr + @ DEST @ SRC @_memcpy: diff --git a/Usermode/Libraries/ld-acess.so_src/elf.c b/Usermode/Libraries/ld-acess.so_src/elf.c index 458343ee..1df3e8e9 100644 --- a/Usermode/Libraries/ld-acess.so_src/elf.c +++ b/Usermode/Libraries/ld-acess.so_src/elf.c @@ -7,7 +7,7 @@ #include "elf32.h" #include "elf64.h" -#define DEBUG 0 +#define DEBUG 1 #if DEBUG # define DEBUGS(v...) SysDebug("ld-acess - " v) @@ -15,6 +15,10 @@ # define DEBUGS(...) #endif +#if BITS > 32 +# define SUPPORT_ELF64 +#endif + // === CONSTANTS === #if DEBUG //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"}; @@ -23,11 +27,13 @@ static const char *csaR_NAMES[] = {"R_386_NONE", "R_386_32", "R_386_PC32", "R_38 // === PROTOTYPES === void *ElfRelocate(void *Base, char **envp, const char *Filename); + int ElfGetSymbol(void *Base, const char *Name, void **Ret); void *Elf32Relocate(void *Base, char **envp, const char *Filename); + int Elf32GetSymbol(void *Base, const char *Name, void **Ret); +#ifdef SUPPORT_ELF64 void *Elf64Relocate(void *Base, char **envp, const char *Filename); - int ElfGetSymbol(void *Base, const char *Name, void **Ret); int Elf64GetSymbol(void *Base, const char *Name, void **Ret); - int Elf32GetSymbol(void *Base, const char *Name, void **Ret); +#endif Uint32 ElfHashString(const char *name); // === CODE === @@ -43,224 +49,35 @@ void *ElfRelocate(void *Base, char **envp, const char *Filename) { case ELFCLASS32: return Elf32Relocate(Base, envp, Filename); +#ifdef SUPPORT_ELF64 case ELFCLASS64: return Elf64Relocate(Base, envp, Filename); +#endif default: SysDebug("ld-acess - ElfRelocate: Unknown file class %i", hdr->e_ident[4]); return NULL; } } -void *Elf64Relocate(void *Base, char **envp, const char *Filename) +/** + * \fn int ElfGetSymbol(Uint Base, const char *name, void **ret) + */ +int ElfGetSymbol(void *Base, const char *Name, void **ret) { - int i; - Elf64_Ehdr *hdr = Base; - Elf64_Phdr *phtab; - Elf64_Dyn *dyntab; - Elf64_Addr compiledBase = -1, baseDiff; - Elf64_Sym *symtab = NULL; - char *strtab = NULL; - Elf64_Word *hashtab = NULL; - Elf64_Rel *rel = NULL; - int rel_count = 0; - Elf64_Rela *rela = NULL; - int rela_count = 0; - void *pltrel = NULL; - int plt_size = 0, plt_type = 0; - - DEBUGS("Elf64Relocate: hdr = {"); - DEBUGS("Elf64Relocate: e_ident = '%.16s'", hdr->e_ident); - DEBUGS("Elf64Relocate: e_type = 0x%x", hdr->e_type); - DEBUGS("Elf64Relocate: e_machine = 0x%x", hdr->e_machine); - DEBUGS("Elf64Relocate: e_version = 0x%x", hdr->e_version); - DEBUGS("Elf64Relocate: e_entry = %p", hdr->e_entry); - DEBUGS("Elf64Relocate: e_phoff = 0x%llx", hdr->e_phoff); - DEBUGS("Elf64Relocate: e_shoff = 0x%llx", hdr->e_shoff); - DEBUGS("Elf64Relocate: e_flags = 0x%x", hdr->e_flags); - DEBUGS("Elf64Relocate: e_ehsize = 0x%x", hdr->e_ehsize); - DEBUGS("Elf64Relocate: e_phentsize = 0x%x", hdr->e_phentsize); - DEBUGS("Elf64Relocate: e_phnum = %i", hdr->e_phnum); - - // Scan for the dynamic table (and find the compiled base) - phtab = Base + hdr->e_phoff; - for( i = 0; i < hdr->e_phnum; i ++ ) - { - if(phtab[i].p_type == PT_DYNAMIC) - dyntab = (void *)(intptr_t)phtab[i].p_vaddr; - if(phtab[i].p_type == PT_LOAD && compiledBase > phtab[i].p_vaddr) - compiledBase = phtab[i].p_vaddr; - } - - baseDiff = (Elf64_Addr)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); - } - - dyntab = (void *)((Elf64_Addr)dyntab + baseDiff); - - // Parse the dynamic table (first pass) - // - Search for String, Symbol and Hash tables - for(i = 0; dyntab[i].d_tag != DT_NULL; i ++) - { - switch(dyntab[i].d_tag) - { - case DT_SYMTAB: - dyntab[i].d_un.d_ptr += baseDiff; - symtab = (void *)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; - break; - case DT_HASH: - dyntab[i].d_un.d_ptr += baseDiff; - hashtab = (void *)dyntab[i].d_un.d_ptr; - break; - } - } - - if( !symtab || !strtab || !hashtab ) { - SysDebug("ld-acess - Elf64Relocate: Missing Symbol, string or hash table"); - return NULL; - } - - // Ready for symbol use - AddLoaded( Filename, Base ); - - // Second pass on dynamic table - for(i = 0; dyntab[i].d_tag != DT_NULL; i ++) - { - DEBUGS("dyntab[%i].d_tag = %i", i, dyntab[i].d_tag); - switch(dyntab[i].d_tag) - { - case DT_SONAME: break; - - case DT_NEEDED: { - char *libPath = strtab + dyntab[i].d_un.d_val; - DEBUGS("Elf64Relocate: libPath = '%s'", libPath); - if(LoadLibrary(libPath, NULL, envp) == 0) { - SysDebug("ld-acess - Elf64Relocate: Unable to load '%s'", libPath); - return NULL; - } - } break; - - // Relocation entries - case DT_REL: - dyntab[i].d_un.d_ptr += baseDiff; - rel = (void *)dyntab[i].d_un.d_ptr; - break; - case DT_RELSZ: - rel_count = dyntab[i].d_un.d_val / sizeof(Elf64_Rel); - break; - case DT_RELENT: - if( dyntab[i].d_un.d_val != sizeof(Elf64_Rel) ) { - SysDebug("ld-acess - Elf64Relocate: DT_RELENT(%i) != sizeof(Elf64_Rel)(%i)", - dyntab[i].d_un.d_val, sizeof(Elf64_Rel)); - return NULL; - } - break; - case DT_RELA: - dyntab[i].d_un.d_ptr += baseDiff; - rela = (void *)dyntab[i].d_un.d_ptr; - break; - case DT_RELASZ: - rela_count = dyntab[i].d_un.d_val / sizeof(Elf64_Rela); - break; - case DT_RELAENT: - if( dyntab[i].d_un.d_val != sizeof(Elf64_Rela) ) { - SysDebug("ld-acess - Elf64Relocate: DT_RELAENT(%i) != sizeof(Elf64_Rela)(%i)", - dyntab[i].d_un.d_val, sizeof(Elf64_Rela)); - return NULL; - } - break; - case DT_JMPREL: - dyntab[i].d_un.d_ptr += baseDiff; - pltrel = (void *)dyntab[i].d_un.d_ptr; - break; - case DT_PLTREL: - plt_type = dyntab[i].d_un.d_val; - break; - case DT_PLTRELSZ: - plt_size = dyntab[i].d_un.d_val; - break; - } - } - - // 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; - default: - SysDebug("ld-acess - _Elf64DoReloc: Unknown relocation type %i", type); - break; - } - } - - 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); - } - } - - if( rela ) - { - 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 ); - } - } + Elf32_Ehdr *hdr = Base; - if( pltrel && plt_type ) + switch(hdr->e_ident[4]) { - if( plt_type == DT_REL ) { - Elf64_Rel *plt = pltrel; - int count = plt_size / sizeof(Elf64_Rel); - 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); - } - } - else { - Elf64_Rela *plt = pltrel; - int count = plt_size / sizeof(Elf64_Rela); - 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); - } - } + case ELFCLASS32: + return Elf32GetSymbol(Base, Name, ret); +#ifdef SUPPORT_ELF64 + case ELFCLASS64: + return Elf64GetSymbol(Base, Name, ret); +#endif + default: + SysDebug("ld-acess - ElfRelocate: Unknown file class %i", hdr->e_ident[4]); + return 0; } - - DEBUGS("Elf64Relocate: Relocations done, return %p", (void *)(hdr->e_entry + baseDiff)); - return (void *)(hdr->e_entry + baseDiff); } void *Elf32Relocate(void *Base, char **envp, const char *Filename) @@ -283,6 +100,7 @@ void *Elf32Relocate(void *Base, char **envp, const char *Filename) 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); DEBUGS("ElfRelocate: (Base=0x%x)", Base); @@ -430,26 +248,23 @@ void *Elf32Relocate(void *Base, char **envp, const char *Filename) DEBUGS(" elf_relocate: Beginning Relocation"); - void elf_doRelocate(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, Elf32_Sym *symtab) + void elf_doRelocate_386(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, const char *Sym) { - int type = ELF32_R_TYPE(r_info); - int sym = ELF32_R_SYM(r_info); Uint32 val; - const char *symname = dynstrtab + symtab[sym].nameOfs; switch( type ) { // Standard 32 Bit Relocation (S+A) case R_386_32: - val = (intptr_t) GetSymbol( symname ); + val = (intptr_t) GetSymbol( Sym ); DEBUGS(" elf_doRelocate: R_386_32 *0x%x += 0x%x('%s')", - ptr, val, symname); + ptr, val, Sym); *ptr = val + addend; break; // 32 Bit Relocation wrt. Offset (S+A-P) case R_386_PC32: - DEBUGS(" elf_doRelocate: #%i: '%s'", sym, symname); - val = (intptr_t) GetSymbol( symname ); + 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; @@ -459,8 +274,8 @@ void *Elf32Relocate(void *Base, char **envp, const char *Filename) // Absolute Value of a symbol (S) case R_386_GLOB_DAT: case R_386_JMP_SLOT: - DEBUGS(" elf_doRelocate: #%i: '%s'", sym, symname); - val = (intptr_t) GetSymbol( symname ); + 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; @@ -472,10 +287,48 @@ void *Elf32Relocate(void *Base, char **envp, const char *Filename) break; default: - DEBUGS(" elf_doRelocate: Rel 0x%x: 0x%x,%s", ptr, sym, csaR_NAMES[type]); + 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); + } + + switch(hdr->machine) + { + case EM_386: + do_relocate = elf_doRelocate_386; + break; + case EM_ARM: + do_relocate = elf_doRelocate_arm; + break; + default: + SysDebug("Elf32Relocate: Unknown machine type %i", hdr->machine); + break; + } // Parse Relocation Entries if(rel && relSz) @@ -487,7 +340,7 @@ void *Elf32Relocate(void *Base, char **envp, const char *Filename) { //DEBUGS(" Rel %i: 0x%x+0x%x", i, iBaseDiff, rel[i].r_offset); ptr = (void*)(iBaseDiff + rel[i].r_offset); - elf_doRelocate(rel[i].r_info, ptr, *ptr, dynsymtab); + _doRelocate(rel[i].r_info, ptr, *ptr); } } // Parse Relocation Entries @@ -499,7 +352,7 @@ void *Elf32Relocate(void *Base, char **envp, const char *Filename) for( i = 0; i < j; i++ ) { ptr = (void*)(iBaseDiff + rela[i].r_offset); - elf_doRelocate(rel[i].r_info, ptr, rela[i].r_addend, dynsymtab); + _doRelocate(rel[i].r_info, ptr, rela[i].r_addend); } } @@ -516,7 +369,7 @@ void *Elf32Relocate(void *Base, char **envp, const char *Filename) for(i=0;ientrypoint + iBaseDiff; } - -/** - * \fn int ElfGetSymbol(Uint Base, const char *name, void **ret) - */ -int ElfGetSymbol(void *Base, const char *Name, void **ret) +int Elf32GetSymbol(void *Base, const char *Name, void **ret) { Elf32_Ehdr *hdr = Base; - - switch(hdr->e_ident[4]) - { - case ELFCLASS32: - return Elf32GetSymbol(Base, Name, ret); - case ELFCLASS64: - return Elf64GetSymbol(Base, Name, ret); - default: - SysDebug("ld-acess - ElfRelocate: Unknown file class %i", hdr->e_ident[4]); - return 0; - } -} - -int Elf64GetSymbol(void *Base, const char *Name, void **Ret) -{ - Elf64_Ehdr *hdr = Base; - Elf64_Sym *symtab; + Elf32_Sym *symtab; int nbuckets = 0; int iSymCount = 0; int i; - Elf64_Word *pBuckets; - Elf64_Word *pChains; + Uint32 *pBuckets; + Uint32 *pChains; uint32_t iNameHash; const char *dynstrtab; uintptr_t iBaseDiff = -1; + Elf32_Phdr *phtab; + Elf32_Dyn *dynTab = NULL; -// DEBUGS("sizeof(uint32_t) = %i, sizeof(Elf64_Word) = %i", sizeof(uint32_t), sizeof(Elf64_Word)); - - dynstrtab = NULL; - pBuckets = NULL; - symtab = NULL; - - // Catch the current executable - if( !pBuckets ) + // Locate the tables + phtab = (void*)( Base + hdr->phoff ); + for( i = 0; i < hdr->phentcount; i ++ ) { - Elf64_Phdr *phtab; - Elf64_Dyn *dynTab = NULL; - int j; - - // Locate the tables - phtab = (void*)( Base + hdr->e_phoff ); - for( i = 0; i < hdr->e_phnum; i ++ ) - { - if(phtab[i].p_type == PT_LOAD && iBaseDiff > phtab[i].p_vaddr) - iBaseDiff = phtab[i].p_vaddr; - if( phtab[i].p_type == PT_DYNAMIC ) { - dynTab = (void*)(intptr_t)phtab[i].p_vaddr; - } - } - if( !dynTab ) { - SysDebug("ERROR - Unable to find DYNAMIC segment in %p", Base); - return 0; + if(phtab[i].Type == PT_LOAD && iBaseDiff > phtab[i].VAddr) + iBaseDiff = phtab[i].VAddr; + if( phtab[i].Type == PT_DYNAMIC ) { + dynTab = (void*)(intptr_t)phtab[i].VAddr; } - iBaseDiff = (intptr_t)Base - iBaseDiff; // Make iBaseDiff actually the diff - dynTab = (void*)( (intptr_t)dynTab + iBaseDiff ); - - for( j = 0; dynTab[j].d_tag != DT_NULL; j++) + } + if( !dynTab ) { + SysDebug("ERROR - Unable to find DYNAMIC segment in %p", Base); + return 0; + } + 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++) + { + switch(dynTab[i].d_tag) { - switch(dynTab[j].d_tag) - { - // --- Symbol Table --- - case DT_SYMTAB: - symtab = (void*)(intptr_t) dynTab[j].d_un.d_val; // Rebased in Relocate - break; - case DT_STRTAB: - dynstrtab = (void*)(intptr_t) dynTab[j].d_un.d_val; - break; - // --- Hash Table -- - case DT_HASH: - pBuckets = (void*)(intptr_t) dynTab[j].d_un.d_val; - break; - } + // --- Symbol Table --- + case DT_SYMTAB: + symtab = (void*)(intptr_t) dynTab[i].d_val; // Rebased in Relocate + break; + case DT_STRTAB: + dynstrtab = (void*)(intptr_t) dynTab[i].d_val; + break; + // --- Hash Table -- + case DT_HASH: + pBuckets = (void*)(intptr_t) dynTab[i].d_val; + break; } } -// DEBUGS("pBuckets = %p", pBuckets); - + nbuckets = pBuckets[0]; iSymCount = pBuckets[1]; pBuckets = &pBuckets[2]; -// DEBUGS("nbuckets = %i", nbuckets); pChains = &pBuckets[ nbuckets ]; // Get hash @@ -631,43 +449,249 @@ int Elf64GetSymbol(void *Base, const char *Name, void **Ret) // Walk Chain i = pBuckets[ iNameHash ]; -// DEBUGS("dynstrtab = %p", dynstrtab); -// DEBUGS("symtab = %p, i = %i", symtab, 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; - DEBUGS("%s = %p", Name, *Ret); + 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]; -// DEBUGS("chains i = %i", 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; - DEBUGS("%s = %p", Name, *Ret); + if(symtab[i].shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[ i ].nameOfs, Name) == 0) { + *ret = (void*)(intptr_t)symtab[ i ].value + iBaseDiff; return 1; } } -// DEBUGS("Elf64GetSymbol: RETURN 0, Symbol '%s' not found", Name); return 0; } -int Elf32GetSymbol(void *Base, const char *Name, void **ret) +#ifdef SUPPORT_ELF64 +void *Elf64Relocate(void *Base, char **envp, const char *Filename) { - Elf32_Ehdr *hdr = Base; - Elf32_Sym *symtab; + int i; + Elf64_Ehdr *hdr = Base; + Elf64_Phdr *phtab; + Elf64_Dyn *dyntab; + Elf64_Addr compiledBase = -1, baseDiff; + Elf64_Sym *symtab = NULL; + char *strtab = NULL; + Elf64_Word *hashtab = NULL; + Elf64_Rel *rel = NULL; + int rel_count = 0; + Elf64_Rela *rela = NULL; + int rela_count = 0; + void *pltrel = NULL; + int plt_size = 0, plt_type = 0; + + DEBUGS("Elf64Relocate: hdr = {"); + DEBUGS("Elf64Relocate: e_ident = '%.16s'", hdr->e_ident); + DEBUGS("Elf64Relocate: e_type = 0x%x", hdr->e_type); + DEBUGS("Elf64Relocate: e_machine = 0x%x", hdr->e_machine); + DEBUGS("Elf64Relocate: e_version = 0x%x", hdr->e_version); + DEBUGS("Elf64Relocate: e_entry = %p", hdr->e_entry); + DEBUGS("Elf64Relocate: e_phoff = 0x%llx", hdr->e_phoff); + DEBUGS("Elf64Relocate: e_shoff = 0x%llx", hdr->e_shoff); + DEBUGS("Elf64Relocate: e_flags = 0x%x", hdr->e_flags); + DEBUGS("Elf64Relocate: e_ehsize = 0x%x", hdr->e_ehsize); + DEBUGS("Elf64Relocate: e_phentsize = 0x%x", hdr->e_phentsize); + DEBUGS("Elf64Relocate: e_phnum = %i", hdr->e_phnum); + + // Scan for the dynamic table (and find the compiled base) + phtab = Base + hdr->e_phoff; + for( i = 0; i < hdr->e_phnum; i ++ ) + { + if(phtab[i].p_type == PT_DYNAMIC) + dyntab = (void *)(intptr_t)phtab[i].p_vaddr; + if(phtab[i].p_type == PT_LOAD && compiledBase > phtab[i].p_vaddr) + compiledBase = phtab[i].p_vaddr; + } + + baseDiff = (Elf64_Addr)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); + } + + dyntab = (void *)((Elf64_Addr)dyntab + baseDiff); + + // Parse the dynamic table (first pass) + // - Search for String, Symbol and Hash tables + for(i = 0; dyntab[i].d_tag != DT_NULL; i ++) + { + switch(dyntab[i].d_tag) + { + case DT_SYMTAB: + dyntab[i].d_un.d_ptr += baseDiff; + symtab = (void *)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; + break; + case DT_HASH: + dyntab[i].d_un.d_ptr += baseDiff; + hashtab = (void *)dyntab[i].d_un.d_ptr; + break; + } + } + + if( !symtab || !strtab || !hashtab ) { + SysDebug("ld-acess - Elf64Relocate: Missing Symbol, string or hash table"); + return NULL; + } + + // Ready for symbol use + AddLoaded( Filename, Base ); + + // Second pass on dynamic table + for(i = 0; dyntab[i].d_tag != DT_NULL; i ++) + { + DEBUGS("dyntab[%i].d_tag = %i", i, dyntab[i].d_tag); + switch(dyntab[i].d_tag) + { + case DT_SONAME: break; + + case DT_NEEDED: { + char *libPath = strtab + dyntab[i].d_un.d_val; + DEBUGS("Elf64Relocate: libPath = '%s'", libPath); + if(LoadLibrary(libPath, NULL, envp) == 0) { + SysDebug("ld-acess - Elf64Relocate: Unable to load '%s'", libPath); + return NULL; + } + } break; + + // Relocation entries + case DT_REL: + dyntab[i].d_un.d_ptr += baseDiff; + rel = (void *)dyntab[i].d_un.d_ptr; + break; + case DT_RELSZ: + rel_count = dyntab[i].d_un.d_val / sizeof(Elf64_Rel); + break; + case DT_RELENT: + if( dyntab[i].d_un.d_val != sizeof(Elf64_Rel) ) { + SysDebug("ld-acess - Elf64Relocate: DT_RELENT(%i) != sizeof(Elf64_Rel)(%i)", + dyntab[i].d_un.d_val, sizeof(Elf64_Rel)); + return NULL; + } + break; + case DT_RELA: + dyntab[i].d_un.d_ptr += baseDiff; + rela = (void *)dyntab[i].d_un.d_ptr; + break; + case DT_RELASZ: + rela_count = dyntab[i].d_un.d_val / sizeof(Elf64_Rela); + break; + case DT_RELAENT: + if( dyntab[i].d_un.d_val != sizeof(Elf64_Rela) ) { + SysDebug("ld-acess - Elf64Relocate: DT_RELAENT(%i) != sizeof(Elf64_Rela)(%i)", + dyntab[i].d_un.d_val, sizeof(Elf64_Rela)); + return NULL; + } + break; + case DT_JMPREL: + dyntab[i].d_un.d_ptr += baseDiff; + pltrel = (void *)dyntab[i].d_un.d_ptr; + break; + case DT_PLTREL: + plt_type = dyntab[i].d_un.d_val; + break; + case DT_PLTRELSZ: + plt_size = dyntab[i].d_un.d_val; + break; + } + } + + // 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; + default: + SysDebug("ld-acess - _Elf64DoReloc: Unknown relocation type %i", type); + break; + } + } + + 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); + } + } + + if( rela ) + { + 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 ); + } + } + + if( pltrel && plt_type ) + { + if( plt_type == DT_REL ) { + Elf64_Rel *plt = pltrel; + int count = plt_size / sizeof(Elf64_Rel); + 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); + } + } + else { + Elf64_Rela *plt = pltrel; + int count = plt_size / sizeof(Elf64_Rela); + 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); + } + } + } + + DEBUGS("Elf64Relocate: Relocations done, return %p", (void *)(hdr->e_entry + baseDiff)); + return (void *)(hdr->e_entry + baseDiff); +} + +int Elf64GetSymbol(void *Base, const char *Name, void **Ret) +{ + Elf64_Ehdr *hdr = Base; + Elf64_Sym *symtab; int nbuckets = 0; int iSymCount = 0; int i; - Uint32 *pBuckets; - Uint32 *pChains; + Elf64_Word *pBuckets; + Elf64_Word *pChains; uint32_t iNameHash; const char *dynstrtab; uintptr_t iBaseDiff = -1; - //DEBUGS("ElfGetSymbol: (Base=0x%x, Name='%s')", Base, Name); dynstrtab = NULL; pBuckets = NULL; symtab = NULL; @@ -675,18 +699,18 @@ int Elf32GetSymbol(void *Base, const char *Name, void **ret) // Catch the current executable if( !pBuckets ) { - Elf32_Phdr *phtab; - Elf32_Dyn *dynTab = NULL; + Elf64_Phdr *phtab; + Elf64_Dyn *dynTab = NULL; int j; // Locate the tables - phtab = (void*)( Base + hdr->phoff ); - for( i = 0; i < hdr->phentcount; i ++ ) + phtab = (void*)( Base + hdr->e_phoff ); + for( i = 0; i < hdr->e_phnum; i ++ ) { - if(phtab[i].Type == PT_LOAD && iBaseDiff > phtab[i].VAddr) - iBaseDiff = phtab[i].VAddr; - if( phtab[i].Type == PT_DYNAMIC ) { - dynTab = (void*)(intptr_t)phtab[i].VAddr; + if(phtab[i].p_type == PT_LOAD && iBaseDiff > phtab[i].p_vaddr) + iBaseDiff = phtab[i].p_vaddr; + if( phtab[i].p_type == PT_DYNAMIC ) { + dynTab = (void*)(intptr_t)phtab[i].p_vaddr; } } if( !dynTab ) { @@ -702,23 +726,17 @@ int Elf32GetSymbol(void *Base, const char *Name, void **ret) { // --- Symbol Table --- case DT_SYMTAB: - symtab = (void*)(intptr_t) dynTab[j].d_val; // Rebased in Relocate + symtab = (void*)(intptr_t) dynTab[j].d_un.d_val; // Rebased in Relocate break; case DT_STRTAB: - dynstrtab = (void*)(intptr_t) dynTab[j].d_val; + dynstrtab = (void*)(intptr_t) dynTab[j].d_un.d_val; break; // --- Hash Table -- case DT_HASH: - pBuckets = (void*)(intptr_t) dynTab[j].d_val; + pBuckets = (void*)(intptr_t) dynTab[j].d_un.d_val; break; } } - - #if 0 - hdr->misc.HashTable = pBuckets; - hdr->misc.SymTable = symtab; - hdr->misc.StrTab = dynstrtab; - #endif } nbuckets = pBuckets[0]; @@ -729,32 +747,29 @@ int Elf32GetSymbol(void *Base, const char *Name, void **ret) // Get hash iNameHash = ElfHashString(Name); iNameHash %= nbuckets; - //DEBUGS(" ElfGetSymbol: iNameHash = 0x%x", iNameHash); // Walk Chain i = pBuckets[ iNameHash ]; - //DEBUGS(" ElfGetSymbol: strcmp(Name, \"%s\")", symtab[i].name); - if(symtab[i].shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].nameOfs, Name) == 0) { - *ret = (void*) (intptr_t) symtab[ i ].value + iBaseDiff; + if(symtab[i].st_shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].st_name, Name) == 0) { + *Ret = (void*) (intptr_t) symtab[i].st_value + iBaseDiff; + DEBUGS("%s = %p", Name, *Ret); return 1; } - //DEBUGS(" ElfGetSymbol: Hash of first = 0x%x", ElfHashString( symtab[i].name ) % nbuckets); while(pChains[i] != STN_UNDEF) { - //DEBUGS(" pChains[%i] = %i", i, pChains[i]); i = pChains[i]; - //DEBUGS(" ElfGetSymbol: strcmp(Name, \"%s\")", symtab[ i ].name); - if(symtab[i].shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[ i ].nameOfs, Name) == 0) { - //DEBUGS("ElfGetSymbol: RETURN 1, '%s' = 0x%x", symtab[ i ].name, symtab[ i ].value); - *ret = (void*)(intptr_t)symtab[ i ].value + iBaseDiff; + if(symtab[i].st_shndx != SHN_UNDEF && strcmp(dynstrtab + symtab[i].st_name, Name) == 0) { + *Ret = (void*)(intptr_t)symtab[i].st_value + iBaseDiff; + DEBUGS("%s = %p", Name, *Ret); return 1; } } - //DEBUGS("ElfGetSymbol: RETURN 0, Symbol '%s' not found", Name); return 0; } +#endif + Uint32 ElfHashString(const char *name) { @@ -769,17 +784,3 @@ Uint32 ElfHashString(const char *name) return h; } -#if 0 -unsigned long elf_hash(const unsigned char *name) -{ - unsigned long h = 0, g; - while (*name) - { - h = (h << 4) + *name++; - if (g = h & 0xf0000000) - h ^= g >> 24; - h &= ~g; - } - return h; -} -#endif diff --git a/Usermode/Libraries/ld-acess.so_src/elf32.h b/Usermode/Libraries/ld-acess.so_src/elf32.h index 580bbb4a..be31517b 100644 --- a/Usermode/Libraries/ld-acess.so_src/elf32.h +++ b/Usermode/Libraries/ld-acess.so_src/elf32.h @@ -14,6 +14,10 @@ typedef int32_t Elf32_Sword; #define ELFCLASS32 1 +#define EM_NONE 0 +#define EM_386 3 +#define EM_ARM 40 + /** \struct elf_header_s \brief ELF File Header @@ -162,6 +166,37 @@ enum { R_386_LAST // none }; +// NOTES: +// 'T' means the thumb bit +// 'B(S)' Origin of a symbol +enum { + R_ARM_NONE, // No action + R_ARM_PC24, // ((S + A) | T) - P + R_ARM_ABS32, // (S + A) | T + R_ARM_REL32, // ((S + A) | T) - P + R_ARM_LDR_PC_G0, // S + A - P + R_ARM_ABS16, // S + A + R_ARM_ABS12, // S + A + R_ARM_THM_ABS5, // S + A + R_ARM_ABS8, // S + A + R_ARM_SBREL32, // ((S + A) | T) - B(S) + R_ARM_THM_CALL, // ((S + A) | T) - P + R_ARM_THM_PC8, // S + A - Pa, + R_ARM_BREL_ADJ, // ΔB(S) + A + R_ARM_TLS_DESC, // -- + R_ARM_THM_SWI8, // (Reserved) + R_ARM_XPC25, // (Reserved) + R_ARM_THM_XPC22, // (Reserved) + R_ARM_TLS_DTPMOD32, // Module[S] + R_ARM_TLS_DTPOFF32, // S + A - TLS + R_ARM_TLS_TPOFF32, // S + A - tp + R_ARM_COPY, // Misc + R_ARM_GLOB_DAT, // (S + A) | T + R_ARM_JUMP_SLOT, // (S + A) | T + R_ARM_RELATIVE, // B(S) + A (extra?) + // ... More defined (IHI0044) +}; + #define ELF32_R_SYM(i) ((i)>>8) // Takes an info value and returns a symbol index #define ELF32_R_TYPE(i) ((i)&0xFF) // Takes an info value and returns a type #define ELF32_R_INFO(s,t) (((s)<<8)+((t)&0xFF)) // Takes a type and symbol index and returns an info value diff --git a/Usermode/Libraries/ld-acess.so_src/elf64.h b/Usermode/Libraries/ld-acess.so_src/elf64.h index 4fe99ae9..fb39782a 100644 --- a/Usermode/Libraries/ld-acess.so_src/elf64.h +++ b/Usermode/Libraries/ld-acess.so_src/elf64.h @@ -9,6 +9,8 @@ #define ELFCLASS64 2 +#define EM_X86_64 62 + typedef uint16_t Elf64_Half; typedef uint32_t Elf64_Word; diff --git a/Usermode/Libraries/ld-acess.so_src/export.c b/Usermode/Libraries/ld-acess.so_src/export.c index d69c6d24..2da0e6b2 100644 --- a/Usermode/Libraries/ld-acess.so_src/export.c +++ b/Usermode/Libraries/ld-acess.so_src/export.c @@ -4,6 +4,14 @@ #include "common.h" #include +extern uint64_t __udivdi3(uint64_t Num, uint64_t Den); +extern uint64_t __umoddi3(uint64_t Num, uint64_t Den); +extern int32_t __divsi3(int32_t Num, int32_t Den); +extern int32_t __modsi3(int32_t Num, int32_t Den); +extern uint32_t __udivsi3(uint32_t Num, uint32_t Den); +extern uint32_t __umodsi3(uint32_t Num, uint32_t Den); + + #define _STR(x) #x #define STR(x) _STR(x) #define EXP(sym) {&sym, STR(sym)} @@ -62,8 +70,14 @@ const struct { EXP(_SysGetPhys), EXP(_SysAllocate), - EXP(_SysDebug) + EXP(_SysDebug), + EXP(__umoddi3), + EXP(__udivdi3), + EXP(__divsi3), + EXP(__modsi3), + EXP(__udivsi3), + EXP(__umodsi3) }; const int ciNumLocalExports = sizeof(caLocalExports)/sizeof(caLocalExports[0]); diff --git a/Usermode/Libraries/ld-acess.so_src/lib.c b/Usermode/Libraries/ld-acess.so_src/lib.c index d7a581e4..7c8005bc 100644 --- a/Usermode/Libraries/ld-acess.so_src/lib.c +++ b/Usermode/Libraries/ld-acess.so_src/lib.c @@ -162,6 +162,28 @@ int32_t __divsi3(int32_t Num, int32_t Den) return sign * __divmod32(Num, Den, NULL); } +int32_t __modsi3(int32_t Num, int32_t Den) +{ + int32_t sign = 1; + uint32_t tmp; + if(Num < 0) { + Num = -Num; + sign = -sign; + } + if(Den < 0) { + Den = -Den; + sign = -sign; + } + __divmod32(Num, Den, &tmp); + return ((int32_t)tmp)*sign; +} + +uint32_t __udivsi3(uint32_t Num, uint32_t Den) +{ + return __divmod32(Num, Den, NULL); +} + + uint32_t __umodsi3(uint32_t Num, uint32_t Den) { uint32_t ret;