From: John Hodge Date: Tue, 14 Feb 2012 07:33:32 +0000 (+0800) Subject: AcessNative - Little fixes to get it running on 64-bit X-Git-Tag: rel0.15~775 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=d497ef38c1c1e0aa5467722e115be0dc4baa47ab;p=tpg%2Facess2.git AcessNative - Little fixes to get it running on 64-bit - Still broken though :) --- diff --git a/AcessNative/acesskernel_src/main.c b/AcessNative/acesskernel_src/main.c index f3ab1968..fbb457cf 100644 --- a/AcessNative/acesskernel_src/main.c +++ b/AcessNative/acesskernel_src/main.c @@ -31,7 +31,7 @@ extern const char gsGitHash[]; extern int giBuildNumber; // === GLOBALS === -const char *gsAcessDir = "../Usermode/Output/x86"; +const char *gsAcessDir = "../Usermode/Output/x86_64"; // === CODE === int main(int argc, char *argv[]) @@ -68,8 +68,12 @@ int main(int argc, char *argv[]) // Initialise VFS VFS_Init(); // - Start IO Drivers - Video_Install(NULL); - NativeKeyboard_Install(NULL); + if( Video_Install(NULL) ) { + Log_Error("Init", "Unable to load NativeVideo"); + } + if( NativeKeyboard_Install(NULL) ) { + Log_Error("Init", "Unable to load NativeKeyboard"); + } NativeFS_Install(NULL); // - Start VTerm { diff --git a/AcessNative/acesskernel_src/syscalls.c b/AcessNative/acesskernel_src/syscalls.c index 4d85e8c6..d5c051b0 100644 --- a/AcessNative/acesskernel_src/syscalls.c +++ b/AcessNative/acesskernel_src/syscalls.c @@ -272,7 +272,7 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength) } formatString[i] = '\0'; - LOG("Request %i(%s) '%s'", Request->CallID, casSYSCALL_NAMES[Request->CallID], formatString); + //LOG("Request %i(%s) '%s'", Request->CallID, casSYSCALL_NAMES[Request->CallID], formatString); { char argListData[argListLen]; @@ -286,19 +286,19 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength) case ARG_TYPE_VOID: break; case ARG_TYPE_INT32: - LOG("%i INT32: 0x%x", i, *(Uint32*)inData); + //LOG("%i INT32: 0x%x", i, *(Uint32*)inData); *(Uint32*)&argListData[argListLen] = *(Uint32*)inData; argListLen += sizeof(Uint32); inData += sizeof(Uint32); break; case ARG_TYPE_INT64: - LOG("%i INT64: 0x%llx", i, *(Uint64*)inData); + //LOG("%i INT64: 0x%llx", i, *(Uint64*)inData); *(Uint64*)&argListData[argListLen] = *(Uint64*)inData; argListLen += sizeof(Uint64); inData += sizeof(Uint64); break; case ARG_TYPE_STRING: - LOG("%i STR: '%s'", i, (char*)inData); + //LOG("%i STR: '%s'", i, (char*)inData); *(char**)&argListData[argListLen] = (char*)inData; argListLen += sizeof(void*); inData += Request->Params[i].Length; @@ -319,16 +319,16 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength) { // Allocate and zero the buffer returnData[i] = calloc(1, Request->Params[i].Length); - LOG("%i ZDAT: %i %p", i, - Request->Params[i].Length, returnData[i]); + //LOG("%i ZDAT: %i %p", i, + // Request->Params[i].Length, returnData[i]); *(void**)&argListData[argListLen] = returnData[i]; argListLen += sizeof(void*); } else { returnData[i] = (void*)inData; - LOG("%i DATA: %i %p", i, - Request->Params[i].Length, returnData[i]); + //LOG("%i DATA: %i %p", i, + // Request->Params[i].Length, returnData[i]); *(void**)&argListData[argListLen] = (void*)inData; argListLen += sizeof(void*); inData += Request->Params[i].Length; diff --git a/AcessNative/ld-acess_src/Makefile b/AcessNative/ld-acess_src/Makefile index bbce43df..7d821321 100644 --- a/AcessNative/ld-acess_src/Makefile +++ b/AcessNative/ld-acess_src/Makefile @@ -14,10 +14,14 @@ ifeq ($(PLATFORM),win) endif ifeq ($(PLATFORM),lin) BIN := ../ld-acess - LD += -m elf_i386 +# LD += -m elf_i386 endif -CFLAGS += -Wall -Werror -g -m32 +CFLAGS += -Wall +CFLAGS += -Werror +CFLAGS += -g +CPPFLAGS += -DARCHDIR_is_x86_64=1 +LDFLAGS += -g -Wl,-T,obj-$(PLATFORM)/link.ld DEPFILES = $(filter %.o,$(OBJ)) DEPFILES := $(DEPFILES:%=%.dep) @@ -27,10 +31,10 @@ DEPFILES := $(DEPFILES:%=%.dep) all: $(BIN) clean: - $(RM) $(BIN) $(OBJ) $(DEPFILES) + $(RM) $(BIN) $(OBJ) $(DEPFILES) obj-$(PLATFORM)/link.ld $(BIN): obj-$(PLATFORM)/link.ld $(OBJ) - $(CC) -g -o $@ $(OBJ) -m32 -Wl,-T,obj-$(PLATFORM)/link.ld + $(CC) $(LDFLAGS) -o $@ $(OBJ) obj-$(PLATFORM)/%.o: %.c @mkdir -p $(dir $@) @@ -42,7 +46,7 @@ obj-$(PLATFORM)/%.o: %.c obj-lin/link.ld: @mkdir -p $(dir $@) @echo "Making Linker Script ($@)" - @$(LD) --verbose | awk '{ if( substr($$0,0,5) == "====="){ bPrint = !bPrint; } else { if(bPrint){ print $$0;} } }' | sed 's/\b0x0[08][0-9]*\b/0x00100000/g' > $@ + $(LD) --verbose | awk '{ if( substr($$0,0,5) == "====="){ bPrint = !bPrint; } else { if(bPrint){ print $$0;} } }' | sed 's/\b0x[048][0-9]*\b/0x00200000/g' > $@ -include $(DEPFILES) diff --git a/AcessNative/ld-acess_src/elf_load.c b/AcessNative/ld-acess_src/elf_load.c index 9e5a0327..590da377 100644 --- a/AcessNative/ld-acess_src/elf_load.c +++ b/AcessNative/ld-acess_src/elf_load.c @@ -1,5 +1,6 @@ /* - * Acess v0.1 + * Acess2 - AcessNative + * * ELF Executable Loader Code */ #define DEBUG 1 @@ -9,6 +10,7 @@ #include #include "common.h" #include "elf32.h" +#include "elf64.h" #define DEBUG_WARN 1 @@ -20,7 +22,7 @@ # define ENTER(...) printf("%s: ---- ENTER ----\n", __func__); # define LOG(s, ...) printf("%s: " s, __func__, __VA_ARGS__) # define LOGS(s) printf("%s: " s, __func__) -# define LEAVE(...) +# define LEAVE(...) printf("%s: ---- LEAVE ----\n", __func__); #else # define ENTER(...) # define LOG(...) @@ -31,11 +33,12 @@ // === PROTOTYPES === void *Elf_Load(int FD); void *Elf32Load(int FD, Elf32_Ehdr *hdr); +void *Elf64Load(int FD, Elf64_Ehdr *hdr); // === CODE === void *Elf_Load(int FD) { - Elf32_Ehdr hdr; + Elf64_Ehdr hdr; // Read ELF Header acess_read(FD, &hdr, sizeof(hdr)); @@ -49,11 +52,15 @@ void *Elf_Load(int FD) switch(hdr.e_ident[4]) { case ELFCLASS32: - return Elf32Load(FD, &hdr); + return Elf32Load(FD, (void*)&hdr); + case ELFCLASS64: + return Elf64Load(FD, &hdr); default: + Warning("Unknown ELF class (%i)", hdr.e_ident[4]); return NULL; } } + void *Elf32Load(int FD, Elf32_Ehdr *hdr) { Elf32_Phdr *phtab; @@ -122,8 +129,8 @@ void *Elf32Load(int FD, Elf32_Ehdr *hdr) LOG("base = %08x, max = %08x\n", base, max); if( base == 0 ) { - // Find a nice space (31 address bits allowed) - base = FindFreeRange( max, 31 ); + // Find a nice space (47 address bits allowed) + base = FindFreeRange( max, 47 ); LOG("new base = %08x\n", base); if( base == 0 ) return NULL; baseDiff = base; @@ -171,3 +178,131 @@ void *Elf32Load(int FD, Elf32_Ehdr *hdr) LEAVE('p', base); return PTRMK(void, base); } + +void *Elf64Load(int FD, Elf64_Ehdr *hdr) +{ + Elf64_Phdr *phtab; + int i; + int iPageCount; + uint64_t max, base; + uint64_t addr; + uint64_t baseDiff = 0; + + ENTER("iFD", FD); + + #if BITS <= 32 + Warning("ELF64 being loaded in 32-bit env, this may not work"); + #endif + + // Check for a program header + if(hdr->e_phoff == 0) { + #if DEBUG_WARN + Warning("ELF File does not contain a program header\n"); + #endif + LEAVE('n'); + return NULL; + } + + // Read Program Header Table + phtab = malloc( sizeof(Elf64_Phdr) * hdr->e_phnum ); + if( !phtab ) { + LEAVE('n'); + return NULL; + } + LOG("hdr.phoff = 0x%08llx\n", (long long)hdr->e_phoff); + acess_seek(FD, hdr->e_phoff, ACESS_SEEK_SET); + acess_read(FD, phtab, sizeof(Elf64_Phdr) * hdr->e_phnum); + + // Count Pages + iPageCount = 0; + LOG("hdr.phentcount = %i\n", hdr->e_phnum); + for( i = 0; i < hdr->e_phnum; i++ ) + { + // Ignore Non-LOAD types + if(phtab[i].p_type != PT_LOAD) + continue; + iPageCount += ((phtab[i].p_vaddr&0xFFF) + phtab[i].p_memsz + 0xFFF) >> 12; + LOG("phtab[%i] = {VAddr:0x%llx, MemSize:0x%llx}\n", + i, (long long)phtab[i].p_vaddr, (long long)phtab[i].p_memsz); + } + + LOG("iPageCount = %i\n", 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 + max = 0; + base = 0xFFFFFFFF; + for( i = 0; i < hdr->e_phnum; i ++) + { + if( phtab[i].p_type != PT_LOAD ) + continue; + if( phtab[i].p_vaddr < base ) + base = phtab[i].p_vaddr; + if( phtab[i].p_vaddr + phtab[i].p_memsz > max ) + max = phtab[i].p_vaddr + phtab[i].p_memsz; + } + + LOG("base = %08lx, max = %08lx\n", base, max); + + if( base == 0 ) { + // Find a nice space (31 address bits allowed) + base = FindFreeRange( max, 31 ); + LOG("new base = %08lx\n", base); + if( base == 0 ) return NULL; + baseDiff = base; + } + + // Load Pages + for( i = 0; i < hdr->e_phnum; i++ ) + { + // Get Interpreter Name + if( phtab[i].p_type == PT_INTERP ) + { + char *tmp; + //if(ret->Interpreter) continue; + tmp = malloc(phtab[i].p_filesz+1); + tmp[ phtab[i].p_filesz ] = 0; + acess_seek(FD, phtab[i].p_offset, ACESS_SEEK_SET); + acess_read(FD, tmp, phtab[i].p_filesz); + //ret->Interpreter = Binary_RegInterp(tmp); + LOG("Interpreter '%s'\n", tmp); + free(tmp); + continue; + } + // Ignore non-LOAD types + if(phtab[i].p_type != PT_LOAD) continue; + + LOG("phtab[%i] = PT_LOAD {Adj VAddr:0x%llx, Offset:0x%llx, FileSize:0x%llx, MemSize:0x%llx}\n", + i, + (long long)phtab[i].p_vaddr+baseDiff, (long long)phtab[i].p_offset, + (long long)phtab[i].p_filesz, (long long)phtab[i].p_memsz + ); + + addr = phtab[i].p_vaddr + baseDiff; + + if( AllocateMemory( addr, phtab[i].p_memsz ) ) { + fprintf(stderr, "Elf_Load: Unable to map memory at %llx (0x%llx bytes)\n", + (long long)addr, (long long)phtab[i].p_memsz); + free( phtab ); + return NULL; + } + + acess_seek(FD, phtab[i].p_offset, ACESS_SEEK_SET); + acess_read(FD, PTRMK(void, addr), phtab[i].p_filesz); + memset( PTRMK(char, addr) + phtab[i].p_filesz, 0, phtab[i].p_memsz - phtab[i].p_filesz ); + } + + // Clean Up + free(phtab); + // Return + LEAVE('p', base); + return PTRMK(void, base); +} + diff --git a/AcessNative/ld-acess_src/exports.c b/AcessNative/ld-acess_src/exports.c index 947e0d36..5f97bb27 100644 --- a/AcessNative/ld-acess_src/exports.c +++ b/AcessNative/ld-acess_src/exports.c @@ -9,6 +9,7 @@ #include "../syscalls.h" #include "exports.h" #include +#include #define DEBUG(v...) Debug(v) @@ -93,7 +94,7 @@ int acess_ioctl(int fd, int id, void *data) { return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, 1024, data); } int acess_finfo(int fd, t_sysFInfo *info, int maxacls) { - DEBUG("offsetof(size, t_sysFInfo) = %i", offsetof(t_sysFInfo, size)); +// DEBUG("offsetof(size, t_sysFInfo) = %i", offsetof(t_sysFInfo, size)); DEBUG("finfo(%i, %p, %i)", fd, info, maxacls); return _Syscall(SYS_FINFO, ">i i", fd, diff --git a/AcessNative/ld-acess_src/main.c b/AcessNative/ld-acess_src/main.c index cbe0ac1f..cd2b8158 100644 --- a/AcessNative/ld-acess_src/main.c +++ b/AcessNative/ld-acess_src/main.c @@ -20,7 +20,7 @@ int main(int argc, char *argv[], char **envp) int appArgc; char **appArgv; char *appPath = NULL; - int (*appMain)(int, char *[], char **) __attribute__((cdecl)); + int (*appMain)(int, char *[], char **); void *base; int rv;