@@ -92,12 +92,14 @@ void* fdlsym(fdlfcn_handle* handle, const char* symbol_name)
92
92
93
93
for (int j = 0 ; j < symtab_section .sh_size / sizeof (Elf64_Sym ); j ++ )
94
94
{
95
- char * symbol_name_str = (char * )((uint64_t )handle -> symtab_str_section_data + symbols [j ].st_name );
96
- if (strcmp (symbol_name_str , symbol_name ) == 0 && ELF64_ST_BIND (symbols [j ].st_info ) != STB_LOCAL )
95
+ 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 );
98
+ if (strcmp (symbol_name_str , symbol_name ) == 0 && ELF64_ST_BIND (symbol .st_info ) != STB_LOCAL && symbol .st_shndx == handle -> text_section_index )
97
99
{
98
- uintptr_t symbol_address = (uintptr_t )handle -> text_section_data + symbols [ j ] .st_value ;
99
- printf ( "Found symbol '%s'" , symbol_name );
100
- return (void * )symbol_address ;
100
+ uintptr_t symbol_address = (uintptr_t )handle -> text_section_data + symbol .st_value - handle -> text_section_header -> sh_offset ;
101
+ if ( symbol . st_shndx != SHN_UNDEF )
102
+ return (void * )symbol_address ;
101
103
}
102
104
}
103
105
@@ -172,6 +174,7 @@ fdlfcn_handle* fdlopen(void* filedata, int flags)
172
174
int data_section_index = -1 ;
173
175
int rodata_section_index = -1 ;
174
176
int reloc_section_index = -1 ;
177
+ int symtab_str_section_index = -1 ;
175
178
176
179
void * strtableAddr = fdl_load_section (filedata , & section_headers [strtab_index ]);
177
180
for (int i = 0 ; i < elf_header .e_shnum ; i ++ )
@@ -239,7 +242,9 @@ fdlfcn_handle* fdlopen(void* filedata, int flags)
239
242
Elf64_Shdr symtab_section = section_headers [symtab_index ];
240
243
handle -> symbols = malloc (symtab_section .sh_size );
241
244
READ_FROM_MEMORY (handle -> symbols , filedata , symtab_section .sh_offset , symtab_section .sh_size );
242
- symtab_str_section_data = fdl_load_section (filedata , & section_headers [symtab_section .sh_link ]);
245
+ symtab_str_section_index = symtab_section .sh_link ;
246
+ if (symtab_str_section_index != -1 )
247
+ symtab_str_section_data = fdl_load_section (filedata , & section_headers [symtab_str_section_index ]);
243
248
}
244
249
else
245
250
handle -> symbols = NULL ;
@@ -255,12 +260,16 @@ fdlfcn_handle* fdlopen(void* filedata, int flags)
255
260
256
261
handle -> address = text_section_data ;
257
262
handle -> text_section_data = text_section_data ;
258
- handle -> symtab_str_section_data = symtab_str_section_data ;
263
+ handle -> text_section_index = text_section_index ;
259
264
handle -> text_section_header = & section_headers [text_section_index ];
265
+ handle -> string_table_data = strtableAddr ;
266
+ handle -> string_table_header = & section_headers [strtab_index ];
260
267
handle -> data_section_data = data_section_data ;
261
268
handle -> data_section_header = & section_headers [data_section_index ];
262
269
handle -> rodata_section_data = rodata_section_data ;
263
270
handle -> rodata_section_header = & section_headers [rodata_section_index ];
271
+ handle -> symtab_str_section_data = symtab_str_section_data ;
272
+ handle -> symtab_str_section_header = & section_headers [symtab_str_section_index ];
264
273
handle -> string_table_data = strtableAddr ;
265
274
handle -> ehdr = elf_header ;
266
275
handle -> shdrs = section_headers ;
0 commit comments