[libc-commits] [libc] 8bbeed0 - [libc] [startup] add cmake function to merge separated crt1 objects (#75413)
via libc-commits
libc-commits at lists.llvm.org
Wed Dec 20 08:18:56 PST 2023
Author: Schrodinger ZHU Yifan
Date: 2023-12-20T08:18:51-08:00
New Revision: 8bbeed05c4da832a2abf532fee8665c0d6576cbc
URL: https://github.com/llvm/llvm-project/commit/8bbeed05c4da832a2abf532fee8665c0d6576cbc
DIFF: https://github.com/llvm/llvm-project/commit/8bbeed05c4da832a2abf532fee8665c0d6576cbc.diff
LOG: [libc] [startup] add cmake function to merge separated crt1 objects (#75413)
As part of startup refactoring, this patch adds a function to merge
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 can have arch-specific code and arch-agnostic code. To
reduce code cohesion, the implementation is splitted into multiple
units. As a result, we need to merge them into a single relocatable
object.
Added:
Modified:
libc/startup/linux/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/startup/linux/CMakeLists.txt b/libc/startup/linux/CMakeLists.txt
index 007aa30c17d6ab..d81c7db7a558b3 100644
--- a/libc/startup/linux/CMakeLists.txt
+++ b/libc/startup/linux/CMakeLists.txt
@@ -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}
@@ -27,6 +70,13 @@ 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()
@@ -34,11 +84,10 @@ 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(
More information about the libc-commits
mailing list