From d26a8daa491ce56ef719708f2dbefb519c4c2f3e Mon Sep 17 00:00:00 2001 From: David Tenty Date: Fri, 8 May 2020 13:45:44 -0400 Subject: [PATCH] [AIX] Make sure we use export lists for plugins Summary: Besides just generating and consuming the lists, this includes: * Calling nm with the right options in extract_symbols.py. Such as not demangling C++ names, which AIX nm does by default, and accepting both 32/64-bit names. * Not having nm sort the list of symbols or we may run in to memory issues on debug builds, as nm calls a 32-bit sort. * Defaulting to having LLVM_EXPORT_SYMBOLS_FOR_PLUGINS on for AIX * CMake versions prior to 3.16 set the -brtl linker flag globally on AIX. Clear it out early on so we don't run into failures. We will set it as needed. Reviewers: jasonliu, DiggerLin, stevewan, hubert.reinterpretcast Reviewed By: hubert.reinterpretcast Subscribers: hubert.reinterpretcast, mgorny, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D70972 --- llvm/CMakeLists.txt | 24 ++++++++++++++++++++++++ llvm/cmake/modules/AddLLVM.cmake | 6 ++++++ llvm/cmake/modules/HandleLLVMOptions.cmake | 20 ++++++++++++++++---- llvm/utils/extract_symbols.py | 11 ++++++++--- 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt index cc86e5a..3fbe1e6 100644 --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -874,6 +874,30 @@ endif() if (UNIX AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX") add_definitions("-D_XOPEN_SOURCE=700") add_definitions("-D_LARGE_FILE_API") + + # CMake versions less than 3.16 set default linker flags to include -brtl, as + # well as setting -G when building libraries, so clear them out. Note we only + # try to clear the form that CMake will set as part of its initial + # configuration, it is still possible the user may force it as part of a + # compound option. + if(CMAKE_VERSION VERSION_LESS 3.16) + string(REGEX REPLACE "(^|[ \t]+)-Wl,-brtl([ \t]+|$)" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") + string(REGEX REPLACE "(^|[ \t]+)-Wl,-brtl([ \t]+|$)" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") + string(REGEX REPLACE "(^|[ \t]+)-Wl,-brtl([ \t]+|$)" "" CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS}") + string(REGEX REPLACE "(^|[ \t]+)(-Wl,)?-G([ \t]+|$)" "" CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS + "${CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS}") + string(REGEX REPLACE "(^|[ \t]+)(-Wl,)?-G([ \t]+|$)" "" CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS + "${CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS}") + string(REGEX REPLACE "(^|[ \t]+)(-Wl,)?-G([ \t]+|$)" "" CMAKE_SHARED_LIBRARY_CREATE_ASM_FLAGS + "${CMAKE_SHARED_LIBRARY_CREATE_ASM_FLAGS}") + endif() + + # Modules should be built with -G, so we can use runtime linking with + # plugins. + string(APPEND CMAKE_MODULE_LINKER_FLAGS " -G") + + # Also set the correct flags for building shared libraries. + string(APPEND CMAKE_SHARED_LINKER_FLAGS " -shared") endif() # Build with _FILE_OFFSET_BITS=64 on Solaris to match g++ >= 9. diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake index f40d998..7f8e413 100644 --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -90,6 +90,7 @@ function(add_llvm_symbol_exports target_name export_file) set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,-exported_symbols_list,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"") elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX") + set(native_export_file "${export_file}") set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,-bE:${export_file}") elseif(LLVM_HAVE_LINK_VERSION_SCRIPT) @@ -257,6 +258,11 @@ function(add_link_opts target_name) endif() endif() endif() + + if(ARG_SUPPORT_PLUGINS AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX") + set_property(TARGET ${target_name} APPEND_STRING PROPERTY + LINK_FLAGS " -Wl,-brtl") + endif() endfunction(add_link_opts) # Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}. diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake index b76300d..7e61289 100644 --- a/llvm/cmake/modules/HandleLLVMOptions.cmake +++ b/llvm/cmake/modules/HandleLLVMOptions.cmake @@ -11,6 +11,7 @@ include(HandleLLVMStdlib) include(CheckCCompilerFlag) include(CheckCXXCompilerFlag) include(CheckSymbolExists) +include(CMakeDependentOption) if(CMAKE_LINKER MATCHES "lld-link" OR (WIN32 AND LLVM_USE_LINKER STREQUAL "lld") OR LLVM_ENABLE_LLD) set(LINKER_IS_LLD_LINK TRUE) @@ -971,12 +972,23 @@ elseif(LLVM_ENABLE_LTO) endif() endif() +# Set an AIX default for LLVM_EXPORT_SYMBOLS_FOR_PLUGINS based on whether we are +# doing dynamic linking (see below). +set(LLVM_EXPORT_SYMBOLS_FOR_PLUGINS_AIX_default OFF) +if (NOT (BUILD_SHARED_LIBS OR LLVM_LINK_LLVM_DYLIB)) + set(LLVM_EXPORT_SYMBOLS_FOR_PLUGINS_AIX_default ON) +endif() + # This option makes utils/extract_symbols.py be used to determine the list of -# symbols to export from LLVM tools. This is necessary when using MSVC if you -# want to allow plugins, though note that the plugin has to explicitly link -# against (exactly one) tool so we can't unilaterally turn on +# symbols to export from LLVM tools. This is necessary when on AIX or when using +# MSVC if you want to allow plugins. On AIX we don't show this option, and we +# enable it by default except when the LLVM libraries are set up for dynamic +# linking (due to incompatibility). With MSVC, note that the plugin has to +# explicitly link against (exactly one) tool so we can't unilaterally turn on # LLVM_ENABLE_PLUGINS when it's enabled. -option(LLVM_EXPORT_SYMBOLS_FOR_PLUGINS "Export symbols from LLVM tools so that plugins can import them" OFF) +CMAKE_DEPENDENT_OPTION(LLVM_EXPORT_SYMBOLS_FOR_PLUGINS + "Export symbols from LLVM tools so that plugins can import them" OFF + "NOT ${CMAKE_SYSTEM_NAME} MATCHES AIX" ${LLVM_EXPORT_SYMBOLS_FOR_PLUGINS_AIX_default}) if(BUILD_SHARED_LIBS AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS) message(FATAL_ERROR "BUILD_SHARED_LIBS not compatible with LLVM_EXPORT_SYMBOLS_FOR_PLUGINS") endif() diff --git a/llvm/utils/extract_symbols.py b/llvm/utils/extract_symbols.py index 7cdcce1..43f6039 100755 --- a/llvm/utils/extract_symbols.py +++ b/llvm/utils/extract_symbols.py @@ -42,9 +42,14 @@ def dumpbin_get_symbols(lib): process.wait() def nm_get_symbols(lib): - process = subprocess.Popen(['nm','-P',lib], bufsize=1, - stdout=subprocess.PIPE, stdin=subprocess.PIPE, - universal_newlines=True) + if sys.platform.startswith('aix'): + process = subprocess.Popen(['nm','-P','-Xany','-C','-p',lib], bufsize=1, + stdout=subprocess.PIPE, stdin=subprocess.PIPE, + universal_newlines=True) + else: + process = subprocess.Popen(['nm','-P',lib], bufsize=1, + stdout=subprocess.PIPE, stdin=subprocess.PIPE, + universal_newlines=True) process.stdin.close() for line in process.stdout: # Look for external symbols that are defined in some section -- 2.7.4