brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.4 KiB · df2c4b9 Raw
145 lines · plain
1# This function merges multiple objects into a single relocatable object2#                     cc -r obj1.o obj2.o -o obj.o3# A relocatable object is an object file that is not fully linked into an4# executable or a shared library. It is an intermediate file format that can5# be passed into the linker.6# A crt object has arch-specific code and arch-agnostic code. To reduce code7# duplication, the implementation is split into multiple units. As a result,8# we need to merge them into a single relocatable object.9# See also:  https://maskray.me/blog/2022-11-21-relocatable-linking10function(merge_relocatable_object name)11  set(obj_list "")12  set(fq_link_libraries "")13  get_fq_deps_list(fq_dep_list ${ARGN})14  foreach(target IN LISTS fq_dep_list)15    list(APPEND obj_list "$<TARGET_OBJECTS:${target}>")16    get_target_property(libs ${target} DEPS)17    list(APPEND fq_link_libraries "${libs}")18  endforeach()19  list(REMOVE_DUPLICATES obj_list)20  list(REMOVE_DUPLICATES fq_link_libraries)21  get_fq_target_name(${name} fq_name)22  set(relocatable_target "${fq_name}.__relocatable__")23  add_executable(24    ${relocatable_target}25    ${obj_list}26  )27  # Pass -r to the driver is much cleaner than passing -Wl,-r: the compiler knows it is28  # a relocatable linking and will not pass other irrelevant flags to the linker.29  set(link_opts -r -nostdlib)30  if (explicit_target_triple AND LLVM_ENABLE_LLD)31    list(APPEND link_opts --target=${explicit_target_triple})32  endif()33  target_link_options(${relocatable_target} PRIVATE ${link_opts})34  set_target_properties(35    ${relocatable_target}36    PROPERTIES37      RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}38      OUTPUT_NAME ${name}.o39  )40  add_library(${fq_name} OBJECT IMPORTED GLOBAL)41  add_dependencies(${fq_name} ${relocatable_target})42  target_link_libraries(${fq_name} INTERFACE ${fq_link_libraries})43  set_target_properties(44    ${fq_name}45    PROPERTIES46      LINKER_LANGUAGE CXX47      IMPORTED_OBJECTS ${CMAKE_CURRENT_BINARY_DIR}/${name}.o48      TARGET_TYPE ${OBJECT_LIBRARY_TARGET_TYPE}49      DEPS "${fq_link_libraries}"50  )51endfunction()52 53function(add_startup_object name)54  cmake_parse_arguments(55    "ADD_STARTUP_OBJECT"56    "" # Option argument57    "SRC"   # Single value arguments58    "DEPENDS;COMPILE_OPTIONS" # Multi value arguments59    ${ARGN}60  )61 62  get_fq_target_name(${name} fq_target_name)63 64  add_object_library(65    ${name}66    SRCS ${ADD_STARTUP_OBJECT_SRC}67    DEPENDS ${ADD_STARTUP_OBJECT_DEPENDS}68    COMPILE_OPTIONS ${ADD_STARTUP_OBJECT_COMPILE_OPTIONS}69  )70  set_target_properties(71    ${fq_target_name}72    PROPERTIES73      OUTPUT_NAME ${name}.o74  )75endfunction()76 77check_cxx_compiler_flag("-r" LIBC_LINKER_SUPPORTS_RELOCATABLE)78 79if(NOT LIBC_LINKER_SUPPORTS_RELOCATABLE)80  message(STATUS "Skipping startup for target architecture ${LIBC_TARGET_ARCHITECTURE}: linker does not support -r")81  return()82endif()83 84if(NOT (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE}))85  message(STATUS "Skipping startup for target architecture ${LIBC_TARGET_ARCHITECTURE}")86  return()87endif()88 89add_subdirectory(${LIBC_TARGET_ARCHITECTURE})90 91add_object_library(92  do_start93  SRCS94    do_start.cpp95  HDRS96    do_start.h97  DEPENDS98    libc.config.app_h99    libc.hdr.stdint_proxy100    libc.include.sys_mman101    libc.include.sys_syscall102    libc.include.llvm-libc-macros.link_macros103    libc.src.__support.threads.thread104    libc.src.__support.OSUtil.osutil105    libc.src.stdlib.exit106    libc.src.stdlib.atexit107    libc.src.unistd.environ108    libc.src.__support.OSUtil.linux.auxv109  COMPILE_OPTIONS110    -ffreestanding       # To avoid compiler warnings about calling the main function.111    -fno-builtin         # avoid emit unexpected calls112    -fno-stack-protector # stack protect canary is not available yet.113)114 115# TODO: factor out crt1 into multiple objects116merge_relocatable_object(117  crt1118  .${LIBC_TARGET_ARCHITECTURE}.start119  .${LIBC_TARGET_ARCHITECTURE}.tls120  .do_start121)122 123add_startup_object(124  crti125  SRC126    crti.cpp127)128 129add_startup_object(130  crtn131  SRC132    crtn.cpp133)134 135add_custom_target(libc-startup)136set(startup_components crt1 crti crtn)137foreach(target IN LISTS startup_components)138  set(fq_target_name libc.startup.linux.${target})139  add_dependencies(libc-startup ${fq_target_name})140  install(FILES $<TARGET_OBJECTS:${fq_target_name}>141          DESTINATION ${LIBC_INSTALL_LIBRARY_DIR}142          RENAME $<TARGET_PROPERTY:${fq_target_name},OUTPUT_NAME>143          COMPONENT libc)144endforeach()145