From: John Hodge Date: Fri, 26 Aug 2011 03:14:33 +0000 (+0800) Subject: Usermode/ld-acess - Fixing ELF loader modifying .text during relocation X-Git-Tag: rel0.11~120 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=5114cd4c1f8e36f62744a8efa346d619c99bdaf6;p=tpg%2Facess2.git Usermode/ld-acess - Fixing ELF loader modifying .text during relocation --- diff --git a/Usermode/Libraries/Makefile.tpl b/Usermode/Libraries/Makefile.tpl index 350eb08f..5ca97a91 100644 --- a/Usermode/Libraries/Makefile.tpl +++ b/Usermode/Libraries/Makefile.tpl @@ -35,7 +35,7 @@ $(_BIN): $(OBJ) $(_LIBS) @mkdir -p $(dir $(_BIN)) @echo [LD] -o $(BIN) $(OBJ) @$(LD) $(LDFLAGS) -o $(_BIN) $(OBJ) - @$(DISASM) $(_BIN) > $(_OBJPREFIX)$(BIN).dsm + @$(DISASM) -S $(_BIN) > $(_OBJPREFIX)$(BIN).dsm $(_OBJPREFIX)%.o: %.c @echo [CC] -o $@ diff --git a/Usermode/Libraries/ld-acess.so_src/Makefile b/Usermode/Libraries/ld-acess.so_src/Makefile index 87b82130..4b99ecd7 100644 --- a/Usermode/Libraries/ld-acess.so_src/Makefile +++ b/Usermode/Libraries/ld-acess.so_src/Makefile @@ -9,9 +9,9 @@ OBJ += arch/$(ARCHDIR).ao BIN = ld-acess.so EXTRABIN := libld-acess.so -CFLAGS = -Wall -fno-builtin -fno-leading-underscore -fno-stack-protector -fPIC +CFLAGS = -g -Wall -fno-builtin -fno-leading-underscore -fno-stack-protector -fPIC CFLAGS += $(CPPFLAGS) -LDFLAGS = -T arch/$(ARCHDIR).ld -Map map.txt --export-dynamic +LDFLAGS = -g -T arch/$(ARCHDIR).ld -Map map.txt --export-dynamic include ../Makefile.tpl diff --git a/Usermode/Libraries/ld-acess.so_src/common.h b/Usermode/Libraries/ld-acess.so_src/common.h index ec254bf2..7d95540a 100644 --- a/Usermode/Libraries/ld-acess.so_src/common.h +++ b/Usermode/Libraries/ld-acess.so_src/common.h @@ -42,8 +42,8 @@ extern void *DoRelocate(void *Base, char **envp, char *Filename); // === Library/Symbol Manipulation == extern void *LoadLibrary(char *filename, char *SearchDir, char **envp); extern void AddLoaded(char *File, void *base); -extern void *GetSymbol(char *name); -extern int GetSymbolFromBase(void *base, char *name, void **ret); +extern void *GetSymbol(const char *name); +extern int GetSymbolFromBase(void *base, const char *name, void **ret); // === Library Functions === extern char *strcpy(char *dest, const char *src); @@ -63,7 +63,7 @@ extern int open(const char *filename, int flags); extern int close(int fd); // === ELF Loader === -extern int ElfGetSymbol(void *Base, char *name, void **ret); +extern int ElfGetSymbol(void *Base, const char *name, void **ret); // === PE Loader === extern int PE_GetSymbol(void *Base, char *Name, void **ret); diff --git a/Usermode/Libraries/ld-acess.so_src/elf.c b/Usermode/Libraries/ld-acess.so_src/elf.c index bcec1dc6..b969a19c 100644 --- a/Usermode/Libraries/ld-acess.so_src/elf.c +++ b/Usermode/Libraries/ld-acess.so_src/elf.c @@ -21,8 +21,7 @@ static const char *csaR_NAMES[] = {"R_386_NONE", "R_386_32", "R_386_PC32", "R_38 #endif // === PROTOTYPES === -void elf_doRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symtab, Uint base); -Uint ElfHashString(char *name); +Uint32 ElfHashString(const char *name); // === CODE === /** @@ -50,7 +49,7 @@ void *ElfRelocate(void *Base, char **envp, char *Filename) char *dynstrtab = NULL; // .dynamic String Table Elf32_Sym *dynsymtab; - DEBUGS("ElfRelocate: (Base=0x%x)\n", Base); + DEBUGS("ElfRelocate: (Base=0x%x)", Base); // Check magic header @@ -67,7 +66,7 @@ void *ElfRelocate(void *Base, char **envp, char *Filename) // Find Dynamic Section if(phtab[i].Type == PT_DYNAMIC) { if(dynamicTab) { - DEBUGS(" WARNING - elf_relocate: Multiple PT_DYNAMIC segments\n"); + DEBUGS(" WARNING - elf_relocate: Multiple PT_DYNAMIC segments"); continue; } dynamicTab = (void *) (intptr_t) phtab[i].VAddr; @@ -77,17 +76,17 @@ void *ElfRelocate(void *Base, char **envp, char *Filename) // Page Align real base iRealBase &= ~0xFFF; - DEBUGS(" elf_relocate: True Base = 0x%x, Compiled Base = 0x%x\n", Base, iRealBase); + DEBUGS(" elf_relocate: True Base = 0x%x, Compiled Base = 0x%x", Base, iRealBase); // Adjust "Real" Base iBaseDiff = (intptr_t)Base - iRealBase; - hdr->entrypoint += iBaseDiff; // Adjust Entrypoint +// hdr->entrypoint += iBaseDiff; // Adjust Entrypoint // Check if a PT_DYNAMIC segement was found if(!dynamicTab) { - SysDebug(" elf_relocate: No PT_DYNAMIC segment in image, returning\n"); - return (void *)hdr->entrypoint; + SysDebug(" elf_relocate: No PT_DYNAMIC segment in image %p, returning", Base); + return (void *)hdr->entrypoint + iBaseDiff; } // Adjust Dynamic Table @@ -100,63 +99,65 @@ void *ElfRelocate(void *Base, char **envp, char *Filename) { // --- Symbol Table --- case DT_SYMTAB: - DEBUGS(" elf_relocate: DYNAMIC Symbol Table 0x%x (0x%x)\n", + DEBUGS(" elf_relocate: DYNAMIC Symbol Table 0x%x (0x%x)", dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff); - dynamicTab[j].d_val += iBaseDiff; + if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff; dynsymtab = (void*)(dynamicTab[j].d_val); - hdr->misc.SymTable = dynamicTab[j].d_val; // Saved in unused bytes of ident +// hdr->misc.SymTable = dynamicTab[j].d_val; // Saved in unused bytes of ident break; // --- String Table --- case DT_STRTAB: - DEBUGS(" elf_relocate: DYNAMIC String Table 0x%x (0x%x)\n", + DEBUGS(" elf_relocate: DYNAMIC String Table 0x%x (0x%x)", dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff); - dynamicTab[j].d_val += iBaseDiff; + if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff; dynstrtab = (void*)(dynamicTab[j].d_val); break; // --- Hash Table -- case DT_HASH: - dynamicTab[j].d_val += iBaseDiff; + if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff; iSymCount = ((Uint*)(dynamicTab[j].d_val))[1]; - hdr->misc.HashTable = dynamicTab[j].d_val; // Saved in unused bytes of ident +// hdr->misc.HashTable = dynamicTab[j].d_val; // Saved in unused bytes of ident break; } } if(dynsymtab == NULL) { - SysDebug("ld-acess.so - WARNING: No Dynamic Symbol table, returning\n"); - return (void *) hdr->entrypoint; + SysDebug("ld-acess.so - WARNING: No Dynamic Symbol table in %p, returning", hdr); + return (void *) hdr->entrypoint + iBaseDiff; } - + + #if 0 // Alter Symbols to true base for(i=0;i DT_JMPREL) continue; - //DEBUGS(" elf_relocate: %i-%i = %s,0x%x\n", + //DEBUGS(" elf_relocate: %i-%i = %s,0x%x", // i,j, csaDT_NAMES[dynamicTab[j].d_tag],dynamicTab[j].d_val); break; } } - DEBUGS(" elf_relocate: Beginning Relocation\n"); + DEBUGS(" elf_relocate: Beginning Relocation"); + + void elf_doRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symtab) + { + 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 ); + DEBUGS(" elf_doRelocate: R_386_32 *0x%x += 0x%x('%s')", + ptr, val, symname); + *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: R_386_PC32 *0x%x = 0x%x + 0x%x - 0x%x", + ptr, *ptr, val, (Uint)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: #%i: '%s'", sym, symname); + val = (intptr_t) GetSymbol( symname ); + 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: + DEBUGS(" elf_doRelocate: Rel 0x%x: 0x%x,%s", ptr, sym, csaR_NAMES[type]); + break; + } + } // Parse Relocation Entries if(rel && relSz) { Uint32 *ptr; - DEBUGS(" elf_relocate: rel=0x%x, relSz=0x%x, relEntSz=0x%x\n", rel, relSz, relEntSz); + DEBUGS(" elf_relocate: rel=0x%x, relSz=0x%x, relEntSz=0x%x", rel, relSz, relEntSz); j = relSz / relEntSz; for( i = 0; i < j; i++ ) { - //DEBUGS(" Rel %i: 0x%x+0x%x\n", i, iBaseDiff, rel[i].r_offset); + //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, iBaseDiff); + elf_doRelocate(rel[i].r_info, ptr, *ptr, dynsymtab); } } // Parse Relocation Entries if(rela && relaSz) { Uint32 *ptr; - DEBUGS(" elf_relocate: rela=0x%x, relaSz=0x%x, relaEntSz=0x%x\n", rela, relaSz, relaEntSz); + DEBUGS(" elf_relocate: rela=0x%x, relaSz=0x%x, relaEntSz=0x%x", rela, relaSz, relaEntSz); j = relaSz / relaEntSz; 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, iBaseDiff); + elf_doRelocate(rel[i].r_info, ptr, rela[i].r_addend, dynsymtab); } } @@ -224,101 +272,65 @@ void *ElfRelocate(void *Base, char **envp, char *Filename) if(plt && pltSz) { Uint32 *ptr; - DEBUGS(" elf_relocate: Relocate PLT, plt=0x%x\n", plt); + 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\n", j); + DEBUGS(" elf_relocate: PLT Reloc Type = Rel, %i entries", j); for(i=0;ientrypoint); - return (void*)hdr->entrypoint; + DEBUGS("ElfRelocate: RETURN 0x%x", hdr->entrypoint + iBaseDiff); + return (void*)hdr->entrypoint + iBaseDiff; } -void elf_doRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symtab, Uint base) -{ - int type = ELF32_R_TYPE(r_info); - int sym = ELF32_R_SYM(r_info); - Uint32 val; - switch( type ) - { - // Standard 32 Bit Relocation (S+A) - case R_386_32: - val = (intptr_t) GetSymbol( symtab[sym].name ); - DEBUGS(" elf_doRelocate: R_386_32 *0x%x += 0x%x('%s')\n", - ptr, val, symtab[sym].name); - *ptr = val + addend; - break; - - // 32 Bit Relocation wrt. Offset (S+A-P) - case R_386_PC32: - DEBUGS(" elf_doRelocate: #%i: '%s'\n", sym, symtab[sym].name); - val = (intptr_t) GetSymbol( symtab[sym].name ); - DEBUGS(" elf_doRelocate: R_386_PC32 *0x%x = 0x%x + 0x%x - 0x%x\n", - ptr, *ptr, val, (Uint)ptr ); - *ptr = val + addend - (intptr_t)ptr; - //*ptr = val + addend - ((Uint)ptr - base); - break; - - // Absolute Value of a symbol (S) - case R_386_GLOB_DAT: - case R_386_JMP_SLOT: - DEBUGS(" elf_doRelocate: #%i: '%s'\n", sym, symtab[sym].name); - val = (intptr_t) GetSymbol( symtab[sym].name ); - DEBUGS(" elf_doRelocate: %s *0x%x = 0x%x\n", 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\n", ptr, base, addend); - *ptr = base + addend; - break; - - default: - DEBUGS(" elf_doRelocate: Rel 0x%x: 0x%x,%s\n", ptr, sym, csaR_NAMES[type]); - break; - } - -} /** - * \fn int ElfGetSymbol(Uint Base, char *name, void **ret) + * \fn int ElfGetSymbol(Uint Base, const char *name, void **ret) */ -int ElfGetSymbol(void *Base, char *Name, void **ret) +int ElfGetSymbol(void *Base, const char *Name, void **ret) { Elf32_Ehdr *hdr = Base; Elf32_Sym *symtab; int nbuckets = 0; int iSymCount = 0; int i; - Uint *pBuckets; - Uint *pChains; + Uint32 *pBuckets; + Uint32 *pChains; Uint iNameHash; + const char *dynstrtab; + uintptr_t iBaseDiff = -1; - //DEBUGS("ElfGetSymbol: (Base=0x%x, Name='%s')\n", Base, Name); + //DEBUGS("ElfGetSymbol: (Base=0x%x, Name='%s')", Base, Name); + #if 0 + pBuckets = (void *) (intptr_t) hdr->misc.HashTable; + symtab = (void *) (intptr_t) hdr->misc.SymTable; + dynstrtab = (void *) (intptr_t) hdr->misc.StrTab; + #else + dynstrtab = NULL; + pBuckets = NULL; + symtab = NULL; + #endif // Catch the current executable - #if 0 - if( !hdr->misc.HashTable ) + if( !pBuckets ) { Elf32_Phdr *phtab; Elf32_Dyn *dynTab = NULL; @@ -328,15 +340,18 @@ int ElfGetSymbol(void *Base, char *Name, void **ret) phtab = (void*)( Base + hdr->phoff ); for( i = 0; i < hdr->phentcount; i ++ ) { + if(phtab[i].Type == PT_LOAD && iBaseDiff > phtab[i].VAddr) + iBaseDiff = phtab[i].VAddr; if( phtab[i].Type == PT_DYNAMIC ) { dynTab = (void*)phtab[i].VAddr; - break ; } } if( !dynTab ) { - SysDebug("ERROR - Unable to find DYNAMIC segment in %p", (void*)Base); + SysDebug("ERROR - Unable to find DYNAMIC segment in %p"); return 0; } + 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++) { @@ -344,24 +359,25 @@ int ElfGetSymbol(void *Base, char *Name, void **ret) { // --- Symbol Table --- case DT_SYMTAB: - hdr->misc.SymTable = dynTab[j].d_val; + symtab = (void*)(intptr_t) dynTab[j].d_val; // Rebased in Relocate + break; + case DT_STRTAB: + dynstrtab = (void*)(intptr_t) dynTab[j].d_val; break; // --- Hash Table -- case DT_HASH: - hdr->misc.HashTable = dynTab[j].d_val; + pBuckets = (void*)(intptr_t) dynTab[j].d_val; break; } } - } - #endif - - if( !hdr->misc.SymTable || !hdr->misc.HashTable ) { - return 0; + + #if 0 + hdr->misc.HashTable = pBucktets; + hdr->misc.SymTable = symtab; + hdr->misc.StrTab = dynstrtab; + #endif } - pBuckets = (void *) (intptr_t) hdr->misc.HashTable; - symtab = (void *) (intptr_t) hdr->misc.SymTable; - nbuckets = pBuckets[0]; iSymCount = pBuckets[1]; pBuckets = &pBuckets[2]; @@ -370,39 +386,39 @@ int ElfGetSymbol(void *Base, char *Name, void **ret) // Get hash iNameHash = ElfHashString(Name); iNameHash %= nbuckets; - //DEBUGS(" ElfGetSymbol: iNameHash = 0x%x\n", iNameHash); + //DEBUGS(" ElfGetSymbol: iNameHash = 0x%x", iNameHash); // Walk Chain i = pBuckets[ iNameHash ]; - //DEBUGS(" ElfGetSymbol: strcmp(Name, \"%s\")\n", symtab[i].name); - if(symtab[i].shndx != SHN_UNDEF && strcmp(symtab[i].name, Name) == 0) { - *ret = (void*) (intptr_t) symtab[ i ].value; + //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; return 1; } - //DEBUGS(" ElfGetSymbol: Hash of first = 0x%x\n", ElfHashString( symtab[i].name ) % nbuckets); + //DEBUGS(" ElfGetSymbol: Hash of first = 0x%x", ElfHashString( symtab[i].name ) % nbuckets); while(pChains[i] != STN_UNDEF) { - //DEBUGS(" pChains[%i] = %i\n", i, pChains[i]); + //DEBUGS(" pChains[%i] = %i", i, pChains[i]); i = pChains[i]; - //DEBUGS(" ElfGetSymbol: strcmp(Name, \"%s\")\n", symtab[ i ].name); - if(symtab[i].shndx != SHN_UNDEF && strcmp(symtab[ i ].name, Name) == 0) { - //DEBUGS("ElfGetSymbol: RETURN 1, '%s' = 0x%x\n", symtab[ i ].name, symtab[ i ].value); - *ret = (void*)(intptr_t)symtab[ i ].value; + //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; return 1; } } - //DEBUGS("ElfGetSymbol: RETURN 0, Symbol '%s' not found\n", Name); + //DEBUGS("ElfGetSymbol: RETURN 0, Symbol '%s' not found", Name); return 0; } -Uint ElfHashString(char *name) +Uint32 ElfHashString(const char *name) { - Uint h = 0, g; + Uint32 h = 0, g; while(*name) { - h = (h << 4) + *name++; + h = (h << 4) + *(Uint8*)name++; if( (g = h & 0xf0000000) ) h ^= g >> 24; h &= ~g; diff --git a/Usermode/Libraries/ld-acess.so_src/elf32.h b/Usermode/Libraries/ld-acess.so_src/elf32.h index eededf4e..5e43987d 100644 --- a/Usermode/Libraries/ld-acess.so_src/elf32.h +++ b/Usermode/Libraries/ld-acess.so_src/elf32.h @@ -14,10 +14,10 @@ struct sElf32_Ehdr { union { char ident[16]; //!< Identifier Bytes struct { - Uint Ident1; - Uint Ident2; - Uint HashTable; - Uint SymTable; + Uint32 Ident1; + Uint32 StrTab; + Uint32 HashTable; + Uint32 SymTable; } misc; }; Uint16 filetype; //!< File Type @@ -104,10 +104,7 @@ struct sElf32_Shent { }; //sizeof = 40 struct elf_sym_s { - union { - Uint32 nameOfs; - char *name; - }; + Uint32 nameOfs; Uint32 value; //Address Uint32 size; Uint8 info; diff --git a/Usermode/Libraries/ld-acess.so_src/loadlib.c b/Usermode/Libraries/ld-acess.so_src/loadlib.c index da74b824..88297d5f 100644 --- a/Usermode/Libraries/ld-acess.so_src/loadlib.c +++ b/Usermode/Libraries/ld-acess.so_src/loadlib.c @@ -14,7 +14,7 @@ // === PROTOTYPES === void *IsFileLoaded(char *file); - int GetSymbolFromBase(void *base, char *name, void **ret); + int GetSymbolFromBase(void *base, const char *name, void **ret); // === IMPORTS === extern const struct { @@ -192,10 +192,10 @@ void Unload(void *Base) } /** - \fn Uint GetSymbol(char *name) + \fn Uint GetSymbol(const char *name) \brief Gets a symbol value from a loaded library */ -void *GetSymbol(char *name) +void *GetSymbol(const char *name) { int i; void *ret; @@ -207,7 +207,8 @@ void *GetSymbol(char *name) return caLocalExports[i].Value; } - for(i=0;i