1 //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVM module linker.
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/Module.h"
21 #include "llvm/IR/TypeFinder.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Transforms/Utils/Cloning.h"
31 //===----------------------------------------------------------------------===//
32 // TypeMap implementation.
33 //===----------------------------------------------------------------------===//
36 typedef SmallPtrSet<StructType*, 32> TypeSet;
38 class TypeMapTy : public ValueMapTypeRemapper {
39 /// MappedTypes - This is a mapping from a source type to a destination type
41 DenseMap<Type*, Type*> MappedTypes;
43 /// SpeculativeTypes - When checking to see if two subgraphs are isomorphic,
44 /// we speculatively add types to MappedTypes, but keep track of them here in
45 /// case we need to roll back.
46 SmallVector<Type*, 16> SpeculativeTypes;
48 /// SrcDefinitionsToResolve - This is a list of non-opaque structs in the
49 /// source module that are mapped to an opaque struct in the destination
51 SmallVector<StructType*, 16> SrcDefinitionsToResolve;
53 /// DstResolvedOpaqueTypes - This is the set of opaque types in the
54 /// destination modules who are getting a body from the source module.
55 SmallPtrSet<StructType*, 16> DstResolvedOpaqueTypes;
58 TypeMapTy(TypeSet &Set) : DstStructTypesSet(Set) {}
60 TypeSet &DstStructTypesSet;
61 /// addTypeMapping - Indicate that the specified type in the destination
62 /// module is conceptually equivalent to the specified type in the source
64 void addTypeMapping(Type *DstTy, Type *SrcTy);
66 /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest
67 /// module from a type definition in the source module.
68 void linkDefinedTypeBodies();
70 /// get - Return the mapped type to use for the specified input type from the
72 Type *get(Type *SrcTy);
74 FunctionType *get(FunctionType *T) {return cast<FunctionType>(get((Type*)T));}
76 /// dump - Dump out the type map for debugging purposes.
78 for (DenseMap<Type*, Type*>::const_iterator
79 I = MappedTypes.begin(), E = MappedTypes.end(); I != E; ++I) {
80 dbgs() << "TypeMap: ";
89 Type *getImpl(Type *T);
90 /// remapType - Implement the ValueMapTypeRemapper interface.
91 Type *remapType(Type *SrcTy) override {
95 bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
99 void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
100 Type *&Entry = MappedTypes[SrcTy];
103 if (DstTy == SrcTy) {
108 // Check to see if these types are recursively isomorphic and establish a
109 // mapping between them if so.
110 if (!areTypesIsomorphic(DstTy, SrcTy)) {
111 // Oops, they aren't isomorphic. Just discard this request by rolling out
112 // any speculative mappings we've established.
113 for (unsigned i = 0, e = SpeculativeTypes.size(); i != e; ++i)
114 MappedTypes.erase(SpeculativeTypes[i]);
116 SpeculativeTypes.clear();
119 /// areTypesIsomorphic - Recursively walk this pair of types, returning true
120 /// if they are isomorphic, 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()) return false;
125 // If we have an entry in the MappedTypes table, then we have our answer.
126 Type *&Entry = MappedTypes[SrcTy];
128 return Entry == DstTy;
130 // Two identical types are clearly isomorphic. Remember this
131 // non-speculatively.
132 if (DstTy == SrcTy) {
137 // Okay, we have two types with identical kinds that we haven't seen before.
139 // If this is an opaque struct type, special case it.
140 if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
141 // Mapping an opaque type to any struct, just keep the dest struct.
142 if (SSTy->isOpaque()) {
144 SpeculativeTypes.push_back(SrcTy);
148 // Mapping a non-opaque source type to an opaque dest. If this is the first
149 // type that we're mapping onto this destination type then we succeed. Keep
150 // the dest, but fill it in later. This doesn't need to be speculative. If
151 // this is the second (different) type that we're trying to map onto the
152 // 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)))
157 SrcDefinitionsToResolve.push_back(SSTy);
163 // If the number of subtypes disagree between the two types, then we fail.
164 if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
167 // Fail if any of the extra properties (e.g. array size) of the type disagree.
168 if (isa<IntegerType>(DstTy))
169 return false; // bitwidth disagrees.
170 if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
171 if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
174 } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
175 if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
177 } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
178 StructType *SSTy = cast<StructType>(SrcTy);
179 if (DSTy->isLiteral() != SSTy->isLiteral() ||
180 DSTy->isPacked() != SSTy->isPacked())
182 } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
183 if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
185 } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
186 if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements())
190 // Otherwise, we speculate that these two types will line up and recursively
191 // check the subelements.
193 SpeculativeTypes.push_back(SrcTy);
195 for (unsigned i = 0, e = SrcTy->getNumContainedTypes(); i != e; ++i)
196 if (!areTypesIsomorphic(DstTy->getContainedType(i),
197 SrcTy->getContainedType(i)))
200 // If everything seems to have lined up, then everything is great.
204 /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest
205 /// module from a type definition in the source module.
206 void TypeMapTy::linkDefinedTypeBodies() {
207 SmallVector<Type*, 16> Elements;
208 SmallString<16> TmpName;
210 // Note that processing entries in this loop (calling 'get') can add new
211 // entries to the SrcDefinitionsToResolve vector.
212 while (!SrcDefinitionsToResolve.empty()) {
213 StructType *SrcSTy = SrcDefinitionsToResolve.pop_back_val();
214 StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
216 // TypeMap is a many-to-one mapping, if there were multiple types that
217 // provide a body for DstSTy then previous iterations of this loop may have
218 // already handled it. Just ignore this case.
219 if (!DstSTy->isOpaque()) continue;
220 assert(!SrcSTy->isOpaque() && "Not resolving a definition?");
222 // Map the body of the source type over to a new body for the dest type.
223 Elements.resize(SrcSTy->getNumElements());
224 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
225 Elements[i] = getImpl(SrcSTy->getElementType(i));
227 DstSTy->setBody(Elements, SrcSTy->isPacked());
229 // If DstSTy has no name or has a longer name than STy, then viciously steal
231 if (!SrcSTy->hasName()) continue;
232 StringRef SrcName = SrcSTy->getName();
234 if (!DstSTy->hasName() || DstSTy->getName().size() > SrcName.size()) {
235 TmpName.insert(TmpName.end(), SrcName.begin(), SrcName.end());
237 DstSTy->setName(TmpName.str());
242 DstResolvedOpaqueTypes.clear();
245 /// get - Return the mapped type to use for the specified input type from the
247 Type *TypeMapTy::get(Type *Ty) {
248 Type *Result = getImpl(Ty);
250 // If this caused a reference to any struct type, resolve it before returning.
251 if (!SrcDefinitionsToResolve.empty())
252 linkDefinedTypeBodies();
256 /// getImpl - This is the recursive version of get().
257 Type *TypeMapTy::getImpl(Type *Ty) {
258 // If we already have an entry for this type, return it.
259 Type **Entry = &MappedTypes[Ty];
260 if (*Entry) return *Entry;
262 // If this is not a named struct type, then just map all of the elements and
263 // then rebuild the type from inside out.
264 if (!isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral()) {
265 // If there are no element types to map, then the type is itself. This is
266 // true for the anonymous {} struct, things like 'float', integers, etc.
267 if (Ty->getNumContainedTypes() == 0)
270 // Remap all of the elements, keeping track of whether any of them change.
271 bool AnyChange = false;
272 SmallVector<Type*, 4> ElementTypes;
273 ElementTypes.resize(Ty->getNumContainedTypes());
274 for (unsigned i = 0, e = Ty->getNumContainedTypes(); i != e; ++i) {
275 ElementTypes[i] = getImpl(Ty->getContainedType(i));
276 AnyChange |= ElementTypes[i] != Ty->getContainedType(i);
279 // If we found our type while recursively processing stuff, just use it.
280 Entry = &MappedTypes[Ty];
281 if (*Entry) return *Entry;
283 // If all of the element types mapped directly over, then the type is usable
288 // Otherwise, rebuild a modified type.
289 switch (Ty->getTypeID()) {
290 default: llvm_unreachable("unknown derived type to remap");
291 case Type::ArrayTyID:
292 return *Entry = ArrayType::get(ElementTypes[0],
293 cast<ArrayType>(Ty)->getNumElements());
294 case Type::VectorTyID:
295 return *Entry = VectorType::get(ElementTypes[0],
296 cast<VectorType>(Ty)->getNumElements());
297 case Type::PointerTyID:
298 return *Entry = PointerType::get(ElementTypes[0],
299 cast<PointerType>(Ty)->getAddressSpace());
300 case Type::FunctionTyID:
301 return *Entry = FunctionType::get(ElementTypes[0],
302 makeArrayRef(ElementTypes).slice(1),
303 cast<FunctionType>(Ty)->isVarArg());
304 case Type::StructTyID:
305 // Note that this is only reached for anonymous structs.
306 return *Entry = StructType::get(Ty->getContext(), ElementTypes,
307 cast<StructType>(Ty)->isPacked());
311 // Otherwise, this is an unmapped named struct. If the struct can be directly
312 // mapped over, just use it as-is. This happens in a case when the linked-in
313 // module has something like:
314 // %T = type {%T*, i32}
315 // @GV = global %T* null
316 // where T does not exist at all in the destination module.
318 // The other case we watch for is when the type is not in the destination
319 // module, but that it has to be rebuilt because it refers to something that
320 // is already mapped. For example, if the destination module has:
322 // and the source module has something like
323 // %A' = type { i32 }
324 // %B = type { %A'* }
325 // @GV = global %B* null
326 // then we want to create a new type: "%B = type { %A*}" and have it take the
327 // pristine "%B" name from the source module.
329 // To determine which case this is, we have to recursively walk the type graph
330 // speculating that we'll be able to reuse it unmodified. Only if this is
331 // safe would we map the entire thing over. Because this is an optimization,
332 // and is not required for the prettiness of the linked module, we just skip
333 // it and always rebuild a type here.
334 StructType *STy = cast<StructType>(Ty);
336 // If the type is opaque, we can just use it directly.
337 if (STy->isOpaque()) {
338 // A named structure type from src module is used. Add it to the Set of
339 // identified structs in the destination module.
340 DstStructTypesSet.insert(STy);
344 // Otherwise we create a new type and resolve its body later. This will be
345 // resolved by the top level of get().
346 SrcDefinitionsToResolve.push_back(STy);
347 StructType *DTy = StructType::create(STy->getContext());
348 // A new identified structure type was created. Add it to the set of
349 // identified structs in the destination module.
350 DstStructTypesSet.insert(DTy);
351 DstResolvedOpaqueTypes.insert(DTy);
355 //===----------------------------------------------------------------------===//
356 // ModuleLinker implementation.
357 //===----------------------------------------------------------------------===//
362 /// ValueMaterializerTy - Creates prototypes for functions that are lazily
363 /// linked on the fly. This speeds up linking for modules with many
364 /// lazily linked functions of which few get used.
365 class ValueMaterializerTy : public ValueMaterializer {
368 std::vector<Function*> &LazilyLinkFunctions;
370 ValueMaterializerTy(TypeMapTy &TypeMap, Module *DstM,
371 std::vector<Function*> &LazilyLinkFunctions) :
372 ValueMaterializer(), TypeMap(TypeMap), DstM(DstM),
373 LazilyLinkFunctions(LazilyLinkFunctions) {
376 Value *materializeValueFor(Value *V) override;
379 /// ModuleLinker - This is an implementation class for the LinkModules
380 /// function, which is the entrypoint for this file.
385 ValueMaterializerTy ValMaterializer;
387 /// ValueMap - Mapping of values from what they used to be in Src, to what
388 /// they are now in DstM. ValueToValueMapTy is a ValueMap, which involves
389 /// some overhead due to the use of Value handles which the Linker doesn't
390 /// actually need, but this allows us to reuse the ValueMapper code.
391 ValueToValueMapTy ValueMap;
393 struct AppendingVarInfo {
394 GlobalVariable *NewGV; // New aggregate global in dest module.
395 Constant *DstInit; // Old initializer from dest module.
396 Constant *SrcInit; // Old initializer from src module.
399 std::vector<AppendingVarInfo> AppendingVars;
401 unsigned Mode; // Mode to treat source module.
403 // Set of items not to link in from source.
404 SmallPtrSet<const Value*, 16> DoNotLinkFromSource;
406 // Vector of functions to lazily link in.
407 std::vector<Function*> LazilyLinkFunctions;
409 bool SuppressWarnings;
412 std::string ErrorMsg;
414 ModuleLinker(Module *dstM, TypeSet &Set, Module *srcM, unsigned mode,
415 bool SuppressWarnings=false)
416 : DstM(dstM), SrcM(srcM), TypeMap(Set),
417 ValMaterializer(TypeMap, DstM, LazilyLinkFunctions), Mode(mode),
418 SuppressWarnings(SuppressWarnings) {}
423 /// emitError - Helper method for setting a message and returning an error
425 bool emitError(const Twine &Message) {
426 ErrorMsg = Message.str();
430 bool getComdatLeader(Module *M, StringRef ComdatName,
431 const GlobalVariable *&GVar);
432 bool computeResultingSelectionKind(StringRef ComdatName,
433 Comdat::SelectionKind Src,
434 Comdat::SelectionKind Dst,
435 Comdat::SelectionKind &Result,
437 std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>>
439 bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK,
442 /// getLinkageResult - This analyzes the two global values and determines
443 /// what the result will look like in the destination module.
444 bool getLinkageResult(GlobalValue *Dest, const GlobalValue *Src,
445 GlobalValue::LinkageTypes <,
446 GlobalValue::VisibilityTypes &Vis,
449 /// getLinkedToGlobal - Given a global in the source module, return the
450 /// global in the destination module that is being linked to, if any.
451 GlobalValue *getLinkedToGlobal(GlobalValue *SrcGV) {
452 // If the source has no name it can't link. If it has local linkage,
453 // there is no name match-up going on.
454 if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
457 // Otherwise see if we have a match in the destination module's symtab.
458 GlobalValue *DGV = DstM->getNamedValue(SrcGV->getName());
459 if (!DGV) return nullptr;
461 // If we found a global with the same name in the dest module, but it has
462 // internal linkage, we are really not doing any linkage here.
463 if (DGV->hasLocalLinkage())
466 // Otherwise, we do in fact link to the destination global.
470 void computeTypeMapping();
472 bool linkAppendingVarProto(GlobalVariable *DstGV, GlobalVariable *SrcGV);
473 bool linkGlobalProto(GlobalVariable *SrcGV);
474 bool linkFunctionProto(Function *SrcF);
475 bool linkAliasProto(GlobalAlias *SrcA);
476 bool linkModuleFlagsMetadata();
478 void linkAppendingVarInit(const AppendingVarInfo &AVI);
479 void linkGlobalInits();
480 void linkFunctionBody(Function *Dst, Function *Src);
481 void linkAliasBodies();
482 void linkNamedMDNodes();
486 /// forceRenaming - The LLVM SymbolTable class autorenames globals that conflict
487 /// in the symbol table. This is good for all clients except for us. Go
488 /// through the trouble to force this back.
489 static void forceRenaming(GlobalValue *GV, StringRef Name) {
490 // If the global doesn't force its name or if it already has the right name,
491 // there is nothing for us to do.
492 if (GV->hasLocalLinkage() || GV->getName() == Name)
495 Module *M = GV->getParent();
497 // If there is a conflict, rename the conflict.
498 if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
499 GV->takeName(ConflictGV);
500 ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
501 assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
503 GV->setName(Name); // Force the name back
507 /// copyGVAttributes - copy additional attributes (those not needed to construct
508 /// a GlobalValue) from the SrcGV to the DestGV.
509 static void copyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) {
510 // Use the maximum alignment, rather than just copying the alignment of SrcGV.
511 auto *DestGO = dyn_cast<GlobalObject>(DestGV);
514 Alignment = std::max(DestGO->getAlignment(), SrcGV->getAlignment());
516 DestGV->copyAttributesFrom(SrcGV);
519 DestGO->setAlignment(Alignment);
521 forceRenaming(DestGV, SrcGV->getName());
524 static bool isLessConstraining(GlobalValue::VisibilityTypes a,
525 GlobalValue::VisibilityTypes b) {
526 if (a == GlobalValue::HiddenVisibility)
528 if (b == GlobalValue::HiddenVisibility)
530 if (a == GlobalValue::ProtectedVisibility)
532 if (b == GlobalValue::ProtectedVisibility)
537 Value *ValueMaterializerTy::materializeValueFor(Value *V) {
538 Function *SF = dyn_cast<Function>(V);
542 Function *DF = Function::Create(TypeMap.get(SF->getFunctionType()),
543 SF->getLinkage(), SF->getName(), DstM);
544 copyGVAttributes(DF, SF);
546 LazilyLinkFunctions.push_back(SF);
550 bool ModuleLinker::getComdatLeader(Module *M, StringRef ComdatName,
551 const GlobalVariable *&GVar) {
552 const GlobalValue *GVal = M->getNamedValue(ComdatName);
553 if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) {
554 GVal = GA->getBaseObject();
556 // We cannot resolve the size of the aliasee yet.
557 return emitError("Linking COMDATs named '" + ComdatName +
558 "': COMDAT key involves incomputable alias size.");
561 GVar = dyn_cast_or_null<GlobalVariable>(GVal);
564 "Linking COMDATs named '" + ComdatName +
565 "': GlobalVariable required for data dependent selection!");
570 bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName,
571 Comdat::SelectionKind Src,
572 Comdat::SelectionKind Dst,
573 Comdat::SelectionKind &Result,
575 // The ability to mix Comdat::SelectionKind::Any with
576 // Comdat::SelectionKind::Largest is a behavior that comes from COFF.
577 bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any ||
578 Dst == Comdat::SelectionKind::Largest;
579 bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any ||
580 Src == Comdat::SelectionKind::Largest;
581 if (DstAnyOrLargest && SrcAnyOrLargest) {
582 if (Dst == Comdat::SelectionKind::Largest ||
583 Src == Comdat::SelectionKind::Largest)
584 Result = Comdat::SelectionKind::Largest;
586 Result = Comdat::SelectionKind::Any;
587 } else if (Src == Dst) {
590 return emitError("Linking COMDATs named '" + ComdatName +
591 "': invalid selection kinds!");
595 case Comdat::SelectionKind::Any:
599 case Comdat::SelectionKind::NoDuplicates:
600 return emitError("Linking COMDATs named '" + ComdatName +
601 "': noduplicates has been violated!");
602 case Comdat::SelectionKind::ExactMatch:
603 case Comdat::SelectionKind::Largest:
604 case Comdat::SelectionKind::SameSize: {
605 const GlobalVariable *DstGV;
606 const GlobalVariable *SrcGV;
607 if (getComdatLeader(DstM, ComdatName, DstGV) ||
608 getComdatLeader(SrcM, ComdatName, SrcGV))
611 const DataLayout *DstDL = DstM->getDataLayout();
612 const DataLayout *SrcDL = SrcM->getDataLayout();
613 if (!DstDL || !SrcDL) {
615 "Linking COMDATs named '" + ComdatName +
616 "': can't do size dependent selection without DataLayout!");
619 DstDL->getTypeAllocSize(DstGV->getType()->getPointerElementType());
621 SrcDL->getTypeAllocSize(SrcGV->getType()->getPointerElementType());
622 if (Result == Comdat::SelectionKind::ExactMatch) {
623 if (SrcGV->getInitializer() != DstGV->getInitializer())
624 return emitError("Linking COMDATs named '" + ComdatName +
625 "': ExactMatch violated!");
627 } else if (Result == Comdat::SelectionKind::Largest) {
628 LinkFromSrc = SrcSize > DstSize;
629 } else if (Result == Comdat::SelectionKind::SameSize) {
630 if (SrcSize != DstSize)
631 return emitError("Linking COMDATs named '" + ComdatName +
632 "': SameSize violated!");
635 llvm_unreachable("unknown selection kind");
644 bool ModuleLinker::getComdatResult(const Comdat *SrcC,
645 Comdat::SelectionKind &Result,
647 StringRef ComdatName = SrcC->getName();
648 Module::ComdatSymTabType &ComdatSymTab = DstM->getComdatSymbolTable();
649 Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName);
650 if (DstCI != ComdatSymTab.end()) {
651 const Comdat *DstC = &DstCI->second;
652 Comdat::SelectionKind SSK = SrcC->getSelectionKind();
653 Comdat::SelectionKind DSK = DstC->getSelectionKind();
654 if (computeResultingSelectionKind(ComdatName, SSK, DSK, Result, LinkFromSrc))
660 /// getLinkageResult - This analyzes the two global values and determines what
661 /// the result will look like in the destination module. In particular, it
662 /// computes the resultant linkage type and visibility, computes whether the
663 /// global in the source should be copied over to the destination (replacing
664 /// the existing one), and computes whether this linkage is an error or not.
665 bool ModuleLinker::getLinkageResult(GlobalValue *Dest, const GlobalValue *Src,
666 GlobalValue::LinkageTypes <,
667 GlobalValue::VisibilityTypes &Vis,
669 assert(Dest && "Must have two globals being queried");
670 assert(!Src->hasLocalLinkage() &&
671 "If Src has internal linkage, Dest shouldn't be set!");
673 bool SrcIsDeclaration = Src->isDeclaration() && !Src->isMaterializable();
674 bool DestIsDeclaration = Dest->isDeclaration();
676 if (SrcIsDeclaration) {
677 // If Src is external or if both Src & Dest are external.. Just link the
678 // external globals, we aren't adding anything.
679 if (Src->hasDLLImportStorageClass()) {
680 // If one of GVs is marked as DLLImport, result should be dllimport'ed.
681 if (DestIsDeclaration) {
683 LT = Src->getLinkage();
685 } else if (Dest->hasExternalWeakLinkage()) {
686 // If the Dest is weak, use the source linkage.
688 LT = Src->getLinkage();
691 LT = Dest->getLinkage();
693 } else if (DestIsDeclaration && !Dest->hasDLLImportStorageClass()) {
694 // If Dest is external but Src is not:
696 LT = Src->getLinkage();
697 } else if (Src->isWeakForLinker()) {
698 // At this point we know that Dest has LinkOnce, External*, Weak, Common,
700 if (Dest->hasExternalWeakLinkage() ||
701 Dest->hasAvailableExternallyLinkage() ||
702 (Dest->hasLinkOnceLinkage() &&
703 (Src->hasWeakLinkage() || Src->hasCommonLinkage()))) {
705 LT = Src->getLinkage();
708 LT = Dest->getLinkage();
710 } else if (Dest->isWeakForLinker()) {
711 // At this point we know that Src has External* or DLL* linkage.
712 if (Src->hasExternalWeakLinkage()) {
714 LT = Dest->getLinkage();
717 LT = GlobalValue::ExternalLinkage;
720 assert((Dest->hasExternalLinkage() || Dest->hasExternalWeakLinkage()) &&
721 (Src->hasExternalLinkage() || Src->hasExternalWeakLinkage()) &&
722 "Unexpected linkage type!");
723 return emitError("Linking globals named '" + Src->getName() +
724 "': symbol multiply defined!");
727 // Compute the visibility. We follow the rules in the System V Application
729 assert(!GlobalValue::isLocalLinkage(LT) &&
730 "Symbols with local linkage should not be merged");
731 Vis = isLessConstraining(Src->getVisibility(), Dest->getVisibility()) ?
732 Dest->getVisibility() : Src->getVisibility();
736 /// computeTypeMapping - Loop over all of the linked values to compute type
737 /// mappings. For example, if we link "extern Foo *x" and "Foo *x = NULL", then
738 /// we have two struct types 'Foo' but one got renamed when the module was
739 /// loaded into the same LLVMContext.
740 void ModuleLinker::computeTypeMapping() {
741 // Incorporate globals.
742 for (Module::global_iterator I = SrcM->global_begin(),
743 E = SrcM->global_end(); I != E; ++I) {
744 GlobalValue *DGV = getLinkedToGlobal(I);
747 if (!DGV->hasAppendingLinkage() || !I->hasAppendingLinkage()) {
748 TypeMap.addTypeMapping(DGV->getType(), I->getType());
752 // Unify the element type of appending arrays.
753 ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType());
754 ArrayType *SAT = cast<ArrayType>(I->getType()->getElementType());
755 TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
758 // Incorporate functions.
759 for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I) {
760 if (GlobalValue *DGV = getLinkedToGlobal(I))
761 TypeMap.addTypeMapping(DGV->getType(), I->getType());
764 // Incorporate types by name, scanning all the types in the source module.
765 // At this point, the destination module may have a type "%foo = { i32 }" for
766 // example. When the source module got loaded into the same LLVMContext, if
767 // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
768 TypeFinder SrcStructTypes;
769 SrcStructTypes.run(*SrcM, true);
770 SmallPtrSet<StructType*, 32> SrcStructTypesSet(SrcStructTypes.begin(),
771 SrcStructTypes.end());
773 for (unsigned i = 0, e = SrcStructTypes.size(); i != e; ++i) {
774 StructType *ST = SrcStructTypes[i];
775 if (!ST->hasName()) continue;
777 // Check to see if there is a dot in the name followed by a digit.
778 size_t DotPos = ST->getName().rfind('.');
779 if (DotPos == 0 || DotPos == StringRef::npos ||
780 ST->getName().back() == '.' ||
781 !isdigit(static_cast<unsigned char>(ST->getName()[DotPos+1])))
784 // Check to see if the destination module has a struct with the prefix name.
785 if (StructType *DST = DstM->getTypeByName(ST->getName().substr(0, DotPos)))
786 // Don't use it if this actually came from the source module. They're in
787 // the same LLVMContext after all. Also don't use it unless the type is
788 // actually used in the destination module. This can happen in situations
793 // %Z = type { %A } %B = type { %C.1 }
794 // %A = type { %B.1, [7 x i8] } %C.1 = type { i8* }
795 // %B.1 = type { %C } %A.2 = type { %B.3, [5 x i8] }
796 // %C = type { i8* } %B.3 = type { %C.1 }
798 // When we link Module B with Module A, the '%B' in Module B is
799 // used. However, that would then use '%C.1'. But when we process '%C.1',
800 // we prefer to take the '%C' version. So we are then left with both
801 // '%C.1' and '%C' being used for the same types. This leads to some
802 // variables using one type and some using the other.
803 if (!SrcStructTypesSet.count(DST) && TypeMap.DstStructTypesSet.count(DST))
804 TypeMap.addTypeMapping(DST, ST);
807 // Don't bother incorporating aliases, they aren't generally typed well.
809 // Now that we have discovered all of the type equivalences, get a body for
810 // any 'opaque' types in the dest module that are now resolved.
811 TypeMap.linkDefinedTypeBodies();
814 /// linkAppendingVarProto - If there were any appending global variables, link
815 /// them together now. Return true on error.
816 bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV,
817 GlobalVariable *SrcGV) {
819 if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
820 return emitError("Linking globals named '" + SrcGV->getName() +
821 "': can only link appending global with another appending global!");
823 ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
825 cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()));
826 Type *EltTy = DstTy->getElementType();
828 // Check to see that they two arrays agree on type.
829 if (EltTy != SrcTy->getElementType())
830 return emitError("Appending variables with different element types!");
831 if (DstGV->isConstant() != SrcGV->isConstant())
832 return emitError("Appending variables linked with different const'ness!");
834 if (DstGV->getAlignment() != SrcGV->getAlignment())
836 "Appending variables with different alignment need to be linked!");
838 if (DstGV->getVisibility() != SrcGV->getVisibility())
840 "Appending variables with different visibility need to be linked!");
842 if (DstGV->hasUnnamedAddr() != SrcGV->hasUnnamedAddr())
844 "Appending variables with different unnamed_addr need to be linked!");
846 if (StringRef(DstGV->getSection()) != SrcGV->getSection())
848 "Appending variables with different section name need to be linked!");
850 uint64_t NewSize = DstTy->getNumElements() + SrcTy->getNumElements();
851 ArrayType *NewType = ArrayType::get(EltTy, NewSize);
853 // Create the new global variable.
855 new GlobalVariable(*DstGV->getParent(), NewType, SrcGV->isConstant(),
856 DstGV->getLinkage(), /*init*/nullptr, /*name*/"", DstGV,
857 DstGV->getThreadLocalMode(),
858 DstGV->getType()->getAddressSpace());
860 // Propagate alignment, visibility and section info.
861 copyGVAttributes(NG, DstGV);
863 AppendingVarInfo AVI;
865 AVI.DstInit = DstGV->getInitializer();
866 AVI.SrcInit = SrcGV->getInitializer();
867 AppendingVars.push_back(AVI);
869 // Replace any uses of the two global variables with uses of the new
871 ValueMap[SrcGV] = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
873 DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
874 DstGV->eraseFromParent();
876 // Track the source variable so we don't try to link it.
877 DoNotLinkFromSource.insert(SrcGV);
882 /// linkGlobalProto - Loop through the global variables in the src module and
883 /// merge them into the dest module.
884 bool ModuleLinker::linkGlobalProto(GlobalVariable *SGV) {
885 GlobalValue *DGV = getLinkedToGlobal(SGV);
886 llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility;
887 bool HasUnnamedAddr = SGV->hasUnnamedAddr();
889 bool LinkFromSrc = false;
890 Comdat *DC = nullptr;
891 if (const Comdat *SC = SGV->getComdat()) {
892 Comdat::SelectionKind SK;
893 std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
894 DC = DstM->getOrInsertComdat(SC->getName());
895 DC->setSelectionKind(SK);
900 // Concatenation of appending linkage variables is magic and handled later.
901 if (DGV->hasAppendingLinkage() || SGV->hasAppendingLinkage())
902 return linkAppendingVarProto(cast<GlobalVariable>(DGV), SGV);
904 // Determine whether linkage of these two globals follows the source
905 // module's definition or the destination module's definition.
906 GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
907 GlobalValue::VisibilityTypes NV;
908 if (getLinkageResult(DGV, SGV, NewLinkage, NV, LinkFromSrc))
911 HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
913 // If we're not linking from the source, then keep the definition that we
916 // Special case for const propagation.
917 if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV))
918 if (DGVar->isDeclaration() && SGV->isConstant() &&
919 !DGVar->isConstant())
920 DGVar->setConstant(true);
922 // Set calculated linkage, visibility and unnamed_addr.
923 DGV->setLinkage(NewLinkage);
924 DGV->setVisibility(*NewVisibility);
925 DGV->setUnnamedAddr(HasUnnamedAddr);
930 // Make sure to remember this mapping.
931 ValueMap[SGV] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGV->getType()));
933 // Track the source global so that we don't attempt to copy it over when
934 // processing global initializers.
935 DoNotLinkFromSource.insert(SGV);
941 // If the Comdat this variable was inside of wasn't selected, skip it.
942 if (DC && !DGV && !LinkFromSrc) {
943 DoNotLinkFromSource.insert(SGV);
947 // No linking to be performed or linking from the source: simply create an
948 // identical version of the symbol over in the dest module... the
949 // initializer will be filled in later by LinkGlobalInits.
950 GlobalVariable *NewDGV =
951 new GlobalVariable(*DstM, TypeMap.get(SGV->getType()->getElementType()),
952 SGV->isConstant(), SGV->getLinkage(), /*init*/nullptr,
953 SGV->getName(), /*insertbefore*/nullptr,
954 SGV->getThreadLocalMode(),
955 SGV->getType()->getAddressSpace());
956 // Propagate alignment, visibility and section info.
957 copyGVAttributes(NewDGV, SGV);
959 NewDGV->setVisibility(*NewVisibility);
960 NewDGV->setUnnamedAddr(HasUnnamedAddr);
963 NewDGV->setComdat(DC);
966 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV, DGV->getType()));
967 DGV->eraseFromParent();
970 // Make sure to remember this mapping.
971 ValueMap[SGV] = NewDGV;
975 /// linkFunctionProto - Link the function in the source module into the
976 /// destination module if needed, setting up mapping information.
977 bool ModuleLinker::linkFunctionProto(Function *SF) {
978 GlobalValue *DGV = getLinkedToGlobal(SF);
979 llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility;
980 bool HasUnnamedAddr = SF->hasUnnamedAddr();
982 bool LinkFromSrc = false;
983 Comdat *DC = nullptr;
984 if (const Comdat *SC = SF->getComdat()) {
985 Comdat::SelectionKind SK;
986 std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
987 DC = DstM->getOrInsertComdat(SC->getName());
988 DC->setSelectionKind(SK);
993 GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
994 GlobalValue::VisibilityTypes NV;
995 if (getLinkageResult(DGV, SF, NewLinkage, NV, LinkFromSrc))
998 HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
1001 // Set calculated linkage
1002 DGV->setLinkage(NewLinkage);
1003 DGV->setVisibility(*NewVisibility);
1004 DGV->setUnnamedAddr(HasUnnamedAddr);
1009 // Make sure to remember this mapping.
1010 ValueMap[SF] = ConstantExpr::getBitCast(DGV, TypeMap.get(SF->getType()));
1012 // Track the function from the source module so we don't attempt to remap
1014 DoNotLinkFromSource.insert(SF);
1020 // If the function is to be lazily linked, don't create it just yet.
1021 // The ValueMaterializerTy will deal with creating it if it's used.
1022 if (!DGV && (SF->hasLocalLinkage() || SF->hasLinkOnceLinkage() ||
1023 SF->hasAvailableExternallyLinkage())) {
1024 DoNotLinkFromSource.insert(SF);
1028 // If the Comdat this function was inside of wasn't selected, skip it.
1029 if (DC && !DGV && !LinkFromSrc) {
1030 DoNotLinkFromSource.insert(SF);
1034 // If there is no linkage to be performed or we are linking from the source,
1036 Function *NewDF = Function::Create(TypeMap.get(SF->getFunctionType()),
1037 SF->getLinkage(), SF->getName(), DstM);
1038 copyGVAttributes(NewDF, SF);
1040 NewDF->setVisibility(*NewVisibility);
1041 NewDF->setUnnamedAddr(HasUnnamedAddr);
1044 NewDF->setComdat(DC);
1047 // Any uses of DF need to change to NewDF, with cast.
1048 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType()));
1049 DGV->eraseFromParent();
1052 ValueMap[SF] = NewDF;
1056 /// LinkAliasProto - Set up prototypes for any aliases that come over from the
1058 bool ModuleLinker::linkAliasProto(GlobalAlias *SGA) {
1059 GlobalValue *DGV = getLinkedToGlobal(SGA);
1060 llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility;
1061 bool HasUnnamedAddr = SGA->hasUnnamedAddr();
1063 bool LinkFromSrc = false;
1064 Comdat *DC = nullptr;
1065 if (const Comdat *SC = SGA->getComdat()) {
1066 Comdat::SelectionKind SK;
1067 std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
1068 DC = DstM->getOrInsertComdat(SC->getName());
1069 DC->setSelectionKind(SK);
1074 GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
1075 GlobalValue::VisibilityTypes NV;
1076 if (getLinkageResult(DGV, SGA, NewLinkage, NV, LinkFromSrc))
1079 HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
1082 // Set calculated linkage.
1083 DGV->setLinkage(NewLinkage);
1084 DGV->setVisibility(*NewVisibility);
1085 DGV->setUnnamedAddr(HasUnnamedAddr);
1090 // Make sure to remember this mapping.
1091 ValueMap[SGA] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGA->getType()));
1093 // Track the alias from the source module so we don't attempt to remap it.
1094 DoNotLinkFromSource.insert(SGA);
1100 // If the Comdat this alias was inside of wasn't selected, skip it.
1101 if (DC && !DGV && !LinkFromSrc) {
1102 DoNotLinkFromSource.insert(SGA);
1106 // If there is no linkage to be performed or we're linking from the source,
1108 auto *PTy = cast<PointerType>(TypeMap.get(SGA->getType()));
1110 GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1111 SGA->getLinkage(), SGA->getName(), DstM);
1112 copyGVAttributes(NewDA, SGA);
1114 NewDA->setVisibility(*NewVisibility);
1115 NewDA->setUnnamedAddr(HasUnnamedAddr);
1118 // Any uses of DGV need to change to NewDA, with cast.
1119 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDA, DGV->getType()));
1120 DGV->eraseFromParent();
1123 ValueMap[SGA] = NewDA;
1127 static void getArrayElements(Constant *C, SmallVectorImpl<Constant*> &Dest) {
1128 unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
1130 for (unsigned i = 0; i != NumElements; ++i)
1131 Dest.push_back(C->getAggregateElement(i));
1134 void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) {
1135 // Merge the initializer.
1136 SmallVector<Constant*, 16> Elements;
1137 getArrayElements(AVI.DstInit, Elements);
1139 Constant *SrcInit = MapValue(AVI.SrcInit, ValueMap, RF_None, &TypeMap, &ValMaterializer);
1140 getArrayElements(SrcInit, Elements);
1142 ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType());
1143 AVI.NewGV->setInitializer(ConstantArray::get(NewType, Elements));
1146 /// linkGlobalInits - Update the initializers in the Dest module now that all
1147 /// globals that may be referenced are in Dest.
1148 void ModuleLinker::linkGlobalInits() {
1149 // Loop over all of the globals in the src module, mapping them over as we go
1150 for (Module::const_global_iterator I = SrcM->global_begin(),
1151 E = SrcM->global_end(); I != E; ++I) {
1153 // Only process initialized GV's or ones not already in dest.
1154 if (!I->hasInitializer() || DoNotLinkFromSource.count(I)) continue;
1156 // Grab destination global variable.
1157 GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[I]);
1158 // Figure out what the initializer looks like in the dest module.
1159 DGV->setInitializer(MapValue(I->getInitializer(), ValueMap,
1160 RF_None, &TypeMap, &ValMaterializer));
1164 /// linkFunctionBody - Copy the source function over into the dest function and
1165 /// fix up references to values. At this point we know that Dest is an external
1166 /// function, and that Src is not.
1167 void ModuleLinker::linkFunctionBody(Function *Dst, Function *Src) {
1168 assert(Src && Dst && Dst->isDeclaration() && !Src->isDeclaration());
1170 // Go through and convert function arguments over, remembering the mapping.
1171 Function::arg_iterator DI = Dst->arg_begin();
1172 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
1173 I != E; ++I, ++DI) {
1174 DI->setName(I->getName()); // Copy the name over.
1176 // Add a mapping to our mapping.
1180 if (Mode == Linker::DestroySource) {
1181 // Splice the body of the source function into the dest function.
1182 Dst->getBasicBlockList().splice(Dst->end(), Src->getBasicBlockList());
1184 // At this point, all of the instructions and values of the function are now
1185 // copied over. The only problem is that they are still referencing values in
1186 // the Source function as operands. Loop through all of the operands of the
1187 // functions and patch them up to point to the local versions.
1188 for (Function::iterator BB = Dst->begin(), BE = Dst->end(); BB != BE; ++BB)
1189 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1190 RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries,
1191 &TypeMap, &ValMaterializer);
1194 // Clone the body of the function into the dest function.
1195 SmallVector<ReturnInst*, 8> Returns; // Ignore returns.
1196 CloneFunctionInto(Dst, Src, ValueMap, false, Returns, "", nullptr,
1197 &TypeMap, &ValMaterializer);
1200 // There is no need to map the arguments anymore.
1201 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
1207 /// linkAliasBodies - Insert all of the aliases in Src into the Dest module.
1208 void ModuleLinker::linkAliasBodies() {
1209 for (Module::alias_iterator I = SrcM->alias_begin(), E = SrcM->alias_end();
1211 if (DoNotLinkFromSource.count(I))
1213 if (Constant *Aliasee = I->getAliasee()) {
1214 GlobalAlias *DA = cast<GlobalAlias>(ValueMap[I]);
1216 MapValue(Aliasee, ValueMap, RF_None, &TypeMap, &ValMaterializer);
1217 DA->setAliasee(Val);
1222 /// linkNamedMDNodes - Insert all of the named MDNodes in Src into the Dest
1224 void ModuleLinker::linkNamedMDNodes() {
1225 const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1226 for (Module::const_named_metadata_iterator I = SrcM->named_metadata_begin(),
1227 E = SrcM->named_metadata_end(); I != E; ++I) {
1228 // Don't link module flags here. Do them separately.
1229 if (&*I == SrcModFlags) continue;
1230 NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(I->getName());
1231 // Add Src elements into Dest node.
1232 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1233 DestNMD->addOperand(MapValue(I->getOperand(i), ValueMap,
1234 RF_None, &TypeMap, &ValMaterializer));
1238 /// linkModuleFlagsMetadata - Merge the linker flags in Src into the Dest
1240 bool ModuleLinker::linkModuleFlagsMetadata() {
1241 // If the source module has no module flags, we are done.
1242 const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1243 if (!SrcModFlags) return false;
1245 // If the destination module doesn't have module flags yet, then just copy
1246 // over the source module's flags.
1247 NamedMDNode *DstModFlags = DstM->getOrInsertModuleFlagsMetadata();
1248 if (DstModFlags->getNumOperands() == 0) {
1249 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1250 DstModFlags->addOperand(SrcModFlags->getOperand(I));
1255 // First build a map of the existing module flags and requirements.
1256 DenseMap<MDString*, MDNode*> Flags;
1257 SmallSetVector<MDNode*, 16> Requirements;
1258 for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1259 MDNode *Op = DstModFlags->getOperand(I);
1260 ConstantInt *Behavior = cast<ConstantInt>(Op->getOperand(0));
1261 MDString *ID = cast<MDString>(Op->getOperand(1));
1263 if (Behavior->getZExtValue() == Module::Require) {
1264 Requirements.insert(cast<MDNode>(Op->getOperand(2)));
1270 // Merge in the flags from the source module, and also collect its set of
1272 bool HasErr = false;
1273 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1274 MDNode *SrcOp = SrcModFlags->getOperand(I);
1275 ConstantInt *SrcBehavior = cast<ConstantInt>(SrcOp->getOperand(0));
1276 MDString *ID = cast<MDString>(SrcOp->getOperand(1));
1277 MDNode *DstOp = Flags.lookup(ID);
1278 unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
1280 // If this is a requirement, add it and continue.
1281 if (SrcBehaviorValue == Module::Require) {
1282 // If the destination module does not already have this requirement, add
1284 if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
1285 DstModFlags->addOperand(SrcOp);
1290 // If there is no existing flag with this ID, just add it.
1293 DstModFlags->addOperand(SrcOp);
1297 // Otherwise, perform a merge.
1298 ConstantInt *DstBehavior = cast<ConstantInt>(DstOp->getOperand(0));
1299 unsigned DstBehaviorValue = DstBehavior->getZExtValue();
1301 // If either flag has override behavior, handle it first.
1302 if (DstBehaviorValue == Module::Override) {
1303 // Diagnose inconsistent flags which both have override behavior.
1304 if (SrcBehaviorValue == Module::Override &&
1305 SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1306 HasErr |= emitError("linking module flags '" + ID->getString() +
1307 "': IDs have conflicting override values");
1310 } else if (SrcBehaviorValue == Module::Override) {
1311 // Update the destination flag to that of the source.
1312 DstOp->replaceOperandWith(0, SrcBehavior);
1313 DstOp->replaceOperandWith(2, SrcOp->getOperand(2));
1317 // Diagnose inconsistent merge behavior types.
1318 if (SrcBehaviorValue != DstBehaviorValue) {
1319 HasErr |= emitError("linking module flags '" + ID->getString() +
1320 "': IDs have conflicting behaviors");
1324 // Perform the merge for standard behavior types.
1325 switch (SrcBehaviorValue) {
1326 case Module::Require:
1327 case Module::Override: llvm_unreachable("not possible");
1328 case Module::Error: {
1329 // Emit an error if the values differ.
1330 if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1331 HasErr |= emitError("linking module flags '" + ID->getString() +
1332 "': IDs have conflicting values");
1336 case Module::Warning: {
1337 // Emit a warning if the values differ.
1338 if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1339 if (!SuppressWarnings) {
1340 errs() << "WARNING: linking module flags '" << ID->getString()
1341 << "': IDs have conflicting values";
1346 case Module::Append: {
1347 MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1348 MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1349 unsigned NumOps = DstValue->getNumOperands() + SrcValue->getNumOperands();
1350 Value **VP, **Values = VP = new Value*[NumOps];
1351 for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i, ++VP)
1352 *VP = DstValue->getOperand(i);
1353 for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i, ++VP)
1354 *VP = SrcValue->getOperand(i);
1355 DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(),
1356 ArrayRef<Value*>(Values,
1361 case Module::AppendUnique: {
1362 SmallSetVector<Value*, 16> Elts;
1363 MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1364 MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1365 for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i)
1366 Elts.insert(DstValue->getOperand(i));
1367 for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i)
1368 Elts.insert(SrcValue->getOperand(i));
1369 DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(),
1370 ArrayRef<Value*>(Elts.begin(),
1377 // Check all of the requirements.
1378 for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
1379 MDNode *Requirement = Requirements[I];
1380 MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1381 Value *ReqValue = Requirement->getOperand(1);
1383 MDNode *Op = Flags[Flag];
1384 if (!Op || Op->getOperand(2) != ReqValue) {
1385 HasErr |= emitError("linking module flags '" + Flag->getString() +
1386 "': does not have the required value");
1394 bool ModuleLinker::run() {
1395 assert(DstM && "Null destination module");
1396 assert(SrcM && "Null source module");
1398 // Inherit the target data from the source module if the destination module
1399 // doesn't have one already.
1400 if (!DstM->getDataLayout() && SrcM->getDataLayout())
1401 DstM->setDataLayout(SrcM->getDataLayout());
1403 // Copy the target triple from the source to dest if the dest's is empty.
1404 if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
1405 DstM->setTargetTriple(SrcM->getTargetTriple());
1407 if (SrcM->getDataLayout() && DstM->getDataLayout() &&
1408 *SrcM->getDataLayout() != *DstM->getDataLayout()) {
1409 if (!SuppressWarnings) {
1410 errs() << "WARNING: Linking two modules of different data layouts: '"
1411 << SrcM->getModuleIdentifier() << "' is '"
1412 << SrcM->getDataLayoutStr() << "' whereas '"
1413 << DstM->getModuleIdentifier() << "' is '"
1414 << DstM->getDataLayoutStr() << "'\n";
1417 if (!SrcM->getTargetTriple().empty() &&
1418 DstM->getTargetTriple() != SrcM->getTargetTriple()) {
1419 if (!SuppressWarnings) {
1420 errs() << "WARNING: Linking two modules of different target triples: "
1421 << SrcM->getModuleIdentifier() << "' is '"
1422 << SrcM->getTargetTriple() << "' whereas '"
1423 << DstM->getModuleIdentifier() << "' is '"
1424 << DstM->getTargetTriple() << "'\n";
1428 // Append the module inline asm string.
1429 if (!SrcM->getModuleInlineAsm().empty()) {
1430 if (DstM->getModuleInlineAsm().empty())
1431 DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm());
1433 DstM->setModuleInlineAsm(DstM->getModuleInlineAsm()+"\n"+
1434 SrcM->getModuleInlineAsm());
1437 // Loop over all of the linked values to compute type mappings.
1438 computeTypeMapping();
1440 ComdatsChosen.clear();
1441 for (const StringMapEntry<llvm::Comdat> &SMEC : SrcM->getComdatSymbolTable()) {
1442 const Comdat &C = SMEC.getValue();
1443 if (ComdatsChosen.count(&C))
1445 Comdat::SelectionKind SK;
1447 if (getComdatResult(&C, SK, LinkFromSrc))
1449 ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc);
1452 // Insert all of the globals in src into the DstM module... without linking
1453 // initializers (which could refer to functions not yet mapped over).
1454 for (Module::global_iterator I = SrcM->global_begin(),
1455 E = SrcM->global_end(); I != E; ++I)
1456 if (linkGlobalProto(I))
1459 // Link the functions together between the two modules, without doing function
1460 // bodies... this just adds external function prototypes to the DstM
1461 // function... We do this so that when we begin processing function bodies,
1462 // all of the global values that may be referenced are available in our
1464 for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I)
1465 if (linkFunctionProto(I))
1468 // If there were any aliases, link them now.
1469 for (Module::alias_iterator I = SrcM->alias_begin(),
1470 E = SrcM->alias_end(); I != E; ++I)
1471 if (linkAliasProto(I))
1474 for (unsigned i = 0, e = AppendingVars.size(); i != e; ++i)
1475 linkAppendingVarInit(AppendingVars[i]);
1477 // Link in the function bodies that are defined in the source module into
1479 for (Module::iterator SF = SrcM->begin(), E = SrcM->end(); SF != E; ++SF) {
1480 // Skip if not linking from source.
1481 if (DoNotLinkFromSource.count(SF)) continue;
1483 Function *DF = cast<Function>(ValueMap[SF]);
1484 if (SF->hasPrefixData()) {
1485 // Link in the prefix data.
1486 DF->setPrefixData(MapValue(
1487 SF->getPrefixData(), ValueMap, RF_None, &TypeMap, &ValMaterializer));
1490 // Skip if no body (function is external) or materialize.
1491 if (SF->isDeclaration()) {
1492 if (!SF->isMaterializable())
1494 if (SF->Materialize(&ErrorMsg))
1498 linkFunctionBody(DF, SF);
1499 SF->Dematerialize();
1502 // Resolve all uses of aliases with aliasees.
1505 // Remap all of the named MDNodes in Src into the DstM module. We do this
1506 // after linking GlobalValues so that MDNodes that reference GlobalValues
1507 // are properly remapped.
1510 // Merge the module flags into the DstM module.
1511 if (linkModuleFlagsMetadata())
1514 // Update the initializers in the DstM module now that all globals that may
1515 // be referenced are in DstM.
1518 // Process vector of lazily linked in functions.
1519 bool LinkedInAnyFunctions;
1521 LinkedInAnyFunctions = false;
1523 for(std::vector<Function*>::iterator I = LazilyLinkFunctions.begin(),
1524 E = LazilyLinkFunctions.end(); I != E; ++I) {
1529 Function *DF = cast<Function>(ValueMap[SF]);
1530 if (SF->hasPrefixData()) {
1531 // Link in the prefix data.
1532 DF->setPrefixData(MapValue(SF->getPrefixData(),
1539 // Materialize if necessary.
1540 if (SF->isDeclaration()) {
1541 if (!SF->isMaterializable())
1543 if (SF->Materialize(&ErrorMsg))
1547 // Erase from vector *before* the function body is linked - linkFunctionBody could
1549 LazilyLinkFunctions.erase(I);
1551 // Link in function body.
1552 linkFunctionBody(DF, SF);
1553 SF->Dematerialize();
1555 // Set flag to indicate we may have more functions to lazily link in
1556 // since we linked in a function.
1557 LinkedInAnyFunctions = true;
1560 } while (LinkedInAnyFunctions);
1562 // Now that all of the types from the source are used, resolve any structs
1563 // copied over to the dest that didn't exist there.
1564 TypeMap.linkDefinedTypeBodies();
1569 Linker::Linker(Module *M, bool SuppressWarnings)
1570 : Composite(M), SuppressWarnings(SuppressWarnings) {
1571 TypeFinder StructTypes;
1572 StructTypes.run(*M, true);
1573 IdentifiedStructTypes.insert(StructTypes.begin(), StructTypes.end());
1579 void Linker::deleteModule() {
1581 Composite = nullptr;
1584 bool Linker::linkInModule(Module *Src, unsigned Mode, std::string *ErrorMsg) {
1585 ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src, Mode,
1587 if (TheLinker.run()) {
1589 *ErrorMsg = TheLinker.ErrorMsg;
1595 //===----------------------------------------------------------------------===//
1596 // LinkModules entrypoint.
1597 //===----------------------------------------------------------------------===//
1599 /// LinkModules - This function links two modules together, with the resulting
1600 /// Dest module modified to be the composite of the two input modules. If an
1601 /// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
1602 /// the problem. Upon failure, the Dest module could be in a modified state,
1603 /// and shouldn't be relied on to be consistent.
1604 bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Mode,
1605 std::string *ErrorMsg) {
1607 return L.linkInModule(Src, Mode, ErrorMsg);
1610 //===----------------------------------------------------------------------===//
1612 //===----------------------------------------------------------------------===//
1614 LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
1615 LLVMLinkerMode Mode, char **OutMessages) {
1616 std::string Messages;
1617 LLVMBool Result = Linker::LinkModules(unwrap(Dest), unwrap(Src),
1618 Mode, OutMessages? &Messages : nullptr);
1620 *OutMessages = strdup(Messages.c_str());