feat: use PIC code model for shared libraries, fix shared library bugs

This commit is contained in:
2026-04-08 17:07:55 +02:00
parent d87c14da25
commit 28d0614511
10 changed files with 85 additions and 25 deletions
+3 -1
View File
@@ -11,6 +11,8 @@ NM := $(TOOLCHAIN_PREFIX)nm
STRIP := $(TOOLCHAIN_PREFIX)strip
# Compiler flags: freestanding, no stdlib
# Build as PIE so library-internal data references remain valid after the
# kernel maps the image into a per-process library slot.
override CXXFLAGS := \
-std=gnu++20 \
-g -O2 -pipe \
@@ -20,7 +22,7 @@ override CXXFLAGS := \
-ffreestanding \
-fno-stack-protector \
-fno-stack-check \
-fno-PIC \
-fPIE \
-fno-rtti \
-fno-exceptions \
-ffunction-sections \
+38 -10
View File
@@ -8,18 +8,47 @@
#include <montauk/syscall.h>
#include <gui/truetype.hpp>
// Exported demo function - creates a window and renders until closed
// Heap allocation test - allocates, writes, reads, frees
// Returns 0 on success, -1 on failure
extern "C" int hello_malloc_test() {
// Test 1: Allocate 256 bytes
void* buf = montauk::malloc(256);
if (!buf) {
montauk::print(" [libhello] malloc(256) failed\n");
return -1;
}
// Test 2: Write a pattern and verify it
uint8_t* bytes = (uint8_t*)buf;
for (int i = 0; i < 256; i++) {
bytes[i] = (uint8_t)(i & 0xFF);
}
for (int i = 0; i < 256; i++) {
if (bytes[i] != (uint8_t)(i & 0xFF)) {
montauk::print(" [libhello] read verification failed\n");
montauk::mfree(buf);
return -1;
}
}
montauk::mfree(buf);
return 0;
}
extern "C" int hello_run() {
// Create window
static constexpr int kInitialWidth = 320;
static constexpr int kInitialHeight = 200;
static constexpr uint32_t kBackgroundColor = 0xFFFFFFFF;
Montauk::WinCreateResult wres;
if (montauk::win_create("libhello Demo", 320, 200, &wres) < 0 || wres.id < 0) {
if (montauk::win_create("libhello Demo", kInitialWidth, kInitialHeight, &wres) < 0 || wres.id < 0) {
return -1;
}
int winId = wres.id;
uint32_t* pixels = (uint32_t*)(uintptr_t)wres.pixelVa;
int width = 200;
int height = 320;
int width = kInitialWidth;
int height = kInitialHeight;
gui::TrueTypeFont* font = (gui::TrueTypeFont*)montauk::malloc(sizeof(gui::TrueTypeFont));
if (!font) {
@@ -36,16 +65,15 @@ extern "C" int hello_run() {
// Simple render - fill with a color pattern
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// Dark blue background
pixels[y * width + x] = 0xFF1a1a2e;
pixels[y * width + x] = kBackgroundColor;
}
}
// Render text using TrueType
gui::Color text_color;
text_color.r = 0xF5;
text_color.g = 0xF5;
text_color.b = 0xF5;
text_color.r = 0x11;
text_color.g = 0x11;
text_color.b = 0x11;
text_color.a = 255;
font->draw_to_buffer(pixels, width, height, 20, 20, "Hello from libhello!", text_color, 16);
@@ -76,7 +104,7 @@ extern "C" int hello_run() {
// Re-render on resize
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixels[y * width + x] = 0xFF1a1a2e;
pixels[y * width + x] = kBackgroundColor;
}
}
font->draw_to_buffer(pixels, width, height, 20, 20, "Hello from libhello!", text_color, 16);
Binary file not shown.
Binary file not shown.
+4 -2
View File
@@ -11,6 +11,8 @@ NM := $(TOOLCHAIN_PREFIX)nm
STRIP := $(TOOLCHAIN_PREFIX)strip
# Compiler flags: freestanding, no stdlib
# Build as PIE so library code remains valid when loaded into a slot-specific
# base address by the kernel loader.
override CXXFLAGS := \
-std=gnu++20 \
-g -O2 -pipe \
@@ -20,7 +22,7 @@ override CXXFLAGS := \
-ffreestanding \
-fno-stack-protector \
-fno-stack-check \
-fno-PIC \
-fPIE \
-fno-rtti \
-fno-exceptions \
-ffunction-sections \
@@ -55,7 +57,7 @@ LIBSRC := src/libmath.cpp
all: $(OUTDIR)/$(LIBNAME).lib $(OUTDIR)/$(LIBNAME).lib.sym
$(OUTDIR)/$(LIBNAME).lib: $(LIBSRC)
$(OUTDIR)/$(LIBNAME).lib: $(LIBSRC) Makefile
@mkdir -p $(OUTDIR)
$(CXX) $(CXXFLAGS) -c $(LIBSRC) -o src/libmath.o
$(CXX) $(CXXFLAGS) $(LDFLAGS) src/libmath.o -o $@
Binary file not shown.