Skip to content

Commit 536b889

Browse files
committed
Slightly improved fdlfcn functionality (still no clue on freeing those structs)
1 parent 6d2c510 commit 536b889

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

source/boot/libFrostedWM.so

66.7 KB
Binary file not shown.

source/includes/fdlfcn.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
#include <elf.h>
55

6-
typedef struct
6+
// signifies that (for example dlysm) should look through the list of loaded so files and find the address of the given symbol
7+
#define FLD_NEXT (void*)-1
8+
9+
typedef struct fdlfcn_handle
710
{
811
void* address;
912
Elf64_Ehdr ehdr;
@@ -24,6 +27,9 @@ typedef struct
2427
Elf64_Shdr* data_section_header;
2528
Elf64_Shdr* rodata_section_header;
2629
Elf64_Shdr* symtab_str_section_header;
30+
31+
struct fdlfcn_handle* prev;
32+
struct fdlfcn_handle* next;
2733
} fdlfcn_handle;
2834

2935
// immediately load sections into memory
@@ -33,4 +39,9 @@ fdlfcn_handle* fdlopen(void* filedata, int flags);
3339
void* fdlsym(fdlfcn_handle* handle, const char* symbol_name);
3440
int fdlclose(fdlfcn_handle* handle);
3541

42+
// defines that make my life easier
43+
#define dlopen fdlopen
44+
#define dlsym fdlsym
45+
#define dlclose fdlclose
46+
3647
#endif

source/kernel/C/fdlfcn.c

+45-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
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

1113
void* 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;

source/kernel/C/shell/sh.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,9 @@ extern int64* wm_addr;
9999

100100
void start_window_manager(){
101101
void* file_addr = wm_addr;
102-
elf_load_from_memory(file_addr);
103102
fdlfcn_handle* handle = fdlopen(file_addr, FDL_IMMEDIATE);
104103
int(*startfunction)(void);
105-
startfunction = (int(*)(void))fdlsym(handle, "_start");
104+
startfunction = (int(*)(void))fdlsym(FLD_NEXT, "_start");
106105
if (startfunction != NULL)
107106
{
108107
int result = startfunction();

0 commit comments

Comments
 (0)