1361e513dSReid Spencer //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===// 2361e513dSReid Spencer // 3361e513dSReid Spencer // The LLVM Compiler Infrastructure 4361e513dSReid Spencer // 5361e513dSReid Spencer // This file was developed by the LLVM research group and is distributed under 6361e513dSReid Spencer // the University of Illinois Open Source License. See LICENSE.TXT for details. 7361e513dSReid Spencer // 8361e513dSReid Spencer //===----------------------------------------------------------------------===// 9361e513dSReid Spencer // 10361e513dSReid Spencer // This file implements the LLVM module linker. 11361e513dSReid Spencer // 12361e513dSReid Spencer // Specifically, this: 13361e513dSReid Spencer // * Merges global variables between the two modules 14361e513dSReid Spencer // * Uninit + Uninit = Init, Init + Uninit = Init, Init + Init = Error if != 15361e513dSReid Spencer // * Merges functions between two modules 16361e513dSReid Spencer // 17361e513dSReid Spencer //===----------------------------------------------------------------------===// 18361e513dSReid Spencer 199b0ddbbeSReid Spencer #include "llvm/Linker.h" 20361e513dSReid Spencer #include "llvm/Constants.h" 21361e513dSReid Spencer #include "llvm/DerivedTypes.h" 22361e513dSReid Spencer #include "llvm/Module.h" 2332af9e8cSReid Spencer #include "llvm/TypeSymbolTable.h" 243aaaa0b2SReid Spencer #include "llvm/ValueSymbolTable.h" 25361e513dSReid Spencer #include "llvm/Instructions.h" 26361e513dSReid Spencer #include "llvm/Assembly/Writer.h" 277339b8dbSBill Wendling #include "llvm/Support/Streams.h" 28361e513dSReid Spencer #include "llvm/System/Path.h" 2930c0f336SBill Wendling #include <sstream> 30361e513dSReid Spencer using namespace llvm; 31361e513dSReid Spencer 32361e513dSReid Spencer // Error - Simple wrapper function to conditionally assign to E and return true. 33361e513dSReid Spencer // This just makes error return conditions a little bit simpler... 34361e513dSReid Spencer static inline bool Error(std::string *E, const std::string &Message) { 35361e513dSReid Spencer if (E) *E = Message; 36361e513dSReid Spencer return true; 37361e513dSReid Spencer } 38361e513dSReid Spencer 392c4f9a48SReid Spencer // ToStr - Simple wrapper function to convert a type to a string. 40361e513dSReid Spencer static std::string ToStr(const Type *Ty, const Module *M) { 41361e513dSReid Spencer std::ostringstream OS; 42361e513dSReid Spencer WriteTypeSymbolic(OS, Ty, M); 43361e513dSReid Spencer return OS.str(); 44361e513dSReid Spencer } 45361e513dSReid Spencer 46361e513dSReid Spencer // 47361e513dSReid Spencer // Function: ResolveTypes() 48361e513dSReid Spencer // 49361e513dSReid Spencer // Description: 50361e513dSReid Spencer // Attempt to link the two specified types together. 51361e513dSReid Spencer // 52361e513dSReid Spencer // Inputs: 53361e513dSReid Spencer // DestTy - The type to which we wish to resolve. 54361e513dSReid Spencer // SrcTy - The original type which we want to resolve. 55361e513dSReid Spencer // Name - The name of the type. 56361e513dSReid Spencer // 57361e513dSReid Spencer // Outputs: 58361e513dSReid Spencer // DestST - The symbol table in which the new type should be placed. 59361e513dSReid Spencer // 60361e513dSReid Spencer // Return value: 61361e513dSReid Spencer // true - There is an error and the types cannot yet be linked. 62361e513dSReid Spencer // false - No errors. 63361e513dSReid Spencer // 64361e513dSReid Spencer static bool ResolveTypes(const Type *DestTy, const Type *SrcTy, 6532af9e8cSReid Spencer TypeSymbolTable *DestST, const std::string &Name) { 66361e513dSReid Spencer if (DestTy == SrcTy) return false; // If already equal, noop 67361e513dSReid Spencer 68361e513dSReid Spencer // Does the type already exist in the module? 69361e513dSReid Spencer if (DestTy && !isa<OpaqueType>(DestTy)) { // Yup, the type already exists... 70361e513dSReid Spencer if (const OpaqueType *OT = dyn_cast<OpaqueType>(SrcTy)) { 71361e513dSReid Spencer const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(DestTy); 72361e513dSReid Spencer } else { 73361e513dSReid Spencer return true; // Cannot link types... neither is opaque and not-equal 74361e513dSReid Spencer } 75361e513dSReid Spencer } else { // Type not in dest module. Add it now. 76361e513dSReid Spencer if (DestTy) // Type _is_ in module, just opaque... 77361e513dSReid Spencer const_cast<OpaqueType*>(cast<OpaqueType>(DestTy)) 78361e513dSReid Spencer ->refineAbstractTypeTo(SrcTy); 79361e513dSReid Spencer else if (!Name.empty()) 80361e513dSReid Spencer DestST->insert(Name, const_cast<Type*>(SrcTy)); 81361e513dSReid Spencer } 82361e513dSReid Spencer return false; 83361e513dSReid Spencer } 84361e513dSReid Spencer 85361e513dSReid Spencer static const FunctionType *getFT(const PATypeHolder &TH) { 86361e513dSReid Spencer return cast<FunctionType>(TH.get()); 87361e513dSReid Spencer } 88361e513dSReid Spencer static const StructType *getST(const PATypeHolder &TH) { 89361e513dSReid Spencer return cast<StructType>(TH.get()); 90361e513dSReid Spencer } 91361e513dSReid Spencer 92361e513dSReid Spencer // RecursiveResolveTypes - This is just like ResolveTypes, except that it 93361e513dSReid Spencer // recurses down into derived types, merging the used types if the parent types 94361e513dSReid Spencer // are compatible. 95361e513dSReid Spencer static bool RecursiveResolveTypesI(const PATypeHolder &DestTy, 96361e513dSReid Spencer const PATypeHolder &SrcTy, 9732af9e8cSReid Spencer TypeSymbolTable *DestST, 9832af9e8cSReid Spencer const std::string &Name, 99361e513dSReid Spencer std::vector<std::pair<PATypeHolder, PATypeHolder> > &Pointers) { 100361e513dSReid Spencer const Type *SrcTyT = SrcTy.get(); 101361e513dSReid Spencer const Type *DestTyT = DestTy.get(); 102361e513dSReid Spencer if (DestTyT == SrcTyT) return false; // If already equal, noop 103361e513dSReid Spencer 104361e513dSReid Spencer // If we found our opaque type, resolve it now! 105361e513dSReid Spencer if (isa<OpaqueType>(DestTyT) || isa<OpaqueType>(SrcTyT)) 106361e513dSReid Spencer return ResolveTypes(DestTyT, SrcTyT, DestST, Name); 107361e513dSReid Spencer 108361e513dSReid Spencer // Two types cannot be resolved together if they are of different primitive 109361e513dSReid Spencer // type. For example, we cannot resolve an int to a float. 110361e513dSReid Spencer if (DestTyT->getTypeID() != SrcTyT->getTypeID()) return true; 111361e513dSReid Spencer 112361e513dSReid Spencer // Otherwise, resolve the used type used by this derived type... 113361e513dSReid Spencer switch (DestTyT->getTypeID()) { 1147a9c62baSReid Spencer case Type::IntegerTyID: { 1157a9c62baSReid Spencer if (cast<IntegerType>(DestTyT)->getBitWidth() != 1167a9c62baSReid Spencer cast<IntegerType>(SrcTyT)->getBitWidth()) 1177a9c62baSReid Spencer return true; 1187a9c62baSReid Spencer return false; 1197a9c62baSReid Spencer } 120361e513dSReid Spencer case Type::FunctionTyID: { 121361e513dSReid Spencer if (cast<FunctionType>(DestTyT)->isVarArg() != 122361e513dSReid Spencer cast<FunctionType>(SrcTyT)->isVarArg() || 123361e513dSReid Spencer cast<FunctionType>(DestTyT)->getNumContainedTypes() != 124361e513dSReid Spencer cast<FunctionType>(SrcTyT)->getNumContainedTypes()) 125361e513dSReid Spencer return true; 126361e513dSReid Spencer for (unsigned i = 0, e = getFT(DestTy)->getNumContainedTypes(); i != e; ++i) 127361e513dSReid Spencer if (RecursiveResolveTypesI(getFT(DestTy)->getContainedType(i), 128361e513dSReid Spencer getFT(SrcTy)->getContainedType(i), DestST, "", 129361e513dSReid Spencer Pointers)) 130361e513dSReid Spencer return true; 131361e513dSReid Spencer return false; 132361e513dSReid Spencer } 133361e513dSReid Spencer case Type::StructTyID: { 134361e513dSReid Spencer if (getST(DestTy)->getNumContainedTypes() != 135361e513dSReid Spencer getST(SrcTy)->getNumContainedTypes()) return 1; 136361e513dSReid Spencer for (unsigned i = 0, e = getST(DestTy)->getNumContainedTypes(); i != e; ++i) 137361e513dSReid Spencer if (RecursiveResolveTypesI(getST(DestTy)->getContainedType(i), 138361e513dSReid Spencer getST(SrcTy)->getContainedType(i), DestST, "", 139361e513dSReid Spencer Pointers)) 140361e513dSReid Spencer return true; 141361e513dSReid Spencer return false; 142361e513dSReid Spencer } 143361e513dSReid Spencer case Type::ArrayTyID: { 144361e513dSReid Spencer const ArrayType *DAT = cast<ArrayType>(DestTy.get()); 145361e513dSReid Spencer const ArrayType *SAT = cast<ArrayType>(SrcTy.get()); 146361e513dSReid Spencer if (DAT->getNumElements() != SAT->getNumElements()) return true; 147361e513dSReid Spencer return RecursiveResolveTypesI(DAT->getElementType(), SAT->getElementType(), 148361e513dSReid Spencer DestST, "", Pointers); 149361e513dSReid Spencer } 150361e513dSReid Spencer case Type::PointerTyID: { 151361e513dSReid Spencer // If this is a pointer type, check to see if we have already seen it. If 152361e513dSReid Spencer // so, we are in a recursive branch. Cut off the search now. We cannot use 153361e513dSReid Spencer // an associative container for this search, because the type pointers (keys 154361e513dSReid Spencer // in the container) change whenever types get resolved... 155361e513dSReid Spencer for (unsigned i = 0, e = Pointers.size(); i != e; ++i) 156361e513dSReid Spencer if (Pointers[i].first == DestTy) 157361e513dSReid Spencer return Pointers[i].second != SrcTy; 158361e513dSReid Spencer 159361e513dSReid Spencer // Otherwise, add the current pointers to the vector to stop recursion on 160361e513dSReid Spencer // this pair. 161361e513dSReid Spencer Pointers.push_back(std::make_pair(DestTyT, SrcTyT)); 162361e513dSReid Spencer bool Result = 163361e513dSReid Spencer RecursiveResolveTypesI(cast<PointerType>(DestTy.get())->getElementType(), 164361e513dSReid Spencer cast<PointerType>(SrcTy.get())->getElementType(), 165361e513dSReid Spencer DestST, "", Pointers); 166361e513dSReid Spencer Pointers.pop_back(); 167361e513dSReid Spencer return Result; 168361e513dSReid Spencer } 169361e513dSReid Spencer default: assert(0 && "Unexpected type!"); return true; 170361e513dSReid Spencer } 171361e513dSReid Spencer } 172361e513dSReid Spencer 173361e513dSReid Spencer static bool RecursiveResolveTypes(const PATypeHolder &DestTy, 174361e513dSReid Spencer const PATypeHolder &SrcTy, 17532af9e8cSReid Spencer TypeSymbolTable *DestST, 17632af9e8cSReid Spencer const std::string &Name){ 177361e513dSReid Spencer std::vector<std::pair<PATypeHolder, PATypeHolder> > PointerTypes; 178361e513dSReid Spencer return RecursiveResolveTypesI(DestTy, SrcTy, DestST, Name, PointerTypes); 179361e513dSReid Spencer } 180361e513dSReid Spencer 181361e513dSReid Spencer 182361e513dSReid Spencer // LinkTypes - Go through the symbol table of the Src module and see if any 183361e513dSReid Spencer // types are named in the src module that are not named in the Dst module. 184361e513dSReid Spencer // Make sure there are no type name conflicts. 185361e513dSReid Spencer static bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) { 18632af9e8cSReid Spencer TypeSymbolTable *DestST = &Dest->getTypeSymbolTable(); 18732af9e8cSReid Spencer const TypeSymbolTable *SrcST = &Src->getTypeSymbolTable(); 188361e513dSReid Spencer 189361e513dSReid Spencer // Look for a type plane for Type's... 19032af9e8cSReid Spencer TypeSymbolTable::const_iterator TI = SrcST->begin(); 19132af9e8cSReid Spencer TypeSymbolTable::const_iterator TE = SrcST->end(); 192361e513dSReid Spencer if (TI == TE) return false; // No named types, do nothing. 193361e513dSReid Spencer 194361e513dSReid Spencer // Some types cannot be resolved immediately because they depend on other 195361e513dSReid Spencer // types being resolved to each other first. This contains a list of types we 196361e513dSReid Spencer // are waiting to recheck. 197361e513dSReid Spencer std::vector<std::string> DelayedTypesToResolve; 198361e513dSReid Spencer 199361e513dSReid Spencer for ( ; TI != TE; ++TI ) { 200361e513dSReid Spencer const std::string &Name = TI->first; 201361e513dSReid Spencer const Type *RHS = TI->second; 202361e513dSReid Spencer 203361e513dSReid Spencer // Check to see if this type name is already in the dest module... 20432af9e8cSReid Spencer Type *Entry = DestST->lookup(Name); 205361e513dSReid Spencer 206361e513dSReid Spencer if (ResolveTypes(Entry, RHS, DestST, Name)) { 207361e513dSReid Spencer // They look different, save the types 'till later to resolve. 208361e513dSReid Spencer DelayedTypesToResolve.push_back(Name); 209361e513dSReid Spencer } 210361e513dSReid Spencer } 211361e513dSReid Spencer 212361e513dSReid Spencer // Iteratively resolve types while we can... 213361e513dSReid Spencer while (!DelayedTypesToResolve.empty()) { 214361e513dSReid Spencer // Loop over all of the types, attempting to resolve them if possible... 215361e513dSReid Spencer unsigned OldSize = DelayedTypesToResolve.size(); 216361e513dSReid Spencer 217361e513dSReid Spencer // Try direct resolution by name... 218361e513dSReid Spencer for (unsigned i = 0; i != DelayedTypesToResolve.size(); ++i) { 219361e513dSReid Spencer const std::string &Name = DelayedTypesToResolve[i]; 22032af9e8cSReid Spencer Type *T1 = SrcST->lookup(Name); 22132af9e8cSReid Spencer Type *T2 = DestST->lookup(Name); 222361e513dSReid Spencer if (!ResolveTypes(T2, T1, DestST, Name)) { 223361e513dSReid Spencer // We are making progress! 224361e513dSReid Spencer DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i); 225361e513dSReid Spencer --i; 226361e513dSReid Spencer } 227361e513dSReid Spencer } 228361e513dSReid Spencer 229361e513dSReid Spencer // Did we not eliminate any types? 230361e513dSReid Spencer if (DelayedTypesToResolve.size() == OldSize) { 231361e513dSReid Spencer // Attempt to resolve subelements of types. This allows us to merge these 232361e513dSReid Spencer // two types: { int* } and { opaque* } 233361e513dSReid Spencer for (unsigned i = 0, e = DelayedTypesToResolve.size(); i != e; ++i) { 234361e513dSReid Spencer const std::string &Name = DelayedTypesToResolve[i]; 23532af9e8cSReid Spencer PATypeHolder T1(SrcST->lookup(Name)); 23632af9e8cSReid Spencer PATypeHolder T2(DestST->lookup(Name)); 237361e513dSReid Spencer 238361e513dSReid Spencer if (!RecursiveResolveTypes(T2, T1, DestST, Name)) { 239361e513dSReid Spencer // We are making progress! 240361e513dSReid Spencer DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i); 241361e513dSReid Spencer 242361e513dSReid Spencer // Go back to the main loop, perhaps we can resolve directly by name 243361e513dSReid Spencer // now... 244361e513dSReid Spencer break; 245361e513dSReid Spencer } 246361e513dSReid Spencer } 247361e513dSReid Spencer 248361e513dSReid Spencer // If we STILL cannot resolve the types, then there is something wrong. 249361e513dSReid Spencer if (DelayedTypesToResolve.size() == OldSize) { 250361e513dSReid Spencer // Remove the symbol name from the destination. 251361e513dSReid Spencer DelayedTypesToResolve.pop_back(); 252361e513dSReid Spencer } 253361e513dSReid Spencer } 254361e513dSReid Spencer } 255361e513dSReid Spencer 256361e513dSReid Spencer 257361e513dSReid Spencer return false; 258361e513dSReid Spencer } 259361e513dSReid Spencer 260361e513dSReid Spencer static void PrintMap(const std::map<const Value*, Value*> &M) { 261361e513dSReid Spencer for (std::map<const Value*, Value*>::const_iterator I = M.begin(), E =M.end(); 262361e513dSReid Spencer I != E; ++I) { 263f3baad3eSBill Wendling cerr << " Fr: " << (void*)I->first << " "; 264361e513dSReid Spencer I->first->dump(); 265f3baad3eSBill Wendling cerr << " To: " << (void*)I->second << " "; 266361e513dSReid Spencer I->second->dump(); 267f3baad3eSBill Wendling cerr << "\n"; 268361e513dSReid Spencer } 269361e513dSReid Spencer } 270361e513dSReid Spencer 271361e513dSReid Spencer 272d3ba7d9bSReid Spencer // RemapOperand - Use ValueMap to convert constants from one module to another. 273361e513dSReid Spencer static Value *RemapOperand(const Value *In, 2747391dde1SChris Lattner std::map<const Value*, Value*> &ValueMap) { 2757391dde1SChris Lattner std::map<const Value*,Value*>::const_iterator I = ValueMap.find(In); 2763aaaa0b2SReid Spencer if (I != ValueMap.end()) 2773aaaa0b2SReid Spencer return I->second; 278361e513dSReid Spencer 279d3ba7d9bSReid Spencer // Check to see if it's a constant that we are interested in transforming. 280c7745926SChris Lattner Value *Result = 0; 281361e513dSReid Spencer if (const Constant *CPV = dyn_cast<Constant>(In)) { 282361e513dSReid Spencer if ((!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) || 2837a9c62baSReid Spencer isa<ConstantInt>(CPV) || isa<ConstantAggregateZero>(CPV)) 2847391dde1SChris Lattner return const_cast<Constant*>(CPV); // Simple constants stay identical. 285361e513dSReid Spencer 286361e513dSReid Spencer if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) { 287361e513dSReid Spencer std::vector<Constant*> Operands(CPA->getNumOperands()); 288361e513dSReid Spencer for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) 2897391dde1SChris Lattner Operands[i] =cast<Constant>(RemapOperand(CPA->getOperand(i), ValueMap)); 290361e513dSReid Spencer Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands); 291361e513dSReid Spencer } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) { 292361e513dSReid Spencer std::vector<Constant*> Operands(CPS->getNumOperands()); 293361e513dSReid Spencer for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) 2947391dde1SChris Lattner Operands[i] =cast<Constant>(RemapOperand(CPS->getOperand(i), ValueMap)); 295361e513dSReid Spencer Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands); 296361e513dSReid Spencer } else if (isa<ConstantPointerNull>(CPV) || isa<UndefValue>(CPV)) { 297361e513dSReid Spencer Result = const_cast<Constant*>(CPV); 298d84d35baSReid Spencer } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CPV)) { 29993bde9cbSChris Lattner std::vector<Constant*> Operands(CP->getNumOperands()); 30093bde9cbSChris Lattner for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) 30193bde9cbSChris Lattner Operands[i] = cast<Constant>(RemapOperand(CP->getOperand(i), ValueMap)); 302d84d35baSReid Spencer Result = ConstantVector::get(Operands); 303361e513dSReid Spencer } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) { 30419247f36SChris Lattner std::vector<Constant*> Ops; 30519247f36SChris Lattner for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) 30619247f36SChris Lattner Ops.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),ValueMap))); 30719247f36SChris Lattner Result = CE->getWithOperands(Ops); 308d3ba7d9bSReid Spencer } else if (isa<GlobalValue>(CPV)) { 309d3ba7d9bSReid Spencer assert(0 && "Unmapped global?"); 310361e513dSReid Spencer } else { 311361e513dSReid Spencer assert(0 && "Unknown type of derived type constant value!"); 312361e513dSReid Spencer } 313c7745926SChris Lattner } else if (isa<InlineAsm>(In)) { 314c7745926SChris Lattner Result = const_cast<Value*>(In); 315c7745926SChris Lattner } 316361e513dSReid Spencer 317d3ba7d9bSReid Spencer // Cache the mapping in our local map structure 318c7745926SChris Lattner if (Result) { 3197391dde1SChris Lattner ValueMap.insert(std::make_pair(In, Result)); 320361e513dSReid Spencer return Result; 321361e513dSReid Spencer } 322361e513dSReid Spencer 32390246aa7SReid Spencer 324f3baad3eSBill Wendling cerr << "LinkModules ValueMap: \n"; 3257391dde1SChris Lattner PrintMap(ValueMap); 326361e513dSReid Spencer 327f3baad3eSBill Wendling cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n"; 328361e513dSReid Spencer assert(0 && "Couldn't remap value!"); 329361e513dSReid Spencer return 0; 330361e513dSReid Spencer } 331361e513dSReid Spencer 33290246aa7SReid Spencer /// ForceRenaming - The LLVM SymbolTable class autorenames globals that conflict 33390246aa7SReid Spencer /// in the symbol table. This is good for all clients except for us. Go 33490246aa7SReid Spencer /// through the trouble to force this back. 335361e513dSReid Spencer static void ForceRenaming(GlobalValue *GV, const std::string &Name) { 336361e513dSReid Spencer assert(GV->getName() != Name && "Can't force rename to self"); 3373aaaa0b2SReid Spencer ValueSymbolTable &ST = GV->getParent()->getValueSymbolTable(); 338361e513dSReid Spencer 339361e513dSReid Spencer // If there is a conflict, rename the conflict. 3402a8d2e0eSChris Lattner if (GlobalValue *ConflictGV = cast_or_null<GlobalValue>(ST.lookup(Name))) { 34190246aa7SReid Spencer assert(ConflictGV->hasInternalLinkage() && 34290246aa7SReid Spencer "Not conflicting with a static global, should link instead!"); 3432a8d2e0eSChris Lattner GV->takeName(ConflictGV); 344361e513dSReid Spencer ConflictGV->setName(Name); // This will cause ConflictGV to get renamed 3453aaaa0b2SReid Spencer assert(ConflictGV->getName() != Name && "ForceRenaming didn't work"); 3462a8d2e0eSChris Lattner } else { 3472a8d2e0eSChris Lattner GV->setName(Name); // Force the name back 3483aaaa0b2SReid Spencer } 3493aaaa0b2SReid Spencer } 3503aaaa0b2SReid Spencer 3513aaaa0b2SReid Spencer /// CopyGVAttributes - copy additional attributes (those not needed to construct 3523aaaa0b2SReid Spencer /// a GlobalValue) from the SrcGV to the DestGV. 3533aaaa0b2SReid Spencer static void CopyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) { 3543aaaa0b2SReid Spencer // Propagate alignment, visibility and section info. 3553aaaa0b2SReid Spencer DestGV->setAlignment(std::max(DestGV->getAlignment(), SrcGV->getAlignment())); 3563aaaa0b2SReid Spencer DestGV->setSection(SrcGV->getSection()); 3573aaaa0b2SReid Spencer DestGV->setVisibility(SrcGV->getVisibility()); 3583aaaa0b2SReid Spencer if (const Function *SrcF = dyn_cast<Function>(SrcGV)) { 3593aaaa0b2SReid Spencer Function *DestF = cast<Function>(DestGV); 3603aaaa0b2SReid Spencer DestF->setCallingConv(SrcF->getCallingConv()); 3613aaaa0b2SReid Spencer } 362361e513dSReid Spencer } 363361e513dSReid Spencer 364fc61de39SChris Lattner /// GetLinkageResult - This analyzes the two global values and determines what 365fc61de39SChris Lattner /// the result will look like in the destination module. In particular, it 366fc61de39SChris Lattner /// computes the resultant linkage type, computes whether the global in the 367fc61de39SChris Lattner /// source should be copied over to the destination (replacing the existing 36831fc4f95SAnton Korobeynikov /// one), and computes whether this linkage is an error or not. It also performs 36931fc4f95SAnton Korobeynikov /// visibility checks: we cannot link together two symbols with different 37031fc4f95SAnton Korobeynikov /// visibilities. 371fc61de39SChris Lattner static bool GetLinkageResult(GlobalValue *Dest, GlobalValue *Src, 372fc61de39SChris Lattner GlobalValue::LinkageTypes <, bool &LinkFromSrc, 373fc61de39SChris Lattner std::string *Err) { 374fc61de39SChris Lattner assert((!Dest || !Src->hasInternalLinkage()) && 375fc61de39SChris Lattner "If Src has internal linkage, Dest shouldn't be set!"); 376fc61de39SChris Lattner if (!Dest) { 377fc61de39SChris Lattner // Linking something to nothing. 378fc61de39SChris Lattner LinkFromSrc = true; 379fc61de39SChris Lattner LT = Src->getLinkage(); 3805301e7c6SReid Spencer } else if (Src->isDeclaration()) { 381fc61de39SChris Lattner // If Src is external or if both Src & Drc are external.. Just link the 382fc61de39SChris Lattner // external globals, we aren't adding anything. 383d61d39ecSAnton Korobeynikov if (Src->hasDLLImportLinkage()) { 38412c94949SAnton Korobeynikov // If one of GVs has DLLImport linkage, result should be dllimport'ed. 3855301e7c6SReid Spencer if (Dest->isDeclaration()) { 386d61d39ecSAnton Korobeynikov LinkFromSrc = true; 387d61d39ecSAnton Korobeynikov LT = Src->getLinkage(); 388d61d39ecSAnton Korobeynikov } 389e06036d2SAndrew Lenharth } else if (Dest->hasExternalWeakLinkage()) { 390e06036d2SAndrew Lenharth //If the Dest is weak, use the source linkage 391e06036d2SAndrew Lenharth LinkFromSrc = true; 392e06036d2SAndrew Lenharth LT = Src->getLinkage(); 393d61d39ecSAnton Korobeynikov } else { 394fc61de39SChris Lattner LinkFromSrc = false; 395fc61de39SChris Lattner LT = Dest->getLinkage(); 396d61d39ecSAnton Korobeynikov } 3975301e7c6SReid Spencer } else if (Dest->isDeclaration() && !Dest->hasDLLImportLinkage()) { 398fc61de39SChris Lattner // If Dest is external but Src is not: 399fc61de39SChris Lattner LinkFromSrc = true; 400fc61de39SChris Lattner LT = Src->getLinkage(); 401fc61de39SChris Lattner } else if (Src->hasAppendingLinkage() || Dest->hasAppendingLinkage()) { 402fc61de39SChris Lattner if (Src->getLinkage() != Dest->getLinkage()) 403fc61de39SChris Lattner return Error(Err, "Linking globals named '" + Src->getName() + 404fc61de39SChris Lattner "': can only link appending global with another appending global!"); 405fc61de39SChris Lattner LinkFromSrc = true; // Special cased. 406fc61de39SChris Lattner LT = Src->getLinkage(); 407fc61de39SChris Lattner } else if (Src->hasWeakLinkage() || Src->hasLinkOnceLinkage()) { 408d3ba7d9bSReid Spencer // At this point we know that Dest has LinkOnce, External*, Weak, or 409d3ba7d9bSReid Spencer // DLL* linkage. 41012c94949SAnton Korobeynikov if ((Dest->hasLinkOnceLinkage() && Src->hasWeakLinkage()) || 41112c94949SAnton Korobeynikov Dest->hasExternalWeakLinkage()) { 412fc61de39SChris Lattner LinkFromSrc = true; 413fc61de39SChris Lattner LT = Src->getLinkage(); 414fc61de39SChris Lattner } else { 415fc61de39SChris Lattner LinkFromSrc = false; 416fc61de39SChris Lattner LT = Dest->getLinkage(); 417fc61de39SChris Lattner } 418fc61de39SChris Lattner } else if (Dest->hasWeakLinkage() || Dest->hasLinkOnceLinkage()) { 41912c94949SAnton Korobeynikov // At this point we know that Src has External* or DLL* linkage. 42012c94949SAnton Korobeynikov if (Src->hasExternalWeakLinkage()) { 42112c94949SAnton Korobeynikov LinkFromSrc = false; 42212c94949SAnton Korobeynikov LT = Dest->getLinkage(); 42312c94949SAnton Korobeynikov } else { 424fc61de39SChris Lattner LinkFromSrc = true; 425fc61de39SChris Lattner LT = GlobalValue::ExternalLinkage; 42612c94949SAnton Korobeynikov } 427fc61de39SChris Lattner } else { 428d61d39ecSAnton Korobeynikov assert((Dest->hasExternalLinkage() || 429d61d39ecSAnton Korobeynikov Dest->hasDLLImportLinkage() || 43012c94949SAnton Korobeynikov Dest->hasDLLExportLinkage() || 43112c94949SAnton Korobeynikov Dest->hasExternalWeakLinkage()) && 432d61d39ecSAnton Korobeynikov (Src->hasExternalLinkage() || 433d61d39ecSAnton Korobeynikov Src->hasDLLImportLinkage() || 43412c94949SAnton Korobeynikov Src->hasDLLExportLinkage() || 43512c94949SAnton Korobeynikov Src->hasExternalWeakLinkage()) && 436fc61de39SChris Lattner "Unexpected linkage type!"); 437fc61de39SChris Lattner return Error(Err, "Linking globals named '" + Src->getName() + 438fc61de39SChris Lattner "': symbol multiply defined!"); 439fc61de39SChris Lattner } 44031fc4f95SAnton Korobeynikov 44131fc4f95SAnton Korobeynikov // Check visibility 44231fc4f95SAnton Korobeynikov if (Dest && Src->getVisibility() != Dest->getVisibility()) 44331fc4f95SAnton Korobeynikov return Error(Err, "Linking globals named '" + Src->getName() + 44431fc4f95SAnton Korobeynikov "': symbols have different visibilities!"); 445fc61de39SChris Lattner return false; 446fc61de39SChris Lattner } 447361e513dSReid Spencer 448361e513dSReid Spencer // LinkGlobals - Loop through the global variables in the src module and merge 449361e513dSReid Spencer // them into the dest module. 450fc61de39SChris Lattner static bool LinkGlobals(Module *Dest, Module *Src, 451361e513dSReid Spencer std::map<const Value*, Value*> &ValueMap, 452361e513dSReid Spencer std::multimap<std::string, GlobalVariable *> &AppendingVars, 453361e513dSReid Spencer std::string *Err) { 454361e513dSReid Spencer // Loop over all of the globals in the src module, mapping them over as we go 45544ab8aefSChris Lattner for (Module::global_iterator I = Src->global_begin(), E = Src->global_end(); 45644ab8aefSChris Lattner I != E; ++I) { 457fc61de39SChris Lattner GlobalVariable *SGV = I; 458361e513dSReid Spencer GlobalVariable *DGV = 0; 459361e513dSReid Spencer // Check to see if may have to link the global. 4603aaaa0b2SReid Spencer if (SGV->hasName() && !SGV->hasInternalLinkage()) { 4613aaaa0b2SReid Spencer DGV = Dest->getGlobalVariable(SGV->getName()); 4623aaaa0b2SReid Spencer if (DGV && DGV->getType() != SGV->getType()) 463fc61de39SChris Lattner // If types don't agree due to opaque types, try to resolve them. 4643aaaa0b2SReid Spencer RecursiveResolveTypes(SGV->getType(), DGV->getType(), 4653aaaa0b2SReid Spencer &Dest->getTypeSymbolTable(), ""); 466361e513dSReid Spencer } 467361e513dSReid Spencer 468fc61de39SChris Lattner if (DGV && DGV->hasInternalLinkage()) 469fc61de39SChris Lattner DGV = 0; 470fc61de39SChris Lattner 471e06036d2SAndrew Lenharth assert(SGV->hasInitializer() || SGV->hasExternalWeakLinkage() || 472d61d39ecSAnton Korobeynikov SGV->hasExternalLinkage() || SGV->hasDLLImportLinkage() && 473361e513dSReid Spencer "Global must either be external or have an initializer!"); 474361e513dSReid Spencer 4751b9633d7SChris Lattner GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage; 4761b9633d7SChris Lattner bool LinkFromSrc = false; 477fc61de39SChris Lattner if (GetLinkageResult(DGV, SGV, NewLinkage, LinkFromSrc, Err)) 478fc61de39SChris Lattner return true; 479361e513dSReid Spencer 480fc61de39SChris Lattner if (!DGV) { 481361e513dSReid Spencer // No linking to be performed, simply create an identical version of the 482361e513dSReid Spencer // symbol over in the dest module... the initializer will be filled in 483361e513dSReid Spencer // later by LinkGlobalInits... 484361e513dSReid Spencer GlobalVariable *NewDGV = 485361e513dSReid Spencer new GlobalVariable(SGV->getType()->getElementType(), 486361e513dSReid Spencer SGV->isConstant(), SGV->getLinkage(), /*init*/0, 487749e4668SLauro Ramos Venancio SGV->getName(), Dest, SGV->isThreadLocal()); 4882dc36535SReid Spencer // Propagate alignment, visibility and section info. 4893aaaa0b2SReid Spencer CopyGVAttributes(NewDGV, SGV); 490dd924e4eSAndrew Lenharth 491361e513dSReid Spencer // If the LLVM runtime renamed the global, but it is an externally visible 492361e513dSReid Spencer // symbol, DGV must be an existing global with internal linkage. Rename 493361e513dSReid Spencer // it. 494361e513dSReid Spencer if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage()) 495361e513dSReid Spencer ForceRenaming(NewDGV, SGV->getName()); 496361e513dSReid Spencer 497361e513dSReid Spencer // Make sure to remember this mapping... 498361e513dSReid Spencer ValueMap.insert(std::make_pair(SGV, NewDGV)); 499361e513dSReid Spencer if (SGV->hasAppendingLinkage()) 500361e513dSReid Spencer // Keep track that this is an appending variable... 501361e513dSReid Spencer AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV)); 502fc61de39SChris Lattner } else if (DGV->hasAppendingLinkage()) { 503361e513dSReid Spencer // No linking is performed yet. Just insert a new copy of the global, and 504361e513dSReid Spencer // keep track of the fact that it is an appending variable in the 505361e513dSReid Spencer // AppendingVars map. The name is cleared out so that no linkage is 506361e513dSReid Spencer // performed. 507361e513dSReid Spencer GlobalVariable *NewDGV = 508361e513dSReid Spencer new GlobalVariable(SGV->getType()->getElementType(), 509361e513dSReid Spencer SGV->isConstant(), SGV->getLinkage(), /*init*/0, 510749e4668SLauro Ramos Venancio "", Dest, SGV->isThreadLocal()); 511361e513dSReid Spencer 5122dc36535SReid Spencer // Propagate alignment, section and visibility info. 5133aaaa0b2SReid Spencer NewDGV->setAlignment(DGV->getAlignment()); 5143aaaa0b2SReid Spencer CopyGVAttributes(NewDGV, SGV); 515dd924e4eSAndrew Lenharth 516361e513dSReid Spencer // Make sure to remember this mapping... 517361e513dSReid Spencer ValueMap.insert(std::make_pair(SGV, NewDGV)); 518361e513dSReid Spencer 519361e513dSReid Spencer // Keep track that this is an appending variable... 520361e513dSReid Spencer AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV)); 521361e513dSReid Spencer } else { 5222dc36535SReid Spencer // Propagate alignment, section, and visibility info. 5233aaaa0b2SReid Spencer CopyGVAttributes(DGV, SGV); 524dd924e4eSAndrew Lenharth 525fc61de39SChris Lattner // Otherwise, perform the mapping as instructed by GetLinkageResult. If 526fc61de39SChris Lattner // the types don't match, and if we are to link from the source, nuke DGV 527fc61de39SChris Lattner // and create a new one of the appropriate type. 528fc61de39SChris Lattner if (SGV->getType() != DGV->getType() && LinkFromSrc) { 529fc61de39SChris Lattner GlobalVariable *NewDGV = 530fc61de39SChris Lattner new GlobalVariable(SGV->getType()->getElementType(), 531fc61de39SChris Lattner DGV->isConstant(), DGV->getLinkage()); 532749e4668SLauro Ramos Venancio NewDGV->setThreadLocal(DGV->isThreadLocal()); 5333aaaa0b2SReid Spencer CopyGVAttributes(NewDGV, DGV); 534fc61de39SChris Lattner Dest->getGlobalList().insert(DGV, NewDGV); 535b341b086SReid Spencer DGV->replaceAllUsesWith( 536b341b086SReid Spencer ConstantExpr::getBitCast(NewDGV, DGV->getType())); 537fc61de39SChris Lattner DGV->eraseFromParent(); 538fc61de39SChris Lattner NewDGV->setName(SGV->getName()); 539fc61de39SChris Lattner DGV = NewDGV; 540fc61de39SChris Lattner } 541fc61de39SChris Lattner 542fc61de39SChris Lattner DGV->setLinkage(NewLinkage); 543fc61de39SChris Lattner 544fc61de39SChris Lattner if (LinkFromSrc) { 545fc61de39SChris Lattner // Inherit const as appropriate 54616277c17SChris Lattner DGV->setConstant(SGV->isConstant()); 547fc61de39SChris Lattner DGV->setInitializer(0); 548fc61de39SChris Lattner } else { 549fc61de39SChris Lattner if (SGV->isConstant() && !DGV->isConstant()) { 5505301e7c6SReid Spencer if (DGV->isDeclaration()) 551fc61de39SChris Lattner DGV->setConstant(true); 552fc61de39SChris Lattner } 553a57c1056SChris Lattner SGV->setLinkage(GlobalValue::ExternalLinkage); 554a57c1056SChris Lattner SGV->setInitializer(0); 555fc61de39SChris Lattner } 556fc61de39SChris Lattner 557b341b086SReid Spencer ValueMap.insert( 558b341b086SReid Spencer std::make_pair(SGV, ConstantExpr::getBitCast(DGV, SGV->getType()))); 559361e513dSReid Spencer } 560361e513dSReid Spencer } 561361e513dSReid Spencer return false; 562361e513dSReid Spencer } 563361e513dSReid Spencer 564b00c9c0dSLauro Ramos Venancio // LinkAlias - Loop through the alias in the src module and link them into the 565b00c9c0dSLauro Ramos Venancio // dest module. 566b00c9c0dSLauro Ramos Venancio static bool LinkAlias(Module *Dest, const Module *Src, std::string *Err) { 567b00c9c0dSLauro Ramos Venancio // Loop over all alias in the src module 568b00c9c0dSLauro Ramos Venancio for (Module::const_alias_iterator I = Src->alias_begin(), 569b00c9c0dSLauro Ramos Venancio E = Src->alias_end(); I != E; ++I) { 570b00c9c0dSLauro Ramos Venancio const GlobalAlias *GA = I; 571b00c9c0dSLauro Ramos Venancio 572b00c9c0dSLauro Ramos Venancio GlobalValue *NewAliased = NULL; 573b00c9c0dSLauro Ramos Venancio const GlobalValue *Aliased = GA->getAliasedGlobal(); 574b00c9c0dSLauro Ramos Venancio if (isa<GlobalVariable>(*Aliased)) 575b00c9c0dSLauro Ramos Venancio NewAliased = Dest->getGlobalVariable(Aliased->getName()); 576b00c9c0dSLauro Ramos Venancio else if (isa<Function>(*Aliased)) 577b00c9c0dSLauro Ramos Venancio NewAliased = Dest->getFunction(Aliased->getName()); 578b00c9c0dSLauro Ramos Venancio // FIXME: we should handle the bitcast alias. 579b00c9c0dSLauro Ramos Venancio assert(NewAliased && "Can't find the aliased GV."); 580b00c9c0dSLauro Ramos Venancio 581*b2c3a670SLauro Ramos Venancio GlobalAlias *NewGA = new GlobalAlias(GA->getType(), GA->getLinkage(), 582*b2c3a670SLauro Ramos Venancio GA->getName(), NewAliased, Dest); 583b00c9c0dSLauro Ramos Venancio CopyGVAttributes(NewGA, GA); 584b00c9c0dSLauro Ramos Venancio } 585b00c9c0dSLauro Ramos Venancio return false; 586b00c9c0dSLauro Ramos Venancio } 587b00c9c0dSLauro Ramos Venancio 588361e513dSReid Spencer 589361e513dSReid Spencer // LinkGlobalInits - Update the initializers in the Dest module now that all 590361e513dSReid Spencer // globals that may be referenced are in Dest. 591361e513dSReid Spencer static bool LinkGlobalInits(Module *Dest, const Module *Src, 592361e513dSReid Spencer std::map<const Value*, Value*> &ValueMap, 593361e513dSReid Spencer std::string *Err) { 594361e513dSReid Spencer 595361e513dSReid Spencer // Loop over all of the globals in the src module, mapping them over as we go 59644ab8aefSChris Lattner for (Module::const_global_iterator I = Src->global_begin(), 59744ab8aefSChris Lattner E = Src->global_end(); I != E; ++I) { 598361e513dSReid Spencer const GlobalVariable *SGV = I; 599361e513dSReid Spencer 600361e513dSReid Spencer if (SGV->hasInitializer()) { // Only process initialized GV's 601361e513dSReid Spencer // Figure out what the initializer looks like in the dest module... 602361e513dSReid Spencer Constant *SInit = 6037391dde1SChris Lattner cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap)); 604361e513dSReid Spencer 605361e513dSReid Spencer GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]); 606361e513dSReid Spencer if (DGV->hasInitializer()) { 607361e513dSReid Spencer if (SGV->hasExternalLinkage()) { 608361e513dSReid Spencer if (DGV->getInitializer() != SInit) 609361e513dSReid Spencer return Error(Err, "Global Variable Collision on '" + 610361e513dSReid Spencer ToStr(SGV->getType(), Src) +"':%"+SGV->getName()+ 611361e513dSReid Spencer " - Global variables have different initializers"); 612361e513dSReid Spencer } else if (DGV->hasLinkOnceLinkage() || DGV->hasWeakLinkage()) { 613361e513dSReid Spencer // Nothing is required, mapped values will take the new global 614361e513dSReid Spencer // automatically. 615361e513dSReid Spencer } else if (SGV->hasLinkOnceLinkage() || SGV->hasWeakLinkage()) { 616361e513dSReid Spencer // Nothing is required, mapped values will take the new global 617361e513dSReid Spencer // automatically. 618361e513dSReid Spencer } else if (DGV->hasAppendingLinkage()) { 619361e513dSReid Spencer assert(0 && "Appending linkage unimplemented!"); 620361e513dSReid Spencer } else { 621361e513dSReid Spencer assert(0 && "Unknown linkage!"); 622361e513dSReid Spencer } 623361e513dSReid Spencer } else { 624361e513dSReid Spencer // Copy the initializer over now... 625361e513dSReid Spencer DGV->setInitializer(SInit); 626361e513dSReid Spencer } 627361e513dSReid Spencer } 628361e513dSReid Spencer } 629361e513dSReid Spencer return false; 630361e513dSReid Spencer } 631361e513dSReid Spencer 632361e513dSReid Spencer // LinkFunctionProtos - Link the functions together between the two modules, 633361e513dSReid Spencer // without doing function bodies... this just adds external function prototypes 634361e513dSReid Spencer // to the Dest function... 635361e513dSReid Spencer // 636361e513dSReid Spencer static bool LinkFunctionProtos(Module *Dest, const Module *Src, 637361e513dSReid Spencer std::map<const Value*, Value*> &ValueMap, 638361e513dSReid Spencer std::string *Err) { 639d3ba7d9bSReid Spencer // Loop over all of the functions in the src module, mapping them over 640361e513dSReid Spencer for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) { 641361e513dSReid Spencer const Function *SF = I; // SrcFunction 642361e513dSReid Spencer Function *DF = 0; 64390246aa7SReid Spencer if (SF->hasName() && !SF->hasInternalLinkage()) { 64490246aa7SReid Spencer // Check to see if may have to link the function. 6453aaaa0b2SReid Spencer DF = Dest->getFunction(SF->getName()); 6463aaaa0b2SReid Spencer if (DF && SF->getType() != DF->getType()) 6473aaaa0b2SReid Spencer // If types don't agree because of opaque, try to resolve them 6483aaaa0b2SReid Spencer RecursiveResolveTypes(SF->getType(), DF->getType(), 6493aaaa0b2SReid Spencer &Dest->getTypeSymbolTable(), ""); 650361e513dSReid Spencer } 651361e513dSReid Spencer 65231fc4f95SAnton Korobeynikov // Check visibility 65331fc4f95SAnton Korobeynikov if (DF && !DF->hasInternalLinkage() && 65431fc4f95SAnton Korobeynikov SF->getVisibility() != DF->getVisibility()) 65531fc4f95SAnton Korobeynikov return Error(Err, "Linking functions named '" + SF->getName() + 65631fc4f95SAnton Korobeynikov "': symbols have different visibilities!"); 65731fc4f95SAnton Korobeynikov 6583aaaa0b2SReid Spencer if (DF && DF->getType() != SF->getType()) { 6593aaaa0b2SReid Spencer if (DF->isDeclaration() && !SF->isDeclaration()) { 6603aaaa0b2SReid Spencer // We have a definition of the same name but different type in the 6613aaaa0b2SReid Spencer // source module. Copy the prototype to the destination and replace 6623aaaa0b2SReid Spencer // uses of the destination's prototype with the new prototype. 6633aaaa0b2SReid Spencer Function *NewDF = new Function(SF->getFunctionType(), SF->getLinkage(), 6643aaaa0b2SReid Spencer SF->getName(), Dest); 6653aaaa0b2SReid Spencer CopyGVAttributes(NewDF, SF); 6663aaaa0b2SReid Spencer 6673aaaa0b2SReid Spencer // Any uses of DF need to change to NewDF, with cast 6683aaaa0b2SReid Spencer DF->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DF->getType())); 6693aaaa0b2SReid Spencer 6703aaaa0b2SReid Spencer // DF will conflict with NewDF because they both had the same. We must 6713aaaa0b2SReid Spencer // erase this now so ForceRenaming doesn't assert because DF might 6723aaaa0b2SReid Spencer // not have internal linkage. 6733aaaa0b2SReid Spencer DF->eraseFromParent(); 6743aaaa0b2SReid Spencer 6753aaaa0b2SReid Spencer // If the symbol table renamed the function, but it is an externally 6763aaaa0b2SReid Spencer // visible symbol, DF must be an existing function with internal 6773aaaa0b2SReid Spencer // linkage. Rename it. 6783aaaa0b2SReid Spencer if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage()) 6793aaaa0b2SReid Spencer ForceRenaming(NewDF, SF->getName()); 6803aaaa0b2SReid Spencer 6813aaaa0b2SReid Spencer // Remember this mapping so uses in the source module get remapped 6823aaaa0b2SReid Spencer // later by RemapOperand. 6833aaaa0b2SReid Spencer ValueMap[SF] = NewDF; 6843aaaa0b2SReid Spencer } else if (SF->isDeclaration()) { 6853aaaa0b2SReid Spencer // We have two functions of the same name but different type and the 6863aaaa0b2SReid Spencer // source is a declaration while the destination is not. Any use of 6873aaaa0b2SReid Spencer // the source must be mapped to the destination, with a cast. 6883aaaa0b2SReid Spencer ValueMap[SF] = ConstantExpr::getBitCast(DF, SF->getType()); 6893aaaa0b2SReid Spencer } else { 6903aaaa0b2SReid Spencer // We have two functions of the same name but different types and they 6913aaaa0b2SReid Spencer // are both definitions. This is an error. 6923aaaa0b2SReid Spencer return Error(Err, "Function '" + DF->getName() + "' defined as both '" + 6933aaaa0b2SReid Spencer ToStr(SF->getFunctionType(), Src) + "' and '" + 6943aaaa0b2SReid Spencer ToStr(DF->getFunctionType(), Dest) + "'"); 6953aaaa0b2SReid Spencer } 6963aaaa0b2SReid Spencer } else if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) { 697361e513dSReid Spencer // Function does not already exist, simply insert an function signature 698361e513dSReid Spencer // identical to SF into the dest module... 699361e513dSReid Spencer Function *NewDF = new Function(SF->getFunctionType(), SF->getLinkage(), 700361e513dSReid Spencer SF->getName(), Dest); 7013aaaa0b2SReid Spencer CopyGVAttributes(NewDF, SF); 702361e513dSReid Spencer 703361e513dSReid Spencer // If the LLVM runtime renamed the function, but it is an externally 704361e513dSReid Spencer // visible symbol, DF must be an existing function with internal linkage. 705361e513dSReid Spencer // Rename it. 706361e513dSReid Spencer if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage()) 707361e513dSReid Spencer ForceRenaming(NewDF, SF->getName()); 708361e513dSReid Spencer 709361e513dSReid Spencer // ... and remember this mapping... 710361e513dSReid Spencer ValueMap.insert(std::make_pair(SF, NewDF)); 7115301e7c6SReid Spencer } else if (SF->isDeclaration()) { 7123aaaa0b2SReid Spencer // If SF is a declaration or if both SF & DF are declarations, just link 7133aaaa0b2SReid Spencer // the declarations, we aren't adding anything. 714d61d39ecSAnton Korobeynikov if (SF->hasDLLImportLinkage()) { 7155301e7c6SReid Spencer if (DF->isDeclaration()) { 716361e513dSReid Spencer ValueMap.insert(std::make_pair(SF, DF)); 717d61d39ecSAnton Korobeynikov DF->setLinkage(SF->getLinkage()); 718d61d39ecSAnton Korobeynikov } 719d61d39ecSAnton Korobeynikov } else { 720d61d39ecSAnton Korobeynikov ValueMap.insert(std::make_pair(SF, DF)); 721d61d39ecSAnton Korobeynikov } 7225301e7c6SReid Spencer } else if (DF->isDeclaration() && !DF->hasDLLImportLinkage()) { 723d61d39ecSAnton Korobeynikov // If DF is external but SF is not... 724361e513dSReid Spencer // Link the external functions, update linkage qualifiers 725361e513dSReid Spencer ValueMap.insert(std::make_pair(SF, DF)); 726361e513dSReid Spencer DF->setLinkage(SF->getLinkage()); 727361e513dSReid Spencer } else if (SF->hasWeakLinkage() || SF->hasLinkOnceLinkage()) { 72812c94949SAnton Korobeynikov // At this point we know that DF has LinkOnce, Weak, or External* linkage. 729361e513dSReid Spencer ValueMap.insert(std::make_pair(SF, DF)); 730361e513dSReid Spencer 731361e513dSReid Spencer // Linkonce+Weak = Weak 73212c94949SAnton Korobeynikov // *+External Weak = * 73312c94949SAnton Korobeynikov if ((DF->hasLinkOnceLinkage() && SF->hasWeakLinkage()) || 73412c94949SAnton Korobeynikov DF->hasExternalWeakLinkage()) 735361e513dSReid Spencer DF->setLinkage(SF->getLinkage()); 736361e513dSReid Spencer } else if (DF->hasWeakLinkage() || DF->hasLinkOnceLinkage()) { 73712c94949SAnton Korobeynikov // At this point we know that SF has LinkOnce or External* linkage. 738361e513dSReid Spencer ValueMap.insert(std::make_pair(SF, DF)); 73912c94949SAnton Korobeynikov if (!SF->hasLinkOnceLinkage() && !SF->hasExternalWeakLinkage()) 74012c94949SAnton Korobeynikov // Don't inherit linkonce & external weak linkage 741361e513dSReid Spencer DF->setLinkage(SF->getLinkage()); 742361e513dSReid Spencer } else if (SF->getLinkage() != DF->getLinkage()) { 743361e513dSReid Spencer return Error(Err, "Functions named '" + SF->getName() + 744361e513dSReid Spencer "' have different linkage specifiers!"); 745361e513dSReid Spencer } else if (SF->hasExternalLinkage()) { 7463aaaa0b2SReid Spencer // The function is defined identically in both modules!! 747361e513dSReid Spencer return Error(Err, "Function '" + 748361e513dSReid Spencer ToStr(SF->getFunctionType(), Src) + "':\"" + 749361e513dSReid Spencer SF->getName() + "\" - Function is already defined!"); 750361e513dSReid Spencer } else { 751361e513dSReid Spencer assert(0 && "Unknown linkage configuration found!"); 752361e513dSReid Spencer } 753361e513dSReid Spencer } 754361e513dSReid Spencer return false; 755361e513dSReid Spencer } 756361e513dSReid Spencer 757361e513dSReid Spencer // LinkFunctionBody - Copy the source function over into the dest function and 758361e513dSReid Spencer // fix up references to values. At this point we know that Dest is an external 759361e513dSReid Spencer // function, and that Src is not. 7602f0557d0SChris Lattner static bool LinkFunctionBody(Function *Dest, Function *Src, 7613aaaa0b2SReid Spencer std::map<const Value*, Value*> &ValueMap, 762361e513dSReid Spencer std::string *Err) { 7635301e7c6SReid Spencer assert(Src && Dest && Dest->isDeclaration() && !Src->isDeclaration()); 764361e513dSReid Spencer 7657391dde1SChris Lattner // Go through and convert function arguments over, remembering the mapping. 766531f9e92SChris Lattner Function::arg_iterator DI = Dest->arg_begin(); 767531f9e92SChris Lattner for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); 768361e513dSReid Spencer I != E; ++I, ++DI) { 769361e513dSReid Spencer DI->setName(I->getName()); // Copy the name information over... 770361e513dSReid Spencer 771361e513dSReid Spencer // Add a mapping to our local map 7723aaaa0b2SReid Spencer ValueMap.insert(std::make_pair(I, DI)); 773361e513dSReid Spencer } 774361e513dSReid Spencer 7752f0557d0SChris Lattner // Splice the body of the source function into the dest function. 7762f0557d0SChris Lattner Dest->getBasicBlockList().splice(Dest->end(), Src->getBasicBlockList()); 777361e513dSReid Spencer 778361e513dSReid Spencer // At this point, all of the instructions and values of the function are now 779361e513dSReid Spencer // copied over. The only problem is that they are still referencing values in 780361e513dSReid Spencer // the Source function as operands. Loop through all of the operands of the 781361e513dSReid Spencer // functions and patch them up to point to the local versions... 782361e513dSReid Spencer // 783361e513dSReid Spencer for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB) 784361e513dSReid Spencer for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 785361e513dSReid Spencer for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end(); 786361e513dSReid Spencer OI != OE; ++OI) 7872f0557d0SChris Lattner if (!isa<Instruction>(*OI) && !isa<BasicBlock>(*OI)) 7883aaaa0b2SReid Spencer *OI = RemapOperand(*OI, ValueMap); 7897391dde1SChris Lattner 7907391dde1SChris Lattner // There is no need to map the arguments anymore. 79144ab8aefSChris Lattner for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); 79244ab8aefSChris Lattner I != E; ++I) 7933aaaa0b2SReid Spencer ValueMap.erase(I); 794361e513dSReid Spencer 795361e513dSReid Spencer return false; 796361e513dSReid Spencer } 797361e513dSReid Spencer 798361e513dSReid Spencer 799361e513dSReid Spencer // LinkFunctionBodies - Link in the function bodies that are defined in the 800361e513dSReid Spencer // source module into the DestModule. This consists basically of copying the 801361e513dSReid Spencer // function over and fixing up references to values. 8022f0557d0SChris Lattner static bool LinkFunctionBodies(Module *Dest, Module *Src, 803361e513dSReid Spencer std::map<const Value*, Value*> &ValueMap, 804361e513dSReid Spencer std::string *Err) { 805361e513dSReid Spencer 80690246aa7SReid Spencer // Loop over all of the functions in the src module, mapping them over as we 80790246aa7SReid Spencer // go 8082f0557d0SChris Lattner for (Module::iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF) { 8095301e7c6SReid Spencer if (!SF->isDeclaration()) { // No body if function is external 810361e513dSReid Spencer Function *DF = cast<Function>(ValueMap[SF]); // Destination function 811361e513dSReid Spencer 812361e513dSReid Spencer // DF not external SF external? 8133aaaa0b2SReid Spencer if (DF->isDeclaration()) 814361e513dSReid Spencer // Only provide the function body if there isn't one already. 815361e513dSReid Spencer if (LinkFunctionBody(DF, SF, ValueMap, Err)) 816361e513dSReid Spencer return true; 81790246aa7SReid Spencer } 818361e513dSReid Spencer } 819361e513dSReid Spencer return false; 820361e513dSReid Spencer } 821361e513dSReid Spencer 822361e513dSReid Spencer // LinkAppendingVars - If there were any appending global variables, link them 823361e513dSReid Spencer // together now. Return true on error. 824361e513dSReid Spencer static bool LinkAppendingVars(Module *M, 825361e513dSReid Spencer std::multimap<std::string, GlobalVariable *> &AppendingVars, 826361e513dSReid Spencer std::string *ErrorMsg) { 827361e513dSReid Spencer if (AppendingVars.empty()) return false; // Nothing to do. 828361e513dSReid Spencer 829361e513dSReid Spencer // Loop over the multimap of appending vars, processing any variables with the 830361e513dSReid Spencer // same name, forming a new appending global variable with both of the 831361e513dSReid Spencer // initializers merged together, then rewrite references to the old variables 832361e513dSReid Spencer // and delete them. 833361e513dSReid Spencer std::vector<Constant*> Inits; 834361e513dSReid Spencer while (AppendingVars.size() > 1) { 835361e513dSReid Spencer // Get the first two elements in the map... 836361e513dSReid Spencer std::multimap<std::string, 837361e513dSReid Spencer GlobalVariable*>::iterator Second = AppendingVars.begin(), First=Second++; 838361e513dSReid Spencer 839361e513dSReid Spencer // If the first two elements are for different names, there is no pair... 840361e513dSReid Spencer // Otherwise there is a pair, so link them together... 841361e513dSReid Spencer if (First->first == Second->first) { 842361e513dSReid Spencer GlobalVariable *G1 = First->second, *G2 = Second->second; 843361e513dSReid Spencer const ArrayType *T1 = cast<ArrayType>(G1->getType()->getElementType()); 844361e513dSReid Spencer const ArrayType *T2 = cast<ArrayType>(G2->getType()->getElementType()); 845361e513dSReid Spencer 846361e513dSReid Spencer // Check to see that they two arrays agree on type... 847361e513dSReid Spencer if (T1->getElementType() != T2->getElementType()) 848361e513dSReid Spencer return Error(ErrorMsg, 849361e513dSReid Spencer "Appending variables with different element types need to be linked!"); 850361e513dSReid Spencer if (G1->isConstant() != G2->isConstant()) 851361e513dSReid Spencer return Error(ErrorMsg, 852361e513dSReid Spencer "Appending variables linked with different const'ness!"); 853361e513dSReid Spencer 85485703e37SLauro Ramos Venancio if (G1->getAlignment() != G2->getAlignment()) 85585703e37SLauro Ramos Venancio return Error(ErrorMsg, 85685703e37SLauro Ramos Venancio "Appending variables with different alignment need to be linked!"); 85785703e37SLauro Ramos Venancio 85885703e37SLauro Ramos Venancio if (G1->getVisibility() != G2->getVisibility()) 85985703e37SLauro Ramos Venancio return Error(ErrorMsg, 86085703e37SLauro Ramos Venancio "Appending variables with different visibility need to be linked!"); 86185703e37SLauro Ramos Venancio 86285703e37SLauro Ramos Venancio if (G1->getSection() != G2->getSection()) 86385703e37SLauro Ramos Venancio return Error(ErrorMsg, 86485703e37SLauro Ramos Venancio "Appending variables with different section name need to be linked!"); 86585703e37SLauro Ramos Venancio 866361e513dSReid Spencer unsigned NewSize = T1->getNumElements() + T2->getNumElements(); 867361e513dSReid Spencer ArrayType *NewType = ArrayType::get(T1->getElementType(), NewSize); 868361e513dSReid Spencer 869d490d4f0SChris Lattner G1->setName(""); // Clear G1's name in case of a conflict! 870d490d4f0SChris Lattner 871361e513dSReid Spencer // Create the new global variable... 872361e513dSReid Spencer GlobalVariable *NG = 873361e513dSReid Spencer new GlobalVariable(NewType, G1->isConstant(), G1->getLinkage(), 874749e4668SLauro Ramos Venancio /*init*/0, First->first, M, G1->isThreadLocal()); 875361e513dSReid Spencer 87685703e37SLauro Ramos Venancio // Propagate alignment, visibility and section info. 87785703e37SLauro Ramos Venancio CopyGVAttributes(NG, G1); 87885703e37SLauro Ramos Venancio 879361e513dSReid Spencer // Merge the initializer... 880361e513dSReid Spencer Inits.reserve(NewSize); 881361e513dSReid Spencer if (ConstantArray *I = dyn_cast<ConstantArray>(G1->getInitializer())) { 882361e513dSReid Spencer for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i) 883361e513dSReid Spencer Inits.push_back(I->getOperand(i)); 884361e513dSReid Spencer } else { 885361e513dSReid Spencer assert(isa<ConstantAggregateZero>(G1->getInitializer())); 886361e513dSReid Spencer Constant *CV = Constant::getNullValue(T1->getElementType()); 887361e513dSReid Spencer for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i) 888361e513dSReid Spencer Inits.push_back(CV); 889361e513dSReid Spencer } 890361e513dSReid Spencer if (ConstantArray *I = dyn_cast<ConstantArray>(G2->getInitializer())) { 891361e513dSReid Spencer for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i) 892361e513dSReid Spencer Inits.push_back(I->getOperand(i)); 893361e513dSReid Spencer } else { 894361e513dSReid Spencer assert(isa<ConstantAggregateZero>(G2->getInitializer())); 895361e513dSReid Spencer Constant *CV = Constant::getNullValue(T2->getElementType()); 896361e513dSReid Spencer for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i) 897361e513dSReid Spencer Inits.push_back(CV); 898361e513dSReid Spencer } 899361e513dSReid Spencer NG->setInitializer(ConstantArray::get(NewType, Inits)); 900361e513dSReid Spencer Inits.clear(); 901361e513dSReid Spencer 902361e513dSReid Spencer // Replace any uses of the two global variables with uses of the new 903361e513dSReid Spencer // global... 904361e513dSReid Spencer 905361e513dSReid Spencer // FIXME: This should rewrite simple/straight-forward uses such as 906361e513dSReid Spencer // getelementptr instructions to not use the Cast! 907b341b086SReid Spencer G1->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G1->getType())); 908b341b086SReid Spencer G2->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G2->getType())); 909361e513dSReid Spencer 910361e513dSReid Spencer // Remove the two globals from the module now... 911361e513dSReid Spencer M->getGlobalList().erase(G1); 912361e513dSReid Spencer M->getGlobalList().erase(G2); 913361e513dSReid Spencer 914361e513dSReid Spencer // Put the new global into the AppendingVars map so that we can handle 915361e513dSReid Spencer // linking of more than two vars... 916361e513dSReid Spencer Second->second = NG; 917361e513dSReid Spencer } 918361e513dSReid Spencer AppendingVars.erase(First); 919361e513dSReid Spencer } 920361e513dSReid Spencer 921361e513dSReid Spencer return false; 922361e513dSReid Spencer } 923361e513dSReid Spencer 924361e513dSReid Spencer 925361e513dSReid Spencer // LinkModules - This function links two modules together, with the resulting 926361e513dSReid Spencer // left module modified to be the composite of the two input modules. If an 927361e513dSReid Spencer // error occurs, true is returned and ErrorMsg (if not null) is set to indicate 928361e513dSReid Spencer // the problem. Upon failure, the Dest module could be in a modified state, and 929361e513dSReid Spencer // shouldn't be relied on to be consistent. 930c4e31534SReid Spencer bool 931c4e31534SReid Spencer Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) { 932361e513dSReid Spencer assert(Dest != 0 && "Invalid Destination module"); 933361e513dSReid Spencer assert(Src != 0 && "Invalid Source Module"); 934361e513dSReid Spencer 9357f3bd82aSChris Lattner if (Dest->getDataLayout().empty()) { 9367f3bd82aSChris Lattner if (!Src->getDataLayout().empty()) { 93778bddc38SChris Lattner Dest->setDataLayout(Src->getDataLayout()); 9387f3bd82aSChris Lattner } else { 9393ac38e99SReid Spencer std::string DataLayout; 9403ac38e99SReid Spencer 941361e513dSReid Spencer if (Dest->getEndianness() == Module::AnyEndianness) 9423ac38e99SReid Spencer if (Src->getEndianness() == Module::BigEndian) 9433ac38e99SReid Spencer DataLayout.append("E"); 9443ac38e99SReid Spencer else if (Src->getEndianness() == Module::LittleEndian) 9453ac38e99SReid Spencer DataLayout.append("e"); 946361e513dSReid Spencer if (Dest->getPointerSize() == Module::AnyPointerSize) 9473ac38e99SReid Spencer if (Src->getPointerSize() == Module::Pointer64) 9483ac38e99SReid Spencer DataLayout.append(DataLayout.length() == 0 ? "p:64:64" : "-p:64:64"); 9493ac38e99SReid Spencer else if (Src->getPointerSize() == Module::Pointer32) 9503ac38e99SReid Spencer DataLayout.append(DataLayout.length() == 0 ? "p:32:32" : "-p:32:32"); 9513ac38e99SReid Spencer Dest->setDataLayout(DataLayout); 9527f3bd82aSChris Lattner } 9537f3bd82aSChris Lattner } 954361e513dSReid Spencer 955d3ba7d9bSReid Spencer // COpy the target triple from the source to dest if the dest's is empty 9567f3bd82aSChris Lattner if (Dest->getTargetTriple().empty() && !Src->getTargetTriple().empty()) 9577f3bd82aSChris Lattner Dest->setTargetTriple(Src->getTargetTriple()); 9587f3bd82aSChris Lattner 9597f3bd82aSChris Lattner if (!Src->getDataLayout().empty() && !Dest->getDataLayout().empty() && 9607f3bd82aSChris Lattner Src->getDataLayout() != Dest->getDataLayout()) 9613ac38e99SReid Spencer cerr << "WARNING: Linking two modules of different data layouts!\n"; 962b7f5916eSChris Lattner if (!Src->getTargetTriple().empty() && 963b7f5916eSChris Lattner Dest->getTargetTriple() != Src->getTargetTriple()) 964f3baad3eSBill Wendling cerr << "WARNING: Linking two modules of different target triples!\n"; 965361e513dSReid Spencer 966d3ba7d9bSReid Spencer // Append the module inline asm string 9678ebd2164SChris Lattner if (!Src->getModuleInlineAsm().empty()) { 9688ebd2164SChris Lattner if (Dest->getModuleInlineAsm().empty()) 9698ebd2164SChris Lattner Dest->setModuleInlineAsm(Src->getModuleInlineAsm()); 970dd2d3fafSChris Lattner else 9718ebd2164SChris Lattner Dest->setModuleInlineAsm(Dest->getModuleInlineAsm()+"\n"+ 9728ebd2164SChris Lattner Src->getModuleInlineAsm()); 973dd2d3fafSChris Lattner } 974dd2d3fafSChris Lattner 975361e513dSReid Spencer // Update the destination module's dependent libraries list with the libraries 976361e513dSReid Spencer // from the source module. There's no opportunity for duplicates here as the 977361e513dSReid Spencer // Module ensures that duplicate insertions are discarded. 978361e513dSReid Spencer Module::lib_iterator SI = Src->lib_begin(); 979361e513dSReid Spencer Module::lib_iterator SE = Src->lib_end(); 980361e513dSReid Spencer while ( SI != SE ) { 981361e513dSReid Spencer Dest->addLibrary(*SI); 982361e513dSReid Spencer ++SI; 983361e513dSReid Spencer } 984361e513dSReid Spencer 985361e513dSReid Spencer // LinkTypes - Go through the symbol table of the Src module and see if any 986361e513dSReid Spencer // types are named in the src module that are not named in the Dst module. 987361e513dSReid Spencer // Make sure there are no type name conflicts. 988d3ba7d9bSReid Spencer if (LinkTypes(Dest, Src, ErrorMsg)) 989d3ba7d9bSReid Spencer return true; 990361e513dSReid Spencer 991361e513dSReid Spencer // ValueMap - Mapping of values from what they used to be in Src, to what they 992361e513dSReid Spencer // are now in Dest. 993361e513dSReid Spencer std::map<const Value*, Value*> ValueMap; 994361e513dSReid Spencer 995361e513dSReid Spencer // AppendingVars - Keep track of global variables in the destination module 996361e513dSReid Spencer // with appending linkage. After the module is linked together, they are 997361e513dSReid Spencer // appended and the module is rewritten. 998361e513dSReid Spencer std::multimap<std::string, GlobalVariable *> AppendingVars; 99944ab8aefSChris Lattner for (Module::global_iterator I = Dest->global_begin(), E = Dest->global_end(); 100044ab8aefSChris Lattner I != E; ++I) { 1001361e513dSReid Spencer // Add all of the appending globals already in the Dest module to 1002361e513dSReid Spencer // AppendingVars. 1003361e513dSReid Spencer if (I->hasAppendingLinkage()) 1004361e513dSReid Spencer AppendingVars.insert(std::make_pair(I->getName(), I)); 1005361e513dSReid Spencer } 1006361e513dSReid Spencer 1007361e513dSReid Spencer // Insert all of the globals in src into the Dest module... without linking 1008361e513dSReid Spencer // initializers (which could refer to functions not yet mapped over). 10093aaaa0b2SReid Spencer if (LinkGlobals(Dest, Src, ValueMap, AppendingVars, ErrorMsg)) 1010361e513dSReid Spencer return true; 1011361e513dSReid Spencer 1012361e513dSReid Spencer // Link the functions together between the two modules, without doing function 1013361e513dSReid Spencer // bodies... this just adds external function prototypes to the Dest 1014361e513dSReid Spencer // function... We do this so that when we begin processing function bodies, 1015361e513dSReid Spencer // all of the global values that may be referenced are available in our 1016361e513dSReid Spencer // ValueMap. 10173aaaa0b2SReid Spencer if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) 1018361e513dSReid Spencer return true; 1019361e513dSReid Spencer 1020361e513dSReid Spencer // Update the initializers in the Dest module now that all globals that may 1021361e513dSReid Spencer // be referenced are in Dest. 1022361e513dSReid Spencer if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true; 1023361e513dSReid Spencer 1024361e513dSReid Spencer // Link in the function bodies that are defined in the source module into the 1025361e513dSReid Spencer // DestModule. This consists basically of copying the function over and 1026361e513dSReid Spencer // fixing up references to values. 1027361e513dSReid Spencer if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true; 1028361e513dSReid Spencer 1029361e513dSReid Spencer // If there were any appending global variables, link them together now. 1030361e513dSReid Spencer if (LinkAppendingVars(Dest, AppendingVars, ErrorMsg)) return true; 1031361e513dSReid Spencer 1032b00c9c0dSLauro Ramos Venancio // If there were any alias, link them now. 1033b00c9c0dSLauro Ramos Venancio if (LinkAlias(Dest, Src, ErrorMsg)) return true; 1034b00c9c0dSLauro Ramos Venancio 1035361e513dSReid Spencer // If the source library's module id is in the dependent library list of the 1036361e513dSReid Spencer // destination library, remove it since that module is now linked in. 1037361e513dSReid Spencer sys::Path modId; 1038c9c0473fSReid Spencer modId.set(Src->getModuleIdentifier()); 1039361e513dSReid Spencer if (!modId.isEmpty()) 1040361e513dSReid Spencer Dest->removeLibrary(modId.getBasename()); 1041361e513dSReid Spencer 1042361e513dSReid Spencer return false; 1043361e513dSReid Spencer } 1044361e513dSReid Spencer 1045361e513dSReid Spencer // vim: sw=2 1046