C userspace programs

This commit is contained in:
2025-09-04 23:20:30 +02:00
parent afa4d383e0
commit 90266f044b
51 changed files with 259 additions and 174 deletions

View File

@ -1,6 +1,5 @@
ARCH ?= x86_64
CFLAGS := -ffreestanding -Wall -Wextra -g -fcommon -nostdinc
CFLAGS := -ffreestanding -Wall -Wextra -g -fcommon -nostdinc \
-isystem $(ROOT)/std/include -isystem $(ROOT)/ulib
CURRENT_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))

View File

@ -1,17 +1,17 @@
ENTRY(_start)
SECTIONS {
. = 0x0000000000000000;
.rodata ALIGN(4K):
{
*(.rodata .rodata.*)
}
/* . = 0x0000000000000000; */
.text ALIGN(4K):
{
*(.text .text.*)
}
.rodata (READONLY): ALIGN(4K)
{
*(.rodata .rodata.*)
}
.data ALIGN(4K):
{
@ -20,6 +20,8 @@ SECTIONS {
.bss ALIGN(4K):
{
_bss_start = .;
*(.bss .bss.*)
_bss_end = .;
}
}

View File

@ -1,18 +1,5 @@
CC := x86_64-elf-gcc
LD := x86_64-elf-ld
CFLAGS += -m64 \
-fPIE \
-mno-80387 \
-mno-mmx \
-mno-sse \
-nostartfiles \
-nostdlib \
-mno-sse2 \
-mno-red-zone \
-fno-stack-protector \
-fno-stack-check \
-Os \
include $(ROOT)/mk/user/x86_64.mk
include $(ROOT)/mk/arch/toolchain-x86_64.mk
LDFLAGS += -m elf_x86_64 \
-pie \

View File

@ -14,9 +14,9 @@ OBJ := $(call GET_OBJ, $(SRCFILES))
all: $(TARGET)
hello: $(OBJ)
$(TARGET): $(OBJ)
$(LD) $^ $(LDFLAGS) -o $@
echo $(realpath hello) >> $(FILES)
echo $$(realpath $(TARGET)) >> $(FILES)
clean:
rm -f $(OBJ) $(TARGET)

View File

@ -11,8 +11,7 @@ _start:
movq $1, %rax
lea STRING(%rip), %rdi
lea STRING_LEN(%rip), %rsi
syscall
int $0x80
movq $2, %rax
syscall
int $0x80

2
user/init/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.o
init

24
user/init/Makefile Normal file
View File

@ -0,0 +1,24 @@
include $(ROOT)/mk/grabsrc.mk
include ../Makefile.inc
.PHONY: all clean
TARGET := init
LDFLAGS += -L$(ROOT)/ulib -l:libulib.a
SRCFILES := $(call GRABSRC, .)
CFILES := $(call GET_CFILES, $(SRCFILES))
OBJ := $(call GET_OBJ, $(SRCFILES))
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
all: $(TARGET)
$(TARGET): $(OBJ)
$(LD) $^ $(LDFLAGS) -o $@
echo $$(realpath $(TARGET)) >> $(FILES)
clean:
rm -f $(OBJ) $(TARGET)

6
user/init/main.c Normal file
View File

@ -0,0 +1,6 @@
#include <system/system.h>
void main(void) {
sys_debugprint("Hello world from userspace in C");
}