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:
Bananymous 2023-10-30 11:06:13 +02:00
parent f33c0bad99
commit 0b5fcb3f88
5 changed files with 49 additions and 19 deletions

View File

@ -162,17 +162,6 @@ endif()
target_link_options(kernel PUBLIC -ffreestanding -nostdlib) target_link_options(kernel PUBLIC -ffreestanding -nostdlib)
add_custom_target(crt0
COMMAND ${CMAKE_CXX_COMPILER} -c ${CMAKE_CURRENT_SOURCE_DIR}/arch/${BANAN_ARCH}/crt0.S -o ${CMAKE_CURRENT_BINARY_DIR}/crt0.o
DEPENDS headers
)
add_custom_command(
TARGET crt0
POST_BUILD
COMMAND sudo cp ${CMAKE_CURRENT_BINARY_DIR}/crt0.o ${BANAN_LIB}/
)
add_custom_target(kernel-headers add_custom_target(kernel-headers
COMMAND sudo rsync -a ${CMAKE_CURRENT_SOURCE_DIR}/include/ ${BANAN_INCLUDE}/ COMMAND sudo rsync -a ${CMAKE_CURRENT_SOURCE_DIR}/include/ ${BANAN_INCLUDE}/
COMMAND sudo rsync -a ${CMAKE_CURRENT_SOURCE_DIR}/lai/include/ ${BANAN_INCLUDE}/ COMMAND sudo rsync -a ${CMAKE_CURRENT_SOURCE_DIR}/lai/include/ ${BANAN_INCLUDE}/
@ -193,8 +182,8 @@ add_custom_command(
TARGET kernel PRE_LINK TARGET kernel PRE_LINK
COMMAND ${CMAKE_CXX_COMPILER} -MD -c ${CMAKE_CURRENT_SOURCE_DIR}/arch/${BANAN_ARCH}/crti.S ${COMPILE_OPTIONS} COMMAND ${CMAKE_CXX_COMPILER} -MD -c ${CMAKE_CURRENT_SOURCE_DIR}/arch/${BANAN_ARCH}/crti.S ${COMPILE_OPTIONS}
COMMAND ${CMAKE_CXX_COMPILER} -MD -c ${CMAKE_CURRENT_SOURCE_DIR}/arch/${BANAN_ARCH}/crtn.S ${COMPILE_OPTIONS} COMMAND ${CMAKE_CXX_COMPILER} -MD -c ${CMAKE_CURRENT_SOURCE_DIR}/arch/${BANAN_ARCH}/crtn.S ${COMPILE_OPTIONS}
COMMAND cp ${CRTBEGIN} . COMMAND ${CMAKE_COMMAND} -E copy ${CRTBEGIN} .
COMMAND cp ${CRTEND} . COMMAND ${CMAKE_COMMAND} -E copy ${CRTEND} .
) )
#add_custom_command( #add_custom_command(

View File

@ -31,8 +31,22 @@ add_custom_target(libc-headers
USES_TERMINAL 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_library(libc ${LIBC_SOURCES})
add_dependencies(libc headers crt0) add_dependencies(libc headers crtx-install)
target_compile_options(libc PRIVATE -g -Wstack-usage=512) target_compile_options(libc PRIVATE -g -Wstack-usage=512)

View File

@ -8,19 +8,19 @@ _start:
pushq %rbp # rbp=0 pushq %rbp # rbp=0
movq %rsp, %rbp movq %rsp, %rbp
# We need those in a moment when we call main. # Save argc, argv, environ
pushq %rdx pushq %rdx
pushq %rsi pushq %rsi
pushq %rdi pushq %rdi
# Prepare signals, memory allocation, stdio and such. # Prepare malloc, environment
movq %rdx, %rdi movq %rdx, %rdi
call _init_libc call _init_libc
# Run the global constructors. # Call global constructos
call _init call _init
# Restore argc and argv. # Restore argc, argv, environ
popq %rdi popq %rdi
popq %rsi popq %rsi
popq %rdx popq %rdx
@ -28,7 +28,8 @@ _start:
# Run main # Run main
call main call main
# Terminate the process with the exit code. # Cleanly exit the process
movl %eax, %edi movl %eax, %edi
call exit call exit
.size _start, . - _start .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