• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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/Module.h"
21 #include "llvm/IR/TypeFinder.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Transforms/Utils/Cloning.h"
25 
26 #include <ctype.h>
27 using namespace llvm;
28 
29 //===----------------------------------------------------------------------===//
30 // TypeMap implementation.
31 //===----------------------------------------------------------------------===//
32 
33 namespace {
34   typedef SmallPtrSet<StructType*, 32> TypeSet;
35 
36 class TypeMapTy : public ValueMapTypeRemapper {
37   /// MappedTypes - This is a mapping from a source type to a destination type
38   /// to use.
39   DenseMap<Type*, Type*> MappedTypes;
40 
41   /// SpeculativeTypes - When checking to see if two subgraphs are isomorphic,
42   /// we speculatively add types to MappedTypes, but keep track of them here in
43   /// case we need to roll back.
44   SmallVector<Type*, 16> SpeculativeTypes;
45 
46   /// SrcDefinitionsToResolve - This is a list of non-opaque structs in the
47   /// source module that are mapped to an opaque struct in the destination
48   /// module.
49   SmallVector<StructType*, 16> SrcDefinitionsToResolve;
50 
51   /// DstResolvedOpaqueTypes - This is the set of opaque types in the
52   /// destination modules who are getting a body from the source module.
53   SmallPtrSet<StructType*, 16> DstResolvedOpaqueTypes;
54 
55 public:
TypeMapTy(TypeSet & Set)56   TypeMapTy(TypeSet &Set) : DstStructTypesSet(Set) {}
57 
58   TypeSet &DstStructTypesSet;
59   /// addTypeMapping - Indicate that the specified type in the destination
60   /// module is conceptually equivalent to the specified type in the source
61   /// module.
62   void addTypeMapping(Type *DstTy, Type *SrcTy);
63 
64   /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest
65   /// module from a type definition in the source module.
66   void linkDefinedTypeBodies();
67 
68   /// get - Return the mapped type to use for the specified input type from the
69   /// source module.
70   Type *get(Type *SrcTy);
71 
get(FunctionType * T)72   FunctionType *get(FunctionType *T) {return cast<FunctionType>(get((Type*)T));}
73 
74   /// dump - Dump out the type map for debugging purposes.
dump() const75   void dump() const {
76     for (DenseMap<Type*, Type*>::const_iterator
77            I = MappedTypes.begin(), E = MappedTypes.end(); I != E; ++I) {
78       dbgs() << "TypeMap: ";
79       I->first->dump();
80       dbgs() << " => ";
81       I->second->dump();
82       dbgs() << '\n';
83     }
84   }
85 
86 private:
87   Type *getImpl(Type *T);
88   /// remapType - Implement the ValueMapTypeRemapper interface.
remapType(Type * SrcTy)89   Type *remapType(Type *SrcTy) {
90     return get(SrcTy);
91   }
92 
93   bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
94 };
95 }
96 
addTypeMapping(Type * DstTy,Type * SrcTy)97 void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
98   Type *&Entry = MappedTypes[SrcTy];
99   if (Entry) return;
100 
101   if (DstTy == SrcTy) {
102     Entry = DstTy;
103     return;
104   }
105 
106   // Check to see if these types are recursively isomorphic and establish a
107   // mapping between them if so.
108   if (!areTypesIsomorphic(DstTy, SrcTy)) {
109     // Oops, they aren't isomorphic.  Just discard this request by rolling out
110     // any speculative mappings we've established.
111     for (unsigned i = 0, e = SpeculativeTypes.size(); i != e; ++i)
112       MappedTypes.erase(SpeculativeTypes[i]);
113   }
114   SpeculativeTypes.clear();
115 }
116 
117 /// areTypesIsomorphic - Recursively walk this pair of types, returning true
118 /// if they are isomorphic, false if they are not.
areTypesIsomorphic(Type * DstTy,Type * SrcTy)119 bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
120   // Two types with differing kinds are clearly not isomorphic.
121   if (DstTy->getTypeID() != SrcTy->getTypeID()) return false;
122 
123   // If we have an entry in the MappedTypes table, then we have our answer.
124   Type *&Entry = MappedTypes[SrcTy];
125   if (Entry)
126     return Entry == DstTy;
127 
128   // Two identical types are clearly isomorphic.  Remember this
129   // non-speculatively.
130   if (DstTy == SrcTy) {
131     Entry = DstTy;
132     return true;
133   }
134 
135   // Okay, we have two types with identical kinds that we haven't seen before.
136 
137   // If this is an opaque struct type, special case it.
138   if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
139     // Mapping an opaque type to any struct, just keep the dest struct.
140     if (SSTy->isOpaque()) {
141       Entry = DstTy;
142       SpeculativeTypes.push_back(SrcTy);
143       return true;
144     }
145 
146     // Mapping a non-opaque source type to an opaque dest.  If this is the first
147     // type that we're mapping onto this destination type then we succeed.  Keep
148     // the dest, but fill it in later.  This doesn't need to be speculative.  If
149     // this is the second (different) type that we're trying to map onto the
150     // same opaque type then we fail.
151     if (cast<StructType>(DstTy)->isOpaque()) {
152       // We can only map one source type onto the opaque destination type.
153       if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)))
154         return false;
155       SrcDefinitionsToResolve.push_back(SSTy);
156       Entry = DstTy;
157       return true;
158     }
159   }
160 
161   // If the number of subtypes disagree between the two types, then we fail.
162   if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
163     return false;
164 
165   // Fail if any of the extra properties (e.g. array size) of the type disagree.
166   if (isa<IntegerType>(DstTy))
167     return false;  // bitwidth disagrees.
168   if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
169     if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
170       return false;
171 
172   } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
173     if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
174       return false;
175   } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
176     StructType *SSTy = cast<StructType>(SrcTy);
177     if (DSTy->isLiteral() != SSTy->isLiteral() ||
178         DSTy->isPacked() != SSTy->isPacked())
179       return false;
180   } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
181     if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
182       return false;
183   } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
184     if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements())
185       return false;
186   }
187 
188   // Otherwise, we speculate that these two types will line up and recursively
189   // check the subelements.
190   Entry = DstTy;
191   SpeculativeTypes.push_back(SrcTy);
192 
193   for (unsigned i = 0, e = SrcTy->getNumContainedTypes(); i != e; ++i)
194     if (!areTypesIsomorphic(DstTy->getContainedType(i),
195                             SrcTy->getContainedType(i)))
196       return false;
197 
198   // If everything seems to have lined up, then everything is great.
199   return true;
200 }
201 
202 /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest
203 /// module from a type definition in the source module.
linkDefinedTypeBodies()204 void TypeMapTy::linkDefinedTypeBodies() {
205   SmallVector<Type*, 16> Elements;
206   SmallString<16> TmpName;
207 
208   // Note that processing entries in this loop (calling 'get') can add new
209   // entries to the SrcDefinitionsToResolve vector.
210   while (!SrcDefinitionsToResolve.empty()) {
211     StructType *SrcSTy = SrcDefinitionsToResolve.pop_back_val();
212     StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
213 
214     // TypeMap is a many-to-one mapping, if there were multiple types that
215     // provide a body for DstSTy then previous iterations of this loop may have
216     // already handled it.  Just ignore this case.
217     if (!DstSTy->isOpaque()) continue;
218     assert(!SrcSTy->isOpaque() && "Not resolving a definition?");
219 
220     // Map the body of the source type over to a new body for the dest type.
221     Elements.resize(SrcSTy->getNumElements());
222     for (unsigned i = 0, e = Elements.size(); i != e; ++i)
223       Elements[i] = getImpl(SrcSTy->getElementType(i));
224 
225     DstSTy->setBody(Elements, SrcSTy->isPacked());
226 
227     // If DstSTy has no name or has a longer name than STy, then viciously steal
228     // STy's name.
229     if (!SrcSTy->hasName()) continue;
230     StringRef SrcName = SrcSTy->getName();
231 
232     if (!DstSTy->hasName() || DstSTy->getName().size() > SrcName.size()) {
233       TmpName.insert(TmpName.end(), SrcName.begin(), SrcName.end());
234       SrcSTy->setName("");
235       DstSTy->setName(TmpName.str());
236       TmpName.clear();
237     }
238   }
239 
240   DstResolvedOpaqueTypes.clear();
241 }
242 
243 /// get - Return the mapped type to use for the specified input type from the
244 /// source module.
get(Type * Ty)245 Type *TypeMapTy::get(Type *Ty) {
246   Type *Result = getImpl(Ty);
247 
248   // If this caused a reference to any struct type, resolve it before returning.
249   if (!SrcDefinitionsToResolve.empty())
250     linkDefinedTypeBodies();
251   return Result;
252 }
253 
254 /// getImpl - This is the recursive version of get().
getImpl(Type * Ty)255 Type *TypeMapTy::getImpl(Type *Ty) {
256   // If we already have an entry for this type, return it.
257   Type **Entry = &MappedTypes[Ty];
258   if (*Entry) return *Entry;
259 
260   // If this is not a named struct type, then just map all of the elements and
261   // then rebuild the type from inside out.
262   if (!isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral()) {
263     // If there are no element types to map, then the type is itself.  This is
264     // true for the anonymous {} struct, things like 'float', integers, etc.
265     if (Ty->getNumContainedTypes() == 0)
266       return *Entry = Ty;
267 
268     // Remap all of the elements, keeping track of whether any of them change.
269     bool AnyChange = false;
270     SmallVector<Type*, 4> ElementTypes;
271     ElementTypes.resize(Ty->getNumContainedTypes());
272     for (unsigned i = 0, e = Ty->getNumContainedTypes(); i != e; ++i) {
273       ElementTypes[i] = getImpl(Ty->getContainedType(i));
274       AnyChange |= ElementTypes[i] != Ty->getContainedType(i);
275     }
276 
277     // If we found our type while recursively processing stuff, just use it.
278     Entry = &MappedTypes[Ty];
279     if (*Entry) return *Entry;
280 
281     // If all of the element types mapped directly over, then the type is usable
282     // as-is.
283     if (!AnyChange)
284       return *Entry = Ty;
285 
286     // Otherwise, rebuild a modified type.
287     switch (Ty->getTypeID()) {
288     default: llvm_unreachable("unknown derived type to remap");
289     case Type::ArrayTyID:
290       return *Entry = ArrayType::get(ElementTypes[0],
291                                      cast<ArrayType>(Ty)->getNumElements());
292     case Type::VectorTyID:
293       return *Entry = VectorType::get(ElementTypes[0],
294                                       cast<VectorType>(Ty)->getNumElements());
295     case Type::PointerTyID:
296       return *Entry = PointerType::get(ElementTypes[0],
297                                       cast<PointerType>(Ty)->getAddressSpace());
298     case Type::FunctionTyID:
299       return *Entry = FunctionType::get(ElementTypes[0],
300                                         makeArrayRef(ElementTypes).slice(1),
301                                         cast<FunctionType>(Ty)->isVarArg());
302     case Type::StructTyID:
303       // Note that this is only reached for anonymous structs.
304       return *Entry = StructType::get(Ty->getContext(), ElementTypes,
305                                       cast<StructType>(Ty)->isPacked());
306     }
307   }
308 
309   // Otherwise, this is an unmapped named struct.  If the struct can be directly
310   // mapped over, just use it as-is.  This happens in a case when the linked-in
311   // module has something like:
312   //   %T = type {%T*, i32}
313   //   @GV = global %T* null
314   // where T does not exist at all in the destination module.
315   //
316   // The other case we watch for is when the type is not in the destination
317   // module, but that it has to be rebuilt because it refers to something that
318   // is already mapped.  For example, if the destination module has:
319   //  %A = type { i32 }
320   // and the source module has something like
321   //  %A' = type { i32 }
322   //  %B = type { %A'* }
323   //  @GV = global %B* null
324   // then we want to create a new type: "%B = type { %A*}" and have it take the
325   // pristine "%B" name from the source module.
326   //
327   // To determine which case this is, we have to recursively walk the type graph
328   // speculating that we'll be able to reuse it unmodified.  Only if this is
329   // safe would we map the entire thing over.  Because this is an optimization,
330   // and is not required for the prettiness of the linked module, we just skip
331   // it and always rebuild a type here.
332   StructType *STy = cast<StructType>(Ty);
333 
334   // If the type is opaque, we can just use it directly.
335   if (STy->isOpaque()) {
336     // A named structure type from src module is used. Add it to the Set of
337     // identified structs in the destination module.
338     DstStructTypesSet.insert(STy);
339     return *Entry = STy;
340   }
341 
342   // Otherwise we create a new type and resolve its body later.  This will be
343   // resolved by the top level of get().
344   SrcDefinitionsToResolve.push_back(STy);
345   StructType *DTy = StructType::create(STy->getContext());
346   // A new identified structure type was created. Add it to the set of
347   // identified structs in the destination module.
348   DstStructTypesSet.insert(DTy);
349   DstResolvedOpaqueTypes.insert(DTy);
350   return *Entry = DTy;
351 }
352 
353 //===----------------------------------------------------------------------===//
354 // ModuleLinker implementation.
355 //===----------------------------------------------------------------------===//
356 
357 namespace {
358   class ModuleLinker;
359 
360   /// ValueMaterializerTy - Creates prototypes for functions that are lazily
361   /// linked on the fly. This speeds up linking for modules with many
362   /// lazily linked functions of which few get used.
363   class ValueMaterializerTy : public ValueMaterializer {
364     TypeMapTy &TypeMap;
365     Module *DstM;
366     std::vector<Function*> &LazilyLinkFunctions;
367   public:
ValueMaterializerTy(TypeMapTy & TypeMap,Module * DstM,std::vector<Function * > & LazilyLinkFunctions)368     ValueMaterializerTy(TypeMapTy &TypeMap, Module *DstM,
369                         std::vector<Function*> &LazilyLinkFunctions) :
370       ValueMaterializer(), TypeMap(TypeMap), DstM(DstM),
371       LazilyLinkFunctions(LazilyLinkFunctions) {
372     }
373 
374     virtual Value *materializeValueFor(Value *V);
375   };
376 
377   /// ModuleLinker - This is an implementation class for the LinkModules
378   /// function, which is the entrypoint for this file.
379   class ModuleLinker {
380     Module *DstM, *SrcM;
381 
382     TypeMapTy TypeMap;
383     ValueMaterializerTy ValMaterializer;
384 
385     /// ValueMap - Mapping of values from what they used to be in Src, to what
386     /// they are now in DstM.  ValueToValueMapTy is a ValueMap, which involves
387     /// some overhead due to the use of Value handles which the Linker doesn't
388     /// actually need, but this allows us to reuse the ValueMapper code.
389     ValueToValueMapTy ValueMap;
390 
391     struct AppendingVarInfo {
392       GlobalVariable *NewGV;  // New aggregate global in dest module.
393       Constant *DstInit;      // Old initializer from dest module.
394       Constant *SrcInit;      // Old initializer from src module.
395     };
396 
397     std::vector<AppendingVarInfo> AppendingVars;
398 
399     unsigned Mode; // Mode to treat source module.
400 
401     // Set of items not to link in from source.
402     SmallPtrSet<const Value*, 16> DoNotLinkFromSource;
403 
404     // Vector of functions to lazily link in.
405     std::vector<Function*> LazilyLinkFunctions;
406 
407   public:
408     std::string ErrorMsg;
409 
ModuleLinker(Module * dstM,TypeSet & Set,Module * srcM,unsigned mode)410     ModuleLinker(Module *dstM, TypeSet &Set, Module *srcM, unsigned mode)
411       : DstM(dstM), SrcM(srcM), TypeMap(Set),
412         ValMaterializer(TypeMap, DstM, LazilyLinkFunctions),
413         Mode(mode) { }
414 
415     bool run();
416 
417   private:
418     /// emitError - Helper method for setting a message and returning an error
419     /// code.
emitError(const Twine & Message)420     bool emitError(const Twine &Message) {
421       ErrorMsg = Message.str();
422       return true;
423     }
424 
425     /// getLinkageResult - This analyzes the two global values and determines
426     /// what the result will look like in the destination module.
427     bool getLinkageResult(GlobalValue *Dest, const GlobalValue *Src,
428                           GlobalValue::LinkageTypes &LT,
429                           GlobalValue::VisibilityTypes &Vis,
430                           bool &LinkFromSrc);
431 
432     /// getLinkedToGlobal - Given a global in the source module, return the
433     /// global in the destination module that is being linked to, if any.
getLinkedToGlobal(GlobalValue * SrcGV)434     GlobalValue *getLinkedToGlobal(GlobalValue *SrcGV) {
435       // If the source has no name it can't link.  If it has local linkage,
436       // there is no name match-up going on.
437       if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
438         return 0;
439 
440       // Otherwise see if we have a match in the destination module's symtab.
441       GlobalValue *DGV = DstM->getNamedValue(SrcGV->getName());
442       if (DGV == 0) return 0;
443 
444       // If we found a global with the same name in the dest module, but it has
445       // internal linkage, we are really not doing any linkage here.
446       if (DGV->hasLocalLinkage())
447         return 0;
448 
449       // Otherwise, we do in fact link to the destination global.
450       return DGV;
451     }
452 
453     void computeTypeMapping();
454 
455     bool linkAppendingVarProto(GlobalVariable *DstGV, GlobalVariable *SrcGV);
456     bool linkGlobalProto(GlobalVariable *SrcGV);
457     bool linkFunctionProto(Function *SrcF);
458     bool linkAliasProto(GlobalAlias *SrcA);
459     bool linkModuleFlagsMetadata();
460 
461     void linkAppendingVarInit(const AppendingVarInfo &AVI);
462     void linkGlobalInits();
463     void linkFunctionBody(Function *Dst, Function *Src);
464     void linkAliasBodies();
465     void linkNamedMDNodes();
466   };
467 }
468 
469 /// forceRenaming - The LLVM SymbolTable class autorenames globals that conflict
470 /// in the symbol table.  This is good for all clients except for us.  Go
471 /// through the trouble to force this back.
forceRenaming(GlobalValue * GV,StringRef Name)472 static void forceRenaming(GlobalValue *GV, StringRef Name) {
473   // If the global doesn't force its name or if it already has the right name,
474   // there is nothing for us to do.
475   if (GV->hasLocalLinkage() || GV->getName() == Name)
476     return;
477 
478   Module *M = GV->getParent();
479 
480   // If there is a conflict, rename the conflict.
481   if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
482     GV->takeName(ConflictGV);
483     ConflictGV->setName(Name);    // This will cause ConflictGV to get renamed
484     assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
485   } else {
486     GV->setName(Name);              // Force the name back
487   }
488 }
489 
490 /// copyGVAttributes - copy additional attributes (those not needed to construct
491 /// a GlobalValue) from the SrcGV to the DestGV.
copyGVAttributes(GlobalValue * DestGV,const GlobalValue * SrcGV)492 static void copyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) {
493   // Use the maximum alignment, rather than just copying the alignment of SrcGV.
494   unsigned Alignment = std::max(DestGV->getAlignment(), SrcGV->getAlignment());
495   DestGV->copyAttributesFrom(SrcGV);
496   DestGV->setAlignment(Alignment);
497 
498   forceRenaming(DestGV, SrcGV->getName());
499 }
500 
isLessConstraining(GlobalValue::VisibilityTypes a,GlobalValue::VisibilityTypes b)501 static bool isLessConstraining(GlobalValue::VisibilityTypes a,
502                                GlobalValue::VisibilityTypes b) {
503   if (a == GlobalValue::HiddenVisibility)
504     return false;
505   if (b == GlobalValue::HiddenVisibility)
506     return true;
507   if (a == GlobalValue::ProtectedVisibility)
508     return false;
509   if (b == GlobalValue::ProtectedVisibility)
510     return true;
511   return false;
512 }
513 
materializeValueFor(Value * V)514 Value *ValueMaterializerTy::materializeValueFor(Value *V) {
515   Function *SF = dyn_cast<Function>(V);
516   if (!SF)
517     return NULL;
518 
519   Function *DF = Function::Create(TypeMap.get(SF->getFunctionType()),
520                                   SF->getLinkage(), SF->getName(), DstM);
521   copyGVAttributes(DF, SF);
522 
523   LazilyLinkFunctions.push_back(SF);
524   return DF;
525 }
526 
527 
528 /// getLinkageResult - This analyzes the two global values and determines what
529 /// the result will look like in the destination module.  In particular, it
530 /// computes the resultant linkage type and visibility, computes whether the
531 /// global in the source should be copied over to the destination (replacing
532 /// the existing one), and computes whether this linkage is an error or not.
getLinkageResult(GlobalValue * Dest,const GlobalValue * Src,GlobalValue::LinkageTypes & LT,GlobalValue::VisibilityTypes & Vis,bool & LinkFromSrc)533 bool ModuleLinker::getLinkageResult(GlobalValue *Dest, const GlobalValue *Src,
534                                     GlobalValue::LinkageTypes &LT,
535                                     GlobalValue::VisibilityTypes &Vis,
536                                     bool &LinkFromSrc) {
537   assert(Dest && "Must have two globals being queried");
538   assert(!Src->hasLocalLinkage() &&
539          "If Src has internal linkage, Dest shouldn't be set!");
540 
541   bool SrcIsDeclaration = Src->isDeclaration() && !Src->isMaterializable();
542   bool DestIsDeclaration = Dest->isDeclaration();
543 
544   if (SrcIsDeclaration) {
545     // If Src is external or if both Src & Dest are external..  Just link the
546     // external globals, we aren't adding anything.
547     if (Src->hasDLLImportLinkage()) {
548       // If one of GVs has DLLImport linkage, result should be dllimport'ed.
549       if (DestIsDeclaration) {
550         LinkFromSrc = true;
551         LT = Src->getLinkage();
552       }
553     } else if (Dest->hasExternalWeakLinkage()) {
554       // If the Dest is weak, use the source linkage.
555       LinkFromSrc = true;
556       LT = Src->getLinkage();
557     } else {
558       LinkFromSrc = false;
559       LT = Dest->getLinkage();
560     }
561   } else if (DestIsDeclaration && !Dest->hasDLLImportLinkage()) {
562     // If Dest is external but Src is not:
563     LinkFromSrc = true;
564     LT = Src->getLinkage();
565   } else if (Src->isWeakForLinker()) {
566     // At this point we know that Dest has LinkOnce, External*, Weak, Common,
567     // or DLL* linkage.
568     if (Dest->hasExternalWeakLinkage() ||
569         Dest->hasAvailableExternallyLinkage() ||
570         (Dest->hasLinkOnceLinkage() &&
571          (Src->hasWeakLinkage() || Src->hasCommonLinkage()))) {
572       LinkFromSrc = true;
573       LT = Src->getLinkage();
574     } else {
575       LinkFromSrc = false;
576       LT = Dest->getLinkage();
577     }
578   } else if (Dest->isWeakForLinker()) {
579     // At this point we know that Src has External* or DLL* linkage.
580     if (Src->hasExternalWeakLinkage()) {
581       LinkFromSrc = false;
582       LT = Dest->getLinkage();
583     } else {
584       LinkFromSrc = true;
585       LT = GlobalValue::ExternalLinkage;
586     }
587   } else {
588     assert((Dest->hasExternalLinkage()  || Dest->hasDLLImportLinkage() ||
589             Dest->hasDLLExportLinkage() || Dest->hasExternalWeakLinkage()) &&
590            (Src->hasExternalLinkage()   || Src->hasDLLImportLinkage() ||
591             Src->hasDLLExportLinkage()  || Src->hasExternalWeakLinkage()) &&
592            "Unexpected linkage type!");
593     return emitError("Linking globals named '" + Src->getName() +
594                  "': symbol multiply defined!");
595   }
596 
597   // Compute the visibility. We follow the rules in the System V Application
598   // Binary Interface.
599   Vis = isLessConstraining(Src->getVisibility(), Dest->getVisibility()) ?
600     Dest->getVisibility() : Src->getVisibility();
601   return false;
602 }
603 
604 /// computeTypeMapping - Loop over all of the linked values to compute type
605 /// mappings.  For example, if we link "extern Foo *x" and "Foo *x = NULL", then
606 /// we have two struct types 'Foo' but one got renamed when the module was
607 /// loaded into the same LLVMContext.
computeTypeMapping()608 void ModuleLinker::computeTypeMapping() {
609   // Incorporate globals.
610   for (Module::global_iterator I = SrcM->global_begin(),
611        E = SrcM->global_end(); I != E; ++I) {
612     GlobalValue *DGV = getLinkedToGlobal(I);
613     if (DGV == 0) continue;
614 
615     if (!DGV->hasAppendingLinkage() || !I->hasAppendingLinkage()) {
616       TypeMap.addTypeMapping(DGV->getType(), I->getType());
617       continue;
618     }
619 
620     // Unify the element type of appending arrays.
621     ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType());
622     ArrayType *SAT = cast<ArrayType>(I->getType()->getElementType());
623     TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
624   }
625 
626   // Incorporate functions.
627   for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I) {
628     if (GlobalValue *DGV = getLinkedToGlobal(I))
629       TypeMap.addTypeMapping(DGV->getType(), I->getType());
630   }
631 
632   // Incorporate types by name, scanning all the types in the source module.
633   // At this point, the destination module may have a type "%foo = { i32 }" for
634   // example.  When the source module got loaded into the same LLVMContext, if
635   // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
636   TypeFinder SrcStructTypes;
637   SrcStructTypes.run(*SrcM, true);
638   SmallPtrSet<StructType*, 32> SrcStructTypesSet(SrcStructTypes.begin(),
639                                                  SrcStructTypes.end());
640 
641   for (unsigned i = 0, e = SrcStructTypes.size(); i != e; ++i) {
642     StructType *ST = SrcStructTypes[i];
643     if (!ST->hasName()) continue;
644 
645     // Check to see if there is a dot in the name followed by a digit.
646     size_t DotPos = ST->getName().rfind('.');
647     if (DotPos == 0 || DotPos == StringRef::npos ||
648         ST->getName().back() == '.' ||
649         !isdigit(static_cast<unsigned char>(ST->getName()[DotPos+1])))
650       continue;
651 
652     // Check to see if the destination module has a struct with the prefix name.
653     if (StructType *DST = DstM->getTypeByName(ST->getName().substr(0, DotPos)))
654       // Don't use it if this actually came from the source module. They're in
655       // the same LLVMContext after all. Also don't use it unless the type is
656       // actually used in the destination module. This can happen in situations
657       // like this:
658       //
659       //      Module A                         Module B
660       //      --------                         --------
661       //   %Z = type { %A }                %B = type { %C.1 }
662       //   %A = type { %B.1, [7 x i8] }    %C.1 = type { i8* }
663       //   %B.1 = type { %C }              %A.2 = type { %B.3, [5 x i8] }
664       //   %C = type { i8* }               %B.3 = type { %C.1 }
665       //
666       // When we link Module B with Module A, the '%B' in Module B is
667       // used. However, that would then use '%C.1'. But when we process '%C.1',
668       // we prefer to take the '%C' version. So we are then left with both
669       // '%C.1' and '%C' being used for the same types. This leads to some
670       // variables using one type and some using the other.
671       if (!SrcStructTypesSet.count(DST) && TypeMap.DstStructTypesSet.count(DST))
672         TypeMap.addTypeMapping(DST, ST);
673   }
674 
675   // Don't bother incorporating aliases, they aren't generally typed well.
676 
677   // Now that we have discovered all of the type equivalences, get a body for
678   // any 'opaque' types in the dest module that are now resolved.
679   TypeMap.linkDefinedTypeBodies();
680 }
681 
682 /// linkAppendingVarProto - If there were any appending global variables, link
683 /// them together now.  Return true on error.
linkAppendingVarProto(GlobalVariable * DstGV,GlobalVariable * SrcGV)684 bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV,
685                                          GlobalVariable *SrcGV) {
686 
687   if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
688     return emitError("Linking globals named '" + SrcGV->getName() +
689            "': can only link appending global with another appending global!");
690 
691   ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
692   ArrayType *SrcTy =
693     cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()));
694   Type *EltTy = DstTy->getElementType();
695 
696   // Check to see that they two arrays agree on type.
697   if (EltTy != SrcTy->getElementType())
698     return emitError("Appending variables with different element types!");
699   if (DstGV->isConstant() != SrcGV->isConstant())
700     return emitError("Appending variables linked with different const'ness!");
701 
702   if (DstGV->getAlignment() != SrcGV->getAlignment())
703     return emitError(
704              "Appending variables with different alignment need to be linked!");
705 
706   if (DstGV->getVisibility() != SrcGV->getVisibility())
707     return emitError(
708             "Appending variables with different visibility need to be linked!");
709 
710   if (DstGV->getSection() != SrcGV->getSection())
711     return emitError(
712           "Appending variables with different section name need to be linked!");
713 
714   uint64_t NewSize = DstTy->getNumElements() + SrcTy->getNumElements();
715   ArrayType *NewType = ArrayType::get(EltTy, NewSize);
716 
717   // Create the new global variable.
718   GlobalVariable *NG =
719     new GlobalVariable(*DstGV->getParent(), NewType, SrcGV->isConstant(),
720                        DstGV->getLinkage(), /*init*/0, /*name*/"", DstGV,
721                        DstGV->getThreadLocalMode(),
722                        DstGV->getType()->getAddressSpace());
723 
724   // Propagate alignment, visibility and section info.
725   copyGVAttributes(NG, DstGV);
726 
727   AppendingVarInfo AVI;
728   AVI.NewGV = NG;
729   AVI.DstInit = DstGV->getInitializer();
730   AVI.SrcInit = SrcGV->getInitializer();
731   AppendingVars.push_back(AVI);
732 
733   // Replace any uses of the two global variables with uses of the new
734   // global.
735   ValueMap[SrcGV] = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
736 
737   DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
738   DstGV->eraseFromParent();
739 
740   // Track the source variable so we don't try to link it.
741   DoNotLinkFromSource.insert(SrcGV);
742 
743   return false;
744 }
745 
746 /// linkGlobalProto - Loop through the global variables in the src module and
747 /// merge them into the dest module.
linkGlobalProto(GlobalVariable * SGV)748 bool ModuleLinker::linkGlobalProto(GlobalVariable *SGV) {
749   GlobalValue *DGV = getLinkedToGlobal(SGV);
750   llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility;
751 
752   if (DGV) {
753     // Concatenation of appending linkage variables is magic and handled later.
754     if (DGV->hasAppendingLinkage() || SGV->hasAppendingLinkage())
755       return linkAppendingVarProto(cast<GlobalVariable>(DGV), SGV);
756 
757     // Determine whether linkage of these two globals follows the source
758     // module's definition or the destination module's definition.
759     GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
760     GlobalValue::VisibilityTypes NV;
761     bool LinkFromSrc = false;
762     if (getLinkageResult(DGV, SGV, NewLinkage, NV, LinkFromSrc))
763       return true;
764     NewVisibility = NV;
765 
766     // If we're not linking from the source, then keep the definition that we
767     // have.
768     if (!LinkFromSrc) {
769       // Special case for const propagation.
770       if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV))
771         if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant())
772           DGVar->setConstant(true);
773 
774       // Set calculated linkage and visibility.
775       DGV->setLinkage(NewLinkage);
776       DGV->setVisibility(*NewVisibility);
777 
778       // Make sure to remember this mapping.
779       ValueMap[SGV] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGV->getType()));
780 
781       // Track the source global so that we don't attempt to copy it over when
782       // processing global initializers.
783       DoNotLinkFromSource.insert(SGV);
784 
785       return false;
786     }
787   }
788 
789   // No linking to be performed or linking from the source: simply create an
790   // identical version of the symbol over in the dest module... the
791   // initializer will be filled in later by LinkGlobalInits.
792   GlobalVariable *NewDGV =
793     new GlobalVariable(*DstM, TypeMap.get(SGV->getType()->getElementType()),
794                        SGV->isConstant(), SGV->getLinkage(), /*init*/0,
795                        SGV->getName(), /*insertbefore*/0,
796                        SGV->getThreadLocalMode(),
797                        SGV->getType()->getAddressSpace());
798   // Propagate alignment, visibility and section info.
799   copyGVAttributes(NewDGV, SGV);
800   if (NewVisibility)
801     NewDGV->setVisibility(*NewVisibility);
802 
803   if (DGV) {
804     DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV, DGV->getType()));
805     DGV->eraseFromParent();
806   }
807 
808   // Make sure to remember this mapping.
809   ValueMap[SGV] = NewDGV;
810   return false;
811 }
812 
813 /// linkFunctionProto - Link the function in the source module into the
814 /// destination module if needed, setting up mapping information.
linkFunctionProto(Function * SF)815 bool ModuleLinker::linkFunctionProto(Function *SF) {
816   GlobalValue *DGV = getLinkedToGlobal(SF);
817   llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility;
818 
819   if (DGV) {
820     GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
821     bool LinkFromSrc = false;
822     GlobalValue::VisibilityTypes NV;
823     if (getLinkageResult(DGV, SF, NewLinkage, NV, LinkFromSrc))
824       return true;
825     NewVisibility = NV;
826 
827     if (!LinkFromSrc) {
828       // Set calculated linkage
829       DGV->setLinkage(NewLinkage);
830       DGV->setVisibility(*NewVisibility);
831 
832       // Make sure to remember this mapping.
833       ValueMap[SF] = ConstantExpr::getBitCast(DGV, TypeMap.get(SF->getType()));
834 
835       // Track the function from the source module so we don't attempt to remap
836       // it.
837       DoNotLinkFromSource.insert(SF);
838 
839       return false;
840     }
841   }
842 
843   // If the function is to be lazily linked, don't create it just yet.
844   // The ValueMaterializerTy will deal with creating it if it's used.
845   if (!DGV && (SF->hasLocalLinkage() || SF->hasLinkOnceLinkage() ||
846                SF->hasAvailableExternallyLinkage())) {
847     DoNotLinkFromSource.insert(SF);
848     return false;
849   }
850 
851   // If there is no linkage to be performed or we are linking from the source,
852   // bring SF over.
853   Function *NewDF = Function::Create(TypeMap.get(SF->getFunctionType()),
854                                      SF->getLinkage(), SF->getName(), DstM);
855   copyGVAttributes(NewDF, SF);
856   if (NewVisibility)
857     NewDF->setVisibility(*NewVisibility);
858 
859   if (DGV) {
860     // Any uses of DF need to change to NewDF, with cast.
861     DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType()));
862     DGV->eraseFromParent();
863   }
864 
865   ValueMap[SF] = NewDF;
866   return false;
867 }
868 
869 /// LinkAliasProto - Set up prototypes for any aliases that come over from the
870 /// source module.
linkAliasProto(GlobalAlias * SGA)871 bool ModuleLinker::linkAliasProto(GlobalAlias *SGA) {
872   GlobalValue *DGV = getLinkedToGlobal(SGA);
873   llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility;
874 
875   if (DGV) {
876     GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
877     GlobalValue::VisibilityTypes NV;
878     bool LinkFromSrc = false;
879     if (getLinkageResult(DGV, SGA, NewLinkage, NV, LinkFromSrc))
880       return true;
881     NewVisibility = NV;
882 
883     if (!LinkFromSrc) {
884       // Set calculated linkage.
885       DGV->setLinkage(NewLinkage);
886       DGV->setVisibility(*NewVisibility);
887 
888       // Make sure to remember this mapping.
889       ValueMap[SGA] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGA->getType()));
890 
891       // Track the alias from the source module so we don't attempt to remap it.
892       DoNotLinkFromSource.insert(SGA);
893 
894       return false;
895     }
896   }
897 
898   // If there is no linkage to be performed or we're linking from the source,
899   // bring over SGA.
900   GlobalAlias *NewDA = new GlobalAlias(TypeMap.get(SGA->getType()),
901                                        SGA->getLinkage(), SGA->getName(),
902                                        /*aliasee*/0, DstM);
903   copyGVAttributes(NewDA, SGA);
904   if (NewVisibility)
905     NewDA->setVisibility(*NewVisibility);
906 
907   if (DGV) {
908     // Any uses of DGV need to change to NewDA, with cast.
909     DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDA, DGV->getType()));
910     DGV->eraseFromParent();
911   }
912 
913   ValueMap[SGA] = NewDA;
914   return false;
915 }
916 
getArrayElements(Constant * C,SmallVectorImpl<Constant * > & Dest)917 static void getArrayElements(Constant *C, SmallVectorImpl<Constant*> &Dest) {
918   unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
919 
920   for (unsigned i = 0; i != NumElements; ++i)
921     Dest.push_back(C->getAggregateElement(i));
922 }
923 
linkAppendingVarInit(const AppendingVarInfo & AVI)924 void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) {
925   // Merge the initializer.
926   SmallVector<Constant*, 16> Elements;
927   getArrayElements(AVI.DstInit, Elements);
928 
929   Constant *SrcInit = MapValue(AVI.SrcInit, ValueMap, RF_None, &TypeMap, &ValMaterializer);
930   getArrayElements(SrcInit, Elements);
931 
932   ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType());
933   AVI.NewGV->setInitializer(ConstantArray::get(NewType, Elements));
934 }
935 
936 /// linkGlobalInits - Update the initializers in the Dest module now that all
937 /// globals that may be referenced are in Dest.
linkGlobalInits()938 void ModuleLinker::linkGlobalInits() {
939   // Loop over all of the globals in the src module, mapping them over as we go
940   for (Module::const_global_iterator I = SrcM->global_begin(),
941        E = SrcM->global_end(); I != E; ++I) {
942 
943     // Only process initialized GV's or ones not already in dest.
944     if (!I->hasInitializer() || DoNotLinkFromSource.count(I)) continue;
945 
946     // Grab destination global variable.
947     GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[I]);
948     // Figure out what the initializer looks like in the dest module.
949     DGV->setInitializer(MapValue(I->getInitializer(), ValueMap,
950                                  RF_None, &TypeMap, &ValMaterializer));
951   }
952 }
953 
954 /// linkFunctionBody - Copy the source function over into the dest function and
955 /// fix up references to values.  At this point we know that Dest is an external
956 /// function, and that Src is not.
linkFunctionBody(Function * Dst,Function * Src)957 void ModuleLinker::linkFunctionBody(Function *Dst, Function *Src) {
958   assert(Src && Dst && Dst->isDeclaration() && !Src->isDeclaration());
959 
960   // Go through and convert function arguments over, remembering the mapping.
961   Function::arg_iterator DI = Dst->arg_begin();
962   for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
963        I != E; ++I, ++DI) {
964     DI->setName(I->getName());  // Copy the name over.
965 
966     // Add a mapping to our mapping.
967     ValueMap[I] = DI;
968   }
969 
970   if (Mode == Linker::DestroySource) {
971     // Splice the body of the source function into the dest function.
972     Dst->getBasicBlockList().splice(Dst->end(), Src->getBasicBlockList());
973 
974     // At this point, all of the instructions and values of the function are now
975     // copied over.  The only problem is that they are still referencing values in
976     // the Source function as operands.  Loop through all of the operands of the
977     // functions and patch them up to point to the local versions.
978     for (Function::iterator BB = Dst->begin(), BE = Dst->end(); BB != BE; ++BB)
979       for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
980         RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries,
981                          &TypeMap, &ValMaterializer);
982 
983   } else {
984     // Clone the body of the function into the dest function.
985     SmallVector<ReturnInst*, 8> Returns; // Ignore returns.
986     CloneFunctionInto(Dst, Src, ValueMap, false, Returns, "", NULL,
987                       &TypeMap, &ValMaterializer);
988   }
989 
990   // There is no need to map the arguments anymore.
991   for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
992        I != E; ++I)
993     ValueMap.erase(I);
994 
995 }
996 
997 /// linkAliasBodies - Insert all of the aliases in Src into the Dest module.
linkAliasBodies()998 void ModuleLinker::linkAliasBodies() {
999   for (Module::alias_iterator I = SrcM->alias_begin(), E = SrcM->alias_end();
1000        I != E; ++I) {
1001     if (DoNotLinkFromSource.count(I))
1002       continue;
1003     if (Constant *Aliasee = I->getAliasee()) {
1004       GlobalAlias *DA = cast<GlobalAlias>(ValueMap[I]);
1005       DA->setAliasee(MapValue(Aliasee, ValueMap, RF_None,
1006                               &TypeMap, &ValMaterializer));
1007     }
1008   }
1009 }
1010 
1011 /// linkNamedMDNodes - Insert all of the named MDNodes in Src into the Dest
1012 /// module.
linkNamedMDNodes()1013 void ModuleLinker::linkNamedMDNodes() {
1014   const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1015   for (Module::const_named_metadata_iterator I = SrcM->named_metadata_begin(),
1016        E = SrcM->named_metadata_end(); I != E; ++I) {
1017     // Don't link module flags here. Do them separately.
1018     if (&*I == SrcModFlags) continue;
1019     NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(I->getName());
1020     // Add Src elements into Dest node.
1021     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1022       DestNMD->addOperand(MapValue(I->getOperand(i), ValueMap,
1023                                    RF_None, &TypeMap, &ValMaterializer));
1024   }
1025 }
1026 
1027 /// linkModuleFlagsMetadata - Merge the linker flags in Src into the Dest
1028 /// module.
linkModuleFlagsMetadata()1029 bool ModuleLinker::linkModuleFlagsMetadata() {
1030   // If the source module has no module flags, we are done.
1031   const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1032   if (!SrcModFlags) return false;
1033 
1034   // If the destination module doesn't have module flags yet, then just copy
1035   // over the source module's flags.
1036   NamedMDNode *DstModFlags = DstM->getOrInsertModuleFlagsMetadata();
1037   if (DstModFlags->getNumOperands() == 0) {
1038     for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1039       DstModFlags->addOperand(SrcModFlags->getOperand(I));
1040 
1041     return false;
1042   }
1043 
1044   // First build a map of the existing module flags and requirements.
1045   DenseMap<MDString*, MDNode*> Flags;
1046   SmallSetVector<MDNode*, 16> Requirements;
1047   for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1048     MDNode *Op = DstModFlags->getOperand(I);
1049     ConstantInt *Behavior = cast<ConstantInt>(Op->getOperand(0));
1050     MDString *ID = cast<MDString>(Op->getOperand(1));
1051 
1052     if (Behavior->getZExtValue() == Module::Require) {
1053       Requirements.insert(cast<MDNode>(Op->getOperand(2)));
1054     } else {
1055       Flags[ID] = Op;
1056     }
1057   }
1058 
1059   // Merge in the flags from the source module, and also collect its set of
1060   // requirements.
1061   bool HasErr = false;
1062   for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1063     MDNode *SrcOp = SrcModFlags->getOperand(I);
1064     ConstantInt *SrcBehavior = cast<ConstantInt>(SrcOp->getOperand(0));
1065     MDString *ID = cast<MDString>(SrcOp->getOperand(1));
1066     MDNode *DstOp = Flags.lookup(ID);
1067     unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
1068 
1069     // If this is a requirement, add it and continue.
1070     if (SrcBehaviorValue == Module::Require) {
1071       // If the destination module does not already have this requirement, add
1072       // it.
1073       if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
1074         DstModFlags->addOperand(SrcOp);
1075       }
1076       continue;
1077     }
1078 
1079     // If there is no existing flag with this ID, just add it.
1080     if (!DstOp) {
1081       Flags[ID] = SrcOp;
1082       DstModFlags->addOperand(SrcOp);
1083       continue;
1084     }
1085 
1086     // Otherwise, perform a merge.
1087     ConstantInt *DstBehavior = cast<ConstantInt>(DstOp->getOperand(0));
1088     unsigned DstBehaviorValue = DstBehavior->getZExtValue();
1089 
1090     // If either flag has override behavior, handle it first.
1091     if (DstBehaviorValue == Module::Override) {
1092       // Diagnose inconsistent flags which both have override behavior.
1093       if (SrcBehaviorValue == Module::Override &&
1094           SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1095         HasErr |= emitError("linking module flags '" + ID->getString() +
1096                             "': IDs have conflicting override values");
1097       }
1098       continue;
1099     } else if (SrcBehaviorValue == Module::Override) {
1100       // Update the destination flag to that of the source.
1101       DstOp->replaceOperandWith(0, SrcBehavior);
1102       DstOp->replaceOperandWith(2, SrcOp->getOperand(2));
1103       continue;
1104     }
1105 
1106     // Diagnose inconsistent merge behavior types.
1107     if (SrcBehaviorValue != DstBehaviorValue) {
1108       HasErr |= emitError("linking module flags '" + ID->getString() +
1109                           "': IDs have conflicting behaviors");
1110       continue;
1111     }
1112 
1113     // Perform the merge for standard behavior types.
1114     switch (SrcBehaviorValue) {
1115     case Module::Require:
1116     case Module::Override: assert(0 && "not possible"); break;
1117     case Module::Error: {
1118       // Emit an error if the values differ.
1119       if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1120         HasErr |= emitError("linking module flags '" + ID->getString() +
1121                             "': IDs have conflicting values");
1122       }
1123       continue;
1124     }
1125     case Module::Warning: {
1126       // Emit a warning if the values differ.
1127       if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1128         errs() << "WARNING: linking module flags '" << ID->getString()
1129                << "': IDs have conflicting values";
1130       }
1131       continue;
1132     }
1133     case Module::Append: {
1134       MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1135       MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1136       unsigned NumOps = DstValue->getNumOperands() + SrcValue->getNumOperands();
1137       Value **VP, **Values = VP = new Value*[NumOps];
1138       for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i, ++VP)
1139         *VP = DstValue->getOperand(i);
1140       for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i, ++VP)
1141         *VP = SrcValue->getOperand(i);
1142       DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(),
1143                                                ArrayRef<Value*>(Values,
1144                                                                 NumOps)));
1145       delete[] Values;
1146       break;
1147     }
1148     case Module::AppendUnique: {
1149       SmallSetVector<Value*, 16> Elts;
1150       MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1151       MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1152       for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i)
1153         Elts.insert(DstValue->getOperand(i));
1154       for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i)
1155         Elts.insert(SrcValue->getOperand(i));
1156       DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(),
1157                                                ArrayRef<Value*>(Elts.begin(),
1158                                                                 Elts.end())));
1159       break;
1160     }
1161     }
1162   }
1163 
1164   // Check all of the requirements.
1165   for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
1166     MDNode *Requirement = Requirements[I];
1167     MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1168     Value *ReqValue = Requirement->getOperand(1);
1169 
1170     MDNode *Op = Flags[Flag];
1171     if (!Op || Op->getOperand(2) != ReqValue) {
1172       HasErr |= emitError("linking module flags '" + Flag->getString() +
1173                           "': does not have the required value");
1174       continue;
1175     }
1176   }
1177 
1178   return HasErr;
1179 }
1180 
run()1181 bool ModuleLinker::run() {
1182   assert(DstM && "Null destination module");
1183   assert(SrcM && "Null source module");
1184 
1185   // Inherit the target data from the source module if the destination module
1186   // doesn't have one already.
1187   if (DstM->getDataLayout().empty() && !SrcM->getDataLayout().empty())
1188     DstM->setDataLayout(SrcM->getDataLayout());
1189 
1190   // Copy the target triple from the source to dest if the dest's is empty.
1191   if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
1192     DstM->setTargetTriple(SrcM->getTargetTriple());
1193 
1194   if (!SrcM->getDataLayout().empty() && !DstM->getDataLayout().empty() &&
1195       SrcM->getDataLayout() != DstM->getDataLayout())
1196     errs() << "WARNING: Linking two modules of different data layouts!\n";
1197   if (!SrcM->getTargetTriple().empty() &&
1198       DstM->getTargetTriple() != SrcM->getTargetTriple()) {
1199     errs() << "WARNING: Linking two modules of different target triples: ";
1200     if (!SrcM->getModuleIdentifier().empty())
1201       errs() << SrcM->getModuleIdentifier() << ": ";
1202     errs() << "'" << SrcM->getTargetTriple() << "' and '"
1203            << DstM->getTargetTriple() << "'\n";
1204   }
1205 
1206   // Append the module inline asm string.
1207   if (!SrcM->getModuleInlineAsm().empty()) {
1208     if (DstM->getModuleInlineAsm().empty())
1209       DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm());
1210     else
1211       DstM->setModuleInlineAsm(DstM->getModuleInlineAsm()+"\n"+
1212                                SrcM->getModuleInlineAsm());
1213   }
1214 
1215   // Loop over all of the linked values to compute type mappings.
1216   computeTypeMapping();
1217 
1218   // Insert all of the globals in src into the DstM module... without linking
1219   // initializers (which could refer to functions not yet mapped over).
1220   for (Module::global_iterator I = SrcM->global_begin(),
1221        E = SrcM->global_end(); I != E; ++I)
1222     if (linkGlobalProto(I))
1223       return true;
1224 
1225   // Link the functions together between the two modules, without doing function
1226   // bodies... this just adds external function prototypes to the DstM
1227   // function...  We do this so that when we begin processing function bodies,
1228   // all of the global values that may be referenced are available in our
1229   // ValueMap.
1230   for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I)
1231     if (linkFunctionProto(I))
1232       return true;
1233 
1234   // If there were any aliases, link them now.
1235   for (Module::alias_iterator I = SrcM->alias_begin(),
1236        E = SrcM->alias_end(); I != E; ++I)
1237     if (linkAliasProto(I))
1238       return true;
1239 
1240   for (unsigned i = 0, e = AppendingVars.size(); i != e; ++i)
1241     linkAppendingVarInit(AppendingVars[i]);
1242 
1243   // Update the initializers in the DstM module now that all globals that may
1244   // be referenced are in DstM.
1245   linkGlobalInits();
1246 
1247   // Link in the function bodies that are defined in the source module into
1248   // DstM.
1249   for (Module::iterator SF = SrcM->begin(), E = SrcM->end(); SF != E; ++SF) {
1250     // Skip if not linking from source.
1251     if (DoNotLinkFromSource.count(SF)) continue;
1252 
1253     // Skip if no body (function is external) or materialize.
1254     if (SF->isDeclaration()) {
1255       if (!SF->isMaterializable())
1256         continue;
1257       if (SF->Materialize(&ErrorMsg))
1258         return true;
1259     }
1260 
1261     linkFunctionBody(cast<Function>(ValueMap[SF]), SF);
1262     SF->Dematerialize();
1263   }
1264 
1265   // Resolve all uses of aliases with aliasees.
1266   linkAliasBodies();
1267 
1268   // Remap all of the named MDNodes in Src into the DstM module. We do this
1269   // after linking GlobalValues so that MDNodes that reference GlobalValues
1270   // are properly remapped.
1271   linkNamedMDNodes();
1272 
1273   // Merge the module flags into the DstM module.
1274   if (linkModuleFlagsMetadata())
1275     return true;
1276 
1277   // Process vector of lazily linked in functions.
1278   bool LinkedInAnyFunctions;
1279   do {
1280     LinkedInAnyFunctions = false;
1281 
1282     for(std::vector<Function*>::iterator I = LazilyLinkFunctions.begin(),
1283         E = LazilyLinkFunctions.end(); I != E; ++I) {
1284       Function *SF = *I;
1285       if (!SF)
1286         continue;
1287 
1288       Function *DF = cast<Function>(ValueMap[SF]);
1289 
1290       // Materialize if necessary.
1291       if (SF->isDeclaration()) {
1292         if (!SF->isMaterializable())
1293           continue;
1294         if (SF->Materialize(&ErrorMsg))
1295           return true;
1296       }
1297 
1298       // Erase from vector *before* the function body is linked - linkFunctionBody could
1299       // invalidate I.
1300       LazilyLinkFunctions.erase(I);
1301 
1302       // Link in function body.
1303       linkFunctionBody(DF, SF);
1304       SF->Dematerialize();
1305 
1306       // Set flag to indicate we may have more functions to lazily link in
1307       // since we linked in a function.
1308       LinkedInAnyFunctions = true;
1309       break;
1310     }
1311   } while (LinkedInAnyFunctions);
1312 
1313   // Now that all of the types from the source are used, resolve any structs
1314   // copied over to the dest that didn't exist there.
1315   TypeMap.linkDefinedTypeBodies();
1316 
1317   return false;
1318 }
1319 
Linker(Module * M)1320 Linker::Linker(Module *M) : Composite(M) {
1321   TypeFinder StructTypes;
1322   StructTypes.run(*M, true);
1323   IdentifiedStructTypes.insert(StructTypes.begin(), StructTypes.end());
1324 }
1325 
~Linker()1326 Linker::~Linker() {
1327 }
1328 
linkInModule(Module * Src,unsigned Mode,std::string * ErrorMsg)1329 bool Linker::linkInModule(Module *Src, unsigned Mode, std::string *ErrorMsg) {
1330   ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src, Mode);
1331   if (TheLinker.run()) {
1332     if (ErrorMsg)
1333       *ErrorMsg = TheLinker.ErrorMsg;
1334     return true;
1335   }
1336   return false;
1337 }
1338 
1339 //===----------------------------------------------------------------------===//
1340 // LinkModules entrypoint.
1341 //===----------------------------------------------------------------------===//
1342 
1343 /// LinkModules - This function links two modules together, with the resulting
1344 /// Dest module modified to be the composite of the two input modules.  If an
1345 /// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
1346 /// the problem.  Upon failure, the Dest module could be in a modified state,
1347 /// and shouldn't be relied on to be consistent.
LinkModules(Module * Dest,Module * Src,unsigned Mode,std::string * ErrorMsg)1348 bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Mode,
1349                          std::string *ErrorMsg) {
1350   Linker L(Dest);
1351   return L.linkInModule(Src, Mode, ErrorMsg);
1352 }
1353 
1354 //===----------------------------------------------------------------------===//
1355 // C API.
1356 //===----------------------------------------------------------------------===//
1357 
LLVMLinkModules(LLVMModuleRef Dest,LLVMModuleRef Src,LLVMLinkerMode Mode,char ** OutMessages)1358 LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
1359                          LLVMLinkerMode Mode, char **OutMessages) {
1360   std::string Messages;
1361   LLVMBool Result = Linker::LinkModules(unwrap(Dest), unwrap(Src),
1362                                         Mode, OutMessages? &Messages : 0);
1363   if (OutMessages)
1364     *OutMessages = strdup(Messages.c_str());
1365   return Result;
1366 }
1367