Updated libc

This commit is contained in:
Alex
2022-12-07 13:53:08 +02:00
parent bc92258a5f
commit 2b57c3126a
28 changed files with 360 additions and 65 deletions

View File

@ -2,12 +2,13 @@
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')
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}
OBJ = ${C_SOURCES:.c=.o} ${ASM_SOURCES:.asm=.o} ${S_SOURCES:.S=.o}
ifeq ($(OSARCH), amd64)
ASM_ARCH := elf64
@ -19,13 +20,13 @@ build: $(OBJ)
mv $^ ../../out/system/lib/
%.o: %.c
$(CC) -nostdlib -std=c17 -c $< -o $@
$(CC) -nostdlib -mno-red-zone -std=c17 -c $< -o $@
%.o: %.asm
$(NASM) $< -f $(ASM_ARCH) -o $@
%.bin: %.s
$(NASM) $< -f $(ASM_ARCH) -o $@
%.o: %.S
$(AS) -c $< -o $@
clean:

23
libc/runtime/crt0.S Normal file
View File

@ -0,0 +1,23 @@
# https://wiki.osdev.org/Creating_a_C_Library
.section .text
.global _start
_start:
movq $0, %rbp
pushq %rbp
pushq %rbp
movq %rsp, %rbp
pushq %rsi
pushq %rdi
call __libc_init
call _init
popq %rdi
popq %rsi
call main
movl %eax, %edi
call _exit
.size _start, . - _start

View File

@ -1,15 +0,0 @@
int main(int argc, char *argv[], char *envp[]);
void _start()
{
register int argc __asm__("rdi");
register char **argv __asm__("rsi");
register char **envp __asm__("rdx");
int mainret = main((int)argc, (char **)argv, (char **)envp);
__asm__ __volatile__("syscall"
:
: "a"(0), "D"(mainret)
: "rcx", "r11", "memory");
return;
}
// C stuff

23
libc/runtime/crt1.S Normal file
View File

@ -0,0 +1,23 @@
# https://wiki.osdev.org/Creating_a_C_Library
.section .text
.global _start
_start:
movq $0, %rbp
pushq %rbp
pushq %rbp
movq %rsp, %rbp
pushq %rsi
pushq %rdi
call __libc_init
call _init
popq %rdi
popq %rsi
call main
movl %eax, %edi
call _exit
.size _start, . - _start

View File

@ -1,15 +0,0 @@
int main(int argc, char *argv[], char *envp[]);
void _start()
{
register int argc __asm__("rdi");
register char **argv __asm__("rsi");
register char **envp __asm__("rdx");
int mainret = main((int)argc, (char **)argv, (char **)envp);
__asm__ __volatile__("syscall"
:
: "a"(0), "D"(mainret)
: "rcx", "r11", "memory");
return;
}
// C++ stuff

14
libc/runtime/crtbegin.S Normal file
View File

@ -0,0 +1,14 @@
# I WILL PLACE HERE THE crti.o BECAUSE GCC REFUSE TO LINK IT AND I DON'T KNOW WHY
.section .init
.global _init
.type _init, @function
_init:
push %rbp
movq %rsp, %rbp
.section .fini
.global _fini
.type _fini, @function
_fini:
push %rbp
movq %rsp, %rbp

View File

@ -1 +0,0 @@
// C++ constructor/destructor stuff

8
libc/runtime/crtend.S Normal file
View File

@ -0,0 +1,8 @@
# I WILL PLACE HERE THE crtn.o BECAUSE GCC REFUSE TO LINK IT AND I DON'T KNOW WHY
.section .init
popq %rbp
ret
.section .fini
popq %rbp
ret

View File

@ -1 +0,0 @@
// C++ constructor/destructor stuff