X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=AcessNative%2Fld-acess_src%2Fbinary.c;h=3ae8d7f12be57dd56214592031157dd1db8d7c41;hb=2c3ab58b961dea91fdfbf432a7fdf9593c6e7a8e;hp=b8f6e71bbf8861467e4d814605a07b88ca889cd4;hpb=a09032f44bba55ce1e60dfab92a39cf6c909220b;p=tpg%2Facess2.git diff --git a/AcessNative/ld-acess_src/binary.c b/AcessNative/ld-acess_src/binary.c index b8f6e71b..3ae8d7f1 100644 --- a/AcessNative/ld-acess_src/binary.c +++ b/AcessNative/ld-acess_src/binary.c @@ -6,7 +6,7 @@ #include #include -#define LIBRARY_PATH "../Usermode/Output/i386/Libs" +#define LIBRARY_PATH "$$$$../Usermode/Output/i386/Libs" // === TYPES === typedef struct sBinary { @@ -18,7 +18,7 @@ typedef struct sBinary { } tBinary; // === IMPORTS === -extern void *Elf_Load(FILE *FP); +extern void *Elf_Load(int fd); extern uintptr_t Elf_Relocate(void *Base); extern int Elf_GetSymbol(void *Base, char *Name, uintptr_t *ret); extern int ciNumBuiltinSymbols; @@ -43,7 +43,7 @@ char *Binary_LocateLibrary(const char *Name) { char *envPath = getenv("ACESS_LIBRARY_PATH"); int nameLen = strlen(Name); - FILE *fp; + int fd; if( strcmp(Name, "libld-acess.so") == 0 ) { return strdup("libld-acess.so"); @@ -59,9 +59,9 @@ char *Binary_LocateLibrary(const char *Name) strcat(tmp, "/"); strcat(tmp, Name); - fp = fopen(tmp, "r"); - if(fp) { - fclose(fp); + fd = acess_open(tmp, 4); // OPENFLAG_EXEC + if(fd != -1) { + acess_close(fd); return strdup(tmp); } } @@ -74,16 +74,20 @@ char *Binary_LocateLibrary(const char *Name) strcat(tmp, "/"); strcat(tmp, Name); + #if DEBUG printf("Binary_LocateLibrary: tmp = '%s'\n", tmp); + #endif - fp = fopen(tmp, "r"); - if(fp) { - fclose(fp); + fd = acess_open(tmp, 4); // OPENFLAG_EXEC + if(fd != -1) { + acess_close(fd); return strdup(tmp); } } + #if DEBUG fprintf(stderr, "Unable to locate library '%s'\n", Name); + #endif return NULL; } @@ -92,23 +96,30 @@ void *Binary_LoadLibrary(const char *Name) { char *path; void *ret; - int (*entry)(int,char*[],char**) = NULL; + int (*entry)(void *,int,char*[],char**) = NULL; // Find File path = Binary_LocateLibrary(Name); + #if DEBUG printf("Binary_LoadLibrary: path = '%s'\n", path); + #endif if( !path ) { return NULL; } ret = Binary_Load(path, (uintptr_t*)&entry); + printf("LOADED '%s' to %p (Entry=%p)\n", path, ret, entry); free(path); + #if DEBUG printf("Binary_LoadLibrary: ret = %p, entry = %p\n", ret, entry); + #endif if( entry ) { char *argv[] = {NULL}; + #if DEBUG printf("Calling '%s' entry point %p\n", Name, entry); - entry(0, argv, NULL); + #endif + entry(ret, 0, argv, NULL); } return ret; @@ -116,8 +127,8 @@ void *Binary_LoadLibrary(const char *Name) void *Binary_Load(const char *Filename, uintptr_t *EntryPoint) { - FILE *fp; - uint32_t dword; + int fd; + uint32_t dword = 0xFA17FA17; void *ret; uintptr_t entry = 0; tBinFmt *fmt; @@ -138,37 +149,43 @@ void *Binary_Load(const char *Filename, uintptr_t *EntryPoint) } } - fp = fopen(Filename, "r"); - if( !fp ) { + fd = acess_open(Filename, 2|1); // Execute and Read + if( fd == -1 ) { // TODO: Handle libary directories perror("Opening binary"); return NULL; } - fread(&dword, 1, 4, fp); - fseek(fp, 0, SEEK_SET); - printf("dword = %08x\n", dword); + acess_read(fd, 4, &dword); + acess_seek(fd, 0, ACESS_SEEK_SET); if( memcmp(&dword, "\x7F""ELF", 4) == 0 ) { fmt = &gElf_FormatDef; } else { - fclose(fp); + fprintf(stderr, "Unknown executable format (0x%08x)\n", dword); + acess_close(fd); return NULL; } - printf("fmt->Load(%p)...\n", fp); - ret = fmt->Load(fp); - printf("fmt->Load(%p): %p\n", fp, ret); + #if DEBUG + printf("fmt->Load(%i)...\n", fd); + #endif + ret = fmt->Load(fd); + acess_close(fd); + #if DEBUG + printf("fmt->Load(%p): %p\n", fd, ret); + #endif if( !ret ) { - fclose(fp); return NULL; } Binary_AddToList(Filename, ret, fmt); entry = fmt->Relocate(ret); + #if DEBUG printf("fmt->Relocate(%p): %p\n", ret, (void*)entry); + #endif if( !entry ) { // TODO: Clean up return NULL; @@ -177,8 +194,6 @@ void *Binary_Load(const char *Filename, uintptr_t *EntryPoint) if( EntryPoint ) *EntryPoint = entry; - fclose(fp); - Binary_SetReadyToUse(ret); return ret; @@ -211,8 +226,8 @@ int Binary_GetSymbol(const char *SymbolName, uintptr_t *Value) int i; tBinary *bin; - printf("Binary_GetSymbol: (SymbolName='%s', Value=%p)\n", - SymbolName, Value); + //printf("Binary_GetSymbol: (SymbolName='%s', Value=%p)\n", + // SymbolName, Value); // Search builtins // - Placed first to override smartarses that define their own versions @@ -225,16 +240,16 @@ int Binary_GetSymbol(const char *SymbolName, uintptr_t *Value) } } - // TODO: Search list of loaded binaries + // Search list of loaded binaries for(bin = gLoadedBinaries; bin; bin = bin->Next) { if( !bin->Ready ) continue; - printf(" Binary_GetSymbol: bin = %p{%p, %s}\n", bin, bin->Base, bin->Path); + //printf(" Binary_GetSymbol: bin = %p{%p, %s}\n", bin, bin->Base, bin->Path); if( bin->Format->GetSymbol(bin->Base, (char*)SymbolName, Value) ) return 1; } - printf("Binary_GetSymbol: RETURN 0, not found\n"); + //printf("Binary_GetSymbol: RETURN 0, not found\n"); return 0; }