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 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;
70 int len = strlen(LIBRARY_PATH)+1+nameLen+1;
73 strcpy(tmp, LIBRARY_PATH);
77 printf("Binary_LocateLibrary: tmp = '%s'\n", tmp);
86 fprintf(stderr, "Unable to locate library '%s'\n", Name);
91 void *Binary_LoadLibrary(const char *Name)
95 int (*entry)(int,char*[],char**) = NULL;
98 path = Binary_LocateLibrary(Name);
99 printf("Binary_LoadLibrary: path = '%s'\n", path);
104 ret = Binary_Load(path, (uintptr_t*)&entry);
107 printf("Binary_LoadLibrary: ret = %p, entry = %p\n", ret, entry);
109 char *argv[] = {NULL};
110 printf("Calling '%s' entry point %p\n", Name, entry);
111 entry(0, argv, NULL);
117 void *Binary_Load(const char *Filename, uintptr_t *EntryPoint)
125 // Ignore loading ld-acess
126 if( strcmp(Filename, "libld-acess.so") == 0 ) {
133 for(bin = gLoadedBinaries; bin; bin = bin->Next)
135 if( strcmp(Filename, bin->Path) == 0 ) {
141 fp = fopen(Filename, "r");
143 // TODO: Handle libary directories
144 perror("Opening binary");
148 fread(&dword, 1, 4, fp);
149 fseek(fp, 0, SEEK_SET);
151 if( memcmp(&dword, "\x7F""ELF", 4) == 0 ) {
152 fmt = &gElf_FormatDef;
159 printf("fmt->Load(%p)...\n", fp);
161 printf("fmt->Load(%p): %p\n", fp, ret);
167 Binary_AddToList(Filename, ret, fmt);
169 entry = fmt->Relocate(ret);
170 printf("fmt->Relocate(%p): %p\n", ret, (void*)entry);
181 Binary_SetReadyToUse(ret);
186 void Binary_AddToList(const char *Filename, void *Base, tBinFmt *Format)
188 tBinary *bin = malloc(sizeof(tBinary) + strlen(Filename) + 1);
190 bin->Format = Format;
191 strcpy(bin->Path, Filename);
194 bin->Next = gLoadedBinaries;
195 gLoadedBinaries = bin;
198 void Binary_SetReadyToUse(void *Base)
201 for(bin = gLoadedBinaries; bin; bin = bin->Next)
203 if( bin->Base != Base ) continue ;
208 int Binary_GetSymbol(const char *SymbolName, uintptr_t *Value)
213 //printf("Binary_GetSymbol: (SymbolName='%s', Value=%p)\n",
214 // SymbolName, Value);
217 // - Placed first to override smartarses that define their own versions
219 for( i = 0; i < ciNumBuiltinSymbols; i ++ )
221 if( strcmp(caBuiltinSymbols[i].Name, SymbolName) == 0 ) {
222 *Value = (uintptr_t)caBuiltinSymbols[i].Value;
227 // TODO: Search list of loaded binaries
228 for(bin = gLoadedBinaries; bin; bin = bin->Next)
230 if( !bin->Ready ) continue;
231 //printf(" Binary_GetSymbol: bin = %p{%p, %s}\n", bin, bin->Base, bin->Path);
232 if( bin->Format->GetSymbol(bin->Base, (char*)SymbolName, Value) )
236 //printf("Binary_GetSymbol: RETURN 0, not found\n");