Skip to content

[libc] [startup] add cmake function to merge separated crt1 objects #75413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 60 additions & 11 deletions libc/startup/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,62 @@
# This function merges multiple objects into a single relocatable object
# cc -r obj1.o obj2.o -o obj.o
# A relocatable object is an object file that is not fully linked into an
# executable or a shared library. It is an intermediate file format that can
# be passed into the linker.
# A crt object has arch-specific code and arch-agnostic code. To reduce code
# duplication, the implementation is split into multiple units. As a result,
# we need to merge them into a single relocatable object.
# See also: https://maskray.me/blog/2022-11-21-relocatable-linking
function(merge_relocatable_object name)
set(obj_list "")
set(fq_link_libraries "")
get_fq_deps_list(fq_dep_list ${ARGN})
foreach(target IN LISTS fq_dep_list)
list(APPEND obj_list "$<TARGET_OBJECTS:${target}>")
get_target_property(libs ${target} DEPS)
list(APPEND fq_link_libraries "${libs}")
endforeach()
list(REMOVE_DUPLICATES obj_list)
list(REMOVE_DUPLICATES fq_link_libraries)
get_fq_target_name(${name} fq_name)
set(relocatable_target "${fq_name}.__relocatable__")
add_executable(
${relocatable_target}
${obj_list}
)
# Pass -r to the driver is much cleaner than passing -Wl,-r: the compiler knows it is
# a relocatable linking and will not pass other irrelevant flags to the linker.
target_link_options(${relocatable_target} PRIVATE -r)
set_target_properties(
${relocatable_target}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
OUTPUT_NAME ${name}.o
)
add_library(${fq_name} OBJECT IMPORTED GLOBAL)
add_dependencies(${fq_name} ${relocatable_target})
target_link_libraries(${fq_name} INTERFACE ${fq_link_libraries})
set_target_properties(
${fq_name}
PROPERTIES
LINKER_LANGUAGE CXX
IMPORTED_OBJECTS ${CMAKE_CURRENT_BINARY_DIR}/${name}.o
TARGET_TYPE ${OBJECT_LIBRARY_TARGET_TYPE}
DEPS "${fq_link_libraries}"
)
endfunction()

function(add_startup_object name)
cmake_parse_arguments(
"ADD_STARTUP_OBJECT"
"ALIAS" # Option argument
"" # Option argument
"SRC" # Single value arguments
"DEPENDS;COMPILE_OPTIONS" # Multi value arguments
${ARGN}
)

get_fq_target_name(${name} fq_target_name)
if(ADD_STARTUP_OBJECT_ALIAS)
get_fq_deps_list(fq_dep_list ${ADD_STARTUP_OBJECT_DEPENDS})
add_library(${fq_target_name} ALIAS ${fq_dep_list})
return()
endif()


add_object_library(
${name}
SRCS ${ADD_STARTUP_OBJECT_SRC}
Expand All @@ -27,18 +70,24 @@ function(add_startup_object name)
)
endfunction()

check_cxx_compiler_flag("-r" LIBC_LINKER_SUPPORTS_RELOCATABLE)

if(NOT LIBC_LINKER_SUPPORTS_RELOCATABLE)
message(STATUS "Skipping startup for target architecture ${LIBC_TARGET_ARCHITECTURE}: linker does not support -r")
return()
endif()

if(NOT (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE}))
message(STATUS "Skipping startup for target architecture ${LIBC_TARGET_ARCHITECTURE}")
return()
endif()

add_subdirectory(${LIBC_TARGET_ARCHITECTURE})

add_startup_object(
# TODO: factor out crt1 into multiple objects
merge_relocatable_object(
crt1
ALIAS
DEPENDS
.${LIBC_TARGET_ARCHITECTURE}.crt1
.${LIBC_TARGET_ARCHITECTURE}.crt1
)

add_startup_object(
Expand Down