feat: revamp libraries to use ELF shared objects

This commit is contained in:
2026-04-08 18:35:32 +02:00
parent 28d0614511
commit 8a2a86696a
11 changed files with 638 additions and 233 deletions
+13 -22
View File
@@ -1,5 +1,5 @@
# Makefile for libmath shared library
# Builds libmath.lib at fixed address 0x400000 (relocated at load time)
# Builds libmath.lib as an ELF shared object (ET_DYN).
# Target architecture.
ARCH := x86_64
@@ -7,12 +7,12 @@ ARCH := x86_64
# Toolchain
TOOLCHAIN_PREFIX := $(shell cd ../../.. && pwd)/toolchain/local/bin/x86_64-elf-
CXX := $(TOOLCHAIN_PREFIX)g++
LD := $(TOOLCHAIN_PREFIX)ld
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.
# Build as PIC so the final ET_DYN image can be relocated by the kernel loader.
override CXXFLAGS := \
-std=gnu++20 \
-g -O2 -pipe \
@@ -22,7 +22,7 @@ override CXXFLAGS := \
-ffreestanding \
-fno-stack-protector \
-fno-stack-check \
-fPIE \
-fPIC \
-fno-rtti \
-fno-exceptions \
-ffunction-sections \
@@ -39,14 +39,13 @@ override CXXFLAGS := \
-isystem ../../include/libc \
-isystem ../../include/montauk
# Linker flags: freestanding static ELF at fixed address
# Note: NOT using --gc-sections so all functions are kept
# Linker flags for a bare-metal ELF shared object.
override LDFLAGS := \
-nostdlib \
-static \
-Wl,--build-id=none \
-Wl,-m,elf_x86_64 \
-T link.ld
-shared \
--build-id=none \
--hash-style=sysv \
-m elf_x86_64 \
-z max-page-size=0x1000
# Output
OUTDIR := ../../bin/os
@@ -55,21 +54,13 @@ LIBSRC := src/libmath.cpp
.PHONY: all clean
all: $(OUTDIR)/$(LIBNAME).lib $(OUTDIR)/$(LIBNAME).lib.sym
all: $(OUTDIR)/$(LIBNAME).lib
$(OUTDIR)/$(LIBNAME).lib: $(LIBSRC) Makefile
@mkdir -p $(OUTDIR)
rm -f $(OUTDIR)/$(LIBNAME).lib.sym
$(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: $@"
$(LD) $(LDFLAGS) src/libmath.o -o $@
clean:
rm -rf ../../bin/os/$(LIBNAME).lib ../../bin/os/$(LIBNAME).lib.sym src/libmath.o
Binary file not shown.