mirror of
https://github.com/EnderIce2/Fennix.git
synced 2025-07-02 10:59:15 +00:00
Merge remote-tracking branch 'Userspace/master'
This commit is contained in:
41
Userspace/libc/runtime/Makefile
Normal file
41
Userspace/libc/runtime/Makefile
Normal file
@ -0,0 +1,41 @@
|
||||
# Config file
|
||||
include ../../../Makefile.conf
|
||||
|
||||
CC = ../../../$(COMPILER_PATH)/$(COMPILER_ARCH)gcc
|
||||
AS = ../../../$(COMPILER_PATH)/$(COMPILER_ARCH)as
|
||||
NASM = /usr/bin/nasm
|
||||
|
||||
C_SOURCES = $(shell find ./ -type f -name '*.c')
|
||||
S_SOURCES = $(shell find ./ -type f -name '*.S')
|
||||
ASM_SOURCES = $(shell find ./ -type f -name '*.asm')
|
||||
OBJ = ${C_SOURCES:.c=.o} ${ASM_SOURCES:.asm=.o} ${S_SOURCES:.S=.o}
|
||||
|
||||
ifeq ($(OSARCH), amd64)
|
||||
ASM_ARCH := elf64
|
||||
else ifeq ($(OSARCH), i386)
|
||||
ASM_ARCH := elf32
|
||||
endif
|
||||
|
||||
CRTBEGIN_PATH = $(shell $(CC) -print-file-name=crtbegin.o)
|
||||
CRTEND_PATH = $(shell $(CC) -print-file-name=crtend.o)
|
||||
CRTI_PATH = $(shell $(CC) -print-file-name=crti.o)
|
||||
CRTN_PATH = $(shell $(CC) -print-file-name=crtn.o)
|
||||
|
||||
build: $(OBJ)
|
||||
mv $^ ../../out/lib/
|
||||
cp $(CRTBEGIN_PATH) $(CRTEND_PATH) $(CRTI_PATH) $(CRTN_PATH) ../../out/lib/
|
||||
|
||||
%.o: %.c
|
||||
$(info Compiling $<)
|
||||
$(CC) -nostdlib -mno-red-zone -std=c17 -c $< -o $@
|
||||
|
||||
%.o: %.asm
|
||||
$(info Compiling $<)
|
||||
$(NASM) $< -f $(ASM_ARCH) -o $@
|
||||
|
||||
%.o: %.S
|
||||
$(info Compiling $<)
|
||||
$(AS) -c $< -o $@
|
||||
|
||||
clean:
|
||||
|
21
Userspace/libc/runtime/crt0.c
Normal file
21
Userspace/libc/runtime/crt0.c
Normal file
@ -0,0 +1,21 @@
|
||||
// https://wiki.osdev.org/Creating_a_C_Library
|
||||
#define asm __asm__ __volatile__
|
||||
__attribute__((naked, used, no_stack_protector, section(".text"))) void _start()
|
||||
{
|
||||
asm("movq $0, %rbp\n");
|
||||
asm("pushq %rbp\n");
|
||||
asm("pushq %rbp\n");
|
||||
asm("movq %rsp, %rbp\n");
|
||||
asm("pushq %rcx\n"
|
||||
"pushq %rdx\n"
|
||||
"pushq %rsi\n"
|
||||
"pushq %rdi\n");
|
||||
asm("call __libc_init\n");
|
||||
asm("popq %rdi\n"
|
||||
"popq %rsi\n"
|
||||
"popq %rdx\n"
|
||||
"popq %rcx\n");
|
||||
asm("call main\n");
|
||||
asm("movl %eax, %edi\n");
|
||||
asm("call _exit\n");
|
||||
}
|
21
Userspace/libc/runtime/crt1.c
Normal file
21
Userspace/libc/runtime/crt1.c
Normal file
@ -0,0 +1,21 @@
|
||||
// https://wiki.osdev.org/Creating_a_C_Library
|
||||
#define asm __asm__ __volatile__
|
||||
__attribute__((naked, used, no_stack_protector, section(".text"))) void _start()
|
||||
{
|
||||
asm("movq $0, %rbp\n");
|
||||
asm("pushq %rbp\n");
|
||||
asm("pushq %rbp\n");
|
||||
asm("movq %rsp, %rbp\n");
|
||||
asm("pushq %rcx\n"
|
||||
"pushq %rdx\n"
|
||||
"pushq %rsi\n"
|
||||
"pushq %rdi\n");
|
||||
asm("call __libc_init\n");
|
||||
asm("popq %rdi\n"
|
||||
"popq %rsi\n"
|
||||
"popq %rdx\n"
|
||||
"popq %rcx\n");
|
||||
asm("call main\n");
|
||||
asm("movl %eax, %edi\n");
|
||||
asm("call _exit\n");
|
||||
}
|
Reference in New Issue
Block a user