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 // Specifically, this:
13 // * Merges global variables between the two modules
14 // * Uninit + Uninit = Init, Init + Uninit = Init, Init + Init = Error if !=
15 // * Merges functions between two modules
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Linker.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/LLVMContext.h"
23 #include "llvm/Module.h"
24 #include "llvm/TypeSymbolTable.h"
25 #include "llvm/ValueSymbolTable.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/Assembly/Writer.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/System/Path.h"
32 #include "llvm/Transforms/Utils/ValueMapper.h"
33 #include "llvm/ADT/DenseMap.h"
36 // Error - Simple wrapper function to conditionally assign to E and return true.
37 // This just makes error return conditions a little bit simpler...
38 static inline bool Error(std::string *E, const Twine &Message) {
39 if (E) *E = Message.str();
43 // Function: ResolveTypes()
46 // Attempt to link the two specified types together.
49 // DestTy - The type to which we wish to resolve.
50 // SrcTy - The original type which we want to resolve.
53 // DestST - The symbol table in which the new type should be placed.
56 // true - There is an error and the types cannot yet be linked.
59 static bool ResolveTypes(const Type *DestTy, const Type *SrcTy) {
60 if (DestTy == SrcTy) return false; // If already equal, noop
61 assert(DestTy && SrcTy && "Can't handle null types");
63 if (const OpaqueType *OT = dyn_cast<OpaqueType>(DestTy)) {
64 // Type _is_ in module, just opaque...
65 const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(SrcTy);
66 } else if (const OpaqueType *OT = dyn_cast<OpaqueType>(SrcTy)) {
67 const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(DestTy);
69 return true; // Cannot link types... not-equal and neither is opaque.
74 /// LinkerTypeMap - This implements a map of types that is stable
75 /// even if types are resolved/refined to other types. This is not a general
76 /// purpose map, it is specific to the linker's use.
78 class LinkerTypeMap : public AbstractTypeUser {
79 typedef DenseMap<const Type*, PATypeHolder> TheMapTy;
82 LinkerTypeMap(const LinkerTypeMap&); // DO NOT IMPLEMENT
83 void operator=(const LinkerTypeMap&); // DO NOT IMPLEMENT
87 for (DenseMap<const Type*, PATypeHolder>::iterator I = TheMap.begin(),
88 E = TheMap.end(); I != E; ++I)
89 I->first->removeAbstractTypeUser(this);
92 /// lookup - Return the value for the specified type or null if it doesn't
94 const Type *lookup(const Type *Ty) const {
95 TheMapTy::const_iterator I = TheMap.find(Ty);
96 if (I != TheMap.end()) return I->second;
100 /// erase - Remove the specified type, returning true if it was in the set.
101 bool erase(const Type *Ty) {
102 if (!TheMap.erase(Ty))
104 if (Ty->isAbstract())
105 Ty->removeAbstractTypeUser(this);
109 /// insert - This returns true if the pointer was new to the set, false if it
110 /// was already in the set.
111 bool insert(const Type *Src, const Type *Dst) {
112 if (!TheMap.insert(std::make_pair(Src, PATypeHolder(Dst))).second)
113 return false; // Already in map.
114 if (Src->isAbstract())
115 Src->addAbstractTypeUser(this);
120 /// refineAbstractType - The callback method invoked when an abstract type is
121 /// resolved to another type. An object must override this method to update
122 /// its internal state to reference NewType instead of OldType.
124 virtual void refineAbstractType(const DerivedType *OldTy,
126 TheMapTy::iterator I = TheMap.find(OldTy);
127 const Type *DstTy = I->second;
130 if (OldTy->isAbstract())
131 OldTy->removeAbstractTypeUser(this);
133 // Don't reinsert into the map if the key is concrete now.
134 if (NewTy->isAbstract())
135 insert(NewTy, DstTy);
138 /// The other case which AbstractTypeUsers must be aware of is when a type
139 /// makes the transition from being abstract (where it has clients on it's
140 /// AbstractTypeUsers list) to concrete (where it does not). This method
141 /// notifies ATU's when this occurs for a type.
142 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
144 AbsTy->removeAbstractTypeUser(this);
148 virtual void dump() const {
149 dbgs() << "AbstractTypeSet!\n";
155 // RecursiveResolveTypes - This is just like ResolveTypes, except that it
156 // recurses down into derived types, merging the used types if the parent types
158 static bool RecursiveResolveTypesI(const Type *DstTy, const Type *SrcTy,
159 LinkerTypeMap &Pointers) {
160 if (DstTy == SrcTy) return false; // If already equal, noop
162 // If we found our opaque type, resolve it now!
163 if (DstTy->isOpaqueTy() || SrcTy->isOpaqueTy())
164 return ResolveTypes(DstTy, SrcTy);
166 // Two types cannot be resolved together if they are of different primitive
167 // type. For example, we cannot resolve an int to a float.
168 if (DstTy->getTypeID() != SrcTy->getTypeID()) return true;
170 // If neither type is abstract, then they really are just different types.
171 if (!DstTy->isAbstract() && !SrcTy->isAbstract())
174 // Otherwise, resolve the used type used by this derived type...
175 switch (DstTy->getTypeID()) {
178 case Type::FunctionTyID: {
179 const FunctionType *DstFT = cast<FunctionType>(DstTy);
180 const FunctionType *SrcFT = cast<FunctionType>(SrcTy);
181 if (DstFT->isVarArg() != SrcFT->isVarArg() ||
182 DstFT->getNumContainedTypes() != SrcFT->getNumContainedTypes())
185 // Use TypeHolder's so recursive resolution won't break us.
186 PATypeHolder ST(SrcFT), DT(DstFT);
187 for (unsigned i = 0, e = DstFT->getNumContainedTypes(); i != e; ++i) {
188 const Type *SE = ST->getContainedType(i), *DE = DT->getContainedType(i);
189 if (SE != DE && RecursiveResolveTypesI(DE, SE, Pointers))
194 case Type::StructTyID: {
195 const StructType *DstST = cast<StructType>(DstTy);
196 const StructType *SrcST = cast<StructType>(SrcTy);
197 if (DstST->getNumContainedTypes() != SrcST->getNumContainedTypes())
200 PATypeHolder ST(SrcST), DT(DstST);
201 for (unsigned i = 0, e = DstST->getNumContainedTypes(); i != e; ++i) {
202 const Type *SE = ST->getContainedType(i), *DE = DT->getContainedType(i);
203 if (SE != DE && RecursiveResolveTypesI(DE, SE, Pointers))
208 case Type::ArrayTyID: {
209 const ArrayType *DAT = cast<ArrayType>(DstTy);
210 const ArrayType *SAT = cast<ArrayType>(SrcTy);
211 if (DAT->getNumElements() != SAT->getNumElements()) return true;
212 return RecursiveResolveTypesI(DAT->getElementType(), SAT->getElementType(),
215 case Type::VectorTyID: {
216 const VectorType *DVT = cast<VectorType>(DstTy);
217 const VectorType *SVT = cast<VectorType>(SrcTy);
218 if (DVT->getNumElements() != SVT->getNumElements()) return true;
219 return RecursiveResolveTypesI(DVT->getElementType(), SVT->getElementType(),
222 case Type::PointerTyID: {
223 const PointerType *DstPT = cast<PointerType>(DstTy);
224 const PointerType *SrcPT = cast<PointerType>(SrcTy);
226 if (DstPT->getAddressSpace() != SrcPT->getAddressSpace())
229 // If this is a pointer type, check to see if we have already seen it. If
230 // so, we are in a recursive branch. Cut off the search now. We cannot use
231 // an associative container for this search, because the type pointers (keys
232 // in the container) change whenever types get resolved.
233 if (SrcPT->isAbstract())
234 if (const Type *ExistingDestTy = Pointers.lookup(SrcPT))
235 return ExistingDestTy != DstPT;
237 if (DstPT->isAbstract())
238 if (const Type *ExistingSrcTy = Pointers.lookup(DstPT))
239 return ExistingSrcTy != SrcPT;
240 // Otherwise, add the current pointers to the vector to stop recursion on
242 if (DstPT->isAbstract())
243 Pointers.insert(DstPT, SrcPT);
244 if (SrcPT->isAbstract())
245 Pointers.insert(SrcPT, DstPT);
247 return RecursiveResolveTypesI(DstPT->getElementType(),
248 SrcPT->getElementType(), Pointers);
253 static bool RecursiveResolveTypes(const Type *DestTy, const Type *SrcTy) {
254 LinkerTypeMap PointerTypes;
255 return RecursiveResolveTypesI(DestTy, SrcTy, PointerTypes);
259 // LinkTypes - Go through the symbol table of the Src module and see if any
260 // types are named in the src module that are not named in the Dst module.
261 // Make sure there are no type name conflicts.
262 static bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) {
263 TypeSymbolTable *DestST = &Dest->getTypeSymbolTable();
264 const TypeSymbolTable *SrcST = &Src->getTypeSymbolTable();
266 // Look for a type plane for Type's...
267 TypeSymbolTable::const_iterator TI = SrcST->begin();
268 TypeSymbolTable::const_iterator TE = SrcST->end();
269 if (TI == TE) return false; // No named types, do nothing.
271 // Some types cannot be resolved immediately because they depend on other
272 // types being resolved to each other first. This contains a list of types we
273 // are waiting to recheck.
274 std::vector<std::string> DelayedTypesToResolve;
276 for ( ; TI != TE; ++TI ) {
277 const std::string &Name = TI->first;
278 const Type *RHS = TI->second;
280 // Check to see if this type name is already in the dest module.
281 Type *Entry = DestST->lookup(Name);
283 // If the name is just in the source module, bring it over to the dest.
286 DestST->insert(Name, const_cast<Type*>(RHS));
287 } else if (ResolveTypes(Entry, RHS)) {
288 // They look different, save the types 'till later to resolve.
289 DelayedTypesToResolve.push_back(Name);
293 // Iteratively resolve types while we can...
294 while (!DelayedTypesToResolve.empty()) {
295 // Loop over all of the types, attempting to resolve them if possible...
296 unsigned OldSize = DelayedTypesToResolve.size();
298 // Try direct resolution by name...
299 for (unsigned i = 0; i != DelayedTypesToResolve.size(); ++i) {
300 const std::string &Name = DelayedTypesToResolve[i];
301 Type *T1 = SrcST->lookup(Name);
302 Type *T2 = DestST->lookup(Name);
303 if (!ResolveTypes(T2, T1)) {
304 // We are making progress!
305 DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i);
310 // Did we not eliminate any types?
311 if (DelayedTypesToResolve.size() == OldSize) {
312 // Attempt to resolve subelements of types. This allows us to merge these
313 // two types: { int* } and { opaque* }
314 for (unsigned i = 0, e = DelayedTypesToResolve.size(); i != e; ++i) {
315 const std::string &Name = DelayedTypesToResolve[i];
316 if (!RecursiveResolveTypes(SrcST->lookup(Name), DestST->lookup(Name))) {
317 // We are making progress!
318 DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i);
320 // Go back to the main loop, perhaps we can resolve directly by name
326 // If we STILL cannot resolve the types, then there is something wrong.
327 if (DelayedTypesToResolve.size() == OldSize) {
328 // Remove the symbol name from the destination.
329 DelayedTypesToResolve.pop_back();
338 /// ForceRenaming - The LLVM SymbolTable class autorenames globals that conflict
339 /// in the symbol table. This is good for all clients except for us. Go
340 /// through the trouble to force this back.
341 static void ForceRenaming(GlobalValue *GV, const std::string &Name) {
342 assert(GV->getName() != Name && "Can't force rename to self");
343 ValueSymbolTable &ST = GV->getParent()->getValueSymbolTable();
345 // If there is a conflict, rename the conflict.
346 if (GlobalValue *ConflictGV = cast_or_null<GlobalValue>(ST.lookup(Name))) {
347 assert(ConflictGV->hasLocalLinkage() &&
348 "Not conflicting with a static global, should link instead!");
349 GV->takeName(ConflictGV);
350 ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
351 assert(ConflictGV->getName() != Name && "ForceRenaming didn't work");
353 GV->setName(Name); // Force the name back
357 /// CopyGVAttributes - copy additional attributes (those not needed to construct
358 /// a GlobalValue) from the SrcGV to the DestGV.
359 static void CopyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) {
360 // Use the maximum alignment, rather than just copying the alignment of SrcGV.
361 unsigned Alignment = std::max(DestGV->getAlignment(), SrcGV->getAlignment());
362 DestGV->copyAttributesFrom(SrcGV);
363 DestGV->setAlignment(Alignment);
366 /// GetLinkageResult - This analyzes the two global values and determines what
367 /// the result will look like in the destination module. In particular, it
368 /// computes the resultant linkage type, computes whether the global in the
369 /// source should be copied over to the destination (replacing the existing
370 /// one), and computes whether this linkage is an error or not. It also performs
371 /// visibility checks: we cannot link together two symbols with different
373 static bool GetLinkageResult(GlobalValue *Dest, const GlobalValue *Src,
374 GlobalValue::LinkageTypes <, bool &LinkFromSrc,
376 assert((!Dest || !Src->hasLocalLinkage()) &&
377 "If Src has internal linkage, Dest shouldn't be set!");
379 // Linking something to nothing.
381 LT = Src->getLinkage();
382 } else if (Src->isDeclaration()) {
383 // If Src is external or if both Src & Dest are external.. Just link the
384 // external globals, we aren't adding anything.
385 if (Src->hasDLLImportLinkage()) {
386 // If one of GVs has DLLImport linkage, result should be dllimport'ed.
387 if (Dest->isDeclaration()) {
389 LT = Src->getLinkage();
391 } else if (Dest->hasExternalWeakLinkage()) {
392 // If the Dest is weak, use the source linkage.
394 LT = Src->getLinkage();
397 LT = Dest->getLinkage();
399 } else if (Dest->isDeclaration() && !Dest->hasDLLImportLinkage()) {
400 // If Dest is external but Src is not:
402 LT = Src->getLinkage();
403 } else if (Src->hasAppendingLinkage() || Dest->hasAppendingLinkage()) {
404 if (Src->getLinkage() != Dest->getLinkage())
405 return Error(Err, "Linking globals named '" + Src->getName() +
406 "': can only link appending global with another appending global!");
407 LinkFromSrc = true; // Special cased.
408 LT = Src->getLinkage();
409 } else if (Src->isWeakForLinker()) {
410 // At this point we know that Dest has LinkOnce, External*, Weak, Common,
412 if (Dest->hasExternalWeakLinkage() ||
413 Dest->hasAvailableExternallyLinkage() ||
414 (Dest->hasLinkOnceLinkage() &&
415 (Src->hasWeakLinkage() || Src->hasCommonLinkage()))) {
417 LT = Src->getLinkage();
420 LT = Dest->getLinkage();
422 } else if (Dest->isWeakForLinker()) {
423 // At this point we know that Src has External* or DLL* linkage.
424 if (Src->hasExternalWeakLinkage()) {
426 LT = Dest->getLinkage();
429 LT = GlobalValue::ExternalLinkage;
432 assert((Dest->hasExternalLinkage() ||
433 Dest->hasDLLImportLinkage() ||
434 Dest->hasDLLExportLinkage() ||
435 Dest->hasExternalWeakLinkage()) &&
436 (Src->hasExternalLinkage() ||
437 Src->hasDLLImportLinkage() ||
438 Src->hasDLLExportLinkage() ||
439 Src->hasExternalWeakLinkage()) &&
440 "Unexpected linkage type!");
441 return Error(Err, "Linking globals named '" + Src->getName() +
442 "': symbol multiply defined!");
446 if (Dest && Src->getVisibility() != Dest->getVisibility())
447 if (!Src->isDeclaration() && !Dest->isDeclaration())
448 return Error(Err, "Linking globals named '" + Src->getName() +
449 "': symbols have different visibilities!");
453 // Insert all of the named mdnoes in Src into the Dest module.
454 static void LinkNamedMDNodes(Module *Dest, Module *Src,
455 ValueToValueMapTy &ValueMap) {
456 for (Module::const_named_metadata_iterator I = Src->named_metadata_begin(),
457 E = Src->named_metadata_end(); I != E; ++I) {
458 const NamedMDNode *SrcNMD = I;
459 NamedMDNode *DestNMD = Dest->getOrInsertNamedMetadata(SrcNMD->getName());
460 // Add Src elements into Dest node.
461 for (unsigned i = 0, e = SrcNMD->getNumOperands(); i != e; ++i)
462 DestNMD->addOperand(cast<MDNode>(MapValue(SrcNMD->getOperand(i),
467 // LinkGlobals - Loop through the global variables in the src module and merge
468 // them into the dest module.
469 static bool LinkGlobals(Module *Dest, const Module *Src,
470 ValueToValueMapTy &ValueMap,
471 std::multimap<std::string, GlobalVariable *> &AppendingVars,
473 ValueSymbolTable &DestSymTab = Dest->getValueSymbolTable();
475 // Loop over all of the globals in the src module, mapping them over as we go
476 for (Module::const_global_iterator I = Src->global_begin(),
477 E = Src->global_end(); I != E; ++I) {
478 const GlobalVariable *SGV = I;
479 GlobalValue *DGV = 0;
481 // Check to see if may have to link the global with the global, alias or
483 if (SGV->hasName() && !SGV->hasLocalLinkage())
484 DGV = cast_or_null<GlobalValue>(DestSymTab.lookup(SGV->getName()));
486 // If we found a global with the same name in the dest module, but it has
487 // internal linkage, we are really not doing any linkage here.
488 if (DGV && DGV->hasLocalLinkage())
491 // If types don't agree due to opaque types, try to resolve them.
492 if (DGV && DGV->getType() != SGV->getType())
493 RecursiveResolveTypes(SGV->getType(), DGV->getType());
495 assert((SGV->hasInitializer() || SGV->hasExternalWeakLinkage() ||
496 SGV->hasExternalLinkage() || SGV->hasDLLImportLinkage()) &&
497 "Global must either be external or have an initializer!");
499 GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
500 bool LinkFromSrc = false;
501 if (GetLinkageResult(DGV, SGV, NewLinkage, LinkFromSrc, Err))
505 // No linking to be performed, simply create an identical version of the
506 // symbol over in the dest module... the initializer will be filled in
507 // later by LinkGlobalInits.
508 GlobalVariable *NewDGV =
509 new GlobalVariable(*Dest, SGV->getType()->getElementType(),
510 SGV->isConstant(), SGV->getLinkage(), /*init*/0,
511 SGV->getName(), 0, false,
512 SGV->getType()->getAddressSpace());
513 // Propagate alignment, visibility and section info.
514 CopyGVAttributes(NewDGV, SGV);
516 // If the LLVM runtime renamed the global, but it is an externally visible
517 // symbol, DGV must be an existing global with internal linkage. Rename
519 if (!NewDGV->hasLocalLinkage() && NewDGV->getName() != SGV->getName())
520 ForceRenaming(NewDGV, SGV->getName());
522 // Make sure to remember this mapping.
523 ValueMap[SGV] = NewDGV;
525 // Keep track that this is an appending variable.
526 if (SGV->hasAppendingLinkage())
527 AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
531 // If the visibilities of the symbols disagree and the destination is a
532 // prototype, take the visibility of its input.
533 if (DGV->isDeclaration())
534 DGV->setVisibility(SGV->getVisibility());
536 if (DGV->hasAppendingLinkage()) {
537 // No linking is performed yet. Just insert a new copy of the global, and
538 // keep track of the fact that it is an appending variable in the
539 // AppendingVars map. The name is cleared out so that no linkage is
541 GlobalVariable *NewDGV =
542 new GlobalVariable(*Dest, SGV->getType()->getElementType(),
543 SGV->isConstant(), SGV->getLinkage(), /*init*/0,
545 SGV->getType()->getAddressSpace());
547 // Set alignment allowing CopyGVAttributes merge it with alignment of SGV.
548 NewDGV->setAlignment(DGV->getAlignment());
549 // Propagate alignment, section and visibility info.
550 CopyGVAttributes(NewDGV, SGV);
552 // Make sure to remember this mapping...
553 ValueMap[SGV] = NewDGV;
555 // Keep track that this is an appending variable...
556 AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
561 if (isa<GlobalAlias>(DGV))
562 return Error(Err, "Global-Alias Collision on '" + SGV->getName() +
563 "': symbol multiple defined");
565 // If the types don't match, and if we are to link from the source, nuke
566 // DGV and create a new one of the appropriate type. Note that the thing
567 // we are replacing may be a function (if a prototype, weak, etc) or a
569 GlobalVariable *NewDGV =
570 new GlobalVariable(*Dest, SGV->getType()->getElementType(),
571 SGV->isConstant(), NewLinkage, /*init*/0,
572 DGV->getName(), 0, false,
573 SGV->getType()->getAddressSpace());
575 // Propagate alignment, section, and visibility info.
576 CopyGVAttributes(NewDGV, SGV);
577 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV,
580 // DGV will conflict with NewDGV because they both had the same
581 // name. We must erase this now so ForceRenaming doesn't assert
582 // because DGV might not have internal linkage.
583 if (GlobalVariable *Var = dyn_cast<GlobalVariable>(DGV))
584 Var->eraseFromParent();
586 cast<Function>(DGV)->eraseFromParent();
588 // If the symbol table renamed the global, but it is an externally visible
589 // symbol, DGV must be an existing global with internal linkage. Rename.
590 if (NewDGV->getName() != SGV->getName() && !NewDGV->hasLocalLinkage())
591 ForceRenaming(NewDGV, SGV->getName());
593 // Inherit const as appropriate.
594 NewDGV->setConstant(SGV->isConstant());
596 // Make sure to remember this mapping.
597 ValueMap[SGV] = NewDGV;
601 // Not "link from source", keep the one in the DestModule and remap the
604 // Special case for const propagation.
605 if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV))
606 if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant())
607 DGVar->setConstant(true);
609 // SGV is global, but DGV is alias.
610 if (isa<GlobalAlias>(DGV)) {
611 // The only valid mappings are:
612 // - SGV is external declaration, which is effectively a no-op.
613 // - SGV is weak, when we just need to throw SGV out.
614 if (!SGV->isDeclaration() && !SGV->isWeakForLinker())
615 return Error(Err, "Global-Alias Collision on '" + SGV->getName() +
616 "': symbol multiple defined");
619 // Set calculated linkage
620 DGV->setLinkage(NewLinkage);
622 // Make sure to remember this mapping...
623 ValueMap[SGV] = ConstantExpr::getBitCast(DGV, SGV->getType());
628 static GlobalValue::LinkageTypes
629 CalculateAliasLinkage(const GlobalValue *SGV, const GlobalValue *DGV) {
630 GlobalValue::LinkageTypes SL = SGV->getLinkage();
631 GlobalValue::LinkageTypes DL = DGV->getLinkage();
632 if (SL == GlobalValue::ExternalLinkage || DL == GlobalValue::ExternalLinkage)
633 return GlobalValue::ExternalLinkage;
634 else if (SL == GlobalValue::WeakAnyLinkage ||
635 DL == GlobalValue::WeakAnyLinkage)
636 return GlobalValue::WeakAnyLinkage;
637 else if (SL == GlobalValue::WeakODRLinkage ||
638 DL == GlobalValue::WeakODRLinkage)
639 return GlobalValue::WeakODRLinkage;
640 else if (SL == GlobalValue::InternalLinkage &&
641 DL == GlobalValue::InternalLinkage)
642 return GlobalValue::InternalLinkage;
643 else if (SL == GlobalValue::LinkerPrivateLinkage &&
644 DL == GlobalValue::LinkerPrivateLinkage)
645 return GlobalValue::LinkerPrivateLinkage;
647 assert (SL == GlobalValue::PrivateLinkage &&
648 DL == GlobalValue::PrivateLinkage && "Unexpected linkage type");
649 return GlobalValue::PrivateLinkage;
653 // LinkAlias - Loop through the alias in the src module and link them into the
654 // dest module. We're assuming, that all functions/global variables were already
656 static bool LinkAlias(Module *Dest, const Module *Src,
657 ValueToValueMapTy &ValueMap,
659 // Loop over all alias in the src module
660 for (Module::const_alias_iterator I = Src->alias_begin(),
661 E = Src->alias_end(); I != E; ++I) {
662 const GlobalAlias *SGA = I;
663 const GlobalValue *SAliasee = SGA->getAliasedGlobal();
664 GlobalAlias *NewGA = NULL;
666 // Globals were already linked, thus we can just query ValueMap for variant
667 // of SAliasee in Dest.
668 ValueToValueMapTy::const_iterator VMI = ValueMap.find(SAliasee);
669 assert(VMI != ValueMap.end() && "Aliasee not linked");
670 GlobalValue* DAliasee = cast<GlobalValue>(VMI->second);
671 GlobalValue* DGV = NULL;
673 // Try to find something 'similar' to SGA in destination module.
674 if (!DGV && !SGA->hasLocalLinkage()) {
675 DGV = Dest->getNamedAlias(SGA->getName());
677 // If types don't agree due to opaque types, try to resolve them.
678 if (DGV && DGV->getType() != SGA->getType())
679 RecursiveResolveTypes(SGA->getType(), DGV->getType());
682 if (!DGV && !SGA->hasLocalLinkage()) {
683 DGV = Dest->getGlobalVariable(SGA->getName());
685 // If types don't agree due to opaque types, try to resolve them.
686 if (DGV && DGV->getType() != SGA->getType())
687 RecursiveResolveTypes(SGA->getType(), DGV->getType());
690 if (!DGV && !SGA->hasLocalLinkage()) {
691 DGV = Dest->getFunction(SGA->getName());
693 // If types don't agree due to opaque types, try to resolve them.
694 if (DGV && DGV->getType() != SGA->getType())
695 RecursiveResolveTypes(SGA->getType(), DGV->getType());
698 // No linking to be performed on internal stuff.
699 if (DGV && DGV->hasLocalLinkage())
702 if (GlobalAlias *DGA = dyn_cast_or_null<GlobalAlias>(DGV)) {
703 // Types are known to be the same, check whether aliasees equal. As
704 // globals are already linked we just need query ValueMap to find the
706 if (DAliasee == DGA->getAliasedGlobal()) {
707 // This is just two copies of the same alias. Propagate linkage, if
709 DGA->setLinkage(CalculateAliasLinkage(SGA, DGA));
712 // Proceed to 'common' steps
714 return Error(Err, "Alias Collision on '" + SGA->getName()+
715 "': aliases have different aliasees");
716 } else if (GlobalVariable *DGVar = dyn_cast_or_null<GlobalVariable>(DGV)) {
717 // The only allowed way is to link alias with external declaration or weak
719 if (DGVar->isDeclaration() || DGVar->isWeakForLinker()) {
720 // But only if aliasee is global too...
721 if (!isa<GlobalVariable>(DAliasee))
722 return Error(Err, "Global-Alias Collision on '" + SGA->getName() +
723 "': aliasee is not global variable");
725 NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(),
726 SGA->getName(), DAliasee, Dest);
727 CopyGVAttributes(NewGA, SGA);
729 // Any uses of DGV need to change to NewGA, with cast, if needed.
730 if (SGA->getType() != DGVar->getType())
731 DGVar->replaceAllUsesWith(ConstantExpr::getBitCast(NewGA,
734 DGVar->replaceAllUsesWith(NewGA);
736 // DGVar will conflict with NewGA because they both had the same
737 // name. We must erase this now so ForceRenaming doesn't assert
738 // because DGV might not have internal linkage.
739 DGVar->eraseFromParent();
741 // Proceed to 'common' steps
743 return Error(Err, "Global-Alias Collision on '" + SGA->getName() +
744 "': symbol multiple defined");
745 } else if (Function *DF = dyn_cast_or_null<Function>(DGV)) {
746 // The only allowed way is to link alias with external declaration or weak
748 if (DF->isDeclaration() || DF->isWeakForLinker()) {
749 // But only if aliasee is function too...
750 if (!isa<Function>(DAliasee))
751 return Error(Err, "Function-Alias Collision on '" + SGA->getName() +
752 "': aliasee is not function");
754 NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(),
755 SGA->getName(), DAliasee, Dest);
756 CopyGVAttributes(NewGA, SGA);
758 // Any uses of DF need to change to NewGA, with cast, if needed.
759 if (SGA->getType() != DF->getType())
760 DF->replaceAllUsesWith(ConstantExpr::getBitCast(NewGA,
763 DF->replaceAllUsesWith(NewGA);
765 // DF will conflict with NewGA because they both had the same
766 // name. We must erase this now so ForceRenaming doesn't assert
767 // because DF might not have internal linkage.
768 DF->eraseFromParent();
770 // Proceed to 'common' steps
772 return Error(Err, "Function-Alias Collision on '" + SGA->getName() +
773 "': symbol multiple defined");
775 // No linking to be performed, simply create an identical version of the
776 // alias over in the dest module...
777 Constant *Aliasee = DAliasee;
778 // Fixup aliases to bitcasts. Note that aliases to GEPs are still broken
779 // by this, but aliases to GEPs are broken to a lot of other things, so
780 // it's less important.
781 if (SGA->getType() != DAliasee->getType())
782 Aliasee = ConstantExpr::getBitCast(DAliasee, SGA->getType());
783 NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(),
784 SGA->getName(), Aliasee, Dest);
785 CopyGVAttributes(NewGA, SGA);
787 // Proceed to 'common' steps
790 assert(NewGA && "No alias was created in destination module!");
792 // If the symbol table renamed the alias, but it is an externally visible
793 // symbol, DGA must be an global value with internal linkage. Rename it.
794 if (NewGA->getName() != SGA->getName() &&
795 !NewGA->hasLocalLinkage())
796 ForceRenaming(NewGA, SGA->getName());
798 // Remember this mapping so uses in the source module get remapped
799 // later by MapValue.
800 ValueMap[SGA] = NewGA;
807 // LinkGlobalInits - Update the initializers in the Dest module now that all
808 // globals that may be referenced are in Dest.
809 static bool LinkGlobalInits(Module *Dest, const Module *Src,
810 ValueToValueMapTy &ValueMap,
812 // Loop over all of the globals in the src module, mapping them over as we go
813 for (Module::const_global_iterator I = Src->global_begin(),
814 E = Src->global_end(); I != E; ++I) {
815 const GlobalVariable *SGV = I;
817 if (SGV->hasInitializer()) { // Only process initialized GV's
818 // Figure out what the initializer looks like in the dest module...
820 cast<Constant>(MapValue(SGV->getInitializer(), ValueMap));
821 // Grab destination global variable or alias.
822 GlobalValue *DGV = cast<GlobalValue>(ValueMap[SGV]->stripPointerCasts());
824 // If dest if global variable, check that initializers match.
825 if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV)) {
826 if (DGVar->hasInitializer()) {
827 if (SGV->hasExternalLinkage()) {
828 if (DGVar->getInitializer() != SInit)
829 return Error(Err, "Global Variable Collision on '" +
831 "': global variables have different initializers");
832 } else if (DGVar->isWeakForLinker()) {
833 // Nothing is required, mapped values will take the new global
835 } else if (SGV->isWeakForLinker()) {
836 // Nothing is required, mapped values will take the new global
838 } else if (DGVar->hasAppendingLinkage()) {
839 llvm_unreachable("Appending linkage unimplemented!");
841 llvm_unreachable("Unknown linkage!");
844 // Copy the initializer over now...
845 DGVar->setInitializer(SInit);
848 // Destination is alias, the only valid situation is when source is
849 // weak. Also, note, that we already checked linkage in LinkGlobals(),
850 // thus we assert here.
851 // FIXME: Should we weaken this assumption, 'dereference' alias and
852 // check for initializer of aliasee?
853 assert(SGV->isWeakForLinker());
860 // LinkFunctionProtos - Link the functions together between the two modules,
861 // without doing function bodies... this just adds external function prototypes
862 // to the Dest function...
864 static bool LinkFunctionProtos(Module *Dest, const Module *Src,
865 ValueToValueMapTy &ValueMap,
867 ValueSymbolTable &DestSymTab = Dest->getValueSymbolTable();
869 // Loop over all of the functions in the src module, mapping them over
870 for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
871 const Function *SF = I; // SrcFunction
872 GlobalValue *DGV = 0;
874 // Check to see if may have to link the function with the global, alias or
876 if (SF->hasName() && !SF->hasLocalLinkage())
877 DGV = cast_or_null<GlobalValue>(DestSymTab.lookup(SF->getName()));
879 // If we found a global with the same name in the dest module, but it has
880 // internal linkage, we are really not doing any linkage here.
881 if (DGV && DGV->hasLocalLinkage())
884 // If types don't agree due to opaque types, try to resolve them.
885 if (DGV && DGV->getType() != SF->getType())
886 RecursiveResolveTypes(SF->getType(), DGV->getType());
888 GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
889 bool LinkFromSrc = false;
890 if (GetLinkageResult(DGV, SF, NewLinkage, LinkFromSrc, Err))
893 // If there is no linkage to be performed, just bring over SF without
896 // Function does not already exist, simply insert an function signature
897 // identical to SF into the dest module.
898 Function *NewDF = Function::Create(SF->getFunctionType(),
900 SF->getName(), Dest);
901 CopyGVAttributes(NewDF, SF);
903 // If the LLVM runtime renamed the function, but it is an externally
904 // visible symbol, DF must be an existing function with internal linkage.
906 if (!NewDF->hasLocalLinkage() && NewDF->getName() != SF->getName())
907 ForceRenaming(NewDF, SF->getName());
909 // ... and remember this mapping...
910 ValueMap[SF] = NewDF;
914 // If the visibilities of the symbols disagree and the destination is a
915 // prototype, take the visibility of its input.
916 if (DGV->isDeclaration())
917 DGV->setVisibility(SF->getVisibility());
920 if (isa<GlobalAlias>(DGV))
921 return Error(Err, "Function-Alias Collision on '" + SF->getName() +
922 "': symbol multiple defined");
924 // We have a definition of the same name but different type in the
925 // source module. Copy the prototype to the destination and replace
926 // uses of the destination's prototype with the new prototype.
927 Function *NewDF = Function::Create(SF->getFunctionType(), NewLinkage,
928 SF->getName(), Dest);
929 CopyGVAttributes(NewDF, SF);
931 // Any uses of DF need to change to NewDF, with cast
932 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF,
935 // DF will conflict with NewDF because they both had the same. We must
936 // erase this now so ForceRenaming doesn't assert because DF might
937 // not have internal linkage.
938 if (GlobalVariable *Var = dyn_cast<GlobalVariable>(DGV))
939 Var->eraseFromParent();
941 cast<Function>(DGV)->eraseFromParent();
943 // If the symbol table renamed the function, but it is an externally
944 // visible symbol, DF must be an existing function with internal
945 // linkage. Rename it.
946 if (NewDF->getName() != SF->getName() && !NewDF->hasLocalLinkage())
947 ForceRenaming(NewDF, SF->getName());
949 // Remember this mapping so uses in the source module get remapped
950 // later by MapValue.
951 ValueMap[SF] = NewDF;
955 // Not "link from source", keep the one in the DestModule and remap the
958 if (isa<GlobalAlias>(DGV)) {
959 // The only valid mappings are:
960 // - SF is external declaration, which is effectively a no-op.
961 // - SF is weak, when we just need to throw SF out.
962 if (!SF->isDeclaration() && !SF->isWeakForLinker())
963 return Error(Err, "Function-Alias Collision on '" + SF->getName() +
964 "': symbol multiple defined");
967 // Set calculated linkage
968 DGV->setLinkage(NewLinkage);
970 // Make sure to remember this mapping.
971 ValueMap[SF] = ConstantExpr::getBitCast(DGV, SF->getType());
976 // LinkFunctionBody - Copy the source function over into the dest function and
977 // fix up references to values. At this point we know that Dest is an external
978 // function, and that Src is not.
979 static bool LinkFunctionBody(Function *Dest, Function *Src,
980 ValueToValueMapTy &ValueMap,
982 assert(Src && Dest && Dest->isDeclaration() && !Src->isDeclaration());
984 // Go through and convert function arguments over, remembering the mapping.
985 Function::arg_iterator DI = Dest->arg_begin();
986 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
988 DI->setName(I->getName()); // Copy the name information over...
990 // Add a mapping to our local map
994 // Splice the body of the source function into the dest function.
995 Dest->getBasicBlockList().splice(Dest->end(), Src->getBasicBlockList());
997 // At this point, all of the instructions and values of the function are now
998 // copied over. The only problem is that they are still referencing values in
999 // the Source function as operands. Loop through all of the operands of the
1000 // functions and patch them up to point to the local versions...
1002 for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
1003 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1004 for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
1006 if (!isa<Instruction>(*OI) && !isa<BasicBlock>(*OI))
1007 *OI = MapValue(*OI, ValueMap);
1009 // There is no need to map the arguments anymore.
1010 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
1018 // LinkFunctionBodies - Link in the function bodies that are defined in the
1019 // source module into the DestModule. This consists basically of copying the
1020 // function over and fixing up references to values.
1021 static bool LinkFunctionBodies(Module *Dest, Module *Src,
1022 ValueToValueMapTy &ValueMap,
1025 // Loop over all of the functions in the src module, mapping them over as we
1027 for (Module::iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF) {
1028 if (!SF->isDeclaration()) { // No body if function is external
1029 Function *DF = dyn_cast<Function>(ValueMap[SF]); // Destination function
1031 // DF not external SF external?
1032 if (DF && DF->isDeclaration())
1033 // Only provide the function body if there isn't one already.
1034 if (LinkFunctionBody(DF, SF, ValueMap, Err))
1041 // LinkAppendingVars - If there were any appending global variables, link them
1042 // together now. Return true on error.
1043 static bool LinkAppendingVars(Module *M,
1044 std::multimap<std::string, GlobalVariable *> &AppendingVars,
1045 std::string *ErrorMsg) {
1046 if (AppendingVars.empty()) return false; // Nothing to do.
1048 // Loop over the multimap of appending vars, processing any variables with the
1049 // same name, forming a new appending global variable with both of the
1050 // initializers merged together, then rewrite references to the old variables
1052 std::vector<Constant*> Inits;
1053 while (AppendingVars.size() > 1) {
1054 // Get the first two elements in the map...
1055 std::multimap<std::string,
1056 GlobalVariable*>::iterator Second = AppendingVars.begin(), First=Second++;
1058 // If the first two elements are for different names, there is no pair...
1059 // Otherwise there is a pair, so link them together...
1060 if (First->first == Second->first) {
1061 GlobalVariable *G1 = First->second, *G2 = Second->second;
1062 const ArrayType *T1 = cast<ArrayType>(G1->getType()->getElementType());
1063 const ArrayType *T2 = cast<ArrayType>(G2->getType()->getElementType());
1065 // Check to see that they two arrays agree on type...
1066 if (T1->getElementType() != T2->getElementType())
1067 return Error(ErrorMsg,
1068 "Appending variables with different element types need to be linked!");
1069 if (G1->isConstant() != G2->isConstant())
1070 return Error(ErrorMsg,
1071 "Appending variables linked with different const'ness!");
1073 if (G1->getAlignment() != G2->getAlignment())
1074 return Error(ErrorMsg,
1075 "Appending variables with different alignment need to be linked!");
1077 if (G1->getVisibility() != G2->getVisibility())
1078 return Error(ErrorMsg,
1079 "Appending variables with different visibility need to be linked!");
1081 if (G1->getSection() != G2->getSection())
1082 return Error(ErrorMsg,
1083 "Appending variables with different section name need to be linked!");
1085 unsigned NewSize = T1->getNumElements() + T2->getNumElements();
1086 ArrayType *NewType = ArrayType::get(T1->getElementType(),
1089 G1->setName(""); // Clear G1's name in case of a conflict!
1091 // Create the new global variable...
1092 GlobalVariable *NG =
1093 new GlobalVariable(*M, NewType, G1->isConstant(), G1->getLinkage(),
1094 /*init*/0, First->first, 0, G1->isThreadLocal(),
1095 G1->getType()->getAddressSpace());
1097 // Propagate alignment, visibility and section info.
1098 CopyGVAttributes(NG, G1);
1100 // Merge the initializer...
1101 Inits.reserve(NewSize);
1102 if (ConstantArray *I = dyn_cast<ConstantArray>(G1->getInitializer())) {
1103 for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
1104 Inits.push_back(I->getOperand(i));
1106 assert(isa<ConstantAggregateZero>(G1->getInitializer()));
1107 Constant *CV = Constant::getNullValue(T1->getElementType());
1108 for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
1109 Inits.push_back(CV);
1111 if (ConstantArray *I = dyn_cast<ConstantArray>(G2->getInitializer())) {
1112 for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
1113 Inits.push_back(I->getOperand(i));
1115 assert(isa<ConstantAggregateZero>(G2->getInitializer()));
1116 Constant *CV = Constant::getNullValue(T2->getElementType());
1117 for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
1118 Inits.push_back(CV);
1120 NG->setInitializer(ConstantArray::get(NewType, Inits));
1123 // Replace any uses of the two global variables with uses of the new
1126 // FIXME: This should rewrite simple/straight-forward uses such as
1127 // getelementptr instructions to not use the Cast!
1128 G1->replaceAllUsesWith(ConstantExpr::getBitCast(NG,
1130 G2->replaceAllUsesWith(ConstantExpr::getBitCast(NG,
1133 // Remove the two globals from the module now...
1134 M->getGlobalList().erase(G1);
1135 M->getGlobalList().erase(G2);
1137 // Put the new global into the AppendingVars map so that we can handle
1138 // linking of more than two vars...
1139 Second->second = NG;
1141 AppendingVars.erase(First);
1147 static bool ResolveAliases(Module *Dest) {
1148 for (Module::alias_iterator I = Dest->alias_begin(), E = Dest->alias_end();
1150 // We can't sue resolveGlobalAlias here because we need to preserve
1151 // bitcasts and GEPs.
1152 if (const Constant *C = I->getAliasee()) {
1153 while (dyn_cast<GlobalAlias>(C))
1154 C = cast<GlobalAlias>(C)->getAliasee();
1155 const GlobalValue *GV = dyn_cast<GlobalValue>(C);
1156 if (C != I && !(GV && GV->isDeclaration()))
1157 I->replaceAllUsesWith(const_cast<Constant*>(C));
1163 // LinkModules - This function links two modules together, with the resulting
1164 // left module modified to be the composite of the two input modules. If an
1165 // error occurs, true is returned and ErrorMsg (if not null) is set to indicate
1166 // the problem. Upon failure, the Dest module could be in a modified state, and
1167 // shouldn't be relied on to be consistent.
1169 Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) {
1170 assert(Dest != 0 && "Invalid Destination module");
1171 assert(Src != 0 && "Invalid Source Module");
1173 if (Dest->getDataLayout().empty()) {
1174 if (!Src->getDataLayout().empty()) {
1175 Dest->setDataLayout(Src->getDataLayout());
1177 std::string DataLayout;
1179 if (Dest->getEndianness() == Module::AnyEndianness) {
1180 if (Src->getEndianness() == Module::BigEndian)
1181 DataLayout.append("E");
1182 else if (Src->getEndianness() == Module::LittleEndian)
1183 DataLayout.append("e");
1186 if (Dest->getPointerSize() == Module::AnyPointerSize) {
1187 if (Src->getPointerSize() == Module::Pointer64)
1188 DataLayout.append(DataLayout.length() == 0 ? "p:64:64" : "-p:64:64");
1189 else if (Src->getPointerSize() == Module::Pointer32)
1190 DataLayout.append(DataLayout.length() == 0 ? "p:32:32" : "-p:32:32");
1192 Dest->setDataLayout(DataLayout);
1196 // Copy the target triple from the source to dest if the dest's is empty.
1197 if (Dest->getTargetTriple().empty() && !Src->getTargetTriple().empty())
1198 Dest->setTargetTriple(Src->getTargetTriple());
1200 if (!Src->getDataLayout().empty() && !Dest->getDataLayout().empty() &&
1201 Src->getDataLayout() != Dest->getDataLayout())
1202 errs() << "WARNING: Linking two modules of different data layouts!\n";
1203 if (!Src->getTargetTriple().empty() &&
1204 Dest->getTargetTriple() != Src->getTargetTriple())
1205 errs() << "WARNING: Linking two modules of different target triples!\n";
1207 // Append the module inline asm string.
1208 if (!Src->getModuleInlineAsm().empty()) {
1209 if (Dest->getModuleInlineAsm().empty())
1210 Dest->setModuleInlineAsm(Src->getModuleInlineAsm());
1212 Dest->setModuleInlineAsm(Dest->getModuleInlineAsm()+"\n"+
1213 Src->getModuleInlineAsm());
1216 // Update the destination module's dependent libraries list with the libraries
1217 // from the source module. There's no opportunity for duplicates here as the
1218 // Module ensures that duplicate insertions are discarded.
1219 for (Module::lib_iterator SI = Src->lib_begin(), SE = Src->lib_end();
1221 Dest->addLibrary(*SI);
1223 // LinkTypes - Go through the symbol table of the Src module and see if any
1224 // types are named in the src module that are not named in the Dst module.
1225 // Make sure there are no type name conflicts.
1226 if (LinkTypes(Dest, Src, ErrorMsg))
1229 // ValueMap - Mapping of values from what they used to be in Src, to what they
1230 // are now in Dest. ValueToValueMapTy is a ValueMap, which involves some
1231 // overhead due to the use of Value handles which the Linker doesn't actually
1232 // need, but this allows us to reuse the ValueMapper code.
1233 ValueToValueMapTy ValueMap;
1235 // AppendingVars - Keep track of global variables in the destination module
1236 // with appending linkage. After the module is linked together, they are
1237 // appended and the module is rewritten.
1238 std::multimap<std::string, GlobalVariable *> AppendingVars;
1239 for (Module::global_iterator I = Dest->global_begin(), E = Dest->global_end();
1241 // Add all of the appending globals already in the Dest module to
1243 if (I->hasAppendingLinkage())
1244 AppendingVars.insert(std::make_pair(I->getName(), I));
1247 // Insert all of the named mdnoes in Src into the Dest module.
1248 LinkNamedMDNodes(Dest, Src, ValueMap);
1250 // Insert all of the globals in src into the Dest module... without linking
1251 // initializers (which could refer to functions not yet mapped over).
1252 if (LinkGlobals(Dest, Src, ValueMap, AppendingVars, ErrorMsg))
1255 // Link the functions together between the two modules, without doing function
1256 // bodies... this just adds external function prototypes to the Dest
1257 // function... We do this so that when we begin processing function bodies,
1258 // all of the global values that may be referenced are available in our
1260 if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg))
1263 // If there were any alias, link them now. We really need to do this now,
1264 // because all of the aliases that may be referenced need to be available in
1266 if (LinkAlias(Dest, Src, ValueMap, ErrorMsg)) return true;
1268 // Update the initializers in the Dest module now that all globals that may
1269 // be referenced are in Dest.
1270 if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
1272 // Link in the function bodies that are defined in the source module into the
1273 // DestModule. This consists basically of copying the function over and
1274 // fixing up references to values.
1275 if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true;
1277 // If there were any appending global variables, link them together now.
1278 if (LinkAppendingVars(Dest, AppendingVars, ErrorMsg)) return true;
1280 // Resolve all uses of aliases with aliasees
1281 if (ResolveAliases(Dest)) return true;
1283 // If the source library's module id is in the dependent library list of the
1284 // destination library, remove it since that module is now linked in.
1286 modId.set(Src->getModuleIdentifier());
1287 if (!modId.isEmpty())
1288 Dest->removeLibrary(modId.getBasename());