]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - opencl/llvm.git/blob - lib/Linker/LinkModules.cpp
Set the body of a new struct as soon as it is created.
[opencl/llvm.git] / lib / Linker / LinkModules.cpp
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 //===----------------------------------------------------------------------===//
14 #include "llvm/Linker/Linker.h"
15 #include "llvm-c/Linker.h"
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/DiagnosticPrinter.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/TypeFinder.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Transforms/Utils/Cloning.h"
29 #include <cctype>
30 #include <tuple>
31 using namespace llvm;
34 //===----------------------------------------------------------------------===//
35 // TypeMap implementation.
36 //===----------------------------------------------------------------------===//
38 namespace {
39 typedef SmallPtrSet<StructType *, 32> TypeSet;
41 class TypeMapTy : public ValueMapTypeRemapper {
42   /// This is a mapping from a source type to a destination type to use.
43   DenseMap<Type*, Type*> MappedTypes;
45   /// When checking to see if two subgraphs are isomorphic, we speculatively
46   /// add types to MappedTypes, but keep track of them here in case we need to
47   /// roll back.
48   SmallVector<Type*, 16> SpeculativeTypes;
50   /// This is a list of non-opaque structs in the source module that are mapped
51   /// to an opaque struct in the destination module.
52   SmallVector<StructType*, 16> SrcDefinitionsToResolve;
54   /// This is the set of opaque types in the destination modules who are
55   /// getting a body from the source module.
56   SmallPtrSet<StructType*, 16> DstResolvedOpaqueTypes;
58 public:
59   TypeMapTy(TypeSet &Set) : DstStructTypesSet(Set) {}
61   TypeSet &DstStructTypesSet;
62   /// Indicate that the specified type in the destination module is conceptually
63   /// equivalent to the specified type in the source module.
64   void addTypeMapping(Type *DstTy, Type *SrcTy);
66   /// Produce a body for an opaque type in the dest module from a type
67   /// definition in the source module.
68   void linkDefinedTypeBodies();
70   /// Return the mapped type to use for the specified input type from the
71   /// source module.
72   Type *get(Type *SrcTy);
74   FunctionType *get(FunctionType *T) {
75     return cast<FunctionType>(get((Type *)T));
76   }
78   /// Dump out the type map for debugging purposes.
79   void dump() const {
80     for (auto &Pair : MappedTypes) {
81       dbgs() << "TypeMap: ";
82       Pair.first->print(dbgs());
83       dbgs() << " => ";
84       Pair.second->print(dbgs());
85       dbgs() << '\n';
86     }
87   }
89 private:
90   Type *remapType(Type *SrcTy) override { return get(SrcTy); }
92   bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
93 };
94 }
96 void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
97   // Check to see if these types are recursively isomorphic and establish a
98   // mapping between them if so.
99   if (areTypesIsomorphic(DstTy, SrcTy)) {
100     SpeculativeTypes.clear();
101     return;
102   }
104   // Oops, they aren't isomorphic. Just discard this request by rolling out
105   // any speculative mappings we've established.
106   unsigned Removed = 0;
107   for (unsigned I = 0, E = SpeculativeTypes.size(); I != E; ++I) {
108     Type *SrcTy = SpeculativeTypes[I];
109     auto Iter = MappedTypes.find(SrcTy);
110     auto *DstTy = dyn_cast<StructType>(Iter->second);
111     if (DstTy && DstResolvedOpaqueTypes.erase(DstTy))
112       Removed++;
113     MappedTypes.erase(Iter);
114   }
115   SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() - Removed);
116   SpeculativeTypes.clear();
119 /// Recursively walk this pair of types, returning true if they are isomorphic,
120 /// false if they are not.
121 bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
122   // Two types with differing kinds are clearly not isomorphic.
123   if (DstTy->getTypeID() != SrcTy->getTypeID())
124     return false;
126   // If we have an entry in the MappedTypes table, then we have our answer.
127   Type *&Entry = MappedTypes[SrcTy];
128   if (Entry)
129     return Entry == DstTy;
131   // Two identical types are clearly isomorphic.  Remember this
132   // non-speculatively.
133   if (DstTy == SrcTy) {
134     Entry = DstTy;
135     return true;
136   }
138   // Okay, we have two types with identical kinds that we haven't seen before.
140   // If this is an opaque struct type, special case it.
141   if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
142     // Mapping an opaque type to any struct, just keep the dest struct.
143     if (SSTy->isOpaque()) {
144       Entry = DstTy;
145       SpeculativeTypes.push_back(SrcTy);
146       return true;
147     }
149     // Mapping a non-opaque source type to an opaque dest.  If this is the first
150     // type that we're mapping onto this destination type then we succeed.  Keep
151     // the dest, but fill it in later. If this is the second (different) type
152     // that we're trying to map onto the same opaque type then we fail.
153     if (cast<StructType>(DstTy)->isOpaque()) {
154       // We can only map one source type onto the opaque destination type.
155       if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)
156         return false;
157       SrcDefinitionsToResolve.push_back(SSTy);
158       SpeculativeTypes.push_back(SrcTy);
159       Entry = DstTy;
160       return true;
161     }
162   }
164   // If the number of subtypes disagree between the two types, then we fail.
165   if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
166     return false;
168   // Fail if any of the extra properties (e.g. array size) of the type disagree.
169   if (isa<IntegerType>(DstTy))
170     return false;  // bitwidth disagrees.
171   if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
172     if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
173       return false;
175   } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
176     if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
177       return false;
178   } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
179     StructType *SSTy = cast<StructType>(SrcTy);
180     if (DSTy->isLiteral() != SSTy->isLiteral() ||
181         DSTy->isPacked() != SSTy->isPacked())
182       return false;
183   } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
184     if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
185       return false;
186   } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
187     if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements())
188       return false;
189   }
191   // Otherwise, we speculate that these two types will line up and recursively
192   // check the subelements.
193   Entry = DstTy;
194   SpeculativeTypes.push_back(SrcTy);
196   for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I)
197     if (!areTypesIsomorphic(DstTy->getContainedType(I),
198                             SrcTy->getContainedType(I)))
199       return false;
201   // If everything seems to have lined up, then everything is great.
202   return true;
205 void TypeMapTy::linkDefinedTypeBodies() {
206   SmallVector<Type*, 16> Elements;
207   for (StructType *SrcSTy : SrcDefinitionsToResolve) {
208     StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
209     assert(DstSTy->isOpaque());
211     // Map the body of the source type over to a new body for the dest type.
212     Elements.resize(SrcSTy->getNumElements());
213     for (unsigned I = 0, E = Elements.size(); I != E; ++I)
214       Elements[I] = get(SrcSTy->getElementType(I));
216     DstSTy->setBody(Elements, SrcSTy->isPacked());
217   }
218   SrcDefinitionsToResolve.clear();
219   DstResolvedOpaqueTypes.clear();
222 Type *TypeMapTy::get(Type *Ty) {
223   // If we already have an entry for this type, return it.
224   Type **Entry = &MappedTypes[Ty];
225   if (*Entry)
226     return *Entry;
228   // If this is not a named struct type, then just map all of the elements and
229   // then rebuild the type from inside out.
230   if (!isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral()) {
231     // If there are no element types to map, then the type is itself.  This is
232     // true for the anonymous {} struct, things like 'float', integers, etc.
233     if (Ty->getNumContainedTypes() == 0)
234       return *Entry = Ty;
236     // Remap all of the elements, keeping track of whether any of them change.
237     bool AnyChange = false;
238     SmallVector<Type*, 4> ElementTypes;
239     ElementTypes.resize(Ty->getNumContainedTypes());
240     for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
241       ElementTypes[I] = get(Ty->getContainedType(I));
242       AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
243     }
245     // If we found our type while recursively processing stuff, just use it.
246     Entry = &MappedTypes[Ty];
247     if (*Entry)
248       return *Entry;
250     // If all of the element types mapped directly over, then the type is usable
251     // as-is.
252     if (!AnyChange)
253       return *Entry = Ty;
255     // Otherwise, rebuild a modified type.
256     switch (Ty->getTypeID()) {
257     default:
258       llvm_unreachable("unknown derived type to remap");
259     case Type::ArrayTyID:
260       return *Entry = ArrayType::get(ElementTypes[0],
261                                      cast<ArrayType>(Ty)->getNumElements());
262     case Type::VectorTyID:
263       return *Entry = VectorType::get(ElementTypes[0],
264                                       cast<VectorType>(Ty)->getNumElements());
265     case Type::PointerTyID:
266       return *Entry = PointerType::get(
267                  ElementTypes[0], cast<PointerType>(Ty)->getAddressSpace());
268     case Type::FunctionTyID:
269       return *Entry = FunctionType::get(ElementTypes[0],
270                                         makeArrayRef(ElementTypes).slice(1),
271                                         cast<FunctionType>(Ty)->isVarArg());
272     case Type::StructTyID:
273       // Note that this is only reached for anonymous structs.
274       return *Entry = StructType::get(Ty->getContext(), ElementTypes,
275                                       cast<StructType>(Ty)->isPacked());
276     }
277   }
279   // Otherwise, this is an unmapped named struct.  If the struct can be directly
280   // mapped over, just use it as-is.  This happens in a case when the linked-in
281   // module has something like:
282   //   %T = type {%T*, i32}
283   //   @GV = global %T* null
284   // where T does not exist at all in the destination module.
285   //
286   // The other case we watch for is when the type is not in the destination
287   // module, but that it has to be rebuilt because it refers to something that
288   // is already mapped.  For example, if the destination module has:
289   //  %A = type { i32 }
290   // and the source module has something like
291   //  %A' = type { i32 }
292   //  %B = type { %A'* }
293   //  @GV = global %B* null
294   // then we want to create a new type: "%B = type { %A*}" and have it take the
295   // pristine "%B" name from the source module.
296   //
297   // To determine which case this is, we have to recursively walk the type graph
298   // speculating that we'll be able to reuse it unmodified.  Only if this is
299   // safe would we map the entire thing over.  Because this is an optimization,
300   // and is not required for the prettiness of the linked module, we just skip
301   // it and always rebuild a type here.
302   StructType *STy = cast<StructType>(Ty);
304   // If the type is opaque, we can just use it directly.
305   if (STy->isOpaque()) {
306     // A named structure type from src module is used. Add it to the Set of
307     // identified structs in the destination module.
308     DstStructTypesSet.insert(STy);
309     return *Entry = STy;
310   }
312   // Otherwise we create a new type.
313   StructType *DTy = StructType::create(STy->getContext());
314   // A new identified structure type was created. Add it to the set of
315   // identified structs in the destination module.
316   DstStructTypesSet.insert(DTy);
317   *Entry = DTy;
319   SmallVector<Type*, 4> ElementTypes;
320   ElementTypes.resize(STy->getNumElements());
321   for (unsigned I = 0, E = ElementTypes.size(); I != E; ++I)
322     ElementTypes[I] = get(STy->getElementType(I));
323   DTy->setBody(ElementTypes, STy->isPacked());
325   // Steal STy's name.
326   if (STy->hasName()) {
327     SmallString<16> TmpName = STy->getName();
328     STy->setName("");
329     DTy->setName(TmpName);
330   }
332   return DTy;
335 //===----------------------------------------------------------------------===//
336 // ModuleLinker implementation.
337 //===----------------------------------------------------------------------===//
339 namespace {
340 class ModuleLinker;
342 /// Creates prototypes for functions that are lazily linked on the fly. This
343 /// speeds up linking for modules with many/ lazily linked functions of which
344 /// few get used.
345 class ValueMaterializerTy : public ValueMaterializer {
346   TypeMapTy &TypeMap;
347   Module *DstM;
348   std::vector<Function *> &LazilyLinkFunctions;
350 public:
351   ValueMaterializerTy(TypeMapTy &TypeMap, Module *DstM,
352                       std::vector<Function *> &LazilyLinkFunctions)
353       : ValueMaterializer(), TypeMap(TypeMap), DstM(DstM),
354         LazilyLinkFunctions(LazilyLinkFunctions) {}
356   Value *materializeValueFor(Value *V) override;
357 };
359 class LinkDiagnosticInfo : public DiagnosticInfo {
360   const Twine &Msg;
362 public:
363   LinkDiagnosticInfo(DiagnosticSeverity Severity, const Twine &Msg);
364   void print(DiagnosticPrinter &DP) const override;
365 };
366 LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity,
367                                        const Twine &Msg)
368     : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}
369 void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
371 /// This is an implementation class for the LinkModules function, which is the
372 /// entrypoint for this file.
373 class ModuleLinker {
374   Module *DstM, *SrcM;
376   TypeMapTy TypeMap;
377   ValueMaterializerTy ValMaterializer;
379   /// Mapping of values from what they used to be in Src, to what they are now
380   /// in DstM.  ValueToValueMapTy is a ValueMap, which involves some overhead
381   /// due to the use of Value handles which the Linker doesn't actually need,
382   /// but this allows us to reuse the ValueMapper code.
383   ValueToValueMapTy ValueMap;
385   struct AppendingVarInfo {
386     GlobalVariable *NewGV;   // New aggregate global in dest module.
387     const Constant *DstInit; // Old initializer from dest module.
388     const Constant *SrcInit; // Old initializer from src module.
389   };
391   std::vector<AppendingVarInfo> AppendingVars;
393   // Set of items not to link in from source.
394   SmallPtrSet<const Value *, 16> DoNotLinkFromSource;
396   // Vector of functions to lazily link in.
397   std::vector<Function *> LazilyLinkFunctions;
399   Linker::DiagnosticHandlerFunction DiagnosticHandler;
401 public:
402   ModuleLinker(Module *dstM, TypeSet &Set, Module *srcM,
403                Linker::DiagnosticHandlerFunction DiagnosticHandler)
404       : DstM(dstM), SrcM(srcM), TypeMap(Set),
405         ValMaterializer(TypeMap, DstM, LazilyLinkFunctions),
406         DiagnosticHandler(DiagnosticHandler) {}
408   bool run();
410 private:
411   bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest,
412                             const GlobalValue &Src);
414   /// Helper method for setting a message and returning an error code.
415   bool emitError(const Twine &Message) {
416     DiagnosticHandler(LinkDiagnosticInfo(DS_Error, Message));
417     return true;
418   }
420   void emitWarning(const Twine &Message) {
421     DiagnosticHandler(LinkDiagnosticInfo(DS_Warning, Message));
422   }
424   bool getComdatLeader(Module *M, StringRef ComdatName,
425                        const GlobalVariable *&GVar);
426   bool computeResultingSelectionKind(StringRef ComdatName,
427                                      Comdat::SelectionKind Src,
428                                      Comdat::SelectionKind Dst,
429                                      Comdat::SelectionKind &Result,
430                                      bool &LinkFromSrc);
431   std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>>
432       ComdatsChosen;
433   bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK,
434                        bool &LinkFromSrc);
436   /// Given a global in the source module, return the global in the
437   /// destination module that is being linked to, if any.
438   GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
439     // If the source has no name it can't link.  If it has local linkage,
440     // there is no name match-up going on.
441     if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
442       return nullptr;
444     // Otherwise see if we have a match in the destination module's symtab.
445     GlobalValue *DGV = DstM->getNamedValue(SrcGV->getName());
446     if (!DGV)
447       return nullptr;
449     // If we found a global with the same name in the dest module, but it has
450     // internal linkage, we are really not doing any linkage here.
451     if (DGV->hasLocalLinkage())
452       return nullptr;
454     // Otherwise, we do in fact link to the destination global.
455     return DGV;
456   }
458   void computeTypeMapping();
460   void upgradeMismatchedGlobalArray(StringRef Name);
461   void upgradeMismatchedGlobals();
463   bool linkAppendingVarProto(GlobalVariable *DstGV,
464                              const GlobalVariable *SrcGV);
466   bool linkGlobalValueProto(GlobalValue *GV);
467   GlobalValue *linkGlobalVariableProto(const GlobalVariable *SGVar,
468                                        GlobalValue *DGV, bool LinkFromSrc);
469   GlobalValue *linkFunctionProto(const Function *SF, GlobalValue *DGV,
470                                  bool LinkFromSrc);
471   GlobalValue *linkGlobalAliasProto(const GlobalAlias *SGA, GlobalValue *DGV,
472                                     bool LinkFromSrc);
474   bool linkModuleFlagsMetadata();
476   void linkAppendingVarInit(const AppendingVarInfo &AVI);
477   void linkGlobalInits();
478   void linkFunctionBody(Function *Dst, Function *Src);
479   void linkAliasBodies();
480   void linkNamedMDNodes();
481 };
484 /// The LLVM SymbolTable class autorenames globals that conflict in the symbol
485 /// table. This is good for all clients except for us. Go through the trouble
486 /// to force this back.
487 static void forceRenaming(GlobalValue *GV, StringRef Name) {
488   // If the global doesn't force its name or if it already has the right name,
489   // there is nothing for us to do.
490   if (GV->hasLocalLinkage() || GV->getName() == Name)
491     return;
493   Module *M = GV->getParent();
495   // If there is a conflict, rename the conflict.
496   if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
497     GV->takeName(ConflictGV);
498     ConflictGV->setName(Name);    // This will cause ConflictGV to get renamed
499     assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
500   } else {
501     GV->setName(Name);              // Force the name back
502   }
505 /// copy additional attributes (those not needed to construct a GlobalValue)
506 /// from the SrcGV to the DestGV.
507 static void copyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) {
508   // Use the maximum alignment, rather than just copying the alignment of SrcGV.
509   auto *DestGO = dyn_cast<GlobalObject>(DestGV);
510   unsigned Alignment;
511   if (DestGO)
512     Alignment = std::max(DestGO->getAlignment(), SrcGV->getAlignment());
514   DestGV->copyAttributesFrom(SrcGV);
516   if (DestGO)
517     DestGO->setAlignment(Alignment);
519   forceRenaming(DestGV, SrcGV->getName());
522 static bool isLessConstraining(GlobalValue::VisibilityTypes a,
523                                GlobalValue::VisibilityTypes b) {
524   if (a == GlobalValue::HiddenVisibility)
525     return false;
526   if (b == GlobalValue::HiddenVisibility)
527     return true;
528   if (a == GlobalValue::ProtectedVisibility)
529     return false;
530   if (b == GlobalValue::ProtectedVisibility)
531     return true;
532   return false;
535 Value *ValueMaterializerTy::materializeValueFor(Value *V) {
536   Function *SF = dyn_cast<Function>(V);
537   if (!SF)
538     return nullptr;
540   Function *DF = Function::Create(TypeMap.get(SF->getFunctionType()),
541                                   SF->getLinkage(), SF->getName(), DstM);
542   copyGVAttributes(DF, SF);
544   if (Comdat *SC = SF->getComdat()) {
545     Comdat *DC = DstM->getOrInsertComdat(SC->getName());
546     DF->setComdat(DC);
547   }
549   LazilyLinkFunctions.push_back(SF);
550   return DF;
553 bool ModuleLinker::getComdatLeader(Module *M, StringRef ComdatName,
554                                    const GlobalVariable *&GVar) {
555   const GlobalValue *GVal = M->getNamedValue(ComdatName);
556   if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) {
557     GVal = GA->getBaseObject();
558     if (!GVal)
559       // We cannot resolve the size of the aliasee yet.
560       return emitError("Linking COMDATs named '" + ComdatName +
561                        "': COMDAT key involves incomputable alias size.");
562   }
564   GVar = dyn_cast_or_null<GlobalVariable>(GVal);
565   if (!GVar)
566     return emitError(
567         "Linking COMDATs named '" + ComdatName +
568         "': GlobalVariable required for data dependent selection!");
570   return false;
573 bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName,
574                                                  Comdat::SelectionKind Src,
575                                                  Comdat::SelectionKind Dst,
576                                                  Comdat::SelectionKind &Result,
577                                                  bool &LinkFromSrc) {
578   // The ability to mix Comdat::SelectionKind::Any with
579   // Comdat::SelectionKind::Largest is a behavior that comes from COFF.
580   bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any ||
581                          Dst == Comdat::SelectionKind::Largest;
582   bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any ||
583                          Src == Comdat::SelectionKind::Largest;
584   if (DstAnyOrLargest && SrcAnyOrLargest) {
585     if (Dst == Comdat::SelectionKind::Largest ||
586         Src == Comdat::SelectionKind::Largest)
587       Result = Comdat::SelectionKind::Largest;
588     else
589       Result = Comdat::SelectionKind::Any;
590   } else if (Src == Dst) {
591     Result = Dst;
592   } else {
593     return emitError("Linking COMDATs named '" + ComdatName +
594                      "': invalid selection kinds!");
595   }
597   switch (Result) {
598   case Comdat::SelectionKind::Any:
599     // Go with Dst.
600     LinkFromSrc = false;
601     break;
602   case Comdat::SelectionKind::NoDuplicates:
603     return emitError("Linking COMDATs named '" + ComdatName +
604                      "': noduplicates has been violated!");
605   case Comdat::SelectionKind::ExactMatch:
606   case Comdat::SelectionKind::Largest:
607   case Comdat::SelectionKind::SameSize: {
608     const GlobalVariable *DstGV;
609     const GlobalVariable *SrcGV;
610     if (getComdatLeader(DstM, ComdatName, DstGV) ||
611         getComdatLeader(SrcM, ComdatName, SrcGV))
612       return true;
614     const DataLayout *DstDL = DstM->getDataLayout();
615     const DataLayout *SrcDL = SrcM->getDataLayout();
616     if (!DstDL || !SrcDL) {
617       return emitError(
618           "Linking COMDATs named '" + ComdatName +
619           "': can't do size dependent selection without DataLayout!");
620     }
621     uint64_t DstSize =
622         DstDL->getTypeAllocSize(DstGV->getType()->getPointerElementType());
623     uint64_t SrcSize =
624         SrcDL->getTypeAllocSize(SrcGV->getType()->getPointerElementType());
625     if (Result == Comdat::SelectionKind::ExactMatch) {
626       if (SrcGV->getInitializer() != DstGV->getInitializer())
627         return emitError("Linking COMDATs named '" + ComdatName +
628                          "': ExactMatch violated!");
629       LinkFromSrc = false;
630     } else if (Result == Comdat::SelectionKind::Largest) {
631       LinkFromSrc = SrcSize > DstSize;
632     } else if (Result == Comdat::SelectionKind::SameSize) {
633       if (SrcSize != DstSize)
634         return emitError("Linking COMDATs named '" + ComdatName +
635                          "': SameSize violated!");
636       LinkFromSrc = false;
637     } else {
638       llvm_unreachable("unknown selection kind");
639     }
640     break;
641   }
642   }
644   return false;
647 bool ModuleLinker::getComdatResult(const Comdat *SrcC,
648                                    Comdat::SelectionKind &Result,
649                                    bool &LinkFromSrc) {
650   Comdat::SelectionKind SSK = SrcC->getSelectionKind();
651   StringRef ComdatName = SrcC->getName();
652   Module::ComdatSymTabType &ComdatSymTab = DstM->getComdatSymbolTable();
653   Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName);
655   if (DstCI == ComdatSymTab.end()) {
656     // Use the comdat if it is only available in one of the modules.
657     LinkFromSrc = true;
658     Result = SSK;
659     return false;
660   }
662   const Comdat *DstC = &DstCI->second;
663   Comdat::SelectionKind DSK = DstC->getSelectionKind();
664   return computeResultingSelectionKind(ComdatName, SSK, DSK, Result,
665                                        LinkFromSrc);
668 bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
669                                         const GlobalValue &Dest,
670                                         const GlobalValue &Src) {
671   // We always have to add Src if it has appending linkage.
672   if (Src.hasAppendingLinkage()) {
673     LinkFromSrc = true;
674     return false;
675   }
677   bool SrcIsDeclaration = Src.isDeclarationForLinker();
678   bool DestIsDeclaration = Dest.isDeclarationForLinker();
680   if (SrcIsDeclaration) {
681     // If Src is external or if both Src & Dest are external..  Just link the
682     // external globals, we aren't adding anything.
683     if (Src.hasDLLImportStorageClass()) {
684       // If one of GVs is marked as DLLImport, result should be dllimport'ed.
685       LinkFromSrc = DestIsDeclaration;
686       return false;
687     }
688     // If the Dest is weak, use the source linkage.
689     LinkFromSrc = Dest.hasExternalWeakLinkage();
690     return false;
691   }
693   if (DestIsDeclaration) {
694     // If Dest is external but Src is not:
695     LinkFromSrc = true;
696     return false;
697   }
699   if (Src.hasCommonLinkage()) {
700     if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) {
701       LinkFromSrc = true;
702       return false;
703     }
705     if (!Dest.hasCommonLinkage()) {
706       LinkFromSrc = false;
707       return false;
708     }
710     // FIXME: Make datalayout mandatory and just use getDataLayout().
711     DataLayout DL(Dest.getParent());
713     uint64_t DestSize = DL.getTypeAllocSize(Dest.getType()->getElementType());
714     uint64_t SrcSize = DL.getTypeAllocSize(Src.getType()->getElementType());
715     LinkFromSrc = SrcSize > DestSize;
716     return false;
717   }
719   if (Src.isWeakForLinker()) {
720     assert(!Dest.hasExternalWeakLinkage());
721     assert(!Dest.hasAvailableExternallyLinkage());
723     if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage()) {
724       LinkFromSrc = true;
725       return false;
726     }
728     LinkFromSrc = false;
729     return false;
730   }
732   if (Dest.isWeakForLinker()) {
733     assert(Src.hasExternalLinkage());
734     LinkFromSrc = true;
735     return false;
736   }
738   assert(!Src.hasExternalWeakLinkage());
739   assert(!Dest.hasExternalWeakLinkage());
740   assert(Dest.hasExternalLinkage() && Src.hasExternalLinkage() &&
741          "Unexpected linkage type!");
742   return emitError("Linking globals named '" + Src.getName() +
743                    "': symbol multiply defined!");
746 /// Loop over all of the linked values to compute type mappings.  For example,
747 /// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct
748 /// types 'Foo' but one got renamed when the module was loaded into the same
749 /// LLVMContext.
750 void ModuleLinker::computeTypeMapping() {
751   for (GlobalValue &SGV : SrcM->globals()) {
752     GlobalValue *DGV = getLinkedToGlobal(&SGV);
753     if (!DGV)
754       continue;
756     if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {
757       TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
758       continue;
759     }
761     // Unify the element type of appending arrays.
762     ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType());
763     ArrayType *SAT = cast<ArrayType>(SGV.getType()->getElementType());
764     TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
765   }
767   for (GlobalValue &SGV : *SrcM) {
768     if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
769       TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
770   }
772   for (GlobalValue &SGV : SrcM->aliases()) {
773     if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
774       TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
775   }
777   // Incorporate types by name, scanning all the types in the source module.
778   // At this point, the destination module may have a type "%foo = { i32 }" for
779   // example.  When the source module got loaded into the same LLVMContext, if
780   // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
781   TypeFinder SrcStructTypes;
782   SrcStructTypes.run(*SrcM, true);
783   SmallPtrSet<StructType*, 32> SrcStructTypesSet(SrcStructTypes.begin(),
784                                                  SrcStructTypes.end());
786   for (unsigned i = 0, e = SrcStructTypes.size(); i != e; ++i) {
787     StructType *ST = SrcStructTypes[i];
788     if (!ST->hasName()) continue;
790     // Check to see if there is a dot in the name followed by a digit.
791     size_t DotPos = ST->getName().rfind('.');
792     if (DotPos == 0 || DotPos == StringRef::npos ||
793         ST->getName().back() == '.' ||
794         !isdigit(static_cast<unsigned char>(ST->getName()[DotPos+1])))
795       continue;
797     // Check to see if the destination module has a struct with the prefix name.
798     if (StructType *DST = DstM->getTypeByName(ST->getName().substr(0, DotPos)))
799       // Don't use it if this actually came from the source module. They're in
800       // the same LLVMContext after all. Also don't use it unless the type is
801       // actually used in the destination module. This can happen in situations
802       // like this:
803       //
804       //      Module A                         Module B
805       //      --------                         --------
806       //   %Z = type { %A }                %B = type { %C.1 }
807       //   %A = type { %B.1, [7 x i8] }    %C.1 = type { i8* }
808       //   %B.1 = type { %C }              %A.2 = type { %B.3, [5 x i8] }
809       //   %C = type { i8* }               %B.3 = type { %C.1 }
810       //
811       // When we link Module B with Module A, the '%B' in Module B is
812       // used. However, that would then use '%C.1'. But when we process '%C.1',
813       // we prefer to take the '%C' version. So we are then left with both
814       // '%C.1' and '%C' being used for the same types. This leads to some
815       // variables using one type and some using the other.
816       if (!SrcStructTypesSet.count(DST) && TypeMap.DstStructTypesSet.count(DST))
817         TypeMap.addTypeMapping(DST, ST);
818   }
820   // Now that we have discovered all of the type equivalences, get a body for
821   // any 'opaque' types in the dest module that are now resolved.
822   TypeMap.linkDefinedTypeBodies();
825 static void upgradeGlobalArray(GlobalVariable *GV) {
826   ArrayType *ATy = cast<ArrayType>(GV->getType()->getElementType());
827   StructType *OldTy = cast<StructType>(ATy->getElementType());
828   assert(OldTy->getNumElements() == 2 && "Expected to upgrade from 2 elements");
830   // Get the upgraded 3 element type.
831   PointerType *VoidPtrTy = Type::getInt8Ty(GV->getContext())->getPointerTo();
832   Type *Tys[3] = {OldTy->getElementType(0), OldTy->getElementType(1),
833                   VoidPtrTy};
834   StructType *NewTy = StructType::get(GV->getContext(), Tys, false);
836   // Build new constants with a null third field filled in.
837   Constant *OldInitC = GV->getInitializer();
838   ConstantArray *OldInit = dyn_cast<ConstantArray>(OldInitC);
839   if (!OldInit && !isa<ConstantAggregateZero>(OldInitC))
840     // Invalid initializer; give up.
841     return;
842   std::vector<Constant *> Initializers;
843   if (OldInit && OldInit->getNumOperands()) {
844     Value *Null = Constant::getNullValue(VoidPtrTy);
845     for (Use &U : OldInit->operands()) {
846       ConstantStruct *Init = cast<ConstantStruct>(U.get());
847       Initializers.push_back(ConstantStruct::get(
848           NewTy, Init->getOperand(0), Init->getOperand(1), Null, nullptr));
849     }
850   }
851   assert(Initializers.size() == ATy->getNumElements() &&
852          "Failed to copy all array elements");
854   // Replace the old GV with a new one.
855   ATy = ArrayType::get(NewTy, Initializers.size());
856   Constant *NewInit = ConstantArray::get(ATy, Initializers);
857   GlobalVariable *NewGV = new GlobalVariable(
858       *GV->getParent(), ATy, GV->isConstant(), GV->getLinkage(), NewInit, "",
859       GV, GV->getThreadLocalMode(), GV->getType()->getAddressSpace(),
860       GV->isExternallyInitialized());
861   NewGV->copyAttributesFrom(GV);
862   NewGV->takeName(GV);
863   assert(GV->use_empty() && "program cannot use initializer list");
864   GV->eraseFromParent();
867 void ModuleLinker::upgradeMismatchedGlobalArray(StringRef Name) {
868   // Look for the global arrays.
869   auto *DstGV = dyn_cast_or_null<GlobalVariable>(DstM->getNamedValue(Name));
870   if (!DstGV)
871     return;
872   auto *SrcGV = dyn_cast_or_null<GlobalVariable>(SrcM->getNamedValue(Name));
873   if (!SrcGV)
874     return;
876   // Check if the types already match.
877   auto *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
878   auto *SrcTy =
879       cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()));
880   if (DstTy == SrcTy)
881     return;
883   // Grab the element types.  We can only upgrade an array of a two-field
884   // struct.  Only bother if the other one has three-fields.
885   auto *DstEltTy = cast<StructType>(DstTy->getElementType());
886   auto *SrcEltTy = cast<StructType>(SrcTy->getElementType());
887   if (DstEltTy->getNumElements() == 2 && SrcEltTy->getNumElements() == 3) {
888     upgradeGlobalArray(DstGV);
889     return;
890   }
891   if (DstEltTy->getNumElements() == 3 && SrcEltTy->getNumElements() == 2)
892     upgradeGlobalArray(SrcGV);
894   // We can't upgrade any other differences.
897 void ModuleLinker::upgradeMismatchedGlobals() {
898   upgradeMismatchedGlobalArray("llvm.global_ctors");
899   upgradeMismatchedGlobalArray("llvm.global_dtors");
902 /// If there were any appending global variables, link them together now.
903 /// Return true on error.
904 bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV,
905                                          const GlobalVariable *SrcGV) {
907   if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
908     return emitError("Linking globals named '" + SrcGV->getName() +
909            "': can only link appending global with another appending global!");
911   ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
912   ArrayType *SrcTy =
913     cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()));
914   Type *EltTy = DstTy->getElementType();
916   // Check to see that they two arrays agree on type.
917   if (EltTy != SrcTy->getElementType())
918     return emitError("Appending variables with different element types!");
919   if (DstGV->isConstant() != SrcGV->isConstant())
920     return emitError("Appending variables linked with different const'ness!");
922   if (DstGV->getAlignment() != SrcGV->getAlignment())
923     return emitError(
924              "Appending variables with different alignment need to be linked!");
926   if (DstGV->getVisibility() != SrcGV->getVisibility())
927     return emitError(
928             "Appending variables with different visibility need to be linked!");
930   if (DstGV->hasUnnamedAddr() != SrcGV->hasUnnamedAddr())
931     return emitError(
932         "Appending variables with different unnamed_addr need to be linked!");
934   if (StringRef(DstGV->getSection()) != SrcGV->getSection())
935     return emitError(
936           "Appending variables with different section name need to be linked!");
938   uint64_t NewSize = DstTy->getNumElements() + SrcTy->getNumElements();
939   ArrayType *NewType = ArrayType::get(EltTy, NewSize);
941   // Create the new global variable.
942   GlobalVariable *NG =
943     new GlobalVariable(*DstGV->getParent(), NewType, SrcGV->isConstant(),
944                        DstGV->getLinkage(), /*init*/nullptr, /*name*/"", DstGV,
945                        DstGV->getThreadLocalMode(),
946                        DstGV->getType()->getAddressSpace());
948   // Propagate alignment, visibility and section info.
949   copyGVAttributes(NG, DstGV);
951   AppendingVarInfo AVI;
952   AVI.NewGV = NG;
953   AVI.DstInit = DstGV->getInitializer();
954   AVI.SrcInit = SrcGV->getInitializer();
955   AppendingVars.push_back(AVI);
957   // Replace any uses of the two global variables with uses of the new
958   // global.
959   ValueMap[SrcGV] = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
961   DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
962   DstGV->eraseFromParent();
964   // Track the source variable so we don't try to link it.
965   DoNotLinkFromSource.insert(SrcGV);
967   return false;
970 bool ModuleLinker::linkGlobalValueProto(GlobalValue *SGV) {
971   GlobalValue *DGV = getLinkedToGlobal(SGV);
973   // Handle the ultra special appending linkage case first.
974   if (DGV && DGV->hasAppendingLinkage())
975     return linkAppendingVarProto(cast<GlobalVariable>(DGV),
976                                  cast<GlobalVariable>(SGV));
978   bool LinkFromSrc = true;
979   Comdat *C = nullptr;
980   GlobalValue::VisibilityTypes Visibility = SGV->getVisibility();
981   bool HasUnnamedAddr = SGV->hasUnnamedAddr();
983   if (const Comdat *SC = SGV->getComdat()) {
984     Comdat::SelectionKind SK;
985     std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
986     C = DstM->getOrInsertComdat(SC->getName());
987     C->setSelectionKind(SK);
988   } else if (DGV) {
989     if (shouldLinkFromSource(LinkFromSrc, *DGV, *SGV))
990       return true;
991   }
993   if (!LinkFromSrc) {
994     // Track the source global so that we don't attempt to copy it over when
995     // processing global initializers.
996     DoNotLinkFromSource.insert(SGV);
998     if (DGV)
999       // Make sure to remember this mapping.
1000       ValueMap[SGV] =
1001           ConstantExpr::getBitCast(DGV, TypeMap.get(SGV->getType()));
1002   }
1004   if (DGV) {
1005     Visibility = isLessConstraining(Visibility, DGV->getVisibility())
1006                      ? DGV->getVisibility()
1007                      : Visibility;
1008     HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
1009   }
1011   if (!LinkFromSrc && !DGV)
1012     return false;
1014   GlobalValue *NewGV;
1015   if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) {
1016     NewGV = linkGlobalVariableProto(SGVar, DGV, LinkFromSrc);
1017     if (!NewGV)
1018       return true;
1019   } else if (auto *SF = dyn_cast<Function>(SGV)) {
1020     NewGV = linkFunctionProto(SF, DGV, LinkFromSrc);
1021   } else {
1022     NewGV = linkGlobalAliasProto(cast<GlobalAlias>(SGV), DGV, LinkFromSrc);
1023   }
1025   if (NewGV) {
1026     if (NewGV != DGV)
1027       copyGVAttributes(NewGV, SGV);
1029     NewGV->setUnnamedAddr(HasUnnamedAddr);
1030     NewGV->setVisibility(Visibility);
1032     if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {
1033       if (C)
1034         NewGO->setComdat(C);
1035     }
1037     // Make sure to remember this mapping.
1038     if (NewGV != DGV) {
1039       if (DGV) {
1040         DGV->replaceAllUsesWith(
1041             ConstantExpr::getBitCast(NewGV, DGV->getType()));
1042         DGV->eraseFromParent();
1043       }
1044       ValueMap[SGV] = NewGV;
1045     }
1046   }
1048   return false;
1051 /// Loop through the global variables in the src module and merge them into the
1052 /// dest module.
1053 GlobalValue *ModuleLinker::linkGlobalVariableProto(const GlobalVariable *SGVar,
1054                                                    GlobalValue *DGV,
1055                                                    bool LinkFromSrc) {
1056   unsigned Alignment = 0;
1057   bool ClearConstant = false;
1059   if (DGV) {
1060     if (DGV->hasCommonLinkage() && SGVar->hasCommonLinkage())
1061       Alignment = std::max(SGVar->getAlignment(), DGV->getAlignment());
1063     auto *DGVar = dyn_cast<GlobalVariable>(DGV);
1064     if (!SGVar->isConstant() || (DGVar && !DGVar->isConstant()))
1065       ClearConstant = true;
1066   }
1068   if (!LinkFromSrc) {
1069     if (auto *NewGVar = dyn_cast<GlobalVariable>(DGV)) {
1070       if (Alignment)
1071         NewGVar->setAlignment(Alignment);
1072       if (NewGVar->isDeclaration() && ClearConstant)
1073         NewGVar->setConstant(false);
1074     }
1075     return DGV;
1076   }
1078   // No linking to be performed or linking from the source: simply create an
1079   // identical version of the symbol over in the dest module... the
1080   // initializer will be filled in later by LinkGlobalInits.
1081   GlobalVariable *NewDGV = new GlobalVariable(
1082       *DstM, TypeMap.get(SGVar->getType()->getElementType()),
1083       SGVar->isConstant(), SGVar->getLinkage(), /*init*/ nullptr,
1084       SGVar->getName(), /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
1085       SGVar->getType()->getAddressSpace());
1087   if (Alignment)
1088     NewDGV->setAlignment(Alignment);
1090   return NewDGV;
1093 /// Link the function in the source module into the destination module if
1094 /// needed, setting up mapping information.
1095 GlobalValue *ModuleLinker::linkFunctionProto(const Function *SF,
1096                                              GlobalValue *DGV,
1097                                              bool LinkFromSrc) {
1098   if (!LinkFromSrc)
1099     return DGV;
1101   // If the function is to be lazily linked, don't create it just yet.
1102   // The ValueMaterializerTy will deal with creating it if it's used.
1103   if (!DGV && (SF->hasLocalLinkage() || SF->hasLinkOnceLinkage() ||
1104                SF->hasAvailableExternallyLinkage())) {
1105     DoNotLinkFromSource.insert(SF);
1106     return nullptr;
1107   }
1109   // If there is no linkage to be performed or we are linking from the source,
1110   // bring SF over.
1111   return Function::Create(TypeMap.get(SF->getFunctionType()), SF->getLinkage(),
1112                           SF->getName(), DstM);
1115 /// Set up prototypes for any aliases that come over from the source module.
1116 GlobalValue *ModuleLinker::linkGlobalAliasProto(const GlobalAlias *SGA,
1117                                                 GlobalValue *DGV,
1118                                                 bool LinkFromSrc) {
1119   if (!LinkFromSrc)
1120     return DGV;
1122   // If there is no linkage to be performed or we're linking from the source,
1123   // bring over SGA.
1124   auto *PTy = cast<PointerType>(TypeMap.get(SGA->getType()));
1125   return GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1126                              SGA->getLinkage(), SGA->getName(), DstM);
1129 static void getArrayElements(const Constant *C,
1130                              SmallVectorImpl<Constant *> &Dest) {
1131   unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
1133   for (unsigned i = 0; i != NumElements; ++i)
1134     Dest.push_back(C->getAggregateElement(i));
1137 void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) {
1138   // Merge the initializer.
1139   SmallVector<Constant *, 16> DstElements;
1140   getArrayElements(AVI.DstInit, DstElements);
1142   SmallVector<Constant *, 16> SrcElements;
1143   getArrayElements(AVI.SrcInit, SrcElements);
1145   ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType());
1147   StringRef Name = AVI.NewGV->getName();
1148   bool IsNewStructor =
1149       (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") &&
1150       cast<StructType>(NewType->getElementType())->getNumElements() == 3;
1152   for (auto *V : SrcElements) {
1153     if (IsNewStructor) {
1154       Constant *Key = V->getAggregateElement(2);
1155       if (DoNotLinkFromSource.count(Key))
1156         continue;
1157     }
1158     DstElements.push_back(
1159         MapValue(V, ValueMap, RF_None, &TypeMap, &ValMaterializer));
1160   }
1161   if (IsNewStructor) {
1162     NewType = ArrayType::get(NewType->getElementType(), DstElements.size());
1163     AVI.NewGV->mutateType(PointerType::get(NewType, 0));
1164   }
1166   AVI.NewGV->setInitializer(ConstantArray::get(NewType, DstElements));
1169 /// Update the initializers in the Dest module now that all globals that may be
1170 /// referenced are in Dest.
1171 void ModuleLinker::linkGlobalInits() {
1172   // Loop over all of the globals in the src module, mapping them over as we go
1173   for (Module::const_global_iterator I = SrcM->global_begin(),
1174        E = SrcM->global_end(); I != E; ++I) {
1176     // Only process initialized GV's or ones not already in dest.
1177     if (!I->hasInitializer() || DoNotLinkFromSource.count(I)) continue;
1179     // Grab destination global variable.
1180     GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[I]);
1181     // Figure out what the initializer looks like in the dest module.
1182     DGV->setInitializer(MapValue(I->getInitializer(), ValueMap,
1183                                  RF_None, &TypeMap, &ValMaterializer));
1184   }
1187 /// Copy the source function over into the dest function and fix up references
1188 /// to values. At this point we know that Dest is an external function, and
1189 /// that Src is not.
1190 void ModuleLinker::linkFunctionBody(Function *Dst, Function *Src) {
1191   assert(Src && Dst && Dst->isDeclaration() && !Src->isDeclaration());
1193   // Go through and convert function arguments over, remembering the mapping.
1194   Function::arg_iterator DI = Dst->arg_begin();
1195   for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
1196        I != E; ++I, ++DI) {
1197     DI->setName(I->getName());  // Copy the name over.
1199     // Add a mapping to our mapping.
1200     ValueMap[I] = DI;
1201   }
1203   // Splice the body of the source function into the dest function.
1204   Dst->getBasicBlockList().splice(Dst->end(), Src->getBasicBlockList());
1206   // At this point, all of the instructions and values of the function are now
1207   // copied over.  The only problem is that they are still referencing values in
1208   // the Source function as operands.  Loop through all of the operands of the
1209   // functions and patch them up to point to the local versions.
1210   for (Function::iterator BB = Dst->begin(), BE = Dst->end(); BB != BE; ++BB)
1211     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1212       RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries, &TypeMap,
1213                        &ValMaterializer);
1215   // There is no need to map the arguments anymore.
1216   for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
1217        I != E; ++I)
1218     ValueMap.erase(I);
1222 /// Insert all of the aliases in Src into the Dest module.
1223 void ModuleLinker::linkAliasBodies() {
1224   for (Module::alias_iterator I = SrcM->alias_begin(), E = SrcM->alias_end();
1225        I != E; ++I) {
1226     if (DoNotLinkFromSource.count(I))
1227       continue;
1228     if (Constant *Aliasee = I->getAliasee()) {
1229       GlobalAlias *DA = cast<GlobalAlias>(ValueMap[I]);
1230       Constant *Val =
1231           MapValue(Aliasee, ValueMap, RF_None, &TypeMap, &ValMaterializer);
1232       DA->setAliasee(Val);
1233     }
1234   }
1237 /// Insert all of the named MDNodes in Src into the Dest module.
1238 void ModuleLinker::linkNamedMDNodes() {
1239   const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1240   for (Module::const_named_metadata_iterator I = SrcM->named_metadata_begin(),
1241        E = SrcM->named_metadata_end(); I != E; ++I) {
1242     // Don't link module flags here. Do them separately.
1243     if (&*I == SrcModFlags) continue;
1244     NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(I->getName());
1245     // Add Src elements into Dest node.
1246     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1247       DestNMD->addOperand(MapValue(I->getOperand(i), ValueMap,
1248                                    RF_None, &TypeMap, &ValMaterializer));
1249   }
1252 /// Merge the linker flags in Src into the Dest module.
1253 bool ModuleLinker::linkModuleFlagsMetadata() {
1254   // If the source module has no module flags, we are done.
1255   const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1256   if (!SrcModFlags) return false;
1258   // If the destination module doesn't have module flags yet, then just copy
1259   // over the source module's flags.
1260   NamedMDNode *DstModFlags = DstM->getOrInsertModuleFlagsMetadata();
1261   if (DstModFlags->getNumOperands() == 0) {
1262     for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1263       DstModFlags->addOperand(SrcModFlags->getOperand(I));
1265     return false;
1266   }
1268   // First build a map of the existing module flags and requirements.
1269   DenseMap<MDString*, MDNode*> Flags;
1270   SmallSetVector<MDNode*, 16> Requirements;
1271   for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1272     MDNode *Op = DstModFlags->getOperand(I);
1273     ConstantInt *Behavior = cast<ConstantInt>(Op->getOperand(0));
1274     MDString *ID = cast<MDString>(Op->getOperand(1));
1276     if (Behavior->getZExtValue() == Module::Require) {
1277       Requirements.insert(cast<MDNode>(Op->getOperand(2)));
1278     } else {
1279       Flags[ID] = Op;
1280     }
1281   }
1283   // Merge in the flags from the source module, and also collect its set of
1284   // requirements.
1285   bool HasErr = false;
1286   for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1287     MDNode *SrcOp = SrcModFlags->getOperand(I);
1288     ConstantInt *SrcBehavior = cast<ConstantInt>(SrcOp->getOperand(0));
1289     MDString *ID = cast<MDString>(SrcOp->getOperand(1));
1290     MDNode *DstOp = Flags.lookup(ID);
1291     unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
1293     // If this is a requirement, add it and continue.
1294     if (SrcBehaviorValue == Module::Require) {
1295       // If the destination module does not already have this requirement, add
1296       // it.
1297       if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
1298         DstModFlags->addOperand(SrcOp);
1299       }
1300       continue;
1301     }
1303     // If there is no existing flag with this ID, just add it.
1304     if (!DstOp) {
1305       Flags[ID] = SrcOp;
1306       DstModFlags->addOperand(SrcOp);
1307       continue;
1308     }
1310     // Otherwise, perform a merge.
1311     ConstantInt *DstBehavior = cast<ConstantInt>(DstOp->getOperand(0));
1312     unsigned DstBehaviorValue = DstBehavior->getZExtValue();
1314     // If either flag has override behavior, handle it first.
1315     if (DstBehaviorValue == Module::Override) {
1316       // Diagnose inconsistent flags which both have override behavior.
1317       if (SrcBehaviorValue == Module::Override &&
1318           SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1319         HasErr |= emitError("linking module flags '" + ID->getString() +
1320                             "': IDs have conflicting override values");
1321       }
1322       continue;
1323     } else if (SrcBehaviorValue == Module::Override) {
1324       // Update the destination flag to that of the source.
1325       DstOp->replaceOperandWith(0, SrcBehavior);
1326       DstOp->replaceOperandWith(2, SrcOp->getOperand(2));
1327       continue;
1328     }
1330     // Diagnose inconsistent merge behavior types.
1331     if (SrcBehaviorValue != DstBehaviorValue) {
1332       HasErr |= emitError("linking module flags '" + ID->getString() +
1333                           "': IDs have conflicting behaviors");
1334       continue;
1335     }
1337     // Perform the merge for standard behavior types.
1338     switch (SrcBehaviorValue) {
1339     case Module::Require:
1340     case Module::Override: llvm_unreachable("not possible");
1341     case Module::Error: {
1342       // Emit an error if the values differ.
1343       if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1344         HasErr |= emitError("linking module flags '" + ID->getString() +
1345                             "': IDs have conflicting values");
1346       }
1347       continue;
1348     }
1349     case Module::Warning: {
1350       // Emit a warning if the values differ.
1351       if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1352         emitWarning("linking module flags '" + ID->getString() +
1353                     "': IDs have conflicting values");
1354       }
1355       continue;
1356     }
1357     case Module::Append: {
1358       MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1359       MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1360       unsigned NumOps = DstValue->getNumOperands() + SrcValue->getNumOperands();
1361       Value **VP, **Values = VP = new Value*[NumOps];
1362       for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i, ++VP)
1363         *VP = DstValue->getOperand(i);
1364       for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i, ++VP)
1365         *VP = SrcValue->getOperand(i);
1366       DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(),
1367                                                ArrayRef<Value*>(Values,
1368                                                                 NumOps)));
1369       delete[] Values;
1370       break;
1371     }
1372     case Module::AppendUnique: {
1373       SmallSetVector<Value*, 16> Elts;
1374       MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1375       MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1376       for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i)
1377         Elts.insert(DstValue->getOperand(i));
1378       for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i)
1379         Elts.insert(SrcValue->getOperand(i));
1380       DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(),
1381                                                ArrayRef<Value*>(Elts.begin(),
1382                                                                 Elts.end())));
1383       break;
1384     }
1385     }
1386   }
1388   // Check all of the requirements.
1389   for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
1390     MDNode *Requirement = Requirements[I];
1391     MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1392     Value *ReqValue = Requirement->getOperand(1);
1394     MDNode *Op = Flags[Flag];
1395     if (!Op || Op->getOperand(2) != ReqValue) {
1396       HasErr |= emitError("linking module flags '" + Flag->getString() +
1397                           "': does not have the required value");
1398       continue;
1399     }
1400   }
1402   return HasErr;
1405 bool ModuleLinker::run() {
1406   assert(DstM && "Null destination module");
1407   assert(SrcM && "Null source module");
1409   // Inherit the target data from the source module if the destination module
1410   // doesn't have one already.
1411   if (!DstM->getDataLayout() && SrcM->getDataLayout())
1412     DstM->setDataLayout(SrcM->getDataLayout());
1414   // Copy the target triple from the source to dest if the dest's is empty.
1415   if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
1416     DstM->setTargetTriple(SrcM->getTargetTriple());
1418   if (SrcM->getDataLayout() && DstM->getDataLayout() &&
1419       *SrcM->getDataLayout() != *DstM->getDataLayout()) {
1420     emitWarning("Linking two modules of different data layouts: '" +
1421                 SrcM->getModuleIdentifier() + "' is '" +
1422                 SrcM->getDataLayoutStr() + "' whereas '" +
1423                 DstM->getModuleIdentifier() + "' is '" +
1424                 DstM->getDataLayoutStr() + "'\n");
1425   }
1426   if (!SrcM->getTargetTriple().empty() &&
1427       DstM->getTargetTriple() != SrcM->getTargetTriple()) {
1428     emitWarning("Linking two modules of different target triples: " +
1429                 SrcM->getModuleIdentifier() + "' is '" +
1430                 SrcM->getTargetTriple() + "' whereas '" +
1431                 DstM->getModuleIdentifier() + "' is '" +
1432                 DstM->getTargetTriple() + "'\n");
1433   }
1435   // Append the module inline asm string.
1436   if (!SrcM->getModuleInlineAsm().empty()) {
1437     if (DstM->getModuleInlineAsm().empty())
1438       DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm());
1439     else
1440       DstM->setModuleInlineAsm(DstM->getModuleInlineAsm()+"\n"+
1441                                SrcM->getModuleInlineAsm());
1442   }
1444   // Loop over all of the linked values to compute type mappings.
1445   computeTypeMapping();
1447   ComdatsChosen.clear();
1448   for (const auto &SMEC : SrcM->getComdatSymbolTable()) {
1449     const Comdat &C = SMEC.getValue();
1450     if (ComdatsChosen.count(&C))
1451       continue;
1452     Comdat::SelectionKind SK;
1453     bool LinkFromSrc;
1454     if (getComdatResult(&C, SK, LinkFromSrc))
1455       return true;
1456     ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc);
1457   }
1459   // Upgrade mismatched global arrays.
1460   upgradeMismatchedGlobals();
1462   // Insert all of the globals in src into the DstM module... without linking
1463   // initializers (which could refer to functions not yet mapped over).
1464   for (Module::global_iterator I = SrcM->global_begin(),
1465        E = SrcM->global_end(); I != E; ++I)
1466     if (linkGlobalValueProto(I))
1467       return true;
1469   // Link the functions together between the two modules, without doing function
1470   // bodies... this just adds external function prototypes to the DstM
1471   // function...  We do this so that when we begin processing function bodies,
1472   // all of the global values that may be referenced are available in our
1473   // ValueMap.
1474   for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I)
1475     if (linkGlobalValueProto(I))
1476       return true;
1478   // If there were any aliases, link them now.
1479   for (Module::alias_iterator I = SrcM->alias_begin(),
1480        E = SrcM->alias_end(); I != E; ++I)
1481     if (linkGlobalValueProto(I))
1482       return true;
1484   for (unsigned i = 0, e = AppendingVars.size(); i != e; ++i)
1485     linkAppendingVarInit(AppendingVars[i]);
1487   // Link in the function bodies that are defined in the source module into
1488   // DstM.
1489   for (Module::iterator SF = SrcM->begin(), E = SrcM->end(); SF != E; ++SF) {
1490     // Skip if not linking from source.
1491     if (DoNotLinkFromSource.count(SF)) continue;
1493     Function *DF = cast<Function>(ValueMap[SF]);
1494     if (SF->hasPrefixData()) {
1495       // Link in the prefix data.
1496       DF->setPrefixData(MapValue(
1497           SF->getPrefixData(), ValueMap, RF_None, &TypeMap, &ValMaterializer));
1498     }
1500     // Materialize if needed.
1501     if (std::error_code EC = SF->materialize())
1502       return emitError(EC.message());
1504     // Skip if no body (function is external).
1505     if (SF->isDeclaration())
1506       continue;
1508     linkFunctionBody(DF, SF);
1509     SF->Dematerialize();
1510   }
1512   // Resolve all uses of aliases with aliasees.
1513   linkAliasBodies();
1515   // Remap all of the named MDNodes in Src into the DstM module. We do this
1516   // after linking GlobalValues so that MDNodes that reference GlobalValues
1517   // are properly remapped.
1518   linkNamedMDNodes();
1520   // Merge the module flags into the DstM module.
1521   if (linkModuleFlagsMetadata())
1522     return true;
1524   // Update the initializers in the DstM module now that all globals that may
1525   // be referenced are in DstM.
1526   linkGlobalInits();
1528   // Process vector of lazily linked in functions.
1529   bool LinkedInAnyFunctions;
1530   do {
1531     LinkedInAnyFunctions = false;
1533     for(std::vector<Function*>::iterator I = LazilyLinkFunctions.begin(),
1534         E = LazilyLinkFunctions.end(); I != E; ++I) {
1535       Function *SF = *I;
1536       if (!SF)
1537         continue;
1539       Function *DF = cast<Function>(ValueMap[SF]);
1540       if (SF->hasPrefixData()) {
1541         // Link in the prefix data.
1542         DF->setPrefixData(MapValue(SF->getPrefixData(),
1543                                    ValueMap,
1544                                    RF_None,
1545                                    &TypeMap,
1546                                    &ValMaterializer));
1547       }
1549       // Materialize if needed.
1550       if (std::error_code EC = SF->materialize())
1551         return emitError(EC.message());
1553       // Skip if no body (function is external).
1554       if (SF->isDeclaration())
1555         continue;
1557       // Erase from vector *before* the function body is linked - linkFunctionBody could
1558       // invalidate I.
1559       LazilyLinkFunctions.erase(I);
1561       // Link in function body.
1562       linkFunctionBody(DF, SF);
1563       SF->Dematerialize();
1565       // Set flag to indicate we may have more functions to lazily link in
1566       // since we linked in a function.
1567       LinkedInAnyFunctions = true;
1568       break;
1569     }
1570   } while (LinkedInAnyFunctions);
1572   return false;
1575 void Linker::init(Module *M, DiagnosticHandlerFunction DiagnosticHandler) {
1576   this->Composite = M;
1577   this->DiagnosticHandler = DiagnosticHandler;
1579   TypeFinder StructTypes;
1580   StructTypes.run(*M, true);
1581   IdentifiedStructTypes.insert(StructTypes.begin(), StructTypes.end());
1584 Linker::Linker(Module *M, DiagnosticHandlerFunction DiagnosticHandler) {
1585   init(M, DiagnosticHandler);
1588 Linker::Linker(Module *M) {
1589   init(M, [this](const DiagnosticInfo &DI) {
1590     Composite->getContext().diagnose(DI);
1591   });
1594 Linker::~Linker() {
1597 void Linker::deleteModule() {
1598   delete Composite;
1599   Composite = nullptr;
1602 bool Linker::linkInModule(Module *Src) {
1603   ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src,
1604                          DiagnosticHandler);
1605   return TheLinker.run();
1608 //===----------------------------------------------------------------------===//
1609 // LinkModules entrypoint.
1610 //===----------------------------------------------------------------------===//
1612 /// This function links two modules together, with the resulting Dest module
1613 /// modified to be the composite of the two input modules. If an error occurs,
1614 /// true is returned and ErrorMsg (if not null) is set to indicate the problem.
1615 /// Upon failure, the Dest module could be in a modified state, and shouldn't be
1616 /// relied on to be consistent.
1617 bool Linker::LinkModules(Module *Dest, Module *Src,
1618                          DiagnosticHandlerFunction DiagnosticHandler) {
1619   Linker L(Dest, DiagnosticHandler);
1620   return L.linkInModule(Src);
1623 bool Linker::LinkModules(Module *Dest, Module *Src) {
1624   Linker L(Dest);
1625   return L.linkInModule(Src);
1628 //===----------------------------------------------------------------------===//
1629 // C API.
1630 //===----------------------------------------------------------------------===//
1632 LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
1633                          LLVMLinkerMode Mode, char **OutMessages) {
1634   Module *D = unwrap(Dest);
1635   std::string Message;
1636   raw_string_ostream Stream(Message);
1637   DiagnosticPrinterRawOStream DP(Stream);
1639   LLVMBool Result = Linker::LinkModules(
1640       D, unwrap(Src), [&](const DiagnosticInfo &DI) { DI.print(DP); });
1642   if (OutMessages && Result)
1643     *OutMessages = strdup(Message.c_str());
1644   return Result;