-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
[libc] [startup] add cmake function to merge separated crt1 objects #75413
Conversation
@llvm/pr-subscribers-libc Author: Schrodinger ZHU Yifan (SchrodingerZhu) ChangesAs part of startup refactoring, this patch adds a function to merge multiple objects into a single relocatable object: 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:
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(
|
@jhuber6 unfortunately, |
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.
b1dee6c
to
dc5c97d
Compare
Call for a merge if no further objection. |
ah, @SchrodingerZhu it looks like this broke the post submit bots:
|
So it looks like
Can you see if this can be fixed forward quickly? Otherwise I can revert. I suspect |
I actually had full build on locally. Inspecting the log, I think it is due to the unaligned behavior of the compilers. I suppose adding |
@nickdesaulniers maybe revert it for now? I cannot work on this right now. I can get to this 2-3 hours later. |
Never mind. My meeting was canceled just now. I can submit a quick fix attempt with -nostdlib. |
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.