@@ -425,19 +425,23 @@ class ModuleLinker {
425425 DiagnosticHandlerFunction DiagnosticHandler;
426426
427427 // / For symbol clashes, prefer those from Src.
428- bool OverrideFromSrc ;
428+ unsigned Flags ;
429429
430430public:
431431 ModuleLinker (Module *dstM, Linker::IdentifiedStructTypeSet &Set, Module *srcM,
432- DiagnosticHandlerFunction DiagnosticHandler,
433- bool OverrideFromSrc)
432+ DiagnosticHandlerFunction DiagnosticHandler, unsigned Flags)
434433 : DstM(dstM), SrcM(srcM), TypeMap(Set),
435434 ValMaterializer (TypeMap, DstM, LazilyLinkGlobalValues),
436- DiagnosticHandler(DiagnosticHandler), OverrideFromSrc(OverrideFromSrc) {
437- }
435+ DiagnosticHandler(DiagnosticHandler), Flags(Flags) {}
438436
439437 bool run ();
440438
439+ bool shouldOverrideFromSrc () { return Flags & Linker::OverrideFromSrc; }
440+ bool shouldLinkOnlyNeeded () { return Flags & Linker::LinkOnlyNeeded; }
441+ bool shouldInternalizeLinkedSymbols () {
442+ return Flags & Linker::InternalizeLinkedSymbols;
443+ }
444+
441445private:
442446 bool shouldLinkFromSource (bool &LinkFromSrc, const GlobalValue &Dest,
443447 const GlobalValue &Src);
@@ -730,7 +734,7 @@ bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
730734 const GlobalValue &Dest,
731735 const GlobalValue &Src) {
732736 // Should we unconditionally use the Src?
733- if (OverrideFromSrc ) {
737+ if (shouldOverrideFromSrc () ) {
734738 LinkFromSrc = true ;
735739 return false ;
736740 }
@@ -1081,13 +1085,20 @@ bool ModuleLinker::linkGlobalValueProto(GlobalValue *SGV) {
10811085 } else {
10821086 // If the GV is to be lazily linked, don't create it just yet.
10831087 // The ValueMaterializerTy will deal with creating it if it's used.
1084- if (!DGV && !OverrideFromSrc &&
1088+ if (!DGV && !shouldOverrideFromSrc () &&
10851089 (SGV->hasLocalLinkage () || SGV->hasLinkOnceLinkage () ||
10861090 SGV->hasAvailableExternallyLinkage ())) {
10871091 DoNotLinkFromSource.insert (SGV);
10881092 return false ;
10891093 }
10901094
1095+ // When we only want to link in unresolved dependencies, blacklist
1096+ // the symbol unless unless DestM has a matching declaration (DGV).
1097+ if (shouldLinkOnlyNeeded () && !(DGV && DGV->isDeclaration ())) {
1098+ DoNotLinkFromSource.insert (SGV);
1099+ return false ;
1100+ }
1101+
10911102 NewGV = copyGlobalValueProto (TypeMap, *DstM, SGV);
10921103
10931104 if (DGV && isa<Function>(DGV))
@@ -1249,6 +1260,9 @@ void ModuleLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) {
12491260bool ModuleLinker::linkGlobalValueBody (GlobalValue &Src) {
12501261 Value *Dst = ValueMap[&Src];
12511262 assert (Dst);
1263+ if (shouldInternalizeLinkedSymbols ())
1264+ if (auto *DGV = dyn_cast<GlobalValue>(Dst))
1265+ DGV->setLinkage (GlobalValue::InternalLinkage);
12521266 if (auto *F = dyn_cast<Function>(&Src))
12531267 return linkFunctionBody (cast<Function>(*Dst), *F);
12541268 if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) {
@@ -1632,6 +1646,11 @@ bool ModuleLinker::run() {
16321646 GlobalValue *SGV = LazilyLinkGlobalValues.back ();
16331647 LazilyLinkGlobalValues.pop_back ();
16341648
1649+ // Skip declarations that ValueMaterializer may have created in
1650+ // case we link in only some of SrcM.
1651+ if (shouldLinkOnlyNeeded () && SGV->isDeclaration ())
1652+ continue ;
1653+
16351654 assert (!SGV->isDeclaration () && " users should not pass down decls" );
16361655 if (linkGlobalValueBody (*SGV))
16371656 return true ;
@@ -1759,9 +1778,9 @@ void Linker::deleteModule() {
17591778 Composite = nullptr ;
17601779}
17611780
1762- bool Linker::linkInModule (Module *Src, bool OverrideSymbols ) {
1781+ bool Linker::linkInModule (Module *Src, unsigned Flags ) {
17631782 ModuleLinker TheLinker (Composite, IdentifiedStructTypes, Src,
1764- DiagnosticHandler, OverrideSymbols );
1783+ DiagnosticHandler, Flags );
17651784 bool RetCode = TheLinker.run ();
17661785 Composite->dropTriviallyDeadConstantArrays ();
17671786 return RetCode;
@@ -1781,14 +1800,15 @@ void Linker::setModule(Module *Dst) {
17811800// / Upon failure, the Dest module could be in a modified state, and shouldn't be
17821801// / relied on to be consistent.
17831802bool Linker::LinkModules (Module *Dest, Module *Src,
1784- DiagnosticHandlerFunction DiagnosticHandler) {
1803+ DiagnosticHandlerFunction DiagnosticHandler,
1804+ unsigned Flags) {
17851805 Linker L (Dest, DiagnosticHandler);
1786- return L.linkInModule (Src);
1806+ return L.linkInModule (Src, Flags );
17871807}
17881808
1789- bool Linker::LinkModules (Module *Dest, Module *Src) {
1809+ bool Linker::LinkModules (Module *Dest, Module *Src, unsigned Flags ) {
17901810 Linker L (Dest);
1791- return L.linkInModule (Src);
1811+ return L.linkInModule (Src, Flags );
17921812}
17931813
17941814// ===----------------------------------------------------------------------===//
0 commit comments