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.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Module.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Transforms/Utils/Cloning.h"
23 #include "llvm/Transforms/Utils/ValueMapper.h"
26 //===----------------------------------------------------------------------===//
27 // TypeMap implementation.
28 //===----------------------------------------------------------------------===//
31 class TypeMapTy : public ValueMapTypeRemapper {
32 /// MappedTypes - This is a mapping from a source type to a destination type
34 DenseMap<Type*, Type*> MappedTypes;
36 /// SpeculativeTypes - When checking to see if two subgraphs are isomorphic,
37 /// we speculatively add types to MappedTypes, but keep track of them here in
38 /// case we need to roll back.
39 SmallVector<Type*, 16> SpeculativeTypes;
41 /// DefinitionsToResolve - This is a list of non-opaque structs in the source
42 /// module that are mapped to an opaque struct in the destination module.
43 SmallVector<StructType*, 16> DefinitionsToResolve;
46 /// addTypeMapping - Indicate that the specified type in the destination
47 /// module is conceptually equivalent to the specified type in the source
49 void addTypeMapping(Type *DstTy, Type *SrcTy);
51 /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest
52 /// module from a type definition in the source module.
53 void linkDefinedTypeBodies();
55 /// get - Return the mapped type to use for the specified input type from the
57 Type *get(Type *SrcTy);
59 FunctionType *get(FunctionType *T) {return cast<FunctionType>(get((Type*)T));}
62 Type *getImpl(Type *T);
63 /// remapType - Implement the ValueMapTypeRemapper interface.
64 Type *remapType(Type *SrcTy) {
68 bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
72 void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
73 Type *&Entry = MappedTypes[SrcTy];
81 // Check to see if these types are recursively isomorphic and establish a
82 // mapping between them if so.
83 if (!areTypesIsomorphic(DstTy, SrcTy)) {
84 // Oops, they aren't isomorphic. Just discard this request by rolling out
85 // any speculative mappings we've established.
86 for (unsigned i = 0, e = SpeculativeTypes.size(); i != e; ++i)
87 MappedTypes.erase(SpeculativeTypes[i]);
89 SpeculativeTypes.clear();
92 /// areTypesIsomorphic - Recursively walk this pair of types, returning true
93 /// if they are isomorphic, false if they are not.
94 bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
95 // Two types with differing kinds are clearly not isomorphic.
96 if (DstTy->getTypeID() != SrcTy->getTypeID()) return false;
98 // If we have an entry in the MappedTypes table, then we have our answer.
99 Type *&Entry = MappedTypes[SrcTy];
101 return Entry == DstTy;
103 // Two identical types are clearly isomorphic. Remember this
104 // non-speculatively.
105 if (DstTy == SrcTy) {
110 // Okay, we have two types with identical kinds that we haven't seen before.
112 // If this is an opaque struct type, special case it.
113 if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
114 // Mapping an opaque type to any struct, just keep the dest struct.
115 if (SSTy->isOpaque()) {
117 SpeculativeTypes.push_back(SrcTy);
121 // Mapping a non-opaque source type to an opaque dest. Keep the dest, but
122 // fill it in later. This doesn't need to be speculative.
123 if (cast<StructType>(DstTy)->isOpaque()) {
125 DefinitionsToResolve.push_back(SSTy);
130 // If the number of subtypes disagree between the two types, then we fail.
131 if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
134 // Fail if any of the extra properties (e.g. array size) of the type disagree.
135 if (isa<IntegerType>(DstTy))
136 return false; // bitwidth disagrees.
137 if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
138 if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
140 } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
141 if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
143 } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
144 StructType *SSTy = cast<StructType>(SrcTy);
145 if (DSTy->isLiteral() != SSTy->isLiteral() ||
146 DSTy->isPacked() != SSTy->isPacked())
148 } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
149 if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
151 } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
152 if (DVTy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
156 // Otherwise, we speculate that these two types will line up and recursively
157 // check the subelements.
159 SpeculativeTypes.push_back(SrcTy);
161 for (unsigned i = 0, e = SrcTy->getNumContainedTypes(); i != e; ++i)
162 if (!areTypesIsomorphic(DstTy->getContainedType(i),
163 SrcTy->getContainedType(i)))
166 // If everything seems to have lined up, then everything is great.
170 /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest
171 /// module from a type definition in the source module.
172 void TypeMapTy::linkDefinedTypeBodies() {
173 SmallVector<Type*, 16> Elements;
174 SmallString<16> TmpName;
176 // Note that processing entries in this loop (calling 'get') can add new
177 // entries to the DefinitionsToResolve vector.
178 while (!DefinitionsToResolve.empty()) {
179 StructType *SrcSTy = DefinitionsToResolve.pop_back_val();
180 StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
182 // TypeMap is a many-to-one mapping, if there were multiple types that
183 // provide a body for DstSTy then previous iterations of this loop may have
184 // already handled it. Just ignore this case.
185 if (!DstSTy->isOpaque()) continue;
186 assert(!SrcSTy->isOpaque() && "Not resolving a definition?");
188 // Map the body of the source type over to a new body for the dest type.
189 Elements.resize(SrcSTy->getNumElements());
190 for (unsigned i = 0, e = Elements.size(); i != e; ++i)
191 Elements[i] = getImpl(SrcSTy->getElementType(i));
193 DstSTy->setBody(Elements, SrcSTy->isPacked());
195 // If DstSTy has no name or has a longer name than STy, then viciously steal
197 if (!SrcSTy->hasName()) continue;
198 StringRef SrcName = SrcSTy->getName();
200 if (!DstSTy->hasName() || DstSTy->getName().size() > SrcName.size()) {
201 TmpName.insert(TmpName.end(), SrcName.begin(), SrcName.end());
203 DstSTy->setName(TmpName.str());
210 /// get - Return the mapped type to use for the specified input type from the
212 Type *TypeMapTy::get(Type *Ty) {
213 Type *Result = getImpl(Ty);
215 // If this caused a reference to any struct type, resolve it before returning.
216 if (!DefinitionsToResolve.empty())
217 linkDefinedTypeBodies();
221 /// getImpl - This is the recursive version of get().
222 Type *TypeMapTy::getImpl(Type *Ty) {
223 // If we already have an entry for this type, return it.
224 Type **Entry = &MappedTypes[Ty];
225 if (*Entry) return *Entry;
227 // If this is not a named struct type, then just map all of the elements and
228 // then rebuild the type from inside out.
229 if (!isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral()) {
230 // If there are no element types to map, then the type is itself. This is
231 // true for the anonymous {} struct, things like 'float', integers, etc.
232 if (Ty->getNumContainedTypes() == 0)
235 // Remap all of the elements, keeping track of whether any of them change.
236 bool AnyChange = false;
237 SmallVector<Type*, 4> ElementTypes;
238 ElementTypes.resize(Ty->getNumContainedTypes());
239 for (unsigned i = 0, e = Ty->getNumContainedTypes(); i != e; ++i) {
240 ElementTypes[i] = getImpl(Ty->getContainedType(i));
241 AnyChange |= ElementTypes[i] != Ty->getContainedType(i);
244 // If we found our type while recursively processing stuff, just use it.
245 Entry = &MappedTypes[Ty];
246 if (*Entry) return *Entry;
248 // If all of the element types mapped directly over, then the type is usable
253 // Otherwise, rebuild a modified type.
254 switch (Ty->getTypeID()) {
255 default: assert(0 && "unknown derived type to remap");
256 case Type::ArrayTyID:
257 return *Entry = ArrayType::get(ElementTypes[0],
258 cast<ArrayType>(Ty)->getNumElements());
259 case Type::VectorTyID:
260 return *Entry = VectorType::get(ElementTypes[0],
261 cast<VectorType>(Ty)->getNumElements());
262 case Type::PointerTyID:
263 return *Entry = PointerType::get(ElementTypes[0],
264 cast<PointerType>(Ty)->getAddressSpace());
265 case Type::FunctionTyID:
266 return *Entry = FunctionType::get(ElementTypes[0],
267 makeArrayRef(ElementTypes).slice(1),
268 cast<FunctionType>(Ty)->isVarArg());
269 case Type::StructTyID:
270 // Note that this is only reached for anonymous structs.
271 return *Entry = StructType::get(Ty->getContext(), ElementTypes,
272 cast<StructType>(Ty)->isPacked());
276 // Otherwise, this is an unmapped named struct. If the struct can be directly
277 // mapped over, just use it as-is. This happens in a case when the linked-in
278 // module has something like:
279 // %T = type {%T*, i32}
280 // @GV = global %T* null
281 // where T does not exist at all in the destination module.
283 // The other case we watch for is when the type is not in the destination
284 // module, but that it has to be rebuilt because it refers to something that
285 // is already mapped. For example, if the destination module has:
287 // and the source module has something like
288 // %A' = type { i32 }
289 // %B = type { %A'* }
290 // @GV = global %B* null
291 // then we want to create a new type: "%B = type { %A*}" and have it take the
292 // pristine "%B" name from the source module.
294 // To determine which case this is, we have to recursively walk the type graph
295 // speculating that we'll be able to reuse it unmodified. Only if this is
296 // safe would we map the entire thing over. Because this is an optimization,
297 // and is not required for the prettiness of the linked module, we just skip
298 // it and always rebuild a type here.
299 StructType *STy = cast<StructType>(Ty);
301 // If the type is opaque, we can just use it directly.
305 // Otherwise we create a new type and resolve its body later. This will be
306 // resolved by the top level of get().
307 DefinitionsToResolve.push_back(STy);
308 return *Entry = StructType::create(STy->getContext());
313 //===----------------------------------------------------------------------===//
314 // ModuleLinker implementation.
315 //===----------------------------------------------------------------------===//
318 /// ModuleLinker - This is an implementation class for the LinkModules
319 /// function, which is the entrypoint for this file.
325 /// ValueMap - Mapping of values from what they used to be in Src, to what
326 /// they are now in DstM. ValueToValueMapTy is a ValueMap, which involves
327 /// some overhead due to the use of Value handles which the Linker doesn't
328 /// actually need, but this allows us to reuse the ValueMapper code.
329 ValueToValueMapTy ValueMap;
331 struct AppendingVarInfo {
332 GlobalVariable *NewGV; // New aggregate global in dest module.
333 Constant *DstInit; // Old initializer from dest module.
334 Constant *SrcInit; // Old initializer from src module.
337 std::vector<AppendingVarInfo> AppendingVars;
339 unsigned Mode; // Mode to treat source module.
341 // Set of items not to link in from source.
342 SmallPtrSet<const Value*, 16> DoNotLinkFromSource;
344 // Vector of functions to lazily link in.
345 std::vector<Function*> LazilyLinkFunctions;
348 std::string ErrorMsg;
350 ModuleLinker(Module *dstM, Module *srcM, unsigned mode)
351 : DstM(dstM), SrcM(srcM), Mode(mode) { }
356 /// emitError - Helper method for setting a message and returning an error
358 bool emitError(const Twine &Message) {
359 ErrorMsg = Message.str();
363 /// getLinkageResult - This analyzes the two global values and determines
364 /// what the result will look like in the destination module.
365 bool getLinkageResult(GlobalValue *Dest, const GlobalValue *Src,
366 GlobalValue::LinkageTypes <, bool &LinkFromSrc);
368 /// getLinkedToGlobal - Given a global in the source module, return the
369 /// global in the destination module that is being linked to, if any.
370 GlobalValue *getLinkedToGlobal(GlobalValue *SrcGV) {
371 // If the source has no name it can't link. If it has local linkage,
372 // there is no name match-up going on.
373 if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
376 // Otherwise see if we have a match in the destination module's symtab.
377 GlobalValue *DGV = DstM->getNamedValue(SrcGV->getName());
378 if (DGV == 0) return 0;
380 // If we found a global with the same name in the dest module, but it has
381 // internal linkage, we are really not doing any linkage here.
382 if (DGV->hasLocalLinkage())
385 // Otherwise, we do in fact link to the destination global.
389 void computeTypeMapping();
391 bool linkAppendingVarProto(GlobalVariable *DstGV, GlobalVariable *SrcGV);
392 bool linkGlobalProto(GlobalVariable *SrcGV);
393 bool linkFunctionProto(Function *SrcF);
394 bool linkAliasProto(GlobalAlias *SrcA);
396 void linkAppendingVarInit(const AppendingVarInfo &AVI);
397 void linkGlobalInits();
398 void linkFunctionBody(Function *Dst, Function *Src);
399 void linkAliasBodies();
400 void linkNamedMDNodes();
406 /// forceRenaming - The LLVM SymbolTable class autorenames globals that conflict
407 /// in the symbol table. This is good for all clients except for us. Go
408 /// through the trouble to force this back.
409 static void forceRenaming(GlobalValue *GV, StringRef Name) {
410 // If the global doesn't force its name or if it already has the right name,
411 // there is nothing for us to do.
412 if (GV->hasLocalLinkage() || GV->getName() == Name)
415 Module *M = GV->getParent();
417 // If there is a conflict, rename the conflict.
418 if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
419 GV->takeName(ConflictGV);
420 ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
421 assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
423 GV->setName(Name); // Force the name back
427 /// CopyGVAttributes - copy additional attributes (those not needed to construct
428 /// a GlobalValue) from the SrcGV to the DestGV.
429 static void CopyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) {
430 // Use the maximum alignment, rather than just copying the alignment of SrcGV.
431 unsigned Alignment = std::max(DestGV->getAlignment(), SrcGV->getAlignment());
432 DestGV->copyAttributesFrom(SrcGV);
433 DestGV->setAlignment(Alignment);
435 forceRenaming(DestGV, SrcGV->getName());
438 /// getLinkageResult - This analyzes the two global values and determines what
439 /// the result will look like in the destination module. In particular, it
440 /// computes the resultant linkage type, computes whether the global in the
441 /// source should be copied over to the destination (replacing the existing
442 /// one), and computes whether this linkage is an error or not. It also performs
443 /// visibility checks: we cannot link together two symbols with different
445 bool ModuleLinker::getLinkageResult(GlobalValue *Dest, const GlobalValue *Src,
446 GlobalValue::LinkageTypes <,
448 assert(Dest && "Must have two globals being queried");
449 assert(!Src->hasLocalLinkage() &&
450 "If Src has internal linkage, Dest shouldn't be set!");
452 bool SrcIsDeclaration = Src->isDeclaration() && !Src->isMaterializable();
453 bool DestIsDeclaration = Dest->isDeclaration();
455 if (SrcIsDeclaration) {
456 // If Src is external or if both Src & Dest are external.. Just link the
457 // external globals, we aren't adding anything.
458 if (Src->hasDLLImportLinkage()) {
459 // If one of GVs has DLLImport linkage, result should be dllimport'ed.
460 if (DestIsDeclaration) {
462 LT = Src->getLinkage();
464 } else if (Dest->hasExternalWeakLinkage()) {
465 // If the Dest is weak, use the source linkage.
467 LT = Src->getLinkage();
470 LT = Dest->getLinkage();
472 } else if (DestIsDeclaration && !Dest->hasDLLImportLinkage()) {
473 // If Dest is external but Src is not:
475 LT = Src->getLinkage();
476 } else if (Src->isWeakForLinker()) {
477 // At this point we know that Dest has LinkOnce, External*, Weak, Common,
479 if (Dest->hasExternalWeakLinkage() ||
480 Dest->hasAvailableExternallyLinkage() ||
481 (Dest->hasLinkOnceLinkage() &&
482 (Src->hasWeakLinkage() || Src->hasCommonLinkage()))) {
484 LT = Src->getLinkage();
487 LT = Dest->getLinkage();
489 } else if (Dest->isWeakForLinker()) {
490 // At this point we know that Src has External* or DLL* linkage.
491 if (Src->hasExternalWeakLinkage()) {
493 LT = Dest->getLinkage();
496 LT = GlobalValue::ExternalLinkage;
499 assert((Dest->hasExternalLinkage() || Dest->hasDLLImportLinkage() ||
500 Dest->hasDLLExportLinkage() || Dest->hasExternalWeakLinkage()) &&
501 (Src->hasExternalLinkage() || Src->hasDLLImportLinkage() ||
502 Src->hasDLLExportLinkage() || Src->hasExternalWeakLinkage()) &&
503 "Unexpected linkage type!");
504 return emitError("Linking globals named '" + Src->getName() +
505 "': symbol multiply defined!");
509 if (Src->getVisibility() != Dest->getVisibility() &&
510 !SrcIsDeclaration && !DestIsDeclaration &&
511 !Src->hasAvailableExternallyLinkage() &&
512 !Dest->hasAvailableExternallyLinkage())
513 return emitError("Linking globals named '" + Src->getName() +
514 "': symbols have different visibilities!");
518 /// computeTypeMapping - Loop over all of the linked values to compute type
519 /// mappings. For example, if we link "extern Foo *x" and "Foo *x = NULL", then
520 /// we have two struct types 'Foo' but one got renamed when the module was
521 /// loaded into the same LLVMContext.
522 void ModuleLinker::computeTypeMapping() {
523 // Incorporate globals.
524 for (Module::global_iterator I = SrcM->global_begin(),
525 E = SrcM->global_end(); I != E; ++I) {
526 GlobalValue *DGV = getLinkedToGlobal(I);
527 if (DGV == 0) continue;
529 if (!DGV->hasAppendingLinkage() || !I->hasAppendingLinkage()) {
530 TypeMap.addTypeMapping(DGV->getType(), I->getType());
534 // Unify the element type of appending arrays.
535 ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType());
536 ArrayType *SAT = cast<ArrayType>(I->getType()->getElementType());
537 TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
540 // Incorporate functions.
541 for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I) {
542 if (GlobalValue *DGV = getLinkedToGlobal(I))
543 TypeMap.addTypeMapping(DGV->getType(), I->getType());
546 // Don't bother incorporating aliases, they aren't generally typed well.
548 // Now that we have discovered all of the type equivalences, get a body for
549 // any 'opaque' types in the dest module that are now resolved.
550 TypeMap.linkDefinedTypeBodies();
553 /// linkAppendingVarProto - If there were any appending global variables, link
554 /// them together now. Return true on error.
555 bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV,
556 GlobalVariable *SrcGV) {
558 if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
559 return emitError("Linking globals named '" + SrcGV->getName() +
560 "': can only link appending global with another appending global!");
562 ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
564 cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()));
565 Type *EltTy = DstTy->getElementType();
567 // Check to see that they two arrays agree on type.
568 if (EltTy != SrcTy->getElementType())
569 return emitError("Appending variables with different element types!");
570 if (DstGV->isConstant() != SrcGV->isConstant())
571 return emitError("Appending variables linked with different const'ness!");
573 if (DstGV->getAlignment() != SrcGV->getAlignment())
575 "Appending variables with different alignment need to be linked!");
577 if (DstGV->getVisibility() != SrcGV->getVisibility())
579 "Appending variables with different visibility need to be linked!");
581 if (DstGV->getSection() != SrcGV->getSection())
583 "Appending variables with different section name need to be linked!");
585 uint64_t NewSize = DstTy->getNumElements() + SrcTy->getNumElements();
586 ArrayType *NewType = ArrayType::get(EltTy, NewSize);
588 // Create the new global variable.
590 new GlobalVariable(*DstGV->getParent(), NewType, SrcGV->isConstant(),
591 DstGV->getLinkage(), /*init*/0, /*name*/"", DstGV,
592 DstGV->isThreadLocal(),
593 DstGV->getType()->getAddressSpace());
595 // Propagate alignment, visibility and section info.
596 CopyGVAttributes(NG, DstGV);
598 AppendingVarInfo AVI;
600 AVI.DstInit = DstGV->getInitializer();
601 AVI.SrcInit = SrcGV->getInitializer();
602 AppendingVars.push_back(AVI);
604 // Replace any uses of the two global variables with uses of the new
606 ValueMap[SrcGV] = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
608 DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
609 DstGV->eraseFromParent();
611 // Track the source variable so we don't try to link it.
612 DoNotLinkFromSource.insert(SrcGV);
617 /// linkGlobalProto - Loop through the global variables in the src module and
618 /// merge them into the dest module.
619 bool ModuleLinker::linkGlobalProto(GlobalVariable *SGV) {
620 GlobalValue *DGV = getLinkedToGlobal(SGV);
623 // Concatenation of appending linkage variables is magic and handled later.
624 if (DGV->hasAppendingLinkage() || SGV->hasAppendingLinkage())
625 return linkAppendingVarProto(cast<GlobalVariable>(DGV), SGV);
627 // Determine whether linkage of these two globals follows the source
628 // module's definition or the destination module's definition.
629 GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
630 bool LinkFromSrc = false;
631 if (getLinkageResult(DGV, SGV, NewLinkage, LinkFromSrc))
634 // If we're not linking from the source, then keep the definition that we
637 // Special case for const propagation.
638 if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV))
639 if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant())
640 DGVar->setConstant(true);
642 // Set calculated linkage.
643 DGV->setLinkage(NewLinkage);
645 // Make sure to remember this mapping.
646 ValueMap[SGV] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGV->getType()));
648 // Track the source global so that we don't attempt to copy it over when
649 // processing global initializers.
650 DoNotLinkFromSource.insert(SGV);
656 // No linking to be performed or linking from the source: simply create an
657 // identical version of the symbol over in the dest module... the
658 // initializer will be filled in later by LinkGlobalInits.
659 GlobalVariable *NewDGV =
660 new GlobalVariable(*DstM, TypeMap.get(SGV->getType()->getElementType()),
661 SGV->isConstant(), SGV->getLinkage(), /*init*/0,
662 SGV->getName(), /*insertbefore*/0,
663 SGV->isThreadLocal(),
664 SGV->getType()->getAddressSpace());
665 // Propagate alignment, visibility and section info.
666 CopyGVAttributes(NewDGV, SGV);
669 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV, DGV->getType()));
670 DGV->eraseFromParent();
673 // Make sure to remember this mapping.
674 ValueMap[SGV] = NewDGV;
678 /// linkFunctionProto - Link the function in the source module into the
679 /// destination module if needed, setting up mapping information.
680 bool ModuleLinker::linkFunctionProto(Function *SF) {
681 GlobalValue *DGV = getLinkedToGlobal(SF);
684 GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
685 bool LinkFromSrc = false;
686 if (getLinkageResult(DGV, SF, NewLinkage, LinkFromSrc))
690 // Set calculated linkage
691 DGV->setLinkage(NewLinkage);
693 // Make sure to remember this mapping.
694 ValueMap[SF] = ConstantExpr::getBitCast(DGV, TypeMap.get(SF->getType()));
696 // Track the function from the source module so we don't attempt to remap
698 DoNotLinkFromSource.insert(SF);
704 // If there is no linkage to be performed or we are linking from the source,
706 Function *NewDF = Function::Create(TypeMap.get(SF->getFunctionType()),
707 SF->getLinkage(), SF->getName(), DstM);
708 CopyGVAttributes(NewDF, SF);
711 // Any uses of DF need to change to NewDF, with cast.
712 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType()));
713 DGV->eraseFromParent();
715 // Internal, LO_ODR, or LO linkage - stick in set to ignore and lazily link.
716 if (SF->hasLocalLinkage() || SF->hasLinkOnceLinkage() ||
717 SF->hasAvailableExternallyLinkage()) {
718 DoNotLinkFromSource.insert(SF);
719 LazilyLinkFunctions.push_back(SF);
723 ValueMap[SF] = NewDF;
727 /// LinkAliasProto - Set up prototypes for any aliases that come over from the
729 bool ModuleLinker::linkAliasProto(GlobalAlias *SGA) {
730 GlobalValue *DGV = getLinkedToGlobal(SGA);
733 GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
734 bool LinkFromSrc = false;
735 if (getLinkageResult(DGV, SGA, NewLinkage, LinkFromSrc))
739 // Set calculated linkage.
740 DGV->setLinkage(NewLinkage);
742 // Make sure to remember this mapping.
743 ValueMap[SGA] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGA->getType()));
745 // Track the alias from the source module so we don't attempt to remap it.
746 DoNotLinkFromSource.insert(SGA);
752 // If there is no linkage to be performed or we're linking from the source,
754 GlobalAlias *NewDA = new GlobalAlias(TypeMap.get(SGA->getType()),
755 SGA->getLinkage(), SGA->getName(),
757 CopyGVAttributes(NewDA, SGA);
760 // Any uses of DGV need to change to NewDA, with cast.
761 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDA, DGV->getType()));
762 DGV->eraseFromParent();
765 ValueMap[SGA] = NewDA;
769 void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) {
770 // Merge the initializer.
771 SmallVector<Constant*, 16> Elements;
772 if (ConstantArray *I = dyn_cast<ConstantArray>(AVI.DstInit)) {
773 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
774 Elements.push_back(I->getOperand(i));
776 assert(isa<ConstantAggregateZero>(AVI.DstInit));
777 ArrayType *DstAT = cast<ArrayType>(AVI.DstInit->getType());
778 Type *EltTy = DstAT->getElementType();
779 Elements.append(DstAT->getNumElements(), Constant::getNullValue(EltTy));
782 Constant *SrcInit = MapValue(AVI.SrcInit, ValueMap, RF_None, &TypeMap);
783 if (const ConstantArray *I = dyn_cast<ConstantArray>(SrcInit)) {
784 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
785 Elements.push_back(I->getOperand(i));
787 assert(isa<ConstantAggregateZero>(SrcInit));
788 ArrayType *SrcAT = cast<ArrayType>(SrcInit->getType());
789 Type *EltTy = SrcAT->getElementType();
790 Elements.append(SrcAT->getNumElements(), Constant::getNullValue(EltTy));
792 ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType());
793 AVI.NewGV->setInitializer(ConstantArray::get(NewType, Elements));
797 // linkGlobalInits - Update the initializers in the Dest module now that all
798 // globals that may be referenced are in Dest.
799 void ModuleLinker::linkGlobalInits() {
800 // Loop over all of the globals in the src module, mapping them over as we go
801 for (Module::const_global_iterator I = SrcM->global_begin(),
802 E = SrcM->global_end(); I != E; ++I) {
804 // Only process initialized GV's or ones not already in dest.
805 if (!I->hasInitializer() || DoNotLinkFromSource.count(I)) continue;
807 // Grab destination global variable.
808 GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[I]);
809 // Figure out what the initializer looks like in the dest module.
810 DGV->setInitializer(MapValue(I->getInitializer(), ValueMap,
815 // linkFunctionBody - Copy the source function over into the dest function and
816 // fix up references to values. At this point we know that Dest is an external
817 // function, and that Src is not.
818 void ModuleLinker::linkFunctionBody(Function *Dst, Function *Src) {
819 assert(Src && Dst && Dst->isDeclaration() && !Src->isDeclaration());
821 // Go through and convert function arguments over, remembering the mapping.
822 Function::arg_iterator DI = Dst->arg_begin();
823 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
825 DI->setName(I->getName()); // Copy the name over.
827 // Add a mapping to our mapping.
831 if (Mode == Linker::DestroySource) {
832 // Splice the body of the source function into the dest function.
833 Dst->getBasicBlockList().splice(Dst->end(), Src->getBasicBlockList());
835 // At this point, all of the instructions and values of the function are now
836 // copied over. The only problem is that they are still referencing values in
837 // the Source function as operands. Loop through all of the operands of the
838 // functions and patch them up to point to the local versions.
839 for (Function::iterator BB = Dst->begin(), BE = Dst->end(); BB != BE; ++BB)
840 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
841 RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries, &TypeMap);
844 // Clone the body of the function into the dest function.
845 SmallVector<ReturnInst*, 8> Returns; // Ignore returns.
846 CloneFunctionInto(Dst, Src, ValueMap, false, Returns);
849 // There is no need to map the arguments anymore.
850 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
857 void ModuleLinker::linkAliasBodies() {
858 for (Module::alias_iterator I = SrcM->alias_begin(), E = SrcM->alias_end();
860 if (DoNotLinkFromSource.count(I))
862 if (Constant *Aliasee = I->getAliasee()) {
863 GlobalAlias *DA = cast<GlobalAlias>(ValueMap[I]);
864 DA->setAliasee(MapValue(Aliasee, ValueMap, RF_None, &TypeMap));
869 /// linkNamedMDNodes - Insert all of the named mdnodes in Src into the Dest
871 void ModuleLinker::linkNamedMDNodes() {
872 for (Module::const_named_metadata_iterator I = SrcM->named_metadata_begin(),
873 E = SrcM->named_metadata_end(); I != E; ++I) {
874 NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(I->getName());
875 // Add Src elements into Dest node.
876 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
877 DestNMD->addOperand(MapValue(I->getOperand(i), ValueMap,
882 bool ModuleLinker::run() {
883 assert(DstM && "Null Destination module");
884 assert(SrcM && "Null Source Module");
886 // Inherit the target data from the source module if the destination module
887 // doesn't have one already.
888 if (DstM->getDataLayout().empty() && !SrcM->getDataLayout().empty())
889 DstM->setDataLayout(SrcM->getDataLayout());
891 // Copy the target triple from the source to dest if the dest's is empty.
892 if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
893 DstM->setTargetTriple(SrcM->getTargetTriple());
895 if (!SrcM->getDataLayout().empty() && !DstM->getDataLayout().empty() &&
896 SrcM->getDataLayout() != DstM->getDataLayout())
897 errs() << "WARNING: Linking two modules of different data layouts!\n";
898 if (!SrcM->getTargetTriple().empty() &&
899 DstM->getTargetTriple() != SrcM->getTargetTriple()) {
900 errs() << "WARNING: Linking two modules of different target triples: ";
901 if (!SrcM->getModuleIdentifier().empty())
902 errs() << SrcM->getModuleIdentifier() << ": ";
903 errs() << "'" << SrcM->getTargetTriple() << "' and '"
904 << DstM->getTargetTriple() << "'\n";
907 // Append the module inline asm string.
908 if (!SrcM->getModuleInlineAsm().empty()) {
909 if (DstM->getModuleInlineAsm().empty())
910 DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm());
912 DstM->setModuleInlineAsm(DstM->getModuleInlineAsm()+"\n"+
913 SrcM->getModuleInlineAsm());
916 // Update the destination module's dependent libraries list with the libraries
917 // from the source module. There's no opportunity for duplicates here as the
918 // Module ensures that duplicate insertions are discarded.
919 for (Module::lib_iterator SI = SrcM->lib_begin(), SE = SrcM->lib_end();
921 DstM->addLibrary(*SI);
923 // If the source library's module id is in the dependent library list of the
924 // destination library, remove it since that module is now linked in.
925 StringRef ModuleId = SrcM->getModuleIdentifier();
926 if (!ModuleId.empty())
927 DstM->removeLibrary(sys::path::stem(ModuleId));
929 // Loop over all of the linked values to compute type mappings.
930 computeTypeMapping();
932 // Insert all of the globals in src into the DstM module... without linking
933 // initializers (which could refer to functions not yet mapped over).
934 for (Module::global_iterator I = SrcM->global_begin(),
935 E = SrcM->global_end(); I != E; ++I)
936 if (linkGlobalProto(I))
939 // Link the functions together between the two modules, without doing function
940 // bodies... this just adds external function prototypes to the DstM
941 // function... We do this so that when we begin processing function bodies,
942 // all of the global values that may be referenced are available in our
944 for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I)
945 if (linkFunctionProto(I))
948 // If there were any aliases, link them now.
949 for (Module::alias_iterator I = SrcM->alias_begin(),
950 E = SrcM->alias_end(); I != E; ++I)
951 if (linkAliasProto(I))
954 for (unsigned i = 0, e = AppendingVars.size(); i != e; ++i)
955 linkAppendingVarInit(AppendingVars[i]);
957 // Update the initializers in the DstM module now that all globals that may
958 // be referenced are in DstM.
961 // Link in the function bodies that are defined in the source module into
963 for (Module::iterator SF = SrcM->begin(), E = SrcM->end(); SF != E; ++SF) {
965 // Skip if not linking from source.
966 if (DoNotLinkFromSource.count(SF)) continue;
968 // Skip if no body (function is external) or materialize.
969 if (SF->isDeclaration()) {
970 if (!SF->isMaterializable())
972 if (SF->Materialize(&ErrorMsg))
976 linkFunctionBody(cast<Function>(ValueMap[SF]), SF);
979 // Resolve all uses of aliases with aliasees.
982 // Remap all of the named mdnoes in Src into the DstM module. We do this
983 // after linking GlobalValues so that MDNodes that reference GlobalValues
984 // are properly remapped.
987 // Process vector of lazily linked in functions.
988 bool LinkedInAnyFunctions;
990 LinkedInAnyFunctions = false;
992 for(std::vector<Function*>::iterator I = LazilyLinkFunctions.begin(),
993 E = LazilyLinkFunctions.end(); I != E; ++I) {
998 Function *DF = cast<Function>(ValueMap[SF]);
1000 if (!DF->use_empty()) {
1002 // Materialize if necessary.
1003 if (SF->isDeclaration()) {
1004 if (!SF->isMaterializable())
1006 if (SF->Materialize(&ErrorMsg))
1010 // Link in function body.
1011 linkFunctionBody(DF, SF);
1013 // "Remove" from vector by setting the element to 0.
1016 // Set flag to indicate we may have more functions to lazily link in
1017 // since we linked in a function.
1018 LinkedInAnyFunctions = true;
1021 } while (LinkedInAnyFunctions);
1023 // Remove any prototypes of functions that were not actually linked in.
1024 for(std::vector<Function*>::iterator I = LazilyLinkFunctions.begin(),
1025 E = LazilyLinkFunctions.end(); I != E; ++I) {
1030 Function *DF = cast<Function>(ValueMap[SF]);
1031 if (DF->use_empty())
1032 DF->eraseFromParent();
1035 // Now that all of the types from the source are used, resolve any structs
1036 // copied over to the dest that didn't exist there.
1037 TypeMap.linkDefinedTypeBodies();
1042 //===----------------------------------------------------------------------===//
1043 // LinkModules entrypoint.
1044 //===----------------------------------------------------------------------===//
1046 // LinkModules - This function links two modules together, with the resulting
1047 // left module modified to be the composite of the two input modules. If an
1048 // error occurs, true is returned and ErrorMsg (if not null) is set to indicate
1049 // the problem. Upon failure, the Dest module could be in a modified state, and
1050 // shouldn't be relied on to be consistent.
1051 bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Mode,
1052 std::string *ErrorMsg) {
1053 ModuleLinker TheLinker(Dest, Src, Mode);
1054 if (TheLinker.run()) {
1055 if (ErrorMsg) *ErrorMsg = TheLinker.ErrorMsg;