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