diff options
author | Fangrui Song <maskray@google.com> | 2020-02-15 17:23:18 -0800 |
---|---|---|
committer | Fangrui Song <maskray@google.com> | 2020-04-07 15:46:01 -0700 |
commit | d2ef8c1f2ca33457247be26374852573098553c7 (patch) | |
tree | 00728c3ccc87a749c32dd9ad87e9bbfeb88cdcc2 /llvm/lib/LTO/LTOBackend.cpp | |
parent | 2f8fb4d1cde803aee60171ce73a7f5a95868ff84 (diff) | |
download | llvm-d2ef8c1f2ca33457247be26374852573098553c7.zip llvm-d2ef8c1f2ca33457247be26374852573098553c7.tar.gz llvm-d2ef8c1f2ca33457247be26374852573098553c7.tar.bz2 |
[ThinLTO] Drop dso_local if a GlobalVariable satisfies isDeclarationForLinker()
dso_local leads to direct access even if the definition is not within this compilation unit (it is
still in the same linkage unit). On ELF, such a relocation (e.g. R_X86_64_PC32) referencing a
STB_GLOBAL STV_DEFAULT object can cause a linker error in a -shared link.
If the linkage is changed to available_externally, the dso_local flag should be dropped, so that no
direct access will be generated.
The current behavior is benign, because -fpic does not assume dso_local
(clang/lib/CodeGen/CodeGenModule.cpp:shouldAssumeDSOLocal).
If we do that for -fno-semantic-interposition (D73865), there will be an
R_X86_64_PC32 linker error without this patch.
Reviewed By: tejohnson
Differential Revision: https://reviews.llvm.org/D74751
Diffstat (limited to 'llvm/lib/LTO/LTOBackend.cpp')
-rw-r--r-- | llvm/lib/LTO/LTOBackend.cpp | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp index b749909..dbd6f8c 100644 --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -521,7 +521,13 @@ Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream, if (Conf.PreOptModuleHook && !Conf.PreOptModuleHook(Task, Mod)) return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile)); - renameModuleForThinLTO(Mod, CombinedIndex); + // When linking an ELF shared object, dso_local should be dropped. We + // conservatively do this for -fpic. + bool ClearDSOLocalOnDeclarations = + TM->getTargetTriple().isOSBinFormatELF() && + TM->getRelocationModel() != Reloc::Static && + Mod.getPIELevel() == PIELevel::Default; + renameModuleForThinLTO(Mod, CombinedIndex, ClearDSOLocalOnDeclarations); dropDeadSymbols(Mod, DefinedGlobals, CombinedIndex); @@ -547,7 +553,8 @@ Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream, /*IsImporting*/ true); }; - FunctionImporter Importer(CombinedIndex, ModuleLoader); + FunctionImporter Importer(CombinedIndex, ModuleLoader, + ClearDSOLocalOnDeclarations); if (Error Err = Importer.importFunctions(Mod, ImportList).takeError()) return Err; |