From a45f1df00d58b17295c70ac982cbd0efbc228b06 Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Wed, 15 Jul 2026 07:52:57 +0000 Subject: [PATCH] urt: Add user run-time library groundwork Signed-off-by: Chloe M. --- build.sh | 4 +++ service/dev/build.env | 2 ++ service/dev/bvars.env | 2 ++ service/mk/user.mk | 51 +++++++++++++++++++++++++++++++ service/urt/Makefile | 16 ++++++++++ service/urt/arch/amd64/Makefile | 18 +++++++++++ service/urt/arch/amd64/crt/crti.S | 15 +++++++++ service/urt/dev/link.ld | 36 ++++++++++++++++++++++ 8 files changed, 144 insertions(+) create mode 100644 service/mk/user.mk create mode 100644 service/urt/Makefile create mode 100644 service/urt/arch/amd64/Makefile create mode 100644 service/urt/arch/amd64/crt/crti.S create mode 100644 service/urt/dev/link.ld diff --git a/build.sh b/build.sh index 67e4e03..ee0705f 100755 --- a/build.sh +++ b/build.sh @@ -68,6 +68,10 @@ build() { service/host \ "Building host utils..." + make_build \ + service/urt/ \ + "Building user runtime lib..." + make_build \ service/ptos/ \ "Building kernel..." diff --git a/service/dev/build.env b/service/dev/build.env index 3defa49..d99a5fb 100644 --- a/service/dev/build.env +++ b/service/dev/build.env @@ -13,6 +13,8 @@ export PT_PROJECT_ROOT=$PT_PROJECT_ROOT export PT_TARGET_ARCH=$PT_TARGET_ARCH export PT_BOOT_PROTO=$PT_BOOT_PROTO +export PT_URT_LIB=$PT_URT_LIB +export PT_URT_CRT=$PT_URT_CRT if [ "$PT_BUILD_SOURCED" != "1" ] then diff --git a/service/dev/bvars.env b/service/dev/bvars.env index af2f356..c569a79 100644 --- a/service/dev/bvars.env +++ b/service/dev/bvars.env @@ -9,3 +9,5 @@ PT_PROJECT_ROOT=$(pwd) PT_TARGET_ARCH=amd64 PT_BOOT_PROTO=limine +PT_URT_LIB=$PT_PROJECT_ROOT/service/urt/ +PT_URT_CRT=$PT_PROJECT_ROOT/service/urt/arch/$PT_TARGET_ARCH diff --git a/service/mk/user.mk b/service/mk/user.mk new file mode 100644 index 0000000..7f747ba --- /dev/null +++ b/service/mk/user.mk @@ -0,0 +1,51 @@ +# +# Copyright (c) 2026, Chloe M. +# Provided under the BSD-3 clause +# +# Description: User build flags, etc +# Author: Chloe M. +# + +.SILENT: +PROMPT := printf "%s\t\t%s\n" + +ifeq ($(PT_TARGET_ARCH), amd64) + CC_TARGET = x86_64-unknown-elf +else + CC_TARGET = +endif + +CC = \ + clang +ld = \ + ld + +SYS_CFLAGS = \ + -nostdlib \ + -nostdinc \ + -ffreestanding \ + -fexceptions \ + -target $(CC_TARGET)\ + -Wno-attributes \ + -Wno-multichar \ + -fno-stack-protector\ + -I$(PT_PROJECT_ROOT)/sdk/head + +ifeq ($(PT_TARGET_ARCH),amd64) + SYS_CFLAGS += \ + -mno-sse \ + -mno-sse2 \ + -mno-sse3 \ + -mno-avx \ + -mno-avx2 \ + -mno-80387 \ + -mno-3dnow \ + -mno-mmx +endif + +PASSDOWN_ARGS = \ + ARCH=$(PT_TARGET_ARCH) \ + SYS_CC=$(CC) \ + SYS_LD=$(LD) \ + SYS_CFLAGS="$(SYS_CFLAGS)" \ + USER_LDS=$(PT_PROJECT_ROOT)/service/urt/dev/link.ld diff --git a/service/urt/Makefile b/service/urt/Makefile new file mode 100644 index 0000000..522fdda --- /dev/null +++ b/service/urt/Makefile @@ -0,0 +1,16 @@ +# +# Copyright (c) 2026, Chloe M. +# Provided under the BSD-3 clause +# +# Description: User run-time build script +# Author: Chloe M. +# + +include ../mk/user.mk + +.PHONY: urt +urt: machine + +.PHONY: machine +machine: + cd arch/$(PT_TARGET_ARCH)/; $(MAKE) $(PASSDOWN_ARGS) diff --git a/service/urt/arch/amd64/Makefile b/service/urt/arch/amd64/Makefile new file mode 100644 index 0000000..55ab913 --- /dev/null +++ b/service/urt/arch/amd64/Makefile @@ -0,0 +1,18 @@ +# +# Copyright (c) 2026, Chloe M. +# Provided under the BSD-3 clause +# +# Description: User run-time machdep build script +# Author: Chloe M. +# + +ASMFILES = $(shell find . -name "*.S") +ASMOFILES = $(ASMFILES:.S=.S.o) + +CFLAGS = $(SYS_CFLAGS) + +.PHONY: all +all: $(ASMOFILES) + +%.S.o: %.S + $(SYS_CC) -c $< $(CFLAGS) -o $@ diff --git a/service/urt/arch/amd64/crt/crti.S b/service/urt/arch/amd64/crt/crti.S new file mode 100644 index 0000000..ef78700 --- /dev/null +++ b/service/urt/arch/amd64/crt/crti.S @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: C-runtime entry + * Author: Chloe M. + */ + + .text + .globl _start +_start: + xor %rbp, %rbp /* Terminate callstack */ + call Main /* Enter program main */ +1: pause + jmp 1b diff --git a/service/urt/dev/link.ld b/service/urt/dev/link.ld new file mode 100644 index 0000000..2597c3e --- /dev/null +++ b/service/urt/dev/link.ld @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: User linker script + * Author: Chloe M. + */ + +ENTRY(_start) + +SECTIONS { + /* User programs live here */ + . = 0x9000; + + .text : { + *(.text .text.*) + } + + .data : { + *(.data .data.*) + } + + .bss : { + *(.bss .bss.*) + *(COMMON) + } + + .rodata : { + *(.rodata .rodata.*) + } + + /DISCARD/ : { + *(.eh_frame .eh_frame.*) + *(.note .note.*) + } +}