blob: d771d053ad867174ac17f46f72d3a2e6b15c4fc7 [file] [log] [blame]
Reid Spencer7f496022004-11-12 20:37:43 +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
Reid Spencer719012d2004-11-25 09:29:44 +000040// ToStr - Simple wrapper function to convert a type to a string.
Chris Lattner72cf7df2004-08-21 00:50:59 +000041static std::string ToStr(const Type *Ty, const Module *M) {
42 std::ostringstream OS;
43 WriteTypeSymbolic(OS, Ty, M);
44 return OS.str();
45}
46
John Criswell700867b2003-11-04 15:22:26 +000047//
48// Function: ResolveTypes()
49//
50// Description:
51// Attempt to link the two specified types together.
52//
53// Inputs:
54// DestTy - The type to which we wish to resolve.
55// SrcTy - The original type which we want to resolve.
John Criswell700867b2003-11-04 15:22:26 +000056//
57// Outputs:
58// DestST - The symbol table in which the new type should be placed.
59//
60// Return value:
61// true - There is an error and the types cannot yet be linked.
62// false - No errors.
Chris Lattner4c00e532003-05-15 16:30:55 +000063//
Chris Lattnerbc1c82a2008-06-16 18:19:05 +000064static bool ResolveTypes(const Type *DestTy, const Type *SrcTy) {
Chris Lattnere76c57a2003-08-22 06:07:12 +000065 if (DestTy == SrcTy) return false; // If already equal, noop
Chris Lattnerbc1c82a2008-06-16 18:19:05 +000066 assert(DestTy && SrcTy && "Can't handle null types");
Chris Lattnere76c57a2003-08-22 06:07:12 +000067
Chris Lattnerbc1c82a2008-06-16 18:19:05 +000068 if (const OpaqueType *OT = dyn_cast<OpaqueType>(DestTy)) {
69 // Type _is_ in module, just opaque...
70 const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(SrcTy);
71 } else if (const OpaqueType *OT = dyn_cast<OpaqueType>(SrcTy)) {
72 const_cast<OpaqueType*>(OT)->refineAbstractTypeTo(DestTy);
73 } else {
74 return true; // Cannot link types... not-equal and neither is opaque.
Chris Lattner4c00e532003-05-15 16:30:55 +000075 }
76 return false;
77}
78
Chris Lattner62a81a12008-06-16 21:00:18 +000079/// LinkerTypeMap - This implements a map of types that is stable
80/// even if types are resolved/refined to other types. This is not a general
81/// purpose map, it is specific to the linker's use.
82namespace {
83class LinkerTypeMap : public AbstractTypeUser {
84 typedef DenseMap<const Type*, PATypeHolder> TheMapTy;
85 TheMapTy TheMap;
Chris Lattner62a81a12008-06-16 21:00:18 +000086
Chris Lattnerfc196f92008-06-16 23:06:51 +000087 LinkerTypeMap(const LinkerTypeMap&); // DO NOT IMPLEMENT
88 void operator=(const LinkerTypeMap&); // DO NOT IMPLEMENT
89public:
90 LinkerTypeMap() {}
91 ~LinkerTypeMap() {
Chris Lattner62a81a12008-06-16 21:00:18 +000092 for (DenseMap<const Type*, PATypeHolder>::iterator I = TheMap.begin(),
93 E = TheMap.end(); I != E; ++I)
94 I->first->removeAbstractTypeUser(this);
95 }
96
97 /// lookup - Return the value for the specified type or null if it doesn't
98 /// exist.
99 const Type *lookup(const Type *Ty) const {
100 TheMapTy::const_iterator I = TheMap.find(Ty);
101 if (I != TheMap.end()) return I->second;
102 return 0;
103 }
104
105 /// erase - Remove the specified type, returning true if it was in the set.
106 bool erase(const Type *Ty) {
107 if (!TheMap.erase(Ty))
108 return false;
109 if (Ty->isAbstract())
110 Ty->removeAbstractTypeUser(this);
111 return true;
112 }
113
114 /// insert - This returns true if the pointer was new to the set, false if it
115 /// was already in the set.
116 bool insert(const Type *Src, const Type *Dst) {
Dan Gohman6b345ee2008-07-07 17:46:23 +0000117 if (!TheMap.insert(std::make_pair(Src, PATypeHolder(Dst))).second)
Chris Lattner62a81a12008-06-16 21:00:18 +0000118 return false; // Already in map.
119 if (Src->isAbstract())
120 Src->addAbstractTypeUser(this);
121 return true;
122 }
123
124protected:
125 /// refineAbstractType - The callback method invoked when an abstract type is
126 /// resolved to another type. An object must override this method to update
127 /// its internal state to reference NewType instead of OldType.
128 ///
129 virtual void refineAbstractType(const DerivedType *OldTy,
130 const Type *NewTy) {
131 TheMapTy::iterator I = TheMap.find(OldTy);
132 const Type *DstTy = I->second;
133
134 TheMap.erase(I);
135 if (OldTy->isAbstract())
136 OldTy->removeAbstractTypeUser(this);
137
138 // Don't reinsert into the map if the key is concrete now.
139 if (NewTy->isAbstract())
140 insert(NewTy, DstTy);
141 }
142
143 /// The other case which AbstractTypeUsers must be aware of is when a type
144 /// makes the transition from being abstract (where it has clients on it's
145 /// AbstractTypeUsers list) to concrete (where it does not). This method
146 /// notifies ATU's when this occurs for a type.
147 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
148 TheMap.erase(AbsTy);
149 AbsTy->removeAbstractTypeUser(this);
150 }
151
152 // for debugging...
153 virtual void dump() const {
154 cerr << "AbstractTypeSet!\n";
155 }
156};
157}
158
159
Chris Lattnere76c57a2003-08-22 06:07:12 +0000160// RecursiveResolveTypes - This is just like ResolveTypes, except that it
161// recurses down into derived types, merging the used types if the parent types
162// are compatible.
Chris Lattnera4477f92008-06-16 21:17:12 +0000163static bool RecursiveResolveTypesI(const Type *DstTy, const Type *SrcTy,
Chris Lattner62a81a12008-06-16 21:00:18 +0000164 LinkerTypeMap &Pointers) {
Chris Lattnera4477f92008-06-16 21:17:12 +0000165 if (DstTy == SrcTy) return false; // If already equal, noop
Misha Brukmanf976c852005-04-21 22:55:34 +0000166
Chris Lattnere76c57a2003-08-22 06:07:12 +0000167 // If we found our opaque type, resolve it now!
Chris Lattnera4477f92008-06-16 21:17:12 +0000168 if (isa<OpaqueType>(DstTy) || isa<OpaqueType>(SrcTy))
169 return ResolveTypes(DstTy, SrcTy);
Misha Brukmanf976c852005-04-21 22:55:34 +0000170
Chris Lattnere76c57a2003-08-22 06:07:12 +0000171 // Two types cannot be resolved together if they are of different primitive
172 // type. For example, we cannot resolve an int to a float.
Chris Lattnera4477f92008-06-16 21:17:12 +0000173 if (DstTy->getTypeID() != SrcTy->getTypeID()) return true;
Chris Lattnere76c57a2003-08-22 06:07:12 +0000174
Chris Lattner56539652008-06-16 20:03:01 +0000175 // If neither type is abstract, then they really are just different types.
Chris Lattnera4477f92008-06-16 21:17:12 +0000176 if (!DstTy->isAbstract() && !SrcTy->isAbstract())
Chris Lattner56539652008-06-16 20:03:01 +0000177 return true;
178
Chris Lattnere76c57a2003-08-22 06:07:12 +0000179 // Otherwise, resolve the used type used by this derived type...
Chris Lattnera4477f92008-06-16 21:17:12 +0000180 switch (DstTy->getTypeID()) {
Chris Lattnerf6f4f7a2008-06-16 18:27:53 +0000181 default:
182 return true;
Chris Lattnere76c57a2003-08-22 06:07:12 +0000183 case Type::FunctionTyID: {
Chris Lattnera4477f92008-06-16 21:17:12 +0000184 const FunctionType *DstFT = cast<FunctionType>(DstTy);
185 const FunctionType *SrcFT = cast<FunctionType>(SrcTy);
Chris Lattner9ddf2c82008-06-16 19:55:40 +0000186 if (DstFT->isVarArg() != SrcFT->isVarArg() ||
187 DstFT->getNumContainedTypes() != SrcFT->getNumContainedTypes())
Chris Lattner43f4ba82003-08-22 19:12:55 +0000188 return true;
Chris Lattnera4477f92008-06-16 21:17:12 +0000189
190 // Use TypeHolder's so recursive resolution won't break us.
191 PATypeHolder ST(SrcFT), DT(DstFT);
192 for (unsigned i = 0, e = DstFT->getNumContainedTypes(); i != e; ++i) {
193 const Type *SE = ST->getContainedType(i), *DE = DT->getContainedType(i);
194 if (SE != DE && RecursiveResolveTypesI(DE, SE, Pointers))
Chris Lattnere76c57a2003-08-22 06:07:12 +0000195 return true;
Chris Lattnera4477f92008-06-16 21:17:12 +0000196 }
Chris Lattnere76c57a2003-08-22 06:07:12 +0000197 return false;
198 }
199 case Type::StructTyID: {
Chris Lattnera4477f92008-06-16 21:17:12 +0000200 const StructType *DstST = cast<StructType>(DstTy);
201 const StructType *SrcST = cast<StructType>(SrcTy);
Chris Lattner9ddf2c82008-06-16 19:55:40 +0000202 if (DstST->getNumContainedTypes() != SrcST->getNumContainedTypes())
Chris Lattnerf6f4f7a2008-06-16 18:27:53 +0000203 return true;
Chris Lattnera4477f92008-06-16 21:17:12 +0000204
205 PATypeHolder ST(SrcST), DT(DstST);
206 for (unsigned i = 0, e = DstST->getNumContainedTypes(); i != e; ++i) {
207 const Type *SE = ST->getContainedType(i), *DE = DT->getContainedType(i);
208 if (SE != DE && RecursiveResolveTypesI(DE, SE, Pointers))
Chris Lattnere76c57a2003-08-22 06:07:12 +0000209 return true;
Chris Lattnera4477f92008-06-16 21:17:12 +0000210 }
Chris Lattnere76c57a2003-08-22 06:07:12 +0000211 return false;
212 }
213 case Type::ArrayTyID: {
Chris Lattnera4477f92008-06-16 21:17:12 +0000214 const ArrayType *DAT = cast<ArrayType>(DstTy);
215 const ArrayType *SAT = cast<ArrayType>(SrcTy);
Chris Lattnere76c57a2003-08-22 06:07:12 +0000216 if (DAT->getNumElements() != SAT->getNumElements()) return true;
Chris Lattnere3092c92003-08-23 21:25:54 +0000217 return RecursiveResolveTypesI(DAT->getElementType(), SAT->getElementType(),
Chris Lattnerbc1c82a2008-06-16 18:19:05 +0000218 Pointers);
Chris Lattnere76c57a2003-08-22 06:07:12 +0000219 }
Chris Lattnerf6f4f7a2008-06-16 18:27:53 +0000220 case Type::VectorTyID: {
Chris Lattnera4477f92008-06-16 21:17:12 +0000221 const VectorType *DVT = cast<VectorType>(DstTy);
222 const VectorType *SVT = cast<VectorType>(SrcTy);
Chris Lattnerf6f4f7a2008-06-16 18:27:53 +0000223 if (DVT->getNumElements() != SVT->getNumElements()) return true;
224 return RecursiveResolveTypesI(DVT->getElementType(), SVT->getElementType(),
225 Pointers);
226 }
Chris Lattnere3092c92003-08-23 21:25:54 +0000227 case Type::PointerTyID: {
Chris Lattnera4477f92008-06-16 21:17:12 +0000228 const PointerType *DstPT = cast<PointerType>(DstTy);
229 const PointerType *SrcPT = cast<PointerType>(SrcTy);
Chris Lattner9ddf2c82008-06-16 19:55:40 +0000230
231 if (DstPT->getAddressSpace() != SrcPT->getAddressSpace())
232 return true;
233
Chris Lattnere3092c92003-08-23 21:25:54 +0000234 // If this is a pointer type, check to see if we have already seen it. If
235 // so, we are in a recursive branch. Cut off the search now. We cannot use
236 // an associative container for this search, because the type pointers (keys
Chris Lattner62a81a12008-06-16 21:00:18 +0000237 // in the container) change whenever types get resolved.
238 if (SrcPT->isAbstract())
239 if (const Type *ExistingDestTy = Pointers.lookup(SrcPT))
240 return ExistingDestTy != DstPT;
241
242 if (DstPT->isAbstract())
243 if (const Type *ExistingSrcTy = Pointers.lookup(DstPT))
244 return ExistingSrcTy != SrcPT;
Chris Lattnere3092c92003-08-23 21:25:54 +0000245 // Otherwise, add the current pointers to the vector to stop recursion on
246 // this pair.
Chris Lattner62a81a12008-06-16 21:00:18 +0000247 if (DstPT->isAbstract())
248 Pointers.insert(DstPT, SrcPT);
249 if (SrcPT->isAbstract())
250 Pointers.insert(SrcPT, DstPT);
Chris Lattnera4477f92008-06-16 21:17:12 +0000251
Chris Lattner9ddf2c82008-06-16 19:55:40 +0000252 return RecursiveResolveTypesI(DstPT->getElementType(),
253 SrcPT->getElementType(), Pointers);
Chris Lattnere3092c92003-08-23 21:25:54 +0000254 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000255 }
Chris Lattnere76c57a2003-08-22 06:07:12 +0000256}
257
Chris Lattnera4477f92008-06-16 21:17:12 +0000258static bool RecursiveResolveTypes(const Type *DestTy, const Type *SrcTy) {
Chris Lattner62a81a12008-06-16 21:00:18 +0000259 LinkerTypeMap PointerTypes;
Chris Lattnerbc1c82a2008-06-16 18:19:05 +0000260 return RecursiveResolveTypesI(DestTy, SrcTy, PointerTypes);
Chris Lattnere3092c92003-08-23 21:25:54 +0000261}
262
Chris Lattnere76c57a2003-08-22 06:07:12 +0000263
Chris Lattner2c236f32001-11-03 05:18:24 +0000264// LinkTypes - Go through the symbol table of the Src module and see if any
265// types are named in the src module that are not named in the Dst module.
266// Make sure there are no type name conflicts.
Chris Lattner5c2d3352003-01-30 19:53:34 +0000267static bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) {
Reid Spencer78d033e2007-01-06 07:24:44 +0000268 TypeSymbolTable *DestST = &Dest->getTypeSymbolTable();
269 const TypeSymbolTable *SrcST = &Src->getTypeSymbolTable();
Chris Lattner2c236f32001-11-03 05:18:24 +0000270
271 // Look for a type plane for Type's...
Reid Spencer78d033e2007-01-06 07:24:44 +0000272 TypeSymbolTable::const_iterator TI = SrcST->begin();
273 TypeSymbolTable::const_iterator TE = SrcST->end();
Reid Spencer567bc2c2004-05-25 08:52:20 +0000274 if (TI == TE) return false; // No named types, do nothing.
Chris Lattner2c236f32001-11-03 05:18:24 +0000275
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000276 // Some types cannot be resolved immediately because they depend on other
277 // types being resolved to each other first. This contains a list of types we
278 // are waiting to recheck.
Chris Lattner4c00e532003-05-15 16:30:55 +0000279 std::vector<std::string> DelayedTypesToResolve;
280
Reid Spencer567bc2c2004-05-25 08:52:20 +0000281 for ( ; TI != TE; ++TI ) {
282 const std::string &Name = TI->first;
Reid Spencerc28a2242004-07-04 11:52:49 +0000283 const Type *RHS = TI->second;
Chris Lattner2c236f32001-11-03 05:18:24 +0000284
Chris Lattnerbc1c82a2008-06-16 18:19:05 +0000285 // Check to see if this type name is already in the dest module.
Reid Spencer78d033e2007-01-06 07:24:44 +0000286 Type *Entry = DestST->lookup(Name);
Chris Lattner2f6bb2b2003-01-30 20:53:43 +0000287
Chris Lattnerbc1c82a2008-06-16 18:19:05 +0000288 // If the name is just in the source module, bring it over to the dest.
289 if (Entry == 0) {
290 if (!Name.empty())
291 DestST->insert(Name, const_cast<Type*>(RHS));
292 } else if (ResolveTypes(Entry, RHS)) {
Chris Lattner4c00e532003-05-15 16:30:55 +0000293 // They look different, save the types 'till later to resolve.
294 DelayedTypesToResolve.push_back(Name);
Chris Lattner2c236f32001-11-03 05:18:24 +0000295 }
296 }
Chris Lattner4c00e532003-05-15 16:30:55 +0000297
298 // Iteratively resolve types while we can...
299 while (!DelayedTypesToResolve.empty()) {
300 // Loop over all of the types, attempting to resolve them if possible...
301 unsigned OldSize = DelayedTypesToResolve.size();
302
Chris Lattnere76c57a2003-08-22 06:07:12 +0000303 // Try direct resolution by name...
Chris Lattner4c00e532003-05-15 16:30:55 +0000304 for (unsigned i = 0; i != DelayedTypesToResolve.size(); ++i) {
305 const std::string &Name = DelayedTypesToResolve[i];
Reid Spencer78d033e2007-01-06 07:24:44 +0000306 Type *T1 = SrcST->lookup(Name);
307 Type *T2 = DestST->lookup(Name);
Chris Lattnerbc1c82a2008-06-16 18:19:05 +0000308 if (!ResolveTypes(T2, T1)) {
Chris Lattner4c00e532003-05-15 16:30:55 +0000309 // We are making progress!
310 DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i);
311 --i;
312 }
313 }
314
315 // Did we not eliminate any types?
316 if (DelayedTypesToResolve.size() == OldSize) {
Chris Lattnere76c57a2003-08-22 06:07:12 +0000317 // Attempt to resolve subelements of types. This allows us to merge these
318 // two types: { int* } and { opaque* }
Chris Lattner4c00e532003-05-15 16:30:55 +0000319 for (unsigned i = 0, e = DelayedTypesToResolve.size(); i != e; ++i) {
320 const std::string &Name = DelayedTypesToResolve[i];
Chris Lattnera4477f92008-06-16 21:17:12 +0000321 if (!RecursiveResolveTypes(SrcST->lookup(Name), DestST->lookup(Name))) {
Chris Lattnere76c57a2003-08-22 06:07:12 +0000322 // We are making progress!
323 DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i);
Misha Brukmanf976c852005-04-21 22:55:34 +0000324
Chris Lattnere76c57a2003-08-22 06:07:12 +0000325 // Go back to the main loop, perhaps we can resolve directly by name
326 // now...
327 break;
328 }
Chris Lattner4c00e532003-05-15 16:30:55 +0000329 }
Chris Lattnere76c57a2003-08-22 06:07:12 +0000330
331 // If we STILL cannot resolve the types, then there is something wrong.
Chris Lattnere76c57a2003-08-22 06:07:12 +0000332 if (DelayedTypesToResolve.size() == OldSize) {
Chris Lattneraeb18ce2003-10-21 22:46:38 +0000333 // Remove the symbol name from the destination.
334 DelayedTypesToResolve.pop_back();
Chris Lattnere76c57a2003-08-22 06:07:12 +0000335 }
Chris Lattner4c00e532003-05-15 16:30:55 +0000336 }
337 }
338
339
Chris Lattner2c236f32001-11-03 05:18:24 +0000340 return false;
341}
342
Chris Lattner0bb87572008-07-14 05:52:33 +0000343#ifndef NDEBUG
Chris Lattner5c2d3352003-01-30 19:53:34 +0000344static void PrintMap(const std::map<const Value*, Value*> &M) {
345 for (std::map<const Value*, Value*>::const_iterator I = M.begin(), E =M.end();
Chris Lattner2d3e8bb2001-11-03 03:27:29 +0000346 I != E; ++I) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000347 cerr << " Fr: " << (void*)I->first << " ";
Chris Lattner87182ae2002-04-07 22:31:23 +0000348 I->first->dump();
Bill Wendlinge8156192006-12-07 01:30:32 +0000349 cerr << " To: " << (void*)I->second << " ";
Chris Lattner87182ae2002-04-07 22:31:23 +0000350 I->second->dump();
Bill Wendlinge8156192006-12-07 01:30:32 +0000351 cerr << "\n";
Chris Lattner2d3e8bb2001-11-03 03:27:29 +0000352 }
353}
Chris Lattner0bb87572008-07-14 05:52:33 +0000354#endif
Chris Lattner2d3e8bb2001-11-03 03:27:29 +0000355
356
Reid Spencer619f0242007-02-04 04:43:17 +0000357// RemapOperand - Use ValueMap to convert constants from one module to another.
Chris Lattner5c2d3352003-01-30 19:53:34 +0000358static Value *RemapOperand(const Value *In,
Chris Lattner0033baf2004-11-16 17:12:38 +0000359 std::map<const Value*, Value*> &ValueMap) {
360 std::map<const Value*,Value*>::const_iterator I = ValueMap.find(In);
Reid Spenceref9b9a72007-02-05 20:47:22 +0000361 if (I != ValueMap.end())
362 return I->second;
Chris Lattner5c377c52001-10-14 23:29:15 +0000363
Reid Spencer619f0242007-02-04 04:43:17 +0000364 // Check to see if it's a constant that we are interested in transforming.
Chris Lattner620fd682006-06-01 19:14:22 +0000365 Value *Result = 0;
Chris Lattner18961502002-06-25 16:12:52 +0000366 if (const Constant *CPV = dyn_cast<Constant>(In)) {
Chris Lattnerde512b52004-02-15 05:55:15 +0000367 if ((!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) ||
Reid Spencera54b7cb2007-01-12 07:05:14 +0000368 isa<ConstantInt>(CPV) || isa<ConstantAggregateZero>(CPV))
Chris Lattner0033baf2004-11-16 17:12:38 +0000369 return const_cast<Constant*>(CPV); // Simple constants stay identical.
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000370
Chris Lattner18961502002-06-25 16:12:52 +0000371 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
Alkis Evlogimenoscc7ba492004-08-04 08:08:13 +0000372 std::vector<Constant*> Operands(CPA->getNumOperands());
373 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
Chris Lattner0033baf2004-11-16 17:12:38 +0000374 Operands[i] =cast<Constant>(RemapOperand(CPA->getOperand(i), ValueMap));
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000375 Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
Chris Lattner18961502002-06-25 16:12:52 +0000376 } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
Alkis Evlogimenoscc7ba492004-08-04 08:08:13 +0000377 std::vector<Constant*> Operands(CPS->getNumOperands());
378 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
Chris Lattner0033baf2004-11-16 17:12:38 +0000379 Operands[i] =cast<Constant>(RemapOperand(CPS->getOperand(i), ValueMap));
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000380 Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
Chris Lattnerb976e662004-10-16 18:08:06 +0000381 } else if (isa<ConstantPointerNull>(CPV) || isa<UndefValue>(CPV)) {
Chris Lattner18961502002-06-25 16:12:52 +0000382 Result = const_cast<Constant*>(CPV);
Reid Spencer9d6565a2007-02-15 02:26:10 +0000383 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CPV)) {
Chris Lattnera88eb922006-01-19 23:15:58 +0000384 std::vector<Constant*> Operands(CP->getNumOperands());
385 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
386 Operands[i] = cast<Constant>(RemapOperand(CP->getOperand(i), ValueMap));
Reid Spencer9d6565a2007-02-15 02:26:10 +0000387 Result = ConstantVector::get(Operands);
Chris Lattner6cdf1972002-07-18 00:13:08 +0000388 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
Chris Lattner27d67212006-07-14 22:21:31 +0000389 std::vector<Constant*> Ops;
390 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
391 Ops.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),ValueMap)));
392 Result = CE->getWithOperands(Ops);
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000393 } else {
Chris Lattner0bb87572008-07-14 05:52:33 +0000394 assert(!isa<GlobalValue>(CPV) && "Unmapped global?");
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000395 assert(0 && "Unknown type of derived type constant value!");
396 }
Chris Lattner620fd682006-06-01 19:14:22 +0000397 } else if (isa<InlineAsm>(In)) {
398 Result = const_cast<Value*>(In);
399 }
400
Reid Spencer619f0242007-02-04 04:43:17 +0000401 // Cache the mapping in our local map structure
Chris Lattner620fd682006-06-01 19:14:22 +0000402 if (Result) {
Anton Korobeynikov817bf2a2008-03-10 22:36:08 +0000403 ValueMap[In] = Result;
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000404 return Result;
405 }
Reid Spencer8bef0372007-02-04 04:29:21 +0000406
Chris Lattner0bb87572008-07-14 05:52:33 +0000407#ifndef NDEBUG
Bill Wendlinge8156192006-12-07 01:30:32 +0000408 cerr << "LinkModules ValueMap: \n";
Chris Lattner0033baf2004-11-16 17:12:38 +0000409 PrintMap(ValueMap);
Chris Lattner2d3e8bb2001-11-03 03:27:29 +0000410
Bill Wendlinge8156192006-12-07 01:30:32 +0000411 cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n";
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000412 assert(0 && "Couldn't remap value!");
Chris Lattner0bb87572008-07-14 05:52:33 +0000413#endif
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000414 return 0;
Chris Lattner5c377c52001-10-14 23:29:15 +0000415}
416
Reid Spencer8bef0372007-02-04 04:29:21 +0000417/// ForceRenaming - The LLVM SymbolTable class autorenames globals that conflict
418/// in the symbol table. This is good for all clients except for us. Go
419/// through the trouble to force this back.
Chris Lattnerc0036282004-08-04 07:05:54 +0000420static void ForceRenaming(GlobalValue *GV, const std::string &Name) {
421 assert(GV->getName() != Name && "Can't force rename to self");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000422 ValueSymbolTable &ST = GV->getParent()->getValueSymbolTable();
Chris Lattnerc0036282004-08-04 07:05:54 +0000423
424 // If there is a conflict, rename the conflict.
Chris Lattner33f29492007-02-11 00:39:38 +0000425 if (GlobalValue *ConflictGV = cast_or_null<GlobalValue>(ST.lookup(Name))) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000426 assert(ConflictGV->hasInternalLinkage() &&
427 "Not conflicting with a static global, should link instead!");
Chris Lattner33f29492007-02-11 00:39:38 +0000428 GV->takeName(ConflictGV);
429 ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
Reid Spenceref9b9a72007-02-05 20:47:22 +0000430 assert(ConflictGV->getName() != Name && "ForceRenaming didn't work");
Chris Lattner33f29492007-02-11 00:39:38 +0000431 } else {
432 GV->setName(Name); // Force the name back
Reid Spenceref9b9a72007-02-05 20:47:22 +0000433 }
Reid Spenceref9b9a72007-02-05 20:47:22 +0000434}
Reid Spencer8bef0372007-02-04 04:29:21 +0000435
Reid Spenceref9b9a72007-02-05 20:47:22 +0000436/// CopyGVAttributes - copy additional attributes (those not needed to construct
437/// a GlobalValue) from the SrcGV to the DestGV.
438static void CopyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) {
Duncan Sands28c3cff2008-05-26 19:58:59 +0000439 // Use the maximum alignment, rather than just copying the alignment of SrcGV.
440 unsigned Alignment = std::max(DestGV->getAlignment(), SrcGV->getAlignment());
441 DestGV->copyAttributesFrom(SrcGV);
442 DestGV->setAlignment(Alignment);
Chris Lattnerc0036282004-08-04 07:05:54 +0000443}
444
Chris Lattneraee38ea2004-12-03 22:18:41 +0000445/// GetLinkageResult - This analyzes the two global values and determines what
446/// the result will look like in the destination module. In particular, it
447/// computes the resultant linkage type, computes whether the global in the
448/// source should be copied over to the destination (replacing the existing
Anton Korobeynikov9cd3ccf2007-04-29 20:56:48 +0000449/// one), and computes whether this linkage is an error or not. It also performs
450/// visibility checks: we cannot link together two symbols with different
451/// visibilities.
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000452static bool GetLinkageResult(GlobalValue *Dest, const GlobalValue *Src,
Chris Lattneraee38ea2004-12-03 22:18:41 +0000453 GlobalValue::LinkageTypes &LT, bool &LinkFromSrc,
454 std::string *Err) {
455 assert((!Dest || !Src->hasInternalLinkage()) &&
456 "If Src has internal linkage, Dest shouldn't be set!");
457 if (!Dest) {
458 // Linking something to nothing.
459 LinkFromSrc = true;
460 LT = Src->getLinkage();
Reid Spencer5cbf9852007-01-30 20:08:39 +0000461 } else if (Src->isDeclaration()) {
Anton Korobeynikov2b48ef02008-03-10 22:33:22 +0000462 // If Src is external or if both Src & Dest are external.. Just link the
Chris Lattneraee38ea2004-12-03 22:18:41 +0000463 // external globals, we aren't adding anything.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000464 if (Src->hasDLLImportLinkage()) {
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000465 // If one of GVs has DLLImport linkage, result should be dllimport'ed.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000466 if (Dest->isDeclaration()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000467 LinkFromSrc = true;
468 LT = Src->getLinkage();
469 }
Andrew Lenharth8753c442006-12-15 17:35:32 +0000470 } else if (Dest->hasExternalWeakLinkage()) {
471 //If the Dest is weak, use the source linkage
472 LinkFromSrc = true;
473 LT = Src->getLinkage();
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000474 } else {
475 LinkFromSrc = false;
476 LT = Dest->getLinkage();
477 }
Reid Spencer5cbf9852007-01-30 20:08:39 +0000478 } else if (Dest->isDeclaration() && !Dest->hasDLLImportLinkage()) {
Chris Lattneraee38ea2004-12-03 22:18:41 +0000479 // If Dest is external but Src is not:
480 LinkFromSrc = true;
481 LT = Src->getLinkage();
482 } else if (Src->hasAppendingLinkage() || Dest->hasAppendingLinkage()) {
483 if (Src->getLinkage() != Dest->getLinkage())
484 return Error(Err, "Linking globals named '" + Src->getName() +
485 "': can only link appending global with another appending global!");
486 LinkFromSrc = true; // Special cased.
487 LT = Src->getLinkage();
Anton Korobeynikov80585f12008-07-05 23:48:30 +0000488 } else if (Src->isWeakForLinker()) {
Dale Johannesenaafce772008-05-14 20:12:51 +0000489 // At this point we know that Dest has LinkOnce, External*, Weak, Common,
490 // or DLL* linkage.
Anton Korobeynikov80585f12008-07-05 23:48:30 +0000491 if ((Dest->hasLinkOnceLinkage() &&
Dale Johannesenaafce772008-05-14 20:12:51 +0000492 (Src->hasWeakLinkage() || Src->hasCommonLinkage())) ||
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000493 Dest->hasExternalWeakLinkage()) {
Chris Lattneraee38ea2004-12-03 22:18:41 +0000494 LinkFromSrc = true;
495 LT = Src->getLinkage();
496 } else {
497 LinkFromSrc = false;
498 LT = Dest->getLinkage();
499 }
Anton Korobeynikov80585f12008-07-05 23:48:30 +0000500 } else if (Dest->isWeakForLinker()) {
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000501 // At this point we know that Src has External* or DLL* linkage.
502 if (Src->hasExternalWeakLinkage()) {
503 LinkFromSrc = false;
504 LT = Dest->getLinkage();
505 } else {
506 LinkFromSrc = true;
507 LT = GlobalValue::ExternalLinkage;
508 }
Chris Lattneraee38ea2004-12-03 22:18:41 +0000509 } else {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000510 assert((Dest->hasExternalLinkage() ||
511 Dest->hasDLLImportLinkage() ||
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000512 Dest->hasDLLExportLinkage() ||
513 Dest->hasExternalWeakLinkage()) &&
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000514 (Src->hasExternalLinkage() ||
515 Src->hasDLLImportLinkage() ||
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000516 Src->hasDLLExportLinkage() ||
517 Src->hasExternalWeakLinkage()) &&
Chris Lattneraee38ea2004-12-03 22:18:41 +0000518 "Unexpected linkage type!");
Misha Brukmanf976c852005-04-21 22:55:34 +0000519 return Error(Err, "Linking globals named '" + Src->getName() +
Chris Lattneraee38ea2004-12-03 22:18:41 +0000520 "': symbol multiply defined!");
521 }
Anton Korobeynikov9cd3ccf2007-04-29 20:56:48 +0000522
523 // Check visibility
524 if (Dest && Src->getVisibility() != Dest->getVisibility())
Chris Lattner97f8b092007-08-19 22:22:54 +0000525 if (!Src->isDeclaration() && !Dest->isDeclaration())
526 return Error(Err, "Linking globals named '" + Src->getName() +
527 "': symbols have different visibilities!");
Chris Lattneraee38ea2004-12-03 22:18:41 +0000528 return false;
529}
Chris Lattner5c377c52001-10-14 23:29:15 +0000530
531// LinkGlobals - Loop through the global variables in the src module and merge
Chris Lattner8166e6e2003-05-13 21:33:43 +0000532// them into the dest module.
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000533static bool LinkGlobals(Module *Dest, const Module *Src,
Chris Lattner5c2d3352003-01-30 19:53:34 +0000534 std::map<const Value*, Value*> &ValueMap,
Chris Lattner8166e6e2003-05-13 21:33:43 +0000535 std::multimap<std::string, GlobalVariable *> &AppendingVars,
Chris Lattner5c2d3352003-01-30 19:53:34 +0000536 std::string *Err) {
Chris Lattner5c377c52001-10-14 23:29:15 +0000537 // Loop over all of the globals in the src module, mapping them over as we go
Chris Lattner0bb87572008-07-14 05:52:33 +0000538 for (Module::const_global_iterator I = Src->global_begin(),
539 E = Src->global_end(); I != E; ++I) {
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000540 const GlobalVariable *SGV = I;
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000541 GlobalValue *DGV = 0;
542
543 // Check to see if may have to link the global with the global
Reid Spenceref9b9a72007-02-05 20:47:22 +0000544 if (SGV->hasName() && !SGV->hasInternalLinkage()) {
545 DGV = Dest->getGlobalVariable(SGV->getName());
546 if (DGV && DGV->getType() != SGV->getType())
547 // If types don't agree due to opaque types, try to resolve them.
Chris Lattnerbc1c82a2008-06-16 18:19:05 +0000548 RecursiveResolveTypes(SGV->getType(), DGV->getType());
Reid Spenceref9b9a72007-02-05 20:47:22 +0000549 }
Chris Lattner5c377c52001-10-14 23:29:15 +0000550
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000551 // Check to see if may have to link the global with the alias
Anton Korobeynikovaeb09962008-03-10 22:35:31 +0000552 if (!DGV && SGV->hasName() && !SGV->hasInternalLinkage()) {
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000553 DGV = Dest->getNamedAlias(SGV->getName());
554 if (DGV && DGV->getType() != SGV->getType())
555 // If types don't agree due to opaque types, try to resolve them.
Chris Lattnerbc1c82a2008-06-16 18:19:05 +0000556 RecursiveResolveTypes(SGV->getType(), DGV->getType());
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000557 }
558
Chris Lattneraee38ea2004-12-03 22:18:41 +0000559 if (DGV && DGV->hasInternalLinkage())
560 DGV = 0;
561
Dan Gohmanc3183292007-10-08 15:13:30 +0000562 assert((SGV->hasInitializer() || SGV->hasExternalWeakLinkage() ||
563 SGV->hasExternalLinkage() || SGV->hasDLLImportLinkage()) &&
Chris Lattner4ad02e72003-04-16 20:28:45 +0000564 "Global must either be external or have an initializer!");
565
Chris Lattnerb324bd72006-11-09 05:18:12 +0000566 GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
567 bool LinkFromSrc = false;
Chris Lattneraee38ea2004-12-03 22:18:41 +0000568 if (GetLinkageResult(DGV, SGV, NewLinkage, LinkFromSrc, Err))
569 return true;
Chris Lattner0fec08e2003-04-21 21:07:05 +0000570
Chris Lattneraee38ea2004-12-03 22:18:41 +0000571 if (!DGV) {
Chris Lattner4ad02e72003-04-16 20:28:45 +0000572 // No linking to be performed, simply create an identical version of the
573 // symbol over in the dest module... the initializer will be filled in
574 // later by LinkGlobalInits...
Chris Lattner2719bac2003-04-21 21:15:04 +0000575 GlobalVariable *NewDGV =
576 new GlobalVariable(SGV->getType()->getElementType(),
577 SGV->isConstant(), SGV->getLinkage(), /*init*/0,
Chris Lattnera534b0f2008-06-27 03:10:24 +0000578 SGV->getName(), Dest, false,
579 SGV->getType()->getAddressSpace());
Reid Spencer471feac2007-02-04 04:30:33 +0000580 // Propagate alignment, visibility and section info.
Reid Spenceref9b9a72007-02-05 20:47:22 +0000581 CopyGVAttributes(NewDGV, SGV);
Andrew Lenharth5dfbaf12007-02-01 17:12:54 +0000582
Chris Lattner2719bac2003-04-21 21:15:04 +0000583 // If the LLVM runtime renamed the global, but it is an externally visible
584 // symbol, DGV must be an existing global with internal linkage. Rename
585 // it.
Chris Lattnerc0036282004-08-04 07:05:54 +0000586 if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage())
587 ForceRenaming(NewDGV, SGV->getName());
Chris Lattner4ad02e72003-04-16 20:28:45 +0000588
589 // Make sure to remember this mapping...
Anton Korobeynikov817bf2a2008-03-10 22:36:08 +0000590 ValueMap[SGV] = NewDGV;
591
Chris Lattner8166e6e2003-05-13 21:33:43 +0000592 if (SGV->hasAppendingLinkage())
593 // Keep track that this is an appending variable...
594 AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
Chris Lattneraee38ea2004-12-03 22:18:41 +0000595 } else if (DGV->hasAppendingLinkage()) {
Chris Lattner8166e6e2003-05-13 21:33:43 +0000596 // No linking is performed yet. Just insert a new copy of the global, and
597 // keep track of the fact that it is an appending variable in the
598 // AppendingVars map. The name is cleared out so that no linkage is
599 // performed.
600 GlobalVariable *NewDGV =
601 new GlobalVariable(SGV->getType()->getElementType(),
602 SGV->isConstant(), SGV->getLinkage(), /*init*/0,
Chris Lattnera534b0f2008-06-27 03:10:24 +0000603 "", Dest, false,
604 SGV->getType()->getAddressSpace());
Chris Lattner8166e6e2003-05-13 21:33:43 +0000605
Anton Korobeynikov75c79152008-03-07 18:34:50 +0000606 // Set alignment allowing CopyGVAttributes merge it with alignment of SGV.
Reid Spenceref9b9a72007-02-05 20:47:22 +0000607 NewDGV->setAlignment(DGV->getAlignment());
Anton Korobeynikov75c79152008-03-07 18:34:50 +0000608 // Propagate alignment, section and visibility info.
Reid Spenceref9b9a72007-02-05 20:47:22 +0000609 CopyGVAttributes(NewDGV, SGV);
Andrew Lenharth5dfbaf12007-02-01 17:12:54 +0000610
Chris Lattner8166e6e2003-05-13 21:33:43 +0000611 // Make sure to remember this mapping...
Anton Korobeynikov817bf2a2008-03-10 22:36:08 +0000612 ValueMap[SGV] = NewDGV;
Chris Lattner8166e6e2003-05-13 21:33:43 +0000613
614 // Keep track that this is an appending variable...
615 AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000616 } else if (GlobalAlias *DGA = dyn_cast<GlobalAlias>(DGV)) {
617 // SGV is global, but DGV is alias. The only valid mapping is when SGV is
618 // external declaration, which is effectively a no-op. Also make sure
Anton Korobeynikov817bf2a2008-03-10 22:36:08 +0000619 // linkage calculation was correct.
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000620 if (SGV->isDeclaration() && !LinkFromSrc) {
621 // Make sure to remember this mapping...
Anton Korobeynikov817bf2a2008-03-10 22:36:08 +0000622 ValueMap[SGV] = DGA;
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000623 } else
Anton Korobeynikov1438b9d2008-03-10 22:34:46 +0000624 return Error(Err, "Global-Alias Collision on '" + SGV->getName() +
625 "': symbol multiple defined");
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000626 } else if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV)) {
Anton Korobeynikov817bf2a2008-03-10 22:36:08 +0000627 // Otherwise, perform the global-global mapping as instructed by
628 // GetLinkageResult.
Chris Lattneraee38ea2004-12-03 22:18:41 +0000629 if (LinkFromSrc) {
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000630 // Propagate alignment, section, and visibility info.
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000631 CopyGVAttributes(DGVar, SGV);
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000632
633 // If the types don't match, and if we are to link from the source, nuke
634 // DGV and create a new one of the appropriate type.
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000635 if (SGV->getType() != DGVar->getType()) {
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000636 GlobalVariable *NewDGV =
637 new GlobalVariable(SGV->getType()->getElementType(),
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000638 DGVar->isConstant(), DGVar->getLinkage(),
Chris Lattnera534b0f2008-06-27 03:10:24 +0000639 /*init*/0, DGVar->getName(), Dest, false,
640 SGV->getType()->getAddressSpace());
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000641 CopyGVAttributes(NewDGV, DGVar);
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000642 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV,
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000643 DGVar->getType()));
644 // DGVar will conflict with NewDGV because they both had the same
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000645 // name. We must erase this now so ForceRenaming doesn't assert
646 // because DGV might not have internal linkage.
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000647 DGVar->eraseFromParent();
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000648
649 // If the symbol table renamed the global, but it is an externally
650 // visible symbol, DGV must be an existing global with internal
651 // linkage. Rename it.
652 if (NewDGV->getName() != SGV->getName() &&
653 !NewDGV->hasInternalLinkage())
654 ForceRenaming(NewDGV, SGV->getName());
655
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000656 DGVar = NewDGV;
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000657 }
658
Chris Lattneraee38ea2004-12-03 22:18:41 +0000659 // Inherit const as appropriate
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000660 DGVar->setConstant(SGV->isConstant());
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000661
662 // Set initializer to zero, so we can link the stuff later
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000663 DGVar->setInitializer(0);
Chris Lattneraee38ea2004-12-03 22:18:41 +0000664 } else {
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000665 // Special case for const propagation
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000666 if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant())
667 DGVar->setConstant(true);
Chris Lattneraee38ea2004-12-03 22:18:41 +0000668 }
669
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000670 // Set calculated linkage
Anton Korobeynikov01f69392008-03-10 22:34:28 +0000671 DGVar->setLinkage(NewLinkage);
Anton Korobeynikov968e39a2008-03-10 22:33:53 +0000672
673 // Make sure to remember this mapping...
Anton Korobeynikov817bf2a2008-03-10 22:36:08 +0000674 ValueMap[SGV] = ConstantExpr::getBitCast(DGVar, SGV->getType());
Chris Lattner5c377c52001-10-14 23:29:15 +0000675 }
676 }
677 return false;
678}
679
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000680static GlobalValue::LinkageTypes
681CalculateAliasLinkage(const GlobalValue *SGV, const GlobalValue *DGV) {
682 if (SGV->hasExternalLinkage() || DGV->hasExternalLinkage())
683 return GlobalValue::ExternalLinkage;
684 else if (SGV->hasWeakLinkage() || DGV->hasWeakLinkage())
685 return GlobalValue::WeakLinkage;
686 else {
687 assert(SGV->hasInternalLinkage() && DGV->hasInternalLinkage() &&
688 "Unexpected linkage type");
689 return GlobalValue::InternalLinkage;
690 }
691}
692
Lauro Ramos Venancio31ed0fb2007-06-28 19:02:54 +0000693// LinkAlias - Loop through the alias in the src module and link them into the
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000694// dest module. We're assuming, that all functions/global variables were already
695// linked in.
Anton Korobeynikov4fb28732008-03-05 15:27:21 +0000696static bool LinkAlias(Module *Dest, const Module *Src,
697 std::map<const Value*, Value*> &ValueMap,
698 std::string *Err) {
Lauro Ramos Venancio31ed0fb2007-06-28 19:02:54 +0000699 // Loop over all alias in the src module
700 for (Module::const_alias_iterator I = Src->alias_begin(),
701 E = Src->alias_end(); I != E; ++I) {
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000702 const GlobalAlias *SGA = I;
703 const GlobalValue *SAliasee = SGA->getAliasedGlobal();
704 GlobalAlias *NewGA = NULL;
Lauro Ramos Venancio31ed0fb2007-06-28 19:02:54 +0000705
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000706 // Globals were already linked, thus we can just query ValueMap for variant
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000707 // of SAliasee in Dest.
Ted Kremenek58d5e052008-03-09 18:32:50 +0000708 std::map<const Value*,Value*>::const_iterator VMI = ValueMap.find(SAliasee);
709 assert(VMI != ValueMap.end() && "Aliasee not linked");
710 GlobalValue* DAliasee = cast<GlobalValue>(VMI->second);
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000711 GlobalValue* DGV = NULL;
Lauro Ramos Venancio31ed0fb2007-06-28 19:02:54 +0000712
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000713 // Try to find something 'similar' to SGA in destination module.
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000714 if (!DGV && !SGA->hasInternalLinkage()) {
715 DGV = Dest->getNamedAlias(SGA->getName());
Anton Korobeynikov4fb28732008-03-05 15:27:21 +0000716
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000717 // If types don't agree due to opaque types, try to resolve them.
718 if (DGV && DGV->getType() != SGA->getType())
Chris Lattner5ed2ba22008-07-10 01:09:33 +0000719 RecursiveResolveTypes(SGA->getType(), DGV->getType());
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000720 }
721
722 if (!DGV && !SGA->hasInternalLinkage()) {
723 DGV = Dest->getGlobalVariable(SGA->getName());
724
725 // If types don't agree due to opaque types, try to resolve them.
726 if (DGV && DGV->getType() != SGA->getType())
Chris Lattner5ed2ba22008-07-10 01:09:33 +0000727 RecursiveResolveTypes(SGA->getType(), DGV->getType());
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000728 }
729
730 if (!DGV && !SGA->hasInternalLinkage()) {
731 DGV = Dest->getFunction(SGA->getName());
732
733 // If types don't agree due to opaque types, try to resolve them.
734 if (DGV && DGV->getType() != SGA->getType())
Chris Lattner5ed2ba22008-07-10 01:09:33 +0000735 RecursiveResolveTypes(SGA->getType(), DGV->getType());
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000736 }
737
738 // No linking to be performed on internal stuff.
739 if (DGV && DGV->hasInternalLinkage())
740 DGV = NULL;
741
742 if (GlobalAlias *DGA = dyn_cast_or_null<GlobalAlias>(DGV)) {
743 // Types are known to be the same, check whether aliasees equal. As
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000744 // globals are already linked we just need query ValueMap to find the
745 // mapping.
746 if (DAliasee == DGA->getAliasedGlobal()) {
747 // This is just two copies of the same alias. Propagate linkage, if
748 // necessary.
749 DGA->setLinkage(CalculateAliasLinkage(SGA, DGA));
750
751 NewGA = DGA;
752 // Proceed to 'common' steps
753 } else
Anton Korobeynikov1438b9d2008-03-10 22:34:46 +0000754 return Error(Err, "Alias Collision on '" + SGA->getName()+
755 "': aliases have different aliasees");
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000756 } else if (GlobalVariable *DGVar = dyn_cast_or_null<GlobalVariable>(DGV)) {
Anton Korobeynikovf88bc652008-07-05 23:33:22 +0000757 // The only allowed way is to link alias with external declaration or weak
758 // symbol..
Anton Korobeynikov80585f12008-07-05 23:48:30 +0000759 if (DGVar->isDeclaration() || DGVar->isWeakForLinker()) {
Anton Korobeynikoved61c0b2008-03-10 22:36:53 +0000760 // But only if aliasee is global too...
761 if (!isa<GlobalVariable>(DAliasee))
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000762 return Error(Err, "Global-Alias Collision on '" + SGA->getName() +
763 "': aliasee is not global variable");
Anton Korobeynikoved61c0b2008-03-10 22:36:53 +0000764
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000765 NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(),
766 SGA->getName(), DAliasee, Dest);
767 CopyGVAttributes(NewGA, SGA);
768
769 // Any uses of DGV need to change to NewGA, with cast, if needed.
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000770 if (SGA->getType() != DGVar->getType())
771 DGVar->replaceAllUsesWith(ConstantExpr::getBitCast(NewGA,
772 DGVar->getType()));
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000773 else
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000774 DGVar->replaceAllUsesWith(NewGA);
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000775
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000776 // DGVar will conflict with NewGA because they both had the same
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000777 // name. We must erase this now so ForceRenaming doesn't assert
778 // because DGV might not have internal linkage.
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000779 DGVar->eraseFromParent();
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000780
781 // Proceed to 'common' steps
782 } else
Anton Korobeynikov1438b9d2008-03-10 22:34:46 +0000783 return Error(Err, "Global-Alias Collision on '" + SGA->getName() +
784 "': symbol multiple defined");
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000785 } else if (Function *DF = dyn_cast_or_null<Function>(DGV)) {
Anton Korobeynikovf88bc652008-07-05 23:33:22 +0000786 // The only allowed way is to link alias with external declaration or weak
787 // symbol...
Anton Korobeynikov80585f12008-07-05 23:48:30 +0000788 if (DF->isDeclaration() || DF->isWeakForLinker()) {
Anton Korobeynikoved61c0b2008-03-10 22:36:53 +0000789 // But only if aliasee is function too...
790 if (!isa<Function>(DAliasee))
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000791 return Error(Err, "Function-Alias Collision on '" + SGA->getName() +
792 "': aliasee is not function");
Anton Korobeynikoved61c0b2008-03-10 22:36:53 +0000793
Anton Korobeynikovb5a4bd82008-03-05 23:08:16 +0000794 NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(),
795 SGA->getName(), DAliasee, Dest);
796 CopyGVAttributes(NewGA, SGA);
797
798 // Any uses of DF need to change to NewGA, with cast, if needed.
799 if (SGA->getType() != DF->getType())
800 DF->replaceAllUsesWith(ConstantExpr::getBitCast(NewGA,
801 DF->getType()));
802 else
803 DF->replaceAllUsesWith(NewGA);
804
805 // DF will conflict with NewGA because they both had the same
806 // name. We must erase this now so ForceRenaming doesn't assert
807 // because DF might not have internal linkage.
808 DF->eraseFromParent();
809
810 // Proceed to 'common' steps
811 } else
Anton Korobeynikov1438b9d2008-03-10 22:34:46 +0000812 return Error(Err, "Function-Alias Collision on '" + SGA->getName() +
813 "': symbol multiple defined");
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000814 } else {
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000815 // No linking to be performed, simply create an identical version of the
816 // alias over in the dest module...
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000817
818 NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(),
819 SGA->getName(), DAliasee, Dest);
820 CopyGVAttributes(NewGA, SGA);
821
822 // Proceed to 'common' steps
823 }
824
825 assert(NewGA && "No alias was created in destination module!");
826
Anton Korobeynikovb8cdaf72008-03-10 22:36:35 +0000827 // If the symbol table renamed the alias, but it is an externally visible
Anton Korobeynikovcaa8ae82008-05-10 14:41:43 +0000828 // symbol, DGA must be an global value with internal linkage. Rename it.
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000829 if (NewGA->getName() != SGA->getName() &&
830 !NewGA->hasInternalLinkage())
831 ForceRenaming(NewGA, SGA->getName());
832
833 // Remember this mapping so uses in the source module get remapped
834 // later by RemapOperand.
Anton Korobeynikov817bf2a2008-03-10 22:36:08 +0000835 ValueMap[SGA] = NewGA;
Lauro Ramos Venancio31ed0fb2007-06-28 19:02:54 +0000836 }
Anton Korobeynikov58887bc2008-03-05 22:22:46 +0000837
Lauro Ramos Venancio31ed0fb2007-06-28 19:02:54 +0000838 return false;
839}
840
Chris Lattner5c377c52001-10-14 23:29:15 +0000841
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000842// LinkGlobalInits - Update the initializers in the Dest module now that all
843// globals that may be referenced are in Dest.
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000844static bool LinkGlobalInits(Module *Dest, const Module *Src,
Chris Lattner5c2d3352003-01-30 19:53:34 +0000845 std::map<const Value*, Value*> &ValueMap,
846 std::string *Err) {
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000847
848 // Loop over all of the globals in the src module, mapping them over as we go
Chris Lattner11273152006-06-16 01:24:04 +0000849 for (Module::const_global_iterator I = Src->global_begin(),
850 E = Src->global_end(); I != E; ++I) {
Chris Lattner18961502002-06-25 16:12:52 +0000851 const GlobalVariable *SGV = I;
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000852
853 if (SGV->hasInitializer()) { // Only process initialized GV's
854 // Figure out what the initializer looks like in the dest module...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000855 Constant *SInit =
Chris Lattner0033baf2004-11-16 17:12:38 +0000856 cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap));
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000857
Anton Korobeynikov0b12ecf2008-05-07 22:54:15 +0000858 GlobalVariable *DGV =
859 cast<GlobalVariable>(ValueMap[SGV]->stripPointerCasts());
Chris Lattner4ad02e72003-04-16 20:28:45 +0000860 if (DGV->hasInitializer()) {
Chris Lattner4ad02e72003-04-16 20:28:45 +0000861 if (SGV->hasExternalLinkage()) {
862 if (DGV->getInitializer() != SInit)
Anton Korobeynikov1438b9d2008-03-10 22:34:46 +0000863 return Error(Err, "Global Variable Collision on '" + SGV->getName() +
864 "': global variables have different initializers");
Anton Korobeynikov80585f12008-07-05 23:48:30 +0000865 } else if (DGV->isWeakForLinker()) {
Chris Lattner4ad02e72003-04-16 20:28:45 +0000866 // Nothing is required, mapped values will take the new global
867 // automatically.
Anton Korobeynikov80585f12008-07-05 23:48:30 +0000868 } else if (SGV->isWeakForLinker()) {
Chris Lattner57cb9882004-02-17 21:56:04 +0000869 // Nothing is required, mapped values will take the new global
870 // automatically.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000871 } else if (DGV->hasAppendingLinkage()) {
872 assert(0 && "Appending linkage unimplemented!");
873 } else {
874 assert(0 && "Unknown linkage!");
875 }
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000876 } else {
877 // Copy the initializer over now...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000878 DGV->setInitializer(SInit);
Chris Lattner8d2de8a2001-10-15 03:12:52 +0000879 }
880 }
881 }
882 return false;
883}
Chris Lattner5c377c52001-10-14 23:29:15 +0000884
Chris Lattner79df7c02002-03-26 18:01:55 +0000885// LinkFunctionProtos - Link the functions together between the two modules,
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +0000886// without doing function bodies... this just adds external function prototypes
887// to the Dest function...
Chris Lattner5c377c52001-10-14 23:29:15 +0000888//
Chris Lattner79df7c02002-03-26 18:01:55 +0000889static bool LinkFunctionProtos(Module *Dest, const Module *Src,
Chris Lattner5c2d3352003-01-30 19:53:34 +0000890 std::map<const Value*, Value*> &ValueMap,
891 std::string *Err) {
Reid Spencer619f0242007-02-04 04:43:17 +0000892 // Loop over all of the functions in the src module, mapping them over
Chris Lattner5c377c52001-10-14 23:29:15 +0000893 for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
Chris Lattner18961502002-06-25 16:12:52 +0000894 const Function *SF = I; // SrcFunction
Chris Lattner82468492008-06-09 07:36:11 +0000895
Anton Korobeynikov194c2ce2008-07-05 23:03:21 +0000896 GlobalValue *DGV = 0;
Chris Lattnerec91ccb2008-06-20 05:29:39 +0000897 Value *MappedDF;
Chris Lattner82468492008-06-09 07:36:11 +0000898
899 // If this function is internal or has no name, it doesn't participate in
900 // linkage.
Reid Spencer8bef0372007-02-04 04:29:21 +0000901 if (SF->hasName() && !SF->hasInternalLinkage()) {
902 // Check to see if may have to link the function.
Anton Korobeynikov194c2ce2008-07-05 23:03:21 +0000903 DGV = Dest->getFunction(SF->getName());
Reid Spencer8bef0372007-02-04 04:29:21 +0000904 }
Anton Korobeynikov194c2ce2008-07-05 23:03:21 +0000905
906 // Check to see if may have to link the function with the alias
907 if (!DGV && SF->hasName() && !SF->hasInternalLinkage()) {
908 DGV = Dest->getNamedAlias(SF->getName());
909 if (DGV && DGV->getType() != SF->getType())
910 // If types don't agree due to opaque types, try to resolve them.
911 RecursiveResolveTypes(SF->getType(), DGV->getType());
912 }
913
914 if (DGV && DGV->hasInternalLinkage())
915 DGV = 0;
916
Chris Lattner82468492008-06-09 07:36:11 +0000917 // If there is no linkage to be performed, just bring over SF without
918 // modifying it.
Anton Korobeynikov194c2ce2008-07-05 23:03:21 +0000919 if (DGV == 0) {
Chris Lattner82468492008-06-09 07:36:11 +0000920 // Function does not already exist, simply insert an function signature
921 // identical to SF into the dest module.
922 Function *NewDF = Function::Create(SF->getFunctionType(),
923 SF->getLinkage(),
924 SF->getName(), Dest);
925 CopyGVAttributes(NewDF, SF);
926
927 // If the LLVM runtime renamed the function, but it is an externally
928 // visible symbol, DF must be an existing function with internal linkage.
929 // Rename it.
930 if (!NewDF->hasInternalLinkage() && NewDF->getName() != SF->getName())
931 ForceRenaming(NewDF, SF->getName());
932
933 // ... and remember this mapping...
934 ValueMap[SF] = NewDF;
935 continue;
Anton Korobeynikov194c2ce2008-07-05 23:03:21 +0000936 } else if (GlobalAlias *DGA = dyn_cast<GlobalAlias>(DGV)) {
Anton Korobeynikovf88bc652008-07-05 23:33:22 +0000937 // SF is function, but DF is alias.
938 // The only valid mappings are:
939 // - SF is external declaration, which is effectively a no-op.
940 // - SF is weak, when we just need to throw SF out.
Anton Korobeynikov80585f12008-07-05 23:48:30 +0000941 if (!SF->isDeclaration() && !SF->isWeakForLinker())
Anton Korobeynikov194c2ce2008-07-05 23:03:21 +0000942 return Error(Err, "Function-Alias Collision on '" + SF->getName() +
943 "': symbol multiple defined");
944
945 // Make sure to remember this mapping...
Anton Korobeynikovf88bc652008-07-05 23:33:22 +0000946 ValueMap[SF] = ConstantExpr::getBitCast(DGA, SF->getType());
Anton Korobeynikov194c2ce2008-07-05 23:03:21 +0000947 continue;
Chris Lattner82468492008-06-09 07:36:11 +0000948 }
Anton Korobeynikov194c2ce2008-07-05 23:03:21 +0000949
950 Function* DF = cast<Function>(DGV);
Chris Lattner82468492008-06-09 07:36:11 +0000951 // If types don't agree because of opaque, try to resolve them.
952 if (SF->getType() != DF->getType())
Chris Lattnerbc1c82a2008-06-16 18:19:05 +0000953 RecursiveResolveTypes(SF->getType(), DF->getType());
Chris Lattner82468492008-06-09 07:36:11 +0000954
955 // Check visibility, merging if a definition overrides a prototype.
956 if (SF->getVisibility() != DF->getVisibility()) {
Chris Lattner97f8b092007-08-19 22:22:54 +0000957 // If one is a prototype, ignore its visibility. Prototypes are always
958 // overridden by the definition.
959 if (!SF->isDeclaration() && !DF->isDeclaration())
960 return Error(Err, "Linking functions named '" + SF->getName() +
961 "': symbols have different visibilities!");
Chris Lattnerbc3d1c72008-06-09 07:25:28 +0000962
963 // Otherwise, replace the visibility of DF if DF is a prototype.
964 if (DF->isDeclaration())
965 DF->setVisibility(SF->getVisibility());
Chris Lattner97f8b092007-08-19 22:22:54 +0000966 }
Reid Spenceref9b9a72007-02-05 20:47:22 +0000967
Chris Lattner82468492008-06-09 07:36:11 +0000968 if (DF->getType() != SF->getType()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000969 if (DF->isDeclaration() && !SF->isDeclaration()) {
970 // We have a definition of the same name but different type in the
971 // source module. Copy the prototype to the destination and replace
972 // uses of the destination's prototype with the new prototype.
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000973 Function *NewDF = Function::Create(SF->getFunctionType(),
974 SF->getLinkage(),
Gabor Greif051a9502008-04-06 20:25:17 +0000975 SF->getName(), Dest);
Reid Spenceref9b9a72007-02-05 20:47:22 +0000976 CopyGVAttributes(NewDF, SF);
Chris Lattner5c377c52001-10-14 23:29:15 +0000977
Reid Spenceref9b9a72007-02-05 20:47:22 +0000978 // Any uses of DF need to change to NewDF, with cast
979 DF->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DF->getType()));
980
981 // DF will conflict with NewDF because they both had the same. We must
982 // erase this now so ForceRenaming doesn't assert because DF might
983 // not have internal linkage.
984 DF->eraseFromParent();
985
986 // If the symbol table renamed the function, but it is an externally
987 // visible symbol, DF must be an existing function with internal
988 // linkage. Rename it.
989 if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage())
990 ForceRenaming(NewDF, SF->getName());
991
992 // Remember this mapping so uses in the source module get remapped
993 // later by RemapOperand.
994 ValueMap[SF] = NewDF;
Chris Lattnerec91ccb2008-06-20 05:29:39 +0000995 continue;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000996 } else {
Chris Lattnerec91ccb2008-06-20 05:29:39 +0000997 // We have two functions of the same name but different type. Any use
998 // of the source must be mapped to the destination, with a cast.
999 MappedDF = ConstantExpr::getBitCast(DF, SF->getType());
Reid Spenceref9b9a72007-02-05 20:47:22 +00001000 }
Chris Lattnerec91ccb2008-06-20 05:29:39 +00001001 } else {
1002 MappedDF = DF;
Chris Lattner82468492008-06-09 07:36:11 +00001003 }
1004
1005 if (SF->isDeclaration()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +00001006 // If SF is a declaration or if both SF & DF are declarations, just link
1007 // the declarations, we aren't adding anything.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001008 if (SF->hasDLLImportLinkage()) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001009 if (DF->isDeclaration()) {
Chris Lattnerec91ccb2008-06-20 05:29:39 +00001010 ValueMap[SF] = MappedDF;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001011 DF->setLinkage(SF->getLinkage());
Chris Lattner822143e2008-06-09 07:47:34 +00001012 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001013 } else {
Chris Lattnerec91ccb2008-06-20 05:29:39 +00001014 ValueMap[SF] = MappedDF;
Chris Lattner822143e2008-06-09 07:47:34 +00001015 }
1016 continue;
1017 }
1018
1019 // If DF is external but SF is not, link the external functions, update
1020 // linkage qualifiers.
1021 if (DF->isDeclaration() && !DF->hasDLLImportLinkage()) {
Chris Lattnerec91ccb2008-06-20 05:29:39 +00001022 ValueMap.insert(std::make_pair(SF, MappedDF));
Chris Lattnerc2b97d42003-04-23 18:38:39 +00001023 DF->setLinkage(SF->getLinkage());
Chris Lattner822143e2008-06-09 07:47:34 +00001024 continue;
1025 }
1026
1027 // At this point we know that DF has LinkOnce, Weak, or External* linkage.
Anton Korobeynikov80585f12008-07-05 23:48:30 +00001028 if (SF->isWeakForLinker()) {
Chris Lattnerec91ccb2008-06-20 05:29:39 +00001029 ValueMap[SF] = MappedDF;
Chris Lattner72ac148d2003-10-16 18:29:00 +00001030
Chris Lattner35956552003-10-27 16:39:39 +00001031 // Linkonce+Weak = Weak
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +00001032 // *+External Weak = *
Anton Korobeynikov80585f12008-07-05 23:48:30 +00001033 if ((DF->hasLinkOnceLinkage() &&
Dale Johannesenaafce772008-05-14 20:12:51 +00001034 (SF->hasWeakLinkage() || SF->hasCommonLinkage())) ||
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +00001035 DF->hasExternalWeakLinkage())
Chris Lattner35956552003-10-27 16:39:39 +00001036 DF->setLinkage(SF->getLinkage());
Chris Lattner822143e2008-06-09 07:47:34 +00001037 continue;
1038 }
1039
Anton Korobeynikov80585f12008-07-05 23:48:30 +00001040 if (DF->isWeakForLinker()) {
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +00001041 // At this point we know that SF has LinkOnce or External* linkage.
Chris Lattnerec91ccb2008-06-20 05:29:39 +00001042 ValueMap[SF] = MappedDF;
Chris Lattner822143e2008-06-09 07:47:34 +00001043
1044 // If the source function has stronger linkage than the destination,
1045 // its body and linkage should override ours.
1046 if (!SF->hasLinkOnceLinkage() && !SF->hasExternalWeakLinkage()) {
1047 // Don't inherit linkonce & external weak linkage.
Chris Lattner72ac148d2003-10-16 18:29:00 +00001048 DF->setLinkage(SF->getLinkage());
Chris Lattner822143e2008-06-09 07:47:34 +00001049 DF->deleteBody();
1050 }
1051 continue;
1052 }
1053
1054 if (SF->getLinkage() != DF->getLinkage())
1055 return Error(Err, "Functions named '" + SF->getName() +
1056 "' have different linkage specifiers!");
1057
1058 // The function is defined identically in both modules!
1059 if (SF->hasExternalLinkage())
Misha Brukmanf976c852005-04-21 22:55:34 +00001060 return Error(Err, "Function '" +
1061 ToStr(SF->getFunctionType(), Src) + "':\"" +
Chris Lattnerc2b97d42003-04-23 18:38:39 +00001062 SF->getName() + "\" - Function is already defined!");
Chris Lattner822143e2008-06-09 07:47:34 +00001063 assert(0 && "Unknown linkage configuration found!");
Chris Lattner5c377c52001-10-14 23:29:15 +00001064 }
1065 return false;
1066}
1067
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +00001068// LinkFunctionBody - Copy the source function over into the dest function and
1069// fix up references to values. At this point we know that Dest is an external
1070// function, and that Src is not.
Chris Lattner4bbfbff2004-11-16 07:31:51 +00001071static bool LinkFunctionBody(Function *Dest, Function *Src,
Reid Spenceref9b9a72007-02-05 20:47:22 +00001072 std::map<const Value*, Value*> &ValueMap,
Chris Lattner5c2d3352003-01-30 19:53:34 +00001073 std::string *Err) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001074 assert(Src && Dest && Dest->isDeclaration() && !Src->isDeclaration());
Chris Lattner5c377c52001-10-14 23:29:15 +00001075
Chris Lattner0033baf2004-11-16 17:12:38 +00001076 // Go through and convert function arguments over, remembering the mapping.
Chris Lattnere4d5c442005-03-15 04:54:21 +00001077 Function::arg_iterator DI = Dest->arg_begin();
1078 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001079 I != E; ++I, ++DI) {
Owen Anderson6bc41e82008-04-14 17:38:21 +00001080 DI->setName(I->getName()); // Copy the name information over...
Chris Lattner5c377c52001-10-14 23:29:15 +00001081
1082 // Add a mapping to our local map
Anton Korobeynikov817bf2a2008-03-10 22:36:08 +00001083 ValueMap[I] = DI;
Chris Lattner5c377c52001-10-14 23:29:15 +00001084 }
1085
Chris Lattner4bbfbff2004-11-16 07:31:51 +00001086 // Splice the body of the source function into the dest function.
1087 Dest->getBasicBlockList().splice(Dest->end(), Src->getBasicBlockList());
Chris Lattner5c377c52001-10-14 23:29:15 +00001088
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +00001089 // At this point, all of the instructions and values of the function are now
1090 // copied over. The only problem is that they are still referencing values in
1091 // the Source function as operands. Loop through all of the operands of the
1092 // functions and patch them up to point to the local versions...
Chris Lattner5c377c52001-10-14 23:29:15 +00001093 //
Chris Lattner18961502002-06-25 16:12:52 +00001094 for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
1095 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1096 for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
Chris Lattner221d6882002-02-12 21:07:25 +00001097 OI != OE; ++OI)
Chris Lattner4bbfbff2004-11-16 07:31:51 +00001098 if (!isa<Instruction>(*OI) && !isa<BasicBlock>(*OI))
Reid Spenceref9b9a72007-02-05 20:47:22 +00001099 *OI = RemapOperand(*OI, ValueMap);
Chris Lattner0033baf2004-11-16 17:12:38 +00001100
1101 // There is no need to map the arguments anymore.
Chris Lattner11273152006-06-16 01:24:04 +00001102 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
1103 I != E; ++I)
Reid Spenceref9b9a72007-02-05 20:47:22 +00001104 ValueMap.erase(I);
Chris Lattner5c377c52001-10-14 23:29:15 +00001105
1106 return false;
1107}
1108
1109
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +00001110// LinkFunctionBodies - Link in the function bodies that are defined in the
1111// source module into the DestModule. This consists basically of copying the
1112// function over and fixing up references to values.
Chris Lattner4bbfbff2004-11-16 07:31:51 +00001113static bool LinkFunctionBodies(Module *Dest, Module *Src,
Chris Lattner5c2d3352003-01-30 19:53:34 +00001114 std::map<const Value*, Value*> &ValueMap,
1115 std::string *Err) {
Chris Lattner5c377c52001-10-14 23:29:15 +00001116
Reid Spencer8bef0372007-02-04 04:29:21 +00001117 // Loop over all of the functions in the src module, mapping them over as we
1118 // go
Chris Lattner4bbfbff2004-11-16 07:31:51 +00001119 for (Module::iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF) {
Reid Spencer619f0242007-02-04 04:43:17 +00001120 if (!SF->isDeclaration()) { // No body if function is external
Chris Lattnerec91ccb2008-06-20 05:29:39 +00001121 Function *DF = dyn_cast<Function>(ValueMap[SF]); // Destination function
Chris Lattner5c377c52001-10-14 23:29:15 +00001122
Chris Lattner18961502002-06-25 16:12:52 +00001123 // DF not external SF external?
Chris Lattnerec91ccb2008-06-20 05:29:39 +00001124 if (DF && DF->isDeclaration())
Chris Lattner35956552003-10-27 16:39:39 +00001125 // Only provide the function body if there isn't one already.
1126 if (LinkFunctionBody(DF, SF, ValueMap, Err))
1127 return true;
Chris Lattnerc2d774b2001-10-23 20:43:42 +00001128 }
Chris Lattner5c377c52001-10-14 23:29:15 +00001129 }
1130 return false;
1131}
1132
Chris Lattner8166e6e2003-05-13 21:33:43 +00001133// LinkAppendingVars - If there were any appending global variables, link them
1134// together now. Return true on error.
Chris Lattner8166e6e2003-05-13 21:33:43 +00001135static bool LinkAppendingVars(Module *M,
1136 std::multimap<std::string, GlobalVariable *> &AppendingVars,
1137 std::string *ErrorMsg) {
1138 if (AppendingVars.empty()) return false; // Nothing to do.
Misha Brukmanf976c852005-04-21 22:55:34 +00001139
Chris Lattner8166e6e2003-05-13 21:33:43 +00001140 // Loop over the multimap of appending vars, processing any variables with the
1141 // same name, forming a new appending global variable with both of the
1142 // initializers merged together, then rewrite references to the old variables
1143 // and delete them.
Chris Lattner8166e6e2003-05-13 21:33:43 +00001144 std::vector<Constant*> Inits;
1145 while (AppendingVars.size() > 1) {
1146 // Get the first two elements in the map...
1147 std::multimap<std::string,
1148 GlobalVariable*>::iterator Second = AppendingVars.begin(), First=Second++;
1149
1150 // If the first two elements are for different names, there is no pair...
1151 // Otherwise there is a pair, so link them together...
1152 if (First->first == Second->first) {
1153 GlobalVariable *G1 = First->second, *G2 = Second->second;
1154 const ArrayType *T1 = cast<ArrayType>(G1->getType()->getElementType());
1155 const ArrayType *T2 = cast<ArrayType>(G2->getType()->getElementType());
Misha Brukmanf976c852005-04-21 22:55:34 +00001156
Chris Lattner8166e6e2003-05-13 21:33:43 +00001157 // Check to see that they two arrays agree on type...
1158 if (T1->getElementType() != T2->getElementType())
1159 return Error(ErrorMsg,
1160 "Appending variables with different element types need to be linked!");
1161 if (G1->isConstant() != G2->isConstant())
1162 return Error(ErrorMsg,
1163 "Appending variables linked with different const'ness!");
1164
Lauro Ramos Venancio9613e872007-06-06 22:01:12 +00001165 if (G1->getAlignment() != G2->getAlignment())
1166 return Error(ErrorMsg,
1167 "Appending variables with different alignment need to be linked!");
1168
1169 if (G1->getVisibility() != G2->getVisibility())
1170 return Error(ErrorMsg,
1171 "Appending variables with different visibility need to be linked!");
1172
1173 if (G1->getSection() != G2->getSection())
1174 return Error(ErrorMsg,
1175 "Appending variables with different section name need to be linked!");
1176
Chris Lattner8166e6e2003-05-13 21:33:43 +00001177 unsigned NewSize = T1->getNumElements() + T2->getNumElements();
1178 ArrayType *NewType = ArrayType::get(T1->getElementType(), NewSize);
1179
Chris Lattnered74a4e2005-12-06 17:30:58 +00001180 G1->setName(""); // Clear G1's name in case of a conflict!
1181
Chris Lattner8166e6e2003-05-13 21:33:43 +00001182 // Create the new global variable...
1183 GlobalVariable *NG =
1184 new GlobalVariable(NewType, G1->isConstant(), G1->getLinkage(),
Chris Lattnera534b0f2008-06-27 03:10:24 +00001185 /*init*/0, First->first, M, G1->isThreadLocal(),
1186 G1->getType()->getAddressSpace());
Chris Lattner8166e6e2003-05-13 21:33:43 +00001187
Lauro Ramos Venancio9613e872007-06-06 22:01:12 +00001188 // Propagate alignment, visibility and section info.
1189 CopyGVAttributes(NG, G1);
1190
Chris Lattner8166e6e2003-05-13 21:33:43 +00001191 // Merge the initializer...
1192 Inits.reserve(NewSize);
Chris Lattnerde512b52004-02-15 05:55:15 +00001193 if (ConstantArray *I = dyn_cast<ConstantArray>(G1->getInitializer())) {
1194 for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
Alkis Evlogimenoscc7ba492004-08-04 08:08:13 +00001195 Inits.push_back(I->getOperand(i));
Chris Lattnerde512b52004-02-15 05:55:15 +00001196 } else {
1197 assert(isa<ConstantAggregateZero>(G1->getInitializer()));
1198 Constant *CV = Constant::getNullValue(T1->getElementType());
1199 for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
1200 Inits.push_back(CV);
1201 }
1202 if (ConstantArray *I = dyn_cast<ConstantArray>(G2->getInitializer())) {
1203 for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
Alkis Evlogimenoscc7ba492004-08-04 08:08:13 +00001204 Inits.push_back(I->getOperand(i));
Chris Lattnerde512b52004-02-15 05:55:15 +00001205 } else {
1206 assert(isa<ConstantAggregateZero>(G2->getInitializer()));
1207 Constant *CV = Constant::getNullValue(T2->getElementType());
1208 for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
1209 Inits.push_back(CV);
1210 }
Chris Lattner8166e6e2003-05-13 21:33:43 +00001211 NG->setInitializer(ConstantArray::get(NewType, Inits));
1212 Inits.clear();
1213
1214 // Replace any uses of the two global variables with uses of the new
1215 // global...
1216
1217 // FIXME: This should rewrite simple/straight-forward uses such as
1218 // getelementptr instructions to not use the Cast!
Reid Spencer4da49122006-12-12 05:05:00 +00001219 G1->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G1->getType()));
1220 G2->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G2->getType()));
Chris Lattner8166e6e2003-05-13 21:33:43 +00001221
1222 // Remove the two globals from the module now...
1223 M->getGlobalList().erase(G1);
1224 M->getGlobalList().erase(G2);
1225
1226 // Put the new global into the AppendingVars map so that we can handle
1227 // linking of more than two vars...
1228 Second->second = NG;
1229 }
1230 AppendingVars.erase(First);
1231 }
1232
1233 return false;
1234}
Chris Lattner52f7e902001-10-13 07:03:50 +00001235
Anton Korobeynikov9f2ee702008-03-05 23:21:39 +00001236static bool ResolveAliases(Module *Dest) {
1237 for (Module::alias_iterator I = Dest->alias_begin(), E = Dest->alias_end();
Anton Korobeynikov52419572008-03-11 22:51:09 +00001238 I != E; ++I)
1239 if (const GlobalValue *GV = I->resolveAliasedGlobal())
1240 if (!GV->isDeclaration())
1241 I->replaceAllUsesWith(const_cast<GlobalValue*>(GV));
Anton Korobeynikov9f2ee702008-03-05 23:21:39 +00001242
1243 return false;
1244}
Chris Lattner52f7e902001-10-13 07:03:50 +00001245
1246// LinkModules - This function links two modules together, with the resulting
1247// left module modified to be the composite of the two input modules. If an
1248// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
Chris Lattner5c377c52001-10-14 23:29:15 +00001249// the problem. Upon failure, the Dest module could be in a modified state, and
1250// shouldn't be relied on to be consistent.
Misha Brukmanf976c852005-04-21 22:55:34 +00001251bool
Reid Spencer0ba9e212004-12-13 03:00:16 +00001252Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) {
Reid Spencer57a0efa2004-09-11 04:25:17 +00001253 assert(Dest != 0 && "Invalid Destination module");
1254 assert(Src != 0 && "Invalid Source Module");
1255
Chris Lattnerc36357c2007-01-29 00:21:34 +00001256 if (Dest->getDataLayout().empty()) {
1257 if (!Src->getDataLayout().empty()) {
Chris Lattnerec9bfdc2007-01-29 02:18:13 +00001258 Dest->setDataLayout(Src->getDataLayout());
Chris Lattnerc36357c2007-01-29 00:21:34 +00001259 } else {
1260 std::string DataLayout;
Reid Spencer26f23852007-01-26 08:11:39 +00001261
Anton Korobeynikova27694d2008-02-20 11:27:04 +00001262 if (Dest->getEndianness() == Module::AnyEndianness) {
Chris Lattnerc36357c2007-01-29 00:21:34 +00001263 if (Src->getEndianness() == Module::BigEndian)
1264 DataLayout.append("E");
1265 else if (Src->getEndianness() == Module::LittleEndian)
1266 DataLayout.append("e");
Anton Korobeynikova27694d2008-02-20 11:27:04 +00001267 }
1268
1269 if (Dest->getPointerSize() == Module::AnyPointerSize) {
Chris Lattnerc36357c2007-01-29 00:21:34 +00001270 if (Src->getPointerSize() == Module::Pointer64)
1271 DataLayout.append(DataLayout.length() == 0 ? "p:64:64" : "-p:64:64");
1272 else if (Src->getPointerSize() == Module::Pointer32)
1273 DataLayout.append(DataLayout.length() == 0 ? "p:32:32" : "-p:32:32");
Anton Korobeynikova27694d2008-02-20 11:27:04 +00001274 }
Chris Lattnerc36357c2007-01-29 00:21:34 +00001275 Dest->setDataLayout(DataLayout);
1276 }
1277 }
1278
Chris Lattnerf27dfcb2008-02-19 18:49:08 +00001279 // Copy the target triple from the source to dest if the dest's is empty.
Chris Lattnerc36357c2007-01-29 00:21:34 +00001280 if (Dest->getTargetTriple().empty() && !Src->getTargetTriple().empty())
Chris Lattner152f19a2004-12-10 20:26:15 +00001281 Dest->setTargetTriple(Src->getTargetTriple());
Chris Lattnerc36357c2007-01-29 00:21:34 +00001282
1283 if (!Src->getDataLayout().empty() && !Dest->getDataLayout().empty() &&
1284 Src->getDataLayout() != Dest->getDataLayout())
Reid Spencer26f23852007-01-26 08:11:39 +00001285 cerr << "WARNING: Linking two modules of different data layouts!\n";
Chris Lattner152f19a2004-12-10 20:26:15 +00001286 if (!Src->getTargetTriple().empty() &&
1287 Dest->getTargetTriple() != Src->getTargetTriple())
Bill Wendlinge8156192006-12-07 01:30:32 +00001288 cerr << "WARNING: Linking two modules of different target triples!\n";
Misha Brukmanf976c852005-04-21 22:55:34 +00001289
Chris Lattnerf27dfcb2008-02-19 18:49:08 +00001290 // Append the module inline asm string.
Chris Lattner66316012006-01-24 04:14:29 +00001291 if (!Src->getModuleInlineAsm().empty()) {
1292 if (Dest->getModuleInlineAsm().empty())
1293 Dest->setModuleInlineAsm(Src->getModuleInlineAsm());
Chris Lattnere1b2e142006-01-23 23:08:37 +00001294 else
Chris Lattner66316012006-01-24 04:14:29 +00001295 Dest->setModuleInlineAsm(Dest->getModuleInlineAsm()+"\n"+
1296 Src->getModuleInlineAsm());
Chris Lattnere1b2e142006-01-23 23:08:37 +00001297 }
1298
Reid Spencer719012d2004-11-25 09:29:44 +00001299 // Update the destination module's dependent libraries list with the libraries
Reid Spencer57a0efa2004-09-11 04:25:17 +00001300 // from the source module. There's no opportunity for duplicates here as the
1301 // Module ensures that duplicate insertions are discarded.
Chris Lattnerf27dfcb2008-02-19 18:49:08 +00001302 for (Module::lib_iterator SI = Src->lib_begin(), SE = Src->lib_end();
1303 SI != SE; ++SI)
Reid Spencer57a0efa2004-09-11 04:25:17 +00001304 Dest->addLibrary(*SI);
Reid Spencer57a0efa2004-09-11 04:25:17 +00001305
Chris Lattner2c236f32001-11-03 05:18:24 +00001306 // LinkTypes - Go through the symbol table of the Src module and see if any
1307 // types are named in the src module that are not named in the Dst module.
1308 // Make sure there are no type name conflicts.
Reid Spencer619f0242007-02-04 04:43:17 +00001309 if (LinkTypes(Dest, Src, ErrorMsg))
1310 return true;
Chris Lattner2c236f32001-11-03 05:18:24 +00001311
Chris Lattner5c377c52001-10-14 23:29:15 +00001312 // ValueMap - Mapping of values from what they used to be in Src, to what they
1313 // are now in Dest.
Chris Lattner5c2d3352003-01-30 19:53:34 +00001314 std::map<const Value*, Value*> ValueMap;
Chris Lattner5c377c52001-10-14 23:29:15 +00001315
Chris Lattner8166e6e2003-05-13 21:33:43 +00001316 // AppendingVars - Keep track of global variables in the destination module
1317 // with appending linkage. After the module is linked together, they are
1318 // appended and the module is rewritten.
Chris Lattner8166e6e2003-05-13 21:33:43 +00001319 std::multimap<std::string, GlobalVariable *> AppendingVars;
Chris Lattner11273152006-06-16 01:24:04 +00001320 for (Module::global_iterator I = Dest->global_begin(), E = Dest->global_end();
1321 I != E; ++I) {
Chris Lattner5a837de2004-08-04 07:44:58 +00001322 // Add all of the appending globals already in the Dest module to
1323 // AppendingVars.
Chris Lattnerf4146462003-05-14 12:11:51 +00001324 if (I->hasAppendingLinkage())
1325 AppendingVars.insert(std::make_pair(I->getName(), I));
Chris Lattner5a837de2004-08-04 07:44:58 +00001326 }
1327
Chris Lattner8166e6e2003-05-13 21:33:43 +00001328 // Insert all of the globals in src into the Dest module... without linking
1329 // initializers (which could refer to functions not yet mapped over).
Reid Spenceref9b9a72007-02-05 20:47:22 +00001330 if (LinkGlobals(Dest, Src, ValueMap, AppendingVars, ErrorMsg))
Chris Lattner5a837de2004-08-04 07:44:58 +00001331 return true;
Chris Lattner5c377c52001-10-14 23:29:15 +00001332
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +00001333 // Link the functions together between the two modules, without doing function
1334 // bodies... this just adds external function prototypes to the Dest
1335 // function... We do this so that when we begin processing function bodies,
1336 // all of the global values that may be referenced are available in our
1337 // ValueMap.
Reid Spenceref9b9a72007-02-05 20:47:22 +00001338 if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg))
Chris Lattner5a837de2004-08-04 07:44:58 +00001339 return true;
Chris Lattner5c377c52001-10-14 23:29:15 +00001340
Anton Korobeynikov4fb28732008-03-05 15:27:21 +00001341 // If there were any alias, link them now. We really need to do this now,
1342 // because all of the aliases that may be referenced need to be available in
1343 // ValueMap
1344 if (LinkAlias(Dest, Src, ValueMap, ErrorMsg)) return true;
1345
Chris Lattner6cdf1972002-07-18 00:13:08 +00001346 // Update the initializers in the Dest module now that all globals that may
1347 // be referenced are in Dest.
Chris Lattner6cdf1972002-07-18 00:13:08 +00001348 if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
1349
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +00001350 // Link in the function bodies that are defined in the source module into the
1351 // DestModule. This consists basically of copying the function over and
1352 // fixing up references to values.
Chris Lattner79df7c02002-03-26 18:01:55 +00001353 if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true;
Chris Lattner52f7e902001-10-13 07:03:50 +00001354
Chris Lattner8166e6e2003-05-13 21:33:43 +00001355 // If there were any appending global variables, link them together now.
Chris Lattner8166e6e2003-05-13 21:33:43 +00001356 if (LinkAppendingVars(Dest, AppendingVars, ErrorMsg)) return true;
1357
Anton Korobeynikov3db91912008-03-05 23:08:47 +00001358 // Resolve all uses of aliases with aliasees
1359 if (ResolveAliases(Dest)) return true;
1360
Reid Spencer57a0efa2004-09-11 04:25:17 +00001361 // If the source library's module id is in the dependent library list of the
1362 // destination library, remove it since that module is now linked in.
1363 sys::Path modId;
Reid Spencerdd04df02005-07-07 23:21:43 +00001364 modId.set(Src->getModuleIdentifier());
Reid Spencer07adb282004-11-05 22:15:36 +00001365 if (!modId.isEmpty())
1366 Dest->removeLibrary(modId.getBasename());
Reid Spencer57a0efa2004-09-11 04:25:17 +00001367
Chris Lattner52f7e902001-10-13 07:03:50 +00001368 return false;
1369}
Vikram S. Adve9466f512001-10-28 21:38:02 +00001370
Reid Spencer567bc2c2004-05-25 08:52:20 +00001371// vim: sw=2