aboutsummaryrefslogtreecommitdiff
path: root/libc/startup
diff options
context:
space:
mode:
authorSchrodinger ZHU Yifan <yifanzhu@rochester.edu>2023-12-20 11:18:51 -0500
committerGitHub <noreply@github.com>2023-12-20 08:18:51 -0800
commit8bbeed05c4da832a2abf532fee8665c0d6576cbc (patch)
tree54ca161edf3fc736d29da437ff6cfc3c534a7b34 /libc/startup
parente4f4022b7044b135a950a5fdc033af13fee9f6fa (diff)
downloadllvm-8bbeed05c4da832a2abf532fee8665c0d6576cbc.zip
llvm-8bbeed05c4da832a2abf532fee8665c0d6576cbc.tar.gz
llvm-8bbeed05c4da832a2abf532fee8665c0d6576cbc.tar.bz2
[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.
Diffstat (limited to 'libc/startup')
-rw-r--r--libc/startup/linux/CMakeLists.txt71
1 files changed, 60 insertions, 11 deletions
diff --git a/libc/startup/linux/CMakeLists.txt b/libc/startup/linux/CMakeLists.txt
index 007aa30..d81c7db 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(