2 * AcessNative Dynamic Linker
3 * - By John Hodge (thePowersGang)
6 * - Provides binary loading and type abstraction
14 #define LIBRARY_PATH "$$$$../Usermode/Output/x86_64/Libs"
17 typedef struct sBinary {
26 extern void *Elf_Load(int fd);
27 extern uintptr_t ElfRelocate(void *Base);
28 extern int ElfGetSymbol(void *Base, char *Name, uintptr_t *ret, size_t *size);
29 extern int ciNumBuiltinSymbols;
30 extern tSym caBuiltinSymbols[];
34 void Binary_AddToList(const char *Filename, void *Base, tBinFmt *Format);
37 tBinFmt gElf_FormatDef = {
38 // .Mask = 0xFFFFFFFF,
39 // .Magic = "\x7F""ELF",
42 .Relocate = ElfRelocate,
43 .GetSymbol = ElfGetSymbol
45 tBinary *gLoadedBinaries;
48 char *Binary_LocateLibrary(const char *Name)
50 char *envPath = getenv("ACESS_LIBRARY_PATH");
51 int nameLen = strlen(Name);
54 if( strcmp(Name, "libld-acess.so") == 0 ) {
55 return strdup("libld-acess.so");
58 // Try the environment ACESS_LIBRARY_PATH
59 if( envPath && envPath[0] != '\0' )
61 int len = strlen(envPath)+1+nameLen+1;
68 fd = acess_open(tmp, 4); // OPENFLAG_EXEC
76 int len = strlen(LIBRARY_PATH)+1+nameLen+1;
79 strcpy(tmp, LIBRARY_PATH);
84 printf("Binary_LocateLibrary: tmp = '%s'\n", tmp);
87 fd = acess_open(tmp, 4); // OPENFLAG_EXEC
95 fprintf(stderr, "Unable to locate library '%s'\n", Name);
101 void *Binary_LoadLibrary(const char *Name)
105 int (*entry)(void *,int,char*[],char**) = NULL;
108 path = Binary_LocateLibrary(Name);
110 printf("Binary_LoadLibrary: path = '%s'\n", path);
116 ret = Binary_Load(path, (uintptr_t*)&entry);
117 if( ret != (void*)-1 )
118 Debug("LOADED '%s' to %p (Entry=%p)", path, ret, entry);
122 printf("Binary_LoadLibrary: ret = %p, entry = %p\n", ret, entry);
125 char *argv[] = {NULL};
127 printf("Calling '%s' entry point %p\n", Name, entry);
129 entry(ret, 0, argv, gEnvP);
135 void *Binary_Load(const char *Filename, uintptr_t *EntryPoint)
138 uint32_t dword = 0xFA17FA17;
143 // Ignore loading ld-acess
144 if( strcmp(Filename, "libld-acess.so") == 0 ) {
151 for(bin = gLoadedBinaries; bin; bin = bin->Next)
153 if( strcmp(Filename, bin->Path) == 0 ) {
159 fd = acess_open(Filename, 2|1); // Execute and Read
161 // TODO: Handle libary directories
162 perror("Opening binary");
166 acess_read(fd, &dword, 4);
167 acess_seek(fd, 0, ACESS_SEEK_SET);
169 if( memcmp(&dword, "\x7F""ELF", 4) == 0 ) {
170 fmt = &gElf_FormatDef;
173 fprintf(stderr, "Unknown executable format (0x%08x)\n", dword);
179 printf("fmt->Load(0x%x)...\n", fd);
184 printf("fmt->Load(0x%x): %p\n", fd, ret);
190 Binary_AddToList(Filename, ret, fmt);
192 entry = fmt->Relocate(ret);
194 printf("fmt->Relocate(%p): %p\n", ret, (void*)entry);
204 Binary_SetReadyToUse(ret);
209 void Binary_AddToList(const char *Filename, void *Base, tBinFmt *Format)
211 tBinary *bin = malloc(sizeof(tBinary) + strlen(Filename) + 1);
213 bin->Format = Format;
214 strcpy(bin->Path, Filename);
217 bin->Next = gLoadedBinaries;
218 gLoadedBinaries = bin;
221 void Binary_SetReadyToUse(void *Base)
224 for(bin = gLoadedBinaries; bin; bin = bin->Next)
226 if( bin->Base != Base ) continue ;
231 int Binary_GetSymbol(const char *SymbolName, uintptr_t *Value, size_t *Size)
236 //printf("Binary_GetSymbol: (SymbolName='%s', Value=%p)\n",
237 // SymbolName, Value);
240 // - Placed first to override smartarses that define their own versions
242 for( i = 0; i < ciNumBuiltinSymbols; i ++ )
244 if( strcmp(caBuiltinSymbols[i].Name, SymbolName) == 0 ) {
245 *Value = (uintptr_t)caBuiltinSymbols[i].Value;
251 // Search list of loaded binaries
252 for(bin = gLoadedBinaries; bin; bin = bin->Next)
254 if( !bin->Ready ) continue;
255 //printf(" Binary_GetSymbol: bin = %p{%p, %s}\n", bin, bin->Base, bin->Path);
256 if( bin->Format->GetSymbol(bin->Base, (char*)SymbolName, Value, Size) )
260 //printf("Binary_GetSymbol: RETURN 0, not found\n");
261 printf("--- ERROR: Unable to find symbol '%s'\n", SymbolName);