Toolchain+LibC: Fix usage of crt files

We now configure the gcc spec files to use a different crt files for
static & PIE binaries.

This relieves us from the need to explicitly specify the desired crt0
file in cmake scripts.
This commit is contained in:
Itamar 2020-12-20 22:32:10 +02:00 committed by Andreas Kling
parent 6990d62977
commit bbedd320b5
Notes: sideshowbarker 2024-07-19 00:37:34 +09:00
9 changed files with 32 additions and 20 deletions

View file

@ -113,8 +113,8 @@ add_compile_options(-ffile-prefix-map=${CMAKE_SOURCE_DIR}=.)
add_compile_definitions(DEBUG SANITIZE_PTRS) add_compile_definitions(DEBUG SANITIZE_PTRS)
set(CMAKE_CXX_FLAGS_STATIC "${CMAKE_CXX_FLAGS} -static") set(CMAKE_CXX_FLAGS_STATIC "${CMAKE_CXX_FLAGS} -static")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostartfiles -pie -fpic") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pie -fpic")
add_link_options(--sysroot ${CMAKE_BINARY_DIR}/Root) add_link_options(--sysroot ${CMAKE_BINARY_DIR}/Root)
include_directories(Libraries/LibC) include_directories(Libraries/LibC)

View file

@ -1,6 +1,5 @@
set(SOURCES set(SOURCES
main.cpp main.cpp
../../Libraries/LibC/crt0_shared.cpp
) )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostartfiles -lgcc_s -pie -fpic ") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostartfiles -lgcc_s -pie -fpic ")

View file

@ -1,7 +1,6 @@
set(LIBC_SOURCES set(LIBC_SOURCES
arpa/inet.cpp arpa/inet.cpp
assert.cpp assert.cpp
crt0_shared.cpp
ctype.cpp ctype.cpp
cxxabi.cpp cxxabi.cpp
dirent.cpp dirent.cpp
@ -64,6 +63,11 @@ add_custom_command(
TARGET crt0 TARGET crt0
COMMAND ${INSTALL_COMMAND} -D $<TARGET_OBJECTS:crt0> ${CMAKE_INSTALL_PREFIX}/usr/lib/crt0.o COMMAND ${INSTALL_COMMAND} -D $<TARGET_OBJECTS:crt0> ${CMAKE_INSTALL_PREFIX}/usr/lib/crt0.o
) )
add_library(crt0_shared STATIC crt0_shared.cpp)
add_custom_command(
TARGET crt0_shared
COMMAND ${INSTALL_COMMAND} -D $<TARGET_OBJECTS:crt0_shared> ${CMAKE_INSTALL_PREFIX}/usr/lib/crt0_shared.o
)
set(SOURCES ${LIBC_SOURCES} ${AK_SOURCES} ${ELF_SOURCES}) set(SOURCES ${LIBC_SOURCES} ${AK_SOURCES} ${ELF_SOURCES})

View file

@ -327,17 +327,32 @@ void DynamicLoader::do_relocations(size_t total_tls_size)
} }
case R_386_GLOB_DAT: { case R_386_GLOB_DAT: {
auto symbol = relocation.symbol(); auto symbol = relocation.symbol();
if (!strcmp(symbol.name(), "__deregister_frame_info") || !strcmp(symbol.name(), "_ITM_registerTMCloneTable")
|| !strcmp(symbol.name(), "_ITM_deregisterTMCloneTable") || !strcmp(symbol.name(), "__register_frame_info")
|| !strcmp(symbol.name(), "__cxa_finalize") // __cxa_finalize will be called from libc's exit()
) {
// We do not support these
break;
}
VERBOSE("Global data relocation: '%s', value: %p\n", symbol.name(), symbol.value()); VERBOSE("Global data relocation: '%s', value: %p\n", symbol.name(), symbol.value());
auto res = lookup_symbol(symbol); auto res = lookup_symbol(symbol);
if (!res.found) {
// We do not support these
// TODO: Can we tell gcc not to generate the piece of code that uses these?
// (--disable-tm-clone-registry flag in gcc conifugraion?)
if (!strcmp(symbol.name(), "__deregister_frame_info") || !strcmp(symbol.name(), "_ITM_registerTMCloneTable")
|| !strcmp(symbol.name(), "_ITM_deregisterTMCloneTable") || !strcmp(symbol.name(), "__register_frame_info")) {
break;
}
// The "__do_global_dtors_aux" function in libgcc_s.so needs this symbol,
// but we do not use that function so we don't actually need to resolve this symbol.
// The reason we can't resolve it here is that the symbol is defined in libc.so,
// but there's a circular dependecy between libgcc_s.so and libc.so,
// we deal with it by first loading libgcc_s and then libc.
// So we cannot find this symbol at this time (libc is not yet loaded).
if (m_filename == "libgcc_s.so" && !strcmp(symbol.name(), "__cxa_finalize")) {
break;
}
// Symbol not found
ASSERT_NOT_REACHED();
}
VERBOSE("was symbol found? %d, address: 0x%x\n", res.found, res.address); VERBOSE("was symbol found? %d, address: 0x%x\n", res.found, res.address);
ASSERT(res.found); VERBOSE("object: %s\n", m_filename.characters());
if (!res.found) { if (!res.found) {
// TODO this is a hack // TODO this is a hack

View file

@ -28,8 +28,7 @@ function(serenity_lib target_name fs_name)
serenity_install_headers(${target_name}) serenity_install_headers(${target_name})
serenity_install_sources("Libraries/${target_name}") serenity_install_sources("Libraries/${target_name}")
#add_library(${target_name} SHARED ${SOURCES} ${GENERATED_SOURCES}) #add_library(${target_name} SHARED ${SOURCES} ${GENERATED_SOURCES})
add_library(${target_name} SHARED ${SOURCES} ${GENERATED_SOURCES} ${CMAKE_SOURCE_DIR}/Libraries/LibC/crt0_shared.cpp) add_library(${target_name} SHARED ${SOURCES} ${GENERATED_SOURCES})
#library_sources("{target_name}" PRIVATE ${CMAKE_SOURCE_DIR}/Libraries/LibC/crt0_shared.cpp)
install(TARGETS ${target_name} DESTINATION usr/lib) install(TARGETS ${target_name} DESTINATION usr/lib)
set_target_properties(${target_name} PROPERTIES OUTPUT_NAME ${fs_name}) set_target_properties(${target_name} PROPERTIES OUTPUT_NAME ${fs_name})
serenity_generated_sources(${target_name}) serenity_generated_sources(${target_name})
@ -67,7 +66,6 @@ endfunction()
function(serenity_bin target_name) function(serenity_bin target_name)
add_executable(${target_name} ${SOURCES}) add_executable(${target_name} ${SOURCES})
target_sources(${target_name} PRIVATE ${CMAKE_SOURCE_DIR}/Libraries/LibC/crt0_shared.cpp)
install(TARGETS ${target_name} RUNTIME DESTINATION bin) install(TARGETS ${target_name} RUNTIME DESTINATION bin)
serenity_generated_sources(${target_name}) serenity_generated_sources(${target_name})
endfunction() endfunction()

View file

@ -115,7 +115,7 @@ diff -ruN a/gcc/config/serenity.h b/gcc/config/serenity.h
+/* Files that are linked before user code. +/* Files that are linked before user code.
+ The %s tells GCC to look for these files in the library directory. */ + The %s tells GCC to look for these files in the library directory. */
+#undef STARTFILE_SPEC +#undef STARTFILE_SPEC
+#define STARTFILE_SPEC "%{!shared:crt0.o%s} crti.o%s %{shared|pie:crtbeginS.o%s; :crtbegin.o%s}" +#define STARTFILE_SPEC "%{static:crt0.o%s} crti.o%s %{!static: %{!fbuilding-libgcc:crt0_shared.o%s}} %{shared|pie:crtbeginS.o%s; :crtbegin.o%s}"
+ +
+/* Files that are linked after user code. */ +/* Files that are linked after user code. */
+#undef ENDFILE_SPEC +#undef ENDFILE_SPEC

View file

@ -5,13 +5,11 @@ foreach(CMD_SRC ${CMD_SOURCES})
get_filename_component(CMD_NAME ${CMD_SRC} NAME_WE) get_filename_component(CMD_NAME ${CMD_SRC} NAME_WE)
if (CMD_NAME IN_LIST SPECIAL_TARGETS) if (CMD_NAME IN_LIST SPECIAL_TARGETS)
add_executable("${CMD_NAME}-bin" ${CMD_SRC}) add_executable("${CMD_NAME}-bin" ${CMD_SRC})
target_sources("${CMD_NAME}-bin" PRIVATE ${CMAKE_SOURCE_DIR}/Libraries/LibC/crt0_shared.cpp)
target_link_libraries("${CMD_NAME}-bin" LibCore) target_link_libraries("${CMD_NAME}-bin" LibCore)
install(TARGETS "${CMD_NAME}-bin" RUNTIME DESTINATION bin) install(TARGETS "${CMD_NAME}-bin" RUNTIME DESTINATION bin)
install(CODE "execute_process(COMMAND mv ${CMD_NAME}-bin ${CMD_NAME} WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX}/bin)") install(CODE "execute_process(COMMAND mv ${CMD_NAME}-bin ${CMD_NAME} WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX}/bin)")
else () else ()
add_executable(${CMD_NAME} ${CMD_SRC}) add_executable(${CMD_NAME} ${CMD_SRC})
target_sources("${CMD_NAME}" PRIVATE ${CMAKE_SOURCE_DIR}/Libraries/LibC/crt0_shared.cpp)
target_link_libraries(${CMD_NAME} LibCore) target_link_libraries(${CMD_NAME} LibCore)
install(TARGETS ${CMD_NAME} RUNTIME DESTINATION bin) install(TARGETS ${CMD_NAME} RUNTIME DESTINATION bin)
endif() endif()

View file

@ -3,7 +3,6 @@ file(GLOB CMD_SOURCES CONFIGURE_DEPENDS "*.cpp")
foreach(CMD_SRC ${CMD_SOURCES}) foreach(CMD_SRC ${CMD_SOURCES})
get_filename_component(CMD_NAME ${CMD_SRC} NAME_WE) get_filename_component(CMD_NAME ${CMD_SRC} NAME_WE)
add_executable(${CMD_NAME} ${CMD_SRC}) add_executable(${CMD_NAME} ${CMD_SRC})
target_sources("${CMD_NAME}" PRIVATE ${CMAKE_SOURCE_DIR}/Libraries/LibC/crt0_shared.cpp)
target_link_libraries(${CMD_NAME} LibCore) target_link_libraries(${CMD_NAME} LibCore)
install(TARGETS ${CMD_NAME} RUNTIME DESTINATION usr/Tests/Kernel) install(TARGETS ${CMD_NAME} RUNTIME DESTINATION usr/Tests/Kernel)
endforeach() endforeach()

View file

@ -3,7 +3,6 @@ file(GLOB CMD_SOURCES CONFIGURE_DEPENDS "*.cpp")
foreach(CMD_SRC ${CMD_SOURCES}) foreach(CMD_SRC ${CMD_SOURCES})
get_filename_component(CMD_NAME ${CMD_SRC} NAME_WE) get_filename_component(CMD_NAME ${CMD_SRC} NAME_WE)
add_executable(${CMD_NAME} ${CMD_SRC}) add_executable(${CMD_NAME} ${CMD_SRC})
target_sources("${CMD_NAME}" PRIVATE ${CMAKE_SOURCE_DIR}/Libraries/LibC/crt0_shared.cpp)
target_link_libraries(${CMD_NAME} LibCore) target_link_libraries(${CMD_NAME} LibCore)
install(TARGETS ${CMD_NAME} RUNTIME DESTINATION usr/Tests/LibC) install(TARGETS ${CMD_NAME} RUNTIME DESTINATION usr/Tests/LibC)
endforeach() endforeach()