blob: 9e8696ddb1d2141f4d0a92e6de16ad37f093e626 [file] [log] [blame]
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +00001 //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ---------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner52f7e902001-10-13 07:03:50 +00009//
10// This file implements the LLVM module linker.
11//
12// Specifically, this:
Chris Lattner8d2de8a2001-10-15 03:12:52 +000013// * Merges global variables between the two modules
14// * Uninit + Uninit = Init, Init + Uninit = Init, Init + Init = Error if !=
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +000015// * Merges functions between two modules
Chris Lattner52f7e902001-10-13 07:03:50 +000016//
17//===----------------------------------------------------------------------===//
18
Reid Spencer7cc371a2004-11-14 23:27:04 +000019#include "llvm/Linker.h"
Chris Lattneradbc0b52003-11-20 18:23:14 +000020#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
Chris Lattner5c377c52001-10-14 23:29:15 +000022#include "llvm/Module.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000023#include "llvm/TypeSymbolTable.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000024#include "llvm/ValueSymbolTable.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000025#include "llvm/Instructions.h"
Chris Lattneradbc0b52003-11-20 18:23:14 +000026#include "llvm/Assembly/Writer.h"
Bill Wendling41edad72006-11-27 10:09:12 +000027#include "llvm/Support/Streams.h"
Reid Spencer57a0efa2004-09-11 04:25:17 +000028#include "llvm/System/Path.h"
Chris Lattner62a81a12008-06-16 21:00:18 +000029#include "llvm/ADT/DenseMap.h"
Bill Wendling1a097e32006-12-07 23:41:45 +000030#include <sstream>
Chris Lattnerf7703df2004-01-09 06:12:26 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner5c377c52001-10-14 23:29:15 +000033// Error - Simple wrapper function to conditionally assign to E and return true.
34// This just makes error return conditions a little bit simpler...
Chris Lattner8166e6e2003-05-13 21:33:43 +000035static inline bool Error(std::string *E, const std::string &Message) {
Chris Lattner5c377c52001-10-14 23:29:15 +000036 if (E) *E = Message;
37 return true;
38}
39
John Criswell700867b2003-11-04 15:22:26 +000040// 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 Criswell700867b2003-11-04 15:22:26 +000048//
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 Lattner4c00e532003-05-15 16:30:55 +000055//
Chris Lattnerbc1c82a2008-06-16 18:19:05 +000056static bool ResolveTypes(const Type *DestTy, const Type *SrcTy) {
Chris Lattnere76c57a2003-08-22 06:07:12 +000057 if (DestTy == SrcTy) return false; // If already equal, noop
Chris Lattnerbc1c82a2008-06-16 18:19:05 +000058 assert(DestTy && SrcTy && "Can't handle null types");
Chris Lattnere76c57a2003-08-22 06:07:12 +000059
Chris Lattnerbc1c82a2008-06-16 18:19:05 +000060 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 Lattner4c00e532003-05-15 16:30:55 +000067 }
68 return false;
69}
70
Chris Lattner62a81a12008-06-16 21:00:18 +000071/// 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.
74namespace {
75class LinkerTypeMap : public AbstractTypeUser {
76 typedef DenseMap<const Type*, PATypeHolder> TheMapTy;
77 TheMapTy TheMap;
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +000078
Chris Lattnerfc196f92008-06-16 23:06:51 +000079 LinkerTypeMap(const LinkerTypeMap&); // DO NOT IMPLEMENT
80 void operator=(const LinkerTypeMap&); // DO NOT IMPLEMENT
81public:
82 LinkerTypeMap() {}
83 ~LinkerTypeMap() {
Chris Lattner62a81a12008-06-16 21:00:18 +000084 for (DenseMap<const Type*, PATypeHolder>::iterator I = TheMap.begin(),
85 E = TheMap.end(); I != E; ++I)
86 I->first->removeAbstractTypeUser(this);
87 }
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +000088
Chris Lattner62a81a12008-06-16 21:00:18 +000089 /// 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 }
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +000096
Chris Lattner62a81a12008-06-16 21:00:18 +000097 /// 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 }
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000105
Chris Lattner62a81a12008-06-16 21:00:18 +0000106 /// 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 Gohman6b345ee2008-07-07 17:46:23 +0000109 if (!TheMap.insert(std::make_pair(Src, PATypeHolder(Dst))).second)
Chris Lattner62a81a12008-06-16 21:00:18 +0000110 return false; // Already in map.
111 if (Src->isAbstract())
112 Src->addAbstractTypeUser(this);
113 return true;
114 }
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000115
Chris Lattner62a81a12008-06-16 21:00:18 +0000116protected:
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;
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000125
Chris Lattner62a81a12008-06-16 21:00:18 +0000126 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 }
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000134
Chris Lattner62a81a12008-06-16 21:00:18 +0000135 /// 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 }
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000143
Chris Lattner62a81a12008-06-16 21:00:18 +0000144 // for debugging...
145 virtual void dump() const {
146 cerr << "AbstractTypeSet!\n";
147 }
148};
149}
150
151
Chris Lattnere76c57a2003-08-22 06:07:12 +0000152// 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 Lattnera4477f92008-06-16 21:17:12 +0000155static bool RecursiveResolveTypesI(const Type *DstTy, const Type *SrcTy,
Chris Lattner62a81a12008-06-16 21:00:18 +0000156 LinkerTypeMap &Pointers) {
Chris Lattnera4477f92008-06-16 21:17:12 +0000157 if (DstTy == SrcTy) return false; // If already equal, noop
Misha Brukmanf976c852005-04-21 22:55:34 +0000158
Chris Lattnere76c57a2003-08-22 06:07:12 +0000159 // If we found our opaque type, resolve it now!
Chris Lattnera4477f92008-06-16 21:17:12 +0000160 if (isa<OpaqueType>(DstTy) || isa<OpaqueType>(SrcTy))
161 return ResolveTypes(DstTy, SrcTy);
Misha Brukmanf976c852005-04-21 22:55:34 +0000162
Chris Lattnere76c57a2003-08-22 06:07:12 +0000163 // 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 Lattnera4477f92008-06-16 21:17:12 +0000165 if (DstTy->getTypeID() != SrcTy->getTypeID()) return true;
Chris Lattnere76c57a2003-08-22 06:07:12 +0000166
Chris Lattner56539652008-06-16 20:03:01 +0000167 // If neither type is abstract, then they really are just different types.
Chris Lattnera4477f92008-06-16 21:17:12 +0000168 if (!DstTy->isAbstract() && !SrcTy->isAbstract())
Chris Lattner56539652008-06-16 20:03:01 +0000169 return true;
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000170
Chris Lattnere76c57a2003-08-22 06:07:12 +0000171 // Otherwise, resolve the used type used by this derived type...
Chris Lattnera4477f92008-06-16 21:17:12 +0000172 switch (DstTy->getTypeID()) {
Chris Lattnerf6f4f7a2008-06-16 18:27:53 +0000173 default:
174 return true;
Chris Lattnere76c57a2003-08-22 06:07:12 +0000175 case Type::FunctionTyID: {
Chris Lattnera4477f92008-06-16 21:17:12 +0000176 const FunctionType *DstFT = cast<FunctionType>(DstTy);
177 const FunctionType *SrcFT = cast<FunctionType>(SrcTy);
Chris Lattner9ddf2c82008-06-16 19:55:40 +0000178 if (DstFT->isVarArg() != SrcFT->isVarArg() ||
179 DstFT->getNumContainedTypes() != SrcFT->getNumContainedTypes())
Chris Lattner43f4ba82003-08-22 19:12:55 +0000180 return true;
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000181
Chris Lattnera4477f92008-06-16 21:17:12 +0000182 // 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 Lattnere76c57a2003-08-22 06:07:12 +0000187 return true;
Chris Lattnera4477f92008-06-16 21:17:12 +0000188 }
Chris Lattnere76c57a2003-08-22 06:07:12 +0000189 return false;
190 }
191 case Type::StructTyID: {
Chris Lattnera4477f92008-06-16 21:17:12 +0000192 const StructType *DstST = cast<StructType>(DstTy);
193 const StructType *SrcST = cast<StructType>(SrcTy);
Chris Lattner9ddf2c82008-06-16 19:55:40 +0000194 if (DstST->getNumContainedTypes() != SrcST->getNumContainedTypes())
Chris Lattnerf6f4f7a2008-06-16 18:27:53 +0000195 return true;
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000196
Chris Lattnera4477f92008-06-16 21:17:12 +0000197 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 Lattnere76c57a2003-08-22 06:07:12 +0000201 return true;
Chris Lattnera4477f92008-06-16 21:17:12 +0000202 }
Chris Lattnere76c57a2003-08-22 06:07:12 +0000203 return false;
204 }
205 case Type::ArrayTyID: {
Chris Lattnera4477f92008-06-16 21:17:12 +0000206 const ArrayType *DAT = cast<ArrayType>(DstTy);
207 const ArrayType *SAT = cast<ArrayType>(SrcTy);
Chris Lattnere76c57a2003-08-22 06:07:12 +0000208 if (DAT->getNumElements() != SAT->getNumElements()) return true;
Chris Lattnere3092c92003-08-23 21:25:54 +0000209 return RecursiveResolveTypesI(DAT->getElementType(), SAT->getElementType(),
Chris Lattnerbc1c82a2008-06-16 18:19:05 +0000210 Pointers);
Chris Lattnere76c57a2003-08-22 06:07:12 +0000211 }
Chris Lattnerf6f4f7a2008-06-16 18:27:53 +0000212 case Type::VectorTyID: {
Chris Lattnera4477f92008-06-16 21:17:12 +0000213 const VectorType *DVT = cast<VectorType>(DstTy);
214 const VectorType *SVT = cast<VectorType>(SrcTy);
Chris Lattnerf6f4f7a2008-06-16 18:27:53 +0000215 if (DVT->getNumElements() != SVT->getNumElements()) return true;
216 return RecursiveResolveTypesI(DVT->getElementType(), SVT->getElementType(),
217 Pointers);
218 }
Chris Lattnere3092c92003-08-23 21:25:54 +0000219 case Type::PointerTyID: {
Chris Lattnera4477f92008-06-16 21:17:12 +0000220 const PointerType *DstPT = cast<PointerType>(DstTy);
221 const PointerType *SrcPT = cast<PointerType>(SrcTy);
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000222
Chris Lattner9ddf2c82008-06-16 19:55:40 +0000223 if (DstPT->getAddressSpace() != SrcPT->getAddressSpace())
224 return true;
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000225
Chris Lattnere3092c92003-08-23 21:25:54 +0000226 // 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 Lattner62a81a12008-06-16 21:00:18 +0000229 // in the container) change whenever types get resolved.
230 if (SrcPT->isAbstract())
231 if (const Type *ExistingDestTy = Pointers.lookup(SrcPT))
232 return ExistingDestTy != DstPT;
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000233
Chris Lattner62a81a12008-06-16 21:00:18 +0000234 if (DstPT->isAbstract())
235 if (const Type *ExistingSrcTy = Pointers.lookup(DstPT))
236 return ExistingSrcTy != SrcPT;
Chris Lattnere3092c92003-08-23 21:25:54 +0000237 // Otherwise, add the current pointers to the vector to stop recursion on
238 // this pair.
Chris Lattner62a81a12008-06-16 21:00:18 +0000239 if (DstPT->isAbstract())
240 Pointers.insert(DstPT, SrcPT);
241 if (SrcPT->isAbstract())
242 Pointers.insert(SrcPT, DstPT);
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000243
Chris Lattner9ddf2c82008-06-16 19:55:40 +0000244 return RecursiveResolveTypesI(DstPT->getElementType(),
245 SrcPT->getElementType(), Pointers);
Chris Lattnere3092c92003-08-23 21:25:54 +0000246 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000247 }
Chris Lattnere76c57a2003-08-22 06:07:12 +0000248}
249
Chris Lattnera4477f92008-06-16 21:17:12 +0000250static bool RecursiveResolveTypes(const Type *DestTy, const Type *SrcTy) {
Chris Lattner62a81a12008-06-16 21:00:18 +0000251 LinkerTypeMap PointerTypes;
Chris Lattnerbc1c82a2008-06-16 18:19:05 +0000252 return RecursiveResolveTypesI(DestTy, SrcTy, PointerTypes);
Chris Lattnere3092c92003-08-23 21:25:54 +0000253}
254
Chris Lattnere76c57a2003-08-22 06:07:12 +0000255
Chris Lattner2c236f32001-11-03 05:18:24 +0000256// 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 Lattner5c2d3352003-01-30 19:53:34 +0000259static bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) {
Reid Spencer78d033e2007-01-06 07:24:44 +0000260 TypeSymbolTable *DestST = &Dest->getTypeSymbolTable();
261 const TypeSymbolTable *SrcST = &Src->getTypeSymbolTable();
Chris Lattner2c236f32001-11-03 05:18:24 +0000262
263 // Look for a type plane for Type's...
Reid Spencer78d033e2007-01-06 07:24:44 +0000264 TypeSymbolTable::const_iterator TI = SrcST->begin();
265 TypeSymbolTable::const_iterator TE = SrcST->end();
Reid Spencer567bc2c2004-05-25 08:52:20 +0000266 if (TI == TE) return false; // No named types, do nothing.
Chris Lattner2c236f32001-11-03 05:18:24 +0000267
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000268 // 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 Lattner4c00e532003-05-15 16:30:55 +0000271 std::vector<std::string> DelayedTypesToResolve;
272
Reid Spencer567bc2c2004-05-25 08:52:20 +0000273 for ( ; TI != TE; ++TI ) {
274 const std::string &Name = TI->first;
Reid Spencerc28a2242004-07-04 11:52:49 +0000275 const Type *RHS = TI->second;
Chris Lattner2c236f32001-11-03 05:18:24 +0000276
Chris Lattnerbc1c82a2008-06-16 18:19:05 +0000277 // Check to see if this type name is already in the dest module.
Reid Spencer78d033e2007-01-06 07:24:44 +0000278 Type *Entry = DestST->lookup(Name);
Chris Lattner2f6bb2b2003-01-30 20:53:43 +0000279
Chris Lattnerbc1c82a2008-06-16 18:19:05 +0000280 // 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 Lattner4c00e532003-05-15 16:30:55 +0000285 // They look different, save the types 'till later to resolve.
286 DelayedTypesToResolve.push_back(Name);
Chris Lattner2c236f32001-11-03 05:18:24 +0000287 }
288 }
Chris Lattner4c00e532003-05-15 16:30:55 +0000289
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 Lattnere76c57a2003-08-22 06:07:12 +0000295 // Try direct resolution by name...
Chris Lattner4c00e532003-05-15 16:30:55 +0000296 for (unsigned i = 0; i != DelayedTypesToResolve.size(); ++i) {
297 const std::string &Name = DelayedTypesToResolve[i];
Reid Spencer78d033e2007-01-06 07:24:44 +0000298 Type *T1 = SrcST->lookup(Name);
299 Type *T2 = DestST->lookup(Name);
Chris Lattnerbc1c82a2008-06-16 18:19:05 +0000300 if (!ResolveTypes(T2, T1)) {
Chris Lattner4c00e532003-05-15 16:30:55 +0000301 // 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 Lattnere76c57a2003-08-22 06:07:12 +0000309 // Attempt to resolve subelements of types. This allows us to merge these
310 // two types: { int* } and { opaque* }
Chris Lattner4c00e532003-05-15 16:30:55 +0000311 for (unsigned i = 0, e = DelayedTypesToResolve.size(); i != e; ++i) {
312 const std::string &Name = DelayedTypesToResolve[i];
Chris Lattnera4477f92008-06-16 21:17:12 +0000313 if (!RecursiveResolveTypes(SrcST->lookup(Name), DestST->lookup(Name))) {
Chris Lattnere76c57a2003-08-22 06:07:12 +0000314 // We are making progress!
315 DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i);
Misha Brukmanf976c852005-04-21 22:55:34 +0000316
Chris Lattnere76c57a2003-08-22 06:07:12 +0000317 // Go back to the main loop, perhaps we can resolve directly by name
318 // now...
319 break;
320 }
Chris Lattner4c00e532003-05-15 16:30:55 +0000321 }
Chris Lattnere76c57a2003-08-22 06:07:12 +0000322
323 // If we STILL cannot resolve the types, then there is something wrong.
Chris Lattnere76c57a2003-08-22 06:07:12 +0000324 if (DelayedTypesToResolve.size() == OldSize) {
Chris Lattneraeb18ce2003-10-21 22:46:38 +0000325 // Remove the symbol name from the destination.
326 DelayedTypesToResolve.pop_back();
Chris Lattnere76c57a2003-08-22 06:07:12 +0000327 }
Chris Lattner4c00e532003-05-15 16:30:55 +0000328 }
329 }
330
331
Chris Lattner2c236f32001-11-03 05:18:24 +0000332 return false;
333}
334
Chris Lattner0bb87572008-07-14 05:52:33 +0000335#ifndef NDEBUG
Chris Lattner5c2d3352003-01-30 19:53:34 +0000336static 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 Lattner2d3e8bb2001-11-03 03:27:29 +0000338 I != E; ++I) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000339 cerr << " Fr: " << (void*)I->first << " ";
Chris Lattner87182ae2002-04-07 22:31:23 +0000340 I->first->dump();
Bill Wendlinge8156192006-12-07 01:30:32 +0000341 cerr << " To: " << (void*)I->second << " ";
Chris Lattner87182ae2002-04-07 22:31:23 +0000342 I->second->dump();
Bill Wendlinge8156192006-12-07 01:30:32 +0000343 cerr << "\n";
Chris Lattner2d3e8bb2001-11-03 03:27:29 +0000344 }
345}
Chris Lattner0bb87572008-07-14 05:52:33 +0000346#endif
Chris Lattner2d3e8bb2001-11-03 03:27:29 +0000347
348
Reid Spencer619f0242007-02-04 04:43:17 +0000349// RemapOperand - Use ValueMap to convert constants from one module to another.
Chris Lattner5c2d3352003-01-30 19:53:34 +0000350static Value *RemapOperand(const Value *In,
Chris Lattner0033baf2004-11-16 17:12:38 +0000351 std::map<const Value*, Value*> &ValueMap) {
352 std::map<const Value*,Value*>::const_iterator I = ValueMap.find(In);
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000353 if (I != ValueMap.end())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000354 return I->second;
Chris Lattner5c377c52001-10-14 23:29:15 +0000355
Reid Spencer619f0242007-02-04 04:43:17 +0000356 // Check to see if it's a constant that we are interested in transforming.
Chris Lattner620fd682006-06-01 19:14:22 +0000357 Value *Result = 0;
Chris Lattner18961502002-06-25 16:12:52 +0000358 if (const Constant *CPV = dyn_cast<Constant>(In)) {
Chris Lattnerde512b52004-02-15 05:55:15 +0000359 if ((!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) ||
Reid Spencera54b7cb2007-01-12 07:05:14 +0000360 isa<ConstantInt>(CPV) || isa<ConstantAggregateZero>(CPV))
Chris Lattner0033baf2004-11-16 17:12:38 +0000361 return const_cast<Constant*>(CPV); // Simple constants stay identical.
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000362
Chris Lattner18961502002-06-25 16:12:52 +0000363 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
Alkis Evlogimenoscc7ba492004-08-04 08:08:13 +0000364 std::vector<Constant*> Operands(CPA->getNumOperands());
365 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
Chris Lattner0033baf2004-11-16 17:12:38 +0000366 Operands[i] =cast<Constant>(RemapOperand(CPA->getOperand(i), ValueMap));
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000367 Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
Chris Lattner18961502002-06-25 16:12:52 +0000368 } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
Alkis Evlogimenoscc7ba492004-08-04 08:08:13 +0000369 std::vector<Constant*> Operands(CPS->getNumOperands());
370 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
Chris Lattner0033baf2004-11-16 17:12:38 +0000371 Operands[i] =cast<Constant>(RemapOperand(CPS->getOperand(i), ValueMap));
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000372 Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
Chris Lattnerb976e662004-10-16 18:08:06 +0000373 } else if (isa<ConstantPointerNull>(CPV) || isa<UndefValue>(CPV)) {
Chris Lattner18961502002-06-25 16:12:52 +0000374 Result = const_cast<Constant*>(CPV);
Reid Spencer9d6565a2007-02-15 02:26:10 +0000375 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CPV)) {
Chris Lattnera88eb922006-01-19 23:15:58 +0000376 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 Spencer9d6565a2007-02-15 02:26:10 +0000379 Result = ConstantVector::get(Operands);
Chris Lattner6cdf1972002-07-18 00:13:08 +0000380 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
Chris Lattner27d67212006-07-14 22:21:31 +0000381 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 Lattner8d2de8a2001-10-15 03:12:52 +0000385 } else {
Chris Lattner0bb87572008-07-14 05:52:33 +0000386 assert(!isa<GlobalValue>(CPV) && "Unmapped global?");
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000387 assert(0 && "Unknown type of derived type constant value!");
388 }
Chris Lattner620fd682006-06-01 19:14:22 +0000389 } else if (isa<InlineAsm>(In)) {
390 Result = const_cast<Value*>(In);
391 }
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000392
Reid Spencer619f0242007-02-04 04:43:17 +0000393 // Cache the mapping in our local map structure
Chris Lattner620fd682006-06-01 19:14:22 +0000394 if (Result) {
Anton Korobeynikov817bf2a2008-03-10 22:36:08 +0000395 ValueMap[In] = Result;
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000396 return Result;
397 }
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000398
Chris Lattner0bb87572008-07-14 05:52:33 +0000399#ifndef NDEBUG
Bill Wendlinge8156192006-12-07 01:30:32 +0000400 cerr << "LinkModules ValueMap: \n";
Chris Lattner0033baf2004-11-16 17:12:38 +0000401 PrintMap(ValueMap);
Chris Lattner2d3e8bb2001-11-03 03:27:29 +0000402
Bill Wendlinge8156192006-12-07 01:30:32 +0000403 cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n";
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000404 assert(0 && "Couldn't remap value!");
Chris Lattner0bb87572008-07-14 05:52:33 +0000405#endif
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000406 return 0;
Chris Lattner5c377c52001-10-14 23:29:15 +0000407}
408
Reid Spencer8bef0372007-02-04 04:29:21 +0000409/// 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 Lattnerc0036282004-08-04 07:05:54 +0000412static void ForceRenaming(GlobalValue *GV, const std::string &Name) {
413 assert(GV->getName() != Name && "Can't force rename to self");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000414 ValueSymbolTable &ST = GV->getParent()->getValueSymbolTable();
Chris Lattnerc0036282004-08-04 07:05:54 +0000415
416 // If there is a conflict, rename the conflict.
Chris Lattner33f29492007-02-11 00:39:38 +0000417 if (GlobalValue *ConflictGV = cast_or_null<GlobalValue>(ST.lookup(Name))) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000418 assert(ConflictGV->hasLocalLinkage() &&
Reid Spenceref9b9a72007-02-05 20:47:22 +0000419 "Not conflicting with a static global, should link instead!");
Chris Lattner33f29492007-02-11 00:39:38 +0000420 GV->takeName(ConflictGV);
421 ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
Reid Spenceref9b9a72007-02-05 20:47:22 +0000422 assert(ConflictGV->getName() != Name && "ForceRenaming didn't work");
Chris Lattner33f29492007-02-11 00:39:38 +0000423 } else {
424 GV->setName(Name); // Force the name back
Reid Spenceref9b9a72007-02-05 20:47:22 +0000425 }
Reid Spenceref9b9a72007-02-05 20:47:22 +0000426}
Reid Spencer8bef0372007-02-04 04:29:21 +0000427
Reid Spenceref9b9a72007-02-05 20:47:22 +0000428/// CopyGVAttributes - copy additional attributes (those not needed to construct
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000429/// a GlobalValue) from the SrcGV to the DestGV.
Reid Spenceref9b9a72007-02-05 20:47:22 +0000430static void CopyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) {
Duncan Sands28c3cff2008-05-26 19:58:59 +0000431 // 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 Lattnerc0036282004-08-04 07:05:54 +0000435}
436
Chris Lattneraee38ea2004-12-03 22:18:41 +0000437/// 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 Korobeynikov9cd3ccf2007-04-29 20:56:48 +0000441/// 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 Korobeynikov968e39a2008-03-10 22:33:53 +0000444static bool GetLinkageResult(GlobalValue *Dest, const GlobalValue *Src,
Chris Lattneraee38ea2004-12-03 22:18:41 +0000445 GlobalValue::LinkageTypes &LT, bool &LinkFromSrc,
446 std::string *Err) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000447 assert((!Dest || !Src->hasLocalLinkage()) &&
Chris Lattneraee38ea2004-12-03 22:18:41 +0000448 "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 Spencer5cbf9852007-01-30 20:08:39 +0000453 } else if (Src->isDeclaration()) {
Anton Korobeynikov2b48ef02008-03-10 22:33:22 +0000454 // If Src is external or if both Src & Dest are external.. Just link the
Chris Lattneraee38ea2004-12-03 22:18:41 +0000455 // external globals, we aren't adding anything.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000456 if (Src->hasDLLImportLinkage()) {
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000457 // If one of GVs has DLLImport linkage, result should be dllimport'ed.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000458 if (Dest->isDeclaration()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000459 LinkFromSrc = true;
460 LT = Src->getLinkage();
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000461 }
Andrew Lenharth8753c442006-12-15 17:35:32 +0000462 } else if (Dest->hasExternalWeakLinkage()) {
463 //If the Dest is weak, use the source linkage
464 LinkFromSrc = true;
465 LT = Src->getLinkage();
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000466 } else {
467 LinkFromSrc = false;
468 LT = Dest->getLinkage();
469 }
Reid Spencer5cbf9852007-01-30 20:08:39 +0000470 } else if (Dest->isDeclaration() && !Dest->hasDLLImportLinkage()) {
Chris Lattneraee38ea2004-12-03 22:18:41 +0000471 // 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 Sands5df31862008-09-29 11:25:42 +0000480 } else if (Src->mayBeOverridden()) {
Dale Johannesenaafce772008-05-14 20:12:51 +0000481 // At this point we know that Dest has LinkOnce, External*, Weak, Common,
482 // or DLL* linkage.
Anton Korobeynikov80585f12008-07-05 23:48:30 +0000483 if ((Dest->hasLinkOnceLinkage() &&
Dale Johannesenaafce772008-05-14 20:12:51 +0000484 (Src->hasWeakLinkage() || Src->hasCommonLinkage())) ||
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000485 Dest->hasExternalWeakLinkage()) {
Chris Lattneraee38ea2004-12-03 22:18:41 +0000486 LinkFromSrc = true;
487 LT = Src->getLinkage();
488 } else {
489 LinkFromSrc = false;
490 LT = Dest->getLinkage();
491 }
Duncan Sands5df31862008-09-29 11:25:42 +0000492 } else if (Dest->mayBeOverridden()) {
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000493 // 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 Lattneraee38ea2004-12-03 22:18:41 +0000501 } else {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000502 assert((Dest->hasExternalLinkage() ||
503 Dest->hasDLLImportLinkage() ||
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000504 Dest->hasDLLExportLinkage() ||
505 Dest->hasExternalWeakLinkage()) &&
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000506 (Src->hasExternalLinkage() ||
507 Src->hasDLLImportLinkage() ||
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000508 Src->hasDLLExportLinkage() ||
509 Src->hasExternalWeakLinkage()) &&
Chris Lattneraee38ea2004-12-03 22:18:41 +0000510 "Unexpected linkage type!");
Misha Brukmanf976c852005-04-21 22:55:34 +0000511 return Error(Err, "Linking globals named '" + Src->getName() +
Chris Lattneraee38ea2004-12-03 22:18:41 +0000512 "': symbol multiply defined!");
513 }
Anton Korobeynikov9cd3ccf2007-04-29 20:56:48 +0000514
515 // Check visibility
516 if (Dest && Src->getVisibility() != Dest->getVisibility())
Chris Lattner97f8b092007-08-19 22:22:54 +0000517 if (!Src->isDeclaration() && !Dest->isDeclaration())
518 return Error(Err, "Linking globals named '" + Src->getName() +
519 "': symbols have different visibilities!");
Chris Lattneraee38ea2004-12-03 22:18:41 +0000520 return false;
521}
Chris Lattner5c377c52001-10-14 23:29:15 +0000522
523// LinkGlobals - Loop through the global variables in the src module and merge
Chris Lattner8166e6e2003-05-13 21:33:43 +0000524// them into the dest module.
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000525static bool LinkGlobals(Module *Dest, const Module *Src,
Chris Lattner5c2d3352003-01-30 19:53:34 +0000526 std::map<const Value*, Value*> &ValueMap,
Chris Lattner8166e6e2003-05-13 21:33:43 +0000527 std::multimap<std::string, GlobalVariable *> &AppendingVars,
Chris Lattner5c2d3352003-01-30 19:53:34 +0000528 std::string *Err) {
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000529 ValueSymbolTable &DestSymTab = Dest->getValueSymbolTable();
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000530
Chris Lattner5c377c52001-10-14 23:29:15 +0000531 // Loop over all of the globals in the src module, mapping them over as we go
Chris Lattner0bb87572008-07-14 05:52:33 +0000532 for (Module::const_global_iterator I = Src->global_begin(),
533 E = Src->global_end(); I != E; ++I) {
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000534 const GlobalVariable *SGV = I;
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000535 GlobalValue *DGV = 0;
536
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000537 // Check to see if may have to link the global with the global, alias or
538 // function.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000539 if (SGV->hasName() && !SGV->hasLocalLinkage())
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000540 DGV = cast_or_null<GlobalValue>(DestSymTab.lookup(SGV->getNameStart(),
541 SGV->getNameEnd()));
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000542
Chris Lattnerae1132d2008-07-14 06:52:19 +0000543 // 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.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000545 if (DGV && DGV->hasLocalLinkage())
Chris Lattnerae1132d2008-07-14 06:52:19 +0000546 DGV = 0;
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000547
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000548 // 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 Korobeynikov01f69392008-03-10 22:34:28 +0000551
Dan Gohmanc3183292007-10-08 15:13:30 +0000552 assert((SGV->hasInitializer() || SGV->hasExternalWeakLinkage() ||
553 SGV->hasExternalLinkage() || SGV->hasDLLImportLinkage()) &&
Chris Lattner4ad02e72003-04-16 20:28:45 +0000554 "Global must either be external or have an initializer!");
555
Chris Lattnerb324bd72006-11-09 05:18:12 +0000556 GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
557 bool LinkFromSrc = false;
Chris Lattneraee38ea2004-12-03 22:18:41 +0000558 if (GetLinkageResult(DGV, SGV, NewLinkage, LinkFromSrc, Err))
559 return true;
Chris Lattner0fec08e2003-04-21 21:07:05 +0000560
Chris Lattner6157e382008-07-14 07:23:24 +0000561 if (DGV == 0) {
Chris Lattner4ad02e72003-04-16 20:28:45 +0000562 // 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 Lattnerd1ec48c2008-07-14 06:49:45 +0000564 // later by LinkGlobalInits.
Chris Lattner2719bac2003-04-21 21:15:04 +0000565 GlobalVariable *NewDGV =
566 new GlobalVariable(SGV->getType()->getElementType(),
567 SGV->isConstant(), SGV->getLinkage(), /*init*/0,
Chris Lattnera534b0f2008-06-27 03:10:24 +0000568 SGV->getName(), Dest, false,
569 SGV->getType()->getAddressSpace());
Reid Spencer471feac2007-02-04 04:30:33 +0000570 // Propagate alignment, visibility and section info.
Reid Spenceref9b9a72007-02-05 20:47:22 +0000571 CopyGVAttributes(NewDGV, SGV);
Andrew Lenharth5dfbaf12007-02-01 17:12:54 +0000572
Chris Lattner2719bac2003-04-21 21:15:04 +0000573 // 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.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000576 if (!NewDGV->hasLocalLinkage() && NewDGV->getName() != SGV->getName())
Chris Lattnerc0036282004-08-04 07:05:54 +0000577 ForceRenaming(NewDGV, SGV->getName());
Chris Lattner4ad02e72003-04-16 20:28:45 +0000578
Chris Lattner6157e382008-07-14 07:23:24 +0000579 // Make sure to remember this mapping.
Anton Korobeynikov817bf2a2008-03-10 22:36:08 +0000580 ValueMap[SGV] = NewDGV;
581
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000582 // Keep track that this is an appending variable.
Chris Lattner8166e6e2003-05-13 21:33:43 +0000583 if (SGV->hasAppendingLinkage())
Chris Lattner8166e6e2003-05-13 21:33:43 +0000584 AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
Chris Lattner6157e382008-07-14 07:23:24 +0000585 continue;
586 }
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000587
Chris Lattner6157e382008-07-14 07:23:24 +0000588 // 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());
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000592
Chris Lattner6157e382008-07-14 07:23:24 +0000593 if (DGV->hasAppendingLinkage()) {
Chris Lattner8166e6e2003-05-13 21:33:43 +0000594 // 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 Lattnera534b0f2008-06-27 03:10:24 +0000601 "", Dest, false,
602 SGV->getType()->getAddressSpace());
Chris Lattner8166e6e2003-05-13 21:33:43 +0000603
Anton Korobeynikov75c79152008-03-07 18:34:50 +0000604 // Set alignment allowing CopyGVAttributes merge it with alignment of SGV.
Reid Spenceref9b9a72007-02-05 20:47:22 +0000605 NewDGV->setAlignment(DGV->getAlignment());
Anton Korobeynikov75c79152008-03-07 18:34:50 +0000606 // Propagate alignment, section and visibility info.
Reid Spenceref9b9a72007-02-05 20:47:22 +0000607 CopyGVAttributes(NewDGV, SGV);
Andrew Lenharth5dfbaf12007-02-01 17:12:54 +0000608
Chris Lattner8166e6e2003-05-13 21:33:43 +0000609 // Make sure to remember this mapping...
Anton Korobeynikov817bf2a2008-03-10 22:36:08 +0000610 ValueMap[SGV] = NewDGV;
Chris Lattner8166e6e2003-05-13 21:33:43 +0000611
612 // Keep track that this is an appending variable...
613 AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
Chris Lattner6157e382008-07-14 07:23:24 +0000614 continue;
615 }
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000616
Chris Lattner6157e382008-07-14 07:23:24 +0000617 if (LinkFromSrc) {
618 if (isa<GlobalAlias>(DGV))
Anton Korobeynikov1438b9d2008-03-10 22:34:46 +0000619 return Error(Err, "Global-Alias Collision on '" + SGV->getName() +
620 "': symbol multiple defined");
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000621
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000622 // 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 Lattner6157e382008-07-14 07:23:24 +0000627 new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
628 NewLinkage, /*init*/0, DGV->getName(), Dest, false,
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000629 SGV->getType()->getAddressSpace());
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000630
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000631 // Propagate alignment, section, and visibility info.
632 CopyGVAttributes(NewDGV, SGV);
633 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV, DGV->getType()));
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000634
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000635 // 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 Korobeynikov968e39a2008-03-10 22:33:53 +0000643
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000644 // 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.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000646 if (NewDGV->getName() != SGV->getName() && !NewDGV->hasLocalLinkage())
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000647 ForceRenaming(NewDGV, SGV->getName());
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000648
Chris Lattner6157e382008-07-14 07:23:24 +0000649 // Inherit const as appropriate.
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000650 NewDGV->setConstant(SGV->isConstant());
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000651
Chris Lattner6157e382008-07-14 07:23:24 +0000652 // Make sure to remember this mapping.
653 ValueMap[SGV] = NewDGV;
654 continue;
Chris Lattner5c377c52001-10-14 23:29:15 +0000655 }
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000656
Chris Lattner6157e382008-07-14 07:23:24 +0000657 // Not "link from source", keep the one in the DestModule and remap the
658 // input onto it.
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000659
Chris Lattner6157e382008-07-14 07:23:24 +0000660 // 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 Korobeynikovd13726f2008-10-15 20:10:50 +0000665 // 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 }
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000674
Chris Lattner6157e382008-07-14 07:23:24 +0000675 // Set calculated linkage
676 DGV->setLinkage(NewLinkage);
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000677
Chris Lattner6157e382008-07-14 07:23:24 +0000678 // Make sure to remember this mapping...
679 ValueMap[SGV] = ConstantExpr::getBitCast(DGV, SGV->getType());
Chris Lattner5c377c52001-10-14 23:29:15 +0000680 }
681 return false;
682}
683
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000684static GlobalValue::LinkageTypes
685CalculateAliasLinkage(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;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000690 else if (SGV->hasInternalLinkage() && DGV->hasInternalLinkage())
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000691 return GlobalValue::InternalLinkage;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000692 else {
693 assert (SGV->hasPrivateLinkage() && DGV->hasPrivateLinkage() &&
694 "Unexpected linkage type");
695 return GlobalValue::PrivateLinkage;
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000696 }
697}
698
Lauro Ramos Venancio31ed0fb2007-06-28 19:02:54 +0000699// LinkAlias - Loop through the alias in the src module and link them into the
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000700// dest module. We're assuming, that all functions/global variables were already
701// linked in.
Anton Korobeynikov4fb28732008-03-05 15:27:21 +0000702static bool LinkAlias(Module *Dest, const Module *Src,
703 std::map<const Value*, Value*> &ValueMap,
704 std::string *Err) {
Lauro Ramos Venancio31ed0fb2007-06-28 19:02:54 +0000705 // Loop over all alias in the src module
706 for (Module::const_alias_iterator I = Src->alias_begin(),
707 E = Src->alias_end(); I != E; ++I) {
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000708 const GlobalAlias *SGA = I;
709 const GlobalValue *SAliasee = SGA->getAliasedGlobal();
710 GlobalAlias *NewGA = NULL;
Lauro Ramos Venancio31ed0fb2007-06-28 19:02:54 +0000711
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000712 // Globals were already linked, thus we can just query ValueMap for variant
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000713 // of SAliasee in Dest.
Ted Kremenek58d5e052008-03-09 18:32:50 +0000714 std::map<const Value*,Value*>::const_iterator VMI = ValueMap.find(SAliasee);
715 assert(VMI != ValueMap.end() && "Aliasee not linked");
716 GlobalValue* DAliasee = cast<GlobalValue>(VMI->second);
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000717 GlobalValue* DGV = NULL;
Lauro Ramos Venancio31ed0fb2007-06-28 19:02:54 +0000718
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000719 // Try to find something 'similar' to SGA in destination module.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000720 if (!DGV && !SGA->hasLocalLinkage()) {
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000721 DGV = Dest->getNamedAlias(SGA->getName());
Anton Korobeynikov4fb28732008-03-05 15:27:21 +0000722
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000723 // If types don't agree due to opaque types, try to resolve them.
724 if (DGV && DGV->getType() != SGA->getType())
Chris Lattner5ed2ba22008-07-10 01:09:33 +0000725 RecursiveResolveTypes(SGA->getType(), DGV->getType());
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000726 }
727
Rafael Espindolabb46f522009-01-15 20:18:42 +0000728 if (!DGV && !SGA->hasLocalLinkage()) {
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000729 DGV = Dest->getGlobalVariable(SGA->getName());
730
731 // If types don't agree due to opaque types, try to resolve them.
732 if (DGV && DGV->getType() != SGA->getType())
Chris Lattner5ed2ba22008-07-10 01:09:33 +0000733 RecursiveResolveTypes(SGA->getType(), DGV->getType());
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000734 }
735
Rafael Espindolabb46f522009-01-15 20:18:42 +0000736 if (!DGV && !SGA->hasLocalLinkage()) {
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000737 DGV = Dest->getFunction(SGA->getName());
738
739 // If types don't agree due to opaque types, try to resolve them.
740 if (DGV && DGV->getType() != SGA->getType())
Chris Lattner5ed2ba22008-07-10 01:09:33 +0000741 RecursiveResolveTypes(SGA->getType(), DGV->getType());
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000742 }
743
744 // No linking to be performed on internal stuff.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000745 if (DGV && DGV->hasLocalLinkage())
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000746 DGV = NULL;
747
748 if (GlobalAlias *DGA = dyn_cast_or_null<GlobalAlias>(DGV)) {
749 // Types are known to be the same, check whether aliasees equal. As
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000750 // globals are already linked we just need query ValueMap to find the
751 // mapping.
752 if (DAliasee == DGA->getAliasedGlobal()) {
753 // This is just two copies of the same alias. Propagate linkage, if
754 // necessary.
755 DGA->setLinkage(CalculateAliasLinkage(SGA, DGA));
756
757 NewGA = DGA;
758 // Proceed to 'common' steps
759 } else
Anton Korobeynikov1438b9d2008-03-10 22:34:46 +0000760 return Error(Err, "Alias Collision on '" + SGA->getName()+
761 "': aliases have different aliasees");
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000762 } else if (GlobalVariable *DGVar = dyn_cast_or_null<GlobalVariable>(DGV)) {
Anton Korobeynikovf88bc652008-07-05 23:33:22 +0000763 // The only allowed way is to link alias with external declaration or weak
764 // symbol..
Duncan Sands5df31862008-09-29 11:25:42 +0000765 if (DGVar->isDeclaration() || DGVar->mayBeOverridden()) {
Anton Korobeynikoved61c0b2008-03-10 22:36:53 +0000766 // But only if aliasee is global too...
767 if (!isa<GlobalVariable>(DAliasee))
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000768 return Error(Err, "Global-Alias Collision on '" + SGA->getName() +
769 "': aliasee is not global variable");
Anton Korobeynikoved61c0b2008-03-10 22:36:53 +0000770
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000771 NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(),
772 SGA->getName(), DAliasee, Dest);
773 CopyGVAttributes(NewGA, SGA);
774
775 // Any uses of DGV need to change to NewGA, with cast, if needed.
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000776 if (SGA->getType() != DGVar->getType())
777 DGVar->replaceAllUsesWith(ConstantExpr::getBitCast(NewGA,
778 DGVar->getType()));
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000779 else
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000780 DGVar->replaceAllUsesWith(NewGA);
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000781
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000782 // DGVar will conflict with NewGA because they both had the same
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000783 // name. We must erase this now so ForceRenaming doesn't assert
784 // because DGV might not have internal linkage.
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000785 DGVar->eraseFromParent();
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000786
787 // Proceed to 'common' steps
788 } else
Anton Korobeynikov1438b9d2008-03-10 22:34:46 +0000789 return Error(Err, "Global-Alias Collision on '" + SGA->getName() +
790 "': symbol multiple defined");
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000791 } else if (Function *DF = dyn_cast_or_null<Function>(DGV)) {
Anton Korobeynikovf88bc652008-07-05 23:33:22 +0000792 // The only allowed way is to link alias with external declaration or weak
793 // symbol...
Duncan Sands5df31862008-09-29 11:25:42 +0000794 if (DF->isDeclaration() || DF->mayBeOverridden()) {
Anton Korobeynikoved61c0b2008-03-10 22:36:53 +0000795 // But only if aliasee is function too...
796 if (!isa<Function>(DAliasee))
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000797 return Error(Err, "Function-Alias Collision on '" + SGA->getName() +
798 "': aliasee is not function");
Anton Korobeynikoved61c0b2008-03-10 22:36:53 +0000799
Anton Korobeynikovb5a4bd82008-03-05 23:08:16 +0000800 NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(),
801 SGA->getName(), DAliasee, Dest);
802 CopyGVAttributes(NewGA, SGA);
803
804 // Any uses of DF need to change to NewGA, with cast, if needed.
805 if (SGA->getType() != DF->getType())
806 DF->replaceAllUsesWith(ConstantExpr::getBitCast(NewGA,
807 DF->getType()));
808 else
809 DF->replaceAllUsesWith(NewGA);
810
811 // DF will conflict with NewGA because they both had the same
812 // name. We must erase this now so ForceRenaming doesn't assert
813 // because DF might not have internal linkage.
814 DF->eraseFromParent();
815
816 // Proceed to 'common' steps
817 } else
Anton Korobeynikov1438b9d2008-03-10 22:34:46 +0000818 return Error(Err, "Function-Alias Collision on '" + SGA->getName() +
819 "': symbol multiple defined");
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000820 } else {
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000821 // No linking to be performed, simply create an identical version of the
822 // alias over in the dest module...
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000823
824 NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(),
825 SGA->getName(), DAliasee, Dest);
826 CopyGVAttributes(NewGA, SGA);
827
828 // Proceed to 'common' steps
829 }
830
831 assert(NewGA && "No alias was created in destination module!");
832
Anton Korobeynikovb8cdaf72008-03-10 22:36:35 +0000833 // If the symbol table renamed the alias, but it is an externally visible
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000834 // symbol, DGA must be an global value with internal linkage. Rename it.
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000835 if (NewGA->getName() != SGA->getName() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000836 !NewGA->hasLocalLinkage())
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000837 ForceRenaming(NewGA, SGA->getName());
838
839 // Remember this mapping so uses in the source module get remapped
840 // later by RemapOperand.
Anton Korobeynikov817bf2a2008-03-10 22:36:08 +0000841 ValueMap[SGA] = NewGA;
Lauro Ramos Venancio31ed0fb2007-06-28 19:02:54 +0000842 }
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000843
Lauro Ramos Venancio31ed0fb2007-06-28 19:02:54 +0000844 return false;
845}
846
Chris Lattner5c377c52001-10-14 23:29:15 +0000847
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000848// LinkGlobalInits - Update the initializers in the Dest module now that all
849// globals that may be referenced are in Dest.
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000850static bool LinkGlobalInits(Module *Dest, const Module *Src,
Chris Lattner5c2d3352003-01-30 19:53:34 +0000851 std::map<const Value*, Value*> &ValueMap,
852 std::string *Err) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000853 // Loop over all of the globals in the src module, mapping them over as we go
Chris Lattner11273152006-06-16 01:24:04 +0000854 for (Module::const_global_iterator I = Src->global_begin(),
855 E = Src->global_end(); I != E; ++I) {
Chris Lattner18961502002-06-25 16:12:52 +0000856 const GlobalVariable *SGV = I;
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000857
858 if (SGV->hasInitializer()) { // Only process initialized GV's
859 // Figure out what the initializer looks like in the dest module...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000860 Constant *SInit =
Chris Lattner0033baf2004-11-16 17:12:38 +0000861 cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap));
Anton Korobeynikovd13726f2008-10-15 20:10:50 +0000862 // Grab destination global variable or alias.
863 GlobalValue *DGV = cast<GlobalValue>(ValueMap[SGV]->stripPointerCasts());
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000864
Anton Korobeynikovd13726f2008-10-15 20:10:50 +0000865 // If dest if global variable, check that initializers match.
866 if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV)) {
867 if (DGVar->hasInitializer()) {
868 if (SGV->hasExternalLinkage()) {
869 if (DGVar->getInitializer() != SInit)
870 return Error(Err, "Global Variable Collision on '" +
871 SGV->getName() +
872 "': global variables have different initializers");
873 } else if (DGVar->mayBeOverridden()) {
874 // Nothing is required, mapped values will take the new global
875 // automatically.
876 } else if (SGV->mayBeOverridden()) {
877 // Nothing is required, mapped values will take the new global
878 // automatically.
879 } else if (DGVar->hasAppendingLinkage()) {
880 assert(0 && "Appending linkage unimplemented!");
881 } else {
882 assert(0 && "Unknown linkage!");
883 }
Chris Lattner4ad02e72003-04-16 20:28:45 +0000884 } else {
Anton Korobeynikovd13726f2008-10-15 20:10:50 +0000885 // Copy the initializer over now...
886 DGVar->setInitializer(SInit);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000887 }
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000888 } else {
Anton Korobeynikovd13726f2008-10-15 20:10:50 +0000889 // Destination is alias, the only valid situation is when source is
890 // weak. Also, note, that we already checked linkage in LinkGlobals(),
891 // thus we assert here.
892 // FIXME: Should we weaken this assumption, 'dereference' alias and
893 // check for initializer of aliasee?
894 assert(SGV->mayBeOverridden());
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000895 }
896 }
897 }
898 return false;
899}
Chris Lattner5c377c52001-10-14 23:29:15 +0000900
Chris Lattner79df7c02002-03-26 18:01:55 +0000901// LinkFunctionProtos - Link the functions together between the two modules,
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000902// without doing function bodies... this just adds external function prototypes
903// to the Dest function...
Chris Lattner5c377c52001-10-14 23:29:15 +0000904//
Chris Lattner79df7c02002-03-26 18:01:55 +0000905static bool LinkFunctionProtos(Module *Dest, const Module *Src,
Chris Lattner5c2d3352003-01-30 19:53:34 +0000906 std::map<const Value*, Value*> &ValueMap,
907 std::string *Err) {
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000908 ValueSymbolTable &DestSymTab = Dest->getValueSymbolTable();
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000909
Reid Spencer619f0242007-02-04 04:43:17 +0000910 // Loop over all of the functions in the src module, mapping them over
Chris Lattner5c377c52001-10-14 23:29:15 +0000911 for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
Chris Lattner18961502002-06-25 16:12:52 +0000912 const Function *SF = I; // SrcFunction
Anton Korobeynikov194c2ce2008-07-05 23:03:21 +0000913 GlobalValue *DGV = 0;
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000914
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000915 // Check to see if may have to link the function with the global, alias or
916 // function.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000917 if (SF->hasName() && !SF->hasLocalLinkage())
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000918 DGV = cast_or_null<GlobalValue>(DestSymTab.lookup(SF->getNameStart(),
919 SF->getNameEnd()));
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000920
Chris Lattnerae1132d2008-07-14 06:52:19 +0000921 // If we found a global with the same name in the dest module, but it has
922 // internal linkage, we are really not doing any linkage here.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000923 if (DGV && DGV->hasLocalLinkage())
Chris Lattnerae1132d2008-07-14 06:52:19 +0000924 DGV = 0;
925
Chris Lattnerd1ec48c2008-07-14 06:49:45 +0000926 // If types don't agree due to opaque types, try to resolve them.
927 if (DGV && DGV->getType() != SF->getType())
928 RecursiveResolveTypes(SF->getType(), DGV->getType());
Anton Korobeynikov194c2ce2008-07-05 23:03:21 +0000929
Chris Lattner6157e382008-07-14 07:23:24 +0000930 GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
931 bool LinkFromSrc = false;
932 if (GetLinkageResult(DGV, SF, NewLinkage, LinkFromSrc, Err))
933 return true;
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000934
Chris Lattner82468492008-06-09 07:36:11 +0000935 // If there is no linkage to be performed, just bring over SF without
936 // modifying it.
Anton Korobeynikov194c2ce2008-07-05 23:03:21 +0000937 if (DGV == 0) {
Chris Lattner82468492008-06-09 07:36:11 +0000938 // Function does not already exist, simply insert an function signature
939 // identical to SF into the dest module.
940 Function *NewDF = Function::Create(SF->getFunctionType(),
941 SF->getLinkage(),
942 SF->getName(), Dest);
943 CopyGVAttributes(NewDF, SF);
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000944
Chris Lattner82468492008-06-09 07:36:11 +0000945 // If the LLVM runtime renamed the function, but it is an externally
946 // visible symbol, DF must be an existing function with internal linkage.
947 // Rename it.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000948 if (!NewDF->hasLocalLinkage() && NewDF->getName() != SF->getName())
Chris Lattner82468492008-06-09 07:36:11 +0000949 ForceRenaming(NewDF, SF->getName());
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000950
Chris Lattner82468492008-06-09 07:36:11 +0000951 // ... and remember this mapping...
952 ValueMap[SF] = NewDF;
953 continue;
Chris Lattner6157e382008-07-14 07:23:24 +0000954 }
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000955
Chris Lattner6157e382008-07-14 07:23:24 +0000956 // If the visibilities of the symbols disagree and the destination is a
957 // prototype, take the visibility of its input.
958 if (DGV->isDeclaration())
959 DGV->setVisibility(SF->getVisibility());
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000960
Chris Lattner6157e382008-07-14 07:23:24 +0000961 if (LinkFromSrc) {
962 if (isa<GlobalAlias>(DGV))
963 return Error(Err, "Function-Alias Collision on '" + SF->getName() +
964 "': symbol multiple defined");
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000965
Chris Lattner6157e382008-07-14 07:23:24 +0000966 // We have a definition of the same name but different type in the
967 // source module. Copy the prototype to the destination and replace
968 // uses of the destination's prototype with the new prototype.
969 Function *NewDF = Function::Create(SF->getFunctionType(), NewLinkage,
970 SF->getName(), Dest);
971 CopyGVAttributes(NewDF, SF);
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000972
Chris Lattner6157e382008-07-14 07:23:24 +0000973 // Any uses of DF need to change to NewDF, with cast
974 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType()));
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000975
Chris Lattner6157e382008-07-14 07:23:24 +0000976 // DF will conflict with NewDF because they both had the same. We must
977 // erase this now so ForceRenaming doesn't assert because DF might
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000978 // not have internal linkage.
Chris Lattner6157e382008-07-14 07:23:24 +0000979 if (GlobalVariable *Var = dyn_cast<GlobalVariable>(DGV))
980 Var->eraseFromParent();
981 else
982 cast<Function>(DGV)->eraseFromParent();
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000983
Chris Lattner6157e382008-07-14 07:23:24 +0000984 // If the symbol table renamed the function, but it is an externally
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000985 // visible symbol, DF must be an existing function with internal
Chris Lattner6157e382008-07-14 07:23:24 +0000986 // linkage. Rename it.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000987 if (NewDF->getName() != SF->getName() && !NewDF->hasLocalLinkage())
Chris Lattner6157e382008-07-14 07:23:24 +0000988 ForceRenaming(NewDF, SF->getName());
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000989
Chris Lattner6157e382008-07-14 07:23:24 +0000990 // Remember this mapping so uses in the source module get remapped
991 // later by RemapOperand.
992 ValueMap[SF] = NewDF;
993 continue;
994 }
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000995
Chris Lattner6157e382008-07-14 07:23:24 +0000996 // Not "link from source", keep the one in the DestModule and remap the
997 // input onto it.
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +0000998
Chris Lattner6157e382008-07-14 07:23:24 +0000999 if (isa<GlobalAlias>(DGV)) {
Anton Korobeynikovf88bc652008-07-05 23:33:22 +00001000 // The only valid mappings are:
1001 // - SF is external declaration, which is effectively a no-op.
1002 // - SF is weak, when we just need to throw SF out.
Anton Korobeynikove655e372008-10-15 20:10:08 +00001003 if (!SF->isDeclaration() && !SF->mayBeOverridden())
Anton Korobeynikov194c2ce2008-07-05 23:03:21 +00001004 return Error(Err, "Function-Alias Collision on '" + SF->getName() +
1005 "': symbol multiple defined");
Chris Lattner82468492008-06-09 07:36:11 +00001006 }
Anton Korobeynikov194c2ce2008-07-05 23:03:21 +00001007
Chris Lattner6157e382008-07-14 07:23:24 +00001008 // Set calculated linkage
1009 DGV->setLinkage(NewLinkage);
Chris Lattner5c377c52001-10-14 23:29:15 +00001010
Chris Lattner6157e382008-07-14 07:23:24 +00001011 // Make sure to remember this mapping.
1012 ValueMap[SF] = ConstantExpr::getBitCast(DGV, SF->getType());
Chris Lattner5c377c52001-10-14 23:29:15 +00001013 }
1014 return false;
1015}
1016
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +00001017// LinkFunctionBody - Copy the source function over into the dest function and
1018// fix up references to values. At this point we know that Dest is an external
1019// function, and that Src is not.
Chris Lattner4bbfbff2004-11-16 07:31:51 +00001020static bool LinkFunctionBody(Function *Dest, Function *Src,
Reid Spenceref9b9a72007-02-05 20:47:22 +00001021 std::map<const Value*, Value*> &ValueMap,
Chris Lattner5c2d3352003-01-30 19:53:34 +00001022 std::string *Err) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001023 assert(Src && Dest && Dest->isDeclaration() && !Src->isDeclaration());
Chris Lattner5c377c52001-10-14 23:29:15 +00001024
Chris Lattner0033baf2004-11-16 17:12:38 +00001025 // Go through and convert function arguments over, remembering the mapping.
Chris Lattnere4d5c442005-03-15 04:54:21 +00001026 Function::arg_iterator DI = Dest->arg_begin();
1027 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001028 I != E; ++I, ++DI) {
Owen Anderson6bc41e82008-04-14 17:38:21 +00001029 DI->setName(I->getName()); // Copy the name information over...
Chris Lattner5c377c52001-10-14 23:29:15 +00001030
1031 // Add a mapping to our local map
Anton Korobeynikov817bf2a2008-03-10 22:36:08 +00001032 ValueMap[I] = DI;
Chris Lattner5c377c52001-10-14 23:29:15 +00001033 }
1034
Chris Lattner4bbfbff2004-11-16 07:31:51 +00001035 // Splice the body of the source function into the dest function.
1036 Dest->getBasicBlockList().splice(Dest->end(), Src->getBasicBlockList());
Chris Lattner5c377c52001-10-14 23:29:15 +00001037
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +00001038 // At this point, all of the instructions and values of the function are now
1039 // copied over. The only problem is that they are still referencing values in
1040 // the Source function as operands. Loop through all of the operands of the
1041 // functions and patch them up to point to the local versions...
Chris Lattner5c377c52001-10-14 23:29:15 +00001042 //
Chris Lattner18961502002-06-25 16:12:52 +00001043 for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
1044 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1045 for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
Chris Lattner221d6882002-02-12 21:07:25 +00001046 OI != OE; ++OI)
Chris Lattner4bbfbff2004-11-16 07:31:51 +00001047 if (!isa<Instruction>(*OI) && !isa<BasicBlock>(*OI))
Reid Spenceref9b9a72007-02-05 20:47:22 +00001048 *OI = RemapOperand(*OI, ValueMap);
Chris Lattner0033baf2004-11-16 17:12:38 +00001049
1050 // There is no need to map the arguments anymore.
Chris Lattner11273152006-06-16 01:24:04 +00001051 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
1052 I != E; ++I)
Reid Spenceref9b9a72007-02-05 20:47:22 +00001053 ValueMap.erase(I);
Chris Lattner5c377c52001-10-14 23:29:15 +00001054
1055 return false;
1056}
1057
1058
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +00001059// LinkFunctionBodies - Link in the function bodies that are defined in the
1060// source module into the DestModule. This consists basically of copying the
1061// function over and fixing up references to values.
Chris Lattner4bbfbff2004-11-16 07:31:51 +00001062static bool LinkFunctionBodies(Module *Dest, Module *Src,
Chris Lattner5c2d3352003-01-30 19:53:34 +00001063 std::map<const Value*, Value*> &ValueMap,
1064 std::string *Err) {
Chris Lattner5c377c52001-10-14 23:29:15 +00001065
Reid Spencer8bef0372007-02-04 04:29:21 +00001066 // Loop over all of the functions in the src module, mapping them over as we
1067 // go
Chris Lattner4bbfbff2004-11-16 07:31:51 +00001068 for (Module::iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF) {
Reid Spencer619f0242007-02-04 04:43:17 +00001069 if (!SF->isDeclaration()) { // No body if function is external
Chris Lattnerec91ccb2008-06-20 05:29:39 +00001070 Function *DF = dyn_cast<Function>(ValueMap[SF]); // Destination function
Chris Lattner5c377c52001-10-14 23:29:15 +00001071
Chris Lattner18961502002-06-25 16:12:52 +00001072 // DF not external SF external?
Chris Lattnerec91ccb2008-06-20 05:29:39 +00001073 if (DF && DF->isDeclaration())
Chris Lattner35956552003-10-27 16:39:39 +00001074 // Only provide the function body if there isn't one already.
1075 if (LinkFunctionBody(DF, SF, ValueMap, Err))
1076 return true;
Chris Lattnerc2d774b2001-10-23 20:43:42 +00001077 }
Chris Lattner5c377c52001-10-14 23:29:15 +00001078 }
1079 return false;
1080}
1081
Chris Lattner8166e6e2003-05-13 21:33:43 +00001082// LinkAppendingVars - If there were any appending global variables, link them
1083// together now. Return true on error.
Chris Lattner8166e6e2003-05-13 21:33:43 +00001084static bool LinkAppendingVars(Module *M,
1085 std::multimap<std::string, GlobalVariable *> &AppendingVars,
1086 std::string *ErrorMsg) {
1087 if (AppendingVars.empty()) return false; // Nothing to do.
Misha Brukmanf976c852005-04-21 22:55:34 +00001088
Chris Lattner8166e6e2003-05-13 21:33:43 +00001089 // Loop over the multimap of appending vars, processing any variables with the
1090 // same name, forming a new appending global variable with both of the
1091 // initializers merged together, then rewrite references to the old variables
1092 // and delete them.
Chris Lattner8166e6e2003-05-13 21:33:43 +00001093 std::vector<Constant*> Inits;
1094 while (AppendingVars.size() > 1) {
1095 // Get the first two elements in the map...
1096 std::multimap<std::string,
1097 GlobalVariable*>::iterator Second = AppendingVars.begin(), First=Second++;
1098
1099 // If the first two elements are for different names, there is no pair...
1100 // Otherwise there is a pair, so link them together...
1101 if (First->first == Second->first) {
1102 GlobalVariable *G1 = First->second, *G2 = Second->second;
1103 const ArrayType *T1 = cast<ArrayType>(G1->getType()->getElementType());
1104 const ArrayType *T2 = cast<ArrayType>(G2->getType()->getElementType());
Misha Brukmanf976c852005-04-21 22:55:34 +00001105
Chris Lattner8166e6e2003-05-13 21:33:43 +00001106 // Check to see that they two arrays agree on type...
1107 if (T1->getElementType() != T2->getElementType())
1108 return Error(ErrorMsg,
1109 "Appending variables with different element types need to be linked!");
1110 if (G1->isConstant() != G2->isConstant())
1111 return Error(ErrorMsg,
1112 "Appending variables linked with different const'ness!");
1113
Lauro Ramos Venancio9613e872007-06-06 22:01:12 +00001114 if (G1->getAlignment() != G2->getAlignment())
1115 return Error(ErrorMsg,
1116 "Appending variables with different alignment need to be linked!");
1117
1118 if (G1->getVisibility() != G2->getVisibility())
1119 return Error(ErrorMsg,
1120 "Appending variables with different visibility need to be linked!");
1121
1122 if (G1->getSection() != G2->getSection())
1123 return Error(ErrorMsg,
1124 "Appending variables with different section name need to be linked!");
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +00001125
Chris Lattner8166e6e2003-05-13 21:33:43 +00001126 unsigned NewSize = T1->getNumElements() + T2->getNumElements();
1127 ArrayType *NewType = ArrayType::get(T1->getElementType(), NewSize);
1128
Chris Lattnered74a4e2005-12-06 17:30:58 +00001129 G1->setName(""); // Clear G1's name in case of a conflict!
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +00001130
Chris Lattner8166e6e2003-05-13 21:33:43 +00001131 // Create the new global variable...
1132 GlobalVariable *NG =
1133 new GlobalVariable(NewType, G1->isConstant(), G1->getLinkage(),
Chris Lattnera534b0f2008-06-27 03:10:24 +00001134 /*init*/0, First->first, M, G1->isThreadLocal(),
1135 G1->getType()->getAddressSpace());
Chris Lattner8166e6e2003-05-13 21:33:43 +00001136
Lauro Ramos Venancio9613e872007-06-06 22:01:12 +00001137 // Propagate alignment, visibility and section info.
1138 CopyGVAttributes(NG, G1);
1139
Chris Lattner8166e6e2003-05-13 21:33:43 +00001140 // Merge the initializer...
1141 Inits.reserve(NewSize);
Chris Lattnerde512b52004-02-15 05:55:15 +00001142 if (ConstantArray *I = dyn_cast<ConstantArray>(G1->getInitializer())) {
1143 for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
Alkis Evlogimenoscc7ba492004-08-04 08:08:13 +00001144 Inits.push_back(I->getOperand(i));
Chris Lattnerde512b52004-02-15 05:55:15 +00001145 } else {
1146 assert(isa<ConstantAggregateZero>(G1->getInitializer()));
1147 Constant *CV = Constant::getNullValue(T1->getElementType());
1148 for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
1149 Inits.push_back(CV);
1150 }
1151 if (ConstantArray *I = dyn_cast<ConstantArray>(G2->getInitializer())) {
1152 for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
Alkis Evlogimenoscc7ba492004-08-04 08:08:13 +00001153 Inits.push_back(I->getOperand(i));
Chris Lattnerde512b52004-02-15 05:55:15 +00001154 } else {
1155 assert(isa<ConstantAggregateZero>(G2->getInitializer()));
1156 Constant *CV = Constant::getNullValue(T2->getElementType());
1157 for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
1158 Inits.push_back(CV);
1159 }
Chris Lattner8166e6e2003-05-13 21:33:43 +00001160 NG->setInitializer(ConstantArray::get(NewType, Inits));
1161 Inits.clear();
1162
1163 // Replace any uses of the two global variables with uses of the new
1164 // global...
1165
1166 // FIXME: This should rewrite simple/straight-forward uses such as
1167 // getelementptr instructions to not use the Cast!
Reid Spencer4da49122006-12-12 05:05:00 +00001168 G1->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G1->getType()));
1169 G2->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G2->getType()));
Chris Lattner8166e6e2003-05-13 21:33:43 +00001170
1171 // Remove the two globals from the module now...
1172 M->getGlobalList().erase(G1);
1173 M->getGlobalList().erase(G2);
1174
1175 // Put the new global into the AppendingVars map so that we can handle
1176 // linking of more than two vars...
1177 Second->second = NG;
1178 }
1179 AppendingVars.erase(First);
1180 }
1181
1182 return false;
1183}
Chris Lattner52f7e902001-10-13 07:03:50 +00001184
Anton Korobeynikov9f2ee702008-03-05 23:21:39 +00001185static bool ResolveAliases(Module *Dest) {
1186 for (Module::alias_iterator I = Dest->alias_begin(), E = Dest->alias_end();
Anton Korobeynikov52419572008-03-11 22:51:09 +00001187 I != E; ++I)
Anton Korobeynikov19e861a2008-09-09 20:05:04 +00001188 if (const GlobalValue *GV = I->resolveAliasedGlobal())
Anton Korobeynikov832b2a92008-09-09 18:23:48 +00001189 if (GV != I && !GV->isDeclaration())
Anton Korobeynikov52419572008-03-11 22:51:09 +00001190 I->replaceAllUsesWith(const_cast<GlobalValue*>(GV));
Anton Korobeynikov9f2ee702008-03-05 23:21:39 +00001191
1192 return false;
1193}
Chris Lattner52f7e902001-10-13 07:03:50 +00001194
1195// LinkModules - This function links two modules together, with the resulting
1196// left module modified to be the composite of the two input modules. If an
1197// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
Chris Lattner5c377c52001-10-14 23:29:15 +00001198// the problem. Upon failure, the Dest module could be in a modified state, and
1199// shouldn't be relied on to be consistent.
Misha Brukmanf976c852005-04-21 22:55:34 +00001200bool
Reid Spencer0ba9e212004-12-13 03:00:16 +00001201Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) {
Reid Spencer57a0efa2004-09-11 04:25:17 +00001202 assert(Dest != 0 && "Invalid Destination module");
1203 assert(Src != 0 && "Invalid Source Module");
1204
Chris Lattnerc36357c2007-01-29 00:21:34 +00001205 if (Dest->getDataLayout().empty()) {
1206 if (!Src->getDataLayout().empty()) {
Chris Lattnerec9bfdc2007-01-29 02:18:13 +00001207 Dest->setDataLayout(Src->getDataLayout());
Chris Lattnerc36357c2007-01-29 00:21:34 +00001208 } else {
1209 std::string DataLayout;
Reid Spencer26f23852007-01-26 08:11:39 +00001210
Anton Korobeynikova27694d2008-02-20 11:27:04 +00001211 if (Dest->getEndianness() == Module::AnyEndianness) {
Chris Lattnerc36357c2007-01-29 00:21:34 +00001212 if (Src->getEndianness() == Module::BigEndian)
1213 DataLayout.append("E");
1214 else if (Src->getEndianness() == Module::LittleEndian)
1215 DataLayout.append("e");
Anton Korobeynikova27694d2008-02-20 11:27:04 +00001216 }
1217
1218 if (Dest->getPointerSize() == Module::AnyPointerSize) {
Chris Lattnerc36357c2007-01-29 00:21:34 +00001219 if (Src->getPointerSize() == Module::Pointer64)
1220 DataLayout.append(DataLayout.length() == 0 ? "p:64:64" : "-p:64:64");
1221 else if (Src->getPointerSize() == Module::Pointer32)
1222 DataLayout.append(DataLayout.length() == 0 ? "p:32:32" : "-p:32:32");
Anton Korobeynikova27694d2008-02-20 11:27:04 +00001223 }
Chris Lattnerc36357c2007-01-29 00:21:34 +00001224 Dest->setDataLayout(DataLayout);
1225 }
1226 }
1227
Chris Lattnerf27dfcb2008-02-19 18:49:08 +00001228 // Copy the target triple from the source to dest if the dest's is empty.
Chris Lattnerc36357c2007-01-29 00:21:34 +00001229 if (Dest->getTargetTriple().empty() && !Src->getTargetTriple().empty())
Chris Lattner152f19a2004-12-10 20:26:15 +00001230 Dest->setTargetTriple(Src->getTargetTriple());
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +00001231
Chris Lattnerc36357c2007-01-29 00:21:34 +00001232 if (!Src->getDataLayout().empty() && !Dest->getDataLayout().empty() &&
1233 Src->getDataLayout() != Dest->getDataLayout())
Reid Spencer26f23852007-01-26 08:11:39 +00001234 cerr << "WARNING: Linking two modules of different data layouts!\n";
Chris Lattner152f19a2004-12-10 20:26:15 +00001235 if (!Src->getTargetTriple().empty() &&
1236 Dest->getTargetTriple() != Src->getTargetTriple())
Bill Wendlinge8156192006-12-07 01:30:32 +00001237 cerr << "WARNING: Linking two modules of different target triples!\n";
Misha Brukmanf976c852005-04-21 22:55:34 +00001238
Chris Lattnerf27dfcb2008-02-19 18:49:08 +00001239 // Append the module inline asm string.
Chris Lattner66316012006-01-24 04:14:29 +00001240 if (!Src->getModuleInlineAsm().empty()) {
1241 if (Dest->getModuleInlineAsm().empty())
1242 Dest->setModuleInlineAsm(Src->getModuleInlineAsm());
Chris Lattnere1b2e142006-01-23 23:08:37 +00001243 else
Chris Lattner66316012006-01-24 04:14:29 +00001244 Dest->setModuleInlineAsm(Dest->getModuleInlineAsm()+"\n"+
1245 Src->getModuleInlineAsm());
Chris Lattnere1b2e142006-01-23 23:08:37 +00001246 }
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +00001247
Reid Spencer719012d2004-11-25 09:29:44 +00001248 // Update the destination module's dependent libraries list with the libraries
Reid Spencer57a0efa2004-09-11 04:25:17 +00001249 // from the source module. There's no opportunity for duplicates here as the
1250 // Module ensures that duplicate insertions are discarded.
Chris Lattnerf27dfcb2008-02-19 18:49:08 +00001251 for (Module::lib_iterator SI = Src->lib_begin(), SE = Src->lib_end();
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +00001252 SI != SE; ++SI)
Reid Spencer57a0efa2004-09-11 04:25:17 +00001253 Dest->addLibrary(*SI);
Reid Spencer57a0efa2004-09-11 04:25:17 +00001254
Chris Lattner2c236f32001-11-03 05:18:24 +00001255 // LinkTypes - Go through the symbol table of the Src module and see if any
1256 // types are named in the src module that are not named in the Dst module.
1257 // Make sure there are no type name conflicts.
Mikhail Glushenkoveba2cb02009-03-03 07:22:23 +00001258 if (LinkTypes(Dest, Src, ErrorMsg))
Reid Spencer619f0242007-02-04 04:43:17 +00001259 return true;
Chris Lattner2c236f32001-11-03 05:18:24 +00001260
Chris Lattner5c377c52001-10-14 23:29:15 +00001261 // ValueMap - Mapping of values from what they used to be in Src, to what they
1262 // are now in Dest.
Chris Lattner5c2d3352003-01-30 19:53:34 +00001263 std::map<const Value*, Value*> ValueMap;
Chris Lattner5c377c52001-10-14 23:29:15 +00001264
Chris Lattner8166e6e2003-05-13 21:33:43 +00001265 // AppendingVars - Keep track of global variables in the destination module
1266 // with appending linkage. After the module is linked together, they are
1267 // appended and the module is rewritten.
Chris Lattner8166e6e2003-05-13 21:33:43 +00001268 std::multimap<std::string, GlobalVariable *> AppendingVars;
Chris Lattner11273152006-06-16 01:24:04 +00001269 for (Module::global_iterator I = Dest->global_begin(), E = Dest->global_end();
1270 I != E; ++I) {
Chris Lattner5a837de2004-08-04 07:44:58 +00001271 // Add all of the appending globals already in the Dest module to
1272 // AppendingVars.
Chris Lattnerf4146462003-05-14 12:11:51 +00001273 if (I->hasAppendingLinkage())
1274 AppendingVars.insert(std::make_pair(I->getName(), I));
Chris Lattner5a837de2004-08-04 07:44:58 +00001275 }
1276
Chris Lattner8166e6e2003-05-13 21:33:43 +00001277 // Insert all of the globals in src into the Dest module... without linking
1278 // initializers (which could refer to functions not yet mapped over).
Reid Spenceref9b9a72007-02-05 20:47:22 +00001279 if (LinkGlobals(Dest, Src, ValueMap, AppendingVars, ErrorMsg))
Chris Lattner5a837de2004-08-04 07:44:58 +00001280 return true;
Chris Lattner5c377c52001-10-14 23:29:15 +00001281
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +00001282 // Link the functions together between the two modules, without doing function
1283 // bodies... this just adds external function prototypes to the Dest
1284 // function... We do this so that when we begin processing function bodies,
1285 // all of the global values that may be referenced are available in our
1286 // ValueMap.
Reid Spenceref9b9a72007-02-05 20:47:22 +00001287 if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg))
Chris Lattner5a837de2004-08-04 07:44:58 +00001288 return true;
Chris Lattner5c377c52001-10-14 23:29:15 +00001289
Anton Korobeynikov4fb28732008-03-05 15:27:21 +00001290 // If there were any alias, link them now. We really need to do this now,
1291 // because all of the aliases that may be referenced need to be available in
1292 // ValueMap
1293 if (LinkAlias(Dest, Src, ValueMap, ErrorMsg)) return true;
1294
Chris Lattner6cdf1972002-07-18 00:13:08 +00001295 // Update the initializers in the Dest module now that all globals that may
1296 // be referenced are in Dest.
Chris Lattner6cdf1972002-07-18 00:13:08 +00001297 if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
1298
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +00001299 // Link in the function bodies that are defined in the source module into the
1300 // DestModule. This consists basically of copying the function over and
1301 // fixing up references to values.
Chris Lattner79df7c02002-03-26 18:01:55 +00001302 if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true;
Chris Lattner52f7e902001-10-13 07:03:50 +00001303
Chris Lattner8166e6e2003-05-13 21:33:43 +00001304 // If there were any appending global variables, link them together now.
Chris Lattner8166e6e2003-05-13 21:33:43 +00001305 if (LinkAppendingVars(Dest, AppendingVars, ErrorMsg)) return true;
1306
Anton Korobeynikov3db91912008-03-05 23:08:47 +00001307 // Resolve all uses of aliases with aliasees
1308 if (ResolveAliases(Dest)) return true;
1309
Reid Spencer57a0efa2004-09-11 04:25:17 +00001310 // If the source library's module id is in the dependent library list of the
1311 // destination library, remove it since that module is now linked in.
1312 sys::Path modId;
Reid Spencerdd04df02005-07-07 23:21:43 +00001313 modId.set(Src->getModuleIdentifier());
Reid Spencer07adb282004-11-05 22:15:36 +00001314 if (!modId.isEmpty())
1315 Dest->removeLibrary(modId.getBasename());
Reid Spencer57a0efa2004-09-11 04:25:17 +00001316
Chris Lattner52f7e902001-10-13 07:03:50 +00001317 return false;
1318}
Vikram S. Adve9466f512001-10-28 21:38:02 +00001319
Reid Spencer567bc2c2004-05-25 08:52:20 +00001320// vim: sw=2