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.
This commit is contained in:
Daniel Bertalan 2021-07-12 20:37:38 +02:00 committed by Andreas Kling
parent 7f2eb2f332
commit a88f7c99fe
8 changed files with 93 additions and 35 deletions

View file

@ -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()
{
}
}

View file

@ -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 $<TARGET_OBJECTS:crt0_shared> ${CMAKE_INSTALL_PREFIX}/usr/lib/crt0_shared.o
)
add_library(crti STATIC ${CRTI_SOURCE})
add_custom_command(
TARGET crti
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_OBJECTS:crti> ${CMAKE_INSTALL_PREFIX}/usr/lib/crti.o
)
add_library(crtn STATIC ${CRTN_SOURCE})
add_custom_command(
TARGET crtn
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_OBJECTS:crtn> ${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

View file

@ -0,0 +1,23 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* 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

View file

@ -0,0 +1,15 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* 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

View file

@ -0,0 +1,23 @@
/*
* Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
*
* 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

View file

@ -0,0 +1,15 @@
/*
* Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
*
* 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

View file

@ -1,15 +0,0 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
.global _init
.section .init
_init:
push %ebp
.global _fini
.section .fini
_fini:
push %ebp

View file

@ -1,13 +0,0 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
.section .init
pop %ebp
ret
.section .fini
pop %ebp
ret