9 #define LIBRARY_PATH "../Usermode/Output/i386/Libs"
12 typedef struct sBinary {
21 extern void *Elf_Load(FILE *FP);
22 extern uintptr_t Elf_Relocate(void *Base);
23 extern int Elf_GetSymbol(void *Base, char *Name, uintptr_t *ret);
24 extern int ciNumBuiltinSymbols;
25 extern tSym caBuiltinSymbols[];
28 void Binary_AddToList(const char *Filename, void *Base, tBinFmt *Format);
31 tBinFmt gElf_FormatDef = {
32 // .Mask = 0xFFFFFFFF,
33 // .Magic = "\x7F""ELF",
36 .Relocate = Elf_Relocate,
37 .GetSymbol = Elf_GetSymbol
39 tBinary *gLoadedBinaries;
42 char *Binary_LocateLibrary(const char *Name)
44 char *envPath = getenv("ACESS_LIBRARY_PATH");
45 int nameLen = strlen(Name);
48 // Try the environment ACESS_LIBRARY_PATH
49 if( envPath && envPath[0] != '\0' )
51 int len = strlen(envPath)+1+nameLen+1;
66 int len = strlen(LIBRARY_PATH)+1+nameLen+1;
69 strcpy(tmp, LIBRARY_PATH);
73 printf("Binary_LocateLibrary: tmp = '%s'\n", tmp);
82 fprintf(stderr, "Unable to locate library '%s'\n", Name);
87 void *Binary_LoadLibrary(const char *Name)
91 int (*entry)(int,char*[],char**) = NULL;
94 path = Binary_LocateLibrary(Name);
95 printf("Binary_LoadLibrary: path = '%s'\n", path);
100 ret = Binary_Load(path, (uintptr_t*)&entry);
103 printf("Binary_LoadLibrary: ret = %p, entry = %p\n", ret, entry);
105 char *argv[] = {NULL};
106 entry(0, argv, NULL);
112 void *Binary_Load(const char *Filename, uintptr_t *EntryPoint)
122 for(bin = gLoadedBinaries; bin; bin = bin->Next)
124 if( strcmp(Filename, bin->Path) == 0 ) {
130 fp = fopen(Filename, "r");
132 // TODO: Handle libary directories
133 perror("Opening binary");
137 fread(&dword, 1, 4, fp);
138 fseek(fp, 0, SEEK_SET);
139 printf("dword = %08x\n", dword);
141 if( memcmp(&dword, "\x7F""ELF", 4) == 0 ) {
142 fmt = &gElf_FormatDef;
150 printf("fmt->Load(%p): %p\n", fp, ret);
156 Binary_AddToList(Filename, ret, fmt);
158 entry = fmt->Relocate(ret);
159 printf("fmt->Relocate(%p): %p\n", ret, (void*)entry);
170 Binary_SetReadyToUse(ret);
175 void Binary_AddToList(const char *Filename, void *Base, tBinFmt *Format)
177 tBinary *bin = malloc(sizeof(tBinary) + strlen(Filename) + 1);
179 bin->Format = Format;
180 strcpy(bin->Path, Filename);
183 bin->Next = gLoadedBinaries;
184 gLoadedBinaries = bin;
187 void Binary_SetReadyToUse(void *Base)
190 for(bin = gLoadedBinaries; bin; bin = bin->Next)
192 if( bin->Base != Base ) continue ;
197 int Binary_GetSymbol(const char *SymbolName, uintptr_t *Value)
202 // TODO: Search list of loaded binaries
203 for(bin = gLoadedBinaries; bin; bin = bin->Next)
205 if( !bin->Ready ) continue;
206 printf("Binary_GetSymbol: bin = %p{%p, %s}\n", bin, bin->Base, bin->Path);
207 if( bin->Format->GetSymbol(bin->Base, (char*)SymbolName, Value) )
212 for( i = 0; i < ciNumBuiltinSymbols; i ++ )
214 if( strcmp(caBuiltinSymbols[i].Name, SymbolName) == 0 ) {
215 *Value = (uintptr_t)caBuiltinSymbols[i].Value;