[libc-commits] [libc] [libc] [startup] add cmake function to merge separated crt1 objects (PR #75413)

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Sat Dec 16 13:30:36 PST 2023


https://github.com/SchrodingerZhu updated https://github.com/llvm/llvm-project/pull/75413

>From ab78d8afb01234f74631d5a6761a1bed014db7dd Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Wed, 13 Dec 2023 19:16:34 -0500
Subject: [PATCH 1/7] [libc] [startup] add cmake function to merge separated
 crt1 objects

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.
---
 libc/startup/linux/CMakeLists.txt | 46 ++++++++++++++++++++++++++++---
 1 file changed, 42 insertions(+), 4 deletions(-)

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(

>From 387a9d4cc0856d0aac878db0cdd809fd86174751 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Wed, 13 Dec 2023 19:20:38 -0500
Subject: [PATCH 2/7] fix typo

---
 libc/startup/linux/CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/startup/linux/CMakeLists.txt b/libc/startup/linux/CMakeLists.txt
index 5ca5010f6b1fe4..33bd18e4f126a8 100644
--- a/libc/startup/linux/CMakeLists.txt
+++ b/libc/startup/linux/CMakeLists.txt
@@ -3,7 +3,7 @@
 # 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
+# A crt object has 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)

>From 8cb0b9982f787e304a487393dbce7ded4af3c3d6 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Wed, 13 Dec 2023 19:22:38 -0500
Subject: [PATCH 3/7] remove alias related code

---
 libc/startup/linux/CMakeLists.txt | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/libc/startup/linux/CMakeLists.txt b/libc/startup/linux/CMakeLists.txt
index 33bd18e4f126a8..52b677b49137d8 100644
--- a/libc/startup/linux/CMakeLists.txt
+++ b/libc/startup/linux/CMakeLists.txt
@@ -40,19 +40,14 @@ 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}

>From 8fe53d61fde1bbb3f16b5390edb3877229afcbf9 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Thu, 14 Dec 2023 16:21:10 -0500
Subject: [PATCH 4/7] use dd_executable instead

---
 libc/startup/linux/CMakeLists.txt | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/libc/startup/linux/CMakeLists.txt b/libc/startup/linux/CMakeLists.txt
index 52b677b49137d8..b89eacbf346928 100644
--- a/libc/startup/linux/CMakeLists.txt
+++ b/libc/startup/linux/CMakeLists.txt
@@ -15,14 +15,26 @@ function(merge_relocatable_object name)
     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)
-  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_executable(
+    ${fq_name}.relocatable
+    ${obj_list}
+  )
+  target_link_options(
+    ${fq_name}.relocatable
+    PRIVATE
+      -nostdlib
+      -ffreestanding
+      -no-pie
+      -nostartfiles
+      -Wl,-r)
+  set_target_properties(
+    ${fq_name}.relocatable
+    PROPERTIES
+      OUTPUT_NAME ${name}.o
   )
-  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})
@@ -30,7 +42,6 @@ function(merge_relocatable_object name)
     ${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}"

>From 6f8ab665138fb597ac3286028959f4ded3a33982 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Fri, 15 Dec 2023 11:25:59 -0500
Subject: [PATCH 5/7] fix output path and address CR

---
 libc/startup/linux/CMakeLists.txt | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/libc/startup/linux/CMakeLists.txt b/libc/startup/linux/CMakeLists.txt
index b89eacbf346928..0cfd36f5a1c758 100644
--- a/libc/startup/linux/CMakeLists.txt
+++ b/libc/startup/linux/CMakeLists.txt
@@ -4,7 +4,7 @@
 # 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
-# cohesion, the implementation is splitted into multiple units. As a result,
+# duplication, the implementation is split into multiple units. As a result,
 # we need to merge them into a single relocatable object.
 function(merge_relocatable_object name)
   set(obj_list "")
@@ -18,25 +18,22 @@ function(merge_relocatable_object name)
   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(
-    ${fq_name}.relocatable
+    ${relocatable_target}
     ${obj_list}
   )
-  target_link_options(
-    ${fq_name}.relocatable
-    PRIVATE
-      -nostdlib
-      -ffreestanding
-      -no-pie
-      -nostartfiles
-      -Wl,-r)
+  # Pass -r to the driver is much cleaner than passing -Wl,-r: the compiler knows it is
+  # a relocatable linking and will not passing other irrelevant flags to the linker.
+  target_link_options(${relocatable_target} PRIVATE -r)
   set_target_properties(
-    ${fq_name}.relocatable
+    ${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} ${fq_name}.relocatable)
+  add_dependencies(${fq_name} ${relocatable_target})
   target_link_libraries(${fq_name} INTERFACE ${fq_link_libraries})
   set_target_properties(
     ${fq_name} 
@@ -72,6 +69,13 @@ function(add_startup_object name)
   )
 endfunction()
 
+llvm_check_linker_flag(CXX "-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()

>From 055ae40c2f1765efd895cae11c9737f51f0606b7 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Fri, 15 Dec 2023 11:28:18 -0500
Subject: [PATCH 6/7] fix typo and add reference

---
 libc/startup/linux/CMakeLists.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/startup/linux/CMakeLists.txt b/libc/startup/linux/CMakeLists.txt
index 0cfd36f5a1c758..fe449d736416cf 100644
--- a/libc/startup/linux/CMakeLists.txt
+++ b/libc/startup/linux/CMakeLists.txt
@@ -6,6 +6,7 @@
 # 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 "")
@@ -24,7 +25,7 @@ function(merge_relocatable_object name)
     ${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 passing other irrelevant flags to the linker.
+  # 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}

>From dc5c97d3242a9a78cf631d3bf78cedb721afa189 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Fri, 15 Dec 2023 11:42:05 -0500
Subject: [PATCH 7/7] use cxx_flag to avoid failing

---
 libc/startup/linux/CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/startup/linux/CMakeLists.txt b/libc/startup/linux/CMakeLists.txt
index fe449d736416cf..d81c7db7a558b3 100644
--- a/libc/startup/linux/CMakeLists.txt
+++ b/libc/startup/linux/CMakeLists.txt
@@ -70,7 +70,7 @@ function(add_startup_object name)
   )
 endfunction()
 
-llvm_check_linker_flag(CXX "-r" LIBC_LINKER_SUPPORTS_RELOCATABLE)
+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")



More information about the libc-commits mailing list