wip: shared library support

This commit is contained in:
2026-04-05 15:14:07 +02:00
parent d6c0470c62
commit 01c242d4de
20 changed files with 1026 additions and 4 deletions
+73
View File
@@ -0,0 +1,73 @@
# Makefile for libmath shared library
# Builds libmath.lib at fixed address 0x400000 (relocated at load time)
# Target architecture.
ARCH := x86_64
# Toolchain
TOOLCHAIN_PREFIX := $(shell cd ../../.. && pwd)/toolchain/local/bin/x86_64-elf-
CXX := $(TOOLCHAIN_PREFIX)g++
NM := $(TOOLCHAIN_PREFIX)nm
STRIP := $(TOOLCHAIN_PREFIX)strip
# Compiler flags: freestanding, no stdlib
override CXXFLAGS := \
-std=gnu++20 \
-g -O2 -pipe \
-Wall \
-Wextra \
-nostdinc \
-ffreestanding \
-fno-stack-protector \
-fno-stack-check \
-fno-PIC \
-fno-rtti \
-fno-exceptions \
-ffunction-sections \
-fdata-sections \
-m64 \
-march=x86-64 \
-mno-80387 \
-mno-mmx \
-mno-sse \
-mno-sse2 \
-mno-red-zone \
-mcmodel=small \
-I ../../include \
-isystem ../../include/libc \
-isystem ../../include/montauk
# Linker flags: freestanding static ELF at fixed address
# Note: NOT using --gc-sections so all functions are kept
override LDFLAGS := \
-nostdlib \
-static \
-Wl,--build-id=none \
-Wl,-m,elf_x86_64 \
-T link.ld
# Output
OUTDIR := ../../bin/os
LIBNAME := libmath
LIBSRC := src/libmath.cpp
.PHONY: all clean
all: $(OUTDIR)/$(LIBNAME).lib $(OUTDIR)/$(LIBNAME).lib.sym
$(OUTDIR)/$(LIBNAME).lib: $(LIBSRC)
@mkdir -p $(OUTDIR)
$(CXX) $(CXXFLAGS) -c $(LIBSRC) -o src/libmath.o
$(CXX) $(CXXFLAGS) $(LDFLAGS) src/libmath.o -o $@
# Generate symbol file from nm output
# Format: "symbol_name,offset_in_hex"
$(OUTDIR)/$(LIBNAME).lib.sym: $(OUTDIR)/$(LIBNAME).lib
@mkdir -p $(OUTDIR)
@echo "# Symbol table for $(LIBNAME)" > $@
@echo "# This file maps symbol names to offsets from library base" >> $@
@$(NM) $(OUTDIR)/$(LIBNAME).lib | grep ' T ' | awk '{printf "%s,0x%x\n", $$3, strtonum("0x" $$1) - 0x400000}' >> $@
@echo "Generated symbol file: $@"
clean:
rm -rf ../../bin/os/$(LIBNAME).lib ../../bin/os/$(LIBNAME).lib.sym src/libmath.o
+10
View File
@@ -0,0 +1,10 @@
SECTIONS
{
. = 0x400000;
.text : {
*(.text)
}
.eh_frame : {
*(.eh_frame)
}
}
+55
View File
@@ -0,0 +1,55 @@
/*
* libmath.cpp
* Sample shared math library for MontaukOS
* Copyright (c) 2026 Daniel Hammer
*/
extern "C" {
// Basic math functions
int math_add(int a, int b) {
return a + b;
}
int math_sub(int a, int b) {
return a - b;
}
int math_mul(int a, int b) {
return a * b;
}
int math_div(int a, int b) {
if (b == 0) return 0;
return a / b;
}
int math_mod(int a, int b) {
if (b == 0) return 0;
return a % b;
}
// Comparison
int math_max(int a, int b) {
return (a > b) ? a : b;
}
int math_min(int a, int b) {
return (a < b) ? a : b;
}
int math_abs(int a) {
return (a < 0) ? -a : a;
}
// Utility
int math_pow(int base, int exp) {
int result = 1;
for (int i = 0; i < exp; i++) {
result *= base;
}
return result;
}
} // extern "C"
Binary file not shown.