Skip to content

Commit 52a272c

Browse files
authored
Merge pull request #1314 from domcharrier/fix/hide-non-api-symbols-gcc-clang
[FIX] Hide non-API symbols when CXX compiler supports GNUC style visibility controls to prevent LLVM symbol export and interposition
2 parents 7c39993 + b9994b8 commit 52a272c

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

ffi/CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ include(GNUInstallDirs)
1313

1414
set(CMAKE_CXX_STANDARD 17)
1515

16+
# Preset function visibility to hidden, API functions
17+
# are annotated explicitly to use 'default' visibility.
18+
set(CMAKE_CXX_VISIBILITY_PRESET "hidden")
19+
set(CMAKE_C_VISIBILITY_PRESET "hidden")
20+
1621
find_package(LLVM REQUIRED CONFIG)
1722

1823
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
@@ -316,7 +321,6 @@ endif()
316321
message(STATUS "LLVM target link libraries: ${llvm_libs}")
317322
target_link_libraries(llvmlite ${llvm_libs})
318323

319-
320324
# -flto and --exclude-libs allow removal of unused parts of LLVM
321325
# TODO: these options are just set, they should really be tested for
322326
# suitability.
@@ -328,6 +332,12 @@ if(UNIX AND NOT APPLE)
328332
if (LLVMLITE_FLTO)
329333
STRING(APPEND FORCED_LINK_FLAGS " -flto")
330334
endif()
335+
if(NOT LLVMLITE_SHARED)
336+
# If LLVM is statically linked, we specify -Bsymbolic to prevent
337+
# that LLVM symbols can be interposed upon by previously
338+
# loaded shared objects that export LLVM symbols.
339+
STRING(APPEND FORCED_LINK_FLAGS " -Wl,-Bsymbolic")
340+
endif()
331341
set_property(TARGET llvmlite APPEND_STRING PROPERTY LINK_FLAGS
332342
"${FORCED_LINK_FLAGS}")
333343
elseif(APPLE)

ffi/core.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#if defined(HAVE_DECLSPEC_DLL)
1818
#define API_EXPORT(RTYPE) __declspec(dllexport) RTYPE
1919
#else
20-
#define API_EXPORT(RTYPE) RTYPE
20+
// Non-windows: use visibility attributes.
21+
#define API_EXPORT(RTYPE) __attribute__((visibility("default"))) RTYPE
2122
#endif
2223

2324
extern "C" {

0 commit comments

Comments
 (0)