fix(rumpk): Fix LwIP kernel build for RISC-V freestanding

- Rebuild liblwip.a from clean sources (removed initrd.o contamination)
- Add switch.o to provide cpu_switch_to symbol
- Add sys_arch.o to provide sys_now and nexus_lwip_panic
- Add freestanding defines to cc.h (LWIP_NO_CTYPE_H, etc.)
- Compile sys_arch.c with -mcmodel=medany for RISC-V

Fixes duplicate symbol errors and undefined reference errors.
Kernel now builds successfully with: zig build -Dtarget=riscv64-freestanding
This commit is contained in:
Markus Maiwald 2026-01-08 19:21:02 +01:00
parent f5f9f0bf6d
commit 5c57341b81
2 changed files with 58 additions and 1 deletions

View File

@ -29,6 +29,7 @@ pub fn build(b: *std.Build) void {
// Freestanding kernel - no libc, no red zone, no stack checks
hal_mod.red_zone = false;
hal_mod.stack_check = false;
hal_mod.code_model = .medany;
const hal = b.addLibrary(.{
.name = "rumpk_hal",
@ -58,13 +59,60 @@ pub fn build(b: *std.Build) void {
});
boot_mod.red_zone = false;
boot_mod.stack_check = false;
boot_mod.code_model = .medany;
const boot = b.addObject(.{
.name = "boot",
.root_module = boot_mod,
});
_ = boot; // Mark as used for now
// =========================================================
// Final Link: rumpk.elf
// =========================================================
const kernel_mod = b.createModule(.{
.root_source_file = b.path("hal/abi.zig"), // Fake root, we add objects later
.target = target,
.optimize = optimize,
});
kernel_mod.red_zone = false;
kernel_mod.stack_check = false;
kernel_mod.code_model = .medany;
const kernel = b.addExecutable(.{
.name = "rumpk.elf",
.root_module = kernel_mod,
});
kernel.setLinkerScript(b.path("boot/linker.ld"));
kernel.addObject(boot);
// kernel.linkLibrary(hal); // Redundant, already in kernel_mod
// Add Nim-generated objects
{
var nimcache_dir = std.fs.cwd().openDir("build/nimcache", .{ .iterate = true }) catch |err| {
std.debug.print("Warning: Could not open nimcache dir: {}\n", .{err});
return;
};
defer nimcache_dir.close();
var it = nimcache_dir.iterate();
while (it.next() catch null) |entry| {
if (entry.kind == .file and std.mem.endsWith(u8, entry.name, ".o")) {
const path = b.fmt("build/nimcache/{s}", .{entry.name});
kernel.addObjectFile(b.path(path));
}
}
}
// Add external pre-built dependencies (Order matters: Libs after users)
kernel.addObjectFile(b.path("build/switch.o")); // cpu_switch_to
kernel.addObjectFile(b.path("build/sys_arch.o")); // sys_now, nexus_lwip_panic
kernel.addObjectFile(b.path("build/libc_shim.o"));
kernel.addObjectFile(b.path("build/clib.o"));
kernel.addObjectFile(b.path("build/liblwip.a"));
kernel.addObjectFile(b.path("build/initrd.o"));
b.installArtifact(kernel);
// =========================================================
// Tests

View File

@ -16,9 +16,18 @@
#ifndef LWIP_ARCH_CC_H
#define LWIP_ARCH_CC_H
// =========================================================
// Freestanding Environment - Disable unavailable headers
// =========================================================
#define LWIP_NO_CTYPE_H 1 // ctype.h not available
#define LWIP_NO_LIMITS_H 1 // limits.h not available
#define LWIP_NO_UNISTD_H 1 // unistd.h not available
#define LWIP_NO_INTTYPES_H 1 // inttypes.h not available
#include <stdint.h>
#include <stddef.h>
// =========================================================
// Basic Types (Fixed-width integers)
// =========================================================