From 02a931d1a99b7d5fab90a3d02c474075c6e90fdf Mon Sep 17 00:00:00 2001 From: Nathan Lee Date: Wed, 12 Aug 2026 17:56:28 -0500 Subject: move linker into arch-specific directory --- BuildConfig | 1 + Makefile | 7 +++-- kernel/riscv/linker.ld | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ linker.ld | 75 -------------------------------------------------- 4 files changed, 79 insertions(+), 78 deletions(-) create mode 100644 kernel/riscv/linker.ld delete mode 100644 linker.ld diff --git a/BuildConfig b/BuildConfig index 000d4ee..c985c6c 100644 --- a/BuildConfig +++ b/BuildConfig @@ -1,4 +1,5 @@ kernel: kernel/riscv/*.c kernel/riscv/*.S kernel/common/*.c +linker_script: kernel/riscv/linker.ld kernel_target: kernel/riscv modules: console console: console/*.c diff --git a/Makefile b/Makefile index 03b85a2..12dd521 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ INCLUDE_FLAG = -Iinclude -Ikernel OPT_FLAG = -O2 -g3 WARN_FLAG = -Wall -Wextra -Werror -Wundef -KERNEL_LD_FLAG = -T linker.ld -nostdlib -m elf32lriscv --gc-sections +KERNEL_LD_FLAG = -T $(KERNEL_LD) -nostdlib -m elf32lriscv --gc-sections MODULE_LD_FLAG = -nostdlib -shared -m elf32lriscv --gc-sections KERNEL_OBJCOPY_FLAG = -O binary \ @@ -85,6 +85,7 @@ endef $(foreach module,$(MODULES),$(eval $(call module_template,$(module)))) KERNEL_SRCS = $(wildcard $(shell grep '^kernel:' BuildConfig | sed 's/^kernel://' | awk '{$$1=$$1; print}')) +KERNEL_LD := $(shell grep '^linker_script:' BuildConfig | sed 's/^linker_script://' | awk '{$$1=$$1; print}') KERNEL_OBJS = $(KERNEL_SRCS:.c=.o) KERNEL_OBJS := $(KERNEL_OBJS:.S=.o) KERNEL_BIN = kernel/kernel.bin @@ -112,7 +113,7 @@ $(MODULES): $(MODULE_OBJECTS) @echo " AS $<" $(CC) $(KERNEL_CFLAGS) -c $< -o $@ -$(KERNEL_ELF): $(KERNEL_OBJS) linker.ld | $(KERNEL_OUTDIR)/ +$(KERNEL_ELF): $(KERNEL_OBJS) $(KERNEL_LD) | $(KERNEL_OUTDIR)/ @echo " LD $@" $(LD) $(KERNEL_LD_FLAG) $(KERNEL_OBJS) -o $@ @@ -121,5 +122,5 @@ $(KERNEL_BIN): $(KERNEL_ELF) | $(KERNEL_OUTDIR)/ $(OBJCOPY) $(KERNEL_OBJCOPY_FLAG) $(KERNEL_ELF) $(KERNEL_BIN) clean: - @echo " CLEAN" + @echo " CLEAN $(GENERATED)" $(RM) $(GENERATED) diff --git a/kernel/riscv/linker.ld b/kernel/riscv/linker.ld new file mode 100644 index 0000000..5bf6586 --- /dev/null +++ b/kernel/riscv/linker.ld @@ -0,0 +1,74 @@ +OUTPUT_ARCH(riscv) +ENTRY(_start) + +MEMORY +{ + ROM (rx) : ORIGIN = 0x80000000, LENGTH = 1M + RAM (rwx) : ORIGIN = 0x81000000, LENGTH = 1M + LOGRAM (rw) : ORIGIN = 0x82000000, LENGTH = 1M +} + +SECTIONS +{ + .text : { + KEEP(*(.text._start)) + KEEP(*(.text.start)) + *(.text) + *(.text.*) + } > ROM + + .rodata : { + *(.rodata) + *(.rodata.*) + } > ROM + + .init_array : { + __init_array_start = .; + KEEP(*(.init_array)) + KEEP(*(.init_array.*)) + __init_array_end = .; + } > ROM + + .data : { + __data_start = .; + *(.data) + *(.data.*) + __data_end = .; + } > RAM AT> ROM + __data_load = LOADADDR(.data); + + .bss : { + __bss_start = .; + *(.bss) + *(.bss.*) + *(COMMON) + __bss_end = .; + } > RAM + + .stack (NOLOAD) : { + . = ALIGN(16); + __stack_bottom = .; + . += 8K; + __stack_top = .; + } > RAM + + .ringbuf (NOLOAD) : ALIGN(64) + { + __ringbuf_start = .; + KEEP(*(.ringbuf)) + . = ALIGN(64); + __ringbuf_end = .; + } > LOGRAM + + /DISCARD/ : { + *(.eh_frame) + *(.eh_frame.*) + *(.eh_frame_hdr) + *(.comment) + *(.note.*) + *(.debug_*) + *(.stab*) + *(.llvm_*) + *(.rela.*) + } +} diff --git a/linker.ld b/linker.ld deleted file mode 100644 index 4fec2bf..0000000 --- a/linker.ld +++ /dev/null @@ -1,75 +0,0 @@ -OUTPUT_ARCH(riscv) -ENTRY(_start) - -MEMORY -{ - ROM (rx) : ORIGIN = 0x80000000, LENGTH = 1M - RAM (rwx) : ORIGIN = 0x81000000, LENGTH = 1M - LOGRAM (rw) : ORIGIN = 0x82000000, LENGTH = 1M -} - -SECTIONS -{ - .text : { - KEEP(*(.text._start)) - KEEP(*(.text.start)) - *(.text) - *(.text.*) - } > ROM - - .rodata : { - *(.rodata) - *(.rodata.*) - } > ROM - - .init_array : { - __init_array_start = .; - KEEP(*(.init_array)) - KEEP(*(.init_array.*)) - __init_array_end = .; - } > ROM - - .data : { - __data_start = .; - *(.data) - *(.data.*) - __data_end = .; - } > RAM AT> ROM - __data_load = LOADADDR(.data); - - .bss : { - __bss_start = .; - *(.bss) - *(.bss.*) - *(COMMON) - __bss_end = .; - } > RAM - - .stack (NOLOAD) : { - . = ALIGN(16); - __stack_bottom = .; - . += 8K; - __stack_top = .; - } > RAM - - .ringbuf (NOLOAD) : ALIGN(64) - { - __ringbuf_start = .; - KEEP(*(.ringbuf)) - . = ALIGN(64); - __ringbuf_end = .; - } > LOGRAM - - /DISCARD/ : { - *(.eh_frame) - *(.eh_frame.*) - *(.eh_frame_hdr) - *(.comment) - *(.note.*) - *(.debug_*) - *(.stab*) - *(.llvm_*) - *(.rela.*) - } - _end = .; -} -- cgit v1.2.3