From 0b5fcb3f8898dc13cbc376f2120ae3c7182ea088 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 30 Oct 2023 11:06:13 +0200 Subject: [PATCH] Kernel/LibC: Add crt* files to LibC and remove crt0 from kernel There was no reason for libc get crt0 from kernel. --- kernel/CMakeLists.txt | 15 ++------------- libc/CMakeLists.txt | 16 +++++++++++++++- {kernel => libc}/arch/x86_64/crt0.S | 11 ++++++----- libc/arch/x86_64/crti.S | 16 ++++++++++++++++ libc/arch/x86_64/crtn.S | 10 ++++++++++ 5 files changed, 49 insertions(+), 19 deletions(-) rename {kernel => libc}/arch/x86_64/crt0.S (63%) create mode 100644 libc/arch/x86_64/crti.S create mode 100644 libc/arch/x86_64/crtn.S diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 2c29fa3695..d7e140a580 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -162,17 +162,6 @@ endif() 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 COMMAND sudo rsync -a ${CMAKE_CURRENT_SOURCE_DIR}/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 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 cp ${CRTBEGIN} . - COMMAND cp ${CRTEND} . + COMMAND ${CMAKE_COMMAND} -E copy ${CRTBEGIN} . + COMMAND ${CMAKE_COMMAND} -E copy ${CRTEND} . ) #add_custom_command( diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index 76c4f766fd..5432b6f048 100644 --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -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) diff --git a/kernel/arch/x86_64/crt0.S b/libc/arch/x86_64/crt0.S similarity index 63% rename from kernel/arch/x86_64/crt0.S rename to libc/arch/x86_64/crt0.S index dc3fdffc7d..67804f1990 100644 --- a/kernel/arch/x86_64/crt0.S +++ b/libc/arch/x86_64/crt0.S @@ -8,19 +8,19 @@ _start: pushq %rbp # rbp=0 movq %rsp, %rbp - # We need those in a moment when we call main. + # Save argc, argv, environ pushq %rdx pushq %rsi pushq %rdi - # Prepare signals, memory allocation, stdio and such. + # Prepare malloc, environment movq %rdx, %rdi call _init_libc - # Run the global constructors. + # Call global constructos call _init - # Restore argc and argv. + # Restore argc, argv, environ popq %rdi popq %rsi popq %rdx @@ -28,7 +28,8 @@ _start: # Run main call main - # Terminate the process with the exit code. + # Cleanly exit the process movl %eax, %edi call exit + .size _start, . - _start diff --git a/libc/arch/x86_64/crti.S b/libc/arch/x86_64/crti.S new file mode 100644 index 0000000000..7c9624cc5f --- /dev/null +++ b/libc/arch/x86_64/crti.S @@ -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. */ diff --git a/libc/arch/x86_64/crtn.S b/libc/arch/x86_64/crtn.S new file mode 100644 index 0000000000..5d6f1e9a15 --- /dev/null +++ b/libc/arch/x86_64/crtn.S @@ -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