Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LLVM module linker. |
| 11 | // |
| 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 |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "llvm/Linker.h" |
| 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/TypeSymbolTable.h" |
| 24 | #include "llvm/ValueSymbolTable.h" |
| 25 | #include "llvm/Instructions.h" |
| 26 | #include "llvm/Assembly/Writer.h" |
| 27 | #include "llvm/Support/Streams.h" |
| 28 | #include "llvm/System/Path.h" |
| 29 | #include <sstream> |
| 30 | using namespace llvm; |
| 31 | |
| 32 | // Error - Simple wrapper function to conditionally assign to E and return true. |
| 33 | // This just makes error return conditions a little bit simpler... |
| 34 | static inline bool Error(std::string *E, const std::string &Message) { |
| 35 | if (E) *E = Message; |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | // ToStr - Simple wrapper function to convert a type to a string. |
| 40 | static std::string ToStr(const Type *Ty, const Module *M) { |
| 41 | std::ostringstream OS; |
| 42 | WriteTypeSymbolic(OS, Ty, M); |
| 43 | return OS.str(); |
| 44 | } |
| 45 | |
| 46 | // |
| 47 | // Function: ResolveTypes() |
| 48 | // |
| 49 | // Description: |
| 50 | // Attempt to link the two specified types together. |
| 51 | // |
| 52 | // Inputs: |
| 53 | // DestTy - The type to which we wish to resolve. |
| 54 | // SrcTy - The original type which we want to resolve. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 55 | // |
| 56 | // Outputs: |
| 57 | // DestST - The symbol table in which the new type should be placed. |
| 58 | // |
| 59 | // Return value: |
| 60 | // true - There is an error and the types cannot yet be linked. |
| 61 | // false - No errors. |
| 62 | // |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 63 | static bool ResolveTypes(const Type *DestTy, const Type *SrcTy) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 64 | if (DestTy == SrcTy) return false; // If already equal, noop |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 65 | assert(DestTy && SrcTy && "Can't handle null types"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 67 | if (const OpaqueType *OT = dyn_cast<OpaqueType>(DestTy)) { |
| 68 | // Type _is_ in module, just opaque... |
| 69 | const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(SrcTy); |
| 70 | } else if (const OpaqueType *OT = dyn_cast<OpaqueType>(SrcTy)) { |
| 71 | const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(DestTy); |
| 72 | } else { |
| 73 | return true; // Cannot link types... not-equal and neither is opaque. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | static const FunctionType *getFT(const PATypeHolder &TH) { |
| 79 | return cast<FunctionType>(TH.get()); |
| 80 | } |
| 81 | static const StructType *getST(const PATypeHolder &TH) { |
| 82 | return cast<StructType>(TH.get()); |
| 83 | } |
| 84 | |
| 85 | // RecursiveResolveTypes - This is just like ResolveTypes, except that it |
| 86 | // recurses down into derived types, merging the used types if the parent types |
| 87 | // are compatible. |
| 88 | static bool RecursiveResolveTypesI(const PATypeHolder &DestTy, |
| 89 | const PATypeHolder &SrcTy, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 90 | std::vector<std::pair<PATypeHolder, PATypeHolder> > &Pointers) { |
| 91 | const Type *SrcTyT = SrcTy.get(); |
| 92 | const Type *DestTyT = DestTy.get(); |
| 93 | if (DestTyT == SrcTyT) return false; // If already equal, noop |
| 94 | |
| 95 | // If we found our opaque type, resolve it now! |
| 96 | if (isa<OpaqueType>(DestTyT) || isa<OpaqueType>(SrcTyT)) |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 97 | return ResolveTypes(DestTyT, SrcTyT); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 98 | |
| 99 | // Two types cannot be resolved together if they are of different primitive |
| 100 | // type. For example, we cannot resolve an int to a float. |
| 101 | if (DestTyT->getTypeID() != SrcTyT->getTypeID()) return true; |
| 102 | |
| 103 | // Otherwise, resolve the used type used by this derived type... |
| 104 | switch (DestTyT->getTypeID()) { |
Chris Lattner | 6b9bdb7 | 2008-06-16 18:27:53 +0000 | [diff] [blame^] | 105 | default: |
| 106 | return true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 107 | case Type::FunctionTyID: { |
| 108 | if (cast<FunctionType>(DestTyT)->isVarArg() != |
| 109 | cast<FunctionType>(SrcTyT)->isVarArg() || |
| 110 | cast<FunctionType>(DestTyT)->getNumContainedTypes() != |
| 111 | cast<FunctionType>(SrcTyT)->getNumContainedTypes()) |
| 112 | return true; |
| 113 | for (unsigned i = 0, e = getFT(DestTy)->getNumContainedTypes(); i != e; ++i) |
| 114 | if (RecursiveResolveTypesI(getFT(DestTy)->getContainedType(i), |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 115 | getFT(SrcTy)->getContainedType(i), Pointers)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 116 | return true; |
| 117 | return false; |
| 118 | } |
| 119 | case Type::StructTyID: { |
| 120 | if (getST(DestTy)->getNumContainedTypes() != |
Chris Lattner | 6b9bdb7 | 2008-06-16 18:27:53 +0000 | [diff] [blame^] | 121 | getST(SrcTy)->getNumContainedTypes()) |
| 122 | return true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 123 | for (unsigned i = 0, e = getST(DestTy)->getNumContainedTypes(); i != e; ++i) |
| 124 | if (RecursiveResolveTypesI(getST(DestTy)->getContainedType(i), |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 125 | getST(SrcTy)->getContainedType(i), Pointers)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 126 | return true; |
| 127 | return false; |
| 128 | } |
| 129 | case Type::ArrayTyID: { |
| 130 | const ArrayType *DAT = cast<ArrayType>(DestTy.get()); |
| 131 | const ArrayType *SAT = cast<ArrayType>(SrcTy.get()); |
| 132 | if (DAT->getNumElements() != SAT->getNumElements()) return true; |
| 133 | return RecursiveResolveTypesI(DAT->getElementType(), SAT->getElementType(), |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 134 | Pointers); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 135 | } |
Chris Lattner | 6b9bdb7 | 2008-06-16 18:27:53 +0000 | [diff] [blame^] | 136 | case Type::VectorTyID: { |
| 137 | const VectorType *DVT = cast<VectorType>(DestTy.get()); |
| 138 | const VectorType *SVT = cast<VectorType>(SrcTy.get()); |
| 139 | if (DVT->getNumElements() != SVT->getNumElements()) return true; |
| 140 | return RecursiveResolveTypesI(DVT->getElementType(), SVT->getElementType(), |
| 141 | Pointers); |
| 142 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 143 | case Type::PointerTyID: { |
| 144 | // If this is a pointer type, check to see if we have already seen it. If |
| 145 | // so, we are in a recursive branch. Cut off the search now. We cannot use |
| 146 | // an associative container for this search, because the type pointers (keys |
| 147 | // in the container) change whenever types get resolved... |
| 148 | for (unsigned i = 0, e = Pointers.size(); i != e; ++i) |
| 149 | if (Pointers[i].first == DestTy) |
| 150 | return Pointers[i].second != SrcTy; |
| 151 | |
| 152 | // Otherwise, add the current pointers to the vector to stop recursion on |
| 153 | // this pair. |
| 154 | Pointers.push_back(std::make_pair(DestTyT, SrcTyT)); |
| 155 | bool Result = |
| 156 | RecursiveResolveTypesI(cast<PointerType>(DestTy.get())->getElementType(), |
| 157 | cast<PointerType>(SrcTy.get())->getElementType(), |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 158 | Pointers); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 159 | Pointers.pop_back(); |
| 160 | return Result; |
| 161 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | static bool RecursiveResolveTypes(const PATypeHolder &DestTy, |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 166 | const PATypeHolder &SrcTy) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 167 | std::vector<std::pair<PATypeHolder, PATypeHolder> > PointerTypes; |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 168 | return RecursiveResolveTypesI(DestTy, SrcTy, PointerTypes); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
| 172 | // LinkTypes - Go through the symbol table of the Src module and see if any |
| 173 | // types are named in the src module that are not named in the Dst module. |
| 174 | // Make sure there are no type name conflicts. |
| 175 | static bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) { |
| 176 | TypeSymbolTable *DestST = &Dest->getTypeSymbolTable(); |
| 177 | const TypeSymbolTable *SrcST = &Src->getTypeSymbolTable(); |
| 178 | |
| 179 | // Look for a type plane for Type's... |
| 180 | TypeSymbolTable::const_iterator TI = SrcST->begin(); |
| 181 | TypeSymbolTable::const_iterator TE = SrcST->end(); |
| 182 | if (TI == TE) return false; // No named types, do nothing. |
| 183 | |
| 184 | // Some types cannot be resolved immediately because they depend on other |
| 185 | // types being resolved to each other first. This contains a list of types we |
| 186 | // are waiting to recheck. |
| 187 | std::vector<std::string> DelayedTypesToResolve; |
| 188 | |
| 189 | for ( ; TI != TE; ++TI ) { |
| 190 | const std::string &Name = TI->first; |
| 191 | const Type *RHS = TI->second; |
| 192 | |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 193 | // Check to see if this type name is already in the dest module. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 194 | Type *Entry = DestST->lookup(Name); |
| 195 | |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 196 | // If the name is just in the source module, bring it over to the dest. |
| 197 | if (Entry == 0) { |
| 198 | if (!Name.empty()) |
| 199 | DestST->insert(Name, const_cast<Type*>(RHS)); |
| 200 | } else if (ResolveTypes(Entry, RHS)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 201 | // They look different, save the types 'till later to resolve. |
| 202 | DelayedTypesToResolve.push_back(Name); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Iteratively resolve types while we can... |
| 207 | while (!DelayedTypesToResolve.empty()) { |
| 208 | // Loop over all of the types, attempting to resolve them if possible... |
| 209 | unsigned OldSize = DelayedTypesToResolve.size(); |
| 210 | |
| 211 | // Try direct resolution by name... |
| 212 | for (unsigned i = 0; i != DelayedTypesToResolve.size(); ++i) { |
| 213 | const std::string &Name = DelayedTypesToResolve[i]; |
| 214 | Type *T1 = SrcST->lookup(Name); |
| 215 | Type *T2 = DestST->lookup(Name); |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 216 | if (!ResolveTypes(T2, T1)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 217 | // We are making progress! |
| 218 | DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i); |
| 219 | --i; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // Did we not eliminate any types? |
| 224 | if (DelayedTypesToResolve.size() == OldSize) { |
| 225 | // Attempt to resolve subelements of types. This allows us to merge these |
| 226 | // two types: { int* } and { opaque* } |
| 227 | for (unsigned i = 0, e = DelayedTypesToResolve.size(); i != e; ++i) { |
| 228 | const std::string &Name = DelayedTypesToResolve[i]; |
| 229 | PATypeHolder T1(SrcST->lookup(Name)); |
| 230 | PATypeHolder T2(DestST->lookup(Name)); |
| 231 | |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 232 | if (!RecursiveResolveTypes(T2, T1)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 233 | // We are making progress! |
| 234 | DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i); |
| 235 | |
| 236 | // Go back to the main loop, perhaps we can resolve directly by name |
| 237 | // now... |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // If we STILL cannot resolve the types, then there is something wrong. |
| 243 | if (DelayedTypesToResolve.size() == OldSize) { |
| 244 | // Remove the symbol name from the destination. |
| 245 | DelayedTypesToResolve.pop_back(); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | static void PrintMap(const std::map<const Value*, Value*> &M) { |
| 255 | for (std::map<const Value*, Value*>::const_iterator I = M.begin(), E =M.end(); |
| 256 | I != E; ++I) { |
| 257 | cerr << " Fr: " << (void*)I->first << " "; |
| 258 | I->first->dump(); |
| 259 | cerr << " To: " << (void*)I->second << " "; |
| 260 | I->second->dump(); |
| 261 | cerr << "\n"; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
| 266 | // RemapOperand - Use ValueMap to convert constants from one module to another. |
| 267 | static Value *RemapOperand(const Value *In, |
| 268 | std::map<const Value*, Value*> &ValueMap) { |
| 269 | std::map<const Value*,Value*>::const_iterator I = ValueMap.find(In); |
| 270 | if (I != ValueMap.end()) |
| 271 | return I->second; |
| 272 | |
| 273 | // Check to see if it's a constant that we are interested in transforming. |
| 274 | Value *Result = 0; |
| 275 | if (const Constant *CPV = dyn_cast<Constant>(In)) { |
| 276 | if ((!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) || |
| 277 | isa<ConstantInt>(CPV) || isa<ConstantAggregateZero>(CPV)) |
| 278 | return const_cast<Constant*>(CPV); // Simple constants stay identical. |
| 279 | |
| 280 | if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) { |
| 281 | std::vector<Constant*> Operands(CPA->getNumOperands()); |
| 282 | for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) |
| 283 | Operands[i] =cast<Constant>(RemapOperand(CPA->getOperand(i), ValueMap)); |
| 284 | Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands); |
| 285 | } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) { |
| 286 | std::vector<Constant*> Operands(CPS->getNumOperands()); |
| 287 | for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) |
| 288 | Operands[i] =cast<Constant>(RemapOperand(CPS->getOperand(i), ValueMap)); |
| 289 | Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands); |
| 290 | } else if (isa<ConstantPointerNull>(CPV) || isa<UndefValue>(CPV)) { |
| 291 | Result = const_cast<Constant*>(CPV); |
| 292 | } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CPV)) { |
| 293 | std::vector<Constant*> Operands(CP->getNumOperands()); |
| 294 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 295 | Operands[i] = cast<Constant>(RemapOperand(CP->getOperand(i), ValueMap)); |
| 296 | Result = ConstantVector::get(Operands); |
| 297 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) { |
| 298 | std::vector<Constant*> Ops; |
| 299 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) |
| 300 | Ops.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),ValueMap))); |
| 301 | Result = CE->getWithOperands(Ops); |
| 302 | } else if (isa<GlobalValue>(CPV)) { |
| 303 | assert(0 && "Unmapped global?"); |
| 304 | } else { |
| 305 | assert(0 && "Unknown type of derived type constant value!"); |
| 306 | } |
| 307 | } else if (isa<InlineAsm>(In)) { |
| 308 | Result = const_cast<Value*>(In); |
| 309 | } |
| 310 | |
| 311 | // Cache the mapping in our local map structure |
| 312 | if (Result) { |
Anton Korobeynikov | ef1eb5e | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 313 | ValueMap[In] = Result; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 314 | return Result; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | cerr << "LinkModules ValueMap: \n"; |
| 319 | PrintMap(ValueMap); |
| 320 | |
| 321 | cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n"; |
| 322 | assert(0 && "Couldn't remap value!"); |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | /// ForceRenaming - The LLVM SymbolTable class autorenames globals that conflict |
| 327 | /// in the symbol table. This is good for all clients except for us. Go |
| 328 | /// through the trouble to force this back. |
| 329 | static void ForceRenaming(GlobalValue *GV, const std::string &Name) { |
| 330 | assert(GV->getName() != Name && "Can't force rename to self"); |
| 331 | ValueSymbolTable &ST = GV->getParent()->getValueSymbolTable(); |
| 332 | |
| 333 | // If there is a conflict, rename the conflict. |
| 334 | if (GlobalValue *ConflictGV = cast_or_null<GlobalValue>(ST.lookup(Name))) { |
| 335 | assert(ConflictGV->hasInternalLinkage() && |
| 336 | "Not conflicting with a static global, should link instead!"); |
| 337 | GV->takeName(ConflictGV); |
| 338 | ConflictGV->setName(Name); // This will cause ConflictGV to get renamed |
| 339 | assert(ConflictGV->getName() != Name && "ForceRenaming didn't work"); |
| 340 | } else { |
| 341 | GV->setName(Name); // Force the name back |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /// CopyGVAttributes - copy additional attributes (those not needed to construct |
| 346 | /// a GlobalValue) from the SrcGV to the DestGV. |
| 347 | static void CopyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) { |
Duncan Sands | 0cc9058 | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 348 | // Use the maximum alignment, rather than just copying the alignment of SrcGV. |
| 349 | unsigned Alignment = std::max(DestGV->getAlignment(), SrcGV->getAlignment()); |
| 350 | DestGV->copyAttributesFrom(SrcGV); |
| 351 | DestGV->setAlignment(Alignment); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /// GetLinkageResult - This analyzes the two global values and determines what |
| 355 | /// the result will look like in the destination module. In particular, it |
| 356 | /// computes the resultant linkage type, computes whether the global in the |
| 357 | /// source should be copied over to the destination (replacing the existing |
| 358 | /// one), and computes whether this linkage is an error or not. It also performs |
| 359 | /// visibility checks: we cannot link together two symbols with different |
| 360 | /// visibilities. |
Anton Korobeynikov | ebb5850 | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 361 | static bool GetLinkageResult(GlobalValue *Dest, const GlobalValue *Src, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 362 | GlobalValue::LinkageTypes <, bool &LinkFromSrc, |
| 363 | std::string *Err) { |
| 364 | assert((!Dest || !Src->hasInternalLinkage()) && |
| 365 | "If Src has internal linkage, Dest shouldn't be set!"); |
| 366 | if (!Dest) { |
| 367 | // Linking something to nothing. |
| 368 | LinkFromSrc = true; |
| 369 | LT = Src->getLinkage(); |
| 370 | } else if (Src->isDeclaration()) { |
Anton Korobeynikov | 1552098 | 2008-03-10 22:33:22 +0000 | [diff] [blame] | 371 | // If Src is external or if both Src & Dest are external.. Just link the |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 372 | // external globals, we aren't adding anything. |
| 373 | if (Src->hasDLLImportLinkage()) { |
| 374 | // If one of GVs has DLLImport linkage, result should be dllimport'ed. |
| 375 | if (Dest->isDeclaration()) { |
| 376 | LinkFromSrc = true; |
| 377 | LT = Src->getLinkage(); |
| 378 | } |
| 379 | } else if (Dest->hasExternalWeakLinkage()) { |
| 380 | //If the Dest is weak, use the source linkage |
| 381 | LinkFromSrc = true; |
| 382 | LT = Src->getLinkage(); |
| 383 | } else { |
| 384 | LinkFromSrc = false; |
| 385 | LT = Dest->getLinkage(); |
| 386 | } |
| 387 | } else if (Dest->isDeclaration() && !Dest->hasDLLImportLinkage()) { |
| 388 | // If Dest is external but Src is not: |
| 389 | LinkFromSrc = true; |
| 390 | LT = Src->getLinkage(); |
| 391 | } else if (Src->hasAppendingLinkage() || Dest->hasAppendingLinkage()) { |
| 392 | if (Src->getLinkage() != Dest->getLinkage()) |
| 393 | return Error(Err, "Linking globals named '" + Src->getName() + |
| 394 | "': can only link appending global with another appending global!"); |
| 395 | LinkFromSrc = true; // Special cased. |
| 396 | LT = Src->getLinkage(); |
Dale Johannesen | 49c4412 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 397 | } else if (Src->hasWeakLinkage() || Src->hasLinkOnceLinkage() || |
| 398 | Src->hasCommonLinkage()) { |
| 399 | // At this point we know that Dest has LinkOnce, External*, Weak, Common, |
| 400 | // or DLL* linkage. |
| 401 | if ((Dest->hasLinkOnceLinkage() && |
| 402 | (Src->hasWeakLinkage() || Src->hasCommonLinkage())) || |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 403 | Dest->hasExternalWeakLinkage()) { |
| 404 | LinkFromSrc = true; |
| 405 | LT = Src->getLinkage(); |
| 406 | } else { |
| 407 | LinkFromSrc = false; |
| 408 | LT = Dest->getLinkage(); |
| 409 | } |
Dale Johannesen | 49c4412 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 410 | } else if (Dest->hasWeakLinkage() || Dest->hasLinkOnceLinkage() || |
| 411 | Dest->hasCommonLinkage()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 412 | // At this point we know that Src has External* or DLL* linkage. |
| 413 | if (Src->hasExternalWeakLinkage()) { |
| 414 | LinkFromSrc = false; |
| 415 | LT = Dest->getLinkage(); |
| 416 | } else { |
| 417 | LinkFromSrc = true; |
| 418 | LT = GlobalValue::ExternalLinkage; |
| 419 | } |
| 420 | } else { |
| 421 | assert((Dest->hasExternalLinkage() || |
| 422 | Dest->hasDLLImportLinkage() || |
| 423 | Dest->hasDLLExportLinkage() || |
| 424 | Dest->hasExternalWeakLinkage()) && |
| 425 | (Src->hasExternalLinkage() || |
| 426 | Src->hasDLLImportLinkage() || |
| 427 | Src->hasDLLExportLinkage() || |
| 428 | Src->hasExternalWeakLinkage()) && |
| 429 | "Unexpected linkage type!"); |
| 430 | return Error(Err, "Linking globals named '" + Src->getName() + |
| 431 | "': symbol multiply defined!"); |
| 432 | } |
| 433 | |
| 434 | // Check visibility |
| 435 | if (Dest && Src->getVisibility() != Dest->getVisibility()) |
Chris Lattner | b69fcb8 | 2007-08-19 22:22:54 +0000 | [diff] [blame] | 436 | if (!Src->isDeclaration() && !Dest->isDeclaration()) |
| 437 | return Error(Err, "Linking globals named '" + Src->getName() + |
| 438 | "': symbols have different visibilities!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 439 | return false; |
| 440 | } |
| 441 | |
| 442 | // LinkGlobals - Loop through the global variables in the src module and merge |
| 443 | // them into the dest module. |
Anton Korobeynikov | ebb5850 | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 444 | static bool LinkGlobals(Module *Dest, const Module *Src, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 445 | std::map<const Value*, Value*> &ValueMap, |
| 446 | std::multimap<std::string, GlobalVariable *> &AppendingVars, |
| 447 | std::string *Err) { |
| 448 | // Loop over all of the globals in the src module, mapping them over as we go |
Anton Korobeynikov | ebb5850 | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 449 | for (Module::const_global_iterator I = Src->global_begin(), E = Src->global_end(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 450 | I != E; ++I) { |
Anton Korobeynikov | ebb5850 | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 451 | const GlobalVariable *SGV = I; |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 452 | GlobalValue *DGV = 0; |
| 453 | |
| 454 | // Check to see if may have to link the global with the global |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 455 | if (SGV->hasName() && !SGV->hasInternalLinkage()) { |
| 456 | DGV = Dest->getGlobalVariable(SGV->getName()); |
| 457 | if (DGV && DGV->getType() != SGV->getType()) |
| 458 | // If types don't agree due to opaque types, try to resolve them. |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 459 | RecursiveResolveTypes(SGV->getType(), DGV->getType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 462 | // Check to see if may have to link the global with the alias |
Anton Korobeynikov | 702d1cd | 2008-03-10 22:35:31 +0000 | [diff] [blame] | 463 | if (!DGV && SGV->hasName() && !SGV->hasInternalLinkage()) { |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 464 | DGV = Dest->getNamedAlias(SGV->getName()); |
| 465 | if (DGV && DGV->getType() != SGV->getType()) |
| 466 | // If types don't agree due to opaque types, try to resolve them. |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 467 | RecursiveResolveTypes(SGV->getType(), DGV->getType()); |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 470 | if (DGV && DGV->hasInternalLinkage()) |
| 471 | DGV = 0; |
| 472 | |
Dan Gohman | 930191f | 2007-10-08 15:13:30 +0000 | [diff] [blame] | 473 | assert((SGV->hasInitializer() || SGV->hasExternalWeakLinkage() || |
| 474 | SGV->hasExternalLinkage() || SGV->hasDLLImportLinkage()) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 475 | "Global must either be external or have an initializer!"); |
| 476 | |
| 477 | GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage; |
| 478 | bool LinkFromSrc = false; |
| 479 | if (GetLinkageResult(DGV, SGV, NewLinkage, LinkFromSrc, Err)) |
| 480 | return true; |
| 481 | |
| 482 | if (!DGV) { |
| 483 | // No linking to be performed, simply create an identical version of the |
| 484 | // symbol over in the dest module... the initializer will be filled in |
| 485 | // later by LinkGlobalInits... |
| 486 | GlobalVariable *NewDGV = |
| 487 | new GlobalVariable(SGV->getType()->getElementType(), |
| 488 | SGV->isConstant(), SGV->getLinkage(), /*init*/0, |
Anton Korobeynikov | 2bde2d8 | 2008-03-07 18:32:18 +0000 | [diff] [blame] | 489 | SGV->getName(), Dest); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 490 | // Propagate alignment, visibility and section info. |
| 491 | CopyGVAttributes(NewDGV, SGV); |
| 492 | |
| 493 | // If the LLVM runtime renamed the global, but it is an externally visible |
| 494 | // symbol, DGV must be an existing global with internal linkage. Rename |
| 495 | // it. |
| 496 | if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage()) |
| 497 | ForceRenaming(NewDGV, SGV->getName()); |
| 498 | |
| 499 | // Make sure to remember this mapping... |
Anton Korobeynikov | ef1eb5e | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 500 | ValueMap[SGV] = NewDGV; |
| 501 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 502 | if (SGV->hasAppendingLinkage()) |
| 503 | // Keep track that this is an appending variable... |
| 504 | AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV)); |
| 505 | } else if (DGV->hasAppendingLinkage()) { |
| 506 | // No linking is performed yet. Just insert a new copy of the global, and |
| 507 | // keep track of the fact that it is an appending variable in the |
| 508 | // AppendingVars map. The name is cleared out so that no linkage is |
| 509 | // performed. |
| 510 | GlobalVariable *NewDGV = |
| 511 | new GlobalVariable(SGV->getType()->getElementType(), |
| 512 | SGV->isConstant(), SGV->getLinkage(), /*init*/0, |
Anton Korobeynikov | 2bde2d8 | 2008-03-07 18:32:18 +0000 | [diff] [blame] | 513 | "", Dest); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 514 | |
Anton Korobeynikov | 4da527c | 2008-03-07 18:34:50 +0000 | [diff] [blame] | 515 | // Set alignment allowing CopyGVAttributes merge it with alignment of SGV. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 516 | NewDGV->setAlignment(DGV->getAlignment()); |
Anton Korobeynikov | 4da527c | 2008-03-07 18:34:50 +0000 | [diff] [blame] | 517 | // Propagate alignment, section and visibility info. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 518 | CopyGVAttributes(NewDGV, SGV); |
| 519 | |
| 520 | // Make sure to remember this mapping... |
Anton Korobeynikov | ef1eb5e | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 521 | ValueMap[SGV] = NewDGV; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 522 | |
| 523 | // Keep track that this is an appending variable... |
| 524 | AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV)); |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 525 | } else if (GlobalAlias *DGA = dyn_cast<GlobalAlias>(DGV)) { |
| 526 | // SGV is global, but DGV is alias. The only valid mapping is when SGV is |
| 527 | // external declaration, which is effectively a no-op. Also make sure |
Anton Korobeynikov | ef1eb5e | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 528 | // linkage calculation was correct. |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 529 | if (SGV->isDeclaration() && !LinkFromSrc) { |
| 530 | // Make sure to remember this mapping... |
Anton Korobeynikov | ef1eb5e | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 531 | ValueMap[SGV] = DGA; |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 532 | } else |
Anton Korobeynikov | 82a21e4 | 2008-03-10 22:34:46 +0000 | [diff] [blame] | 533 | return Error(Err, "Global-Alias Collision on '" + SGV->getName() + |
| 534 | "': symbol multiple defined"); |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 535 | } else if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV)) { |
Anton Korobeynikov | ef1eb5e | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 536 | // Otherwise, perform the global-global mapping as instructed by |
| 537 | // GetLinkageResult. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 538 | if (LinkFromSrc) { |
Anton Korobeynikov | ebb5850 | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 539 | // Propagate alignment, section, and visibility info. |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 540 | CopyGVAttributes(DGVar, SGV); |
Anton Korobeynikov | ebb5850 | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 541 | |
| 542 | // If the types don't match, and if we are to link from the source, nuke |
| 543 | // DGV and create a new one of the appropriate type. |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 544 | if (SGV->getType() != DGVar->getType()) { |
Anton Korobeynikov | ebb5850 | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 545 | GlobalVariable *NewDGV = |
| 546 | new GlobalVariable(SGV->getType()->getElementType(), |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 547 | DGVar->isConstant(), DGVar->getLinkage(), |
| 548 | /*init*/0, DGVar->getName(), Dest); |
| 549 | CopyGVAttributes(NewDGV, DGVar); |
Anton Korobeynikov | ebb5850 | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 550 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV, |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 551 | DGVar->getType())); |
| 552 | // DGVar will conflict with NewDGV because they both had the same |
Anton Korobeynikov | ebb5850 | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 553 | // name. We must erase this now so ForceRenaming doesn't assert |
| 554 | // because DGV might not have internal linkage. |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 555 | DGVar->eraseFromParent(); |
Anton Korobeynikov | ebb5850 | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 556 | |
| 557 | // If the symbol table renamed the global, but it is an externally |
| 558 | // visible symbol, DGV must be an existing global with internal |
| 559 | // linkage. Rename it. |
| 560 | if (NewDGV->getName() != SGV->getName() && |
| 561 | !NewDGV->hasInternalLinkage()) |
| 562 | ForceRenaming(NewDGV, SGV->getName()); |
| 563 | |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 564 | DGVar = NewDGV; |
Anton Korobeynikov | ebb5850 | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 567 | // Inherit const as appropriate |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 568 | DGVar->setConstant(SGV->isConstant()); |
Anton Korobeynikov | ebb5850 | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 569 | |
| 570 | // Set initializer to zero, so we can link the stuff later |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 571 | DGVar->setInitializer(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 572 | } else { |
Anton Korobeynikov | ebb5850 | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 573 | // Special case for const propagation |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 574 | if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant()) |
| 575 | DGVar->setConstant(true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Anton Korobeynikov | ebb5850 | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 578 | // Set calculated linkage |
Anton Korobeynikov | 30dd716 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 579 | DGVar->setLinkage(NewLinkage); |
Anton Korobeynikov | ebb5850 | 2008-03-10 22:33:53 +0000 | [diff] [blame] | 580 | |
| 581 | // Make sure to remember this mapping... |
Anton Korobeynikov | ef1eb5e | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 582 | ValueMap[SGV] = ConstantExpr::getBitCast(DGVar, SGV->getType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | return false; |
| 586 | } |
| 587 | |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 588 | static GlobalValue::LinkageTypes |
| 589 | CalculateAliasLinkage(const GlobalValue *SGV, const GlobalValue *DGV) { |
| 590 | if (SGV->hasExternalLinkage() || DGV->hasExternalLinkage()) |
| 591 | return GlobalValue::ExternalLinkage; |
| 592 | else if (SGV->hasWeakLinkage() || DGV->hasWeakLinkage()) |
| 593 | return GlobalValue::WeakLinkage; |
| 594 | else { |
| 595 | assert(SGV->hasInternalLinkage() && DGV->hasInternalLinkage() && |
| 596 | "Unexpected linkage type"); |
| 597 | return GlobalValue::InternalLinkage; |
| 598 | } |
| 599 | } |
| 600 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 601 | // LinkAlias - Loop through the alias in the src module and link them into the |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 602 | // dest module. We're assuming, that all functions/global variables were already |
| 603 | // linked in. |
Anton Korobeynikov | 3cfecfd | 2008-03-05 15:27:21 +0000 | [diff] [blame] | 604 | static bool LinkAlias(Module *Dest, const Module *Src, |
| 605 | std::map<const Value*, Value*> &ValueMap, |
| 606 | std::string *Err) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 607 | // Loop over all alias in the src module |
| 608 | for (Module::const_alias_iterator I = Src->alias_begin(), |
| 609 | E = Src->alias_end(); I != E; ++I) { |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 610 | const GlobalAlias *SGA = I; |
| 611 | const GlobalValue *SAliasee = SGA->getAliasedGlobal(); |
| 612 | GlobalAlias *NewGA = NULL; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 613 | |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 614 | // Globals were already linked, thus we can just query ValueMap for variant |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 615 | // of SAliasee in Dest. |
Ted Kremenek | d40cdd2 | 2008-03-09 18:32:50 +0000 | [diff] [blame] | 616 | std::map<const Value*,Value*>::const_iterator VMI = ValueMap.find(SAliasee); |
| 617 | assert(VMI != ValueMap.end() && "Aliasee not linked"); |
| 618 | GlobalValue* DAliasee = cast<GlobalValue>(VMI->second); |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 619 | GlobalValue* DGV = NULL; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 620 | |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 621 | // Try to find something 'similar' to SGA in destination module. |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 622 | if (!DGV && !SGA->hasInternalLinkage()) { |
| 623 | DGV = Dest->getNamedAlias(SGA->getName()); |
Anton Korobeynikov | 3cfecfd | 2008-03-05 15:27:21 +0000 | [diff] [blame] | 624 | |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 625 | // If types don't agree due to opaque types, try to resolve them. |
| 626 | if (DGV && DGV->getType() != SGA->getType()) |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 627 | if (RecursiveResolveTypes(SGA->getType(), DGV->getType())) |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 628 | return Error(Err, "Alias Collision on '" + SGA->getName()+ |
| 629 | "': aliases have different types"); |
| 630 | } |
| 631 | |
| 632 | if (!DGV && !SGA->hasInternalLinkage()) { |
| 633 | DGV = Dest->getGlobalVariable(SGA->getName()); |
| 634 | |
| 635 | // If types don't agree due to opaque types, try to resolve them. |
| 636 | if (DGV && DGV->getType() != SGA->getType()) |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 637 | if (RecursiveResolveTypes(SGA->getType(), DGV->getType())) |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 638 | return Error(Err, "Alias Collision on '" + SGA->getName()+ |
| 639 | "': aliases have different types"); |
| 640 | } |
| 641 | |
| 642 | if (!DGV && !SGA->hasInternalLinkage()) { |
| 643 | DGV = Dest->getFunction(SGA->getName()); |
| 644 | |
| 645 | // If types don't agree due to opaque types, try to resolve them. |
| 646 | if (DGV && DGV->getType() != SGA->getType()) |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 647 | if (RecursiveResolveTypes(SGA->getType(), DGV->getType())) |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 648 | return Error(Err, "Alias Collision on '" + SGA->getName()+ |
| 649 | "': aliases have different types"); |
| 650 | } |
| 651 | |
| 652 | // No linking to be performed on internal stuff. |
| 653 | if (DGV && DGV->hasInternalLinkage()) |
| 654 | DGV = NULL; |
| 655 | |
| 656 | if (GlobalAlias *DGA = dyn_cast_or_null<GlobalAlias>(DGV)) { |
| 657 | // Types are known to be the same, check whether aliasees equal. As |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 658 | // globals are already linked we just need query ValueMap to find the |
| 659 | // mapping. |
| 660 | if (DAliasee == DGA->getAliasedGlobal()) { |
| 661 | // This is just two copies of the same alias. Propagate linkage, if |
| 662 | // necessary. |
| 663 | DGA->setLinkage(CalculateAliasLinkage(SGA, DGA)); |
| 664 | |
| 665 | NewGA = DGA; |
| 666 | // Proceed to 'common' steps |
| 667 | } else |
Anton Korobeynikov | 82a21e4 | 2008-03-10 22:34:46 +0000 | [diff] [blame] | 668 | return Error(Err, "Alias Collision on '" + SGA->getName()+ |
| 669 | "': aliases have different aliasees"); |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 670 | } else if (GlobalVariable *DGVar = dyn_cast_or_null<GlobalVariable>(DGV)) { |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 671 | // The only allowed way is to link alias with external declaration. |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 672 | if (DGVar->isDeclaration()) { |
Anton Korobeynikov | 0a67e05 | 2008-03-10 22:36:53 +0000 | [diff] [blame] | 673 | // But only if aliasee is global too... |
| 674 | if (!isa<GlobalVariable>(DAliasee)) |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 675 | return Error(Err, "Global-Alias Collision on '" + SGA->getName() + |
| 676 | "': aliasee is not global variable"); |
Anton Korobeynikov | 0a67e05 | 2008-03-10 22:36:53 +0000 | [diff] [blame] | 677 | |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 678 | NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(), |
| 679 | SGA->getName(), DAliasee, Dest); |
| 680 | CopyGVAttributes(NewGA, SGA); |
| 681 | |
| 682 | // Any uses of DGV need to change to NewGA, with cast, if needed. |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 683 | if (SGA->getType() != DGVar->getType()) |
| 684 | DGVar->replaceAllUsesWith(ConstantExpr::getBitCast(NewGA, |
| 685 | DGVar->getType())); |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 686 | else |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 687 | DGVar->replaceAllUsesWith(NewGA); |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 688 | |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 689 | // DGVar will conflict with NewGA because they both had the same |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 690 | // name. We must erase this now so ForceRenaming doesn't assert |
| 691 | // because DGV might not have internal linkage. |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 692 | DGVar->eraseFromParent(); |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 693 | |
| 694 | // Proceed to 'common' steps |
| 695 | } else |
Anton Korobeynikov | 82a21e4 | 2008-03-10 22:34:46 +0000 | [diff] [blame] | 696 | return Error(Err, "Global-Alias Collision on '" + SGA->getName() + |
| 697 | "': symbol multiple defined"); |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 698 | } else if (Function *DF = dyn_cast_or_null<Function>(DGV)) { |
Anton Korobeynikov | cdf208a | 2008-03-05 23:08:16 +0000 | [diff] [blame] | 699 | // The only allowed way is to link alias with external declaration. |
| 700 | if (DF->isDeclaration()) { |
Anton Korobeynikov | 0a67e05 | 2008-03-10 22:36:53 +0000 | [diff] [blame] | 701 | // But only if aliasee is function too... |
| 702 | if (!isa<Function>(DAliasee)) |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 703 | return Error(Err, "Function-Alias Collision on '" + SGA->getName() + |
| 704 | "': aliasee is not function"); |
Anton Korobeynikov | 0a67e05 | 2008-03-10 22:36:53 +0000 | [diff] [blame] | 705 | |
Anton Korobeynikov | cdf208a | 2008-03-05 23:08:16 +0000 | [diff] [blame] | 706 | NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(), |
| 707 | SGA->getName(), DAliasee, Dest); |
| 708 | CopyGVAttributes(NewGA, SGA); |
| 709 | |
| 710 | // Any uses of DF need to change to NewGA, with cast, if needed. |
| 711 | if (SGA->getType() != DF->getType()) |
| 712 | DF->replaceAllUsesWith(ConstantExpr::getBitCast(NewGA, |
| 713 | DF->getType())); |
| 714 | else |
| 715 | DF->replaceAllUsesWith(NewGA); |
| 716 | |
| 717 | // DF will conflict with NewGA because they both had the same |
| 718 | // name. We must erase this now so ForceRenaming doesn't assert |
| 719 | // because DF might not have internal linkage. |
| 720 | DF->eraseFromParent(); |
| 721 | |
| 722 | // Proceed to 'common' steps |
| 723 | } else |
Anton Korobeynikov | 82a21e4 | 2008-03-10 22:34:46 +0000 | [diff] [blame] | 724 | return Error(Err, "Function-Alias Collision on '" + SGA->getName() + |
| 725 | "': symbol multiple defined"); |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 726 | } else { |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 727 | // No linking to be performed, simply create an identical version of the |
| 728 | // alias over in the dest module... |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 729 | |
| 730 | NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(), |
| 731 | SGA->getName(), DAliasee, Dest); |
| 732 | CopyGVAttributes(NewGA, SGA); |
| 733 | |
| 734 | // Proceed to 'common' steps |
| 735 | } |
| 736 | |
| 737 | assert(NewGA && "No alias was created in destination module!"); |
| 738 | |
Anton Korobeynikov | 552ccce | 2008-03-10 22:36:35 +0000 | [diff] [blame] | 739 | // If the symbol table renamed the alias, but it is an externally visible |
Anton Korobeynikov | 1b4f1f7 | 2008-05-10 14:41:43 +0000 | [diff] [blame] | 740 | // symbol, DGA must be an global value with internal linkage. Rename it. |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 741 | if (NewGA->getName() != SGA->getName() && |
| 742 | !NewGA->hasInternalLinkage()) |
| 743 | ForceRenaming(NewGA, SGA->getName()); |
| 744 | |
| 745 | // Remember this mapping so uses in the source module get remapped |
| 746 | // later by RemapOperand. |
Anton Korobeynikov | ef1eb5e | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 747 | ValueMap[SGA] = NewGA; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 748 | } |
Anton Korobeynikov | d039156 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 749 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 750 | return false; |
| 751 | } |
| 752 | |
| 753 | |
| 754 | // LinkGlobalInits - Update the initializers in the Dest module now that all |
| 755 | // globals that may be referenced are in Dest. |
| 756 | static bool LinkGlobalInits(Module *Dest, const Module *Src, |
| 757 | std::map<const Value*, Value*> &ValueMap, |
| 758 | std::string *Err) { |
| 759 | |
| 760 | // Loop over all of the globals in the src module, mapping them over as we go |
| 761 | for (Module::const_global_iterator I = Src->global_begin(), |
| 762 | E = Src->global_end(); I != E; ++I) { |
| 763 | const GlobalVariable *SGV = I; |
| 764 | |
| 765 | if (SGV->hasInitializer()) { // Only process initialized GV's |
| 766 | // Figure out what the initializer looks like in the dest module... |
| 767 | Constant *SInit = |
| 768 | cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap)); |
| 769 | |
Anton Korobeynikov | 48fc88f | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 770 | GlobalVariable *DGV = |
| 771 | cast<GlobalVariable>(ValueMap[SGV]->stripPointerCasts()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 772 | if (DGV->hasInitializer()) { |
| 773 | if (SGV->hasExternalLinkage()) { |
| 774 | if (DGV->getInitializer() != SInit) |
Anton Korobeynikov | 82a21e4 | 2008-03-10 22:34:46 +0000 | [diff] [blame] | 775 | return Error(Err, "Global Variable Collision on '" + SGV->getName() + |
| 776 | "': global variables have different initializers"); |
Dale Johannesen | 49c4412 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 777 | } else if (DGV->hasLinkOnceLinkage() || DGV->hasWeakLinkage() || |
| 778 | DGV->hasCommonLinkage()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 779 | // Nothing is required, mapped values will take the new global |
| 780 | // automatically. |
Dale Johannesen | 49c4412 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 781 | } else if (SGV->hasLinkOnceLinkage() || SGV->hasWeakLinkage() || |
| 782 | SGV->hasCommonLinkage()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 783 | // Nothing is required, mapped values will take the new global |
| 784 | // automatically. |
| 785 | } else if (DGV->hasAppendingLinkage()) { |
| 786 | assert(0 && "Appending linkage unimplemented!"); |
| 787 | } else { |
| 788 | assert(0 && "Unknown linkage!"); |
| 789 | } |
| 790 | } else { |
| 791 | // Copy the initializer over now... |
| 792 | DGV->setInitializer(SInit); |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | return false; |
| 797 | } |
| 798 | |
| 799 | // LinkFunctionProtos - Link the functions together between the two modules, |
| 800 | // without doing function bodies... this just adds external function prototypes |
| 801 | // to the Dest function... |
| 802 | // |
| 803 | static bool LinkFunctionProtos(Module *Dest, const Module *Src, |
| 804 | std::map<const Value*, Value*> &ValueMap, |
| 805 | std::string *Err) { |
| 806 | // Loop over all of the functions in the src module, mapping them over |
| 807 | for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) { |
| 808 | const Function *SF = I; // SrcFunction |
Chris Lattner | 1426bfa | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 809 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 810 | Function *DF = 0; |
Chris Lattner | 1426bfa | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 811 | |
| 812 | // If this function is internal or has no name, it doesn't participate in |
| 813 | // linkage. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 814 | if (SF->hasName() && !SF->hasInternalLinkage()) { |
| 815 | // Check to see if may have to link the function. |
| 816 | DF = Dest->getFunction(SF->getName()); |
Chris Lattner | 1426bfa | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 817 | if (DF && DF->hasInternalLinkage()) |
| 818 | DF = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 819 | } |
Chris Lattner | f7e8419 | 2008-06-09 07:25:28 +0000 | [diff] [blame] | 820 | |
Chris Lattner | 1426bfa | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 821 | // If there is no linkage to be performed, just bring over SF without |
| 822 | // modifying it. |
| 823 | if (DF == 0) { |
| 824 | // Function does not already exist, simply insert an function signature |
| 825 | // identical to SF into the dest module. |
| 826 | Function *NewDF = Function::Create(SF->getFunctionType(), |
| 827 | SF->getLinkage(), |
| 828 | SF->getName(), Dest); |
| 829 | CopyGVAttributes(NewDF, SF); |
| 830 | |
| 831 | // If the LLVM runtime renamed the function, but it is an externally |
| 832 | // visible symbol, DF must be an existing function with internal linkage. |
| 833 | // Rename it. |
| 834 | if (!NewDF->hasInternalLinkage() && NewDF->getName() != SF->getName()) |
| 835 | ForceRenaming(NewDF, SF->getName()); |
| 836 | |
| 837 | // ... and remember this mapping... |
| 838 | ValueMap[SF] = NewDF; |
| 839 | continue; |
| 840 | } |
| 841 | |
| 842 | |
| 843 | // If types don't agree because of opaque, try to resolve them. |
| 844 | if (SF->getType() != DF->getType()) |
Chris Lattner | 06638ab | 2008-06-16 18:19:05 +0000 | [diff] [blame] | 845 | RecursiveResolveTypes(SF->getType(), DF->getType()); |
Chris Lattner | 1426bfa | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 846 | |
| 847 | // Check visibility, merging if a definition overrides a prototype. |
| 848 | if (SF->getVisibility() != DF->getVisibility()) { |
Chris Lattner | b69fcb8 | 2007-08-19 22:22:54 +0000 | [diff] [blame] | 849 | // If one is a prototype, ignore its visibility. Prototypes are always |
| 850 | // overridden by the definition. |
| 851 | if (!SF->isDeclaration() && !DF->isDeclaration()) |
| 852 | return Error(Err, "Linking functions named '" + SF->getName() + |
| 853 | "': symbols have different visibilities!"); |
Chris Lattner | f7e8419 | 2008-06-09 07:25:28 +0000 | [diff] [blame] | 854 | |
| 855 | // Otherwise, replace the visibility of DF if DF is a prototype. |
| 856 | if (DF->isDeclaration()) |
| 857 | DF->setVisibility(SF->getVisibility()); |
Chris Lattner | b69fcb8 | 2007-08-19 22:22:54 +0000 | [diff] [blame] | 858 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 859 | |
Chris Lattner | 1426bfa | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 860 | if (DF->getType() != SF->getType()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 861 | if (DF->isDeclaration() && !SF->isDeclaration()) { |
| 862 | // We have a definition of the same name but different type in the |
| 863 | // source module. Copy the prototype to the destination and replace |
| 864 | // uses of the destination's prototype with the new prototype. |
Gabor Greif | b91ea9d | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 865 | Function *NewDF = Function::Create(SF->getFunctionType(), |
| 866 | SF->getLinkage(), |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 867 | SF->getName(), Dest); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 868 | CopyGVAttributes(NewDF, SF); |
| 869 | |
| 870 | // Any uses of DF need to change to NewDF, with cast |
| 871 | DF->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DF->getType())); |
| 872 | |
| 873 | // DF will conflict with NewDF because they both had the same. We must |
| 874 | // erase this now so ForceRenaming doesn't assert because DF might |
| 875 | // not have internal linkage. |
| 876 | DF->eraseFromParent(); |
| 877 | |
| 878 | // If the symbol table renamed the function, but it is an externally |
| 879 | // visible symbol, DF must be an existing function with internal |
| 880 | // linkage. Rename it. |
| 881 | if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage()) |
| 882 | ForceRenaming(NewDF, SF->getName()); |
| 883 | |
| 884 | // Remember this mapping so uses in the source module get remapped |
| 885 | // later by RemapOperand. |
| 886 | ValueMap[SF] = NewDF; |
| 887 | } else if (SF->isDeclaration()) { |
| 888 | // We have two functions of the same name but different type and the |
| 889 | // source is a declaration while the destination is not. Any use of |
| 890 | // the source must be mapped to the destination, with a cast. |
| 891 | ValueMap[SF] = ConstantExpr::getBitCast(DF, SF->getType()); |
| 892 | } else { |
| 893 | // We have two functions of the same name but different types and they |
| 894 | // are both definitions. This is an error. |
| 895 | return Error(Err, "Function '" + DF->getName() + "' defined as both '" + |
| 896 | ToStr(SF->getFunctionType(), Src) + "' and '" + |
| 897 | ToStr(DF->getFunctionType(), Dest) + "'"); |
| 898 | } |
Chris Lattner | 1426bfa | 2008-06-09 07:36:11 +0000 | [diff] [blame] | 899 | continue; |
| 900 | } |
| 901 | |
| 902 | if (SF->isDeclaration()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 903 | // If SF is a declaration or if both SF & DF are declarations, just link |
| 904 | // the declarations, we aren't adding anything. |
| 905 | if (SF->hasDLLImportLinkage()) { |
| 906 | if (DF->isDeclaration()) { |
Chris Lattner | c082fd4 | 2008-06-09 07:47:34 +0000 | [diff] [blame] | 907 | ValueMap[SF] = DF; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 908 | DF->setLinkage(SF->getLinkage()); |
Chris Lattner | c082fd4 | 2008-06-09 07:47:34 +0000 | [diff] [blame] | 909 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 910 | } else { |
Anton Korobeynikov | ef1eb5e | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 911 | ValueMap[SF] = DF; |
Chris Lattner | c082fd4 | 2008-06-09 07:47:34 +0000 | [diff] [blame] | 912 | } |
| 913 | continue; |
| 914 | } |
| 915 | |
| 916 | // If DF is external but SF is not, link the external functions, update |
| 917 | // linkage qualifiers. |
| 918 | if (DF->isDeclaration() && !DF->hasDLLImportLinkage()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 919 | ValueMap.insert(std::make_pair(SF, DF)); |
| 920 | DF->setLinkage(SF->getLinkage()); |
Chris Lattner | c082fd4 | 2008-06-09 07:47:34 +0000 | [diff] [blame] | 921 | continue; |
| 922 | } |
| 923 | |
| 924 | // At this point we know that DF has LinkOnce, Weak, or External* linkage. |
| 925 | if (SF->hasWeakLinkage() || SF->hasLinkOnceLinkage() || |
| 926 | SF->hasCommonLinkage()) { |
Anton Korobeynikov | ef1eb5e | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 927 | ValueMap[SF] = DF; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 928 | |
| 929 | // Linkonce+Weak = Weak |
| 930 | // *+External Weak = * |
Dale Johannesen | 49c4412 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 931 | if ((DF->hasLinkOnceLinkage() && |
| 932 | (SF->hasWeakLinkage() || SF->hasCommonLinkage())) || |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 933 | DF->hasExternalWeakLinkage()) |
| 934 | DF->setLinkage(SF->getLinkage()); |
Chris Lattner | c082fd4 | 2008-06-09 07:47:34 +0000 | [diff] [blame] | 935 | continue; |
| 936 | } |
| 937 | |
| 938 | if (DF->hasWeakLinkage() || DF->hasLinkOnceLinkage() || |
| 939 | DF->hasCommonLinkage()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 940 | // At this point we know that SF has LinkOnce or External* linkage. |
Anton Korobeynikov | ef1eb5e | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 941 | ValueMap[SF] = DF; |
Chris Lattner | c082fd4 | 2008-06-09 07:47:34 +0000 | [diff] [blame] | 942 | |
| 943 | // If the source function has stronger linkage than the destination, |
| 944 | // its body and linkage should override ours. |
| 945 | if (!SF->hasLinkOnceLinkage() && !SF->hasExternalWeakLinkage()) { |
| 946 | // Don't inherit linkonce & external weak linkage. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 947 | DF->setLinkage(SF->getLinkage()); |
Chris Lattner | c082fd4 | 2008-06-09 07:47:34 +0000 | [diff] [blame] | 948 | DF->deleteBody(); |
| 949 | } |
| 950 | continue; |
| 951 | } |
| 952 | |
| 953 | if (SF->getLinkage() != DF->getLinkage()) |
| 954 | return Error(Err, "Functions named '" + SF->getName() + |
| 955 | "' have different linkage specifiers!"); |
| 956 | |
| 957 | // The function is defined identically in both modules! |
| 958 | if (SF->hasExternalLinkage()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 959 | return Error(Err, "Function '" + |
| 960 | ToStr(SF->getFunctionType(), Src) + "':\"" + |
| 961 | SF->getName() + "\" - Function is already defined!"); |
Chris Lattner | c082fd4 | 2008-06-09 07:47:34 +0000 | [diff] [blame] | 962 | assert(0 && "Unknown linkage configuration found!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 963 | } |
| 964 | return false; |
| 965 | } |
| 966 | |
| 967 | // LinkFunctionBody - Copy the source function over into the dest function and |
| 968 | // fix up references to values. At this point we know that Dest is an external |
| 969 | // function, and that Src is not. |
| 970 | static bool LinkFunctionBody(Function *Dest, Function *Src, |
| 971 | std::map<const Value*, Value*> &ValueMap, |
| 972 | std::string *Err) { |
| 973 | assert(Src && Dest && Dest->isDeclaration() && !Src->isDeclaration()); |
| 974 | |
| 975 | // Go through and convert function arguments over, remembering the mapping. |
| 976 | Function::arg_iterator DI = Dest->arg_begin(); |
| 977 | for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); |
| 978 | I != E; ++I, ++DI) { |
Owen Anderson | ab567f8 | 2008-04-14 17:38:21 +0000 | [diff] [blame] | 979 | DI->setName(I->getName()); // Copy the name information over... |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 980 | |
| 981 | // Add a mapping to our local map |
Anton Korobeynikov | ef1eb5e | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 982 | ValueMap[I] = DI; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | // Splice the body of the source function into the dest function. |
| 986 | Dest->getBasicBlockList().splice(Dest->end(), Src->getBasicBlockList()); |
| 987 | |
| 988 | // At this point, all of the instructions and values of the function are now |
| 989 | // copied over. The only problem is that they are still referencing values in |
| 990 | // the Source function as operands. Loop through all of the operands of the |
| 991 | // functions and patch them up to point to the local versions... |
| 992 | // |
| 993 | for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB) |
| 994 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 995 | for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end(); |
| 996 | OI != OE; ++OI) |
| 997 | if (!isa<Instruction>(*OI) && !isa<BasicBlock>(*OI)) |
| 998 | *OI = RemapOperand(*OI, ValueMap); |
| 999 | |
| 1000 | // There is no need to map the arguments anymore. |
| 1001 | for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); |
| 1002 | I != E; ++I) |
| 1003 | ValueMap.erase(I); |
| 1004 | |
| 1005 | return false; |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | // LinkFunctionBodies - Link in the function bodies that are defined in the |
| 1010 | // source module into the DestModule. This consists basically of copying the |
| 1011 | // function over and fixing up references to values. |
| 1012 | static bool LinkFunctionBodies(Module *Dest, Module *Src, |
| 1013 | std::map<const Value*, Value*> &ValueMap, |
| 1014 | std::string *Err) { |
| 1015 | |
| 1016 | // Loop over all of the functions in the src module, mapping them over as we |
| 1017 | // go |
| 1018 | for (Module::iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF) { |
| 1019 | if (!SF->isDeclaration()) { // No body if function is external |
| 1020 | Function *DF = cast<Function>(ValueMap[SF]); // Destination function |
| 1021 | |
| 1022 | // DF not external SF external? |
| 1023 | if (DF->isDeclaration()) |
| 1024 | // Only provide the function body if there isn't one already. |
| 1025 | if (LinkFunctionBody(DF, SF, ValueMap, Err)) |
| 1026 | return true; |
| 1027 | } |
| 1028 | } |
| 1029 | return false; |
| 1030 | } |
| 1031 | |
| 1032 | // LinkAppendingVars - If there were any appending global variables, link them |
| 1033 | // together now. Return true on error. |
| 1034 | static bool LinkAppendingVars(Module *M, |
| 1035 | std::multimap<std::string, GlobalVariable *> &AppendingVars, |
| 1036 | std::string *ErrorMsg) { |
| 1037 | if (AppendingVars.empty()) return false; // Nothing to do. |
| 1038 | |
| 1039 | // Loop over the multimap of appending vars, processing any variables with the |
| 1040 | // same name, forming a new appending global variable with both of the |
| 1041 | // initializers merged together, then rewrite references to the old variables |
| 1042 | // and delete them. |
| 1043 | std::vector<Constant*> Inits; |
| 1044 | while (AppendingVars.size() > 1) { |
| 1045 | // Get the first two elements in the map... |
| 1046 | std::multimap<std::string, |
| 1047 | GlobalVariable*>::iterator Second = AppendingVars.begin(), First=Second++; |
| 1048 | |
| 1049 | // If the first two elements are for different names, there is no pair... |
| 1050 | // Otherwise there is a pair, so link them together... |
| 1051 | if (First->first == Second->first) { |
| 1052 | GlobalVariable *G1 = First->second, *G2 = Second->second; |
| 1053 | const ArrayType *T1 = cast<ArrayType>(G1->getType()->getElementType()); |
| 1054 | const ArrayType *T2 = cast<ArrayType>(G2->getType()->getElementType()); |
| 1055 | |
| 1056 | // Check to see that they two arrays agree on type... |
| 1057 | if (T1->getElementType() != T2->getElementType()) |
| 1058 | return Error(ErrorMsg, |
| 1059 | "Appending variables with different element types need to be linked!"); |
| 1060 | if (G1->isConstant() != G2->isConstant()) |
| 1061 | return Error(ErrorMsg, |
| 1062 | "Appending variables linked with different const'ness!"); |
| 1063 | |
| 1064 | if (G1->getAlignment() != G2->getAlignment()) |
| 1065 | return Error(ErrorMsg, |
| 1066 | "Appending variables with different alignment need to be linked!"); |
| 1067 | |
| 1068 | if (G1->getVisibility() != G2->getVisibility()) |
| 1069 | return Error(ErrorMsg, |
| 1070 | "Appending variables with different visibility need to be linked!"); |
| 1071 | |
| 1072 | if (G1->getSection() != G2->getSection()) |
| 1073 | return Error(ErrorMsg, |
| 1074 | "Appending variables with different section name need to be linked!"); |
| 1075 | |
| 1076 | unsigned NewSize = T1->getNumElements() + T2->getNumElements(); |
| 1077 | ArrayType *NewType = ArrayType::get(T1->getElementType(), NewSize); |
| 1078 | |
| 1079 | G1->setName(""); // Clear G1's name in case of a conflict! |
| 1080 | |
| 1081 | // Create the new global variable... |
| 1082 | GlobalVariable *NG = |
| 1083 | new GlobalVariable(NewType, G1->isConstant(), G1->getLinkage(), |
| 1084 | /*init*/0, First->first, M, G1->isThreadLocal()); |
| 1085 | |
| 1086 | // Propagate alignment, visibility and section info. |
| 1087 | CopyGVAttributes(NG, G1); |
| 1088 | |
| 1089 | // Merge the initializer... |
| 1090 | Inits.reserve(NewSize); |
| 1091 | if (ConstantArray *I = dyn_cast<ConstantArray>(G1->getInitializer())) { |
| 1092 | for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i) |
| 1093 | Inits.push_back(I->getOperand(i)); |
| 1094 | } else { |
| 1095 | assert(isa<ConstantAggregateZero>(G1->getInitializer())); |
| 1096 | Constant *CV = Constant::getNullValue(T1->getElementType()); |
| 1097 | for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i) |
| 1098 | Inits.push_back(CV); |
| 1099 | } |
| 1100 | if (ConstantArray *I = dyn_cast<ConstantArray>(G2->getInitializer())) { |
| 1101 | for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i) |
| 1102 | Inits.push_back(I->getOperand(i)); |
| 1103 | } else { |
| 1104 | assert(isa<ConstantAggregateZero>(G2->getInitializer())); |
| 1105 | Constant *CV = Constant::getNullValue(T2->getElementType()); |
| 1106 | for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i) |
| 1107 | Inits.push_back(CV); |
| 1108 | } |
| 1109 | NG->setInitializer(ConstantArray::get(NewType, Inits)); |
| 1110 | Inits.clear(); |
| 1111 | |
| 1112 | // Replace any uses of the two global variables with uses of the new |
| 1113 | // global... |
| 1114 | |
| 1115 | // FIXME: This should rewrite simple/straight-forward uses such as |
| 1116 | // getelementptr instructions to not use the Cast! |
| 1117 | G1->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G1->getType())); |
| 1118 | G2->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G2->getType())); |
| 1119 | |
| 1120 | // Remove the two globals from the module now... |
| 1121 | M->getGlobalList().erase(G1); |
| 1122 | M->getGlobalList().erase(G2); |
| 1123 | |
| 1124 | // Put the new global into the AppendingVars map so that we can handle |
| 1125 | // linking of more than two vars... |
| 1126 | Second->second = NG; |
| 1127 | } |
| 1128 | AppendingVars.erase(First); |
| 1129 | } |
| 1130 | |
| 1131 | return false; |
| 1132 | } |
| 1133 | |
Anton Korobeynikov | fdad2d8 | 2008-03-05 23:21:39 +0000 | [diff] [blame] | 1134 | static bool ResolveAliases(Module *Dest) { |
| 1135 | for (Module::alias_iterator I = Dest->alias_begin(), E = Dest->alias_end(); |
Anton Korobeynikov | 8219262 | 2008-03-11 22:51:09 +0000 | [diff] [blame] | 1136 | I != E; ++I) |
| 1137 | if (const GlobalValue *GV = I->resolveAliasedGlobal()) |
| 1138 | if (!GV->isDeclaration()) |
| 1139 | I->replaceAllUsesWith(const_cast<GlobalValue*>(GV)); |
Anton Korobeynikov | fdad2d8 | 2008-03-05 23:21:39 +0000 | [diff] [blame] | 1140 | |
| 1141 | return false; |
| 1142 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1143 | |
| 1144 | // LinkModules - This function links two modules together, with the resulting |
| 1145 | // left module modified to be the composite of the two input modules. If an |
| 1146 | // error occurs, true is returned and ErrorMsg (if not null) is set to indicate |
| 1147 | // the problem. Upon failure, the Dest module could be in a modified state, and |
| 1148 | // shouldn't be relied on to be consistent. |
| 1149 | bool |
| 1150 | Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) { |
| 1151 | assert(Dest != 0 && "Invalid Destination module"); |
| 1152 | assert(Src != 0 && "Invalid Source Module"); |
| 1153 | |
| 1154 | if (Dest->getDataLayout().empty()) { |
| 1155 | if (!Src->getDataLayout().empty()) { |
| 1156 | Dest->setDataLayout(Src->getDataLayout()); |
| 1157 | } else { |
| 1158 | std::string DataLayout; |
| 1159 | |
Anton Korobeynikov | fb782ce | 2008-02-20 11:27:04 +0000 | [diff] [blame] | 1160 | if (Dest->getEndianness() == Module::AnyEndianness) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1161 | if (Src->getEndianness() == Module::BigEndian) |
| 1162 | DataLayout.append("E"); |
| 1163 | else if (Src->getEndianness() == Module::LittleEndian) |
| 1164 | DataLayout.append("e"); |
Anton Korobeynikov | fb782ce | 2008-02-20 11:27:04 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | if (Dest->getPointerSize() == Module::AnyPointerSize) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1168 | if (Src->getPointerSize() == Module::Pointer64) |
| 1169 | DataLayout.append(DataLayout.length() == 0 ? "p:64:64" : "-p:64:64"); |
| 1170 | else if (Src->getPointerSize() == Module::Pointer32) |
| 1171 | DataLayout.append(DataLayout.length() == 0 ? "p:32:32" : "-p:32:32"); |
Anton Korobeynikov | fb782ce | 2008-02-20 11:27:04 +0000 | [diff] [blame] | 1172 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1173 | Dest->setDataLayout(DataLayout); |
| 1174 | } |
| 1175 | } |
| 1176 | |
Chris Lattner | 85dd49c | 2008-02-19 18:49:08 +0000 | [diff] [blame] | 1177 | // Copy the target triple from the source to dest if the dest's is empty. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1178 | if (Dest->getTargetTriple().empty() && !Src->getTargetTriple().empty()) |
| 1179 | Dest->setTargetTriple(Src->getTargetTriple()); |
| 1180 | |
| 1181 | if (!Src->getDataLayout().empty() && !Dest->getDataLayout().empty() && |
| 1182 | Src->getDataLayout() != Dest->getDataLayout()) |
| 1183 | cerr << "WARNING: Linking two modules of different data layouts!\n"; |
| 1184 | if (!Src->getTargetTriple().empty() && |
| 1185 | Dest->getTargetTriple() != Src->getTargetTriple()) |
| 1186 | cerr << "WARNING: Linking two modules of different target triples!\n"; |
| 1187 | |
Chris Lattner | 85dd49c | 2008-02-19 18:49:08 +0000 | [diff] [blame] | 1188 | // Append the module inline asm string. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1189 | if (!Src->getModuleInlineAsm().empty()) { |
| 1190 | if (Dest->getModuleInlineAsm().empty()) |
| 1191 | Dest->setModuleInlineAsm(Src->getModuleInlineAsm()); |
| 1192 | else |
| 1193 | Dest->setModuleInlineAsm(Dest->getModuleInlineAsm()+"\n"+ |
| 1194 | Src->getModuleInlineAsm()); |
| 1195 | } |
| 1196 | |
| 1197 | // Update the destination module's dependent libraries list with the libraries |
| 1198 | // from the source module. There's no opportunity for duplicates here as the |
| 1199 | // Module ensures that duplicate insertions are discarded. |
Chris Lattner | 85dd49c | 2008-02-19 18:49:08 +0000 | [diff] [blame] | 1200 | for (Module::lib_iterator SI = Src->lib_begin(), SE = Src->lib_end(); |
| 1201 | SI != SE; ++SI) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1202 | Dest->addLibrary(*SI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1203 | |
| 1204 | // LinkTypes - Go through the symbol table of the Src module and see if any |
| 1205 | // types are named in the src module that are not named in the Dst module. |
| 1206 | // Make sure there are no type name conflicts. |
| 1207 | if (LinkTypes(Dest, Src, ErrorMsg)) |
| 1208 | return true; |
| 1209 | |
| 1210 | // ValueMap - Mapping of values from what they used to be in Src, to what they |
| 1211 | // are now in Dest. |
| 1212 | std::map<const Value*, Value*> ValueMap; |
| 1213 | |
| 1214 | // AppendingVars - Keep track of global variables in the destination module |
| 1215 | // with appending linkage. After the module is linked together, they are |
| 1216 | // appended and the module is rewritten. |
| 1217 | std::multimap<std::string, GlobalVariable *> AppendingVars; |
| 1218 | for (Module::global_iterator I = Dest->global_begin(), E = Dest->global_end(); |
| 1219 | I != E; ++I) { |
| 1220 | // Add all of the appending globals already in the Dest module to |
| 1221 | // AppendingVars. |
| 1222 | if (I->hasAppendingLinkage()) |
| 1223 | AppendingVars.insert(std::make_pair(I->getName(), I)); |
| 1224 | } |
| 1225 | |
| 1226 | // Insert all of the globals in src into the Dest module... without linking |
| 1227 | // initializers (which could refer to functions not yet mapped over). |
| 1228 | if (LinkGlobals(Dest, Src, ValueMap, AppendingVars, ErrorMsg)) |
| 1229 | return true; |
| 1230 | |
| 1231 | // Link the functions together between the two modules, without doing function |
| 1232 | // bodies... this just adds external function prototypes to the Dest |
| 1233 | // function... We do this so that when we begin processing function bodies, |
| 1234 | // all of the global values that may be referenced are available in our |
| 1235 | // ValueMap. |
| 1236 | if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg)) |
| 1237 | return true; |
| 1238 | |
Anton Korobeynikov | 3cfecfd | 2008-03-05 15:27:21 +0000 | [diff] [blame] | 1239 | // If there were any alias, link them now. We really need to do this now, |
| 1240 | // because all of the aliases that may be referenced need to be available in |
| 1241 | // ValueMap |
| 1242 | if (LinkAlias(Dest, Src, ValueMap, ErrorMsg)) return true; |
| 1243 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1244 | // Update the initializers in the Dest module now that all globals that may |
| 1245 | // be referenced are in Dest. |
| 1246 | if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true; |
| 1247 | |
| 1248 | // Link in the function bodies that are defined in the source module into the |
| 1249 | // DestModule. This consists basically of copying the function over and |
| 1250 | // fixing up references to values. |
| 1251 | if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true; |
| 1252 | |
| 1253 | // If there were any appending global variables, link them together now. |
| 1254 | if (LinkAppendingVars(Dest, AppendingVars, ErrorMsg)) return true; |
| 1255 | |
Anton Korobeynikov | a68796c | 2008-03-05 23:08:47 +0000 | [diff] [blame] | 1256 | // Resolve all uses of aliases with aliasees |
| 1257 | if (ResolveAliases(Dest)) return true; |
| 1258 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1259 | // If the source library's module id is in the dependent library list of the |
| 1260 | // destination library, remove it since that module is now linked in. |
| 1261 | sys::Path modId; |
| 1262 | modId.set(Src->getModuleIdentifier()); |
| 1263 | if (!modId.isEmpty()) |
| 1264 | Dest->removeLibrary(modId.getBasename()); |
| 1265 | |
| 1266 | return false; |
| 1267 | } |
| 1268 | |
| 1269 | // vim: sw=2 |