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