9 #define LIBRARY_PATH "$$$$../Usermode/Output/i386/Libs"
12 typedef struct sBinary {
21 extern void *Elf_Load(int fd);
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 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)(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);
114 printf("Binary_LoadLibrary: ret = %p, entry = %p\n", ret, entry);
117 char *argv[] = {NULL};
119 printf("Calling '%s' entry point %p\n", Name, entry);
121 entry(0, argv, NULL);
127 void *Binary_Load(const char *Filename, uintptr_t *EntryPoint)
130 uint32_t dword = 0xFA17FA17;
135 // Ignore loading ld-acess
136 if( strcmp(Filename, "libld-acess.so") == 0 ) {
143 for(bin = gLoadedBinaries; bin; bin = bin->Next)
145 if( strcmp(Filename, bin->Path) == 0 ) {
151 fd = acess_open(Filename, 2|1); // Execute and Read
153 // TODO: Handle libary directories
154 perror("Opening binary");
158 acess_read(fd, 4, &dword);
159 acess_seek(fd, 0, ACESS_SEEK_SET);
161 if( memcmp(&dword, "\x7F""ELF", 4) == 0 ) {
162 fmt = &gElf_FormatDef;
165 fprintf(stderr, "Unknown executable format (0x%08x)\n", dword);
171 printf("fmt->Load(%i)...\n", fd);
176 printf("fmt->Load(%p): %p\n", fd, ret);
182 Binary_AddToList(Filename, ret, fmt);
184 entry = fmt->Relocate(ret);
186 printf("fmt->Relocate(%p): %p\n", ret, (void*)entry);
196 Binary_SetReadyToUse(ret);
201 void Binary_AddToList(const char *Filename, void *Base, tBinFmt *Format)
203 tBinary *bin = malloc(sizeof(tBinary) + strlen(Filename) + 1);
205 bin->Format = Format;
206 strcpy(bin->Path, Filename);
209 bin->Next = gLoadedBinaries;
210 gLoadedBinaries = bin;
213 void Binary_SetReadyToUse(void *Base)
216 for(bin = gLoadedBinaries; bin; bin = bin->Next)
218 if( bin->Base != Base ) continue ;
223 int Binary_GetSymbol(const char *SymbolName, uintptr_t *Value)
228 //printf("Binary_GetSymbol: (SymbolName='%s', Value=%p)\n",
229 // SymbolName, Value);
232 // - Placed first to override smartarses that define their own versions
234 for( i = 0; i < ciNumBuiltinSymbols; i ++ )
236 if( strcmp(caBuiltinSymbols[i].Name, SymbolName) == 0 ) {
237 *Value = (uintptr_t)caBuiltinSymbols[i].Value;
242 // Search list of loaded binaries
243 for(bin = gLoadedBinaries; bin; bin = bin->Next)
245 if( !bin->Ready ) continue;
246 //printf(" Binary_GetSymbol: bin = %p{%p, %s}\n", bin, bin->Base, bin->Path);
247 if( bin->Format->GetSymbol(bin->Base, (char*)SymbolName, Value) )
251 //printf("Binary_GetSymbol: RETURN 0, not found\n");