[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
Wed Dec 13 16:22:58 PST 2023


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

>From d76bb7ff45ab0aa687b4ec9ddc51f88e95c5d0dc 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/3] [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 b122d1bc22db6e220bab58ac7d50d6f06602837a 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/3] 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 644b22993f3a1c04f783aee03046bf915c18ff09 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/3] 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}



More information about the libc-commits mailing list