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 "llvm/Linker/Linker.h"
15 #include "LinkDiagnosticInfo.h"
16 #include "llvm-c/Linker.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/ADT/StringSet.h"
19 #include "llvm/IR/DiagnosticPrinter.h"
20 #include "llvm/IR/LLVMContext.h"
21 using namespace llvm;
22 
23 namespace {
24 
25 /// This is an implementation class for the LinkModules function, which is the
26 /// entrypoint for this file.
27 class ModuleLinker {
28   IRMover &Mover;
29   Module &SrcM;
30 
31   SetVector<GlobalValue *> ValuesToLink;
32   StringSet<> Internalize;
33 
34   /// For symbol clashes, prefer those from Src.
35   unsigned Flags;
36 
37   /// Function index passed into ModuleLinker for using in function
38   /// importing/exporting handling.
39   const FunctionInfoIndex *ImportIndex;
40 
41   /// Functions to import from source module, all other functions are
42   /// imported as declarations instead of definitions.
43   DenseSet<const GlobalValue *> *FunctionsToImport;
44 
45   /// Set to true if the given FunctionInfoIndex contains any functions
46   /// from this source module, in which case we must conservatively assume
47   /// that any of its functions may be imported into another module
48   /// as part of a different backend compilation process.
49   bool HasExportedFunctions = false;
50 
51   /// Association between metadata value id and temporary metadata that
52   /// remains unmapped after function importing. Saved during function
53   /// importing and consumed during the metadata linking postpass.
54   DenseMap<unsigned, MDNode *> *ValIDToTempMDMap;
55 
56   /// Used as the callback for lazy linking.
57   /// The mover has just hit GV and we have to decide if it, and other members
58   /// of the same comdat, should be linked. Every member to be linked is passed
59   /// to Add.
60   void addLazyFor(GlobalValue &GV, IRMover::ValueAdder Add);
61 
62   bool shouldOverrideFromSrc() { return Flags & Linker::OverrideFromSrc; }
63   bool shouldLinkOnlyNeeded() { return Flags & Linker::LinkOnlyNeeded; }
64   bool shouldInternalizeLinkedSymbols() {
65     return Flags & Linker::InternalizeLinkedSymbols;
66   }
67 
68   bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest,
69                             const GlobalValue &Src);
70 
71   /// Should we have mover and linker error diag info?
72   bool emitError(const Twine &Message) {
73     SrcM.getContext().diagnose(LinkDiagnosticInfo(DS_Error, Message));
74     return true;
75   }
76 
77   bool getComdatLeader(Module &M, StringRef ComdatName,
78                        const GlobalVariable *&GVar);
79   bool computeResultingSelectionKind(StringRef ComdatName,
80                                      Comdat::SelectionKind Src,
81                                      Comdat::SelectionKind Dst,
82                                      Comdat::SelectionKind &Result,
83                                      bool &LinkFromSrc);
84   std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>>
85       ComdatsChosen;
86   bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK,
87                        bool &LinkFromSrc);
88   // Keep track of the global value members of each comdat in source.
89   DenseMap<const Comdat *, std::vector<GlobalValue *>> ComdatMembers;
90 
91   /// Given a global in the source module, return the global in the
92   /// destination module that is being linked to, if any.
93   GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
94     Module &DstM = Mover.getModule();
95     // If the source has no name it can't link.  If it has local linkage,
96     // there is no name match-up going on.
97     if (!SrcGV->hasName() || GlobalValue::isLocalLinkage(SrcGV->getLinkage()))
98       return nullptr;
99 
100     // Otherwise see if we have a match in the destination module's symtab.
101     GlobalValue *DGV = DstM.getNamedValue(SrcGV->getName());
102     if (!DGV)
103       return nullptr;
104 
105     // If we found a global with the same name in the dest module, but it has
106     // internal linkage, we are really not doing any linkage here.
107     if (DGV->hasLocalLinkage())
108       return nullptr;
109 
110     // Otherwise, we do in fact link to the destination global.
111     return DGV;
112   }
113 
114   bool linkIfNeeded(GlobalValue &GV);
115 
116   /// Helper method to check if we are importing from the current source
117   /// module.
118   bool isPerformingImport() const { return FunctionsToImport != nullptr; }
119 
120   /// If we are importing from the source module, checks if we should
121   /// import SGV as a definition, otherwise import as a declaration.
122   bool doImportAsDefinition(const GlobalValue *SGV);
123 
124 public:
125   ModuleLinker(IRMover &Mover, Module &SrcM, unsigned Flags,
126                const FunctionInfoIndex *Index = nullptr,
127                DenseSet<const GlobalValue *> *FunctionsToImport = nullptr,
128                DenseMap<unsigned, MDNode *> *ValIDToTempMDMap = nullptr)
129       : Mover(Mover), SrcM(SrcM), Flags(Flags), ImportIndex(Index),
130         FunctionsToImport(FunctionsToImport),
131         ValIDToTempMDMap(ValIDToTempMDMap) {
132     assert((ImportIndex || !FunctionsToImport) &&
133            "Expect a FunctionInfoIndex when importing");
134     // If we have a FunctionInfoIndex but no function to import,
135     // then this is the primary module being compiled in a ThinLTO
136     // backend compilation, and we need to see if it has functions that
137     // may be exported to another backend compilation.
138     if (ImportIndex && !FunctionsToImport)
139       HasExportedFunctions = ImportIndex->hasExportedFunctions(SrcM);
140   }
141 
142   bool run();
143 };
144 
145 /// Class to handle necessary GlobalValue changes required by ThinLTO including
146 /// linkage changes and any necessary renaming.
147 class ThinLTOGlobalProcessing {
148   /// The Module which we are exporting or importing functions from.
149   Module &M;
150 
151   /// Function index passed in for function importing/exporting handling.
152   const FunctionInfoIndex *ImportIndex;
153 
154   /// Functions to import from this module, all other functions will be
155   /// imported as declarations instead of definitions.
156   DenseSet<const GlobalValue *> *FunctionsToImport;
157 
158   /// Set to true if the given FunctionInfoIndex contains any functions
159   /// from this source module, in which case we must conservatively assume
160   /// that any of its functions may be imported into another module
161   /// as part of a different backend compilation process.
162   bool HasExportedFunctions = false;
163 
164   /// Populated during ThinLTO global processing with locals promoted
165   /// to global scope in an exporting module, which now need to be linked
166   /// in if calling from the ModuleLinker.
167   SetVector<GlobalValue *> NewExportedValues;
168 
169   /// Check if we should promote the given local value to global scope.
170   bool doPromoteLocalToGlobal(const GlobalValue *SGV);
171 
172   /// Helper methods to check if we are importing from or potentially
173   /// exporting from the current source module.
174   bool isPerformingImport() const { return FunctionsToImport != nullptr; }
175   bool isModuleExporting() const { return HasExportedFunctions; }
176 
177   /// If we are importing from the source module, checks if we should
178   /// import SGV as a definition, otherwise import as a declaration.
179   bool doImportAsDefinition(const GlobalValue *SGV);
180 
181   /// Get the name for SGV that should be used in the linked destination
182   /// module. Specifically, this handles the case where we need to rename
183   /// a local that is being promoted to global scope.
184   std::string getName(const GlobalValue *SGV);
185 
186   /// Process globals so that they can be used in ThinLTO. This includes
187   /// promoting local variables so that they can be reference externally by
188   /// thin lto imported globals and converting strong external globals to
189   /// available_externally.
190   void processGlobalsForThinLTO();
191   void processGlobalForThinLTO(GlobalValue &GV);
192 
193   /// Get the new linkage for SGV that should be used in the linked destination
194   /// module. Specifically, for ThinLTO importing or exporting it may need
195   /// to be adjusted.
196   GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV);
197 
198 public:
199   ThinLTOGlobalProcessing(
200       Module &M, const FunctionInfoIndex *Index,
201       DenseSet<const GlobalValue *> *FunctionsToImport = nullptr)
202       : M(M), ImportIndex(Index), FunctionsToImport(FunctionsToImport) {
203     // If we have a FunctionInfoIndex but no function to import,
204     // then this is the primary module being compiled in a ThinLTO
205     // backend compilation, and we need to see if it has functions that
206     // may be exported to another backend compilation.
207     if (!FunctionsToImport)
208       HasExportedFunctions = ImportIndex->hasExportedFunctions(M);
209   }
210 
211   bool run();
212 
213   /// Access the promoted globals that are now exported and need to be linked.
214   SetVector<GlobalValue *> &getNewExportedValues() { return NewExportedValues; }
215 };
216 }
217 
218 /// Checks if we should import SGV as a definition, otherwise import as a
219 /// declaration.
220 static bool
221 doImportAsDefinitionImpl(const GlobalValue *SGV,
222                          DenseSet<const GlobalValue *> *FunctionsToImport) {
223   auto *GA = dyn_cast<GlobalAlias>(SGV);
224   if (GA) {
225     if (GA->hasWeakAnyLinkage())
226       return false;
227     const GlobalObject *GO = GA->getBaseObject();
228     if (!GO->hasLinkOnceODRLinkage())
229       return false;
230     return doImportAsDefinitionImpl(GO, FunctionsToImport);
231   }
232   // Always import GlobalVariable definitions, except for the special
233   // case of WeakAny which are imported as ExternalWeak declarations
234   // (see comments in ModuleLinker::getLinkage). The linkage changes
235   // described in ModuleLinker::getLinkage ensure the correct behavior (e.g.
236   // global variables with external linkage are transformed to
237   // available_externally definitions, which are ultimately turned into
238   // declarations after the EliminateAvailableExternally pass).
239   if (isa<GlobalVariable>(SGV) && !SGV->isDeclaration() &&
240       !SGV->hasWeakAnyLinkage())
241     return true;
242   // Only import the function requested for importing.
243   auto *SF = dyn_cast<Function>(SGV);
244   if (SF && FunctionsToImport->count(SF))
245     return true;
246   // Otherwise no.
247   return false;
248 }
249 
250 bool ThinLTOGlobalProcessing::doImportAsDefinition(const GlobalValue *SGV) {
251   if (!isPerformingImport())
252     return false;
253   return doImportAsDefinitionImpl(SGV, FunctionsToImport);
254 }
255 
256 bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) {
257   if (!isPerformingImport())
258     return false;
259   return doImportAsDefinitionImpl(SGV, FunctionsToImport);
260 }
261 
262 bool ThinLTOGlobalProcessing::doPromoteLocalToGlobal(const GlobalValue *SGV) {
263   assert(SGV->hasLocalLinkage());
264   // Both the imported references and the original local variable must
265   // be promoted.
266   if (!isPerformingImport() && !isModuleExporting())
267     return false;
268 
269   // Local const variables never need to be promoted unless they are address
270   // taken. The imported uses can simply use the clone created in this module.
271   // For now we are conservative in determining which variables are not
272   // address taken by checking the unnamed addr flag. To be more aggressive,
273   // the address taken information must be checked earlier during parsing
274   // of the module and recorded in the function index for use when importing
275   // from that module.
276   auto *GVar = dyn_cast<GlobalVariable>(SGV);
277   if (GVar && GVar->isConstant() && GVar->hasUnnamedAddr())
278     return false;
279 
280   // Eventually we only need to promote functions in the exporting module that
281   // are referenced by a potentially exported function (i.e. one that is in the
282   // function index).
283   return true;
284 }
285 
286 std::string ThinLTOGlobalProcessing::getName(const GlobalValue *SGV) {
287   // For locals that must be promoted to global scope, ensure that
288   // the promoted name uniquely identifies the copy in the original module,
289   // using the ID assigned during combined index creation. When importing,
290   // we rename all locals (not just those that are promoted) in order to
291   // avoid naming conflicts between locals imported from different modules.
292   if (SGV->hasLocalLinkage() &&
293       (doPromoteLocalToGlobal(SGV) || isPerformingImport()))
294     return FunctionInfoIndex::getGlobalNameForLocal(
295         SGV->getName(),
296         ImportIndex->getModuleId(SGV->getParent()->getModuleIdentifier()));
297   return SGV->getName();
298 }
299 
300 GlobalValue::LinkageTypes
301 ThinLTOGlobalProcessing::getLinkage(const GlobalValue *SGV) {
302   // Any local variable that is referenced by an exported function needs
303   // to be promoted to global scope. Since we don't currently know which
304   // functions reference which local variables/functions, we must treat
305   // all as potentially exported if this module is exporting anything.
306   if (isModuleExporting()) {
307     if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV))
308       return GlobalValue::ExternalLinkage;
309     return SGV->getLinkage();
310   }
311 
312   // Otherwise, if we aren't importing, no linkage change is needed.
313   if (!isPerformingImport())
314     return SGV->getLinkage();
315 
316   switch (SGV->getLinkage()) {
317   case GlobalValue::ExternalLinkage:
318     // External defnitions are converted to available_externally
319     // definitions upon import, so that they are available for inlining
320     // and/or optimization, but are turned into declarations later
321     // during the EliminateAvailableExternally pass.
322     if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
323       return GlobalValue::AvailableExternallyLinkage;
324     // An imported external declaration stays external.
325     return SGV->getLinkage();
326 
327   case GlobalValue::AvailableExternallyLinkage:
328     // An imported available_externally definition converts
329     // to external if imported as a declaration.
330     if (!doImportAsDefinition(SGV))
331       return GlobalValue::ExternalLinkage;
332     // An imported available_externally declaration stays that way.
333     return SGV->getLinkage();
334 
335   case GlobalValue::LinkOnceAnyLinkage:
336   case GlobalValue::LinkOnceODRLinkage:
337     // These both stay the same when importing the definition.
338     // The ThinLTO pass will eventually force-import their definitions.
339     return SGV->getLinkage();
340 
341   case GlobalValue::WeakAnyLinkage:
342     // Can't import weak_any definitions correctly, or we might change the
343     // program semantics, since the linker will pick the first weak_any
344     // definition and importing would change the order they are seen by the
345     // linker. The module linking caller needs to enforce this.
346     assert(!doImportAsDefinition(SGV));
347     // If imported as a declaration, it becomes external_weak.
348     return GlobalValue::ExternalWeakLinkage;
349 
350   case GlobalValue::WeakODRLinkage:
351     // For weak_odr linkage, there is a guarantee that all copies will be
352     // equivalent, so the issue described above for weak_any does not exist,
353     // and the definition can be imported. It can be treated similarly
354     // to an imported externally visible global value.
355     if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
356       return GlobalValue::AvailableExternallyLinkage;
357     else
358       return GlobalValue::ExternalLinkage;
359 
360   case GlobalValue::AppendingLinkage:
361     // It would be incorrect to import an appending linkage variable,
362     // since it would cause global constructors/destructors to be
363     // executed multiple times. This should have already been handled
364     // by linkIfNeeded, and we will assert in shouldLinkFromSource
365     // if we try to import, so we simply return AppendingLinkage.
366     return GlobalValue::AppendingLinkage;
367 
368   case GlobalValue::InternalLinkage:
369   case GlobalValue::PrivateLinkage:
370     // If we are promoting the local to global scope, it is handled
371     // similarly to a normal externally visible global.
372     if (doPromoteLocalToGlobal(SGV)) {
373       if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
374         return GlobalValue::AvailableExternallyLinkage;
375       else
376         return GlobalValue::ExternalLinkage;
377     }
378     // A non-promoted imported local definition stays local.
379     // The ThinLTO pass will eventually force-import their definitions.
380     return SGV->getLinkage();
381 
382   case GlobalValue::ExternalWeakLinkage:
383     // External weak doesn't apply to definitions, must be a declaration.
384     assert(!doImportAsDefinition(SGV));
385     // Linkage stays external_weak.
386     return SGV->getLinkage();
387 
388   case GlobalValue::CommonLinkage:
389     // Linkage stays common on definitions.
390     // The ThinLTO pass will eventually force-import their definitions.
391     return SGV->getLinkage();
392   }
393 
394   llvm_unreachable("unknown linkage type");
395 }
396 
397 static GlobalValue::VisibilityTypes
398 getMinVisibility(GlobalValue::VisibilityTypes A,
399                  GlobalValue::VisibilityTypes B) {
400   if (A == GlobalValue::HiddenVisibility || B == GlobalValue::HiddenVisibility)
401     return GlobalValue::HiddenVisibility;
402   if (A == GlobalValue::ProtectedVisibility ||
403       B == GlobalValue::ProtectedVisibility)
404     return GlobalValue::ProtectedVisibility;
405   return GlobalValue::DefaultVisibility;
406 }
407 
408 bool ModuleLinker::getComdatLeader(Module &M, StringRef ComdatName,
409                                    const GlobalVariable *&GVar) {
410   const GlobalValue *GVal = M.getNamedValue(ComdatName);
411   if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) {
412     GVal = GA->getBaseObject();
413     if (!GVal)
414       // We cannot resolve the size of the aliasee yet.
415       return emitError("Linking COMDATs named '" + ComdatName +
416                        "': COMDAT key involves incomputable alias size.");
417   }
418 
419   GVar = dyn_cast_or_null<GlobalVariable>(GVal);
420   if (!GVar)
421     return emitError(
422         "Linking COMDATs named '" + ComdatName +
423         "': GlobalVariable required for data dependent selection!");
424 
425   return false;
426 }
427 
428 bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName,
429                                                  Comdat::SelectionKind Src,
430                                                  Comdat::SelectionKind Dst,
431                                                  Comdat::SelectionKind &Result,
432                                                  bool &LinkFromSrc) {
433   Module &DstM = Mover.getModule();
434   // The ability to mix Comdat::SelectionKind::Any with
435   // Comdat::SelectionKind::Largest is a behavior that comes from COFF.
436   bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any ||
437                          Dst == Comdat::SelectionKind::Largest;
438   bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any ||
439                          Src == Comdat::SelectionKind::Largest;
440   if (DstAnyOrLargest && SrcAnyOrLargest) {
441     if (Dst == Comdat::SelectionKind::Largest ||
442         Src == Comdat::SelectionKind::Largest)
443       Result = Comdat::SelectionKind::Largest;
444     else
445       Result = Comdat::SelectionKind::Any;
446   } else if (Src == Dst) {
447     Result = Dst;
448   } else {
449     return emitError("Linking COMDATs named '" + ComdatName +
450                      "': invalid selection kinds!");
451   }
452 
453   switch (Result) {
454   case Comdat::SelectionKind::Any:
455     // Go with Dst.
456     LinkFromSrc = false;
457     break;
458   case Comdat::SelectionKind::NoDuplicates:
459     return emitError("Linking COMDATs named '" + ComdatName +
460                      "': noduplicates has been violated!");
461   case Comdat::SelectionKind::ExactMatch:
462   case Comdat::SelectionKind::Largest:
463   case Comdat::SelectionKind::SameSize: {
464     const GlobalVariable *DstGV;
465     const GlobalVariable *SrcGV;
466     if (getComdatLeader(DstM, ComdatName, DstGV) ||
467         getComdatLeader(SrcM, ComdatName, SrcGV))
468       return true;
469 
470     const DataLayout &DstDL = DstM.getDataLayout();
471     const DataLayout &SrcDL = SrcM.getDataLayout();
472     uint64_t DstSize = DstDL.getTypeAllocSize(DstGV->getValueType());
473     uint64_t SrcSize = SrcDL.getTypeAllocSize(SrcGV->getValueType());
474     if (Result == Comdat::SelectionKind::ExactMatch) {
475       if (SrcGV->getInitializer() != DstGV->getInitializer())
476         return emitError("Linking COMDATs named '" + ComdatName +
477                          "': ExactMatch violated!");
478       LinkFromSrc = false;
479     } else if (Result == Comdat::SelectionKind::Largest) {
480       LinkFromSrc = SrcSize > DstSize;
481     } else if (Result == Comdat::SelectionKind::SameSize) {
482       if (SrcSize != DstSize)
483         return emitError("Linking COMDATs named '" + ComdatName +
484                          "': SameSize violated!");
485       LinkFromSrc = false;
486     } else {
487       llvm_unreachable("unknown selection kind");
488     }
489     break;
490   }
491   }
492 
493   return false;
494 }
495 
496 bool ModuleLinker::getComdatResult(const Comdat *SrcC,
497                                    Comdat::SelectionKind &Result,
498                                    bool &LinkFromSrc) {
499   Module &DstM = Mover.getModule();
500   Comdat::SelectionKind SSK = SrcC->getSelectionKind();
501   StringRef ComdatName = SrcC->getName();
502   Module::ComdatSymTabType &ComdatSymTab = DstM.getComdatSymbolTable();
503   Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName);
504 
505   if (DstCI == ComdatSymTab.end()) {
506     // Use the comdat if it is only available in one of the modules.
507     LinkFromSrc = true;
508     Result = SSK;
509     return false;
510   }
511 
512   const Comdat *DstC = &DstCI->second;
513   Comdat::SelectionKind DSK = DstC->getSelectionKind();
514   return computeResultingSelectionKind(ComdatName, SSK, DSK, Result,
515                                        LinkFromSrc);
516 }
517 
518 bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
519                                         const GlobalValue &Dest,
520                                         const GlobalValue &Src) {
521 
522   // Should we unconditionally use the Src?
523   if (shouldOverrideFromSrc()) {
524     LinkFromSrc = true;
525     return false;
526   }
527 
528   // We always have to add Src if it has appending linkage.
529   if (Src.hasAppendingLinkage()) {
530     // Should have prevented importing for appending linkage in linkIfNeeded.
531     assert(!isPerformingImport());
532     LinkFromSrc = true;
533     return false;
534   }
535 
536   bool SrcIsDeclaration = Src.isDeclarationForLinker();
537   bool DestIsDeclaration = Dest.isDeclarationForLinker();
538 
539   if (isPerformingImport()) {
540     if (isa<Function>(&Src)) {
541       // For functions, LinkFromSrc iff this is a function requested
542       // for importing. For variables, decide below normally.
543       LinkFromSrc = FunctionsToImport->count(&Src);
544       return false;
545     }
546 
547     // Check if this is an alias with an already existing definition
548     // in Dest, which must have come from a prior importing pass from
549     // the same Src module. Unlike imported function and variable
550     // definitions, which are imported as available_externally and are
551     // not definitions for the linker, that is not a valid linkage for
552     // imported aliases which must be definitions. Simply use the existing
553     // Dest copy.
554     if (isa<GlobalAlias>(&Src) && !DestIsDeclaration) {
555       assert(isa<GlobalAlias>(&Dest));
556       LinkFromSrc = false;
557       return false;
558     }
559   }
560 
561   if (SrcIsDeclaration) {
562     // If Src is external or if both Src & Dest are external..  Just link the
563     // external globals, we aren't adding anything.
564     if (Src.hasDLLImportStorageClass()) {
565       // If one of GVs is marked as DLLImport, result should be dllimport'ed.
566       LinkFromSrc = DestIsDeclaration;
567       return false;
568     }
569     // If the Dest is weak, use the source linkage.
570     if (Dest.hasExternalWeakLinkage()) {
571       LinkFromSrc = true;
572       return false;
573     }
574     // Link an available_externally over a declaration.
575     LinkFromSrc = !Src.isDeclaration() && Dest.isDeclaration();
576     return false;
577   }
578 
579   if (DestIsDeclaration) {
580     // If Dest is external but Src is not:
581     LinkFromSrc = true;
582     return false;
583   }
584 
585   if (Src.hasCommonLinkage()) {
586     if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) {
587       LinkFromSrc = true;
588       return false;
589     }
590 
591     if (!Dest.hasCommonLinkage()) {
592       LinkFromSrc = false;
593       return false;
594     }
595 
596     const DataLayout &DL = Dest.getParent()->getDataLayout();
597     uint64_t DestSize = DL.getTypeAllocSize(Dest.getValueType());
598     uint64_t SrcSize = DL.getTypeAllocSize(Src.getValueType());
599     LinkFromSrc = SrcSize > DestSize;
600     return false;
601   }
602 
603   if (Src.isWeakForLinker()) {
604     assert(!Dest.hasExternalWeakLinkage());
605     assert(!Dest.hasAvailableExternallyLinkage());
606 
607     if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage()) {
608       LinkFromSrc = true;
609       return false;
610     }
611 
612     LinkFromSrc = false;
613     return false;
614   }
615 
616   if (Dest.isWeakForLinker()) {
617     assert(Src.hasExternalLinkage());
618     LinkFromSrc = true;
619     return false;
620   }
621 
622   assert(!Src.hasExternalWeakLinkage());
623   assert(!Dest.hasExternalWeakLinkage());
624   assert(Dest.hasExternalLinkage() && Src.hasExternalLinkage() &&
625          "Unexpected linkage type!");
626   return emitError("Linking globals named '" + Src.getName() +
627                    "': symbol multiply defined!");
628 }
629 
630 bool ModuleLinker::linkIfNeeded(GlobalValue &GV) {
631   GlobalValue *DGV = getLinkedToGlobal(&GV);
632 
633   if (shouldLinkOnlyNeeded() && !(DGV && DGV->isDeclaration()))
634     return false;
635 
636   if (DGV && !GV.hasLocalLinkage() && !GV.hasAppendingLinkage()) {
637     auto *DGVar = dyn_cast<GlobalVariable>(DGV);
638     auto *SGVar = dyn_cast<GlobalVariable>(&GV);
639     if (DGVar && SGVar) {
640       if (DGVar->isDeclaration() && SGVar->isDeclaration() &&
641           (!DGVar->isConstant() || !SGVar->isConstant())) {
642         DGVar->setConstant(false);
643         SGVar->setConstant(false);
644       }
645       if (DGVar->hasCommonLinkage() && SGVar->hasCommonLinkage()) {
646         unsigned Align = std::max(DGVar->getAlignment(), SGVar->getAlignment());
647         SGVar->setAlignment(Align);
648         DGVar->setAlignment(Align);
649       }
650     }
651 
652     GlobalValue::VisibilityTypes Visibility =
653         getMinVisibility(DGV->getVisibility(), GV.getVisibility());
654     DGV->setVisibility(Visibility);
655     GV.setVisibility(Visibility);
656 
657     bool HasUnnamedAddr = GV.hasUnnamedAddr() && DGV->hasUnnamedAddr();
658     DGV->setUnnamedAddr(HasUnnamedAddr);
659     GV.setUnnamedAddr(HasUnnamedAddr);
660   }
661 
662   // Don't want to append to global_ctors list, for example, when we
663   // are importing for ThinLTO, otherwise the global ctors and dtors
664   // get executed multiple times for local variables (the latter causing
665   // double frees).
666   if (GV.hasAppendingLinkage() && isPerformingImport())
667     return false;
668 
669   if (isPerformingImport() && !doImportAsDefinition(&GV))
670     return false;
671 
672   if (!DGV && !shouldOverrideFromSrc() &&
673       (GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() ||
674        GV.hasAvailableExternallyLinkage()))
675     return false;
676 
677   if (GV.isDeclaration())
678     return false;
679 
680   if (const Comdat *SC = GV.getComdat()) {
681     bool LinkFromSrc;
682     Comdat::SelectionKind SK;
683     std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
684     if (LinkFromSrc)
685       ValuesToLink.insert(&GV);
686     return false;
687   }
688 
689   bool LinkFromSrc = true;
690   if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, GV))
691     return true;
692   if (LinkFromSrc)
693     ValuesToLink.insert(&GV);
694   return false;
695 }
696 
697 void ModuleLinker::addLazyFor(GlobalValue &GV, IRMover::ValueAdder Add) {
698   // Add these to the internalize list
699   if (!GV.hasLinkOnceLinkage())
700     return;
701 
702   if (shouldInternalizeLinkedSymbols())
703     Internalize.insert(GV.getName());
704   Add(GV);
705 
706   const Comdat *SC = GV.getComdat();
707   if (!SC)
708     return;
709   for (GlobalValue *GV2 : ComdatMembers[SC]) {
710     if (!GV2->hasLocalLinkage() && shouldInternalizeLinkedSymbols())
711       Internalize.insert(GV2->getName());
712     Add(*GV2);
713   }
714 }
715 
716 void ThinLTOGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
717   if (GV.hasLocalLinkage() &&
718       (doPromoteLocalToGlobal(&GV) || isPerformingImport())) {
719     GV.setName(getName(&GV));
720     GV.setLinkage(getLinkage(&GV));
721     if (!GV.hasLocalLinkage())
722       GV.setVisibility(GlobalValue::HiddenVisibility);
723     if (isModuleExporting())
724       NewExportedValues.insert(&GV);
725     return;
726   }
727   GV.setLinkage(getLinkage(&GV));
728 }
729 
730 void ThinLTOGlobalProcessing::processGlobalsForThinLTO() {
731   for (GlobalVariable &GV : M.globals())
732     processGlobalForThinLTO(GV);
733   for (Function &SF : M)
734     processGlobalForThinLTO(SF);
735   for (GlobalAlias &GA : M.aliases())
736     processGlobalForThinLTO(GA);
737 }
738 
739 bool ThinLTOGlobalProcessing::run() {
740   processGlobalsForThinLTO();
741   return false;
742 }
743 
744 bool ModuleLinker::run() {
745   for (const auto &SMEC : SrcM.getComdatSymbolTable()) {
746     const Comdat &C = SMEC.getValue();
747     if (ComdatsChosen.count(&C))
748       continue;
749     Comdat::SelectionKind SK;
750     bool LinkFromSrc;
751     if (getComdatResult(&C, SK, LinkFromSrc))
752       return true;
753     ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc);
754   }
755 
756   for (GlobalVariable &GV : SrcM.globals())
757     if (const Comdat *SC = GV.getComdat())
758       ComdatMembers[SC].push_back(&GV);
759 
760   for (Function &SF : SrcM)
761     if (const Comdat *SC = SF.getComdat())
762       ComdatMembers[SC].push_back(&SF);
763 
764   for (GlobalAlias &GA : SrcM.aliases())
765     if (const Comdat *SC = GA.getComdat())
766       ComdatMembers[SC].push_back(&GA);
767 
768   // Insert all of the globals in src into the DstM module... without linking
769   // initializers (which could refer to functions not yet mapped over).
770   for (GlobalVariable &GV : SrcM.globals())
771     if (linkIfNeeded(GV))
772       return true;
773 
774   for (Function &SF : SrcM)
775     if (linkIfNeeded(SF))
776       return true;
777 
778   for (GlobalAlias &GA : SrcM.aliases())
779     if (linkIfNeeded(GA))
780       return true;
781 
782   if (ImportIndex) {
783     ThinLTOGlobalProcessing ThinLTOProcessing(SrcM, ImportIndex,
784                                               FunctionsToImport);
785     if (ThinLTOProcessing.run())
786       return true;
787     for (auto *GV : ThinLTOProcessing.getNewExportedValues())
788       ValuesToLink.insert(GV);
789   }
790 
791   for (unsigned I = 0; I < ValuesToLink.size(); ++I) {
792     GlobalValue *GV = ValuesToLink[I];
793     const Comdat *SC = GV->getComdat();
794     if (!SC)
795       continue;
796     for (GlobalValue *GV2 : ComdatMembers[SC])
797       ValuesToLink.insert(GV2);
798   }
799 
800   if (shouldInternalizeLinkedSymbols()) {
801     for (GlobalValue *GV : ValuesToLink)
802       Internalize.insert(GV->getName());
803   }
804 
805   if (Mover.move(SrcM, ValuesToLink.getArrayRef(),
806                  [this](GlobalValue &GV, IRMover::ValueAdder Add) {
807                    addLazyFor(GV, Add);
808                  },
809                  ValIDToTempMDMap, false))
810     return true;
811   Module &DstM = Mover.getModule();
812   for (auto &P : Internalize) {
813     GlobalValue *GV = DstM.getNamedValue(P.first());
814     GV->setLinkage(GlobalValue::InternalLinkage);
815   }
816 
817   return false;
818 }
819 
820 Linker::Linker(Module &M) : Mover(M) {}
821 
822 bool Linker::linkInModule(std::unique_ptr<Module> Src, unsigned Flags,
823                           const FunctionInfoIndex *Index,
824                           DenseSet<const GlobalValue *> *FunctionsToImport,
825                           DenseMap<unsigned, MDNode *> *ValIDToTempMDMap) {
826   ModuleLinker ModLinker(Mover, *Src, Flags, Index, FunctionsToImport,
827                          ValIDToTempMDMap);
828   return ModLinker.run();
829 }
830 
831 bool Linker::linkInModuleForCAPI(Module &Src) {
832   ModuleLinker ModLinker(Mover, Src, 0, nullptr, nullptr);
833   return ModLinker.run();
834 }
835 
836 bool Linker::linkInMetadata(Module &Src,
837                             DenseMap<unsigned, MDNode *> *ValIDToTempMDMap) {
838   SetVector<GlobalValue *> ValuesToLink;
839   if (Mover.move(
840           Src, ValuesToLink.getArrayRef(),
841           [this](GlobalValue &GV, IRMover::ValueAdder Add) { assert(false); },
842           ValIDToTempMDMap, true))
843     return true;
844   return false;
845 }
846 
847 //===----------------------------------------------------------------------===//
848 // LinkModules entrypoint.
849 //===----------------------------------------------------------------------===//
850 
851 /// This function links two modules together, with the resulting Dest module
852 /// modified to be the composite of the two input modules. If an error occurs,
853 /// true is returned and ErrorMsg (if not null) is set to indicate the problem.
854 /// Upon failure, the Dest module could be in a modified state, and shouldn't be
855 /// relied on to be consistent.
856 bool Linker::linkModules(Module &Dest, std::unique_ptr<Module> Src,
857                          unsigned Flags) {
858   Linker L(Dest);
859   return L.linkInModule(std::move(Src), Flags);
860 }
861 
862 bool llvm::renameModuleForThinLTO(Module &M, const FunctionInfoIndex *Index) {
863   ThinLTOGlobalProcessing ThinLTOProcessing(M, Index);
864   return ThinLTOProcessing.run();
865 }
866 
867 //===----------------------------------------------------------------------===//
868 // C API.
869 //===----------------------------------------------------------------------===//
870 
871 static void diagnosticHandler(const DiagnosticInfo &DI, void *C) {
872   auto *Message = reinterpret_cast<std::string *>(C);
873   raw_string_ostream Stream(*Message);
874   DiagnosticPrinterRawOStream DP(Stream);
875   DI.print(DP);
876 }
877 
878 LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
879                          LLVMLinkerMode Unused, char **OutMessages) {
880   Module *D = unwrap(Dest);
881   LLVMContext &Ctx = D->getContext();
882 
883   LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler =
884       Ctx.getDiagnosticHandler();
885   void *OldDiagnosticContext = Ctx.getDiagnosticContext();
886   std::string Message;
887   Ctx.setDiagnosticHandler(diagnosticHandler, &Message, true);
888 
889   Linker L(*D);
890   Module *M = unwrap(Src);
891   LLVMBool Result = L.linkInModuleForCAPI(*M);
892 
893   Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true);
894 
895   if (OutMessages && Result)
896     *OutMessages = strdup(Message.c_str());
897   return Result;
898 }
899 
900 LLVMBool LLVMLinkModules2(LLVMModuleRef Dest, LLVMModuleRef Src) {
901   Module *D = unwrap(Dest);
902   std::unique_ptr<Module> M(unwrap(Src));
903   return Linker::linkModules(*D, std::move(M));
904 }
905