From: John Hodge Date: Sun, 23 Oct 2011 03:58:29 +0000 (+0800) Subject: Usermode - Finally cleaned up ELF32 relocation X-Git-Tag: rel0.14~209 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=1b9f0a3100dd5bbfca97249cee2a4a98028e2eaf;p=tpg%2Facess2.git Usermode - Finally cleaned up ELF32 relocation - No more Uint32 and friends, uint32_t/Elf32_Word instead - Also moved definition of off_t (should be in unistd.h though) --- diff --git a/Usermode/Libraries/ld-acess.so_src/elf.c b/Usermode/Libraries/ld-acess.so_src/elf.c index 77dac9db..6051eafd 100644 --- a/Usermode/Libraries/ld-acess.so_src/elf.c +++ b/Usermode/Libraries/ld-acess.so_src/elf.c @@ -1,6 +1,9 @@ /* - * AcessOS 1 - Dynamic Loader - * By thePowersGang + * Acess2 Dynamic Linker + * - By John Hodge (thePowersGang) + * + * elf.c + * - ELF32/ELF64 relocation */ #define DEBUG 0 @@ -34,7 +37,7 @@ void *Elf32Relocate(void *Base, char **envp, const char *Filename); void *Elf64Relocate(void *Base, char **envp, const char *Filename); 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 === /** @@ -89,10 +92,9 @@ void *Elf32Relocate(void *Base, char **envp, const char *Filename) intptr_t iRealBase = -1; intptr_t iBaseDiff; int iSegmentCount; - int iSymCount; +// 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; @@ -167,7 +169,7 @@ void *Elf32Relocate(void *Base, char **envp, const char *Filename) // --- Hash Table -- case DT_HASH: if(iBaseDiff != 0) dynamicTab[j].d_val += iBaseDiff; - iSymCount = ((Elf32_Word*)(intptr_t)dynamicTab[j].d_val)[1]; +// iSymCount = ((Elf32_Word*)(intptr_t)dynamicTab[j].d_val)[1]; break; } } @@ -205,7 +207,7 @@ void *Elf32Relocate(void *Base, char **envp, const char *Filename) 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; @@ -239,7 +241,7 @@ void *Elf32Relocate(void *Base, char **envp, const char *Filename) void elf_doRelocate_386(uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int type, int bRela, const char *Sym) { - Uint32 val; + intptr_t val; switch( type ) { // Standard 32 Bit Relocation (S+A) @@ -335,7 +337,7 @@ void *Elf32Relocate(void *Base, char **envp, const char *Filename) // 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++ ) @@ -348,7 +350,7 @@ void *Elf32Relocate(void *Base, char **envp, const char *Filename) // 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++ ) @@ -361,7 +363,7 @@ void *Elf32Relocate(void *Base, char **envp, const char *Filename) // === 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) { @@ -396,10 +398,10 @@ int Elf32GetSymbol(void *Base, const char *Name, void **ret, size_t *Size) Elf32_Ehdr *hdr = Base; Elf32_Sym *symtab; int nbuckets = 0; - int iSymCount = 0; +// int iSymCount = 0; int i; - Uint32 *pBuckets; - Uint32 *pChains; + Elf32_Word *pBuckets; + Elf32_Word *pChains; uint32_t iNameHash; const char *dynstrtab; uintptr_t iBaseDiff = -1; @@ -441,7 +443,7 @@ int Elf32GetSymbol(void *Base, const char *Name, void **ret, size_t *Size) } nbuckets = pBuckets[0]; - iSymCount = pBuckets[1]; +// iSymCount = pBuckets[1]; pBuckets = &pBuckets[2]; pChains = &pBuckets[ nbuckets ]; @@ -511,16 +513,16 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename) 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 @@ -530,15 +532,15 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename) { 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; } } @@ -571,7 +573,7 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename) // 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); @@ -585,7 +587,7 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename) 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); @@ -599,7 +601,7 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename) 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; @@ -621,7 +623,7 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename) case R_X86_64_NONE: break; case R_X86_64_64: - *(uint64_t*)ptr = (uint64_t)GetSymbol(symname, NULL) + addend; + *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL) + addend; break; case R_X86_64_COPY: { size_t size; @@ -629,13 +631,13 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename) memcpy(ptr, sym, size); } break; case R_X86_64_GLOB_DAT: - *(uint64_t*)ptr = (uint64_t)GetSymbol(symname, NULL); + *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL); break; case R_X86_64_JUMP_SLOT: - *(uint64_t*)ptr = (uint64_t)GetSymbol(symname, NULL); + *(uint64_t*)ptr = (uintptr_t)GetSymbol(symname, NULL); break; case R_X86_64_RELATIVE: - *(uint64_t*)ptr = (intptr_t)Base + addend; + *(uint64_t*)ptr = (uintptr_t)Base + addend; break; default: SysDebug("ld-acess - _Elf64DoReloc: Unknown relocation type %i", type); @@ -648,7 +650,7 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename) DEBUGS("rel_count = %i", rel_count); for( i = 0; i < rel_count; i ++ ) { - uint64_t *ptr = (void *)( rel[i].r_offset + baseDiff ); + uint64_t *ptr = (void *)(uintptr_t)( rel[i].r_offset + baseDiff ); _Elf64DoReloc( rel[i].r_info, ptr, *ptr); } } @@ -658,7 +660,8 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename) 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 ); + _Elf64DoReloc( rela[i].r_info, ptr, rela[i].r_addend ); } } @@ -670,7 +673,7 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename) DEBUGS("plt rel count = %i", count); for( i = 0; i < count; i ++ ) { - uint64_t *ptr = (void *)( plt[i].r_offset + baseDiff ); + uint64_t *ptr = (void *)(uintptr_t)( plt[i].r_offset + baseDiff ); _Elf64DoReloc( plt[i].r_info, ptr, *ptr); } } @@ -680,13 +683,14 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename) 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 ); + _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); + return (void *)(uintptr_t)(hdr->e_entry + baseDiff); } int Elf64GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size) @@ -694,7 +698,7 @@ 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; @@ -750,7 +754,7 @@ int Elf64GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size) } nbuckets = pBuckets[0]; - iSymCount = pBuckets[1]; +// iSymCount = pBuckets[1]; pBuckets = &pBuckets[2]; pChains = &pBuckets[ nbuckets ]; @@ -783,12 +787,12 @@ int Elf64GetSymbol(void *Base, const char *Name, void **Ret, size_t *Size) #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; diff --git a/Usermode/Libraries/ld-acess.so_src/elf32.h b/Usermode/Libraries/ld-acess.so_src/elf32.h index be31517b..08087473 100644 --- a/Usermode/Libraries/ld-acess.so_src/elf32.h +++ b/Usermode/Libraries/ld-acess.so_src/elf32.h @@ -8,7 +8,9 @@ #include +typedef uint16_t Elf32_Half; typedef uint32_t Elf32_Addr; +typedef uint32_t Elf32_Off; typedef uint32_t Elf32_Word; typedef int32_t Elf32_Sword; @@ -24,19 +26,19 @@ typedef int32_t Elf32_Sword; */ struct sElf32_Ehdr { uint8_t e_ident[16]; //!< Identifier Bytes - uint16_t filetype; //!< File Type - Uint16 machine; //!< Machine / Arch - Uint32 version; //!< Version (File?) - Uint32 entrypoint; //!< Entry Point - Uint32 phoff; //!< Program Header Offset - Uint32 shoff; //!< Section Header Offset - Uint32 flags; //!< Flags - Uint16 headersize; //!< Header Size - Uint16 phentsize; //!< Program Header Entry Size - Uint16 phentcount; //!< Program Header Entry Count - Uint16 shentsize; //!< Section Header Entry Size - Uint16 shentcount; //!< Section Header Entry Count - Uint16 shstrindex; //!< Section Header String Table Index + Elf32_Half filetype; //!< File Type + Elf32_Half machine; //!< Machine / Arch + Elf32_Word version; //!< Version (File?) + Elf32_Addr entrypoint; //!< Entry Point + Elf32_Off phoff; //!< Program Header Offset + Elf32_Word shoff; //!< Section Header Offset + Elf32_Word flags; //!< Flags + Elf32_Half headersize; //!< Header Size + Elf32_Half phentsize; //!< Program Header Entry Size + Elf32_Half phentcount; //!< Program Header Entry Count + Elf32_Half shentsize; //!< Section Header Entry Size + Elf32_Half shentcount; //!< Section Header Entry Count + Elf32_Half shstrindex; //!< Section Header String Table Index }; /** @@ -89,6 +91,7 @@ enum eElfSectionTypes { SHT_HIUSER = 0xffffffff }; +#if 0 #define SHF_WRITE 0x1 #define SHF_ALLOC 0x2 #define SHF_EXECINSTR 0x4 @@ -106,14 +109,15 @@ struct sElf32_Shent { Uint32 addralign; Uint32 entsize; }; //sizeof = 40 +#endif struct elf_sym_s { - Uint32 nameOfs; - Uint32 value; //Address - Uint32 size; - Uint8 info; - Uint8 other; - Uint16 shndx; + Elf32_Word nameOfs; + Elf32_Addr value; //Address + Elf32_Word size; + uint8_t info; + uint8_t other; + Elf32_Half shndx; }; #define STN_UNDEF 0 // Undefined Symbol @@ -130,24 +134,24 @@ enum { }; struct sElf32_Phdr { - Uint32 Type; - Uint32 Offset; + Elf32_Word Type; + Elf32_Off Offset; Elf32_Addr VAddr; Elf32_Addr PAddr; - Uint32 FileSize; - Uint32 MemSize; - Uint32 Flags; - Uint32 Align; + Elf32_Word FileSize; + Elf32_Word MemSize; + Elf32_Word Flags; + Elf32_Word Align; }; struct elf32_rel_s { - Uint32 r_offset; - Uint32 r_info; + Elf32_Addr r_offset; + Elf32_Word r_info; }; struct elf32_rela_s { - Uint32 r_offset; - Uint32 r_info; + Elf32_Addr r_offset; + Elf32_Word r_info; Elf32_Sword r_addend; }; @@ -202,8 +206,8 @@ enum { #define ELF32_R_INFO(s,t) (((s)<<8)+((t)&0xFF)) // Takes a type and symbol index and returns an info value struct elf32_dyn_s { - Uint16 d_tag; - Uint32 d_val; //Also d_ptr + Elf32_Half d_tag; + Elf32_Word d_val; //Also d_ptr }; enum { diff --git a/Usermode/include/stdint.h b/Usermode/include/stdint.h index 0e44f142..477f8ebd 100644 --- a/Usermode/include/stdint.h +++ b/Usermode/include/stdint.h @@ -26,6 +26,6 @@ typedef uint32_t uintptr_t; # error "Unknown pointer size" #endif -typedef uint64_t off_t; +//typedef uint64_t off_t; #endif diff --git a/Usermode/include/sys/types.h b/Usermode/include/sys/types.h index 687457e4..fe17994a 100644 --- a/Usermode/include/sys/types.h +++ b/Usermode/include/sys/types.h @@ -13,6 +13,7 @@ typedef struct stat t_fstat; typedef unsigned long pid_t; typedef unsigned long tid_t; typedef signed long long int time_t; +typedef long long int off_t; typedef unsigned int uint; @@ -51,6 +52,6 @@ extern void FD_CLR(int fd, fd_set *fdsetp); extern void FD_SET(int fd, fd_set *fdsetp); extern int FD_ISSET(int fd, fd_set *fdsetp); -#include +#include "../sys/stat.h" #endif