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[];
29 void Binary_AddToList(const char *Filename, void *Base, tBinFmt *Format);
32 tBinFmt gElf_FormatDef = {
33 // .Mask = 0xFFFFFFFF,
34 // .Magic = "\x7F""ELF",
37 .Relocate = ElfRelocate,
38 .GetSymbol = ElfGetSymbol
40 tBinary *gLoadedBinaries;
43 char *Binary_LocateLibrary(const char *Name)
45 char *envPath = getenv("ACESS_LIBRARY_PATH");
46 int nameLen = strlen(Name);
49 if( strcmp(Name, "libld-acess.so") == 0 ) {
50 return strdup("libld-acess.so");
53 // Try the environment ACESS_LIBRARY_PATH
54 if( envPath && envPath[0] != '\0' )
56 int len = strlen(envPath)+1+nameLen+1;
63 fd = acess_open(tmp, 4); // OPENFLAG_EXEC
71 int len = strlen(LIBRARY_PATH)+1+nameLen+1;
74 strcpy(tmp, LIBRARY_PATH);
79 printf("Binary_LocateLibrary: tmp = '%s'\n", tmp);
82 fd = acess_open(tmp, 4); // OPENFLAG_EXEC
90 fprintf(stderr, "Unable to locate library '%s'\n", Name);
96 void *Binary_LoadLibrary(const char *Name)
100 int (*entry)(void *,int,char*[],char**) = NULL;
103 path = Binary_LocateLibrary(Name);
105 printf("Binary_LoadLibrary: path = '%s'\n", path);
111 ret = Binary_Load(path, (uintptr_t*)&entry);
112 printf("LOADED '%s' to %p (Entry=%p)\n", path, ret, entry);
116 printf("Binary_LoadLibrary: ret = %p, entry = %p\n", ret, entry);
119 char *argv[] = {NULL};
121 printf("Calling '%s' entry point %p\n", Name, entry);
123 entry(ret, 0, argv, NULL);
129 void *Binary_Load(const char *Filename, uintptr_t *EntryPoint)
132 uint32_t dword = 0xFA17FA17;
137 // Ignore loading ld-acess
138 if( strcmp(Filename, "libld-acess.so") == 0 ) {
145 for(bin = gLoadedBinaries; bin; bin = bin->Next)
147 if( strcmp(Filename, bin->Path) == 0 ) {
153 fd = acess_open(Filename, 2|1); // Execute and Read
155 // TODO: Handle libary directories
156 perror("Opening binary");
160 acess_read(fd, &dword, 4);
161 acess_seek(fd, 0, ACESS_SEEK_SET);
163 if( memcmp(&dword, "\x7F""ELF", 4) == 0 ) {
164 fmt = &gElf_FormatDef;
167 fprintf(stderr, "Unknown executable format (0x%08x)\n", dword);
173 printf("fmt->Load(0x%x)...\n", fd);
178 printf("fmt->Load(0x%x): %p\n", fd, ret);
184 Binary_AddToList(Filename, ret, fmt);
186 entry = fmt->Relocate(ret);
188 printf("fmt->Relocate(%p): %p\n", ret, (void*)entry);
198 Binary_SetReadyToUse(ret);
203 void Binary_AddToList(const char *Filename, void *Base, tBinFmt *Format)
205 tBinary *bin = malloc(sizeof(tBinary) + strlen(Filename) + 1);
207 bin->Format = Format;
208 strcpy(bin->Path, Filename);
211 bin->Next = gLoadedBinaries;
212 gLoadedBinaries = bin;
215 void Binary_SetReadyToUse(void *Base)
218 for(bin = gLoadedBinaries; bin; bin = bin->Next)
220 if( bin->Base != Base ) continue ;
225 int Binary_GetSymbol(const char *SymbolName, uintptr_t *Value, size_t *Size)
230 //printf("Binary_GetSymbol: (SymbolName='%s', Value=%p)\n",
231 // SymbolName, Value);
234 // - Placed first to override smartarses that define their own versions
236 for( i = 0; i < ciNumBuiltinSymbols; i ++ )
238 if( strcmp(caBuiltinSymbols[i].Name, SymbolName) == 0 ) {
239 *Value = (uintptr_t)caBuiltinSymbols[i].Value;
245 // Search list of loaded binaries
246 for(bin = gLoadedBinaries; bin; bin = bin->Next)
248 if( !bin->Ready ) continue;
249 //printf(" Binary_GetSymbol: bin = %p{%p, %s}\n", bin, bin->Base, bin->Path);
250 if( bin->Format->GetSymbol(bin->Base, (char*)SymbolName, Value, Size) )
254 //printf("Binary_GetSymbol: RETURN 0, not found\n");
255 printf("--- ERROR: Unable to find symbol '%s'\n", SymbolName);