From a88f7c99fe7026ee6e0c0a2b2385e767c04133c2 Mon Sep 17 00:00:00 2001 From: Daniel Bertalan Date: Mon, 12 Jul 2021 20:37:38 +0200 Subject: [PATCH] LibC: Use our implementation of `crti.o` and `crtn.o` We have had these for quite a while, but we didn't compile them, and used GCC's version instead. Clang does not come with these, so we have to provide our own implementation. Our implementation follows what `musl` and `FreeBSD` do, so this should work fine, even if documentation can hardly be found for them. --- Userland/DynamicLoader/main.cpp | 6 ------ Userland/Libraries/LibC/CMakeLists.txt | 18 ++++++++++++++++- Userland/Libraries/LibC/arch/i386/crti.S | 23 ++++++++++++++++++++++ Userland/Libraries/LibC/arch/i386/crtn.S | 15 ++++++++++++++ Userland/Libraries/LibC/arch/x86_64/crti.S | 23 ++++++++++++++++++++++ Userland/Libraries/LibC/arch/x86_64/crtn.S | 15 ++++++++++++++ Userland/Libraries/LibC/crti.S | 15 -------------- Userland/Libraries/LibC/crtn.S | 13 ------------ 8 files changed, 93 insertions(+), 35 deletions(-) create mode 100644 Userland/Libraries/LibC/arch/i386/crti.S create mode 100644 Userland/Libraries/LibC/arch/i386/crtn.S create mode 100644 Userland/Libraries/LibC/arch/x86_64/crti.S create mode 100644 Userland/Libraries/LibC/arch/x86_64/crtn.S delete mode 100644 Userland/Libraries/LibC/crti.S delete mode 100644 Userland/Libraries/LibC/crtn.S diff --git a/Userland/DynamicLoader/main.cpp b/Userland/DynamicLoader/main.cpp index 74d122f820c..6a027c08328 100644 --- a/Userland/DynamicLoader/main.cpp +++ b/Userland/DynamicLoader/main.cpp @@ -150,10 +150,4 @@ void _entry(int argc, char** argv, char** envp) ELF::DynamicLinker::linker_main(move(main_program_name), main_program_fd, is_secure, argc, argv, envp); VERIFY_NOT_REACHED(); } - -void _fini(); - -void _fini() -{ -} } diff --git a/Userland/Libraries/LibC/CMakeLists.txt b/Userland/Libraries/LibC/CMakeLists.txt index defdeaf7012..ab1288a007d 100644 --- a/Userland/Libraries/LibC/CMakeLists.txt +++ b/Userland/Libraries/LibC/CMakeLists.txt @@ -65,9 +65,13 @@ file(GLOB ELF_SOURCES CONFIGURE_DEPENDS "../LibELF/*.cpp") if ("${SERENITY_ARCH}" STREQUAL "i686") set(ASM_SOURCES "arch/i386/setjmp.S") set(ELF_SOURCES ${ELF_SOURCES} ../LibELF/Arch/i386/entry.S ../LibELF/Arch/i386/plt_trampoline.S) + set(CRTI_SOURCE "arch/i386/crti.S") + set(CRTN_SOURCE "arch/i386/crtn.S") elseif ("${SERENITY_ARCH}" STREQUAL "x86_64") set(ASM_SOURCES "arch/x86_64/setjmp.S") set(ELF_SOURCES ${ELF_SOURCES} ../LibELF/Arch/x86_64/entry.S ../LibELF/Arch/x86_64/plt_trampoline.S) + set(CRTI_SOURCE "arch/x86_64/crti.S") + set(CRTN_SOURCE "arch/x86_64/crtn.S") endif() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-warning-option -DSERENITY_LIBC_BUILD") @@ -83,6 +87,18 @@ add_custom_command( COMMAND ${CMAKE_COMMAND} -E copy $ ${CMAKE_INSTALL_PREFIX}/usr/lib/crt0_shared.o ) +add_library(crti STATIC ${CRTI_SOURCE}) +add_custom_command( + TARGET crti + COMMAND ${CMAKE_COMMAND} -E copy $ ${CMAKE_INSTALL_PREFIX}/usr/lib/crti.o +) + +add_library(crtn STATIC ${CRTN_SOURCE}) +add_custom_command( + TARGET crtn + COMMAND ${CMAKE_COMMAND} -E copy $ ${CMAKE_INSTALL_PREFIX}/usr/lib/crtn.o +) + set_source_files_properties (ssp.cpp PROPERTIES COMPILE_FLAGS "-fno-stack-protector") add_library(ssp STATIC ssp.cpp) @@ -118,7 +134,7 @@ set_property( set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++") serenity_libc(LibC c) -add_dependencies(LibC crt0 crt0_shared) +add_dependencies(LibC crti crt0 crt0_shared crtn) target_link_libraries(LibC ssp system) # We mark LibCStatic as a dependency of LibC because this triggers the build of the LibCStatic target diff --git a/Userland/Libraries/LibC/arch/i386/crti.S b/Userland/Libraries/LibC/arch/i386/crti.S new file mode 100644 index 00000000000..a5cd2f46c0b --- /dev/null +++ b/Userland/Libraries/LibC/arch/i386/crti.S @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2018-2021, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +.section .init, "ax", @progbits +.align 4 +.global _init +.type _init, @function +_init: + pushl %ebp + movl %esp, %ebp + andl $-16, %esp + +.section .fini, "ax", @progbits +.align 4 +.global _fini +.type _fini, @function +_fini: + pushl %ebp + movl %esp, %ebp + andl $-16, %esp diff --git a/Userland/Libraries/LibC/arch/i386/crtn.S b/Userland/Libraries/LibC/arch/i386/crtn.S new file mode 100644 index 00000000000..1861755c74d --- /dev/null +++ b/Userland/Libraries/LibC/arch/i386/crtn.S @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2018-2021, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +.section .init, "ax", @progbits + movl %ebp, %esp + popl %ebp + retl + +.section .fini, "ax", @progbits + movl %ebp, %esp + popl %ebp + retl diff --git a/Userland/Libraries/LibC/arch/x86_64/crti.S b/Userland/Libraries/LibC/arch/x86_64/crti.S new file mode 100644 index 00000000000..af58391ae88 --- /dev/null +++ b/Userland/Libraries/LibC/arch/x86_64/crti.S @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021, Daniel Bertalan + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +.section .init, "ax", @progbits +.align 4 +.global _init +.type _init, @function +_init: + pushq %rbp + movq %rsp, %rbp + andq $-16, %rsp + +.section .fini, "ax", @progbits +.align 4 +.global _fini +.type _fini, @function +_fini: + pushq %rbp + movq %rsp, %rbp + andq $-16, %rsp diff --git a/Userland/Libraries/LibC/arch/x86_64/crtn.S b/Userland/Libraries/LibC/arch/x86_64/crtn.S new file mode 100644 index 00000000000..71d6b29b546 --- /dev/null +++ b/Userland/Libraries/LibC/arch/x86_64/crtn.S @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2021, Daniel Bertalan + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +.section .init, "ax", @progbits + movq %rbp, %rsp + popq %rbp + retq + +.section .fini, "ax", @progbits + movq %rbp, %rsp + popq %rbp + retq diff --git a/Userland/Libraries/LibC/crti.S b/Userland/Libraries/LibC/crti.S deleted file mode 100644 index 5c05ad34eae..00000000000 --- a/Userland/Libraries/LibC/crti.S +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -.global _init -.section .init -_init: - push %ebp - -.global _fini -.section .fini -_fini: - push %ebp diff --git a/Userland/Libraries/LibC/crtn.S b/Userland/Libraries/LibC/crtn.S deleted file mode 100644 index 9e45e1e3460..00000000000 --- a/Userland/Libraries/LibC/crtn.S +++ /dev/null @@ -1,13 +0,0 @@ -/* - * Copyright (c) 2018-2020, Andreas Kling - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -.section .init - pop %ebp - ret - -.section .fini - pop %ebp - ret