Index: lib/LTO/LTOCodeGenerator.cpp =================================================================== --- lib/LTO/LTOCodeGenerator.cpp +++ lib/LTO/LTOCodeGenerator.cpp @@ -348,8 +348,69 @@ RelocModel, CodeModel::Default, CGOptLevel)); } +// If a linkonce global is present in the MustPreserveSymbols, we need to make +// sure we honor this. To force the compiler to not drop it, we add it to the +// "llvm.used" global. +static void preserveDiscardableGV( + Module &TheModule, + const std::function &MustPreserveGV) { + DenseSet UsedValuesSet; + if (GlobalVariable *LLVMUsed = TheModule.getGlobalVariable("llvm.used")) { + ConstantArray *Inits = cast(LLVMUsed->getInitializer()); + for (auto &V : Inits->operands()) + UsedValuesSet.insert(cast(&V)); + LLVMUsed->eraseFromParent(); + } + llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(TheModule.getContext()); + auto mayPreserveGlobal = [&](GlobalValue &GV) { + if (!GV.isDiscardableIfUnused() || GV.isDeclaration()) + return; + if (!MustPreserveGV(GV)) + return; + assert(!GV.hasAvailableExternallyLinkage() && !GV.hasInternalLinkage()); + UsedValuesSet.insert(ConstantExpr::getBitCast(&GV, i8PTy)); + }; + for (auto &GV : TheModule) + mayPreserveGlobal(GV); + for (auto &GV : TheModule.globals()) + mayPreserveGlobal(GV); + for (auto &GV : TheModule.aliases()) + mayPreserveGlobal(GV); + + if (UsedValuesSet.empty()) + return; + std::vector UsedValuesList(UsedValuesSet.begin(), + UsedValuesSet.end()); + + llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedValuesList.size()); + auto *LLVMUsed = new llvm::GlobalVariable( + TheModule, ATy, false, llvm::GlobalValue::AppendingLinkage, + llvm::ConstantArray::get(ATy, UsedValuesList), "llvm.used"); + LLVMUsed->setSection("llvm.metadata"); +} + void LTOCodeGenerator::applyScopeRestrictions() { - if (ScopeRestrictionsDone || !ShouldInternalize) + if (ScopeRestrictionsDone) + + // Declare a callback for the internalize pass that will ask for every + // candidate GlobalValue if it can be internalized or not. + Mangler Mangler; + SmallString<64> MangledName; + auto MustPreserveGV = [&](const GlobalValue &GV) -> bool { + // Need to mangle the GV as the "MustPreserveSymbols" StringSet is filled + // with the linker supplied name, which on Darwin includes a leading + // underscore. + MangledName.clear(); + MangledName.reserve(GV.getName().size() + 1); + Mangler::getNameWithPrefix(MangledName, GV.getName(), + MergedModule->getDataLayout()); + return MustPreserveSymbols.count(MangledName); + }; + + // Preserve linkonce value on linker request + preserveDiscardableGV(*MergedModule, MustPreserveGV); + + if (!ShouldInternalize) return; if (ShouldRestoreGlobalsLinkage) { @@ -373,21 +434,6 @@ // symbols referenced from asm UpdateCompilerUsed(*MergedModule, *TargetMach, AsmUndefinedRefs); - // Declare a callback for the internalize pass that will ask for every - // candidate GlobalValue if it can be internalized or not. - Mangler Mangler; - SmallString<64> MangledName; - auto MustPreserveGV = [&](const GlobalValue &GV) -> bool { - // Need to mangle the GV as the "MustPreserveSymbols" StringSet is filled - // with the linker supplied name, which on Darwin includes a leading - // underscore. - MangledName.clear(); - MangledName.reserve(GV.getName().size() + 1); - Mangler::getNameWithPrefix(MangledName, GV.getName(), - MergedModule->getDataLayout()); - return MustPreserveSymbols.count(MangledName); - }; - internalizeModule(*MergedModule, MustPreserveGV); ScopeRestrictionsDone = true; Index: test/tools/lto/hide-linkonce-odr.ll =================================================================== --- test/tools/lto/hide-linkonce-odr.ll +++ test/tools/lto/hide-linkonce-odr.ll @@ -1,9 +1,11 @@ ; RUN: llvm-as %s -o %t.o -; RUN: %ld64 -lto_library %llvmshlibdir/libLTO.dylib -dylib -arch x86_64 -macosx_version_min 10.10.0 -lSystem -o %t.dylib %t.o -save-temps -undefined dynamic_lookup +; RUN: %ld64 -lto_library %llvmshlibdir/libLTO.dylib -dylib -arch x86_64 -macosx_version_min 10.10.0 -lSystem -o %t.dylib %t.o -save-temps -undefined dynamic_lookup -exported_symbol _c -exported_symbol _b ; RUN: llvm-dis %t.dylib.lto.opt.bc -o - | FileCheck --check-prefix=IR %s -; check that @a is still a linkonce_odr definition -; IR: define linkonce_odr void @a() +; check that @a is no longe a linkonce_odr definition +; IR-NOT: define linkonce_odr void @a() +; check that @b is appended in llvm.used +; IR: @llvm.used = appending global [1 x i8*] [i8* bitcast (void ()* @b to i8*)], section "llvm.metadata" ; RUN: llvm-nm %t.dylib | FileCheck --check-prefix=NM %s ; check that the linker can hide @a but not @b