Mikhail Glushenkov | 59a5afa | 2009-03-03 10:04:23 +0000 | [diff] [blame] | 1 | //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===// |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LLVM module linker. |
| 11 | // |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 6cc07df | 2014-03-06 03:42:23 +0000 | [diff] [blame] | 14 | #include "llvm/Linker/Linker.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm-c/Linker.h" |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Optional.h" |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SetVector.h" |
Eli Bendersky | 0f7fd36 | 2013-03-19 15:26:24 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Module.h" |
Chandler Carruth | dcb603f | 2013-01-07 15:43:51 +0000 | [diff] [blame] | 21 | #include "llvm/IR/TypeFinder.h" |
Eli Bendersky | e17f370 | 2014-02-06 18:01:56 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/Cloning.h" |
Will Dietz | 981af00 | 2013-10-12 00:55:57 +0000 | [diff] [blame] | 26 | #include <cctype> |
David Majnemer | 82d6ff6 | 2014-06-27 18:38:12 +0000 | [diff] [blame] | 27 | #include <tuple> |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Eli Bendersky | e17f370 | 2014-02-06 18:01:56 +0000 | [diff] [blame] | 30 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // TypeMap implementation. |
| 33 | //===----------------------------------------------------------------------===// |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 34 | |
Chris Lattner | eee6f99 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 35 | namespace { |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 36 | typedef SmallPtrSet<StructType*, 32> TypeSet; |
| 37 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 38 | class TypeMapTy : public ValueMapTypeRemapper { |
| 39 | /// MappedTypes - This is a mapping from a source type to a destination type |
| 40 | /// to use. |
| 41 | DenseMap<Type*, Type*> MappedTypes; |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 42 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 43 | /// SpeculativeTypes - When checking to see if two subgraphs are isomorphic, |
| 44 | /// we speculatively add types to MappedTypes, but keep track of them here in |
| 45 | /// case we need to roll back. |
| 46 | SmallVector<Type*, 16> SpeculativeTypes; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 48 | /// SrcDefinitionsToResolve - This is a list of non-opaque structs in the |
| 49 | /// source module that are mapped to an opaque struct in the destination |
| 50 | /// module. |
| 51 | SmallVector<StructType*, 16> SrcDefinitionsToResolve; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 53 | /// DstResolvedOpaqueTypes - This is the set of opaque types in the |
| 54 | /// destination modules who are getting a body from the source module. |
| 55 | SmallPtrSet<StructType*, 16> DstResolvedOpaqueTypes; |
Bill Wendling | 8c2cc41 | 2012-03-22 20:30:41 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 56cdea6 | 2008-06-16 23:06:51 +0000 | [diff] [blame] | 57 | public: |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 58 | TypeMapTy(TypeSet &Set) : DstStructTypesSet(Set) {} |
| 59 | |
| 60 | TypeSet &DstStructTypesSet; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 61 | /// addTypeMapping - Indicate that the specified type in the destination |
| 62 | /// module is conceptually equivalent to the specified type in the source |
| 63 | /// module. |
| 64 | void addTypeMapping(Type *DstTy, Type *SrcTy); |
| 65 | |
| 66 | /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest |
| 67 | /// module from a type definition in the source module. |
| 68 | void linkDefinedTypeBodies(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 69 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 70 | /// get - Return the mapped type to use for the specified input type from the |
| 71 | /// source module. |
| 72 | Type *get(Type *SrcTy); |
| 73 | |
| 74 | FunctionType *get(FunctionType *T) {return cast<FunctionType>(get((Type*)T));} |
| 75 | |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 76 | /// dump - Dump out the type map for debugging purposes. |
| 77 | void dump() const { |
| 78 | for (DenseMap<Type*, Type*>::const_iterator |
| 79 | I = MappedTypes.begin(), E = MappedTypes.end(); I != E; ++I) { |
| 80 | dbgs() << "TypeMap: "; |
Justin Bogner | c0087f3 | 2014-08-12 03:24:59 +0000 | [diff] [blame] | 81 | I->first->print(dbgs()); |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 82 | dbgs() << " => "; |
Justin Bogner | c0087f3 | 2014-08-12 03:24:59 +0000 | [diff] [blame] | 83 | I->second->print(dbgs()); |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 84 | dbgs() << '\n'; |
| 85 | } |
| 86 | } |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 87 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 88 | private: |
| 89 | Type *getImpl(Type *T); |
| 90 | /// remapType - Implement the ValueMapTypeRemapper interface. |
Craig Topper | 8548299 | 2014-03-05 07:52:44 +0000 | [diff] [blame] | 91 | Type *remapType(Type *SrcTy) override { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 92 | return get(SrcTy); |
Chris Lattner | eee6f99 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 93 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 94 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 95 | bool areTypesIsomorphic(Type *DstTy, Type *SrcTy); |
Chris Lattner | eee6f99 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 96 | }; |
| 97 | } |
| 98 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 99 | void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) { |
| 100 | Type *&Entry = MappedTypes[SrcTy]; |
| 101 | if (Entry) return; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 102 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 103 | if (DstTy == SrcTy) { |
| 104 | Entry = DstTy; |
| 105 | return; |
| 106 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 107 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 108 | // Check to see if these types are recursively isomorphic and establish a |
| 109 | // mapping between them if so. |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 110 | if (!areTypesIsomorphic(DstTy, SrcTy)) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 111 | // Oops, they aren't isomorphic. Just discard this request by rolling out |
| 112 | // any speculative mappings we've established. |
| 113 | for (unsigned i = 0, e = SpeculativeTypes.size(); i != e; ++i) |
| 114 | MappedTypes.erase(SpeculativeTypes[i]); |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 115 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 116 | SpeculativeTypes.clear(); |
| 117 | } |
Chris Lattner | eee6f99 | 2008-06-16 21:00:18 +0000 | [diff] [blame] | 118 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 119 | /// areTypesIsomorphic - Recursively walk this pair of types, returning true |
| 120 | /// if they are isomorphic, false if they are not. |
| 121 | bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) { |
| 122 | // Two types with differing kinds are clearly not isomorphic. |
| 123 | if (DstTy->getTypeID() != SrcTy->getTypeID()) return false; |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 124 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 125 | // If we have an entry in the MappedTypes table, then we have our answer. |
| 126 | Type *&Entry = MappedTypes[SrcTy]; |
| 127 | if (Entry) |
| 128 | return Entry == DstTy; |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 129 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 130 | // Two identical types are clearly isomorphic. Remember this |
| 131 | // non-speculatively. |
| 132 | if (DstTy == SrcTy) { |
| 133 | Entry = DstTy; |
Chris Lattner | fe677e9 | 2008-06-16 20:03:01 +0000 | [diff] [blame] | 134 | return true; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 135 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 136 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 137 | // Okay, we have two types with identical kinds that we haven't seen before. |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 138 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 139 | // If this is an opaque struct type, special case it. |
| 140 | if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) { |
| 141 | // Mapping an opaque type to any struct, just keep the dest struct. |
| 142 | if (SSTy->isOpaque()) { |
| 143 | Entry = DstTy; |
| 144 | SpeculativeTypes.push_back(SrcTy); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 145 | return true; |
Chris Lattner | 9be1589 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 146 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 148 | // Mapping a non-opaque source type to an opaque dest. If this is the first |
| 149 | // type that we're mapping onto this destination type then we succeed. Keep |
| 150 | // the dest, but fill it in later. This doesn't need to be speculative. If |
| 151 | // this is the second (different) type that we're trying to map onto the |
| 152 | // same opaque type then we fail. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 153 | if (cast<StructType>(DstTy)->isOpaque()) { |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 154 | // We can only map one source type onto the opaque destination type. |
| 155 | if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy))) |
| 156 | return false; |
| 157 | SrcDefinitionsToResolve.push_back(SSTy); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 158 | Entry = DstTy; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 162 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 163 | // If the number of subtypes disagree between the two types, then we fail. |
| 164 | if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes()) |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 165 | return false; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 166 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 167 | // Fail if any of the extra properties (e.g. array size) of the type disagree. |
| 168 | if (isa<IntegerType>(DstTy)) |
| 169 | return false; // bitwidth disagrees. |
| 170 | if (PointerType *PT = dyn_cast<PointerType>(DstTy)) { |
| 171 | if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace()) |
| 172 | return false; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 173 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 174 | } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) { |
| 175 | if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg()) |
| 176 | return false; |
| 177 | } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) { |
| 178 | StructType *SSTy = cast<StructType>(SrcTy); |
Chris Lattner | 44f7ab4 | 2011-08-12 18:07:26 +0000 | [diff] [blame] | 179 | if (DSTy->isLiteral() != SSTy->isLiteral() || |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 180 | DSTy->isPacked() != SSTy->isPacked()) |
| 181 | return false; |
| 182 | } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) { |
| 183 | if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements()) |
| 184 | return false; |
| 185 | } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) { |
Joey Gouly | 5fad3e9 | 2013-01-10 10:49:36 +0000 | [diff] [blame] | 186 | if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements()) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 187 | return false; |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 188 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 189 | |
| 190 | // Otherwise, we speculate that these two types will line up and recursively |
| 191 | // check the subelements. |
| 192 | Entry = DstTy; |
| 193 | SpeculativeTypes.push_back(SrcTy); |
| 194 | |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 195 | for (unsigned i = 0, e = SrcTy->getNumContainedTypes(); i != e; ++i) |
| 196 | if (!areTypesIsomorphic(DstTy->getContainedType(i), |
| 197 | SrcTy->getContainedType(i))) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 198 | return false; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 199 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 200 | // If everything seems to have lined up, then everything is great. |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest |
| 205 | /// module from a type definition in the source module. |
| 206 | void TypeMapTy::linkDefinedTypeBodies() { |
| 207 | SmallVector<Type*, 16> Elements; |
| 208 | SmallString<16> TmpName; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 209 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 210 | // Note that processing entries in this loop (calling 'get') can add new |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 211 | // entries to the SrcDefinitionsToResolve vector. |
| 212 | while (!SrcDefinitionsToResolve.empty()) { |
| 213 | StructType *SrcSTy = SrcDefinitionsToResolve.pop_back_val(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 214 | StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 215 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 216 | // TypeMap is a many-to-one mapping, if there were multiple types that |
| 217 | // provide a body for DstSTy then previous iterations of this loop may have |
| 218 | // already handled it. Just ignore this case. |
| 219 | if (!DstSTy->isOpaque()) continue; |
| 220 | assert(!SrcSTy->isOpaque() && "Not resolving a definition?"); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 221 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 222 | // Map the body of the source type over to a new body for the dest type. |
| 223 | Elements.resize(SrcSTy->getNumElements()); |
| 224 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) |
| 225 | Elements[i] = getImpl(SrcSTy->getElementType(i)); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 226 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 227 | DstSTy->setBody(Elements, SrcSTy->isPacked()); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 228 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 229 | // If DstSTy has no name or has a longer name than STy, then viciously steal |
| 230 | // STy's name. |
| 231 | if (!SrcSTy->hasName()) continue; |
| 232 | StringRef SrcName = SrcSTy->getName(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 233 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 234 | if (!DstSTy->hasName() || DstSTy->getName().size() > SrcName.size()) { |
| 235 | TmpName.insert(TmpName.end(), SrcName.begin(), SrcName.end()); |
| 236 | SrcSTy->setName(""); |
| 237 | DstSTy->setName(TmpName.str()); |
| 238 | TmpName.clear(); |
| 239 | } |
| 240 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 242 | DstResolvedOpaqueTypes.clear(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 245 | /// get - Return the mapped type to use for the specified input type from the |
| 246 | /// source module. |
| 247 | Type *TypeMapTy::get(Type *Ty) { |
| 248 | Type *Result = getImpl(Ty); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 249 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 250 | // If this caused a reference to any struct type, resolve it before returning. |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 251 | if (!SrcDefinitionsToResolve.empty()) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 252 | linkDefinedTypeBodies(); |
| 253 | return Result; |
| 254 | } |
| 255 | |
| 256 | /// getImpl - This is the recursive version of get(). |
| 257 | Type *TypeMapTy::getImpl(Type *Ty) { |
| 258 | // If we already have an entry for this type, return it. |
| 259 | Type **Entry = &MappedTypes[Ty]; |
| 260 | if (*Entry) return *Entry; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 261 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 262 | // If this is not a named struct type, then just map all of the elements and |
| 263 | // then rebuild the type from inside out. |
Chris Lattner | 44f7ab4 | 2011-08-12 18:07:26 +0000 | [diff] [blame] | 264 | if (!isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral()) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 265 | // If there are no element types to map, then the type is itself. This is |
| 266 | // true for the anonymous {} struct, things like 'float', integers, etc. |
| 267 | if (Ty->getNumContainedTypes() == 0) |
| 268 | return *Entry = Ty; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 269 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 270 | // Remap all of the elements, keeping track of whether any of them change. |
| 271 | bool AnyChange = false; |
| 272 | SmallVector<Type*, 4> ElementTypes; |
| 273 | ElementTypes.resize(Ty->getNumContainedTypes()); |
| 274 | for (unsigned i = 0, e = Ty->getNumContainedTypes(); i != e; ++i) { |
| 275 | ElementTypes[i] = getImpl(Ty->getContainedType(i)); |
| 276 | AnyChange |= ElementTypes[i] != Ty->getContainedType(i); |
| 277 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 278 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 279 | // If we found our type while recursively processing stuff, just use it. |
| 280 | Entry = &MappedTypes[Ty]; |
| 281 | if (*Entry) return *Entry; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 282 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 283 | // If all of the element types mapped directly over, then the type is usable |
| 284 | // as-is. |
| 285 | if (!AnyChange) |
| 286 | return *Entry = Ty; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 287 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 288 | // Otherwise, rebuild a modified type. |
| 289 | switch (Ty->getTypeID()) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 290 | default: llvm_unreachable("unknown derived type to remap"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 291 | case Type::ArrayTyID: |
| 292 | return *Entry = ArrayType::get(ElementTypes[0], |
| 293 | cast<ArrayType>(Ty)->getNumElements()); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 294 | case Type::VectorTyID: |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 295 | return *Entry = VectorType::get(ElementTypes[0], |
| 296 | cast<VectorType>(Ty)->getNumElements()); |
| 297 | case Type::PointerTyID: |
| 298 | return *Entry = PointerType::get(ElementTypes[0], |
| 299 | cast<PointerType>(Ty)->getAddressSpace()); |
| 300 | case Type::FunctionTyID: |
| 301 | return *Entry = FunctionType::get(ElementTypes[0], |
Frits van Bommel | 717d7ed | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 302 | makeArrayRef(ElementTypes).slice(1), |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 303 | cast<FunctionType>(Ty)->isVarArg()); |
| 304 | case Type::StructTyID: |
| 305 | // Note that this is only reached for anonymous structs. |
| 306 | return *Entry = StructType::get(Ty->getContext(), ElementTypes, |
| 307 | cast<StructType>(Ty)->isPacked()); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Otherwise, this is an unmapped named struct. If the struct can be directly |
| 312 | // mapped over, just use it as-is. This happens in a case when the linked-in |
| 313 | // module has something like: |
| 314 | // %T = type {%T*, i32} |
| 315 | // @GV = global %T* null |
| 316 | // where T does not exist at all in the destination module. |
| 317 | // |
| 318 | // The other case we watch for is when the type is not in the destination |
| 319 | // module, but that it has to be rebuilt because it refers to something that |
| 320 | // is already mapped. For example, if the destination module has: |
| 321 | // %A = type { i32 } |
| 322 | // and the source module has something like |
| 323 | // %A' = type { i32 } |
| 324 | // %B = type { %A'* } |
| 325 | // @GV = global %B* null |
| 326 | // then we want to create a new type: "%B = type { %A*}" and have it take the |
| 327 | // pristine "%B" name from the source module. |
| 328 | // |
| 329 | // To determine which case this is, we have to recursively walk the type graph |
| 330 | // speculating that we'll be able to reuse it unmodified. Only if this is |
| 331 | // safe would we map the entire thing over. Because this is an optimization, |
| 332 | // and is not required for the prettiness of the linked module, we just skip |
| 333 | // it and always rebuild a type here. |
| 334 | StructType *STy = cast<StructType>(Ty); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 335 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 336 | // If the type is opaque, we can just use it directly. |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 337 | if (STy->isOpaque()) { |
| 338 | // A named structure type from src module is used. Add it to the Set of |
| 339 | // identified structs in the destination module. |
| 340 | DstStructTypesSet.insert(STy); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 341 | return *Entry = STy; |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 342 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 343 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 344 | // Otherwise we create a new type and resolve its body later. This will be |
| 345 | // resolved by the top level of get(). |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 346 | SrcDefinitionsToResolve.push_back(STy); |
| 347 | StructType *DTy = StructType::create(STy->getContext()); |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 348 | // A new identified structure type was created. Add it to the set of |
| 349 | // identified structs in the destination module. |
| 350 | DstStructTypesSet.insert(DTy); |
Chris Lattner | 5e3bd97 | 2011-12-20 00:03:52 +0000 | [diff] [blame] | 351 | DstResolvedOpaqueTypes.insert(DTy); |
| 352 | return *Entry = DTy; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 355 | //===----------------------------------------------------------------------===// |
| 356 | // ModuleLinker implementation. |
| 357 | //===----------------------------------------------------------------------===// |
| 358 | |
| 359 | namespace { |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 360 | class ModuleLinker; |
| 361 | |
| 362 | /// ValueMaterializerTy - Creates prototypes for functions that are lazily |
| 363 | /// linked on the fly. This speeds up linking for modules with many |
| 364 | /// lazily linked functions of which few get used. |
| 365 | class ValueMaterializerTy : public ValueMaterializer { |
| 366 | TypeMapTy &TypeMap; |
| 367 | Module *DstM; |
| 368 | std::vector<Function*> &LazilyLinkFunctions; |
| 369 | public: |
| 370 | ValueMaterializerTy(TypeMapTy &TypeMap, Module *DstM, |
| 371 | std::vector<Function*> &LazilyLinkFunctions) : |
| 372 | ValueMaterializer(), TypeMap(TypeMap), DstM(DstM), |
| 373 | LazilyLinkFunctions(LazilyLinkFunctions) { |
| 374 | } |
| 375 | |
Craig Topper | 8548299 | 2014-03-05 07:52:44 +0000 | [diff] [blame] | 376 | Value *materializeValueFor(Value *V) override; |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 377 | }; |
| 378 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 379 | /// ModuleLinker - This is an implementation class for the LinkModules |
| 380 | /// function, which is the entrypoint for this file. |
| 381 | class ModuleLinker { |
| 382 | Module *DstM, *SrcM; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 383 | |
| 384 | TypeMapTy TypeMap; |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 385 | ValueMaterializerTy ValMaterializer; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 386 | |
| 387 | /// ValueMap - Mapping of values from what they used to be in Src, to what |
| 388 | /// they are now in DstM. ValueToValueMapTy is a ValueMap, which involves |
| 389 | /// some overhead due to the use of Value handles which the Linker doesn't |
| 390 | /// actually need, but this allows us to reuse the ValueMapper code. |
| 391 | ValueToValueMapTy ValueMap; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 392 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 393 | struct AppendingVarInfo { |
| 394 | GlobalVariable *NewGV; // New aggregate global in dest module. |
| 395 | Constant *DstInit; // Old initializer from dest module. |
| 396 | Constant *SrcInit; // Old initializer from src module. |
| 397 | }; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 398 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 399 | std::vector<AppendingVarInfo> AppendingVars; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 400 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 401 | unsigned Mode; // Mode to treat source module. |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 402 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 403 | // Set of items not to link in from source. |
| 404 | SmallPtrSet<const Value*, 16> DoNotLinkFromSource; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 405 | |
Tanya Lattner | 0a48b87 | 2011-11-02 00:24:56 +0000 | [diff] [blame] | 406 | // Vector of functions to lazily link in. |
Bill Wendling | fa228782 | 2013-03-27 17:54:41 +0000 | [diff] [blame] | 407 | std::vector<Function*> LazilyLinkFunctions; |
Eli Bendersky | 7da92ed | 2014-02-20 22:19:24 +0000 | [diff] [blame] | 408 | |
| 409 | bool SuppressWarnings; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 410 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 411 | public: |
| 412 | std::string ErrorMsg; |
Eli Bendersky | 7da92ed | 2014-02-20 22:19:24 +0000 | [diff] [blame] | 413 | |
| 414 | ModuleLinker(Module *dstM, TypeSet &Set, Module *srcM, unsigned mode, |
| 415 | bool SuppressWarnings=false) |
| 416 | : DstM(dstM), SrcM(srcM), TypeMap(Set), |
| 417 | ValMaterializer(TypeMap, DstM, LazilyLinkFunctions), Mode(mode), |
| 418 | SuppressWarnings(SuppressWarnings) {} |
| 419 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 420 | bool run(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 421 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 422 | private: |
| 423 | /// emitError - Helper method for setting a message and returning an error |
| 424 | /// code. |
| 425 | bool emitError(const Twine &Message) { |
| 426 | ErrorMsg = Message.str(); |
Chris Lattner | 9995302 | 2008-06-16 18:27:53 +0000 | [diff] [blame] | 427 | return true; |
Chris Lattner | 9be1589 | 2008-06-16 21:17:12 +0000 | [diff] [blame] | 428 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 429 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 430 | bool getComdatLeader(Module *M, StringRef ComdatName, |
| 431 | const GlobalVariable *&GVar); |
| 432 | bool computeResultingSelectionKind(StringRef ComdatName, |
| 433 | Comdat::SelectionKind Src, |
| 434 | Comdat::SelectionKind Dst, |
| 435 | Comdat::SelectionKind &Result, |
| 436 | bool &LinkFromSrc); |
| 437 | std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>> |
| 438 | ComdatsChosen; |
| 439 | bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK, |
| 440 | bool &LinkFromSrc); |
| 441 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 442 | /// getLinkageResult - This analyzes the two global values and determines |
| 443 | /// what the result will look like in the destination module. |
| 444 | bool getLinkageResult(GlobalValue *Dest, const GlobalValue *Src, |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 445 | GlobalValue::LinkageTypes <, |
| 446 | GlobalValue::VisibilityTypes &Vis, |
| 447 | bool &LinkFromSrc); |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 448 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 449 | /// getLinkedToGlobal - Given a global in the source module, return the |
| 450 | /// global in the destination module that is being linked to, if any. |
| 451 | GlobalValue *getLinkedToGlobal(GlobalValue *SrcGV) { |
| 452 | // If the source has no name it can't link. If it has local linkage, |
| 453 | // there is no name match-up going on. |
| 454 | if (!SrcGV->hasName() || SrcGV->hasLocalLinkage()) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 455 | return nullptr; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 456 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 457 | // Otherwise see if we have a match in the destination module's symtab. |
| 458 | GlobalValue *DGV = DstM->getNamedValue(SrcGV->getName()); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 459 | if (!DGV) return nullptr; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 460 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 461 | // If we found a global with the same name in the dest module, but it has |
| 462 | // internal linkage, we are really not doing any linkage here. |
| 463 | if (DGV->hasLocalLinkage()) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 464 | return nullptr; |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 465 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 466 | // Otherwise, we do in fact link to the destination global. |
| 467 | return DGV; |
| 468 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 469 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 470 | void computeTypeMapping(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 471 | |
Duncan P. N. Exon Smith | 09d84ad | 2014-08-12 16:46:37 +0000 | [diff] [blame] | 472 | void upgradeMismatchedGlobalArray(StringRef Name); |
| 473 | void upgradeMismatchedGlobals(); |
| 474 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 475 | bool linkAppendingVarProto(GlobalVariable *DstGV, GlobalVariable *SrcGV); |
| 476 | bool linkGlobalProto(GlobalVariable *SrcGV); |
| 477 | bool linkFunctionProto(Function *SrcF); |
| 478 | bool linkAliasProto(GlobalAlias *SrcA); |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 479 | bool linkModuleFlagsMetadata(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 480 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 481 | void linkAppendingVarInit(const AppendingVarInfo &AVI); |
| 482 | void linkGlobalInits(); |
| 483 | void linkFunctionBody(Function *Dst, Function *Src); |
| 484 | void linkAliasBodies(); |
| 485 | void linkNamedMDNodes(); |
| 486 | }; |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 489 | /// forceRenaming - The LLVM SymbolTable class autorenames globals that conflict |
Reid Spencer | 90246aa | 2007-02-04 04:29:21 +0000 | [diff] [blame] | 490 | /// in the symbol table. This is good for all clients except for us. Go |
| 491 | /// through the trouble to force this back. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 492 | static void forceRenaming(GlobalValue *GV, StringRef Name) { |
| 493 | // If the global doesn't force its name or if it already has the right name, |
| 494 | // there is nothing for us to do. |
| 495 | if (GV->hasLocalLinkage() || GV->getName() == Name) |
| 496 | return; |
| 497 | |
| 498 | Module *M = GV->getParent(); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 499 | |
| 500 | // If there is a conflict, rename the conflict. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 501 | if (GlobalValue *ConflictGV = M->getNamedValue(Name)) { |
Chris Lattner | 2a8d2e0 | 2007-02-11 00:39:38 +0000 | [diff] [blame] | 502 | GV->takeName(ConflictGV); |
| 503 | ConflictGV->setName(Name); // This will cause ConflictGV to get renamed |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 504 | assert(ConflictGV->getName() != Name && "forceRenaming didn't work"); |
Chris Lattner | 2a8d2e0 | 2007-02-11 00:39:38 +0000 | [diff] [blame] | 505 | } else { |
| 506 | GV->setName(Name); // Force the name back |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 507 | } |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 508 | } |
Reid Spencer | 90246aa | 2007-02-04 04:29:21 +0000 | [diff] [blame] | 509 | |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 510 | /// copyGVAttributes - copy additional attributes (those not needed to construct |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 511 | /// a GlobalValue) from the SrcGV to the DestGV. |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 512 | static void copyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) { |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 513 | // Use the maximum alignment, rather than just copying the alignment of SrcGV. |
Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 514 | auto *DestGO = dyn_cast<GlobalObject>(DestGV); |
Rafael Espindola | a7d9c69 | 2014-05-06 14:51:36 +0000 | [diff] [blame] | 515 | unsigned Alignment; |
Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 516 | if (DestGO) |
| 517 | Alignment = std::max(DestGO->getAlignment(), SrcGV->getAlignment()); |
Rafael Espindola | a7d9c69 | 2014-05-06 14:51:36 +0000 | [diff] [blame] | 518 | |
Duncan Sands | dd7daee | 2008-05-26 19:58:59 +0000 | [diff] [blame] | 519 | DestGV->copyAttributesFrom(SrcGV); |
Rafael Espindola | a7d9c69 | 2014-05-06 14:51:36 +0000 | [diff] [blame] | 520 | |
Rafael Espindola | 99e05cf | 2014-05-13 18:45:48 +0000 | [diff] [blame] | 521 | if (DestGO) |
| 522 | DestGO->setAlignment(Alignment); |
Rafael Espindola | a7d9c69 | 2014-05-06 14:51:36 +0000 | [diff] [blame] | 523 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 524 | forceRenaming(DestGV, SrcGV->getName()); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 527 | static bool isLessConstraining(GlobalValue::VisibilityTypes a, |
| 528 | GlobalValue::VisibilityTypes b) { |
| 529 | if (a == GlobalValue::HiddenVisibility) |
| 530 | return false; |
| 531 | if (b == GlobalValue::HiddenVisibility) |
| 532 | return true; |
| 533 | if (a == GlobalValue::ProtectedVisibility) |
| 534 | return false; |
| 535 | if (b == GlobalValue::ProtectedVisibility) |
| 536 | return true; |
| 537 | return false; |
| 538 | } |
| 539 | |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 540 | Value *ValueMaterializerTy::materializeValueFor(Value *V) { |
| 541 | Function *SF = dyn_cast<Function>(V); |
| 542 | if (!SF) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 543 | return nullptr; |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 544 | |
| 545 | Function *DF = Function::Create(TypeMap.get(SF->getFunctionType()), |
| 546 | SF->getLinkage(), SF->getName(), DstM); |
| 547 | copyGVAttributes(DF, SF); |
| 548 | |
Rafael Espindola | 3931c28 | 2014-08-15 20:17:08 +0000 | [diff] [blame^] | 549 | if (Comdat *SC = SF->getComdat()) { |
| 550 | Comdat *DC = DstM->getOrInsertComdat(SC->getName()); |
| 551 | DF->setComdat(DC); |
| 552 | } |
| 553 | |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 554 | LazilyLinkFunctions.push_back(SF); |
| 555 | return DF; |
| 556 | } |
| 557 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 558 | bool ModuleLinker::getComdatLeader(Module *M, StringRef ComdatName, |
| 559 | const GlobalVariable *&GVar) { |
| 560 | const GlobalValue *GVal = M->getNamedValue(ComdatName); |
| 561 | if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) { |
| 562 | GVal = GA->getBaseObject(); |
| 563 | if (!GVal) |
| 564 | // We cannot resolve the size of the aliasee yet. |
| 565 | return emitError("Linking COMDATs named '" + ComdatName + |
| 566 | "': COMDAT key involves incomputable alias size."); |
| 567 | } |
| 568 | |
| 569 | GVar = dyn_cast_or_null<GlobalVariable>(GVal); |
| 570 | if (!GVar) |
| 571 | return emitError( |
| 572 | "Linking COMDATs named '" + ComdatName + |
| 573 | "': GlobalVariable required for data dependent selection!"); |
| 574 | |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName, |
| 579 | Comdat::SelectionKind Src, |
| 580 | Comdat::SelectionKind Dst, |
| 581 | Comdat::SelectionKind &Result, |
| 582 | bool &LinkFromSrc) { |
| 583 | // The ability to mix Comdat::SelectionKind::Any with |
| 584 | // Comdat::SelectionKind::Largest is a behavior that comes from COFF. |
| 585 | bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any || |
| 586 | Dst == Comdat::SelectionKind::Largest; |
| 587 | bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any || |
| 588 | Src == Comdat::SelectionKind::Largest; |
| 589 | if (DstAnyOrLargest && SrcAnyOrLargest) { |
| 590 | if (Dst == Comdat::SelectionKind::Largest || |
| 591 | Src == Comdat::SelectionKind::Largest) |
| 592 | Result = Comdat::SelectionKind::Largest; |
| 593 | else |
| 594 | Result = Comdat::SelectionKind::Any; |
| 595 | } else if (Src == Dst) { |
| 596 | Result = Dst; |
| 597 | } else { |
| 598 | return emitError("Linking COMDATs named '" + ComdatName + |
| 599 | "': invalid selection kinds!"); |
| 600 | } |
| 601 | |
| 602 | switch (Result) { |
| 603 | case Comdat::SelectionKind::Any: |
| 604 | // Go with Dst. |
| 605 | LinkFromSrc = false; |
| 606 | break; |
| 607 | case Comdat::SelectionKind::NoDuplicates: |
| 608 | return emitError("Linking COMDATs named '" + ComdatName + |
| 609 | "': noduplicates has been violated!"); |
| 610 | case Comdat::SelectionKind::ExactMatch: |
| 611 | case Comdat::SelectionKind::Largest: |
| 612 | case Comdat::SelectionKind::SameSize: { |
| 613 | const GlobalVariable *DstGV; |
| 614 | const GlobalVariable *SrcGV; |
| 615 | if (getComdatLeader(DstM, ComdatName, DstGV) || |
| 616 | getComdatLeader(SrcM, ComdatName, SrcGV)) |
| 617 | return true; |
| 618 | |
| 619 | const DataLayout *DstDL = DstM->getDataLayout(); |
| 620 | const DataLayout *SrcDL = SrcM->getDataLayout(); |
| 621 | if (!DstDL || !SrcDL) { |
| 622 | return emitError( |
| 623 | "Linking COMDATs named '" + ComdatName + |
| 624 | "': can't do size dependent selection without DataLayout!"); |
| 625 | } |
| 626 | uint64_t DstSize = |
| 627 | DstDL->getTypeAllocSize(DstGV->getType()->getPointerElementType()); |
| 628 | uint64_t SrcSize = |
| 629 | SrcDL->getTypeAllocSize(SrcGV->getType()->getPointerElementType()); |
| 630 | if (Result == Comdat::SelectionKind::ExactMatch) { |
| 631 | if (SrcGV->getInitializer() != DstGV->getInitializer()) |
| 632 | return emitError("Linking COMDATs named '" + ComdatName + |
| 633 | "': ExactMatch violated!"); |
| 634 | LinkFromSrc = false; |
| 635 | } else if (Result == Comdat::SelectionKind::Largest) { |
| 636 | LinkFromSrc = SrcSize > DstSize; |
| 637 | } else if (Result == Comdat::SelectionKind::SameSize) { |
| 638 | if (SrcSize != DstSize) |
| 639 | return emitError("Linking COMDATs named '" + ComdatName + |
| 640 | "': SameSize violated!"); |
| 641 | LinkFromSrc = false; |
| 642 | } else { |
| 643 | llvm_unreachable("unknown selection kind"); |
| 644 | } |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | return false; |
| 650 | } |
| 651 | |
| 652 | bool ModuleLinker::getComdatResult(const Comdat *SrcC, |
| 653 | Comdat::SelectionKind &Result, |
| 654 | bool &LinkFromSrc) { |
Rafael Espindola | b16196a | 2014-08-11 17:07:34 +0000 | [diff] [blame] | 655 | Comdat::SelectionKind SSK = SrcC->getSelectionKind(); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 656 | StringRef ComdatName = SrcC->getName(); |
| 657 | Module::ComdatSymTabType &ComdatSymTab = DstM->getComdatSymbolTable(); |
| 658 | Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName); |
Rafael Espindola | 2ef3f29 | 2014-08-11 16:55:42 +0000 | [diff] [blame] | 659 | |
Rafael Espindola | b16196a | 2014-08-11 17:07:34 +0000 | [diff] [blame] | 660 | if (DstCI == ComdatSymTab.end()) { |
| 661 | // Use the comdat if it is only available in one of the modules. |
| 662 | LinkFromSrc = true; |
| 663 | Result = SSK; |
Rafael Espindola | 2ef3f29 | 2014-08-11 16:55:42 +0000 | [diff] [blame] | 664 | return false; |
Rafael Espindola | b16196a | 2014-08-11 17:07:34 +0000 | [diff] [blame] | 665 | } |
Rafael Espindola | 2ef3f29 | 2014-08-11 16:55:42 +0000 | [diff] [blame] | 666 | |
| 667 | const Comdat *DstC = &DstCI->second; |
Rafael Espindola | 2ef3f29 | 2014-08-11 16:55:42 +0000 | [diff] [blame] | 668 | Comdat::SelectionKind DSK = DstC->getSelectionKind(); |
| 669 | return computeResultingSelectionKind(ComdatName, SSK, DSK, Result, |
| 670 | LinkFromSrc); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 671 | } |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 672 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 673 | /// getLinkageResult - This analyzes the two global values and determines what |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 674 | /// the result will look like in the destination module. In particular, it |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 675 | /// computes the resultant linkage type and visibility, computes whether the |
| 676 | /// global in the source should be copied over to the destination (replacing |
| 677 | /// the existing one), and computes whether this linkage is an error or not. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 678 | bool ModuleLinker::getLinkageResult(GlobalValue *Dest, const GlobalValue *Src, |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 679 | GlobalValue::LinkageTypes <, |
| 680 | GlobalValue::VisibilityTypes &Vis, |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 681 | bool &LinkFromSrc) { |
| 682 | assert(Dest && "Must have two globals being queried"); |
| 683 | assert(!Src->hasLocalLinkage() && |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 684 | "If Src has internal linkage, Dest shouldn't be set!"); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 685 | |
Peter Collingbourne | 8bb15d8 | 2011-10-30 17:46:34 +0000 | [diff] [blame] | 686 | bool SrcIsDeclaration = Src->isDeclaration() && !Src->isMaterializable(); |
Chris Lattner | 0c134b5 | 2011-07-14 20:23:05 +0000 | [diff] [blame] | 687 | bool DestIsDeclaration = Dest->isDeclaration(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 688 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 689 | if (SrcIsDeclaration) { |
Anton Korobeynikov | 1f93c50 | 2008-03-10 22:33:22 +0000 | [diff] [blame] | 690 | // If Src is external or if both Src & Dest are external.. Just link the |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 691 | // external globals, we aren't adding anything. |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 692 | if (Src->hasDLLImportStorageClass()) { |
| 693 | // If one of GVs is marked as DLLImport, result should be dllimport'ed. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 694 | if (DestIsDeclaration) { |
Anton Korobeynikov | d61d39e | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 695 | LinkFromSrc = true; |
| 696 | LT = Src->getLinkage(); |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 697 | } |
Andrew Lenharth | e06036d | 2006-12-15 17:35:32 +0000 | [diff] [blame] | 698 | } else if (Dest->hasExternalWeakLinkage()) { |
Duncan Sands | 12da8ce | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 699 | // If the Dest is weak, use the source linkage. |
Andrew Lenharth | e06036d | 2006-12-15 17:35:32 +0000 | [diff] [blame] | 700 | LinkFromSrc = true; |
| 701 | LT = Src->getLinkage(); |
Anton Korobeynikov | d61d39e | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 702 | } else { |
| 703 | LinkFromSrc = false; |
| 704 | LT = Dest->getLinkage(); |
| 705 | } |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 706 | } else if (DestIsDeclaration && !Dest->hasDLLImportStorageClass()) { |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 707 | // If Dest is external but Src is not: |
| 708 | LinkFromSrc = true; |
| 709 | LT = Src->getLinkage(); |
Duncan Sands | d725c99 | 2009-03-08 13:35:23 +0000 | [diff] [blame] | 710 | } else if (Src->isWeakForLinker()) { |
Dale Johannesen | ce4396b | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 711 | // At this point we know that Dest has LinkOnce, External*, Weak, Common, |
| 712 | // or DLL* linkage. |
Chris Lattner | 184f1be | 2009-04-13 05:44:34 +0000 | [diff] [blame] | 713 | if (Dest->hasExternalWeakLinkage() || |
| 714 | Dest->hasAvailableExternallyLinkage() || |
| 715 | (Dest->hasLinkOnceLinkage() && |
| 716 | (Src->hasWeakLinkage() || Src->hasCommonLinkage()))) { |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 717 | LinkFromSrc = true; |
| 718 | LT = Src->getLinkage(); |
| 719 | } else { |
| 720 | LinkFromSrc = false; |
| 721 | LT = Dest->getLinkage(); |
| 722 | } |
Duncan Sands | d725c99 | 2009-03-08 13:35:23 +0000 | [diff] [blame] | 723 | } else if (Dest->isWeakForLinker()) { |
Anton Korobeynikov | 12c9494 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 724 | // At this point we know that Src has External* or DLL* linkage. |
| 725 | if (Src->hasExternalWeakLinkage()) { |
| 726 | LinkFromSrc = false; |
| 727 | LT = Dest->getLinkage(); |
| 728 | } else { |
| 729 | LinkFromSrc = true; |
| 730 | LT = GlobalValue::ExternalLinkage; |
| 731 | } |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 732 | } else { |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 733 | assert((Dest->hasExternalLinkage() || Dest->hasExternalWeakLinkage()) && |
| 734 | (Src->hasExternalLinkage() || Src->hasExternalWeakLinkage()) && |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 735 | "Unexpected linkage type!"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 736 | return emitError("Linking globals named '" + Src->getName() + |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 737 | "': symbol multiply defined!"); |
| 738 | } |
Anton Korobeynikov | 31fc4f9 | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 739 | |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 740 | // Compute the visibility. We follow the rules in the System V Application |
| 741 | // Binary Interface. |
Duncan P. N. Exon Smith | b2becfd | 2014-05-07 22:55:46 +0000 | [diff] [blame] | 742 | assert(!GlobalValue::isLocalLinkage(LT) && |
| 743 | "Symbols with local linkage should not be merged"); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 744 | Vis = isLessConstraining(Src->getVisibility(), Dest->getVisibility()) ? |
| 745 | Dest->getVisibility() : Src->getVisibility(); |
Chris Lattner | fc61de3 | 2004-12-03 22:18:41 +0000 | [diff] [blame] | 746 | return false; |
| 747 | } |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 748 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 749 | /// computeTypeMapping - Loop over all of the linked values to compute type |
| 750 | /// mappings. For example, if we link "extern Foo *x" and "Foo *x = NULL", then |
| 751 | /// we have two struct types 'Foo' but one got renamed when the module was |
| 752 | /// loaded into the same LLVMContext. |
| 753 | void ModuleLinker::computeTypeMapping() { |
| 754 | // Incorporate globals. |
| 755 | for (Module::global_iterator I = SrcM->global_begin(), |
| 756 | E = SrcM->global_end(); I != E; ++I) { |
| 757 | GlobalValue *DGV = getLinkedToGlobal(I); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 758 | if (!DGV) continue; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 759 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 760 | if (!DGV->hasAppendingLinkage() || !I->hasAppendingLinkage()) { |
| 761 | TypeMap.addTypeMapping(DGV->getType(), I->getType()); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 762 | continue; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 763 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 764 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 765 | // Unify the element type of appending arrays. |
| 766 | ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType()); |
| 767 | ArrayType *SAT = cast<ArrayType>(I->getType()->getElementType()); |
| 768 | TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType()); |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 769 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 770 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 771 | // Incorporate functions. |
| 772 | for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I) { |
| 773 | if (GlobalValue *DGV = getLinkedToGlobal(I)) |
| 774 | TypeMap.addTypeMapping(DGV->getType(), I->getType()); |
| 775 | } |
Bill Wendling | 7b46461 | 2012-02-27 22:34:19 +0000 | [diff] [blame] | 776 | |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 777 | // Incorporate types by name, scanning all the types in the source module. |
| 778 | // At this point, the destination module may have a type "%foo = { i32 }" for |
Bill Wendling | 2b3f61a | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 779 | // example. When the source module got loaded into the same LLVMContext, if |
| 780 | // it had the same type, it would have been renamed to "%foo.42 = { i32 }". |
Bill Wendling | 8555a37 | 2012-08-03 00:30:35 +0000 | [diff] [blame] | 781 | TypeFinder SrcStructTypes; |
| 782 | SrcStructTypes.run(*SrcM, true); |
Bill Wendling | 2b3f61a | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 783 | SmallPtrSet<StructType*, 32> SrcStructTypesSet(SrcStructTypes.begin(), |
| 784 | SrcStructTypes.end()); |
Bill Wendling | 8737480 | 2012-03-23 23:17:38 +0000 | [diff] [blame] | 785 | |
Bill Wendling | 2b3f61a | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 786 | for (unsigned i = 0, e = SrcStructTypes.size(); i != e; ++i) { |
| 787 | StructType *ST = SrcStructTypes[i]; |
| 788 | if (!ST->hasName()) continue; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 789 | |
Bill Wendling | 2b3f61a | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 790 | // Check to see if there is a dot in the name followed by a digit. |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 791 | size_t DotPos = ST->getName().rfind('.'); |
| 792 | if (DotPos == 0 || DotPos == StringRef::npos || |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 793 | ST->getName().back() == '.' || |
| 794 | !isdigit(static_cast<unsigned char>(ST->getName()[DotPos+1]))) |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 795 | continue; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 796 | |
Bill Wendling | 2b3f61a | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 797 | // Check to see if the destination module has a struct with the prefix name. |
Bill Wendling | d48b778 | 2012-02-28 04:01:21 +0000 | [diff] [blame] | 798 | if (StructType *DST = DstM->getTypeByName(ST->getName().substr(0, DotPos))) |
Bill Wendling | 8737480 | 2012-03-23 23:17:38 +0000 | [diff] [blame] | 799 | // Don't use it if this actually came from the source module. They're in |
| 800 | // the same LLVMContext after all. Also don't use it unless the type is |
| 801 | // actually used in the destination module. This can happen in situations |
| 802 | // like this: |
| 803 | // |
| 804 | // Module A Module B |
| 805 | // -------- -------- |
| 806 | // %Z = type { %A } %B = type { %C.1 } |
| 807 | // %A = type { %B.1, [7 x i8] } %C.1 = type { i8* } |
| 808 | // %B.1 = type { %C } %A.2 = type { %B.3, [5 x i8] } |
| 809 | // %C = type { i8* } %B.3 = type { %C.1 } |
| 810 | // |
| 811 | // When we link Module B with Module A, the '%B' in Module B is |
| 812 | // used. However, that would then use '%C.1'. But when we process '%C.1', |
| 813 | // we prefer to take the '%C' version. So we are then left with both |
| 814 | // '%C.1' and '%C' being used for the same types. This leads to some |
| 815 | // variables using one type and some using the other. |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 816 | if (!SrcStructTypesSet.count(DST) && TypeMap.DstStructTypesSet.count(DST)) |
Bill Wendling | 2b3f61a | 2012-02-27 23:48:30 +0000 | [diff] [blame] | 817 | TypeMap.addTypeMapping(DST, ST); |
| 818 | } |
| 819 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 820 | // Don't bother incorporating aliases, they aren't generally typed well. |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 821 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 822 | // Now that we have discovered all of the type equivalences, get a body for |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 823 | // any 'opaque' types in the dest module that are now resolved. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 824 | TypeMap.linkDefinedTypeBodies(); |
Devang Patel | 5c310be | 2009-08-11 18:01:24 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Duncan P. N. Exon Smith | 09d84ad | 2014-08-12 16:46:37 +0000 | [diff] [blame] | 827 | static void upgradeGlobalArray(GlobalVariable *GV) { |
| 828 | ArrayType *ATy = cast<ArrayType>(GV->getType()->getElementType()); |
| 829 | StructType *OldTy = cast<StructType>(ATy->getElementType()); |
| 830 | assert(OldTy->getNumElements() == 2 && "Expected to upgrade from 2 elements"); |
| 831 | |
| 832 | // Get the upgraded 3 element type. |
| 833 | PointerType *VoidPtrTy = Type::getInt8Ty(GV->getContext())->getPointerTo(); |
| 834 | Type *Tys[3] = {OldTy->getElementType(0), OldTy->getElementType(1), |
| 835 | VoidPtrTy}; |
| 836 | StructType *NewTy = StructType::get(GV->getContext(), Tys, false); |
| 837 | |
| 838 | // Build new constants with a null third field filled in. |
| 839 | Constant *OldInitC = GV->getInitializer(); |
| 840 | ConstantArray *OldInit = dyn_cast<ConstantArray>(OldInitC); |
| 841 | if (!OldInit && !isa<ConstantAggregateZero>(OldInitC)) |
| 842 | // Invalid initializer; give up. |
| 843 | return; |
| 844 | std::vector<Constant *> Initializers; |
| 845 | if (OldInit && OldInit->getNumOperands()) { |
| 846 | Value *Null = Constant::getNullValue(VoidPtrTy); |
| 847 | for (Use &U : OldInit->operands()) { |
| 848 | ConstantStruct *Init = cast<ConstantStruct>(U.get()); |
| 849 | Initializers.push_back(ConstantStruct::get( |
| 850 | NewTy, Init->getOperand(0), Init->getOperand(1), Null, nullptr)); |
| 851 | } |
| 852 | } |
| 853 | assert(Initializers.size() == ATy->getNumElements() && |
| 854 | "Failed to copy all array elements"); |
| 855 | |
| 856 | // Replace the old GV with a new one. |
| 857 | ATy = ArrayType::get(NewTy, Initializers.size()); |
| 858 | Constant *NewInit = ConstantArray::get(ATy, Initializers); |
| 859 | GlobalVariable *NewGV = new GlobalVariable( |
| 860 | *GV->getParent(), ATy, GV->isConstant(), GV->getLinkage(), NewInit, "", |
| 861 | GV, GV->getThreadLocalMode(), GV->getType()->getAddressSpace(), |
| 862 | GV->isExternallyInitialized()); |
| 863 | NewGV->copyAttributesFrom(GV); |
| 864 | NewGV->takeName(GV); |
| 865 | assert(GV->use_empty() && "program cannot use initializer list"); |
| 866 | GV->eraseFromParent(); |
| 867 | } |
| 868 | |
| 869 | void ModuleLinker::upgradeMismatchedGlobalArray(StringRef Name) { |
| 870 | // Look for the global arrays. |
| 871 | auto *DstGV = dyn_cast_or_null<GlobalVariable>(DstM->getNamedValue(Name)); |
| 872 | if (!DstGV) |
| 873 | return; |
| 874 | auto *SrcGV = dyn_cast_or_null<GlobalVariable>(SrcM->getNamedValue(Name)); |
| 875 | if (!SrcGV) |
| 876 | return; |
| 877 | |
| 878 | // Check if the types already match. |
| 879 | auto *DstTy = cast<ArrayType>(DstGV->getType()->getElementType()); |
| 880 | auto *SrcTy = |
| 881 | cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType())); |
| 882 | if (DstTy == SrcTy) |
| 883 | return; |
| 884 | |
| 885 | // Grab the element types. We can only upgrade an array of a two-field |
| 886 | // struct. Only bother if the other one has three-fields. |
| 887 | auto *DstEltTy = cast<StructType>(DstTy->getElementType()); |
| 888 | auto *SrcEltTy = cast<StructType>(SrcTy->getElementType()); |
| 889 | if (DstEltTy->getNumElements() == 2 && SrcEltTy->getNumElements() == 3) { |
| 890 | upgradeGlobalArray(DstGV); |
| 891 | return; |
| 892 | } |
| 893 | if (DstEltTy->getNumElements() == 3 && SrcEltTy->getNumElements() == 2) |
| 894 | upgradeGlobalArray(SrcGV); |
| 895 | |
| 896 | // We can't upgrade any other differences. |
| 897 | } |
| 898 | |
| 899 | void ModuleLinker::upgradeMismatchedGlobals() { |
| 900 | upgradeMismatchedGlobalArray("llvm.global_ctors"); |
| 901 | upgradeMismatchedGlobalArray("llvm.global_dtors"); |
| 902 | } |
| 903 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 904 | /// linkAppendingVarProto - If there were any appending global variables, link |
| 905 | /// them together now. Return true on error. |
| 906 | bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV, |
| 907 | GlobalVariable *SrcGV) { |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 908 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 909 | if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage()) |
| 910 | return emitError("Linking globals named '" + SrcGV->getName() + |
| 911 | "': can only link appending global with another appending global!"); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 912 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 913 | ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType()); |
| 914 | ArrayType *SrcTy = |
| 915 | cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType())); |
| 916 | Type *EltTy = DstTy->getElementType(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 917 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 918 | // Check to see that they two arrays agree on type. |
| 919 | if (EltTy != SrcTy->getElementType()) |
| 920 | return emitError("Appending variables with different element types!"); |
| 921 | if (DstGV->isConstant() != SrcGV->isConstant()) |
| 922 | return emitError("Appending variables linked with different const'ness!"); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 923 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 924 | if (DstGV->getAlignment() != SrcGV->getAlignment()) |
| 925 | return emitError( |
| 926 | "Appending variables with different alignment need to be linked!"); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 927 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 928 | if (DstGV->getVisibility() != SrcGV->getVisibility()) |
| 929 | return emitError( |
| 930 | "Appending variables with different visibility need to be linked!"); |
Rafael Espindola | fac3a01 | 2013-09-04 15:33:34 +0000 | [diff] [blame] | 931 | |
| 932 | if (DstGV->hasUnnamedAddr() != SrcGV->hasUnnamedAddr()) |
| 933 | return emitError( |
| 934 | "Appending variables with different unnamed_addr need to be linked!"); |
| 935 | |
Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 936 | if (StringRef(DstGV->getSection()) != SrcGV->getSection()) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 937 | return emitError( |
| 938 | "Appending variables with different section name need to be linked!"); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 939 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 940 | uint64_t NewSize = DstTy->getNumElements() + SrcTy->getNumElements(); |
| 941 | ArrayType *NewType = ArrayType::get(EltTy, NewSize); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 942 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 943 | // Create the new global variable. |
| 944 | GlobalVariable *NG = |
| 945 | new GlobalVariable(*DstGV->getParent(), NewType, SrcGV->isConstant(), |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 946 | DstGV->getLinkage(), /*init*/nullptr, /*name*/"", DstGV, |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 947 | DstGV->getThreadLocalMode(), |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 948 | DstGV->getType()->getAddressSpace()); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 949 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 950 | // Propagate alignment, visibility and section info. |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 951 | copyGVAttributes(NG, DstGV); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 952 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 953 | AppendingVarInfo AVI; |
| 954 | AVI.NewGV = NG; |
| 955 | AVI.DstInit = DstGV->getInitializer(); |
| 956 | AVI.SrcInit = SrcGV->getInitializer(); |
| 957 | AppendingVars.push_back(AVI); |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 958 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 959 | // Replace any uses of the two global variables with uses of the new |
| 960 | // global. |
| 961 | ValueMap[SrcGV] = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType())); |
Anton Korobeynikov | e79f4c7 | 2008-03-10 22:34:28 +0000 | [diff] [blame] | 962 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 963 | DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType())); |
| 964 | DstGV->eraseFromParent(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 965 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 966 | // Track the source variable so we don't try to link it. |
| 967 | DoNotLinkFromSource.insert(SrcGV); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 968 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 969 | return false; |
| 970 | } |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 971 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 972 | /// linkGlobalProto - Loop through the global variables in the src module and |
| 973 | /// merge them into the dest module. |
| 974 | bool ModuleLinker::linkGlobalProto(GlobalVariable *SGV) { |
| 975 | GlobalValue *DGV = getLinkedToGlobal(SGV); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 976 | llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility; |
Rafael Espindola | d4885da | 2013-09-04 14:05:09 +0000 | [diff] [blame] | 977 | bool HasUnnamedAddr = SGV->hasUnnamedAddr(); |
Mikhail Glushenkov | 766d489 | 2009-03-03 07:22:23 +0000 | [diff] [blame] | 978 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 979 | bool LinkFromSrc = false; |
| 980 | Comdat *DC = nullptr; |
| 981 | if (const Comdat *SC = SGV->getComdat()) { |
| 982 | Comdat::SelectionKind SK; |
| 983 | std::tie(SK, LinkFromSrc) = ComdatsChosen[SC]; |
| 984 | DC = DstM->getOrInsertComdat(SC->getName()); |
| 985 | DC->setSelectionKind(SK); |
| 986 | } |
| 987 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 988 | if (DGV) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 989 | if (!DC) { |
| 990 | // Concatenation of appending linkage variables is magic and handled later. |
| 991 | if (DGV->hasAppendingLinkage() || SGV->hasAppendingLinkage()) |
| 992 | return linkAppendingVarProto(cast<GlobalVariable>(DGV), SGV); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 993 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 994 | // Determine whether linkage of these two globals follows the source |
| 995 | // module's definition or the destination module's definition. |
| 996 | GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage; |
| 997 | GlobalValue::VisibilityTypes NV; |
| 998 | if (getLinkageResult(DGV, SGV, NewLinkage, NV, LinkFromSrc)) |
| 999 | return true; |
| 1000 | NewVisibility = NV; |
| 1001 | HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr(); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1002 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1003 | // If we're not linking from the source, then keep the definition that we |
| 1004 | // have. |
| 1005 | if (!LinkFromSrc) { |
| 1006 | // Special case for const propagation. |
| 1007 | if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV)) |
| 1008 | if (DGVar->isDeclaration() && SGV->isConstant() && |
| 1009 | !DGVar->isConstant()) |
| 1010 | DGVar->setConstant(true); |
| 1011 | |
| 1012 | // Set calculated linkage, visibility and unnamed_addr. |
| 1013 | DGV->setLinkage(NewLinkage); |
| 1014 | DGV->setVisibility(*NewVisibility); |
| 1015 | DGV->setUnnamedAddr(HasUnnamedAddr); |
| 1016 | } |
| 1017 | } |
| 1018 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1019 | if (!LinkFromSrc) { |
Chris Lattner | 0ead7a5 | 2008-07-14 07:23:24 +0000 | [diff] [blame] | 1020 | // Make sure to remember this mapping. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1021 | ValueMap[SGV] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGV->getType())); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1022 | |
| 1023 | // Track the source global so that we don't attempt to copy it over when |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1024 | // processing global initializers. |
| 1025 | DoNotLinkFromSource.insert(SGV); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1026 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1027 | return false; |
Chris Lattner | 0ead7a5 | 2008-07-14 07:23:24 +0000 | [diff] [blame] | 1028 | } |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1029 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1030 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1031 | // If the Comdat this variable was inside of wasn't selected, skip it. |
| 1032 | if (DC && !DGV && !LinkFromSrc) { |
| 1033 | DoNotLinkFromSource.insert(SGV); |
| 1034 | return false; |
| 1035 | } |
| 1036 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1037 | // No linking to be performed or linking from the source: simply create an |
| 1038 | // identical version of the symbol over in the dest module... the |
| 1039 | // initializer will be filled in later by LinkGlobalInits. |
| 1040 | GlobalVariable *NewDGV = |
| 1041 | new GlobalVariable(*DstM, TypeMap.get(SGV->getType()->getElementType()), |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1042 | SGV->isConstant(), SGV->getLinkage(), /*init*/nullptr, |
| 1043 | SGV->getName(), /*insertbefore*/nullptr, |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 1044 | SGV->getThreadLocalMode(), |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1045 | SGV->getType()->getAddressSpace()); |
| 1046 | // Propagate alignment, visibility and section info. |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 1047 | copyGVAttributes(NewDGV, SGV); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 1048 | if (NewVisibility) |
| 1049 | NewDGV->setVisibility(*NewVisibility); |
Rafael Espindola | d4885da | 2013-09-04 14:05:09 +0000 | [diff] [blame] | 1050 | NewDGV->setUnnamedAddr(HasUnnamedAddr); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1051 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1052 | if (DC) |
| 1053 | NewDGV->setComdat(DC); |
| 1054 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1055 | if (DGV) { |
| 1056 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV, DGV->getType())); |
| 1057 | DGV->eraseFromParent(); |
| 1058 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1059 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1060 | // Make sure to remember this mapping. |
| 1061 | ValueMap[SGV] = NewDGV; |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1062 | return false; |
| 1063 | } |
| 1064 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1065 | /// linkFunctionProto - Link the function in the source module into the |
| 1066 | /// destination module if needed, setting up mapping information. |
| 1067 | bool ModuleLinker::linkFunctionProto(Function *SF) { |
| 1068 | GlobalValue *DGV = getLinkedToGlobal(SF); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 1069 | llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility; |
Rafael Espindola | fd9a941 | 2013-09-04 14:59:03 +0000 | [diff] [blame] | 1070 | bool HasUnnamedAddr = SF->hasUnnamedAddr(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1071 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1072 | bool LinkFromSrc = false; |
| 1073 | Comdat *DC = nullptr; |
| 1074 | if (const Comdat *SC = SF->getComdat()) { |
| 1075 | Comdat::SelectionKind SK; |
| 1076 | std::tie(SK, LinkFromSrc) = ComdatsChosen[SC]; |
| 1077 | DC = DstM->getOrInsertComdat(SC->getName()); |
| 1078 | DC->setSelectionKind(SK); |
| 1079 | } |
| 1080 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1081 | if (DGV) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1082 | if (!DC) { |
| 1083 | GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage; |
| 1084 | GlobalValue::VisibilityTypes NV; |
| 1085 | if (getLinkageResult(DGV, SF, NewLinkage, NV, LinkFromSrc)) |
| 1086 | return true; |
| 1087 | NewVisibility = NV; |
| 1088 | HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr(); |
| 1089 | |
| 1090 | if (!LinkFromSrc) { |
| 1091 | // Set calculated linkage |
| 1092 | DGV->setLinkage(NewLinkage); |
| 1093 | DGV->setVisibility(*NewVisibility); |
| 1094 | DGV->setUnnamedAddr(HasUnnamedAddr); |
| 1095 | } |
| 1096 | } |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 1097 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1098 | if (!LinkFromSrc) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1099 | // Make sure to remember this mapping. |
| 1100 | ValueMap[SF] = ConstantExpr::getBitCast(DGV, TypeMap.get(SF->getType())); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1101 | |
| 1102 | // Track the function from the source module so we don't attempt to remap |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1103 | // it. |
| 1104 | DoNotLinkFromSource.insert(SF); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1105 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1106 | return false; |
| 1107 | } |
Anton Korobeynikov | dac5fa9 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 1108 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1109 | |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1110 | // If the function is to be lazily linked, don't create it just yet. |
| 1111 | // The ValueMaterializerTy will deal with creating it if it's used. |
| 1112 | if (!DGV && (SF->hasLocalLinkage() || SF->hasLinkOnceLinkage() || |
| 1113 | SF->hasAvailableExternallyLinkage())) { |
| 1114 | DoNotLinkFromSource.insert(SF); |
| 1115 | return false; |
| 1116 | } |
| 1117 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1118 | // If the Comdat this function was inside of wasn't selected, skip it. |
| 1119 | if (DC && !DGV && !LinkFromSrc) { |
| 1120 | DoNotLinkFromSource.insert(SF); |
| 1121 | return false; |
| 1122 | } |
| 1123 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1124 | // If there is no linkage to be performed or we are linking from the source, |
| 1125 | // bring SF over. |
| 1126 | Function *NewDF = Function::Create(TypeMap.get(SF->getFunctionType()), |
| 1127 | SF->getLinkage(), SF->getName(), DstM); |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 1128 | copyGVAttributes(NewDF, SF); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 1129 | if (NewVisibility) |
| 1130 | NewDF->setVisibility(*NewVisibility); |
Rafael Espindola | fd9a941 | 2013-09-04 14:59:03 +0000 | [diff] [blame] | 1131 | NewDF->setUnnamedAddr(HasUnnamedAddr); |
Anton Korobeynikov | dac5fa9 | 2008-03-05 22:22:46 +0000 | [diff] [blame] | 1132 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1133 | if (DC) |
| 1134 | NewDF->setComdat(DC); |
| 1135 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1136 | if (DGV) { |
| 1137 | // Any uses of DF need to change to NewDF, with cast. |
| 1138 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType())); |
| 1139 | DGV->eraseFromParent(); |
Lauro Ramos Venancio | b00c9c0 | 2007-06-28 19:02:54 +0000 | [diff] [blame] | 1140 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1141 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1142 | ValueMap[SF] = NewDF; |
Lauro Ramos Venancio | b00c9c0 | 2007-06-28 19:02:54 +0000 | [diff] [blame] | 1143 | return false; |
| 1144 | } |
| 1145 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1146 | /// LinkAliasProto - Set up prototypes for any aliases that come over from the |
| 1147 | /// source module. |
| 1148 | bool ModuleLinker::linkAliasProto(GlobalAlias *SGA) { |
| 1149 | GlobalValue *DGV = getLinkedToGlobal(SGA); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 1150 | llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility; |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 1151 | bool HasUnnamedAddr = SGA->hasUnnamedAddr(); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 1152 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1153 | bool LinkFromSrc = false; |
| 1154 | Comdat *DC = nullptr; |
| 1155 | if (const Comdat *SC = SGA->getComdat()) { |
| 1156 | Comdat::SelectionKind SK; |
| 1157 | std::tie(SK, LinkFromSrc) = ComdatsChosen[SC]; |
| 1158 | DC = DstM->getOrInsertComdat(SC->getName()); |
| 1159 | DC->setSelectionKind(SK); |
| 1160 | } |
| 1161 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1162 | if (DGV) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1163 | if (!DC) { |
| 1164 | GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage; |
| 1165 | GlobalValue::VisibilityTypes NV; |
| 1166 | if (getLinkageResult(DGV, SGA, NewLinkage, NV, LinkFromSrc)) |
| 1167 | return true; |
| 1168 | NewVisibility = NV; |
| 1169 | HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr(); |
| 1170 | |
| 1171 | if (!LinkFromSrc) { |
| 1172 | // Set calculated linkage. |
| 1173 | DGV->setLinkage(NewLinkage); |
| 1174 | DGV->setVisibility(*NewVisibility); |
| 1175 | DGV->setUnnamedAddr(HasUnnamedAddr); |
| 1176 | } |
| 1177 | } |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 1178 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1179 | if (!LinkFromSrc) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1180 | // Make sure to remember this mapping. |
| 1181 | ValueMap[SGA] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGA->getType())); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1182 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1183 | // Track the alias from the source module so we don't attempt to remap it. |
| 1184 | DoNotLinkFromSource.insert(SGA); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1185 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1186 | return false; |
| 1187 | } |
| 1188 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1189 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1190 | // If the Comdat this alias was inside of wasn't selected, skip it. |
| 1191 | if (DC && !DGV && !LinkFromSrc) { |
| 1192 | DoNotLinkFromSource.insert(SGA); |
| 1193 | return false; |
| 1194 | } |
| 1195 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1196 | // If there is no linkage to be performed or we're linking from the source, |
| 1197 | // bring over SGA. |
Rafael Espindola | 4fe0094 | 2014-05-16 13:34:04 +0000 | [diff] [blame] | 1198 | auto *PTy = cast<PointerType>(TypeMap.get(SGA->getType())); |
Rafael Espindola | f1bedd374 | 2014-05-17 21:29:57 +0000 | [diff] [blame] | 1199 | auto *NewDA = |
| 1200 | GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), |
| 1201 | SGA->getLinkage(), SGA->getName(), DstM); |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 1202 | copyGVAttributes(NewDA, SGA); |
Rafael Espindola | 23f8d64 | 2012-01-05 23:02:01 +0000 | [diff] [blame] | 1203 | if (NewVisibility) |
| 1204 | NewDA->setVisibility(*NewVisibility); |
Rafael Espindola | 42a4c9f | 2014-06-06 01:20:28 +0000 | [diff] [blame] | 1205 | NewDA->setUnnamedAddr(HasUnnamedAddr); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1206 | |
Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 1207 | if (DGV) { |
| 1208 | // Any uses of DGV need to change to NewDA, with cast. |
| 1209 | DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDA, DGV->getType())); |
| 1210 | DGV->eraseFromParent(); |
| 1211 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1212 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1213 | ValueMap[SGA] = NewDA; |
| 1214 | return false; |
| 1215 | } |
| 1216 | |
Chris Lattner | 00245f4 | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1217 | static void getArrayElements(Constant *C, SmallVectorImpl<Constant*> &Dest) { |
Chris Lattner | 6705883 | 2012-01-25 06:48:06 +0000 | [diff] [blame] | 1218 | unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements(); |
| 1219 | |
| 1220 | for (unsigned i = 0; i != NumElements; ++i) |
| 1221 | Dest.push_back(C->getAggregateElement(i)); |
Chris Lattner | 00245f4 | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1222 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1223 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1224 | void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) { |
| 1225 | // Merge the initializer. |
| 1226 | SmallVector<Constant*, 16> Elements; |
Chris Lattner | 00245f4 | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1227 | getArrayElements(AVI.DstInit, Elements); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1228 | |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1229 | Constant *SrcInit = MapValue(AVI.SrcInit, ValueMap, RF_None, &TypeMap, &ValMaterializer); |
Chris Lattner | 00245f4 | 2012-01-24 13:41:11 +0000 | [diff] [blame] | 1230 | getArrayElements(SrcInit, Elements); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1231 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1232 | ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType()); |
| 1233 | AVI.NewGV->setInitializer(ConstantArray::get(NewType, Elements)); |
| 1234 | } |
| 1235 | |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 1236 | /// linkGlobalInits - Update the initializers in the Dest module now that all |
| 1237 | /// globals that may be referenced are in Dest. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1238 | void ModuleLinker::linkGlobalInits() { |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1239 | // Loop over all of the globals in the src module, mapping them over as we go |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1240 | for (Module::const_global_iterator I = SrcM->global_begin(), |
| 1241 | E = SrcM->global_end(); I != E; ++I) { |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1242 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1243 | // Only process initialized GV's or ones not already in dest. |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1244 | if (!I->hasInitializer() || DoNotLinkFromSource.count(I)) continue; |
| 1245 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1246 | // Grab destination global variable. |
| 1247 | GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[I]); |
| 1248 | // Figure out what the initializer looks like in the dest module. |
| 1249 | DGV->setInitializer(MapValue(I->getInitializer(), ValueMap, |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1250 | RF_None, &TypeMap, &ValMaterializer)); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1251 | } |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 1254 | /// linkFunctionBody - Copy the source function over into the dest function and |
| 1255 | /// fix up references to values. At this point we know that Dest is an external |
| 1256 | /// function, and that Src is not. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1257 | void ModuleLinker::linkFunctionBody(Function *Dst, Function *Src) { |
| 1258 | assert(Src && Dst && Dst->isDeclaration() && !Src->isDeclaration()); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1259 | |
Chris Lattner | 7391dde | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 1260 | // Go through and convert function arguments over, remembering the mapping. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1261 | Function::arg_iterator DI = Dst->arg_begin(); |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 1262 | for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1263 | I != E; ++I, ++DI) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1264 | DI->setName(I->getName()); // Copy the name over. |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1265 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1266 | // Add a mapping to our mapping. |
Anton Korobeynikov | 66a6271 | 2008-03-10 22:36:08 +0000 | [diff] [blame] | 1267 | ValueMap[I] = DI; |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1270 | if (Mode == Linker::DestroySource) { |
| 1271 | // Splice the body of the source function into the dest function. |
| 1272 | Dst->getBasicBlockList().splice(Dst->end(), Src->getBasicBlockList()); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1273 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1274 | // At this point, all of the instructions and values of the function are now |
| 1275 | // copied over. The only problem is that they are still referencing values in |
| 1276 | // the Source function as operands. Loop through all of the operands of the |
| 1277 | // functions and patch them up to point to the local versions. |
| 1278 | for (Function::iterator BB = Dst->begin(), BE = Dst->end(); BB != BE; ++BB) |
| 1279 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1280 | RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries, |
| 1281 | &TypeMap, &ValMaterializer); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1282 | |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1283 | } else { |
| 1284 | // Clone the body of the function into the dest function. |
| 1285 | SmallVector<ReturnInst*, 8> Returns; // Ignore returns. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1286 | CloneFunctionInto(Dst, Src, ValueMap, false, Returns, "", nullptr, |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1287 | &TypeMap, &ValMaterializer); |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1288 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1289 | |
Chris Lattner | 7391dde | 2004-11-16 17:12:38 +0000 | [diff] [blame] | 1290 | // There is no need to map the arguments anymore. |
Chris Lattner | 44ab8ae | 2006-06-16 01:24:04 +0000 | [diff] [blame] | 1291 | for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end(); |
| 1292 | I != E; ++I) |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 1293 | ValueMap.erase(I); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1294 | |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 1297 | /// linkAliasBodies - Insert all of the aliases in Src into the Dest module. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1298 | void ModuleLinker::linkAliasBodies() { |
| 1299 | for (Module::alias_iterator I = SrcM->alias_begin(), E = SrcM->alias_end(); |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1300 | I != E; ++I) { |
| 1301 | if (DoNotLinkFromSource.count(I)) |
| 1302 | continue; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1303 | if (Constant *Aliasee = I->getAliasee()) { |
| 1304 | GlobalAlias *DA = cast<GlobalAlias>(ValueMap[I]); |
Rafael Espindola | 6b23863 | 2014-05-16 19:35:39 +0000 | [diff] [blame] | 1305 | Constant *Val = |
| 1306 | MapValue(Aliasee, ValueMap, RF_None, &TypeMap, &ValMaterializer); |
Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 1307 | DA->setAliasee(Val); |
David Chisnall | 2c4a34a | 2010-01-09 16:27:31 +0000 | [diff] [blame] | 1308 | } |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1309 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1310 | } |
Anton Korobeynikov | 2609888 | 2008-03-05 23:21:39 +0000 | [diff] [blame] | 1311 | |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 1312 | /// linkNamedMDNodes - Insert all of the named MDNodes in Src into the Dest |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1313 | /// module. |
| 1314 | void ModuleLinker::linkNamedMDNodes() { |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1315 | const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1316 | for (Module::const_named_metadata_iterator I = SrcM->named_metadata_begin(), |
| 1317 | E = SrcM->named_metadata_end(); I != E; ++I) { |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1318 | // Don't link module flags here. Do them separately. |
| 1319 | if (&*I == SrcModFlags) continue; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1320 | NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(I->getName()); |
| 1321 | // Add Src elements into Dest node. |
| 1322 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 1323 | DestNMD->addOperand(MapValue(I->getOperand(i), ValueMap, |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1324 | RF_None, &TypeMap, &ValMaterializer)); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1325 | } |
| 1326 | } |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1327 | |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1328 | /// linkModuleFlagsMetadata - Merge the linker flags in Src into the Dest |
| 1329 | /// module. |
| 1330 | bool ModuleLinker::linkModuleFlagsMetadata() { |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1331 | // If the source module has no module flags, we are done. |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1332 | const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata(); |
| 1333 | if (!SrcModFlags) return false; |
| 1334 | |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1335 | // If the destination module doesn't have module flags yet, then just copy |
| 1336 | // over the source module's flags. |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1337 | NamedMDNode *DstModFlags = DstM->getOrInsertModuleFlagsMetadata(); |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1338 | if (DstModFlags->getNumOperands() == 0) { |
| 1339 | for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) |
| 1340 | DstModFlags->addOperand(SrcModFlags->getOperand(I)); |
| 1341 | |
| 1342 | return false; |
| 1343 | } |
| 1344 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1345 | // First build a map of the existing module flags and requirements. |
| 1346 | DenseMap<MDString*, MDNode*> Flags; |
| 1347 | SmallSetVector<MDNode*, 16> Requirements; |
| 1348 | for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) { |
| 1349 | MDNode *Op = DstModFlags->getOperand(I); |
| 1350 | ConstantInt *Behavior = cast<ConstantInt>(Op->getOperand(0)); |
| 1351 | MDString *ID = cast<MDString>(Op->getOperand(1)); |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1352 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1353 | if (Behavior->getZExtValue() == Module::Require) { |
| 1354 | Requirements.insert(cast<MDNode>(Op->getOperand(2))); |
| 1355 | } else { |
| 1356 | Flags[ID] = Op; |
| 1357 | } |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1360 | // Merge in the flags from the source module, and also collect its set of |
| 1361 | // requirements. |
| 1362 | bool HasErr = false; |
| 1363 | for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) { |
| 1364 | MDNode *SrcOp = SrcModFlags->getOperand(I); |
| 1365 | ConstantInt *SrcBehavior = cast<ConstantInt>(SrcOp->getOperand(0)); |
| 1366 | MDString *ID = cast<MDString>(SrcOp->getOperand(1)); |
| 1367 | MDNode *DstOp = Flags.lookup(ID); |
| 1368 | unsigned SrcBehaviorValue = SrcBehavior->getZExtValue(); |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1369 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1370 | // If this is a requirement, add it and continue. |
| 1371 | if (SrcBehaviorValue == Module::Require) { |
| 1372 | // If the destination module does not already have this requirement, add |
| 1373 | // it. |
| 1374 | if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) { |
| 1375 | DstModFlags->addOperand(SrcOp); |
| 1376 | } |
| 1377 | continue; |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1380 | // If there is no existing flag with this ID, just add it. |
| 1381 | if (!DstOp) { |
| 1382 | Flags[ID] = SrcOp; |
| 1383 | DstModFlags->addOperand(SrcOp); |
| 1384 | continue; |
| 1385 | } |
| 1386 | |
| 1387 | // Otherwise, perform a merge. |
| 1388 | ConstantInt *DstBehavior = cast<ConstantInt>(DstOp->getOperand(0)); |
| 1389 | unsigned DstBehaviorValue = DstBehavior->getZExtValue(); |
| 1390 | |
| 1391 | // If either flag has override behavior, handle it first. |
| 1392 | if (DstBehaviorValue == Module::Override) { |
| 1393 | // Diagnose inconsistent flags which both have override behavior. |
| 1394 | if (SrcBehaviorValue == Module::Override && |
| 1395 | SrcOp->getOperand(2) != DstOp->getOperand(2)) { |
| 1396 | HasErr |= emitError("linking module flags '" + ID->getString() + |
| 1397 | "': IDs have conflicting override values"); |
| 1398 | } |
| 1399 | continue; |
| 1400 | } else if (SrcBehaviorValue == Module::Override) { |
| 1401 | // Update the destination flag to that of the source. |
| 1402 | DstOp->replaceOperandWith(0, SrcBehavior); |
| 1403 | DstOp->replaceOperandWith(2, SrcOp->getOperand(2)); |
| 1404 | continue; |
| 1405 | } |
| 1406 | |
| 1407 | // Diagnose inconsistent merge behavior types. |
| 1408 | if (SrcBehaviorValue != DstBehaviorValue) { |
| 1409 | HasErr |= emitError("linking module flags '" + ID->getString() + |
| 1410 | "': IDs have conflicting behaviors"); |
| 1411 | continue; |
| 1412 | } |
| 1413 | |
| 1414 | // Perform the merge for standard behavior types. |
| 1415 | switch (SrcBehaviorValue) { |
| 1416 | case Module::Require: |
Craig Topper | 2a30d78 | 2014-06-18 05:05:13 +0000 | [diff] [blame] | 1417 | case Module::Override: llvm_unreachable("not possible"); |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1418 | case Module::Error: { |
| 1419 | // Emit an error if the values differ. |
| 1420 | if (SrcOp->getOperand(2) != DstOp->getOperand(2)) { |
| 1421 | HasErr |= emitError("linking module flags '" + ID->getString() + |
| 1422 | "': IDs have conflicting values"); |
| 1423 | } |
| 1424 | continue; |
| 1425 | } |
| 1426 | case Module::Warning: { |
| 1427 | // Emit a warning if the values differ. |
| 1428 | if (SrcOp->getOperand(2) != DstOp->getOperand(2)) { |
Eli Bendersky | e17f370 | 2014-02-06 18:01:56 +0000 | [diff] [blame] | 1429 | if (!SuppressWarnings) { |
| 1430 | errs() << "WARNING: linking module flags '" << ID->getString() |
| 1431 | << "': IDs have conflicting values"; |
| 1432 | } |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1433 | } |
| 1434 | continue; |
| 1435 | } |
Daniel Dunbar | d77d9fb | 2013-01-16 21:38:56 +0000 | [diff] [blame] | 1436 | case Module::Append: { |
| 1437 | MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2)); |
| 1438 | MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2)); |
| 1439 | unsigned NumOps = DstValue->getNumOperands() + SrcValue->getNumOperands(); |
| 1440 | Value **VP, **Values = VP = new Value*[NumOps]; |
| 1441 | for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i, ++VP) |
| 1442 | *VP = DstValue->getOperand(i); |
| 1443 | for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i, ++VP) |
| 1444 | *VP = SrcValue->getOperand(i); |
| 1445 | DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(), |
| 1446 | ArrayRef<Value*>(Values, |
| 1447 | NumOps))); |
| 1448 | delete[] Values; |
| 1449 | break; |
| 1450 | } |
| 1451 | case Module::AppendUnique: { |
| 1452 | SmallSetVector<Value*, 16> Elts; |
| 1453 | MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2)); |
| 1454 | MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2)); |
| 1455 | for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i) |
| 1456 | Elts.insert(DstValue->getOperand(i)); |
| 1457 | for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i) |
| 1458 | Elts.insert(SrcValue->getOperand(i)); |
| 1459 | DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(), |
| 1460 | ArrayRef<Value*>(Elts.begin(), |
| 1461 | Elts.end()))); |
| 1462 | break; |
| 1463 | } |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1464 | } |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1467 | // Check all of the requirements. |
| 1468 | for (unsigned I = 0, E = Requirements.size(); I != E; ++I) { |
| 1469 | MDNode *Requirement = Requirements[I]; |
| 1470 | MDString *Flag = cast<MDString>(Requirement->getOperand(0)); |
| 1471 | Value *ReqValue = Requirement->getOperand(1); |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1472 | |
Daniel Dunbar | 0ec72bb | 2013-01-16 18:39:23 +0000 | [diff] [blame] | 1473 | MDNode *Op = Flags[Flag]; |
| 1474 | if (!Op || Op->getOperand(2) != ReqValue) { |
| 1475 | HasErr |= emitError("linking module flags '" + Flag->getString() + |
| 1476 | "': does not have the required value"); |
| 1477 | continue; |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | return HasErr; |
| 1482 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1483 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1484 | bool ModuleLinker::run() { |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1485 | assert(DstM && "Null destination module"); |
| 1486 | assert(SrcM && "Null source module"); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1487 | |
| 1488 | // Inherit the target data from the source module if the destination module |
| 1489 | // doesn't have one already. |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 1490 | if (!DstM->getDataLayout() && SrcM->getDataLayout()) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1491 | DstM->setDataLayout(SrcM->getDataLayout()); |
| 1492 | |
| 1493 | // Copy the target triple from the source to dest if the dest's is empty. |
| 1494 | if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty()) |
| 1495 | DstM->setTargetTriple(SrcM->getTargetTriple()); |
| 1496 | |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 1497 | if (SrcM->getDataLayout() && DstM->getDataLayout() && |
Rafael Espindola | ae593f1 | 2014-02-26 17:02:08 +0000 | [diff] [blame] | 1498 | *SrcM->getDataLayout() != *DstM->getDataLayout()) { |
Eli Bendersky | e17f370 | 2014-02-06 18:01:56 +0000 | [diff] [blame] | 1499 | if (!SuppressWarnings) { |
JF Bastien | 026fc5f | 2014-03-05 21:26:42 +0000 | [diff] [blame] | 1500 | errs() << "WARNING: Linking two modules of different data layouts: '" |
| 1501 | << SrcM->getModuleIdentifier() << "' is '" |
| 1502 | << SrcM->getDataLayoutStr() << "' whereas '" |
| 1503 | << DstM->getModuleIdentifier() << "' is '" |
| 1504 | << DstM->getDataLayoutStr() << "'\n"; |
Eli Bendersky | e17f370 | 2014-02-06 18:01:56 +0000 | [diff] [blame] | 1505 | } |
| 1506 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1507 | if (!SrcM->getTargetTriple().empty() && |
| 1508 | DstM->getTargetTriple() != SrcM->getTargetTriple()) { |
Eli Bendersky | e17f370 | 2014-02-06 18:01:56 +0000 | [diff] [blame] | 1509 | if (!SuppressWarnings) { |
JF Bastien | 026fc5f | 2014-03-05 21:26:42 +0000 | [diff] [blame] | 1510 | errs() << "WARNING: Linking two modules of different target triples: " |
| 1511 | << SrcM->getModuleIdentifier() << "' is '" |
| 1512 | << SrcM->getTargetTriple() << "' whereas '" |
| 1513 | << DstM->getModuleIdentifier() << "' is '" |
Eli Bendersky | e17f370 | 2014-02-06 18:01:56 +0000 | [diff] [blame] | 1514 | << DstM->getTargetTriple() << "'\n"; |
| 1515 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | // Append the module inline asm string. |
| 1519 | if (!SrcM->getModuleInlineAsm().empty()) { |
| 1520 | if (DstM->getModuleInlineAsm().empty()) |
| 1521 | DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm()); |
| 1522 | else |
| 1523 | DstM->setModuleInlineAsm(DstM->getModuleInlineAsm()+"\n"+ |
| 1524 | SrcM->getModuleInlineAsm()); |
| 1525 | } |
| 1526 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1527 | // Loop over all of the linked values to compute type mappings. |
| 1528 | computeTypeMapping(); |
| 1529 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1530 | ComdatsChosen.clear(); |
| 1531 | for (const StringMapEntry<llvm::Comdat> &SMEC : SrcM->getComdatSymbolTable()) { |
| 1532 | const Comdat &C = SMEC.getValue(); |
| 1533 | if (ComdatsChosen.count(&C)) |
| 1534 | continue; |
| 1535 | Comdat::SelectionKind SK; |
| 1536 | bool LinkFromSrc; |
| 1537 | if (getComdatResult(&C, SK, LinkFromSrc)) |
| 1538 | return true; |
| 1539 | ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc); |
| 1540 | } |
| 1541 | |
Duncan P. N. Exon Smith | 09d84ad | 2014-08-12 16:46:37 +0000 | [diff] [blame] | 1542 | // Upgrade mismatched global arrays. |
| 1543 | upgradeMismatchedGlobals(); |
| 1544 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1545 | // Insert all of the globals in src into the DstM module... without linking |
| 1546 | // initializers (which could refer to functions not yet mapped over). |
| 1547 | for (Module::global_iterator I = SrcM->global_begin(), |
| 1548 | E = SrcM->global_end(); I != E; ++I) |
| 1549 | if (linkGlobalProto(I)) |
| 1550 | return true; |
| 1551 | |
| 1552 | // Link the functions together between the two modules, without doing function |
| 1553 | // bodies... this just adds external function prototypes to the DstM |
| 1554 | // function... We do this so that when we begin processing function bodies, |
| 1555 | // all of the global values that may be referenced are available in our |
| 1556 | // ValueMap. |
| 1557 | for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I) |
| 1558 | if (linkFunctionProto(I)) |
| 1559 | return true; |
| 1560 | |
| 1561 | // If there were any aliases, link them now. |
| 1562 | for (Module::alias_iterator I = SrcM->alias_begin(), |
| 1563 | E = SrcM->alias_end(); I != E; ++I) |
| 1564 | if (linkAliasProto(I)) |
| 1565 | return true; |
| 1566 | |
| 1567 | for (unsigned i = 0, e = AppendingVars.size(); i != e; ++i) |
| 1568 | linkAppendingVarInit(AppendingVars[i]); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1569 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1570 | // Link in the function bodies that are defined in the source module into |
| 1571 | // DstM. |
| 1572 | for (Module::iterator SF = SrcM->begin(), E = SrcM->end(); SF != E; ++SF) { |
Tanya Lattner | ea166d4 | 2011-10-14 22:17:46 +0000 | [diff] [blame] | 1573 | // Skip if not linking from source. |
| 1574 | if (DoNotLinkFromSource.count(SF)) continue; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1575 | |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1576 | Function *DF = cast<Function>(ValueMap[SF]); |
| 1577 | if (SF->hasPrefixData()) { |
| 1578 | // Link in the prefix data. |
| 1579 | DF->setPrefixData(MapValue( |
| 1580 | SF->getPrefixData(), ValueMap, RF_None, &TypeMap, &ValMaterializer)); |
| 1581 | } |
| 1582 | |
Tanya Lattner | ea166d4 | 2011-10-14 22:17:46 +0000 | [diff] [blame] | 1583 | // Skip if no body (function is external) or materialize. |
| 1584 | if (SF->isDeclaration()) { |
| 1585 | if (!SF->isMaterializable()) |
| 1586 | continue; |
| 1587 | if (SF->Materialize(&ErrorMsg)) |
| 1588 | return true; |
| 1589 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1590 | |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1591 | linkFunctionBody(DF, SF); |
Bill Wendling | 0062378 | 2012-03-23 07:22:49 +0000 | [diff] [blame] | 1592 | SF->Dematerialize(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | // Resolve all uses of aliases with aliasees. |
| 1596 | linkAliasBodies(); |
| 1597 | |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1598 | // Remap all of the named MDNodes in Src into the DstM module. We do this |
Devang Patel | 6ddbb2e | 2011-08-04 19:44:28 +0000 | [diff] [blame] | 1599 | // after linking GlobalValues so that MDNodes that reference GlobalValues |
| 1600 | // are properly remapped. |
| 1601 | linkNamedMDNodes(); |
| 1602 | |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 1603 | // Merge the module flags into the DstM module. |
| 1604 | if (linkModuleFlagsMetadata()) |
| 1605 | return true; |
| 1606 | |
Bill Wendling | 91686d6 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 1607 | // Update the initializers in the DstM module now that all globals that may |
| 1608 | // be referenced are in DstM. |
| 1609 | linkGlobalInits(); |
| 1610 | |
Tanya Lattner | 0a48b87 | 2011-11-02 00:24:56 +0000 | [diff] [blame] | 1611 | // Process vector of lazily linked in functions. |
| 1612 | bool LinkedInAnyFunctions; |
| 1613 | do { |
| 1614 | LinkedInAnyFunctions = false; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1615 | |
Bill Wendling | fa228782 | 2013-03-27 17:54:41 +0000 | [diff] [blame] | 1616 | for(std::vector<Function*>::iterator I = LazilyLinkFunctions.begin(), |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1617 | E = LazilyLinkFunctions.end(); I != E; ++I) { |
Bill Wendling | fa228782 | 2013-03-27 17:54:41 +0000 | [diff] [blame] | 1618 | Function *SF = *I; |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1619 | if (!SF) |
| 1620 | continue; |
Bill Wendling | 0062378 | 2012-03-23 07:22:49 +0000 | [diff] [blame] | 1621 | |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1622 | Function *DF = cast<Function>(ValueMap[SF]); |
Peter Collingbourne | 3fa50f9 | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 1623 | if (SF->hasPrefixData()) { |
| 1624 | // Link in the prefix data. |
| 1625 | DF->setPrefixData(MapValue(SF->getPrefixData(), |
| 1626 | ValueMap, |
| 1627 | RF_None, |
| 1628 | &TypeMap, |
| 1629 | &ValMaterializer)); |
| 1630 | } |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1631 | |
| 1632 | // Materialize if necessary. |
| 1633 | if (SF->isDeclaration()) { |
| 1634 | if (!SF->isMaterializable()) |
| 1635 | continue; |
| 1636 | if (SF->Materialize(&ErrorMsg)) |
| 1637 | return true; |
Tanya Lattner | 0a48b87 | 2011-11-02 00:24:56 +0000 | [diff] [blame] | 1638 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1639 | |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 1640 | // Erase from vector *before* the function body is linked - linkFunctionBody could |
| 1641 | // invalidate I. |
| 1642 | LazilyLinkFunctions.erase(I); |
| 1643 | |
| 1644 | // Link in function body. |
| 1645 | linkFunctionBody(DF, SF); |
| 1646 | SF->Dematerialize(); |
| 1647 | |
| 1648 | // Set flag to indicate we may have more functions to lazily link in |
| 1649 | // since we linked in a function. |
| 1650 | LinkedInAnyFunctions = true; |
| 1651 | break; |
Tanya Lattner | 0a48b87 | 2011-11-02 00:24:56 +0000 | [diff] [blame] | 1652 | } |
| 1653 | } while (LinkedInAnyFunctions); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1654 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1655 | // Now that all of the types from the source are used, resolve any structs |
| 1656 | // copied over to the dest that didn't exist there. |
| 1657 | TypeMap.linkDefinedTypeBodies(); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1658 | |
Anton Korobeynikov | 2609888 | 2008-03-05 23:21:39 +0000 | [diff] [blame] | 1659 | return false; |
| 1660 | } |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1661 | |
Eli Bendersky | 7da92ed | 2014-02-20 22:19:24 +0000 | [diff] [blame] | 1662 | Linker::Linker(Module *M, bool SuppressWarnings) |
| 1663 | : Composite(M), SuppressWarnings(SuppressWarnings) { |
Rafael Espindola | aa9918a | 2013-05-04 05:05:18 +0000 | [diff] [blame] | 1664 | TypeFinder StructTypes; |
| 1665 | StructTypes.run(*M, true); |
| 1666 | IdentifiedStructTypes.insert(StructTypes.begin(), StructTypes.end()); |
| 1667 | } |
Rafael Espindola | 3df61b7 | 2013-05-04 03:48:37 +0000 | [diff] [blame] | 1668 | |
| 1669 | Linker::~Linker() { |
| 1670 | } |
| 1671 | |
Bill Wendling | 91e6f6e | 2013-10-16 08:59:57 +0000 | [diff] [blame] | 1672 | void Linker::deleteModule() { |
| 1673 | delete Composite; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1674 | Composite = nullptr; |
Bill Wendling | 91e6f6e | 2013-10-16 08:59:57 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Rafael Espindola | 3df61b7 | 2013-05-04 03:48:37 +0000 | [diff] [blame] | 1677 | bool Linker::linkInModule(Module *Src, unsigned Mode, std::string *ErrorMsg) { |
Eli Bendersky | 7da92ed | 2014-02-20 22:19:24 +0000 | [diff] [blame] | 1678 | ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src, Mode, |
| 1679 | SuppressWarnings); |
Rafael Espindola | 287f18b | 2013-05-04 04:08:02 +0000 | [diff] [blame] | 1680 | if (TheLinker.run()) { |
| 1681 | if (ErrorMsg) |
| 1682 | *ErrorMsg = TheLinker.ErrorMsg; |
| 1683 | return true; |
| 1684 | } |
| 1685 | return false; |
Rafael Espindola | 3df61b7 | 2013-05-04 03:48:37 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1688 | //===----------------------------------------------------------------------===// |
| 1689 | // LinkModules entrypoint. |
| 1690 | //===----------------------------------------------------------------------===// |
| 1691 | |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 1692 | /// LinkModules - This function links two modules together, with the resulting |
Eli Bendersky | 970cc63 | 2013-03-08 22:29:44 +0000 | [diff] [blame] | 1693 | /// Dest module modified to be the composite of the two input modules. If an |
Bill Wendling | b6af2f3 | 2012-03-22 20:28:27 +0000 | [diff] [blame] | 1694 | /// error occurs, true is returned and ErrorMsg (if not null) is set to indicate |
| 1695 | /// the problem. Upon failure, the Dest module could be in a modified state, |
| 1696 | /// and shouldn't be relied on to be consistent. |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 1697 | bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Mode, |
Tanya Lattner | cbb9140 | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 1698 | std::string *ErrorMsg) { |
Rafael Espindola | 287f18b | 2013-05-04 04:08:02 +0000 | [diff] [blame] | 1699 | Linker L(Dest); |
| 1700 | return L.linkInModule(Src, Mode, ErrorMsg); |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 1701 | } |
Bill Wendling | a3aeb98 | 2012-05-09 08:55:40 +0000 | [diff] [blame] | 1702 | |
| 1703 | //===----------------------------------------------------------------------===// |
| 1704 | // C API. |
| 1705 | //===----------------------------------------------------------------------===// |
| 1706 | |
| 1707 | LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src, |
| 1708 | LLVMLinkerMode Mode, char **OutMessages) { |
| 1709 | std::string Messages; |
| 1710 | LLVMBool Result = Linker::LinkModules(unwrap(Dest), unwrap(Src), |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1711 | Mode, OutMessages? &Messages : nullptr); |
Bill Wendling | a3aeb98 | 2012-05-09 08:55:40 +0000 | [diff] [blame] | 1712 | if (OutMessages) |
| 1713 | *OutMessages = strdup(Messages.c_str()); |
| 1714 | return Result; |
| 1715 | } |