[libc-commits] [libc] [libc] [startup] add cmake function to merge separated crt1 objects (PR #75413)
via libc-commits
libc-commits at lists.llvm.org
Wed Dec 13 16:19:00 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Schrodinger ZHU Yifan (SchrodingerZhu)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/75413.diff
1 Files Affected:
- (modified) libc/startup/linux/CMakeLists.txt (+42-4)
``````````diff
diff --git a/libc/startup/linux/CMakeLists.txt b/libc/startup/linux/CMakeLists.txt
index 007aa30c17d6ab..5ca5010f6b1fe4 100644
--- a/libc/startup/linux/CMakeLists.txt
+++ b/libc/startup/linux/CMakeLists.txt
@@ -1,3 +1,42 @@
+# 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 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.
+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()
+ get_fq_target_name(${name} fq_name)
+ add_custom_command(
+ OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}.o
+ COMMAND ${CMAKE_CXX_COMPILER} -r ${obj_list} -o ${CMAKE_CURRENT_BINARY_DIR}/${name}.o
+ DEPENDS ${obj_list}
+ COMMAND_EXPAND_LISTS
+ )
+ add_custom_target(${fq_name}.relocatable DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}.o)
+ add_library(${fq_name} OBJECT IMPORTED GLOBAL)
+ add_dependencies(${fq_name} ${fq_name}.relocatable)
+ target_link_libraries(${fq_name} INTERFACE ${fq_link_libraries})
+ set_target_properties(
+ ${fq_name}
+ PROPERTIES
+ LINKER_LANGUAGE CXX
+ OBJECT_FILES ${CMAKE_CURRENT_BINARY_DIR}/${name}.o
+ 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"
@@ -34,11 +73,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(
``````````
</details>
https://github.com/llvm/llvm-project/pull/75413
More information about the libc-commits
mailing list