10 #define LIBRARY_PATH "$$$$../Usermode/Output/x86_64/Libs"
13 typedef struct sBinary {
22 extern void *Elf_Load(int fd);
23 extern uintptr_t ElfRelocate(void *Base);
24 extern int ElfGetSymbol(void *Base, char *Name, uintptr_t *ret, size_t *size);
25 extern int ciNumBuiltinSymbols;
26 extern tSym caBuiltinSymbols[];
30 void Binary_AddToList(const char *Filename, void *Base, tBinFmt *Format);
33 tBinFmt gElf_FormatDef = {
34 // .Mask = 0xFFFFFFFF,
35 // .Magic = "\x7F""ELF",
38 .Relocate = ElfRelocate,
39 .GetSymbol = ElfGetSymbol
41 tBinary *gLoadedBinaries;
44 char *Binary_LocateLibrary(const char *Name)
46 char *envPath = getenv("ACESS_LIBRARY_PATH");
47 int nameLen = strlen(Name);
50 if( strcmp(Name, "libld-acess.so") == 0 ) {
51 return strdup("libld-acess.so");
54 // Try the environment ACESS_LIBRARY_PATH
55 if( envPath && envPath[0] != '\0' )
57 int len = strlen(envPath)+1+nameLen+1;
64 fd = acess_open(tmp, 4); // OPENFLAG_EXEC
72 int len = strlen(LIBRARY_PATH)+1+nameLen+1;
75 strcpy(tmp, LIBRARY_PATH);
80 printf("Binary_LocateLibrary: tmp = '%s'\n", tmp);
83 fd = acess_open(tmp, 4); // OPENFLAG_EXEC
91 fprintf(stderr, "Unable to locate library '%s'\n", Name);
97 void *Binary_LoadLibrary(const char *Name)
101 int (*entry)(void *,int,char*[],char**) = NULL;
104 path = Binary_LocateLibrary(Name);
106 printf("Binary_LoadLibrary: path = '%s'\n", path);
112 ret = Binary_Load(path, (uintptr_t*)&entry);
113 printf("LOADED '%s' to %p (Entry=%p)\n", path, ret, entry);
117 printf("Binary_LoadLibrary: ret = %p, entry = %p\n", ret, entry);
120 char *argv[] = {NULL};
122 printf("Calling '%s' entry point %p\n", Name, entry);
124 entry(ret, 0, argv, gEnvP);
130 void *Binary_Load(const char *Filename, uintptr_t *EntryPoint)
133 uint32_t dword = 0xFA17FA17;
138 // Ignore loading ld-acess
139 if( strcmp(Filename, "libld-acess.so") == 0 ) {
146 for(bin = gLoadedBinaries; bin; bin = bin->Next)
148 if( strcmp(Filename, bin->Path) == 0 ) {
154 fd = acess_open(Filename, 2|1); // Execute and Read
156 // TODO: Handle libary directories
157 perror("Opening binary");
161 acess_read(fd, &dword, 4);
162 acess_seek(fd, 0, ACESS_SEEK_SET);
164 if( memcmp(&dword, "\x7F""ELF", 4) == 0 ) {
165 fmt = &gElf_FormatDef;
168 fprintf(stderr, "Unknown executable format (0x%08x)\n", dword);
174 printf("fmt->Load(0x%x)...\n", fd);
179 printf("fmt->Load(0x%x): %p\n", fd, ret);
185 Binary_AddToList(Filename, ret, fmt);
187 entry = fmt->Relocate(ret);
189 printf("fmt->Relocate(%p): %p\n", ret, (void*)entry);
199 Binary_SetReadyToUse(ret);
204 void Binary_AddToList(const char *Filename, void *Base, tBinFmt *Format)
206 tBinary *bin = malloc(sizeof(tBinary) + strlen(Filename) + 1);
208 bin->Format = Format;
209 strcpy(bin->Path, Filename);
212 bin->Next = gLoadedBinaries;
213 gLoadedBinaries = bin;
216 void Binary_SetReadyToUse(void *Base)
219 for(bin = gLoadedBinaries; bin; bin = bin->Next)
221 if( bin->Base != Base ) continue ;
226 int Binary_GetSymbol(const char *SymbolName, uintptr_t *Value, size_t *Size)
231 //printf("Binary_GetSymbol: (SymbolName='%s', Value=%p)\n",
232 // SymbolName, Value);
235 // - Placed first to override smartarses that define their own versions
237 for( i = 0; i < ciNumBuiltinSymbols; i ++ )
239 if( strcmp(caBuiltinSymbols[i].Name, SymbolName) == 0 ) {
240 *Value = (uintptr_t)caBuiltinSymbols[i].Value;
246 // Search list of loaded binaries
247 for(bin = gLoadedBinaries; bin; bin = bin->Next)
249 if( !bin->Ready ) continue;
250 //printf(" Binary_GetSymbol: bin = %p{%p, %s}\n", bin, bin->Base, bin->Path);
251 if( bin->Format->GetSymbol(bin->Base, (char*)SymbolName, Value, Size) )
255 //printf("Binary_GetSymbol: RETURN 0, not found\n");
256 printf("--- ERROR: Unable to find symbol '%s'\n", SymbolName);