Mikhail Glushenkov | c834bbf | 2009-03-03 10:04:23 +0000 | [diff] [blame] | 1 | //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the LLVM module linker. |
| 11 | // |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 7cc371a | 2004-11-14 23:27:04 +0000 | [diff] [blame] | 14 | #include "llvm/Linker.h" |
Chris Lattner | adbc0b5 | 2003-11-20 18:23:14 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
| 16 | #include "llvm/DerivedTypes.h" |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 17 | #include "llvm/Instructions.h" |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Path.h" |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Utils/Cloning.h" |
Dan Gohman | 05ea54e | 2010-08-24 18:50:07 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Utils/ValueMapper.h" |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // TypeMap implementation. |
| 28 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 30 | namespace { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 31 | class TypeMapTy : public ValueMapTypeRemapper { |
| 32 | /// MappedTypes - This is a mapping from a source type to a destination type |
| 33 | /// to use. |
| 34 | DenseMap<Type*, Type*> MappedTypes; |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 36 | /// SpeculativeTypes - When checking to see if two subgraphs are isomorphic, |
| 37 | /// we speculatively add types to MappedTypes, but keep track of them here in |
| 38 | /// case we need to roll back. |
| 39 | SmallVector<Type*, 16> SpeculativeTypes; |
| 40 | |
| 41 | /// DefinitionsToResolve - This is a list of non-opaque structs in the source |
| 42 | /// module that are mapped to an opaque struct in the destination module. |
| 43 | SmallVector<StructType*, 16> DefinitionsToResolve; |
Chris Lattner | fc196f9 | 2008-06-16 23:06:51 +0000 | [diff] [blame] | 44 | public: |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 45 | |
| 46 | /// addTypeMapping - Indicate that the specified type in the destination |
| 47 | /// module is conceptually equivalent to the specified type in the source |
| 48 | /// module. |
| 49 | void addTypeMapping(Type *DstTy, Type *SrcTy); |
| 50 | |
| 51 | /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest |
| 52 | /// module from a type definition in the source module. |
| 53 | void linkDefinedTypeBodies(); |
| 54 | |
| 55 | /// get - Return the mapped type to use for the specified input type from the |
| 56 | /// source module. |
| 57 | Type *get(Type *SrcTy); |
| 58 | |
| 59 | FunctionType *get(FunctionType *T) {return cast<FunctionType>(get((Type*)T));} |
| 60 | |
| 61 | private: |
| 62 | Type *getImpl(Type *T); |
| 63 | /// remapType - Implement the ValueMapTypeRemapper interface. |
| 64 | Type *remapType(Type *SrcTy) { |
| 65 | return get(SrcTy); |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 66 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 67 | |
| 68 | bool areTypesIsomorphic(Type *DstTy, Type *SrcTy); |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 69 | }; |
| 70 | } |
| 71 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 72 | void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) { |
| 73 | Type *&Entry = MappedTypes[SrcTy]; |
| 74 | if (Entry) return; |
| 75 | |
| 76 | if (DstTy == SrcTy) { |
| 77 | Entry = DstTy; |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // Check to see if these types are recursively isomorphic and establish a |
| 82 | // mapping between them if so. |
| 83 | if (!areTypesIsomorphic(DstTy, SrcTy)) { |
| 84 | // Oops, they aren't isomorphic. Just discard this request by rolling out |
| 85 | // any speculative mappings we've established. |
| 86 | for (unsigned i = 0, e = SpeculativeTypes.size(); i != e; ++i) |
| 87 | MappedTypes.erase(SpeculativeTypes[i]); |
| 88 | } |
| 89 | SpeculativeTypes.clear(); |
| 90 | } |
Chris Lattner | 62a81a1 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 92 | /// areTypesIsomorphic - Recursively walk this pair of types, returning true |
| 93 | /// if they are isomorphic, false if they are not. |
| 94 | bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) { |
| 95 | // Two types with differing kinds are clearly not isomorphic. |
| 96 | if (DstTy->getTypeID() != SrcTy->getTypeID()) return false; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 98 | // If we have an entry in the MappedTypes table, then we have our answer. |
| 99 | Type *&Entry = MappedTypes[SrcTy]; |
| 100 | if (Entry) |
| 101 | return Entry == DstTy; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 103 | // Two identical types are clearly isomorphic. Remember this |
| 104 | // non-speculatively. |
| 105 | if (DstTy == SrcTy) { |
| 106 | Entry = DstTy; |
Chris Lattner | 5653965 | 2008-06-16 20:03:01 +0000 | [diff] [blame] | 107 | return true; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Okay, we have two types with identical kinds that we haven't seen before. |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 112 | // If this is an opaque struct type, special case it. |
| 113 | if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) { |
| 114 | // Mapping an opaque type to any struct, just keep the dest struct. |
| 115 | if (SSTy->isOpaque()) { |
| 116 | Entry = DstTy; |
| 117 | SpeculativeTypes.push_back(SrcTy); |
Chris Lattner | 43f4ba8 | 2003-08-22 19:12:55 +0000 | [diff] [blame] | 118 | return true; |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 119 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 120 | |
| 121 | // Mapping a non-opaque source type to an opaque dest. Keep the dest, but |
| 122 | // fill it in later. This doesn't need to be speculative. |
| 123 | if (cast<StructType>(DstTy)->isOpaque()) { |
| 124 | Entry = DstTy; |
| 125 | DefinitionsToResolve.push_back(SSTy); |
| 126 | return true; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // If the number of subtypes disagree between the two types, then we fail. |
| 131 | if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes()) |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 132 | return false; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 133 | |
| 134 | // Fail if any of the extra properties (e.g. array size) of the type disagree. |
| 135 | if (isa<IntegerType>(DstTy)) |
| 136 | return false; // bitwidth disagrees. |
| 137 | if (PointerType *PT = dyn_cast<PointerType>(DstTy)) { |
| 138 | if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace()) |
| 139 | return false; |
| 140 | } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) { |
| 141 | if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg()) |
| 142 | return false; |
| 143 | } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) { |
| 144 | StructType *SSTy = cast<StructType>(SrcTy); |
Chris Lattner | 1bcbf85 | 2011-08-12 18:07:26 +0000 | [diff] [blame] | 145 | if (DSTy->isLiteral() != SSTy->isLiteral() || |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 146 | DSTy->isPacked() != SSTy->isPacked()) |
| 147 | return false; |
| 148 | } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) { |
| 149 | if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements()) |
| 150 | return false; |
| 151 | } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) { |
| 152 | if (DVTy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements()) |
| 153 | return false; |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 154 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 155 | |
| 156 | // Otherwise, we speculate that these two types will line up and recursively |
| 157 | // check the subelements. |
| 158 | Entry = DstTy; |
| 159 | SpeculativeTypes.push_back(SrcTy); |
| 160 | |
| 161 | for (unsigned i = 0, e = SrcTy->getNumContainedTypes(); i != e; ++i) |
| 162 | if (!areTypesIsomorphic(DstTy->getContainedType(i), |
| 163 | SrcTy->getContainedType(i))) |
| 164 | return false; |
| 165 | |
| 166 | // If everything seems to have lined up, then everything is great. |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest |
| 171 | /// module from a type definition in the source module. |
| 172 | void TypeMapTy::linkDefinedTypeBodies() { |
| 173 | SmallVector<Type*, 16> Elements; |
| 174 | SmallString<16> TmpName; |
| 175 | |
| 176 | // Note that processing entries in this loop (calling 'get') can add new |
| 177 | // entries to the DefinitionsToResolve vector. |
| 178 | while (!DefinitionsToResolve.empty()) { |
| 179 | StructType *SrcSTy = DefinitionsToResolve.pop_back_val(); |
| 180 | StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]); |
| 181 | |
| 182 | // TypeMap is a many-to-one mapping, if there were multiple types that |
| 183 | // provide a body for DstSTy then previous iterations of this loop may have |
| 184 | // already handled it. Just ignore this case. |
| 185 | if (!DstSTy->isOpaque()) continue; |
| 186 | assert(!SrcSTy->isOpaque() && "Not resolving a definition?"); |
| 187 | |
| 188 | // Map the body of the source type over to a new body for the dest type. |
| 189 | Elements.resize(SrcSTy->getNumElements()); |
| 190 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) |
| 191 | Elements[i] = getImpl(SrcSTy->getElementType(i)); |
| 192 | |
| 193 | DstSTy->setBody(Elements, SrcSTy->isPacked()); |
| 194 | |
| 195 | // If DstSTy has no name or has a longer name than STy, then viciously steal |
| 196 | // STy's name. |
| 197 | if (!SrcSTy->hasName()) continue; |
| 198 | StringRef SrcName = SrcSTy->getName(); |
| 199 | |
| 200 | if (!DstSTy->hasName() || DstSTy->getName().size() > SrcName.size()) { |
| 201 | TmpName.insert(TmpName.end(), SrcName.begin(), SrcName.end()); |
| 202 | SrcSTy->setName(""); |
| 203 | DstSTy->setName(TmpName.str()); |
| 204 | TmpName.clear(); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | |
| 210 | /// get - Return the mapped type to use for the specified input type from the |
| 211 | /// source module. |
| 212 | Type *TypeMapTy::get(Type *Ty) { |
| 213 | Type *Result = getImpl(Ty); |
| 214 | |
| 215 | // If this caused a reference to any struct type, resolve it before returning. |
| 216 | if (!DefinitionsToResolve.empty()) |
| 217 | linkDefinedTypeBodies(); |
| 218 | return Result; |
| 219 | } |
| 220 | |
| 221 | /// getImpl - This is the recursive version of get(). |
| 222 | Type *TypeMapTy::getImpl(Type *Ty) { |
| 223 | // If we already have an entry for this type, return it. |
| 224 | Type **Entry = &MappedTypes[Ty]; |
| 225 | if (*Entry) return *Entry; |
| 226 | |
| 227 | // If this is not a named struct type, then just map all of the elements and |
| 228 | // then rebuild the type from inside out. |
Chris Lattner | 1bcbf85 | 2011-08-12 18:07:26 +0000 | [diff] [blame] | 229 | if (!isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral()) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 230 | // If there are no element types to map, then the type is itself. This is |
| 231 | // true for the anonymous {} struct, things like 'float', integers, etc. |
| 232 | if (Ty->getNumContainedTypes() == 0) |
| 233 | return *Entry = Ty; |
| 234 | |
| 235 | // Remap all of the elements, keeping track of whether any of them change. |
| 236 | bool AnyChange = false; |
| 237 | SmallVector<Type*, 4> ElementTypes; |
| 238 | ElementTypes.resize(Ty->getNumContainedTypes()); |
| 239 | for (unsigned i = 0, e = Ty->getNumContainedTypes(); i != e; ++i) { |
| 240 | ElementTypes[i] = getImpl(Ty->getContainedType(i)); |
| 241 | AnyChange |= ElementTypes[i] != Ty->getContainedType(i); |
| 242 | } |
| 243 | |
| 244 | // If we found our type while recursively processing stuff, just use it. |
| 245 | Entry = &MappedTypes[Ty]; |
| 246 | if (*Entry) return *Entry; |
| 247 | |
| 248 | // If all of the element types mapped directly over, then the type is usable |
| 249 | // as-is. |
| 250 | if (!AnyChange) |
| 251 | return *Entry = Ty; |
| 252 | |
| 253 | // Otherwise, rebuild a modified type. |
| 254 | switch (Ty->getTypeID()) { |
| 255 | default: assert(0 && "unknown derived type to remap"); |
| 256 | case Type::ArrayTyID: |
| 257 | return *Entry = ArrayType::get(ElementTypes[0], |
| 258 | cast<ArrayType>(Ty)->getNumElements()); |
| 259 | case Type::VectorTyID: |
| 260 | return *Entry = VectorType::get(ElementTypes[0], |
| 261 | cast<VectorType>(Ty)->getNumElements()); |
| 262 | case Type::PointerTyID: |
| 263 | return *Entry = PointerType::get(ElementTypes[0], |
| 264 | cast<PointerType>(Ty)->getAddressSpace()); |
| 265 | case Type::FunctionTyID: |
| 266 | return *Entry = FunctionType::get(ElementTypes[0], |
Frits van Bommel | 39b5abf | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 267 | makeArrayRef(ElementTypes).slice(1), |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 268 | cast<FunctionType>(Ty)->isVarArg()); |
| 269 | case Type::StructTyID: |
| 270 | // Note that this is only reached for anonymous structs. |
| 271 | return *Entry = StructType::get(Ty->getContext(), ElementTypes, |
| 272 | cast<StructType>(Ty)->isPacked()); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Otherwise, this is an unmapped named struct. If the struct can be directly |
| 277 | // mapped over, just use it as-is. This happens in a case when the linked-in |
| 278 | // module has something like: |
| 279 | // %T = type {%T*, i32} |
| 280 | // @GV = global %T* null |
| 281 | // where T does not exist at all in the destination module. |
| 282 | // |
| 283 | // The other case we watch for is when the type is not in the destination |
| 284 | // module, but that it has to be rebuilt because it refers to something that |
| 285 | // is already mapped. For example, if the destination module has: |
| 286 | // %A = type { i32 } |
| 287 | // and the source module has something like |
| 288 | // %A' = type { i32 } |
| 289 | // %B = type { %A'* } |
| 290 | // @GV = global %B* null |
| 291 | // then we want to create a new type: "%B = type { %A*}" and have it take the |
| 292 | // pristine "%B" name from the source module. |
| 293 | // |
| 294 | // To determine which case this is, we have to recursively walk the type graph |
| 295 | // speculating that we'll be able to reuse it unmodified. Only if this is |
| 296 | // safe would we map the entire thing over. Because this is an optimization, |
| 297 | // and is not required for the prettiness of the linked module, we just skip |
| 298 | // it and always rebuild a type here. |
| 299 | StructType *STy = cast<StructType>(Ty); |
| 300 | |
| 301 | // If the type is opaque, we can just use it directly. |
| 302 | if (STy->isOpaque()) |
| 303 | return *Entry = STy; |
| 304 | |
| 305 | // Otherwise we create a new type and resolve its body later. This will be |
| 306 | // resolved by the top level of get(). |
| 307 | DefinitionsToResolve.push_back(STy); |
Chris Lattner | 1bcbf85 | 2011-08-12 18:07:26 +0000 | [diff] [blame] | 308 | return *Entry = StructType::create(STy->getContext()); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | |
| 312 | |
| 313 | //===----------------------------------------------------------------------===// |
| 314 | // ModuleLinker implementation. |
| 315 | //===----------------------------------------------------------------------===// |
| 316 | |
| 317 | namespace { |
| 318 | /// ModuleLinker - This is an implementation class for the LinkModules |
| 319 | /// function, which is the entrypoint for this file. |
| 320 | class ModuleLinker { |
| 321 | Module *DstM, *SrcM; |
| 322 | |
| 323 | TypeMapTy TypeMap; |
| 324 | |
| 325 | /// ValueMap - Mapping of values from what they used to be in Src, to what |
| 326 | /// they are now in DstM. ValueToValueMapTy is a ValueMap, which involves |
| 327 | /// some overhead due to the use of Value handles which the Linker doesn't |
| 328 | /// actually need, but this allows us to reuse the ValueMapper code. |
| 329 | ValueToValueMapTy ValueMap; |
| 330 | |
| 331 | struct AppendingVarInfo { |
| 332 | GlobalVariable *NewGV; // New aggregate global in dest module. |
| 333 | Constant *DstInit; // Old initializer from dest module. |
| 334 | Constant *SrcInit; // Old initializer from src module. |
| 335 | }; |
| 336 | |
| 337 | std::vector<AppendingVarInfo> AppendingVars; |
| 338 | |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 339 | unsigned Mode; // Mode to treat source module. |
| 340 | |
| 341 | // Set of items not to link in from source. |
| 342 | SmallPtrSet<const Value*, 16> DoNotLinkFromSource; |
| 343 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 344 | public: |
| 345 | std::string ErrorMsg; |
| 346 | |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 347 | ModuleLinker(Module *dstM, Module *srcM, unsigned mode) |
| 348 | : DstM(dstM), SrcM(srcM), Mode(mode) { } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 349 | |
| 350 | bool run(); |
| 351 | |
| 352 | private: |
| 353 | /// emitError - Helper method for setting a message and returning an error |
| 354 | /// code. |
| 355 | bool emitError(const Twine &Message) { |
| 356 | ErrorMsg = Message.str(); |
Chris Lattner | f6f4f7a | 2008-06-16 18:27:53 +0000 | [diff] [blame] | 357 | return true; |
Chris Lattner | a4477f9 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 358 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 359 | |
| 360 | /// getLinkageResult - This analyzes the two global values and determines |
| 361 | /// what the result will look like in the destination module. |
| 362 | bool getLinkageResult(GlobalValue *Dest, const GlobalValue *Src, |
| 363 | GlobalValue::LinkageTypes <, bool &LinkFromSrc); |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 365 | /// getLinkedToGlobal - Given a global in the source module, return the |
| 366 | /// global in the destination module that is being linked to, if any. |
| 367 | GlobalValue *getLinkedToGlobal(GlobalValue *SrcGV) { |
| 368 | // If the source has no name it can't link. If it has local linkage, |
| 369 | // there is no name match-up going on. |
| 370 | if (!SrcGV->hasName() || SrcGV->hasLocalLinkage()) |
| 371 | return 0; |
| 372 | |
| 373 | // Otherwise see if we have a match in the destination module's symtab. |
| 374 | GlobalValue *DGV = DstM->getNamedValue(SrcGV->getName()); |
| 375 | if (DGV == 0) return 0; |
| 376 | |
| 377 | // If we found a global with the same name in the dest module, but it has |
| 378 | // internal linkage, we are really not doing any linkage here. |
| 379 | if (DGV->hasLocalLinkage()) |
| 380 | return 0; |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 382 | // Otherwise, we do in fact link to the destination global. |
| 383 | return DGV; |
| 384 | } |
| 385 | |
| 386 | void computeTypeMapping(); |
| 387 | |
| 388 | bool linkAppendingVarProto(GlobalVariable *DstGV, GlobalVariable *SrcGV); |
| 389 | bool linkGlobalProto(GlobalVariable *SrcGV); |
| 390 | bool linkFunctionProto(Function *SrcF); |
| 391 | bool linkAliasProto(GlobalAlias *SrcA); |
| 392 | |
| 393 | void linkAppendingVarInit(const AppendingVarInfo &AVI); |
| 394 | void linkGlobalInits(); |
| 395 | void linkFunctionBody(Function *Dst, Function *Src); |
| 396 | void linkAliasBodies(); |
| 397 | void linkNamedMDNodes(); |
| 398 | }; |
Chris Lattner | e3092c9 | 2003-08-23 21:25:54 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Chris Lattner | e76c57a | 2003-08-22 06:07:12 +0000 | [diff] [blame] | 401 | |
Chris Lattner | 2c236f3 | 2001-11-03 05:18:24 +0000 | [diff] [blame] | 402 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 403 | /// forceRenaming - The LLVM SymbolTable class autorenames globals that conflict |
Reid Spencer | 8bef037 | 2007-02-04 04:29:21 +0000 | [diff] [blame] | 404 | /// in the symbol table. This is good for all clients except for us. Go |
| 405 | /// through the trouble to force this back. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 406 | static void forceRenaming(GlobalValue *GV, StringRef Name) { |
| 407 | // If the global doesn't force its name or if it already has the right name, |
| 408 | // there is nothing for us to do. |
| 409 | if (GV->hasLocalLinkage() || GV->getName() == Name) |
| 410 | return; |
| 411 | |
| 412 | Module *M = GV->getParent(); |
Chris Lattner | c003628 | 2004-08-04 07:05:54 +0000 | [diff] [blame] | 413 | |
| 414 | // If there is a conflict, rename the conflict. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 415 | if (GlobalValue *ConflictGV = M->getNamedValue(Name)) { |
Chris Lattner | 33f2949 | 2007-02-11 00:39:38 +0000 | [diff] [blame] | 416 | GV->takeName(ConflictGV); |
| 417 | ConflictGV->setName(Name); // This will cause ConflictGV to get renamed |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 418 | assert(ConflictGV->getName() != Name && "forceRenaming didn't work"); |
Chris Lattner | 33f2949 | 2007-02-11 00:39:38 +0000 | [diff] [blame] | 419 | } else { |
| 420 | GV->setName(Name); // Force the name back |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 421 | } |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 422 | } |
Reid Spencer | 8bef037 | 2007-02-04 04:29:21 +0000 | [diff] [blame] | 423 | |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 424 | /// CopyGVAttributes - copy additional attributes (those not needed to construct |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 425 | /// a GlobalValue) from the SrcGV to the DestGV. |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 426 | static void CopyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) { |
Duncan Sands | 28c3cff | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 427 | // Use the maximum alignment, rather than just copying the alignment of SrcGV. |
| 428 | unsigned Alignment = std::max(DestGV->getAlignment(), SrcGV->getAlignment()); |
| 429 | DestGV->copyAttributesFrom(SrcGV); |
| 430 | DestGV->setAlignment(Alignment); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 431 | |
| 432 | forceRenaming(DestGV, SrcGV->getName()); |
Chris Lattner | c003628 | 2004-08-04 07:05:54 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 435 | /// getLinkageResult - This analyzes the two global values and determines what |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 436 | /// the result will look like in the destination module. In particular, it |
| 437 | /// computes the resultant linkage type, computes whether the global in the |
| 438 | /// source should be copied over to the destination (replacing the existing |
Anton Korobeynikov | 9cd3ccf | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 439 | /// one), and computes whether this linkage is an error or not. It also performs |
| 440 | /// visibility checks: we cannot link together two symbols with different |
| 441 | /// visibilities. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 442 | bool ModuleLinker::getLinkageResult(GlobalValue *Dest, const GlobalValue *Src, |
| 443 | GlobalValue::LinkageTypes <, |
| 444 | bool &LinkFromSrc) { |
| 445 | assert(Dest && "Must have two globals being queried"); |
| 446 | assert(!Src->hasLocalLinkage() && |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 447 | "If Src has internal linkage, Dest shouldn't be set!"); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 448 | |
Peter Collingbourne | 8895316 | 2011-10-30 17:46:34 +0000 | [diff] [blame] | 449 | bool SrcIsDeclaration = Src->isDeclaration() && !Src->isMaterializable(); |
Chris Lattner | f84c59d | 2011-07-14 20:23:05 +0000 | [diff] [blame] | 450 | bool DestIsDeclaration = Dest->isDeclaration(); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 451 | |
| 452 | if (SrcIsDeclaration) { |
Anton Korobeynikov | 2b48ef0 | 2008-03-10 22:33:22 +0000 | [diff] [blame] | 453 | // If Src is external or if both Src & Dest are external.. Just link the |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 454 | // external globals, we aren't adding anything. |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 455 | if (Src->hasDLLImportLinkage()) { |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 456 | // If one of GVs has DLLImport linkage, result should be dllimport'ed. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 457 | if (DestIsDeclaration) { |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 458 | LinkFromSrc = true; |
| 459 | LT = Src->getLinkage(); |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 460 | } |
Andrew Lenharth | 8753c44 | 2006-12-15 17:35:32 +0000 | [diff] [blame] | 461 | } else if (Dest->hasExternalWeakLinkage()) { |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 462 | // If the Dest is weak, use the source linkage. |
Andrew Lenharth | 8753c44 | 2006-12-15 17:35:32 +0000 | [diff] [blame] | 463 | LinkFromSrc = true; |
| 464 | LT = Src->getLinkage(); |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 465 | } else { |
| 466 | LinkFromSrc = false; |
| 467 | LT = Dest->getLinkage(); |
| 468 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 469 | } else if (DestIsDeclaration && !Dest->hasDLLImportLinkage()) { |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 470 | // If Dest is external but Src is not: |
| 471 | LinkFromSrc = true; |
| 472 | LT = Src->getLinkage(); |
Duncan Sands | a05ef5e | 2009-03-08 13:35:23 +0000 | [diff] [blame] | 473 | } else if (Src->isWeakForLinker()) { |
Dale Johannesen | aafce77 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 474 | // At this point we know that Dest has LinkOnce, External*, Weak, Common, |
| 475 | // or DLL* linkage. |
Chris Lattner | 266c7bb | 2009-04-13 05:44:34 +0000 | [diff] [blame] | 476 | if (Dest->hasExternalWeakLinkage() || |
| 477 | Dest->hasAvailableExternallyLinkage() || |
| 478 | (Dest->hasLinkOnceLinkage() && |
| 479 | (Src->hasWeakLinkage() || Src->hasCommonLinkage()))) { |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 480 | LinkFromSrc = true; |
| 481 | LT = Src->getLinkage(); |
| 482 | } else { |
| 483 | LinkFromSrc = false; |
| 484 | LT = Dest->getLinkage(); |
| 485 | } |
Duncan Sands | a05ef5e | 2009-03-08 13:35:23 +0000 | [diff] [blame] | 486 | } else if (Dest->isWeakForLinker()) { |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 487 | // At this point we know that Src has External* or DLL* linkage. |
| 488 | if (Src->hasExternalWeakLinkage()) { |
| 489 | LinkFromSrc = false; |
| 490 | LT = Dest->getLinkage(); |
| 491 | } else { |
| 492 | LinkFromSrc = true; |
| 493 | LT = GlobalValue::ExternalLinkage; |
| 494 | } |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 495 | } else { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 496 | assert((Dest->hasExternalLinkage() || Dest->hasDLLImportLinkage() || |
| 497 | Dest->hasDLLExportLinkage() || Dest->hasExternalWeakLinkage()) && |
| 498 | (Src->hasExternalLinkage() || Src->hasDLLImportLinkage() || |
| 499 | Src->hasDLLExportLinkage() || Src->hasExternalWeakLinkage()) && |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 500 | "Unexpected linkage type!"); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 501 | return emitError("Linking globals named '" + Src->getName() + |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 502 | "': symbol multiply defined!"); |
| 503 | } |
Anton Korobeynikov | 9cd3ccf | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 504 | |
| 505 | // Check visibility |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 506 | if (Src->getVisibility() != Dest->getVisibility() && |
| 507 | !SrcIsDeclaration && !DestIsDeclaration && |
Rafael Espindola | 4e93885 | 2011-02-01 05:33:52 +0000 | [diff] [blame] | 508 | !Src->hasAvailableExternallyLinkage() && |
| 509 | !Dest->hasAvailableExternallyLinkage()) |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 510 | return emitError("Linking globals named '" + Src->getName() + |
Chris Lattner | 97f8b09 | 2007-08-19 22:22:54 +0000 | [diff] [blame] | 511 | "': symbols have different visibilities!"); |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 512 | return false; |
| 513 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 514 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 515 | /// computeTypeMapping - Loop over all of the linked values to compute type |
| 516 | /// mappings. For example, if we link "extern Foo *x" and "Foo *x = NULL", then |
| 517 | /// we have two struct types 'Foo' but one got renamed when the module was |
| 518 | /// loaded into the same LLVMContext. |
| 519 | void ModuleLinker::computeTypeMapping() { |
| 520 | // Incorporate globals. |
| 521 | for (Module::global_iterator I = SrcM->global_begin(), |
| 522 | E = SrcM->global_end(); I != E; ++I) { |
| 523 | GlobalValue *DGV = getLinkedToGlobal(I); |
| 524 | if (DGV == 0) continue; |
| 525 | |
| 526 | if (!DGV->hasAppendingLinkage() || !I->hasAppendingLinkage()) { |
| 527 | TypeMap.addTypeMapping(DGV->getType(), I->getType()); |
| 528 | continue; |
| 529 | } |
| 530 | |
| 531 | // Unify the element type of appending arrays. |
| 532 | ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType()); |
| 533 | ArrayType *SAT = cast<ArrayType>(I->getType()->getElementType()); |
| 534 | TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType()); |
Devang Patel | ab67e70 | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 535 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 536 | |
| 537 | // Incorporate functions. |
| 538 | for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I) { |
| 539 | if (GlobalValue *DGV = getLinkedToGlobal(I)) |
| 540 | TypeMap.addTypeMapping(DGV->getType(), I->getType()); |
| 541 | } |
| 542 | |
| 543 | // Don't bother incorporating aliases, they aren't generally typed well. |
| 544 | |
| 545 | // Now that we have discovered all of the type equivalences, get a body for |
| 546 | // any 'opaque' types in the dest module that are now resolved. |
| 547 | TypeMap.linkDefinedTypeBodies(); |
Devang Patel | ab67e70 | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 550 | /// linkAppendingVarProto - If there were any appending global variables, link |
| 551 | /// them together now. Return true on error. |
| 552 | bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV, |
| 553 | GlobalVariable *SrcGV) { |
| 554 | |
| 555 | if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage()) |
| 556 | return emitError("Linking globals named '" + SrcGV->getName() + |
| 557 | "': can only link appending global with another appending global!"); |
| 558 | |
| 559 | ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType()); |
| 560 | ArrayType *SrcTy = |
| 561 | cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType())); |
| 562 | Type *EltTy = DstTy->getElementType(); |
| 563 | |
| 564 | // Check to see that they two arrays agree on type. |
| 565 | if (EltTy != SrcTy->getElementType()) |
| 566 | return emitError("Appending variables with different element types!"); |
| 567 | if (DstGV->isConstant() != SrcGV->isConstant()) |
| 568 | return emitError("Appending variables linked with different const'ness!"); |
| 569 | |
| 570 | if (DstGV->getAlignment() != SrcGV->getAlignment()) |
| 571 | return emitError( |
| 572 | "Appending variables with different alignment need to be linked!"); |
| 573 | |
| 574 | if (DstGV->getVisibility() != SrcGV->getVisibility()) |
| 575 | return emitError( |
| 576 | "Appending variables with different visibility need to be linked!"); |
| 577 | |
| 578 | if (DstGV->getSection() != SrcGV->getSection()) |
| 579 | return emitError( |
| 580 | "Appending variables with different section name need to be linked!"); |
| 581 | |
| 582 | uint64_t NewSize = DstTy->getNumElements() + SrcTy->getNumElements(); |
| 583 | ArrayType *NewType = ArrayType::get(EltTy, NewSize); |
| 584 | |
| 585 | // Create the new global variable. |
| 586 | GlobalVariable *NG = |
| 587 | new GlobalVariable(*DstGV->getParent(), NewType, SrcGV->isConstant(), |
| 588 | DstGV->getLinkage(), /*init*/0, /*name*/"", DstGV, |
| 589 | DstGV->isThreadLocal(), |
| 590 | DstGV->getType()->getAddressSpace()); |
| 591 | |
| 592 | // Propagate alignment, visibility and section info. |
| 593 | CopyGVAttributes(NG, DstGV); |
| 594 | |
| 595 | AppendingVarInfo AVI; |
| 596 | AVI.NewGV = NG; |
| 597 | AVI.DstInit = DstGV->getInitializer(); |
| 598 | AVI.SrcInit = SrcGV->getInitializer(); |
| 599 | AppendingVars.push_back(AVI); |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 600 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 601 | // Replace any uses of the two global variables with uses of the new |
| 602 | // global. |
| 603 | ValueMap[SrcGV] = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType())); |
Anton Korobeynikov | 01f6939 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 604 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 605 | DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType())); |
| 606 | DstGV->eraseFromParent(); |
| 607 | |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 608 | // Track the source variable so we don't try to link it. |
| 609 | DoNotLinkFromSource.insert(SrcGV); |
| 610 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 611 | return false; |
| 612 | } |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 613 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 614 | /// linkGlobalProto - Loop through the global variables in the src module and |
| 615 | /// merge them into the dest module. |
| 616 | bool ModuleLinker::linkGlobalProto(GlobalVariable *SGV) { |
| 617 | GlobalValue *DGV = getLinkedToGlobal(SGV); |
Mikhail Glushenkov | eba2cb0 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 618 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 619 | if (DGV) { |
| 620 | // Concatenation of appending linkage variables is magic and handled later. |
| 621 | if (DGV->hasAppendingLinkage() || SGV->hasAppendingLinkage()) |
| 622 | return linkAppendingVarProto(cast<GlobalVariable>(DGV), SGV); |
| 623 | |
| 624 | // Determine whether linkage of these two globals follows the source |
| 625 | // module's definition or the destination module's definition. |
Chris Lattner | b324bd7 | 2006-11-09 05:18:12 +0000 | [diff] [blame] | 626 | GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage; |
| 627 | bool LinkFromSrc = false; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 628 | if (getLinkageResult(DGV, SGV, NewLinkage, LinkFromSrc)) |
Chris Lattner | aee38ea | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 629 | return true; |
Chris Lattner | 0fec08e | 2003-04-21 21:07:05 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 631 | // If we're not linking from the source, then keep the definition that we |
| 632 | // have. |
| 633 | if (!LinkFromSrc) { |
| 634 | // Special case for const propagation. |
| 635 | if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV)) |
| 636 | if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant()) |
| 637 | DGVar->setConstant(true); |
| 638 | |
| 639 | // Set calculated linkage. |
| 640 | DGV->setLinkage(NewLinkage); |
| 641 | |
Chris Lattner | 6157e38 | 2008-07-14 07:23:24 +0000 | [diff] [blame] | 642 | // Make sure to remember this mapping. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 643 | ValueMap[SGV] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGV->getType())); |
| 644 | |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 645 | // Track the source global so that we don't attempt to copy it over when |
| 646 | // processing global initializers. |
| 647 | DoNotLinkFromSource.insert(SGV); |
| 648 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 649 | return false; |
Chris Lattner | 6157e38 | 2008-07-14 07:23:24 +0000 | [diff] [blame] | 650 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 651 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 652 | |
| 653 | // No linking to be performed or linking from the source: simply create an |
| 654 | // identical version of the symbol over in the dest module... the |
| 655 | // initializer will be filled in later by LinkGlobalInits. |
| 656 | GlobalVariable *NewDGV = |
| 657 | new GlobalVariable(*DstM, TypeMap.get(SGV->getType()->getElementType()), |
| 658 | SGV->isConstant(), SGV->getLinkage(), /*init*/0, |
| 659 | SGV->getName(), /*insertbefore*/0, |
| 660 | SGV->isThreadLocal(), |
| 661 | SGV->getType()->getAddressSpace()); |
| 662 | // Propagate alignment, visibility and section info. |
| 663 | CopyGVAttributes(NewDGV, SGV); |
| 664 | |
| 665 | if (DGV) { |
| 666 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV, DGV->getType())); |
| 667 | DGV->eraseFromParent(); |
| 668 | } |
| 669 | |
| 670 | // Make sure to remember this mapping. |
| 671 | ValueMap[SGV] = NewDGV; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 672 | return false; |
| 673 | } |
| 674 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 675 | /// linkFunctionProto - Link the function in the source module into the |
| 676 | /// destination module if needed, setting up mapping information. |
| 677 | bool ModuleLinker::linkFunctionProto(Function *SF) { |
| 678 | GlobalValue *DGV = getLinkedToGlobal(SF); |
| 679 | |
| 680 | if (DGV) { |
| 681 | GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage; |
| 682 | bool LinkFromSrc = false; |
| 683 | if (getLinkageResult(DGV, SF, NewLinkage, LinkFromSrc)) |
| 684 | return true; |
| 685 | |
| 686 | if (!LinkFromSrc) { |
| 687 | // Set calculated linkage |
| 688 | DGV->setLinkage(NewLinkage); |
| 689 | |
| 690 | // Make sure to remember this mapping. |
| 691 | ValueMap[SF] = ConstantExpr::getBitCast(DGV, TypeMap.get(SF->getType())); |
| 692 | |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 693 | // Track the function from the source module so we don't attempt to remap |
| 694 | // it. |
| 695 | DoNotLinkFromSource.insert(SF); |
| 696 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 697 | return false; |
| 698 | } |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 699 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 700 | |
| 701 | // If there is no linkage to be performed or we are linking from the source, |
| 702 | // bring SF over. |
| 703 | Function *NewDF = Function::Create(TypeMap.get(SF->getFunctionType()), |
| 704 | SF->getLinkage(), SF->getName(), DstM); |
| 705 | CopyGVAttributes(NewDF, SF); |
Anton Korobeynikov | 58887bc | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 706 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 707 | if (DGV) { |
| 708 | // Any uses of DF need to change to NewDF, with cast. |
| 709 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType())); |
| 710 | DGV->eraseFromParent(); |
Lauro Ramos Venancio | 31ed0fb | 2007-06-28 19:02:54 +0000 | [diff] [blame] | 711 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 712 | |
| 713 | ValueMap[SF] = NewDF; |
Lauro Ramos Venancio | 31ed0fb | 2007-06-28 19:02:54 +0000 | [diff] [blame] | 714 | return false; |
| 715 | } |
| 716 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 717 | /// LinkAliasProto - Set up prototypes for any aliases that come over from the |
| 718 | /// source module. |
| 719 | bool ModuleLinker::linkAliasProto(GlobalAlias *SGA) { |
| 720 | GlobalValue *DGV = getLinkedToGlobal(SGA); |
| 721 | |
| 722 | if (DGV) { |
| 723 | GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage; |
| 724 | bool LinkFromSrc = false; |
| 725 | if (getLinkageResult(DGV, SGA, NewLinkage, LinkFromSrc)) |
| 726 | return true; |
| 727 | |
| 728 | if (!LinkFromSrc) { |
| 729 | // Set calculated linkage. |
| 730 | DGV->setLinkage(NewLinkage); |
| 731 | |
| 732 | // Make sure to remember this mapping. |
| 733 | ValueMap[SGA] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGA->getType())); |
| 734 | |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 735 | // Track the alias from the source module so we don't attempt to remap it. |
| 736 | DoNotLinkFromSource.insert(SGA); |
| 737 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 738 | return false; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | // If there is no linkage to be performed or we're linking from the source, |
| 743 | // bring over SGA. |
| 744 | GlobalAlias *NewDA = new GlobalAlias(TypeMap.get(SGA->getType()), |
| 745 | SGA->getLinkage(), SGA->getName(), |
| 746 | /*aliasee*/0, DstM); |
| 747 | CopyGVAttributes(NewDA, SGA); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 748 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 749 | if (DGV) { |
| 750 | // Any uses of DGV need to change to NewDA, with cast. |
| 751 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDA, DGV->getType())); |
| 752 | DGV->eraseFromParent(); |
| 753 | } |
| 754 | |
| 755 | ValueMap[SGA] = NewDA; |
| 756 | return false; |
| 757 | } |
| 758 | |
| 759 | void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) { |
| 760 | // Merge the initializer. |
| 761 | SmallVector<Constant*, 16> Elements; |
| 762 | if (ConstantArray *I = dyn_cast<ConstantArray>(AVI.DstInit)) { |
| 763 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 764 | Elements.push_back(I->getOperand(i)); |
| 765 | } else { |
| 766 | assert(isa<ConstantAggregateZero>(AVI.DstInit)); |
| 767 | ArrayType *DstAT = cast<ArrayType>(AVI.DstInit->getType()); |
| 768 | Type *EltTy = DstAT->getElementType(); |
| 769 | Elements.append(DstAT->getNumElements(), Constant::getNullValue(EltTy)); |
| 770 | } |
| 771 | |
| 772 | Constant *SrcInit = MapValue(AVI.SrcInit, ValueMap, RF_None, &TypeMap); |
| 773 | if (const ConstantArray *I = dyn_cast<ConstantArray>(SrcInit)) { |
| 774 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 775 | Elements.push_back(I->getOperand(i)); |
| 776 | } else { |
| 777 | assert(isa<ConstantAggregateZero>(SrcInit)); |
| 778 | ArrayType *SrcAT = cast<ArrayType>(SrcInit->getType()); |
| 779 | Type *EltTy = SrcAT->getElementType(); |
| 780 | Elements.append(SrcAT->getNumElements(), Constant::getNullValue(EltTy)); |
| 781 | } |
| 782 | ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType()); |
| 783 | AVI.NewGV->setInitializer(ConstantArray::get(NewType, Elements)); |
| 784 | } |
| 785 | |
| 786 | |
| 787 | // linkGlobalInits - Update the initializers in the Dest module now that all |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 788 | // globals that may be referenced are in Dest. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 789 | void ModuleLinker::linkGlobalInits() { |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 790 | // Loop over all of the globals in the src module, mapping them over as we go |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 791 | for (Module::const_global_iterator I = SrcM->global_begin(), |
| 792 | E = SrcM->global_end(); I != E; ++I) { |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 793 | |
| 794 | // Only process initialized GV's or ones not already in dest. |
| 795 | if (!I->hasInitializer() || DoNotLinkFromSource.count(I)) continue; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 796 | |
| 797 | // Grab destination global variable. |
| 798 | GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[I]); |
| 799 | // Figure out what the initializer looks like in the dest module. |
| 800 | DGV->setInitializer(MapValue(I->getInitializer(), ValueMap, |
| 801 | RF_None, &TypeMap)); |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 802 | } |
Chris Lattner | 8d2de8a | 2001-10-15 03:12:52 +0000 | [diff] [blame] | 803 | } |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 804 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 805 | // linkFunctionBody - Copy the source function over into the dest function and |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 806 | // fix up references to values. At this point we know that Dest is an external |
| 807 | // function, and that Src is not. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 808 | void ModuleLinker::linkFunctionBody(Function *Dst, Function *Src) { |
| 809 | assert(Src && Dst && Dst->isDeclaration() && !Src->isDeclaration()); |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 810 | |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 811 | // Go through and convert function arguments over, remembering the mapping. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 812 | Function::arg_iterator DI = Dst->arg_begin(); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 813 | for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 814 | I != E; ++I, ++DI) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 815 | DI->setName(I->getName()); // Copy the name over. |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 816 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 817 | // Add a mapping to our mapping. |
Anton Korobeynikov | 817bf2a | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 818 | ValueMap[I] = DI; |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 821 | if (Mode == Linker::DestroySource) { |
| 822 | // Splice the body of the source function into the dest function. |
| 823 | Dst->getBasicBlockList().splice(Dst->end(), Src->getBasicBlockList()); |
| 824 | |
| 825 | // At this point, all of the instructions and values of the function are now |
| 826 | // copied over. The only problem is that they are still referencing values in |
| 827 | // the Source function as operands. Loop through all of the operands of the |
| 828 | // functions and patch them up to point to the local versions. |
| 829 | for (Function::iterator BB = Dst->begin(), BE = Dst->end(); BB != BE; ++BB) |
| 830 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 831 | RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries, &TypeMap); |
| 832 | |
| 833 | } else { |
| 834 | // Clone the body of the function into the dest function. |
| 835 | SmallVector<ReturnInst*, 8> Returns; // Ignore returns. |
| 836 | CloneFunctionInto(Dst, Src, ValueMap, false, Returns); |
| 837 | } |
| 838 | |
Chris Lattner | 0033baf | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 839 | // There is no need to map the arguments anymore. |
Chris Lattner | 1127315 | 2006-06-16 01:24:04 +0000 | [diff] [blame] | 840 | for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); |
| 841 | I != E; ++I) |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 842 | ValueMap.erase(I); |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 843 | |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 847 | void ModuleLinker::linkAliasBodies() { |
| 848 | for (Module::alias_iterator I = SrcM->alias_begin(), E = SrcM->alias_end(); |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 849 | I != E; ++I) { |
| 850 | if (DoNotLinkFromSource.count(I)) |
| 851 | continue; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 852 | if (Constant *Aliasee = I->getAliasee()) { |
| 853 | GlobalAlias *DA = cast<GlobalAlias>(ValueMap[I]); |
| 854 | DA->setAliasee(MapValue(Aliasee, ValueMap, RF_None, &TypeMap)); |
David Chisnall | 3472246 | 2010-01-09 16:27:31 +0000 | [diff] [blame] | 855 | } |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 856 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 857 | } |
Anton Korobeynikov | 9f2ee70 | 2008-03-05 23:21:39 +0000 | [diff] [blame] | 858 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 859 | /// linkNamedMDNodes - Insert all of the named mdnodes in Src into the Dest |
| 860 | /// module. |
| 861 | void ModuleLinker::linkNamedMDNodes() { |
| 862 | for (Module::const_named_metadata_iterator I = SrcM->named_metadata_begin(), |
| 863 | E = SrcM->named_metadata_end(); I != E; ++I) { |
| 864 | NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(I->getName()); |
| 865 | // Add Src elements into Dest node. |
| 866 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 867 | DestNMD->addOperand(MapValue(I->getOperand(i), ValueMap, |
| 868 | RF_None, &TypeMap)); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | bool ModuleLinker::run() { |
| 873 | assert(DstM && "Null Destination module"); |
| 874 | assert(SrcM && "Null Source Module"); |
| 875 | |
| 876 | // Inherit the target data from the source module if the destination module |
| 877 | // doesn't have one already. |
| 878 | if (DstM->getDataLayout().empty() && !SrcM->getDataLayout().empty()) |
| 879 | DstM->setDataLayout(SrcM->getDataLayout()); |
| 880 | |
| 881 | // Copy the target triple from the source to dest if the dest's is empty. |
| 882 | if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty()) |
| 883 | DstM->setTargetTriple(SrcM->getTargetTriple()); |
| 884 | |
| 885 | if (!SrcM->getDataLayout().empty() && !DstM->getDataLayout().empty() && |
| 886 | SrcM->getDataLayout() != DstM->getDataLayout()) |
| 887 | errs() << "WARNING: Linking two modules of different data layouts!\n"; |
| 888 | if (!SrcM->getTargetTriple().empty() && |
| 889 | DstM->getTargetTriple() != SrcM->getTargetTriple()) { |
| 890 | errs() << "WARNING: Linking two modules of different target triples: "; |
| 891 | if (!SrcM->getModuleIdentifier().empty()) |
| 892 | errs() << SrcM->getModuleIdentifier() << ": "; |
| 893 | errs() << "'" << SrcM->getTargetTriple() << "' and '" |
| 894 | << DstM->getTargetTriple() << "'\n"; |
| 895 | } |
| 896 | |
| 897 | // Append the module inline asm string. |
| 898 | if (!SrcM->getModuleInlineAsm().empty()) { |
| 899 | if (DstM->getModuleInlineAsm().empty()) |
| 900 | DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm()); |
| 901 | else |
| 902 | DstM->setModuleInlineAsm(DstM->getModuleInlineAsm()+"\n"+ |
| 903 | SrcM->getModuleInlineAsm()); |
| 904 | } |
| 905 | |
| 906 | // Update the destination module's dependent libraries list with the libraries |
| 907 | // from the source module. There's no opportunity for duplicates here as the |
| 908 | // Module ensures that duplicate insertions are discarded. |
| 909 | for (Module::lib_iterator SI = SrcM->lib_begin(), SE = SrcM->lib_end(); |
| 910 | SI != SE; ++SI) |
| 911 | DstM->addLibrary(*SI); |
| 912 | |
| 913 | // If the source library's module id is in the dependent library list of the |
| 914 | // destination library, remove it since that module is now linked in. |
| 915 | StringRef ModuleId = SrcM->getModuleIdentifier(); |
| 916 | if (!ModuleId.empty()) |
| 917 | DstM->removeLibrary(sys::path::stem(ModuleId)); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 918 | |
| 919 | // Loop over all of the linked values to compute type mappings. |
| 920 | computeTypeMapping(); |
| 921 | |
| 922 | // Insert all of the globals in src into the DstM module... without linking |
| 923 | // initializers (which could refer to functions not yet mapped over). |
| 924 | for (Module::global_iterator I = SrcM->global_begin(), |
| 925 | E = SrcM->global_end(); I != E; ++I) |
| 926 | if (linkGlobalProto(I)) |
| 927 | return true; |
| 928 | |
| 929 | // Link the functions together between the two modules, without doing function |
| 930 | // bodies... this just adds external function prototypes to the DstM |
| 931 | // function... We do this so that when we begin processing function bodies, |
| 932 | // all of the global values that may be referenced are available in our |
| 933 | // ValueMap. |
| 934 | for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I) |
| 935 | if (linkFunctionProto(I)) |
| 936 | return true; |
| 937 | |
| 938 | // If there were any aliases, link them now. |
| 939 | for (Module::alias_iterator I = SrcM->alias_begin(), |
| 940 | E = SrcM->alias_end(); I != E; ++I) |
| 941 | if (linkAliasProto(I)) |
| 942 | return true; |
| 943 | |
| 944 | for (unsigned i = 0, e = AppendingVars.size(); i != e; ++i) |
| 945 | linkAppendingVarInit(AppendingVars[i]); |
| 946 | |
| 947 | // Update the initializers in the DstM module now that all globals that may |
| 948 | // be referenced are in DstM. |
| 949 | linkGlobalInits(); |
| 950 | |
| 951 | // Link in the function bodies that are defined in the source module into |
| 952 | // DstM. |
| 953 | for (Module::iterator SF = SrcM->begin(), E = SrcM->end(); SF != E; ++SF) { |
Tanya Lattner | 2b28a74 | 2011-10-14 22:17:46 +0000 | [diff] [blame] | 954 | |
| 955 | // Skip if not linking from source. |
| 956 | if (DoNotLinkFromSource.count(SF)) continue; |
| 957 | |
| 958 | // Skip if no body (function is external) or materialize. |
| 959 | if (SF->isDeclaration()) { |
| 960 | if (!SF->isMaterializable()) |
| 961 | continue; |
| 962 | if (SF->Materialize(&ErrorMsg)) |
| 963 | return true; |
| 964 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 965 | |
| 966 | linkFunctionBody(cast<Function>(ValueMap[SF]), SF); |
| 967 | } |
| 968 | |
| 969 | // Resolve all uses of aliases with aliasees. |
| 970 | linkAliasBodies(); |
| 971 | |
Devang Patel | 211da8f | 2011-08-04 19:44:28 +0000 | [diff] [blame] | 972 | // Remap all of the named mdnoes in Src into the DstM module. We do this |
| 973 | // after linking GlobalValues so that MDNodes that reference GlobalValues |
| 974 | // are properly remapped. |
| 975 | linkNamedMDNodes(); |
| 976 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 977 | // Now that all of the types from the source are used, resolve any structs |
| 978 | // copied over to the dest that didn't exist there. |
| 979 | TypeMap.linkDefinedTypeBodies(); |
| 980 | |
Anton Korobeynikov | 9f2ee70 | 2008-03-05 23:21:39 +0000 | [diff] [blame] | 981 | return false; |
| 982 | } |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 983 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 984 | //===----------------------------------------------------------------------===// |
| 985 | // LinkModules entrypoint. |
| 986 | //===----------------------------------------------------------------------===// |
| 987 | |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 988 | // LinkModules - This function links two modules together, with the resulting |
| 989 | // left module modified to be the composite of the two input modules. If an |
| 990 | // error occurs, true is returned and ErrorMsg (if not null) is set to indicate |
Chris Lattner | 5c377c5 | 2001-10-14 23:29:15 +0000 | [diff] [blame] | 991 | // the problem. Upon failure, the Dest module could be in a modified state, and |
| 992 | // shouldn't be relied on to be consistent. |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 993 | bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Mode, |
| 994 | std::string *ErrorMsg) { |
| 995 | ModuleLinker TheLinker(Dest, Src, Mode); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 996 | if (TheLinker.run()) { |
| 997 | if (ErrorMsg) *ErrorMsg = TheLinker.ErrorMsg; |
Reid Spencer | 619f024 | 2007-02-04 04:43:17 +0000 | [diff] [blame] | 998 | return true; |
Chris Lattner | 5a837de | 2004-08-04 07:44:58 +0000 | [diff] [blame] | 999 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1000 | |
Chris Lattner | 52f7e90 | 2001-10-13 07:03:50 +0000 | [diff] [blame] | 1001 | return false; |
| 1002 | } |