mirror of
https://github.com/Fennix-Project/Userspace.git
synced 2025-07-10 14:49:22 +00:00
Updated libc
This commit is contained in:
@ -1,12 +1,8 @@
|
||||
build:
|
||||
cp include/* ../out/system/include
|
||||
make --quiet -C runtime build
|
||||
make --quiet -C libgcc build
|
||||
make --quiet -C libssp build
|
||||
make --quiet -C src build
|
||||
|
||||
clean:
|
||||
make --quiet -C runtime clean
|
||||
make --quiet -C libgcc clean
|
||||
make --quiet -C libssp clean
|
||||
make --quiet -C src clean
|
||||
make -C runtime clean
|
||||
make -C src clean
|
||||
|
@ -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
23
libc/runtime/crt0.S
Normal 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
|
@ -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
23
libc/runtime/crt1.S
Normal 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
|
@ -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
14
libc/runtime/crtbegin.S
Normal 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
|
@ -1 +0,0 @@
|
||||
// C++ constructor/destructor stuff
|
8
libc/runtime/crtend.S
Normal file
8
libc/runtime/crtend.S
Normal 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
|
@ -1 +0,0 @@
|
||||
// C++ constructor/destructor stuff
|
21
libc/src/InitArray.c
Normal file
21
libc/src/InitArray.c
Normal file
@ -0,0 +1,21 @@
|
||||
extern void (*__preinit_array_start[])(void) __attribute__((weak));
|
||||
extern void (*__preinit_array_end[])(void) __attribute__((weak));
|
||||
extern void (*__init_array_start[])(void) __attribute__((weak));
|
||||
extern void (*__init_array_end[])(void) __attribute__((weak));
|
||||
extern void (*__fini_array_start []) (void) __attribute__((weak));
|
||||
extern void (*__fini_array_end []) (void) __attribute__((weak));
|
||||
|
||||
extern void _init(void);
|
||||
|
||||
void __libc_init_array(void)
|
||||
{
|
||||
unsigned long Count = __preinit_array_end - __preinit_array_start;
|
||||
for (unsigned long i = 0; i < Count; i++)
|
||||
__preinit_array_start[i]();
|
||||
|
||||
_init();
|
||||
|
||||
Count = __init_array_end - __init_array_start;
|
||||
for (unsigned long i = 0; i < Count; i++)
|
||||
__init_array_start[i]();
|
||||
}
|
@ -8,13 +8,14 @@ OBJECT_NAME=lib$(NAME).a
|
||||
OUTPUT_DIR=../../out/system/lib/
|
||||
|
||||
CC = ../../../$(COMPILER_PATH)/$(COMPILER_ARCH)gcc
|
||||
AS = ../../../$(COMPILER_PATH)/$(COMPILER_ARCH)as
|
||||
AR = ../../../$(COMPILER_PATH)/$(COMPILER_ARCH)ar
|
||||
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}
|
||||
|
||||
INCLUDE_DIR = ../include
|
||||
|
||||
@ -24,7 +25,7 @@ else ifeq ($(OSARCH), i686)
|
||||
ASM_ARCH := elf32
|
||||
endif
|
||||
|
||||
CFLAGS := -fPIC -I$(INCLUDE_DIR)
|
||||
CFLAGS := -fPIC -mno-red-zone -I$(INCLUDE_DIR)
|
||||
|
||||
build: $(OBJECT_NAME)
|
||||
|
||||
@ -37,6 +38,9 @@ $(OBJECT_NAME): $(OBJ)
|
||||
%.o: %.cpp $(HEADERS) $(HEADERS2)
|
||||
$(CC) $(CFLAGS) -std=c++20 -c $< -o $@
|
||||
|
||||
%.o: %.S
|
||||
$(AS) -c $< -o $@
|
||||
|
||||
%.o: %.asm
|
||||
$(NASM) $< -f $(ASM_ARCH) -o $@
|
||||
|
||||
|
18
libc/src/Runtime.c
Normal file
18
libc/src/Runtime.c
Normal file
@ -0,0 +1,18 @@
|
||||
|
||||
extern void __libc_init_array();
|
||||
|
||||
void __libc_init()
|
||||
{
|
||||
__libc_init_array();
|
||||
}
|
||||
|
||||
void _exit(int Code)
|
||||
{
|
||||
__asm__ __volatile__("syscall"
|
||||
:
|
||||
: "a"(0), "D"(Code)
|
||||
: "rcx", "r11", "memory");
|
||||
|
||||
while (1)
|
||||
__asm__ __volatile__("hlt");
|
||||
}
|
Reference in New Issue
Block a user