git.ucc.asn.au
/
tpg
/
acess2.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
AcessNative - Cleaning up debug
[tpg/acess2.git]
/
AcessNative
/
ld-acess_src
/
binary.c
diff --git
a/AcessNative/ld-acess_src/binary.c
b/AcessNative/ld-acess_src/binary.c
index
b8f6e71
..
d04ac6d
100644
(file)
--- a/
AcessNative/ld-acess_src/binary.c
+++ b/
AcessNative/ld-acess_src/binary.c
@@
-1,12
+1,17
@@
/*
/*
- * AcessNative
+ * AcessNative Dynamic Linker
+ * - By John Hodge (thePowersGang)
+ *
+ * binary.c
+ * - Provides binary loading and type abstraction
*/
*/
+#define DEBUG 0
#include "common.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "common.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
-#define LIBRARY_PATH "
../Usermode/Output/i386
/Libs"
+#define LIBRARY_PATH "
$$$$../Usermode/Output/x86_64
/Libs"
// === TYPES ===
typedef struct sBinary {
// === TYPES ===
typedef struct sBinary {
@@
-18,11
+23,12
@@
typedef struct sBinary {
} tBinary;
// === IMPORTS ===
} tBinary;
// === IMPORTS ===
-extern void *Elf_Load(
FILE *FP
);
-extern uintptr_t Elf
_
Relocate(void *Base);
-extern int Elf
_GetSymbol(void *Base, char *Name, uintptr_t *ret
);
+extern void *Elf_Load(
int fd
);
+extern uintptr_t ElfRelocate(void *Base);
+extern int Elf
GetSymbol(void *Base, char *Name, uintptr_t *ret, size_t *size
);
extern int ciNumBuiltinSymbols;
extern tSym caBuiltinSymbols[];
extern int ciNumBuiltinSymbols;
extern tSym caBuiltinSymbols[];
+extern char **gEnvP;
// === PROTOTYPES ===
void Binary_AddToList(const char *Filename, void *Base, tBinFmt *Format);
// === PROTOTYPES ===
void Binary_AddToList(const char *Filename, void *Base, tBinFmt *Format);
@@
-33,8
+39,8
@@
tBinFmt gElf_FormatDef = {
// .Magic = "\x7F""ELF",
.Name = "ELF32",
.Load = Elf_Load,
// .Magic = "\x7F""ELF",
.Name = "ELF32",
.Load = Elf_Load,
- .Relocate = Elf
_
Relocate,
- .GetSymbol = Elf
_
GetSymbol
+ .Relocate = ElfRelocate,
+ .GetSymbol = ElfGetSymbol
};
tBinary *gLoadedBinaries;
};
tBinary *gLoadedBinaries;
@@
-43,7
+49,7
@@
char *Binary_LocateLibrary(const char *Name)
{
char *envPath = getenv("ACESS_LIBRARY_PATH");
int nameLen = strlen(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");
if( strcmp(Name, "libld-acess.so") == 0 ) {
return strdup("libld-acess.so");
@@
-59,9
+65,9
@@
char *Binary_LocateLibrary(const char *Name)
strcat(tmp, "/");
strcat(tmp, Name);
strcat(tmp, "/");
strcat(tmp, Name);
- f
p = fopen(tmp, "r");
- if(f
p
) {
-
fclose(fp
);
+ f
d = acess_open(tmp, 4); // OPENFLAG_EXEC
+ if(f
d != -1
) {
+
acess_close(fd
);
return strdup(tmp);
}
}
return strdup(tmp);
}
}
@@
-74,16
+80,20
@@
char *Binary_LocateLibrary(const char *Name)
strcat(tmp, "/");
strcat(tmp, Name);
strcat(tmp, "/");
strcat(tmp, Name);
+ #if DEBUG
printf("Binary_LocateLibrary: tmp = '%s'\n", tmp);
printf("Binary_LocateLibrary: tmp = '%s'\n", tmp);
+ #endif
- f
p = fopen(tmp, "r");
- if(f
p
) {
-
fclose(fp
);
+ f
d = acess_open(tmp, 4); // OPENFLAG_EXEC
+ if(f
d != -1
) {
+
acess_close(fd
);
return strdup(tmp);
}
}
return strdup(tmp);
}
}
+ #if DEBUG
fprintf(stderr, "Unable to locate library '%s'\n", Name);
fprintf(stderr, "Unable to locate library '%s'\n", Name);
+ #endif
return NULL;
}
return NULL;
}
@@
-92,23
+102,31
@@
void *Binary_LoadLibrary(const char *Name)
{
char *path;
void *ret;
{
char *path;
void *ret;
- int (*entry)(int,char*[],char**) = NULL;
+ int (*entry)(
void *,
int,char*[],char**) = NULL;
// Find File
path = Binary_LocateLibrary(Name);
// Find File
path = Binary_LocateLibrary(Name);
+ #if DEBUG
printf("Binary_LoadLibrary: path = '%s'\n", path);
printf("Binary_LoadLibrary: path = '%s'\n", path);
+ #endif
if( !path ) {
return NULL;
}
ret = Binary_Load(path, (uintptr_t*)&entry);
if( !path ) {
return NULL;
}
ret = Binary_Load(path, (uintptr_t*)&entry);
+ if( ret != (void*)-1 )
+ Debug("LOADED '%s' to %p (Entry=%p)", path, ret, entry);
free(path);
free(path);
+ #if DEBUG
printf("Binary_LoadLibrary: ret = %p, entry = %p\n", ret, entry);
printf("Binary_LoadLibrary: ret = %p, entry = %p\n", ret, entry);
+ #endif
if( entry ) {
char *argv[] = {NULL};
if( entry ) {
char *argv[] = {NULL};
+ #if DEBUG
printf("Calling '%s' entry point %p\n", Name, entry);
printf("Calling '%s' entry point %p\n", Name, entry);
- entry(0, argv, NULL);
+ #endif
+ entry(ret, 0, argv, gEnvP);
}
return ret;
}
return ret;
@@
-116,8
+134,8
@@
void *Binary_LoadLibrary(const char *Name)
void *Binary_Load(const char *Filename, uintptr_t *EntryPoint)
{
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;
void *ret;
uintptr_t entry = 0;
tBinFmt *fmt;
@@
-138,37
+156,43
@@
void *Binary_Load(const char *Filename, uintptr_t *EntryPoint)
}
}
}
}
- f
p = fopen(Filename, "r");
- if(
!fp
) {
+ f
d = acess_open(Filename, 2|1); // Execute and Read
+ if(
fd == -1
) {
// TODO: Handle libary directories
perror("Opening binary");
return NULL;
}
// 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, &dword, 4);
+ acess_seek(fd, 0, ACESS_SEEK_SET);
if( memcmp(&dword, "\x7F""ELF", 4) == 0 ) {
fmt = &gElf_FormatDef;
}
else {
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;
}
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(0x%x)...\n", fd);
+ #endif
+ ret = fmt->Load(fd);
+ acess_close(fd);
+ #if DEBUG
+ printf("fmt->Load(0x%x): %p\n", fd, ret);
+ #endif
if( !ret ) {
if( !ret ) {
- fclose(fp);
return NULL;
}
Binary_AddToList(Filename, ret, fmt);
entry = fmt->Relocate(ret);
return NULL;
}
Binary_AddToList(Filename, ret, fmt);
entry = fmt->Relocate(ret);
+ #if DEBUG
printf("fmt->Relocate(%p): %p\n", ret, (void*)entry);
printf("fmt->Relocate(%p): %p\n", ret, (void*)entry);
+ #endif
if( !entry ) {
// TODO: Clean up
return NULL;
if( !entry ) {
// TODO: Clean up
return NULL;
@@
-177,8
+201,6
@@
void *Binary_Load(const char *Filename, uintptr_t *EntryPoint)
if( EntryPoint )
*EntryPoint = entry;
if( EntryPoint )
*EntryPoint = entry;
- fclose(fp);
-
Binary_SetReadyToUse(ret);
return ret;
Binary_SetReadyToUse(ret);
return ret;
@@
-206,13
+228,13
@@
void Binary_SetReadyToUse(void *Base)
}
}
}
}
-int Binary_GetSymbol(const char *SymbolName, uintptr_t *Value)
+int Binary_GetSymbol(const char *SymbolName, uintptr_t *Value
, size_t *Size
)
{
int i;
tBinary *bin;
{
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
// Search builtins
// - Placed first to override smartarses that define their own versions
@@
-221,20
+243,23
@@
int Binary_GetSymbol(const char *SymbolName, uintptr_t *Value)
{
if( strcmp(caBuiltinSymbols[i].Name, SymbolName) == 0 ) {
*Value = (uintptr_t)caBuiltinSymbols[i].Value;
{
if( strcmp(caBuiltinSymbols[i].Name, SymbolName) == 0 ) {
*Value = (uintptr_t)caBuiltinSymbols[i].Value;
+ if(Size) *Size = 0;
return 1;
}
}
return 1;
}
}
- //
TODO:
Search list of loaded binaries
+ // Search list of loaded binaries
for(bin = gLoadedBinaries; bin; bin = bin->Next)
{
if( !bin->Ready ) continue;
for(bin = gLoadedBinaries; bin; bin = bin->Next)
{
if( !bin->Ready ) continue;
- printf(" Binary_GetSymbol: bin = %p{%p, %s}\n", bin, bin->Base, bin->Path);
- if( bin->Format->GetSymbol(bin->Base, (char*)SymbolName, Value) )
+
//
printf(" Binary_GetSymbol: bin = %p{%p, %s}\n", bin, bin->Base, bin->Path);
+ if( bin->Format->GetSymbol(bin->Base, (char*)SymbolName, Value
, Size
) )
return 1;
}
return 1;
}
- printf("Binary_GetSymbol: RETURN 0, not found\n");
-
+ //printf("Binary_GetSymbol: RETURN 0, not found\n");
+ printf("--- ERROR: Unable to find symbol '%s'\n", SymbolName);
+
+ exit( -1 );
return 0;
}
return 0;
}
UCC
git Repository :: git.ucc.asn.au