From 3eeaffc7c57e2b107e165cf4f5f367935e2f2ce4 Mon Sep 17 00:00:00 2001 From: Bert Maher Date: Mon, 9 Aug 2021 11:22:24 -0700 Subject: [PATCH] Linker version script to hide LLVM symbols (#62906) Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/62906 Test Plan: Imported from OSS Reviewed By: navahgar Differential Revision: D30193893 Pulled By: bertmaher fbshipit-source-id: 9b189bfd8d4c52e8dc4296a4bed517ff44994ba0 --- caffe2/CMakeLists.txt | 9 +++++++++ caffe2/unexported_symbols.lds | 2 ++ caffe2/version_script.lds | 7 +++++++ 3 files changed, 18 insertions(+) create mode 100644 caffe2/unexported_symbols.lds create mode 100644 caffe2/version_script.lds diff --git a/caffe2/CMakeLists.txt b/caffe2/CMakeLists.txt index 32b004fd83b..b41520c5773 100644 --- a/caffe2/CMakeLists.txt +++ b/caffe2/CMakeLists.txt @@ -797,6 +797,15 @@ if(USE_LLVM AND LLVM_FOUND) support core analysis executionengine instcombine scalaropts transformutils ${LLVM_TARGETS_TO_BUILD} orcjit) target_link_libraries(torch_cpu PRIVATE ${LLVM_LINK_LIBS}) + if(APPLE) + set(LINKER_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/unexported_symbols.lds") + set_target_properties(torch_cpu PROPERTIES LINK_DEPENDS ${LINKER_SCRIPT}) + set_target_properties(torch_cpu PROPERTIES LINK_FLAGS "-Wl,-unexported_symbols_list,${LINKER_SCRIPT}") + elseif(UNIX) + set(LINKER_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/version_script.lds") + set_target_properties(torch_cpu PROPERTIES LINK_DEPENDS ${LINKER_SCRIPT}) + set_target_properties(torch_cpu PROPERTIES LINK_FLAGS "-Wl,--version-script=${LINKER_SCRIPT}") + endif() endif(USE_LLVM AND LLVM_FOUND) # This is required for older versions of CMake, which don't allow diff --git a/caffe2/unexported_symbols.lds b/caffe2/unexported_symbols.lds new file mode 100644 index 00000000000..7c05d205ae7 --- /dev/null +++ b/caffe2/unexported_symbols.lds @@ -0,0 +1,2 @@ +__ZN4llvm* +__ZNK4llvm* diff --git a/caffe2/version_script.lds b/caffe2/version_script.lds new file mode 100644 index 00000000000..900c6b87832 --- /dev/null +++ b/caffe2/version_script.lds @@ -0,0 +1,7 @@ +pytorch { + global: + *; + local: + _ZN4llvm*; + _ZNK4llvm*; +};