1 //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the LLVM module linker.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "LinkDiagnosticInfo.h"
15 #include "llvm-c/Linker.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/StringSet.h"
18 #include "llvm/IR/DiagnosticPrinter.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/Linker/Linker.h"
21 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
22 using namespace llvm;
23 
24 namespace {
25 
26 /// This is an implementation class for the LinkModules function, which is the
27 /// entrypoint for this file.
28 class ModuleLinker {
29   IRMover &Mover;
30   std::unique_ptr<Module> SrcM;
31 
32   SetVector<GlobalValue *> ValuesToLink;
33   StringSet<> Internalize;
34 
35   /// For symbol clashes, prefer those from Src.
36   unsigned Flags;
37 
38   /// Functions to import from source module, all other functions are
39   /// imported as declarations instead of definitions.
40   DenseSet<const GlobalValue *> *GlobalsToImport;
41 
42   /// Used as the callback for lazy linking.
43   /// The mover has just hit GV and we have to decide if it, and other members
44   /// of the same comdat, should be linked. Every member to be linked is passed
45   /// to Add.
46   void addLazyFor(GlobalValue &GV, IRMover::ValueAdder Add);
47 
48   bool shouldOverrideFromSrc() { return Flags & Linker::OverrideFromSrc; }
49   bool shouldLinkOnlyNeeded() { return Flags & Linker::LinkOnlyNeeded; }
50   bool shouldInternalizeLinkedSymbols() {
51     return Flags & Linker::InternalizeLinkedSymbols;
52   }
53 
54   bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest,
55                             const GlobalValue &Src);
56 
57   /// Should we have mover and linker error diag info?
58   bool emitError(const Twine &Message) {
59     SrcM->getContext().diagnose(LinkDiagnosticInfo(DS_Error, Message));
60     return true;
61   }
62 
63   bool getComdatLeader(Module &M, StringRef ComdatName,
64                        const GlobalVariable *&GVar);
65   bool computeResultingSelectionKind(StringRef ComdatName,
66                                      Comdat::SelectionKind Src,
67                                      Comdat::SelectionKind Dst,
68                                      Comdat::SelectionKind &Result,
69                                      bool &LinkFromSrc);
70   std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>>
71       ComdatsChosen;
72   bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK,
73                        bool &LinkFromSrc);
74   // Keep track of the lazy linked global members of each comdat in source.
75   DenseMap<const Comdat *, std::vector<GlobalValue *>> LazyComdatMembers;
76 
77   /// Given a global in the source module, return the global in the
78   /// destination module that is being linked to, if any.
79   GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
80     Module &DstM = Mover.getModule();
81     // If the source has no name it can't link.  If it has local linkage,
82     // there is no name match-up going on.
83     if (!SrcGV->hasName() || GlobalValue::isLocalLinkage(SrcGV->getLinkage()))
84       return nullptr;
85 
86     // Otherwise see if we have a match in the destination module's symtab.
87     GlobalValue *DGV = DstM.getNamedValue(SrcGV->getName());
88     if (!DGV)
89       return nullptr;
90 
91     // If we found a global with the same name in the dest module, but it has
92     // internal linkage, we are really not doing any linkage here.
93     if (DGV->hasLocalLinkage())
94       return nullptr;
95 
96     // Otherwise, we do in fact link to the destination global.
97     return DGV;
98   }
99 
100   /// Drop GV if it is a member of a comdat that we are dropping.
101   /// This can happen with COFF's largest selection kind.
102   void dropReplacedComdat(GlobalValue &GV,
103                           const DenseSet<const Comdat *> &ReplacedDstComdats);
104 
105   bool linkIfNeeded(GlobalValue &GV);
106 
107   /// Helper method to check if we are importing from the current source
108   /// module.
109   bool isPerformingImport() const { return GlobalsToImport != nullptr; }
110 
111   /// If we are importing from the source module, checks if we should
112   /// import SGV as a definition, otherwise import as a declaration.
113   bool doImportAsDefinition(const GlobalValue *SGV);
114 
115 public:
116   ModuleLinker(IRMover &Mover, std::unique_ptr<Module> SrcM, unsigned Flags,
117                DenseSet<const GlobalValue *> *GlobalsToImport = nullptr)
118       : Mover(Mover), SrcM(std::move(SrcM)), Flags(Flags),
119         GlobalsToImport(GlobalsToImport) {}
120 
121   bool run();
122 };
123 }
124 
125 bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) {
126   if (!isPerformingImport())
127     return false;
128   return FunctionImportGlobalProcessing::doImportAsDefinition(SGV,
129                                                               GlobalsToImport);
130 }
131 
132 static GlobalValue::VisibilityTypes
133 getMinVisibility(GlobalValue::VisibilityTypes A,
134                  GlobalValue::VisibilityTypes B) {
135   if (A == GlobalValue::HiddenVisibility || B == GlobalValue::HiddenVisibility)
136     return GlobalValue::HiddenVisibility;
137   if (A == GlobalValue::ProtectedVisibility ||
138       B == GlobalValue::ProtectedVisibility)
139     return GlobalValue::ProtectedVisibility;
140   return GlobalValue::DefaultVisibility;
141 }
142 
143 bool ModuleLinker::getComdatLeader(Module &M, StringRef ComdatName,
144                                    const GlobalVariable *&GVar) {
145   const GlobalValue *GVal = M.getNamedValue(ComdatName);
146   if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) {
147     GVal = GA->getBaseObject();
148     if (!GVal)
149       // We cannot resolve the size of the aliasee yet.
150       return emitError("Linking COMDATs named '" + ComdatName +
151                        "': COMDAT key involves incomputable alias size.");
152   }
153 
154   GVar = dyn_cast_or_null<GlobalVariable>(GVal);
155   if (!GVar)
156     return emitError(
157         "Linking COMDATs named '" + ComdatName +
158         "': GlobalVariable required for data dependent selection!");
159 
160   return false;
161 }
162 
163 bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName,
164                                                  Comdat::SelectionKind Src,
165                                                  Comdat::SelectionKind Dst,
166                                                  Comdat::SelectionKind &Result,
167                                                  bool &LinkFromSrc) {
168   Module &DstM = Mover.getModule();
169   // The ability to mix Comdat::SelectionKind::Any with
170   // Comdat::SelectionKind::Largest is a behavior that comes from COFF.
171   bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any ||
172                          Dst == Comdat::SelectionKind::Largest;
173   bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any ||
174                          Src == Comdat::SelectionKind::Largest;
175   if (DstAnyOrLargest && SrcAnyOrLargest) {
176     if (Dst == Comdat::SelectionKind::Largest ||
177         Src == Comdat::SelectionKind::Largest)
178       Result = Comdat::SelectionKind::Largest;
179     else
180       Result = Comdat::SelectionKind::Any;
181   } else if (Src == Dst) {
182     Result = Dst;
183   } else {
184     return emitError("Linking COMDATs named '" + ComdatName +
185                      "': invalid selection kinds!");
186   }
187 
188   switch (Result) {
189   case Comdat::SelectionKind::Any:
190     // Go with Dst.
191     LinkFromSrc = false;
192     break;
193   case Comdat::SelectionKind::NoDuplicates:
194     return emitError("Linking COMDATs named '" + ComdatName +
195                      "': noduplicates has been violated!");
196   case Comdat::SelectionKind::ExactMatch:
197   case Comdat::SelectionKind::Largest:
198   case Comdat::SelectionKind::SameSize: {
199     const GlobalVariable *DstGV;
200     const GlobalVariable *SrcGV;
201     if (getComdatLeader(DstM, ComdatName, DstGV) ||
202         getComdatLeader(*SrcM, ComdatName, SrcGV))
203       return true;
204 
205     const DataLayout &DstDL = DstM.getDataLayout();
206     const DataLayout &SrcDL = SrcM->getDataLayout();
207     uint64_t DstSize = DstDL.getTypeAllocSize(DstGV->getValueType());
208     uint64_t SrcSize = SrcDL.getTypeAllocSize(SrcGV->getValueType());
209     if (Result == Comdat::SelectionKind::ExactMatch) {
210       if (SrcGV->getInitializer() != DstGV->getInitializer())
211         return emitError("Linking COMDATs named '" + ComdatName +
212                          "': ExactMatch violated!");
213       LinkFromSrc = false;
214     } else if (Result == Comdat::SelectionKind::Largest) {
215       LinkFromSrc = SrcSize > DstSize;
216     } else if (Result == Comdat::SelectionKind::SameSize) {
217       if (SrcSize != DstSize)
218         return emitError("Linking COMDATs named '" + ComdatName +
219                          "': SameSize violated!");
220       LinkFromSrc = false;
221     } else {
222       llvm_unreachable("unknown selection kind");
223     }
224     break;
225   }
226   }
227 
228   return false;
229 }
230 
231 bool ModuleLinker::getComdatResult(const Comdat *SrcC,
232                                    Comdat::SelectionKind &Result,
233                                    bool &LinkFromSrc) {
234   Module &DstM = Mover.getModule();
235   Comdat::SelectionKind SSK = SrcC->getSelectionKind();
236   StringRef ComdatName = SrcC->getName();
237   Module::ComdatSymTabType &ComdatSymTab = DstM.getComdatSymbolTable();
238   Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName);
239 
240   if (DstCI == ComdatSymTab.end()) {
241     // Use the comdat if it is only available in one of the modules.
242     LinkFromSrc = true;
243     Result = SSK;
244     return false;
245   }
246 
247   const Comdat *DstC = &DstCI->second;
248   Comdat::SelectionKind DSK = DstC->getSelectionKind();
249   return computeResultingSelectionKind(ComdatName, SSK, DSK, Result,
250                                        LinkFromSrc);
251 }
252 
253 bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
254                                         const GlobalValue &Dest,
255                                         const GlobalValue &Src) {
256 
257   // Should we unconditionally use the Src?
258   if (shouldOverrideFromSrc()) {
259     LinkFromSrc = true;
260     return false;
261   }
262 
263   // We always have to add Src if it has appending linkage.
264   if (Src.hasAppendingLinkage()) {
265     // Should have prevented importing for appending linkage in linkIfNeeded.
266     assert(!isPerformingImport());
267     LinkFromSrc = true;
268     return false;
269   }
270 
271   bool SrcIsDeclaration = Src.isDeclarationForLinker();
272   bool DestIsDeclaration = Dest.isDeclarationForLinker();
273 
274   if (isPerformingImport()) {
275     if (isa<Function>(&Src)) {
276       // For functions, LinkFromSrc iff this is a function requested
277       // for importing. For variables, decide below normally.
278       LinkFromSrc = GlobalsToImport->count(&Src);
279       return false;
280     }
281 
282     // Check if this is an alias with an already existing definition
283     // in Dest, which must have come from a prior importing pass from
284     // the same Src module. Unlike imported function and variable
285     // definitions, which are imported as available_externally and are
286     // not definitions for the linker, that is not a valid linkage for
287     // imported aliases which must be definitions. Simply use the existing
288     // Dest copy.
289     if (isa<GlobalAlias>(&Src) && !DestIsDeclaration) {
290       assert(isa<GlobalAlias>(&Dest));
291       LinkFromSrc = false;
292       return false;
293     }
294   }
295 
296   if (SrcIsDeclaration) {
297     // If Src is external or if both Src & Dest are external..  Just link the
298     // external globals, we aren't adding anything.
299     if (Src.hasDLLImportStorageClass()) {
300       // If one of GVs is marked as DLLImport, result should be dllimport'ed.
301       LinkFromSrc = DestIsDeclaration;
302       return false;
303     }
304     // If the Dest is weak, use the source linkage.
305     if (Dest.hasExternalWeakLinkage()) {
306       LinkFromSrc = true;
307       return false;
308     }
309     // Link an available_externally over a declaration.
310     LinkFromSrc = !Src.isDeclaration() && Dest.isDeclaration();
311     return false;
312   }
313 
314   if (DestIsDeclaration) {
315     // If Dest is external but Src is not:
316     LinkFromSrc = true;
317     return false;
318   }
319 
320   if (Src.hasCommonLinkage()) {
321     if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) {
322       LinkFromSrc = true;
323       return false;
324     }
325 
326     if (!Dest.hasCommonLinkage()) {
327       LinkFromSrc = false;
328       return false;
329     }
330 
331     const DataLayout &DL = Dest.getParent()->getDataLayout();
332     uint64_t DestSize = DL.getTypeAllocSize(Dest.getValueType());
333     uint64_t SrcSize = DL.getTypeAllocSize(Src.getValueType());
334     LinkFromSrc = SrcSize > DestSize;
335     return false;
336   }
337 
338   if (Src.isWeakForLinker()) {
339     assert(!Dest.hasExternalWeakLinkage());
340     assert(!Dest.hasAvailableExternallyLinkage());
341 
342     if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage()) {
343       LinkFromSrc = true;
344       return false;
345     }
346 
347     LinkFromSrc = false;
348     return false;
349   }
350 
351   if (Dest.isWeakForLinker()) {
352     assert(Src.hasExternalLinkage());
353     LinkFromSrc = true;
354     return false;
355   }
356 
357   assert(!Src.hasExternalWeakLinkage());
358   assert(!Dest.hasExternalWeakLinkage());
359   assert(Dest.hasExternalLinkage() && Src.hasExternalLinkage() &&
360          "Unexpected linkage type!");
361   return emitError("Linking globals named '" + Src.getName() +
362                    "': symbol multiply defined!");
363 }
364 
365 bool ModuleLinker::linkIfNeeded(GlobalValue &GV) {
366   GlobalValue *DGV = getLinkedToGlobal(&GV);
367 
368   if (shouldLinkOnlyNeeded() && !(DGV && DGV->isDeclaration()))
369     return false;
370 
371   if (DGV && !GV.hasLocalLinkage() && !GV.hasAppendingLinkage()) {
372     auto *DGVar = dyn_cast<GlobalVariable>(DGV);
373     auto *SGVar = dyn_cast<GlobalVariable>(&GV);
374     if (DGVar && SGVar) {
375       if (DGVar->isDeclaration() && SGVar->isDeclaration() &&
376           (!DGVar->isConstant() || !SGVar->isConstant())) {
377         DGVar->setConstant(false);
378         SGVar->setConstant(false);
379       }
380       if (DGVar->hasCommonLinkage() && SGVar->hasCommonLinkage()) {
381         unsigned Align = std::max(DGVar->getAlignment(), SGVar->getAlignment());
382         SGVar->setAlignment(Align);
383         DGVar->setAlignment(Align);
384       }
385     }
386 
387     GlobalValue::VisibilityTypes Visibility =
388         getMinVisibility(DGV->getVisibility(), GV.getVisibility());
389     DGV->setVisibility(Visibility);
390     GV.setVisibility(Visibility);
391 
392     bool HasUnnamedAddr = GV.hasUnnamedAddr() && DGV->hasUnnamedAddr();
393     DGV->setUnnamedAddr(HasUnnamedAddr);
394     GV.setUnnamedAddr(HasUnnamedAddr);
395   }
396 
397   // Don't want to append to global_ctors list, for example, when we
398   // are importing for ThinLTO, otherwise the global ctors and dtors
399   // get executed multiple times for local variables (the latter causing
400   // double frees).
401   if (GV.hasAppendingLinkage() && isPerformingImport())
402     return false;
403 
404   if (isPerformingImport()) {
405     if (!doImportAsDefinition(&GV))
406       return false;
407   } else if (!DGV && !shouldOverrideFromSrc() &&
408              (GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() ||
409               GV.hasAvailableExternallyLinkage()))
410     return false;
411 
412   if (GV.isDeclaration())
413     return false;
414 
415   if (const Comdat *SC = GV.getComdat()) {
416     bool LinkFromSrc;
417     Comdat::SelectionKind SK;
418     std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
419     if (!LinkFromSrc)
420       return false;
421   }
422 
423   bool LinkFromSrc = true;
424   if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, GV))
425     return true;
426   if (LinkFromSrc)
427     ValuesToLink.insert(&GV);
428   return false;
429 }
430 
431 void ModuleLinker::addLazyFor(GlobalValue &GV, IRMover::ValueAdder Add) {
432   // Add these to the internalize list
433   if (!GV.hasLinkOnceLinkage())
434     return;
435 
436   if (shouldInternalizeLinkedSymbols())
437     Internalize.insert(GV.getName());
438   Add(GV);
439 
440   const Comdat *SC = GV.getComdat();
441   if (!SC)
442     return;
443   for (GlobalValue *GV2 : LazyComdatMembers[SC]) {
444     GlobalValue *DGV = getLinkedToGlobal(GV2);
445     bool LinkFromSrc = true;
446     if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, *GV2))
447       return;
448     if (!LinkFromSrc)
449       continue;
450     if (shouldInternalizeLinkedSymbols())
451       Internalize.insert(GV2->getName());
452     Add(*GV2);
453   }
454 }
455 
456 void ModuleLinker::dropReplacedComdat(
457     GlobalValue &GV, const DenseSet<const Comdat *> &ReplacedDstComdats) {
458   Comdat *C = GV.getComdat();
459   if (!C)
460     return;
461   if (!ReplacedDstComdats.count(C))
462     return;
463   if (GV.use_empty()) {
464     GV.eraseFromParent();
465     return;
466   }
467 
468   if (auto *F = dyn_cast<Function>(&GV)) {
469     F->deleteBody();
470   } else if (auto *Var = dyn_cast<GlobalVariable>(&GV)) {
471     Var->setInitializer(nullptr);
472   } else {
473     auto &Alias = cast<GlobalAlias>(GV);
474     Module &M = *Alias.getParent();
475     PointerType &Ty = *cast<PointerType>(Alias.getType());
476     GlobalValue *Declaration;
477     if (auto *FTy = dyn_cast<FunctionType>(Alias.getValueType())) {
478       Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage, "", &M);
479     } else {
480       Declaration =
481           new GlobalVariable(M, Ty.getElementType(), /*isConstant*/ false,
482                              GlobalValue::ExternalLinkage,
483                              /*Initializer*/ nullptr);
484     }
485     Declaration->takeName(&Alias);
486     Alias.replaceAllUsesWith(Declaration);
487     Alias.eraseFromParent();
488   }
489 }
490 
491 bool ModuleLinker::run() {
492   Module &DstM = Mover.getModule();
493   DenseSet<const Comdat *> ReplacedDstComdats;
494 
495   for (const auto &SMEC : SrcM->getComdatSymbolTable()) {
496     const Comdat &C = SMEC.getValue();
497     if (ComdatsChosen.count(&C))
498       continue;
499     Comdat::SelectionKind SK;
500     bool LinkFromSrc;
501     if (getComdatResult(&C, SK, LinkFromSrc))
502       return true;
503     ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc);
504 
505     if (!LinkFromSrc)
506       continue;
507 
508     Module::ComdatSymTabType &ComdatSymTab = DstM.getComdatSymbolTable();
509     Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(C.getName());
510     if (DstCI == ComdatSymTab.end())
511       continue;
512 
513     // The source comdat is replacing the dest one.
514     const Comdat *DstC = &DstCI->second;
515     ReplacedDstComdats.insert(DstC);
516   }
517 
518   // Alias have to go first, since we are not able to find their comdats
519   // otherwise.
520   for (auto I = DstM.alias_begin(), E = DstM.alias_end(); I != E;) {
521     GlobalAlias &GV = *I++;
522     dropReplacedComdat(GV, ReplacedDstComdats);
523   }
524 
525   for (auto I = DstM.global_begin(), E = DstM.global_end(); I != E;) {
526     GlobalVariable &GV = *I++;
527     dropReplacedComdat(GV, ReplacedDstComdats);
528   }
529 
530   for (auto I = DstM.begin(), E = DstM.end(); I != E;) {
531     Function &GV = *I++;
532     dropReplacedComdat(GV, ReplacedDstComdats);
533   }
534 
535   for (GlobalVariable &GV : SrcM->globals())
536     if (GV.hasLinkOnceLinkage())
537       if (const Comdat *SC = GV.getComdat())
538         LazyComdatMembers[SC].push_back(&GV);
539 
540   for (Function &SF : *SrcM)
541     if (SF.hasLinkOnceLinkage())
542       if (const Comdat *SC = SF.getComdat())
543         LazyComdatMembers[SC].push_back(&SF);
544 
545   for (GlobalAlias &GA : SrcM->aliases())
546     if (GA.hasLinkOnceLinkage())
547       if (const Comdat *SC = GA.getComdat())
548         LazyComdatMembers[SC].push_back(&GA);
549 
550   // Insert all of the globals in src into the DstM module... without linking
551   // initializers (which could refer to functions not yet mapped over).
552   for (GlobalVariable &GV : SrcM->globals())
553     if (linkIfNeeded(GV))
554       return true;
555 
556   for (Function &SF : *SrcM)
557     if (linkIfNeeded(SF))
558       return true;
559 
560   for (GlobalAlias &GA : SrcM->aliases())
561     if (linkIfNeeded(GA))
562       return true;
563 
564   for (unsigned I = 0; I < ValuesToLink.size(); ++I) {
565     GlobalValue *GV = ValuesToLink[I];
566     const Comdat *SC = GV->getComdat();
567     if (!SC)
568       continue;
569     for (GlobalValue *GV2 : LazyComdatMembers[SC]) {
570       GlobalValue *DGV = getLinkedToGlobal(GV2);
571       bool LinkFromSrc = true;
572       if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, *GV2))
573         return true;
574       if (LinkFromSrc)
575         ValuesToLink.insert(GV2);
576     }
577   }
578 
579   if (shouldInternalizeLinkedSymbols()) {
580     for (GlobalValue *GV : ValuesToLink)
581       Internalize.insert(GV->getName());
582   }
583 
584   if (Mover.move(std::move(SrcM), ValuesToLink.getArrayRef(),
585                  [this](GlobalValue &GV, IRMover::ValueAdder Add) {
586                    addLazyFor(GV, Add);
587                  }))
588     return true;
589   for (auto &P : Internalize) {
590     GlobalValue *GV = DstM.getNamedValue(P.first());
591     GV->setLinkage(GlobalValue::InternalLinkage);
592   }
593 
594   return false;
595 }
596 
597 Linker::Linker(Module &M) : Mover(M) {}
598 
599 bool Linker::linkInModule(std::unique_ptr<Module> Src, unsigned Flags,
600                           DenseSet<const GlobalValue *> *GlobalsToImport) {
601   ModuleLinker ModLinker(Mover, std::move(Src), Flags, GlobalsToImport);
602   return ModLinker.run();
603 }
604 
605 //===----------------------------------------------------------------------===//
606 // LinkModules entrypoint.
607 //===----------------------------------------------------------------------===//
608 
609 /// This function links two modules together, with the resulting Dest module
610 /// modified to be the composite of the two input modules. If an error occurs,
611 /// true is returned and ErrorMsg (if not null) is set to indicate the problem.
612 /// Upon failure, the Dest module could be in a modified state, and shouldn't be
613 /// relied on to be consistent.
614 bool Linker::linkModules(Module &Dest, std::unique_ptr<Module> Src,
615                          unsigned Flags) {
616   Linker L(Dest);
617   return L.linkInModule(std::move(Src), Flags);
618 }
619 
620 //===----------------------------------------------------------------------===//
621 // C API.
622 //===----------------------------------------------------------------------===//
623 
624 LLVMBool LLVMLinkModules2(LLVMModuleRef Dest, LLVMModuleRef Src) {
625   Module *D = unwrap(Dest);
626   std::unique_ptr<Module> M(unwrap(Src));
627   return Linker::linkModules(*D, std::move(M));
628 }
629