diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-04-22 18:11:52 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-04-22 18:11:52 +0800 |
| commit | 12cbc1c0cac64f99b3784554eb540b20f33e9f23 (patch) | |
| tree | 0875b9c69a3d3f32c43cabc3d91051c3e461c8f0 /na | |
| parent | 3b7d014ad6d5f25de705447e832d7b13b884604e (diff) | |
| download | lume-12cbc1c0cac64f99b3784554eb540b20f33e9f23.tar.gz lume-12cbc1c0cac64f99b3784554eb540b20f33e9f23.tar.xz | |
feat: add linker script and common macros for RISC-V architecture
Diffstat (limited to 'na')
| -rw-r--r-- | na/Makefile | 9 | ||||
| -rw-r--r-- | na/arch/riscv/kernel/lumeos.lds.S | 81 | ||||
| -rw-r--r-- | na/include/lume/lumeos.lds.h | 74 |
3 files changed, 163 insertions, 1 deletions
diff --git a/na/Makefile b/na/Makefile index 6b5e84c..cc51a65 100644 --- a/na/Makefile +++ b/na/Makefile @@ -42,7 +42,8 @@ ARCH := riscv BUILD := build TARGET := $(BUILD)/kernel.elf IMAGE := $(BUILD)/kernel.bin -LINKER_SCRIPT := linker.ld +LDS_SRC := arch/$(ARCH)/kernel/lumeos.lds.S +LINKER_SCRIPT := $(BUILD)/lumeos.lds # Common flags (base, before module includes) WARNINGS := -Wall -Wextra -Wno-unused-parameter @@ -103,6 +104,12 @@ DEPS := $(OBJS:.o=.d) all: $(IMAGE) +# Preprocess linker script template (.lds.S → .lds) +$(LINKER_SCRIPT): $(LDS_SRC) include/lume/lumeos.lds.h + @echo " LDS $@" + @mkdir -p $(dir $@) + @$(CC) -E -P -x c $(INCLUDES) $< -o $@ + # Link kernel ELF $(TARGET): $(OBJS) $(LINKER_SCRIPT) @echo " LD $@" diff --git a/na/arch/riscv/kernel/lumeos.lds.S b/na/arch/riscv/kernel/lumeos.lds.S new file mode 100644 index 0000000..f47fef2 --- /dev/null +++ b/na/arch/riscv/kernel/lumeos.lds.S @@ -0,0 +1,81 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Copyright (C) 2026 Richard Qin */ + +/* + * RISC-V Kernel Linker Script Template + * + * This is a C-preprocessed linker script (.lds.S). The build system + * runs it through CPP to produce the final vmlinux.lds which is + * passed to the linker. + * + * Architecture-specific definitions: + * - OUTPUT_ARCH, ENTRY + * - PAGE_OFFSET, BASE_ADDRESS, PHYS_BASE + * - .head.text placement + * - GP anchor (__global_pointer$ for RISC-V medany) + * - early_pgdir and boot stacks in .bss + * + * Common section macros come from include/lume/vmlinux.lds.h + * + * Reference: docs/specs/linker_and_entry.md + */ + +#include <lume/lumeos.lds.h> + +/* ===== RISC-V Architecture Constants ===== */ + +OUTPUT_ARCH(riscv) + +PAGE_OFFSET = 0xFFFFFFC000000000; +BASE_ADDRESS = 0xFFFFFFC080200000; +PHYS_BASE = 0x80200000; + +ENTRY(_start) + +/* ===== RISC-V: GP anchor in .data (medany model) ===== */ +#define ARCH_DATA_EXTRA \ + . = ALIGN(16); \ + PROVIDE(__global_pointer$ = . + 0x800); \ + *(.sdata .sdata.*) + +/* ===== RISC-V: early page table + per-CPU boot stacks in .bss ===== */ +#define ARCH_BSS_EXTRA \ + /* Early page table: 4KB aligned, used by entry.S before VMM */ \ + . = ALIGN(4096); \ + early_pgdir = .; \ + . += 4096; \ + /* Per-CPU boot stacks: 8KB per CPU, max 8 CPUs */ \ + . = ALIGN(4096); \ + boot_stack = .; \ + . += 8192 * 8; \ + boot_stack_top = .; + +/* ===== Layout ===== */ + +SECTIONS { + . = BASE_ADDRESS; + + /* .head.text: Image Header (must be at byte 0 of the kernel image) */ + .head.text : AT(PHYS_BASE) { + _start = .; + KEEP(*(.head.text)) + } + + TEXT_SECTION + + . = ALIGN(4096); + + RODATA_SECTION + + . = ALIGN(4096); + + DATA_SECTION + + . = ALIGN(4096); + + BSS_SECTION + + KERNEL_END + + DISCARDS +} diff --git a/na/include/lume/lumeos.lds.h b/na/include/lume/lumeos.lds.h new file mode 100644 index 0000000..b4a8f42 --- /dev/null +++ b/na/include/lume/lumeos.lds.h @@ -0,0 +1,74 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Copyright (C) 2026 Richard Qin */ + +/* + * Common Linker Script Macros (Machine-Independent) + * + * Shared section definitions that every architecture must emit. + * Architecture-specific lumeos.lds.S files #include this header + * and use these macros to avoid duplicating common boilerplate. + * + * Modeled after Linux include/asm-generic/vmlinux.lds.h + * Reference: docs/specs/linker_and_entry.md + */ + +#ifndef _LUME_LUMEOS_LDS_H +#define _LUME_LUMEOS_LDS_H + +/* ===== Text Section: executable code ===== */ +#define TEXT_SECTION \ + .text : AT(ADDR(.text) - PAGE_OFFSET) { \ + _stext = .; \ + *(.text .text.*) \ + _etext = .; \ + } = 0 + +/* ===== Read-Only Data + C++ init_array ===== */ +#define RODATA_SECTION \ + .rodata : AT(ADDR(.rodata) - PAGE_OFFSET) { \ + _srodata = .; \ + *(.rodata .rodata.*) \ + INIT_ARRAY \ + _erodata = .; \ + } + +/* ===== C++ Global Constructor Table ===== */ +#define INIT_ARRAY \ + . = ALIGN(8); \ + __init_array_start = .; \ + KEEP(*(.init_array .init_array.*)) \ + __init_array_end = .; + +/* ===== Initialized Read-Write Data ===== */ +#define DATA_SECTION \ + .data : AT(ADDR(.data) - PAGE_OFFSET) { \ + _sdata = .; \ + *(.data .data.*) \ + ARCH_DATA_EXTRA \ + _edata = .; \ + } + +/* ===== BSS: Uninitialized Data ===== */ +#define BSS_SECTION \ + .bss : AT(ADDR(.bss) - PAGE_OFFSET) { \ + _sbss = .; \ + *(.bss .bss.*) \ + *(.sbss .sbss.*) \ + ARCH_BSS_EXTRA \ + _ebss = .; \ + } + +/* ===== Discard Unwanted Sections ===== */ +#define DISCARDS \ + /DISCARD/ : { \ + *(.eh_frame .eh_frame_hdr) \ + *(.note .note.*) \ + *(.comment) \ + } + +/* ===== Kernel Image Boundary ===== */ +#define KERNEL_END \ + . = ALIGN(4096); \ + _kernel_end = .; + +#endif /* _LUME_LUMEOS_LDS_H */ |
