Skip to content

Commit 78f8c88

Browse files
committed
Actually working(yes.) function loading and calling
1 parent ee44d1e commit 78f8c88

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

source/includes/fdlfcn.h

+3
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ typedef struct
1212
Elf64_Rela* relocations;
1313

1414
int symtab_index;
15+
int text_section_index;
1516
void* string_table_data;
1617
void* text_section_data;
1718
void* data_section_data;
1819
void* rodata_section_data;
1920
void* symtab_str_section_data;
2021

2122
Elf64_Shdr* text_section_header;
23+
Elf64_Shdr* string_table_header;
2224
Elf64_Shdr* data_section_header;
2325
Elf64_Shdr* rodata_section_header;
26+
Elf64_Shdr* symtab_str_section_header;
2427
} fdlfcn_handle;
2528

2629
// immediately load sections into memory

source/kernel/C/fdlfcn.c

+16-7
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@ void* fdlsym(fdlfcn_handle* handle, const char* symbol_name)
9292

9393
for (int j = 0; j < symtab_section.sh_size / sizeof(Elf64_Sym); j++)
9494
{
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)
9799
{
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;
101103
}
102104
}
103105

@@ -172,6 +174,7 @@ fdlfcn_handle* fdlopen(void* filedata, int flags)
172174
int data_section_index = -1;
173175
int rodata_section_index = -1;
174176
int reloc_section_index = -1;
177+
int symtab_str_section_index = -1;
175178

176179
void* strtableAddr = fdl_load_section(filedata, &section_headers[strtab_index]);
177180
for (int i = 0; i < elf_header.e_shnum; i++)
@@ -239,7 +242,9 @@ fdlfcn_handle* fdlopen(void* filedata, int flags)
239242
Elf64_Shdr symtab_section = section_headers[symtab_index];
240243
handle->symbols = malloc(symtab_section.sh_size);
241244
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]);
243248
}
244249
else
245250
handle->symbols = NULL;
@@ -255,12 +260,16 @@ fdlfcn_handle* fdlopen(void* filedata, int flags)
255260

256261
handle->address = text_section_data;
257262
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;
259264
handle->text_section_header = &section_headers[text_section_index];
265+
handle->string_table_data = strtableAddr;
266+
handle->string_table_header = &section_headers[strtab_index];
260267
handle->data_section_data = data_section_data;
261268
handle->data_section_header = &section_headers[data_section_index];
262269
handle->rodata_section_data = rodata_section_data;
263270
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];
264273
handle->string_table_data = strtableAddr;
265274
handle->ehdr = elf_header;
266275
handle->shdrs = section_headers;

source/kernel/C/kernel.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,14 @@ void main(void) {
384384
void* file_addr = module_request.response->modules[0]->address;
385385
elf_load_from_memory(file_addr);
386386
fdlfcn_handle* handle = fdlopen(file_addr, FDL_IMMEDIATE);
387-
void* startFunction = fdlsym(handle, "frostedwm_create_context");
388-
if (startFunction != NULL)
387+
int(*startfunction)(void);
388+
startfunction = (int(*)(void))fdlsym(handle, "_start");
389+
if (startfunction != NULL)
390+
{
391+
int result = startfunction();
392+
printf("Result function: %d\n", result);
389393
info("Successfully loaded function from .so file", __FILE__);
394+
}
390395
fdlclose(handle);
391396

392397
int failed_attempts = 0;

0 commit comments

Comments
 (0)