Kernel/LibC: Add crt* files to LibC and remove crt0 from kernel

There was no reason for libc get crt0 from kernel.
This commit is contained in:
2023-10-30 11:06:13 +02:00
parent f33c0bad99
commit 0b5fcb3f88
5 changed files with 49 additions and 19 deletions

View File

@@ -31,8 +31,22 @@ add_custom_target(libc-headers
USES_TERMINAL
)
add_custom_target(crtx
COMMAND ${CMAKE_C_COMPILER} -c ${CMAKE_CURRENT_SOURCE_DIR}/arch/${BANAN_ARCH}/crt0.S -o crt0.o
COMMAND ${CMAKE_C_COMPILER} -c ${CMAKE_CURRENT_SOURCE_DIR}/arch/${BANAN_ARCH}/crti.S -o crti.o
COMMAND ${CMAKE_C_COMPILER} -c ${CMAKE_CURRENT_SOURCE_DIR}/arch/${BANAN_ARCH}/crtn.S -o crtn.o
)
add_custom_target(crtx-install
COMMAND sudo cp crt0.o ${BANAN_LIB}/
COMMAND sudo cp crti.o ${BANAN_LIB}/
COMMAND sudo cp crtn.o ${BANAN_LIB}/
DEPENDS crtx
USES_TERMINAL
)
add_library(libc ${LIBC_SOURCES})
add_dependencies(libc headers crt0)
add_dependencies(libc headers crtx-install)
target_compile_options(libc PRIVATE -g -Wstack-usage=512)

35
libc/arch/x86_64/crt0.S Normal file
View File

@@ -0,0 +1,35 @@
.section .text
.global _start
_start:
# Set up end of the stack frame linked list.
movq $0, %rbp
pushq %rbp # rip=0
pushq %rbp # rbp=0
movq %rsp, %rbp
# Save argc, argv, environ
pushq %rdx
pushq %rsi
pushq %rdi
# Prepare malloc, environment
movq %rdx, %rdi
call _init_libc
# Call global constructos
call _init
# Restore argc, argv, environ
popq %rdi
popq %rsi
popq %rdx
# Run main
call main
# Cleanly exit the process
movl %eax, %edi
call exit
.size _start, . - _start

16
libc/arch/x86_64/crti.S Normal file
View File

@@ -0,0 +1,16 @@
/* x86-64 crti.s */
.section .init
.global _init
.type _init, @function
_init:
pushq %rbp
movq %rsp, %rbp
/* gcc will nicely put the contents of crtbegin.o's .init section here. */
.section .fini
.global _fini
.type _fini, @function
_fini:
pushq %rbp
movq %rsp, %rbp
/* gcc will nicely put the contents of crtbegin.o's .fini section here. */

10
libc/arch/x86_64/crtn.S Normal file
View File

@@ -0,0 +1,10 @@
/* x86-64 crtn.s */
.section .init
/* gcc will nicely put the contents of crtend.o's .init section here. */
popq %rbp
ret
.section .fini
/* gcc will nicely put the contents of crtend.o's .fini section here. */
popq %rbp
ret