66#include <heap.h>
77#include <elf.h>
88
9- #define READ_FROM_MEMORY (dest , base , offset , size ) printf("reading at address 0x%x", (uint64_t)base + (uint64_t)offset); memcpy(dest, (void*)( (uint64_t)base + (uint64_t)offset ), size)
9+ fdlfcn_handle * global_library_handles ;
10+
11+ #define READ_FROM_MEMORY (dest , base , offset , size ) memcpy(dest, (void*)( (uint64_t)base + (uint64_t)offset ), size)
1012
1113void * fdl_load_section (void * filedata , Elf64_Shdr * section_header )
1214{
@@ -69,6 +71,17 @@ void* fdlsym(fdlfcn_handle* handle, const char* symbol_name)
6971{
7072 if (handle == NULL )
7173 return NULL ;
74+ if (handle == FLD_NEXT )
75+ {
76+ for (fdlfcn_handle * entry = global_library_handles ; entry != NULL ; entry = entry -> next )
77+ {
78+ void * addr = fdlsym (entry , symbol_name );
79+ if (addr != NULL )
80+ return addr ;
81+ }
82+ printf ("Error: No library loaded with symbol '%s'" , symbol_name );
83+ return NULL ;
84+ }
7285
7386 Elf64_Sym * symbols = handle -> symbols ;
7487
@@ -93,8 +106,7 @@ void* fdlsym(fdlfcn_handle* handle, const char* symbol_name)
93106 for (int j = 0 ; j < symtab_section .sh_size / sizeof (Elf64_Sym ); j ++ )
94107 {
95108 Elf64_Sym symbol = symbols [j ];
96- char * symbol_name_str = (char * )((uint64_t )handle -> symtab_str_section_data + symbol .st_name );
97- printf ("Symbol name: '%s'" , symbol_name_str );
109+ char * symbol_name_str = (char * )((uint64_t )handle -> symtab_str_section_data + symbol .st_name );
98110 if (strcmp (symbol_name_str , symbol_name ) == 0 && ELF64_ST_BIND (symbol .st_info ) != STB_LOCAL && symbol .st_shndx == handle -> text_section_index )
99111 {
100112 uintptr_t symbol_address = (uintptr_t )handle -> text_section_data + symbol .st_value - handle -> text_section_header -> sh_offset ;
@@ -112,6 +124,23 @@ int fdlclose(fdlfcn_handle* handle)
112124 if (handle == NULL )
113125 return 1 ;
114126
127+ for (fdlfcn_handle * entry = global_library_handles ; entry != NULL ; entry = entry -> next )
128+ {
129+ if (entry != handle )
130+ continue ;
131+
132+ if (entry -> prev != NULL )
133+ entry -> prev -> next = entry -> next ;
134+
135+ if (entry -> next != NULL )
136+ entry -> next -> prev = entry -> prev ;
137+
138+ if (entry == global_library_handles )
139+ global_library_handles = NULL ;
140+
141+ break ;
142+ }
143+
115144 return 0 ; /// TODO: somehow all the free calls cause a gp fault, fix that
116145
117146 if (handle -> text_section_data != NULL )
@@ -274,6 +303,8 @@ fdlfcn_handle* fdlopen(void* filedata, int flags)
274303 handle -> ehdr = elf_header ;
275304 handle -> shdrs = section_headers ;
276305 handle -> symtab_index = symtab_index ;
306+ handle -> next = NULL ;
307+ handle -> prev = NULL ;
277308
278309 if (fdl_apply_relocations (handle , reloc_section_index ) != 0 && reloc_section_index != -1 )
279310 {
@@ -297,6 +328,17 @@ fdlfcn_handle* fdlopen(void* filedata, int flags)
297328 return NULL ;
298329 }
299330
331+ if (global_library_handles == NULL )
332+ {
333+ global_library_handles = handle ;
334+ }
335+ else
336+ {
337+ global_library_handles -> prev = handle ;
338+ handle -> next = global_library_handles ;
339+ global_library_handles = handle ;
340+ }
341+
300342 info ("fdlopen: Successfully loaded .so file" , __FILE__ );
301343
302344 return handle ;
0 commit comments