From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Reilly Brogan <reilly@reillybrogan.com>
Date: Sun, 26 May 2024 16:08:54 -0500
Subject: [PATCH] Emit relocations for BOLT

---
 clang/tools/clang-shlib/CMakeLists.txt | 2 ++
 clang/tools/driver/CMakeLists.txt      | 2 ++
 clang/tools/libclang/CMakeLists.txt    | 3 +++
 lld/Common/CMakeLists.txt              | 2 ++
 lld/ELF/CMakeLists.txt                 | 2 ++
 lld/tools/lld/CMakeLists.txt           | 2 ++
 llvm/tools/llvm-shlib/CMakeLists.txt   | 2 ++
 llvm/tools/lto/CMakeLists.txt          | 2 ++
 polly/lib/CMakeLists.txt               | 2 ++
 polly/lib/External/CMakeLists.txt      | 2 ++
 10 files changed, 21 insertions(+)

diff --git a/clang/tools/clang-shlib/CMakeLists.txt b/clang/tools/clang-shlib/CMakeLists.txt
index 298d3a9d18fe..a8574bac5b16 100644
--- a/clang/tools/clang-shlib/CMakeLists.txt
+++ b/clang/tools/clang-shlib/CMakeLists.txt
@@ -53,6 +53,8 @@ add_clang_library(clang-cpp
 if (NOT APPLE AND NOT MINGW AND NOT LLVM_LINKER_IS_SOLARISLD_ILLUMOS)
   target_link_options(clang-cpp PRIVATE LINKER:-Bsymbolic-functions)
 endif()
+# We need to emit relocations to BOLT this library on Solus
+target_link_options(clang-cpp PRIVATE LINKER:--emit-relocs)
 if (MINGW OR CYGWIN)
   # The clang-cpp DLL is supposed to export all symbols (except for ones
   # that are explicitly hidden). Normally, this is what happens anyway, but
diff --git a/clang/tools/driver/CMakeLists.txt b/clang/tools/driver/CMakeLists.txt
index 290bf2a42536..a46d0df0d5df 100644
--- a/clang/tools/driver/CMakeLists.txt
+++ b/clang/tools/driver/CMakeLists.txt
@@ -132,3 +132,5 @@ if(CLANG_ORDER_FILE AND
     set_target_properties(clang PROPERTIES LINK_DEPENDS ${CLANG_ORDER_FILE})
   endif()
 endif()
+
+target_link_options(clang PRIVATE LINKER:--emit-relocs)
diff --git a/clang/tools/libclang/CMakeLists.txt b/clang/tools/libclang/CMakeLists.txt
index 7b634003d11f..c3107915a707 100644
--- a/clang/tools/libclang/CMakeLists.txt
+++ b/clang/tools/libclang/CMakeLists.txt
@@ -213,6 +213,9 @@ if(ENABLE_SHARED)
   endif()
 endif()
 
+# We need to emit relocations to BOLT this library
+target_link_options(libclang PRIVATE LINKER:--emit-relocs)
+
 if(INTERNAL_INSTALL_PREFIX)
   set(LIBCLANG_HEADERS_INSTALL_DESTINATION "${INTERNAL_INSTALL_PREFIX}/include")
 else()
diff --git a/lld/Common/CMakeLists.txt b/lld/Common/CMakeLists.txt
index 4f503d04f784..fdd3dc199cb5 100644
--- a/lld/Common/CMakeLists.txt
+++ b/lld/Common/CMakeLists.txt
@@ -58,3 +58,5 @@ add_lld_library(lldCommon
   DEPENDS
   intrinsics_gen
   )
+
+target_link_options(lldCommon PRIVATE LINKER:--emit-relocs)
diff --git a/lld/ELF/CMakeLists.txt b/lld/ELF/CMakeLists.txt
index 83d816ddb060..4cce3dbf0bbc 100644
--- a/lld/ELF/CMakeLists.txt
+++ b/lld/ELF/CMakeLists.txt
@@ -85,3 +85,5 @@ add_lld_library(lldELF
   ELFOptionsTableGen
   intrinsics_gen
   )
+
+target_link_options(lldELF PRIVATE LINKER:--emit-relocs)
diff --git a/lld/tools/lld/CMakeLists.txt b/lld/tools/lld/CMakeLists.txt
index 8498a91597a9..3e7b95c3953a 100644
--- a/lld/tools/lld/CMakeLists.txt
+++ b/lld/tools/lld/CMakeLists.txt
@@ -46,3 +46,5 @@ endforeach()
 if(LLVM_TOOL_LLVM_DRIVER_BUILD)
   set_property(GLOBAL APPEND PROPERTY LLVM_DRIVER_HIDDEN_TOOL_ALIASES_lld ld)
 endif()
+
+target_link_options(lld PRIVATE LINKER:--emit-relocs)
diff --git a/llvm/tools/llvm-shlib/CMakeLists.txt b/llvm/tools/llvm-shlib/CMakeLists.txt
index b20ac318e768..4b5c19ffbc4b 100644
--- a/llvm/tools/llvm-shlib/CMakeLists.txt
+++ b/llvm/tools/llvm-shlib/CMakeLists.txt
@@ -61,6 +61,8 @@ if(LLVM_BUILD_LLVM_DYLIB)
       # Note: for -fno-pic default, the address of a function may be different from
       # inside and outside libLLVM.so.
       target_link_options(LLVM PRIVATE LINKER:-Bsymbolic-functions)
+      # We need to emit relocations to BOLT this library on Solus
+      target_link_options(LLVM PRIVATE LINKER:--emit-relocs)
     endif()
   endif()
 
diff --git a/llvm/tools/lto/CMakeLists.txt b/llvm/tools/lto/CMakeLists.txt
index 67f6d3af40e0..64c3b48d4c6b 100644
--- a/llvm/tools/lto/CMakeLists.txt
+++ b/llvm/tools/lto/CMakeLists.txt
@@ -32,6 +32,8 @@ endif()
 add_llvm_library(${LTO_LIBRARY_NAME} ${LTO_LIBRARY_TYPE} INSTALL_WITH_TOOLCHAIN
     ${SOURCES} DEPENDS intrinsics_gen)
 
+target_link_options(${LTO_LIBRARY_NAME} PRIVATE LINKER:--emit-relocs)
+
 install(FILES ${LLVM_MAIN_INCLUDE_DIR}/llvm-c/lto.h
   DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/llvm-c"
   COMPONENT LTO)
diff --git a/polly/lib/CMakeLists.txt b/polly/lib/CMakeLists.txt
index d91f4ecd37e6..30b93c65df7b 100644
--- a/polly/lib/CMakeLists.txt
+++ b/polly/lib/CMakeLists.txt
@@ -131,6 +131,8 @@ else ()
   # the same command line options, to which LLVM reacts with an error.
   target_link_libraries(LLVMPolly PUBLIC ${ISL_TARGET})
 
+  target_link_options(LLVMPolly PRIVATE LINKER:--emit-relocs)
+
   set_target_properties(LLVMPolly
     PROPERTIES
     LINKER_LANGUAGE CXX
diff --git a/polly/lib/External/CMakeLists.txt b/polly/lib/External/CMakeLists.txt
index 5dd69b7199dc..69a6975e8d17 100644
--- a/polly/lib/External/CMakeLists.txt
+++ b/polly/lib/External/CMakeLists.txt
@@ -287,6 +287,8 @@ if (POLLY_BUNDLED_ISL)
     ${ISL_FILES}
     )
 
+  target_link_options(PollyISL PRIVATE LINKER:--emit-relocs)
+
 
   if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
     install(DIRECTORY
