6
6
#include <heap.h>
7
7
#include <elf.h>
8
8
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)
10
12
11
13
void * fdl_load_section (void * filedata , Elf64_Shdr * section_header )
12
14
{
@@ -69,6 +71,17 @@ void* fdlsym(fdlfcn_handle* handle, const char* symbol_name)
69
71
{
70
72
if (handle == NULL )
71
73
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
+ }
72
85
73
86
Elf64_Sym * symbols = handle -> symbols ;
74
87
@@ -93,8 +106,7 @@ void* fdlsym(fdlfcn_handle* handle, const char* symbol_name)
93
106
for (int j = 0 ; j < symtab_section .sh_size / sizeof (Elf64_Sym ); j ++ )
94
107
{
95
108
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 );
98
110
if (strcmp (symbol_name_str , symbol_name ) == 0 && ELF64_ST_BIND (symbol .st_info ) != STB_LOCAL && symbol .st_shndx == handle -> text_section_index )
99
111
{
100
112
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)
112
124
if (handle == NULL )
113
125
return 1 ;
114
126
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
+
115
144
return 0 ; /// TODO: somehow all the free calls cause a gp fault, fix that
116
145
117
146
if (handle -> text_section_data != NULL )
@@ -274,6 +303,8 @@ fdlfcn_handle* fdlopen(void* filedata, int flags)
274
303
handle -> ehdr = elf_header ;
275
304
handle -> shdrs = section_headers ;
276
305
handle -> symtab_index = symtab_index ;
306
+ handle -> next = NULL ;
307
+ handle -> prev = NULL ;
277
308
278
309
if (fdl_apply_relocations (handle , reloc_section_index ) != 0 && reloc_section_index != -1 )
279
310
{
@@ -297,6 +328,17 @@ fdlfcn_handle* fdlopen(void* filedata, int flags)
297
328
return NULL ;
298
329
}
299
330
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
+
300
342
info ("fdlopen: Successfully loaded .so file" , __FILE__ );
301
343
302
344
return handle ;
0 commit comments