wip: extend shared library support, window demo
This commit is contained in:
@@ -107,6 +107,10 @@ static bool parseSymbolFile(const char* symPath, LibHandle* lib) {
|
||||
|
||||
static uint64_t hexToUint64(const char* str) {
|
||||
uint64_t result = 0;
|
||||
// Skip optional "0x" prefix
|
||||
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
|
||||
str += 2;
|
||||
}
|
||||
while (*str) {
|
||||
char c = *str;
|
||||
uint64_t val = 0;
|
||||
@@ -164,14 +168,9 @@ LibHandle* dlopen(const char* path) {
|
||||
void* dlsym(LibHandle* lib, const char* symbolName) {
|
||||
if (!lib || !symbolName) return nullptr;
|
||||
|
||||
// Get base address if we don't have it
|
||||
// Get base address from kernel if we don't have it
|
||||
if (lib->base == 0) {
|
||||
// The base is stored in the handle - we need a syscall to get it
|
||||
// For now, we'll use a workaround: calculate base from slot
|
||||
// handle = slot + 1, so slot = handle - 1
|
||||
// base = 0x60000000 + slot * 0x200000
|
||||
int slot = lib->handle - 1;
|
||||
lib->base = 0x60000000ULL + (uint64_t)slot * 0x200000ULL;
|
||||
lib->base = montauk::get_libbase(lib->handle);
|
||||
}
|
||||
|
||||
// Look up in symbol table
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
#include <montauk/syscall.h>
|
||||
#include <libloader/libloader.h>
|
||||
|
||||
// Forward declaration
|
||||
// Forward declarations
|
||||
static void print_int(int val);
|
||||
static void print_hex(uint64_t val);
|
||||
|
||||
extern "C" void _start() {
|
||||
montauk::print("=== Dynamic Library Test ===\n\n");
|
||||
@@ -78,6 +79,39 @@ extern "C" void _start() {
|
||||
libloader::dlclose(lib);
|
||||
montauk::print("Library unloaded.\n");
|
||||
|
||||
// Test loading libhello (GUI library)
|
||||
montauk::print("\nLoading libhello (GUI library)...\n");
|
||||
LibHandle* hellolib = libloader::dlopen("0:/os/libhello.lib");
|
||||
if (!hellolib) {
|
||||
montauk::print("ERROR: Failed to load libhello.lib\n");
|
||||
montauk::exit(1);
|
||||
}
|
||||
montauk::print("Library loaded successfully!\n\n");
|
||||
|
||||
// Resolve hello_run
|
||||
typedef int (*hello_run_t)(void);
|
||||
hello_run_t hello_run = (hello_run_t)libloader::dlsym(hellolib, "hello_run");
|
||||
if (!hello_run) {
|
||||
montauk::print("ERROR: Failed to resolve hello_run\n");
|
||||
montauk::exit(1);
|
||||
}
|
||||
montauk::print(" hello_run: OK\n\n");
|
||||
|
||||
// Debug: print hello_run address
|
||||
montauk::print(" DEBUG: hello_run @ ");
|
||||
print_hex((uint64_t)hello_run);
|
||||
montauk::print("\n");
|
||||
|
||||
// Run the GUI demo (this creates a window and event loop)
|
||||
montauk::print("Running GUI demo...\n");
|
||||
hello_run();
|
||||
|
||||
// Unload the library
|
||||
montauk::print("GUI demo closed.\n");
|
||||
montauk::print("Unloading library...\n");
|
||||
libloader::dlclose(hellolib);
|
||||
montauk::print("Library unloaded.\n");
|
||||
|
||||
montauk::print("\n=== All tests passed! ===\n");
|
||||
montauk::exit(0);
|
||||
}
|
||||
@@ -110,3 +144,25 @@ static void print_int(int val) {
|
||||
montauk::putchar(buf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Print hex value (uppercase, with 0x prefix)
|
||||
static void print_hex(uint64_t val) {
|
||||
montauk::print("0x");
|
||||
char buf[20];
|
||||
int idx = 0;
|
||||
|
||||
if (val == 0) {
|
||||
buf[idx++] = '0';
|
||||
} else {
|
||||
while (val > 0) {
|
||||
int digit = val & 0xF;
|
||||
buf[idx++] = (digit < 10) ? ('0' + digit) : ('A' + digit - 10);
|
||||
val >>= 4;
|
||||
}
|
||||
}
|
||||
|
||||
// Reverse the buffer
|
||||
for (int i = idx - 1; i >= 0; i--) {
|
||||
montauk::putchar(buf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user