@@ -425,19 +425,23 @@ class ModuleLinker {
425
425
DiagnosticHandlerFunction DiagnosticHandler;
426
426
427
427
// / For symbol clashes, prefer those from Src.
428
- bool OverrideFromSrc ;
428
+ unsigned Flags ;
429
429
430
430
public:
431
431
ModuleLinker (Module *dstM, Linker::IdentifiedStructTypeSet &Set, Module *srcM,
432
- DiagnosticHandlerFunction DiagnosticHandler,
433
- bool OverrideFromSrc)
432
+ DiagnosticHandlerFunction DiagnosticHandler, unsigned Flags)
434
433
: DstM(dstM), SrcM(srcM), TypeMap(Set),
435
434
ValMaterializer (TypeMap, DstM, LazilyLinkGlobalValues),
436
- DiagnosticHandler(DiagnosticHandler), OverrideFromSrc(OverrideFromSrc) {
437
- }
435
+ DiagnosticHandler(DiagnosticHandler), Flags(Flags) {}
438
436
439
437
bool run ();
440
438
439
+ bool shouldOverrideFromSrc () { return Flags & Linker::OverrideFromSrc; }
440
+ bool shouldLinkOnlyNeeded () { return Flags & Linker::LinkOnlyNeeded; }
441
+ bool shouldInternalizeLinkedSymbols () {
442
+ return Flags & Linker::InternalizeLinkedSymbols;
443
+ }
444
+
441
445
private:
442
446
bool shouldLinkFromSource (bool &LinkFromSrc, const GlobalValue &Dest,
443
447
const GlobalValue &Src);
@@ -730,7 +734,7 @@ bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
730
734
const GlobalValue &Dest,
731
735
const GlobalValue &Src) {
732
736
// Should we unconditionally use the Src?
733
- if (OverrideFromSrc ) {
737
+ if (shouldOverrideFromSrc () ) {
734
738
LinkFromSrc = true ;
735
739
return false ;
736
740
}
@@ -1081,13 +1085,20 @@ bool ModuleLinker::linkGlobalValueProto(GlobalValue *SGV) {
1081
1085
} else {
1082
1086
// If the GV is to be lazily linked, don't create it just yet.
1083
1087
// The ValueMaterializerTy will deal with creating it if it's used.
1084
- if (!DGV && !OverrideFromSrc &&
1088
+ if (!DGV && !shouldOverrideFromSrc () &&
1085
1089
(SGV->hasLocalLinkage () || SGV->hasLinkOnceLinkage () ||
1086
1090
SGV->hasAvailableExternallyLinkage ())) {
1087
1091
DoNotLinkFromSource.insert (SGV);
1088
1092
return false ;
1089
1093
}
1090
1094
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
+
1091
1102
NewGV = copyGlobalValueProto (TypeMap, *DstM, SGV);
1092
1103
1093
1104
if (DGV && isa<Function>(DGV))
@@ -1249,6 +1260,9 @@ void ModuleLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) {
1249
1260
bool ModuleLinker::linkGlobalValueBody (GlobalValue &Src) {
1250
1261
Value *Dst = ValueMap[&Src];
1251
1262
assert (Dst);
1263
+ if (shouldInternalizeLinkedSymbols ())
1264
+ if (auto *DGV = dyn_cast<GlobalValue>(Dst))
1265
+ DGV->setLinkage (GlobalValue::InternalLinkage);
1252
1266
if (auto *F = dyn_cast<Function>(&Src))
1253
1267
return linkFunctionBody (cast<Function>(*Dst), *F);
1254
1268
if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) {
@@ -1632,6 +1646,11 @@ bool ModuleLinker::run() {
1632
1646
GlobalValue *SGV = LazilyLinkGlobalValues.back ();
1633
1647
LazilyLinkGlobalValues.pop_back ();
1634
1648
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
+
1635
1654
assert (!SGV->isDeclaration () && " users should not pass down decls" );
1636
1655
if (linkGlobalValueBody (*SGV))
1637
1656
return true ;
@@ -1759,9 +1778,9 @@ void Linker::deleteModule() {
1759
1778
Composite = nullptr ;
1760
1779
}
1761
1780
1762
- bool Linker::linkInModule (Module *Src, bool OverrideSymbols ) {
1781
+ bool Linker::linkInModule (Module *Src, unsigned Flags ) {
1763
1782
ModuleLinker TheLinker (Composite, IdentifiedStructTypes, Src,
1764
- DiagnosticHandler, OverrideSymbols );
1783
+ DiagnosticHandler, Flags );
1765
1784
bool RetCode = TheLinker.run ();
1766
1785
Composite->dropTriviallyDeadConstantArrays ();
1767
1786
return RetCode;
@@ -1781,14 +1800,15 @@ void Linker::setModule(Module *Dst) {
1781
1800
// / Upon failure, the Dest module could be in a modified state, and shouldn't be
1782
1801
// / relied on to be consistent.
1783
1802
bool Linker::LinkModules (Module *Dest, Module *Src,
1784
- DiagnosticHandlerFunction DiagnosticHandler) {
1803
+ DiagnosticHandlerFunction DiagnosticHandler,
1804
+ unsigned Flags) {
1785
1805
Linker L (Dest, DiagnosticHandler);
1786
- return L.linkInModule (Src);
1806
+ return L.linkInModule (Src, Flags );
1787
1807
}
1788
1808
1789
- bool Linker::LinkModules (Module *Dest, Module *Src) {
1809
+ bool Linker::LinkModules (Module *Dest, Module *Src, unsigned Flags ) {
1790
1810
Linker L (Dest);
1791
- return L.linkInModule (Src);
1811
+ return L.linkInModule (Src, Flags );
1792
1812
}
1793
1813
1794
1814
// ===----------------------------------------------------------------------===//
0 commit comments