From: John Hodge Date: Mon, 20 Dec 2010 01:54:45 +0000 (+0845) Subject: AcessNative - ld-acess - Woking on ELF loading X-Git-Tag: rel0.07~32 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=0575e67eb7c1f96fd88e4fd89f919edce0c30f35;p=tpg%2Facess2.git AcessNative - ld-acess - Woking on ELF loading --- diff --git a/AcessNative/ld-acess.so_src/Makefile b/AcessNative/ld-acess.so_src/Makefile index 3531f710..ba3a9460 100644 --- a/AcessNative/ld-acess.so_src/Makefile +++ b/AcessNative/ld-acess.so_src/Makefile @@ -5,16 +5,18 @@ ifeq ($(PLATFORM),) PLATFORM := lin endif -OBJ := syscalls.$(PLATFORM).o request.$(PLATFORM).o +OBJ := main.o syscalls.o request.o +OBJ += elf.o +OBJ := $(addsuffix .$(PLATFORM),$(OBJ)) ifeq ($(PLATFORM),win) - BIN := ../libacess.dll + BIN := ../ld-acess.exe endif ifeq ($(PLATFORM),lin) - BIN := ../libacess.so - CFLAGS += -fPIC + BIN := ../ld-acess endif +CFLAGS += -Wall -Werror .PHONY: all clean @@ -26,6 +28,6 @@ clean: $(BIN): $(OBJ) $(CC) -shared -o $@ $< -%.$(PLATFORM).o: %.c +%.o.$(PLATFORM): %.c $(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS) diff --git a/AcessNative/ld-acess.so_src/binary.c b/AcessNative/ld-acess.so_src/binary.c new file mode 100644 index 00000000..8378c6c7 --- /dev/null +++ b/AcessNative/ld-acess.so_src/binary.c @@ -0,0 +1,9 @@ +/* + * AcessNative + */ + +// === CODE === +int Binary_GetSymbol(const char *SymbolName, intptr_t *Value) +{ + return 0; +} diff --git a/AcessNative/ld-acess.so_src/common.h b/AcessNative/ld-acess.so_src/common.h new file mode 100644 index 00000000..276938f4 --- /dev/null +++ b/AcessNative/ld-acess.so_src/common.h @@ -0,0 +1,17 @@ +/* + */ +#ifndef _COMMON_H_ +#define _COMMON_H_ + +#include + +extern int Binary_GetSymbol(const char *SymbolName, intptr_t *Value); +extern int Binary_LoadLibrary(const char *Path); + +extern int AllocateMemory(intptr_t VirtAddr, size_t ByteCount); + +extern int Warning(const char *Format, ...); +extern int Notice(const char *Format, ...); + +#endif + diff --git a/AcessNative/ld-acess.so_src/elf.c b/AcessNative/ld-acess.so_src/elf.c new file mode 100644 index 00000000..35ac56b4 --- /dev/null +++ b/AcessNative/ld-acess.so_src/elf.c @@ -0,0 +1,490 @@ +/* + * Acess v0.1 + * ELF Executable Loader Code + */ +#define DEBUG 0 +#include +#include +#include +#include +#include "common.h" +#include "elf.h" + +#define DEBUG_WARN 1 + +#define ENTER(...) +#define LOG(...) +#define LEAVE(...) + +// === PROTOTYPES === + int Elf_Load(int fd); + int Elf_Relocate(void *Base); + int Elf_GetSymbol(void *Base, char *Name, intptr_t *ret); + int Elf_Int_DoRelocate(uint32_t r_info, uint32_t *ptr, uint32_t addend, Elf32_Sym *symtab, intptr_t base); +uint32_t Elf_Int_HashString(char *str); + +// === CODE === +int Elf_Load(int FD) +{ + Elf32_Ehdr hdr; + Elf32_Phdr *phtab; + int i, j; + int iPageCount; + uint32_t max, base = -1; + uint32_t addr; + + ENTER("xFD", FD); + + // Read ELF Header + read(FD, &hdr, sizeof(hdr)); + + // Check the file type + if(hdr.ident[0] != 0x7F || hdr.ident[1] != 'E' || hdr.ident[2] != 'L' || hdr.ident[3] != 'F') { + Warning("Non-ELF File was passed to the ELF loader\n"); + LEAVE('n'); + return 1; + } + + // Check for a program header + if(hdr.phoff == 0) { + #if DEBUG_WARN + Warning("ELF File does not contain a program header\n"); + #endif + LEAVE('n'); + return 1; + } + + // Read Program Header Table + phtab = malloc( sizeof(Elf32_Phdr) * hdr.phentcount ); + if( !phtab ) { + LEAVE('n'); + return 1; + } + LOG("hdr.phoff = 0x%08x", hdr.phoff); + lseek(FD, hdr.phoff, SEEK_SET); + read(FD, phtab, sizeof(Elf32_Phdr)*hdr.phentcount); + + // Count Pages + iPageCount = 0; + LOG("hdr.phentcount = %i", hdr.phentcount); + for( i = 0; i < hdr.phentcount; i++ ) + { + // Ignore Non-LOAD types + if(phtab[i].Type != PT_LOAD) + continue; + iPageCount += ((phtab[i].VAddr&0xFFF) + phtab[i].MemSize + 0xFFF) >> 12; + LOG("phtab[%i] = {VAddr:0x%x, MemSize:0x%x}", i, phtab[i].VAddr, phtab[i].MemSize); + } + + LOG("iPageCount = %i", iPageCount); + + // Allocate Information Structure + //ret = malloc( sizeof(tBinary) + sizeof(tBinaryPage)*iPageCount ); + // Fill Info Struct + //ret->Entry = hdr.entrypoint; + //ret->Base = -1; // Set Base to maximum value + //ret->NumPages = iPageCount; + //ret->Interpreter = NULL; + + // Prescan for base and size + for( i = 0; i < hdr.phentcount; i ++) + { + if( phtab[i].Type != PT_LOAD ) + continue; + if( phtab[i].VAddr < base ) + base = phtab[i].VAddr; + if( phtab[i].VAddr > max ) + max = phtab[i].VAddr; + } + + LOG("base = %08x, max = %08x", base, max); + + // Load Pages + j = 0; + for( i = 0; i < hdr.phentcount; i++ ) + { + //LOG("phtab[%i].Type = 0x%x", i, phtab[i].Type); + LOG("phtab[%i] = {", i); + LOG(" .Type = 0x%08x", phtab[i].Type); + LOG(" .Offset = 0x%08x", phtab[i].Offset); + LOG(" .VAddr = 0x%08x", phtab[i].VAddr); + LOG(" .PAddr = 0x%08x", phtab[i].PAddr); + LOG(" .FileSize = 0x%08x", phtab[i].FileSize); + LOG(" .MemSize = 0x%08x", phtab[i].MemSize); + LOG(" .Flags = 0x%08x", phtab[i].Flags); + LOG(" .Align = 0x%08x", phtab[i].Align); + LOG(" }"); + // Get Interpreter Name + if( phtab[i].Type == PT_INTERP ) + { + char *tmp; + //if(ret->Interpreter) continue; + tmp = malloc(phtab[i].FileSize); + lseek(FD, phtab[i].Offset, 1); + read(FD, tmp, phtab[i].FileSize); + //ret->Interpreter = Binary_RegInterp(tmp); + LOG("Interpreter '%s'", tmp); + free(tmp); + continue; + } + // Ignore non-LOAD types + if(phtab[i].Type != PT_LOAD) continue; + + LOG("phtab[%i] = {VAddr:0x%x,Offset:0x%x,FileSize:0x%x}", + i, phtab[i].VAddr, phtab[i].Offset, phtab[i].FileSize); + + addr = phtab[i].VAddr; + + AllocateMemory( addr, phtab[i].MemSize ); + + lseek(FD, phtab[i].Offset, SEEK_SET); + read(FD, (void*)(intptr_t)addr, phtab[i].FileSize); + memset( (char*)(intptr_t)addr + phtab[i].FileSize, 0, phtab[i].MemSize - phtab[i].FileSize); + } + + // Clean Up + free(phtab); + // Return + LEAVE('i', 0); + return 0; +} + +// --- ELF RELOCATION --- +/** + \fn int Elf_Relocate(void *Base) + \brief Relocates a loaded ELF Executable +*/ +int Elf_Relocate(void *Base) +{ + Elf32_Ehdr *hdr = Base; + Elf32_Phdr *phtab; + int i, j; // Counters + char *libPath; + uint32_t iRealBase = -1; + intptr_t iBaseDiff; + int iSegmentCount; + int iSymCount = 0; + Elf32_Rel *rel = NULL; + Elf32_Rela *rela = NULL; + uint32_t *pltgot = NULL; + void *plt = NULL; + uint32_t *ptr; + int relSz=0, relEntSz=8; + int relaSz=0, relaEntSz=8; + int pltSz=0, pltType=0; + Elf32_Dyn *dynamicTab = NULL; // Dynamic Table Pointer + char *dynstrtab = NULL; // .dynamic String Table + Elf32_Sym *dynsymtab = NULL; + int bFailed = 0; + + ENTER("pBase", Base); + + // Parse Program Header to get Dynamic Table + phtab = Base + hdr->phoff; + iSegmentCount = hdr->phentcount; + for(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) { + Warning("Elf_Relocate - Multiple PT_DYNAMIC segments\n"); + continue; + } + dynamicTab = (void *) (intptr_t) phtab[i].VAddr; + j = i; // Save Dynamic Table ID + break; + } + } + + // Check if a PT_DYNAMIC segement was found + if(!dynamicTab) { + Warning("Elf_Relocate: No PT_DYNAMIC segment in image, returning\n"); + LEAVE('x', hdr->entrypoint); + return hdr->entrypoint; + } + + // Page Align real base + iRealBase &= ~0xFFF; + + // Adjust "Real" Base + iBaseDiff = (intptr_t)Base - iRealBase; + // 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++) + { + switch(dynamicTab[j].d_tag) + { + // --- Symbol Table --- + case DT_SYMTAB: + dynamicTab[j].d_val += iBaseDiff; + dynsymtab = (void*) (intptr_t) dynamicTab[j].d_val; + hdr->misc.SymTable = dynamicTab[j].d_val; // Saved in unused bytes of ident + break; + + // --- String Table --- + case DT_STRTAB: + dynamicTab[j].d_val += iBaseDiff; + dynstrtab = (void*) (intptr_t) dynamicTab[j].d_val; + break; + + // --- Hash Table -- + case DT_HASH: + dynamicTab[j].d_val += iBaseDiff; + iSymCount = ((uint32_t*)((intptr_t)dynamicTab[j].d_val))[1]; + hdr->misc.HashTable = dynamicTab[j].d_val; // Saved in unused bytes of ident + break; + } + } + + + // Alter Symbols to true base + for(i = 0; i < iSymCount; i ++) + { + dynsymtab[i].value += iBaseDiff; + dynsymtab[i].nameOfs += (intptr_t)dynstrtab; + //LOG("Sym '%s' = 0x%x (relocated)\n", dynsymtab[i].name, dynsymtab[i].value); + } + + // === Add to loaded list (can be imported now) === + //Binary_AddLoaded( (intptr_t)Base ); + + // === Parse Relocation Data === + for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++) + { + switch(dynamicTab[j].d_tag) + { + // --- Shared Library Name --- + case DT_SONAME: + LOG(".so Name '%s'\n", dynstrtab+dynamicTab[j].d_val); + break; + // --- Needed Library --- + case DT_NEEDED: + libPath = dynstrtab + dynamicTab[j].d_val; + Notice("%p - Required Library '%s' - TODO load DT_NEEDED\n", Base, libPath); + break; + // --- PLT/GOT --- + 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; + + // --- Relocation --- + case DT_REL: rel = (void*)(iBaseDiff + dynamicTab[j].d_val); break; + case DT_RELSZ: relSz = dynamicTab[j].d_val; break; + case DT_RELENT: relEntSz = dynamicTab[j].d_val; break; + + case DT_RELA: rela = (void*)(iBaseDiff + dynamicTab[j].d_val); break; + case DT_RELASZ: relaSz = dynamicTab[j].d_val; break; + case DT_RELAENT: relaEntSz = dynamicTab[j].d_val; break; + } + } + + // Parse Relocation Entries + if(rel && relSz) + { + j = relSz / relEntSz; + for( i = 0; i < j; i++ ) + { + ptr = (void*)(iBaseDiff + rel[i].r_offset); + if( !Elf_Int_DoRelocate(rel[i].r_info, ptr, *ptr, dynsymtab, (intptr_t)Base) ) { + bFailed = 1; + } + } + } + // Parse Relocation Entries + if(rela && relaSz) + { + j = relaSz / relaEntSz; + for( i = 0; i < j; i++ ) + { + ptr = (void*)(iBaseDiff + rela[i].r_offset); + if( !Elf_Int_DoRelocate(rel[i].r_info, ptr, rela[i].r_addend, dynsymtab, (intptr_t)Base) ) { + bFailed = 1; + } + } + } + + // === Process PLT (Procedure Linkage Table) === + if(plt && pltSz) + { + if(pltType == DT_REL) + { + Elf32_Rel *pltRel = plt; + j = pltSz / sizeof(Elf32_Rel); + LOG("PLT Rel - plt = %p, pltSz = %i (%i ents)", plt, pltSz, j); + for(i = 0; i < j; i++) + { + ptr = (void*)(iBaseDiff + pltRel[i].r_offset); + if( !Elf_Int_DoRelocate(pltRel[i].r_info, ptr, *ptr, dynsymtab, (intptr_t)Base) ) { + bFailed = 1; + } + } + } + else + { + Elf32_Rela *pltRela = plt; + j = pltSz / sizeof(Elf32_Rela); + LOG("PLT RelA - plt = %p, pltSz = %i (%i ents)", plt, pltSz, j); + for(i=0;ientrypoint); + return hdr->entrypoint; +} + +/** + * \fn void Elf_Int_DoRelocate(uint32_t r_info, uint32_t *ptr, uint32_t addend, Elf32_Sym *symtab, uint32_t base) + * \brief Performs a relocation + * \param r_info Field from relocation entry + * \param ptr Pointer to location of relocation + * \param addend Value to add to symbol + * \param symtab Symbol Table + * \param base Base of loaded binary + */ +int Elf_Int_DoRelocate(uint32_t r_info, uint32_t *ptr, uint32_t addend, Elf32_Sym *symtab, intptr_t base) +{ + intptr_t val; + int type = ELF32_R_TYPE(r_info); + int sym = ELF32_R_SYM(r_info); + char *sSymName = symtab[sym].name; + + //LogF("Elf_Int_DoRelocate: (r_info=0x%x, ptr=0x%x, addend=0x%x, .., base=0x%x)\n", + // r_info, ptr, addend, base); + + switch( type ) + { + // Standard 32 Bit Relocation (S+A) + case R_386_32: + if( !Elf_GetSymbol((void*)base, sSymName, &val) ) // Search this binary first + if( !Binary_GetSymbol( sSymName, &val ) ) + return 0; + LOG("%08x R_386_32 *0x%x += 0x%x('%s')", r_info, ptr, val, sSymName); + *ptr = val + addend; + break; + + // 32 Bit Relocation wrt. Offset (S+A-P) + case R_386_PC32: + if( !Elf_GetSymbol( (void*)base, sSymName, &val ) ) + if( !Binary_GetSymbol( sSymName, &val ) ) + return 0; + LOG("%08x R_386_PC32 *0x%x = 0x%x + 0x%x('%s') - %p", r_info, ptr, *ptr, val, sSymName, ptr ); + // TODO: Check if it needs the true value of ptr or the compiled value + // NOTE: Testing using true value + *ptr = val + addend - (intptr_t)ptr; + break; + + // Absolute Value of a symbol (S) + case R_386_GLOB_DAT: + if( !Elf_GetSymbol( (void*)base, sSymName, &val ) ) + if( !Binary_GetSymbol( sSymName, &val ) ) + return 0; + LOG("%08x R_386_GLOB_DAT *0x%x = 0x%x (%s)", r_info, ptr, val, sSymName); + *ptr = val; + break; + + // Absolute Value of a symbol (S) + case R_386_JMP_SLOT: + if( !Elf_GetSymbol( (void*)base, sSymName, &val ) ) + if( !Binary_GetSymbol( sSymName, &val ) ) + return 0; + LOG("%08x R_386_JMP_SLOT %p = 0x%x (%s)", r_info, ptr, val, sSymName); + *ptr = val; + break; + + // Base Address (B+A) + case R_386_RELATIVE: + LOG("%08x R_386_RELATIVE %p = 0x%x + 0x%x", r_info, ptr, base, addend); + *ptr = base + addend; + break; + + default: + LOG("Rel 0x%x: 0x%x,%i", ptr, sym, type); + break; + } + return 1; +} + +/** + * \fn int Elf_GetSymbol(void *Base, char *name, intptr_t *ret) + * \brief Get a symbol from the loaded binary + */ +int Elf_GetSymbol(void *Base, char *Name, intptr_t *ret) +{ + Elf32_Ehdr *hdr = (void*)Base; + Elf32_Sym *symtab; + int nbuckets = 0; + int iSymCount = 0; + int i; + uint32_t *pBuckets; + uint32_t *pChains; + uint32_t iNameHash; + + if(!Base) return 0; + + pBuckets = (void *)(intptr_t) hdr->misc.HashTable; + symtab = (void *)(intptr_t) hdr->misc.SymTable; + + nbuckets = pBuckets[0]; + iSymCount = pBuckets[1]; + pBuckets = &pBuckets[2]; + pChains = &pBuckets[ nbuckets ]; + + // Get hash + iNameHash = Elf_Int_HashString(Name); + iNameHash %= nbuckets; + + // Check Bucket + i = pBuckets[ iNameHash ]; + if(symtab[i].shndx != SHN_UNDEF && strcmp(symtab[i].name, Name) == 0) { + if(ret) *ret = symtab[ i ].value; + return 1; + } + + // Walk Chain + while(pChains[i] != STN_UNDEF) + { + i = pChains[i]; + if(symtab[i].shndx != SHN_UNDEF && strcmp(symtab[ i ].name, Name) == 0) { + if(ret) *ret = symtab[ i ].value; + return 1; + } + } + return 0; +} + +/** + * \fn uint32_t Elf_Int_HashString(char *str) + * \brief Hash a string in the ELF format + * \param str String to hash + * \return Hash value + */ +uint32_t Elf_Int_HashString(char *str) +{ + uint32_t h = 0, g; + while(*str) + { + h = (h << 4) + *str++; + if( (g = h & 0xf0000000) ) + h ^= g >> 24; + h &= ~g; + } + return h; +} diff --git a/AcessNative/ld-acess.so_src/elf.h b/AcessNative/ld-acess.so_src/elf.h new file mode 100644 index 00000000..2527e99a --- /dev/null +++ b/AcessNative/ld-acess.so_src/elf.h @@ -0,0 +1,217 @@ +/** + * \file elf.h + * \brief ELF Exeutable Loader + */ +#ifndef _BIN_ELF_H +#define _BIN_ELF_H + +#include + +/** + * \brief ELF File Header + */ +struct sElf32_Ehdr +{ + union { + char ident[16]; //!< Identifier Bytes + struct { + uint32_t Ident1; + uint32_t Ident2; + uint32_t HashTable; + uint32_t SymTable; + } misc; + }; + uint16_t filetype; //!< File Type + uint16_t machine; //!< Machine / Arch + uint32_t version; //!< Version (File?) + uint32_t entrypoint; //!< Entry Point + uint32_t phoff; //!< Program Header Offset + uint32_t shoff; //!< Section Header Offset + uint32_t flags; //!< Flags + uint16_t headersize; //!< Header Size + uint16_t phentsize; //!< Program Header Entry Size + uint16_t phentcount; //!< Program Header Entry Count + uint16_t shentsize; //!< Section Header Entry Size + uint16_t shentcount; //!< Section Header Entry Count + uint16_t shstrindex; //!< Section Header String Table Index +} __attribute__ ((packed)); + +/** + * \brief Executable Types + */ +enum eElf32_ExecTypes +{ + ET_NONE = 0, //!< NULL Type + ET_REL = 1, //!< Relocatable (Object) + ET_EXEC = 2, //!< Executable + ET_DYN = 3, //!< Dynamic Library + ET_CORE = 4, //!< Core? + ET_LOPROC = 0xFF00, //!< Low Impl Defined + ET_HIPROC = 0xFFFF //!< High Impl Defined +}; + +/** + \name Section IDs + \{ +*/ +#define SHN_UNDEF 0 //!< Undefined Section +#define SHN_LORESERVE 0xFF00 //!< Low Reserved +#define SHN_LOPROC 0xFF00 //!< Low Impl Defined +#define SHN_HIPROC 0xFF1F //!< High Impl Defined +#define SHN_ABS 0xFFF1 //!< Absolute Address (Base: 0, Size: -1) +#define SHN_COMMON 0xFFF2 //!< Common +#define SHN_HIRESERVE 0xFFFF //!< High Reserved +//! \} + +/** + \enum eElfSectionTypes + \brief ELF Section Types +*/ +enum eElfSectionTypes { + SHT_NULL, //0 + SHT_PROGBITS, //1 + SHT_SYMTAB, //2 + SHT_STRTAB, //3 + SHT_RELA, //4 + SHT_HASH, //5 + SHT_DYNAMIC, //6 + SHT_NOTE, //7 + SHT_NOBITS, //8 + SHT_REL, //9 + SHT_SHLIB, //A + SHT_DYNSYM, //B + SHT_LAST, //C + SHT_LOPROC = 0x70000000, + SHT_HIPROC = 0x7fffffff, + SHT_LOUSER = 0x80000000, + SHT_HIUSER = 0xffffffff +}; + +#define SHF_WRITE 0x1 +#define SHF_ALLOC 0x2 +#define SHF_EXECINSTR 0x4 +#define SHF_MASKPROC 0xf0000000 + +struct sElf32_Shent { + uint32_t name; + uint32_t type; + uint32_t flags; + uint32_t address; + uint32_t offset; + uint32_t size; + uint32_t link; + uint32_t info; + uint32_t addralign; + uint32_t entsize; +} __attribute__ ((packed)); //sizeof = 40 + +struct elf_sym_s { + union { + uint32_t nameOfs; + char *name; + }; + uint32_t value; //Address + uint32_t size; + uint8_t info; + uint8_t other; + uint16_t shndx; +} __attribute__ ((packed)); +#define STN_UNDEF 0 // Undefined Symbol + +enum { + PT_NULL, //0 + PT_LOAD, //1 + PT_DYNAMIC, //2 + PT_INTERP, //3 + PT_NOTE, //4 + PT_SHLIB, //5 + PT_PHDR, //6 + PT_LOPROC = 0x70000000, + PT_HIPROC = 0x7fffffff +}; + +struct sElf32_Phdr { + uint32_t Type; + uint32_t Offset; + uint32_t VAddr; + uint32_t PAddr; + uint32_t FileSize; + uint32_t MemSize; + uint32_t Flags; + uint32_t Align; +} __attribute__ ((packed)); + +struct elf32_rel_s { + uint32_t r_offset; + uint32_t r_info; +} __attribute__ ((packed)); + +struct elf32_rela_s { + uint32_t r_offset; + uint32_t r_info; + int32_t r_addend; +} __attribute__ ((packed)); + +enum { + R_386_NONE = 0, // none + R_386_32, // S+A + R_386_PC32, // S+A-P + R_386_GOT32, // G+A-P + R_386_PLT32, // L+A-P + R_386_COPY, // none + R_386_GLOB_DAT, // S + R_386_JMP_SLOT, // S + R_386_RELATIVE, // B+A + R_386_GOTOFF, // S+A-GOT + R_386_GOTPC, // GOT+A-P + R_386_LAST // none +}; + +#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 + +struct elf32_dyn_s { + uint32_t d_tag; + uint32_t d_val; //Also d_ptr +} __attribute__ ((packed)); + +enum { + DT_NULL, //!< Marks End of list + DT_NEEDED, //!< Offset in strtab to needed library + DT_PLTRELSZ, //!< Size in bytes of PLT + DT_PLTGOT, //!< Address of PLT/GOT + DT_HASH, //!< Address of symbol hash table + DT_STRTAB, //!< String Table address + DT_SYMTAB, //!< Symbol Table address + DT_RELA, //!< Relocation table address + DT_RELASZ, //!< Size of relocation table + DT_RELAENT, //!< Size of entry in relocation table + DT_STRSZ, //!< Size of string table + DT_SYMENT, //!< Size of symbol table entry + DT_INIT, //!< Address of initialisation function + DT_FINI, //!< Address of termination function + DT_SONAME, //!< String table offset of so name + DT_RPATH, //!< String table offset of library path + DT_SYMBOLIC,//!< Reverse order of symbol searching for library, search libs first then executable + DT_REL, //!< Relocation Entries (Elf32_Rel instead of Elf32_Rela) + DT_RELSZ, //!< Size of above table (bytes) + DT_RELENT, //!< Size of entry in above table + DT_PLTREL, //!< Relocation entry of PLT + DT_DEBUG, //!< Debugging Entry - Unknown contents + DT_TEXTREL, //!< Indicates that modifcations to a non-writeable segment may occur + DT_JMPREL, //!< Address of PLT only relocation entries + DT_LOPROC = 0x70000000, //!< Low Definable + DT_HIPROC = 0x7FFFFFFF //!< High Definable +}; + +typedef struct sElf32_Ehdr Elf32_Ehdr; +typedef struct sElf32_Phdr Elf32_Phdr; +typedef struct sElf32_Shent Elf32_Shent; +typedef struct elf_sym_s elf_symtab; +typedef struct elf_sym_s Elf32_Sym; +typedef struct elf32_rel_s Elf32_Rel; +typedef struct elf32_rela_s Elf32_Rela; +typedef struct elf32_dyn_s Elf32_Dyn; + +#endif // defined(_EXE_ELF_H) diff --git a/AcessNative/ld-acess.so_src/main.c b/AcessNative/ld-acess.so_src/main.c new file mode 100644 index 00000000..e69de29b diff --git a/AcessNative/ld-acess.so_src/request.c b/AcessNative/ld-acess.so_src/request.c index 5a641194..e806ca0d 100644 --- a/AcessNative/ld-acess.so_src/request.c +++ b/AcessNative/ld-acess.so_src/request.c @@ -1,8 +1,16 @@ /* */ -#include +#include +#include #include -#include +#ifdef __WIN32__ +# include +# include +#else +# include +# include +# include +#endif #define SERVER_PORT 0xACE @@ -10,6 +18,9 @@ #ifdef __WIN32__ WSADATA gWinsock; SOCKET gSocket; +#else + int gSocket; +# define INVALID_SOCKET -1 #endif // === CODE === @@ -32,7 +43,9 @@ int _InitSyscalls() if (gSocket == INVALID_SOCKET) { fprintf(stderr, "Could not create socket.\n"); + #if __WIN32__ WSACleanup(); + #endif exit(0); } @@ -40,28 +53,27 @@ int _InitSyscalls() memset((void *)&server, '\0', sizeof(struct sockaddr_in)); server.sin_family = AF_INET; server.sin_port = htons(SERVER_PORT); - server.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)127; - server.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)0; - server.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)0; - server.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)1; + server.sin_addr.s_addr = htonl(0x7F00001); // Set client address memset((void *)&client, '\0', sizeof(struct sockaddr_in)); client.sin_family = AF_INET; client.sin_port = htons(0); - client.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)127; - client.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)0; - client.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)0; - client.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)1; + client.sin_addr.s_addr = htonl(0x7F00001); // Bind if( bind(gSocket, (struct sockaddr *)&client, sizeof(struct sockaddr_in)) == -1 ) { fprintf(stderr, "Cannot bind address to socket.\n"); + #if __WIN32__ closesocket(gSocket); WSACleanup(); + #else + close(gSocket); + #endif exit(0); } + return 0; } int _Syscall(const char *ArgTypes, ...) diff --git a/AcessNative/ld-acess.so_src/syscalls.c b/AcessNative/ld-acess.so_src/syscalls.c index 35fa6aeb..416cab61 100644 --- a/AcessNative/ld-acess.so_src/syscalls.c +++ b/AcessNative/ld-acess.so_src/syscalls.c @@ -4,6 +4,7 @@ #include #include #include +#include // === IMPORTS === /** @@ -19,12 +20,12 @@ */ int _Syscall(const char *ArgTypes, ...) { - int outBufSize = 0; + // int outBufSize = 0; va_list args; va_start(args, ArgTypes); va_end(args); - + return 0; } int open(const char *Path, int Flags) {