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