9 #define LIBRARY_PATH "$$$$../Usermode/Output/x86/Libs"
12 typedef struct sBinary {
21 extern void *Elf_Load(int fd);
22 extern uintptr_t ElfRelocate(void *Base);
23 extern int ElfGetSymbol(void *Base, char *Name, uintptr_t *ret, size_t *size);
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 = ElfRelocate,
37 .GetSymbol = ElfGetSymbol
39 tBinary *gLoadedBinaries;
42 char *Binary_LocateLibrary(const char *Name)
44 char *envPath = getenv("ACESS_LIBRARY_PATH");
45 int nameLen = strlen(Name);
48 if( strcmp(Name, "libld-acess.so") == 0 ) {
49 return strdup("libld-acess.so");
52 // Try the environment ACESS_LIBRARY_PATH
53 if( envPath && envPath[0] != '\0' )
55 int len = strlen(envPath)+1+nameLen+1;
62 fd = acess_open(tmp, 4); // OPENFLAG_EXEC
70 int len = strlen(LIBRARY_PATH)+1+nameLen+1;
73 strcpy(tmp, LIBRARY_PATH);
78 printf("Binary_LocateLibrary: tmp = '%s'\n", tmp);
81 fd = acess_open(tmp, 4); // OPENFLAG_EXEC
89 fprintf(stderr, "Unable to locate library '%s'\n", Name);
95 void *Binary_LoadLibrary(const char *Name)
99 int (*entry)(void *,int,char*[],char**) = NULL;
102 path = Binary_LocateLibrary(Name);
104 printf("Binary_LoadLibrary: path = '%s'\n", path);
110 ret = Binary_Load(path, (uintptr_t*)&entry);
111 printf("LOADED '%s' to %p (Entry=%p)\n", path, ret, entry);
115 printf("Binary_LoadLibrary: ret = %p, entry = %p\n", ret, entry);
118 char *argv[] = {NULL};
120 printf("Calling '%s' entry point %p\n", Name, entry);
122 entry(ret, 0, argv, NULL);
128 void *Binary_Load(const char *Filename, uintptr_t *EntryPoint)
131 uint32_t dword = 0xFA17FA17;
136 // Ignore loading ld-acess
137 if( strcmp(Filename, "libld-acess.so") == 0 ) {
144 for(bin = gLoadedBinaries; bin; bin = bin->Next)
146 if( strcmp(Filename, bin->Path) == 0 ) {
152 fd = acess_open(Filename, 2|1); // Execute and Read
154 // TODO: Handle libary directories
155 perror("Opening binary");
159 acess_read(fd, &dword, 4);
160 acess_seek(fd, 0, ACESS_SEEK_SET);
162 if( memcmp(&dword, "\x7F""ELF", 4) == 0 ) {
163 fmt = &gElf_FormatDef;
166 fprintf(stderr, "Unknown executable format (0x%08x)\n", dword);
172 printf("fmt->Load(%i)...\n", fd);
177 printf("fmt->Load(%p): %p\n", fd, ret);
183 Binary_AddToList(Filename, ret, fmt);
185 entry = fmt->Relocate(ret);
187 printf("fmt->Relocate(%p): %p\n", ret, (void*)entry);
197 Binary_SetReadyToUse(ret);
202 void Binary_AddToList(const char *Filename, void *Base, tBinFmt *Format)
204 tBinary *bin = malloc(sizeof(tBinary) + strlen(Filename) + 1);
206 bin->Format = Format;
207 strcpy(bin->Path, Filename);
210 bin->Next = gLoadedBinaries;
211 gLoadedBinaries = bin;
214 void Binary_SetReadyToUse(void *Base)
217 for(bin = gLoadedBinaries; bin; bin = bin->Next)
219 if( bin->Base != Base ) continue ;
224 int Binary_GetSymbol(const char *SymbolName, uintptr_t *Value, size_t *Size)
229 //printf("Binary_GetSymbol: (SymbolName='%s', Value=%p)\n",
230 // SymbolName, Value);
233 // - Placed first to override smartarses that define their own versions
235 for( i = 0; i < ciNumBuiltinSymbols; i ++ )
237 if( strcmp(caBuiltinSymbols[i].Name, SymbolName) == 0 ) {
238 *Value = (uintptr_t)caBuiltinSymbols[i].Value;
244 // Search list of loaded binaries
245 for(bin = gLoadedBinaries; bin; bin = bin->Next)
247 if( !bin->Ready ) continue;
248 //printf(" Binary_GetSymbol: bin = %p{%p, %s}\n", bin, bin->Base, bin->Path);
249 if( bin->Format->GetSymbol(bin->Base, (char*)SymbolName, Value, Size) )
253 //printf("Binary_GetSymbol: RETURN 0, not found\n");