blob: b867705fe12064ecc79e01afa3ea304a545c5b62 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVM module linker.
11//
12// Specifically, this:
13// * Merges global variables between the two modules
14// * Uninit + Uninit = Init, Init + Uninit = Init, Init + Init = Error if !=
15// * Merges functions between two modules
16//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Linker.h"
20#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/Module.h"
23#include "llvm/TypeSymbolTable.h"
24#include "llvm/ValueSymbolTable.h"
25#include "llvm/Instructions.h"
26#include "llvm/Assembly/Writer.h"
27#include "llvm/Support/Streams.h"
28#include "llvm/System/Path.h"
Chris Lattner0b228bf2008-06-16 21:00:18 +000029#include "llvm/ADT/DenseMap.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include <sstream>
31using namespace llvm;
32
33// Error - Simple wrapper function to conditionally assign to E and return true.
34// This just makes error return conditions a little bit simpler...
35static inline bool Error(std::string *E, const std::string &Message) {
36 if (E) *E = Message;
37 return true;
38}
39
Dan Gohmanf17a25c2007-07-18 16:29:46 +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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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.
55//
Chris Lattner06638ab2008-06-16 18:19:05 +000056static bool ResolveTypes(const Type *DestTy, const Type *SrcTy) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 if (DestTy == SrcTy) return false; // If already equal, noop
Chris Lattner06638ab2008-06-16 18:19:05 +000058 assert(DestTy && SrcTy && "Can't handle null types");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059
Chris Lattner06638ab2008-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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 }
68 return false;
69}
70
Chris Lattner0b228bf2008-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;
Chris Lattner0b228bf2008-06-16 21:00:18 +000078
Chris Lattnera9326492008-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 Lattner0b228bf2008-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 }
88
89 /// lookup - Return the value for the specified type or null if it doesn't
90 /// exist.
91 const Type *lookup(const Type *Ty) const {
92 TheMapTy::const_iterator I = TheMap.find(Ty);
93 if (I != TheMap.end()) return I->second;
94 return 0;
95 }
96
97 /// erase - Remove the specified type, returning true if it was in the set.
98 bool erase(const Type *Ty) {
99 if (!TheMap.erase(Ty))
100 return false;
101 if (Ty->isAbstract())
102 Ty->removeAbstractTypeUser(this);
103 return true;
104 }
105
106 /// insert - This returns true if the pointer was new to the set, false if it
107 /// was already in the set.
108 bool insert(const Type *Src, const Type *Dst) {
Dan Gohman55d19662008-07-07 17:46:23 +0000109 if (!TheMap.insert(std::make_pair(Src, PATypeHolder(Dst))).second)
Chris Lattner0b228bf2008-06-16 21:00:18 +0000110 return false; // Already in map.
111 if (Src->isAbstract())
112 Src->addAbstractTypeUser(this);
113 return true;
114 }
115
116protected:
117 /// refineAbstractType - The callback method invoked when an abstract type is
118 /// resolved to another type. An object must override this method to update
119 /// its internal state to reference NewType instead of OldType.
120 ///
121 virtual void refineAbstractType(const DerivedType *OldTy,
122 const Type *NewTy) {
123 TheMapTy::iterator I = TheMap.find(OldTy);
124 const Type *DstTy = I->second;
125
126 TheMap.erase(I);
127 if (OldTy->isAbstract())
128 OldTy->removeAbstractTypeUser(this);
129
130 // Don't reinsert into the map if the key is concrete now.
131 if (NewTy->isAbstract())
132 insert(NewTy, DstTy);
133 }
134
135 /// The other case which AbstractTypeUsers must be aware of is when a type
136 /// makes the transition from being abstract (where it has clients on it's
137 /// AbstractTypeUsers list) to concrete (where it does not). This method
138 /// notifies ATU's when this occurs for a type.
139 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
140 TheMap.erase(AbsTy);
141 AbsTy->removeAbstractTypeUser(this);
142 }
143
144 // for debugging...
145 virtual void dump() const {
146 cerr << "AbstractTypeSet!\n";
147 }
148};
149}
150
151
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Lattnerb2d8a032008-06-16 21:17:12 +0000155static bool RecursiveResolveTypesI(const Type *DstTy, const Type *SrcTy,
Chris Lattner0b228bf2008-06-16 21:00:18 +0000156 LinkerTypeMap &Pointers) {
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000157 if (DstTy == SrcTy) return false; // If already equal, noop
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158
159 // If we found our opaque type, resolve it now!
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000160 if (isa<OpaqueType>(DstTy) || isa<OpaqueType>(SrcTy))
161 return ResolveTypes(DstTy, SrcTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162
163 // Two types cannot be resolved together if they are of different primitive
164 // type. For example, we cannot resolve an int to a float.
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000165 if (DstTy->getTypeID() != SrcTy->getTypeID()) return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166
Chris Lattnere174d322008-06-16 20:03:01 +0000167 // If neither type is abstract, then they really are just different types.
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000168 if (!DstTy->isAbstract() && !SrcTy->isAbstract())
Chris Lattnere174d322008-06-16 20:03:01 +0000169 return true;
170
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 // Otherwise, resolve the used type used by this derived type...
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000172 switch (DstTy->getTypeID()) {
Chris Lattner6b9bdb72008-06-16 18:27:53 +0000173 default:
174 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 case Type::FunctionTyID: {
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000176 const FunctionType *DstFT = cast<FunctionType>(DstTy);
177 const FunctionType *SrcFT = cast<FunctionType>(SrcTy);
Chris Lattner41fed262008-06-16 19:55:40 +0000178 if (DstFT->isVarArg() != SrcFT->isVarArg() ||
179 DstFT->getNumContainedTypes() != SrcFT->getNumContainedTypes())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 return true;
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000181
182 // Use TypeHolder's so recursive resolution won't break us.
183 PATypeHolder ST(SrcFT), DT(DstFT);
184 for (unsigned i = 0, e = DstFT->getNumContainedTypes(); i != e; ++i) {
185 const Type *SE = ST->getContainedType(i), *DE = DT->getContainedType(i);
186 if (SE != DE && RecursiveResolveTypesI(DE, SE, Pointers))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 return true;
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000188 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 return false;
190 }
191 case Type::StructTyID: {
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000192 const StructType *DstST = cast<StructType>(DstTy);
193 const StructType *SrcST = cast<StructType>(SrcTy);
Chris Lattner41fed262008-06-16 19:55:40 +0000194 if (DstST->getNumContainedTypes() != SrcST->getNumContainedTypes())
Chris Lattner6b9bdb72008-06-16 18:27:53 +0000195 return true;
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000196
197 PATypeHolder ST(SrcST), DT(DstST);
198 for (unsigned i = 0, e = DstST->getNumContainedTypes(); i != e; ++i) {
199 const Type *SE = ST->getContainedType(i), *DE = DT->getContainedType(i);
200 if (SE != DE && RecursiveResolveTypesI(DE, SE, Pointers))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 return true;
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000202 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 return false;
204 }
205 case Type::ArrayTyID: {
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000206 const ArrayType *DAT = cast<ArrayType>(DstTy);
207 const ArrayType *SAT = cast<ArrayType>(SrcTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 if (DAT->getNumElements() != SAT->getNumElements()) return true;
209 return RecursiveResolveTypesI(DAT->getElementType(), SAT->getElementType(),
Chris Lattner06638ab2008-06-16 18:19:05 +0000210 Pointers);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 }
Chris Lattner6b9bdb72008-06-16 18:27:53 +0000212 case Type::VectorTyID: {
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000213 const VectorType *DVT = cast<VectorType>(DstTy);
214 const VectorType *SVT = cast<VectorType>(SrcTy);
Chris Lattner6b9bdb72008-06-16 18:27:53 +0000215 if (DVT->getNumElements() != SVT->getNumElements()) return true;
216 return RecursiveResolveTypesI(DVT->getElementType(), SVT->getElementType(),
217 Pointers);
218 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 case Type::PointerTyID: {
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000220 const PointerType *DstPT = cast<PointerType>(DstTy);
221 const PointerType *SrcPT = cast<PointerType>(SrcTy);
Chris Lattner41fed262008-06-16 19:55:40 +0000222
223 if (DstPT->getAddressSpace() != SrcPT->getAddressSpace())
224 return true;
225
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Lattner0b228bf2008-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;
233
234 if (DstPT->isAbstract())
235 if (const Type *ExistingSrcTy = Pointers.lookup(DstPT))
236 return ExistingSrcTy != SrcPT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 // Otherwise, add the current pointers to the vector to stop recursion on
238 // this pair.
Chris Lattner0b228bf2008-06-16 21:00:18 +0000239 if (DstPT->isAbstract())
240 Pointers.insert(DstPT, SrcPT);
241 if (SrcPT->isAbstract())
242 Pointers.insert(SrcPT, DstPT);
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000243
Chris Lattner41fed262008-06-16 19:55:40 +0000244 return RecursiveResolveTypesI(DstPT->getElementType(),
245 SrcPT->getElementType(), Pointers);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 }
248}
249
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000250static bool RecursiveResolveTypes(const Type *DestTy, const Type *SrcTy) {
Chris Lattner0b228bf2008-06-16 21:00:18 +0000251 LinkerTypeMap PointerTypes;
Chris Lattner06638ab2008-06-16 18:19:05 +0000252 return RecursiveResolveTypesI(DestTy, SrcTy, PointerTypes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253}
254
255
256// LinkTypes - Go through the symbol table of the Src module and see if any
257// types are named in the src module that are not named in the Dst module.
258// Make sure there are no type name conflicts.
259static bool LinkTypes(Module *Dest, const Module *Src, std::string *Err) {
260 TypeSymbolTable *DestST = &Dest->getTypeSymbolTable();
261 const TypeSymbolTable *SrcST = &Src->getTypeSymbolTable();
262
263 // Look for a type plane for Type's...
264 TypeSymbolTable::const_iterator TI = SrcST->begin();
265 TypeSymbolTable::const_iterator TE = SrcST->end();
266 if (TI == TE) return false; // No named types, do nothing.
267
268 // Some types cannot be resolved immediately because they depend on other
269 // types being resolved to each other first. This contains a list of types we
270 // are waiting to recheck.
271 std::vector<std::string> DelayedTypesToResolve;
272
273 for ( ; TI != TE; ++TI ) {
274 const std::string &Name = TI->first;
275 const Type *RHS = TI->second;
276
Chris Lattner06638ab2008-06-16 18:19:05 +0000277 // Check to see if this type name is already in the dest module.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 Type *Entry = DestST->lookup(Name);
279
Chris Lattner06638ab2008-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)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 // They look different, save the types 'till later to resolve.
286 DelayedTypesToResolve.push_back(Name);
287 }
288 }
289
290 // Iteratively resolve types while we can...
291 while (!DelayedTypesToResolve.empty()) {
292 // Loop over all of the types, attempting to resolve them if possible...
293 unsigned OldSize = DelayedTypesToResolve.size();
294
295 // Try direct resolution by name...
296 for (unsigned i = 0; i != DelayedTypesToResolve.size(); ++i) {
297 const std::string &Name = DelayedTypesToResolve[i];
298 Type *T1 = SrcST->lookup(Name);
299 Type *T2 = DestST->lookup(Name);
Chris Lattner06638ab2008-06-16 18:19:05 +0000300 if (!ResolveTypes(T2, T1)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +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) {
309 // Attempt to resolve subelements of types. This allows us to merge these
310 // two types: { int* } and { opaque* }
311 for (unsigned i = 0, e = DelayedTypesToResolve.size(); i != e; ++i) {
312 const std::string &Name = DelayedTypesToResolve[i];
Chris Lattnerb2d8a032008-06-16 21:17:12 +0000313 if (!RecursiveResolveTypes(SrcST->lookup(Name), DestST->lookup(Name))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 // We are making progress!
315 DelayedTypesToResolve.erase(DelayedTypesToResolve.begin()+i);
316
317 // Go back to the main loop, perhaps we can resolve directly by name
318 // now...
319 break;
320 }
321 }
322
323 // If we STILL cannot resolve the types, then there is something wrong.
324 if (DelayedTypesToResolve.size() == OldSize) {
325 // Remove the symbol name from the destination.
326 DelayedTypesToResolve.pop_back();
327 }
328 }
329 }
330
331
332 return false;
333}
334
Chris Lattnercb13a012008-07-14 05:52:33 +0000335#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +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();
338 I != E; ++I) {
339 cerr << " Fr: " << (void*)I->first << " ";
340 I->first->dump();
341 cerr << " To: " << (void*)I->second << " ";
342 I->second->dump();
343 cerr << "\n";
344 }
345}
Chris Lattnercb13a012008-07-14 05:52:33 +0000346#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347
348
349// RemapOperand - Use ValueMap to convert constants from one module to another.
350static Value *RemapOperand(const Value *In,
351 std::map<const Value*, Value*> &ValueMap) {
352 std::map<const Value*,Value*>::const_iterator I = ValueMap.find(In);
353 if (I != ValueMap.end())
354 return I->second;
355
356 // Check to see if it's a constant that we are interested in transforming.
357 Value *Result = 0;
358 if (const Constant *CPV = dyn_cast<Constant>(In)) {
359 if ((!isa<DerivedType>(CPV->getType()) && !isa<ConstantExpr>(CPV)) ||
360 isa<ConstantInt>(CPV) || isa<ConstantAggregateZero>(CPV))
361 return const_cast<Constant*>(CPV); // Simple constants stay identical.
362
363 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
364 std::vector<Constant*> Operands(CPA->getNumOperands());
365 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
366 Operands[i] =cast<Constant>(RemapOperand(CPA->getOperand(i), ValueMap));
367 Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
368 } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
369 std::vector<Constant*> Operands(CPS->getNumOperands());
370 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
371 Operands[i] =cast<Constant>(RemapOperand(CPS->getOperand(i), ValueMap));
372 Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
373 } else if (isa<ConstantPointerNull>(CPV) || isa<UndefValue>(CPV)) {
374 Result = const_cast<Constant*>(CPV);
375 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CPV)) {
376 std::vector<Constant*> Operands(CP->getNumOperands());
377 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
378 Operands[i] = cast<Constant>(RemapOperand(CP->getOperand(i), ValueMap));
379 Result = ConstantVector::get(Operands);
380 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
381 std::vector<Constant*> Ops;
382 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
383 Ops.push_back(cast<Constant>(RemapOperand(CE->getOperand(i),ValueMap)));
384 Result = CE->getWithOperands(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 } else {
Chris Lattnercb13a012008-07-14 05:52:33 +0000386 assert(!isa<GlobalValue>(CPV) && "Unmapped global?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 assert(0 && "Unknown type of derived type constant value!");
388 }
389 } else if (isa<InlineAsm>(In)) {
390 Result = const_cast<Value*>(In);
391 }
392
393 // Cache the mapping in our local map structure
394 if (Result) {
Anton Korobeynikovef1eb5e2008-03-10 22:36:08 +0000395 ValueMap[In] = Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 return Result;
397 }
398
Chris Lattnercb13a012008-07-14 05:52:33 +0000399#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 cerr << "LinkModules ValueMap: \n";
401 PrintMap(ValueMap);
402
403 cerr << "Couldn't remap value: " << (void*)In << " " << *In << "\n";
404 assert(0 && "Couldn't remap value!");
Chris Lattnercb13a012008-07-14 05:52:33 +0000405#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 return 0;
407}
408
409/// ForceRenaming - The LLVM SymbolTable class autorenames globals that conflict
410/// in the symbol table. This is good for all clients except for us. Go
411/// through the trouble to force this back.
412static void ForceRenaming(GlobalValue *GV, const std::string &Name) {
413 assert(GV->getName() != Name && "Can't force rename to self");
414 ValueSymbolTable &ST = GV->getParent()->getValueSymbolTable();
415
416 // If there is a conflict, rename the conflict.
417 if (GlobalValue *ConflictGV = cast_or_null<GlobalValue>(ST.lookup(Name))) {
418 assert(ConflictGV->hasInternalLinkage() &&
419 "Not conflicting with a static global, should link instead!");
420 GV->takeName(ConflictGV);
421 ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
422 assert(ConflictGV->getName() != Name && "ForceRenaming didn't work");
423 } else {
424 GV->setName(Name); // Force the name back
425 }
426}
427
428/// CopyGVAttributes - copy additional attributes (those not needed to construct
429/// a GlobalValue) from the SrcGV to the DestGV.
430static void CopyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) {
Duncan Sands0cc90582008-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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435}
436
437/// GetLinkageResult - This analyzes the two global values and determines what
438/// the result will look like in the destination module. In particular, it
439/// computes the resultant linkage type, computes whether the global in the
440/// source should be copied over to the destination (replacing the existing
441/// one), and computes whether this linkage is an error or not. It also performs
442/// visibility checks: we cannot link together two symbols with different
443/// visibilities.
Anton Korobeynikovebb58502008-03-10 22:33:53 +0000444static bool GetLinkageResult(GlobalValue *Dest, const GlobalValue *Src,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 GlobalValue::LinkageTypes &LT, bool &LinkFromSrc,
446 std::string *Err) {
447 assert((!Dest || !Src->hasInternalLinkage()) &&
448 "If Src has internal linkage, Dest shouldn't be set!");
449 if (!Dest) {
450 // Linking something to nothing.
451 LinkFromSrc = true;
452 LT = Src->getLinkage();
453 } else if (Src->isDeclaration()) {
Anton Korobeynikov15520982008-03-10 22:33:22 +0000454 // If Src is external or if both Src & Dest are external.. Just link the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 // external globals, we aren't adding anything.
456 if (Src->hasDLLImportLinkage()) {
457 // If one of GVs has DLLImport linkage, result should be dllimport'ed.
458 if (Dest->isDeclaration()) {
459 LinkFromSrc = true;
460 LT = Src->getLinkage();
461 }
462 } else if (Dest->hasExternalWeakLinkage()) {
463 //If the Dest is weak, use the source linkage
464 LinkFromSrc = true;
465 LT = Src->getLinkage();
466 } else {
467 LinkFromSrc = false;
468 LT = Dest->getLinkage();
469 }
470 } else if (Dest->isDeclaration() && !Dest->hasDLLImportLinkage()) {
471 // If Dest is external but Src is not:
472 LinkFromSrc = true;
473 LT = Src->getLinkage();
474 } else if (Src->hasAppendingLinkage() || Dest->hasAppendingLinkage()) {
475 if (Src->getLinkage() != Dest->getLinkage())
476 return Error(Err, "Linking globals named '" + Src->getName() +
477 "': can only link appending global with another appending global!");
478 LinkFromSrc = true; // Special cased.
479 LT = Src->getLinkage();
Anton Korobeynikov46aa9c12008-07-05 23:48:30 +0000480 } else if (Src->isWeakForLinker()) {
Dale Johannesen49c44122008-05-14 20:12:51 +0000481 // At this point we know that Dest has LinkOnce, External*, Weak, Common,
482 // or DLL* linkage.
Anton Korobeynikov46aa9c12008-07-05 23:48:30 +0000483 if ((Dest->hasLinkOnceLinkage() &&
Dale Johannesen49c44122008-05-14 20:12:51 +0000484 (Src->hasWeakLinkage() || Src->hasCommonLinkage())) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 Dest->hasExternalWeakLinkage()) {
486 LinkFromSrc = true;
487 LT = Src->getLinkage();
488 } else {
489 LinkFromSrc = false;
490 LT = Dest->getLinkage();
491 }
Anton Korobeynikov46aa9c12008-07-05 23:48:30 +0000492 } else if (Dest->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 }
501 } else {
502 assert((Dest->hasExternalLinkage() ||
503 Dest->hasDLLImportLinkage() ||
504 Dest->hasDLLExportLinkage() ||
505 Dest->hasExternalWeakLinkage()) &&
506 (Src->hasExternalLinkage() ||
507 Src->hasDLLImportLinkage() ||
508 Src->hasDLLExportLinkage() ||
509 Src->hasExternalWeakLinkage()) &&
510 "Unexpected linkage type!");
511 return Error(Err, "Linking globals named '" + Src->getName() +
512 "': symbol multiply defined!");
513 }
514
515 // Check visibility
516 if (Dest && Src->getVisibility() != Dest->getVisibility())
Chris Lattnerb69fcb82007-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!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 return false;
521}
522
523// LinkGlobals - Loop through the global variables in the src module and merge
524// them into the dest module.
Anton Korobeynikovebb58502008-03-10 22:33:53 +0000525static bool LinkGlobals(Module *Dest, const Module *Src,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 std::map<const Value*, Value*> &ValueMap,
527 std::multimap<std::string, GlobalVariable *> &AppendingVars,
528 std::string *Err) {
Chris Lattner0763d412008-07-14 06:49:45 +0000529 ValueSymbolTable &DestSymTab = Dest->getValueSymbolTable();
530
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 // Loop over all of the globals in the src module, mapping them over as we go
Chris Lattnercb13a012008-07-14 05:52:33 +0000532 for (Module::const_global_iterator I = Src->global_begin(),
533 E = Src->global_end(); I != E; ++I) {
Anton Korobeynikovebb58502008-03-10 22:33:53 +0000534 const GlobalVariable *SGV = I;
Anton Korobeynikov30dd7162008-03-10 22:34:28 +0000535 GlobalValue *DGV = 0;
536
Chris Lattner0763d412008-07-14 06:49:45 +0000537 // Check to see if may have to link the global with the global, alias or
538 // function.
539 if (SGV->hasName() && !SGV->hasInternalLinkage())
540 DGV = cast_or_null<GlobalValue>(DestSymTab.lookup(SGV->getNameStart(),
541 SGV->getNameEnd()));
542
Chris Lattner08d002a2008-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.
545 if (DGV && DGV->hasInternalLinkage())
546 DGV = 0;
547
Chris Lattner0763d412008-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 Korobeynikov30dd7162008-03-10 22:34:28 +0000551
Dan Gohman930191f2007-10-08 15:13:30 +0000552 assert((SGV->hasInitializer() || SGV->hasExternalWeakLinkage() ||
553 SGV->hasExternalLinkage() || SGV->hasDLLImportLinkage()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 "Global must either be external or have an initializer!");
555
556 GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
557 bool LinkFromSrc = false;
558 if (GetLinkageResult(DGV, SGV, NewLinkage, LinkFromSrc, Err))
559 return true;
560
Chris Lattner910c6142008-07-14 07:23:24 +0000561 if (DGV == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Lattner0763d412008-07-14 06:49:45 +0000564 // later by LinkGlobalInits.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 GlobalVariable *NewDGV =
566 new GlobalVariable(SGV->getType()->getElementType(),
567 SGV->isConstant(), SGV->getLinkage(), /*init*/0,
Chris Lattner29ae6c52008-06-27 03:10:24 +0000568 SGV->getName(), Dest, false,
569 SGV->getType()->getAddressSpace());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 // Propagate alignment, visibility and section info.
571 CopyGVAttributes(NewDGV, SGV);
572
573 // If the LLVM runtime renamed the global, but it is an externally visible
574 // symbol, DGV must be an existing global with internal linkage. Rename
575 // it.
Chris Lattner910c6142008-07-14 07:23:24 +0000576 if (!NewDGV->hasInternalLinkage() && NewDGV->getName() != SGV->getName())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577 ForceRenaming(NewDGV, SGV->getName());
578
Chris Lattner910c6142008-07-14 07:23:24 +0000579 // Make sure to remember this mapping.
Anton Korobeynikovef1eb5e2008-03-10 22:36:08 +0000580 ValueMap[SGV] = NewDGV;
581
Chris Lattner0763d412008-07-14 06:49:45 +0000582 // Keep track that this is an appending variable.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 if (SGV->hasAppendingLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
Chris Lattner910c6142008-07-14 07:23:24 +0000585 continue;
586 }
587
588 // If the visibilities of the symbols disagree and the destination is a
589 // prototype, take the visibility of its input.
590 if (DGV->isDeclaration())
591 DGV->setVisibility(SGV->getVisibility());
592
593 if (DGV->hasAppendingLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Lattner29ae6c52008-06-27 03:10:24 +0000601 "", Dest, false,
602 SGV->getType()->getAddressSpace());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603
Anton Korobeynikov4da527c2008-03-07 18:34:50 +0000604 // Set alignment allowing CopyGVAttributes merge it with alignment of SGV.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 NewDGV->setAlignment(DGV->getAlignment());
Anton Korobeynikov4da527c2008-03-07 18:34:50 +0000606 // Propagate alignment, section and visibility info.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 CopyGVAttributes(NewDGV, SGV);
608
609 // Make sure to remember this mapping...
Anton Korobeynikovef1eb5e2008-03-10 22:36:08 +0000610 ValueMap[SGV] = NewDGV;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611
612 // Keep track that this is an appending variable...
613 AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
Chris Lattner910c6142008-07-14 07:23:24 +0000614 continue;
615 }
616
617 if (LinkFromSrc) {
618 if (isa<GlobalAlias>(DGV))
Anton Korobeynikov82a21e42008-03-10 22:34:46 +0000619 return Error(Err, "Global-Alias Collision on '" + SGV->getName() +
620 "': symbol multiple defined");
Chris Lattner910c6142008-07-14 07:23:24 +0000621
Chris Lattner0763d412008-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 Lattner910c6142008-07-14 07:23:24 +0000627 new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
628 NewLinkage, /*init*/0, DGV->getName(), Dest, false,
Chris Lattner0763d412008-07-14 06:49:45 +0000629 SGV->getType()->getAddressSpace());
630
631 // Propagate alignment, section, and visibility info.
632 CopyGVAttributes(NewDGV, SGV);
633 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV, DGV->getType()));
634
635 // DGV will conflict with NewDGV because they both had the same
636 // name. We must erase this now so ForceRenaming doesn't assert
637 // because DGV might not have internal linkage.
638 if (GlobalVariable *Var = dyn_cast<GlobalVariable>(DGV))
639 Var->eraseFromParent();
640 else
641 cast<Function>(DGV)->eraseFromParent();
642 DGV = NewDGV;
Anton Korobeynikovebb58502008-03-10 22:33:53 +0000643
Chris Lattner0763d412008-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.
Chris Lattner910c6142008-07-14 07:23:24 +0000646 if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage())
Chris Lattner0763d412008-07-14 06:49:45 +0000647 ForceRenaming(NewDGV, SGV->getName());
648
Chris Lattner910c6142008-07-14 07:23:24 +0000649 // Inherit const as appropriate.
Chris Lattner0763d412008-07-14 06:49:45 +0000650 NewDGV->setConstant(SGV->isConstant());
651
Chris Lattner910c6142008-07-14 07:23:24 +0000652 // Make sure to remember this mapping.
653 ValueMap[SGV] = NewDGV;
654 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655 }
Chris Lattner910c6142008-07-14 07:23:24 +0000656
657 // Not "link from source", keep the one in the DestModule and remap the
658 // input onto it.
659
660 // Special case for const propagation.
661 if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV))
662 if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant())
663 DGVar->setConstant(true);
664
665 // SGV is global, but DGV is alias. The only valid mapping is when SGV is
666 // external declaration, which is effectively a no-op. Also make sure
667 // linkage calculation was correct.
668 if (isa<GlobalAlias>(DGV) && !SGV->isDeclaration())
669 return Error(Err, "Global-Alias Collision on '" + SGV->getName() +
670 "': symbol multiple defined");
671
672 // Set calculated linkage
673 DGV->setLinkage(NewLinkage);
674
675 // Make sure to remember this mapping...
676 ValueMap[SGV] = ConstantExpr::getBitCast(DGV, SGV->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 }
678 return false;
679}
680
Anton Korobeynikovd0391562008-03-05 22:22:46 +0000681static GlobalValue::LinkageTypes
682CalculateAliasLinkage(const GlobalValue *SGV, const GlobalValue *DGV) {
683 if (SGV->hasExternalLinkage() || DGV->hasExternalLinkage())
684 return GlobalValue::ExternalLinkage;
685 else if (SGV->hasWeakLinkage() || DGV->hasWeakLinkage())
686 return GlobalValue::WeakLinkage;
687 else {
688 assert(SGV->hasInternalLinkage() && DGV->hasInternalLinkage() &&
689 "Unexpected linkage type");
690 return GlobalValue::InternalLinkage;
691 }
692}
693
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694// LinkAlias - Loop through the alias in the src module and link them into the
Anton Korobeynikovd0391562008-03-05 22:22:46 +0000695// dest module. We're assuming, that all functions/global variables were already
696// linked in.
Anton Korobeynikov3cfecfd2008-03-05 15:27:21 +0000697static bool LinkAlias(Module *Dest, const Module *Src,
698 std::map<const Value*, Value*> &ValueMap,
699 std::string *Err) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 // Loop over all alias in the src module
701 for (Module::const_alias_iterator I = Src->alias_begin(),
702 E = Src->alias_end(); I != E; ++I) {
Anton Korobeynikovd0391562008-03-05 22:22:46 +0000703 const GlobalAlias *SGA = I;
704 const GlobalValue *SAliasee = SGA->getAliasedGlobal();
705 GlobalAlias *NewGA = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706
Anton Korobeynikovd0391562008-03-05 22:22:46 +0000707 // Globals were already linked, thus we can just query ValueMap for variant
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000708 // of SAliasee in Dest.
Ted Kremenekd40cdd22008-03-09 18:32:50 +0000709 std::map<const Value*,Value*>::const_iterator VMI = ValueMap.find(SAliasee);
710 assert(VMI != ValueMap.end() && "Aliasee not linked");
711 GlobalValue* DAliasee = cast<GlobalValue>(VMI->second);
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000712 GlobalValue* DGV = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713
Anton Korobeynikovd0391562008-03-05 22:22:46 +0000714 // Try to find something 'similar' to SGA in destination module.
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000715 if (!DGV && !SGA->hasInternalLinkage()) {
716 DGV = Dest->getNamedAlias(SGA->getName());
Anton Korobeynikov3cfecfd2008-03-05 15:27:21 +0000717
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000718 // If types don't agree due to opaque types, try to resolve them.
719 if (DGV && DGV->getType() != SGA->getType())
Chris Lattnere97603b2008-07-10 01:09:33 +0000720 RecursiveResolveTypes(SGA->getType(), DGV->getType());
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000721 }
722
723 if (!DGV && !SGA->hasInternalLinkage()) {
724 DGV = Dest->getGlobalVariable(SGA->getName());
725
726 // If types don't agree due to opaque types, try to resolve them.
727 if (DGV && DGV->getType() != SGA->getType())
Chris Lattnere97603b2008-07-10 01:09:33 +0000728 RecursiveResolveTypes(SGA->getType(), DGV->getType());
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000729 }
730
731 if (!DGV && !SGA->hasInternalLinkage()) {
732 DGV = Dest->getFunction(SGA->getName());
733
734 // If types don't agree due to opaque types, try to resolve them.
735 if (DGV && DGV->getType() != SGA->getType())
Chris Lattnere97603b2008-07-10 01:09:33 +0000736 RecursiveResolveTypes(SGA->getType(), DGV->getType());
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000737 }
738
739 // No linking to be performed on internal stuff.
740 if (DGV && DGV->hasInternalLinkage())
741 DGV = NULL;
742
743 if (GlobalAlias *DGA = dyn_cast_or_null<GlobalAlias>(DGV)) {
744 // Types are known to be the same, check whether aliasees equal. As
Anton Korobeynikovd0391562008-03-05 22:22:46 +0000745 // globals are already linked we just need query ValueMap to find the
746 // mapping.
747 if (DAliasee == DGA->getAliasedGlobal()) {
748 // This is just two copies of the same alias. Propagate linkage, if
749 // necessary.
750 DGA->setLinkage(CalculateAliasLinkage(SGA, DGA));
751
752 NewGA = DGA;
753 // Proceed to 'common' steps
754 } else
Anton Korobeynikov82a21e42008-03-10 22:34:46 +0000755 return Error(Err, "Alias Collision on '" + SGA->getName()+
756 "': aliases have different aliasees");
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000757 } else if (GlobalVariable *DGVar = dyn_cast_or_null<GlobalVariable>(DGV)) {
Anton Korobeynikovcfc59112008-07-05 23:33:22 +0000758 // The only allowed way is to link alias with external declaration or weak
759 // symbol..
Anton Korobeynikov46aa9c12008-07-05 23:48:30 +0000760 if (DGVar->isDeclaration() || DGVar->isWeakForLinker()) {
Anton Korobeynikov0a67e052008-03-10 22:36:53 +0000761 // But only if aliasee is global too...
762 if (!isa<GlobalVariable>(DAliasee))
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000763 return Error(Err, "Global-Alias Collision on '" + SGA->getName() +
764 "': aliasee is not global variable");
Anton Korobeynikov0a67e052008-03-10 22:36:53 +0000765
Anton Korobeynikovd0391562008-03-05 22:22:46 +0000766 NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(),
767 SGA->getName(), DAliasee, Dest);
768 CopyGVAttributes(NewGA, SGA);
769
770 // Any uses of DGV need to change to NewGA, with cast, if needed.
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000771 if (SGA->getType() != DGVar->getType())
772 DGVar->replaceAllUsesWith(ConstantExpr::getBitCast(NewGA,
773 DGVar->getType()));
Anton Korobeynikovd0391562008-03-05 22:22:46 +0000774 else
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000775 DGVar->replaceAllUsesWith(NewGA);
Anton Korobeynikovd0391562008-03-05 22:22:46 +0000776
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000777 // DGVar will conflict with NewGA because they both had the same
Anton Korobeynikovd0391562008-03-05 22:22:46 +0000778 // name. We must erase this now so ForceRenaming doesn't assert
779 // because DGV might not have internal linkage.
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000780 DGVar->eraseFromParent();
Anton Korobeynikovd0391562008-03-05 22:22:46 +0000781
782 // Proceed to 'common' steps
783 } else
Anton Korobeynikov82a21e42008-03-10 22:34:46 +0000784 return Error(Err, "Global-Alias Collision on '" + SGA->getName() +
785 "': symbol multiple defined");
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000786 } else if (Function *DF = dyn_cast_or_null<Function>(DGV)) {
Anton Korobeynikovcfc59112008-07-05 23:33:22 +0000787 // The only allowed way is to link alias with external declaration or weak
788 // symbol...
Anton Korobeynikov46aa9c12008-07-05 23:48:30 +0000789 if (DF->isDeclaration() || DF->isWeakForLinker()) {
Anton Korobeynikov0a67e052008-03-10 22:36:53 +0000790 // But only if aliasee is function too...
791 if (!isa<Function>(DAliasee))
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000792 return Error(Err, "Function-Alias Collision on '" + SGA->getName() +
793 "': aliasee is not function");
Anton Korobeynikov0a67e052008-03-10 22:36:53 +0000794
Anton Korobeynikovcdf208a2008-03-05 23:08:16 +0000795 NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(),
796 SGA->getName(), DAliasee, Dest);
797 CopyGVAttributes(NewGA, SGA);
798
799 // Any uses of DF need to change to NewGA, with cast, if needed.
800 if (SGA->getType() != DF->getType())
801 DF->replaceAllUsesWith(ConstantExpr::getBitCast(NewGA,
802 DF->getType()));
803 else
804 DF->replaceAllUsesWith(NewGA);
805
806 // DF will conflict with NewGA because they both had the same
807 // name. We must erase this now so ForceRenaming doesn't assert
808 // because DF might not have internal linkage.
809 DF->eraseFromParent();
810
811 // Proceed to 'common' steps
812 } else
Anton Korobeynikov82a21e42008-03-10 22:34:46 +0000813 return Error(Err, "Function-Alias Collision on '" + SGA->getName() +
814 "': symbol multiple defined");
Anton Korobeynikovd0391562008-03-05 22:22:46 +0000815 } else {
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000816 // No linking to be performed, simply create an identical version of the
817 // alias over in the dest module...
Anton Korobeynikovd0391562008-03-05 22:22:46 +0000818
819 NewGA = new GlobalAlias(SGA->getType(), SGA->getLinkage(),
820 SGA->getName(), DAliasee, Dest);
821 CopyGVAttributes(NewGA, SGA);
822
823 // Proceed to 'common' steps
824 }
825
826 assert(NewGA && "No alias was created in destination module!");
827
Anton Korobeynikov552ccce2008-03-10 22:36:35 +0000828 // If the symbol table renamed the alias, but it is an externally visible
Anton Korobeynikov1b4f1f72008-05-10 14:41:43 +0000829 // symbol, DGA must be an global value with internal linkage. Rename it.
Anton Korobeynikovd0391562008-03-05 22:22:46 +0000830 if (NewGA->getName() != SGA->getName() &&
831 !NewGA->hasInternalLinkage())
832 ForceRenaming(NewGA, SGA->getName());
833
834 // Remember this mapping so uses in the source module get remapped
835 // later by RemapOperand.
Anton Korobeynikovef1eb5e2008-03-10 22:36:08 +0000836 ValueMap[SGA] = NewGA;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000837 }
Anton Korobeynikovd0391562008-03-05 22:22:46 +0000838
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839 return false;
840}
841
842
843// LinkGlobalInits - Update the initializers in the Dest module now that all
844// globals that may be referenced are in Dest.
845static bool LinkGlobalInits(Module *Dest, const Module *Src,
846 std::map<const Value*, Value*> &ValueMap,
847 std::string *Err) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000848 // Loop over all of the globals in the src module, mapping them over as we go
849 for (Module::const_global_iterator I = Src->global_begin(),
850 E = Src->global_end(); I != E; ++I) {
851 const GlobalVariable *SGV = I;
852
853 if (SGV->hasInitializer()) { // Only process initialized GV's
854 // Figure out what the initializer looks like in the dest module...
855 Constant *SInit =
856 cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap));
857
Anton Korobeynikov48fc88f2008-05-07 22:54:15 +0000858 GlobalVariable *DGV =
859 cast<GlobalVariable>(ValueMap[SGV]->stripPointerCasts());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000860 if (DGV->hasInitializer()) {
861 if (SGV->hasExternalLinkage()) {
862 if (DGV->getInitializer() != SInit)
Anton Korobeynikov82a21e42008-03-10 22:34:46 +0000863 return Error(Err, "Global Variable Collision on '" + SGV->getName() +
864 "': global variables have different initializers");
Anton Korobeynikov46aa9c12008-07-05 23:48:30 +0000865 } else if (DGV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866 // Nothing is required, mapped values will take the new global
867 // automatically.
Anton Korobeynikov46aa9c12008-07-05 23:48:30 +0000868 } else if (SGV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 // Nothing is required, mapped values will take the new global
870 // automatically.
871 } else if (DGV->hasAppendingLinkage()) {
872 assert(0 && "Appending linkage unimplemented!");
873 } else {
874 assert(0 && "Unknown linkage!");
875 }
876 } else {
877 // Copy the initializer over now...
878 DGV->setInitializer(SInit);
879 }
880 }
881 }
882 return false;
883}
884
885// LinkFunctionProtos - Link the functions together between the two modules,
886// without doing function bodies... this just adds external function prototypes
887// to the Dest function...
888//
889static bool LinkFunctionProtos(Module *Dest, const Module *Src,
890 std::map<const Value*, Value*> &ValueMap,
891 std::string *Err) {
Chris Lattner0763d412008-07-14 06:49:45 +0000892 ValueSymbolTable &DestSymTab = Dest->getValueSymbolTable();
893
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000894 // Loop over all of the functions in the src module, mapping them over
895 for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
896 const Function *SF = I; // SrcFunction
Anton Korobeynikovfdeba112008-07-05 23:03:21 +0000897 GlobalValue *DGV = 0;
Chris Lattner1426bfa2008-06-09 07:36:11 +0000898
Chris Lattner0763d412008-07-14 06:49:45 +0000899 // Check to see if may have to link the function with the global, alias or
900 // function.
901 if (SF->hasName() && !SF->hasInternalLinkage())
902 DGV = cast_or_null<GlobalValue>(DestSymTab.lookup(SF->getNameStart(),
903 SF->getNameEnd()));
904
Chris Lattner08d002a2008-07-14 06:52:19 +0000905 // If we found a global with the same name in the dest module, but it has
906 // internal linkage, we are really not doing any linkage here.
907 if (DGV && DGV->hasInternalLinkage())
908 DGV = 0;
909
Chris Lattner0763d412008-07-14 06:49:45 +0000910 // If types don't agree due to opaque types, try to resolve them.
911 if (DGV && DGV->getType() != SF->getType())
912 RecursiveResolveTypes(SF->getType(), DGV->getType());
Anton Korobeynikovfdeba112008-07-05 23:03:21 +0000913
Chris Lattner910c6142008-07-14 07:23:24 +0000914 GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
915 bool LinkFromSrc = false;
916 if (GetLinkageResult(DGV, SF, NewLinkage, LinkFromSrc, Err))
917 return true;
918
Chris Lattner1426bfa2008-06-09 07:36:11 +0000919 // If there is no linkage to be performed, just bring over SF without
920 // modifying it.
Anton Korobeynikovfdeba112008-07-05 23:03:21 +0000921 if (DGV == 0) {
Chris Lattner1426bfa2008-06-09 07:36:11 +0000922 // Function does not already exist, simply insert an function signature
923 // identical to SF into the dest module.
924 Function *NewDF = Function::Create(SF->getFunctionType(),
925 SF->getLinkage(),
926 SF->getName(), Dest);
927 CopyGVAttributes(NewDF, SF);
928
929 // If the LLVM runtime renamed the function, but it is an externally
930 // visible symbol, DF must be an existing function with internal linkage.
931 // Rename it.
932 if (!NewDF->hasInternalLinkage() && NewDF->getName() != SF->getName())
933 ForceRenaming(NewDF, SF->getName());
934
935 // ... and remember this mapping...
936 ValueMap[SF] = NewDF;
937 continue;
Chris Lattner910c6142008-07-14 07:23:24 +0000938 }
939
940 // If the visibilities of the symbols disagree and the destination is a
941 // prototype, take the visibility of its input.
942 if (DGV->isDeclaration())
943 DGV->setVisibility(SF->getVisibility());
944
945 if (LinkFromSrc) {
946 if (isa<GlobalAlias>(DGV))
947 return Error(Err, "Function-Alias Collision on '" + SF->getName() +
948 "': symbol multiple defined");
949
950 // We have a definition of the same name but different type in the
951 // source module. Copy the prototype to the destination and replace
952 // uses of the destination's prototype with the new prototype.
953 Function *NewDF = Function::Create(SF->getFunctionType(), NewLinkage,
954 SF->getName(), Dest);
955 CopyGVAttributes(NewDF, SF);
956
957 // Any uses of DF need to change to NewDF, with cast
958 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType()));
959
960 // DF will conflict with NewDF because they both had the same. We must
961 // erase this now so ForceRenaming doesn't assert because DF might
962 // not have internal linkage.
963 if (GlobalVariable *Var = dyn_cast<GlobalVariable>(DGV))
964 Var->eraseFromParent();
965 else
966 cast<Function>(DGV)->eraseFromParent();
967
968 // If the symbol table renamed the function, but it is an externally
969 // visible symbol, DF must be an existing function with internal
970 // linkage. Rename it.
971 if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage())
972 ForceRenaming(NewDF, SF->getName());
973
974 // Remember this mapping so uses in the source module get remapped
975 // later by RemapOperand.
976 ValueMap[SF] = NewDF;
977 continue;
978 }
979
980 // Not "link from source", keep the one in the DestModule and remap the
981 // input onto it.
982
983 if (isa<GlobalAlias>(DGV)) {
Anton Korobeynikovcfc59112008-07-05 23:33:22 +0000984 // The only valid mappings are:
985 // - SF is external declaration, which is effectively a no-op.
986 // - SF is weak, when we just need to throw SF out.
Chris Lattner910c6142008-07-14 07:23:24 +0000987 if (!SF->isDeclaration())
Anton Korobeynikovfdeba112008-07-05 23:03:21 +0000988 return Error(Err, "Function-Alias Collision on '" + SF->getName() +
989 "': symbol multiple defined");
Chris Lattner1426bfa2008-06-09 07:36:11 +0000990 }
Anton Korobeynikovfdeba112008-07-05 23:03:21 +0000991
Chris Lattner910c6142008-07-14 07:23:24 +0000992 // Set calculated linkage
993 DGV->setLinkage(NewLinkage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994
Chris Lattner910c6142008-07-14 07:23:24 +0000995 // Make sure to remember this mapping.
996 ValueMap[SF] = ConstantExpr::getBitCast(DGV, SF->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997 }
998 return false;
999}
1000
1001// LinkFunctionBody - Copy the source function over into the dest function and
1002// fix up references to values. At this point we know that Dest is an external
1003// function, and that Src is not.
1004static bool LinkFunctionBody(Function *Dest, Function *Src,
1005 std::map<const Value*, Value*> &ValueMap,
1006 std::string *Err) {
1007 assert(Src && Dest && Dest->isDeclaration() && !Src->isDeclaration());
1008
1009 // Go through and convert function arguments over, remembering the mapping.
1010 Function::arg_iterator DI = Dest->arg_begin();
1011 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
1012 I != E; ++I, ++DI) {
Owen Andersonab567f82008-04-14 17:38:21 +00001013 DI->setName(I->getName()); // Copy the name information over...
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014
1015 // Add a mapping to our local map
Anton Korobeynikovef1eb5e2008-03-10 22:36:08 +00001016 ValueMap[I] = DI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001017 }
1018
1019 // Splice the body of the source function into the dest function.
1020 Dest->getBasicBlockList().splice(Dest->end(), Src->getBasicBlockList());
1021
1022 // At this point, all of the instructions and values of the function are now
1023 // copied over. The only problem is that they are still referencing values in
1024 // the Source function as operands. Loop through all of the operands of the
1025 // functions and patch them up to point to the local versions...
1026 //
1027 for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
1028 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1029 for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
1030 OI != OE; ++OI)
1031 if (!isa<Instruction>(*OI) && !isa<BasicBlock>(*OI))
1032 *OI = RemapOperand(*OI, ValueMap);
1033
1034 // There is no need to map the arguments anymore.
1035 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
1036 I != E; ++I)
1037 ValueMap.erase(I);
1038
1039 return false;
1040}
1041
1042
1043// LinkFunctionBodies - Link in the function bodies that are defined in the
1044// source module into the DestModule. This consists basically of copying the
1045// function over and fixing up references to values.
1046static bool LinkFunctionBodies(Module *Dest, Module *Src,
1047 std::map<const Value*, Value*> &ValueMap,
1048 std::string *Err) {
1049
1050 // Loop over all of the functions in the src module, mapping them over as we
1051 // go
1052 for (Module::iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF) {
1053 if (!SF->isDeclaration()) { // No body if function is external
Chris Lattnera518cc92008-06-20 05:29:39 +00001054 Function *DF = dyn_cast<Function>(ValueMap[SF]); // Destination function
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055
1056 // DF not external SF external?
Chris Lattnera518cc92008-06-20 05:29:39 +00001057 if (DF && DF->isDeclaration())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 // Only provide the function body if there isn't one already.
1059 if (LinkFunctionBody(DF, SF, ValueMap, Err))
1060 return true;
1061 }
1062 }
1063 return false;
1064}
1065
1066// LinkAppendingVars - If there were any appending global variables, link them
1067// together now. Return true on error.
1068static bool LinkAppendingVars(Module *M,
1069 std::multimap<std::string, GlobalVariable *> &AppendingVars,
1070 std::string *ErrorMsg) {
1071 if (AppendingVars.empty()) return false; // Nothing to do.
1072
1073 // Loop over the multimap of appending vars, processing any variables with the
1074 // same name, forming a new appending global variable with both of the
1075 // initializers merged together, then rewrite references to the old variables
1076 // and delete them.
1077 std::vector<Constant*> Inits;
1078 while (AppendingVars.size() > 1) {
1079 // Get the first two elements in the map...
1080 std::multimap<std::string,
1081 GlobalVariable*>::iterator Second = AppendingVars.begin(), First=Second++;
1082
1083 // If the first two elements are for different names, there is no pair...
1084 // Otherwise there is a pair, so link them together...
1085 if (First->first == Second->first) {
1086 GlobalVariable *G1 = First->second, *G2 = Second->second;
1087 const ArrayType *T1 = cast<ArrayType>(G1->getType()->getElementType());
1088 const ArrayType *T2 = cast<ArrayType>(G2->getType()->getElementType());
1089
1090 // Check to see that they two arrays agree on type...
1091 if (T1->getElementType() != T2->getElementType())
1092 return Error(ErrorMsg,
1093 "Appending variables with different element types need to be linked!");
1094 if (G1->isConstant() != G2->isConstant())
1095 return Error(ErrorMsg,
1096 "Appending variables linked with different const'ness!");
1097
1098 if (G1->getAlignment() != G2->getAlignment())
1099 return Error(ErrorMsg,
1100 "Appending variables with different alignment need to be linked!");
1101
1102 if (G1->getVisibility() != G2->getVisibility())
1103 return Error(ErrorMsg,
1104 "Appending variables with different visibility need to be linked!");
1105
1106 if (G1->getSection() != G2->getSection())
1107 return Error(ErrorMsg,
1108 "Appending variables with different section name need to be linked!");
1109
1110 unsigned NewSize = T1->getNumElements() + T2->getNumElements();
1111 ArrayType *NewType = ArrayType::get(T1->getElementType(), NewSize);
1112
1113 G1->setName(""); // Clear G1's name in case of a conflict!
1114
1115 // Create the new global variable...
1116 GlobalVariable *NG =
1117 new GlobalVariable(NewType, G1->isConstant(), G1->getLinkage(),
Chris Lattner29ae6c52008-06-27 03:10:24 +00001118 /*init*/0, First->first, M, G1->isThreadLocal(),
1119 G1->getType()->getAddressSpace());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001120
1121 // Propagate alignment, visibility and section info.
1122 CopyGVAttributes(NG, G1);
1123
1124 // Merge the initializer...
1125 Inits.reserve(NewSize);
1126 if (ConstantArray *I = dyn_cast<ConstantArray>(G1->getInitializer())) {
1127 for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
1128 Inits.push_back(I->getOperand(i));
1129 } else {
1130 assert(isa<ConstantAggregateZero>(G1->getInitializer()));
1131 Constant *CV = Constant::getNullValue(T1->getElementType());
1132 for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
1133 Inits.push_back(CV);
1134 }
1135 if (ConstantArray *I = dyn_cast<ConstantArray>(G2->getInitializer())) {
1136 for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
1137 Inits.push_back(I->getOperand(i));
1138 } else {
1139 assert(isa<ConstantAggregateZero>(G2->getInitializer()));
1140 Constant *CV = Constant::getNullValue(T2->getElementType());
1141 for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
1142 Inits.push_back(CV);
1143 }
1144 NG->setInitializer(ConstantArray::get(NewType, Inits));
1145 Inits.clear();
1146
1147 // Replace any uses of the two global variables with uses of the new
1148 // global...
1149
1150 // FIXME: This should rewrite simple/straight-forward uses such as
1151 // getelementptr instructions to not use the Cast!
1152 G1->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G1->getType()));
1153 G2->replaceAllUsesWith(ConstantExpr::getBitCast(NG, G2->getType()));
1154
1155 // Remove the two globals from the module now...
1156 M->getGlobalList().erase(G1);
1157 M->getGlobalList().erase(G2);
1158
1159 // Put the new global into the AppendingVars map so that we can handle
1160 // linking of more than two vars...
1161 Second->second = NG;
1162 }
1163 AppendingVars.erase(First);
1164 }
1165
1166 return false;
1167}
1168
Anton Korobeynikovfdad2d82008-03-05 23:21:39 +00001169static bool ResolveAliases(Module *Dest) {
1170 for (Module::alias_iterator I = Dest->alias_begin(), E = Dest->alias_end();
Anton Korobeynikov82192622008-03-11 22:51:09 +00001171 I != E; ++I)
Anton Korobeynikovc7b90912008-09-09 20:05:04 +00001172 if (const GlobalValue *GV = I->resolveAliasedGlobal())
Anton Korobeynikov94352452008-09-09 18:23:48 +00001173 if (GV != I && !GV->isDeclaration())
Anton Korobeynikov82192622008-03-11 22:51:09 +00001174 I->replaceAllUsesWith(const_cast<GlobalValue*>(GV));
Anton Korobeynikovfdad2d82008-03-05 23:21:39 +00001175
1176 return false;
1177}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001178
1179// LinkModules - This function links two modules together, with the resulting
1180// left module modified to be the composite of the two input modules. If an
1181// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
1182// the problem. Upon failure, the Dest module could be in a modified state, and
1183// shouldn't be relied on to be consistent.
1184bool
1185Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) {
1186 assert(Dest != 0 && "Invalid Destination module");
1187 assert(Src != 0 && "Invalid Source Module");
1188
1189 if (Dest->getDataLayout().empty()) {
1190 if (!Src->getDataLayout().empty()) {
1191 Dest->setDataLayout(Src->getDataLayout());
1192 } else {
1193 std::string DataLayout;
1194
Anton Korobeynikovfb782ce2008-02-20 11:27:04 +00001195 if (Dest->getEndianness() == Module::AnyEndianness) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001196 if (Src->getEndianness() == Module::BigEndian)
1197 DataLayout.append("E");
1198 else if (Src->getEndianness() == Module::LittleEndian)
1199 DataLayout.append("e");
Anton Korobeynikovfb782ce2008-02-20 11:27:04 +00001200 }
1201
1202 if (Dest->getPointerSize() == Module::AnyPointerSize) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001203 if (Src->getPointerSize() == Module::Pointer64)
1204 DataLayout.append(DataLayout.length() == 0 ? "p:64:64" : "-p:64:64");
1205 else if (Src->getPointerSize() == Module::Pointer32)
1206 DataLayout.append(DataLayout.length() == 0 ? "p:32:32" : "-p:32:32");
Anton Korobeynikovfb782ce2008-02-20 11:27:04 +00001207 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001208 Dest->setDataLayout(DataLayout);
1209 }
1210 }
1211
Chris Lattner85dd49c2008-02-19 18:49:08 +00001212 // Copy the target triple from the source to dest if the dest's is empty.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001213 if (Dest->getTargetTriple().empty() && !Src->getTargetTriple().empty())
1214 Dest->setTargetTriple(Src->getTargetTriple());
1215
1216 if (!Src->getDataLayout().empty() && !Dest->getDataLayout().empty() &&
1217 Src->getDataLayout() != Dest->getDataLayout())
1218 cerr << "WARNING: Linking two modules of different data layouts!\n";
1219 if (!Src->getTargetTriple().empty() &&
1220 Dest->getTargetTriple() != Src->getTargetTriple())
1221 cerr << "WARNING: Linking two modules of different target triples!\n";
1222
Chris Lattner85dd49c2008-02-19 18:49:08 +00001223 // Append the module inline asm string.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001224 if (!Src->getModuleInlineAsm().empty()) {
1225 if (Dest->getModuleInlineAsm().empty())
1226 Dest->setModuleInlineAsm(Src->getModuleInlineAsm());
1227 else
1228 Dest->setModuleInlineAsm(Dest->getModuleInlineAsm()+"\n"+
1229 Src->getModuleInlineAsm());
1230 }
1231
1232 // Update the destination module's dependent libraries list with the libraries
1233 // from the source module. There's no opportunity for duplicates here as the
1234 // Module ensures that duplicate insertions are discarded.
Chris Lattner85dd49c2008-02-19 18:49:08 +00001235 for (Module::lib_iterator SI = Src->lib_begin(), SE = Src->lib_end();
1236 SI != SE; ++SI)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001237 Dest->addLibrary(*SI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001238
1239 // LinkTypes - Go through the symbol table of the Src module and see if any
1240 // types are named in the src module that are not named in the Dst module.
1241 // Make sure there are no type name conflicts.
1242 if (LinkTypes(Dest, Src, ErrorMsg))
1243 return true;
1244
1245 // ValueMap - Mapping of values from what they used to be in Src, to what they
1246 // are now in Dest.
1247 std::map<const Value*, Value*> ValueMap;
1248
1249 // AppendingVars - Keep track of global variables in the destination module
1250 // with appending linkage. After the module is linked together, they are
1251 // appended and the module is rewritten.
1252 std::multimap<std::string, GlobalVariable *> AppendingVars;
1253 for (Module::global_iterator I = Dest->global_begin(), E = Dest->global_end();
1254 I != E; ++I) {
1255 // Add all of the appending globals already in the Dest module to
1256 // AppendingVars.
1257 if (I->hasAppendingLinkage())
1258 AppendingVars.insert(std::make_pair(I->getName(), I));
1259 }
1260
1261 // Insert all of the globals in src into the Dest module... without linking
1262 // initializers (which could refer to functions not yet mapped over).
1263 if (LinkGlobals(Dest, Src, ValueMap, AppendingVars, ErrorMsg))
1264 return true;
1265
1266 // Link the functions together between the two modules, without doing function
1267 // bodies... this just adds external function prototypes to the Dest
1268 // function... We do this so that when we begin processing function bodies,
1269 // all of the global values that may be referenced are available in our
1270 // ValueMap.
1271 if (LinkFunctionProtos(Dest, Src, ValueMap, ErrorMsg))
1272 return true;
1273
Anton Korobeynikov3cfecfd2008-03-05 15:27:21 +00001274 // If there were any alias, link them now. We really need to do this now,
1275 // because all of the aliases that may be referenced need to be available in
1276 // ValueMap
1277 if (LinkAlias(Dest, Src, ValueMap, ErrorMsg)) return true;
1278
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001279 // Update the initializers in the Dest module now that all globals that may
1280 // be referenced are in Dest.
1281 if (LinkGlobalInits(Dest, Src, ValueMap, ErrorMsg)) return true;
1282
1283 // Link in the function bodies that are defined in the source module into the
1284 // DestModule. This consists basically of copying the function over and
1285 // fixing up references to values.
1286 if (LinkFunctionBodies(Dest, Src, ValueMap, ErrorMsg)) return true;
1287
1288 // If there were any appending global variables, link them together now.
1289 if (LinkAppendingVars(Dest, AppendingVars, ErrorMsg)) return true;
1290
Anton Korobeynikova68796c2008-03-05 23:08:47 +00001291 // Resolve all uses of aliases with aliasees
1292 if (ResolveAliases(Dest)) return true;
1293
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001294 // If the source library's module id is in the dependent library list of the
1295 // destination library, remove it since that module is now linked in.
1296 sys::Path modId;
1297 modId.set(Src->getModuleIdentifier());
1298 if (!modId.isEmpty())
1299 Dest->removeLibrary(modId.getBasename());
1300
1301 return false;
1302}
1303
1304// vim: sw=2