From a4dea4148157a94a9143dbbd92fdd88b4358676c Mon Sep 17 00:00:00 2001 From: Alexey Gavrilov Date: Sat, 20 Dec 2025 19:39:05 +0700 Subject: Global variables initialization implementation User implementation of C runtime library objects (`crti.s` and `crtn.s`) for initialization (termination functionality are ignored). Changing files order passed to the linker. Rough order is: boot.s crti.o crtbegin.o other-link-objects.o crtend.o crtn.o Signed-off-by: Alexey Gavrilov --- kernel/boot.s | 15 ++++++++++----- libgcc/x86/crti.s | 5 +++++ libgcc/x86/crtn.s | 3 +++ makefile | 10 +++++++++- 4 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 libgcc/x86/crti.s create mode 100644 libgcc/x86/crtn.s diff --git a/kernel/boot.s b/kernel/boot.s index 281e1ca..11175af 100644 --- a/kernel/boot.s +++ b/kernel/boot.s @@ -1,11 +1,16 @@ section .text -extern main -extern _estack -global _start:function (_start.end - _start) +extern main, _estack, _init +global _start:function _start: - mov esp, _estack + ; Stack setup + mov eax, _estack + ; Align stack to odd address + and eax, 0xFFFFFFFE + mov esp, eax + + call _init call main cli .hang: hlt jmp .hang -.end: +; .end: diff --git a/libgcc/x86/crti.s b/libgcc/x86/crti.s new file mode 100644 index 0000000..57cb8c1 --- /dev/null +++ b/libgcc/x86/crti.s @@ -0,0 +1,5 @@ +section .init +global _init:function +_init: + push ebp + mov ebp, esp diff --git a/libgcc/x86/crtn.s b/libgcc/x86/crtn.s new file mode 100644 index 0000000..537fa65 --- /dev/null +++ b/libgcc/x86/crtn.s @@ -0,0 +1,3 @@ +section .init + pop ebp + ret diff --git a/makefile b/makefile index 051eb0e..79152f8 100644 --- a/makefile +++ b/makefile @@ -25,13 +25,21 @@ endif OS_BINARY = $(BDIR)/kernel.bin BDIR = ./build +BOOTOBJ = $(filter %boot.o, $(OBJ_FILES)) +CRTIOBJ = $(filter %crti.o, $(OBJ_FILES)) +CRTNOBJ = $(filter %crtn.o, $(OBJ_FILES)) +OBJ_REORDERING = $(filter-out $(BOOTOBJ), $(filter-out $(CRTIOBJ), $(filter-out $(CRTNOBJ), $(OBJ_FILES)))) + + .PHONY: install $(SOURCE_FILES) build: startbuild $(SOURCE_FILES) + @echo "Bootfile: " $(BOOTOBJ) $(CRTIOBJ) $(CRTNOBJ) + @echo $(OBJ_FILES) echo $(AFLAGS) echo $(CFLAGS) @echo "Link object files..." - $(LD) -o $(OS_BINARY) -L$(ARCHDIR) -T$(LDFILE) $(OBJ_FILES) $(LDFLAGS) + $(LD) -o $(OS_BINARY) -L$(ARCHDIR) -T$(LDFILE) $(BOOTOBJ) $(CRTIOBJ) $(LIBS_PATH)crtbegin.o $(OBJ_REORDERING) $(LIBS_PATH)crtend.o $(CRTNOBJ) $(LDFLAGS) @echo "Project was built" $(CFILES): -- cgit v1.2.3