When using a statically linked opt executable (no BUILD_SHARED_LIBS or LLVM_LINK_LLVM_DYLIB), the linker only includes symbols used (directly or transitively) by the opt program itself. However, when loading another module using the "-load" mechanism from Passes/PassPlugin.cpp or Support/PluginLoader.cpp, it may require symbols that have not been included into the executable.
In this specific case, Polly uses the LLVM-IR linker, which is not included in the statically linked opt executable. As a result, some Polly regressions tests may fail with the error message
opt: symbol lookup error: LLVMPolly.so: undefined symbol: _ZN4llvm6LinkerC1ERNS_6ModuleE
(It does work using clang which uses the LLVM linker).
This patch ensures that the LLVMLinker is available in the opt executable by referencing it in one of opt's main object files. The exported symbol ensurePluginSymbols creates a linker object without being called anywhere.
This is a pragmatic fix for Polly and unfortunately does not solve the more general problem that not all symbols are available to plugins in statically linked tools. I am open for better solutions.