blob: a6a26be6a44fbdc94b9eab35f5c7b442284efd76 [file] [log] [blame]
Mikhail Glushenkov59a5afa2009-03-03 10:04:23 +00001//===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Reid Spencer361e5132004-11-12 20:37:43 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman10468d82005-04-21 22:55:34 +00007//
Reid Spencer361e5132004-11-12 20:37:43 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVM module linker.
11//
Reid Spencer361e5132004-11-12 20:37:43 +000012//===----------------------------------------------------------------------===//
13
Chandler Carruth6cc07df2014-03-06 03:42:23 +000014#include "llvm/Linker/Linker.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm-c/Linker.h"
Bill Wendling66f02412012-02-11 11:38:06 +000016#include "llvm/ADT/SetVector.h"
Eli Bendersky0f7fd362013-03-19 15:26:24 +000017#include "llvm/ADT/SmallString.h"
Akira Hatanakac43df512015-02-13 00:40:41 +000018#include "llvm/ADT/Triple.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
Rafael Espindolad12b4a32014-10-25 04:06:10 +000020#include "llvm/IR/DiagnosticInfo.h"
21#include "llvm/IR/DiagnosticPrinter.h"
22#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Module.h"
Chandler Carruthdcb603f2013-01-07 15:43:51 +000024#include "llvm/IR/TypeFinder.h"
Tanya Lattnercbb91402011-10-11 00:24:54 +000025#include "llvm/Transforms/Utils/Cloning.h"
Reid Spencer361e5132004-11-12 20:37:43 +000026using namespace llvm;
27
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000028//===----------------------------------------------------------------------===//
29// TypeMap implementation.
30//===----------------------------------------------------------------------===//
Reid Spencer361e5132004-11-12 20:37:43 +000031
Chris Lattnereee6f992008-06-16 21:00:18 +000032namespace {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000033class TypeMapTy : public ValueMapTypeRemapper {
Rafael Espindola18c89412014-10-27 02:35:46 +000034 /// This is a mapping from a source type to a destination type to use.
Rafael Espindolae3a933a2015-12-01 20:11:43 +000035 DenseMap<Type *, Type *> MappedTypes;
Mikhail Glushenkov766d4892009-03-03 07:22:23 +000036
Rafael Espindola18c89412014-10-27 02:35:46 +000037 /// When checking to see if two subgraphs are isomorphic, we speculatively
38 /// add types to MappedTypes, but keep track of them here in case we need to
39 /// roll back.
Rafael Espindolae3a933a2015-12-01 20:11:43 +000040 SmallVector<Type *, 16> SpeculativeTypes;
Rafael Espindolaed6dc372014-05-09 14:39:25 +000041
Rafael Espindolae3a933a2015-12-01 20:11:43 +000042 SmallVector<StructType *, 16> SpeculativeDstOpaqueTypes;
Rafael Espindolaa96f2352014-11-28 16:41:24 +000043
Rafael Espindola18c89412014-10-27 02:35:46 +000044 /// This is a list of non-opaque structs in the source module that are mapped
45 /// to an opaque struct in the destination module.
Rafael Espindolae3a933a2015-12-01 20:11:43 +000046 SmallVector<StructType *, 16> SrcDefinitionsToResolve;
Rafael Espindolaed6dc372014-05-09 14:39:25 +000047
Rafael Espindola18c89412014-10-27 02:35:46 +000048 /// This is the set of opaque types in the destination modules who are
49 /// getting a body from the source module.
Rafael Espindolae3a933a2015-12-01 20:11:43 +000050 SmallPtrSet<StructType *, 16> DstResolvedOpaqueTypes;
Bill Wendling8c2cc412012-03-22 20:30:41 +000051
Chris Lattner56cdea62008-06-16 23:06:51 +000052public:
Rafael Espindola31ad4682014-12-03 22:36:37 +000053 TypeMapTy(Linker::IdentifiedStructTypeSet &DstStructTypesSet)
54 : DstStructTypesSet(DstStructTypesSet) {}
Rafael Espindolaaa9918a2013-05-04 05:05:18 +000055
Rafael Espindola31ad4682014-12-03 22:36:37 +000056 Linker::IdentifiedStructTypeSet &DstStructTypesSet;
Rafael Espindola18c89412014-10-27 02:35:46 +000057 /// Indicate that the specified type in the destination module is conceptually
58 /// equivalent to the specified type in the source module.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000059 void addTypeMapping(Type *DstTy, Type *SrcTy);
60
Rafael Espindolad2a13a22014-11-25 04:28:31 +000061 /// Produce a body for an opaque type in the dest module from a type
62 /// definition in the source module.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000063 void linkDefinedTypeBodies();
Rafael Espindolaed6dc372014-05-09 14:39:25 +000064
Rafael Espindola18c89412014-10-27 02:35:46 +000065 /// Return the mapped type to use for the specified input type from the
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000066 /// source module.
67 Type *get(Type *SrcTy);
Rafael Espindola31ad4682014-12-03 22:36:37 +000068 Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited);
69
70 void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000071
Rafael Espindola290e2cc2014-11-25 14:35:53 +000072 FunctionType *get(FunctionType *T) {
73 return cast<FunctionType>(get((Type *)T));
74 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000075
Rafael Espindola18c89412014-10-27 02:35:46 +000076 /// Dump out the type map for debugging purposes.
Bill Wendlingb6af2f32012-03-22 20:28:27 +000077 void dump() const {
Rafael Espindola8f144712014-11-25 06:16:27 +000078 for (auto &Pair : MappedTypes) {
Bill Wendlingb6af2f32012-03-22 20:28:27 +000079 dbgs() << "TypeMap: ";
Rafael Espindola8f144712014-11-25 06:16:27 +000080 Pair.first->print(dbgs());
Bill Wendlingb6af2f32012-03-22 20:28:27 +000081 dbgs() << " => ";
Rafael Espindola8f144712014-11-25 06:16:27 +000082 Pair.second->print(dbgs());
Bill Wendlingb6af2f32012-03-22 20:28:27 +000083 dbgs() << '\n';
84 }
85 }
Bill Wendlingb6af2f32012-03-22 20:28:27 +000086
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000087private:
Rafael Espindola290e2cc2014-11-25 14:35:53 +000088 Type *remapType(Type *SrcTy) override { return get(SrcTy); }
Rafael Espindolaed6dc372014-05-09 14:39:25 +000089
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000090 bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
Chris Lattnereee6f992008-06-16 21:00:18 +000091};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000092}
Chris Lattnereee6f992008-06-16 21:00:18 +000093
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000094void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
Rafael Espindola3d097412014-11-28 16:26:14 +000095 assert(SpeculativeTypes.empty());
Rafael Espindolaa96f2352014-11-28 16:41:24 +000096 assert(SpeculativeDstOpaqueTypes.empty());
Rafael Espindola3d097412014-11-28 16:26:14 +000097
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000098 // Check to see if these types are recursively isomorphic and establish a
99 // mapping between them if so.
Duncan P. N. Exon Smithc586eaa2014-11-27 17:01:10 +0000100 if (!areTypesIsomorphic(DstTy, SrcTy)) {
101 // Oops, they aren't isomorphic. Just discard this request by rolling out
102 // any speculative mappings we've established.
Rafael Espindola3d097412014-11-28 16:26:14 +0000103 for (Type *Ty : SpeculativeTypes)
104 MappedTypes.erase(Ty);
Rafael Espindolaa96f2352014-11-28 16:41:24 +0000105
106 SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() -
107 SpeculativeDstOpaqueTypes.size());
108 for (StructType *Ty : SpeculativeDstOpaqueTypes)
109 DstResolvedOpaqueTypes.erase(Ty);
Rafael Espindola04a74af2014-12-01 04:15:59 +0000110 } else {
111 for (Type *Ty : SpeculativeTypes)
112 if (auto *STy = dyn_cast<StructType>(Ty))
113 if (STy->hasName())
114 STy->setName("");
Bill Wendlingd48b7782012-02-28 04:01:21 +0000115 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000116 SpeculativeTypes.clear();
Rafael Espindolaa96f2352014-11-28 16:41:24 +0000117 SpeculativeDstOpaqueTypes.clear();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000118}
Chris Lattnereee6f992008-06-16 21:00:18 +0000119
Rafael Espindola18c89412014-10-27 02:35:46 +0000120/// Recursively walk this pair of types, returning true if they are isomorphic,
121/// false if they are not.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000122bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
123 // Two types with differing kinds are clearly not isomorphic.
Rafael Espindola290e2cc2014-11-25 14:35:53 +0000124 if (DstTy->getTypeID() != SrcTy->getTypeID())
125 return false;
Misha Brukman10468d82005-04-21 22:55:34 +0000126
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000127 // If we have an entry in the MappedTypes table, then we have our answer.
128 Type *&Entry = MappedTypes[SrcTy];
129 if (Entry)
130 return Entry == DstTy;
Misha Brukman10468d82005-04-21 22:55:34 +0000131
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000132 // Two identical types are clearly isomorphic. Remember this
133 // non-speculatively.
134 if (DstTy == SrcTy) {
135 Entry = DstTy;
Chris Lattnerfe677e92008-06-16 20:03:01 +0000136 return true;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000137 }
Rafael Espindolaed6dc372014-05-09 14:39:25 +0000138
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000139 // Okay, we have two types with identical kinds that we haven't seen before.
Mikhail Glushenkov766d4892009-03-03 07:22:23 +0000140
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000141 // If this is an opaque struct type, special case it.
142 if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
143 // Mapping an opaque type to any struct, just keep the dest struct.
144 if (SSTy->isOpaque()) {
145 Entry = DstTy;
146 SpeculativeTypes.push_back(SrcTy);
Reid Spencer361e5132004-11-12 20:37:43 +0000147 return true;
Chris Lattner9be15892008-06-16 21:17:12 +0000148 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000149
Chris Lattner5e3bd972011-12-20 00:03:52 +0000150 // Mapping a non-opaque source type to an opaque dest. If this is the first
151 // type that we're mapping onto this destination type then we succeed. Keep
Rafael Espindolaa96f2352014-11-28 16:41:24 +0000152 // the dest, but fill it in later. If this is the second (different) type
153 // that we're trying to map onto the same opaque type then we fail.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000154 if (cast<StructType>(DstTy)->isOpaque()) {
Chris Lattner5e3bd972011-12-20 00:03:52 +0000155 // We can only map one source type onto the opaque destination type.
David Blaikie70573dc2014-11-19 07:49:26 +0000156 if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)
Chris Lattner5e3bd972011-12-20 00:03:52 +0000157 return false;
158 SrcDefinitionsToResolve.push_back(SSTy);
Rafael Espindolaa96f2352014-11-28 16:41:24 +0000159 SpeculativeTypes.push_back(SrcTy);
160 SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000161 Entry = DstTy;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000162 return true;
163 }
164 }
Rafael Espindolaed6dc372014-05-09 14:39:25 +0000165
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000166 // If the number of subtypes disagree between the two types, then we fail.
167 if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
Reid Spencer361e5132004-11-12 20:37:43 +0000168 return false;
Rafael Espindolaed6dc372014-05-09 14:39:25 +0000169
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000170 // Fail if any of the extra properties (e.g. array size) of the type disagree.
171 if (isa<IntegerType>(DstTy))
Rafael Espindolae3a933a2015-12-01 20:11:43 +0000172 return false; // bitwidth disagrees.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000173 if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
174 if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
175 return false;
Rafael Espindolaed6dc372014-05-09 14:39:25 +0000176
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000177 } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
178 if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
179 return false;
180 } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
181 StructType *SSTy = cast<StructType>(SrcTy);
Chris Lattner44f7ab42011-08-12 18:07:26 +0000182 if (DSTy->isLiteral() != SSTy->isLiteral() ||
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000183 DSTy->isPacked() != SSTy->isPacked())
184 return false;
185 } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
186 if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
187 return false;
188 } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Joey Gouly5fad3e92013-01-10 10:49:36 +0000189 if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000190 return false;
Reid Spencer361e5132004-11-12 20:37:43 +0000191 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000192
193 // Otherwise, we speculate that these two types will line up and recursively
194 // check the subelements.
195 Entry = DstTy;
196 SpeculativeTypes.push_back(SrcTy);
197
Rafael Espindola290e2cc2014-11-25 14:35:53 +0000198 for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I)
199 if (!areTypesIsomorphic(DstTy->getContainedType(I),
200 SrcTy->getContainedType(I)))
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000201 return false;
Rafael Espindolaed6dc372014-05-09 14:39:25 +0000202
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000203 // If everything seems to have lined up, then everything is great.
204 return true;
205}
206
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000207void TypeMapTy::linkDefinedTypeBodies() {
Rafael Espindolae3a933a2015-12-01 20:11:43 +0000208 SmallVector<Type *, 16> Elements;
Rafael Espindolac81c3f52014-11-25 15:33:40 +0000209 for (StructType *SrcSTy : SrcDefinitionsToResolve) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000210 StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
Rafael Espindolac81c3f52014-11-25 15:33:40 +0000211 assert(DstSTy->isOpaque());
Rafael Espindolaed6dc372014-05-09 14:39:25 +0000212
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000213 // Map the body of the source type over to a new body for the dest type.
214 Elements.resize(SrcSTy->getNumElements());
Rafael Espindola290e2cc2014-11-25 14:35:53 +0000215 for (unsigned I = 0, E = Elements.size(); I != E; ++I)
Rafael Espindolac81c3f52014-11-25 15:33:40 +0000216 Elements[I] = get(SrcSTy->getElementType(I));
Rafael Espindolaed6dc372014-05-09 14:39:25 +0000217
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000218 DstSTy->setBody(Elements, SrcSTy->isPacked());
Rafael Espindolaa5b9e1c2015-03-06 00:50:21 +0000219 DstStructTypesSet.switchToNonOpaque(DstSTy);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000220 }
Rafael Espindolac81c3f52014-11-25 15:33:40 +0000221 SrcDefinitionsToResolve.clear();
Chris Lattner5e3bd972011-12-20 00:03:52 +0000222 DstResolvedOpaqueTypes.clear();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000223}
224
Rafael Espindola31ad4682014-12-03 22:36:37 +0000225void TypeMapTy::finishType(StructType *DTy, StructType *STy,
226 ArrayRef<Type *> ETypes) {
227 DTy->setBody(ETypes, STy->isPacked());
Rafael Espindolac81c3f52014-11-25 15:33:40 +0000228
229 // Steal STy's name.
230 if (STy->hasName()) {
231 SmallString<16> TmpName = STy->getName();
232 STy->setName("");
233 DTy->setName(TmpName);
234 }
235
Rafael Espindola31ad4682014-12-03 22:36:37 +0000236 DstStructTypesSet.addNonOpaque(DTy);
237}
238
239Type *TypeMapTy::get(Type *Ty) {
240 SmallPtrSet<StructType *, 8> Visited;
241 return get(Ty, Visited);
242}
243
244Type *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) {
245 // If we already have an entry for this type, return it.
246 Type **Entry = &MappedTypes[Ty];
247 if (*Entry)
248 return *Entry;
249
250 // These are types that LLVM itself will unique.
251 bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral();
252
253#ifndef NDEBUG
254 if (!IsUniqued) {
255 for (auto &Pair : MappedTypes) {
256 assert(!(Pair.first != Ty && Pair.second == Ty) &&
257 "mapping to a source type");
258 }
259 }
260#endif
261
262 if (!IsUniqued && !Visited.insert(cast<StructType>(Ty)).second) {
263 StructType *DTy = StructType::create(Ty->getContext());
264 return *Entry = DTy;
265 }
266
267 // If this is not a recursive type, then just map all of the elements and
268 // then rebuild the type from inside out.
269 SmallVector<Type *, 4> ElementTypes;
270
271 // If there are no element types to map, then the type is itself. This is
272 // true for the anonymous {} struct, things like 'float', integers, etc.
273 if (Ty->getNumContainedTypes() == 0 && IsUniqued)
274 return *Entry = Ty;
275
276 // Remap all of the elements, keeping track of whether any of them change.
277 bool AnyChange = false;
278 ElementTypes.resize(Ty->getNumContainedTypes());
279 for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
280 ElementTypes[I] = get(Ty->getContainedType(I), Visited);
281 AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
282 }
283
284 // If we found our type while recursively processing stuff, just use it.
285 Entry = &MappedTypes[Ty];
286 if (*Entry) {
287 if (auto *DTy = dyn_cast<StructType>(*Entry)) {
288 if (DTy->isOpaque()) {
289 auto *STy = cast<StructType>(Ty);
290 finishType(DTy, STy, ElementTypes);
291 }
292 }
293 return *Entry;
294 }
295
296 // If all of the element types mapped directly over and the type is not
297 // a nomed struct, then the type is usable as-is.
298 if (!AnyChange && IsUniqued)
299 return *Entry = Ty;
300
301 // Otherwise, rebuild a modified type.
302 switch (Ty->getTypeID()) {
303 default:
304 llvm_unreachable("unknown derived type to remap");
305 case Type::ArrayTyID:
306 return *Entry = ArrayType::get(ElementTypes[0],
307 cast<ArrayType>(Ty)->getNumElements());
308 case Type::VectorTyID:
309 return *Entry = VectorType::get(ElementTypes[0],
310 cast<VectorType>(Ty)->getNumElements());
311 case Type::PointerTyID:
312 return *Entry = PointerType::get(ElementTypes[0],
313 cast<PointerType>(Ty)->getAddressSpace());
314 case Type::FunctionTyID:
315 return *Entry = FunctionType::get(ElementTypes[0],
316 makeArrayRef(ElementTypes).slice(1),
317 cast<FunctionType>(Ty)->isVarArg());
318 case Type::StructTyID: {
319 auto *STy = cast<StructType>(Ty);
320 bool IsPacked = STy->isPacked();
321 if (IsUniqued)
322 return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);
323
324 // If the type is opaque, we can just use it directly.
325 if (STy->isOpaque()) {
326 DstStructTypesSet.addOpaque(STy);
327 return *Entry = Ty;
328 }
329
330 if (StructType *OldT =
331 DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {
332 STy->setName("");
333 return *Entry = OldT;
334 }
335
336 if (!AnyChange) {
337 DstStructTypesSet.addNonOpaque(STy);
338 return *Entry = Ty;
339 }
340
341 StructType *DTy = StructType::create(Ty->getContext());
342 finishType(DTy, STy, ElementTypes);
343 return *Entry = DTy;
344 }
345 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000346}
347
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000348//===----------------------------------------------------------------------===//
349// ModuleLinker implementation.
350//===----------------------------------------------------------------------===//
351
352namespace {
Rafael Espindolac84f6082014-11-25 06:11:24 +0000353class ModuleLinker;
James Molloyf6f121e2013-05-28 15:17:05 +0000354
Rafael Espindolac84f6082014-11-25 06:11:24 +0000355/// Creates prototypes for functions that are lazily linked on the fly. This
356/// speeds up linking for modules with many/ lazily linked functions of which
357/// few get used.
David Blaikie437aafd2015-10-19 22:15:55 +0000358class ValueMaterializerTy final : public ValueMaterializer {
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000359 ModuleLinker *ModLinker;
James Molloyf6f121e2013-05-28 15:17:05 +0000360
Rafael Espindolac84f6082014-11-25 06:11:24 +0000361public:
Rafael Espindola19b52382015-11-27 20:28:19 +0000362 ValueMaterializerTy(ModuleLinker *ModLinker) : ModLinker(ModLinker) {}
Rafael Espindolac84f6082014-11-25 06:11:24 +0000363
Rafael Espindola19b52382015-11-27 20:28:19 +0000364 Value *materializeDeclFor(Value *V) override;
365 void materializeInitFor(GlobalValue *New, GlobalValue *Old) override;
Rafael Espindolac84f6082014-11-25 06:11:24 +0000366};
367
368class LinkDiagnosticInfo : public DiagnosticInfo {
369 const Twine &Msg;
370
371public:
372 LinkDiagnosticInfo(DiagnosticSeverity Severity, const Twine &Msg);
373 void print(DiagnosticPrinter &DP) const override;
374};
375LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity,
376 const Twine &Msg)
377 : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}
378void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
379
380/// This is an implementation class for the LinkModules function, which is the
381/// entrypoint for this file.
382class ModuleLinker {
Rafael Espindola0e309fe2015-12-01 19:50:54 +0000383 Module &DstM;
384 Module &SrcM;
Rafael Espindolac84f6082014-11-25 06:11:24 +0000385
386 TypeMapTy TypeMap;
387 ValueMaterializerTy ValMaterializer;
388
389 /// Mapping of values from what they used to be in Src, to what they are now
390 /// in DstM. ValueToValueMapTy is a ValueMap, which involves some overhead
391 /// due to the use of Value handles which the Linker doesn't actually need,
392 /// but this allows us to reuse the ValueMapper code.
393 ValueToValueMapTy ValueMap;
394
Rafael Espindola4b5ec262015-12-02 22:59:04 +0000395 SetVector<GlobalValue *> ValuesToLink;
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000396
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000397 DiagnosticHandlerFunction DiagnosticHandler;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000398
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000399 /// For symbol clashes, prefer those from Src.
Artem Belevich020d4fb2015-09-01 17:55:55 +0000400 unsigned Flags;
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +0000401
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000402 /// Function index passed into ModuleLinker for using in function
403 /// importing/exporting handling.
Mehdi Amini8220e8a2015-11-23 01:59:16 +0000404 const FunctionInfoIndex *ImportIndex;
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000405
406 /// Function to import from source module, all other functions are
407 /// imported as declarations instead of definitions.
Mehdi Aminiffe2e4a2015-12-02 04:34:28 +0000408 DenseSet<const GlobalValue *> *ImportFunction;
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000409
410 /// Set to true if the given FunctionInfoIndex contains any functions
411 /// from this source module, in which case we must conservatively assume
412 /// that any of its functions may be imported into another module
413 /// as part of a different backend compilation process.
Rafael Espindolaaf714762015-12-01 23:06:26 +0000414 bool HasExportedFunctions = false;
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000415
Teresa Johnson10632932015-11-06 17:50:53 +0000416 /// Set to true when all global value body linking is complete (including
417 /// lazy linking). Used to prevent metadata linking from creating new
418 /// references.
Rafael Espindolaaf714762015-12-01 23:06:26 +0000419 bool DoneLinkingBodies = false;
Teresa Johnson10632932015-11-06 17:50:53 +0000420
Rafael Espindolabaa3bf82015-12-01 15:19:48 +0000421 bool HasError = false;
422
Rafael Espindolac84f6082014-11-25 06:11:24 +0000423public:
Rafael Espindola0e309fe2015-12-01 19:50:54 +0000424 ModuleLinker(Module &DstM, Linker::IdentifiedStructTypeSet &Set, Module &SrcM,
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000425 DiagnosticHandlerFunction DiagnosticHandler, unsigned Flags,
Mehdi Amini8220e8a2015-11-23 01:59:16 +0000426 const FunctionInfoIndex *Index = nullptr,
Mehdi Amini7471cf82015-12-03 02:37:30 +0000427 DenseSet<const GlobalValue *> *FunctionsToImport = nullptr)
Rafael Espindola0e309fe2015-12-01 19:50:54 +0000428 : DstM(DstM), SrcM(SrcM), TypeMap(Set), ValMaterializer(this),
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000429 DiagnosticHandler(DiagnosticHandler), Flags(Flags), ImportIndex(Index),
Mehdi Amini7471cf82015-12-03 02:37:30 +0000430 ImportFunction(FunctionsToImport) {
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000431 assert((ImportIndex || !ImportFunction) &&
432 "Expect a FunctionInfoIndex when importing");
433 // If we have a FunctionInfoIndex but no function to import,
434 // then this is the primary module being compiled in a ThinLTO
435 // backend compilation, and we need to see if it has functions that
436 // may be exported to another backend compilation.
437 if (ImportIndex && !ImportFunction)
Mehdi Amini9abe1082015-12-03 02:37:23 +0000438 HasExportedFunctions = ImportIndex->hasExportedFunctions(SrcM);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000439 }
Rafael Espindolaed6dc372014-05-09 14:39:25 +0000440
Rafael Espindolac84f6082014-11-25 06:11:24 +0000441 bool run();
Rafael Espindola19b52382015-11-27 20:28:19 +0000442 Value *materializeDeclFor(Value *V);
443 void materializeInitFor(GlobalValue *New, GlobalValue *Old);
Rafael Espindolaed6dc372014-05-09 14:39:25 +0000444
Rafael Espindola19b52382015-11-27 20:28:19 +0000445private:
Artem Belevich020d4fb2015-09-01 17:55:55 +0000446 bool shouldOverrideFromSrc() { return Flags & Linker::OverrideFromSrc; }
447 bool shouldLinkOnlyNeeded() { return Flags & Linker::LinkOnlyNeeded; }
448 bool shouldInternalizeLinkedSymbols() {
449 return Flags & Linker::InternalizeLinkedSymbols;
450 }
451
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000452 /// Handles cloning of a global values from the source module into
453 /// the destination module, including setting the attributes and visibility.
Rafael Espindola792b7952015-12-03 14:48:20 +0000454 GlobalValue *copyGlobalValueProto(const GlobalValue *SGV,
Rafael Espindolaf3518c92015-12-02 19:30:52 +0000455 const GlobalValue *DGV, bool ForDefinition);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000456
457 /// Check if we should promote the given local value to global scope.
458 bool doPromoteLocalToGlobal(const GlobalValue *SGV);
459
Rafael Espindolac84f6082014-11-25 06:11:24 +0000460 bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest,
461 const GlobalValue &Src);
Rafael Espindolaed6dc372014-05-09 14:39:25 +0000462
Rafael Espindolac84f6082014-11-25 06:11:24 +0000463 /// Helper method for setting a message and returning an error code.
464 bool emitError(const Twine &Message) {
465 DiagnosticHandler(LinkDiagnosticInfo(DS_Error, Message));
Rafael Espindolabaa3bf82015-12-01 15:19:48 +0000466 HasError = true;
Rafael Espindolac84f6082014-11-25 06:11:24 +0000467 return true;
468 }
Rafael Espindolaed6dc372014-05-09 14:39:25 +0000469
Rafael Espindolac84f6082014-11-25 06:11:24 +0000470 void emitWarning(const Twine &Message) {
471 DiagnosticHandler(LinkDiagnosticInfo(DS_Warning, Message));
472 }
Eli Bendersky7da92ed2014-02-20 22:19:24 +0000473
Rafael Espindola0e309fe2015-12-01 19:50:54 +0000474 bool getComdatLeader(Module &M, StringRef ComdatName,
Rafael Espindolac84f6082014-11-25 06:11:24 +0000475 const GlobalVariable *&GVar);
476 bool computeResultingSelectionKind(StringRef ComdatName,
477 Comdat::SelectionKind Src,
478 Comdat::SelectionKind Dst,
479 Comdat::SelectionKind &Result,
480 bool &LinkFromSrc);
481 std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>>
482 ComdatsChosen;
483 bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK,
484 bool &LinkFromSrc);
Teresa Johnson2d5fb8c2015-11-10 21:09:06 +0000485 // Keep track of the global value members of each comdat in source.
486 DenseMap<const Comdat *, std::vector<GlobalValue *>> ComdatMembers;
Rafael Espindola4160f5d2014-10-27 23:02:10 +0000487
Rafael Espindolac84f6082014-11-25 06:11:24 +0000488 /// Given a global in the source module, return the global in the
489 /// destination module that is being linked to, if any.
490 GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
491 // If the source has no name it can't link. If it has local linkage,
492 // there is no name match-up going on.
Teresa Johnsonb098f0c2015-11-24 19:46:58 +0000493 if (!SrcGV->hasName() || GlobalValue::isLocalLinkage(getLinkage(SrcGV)))
Rafael Espindolac84f6082014-11-25 06:11:24 +0000494 return nullptr;
Eli Bendersky7da92ed2014-02-20 22:19:24 +0000495
Rafael Espindolac84f6082014-11-25 06:11:24 +0000496 // Otherwise see if we have a match in the destination module's symtab.
Rafael Espindola0e309fe2015-12-01 19:50:54 +0000497 GlobalValue *DGV = DstM.getNamedValue(getName(SrcGV));
Rafael Espindolac84f6082014-11-25 06:11:24 +0000498 if (!DGV)
499 return nullptr;
Rafael Espindolaed6dc372014-05-09 14:39:25 +0000500
Rafael Espindolac84f6082014-11-25 06:11:24 +0000501 // If we found a global with the same name in the dest module, but it has
502 // internal linkage, we are really not doing any linkage here.
503 if (DGV->hasLocalLinkage())
504 return nullptr;
Rafael Espindoladbb0bd12014-09-09 15:21:00 +0000505
Rafael Espindolac84f6082014-11-25 06:11:24 +0000506 // Otherwise, we do in fact link to the destination global.
507 return DGV;
508 }
Rafael Espindolaed6dc372014-05-09 14:39:25 +0000509
Rafael Espindolac84f6082014-11-25 06:11:24 +0000510 void computeTypeMapping();
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000511
Rafael Espindolac84f6082014-11-25 06:11:24 +0000512 void upgradeMismatchedGlobalArray(StringRef Name);
513 void upgradeMismatchedGlobals();
David Majnemerdad0a642014-06-27 18:19:56 +0000514
Rafael Espindolabaa3bf82015-12-01 15:19:48 +0000515 bool linkIfNeeded(GlobalValue &GV);
Rafael Espindolac84f6082014-11-25 06:11:24 +0000516 bool linkAppendingVarProto(GlobalVariable *DstGV,
517 const GlobalVariable *SrcGV);
Rafael Espindolaed6dc372014-05-09 14:39:25 +0000518
Rafael Espindolac84f6082014-11-25 06:11:24 +0000519 bool linkGlobalValueProto(GlobalValue *GV);
Rafael Espindolac84f6082014-11-25 06:11:24 +0000520 bool linkModuleFlagsMetadata();
Mikhail Glushenkov766d4892009-03-03 07:22:23 +0000521
Rafael Espindolaef237112014-12-08 18:45:16 +0000522 void linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src);
523 bool linkFunctionBody(Function &Dst, Function &Src);
524 void linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src);
Rafael Espindolae39cd5b2015-12-01 22:40:40 +0000525 bool linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src);
Rafael Espindolaef237112014-12-08 18:45:16 +0000526
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000527 /// Functions that take care of cloning a specific global value type
528 /// into the destination module.
Rafael Espindola792b7952015-12-03 14:48:20 +0000529 GlobalVariable *copyGlobalVariableProto(const GlobalVariable *SGVar);
530 Function *copyFunctionProto(const Function *SF);
531 GlobalValue *copyGlobalAliasProto(const GlobalAlias *SGA);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000532
533 /// Helper methods to check if we are importing from or potentially
534 /// exporting from the current source module.
535 bool isPerformingImport() { return ImportFunction != nullptr; }
536 bool isModuleExporting() { return HasExportedFunctions; }
537
538 /// If we are importing from the source module, checks if we should
539 /// import SGV as a definition, otherwise import as a declaration.
540 bool doImportAsDefinition(const GlobalValue *SGV);
541
542 /// Get the name for SGV that should be used in the linked destination
543 /// module. Specifically, this handles the case where we need to rename
544 /// a local that is being promoted to global scope.
545 std::string getName(const GlobalValue *SGV);
546
547 /// Get the new linkage for SGV that should be used in the linked destination
548 /// module. Specifically, for ThinLTO importing or exporting it may need
549 /// to be adjusted.
550 GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV);
551
552 /// Copies the necessary global value attributes and name from the source
553 /// to the newly cloned global value.
554 void copyGVAttributes(GlobalValue *NewGV, const GlobalValue *SrcGV);
555
556 /// Updates the visibility for the new global cloned from the source
557 /// and, if applicable, linked with an existing destination global.
558 /// Handles visibility change required for promoted locals.
559 void setVisibility(GlobalValue *NewGV, const GlobalValue *SGV,
560 const GlobalValue *DGV = nullptr);
561
Rafael Espindolac84f6082014-11-25 06:11:24 +0000562 void linkNamedMDNodes();
563};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000564}
Bill Wendlingd48b7782012-02-28 04:01:21 +0000565
Rafael Espindola18c89412014-10-27 02:35:46 +0000566/// The LLVM SymbolTable class autorenames globals that conflict in the symbol
567/// table. This is good for all clients except for us. Go through the trouble
568/// to force this back.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000569static void forceRenaming(GlobalValue *GV, StringRef Name) {
570 // If the global doesn't force its name or if it already has the right name,
571 // there is nothing for us to do.
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000572 // Note that any required local to global promotion should already be done,
573 // so promoted locals will not skip this handling as their linkage is no
574 // longer local.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000575 if (GV->hasLocalLinkage() || GV->getName() == Name)
576 return;
577
578 Module *M = GV->getParent();
Reid Spencer361e5132004-11-12 20:37:43 +0000579
580 // If there is a conflict, rename the conflict.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000581 if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
Chris Lattner2a8d2e02007-02-11 00:39:38 +0000582 GV->takeName(ConflictGV);
Rafael Espindolae3a933a2015-12-01 20:11:43 +0000583 ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000584 assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
Chris Lattner2a8d2e02007-02-11 00:39:38 +0000585 } else {
Rafael Espindolae3a933a2015-12-01 20:11:43 +0000586 GV->setName(Name); // Force the name back
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000587 }
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000588}
Reid Spencer90246aa2007-02-04 04:29:21 +0000589
Rafael Espindola18c89412014-10-27 02:35:46 +0000590/// copy additional attributes (those not needed to construct a GlobalValue)
591/// from the SrcGV to the DestGV.
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000592void ModuleLinker::copyGVAttributes(GlobalValue *NewGV,
593 const GlobalValue *SrcGV) {
Rafael Espindola769efe62015-12-02 20:03:17 +0000594 NewGV->copyAttributesFrom(SrcGV);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000595 forceRenaming(NewGV, getName(SrcGV));
Reid Spencer361e5132004-11-12 20:37:43 +0000596}
597
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000598bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) {
599 if (!isPerformingImport())
600 return false;
Teresa Johnson3cd81612015-11-10 18:20:11 +0000601 auto *GA = dyn_cast<GlobalAlias>(SGV);
602 if (GA) {
603 if (GA->hasWeakAnyLinkage())
604 return false;
Rafael Espindola89345772015-11-26 19:22:59 +0000605 const GlobalObject *GO = GA->getBaseObject();
606 if (!GO->hasLinkOnceODRLinkage())
607 return false;
608 return doImportAsDefinition(GO);
Teresa Johnson3cd81612015-11-10 18:20:11 +0000609 }
Teresa Johnsondfbebc32015-11-10 18:26:31 +0000610 // Always import GlobalVariable definitions, except for the special
611 // case of WeakAny which are imported as ExternalWeak declarations
612 // (see comments in ModuleLinker::getLinkage). The linkage changes
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000613 // described in ModuleLinker::getLinkage ensure the correct behavior (e.g.
614 // global variables with external linkage are transformed to
Teresa Johnson3cd81612015-11-10 18:20:11 +0000615 // available_externally definitions, which are ultimately turned into
616 // declarations after the EliminateAvailableExternally pass).
Craig Topper66059c92015-11-18 07:07:59 +0000617 if (isa<GlobalVariable>(SGV) && !SGV->isDeclaration() &&
Teresa Johnson3cd81612015-11-10 18:20:11 +0000618 !SGV->hasWeakAnyLinkage())
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000619 return true;
620 // Only import the function requested for importing.
621 auto *SF = dyn_cast<Function>(SGV);
Mehdi Aminiffe2e4a2015-12-02 04:34:28 +0000622 if (SF && ImportFunction->count(SF))
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000623 return true;
624 // Otherwise no.
625 return false;
626}
627
628bool ModuleLinker::doPromoteLocalToGlobal(const GlobalValue *SGV) {
629 assert(SGV->hasLocalLinkage());
630 // Both the imported references and the original local variable must
631 // be promoted.
632 if (!isPerformingImport() && !isModuleExporting())
633 return false;
634
635 // Local const variables never need to be promoted unless they are address
636 // taken. The imported uses can simply use the clone created in this module.
637 // For now we are conservative in determining which variables are not
638 // address taken by checking the unnamed addr flag. To be more aggressive,
639 // the address taken information must be checked earlier during parsing
640 // of the module and recorded in the function index for use when importing
641 // from that module.
642 auto *GVar = dyn_cast<GlobalVariable>(SGV);
643 if (GVar && GVar->isConstant() && GVar->hasUnnamedAddr())
644 return false;
645
646 // Eventually we only need to promote functions in the exporting module that
647 // are referenced by a potentially exported function (i.e. one that is in the
648 // function index).
649 return true;
650}
651
652std::string ModuleLinker::getName(const GlobalValue *SGV) {
653 // For locals that must be promoted to global scope, ensure that
654 // the promoted name uniquely identifies the copy in the original module,
655 // using the ID assigned during combined index creation. When importing,
656 // we rename all locals (not just those that are promoted) in order to
657 // avoid naming conflicts between locals imported from different modules.
658 if (SGV->hasLocalLinkage() &&
659 (doPromoteLocalToGlobal(SGV) || isPerformingImport()))
660 return FunctionInfoIndex::getGlobalNameForLocal(
661 SGV->getName(),
662 ImportIndex->getModuleId(SGV->getParent()->getModuleIdentifier()));
663 return SGV->getName();
664}
665
666GlobalValue::LinkageTypes ModuleLinker::getLinkage(const GlobalValue *SGV) {
667 // Any local variable that is referenced by an exported function needs
668 // to be promoted to global scope. Since we don't currently know which
669 // functions reference which local variables/functions, we must treat
670 // all as potentially exported if this module is exporting anything.
671 if (isModuleExporting()) {
672 if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV))
673 return GlobalValue::ExternalLinkage;
674 return SGV->getLinkage();
675 }
676
677 // Otherwise, if we aren't importing, no linkage change is needed.
678 if (!isPerformingImport())
679 return SGV->getLinkage();
680
681 switch (SGV->getLinkage()) {
682 case GlobalValue::ExternalLinkage:
683 // External defnitions are converted to available_externally
684 // definitions upon import, so that they are available for inlining
685 // and/or optimization, but are turned into declarations later
686 // during the EliminateAvailableExternally pass.
Teresa Johnson3cd81612015-11-10 18:20:11 +0000687 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000688 return GlobalValue::AvailableExternallyLinkage;
689 // An imported external declaration stays external.
690 return SGV->getLinkage();
691
692 case GlobalValue::AvailableExternallyLinkage:
693 // An imported available_externally definition converts
694 // to external if imported as a declaration.
695 if (!doImportAsDefinition(SGV))
696 return GlobalValue::ExternalLinkage;
697 // An imported available_externally declaration stays that way.
698 return SGV->getLinkage();
699
700 case GlobalValue::LinkOnceAnyLinkage:
701 case GlobalValue::LinkOnceODRLinkage:
702 // These both stay the same when importing the definition.
703 // The ThinLTO pass will eventually force-import their definitions.
704 return SGV->getLinkage();
705
706 case GlobalValue::WeakAnyLinkage:
707 // Can't import weak_any definitions correctly, or we might change the
708 // program semantics, since the linker will pick the first weak_any
709 // definition and importing would change the order they are seen by the
710 // linker. The module linking caller needs to enforce this.
711 assert(!doImportAsDefinition(SGV));
712 // If imported as a declaration, it becomes external_weak.
713 return GlobalValue::ExternalWeakLinkage;
714
715 case GlobalValue::WeakODRLinkage:
716 // For weak_odr linkage, there is a guarantee that all copies will be
717 // equivalent, so the issue described above for weak_any does not exist,
718 // and the definition can be imported. It can be treated similarly
719 // to an imported externally visible global value.
Teresa Johnson3cd81612015-11-10 18:20:11 +0000720 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000721 return GlobalValue::AvailableExternallyLinkage;
722 else
723 return GlobalValue::ExternalLinkage;
724
725 case GlobalValue::AppendingLinkage:
726 // It would be incorrect to import an appending linkage variable,
727 // since it would cause global constructors/destructors to be
728 // executed multiple times. This should have already been handled
729 // by linkGlobalValueProto.
Davide Italianof3d23292015-11-13 02:16:51 +0000730 llvm_unreachable("Cannot import appending linkage variable");
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000731
732 case GlobalValue::InternalLinkage:
733 case GlobalValue::PrivateLinkage:
734 // If we are promoting the local to global scope, it is handled
735 // similarly to a normal externally visible global.
736 if (doPromoteLocalToGlobal(SGV)) {
Teresa Johnson3cd81612015-11-10 18:20:11 +0000737 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000738 return GlobalValue::AvailableExternallyLinkage;
739 else
740 return GlobalValue::ExternalLinkage;
741 }
742 // A non-promoted imported local definition stays local.
743 // The ThinLTO pass will eventually force-import their definitions.
744 return SGV->getLinkage();
745
746 case GlobalValue::ExternalWeakLinkage:
747 // External weak doesn't apply to definitions, must be a declaration.
748 assert(!doImportAsDefinition(SGV));
749 // Linkage stays external_weak.
750 return SGV->getLinkage();
751
752 case GlobalValue::CommonLinkage:
753 // Linkage stays common on definitions.
754 // The ThinLTO pass will eventually force-import their definitions.
755 return SGV->getLinkage();
756 }
757
758 llvm_unreachable("unknown linkage type");
759}
760
Rafael Espindolaef237112014-12-08 18:45:16 +0000761/// Loop through the global variables in the src module and merge them into the
762/// dest module.
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000763GlobalVariable *
Rafael Espindola792b7952015-12-03 14:48:20 +0000764ModuleLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) {
Rafael Espindolaef237112014-12-08 18:45:16 +0000765 // No linking to be performed or linking from the source: simply create an
766 // identical version of the symbol over in the dest module... the
767 // initializer will be filled in later by LinkGlobalInits.
Rafael Espindolaf3518c92015-12-02 19:30:52 +0000768 GlobalVariable *NewDGV =
769 new GlobalVariable(DstM, TypeMap.get(SGVar->getType()->getElementType()),
770 SGVar->isConstant(), GlobalValue::ExternalLinkage,
771 /*init*/ nullptr, getName(SGVar),
772 /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
773 SGVar->getType()->getAddressSpace());
Rafael Espindolaef237112014-12-08 18:45:16 +0000774
775 return NewDGV;
776}
777
778/// Link the function in the source module into the destination module if
779/// needed, setting up mapping information.
Rafael Espindola792b7952015-12-03 14:48:20 +0000780Function *ModuleLinker::copyFunctionProto(const Function *SF) {
Rafael Espindolaef237112014-12-08 18:45:16 +0000781 // If there is no linkage to be performed or we are linking from the source,
782 // bring SF over.
Rafael Espindolaf3518c92015-12-02 19:30:52 +0000783 return Function::Create(TypeMap.get(SF->getFunctionType()),
784 GlobalValue::ExternalLinkage, getName(SF), &DstM);
Rafael Espindolaef237112014-12-08 18:45:16 +0000785}
786
787/// Set up prototypes for any aliases that come over from the source module.
Rafael Espindola792b7952015-12-03 14:48:20 +0000788GlobalValue *ModuleLinker::copyGlobalAliasProto(const GlobalAlias *SGA) {
Rafael Espindolaef237112014-12-08 18:45:16 +0000789 // If there is no linkage to be performed or we're linking from the source,
790 // bring over SGA.
David Blaikie6614d8d2015-09-14 20:29:26 +0000791 auto *Ty = TypeMap.get(SGA->getValueType());
792 return GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
Rafael Espindolaf3518c92015-12-02 19:30:52 +0000793 GlobalValue::ExternalLinkage, getName(SGA), &DstM);
Rafael Espindolaef237112014-12-08 18:45:16 +0000794}
795
Rafael Espindolaeb5e0a72015-11-29 14:33:06 +0000796static GlobalValue::VisibilityTypes
797getMinVisibility(GlobalValue::VisibilityTypes A,
798 GlobalValue::VisibilityTypes B) {
799 if (A == GlobalValue::HiddenVisibility || B == GlobalValue::HiddenVisibility)
800 return GlobalValue::HiddenVisibility;
801 if (A == GlobalValue::ProtectedVisibility ||
802 B == GlobalValue::ProtectedVisibility)
803 return GlobalValue::ProtectedVisibility;
804 return GlobalValue::DefaultVisibility;
805}
806
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000807void ModuleLinker::setVisibility(GlobalValue *NewGV, const GlobalValue *SGV,
808 const GlobalValue *DGV) {
809 GlobalValue::VisibilityTypes Visibility = SGV->getVisibility();
810 if (DGV)
Rafael Espindolaeb5e0a72015-11-29 14:33:06 +0000811 Visibility = getMinVisibility(DGV->getVisibility(), Visibility);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000812 // For promoted locals, mark them hidden so that they can later be
813 // stripped from the symbol table to reduce bloat.
814 if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV))
815 Visibility = GlobalValue::HiddenVisibility;
816 NewGV->setVisibility(Visibility);
817}
818
Rafael Espindola792b7952015-12-03 14:48:20 +0000819GlobalValue *ModuleLinker::copyGlobalValueProto(const GlobalValue *SGV,
Rafael Espindolaf3518c92015-12-02 19:30:52 +0000820 const GlobalValue *DGV,
821 bool ForDefinition) {
Rafael Espindolaef237112014-12-08 18:45:16 +0000822 GlobalValue *NewGV;
Rafael Espindolaf3518c92015-12-02 19:30:52 +0000823 if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) {
Rafael Espindola792b7952015-12-03 14:48:20 +0000824 NewGV = copyGlobalVariableProto(SGVar);
Rafael Espindolaf3518c92015-12-02 19:30:52 +0000825 } else if (auto *SF = dyn_cast<Function>(SGV)) {
Rafael Espindola792b7952015-12-03 14:48:20 +0000826 NewGV = copyFunctionProto(SF);
Rafael Espindolaf3518c92015-12-02 19:30:52 +0000827 } else {
828 if (ForDefinition)
Rafael Espindola792b7952015-12-03 14:48:20 +0000829 NewGV = copyGlobalAliasProto(cast<GlobalAlias>(SGV));
Rafael Espindolaf3518c92015-12-02 19:30:52 +0000830 else
831 NewGV = new GlobalVariable(
832 DstM, TypeMap.get(SGV->getType()->getElementType()),
833 /*isConstant*/ false, GlobalValue::ExternalLinkage,
834 /*init*/ nullptr, getName(SGV),
835 /*insertbefore*/ nullptr, SGV->getThreadLocalMode(),
836 SGV->getType()->getAddressSpace());
837 }
838
839 if (ForDefinition)
840 NewGV->setLinkage(getLinkage(SGV));
841 else if (SGV->hasAvailableExternallyLinkage() || SGV->hasWeakLinkage() ||
842 SGV->hasLinkOnceLinkage())
843 NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);
844
Rafael Espindolaef237112014-12-08 18:45:16 +0000845 copyGVAttributes(NewGV, SGV);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +0000846 setVisibility(NewGV, SGV, DGV);
Rafael Espindolaef237112014-12-08 18:45:16 +0000847 return NewGV;
848}
849
Rafael Espindola19b52382015-11-27 20:28:19 +0000850Value *ValueMaterializerTy::materializeDeclFor(Value *V) {
851 return ModLinker->materializeDeclFor(V);
852}
853
854Value *ModuleLinker::materializeDeclFor(Value *V) {
Rafael Espindolaef237112014-12-08 18:45:16 +0000855 auto *SGV = dyn_cast<GlobalValue>(V);
856 if (!SGV)
Craig Topper2617dcc2014-04-15 06:32:26 +0000857 return nullptr;
James Molloyf6f121e2013-05-28 15:17:05 +0000858
Rafael Espindolabaa3bf82015-12-01 15:19:48 +0000859 linkGlobalValueProto(SGV);
Rafael Espindola4b5ec262015-12-02 22:59:04 +0000860 return ValueMap[SGV];
James Molloyf6f121e2013-05-28 15:17:05 +0000861}
862
Rafael Espindola19b52382015-11-27 20:28:19 +0000863void ValueMaterializerTy::materializeInitFor(GlobalValue *New,
864 GlobalValue *Old) {
865 return ModLinker->materializeInitFor(New, Old);
866}
867
Rafael Espindola4b5ec262015-12-02 22:59:04 +0000868static bool shouldLazyLink(const GlobalValue &GV) {
869 return GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() ||
870 GV.hasAvailableExternallyLinkage();
871}
872
Rafael Espindola19b52382015-11-27 20:28:19 +0000873void ModuleLinker::materializeInitFor(GlobalValue *New, GlobalValue *Old) {
Rafael Espindolabaa3bf82015-12-01 15:19:48 +0000874 if (auto *F = dyn_cast<Function>(New)) {
875 if (!F->isDeclaration())
876 return;
877 } else if (auto *V = dyn_cast<GlobalVariable>(New)) {
878 if (V->hasInitializer())
879 return;
880 } else {
881 auto *A = cast<GlobalAlias>(New);
882 if (A->getAliasee())
883 return;
884 }
885
886 if (Old->isDeclaration())
887 return;
888
Rafael Espindola19b52382015-11-27 20:28:19 +0000889 if (isPerformingImport() && !doImportAsDefinition(Old))
890 return;
891
Rafael Espindola4b5ec262015-12-02 22:59:04 +0000892 if (!ValuesToLink.count(Old) && !shouldLazyLink(*Old))
Rafael Espindola19b52382015-11-27 20:28:19 +0000893 return;
894
Rafael Espindolae39cd5b2015-12-01 22:40:40 +0000895 linkGlobalValueBody(*New, *Old);
Rafael Espindola19b52382015-11-27 20:28:19 +0000896}
897
Rafael Espindola0e309fe2015-12-01 19:50:54 +0000898bool ModuleLinker::getComdatLeader(Module &M, StringRef ComdatName,
David Majnemerdad0a642014-06-27 18:19:56 +0000899 const GlobalVariable *&GVar) {
Rafael Espindola0e309fe2015-12-01 19:50:54 +0000900 const GlobalValue *GVal = M.getNamedValue(ComdatName);
David Majnemerdad0a642014-06-27 18:19:56 +0000901 if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) {
902 GVal = GA->getBaseObject();
903 if (!GVal)
904 // We cannot resolve the size of the aliasee yet.
905 return emitError("Linking COMDATs named '" + ComdatName +
906 "': COMDAT key involves incomputable alias size.");
907 }
908
909 GVar = dyn_cast_or_null<GlobalVariable>(GVal);
910 if (!GVar)
911 return emitError(
912 "Linking COMDATs named '" + ComdatName +
913 "': GlobalVariable required for data dependent selection!");
914
915 return false;
916}
917
918bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName,
919 Comdat::SelectionKind Src,
920 Comdat::SelectionKind Dst,
921 Comdat::SelectionKind &Result,
922 bool &LinkFromSrc) {
923 // The ability to mix Comdat::SelectionKind::Any with
924 // Comdat::SelectionKind::Largest is a behavior that comes from COFF.
925 bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any ||
926 Dst == Comdat::SelectionKind::Largest;
927 bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any ||
928 Src == Comdat::SelectionKind::Largest;
929 if (DstAnyOrLargest && SrcAnyOrLargest) {
930 if (Dst == Comdat::SelectionKind::Largest ||
931 Src == Comdat::SelectionKind::Largest)
932 Result = Comdat::SelectionKind::Largest;
933 else
934 Result = Comdat::SelectionKind::Any;
935 } else if (Src == Dst) {
936 Result = Dst;
937 } else {
938 return emitError("Linking COMDATs named '" + ComdatName +
939 "': invalid selection kinds!");
940 }
941
942 switch (Result) {
943 case Comdat::SelectionKind::Any:
944 // Go with Dst.
945 LinkFromSrc = false;
946 break;
947 case Comdat::SelectionKind::NoDuplicates:
948 return emitError("Linking COMDATs named '" + ComdatName +
949 "': noduplicates has been violated!");
950 case Comdat::SelectionKind::ExactMatch:
951 case Comdat::SelectionKind::Largest:
952 case Comdat::SelectionKind::SameSize: {
953 const GlobalVariable *DstGV;
954 const GlobalVariable *SrcGV;
955 if (getComdatLeader(DstM, ComdatName, DstGV) ||
956 getComdatLeader(SrcM, ComdatName, SrcGV))
957 return true;
958
Rafael Espindola0e309fe2015-12-01 19:50:54 +0000959 const DataLayout &DstDL = DstM.getDataLayout();
960 const DataLayout &SrcDL = SrcM.getDataLayout();
David Majnemerdad0a642014-06-27 18:19:56 +0000961 uint64_t DstSize =
Mehdi Amini46a43552015-03-04 18:43:29 +0000962 DstDL.getTypeAllocSize(DstGV->getType()->getPointerElementType());
David Majnemerdad0a642014-06-27 18:19:56 +0000963 uint64_t SrcSize =
Mehdi Amini46a43552015-03-04 18:43:29 +0000964 SrcDL.getTypeAllocSize(SrcGV->getType()->getPointerElementType());
David Majnemerdad0a642014-06-27 18:19:56 +0000965 if (Result == Comdat::SelectionKind::ExactMatch) {
966 if (SrcGV->getInitializer() != DstGV->getInitializer())
967 return emitError("Linking COMDATs named '" + ComdatName +
968 "': ExactMatch violated!");
969 LinkFromSrc = false;
970 } else if (Result == Comdat::SelectionKind::Largest) {
971 LinkFromSrc = SrcSize > DstSize;
972 } else if (Result == Comdat::SelectionKind::SameSize) {
973 if (SrcSize != DstSize)
974 return emitError("Linking COMDATs named '" + ComdatName +
975 "': SameSize violated!");
976 LinkFromSrc = false;
977 } else {
978 llvm_unreachable("unknown selection kind");
979 }
980 break;
981 }
982 }
983
984 return false;
985}
986
987bool ModuleLinker::getComdatResult(const Comdat *SrcC,
988 Comdat::SelectionKind &Result,
989 bool &LinkFromSrc) {
Rafael Espindolab16196a2014-08-11 17:07:34 +0000990 Comdat::SelectionKind SSK = SrcC->getSelectionKind();
David Majnemerdad0a642014-06-27 18:19:56 +0000991 StringRef ComdatName = SrcC->getName();
Rafael Espindola0e309fe2015-12-01 19:50:54 +0000992 Module::ComdatSymTabType &ComdatSymTab = DstM.getComdatSymbolTable();
David Majnemerdad0a642014-06-27 18:19:56 +0000993 Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName);
Rafael Espindola2ef3f292014-08-11 16:55:42 +0000994
Rafael Espindolab16196a2014-08-11 17:07:34 +0000995 if (DstCI == ComdatSymTab.end()) {
996 // Use the comdat if it is only available in one of the modules.
997 LinkFromSrc = true;
998 Result = SSK;
Rafael Espindola2ef3f292014-08-11 16:55:42 +0000999 return false;
Rafael Espindolab16196a2014-08-11 17:07:34 +00001000 }
Rafael Espindola2ef3f292014-08-11 16:55:42 +00001001
1002 const Comdat *DstC = &DstCI->second;
Rafael Espindola2ef3f292014-08-11 16:55:42 +00001003 Comdat::SelectionKind DSK = DstC->getSelectionKind();
1004 return computeResultingSelectionKind(ComdatName, SSK, DSK, Result,
1005 LinkFromSrc);
David Majnemerdad0a642014-06-27 18:19:56 +00001006}
James Molloyf6f121e2013-05-28 15:17:05 +00001007
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001008bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
1009 const GlobalValue &Dest,
Rafael Espindoladbb0bd12014-09-09 15:21:00 +00001010 const GlobalValue &Src) {
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +00001011 // Should we unconditionally use the Src?
Artem Belevich020d4fb2015-09-01 17:55:55 +00001012 if (shouldOverrideFromSrc()) {
Duncan P. N. Exon Smithe8681232015-04-22 04:11:00 +00001013 LinkFromSrc = true;
1014 return false;
1015 }
1016
Rafael Espindola778fcc72014-11-02 13:28:57 +00001017 // We always have to add Src if it has appending linkage.
1018 if (Src.hasAppendingLinkage()) {
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +00001019 // Caller should have already determined that we can't link from source
1020 // when importing (see comments in linkGlobalValueProto).
1021 assert(!isPerformingImport());
Rafael Espindola778fcc72014-11-02 13:28:57 +00001022 LinkFromSrc = true;
1023 return false;
1024 }
1025
Rafael Espindolad4bcefc2014-10-24 18:13:04 +00001026 bool SrcIsDeclaration = Src.isDeclarationForLinker();
1027 bool DestIsDeclaration = Dest.isDeclarationForLinker();
Rafael Espindoladbb0bd12014-09-09 15:21:00 +00001028
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +00001029 if (isPerformingImport()) {
1030 if (isa<Function>(&Src)) {
1031 // For functions, LinkFromSrc iff this is the function requested
1032 // for importing. For variables, decide below normally.
Mehdi Aminiffe2e4a2015-12-02 04:34:28 +00001033 LinkFromSrc = ImportFunction->count(&Src);
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +00001034 return false;
1035 }
1036
1037 // Check if this is an alias with an already existing definition
1038 // in Dest, which must have come from a prior importing pass from
1039 // the same Src module. Unlike imported function and variable
1040 // definitions, which are imported as available_externally and are
1041 // not definitions for the linker, that is not a valid linkage for
1042 // imported aliases which must be definitions. Simply use the existing
1043 // Dest copy.
1044 if (isa<GlobalAlias>(&Src) && !DestIsDeclaration) {
1045 assert(isa<GlobalAlias>(&Dest));
1046 LinkFromSrc = false;
1047 return false;
1048 }
1049 }
1050
Rafael Espindoladbb0bd12014-09-09 15:21:00 +00001051 if (SrcIsDeclaration) {
1052 // If Src is external or if both Src & Dest are external.. Just link the
1053 // external globals, we aren't adding anything.
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001054 if (Src.hasDLLImportStorageClass()) {
Rafael Espindoladbb0bd12014-09-09 15:21:00 +00001055 // If one of GVs is marked as DLLImport, result should be dllimport'ed.
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001056 LinkFromSrc = DestIsDeclaration;
1057 return false;
1058 }
Rafael Espindoladbb0bd12014-09-09 15:21:00 +00001059 // If the Dest is weak, use the source linkage.
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001060 LinkFromSrc = Dest.hasExternalWeakLinkage();
1061 return false;
Rafael Espindoladbb0bd12014-09-09 15:21:00 +00001062 }
1063
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001064 if (DestIsDeclaration) {
Rafael Espindoladbb0bd12014-09-09 15:21:00 +00001065 // If Dest is external but Src is not:
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001066 LinkFromSrc = true;
1067 return false;
1068 }
Rafael Espindoladbb0bd12014-09-09 15:21:00 +00001069
Rafael Espindola09106052014-09-09 15:59:12 +00001070 if (Src.hasCommonLinkage()) {
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001071 if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) {
1072 LinkFromSrc = true;
Rafael Espindola09106052014-09-09 15:59:12 +00001073 return false;
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001074 }
1075
1076 if (!Dest.hasCommonLinkage()) {
1077 LinkFromSrc = false;
1078 return false;
1079 }
Rafael Espindola09106052014-09-09 15:59:12 +00001080
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001081 const DataLayout &DL = Dest.getParent()->getDataLayout();
Rafael Espindola09106052014-09-09 15:59:12 +00001082 uint64_t DestSize = DL.getTypeAllocSize(Dest.getType()->getElementType());
1083 uint64_t SrcSize = DL.getTypeAllocSize(Src.getType()->getElementType());
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001084 LinkFromSrc = SrcSize > DestSize;
1085 return false;
Rafael Espindola09106052014-09-09 15:59:12 +00001086 }
1087
Rafael Espindoladbb0bd12014-09-09 15:21:00 +00001088 if (Src.isWeakForLinker()) {
1089 assert(!Dest.hasExternalWeakLinkage());
1090 assert(!Dest.hasAvailableExternallyLinkage());
Rafael Espindola09106052014-09-09 15:59:12 +00001091
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001092 if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage()) {
1093 LinkFromSrc = true;
1094 return false;
1095 }
Rafael Espindoladbb0bd12014-09-09 15:21:00 +00001096
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001097 LinkFromSrc = false;
Rafael Espindola09106052014-09-09 15:59:12 +00001098 return false;
Rafael Espindoladbb0bd12014-09-09 15:21:00 +00001099 }
1100
1101 if (Dest.isWeakForLinker()) {
1102 assert(Src.hasExternalLinkage());
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001103 LinkFromSrc = true;
1104 return false;
Rafael Espindoladbb0bd12014-09-09 15:21:00 +00001105 }
1106
1107 assert(!Src.hasExternalWeakLinkage());
1108 assert(!Dest.hasExternalWeakLinkage());
1109 assert(Dest.hasExternalLinkage() && Src.hasExternalLinkage() &&
1110 "Unexpected linkage type!");
1111 return emitError("Linking globals named '" + Src.getName() +
1112 "': symbol multiply defined!");
1113}
1114
Rafael Espindola18c89412014-10-27 02:35:46 +00001115/// Loop over all of the linked values to compute type mappings. For example,
1116/// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct
1117/// types 'Foo' but one got renamed when the module was loaded into the same
1118/// LLVMContext.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001119void ModuleLinker::computeTypeMapping() {
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001120 for (GlobalValue &SGV : SrcM.globals()) {
Rafael Espindolac8a476e2014-11-25 04:26:19 +00001121 GlobalValue *DGV = getLinkedToGlobal(&SGV);
1122 if (!DGV)
1123 continue;
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001124
Rafael Espindolac8a476e2014-11-25 04:26:19 +00001125 if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {
1126 TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001127 continue;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001128 }
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001129
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001130 // Unify the element type of appending arrays.
1131 ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType());
Rafael Espindolac8a476e2014-11-25 04:26:19 +00001132 ArrayType *SAT = cast<ArrayType>(SGV.getType()->getElementType());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001133 TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
Devang Patel5c310be2009-08-11 18:01:24 +00001134 }
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001135
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001136 for (GlobalValue &SGV : SrcM) {
Rafael Espindolac8a476e2014-11-25 04:26:19 +00001137 if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
1138 TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001139 }
Bill Wendling7b464612012-02-27 22:34:19 +00001140
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001141 for (GlobalValue &SGV : SrcM.aliases()) {
Rafael Espindolae96d7eb2014-11-25 04:43:59 +00001142 if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
1143 TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
1144 }
1145
Bill Wendlingd48b7782012-02-28 04:01:21 +00001146 // Incorporate types by name, scanning all the types in the source module.
1147 // At this point, the destination module may have a type "%foo = { i32 }" for
Bill Wendling2b3f61a2012-02-27 23:48:30 +00001148 // example. When the source module got loaded into the same LLVMContext, if
1149 // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001150 std::vector<StructType *> Types = SrcM.getIdentifiedStructTypes();
Rafael Espindola2fa1e432014-12-03 07:18:23 +00001151 for (StructType *ST : Types) {
Rafael Espindola4d4c9382014-12-01 19:08:07 +00001152 if (!ST->hasName())
1153 continue;
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001154
Bill Wendling2b3f61a2012-02-27 23:48:30 +00001155 // Check to see if there is a dot in the name followed by a digit.
Bill Wendlingd48b7782012-02-28 04:01:21 +00001156 size_t DotPos = ST->getName().rfind('.');
1157 if (DotPos == 0 || DotPos == StringRef::npos ||
Guy Benyei83c74e92013-02-12 21:21:59 +00001158 ST->getName().back() == '.' ||
Rafael Espindola973b3612014-12-01 19:17:46 +00001159 !isdigit(static_cast<unsigned char>(ST->getName()[DotPos + 1])))
Bill Wendlingd48b7782012-02-28 04:01:21 +00001160 continue;
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001161
Bill Wendling2b3f61a2012-02-27 23:48:30 +00001162 // Check to see if the destination module has a struct with the prefix name.
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001163 StructType *DST = DstM.getTypeByName(ST->getName().substr(0, DotPos));
Rafael Espindola973b3612014-12-01 19:17:46 +00001164 if (!DST)
1165 continue;
1166
1167 // Don't use it if this actually came from the source module. They're in
1168 // the same LLVMContext after all. Also don't use it unless the type is
1169 // actually used in the destination module. This can happen in situations
1170 // like this:
1171 //
1172 // Module A Module B
1173 // -------- --------
1174 // %Z = type { %A } %B = type { %C.1 }
1175 // %A = type { %B.1, [7 x i8] } %C.1 = type { i8* }
1176 // %B.1 = type { %C } %A.2 = type { %B.3, [5 x i8] }
1177 // %C = type { i8* } %B.3 = type { %C.1 }
1178 //
1179 // When we link Module B with Module A, the '%B' in Module B is
1180 // used. However, that would then use '%C.1'. But when we process '%C.1',
1181 // we prefer to take the '%C' version. So we are then left with both
1182 // '%C.1' and '%C' being used for the same types. This leads to some
1183 // variables using one type and some using the other.
Rafael Espindola31ad4682014-12-03 22:36:37 +00001184 if (TypeMap.DstStructTypesSet.hasType(DST))
Rafael Espindola973b3612014-12-01 19:17:46 +00001185 TypeMap.addTypeMapping(DST, ST);
Bill Wendling2b3f61a2012-02-27 23:48:30 +00001186 }
1187
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001188 // Now that we have discovered all of the type equivalences, get a body for
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001189 // any 'opaque' types in the dest module that are now resolved.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001190 TypeMap.linkDefinedTypeBodies();
Devang Patel5c310be2009-08-11 18:01:24 +00001191}
1192
Duncan P. N. Exon Smith09d84ad2014-08-12 16:46:37 +00001193static void upgradeGlobalArray(GlobalVariable *GV) {
1194 ArrayType *ATy = cast<ArrayType>(GV->getType()->getElementType());
1195 StructType *OldTy = cast<StructType>(ATy->getElementType());
1196 assert(OldTy->getNumElements() == 2 && "Expected to upgrade from 2 elements");
1197
1198 // Get the upgraded 3 element type.
1199 PointerType *VoidPtrTy = Type::getInt8Ty(GV->getContext())->getPointerTo();
1200 Type *Tys[3] = {OldTy->getElementType(0), OldTy->getElementType(1),
1201 VoidPtrTy};
1202 StructType *NewTy = StructType::get(GV->getContext(), Tys, false);
1203
1204 // Build new constants with a null third field filled in.
1205 Constant *OldInitC = GV->getInitializer();
1206 ConstantArray *OldInit = dyn_cast<ConstantArray>(OldInitC);
1207 if (!OldInit && !isa<ConstantAggregateZero>(OldInitC))
1208 // Invalid initializer; give up.
1209 return;
1210 std::vector<Constant *> Initializers;
1211 if (OldInit && OldInit->getNumOperands()) {
1212 Value *Null = Constant::getNullValue(VoidPtrTy);
1213 for (Use &U : OldInit->operands()) {
1214 ConstantStruct *Init = cast<ConstantStruct>(U.get());
1215 Initializers.push_back(ConstantStruct::get(
1216 NewTy, Init->getOperand(0), Init->getOperand(1), Null, nullptr));
1217 }
1218 }
1219 assert(Initializers.size() == ATy->getNumElements() &&
1220 "Failed to copy all array elements");
1221
1222 // Replace the old GV with a new one.
1223 ATy = ArrayType::get(NewTy, Initializers.size());
1224 Constant *NewInit = ConstantArray::get(ATy, Initializers);
1225 GlobalVariable *NewGV = new GlobalVariable(
1226 *GV->getParent(), ATy, GV->isConstant(), GV->getLinkage(), NewInit, "",
1227 GV, GV->getThreadLocalMode(), GV->getType()->getAddressSpace(),
1228 GV->isExternallyInitialized());
1229 NewGV->copyAttributesFrom(GV);
1230 NewGV->takeName(GV);
1231 assert(GV->use_empty() && "program cannot use initializer list");
1232 GV->eraseFromParent();
1233}
1234
1235void ModuleLinker::upgradeMismatchedGlobalArray(StringRef Name) {
1236 // Look for the global arrays.
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001237 auto *DstGV = dyn_cast_or_null<GlobalVariable>(DstM.getNamedValue(Name));
Duncan P. N. Exon Smith09d84ad2014-08-12 16:46:37 +00001238 if (!DstGV)
1239 return;
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001240 auto *SrcGV = dyn_cast_or_null<GlobalVariable>(SrcM.getNamedValue(Name));
Duncan P. N. Exon Smith09d84ad2014-08-12 16:46:37 +00001241 if (!SrcGV)
1242 return;
1243
1244 // Check if the types already match.
1245 auto *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
1246 auto *SrcTy =
1247 cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()));
1248 if (DstTy == SrcTy)
1249 return;
1250
1251 // Grab the element types. We can only upgrade an array of a two-field
1252 // struct. Only bother if the other one has three-fields.
1253 auto *DstEltTy = cast<StructType>(DstTy->getElementType());
1254 auto *SrcEltTy = cast<StructType>(SrcTy->getElementType());
1255 if (DstEltTy->getNumElements() == 2 && SrcEltTy->getNumElements() == 3) {
1256 upgradeGlobalArray(DstGV);
1257 return;
1258 }
1259 if (DstEltTy->getNumElements() == 3 && SrcEltTy->getNumElements() == 2)
1260 upgradeGlobalArray(SrcGV);
1261
1262 // We can't upgrade any other differences.
1263}
1264
1265void ModuleLinker::upgradeMismatchedGlobals() {
1266 upgradeMismatchedGlobalArray("llvm.global_ctors");
1267 upgradeMismatchedGlobalArray("llvm.global_dtors");
1268}
1269
Rafael Espindola6e8ab922015-12-01 17:17:04 +00001270static void getArrayElements(const Constant *C,
1271 SmallVectorImpl<Constant *> &Dest) {
1272 unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
1273
1274 for (unsigned i = 0; i != NumElements; ++i)
1275 Dest.push_back(C->getAggregateElement(i));
1276}
1277
Rafael Espindola18c89412014-10-27 02:35:46 +00001278/// If there were any appending global variables, link them together now.
1279/// Return true on error.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001280bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV,
Rafael Espindola3e8bc6a2014-10-31 16:08:17 +00001281 const GlobalVariable *SrcGV) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001282 ArrayType *SrcTy =
Rafael Espindolac945c8d2015-11-29 03:29:42 +00001283 cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()));
1284 Type *EltTy = SrcTy->getElementType();
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001285
Rafael Espindolac945c8d2015-11-29 03:29:42 +00001286 if (DstGV) {
1287 ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001288
Rafael Espindolac945c8d2015-11-29 03:29:42 +00001289 if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
1290 return emitError(
1291 "Linking globals named '" + SrcGV->getName() +
1292 "': can only link appending global with another appending global!");
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001293
Rafael Espindolac945c8d2015-11-29 03:29:42 +00001294 // Check to see that they two arrays agree on type.
1295 if (EltTy != DstTy->getElementType())
1296 return emitError("Appending variables with different element types!");
1297 if (DstGV->isConstant() != SrcGV->isConstant())
1298 return emitError("Appending variables linked with different const'ness!");
Rafael Espindolafac3a012013-09-04 15:33:34 +00001299
Rafael Espindolac945c8d2015-11-29 03:29:42 +00001300 if (DstGV->getAlignment() != SrcGV->getAlignment())
1301 return emitError(
1302 "Appending variables with different alignment need to be linked!");
Rafael Espindolafac3a012013-09-04 15:33:34 +00001303
Rafael Espindolac945c8d2015-11-29 03:29:42 +00001304 if (DstGV->getVisibility() != SrcGV->getVisibility())
1305 return emitError(
1306 "Appending variables with different visibility need to be linked!");
1307
1308 if (DstGV->hasUnnamedAddr() != SrcGV->hasUnnamedAddr())
1309 return emitError(
1310 "Appending variables with different unnamed_addr need to be linked!");
1311
1312 if (StringRef(DstGV->getSection()) != SrcGV->getSection())
1313 return emitError(
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001314 "Appending variables with different section name need to be linked!");
Rafael Espindolac945c8d2015-11-29 03:29:42 +00001315 }
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001316
Rafael Espindola6e8ab922015-12-01 17:17:04 +00001317 SmallVector<Constant *, 16> DstElements;
1318 if (DstGV)
1319 getArrayElements(DstGV->getInitializer(), DstElements);
1320
1321 SmallVector<Constant *, 16> SrcElements;
1322 getArrayElements(SrcGV->getInitializer(), SrcElements);
1323
1324 StringRef Name = SrcGV->getName();
1325 bool IsNewStructor =
1326 (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") &&
1327 cast<StructType>(EltTy)->getNumElements() == 3;
1328 if (IsNewStructor)
1329 SrcElements.erase(
1330 std::remove_if(SrcElements.begin(), SrcElements.end(),
1331 [this](Constant *E) {
1332 auto *Key = dyn_cast<GlobalValue>(
1333 E->getAggregateElement(2)->stripPointerCasts());
Rafael Espindola4b5ec262015-12-02 22:59:04 +00001334 return Key && !ValuesToLink.count(Key) &&
1335 !shouldLazyLink(*Key);
Rafael Espindola6e8ab922015-12-01 17:17:04 +00001336 }),
1337 SrcElements.end());
1338 uint64_t NewSize = DstElements.size() + SrcElements.size();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001339 ArrayType *NewType = ArrayType::get(EltTy, NewSize);
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001340
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001341 // Create the new global variable.
Rafael Espindolac945c8d2015-11-29 03:29:42 +00001342 GlobalVariable *NG = new GlobalVariable(
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001343 DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(),
Rafael Espindolac945c8d2015-11-29 03:29:42 +00001344 /*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(),
1345 SrcGV->getType()->getAddressSpace());
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001346
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001347 // Propagate alignment, visibility and section info.
Rafael Espindolac945c8d2015-11-29 03:29:42 +00001348 copyGVAttributes(NG, SrcGV);
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001349
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001350 // Replace any uses of the two global variables with uses of the new
1351 // global.
1352 ValueMap[SrcGV] = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
Anton Korobeynikove79f4c72008-03-10 22:34:28 +00001353
Rafael Espindola6e8ab922015-12-01 17:17:04 +00001354 for (auto *V : SrcElements) {
1355 DstElements.push_back(
1356 MapValue(V, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer));
1357 }
1358
1359 NG->setInitializer(ConstantArray::get(NewType, DstElements));
1360
Rafael Espindolac945c8d2015-11-29 03:29:42 +00001361 if (DstGV) {
1362 DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
1363 DstGV->eraseFromParent();
1364 }
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001365
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001366 return false;
1367}
Mikhail Glushenkov766d4892009-03-03 07:22:23 +00001368
Rafael Espindola778fcc72014-11-02 13:28:57 +00001369bool ModuleLinker::linkGlobalValueProto(GlobalValue *SGV) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001370 GlobalValue *DGV = getLinkedToGlobal(SGV);
Mikhail Glushenkov766d4892009-03-03 07:22:23 +00001371
Rafael Espindola778fcc72014-11-02 13:28:57 +00001372 // Handle the ultra special appending linkage case first.
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +00001373 assert(!DGV || SGV->hasAppendingLinkage() == DGV->hasAppendingLinkage());
Rafael Espindolac945c8d2015-11-29 03:29:42 +00001374 if (SGV->hasAppendingLinkage())
1375 return linkAppendingVarProto(cast_or_null<GlobalVariable>(DGV),
Rafael Espindola778fcc72014-11-02 13:28:57 +00001376 cast<GlobalVariable>(SGV));
1377
1378 bool LinkFromSrc = true;
1379 Comdat *C = nullptr;
Rafael Espindola778fcc72014-11-02 13:28:57 +00001380 bool HasUnnamedAddr = SGV->hasUnnamedAddr();
1381
Rafael Espindola8c044722015-12-02 22:22:24 +00001382 if (isPerformingImport() && !doImportAsDefinition(SGV)) {
1383 LinkFromSrc = false;
1384 } else if (const Comdat *SC = SGV->getComdat()) {
David Majnemerdad0a642014-06-27 18:19:56 +00001385 Comdat::SelectionKind SK;
1386 std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001387 C = DstM.getOrInsertComdat(SC->getName());
Rafael Espindola778fcc72014-11-02 13:28:57 +00001388 C->setSelectionKind(SK);
Rafael Espindola0a80da02015-12-02 20:57:33 +00001389 if (SGV->hasLocalLinkage())
Rafael Espindolabaa3bf82015-12-01 15:19:48 +00001390 LinkFromSrc = true;
Rafael Espindola778fcc72014-11-02 13:28:57 +00001391 } else if (DGV) {
1392 if (shouldLinkFromSource(LinkFromSrc, *DGV, *SGV))
1393 return true;
1394 }
1395
Rafael Espindola4b5ec262015-12-02 22:59:04 +00001396 if (!LinkFromSrc && DGV) {
1397 // Make sure to remember this mapping.
1398 ValueMap[SGV] = ConstantExpr::getBitCast(DGV, TypeMap.get(SGV->getType()));
David Majnemerdad0a642014-06-27 18:19:56 +00001399 }
1400
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +00001401 if (DGV)
Rafael Espindola778fcc72014-11-02 13:28:57 +00001402 HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001403
Rafael Espindola778fcc72014-11-02 13:28:57 +00001404 GlobalValue *NewGV;
Rafael Espindolabaa3bf82015-12-01 15:19:48 +00001405 if (!LinkFromSrc && DGV) {
Rafael Espindola26c29512014-12-05 17:53:15 +00001406 NewGV = DGV;
Teresa Johnsonc7ed52f2015-11-03 00:14:15 +00001407 // When linking from source we setVisibility from copyGlobalValueProto.
1408 setVisibility(NewGV, SGV, DGV);
Rafael Espindola26c29512014-12-05 17:53:15 +00001409 } else {
Rafael Espindola4b5ec262015-12-02 22:59:04 +00001410 // If we are done linking global value bodies (i.e. we are performing
1411 // metadata linking), don't link in the global value due to this
1412 // reference, simply map it to null.
1413 if (DoneLinkingBodies)
1414 return false;
1415
Rafael Espindola792b7952015-12-03 14:48:20 +00001416 NewGV = copyGlobalValueProto(SGV, DGV, LinkFromSrc);
Rafael Espindola26c29512014-12-05 17:53:15 +00001417 }
Rafael Espindolafe3842c2014-09-09 17:48:18 +00001418
Rafael Espindolaad9d0ca2014-12-05 15:42:30 +00001419 NewGV->setUnnamedAddr(HasUnnamedAddr);
Rafael Espindola439835a2014-12-05 00:09:02 +00001420
Rafael Espindolaad9d0ca2014-12-05 15:42:30 +00001421 if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {
Rafael Espindolabaa3bf82015-12-01 15:19:48 +00001422 if (C && LinkFromSrc)
Rafael Espindolaad9d0ca2014-12-05 15:42:30 +00001423 NewGO->setComdat(C);
1424
1425 if (DGV && DGV->hasCommonLinkage() && SGV->hasCommonLinkage())
1426 NewGO->setAlignment(std::max(DGV->getAlignment(), SGV->getAlignment()));
1427 }
1428
Rafael Espindola879aeb72014-12-05 16:05:19 +00001429 if (auto *NewGVar = dyn_cast<GlobalVariable>(NewGV)) {
1430 auto *DGVar = dyn_cast_or_null<GlobalVariable>(DGV);
1431 auto *SGVar = dyn_cast<GlobalVariable>(SGV);
1432 if (DGVar && SGVar && DGVar->isDeclaration() && SGVar->isDeclaration() &&
1433 (!DGVar->isConstant() || !SGVar->isConstant()))
1434 NewGVar->setConstant(false);
1435 }
1436
Rafael Espindolaad9d0ca2014-12-05 15:42:30 +00001437 // Make sure to remember this mapping.
1438 if (NewGV != DGV) {
1439 if (DGV) {
1440 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewGV, DGV->getType()));
1441 DGV->eraseFromParent();
Chandler Carruthfd38af22014-11-02 09:10:31 +00001442 }
Rafael Espindolaad9d0ca2014-12-05 15:42:30 +00001443 ValueMap[SGV] = NewGV;
Reid Spencer361e5132004-11-12 20:37:43 +00001444 }
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001445
Rafael Espindola778fcc72014-11-02 13:28:57 +00001446 return false;
1447}
1448
Rafael Espindola18c89412014-10-27 02:35:46 +00001449/// Update the initializers in the Dest module now that all globals that may be
1450/// referenced are in Dest.
Rafael Espindolaef237112014-12-08 18:45:16 +00001451void ModuleLinker::linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src) {
1452 // Figure out what the initializer looks like in the dest module.
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +00001453 Dst.setInitializer(MapValue(Src.getInitializer(), ValueMap,
1454 RF_MoveDistinctMDs, &TypeMap, &ValMaterializer));
Reid Spencer361e5132004-11-12 20:37:43 +00001455}
1456
Rafael Espindola18c89412014-10-27 02:35:46 +00001457/// Copy the source function over into the dest function and fix up references
1458/// to values. At this point we know that Dest is an external function, and
1459/// that Src is not.
Rafael Espindolaef237112014-12-08 18:45:16 +00001460bool ModuleLinker::linkFunctionBody(Function &Dst, Function &Src) {
1461 assert(Dst.isDeclaration() && !Src.isDeclaration());
Reid Spencer361e5132004-11-12 20:37:43 +00001462
Rafael Espindola40d7ebe2014-12-08 13:29:33 +00001463 // Materialize if needed.
Rafael Espindola3519da82014-12-08 14:25:26 +00001464 if (std::error_code EC = Src.materialize())
Rafael Espindola40d7ebe2014-12-08 13:29:33 +00001465 return emitError(EC.message());
1466
Rafael Espindola869d1ce2014-12-08 13:44:38 +00001467 // Link in the prefix data.
Rafael Espindola3519da82014-12-08 14:25:26 +00001468 if (Src.hasPrefixData())
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +00001469 Dst.setPrefixData(MapValue(Src.getPrefixData(), ValueMap,
1470 RF_MoveDistinctMDs, &TypeMap, &ValMaterializer));
Rafael Espindola869d1ce2014-12-08 13:44:38 +00001471
1472 // Link in the prologue data.
Rafael Espindola3519da82014-12-08 14:25:26 +00001473 if (Src.hasPrologueData())
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +00001474 Dst.setPrologueData(MapValue(Src.getPrologueData(), ValueMap,
1475 RF_MoveDistinctMDs, &TypeMap,
1476 &ValMaterializer));
Rafael Espindola869d1ce2014-12-08 13:44:38 +00001477
David Majnemer7fddecc2015-06-17 20:52:32 +00001478 // Link in the personality function.
1479 if (Src.hasPersonalityFn())
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +00001480 Dst.setPersonalityFn(MapValue(Src.getPersonalityFn(), ValueMap,
1481 RF_MoveDistinctMDs, &TypeMap,
1482 &ValMaterializer));
David Majnemer7fddecc2015-06-17 20:52:32 +00001483
Chris Lattner7391dde2004-11-16 17:12:38 +00001484 // Go through and convert function arguments over, remembering the mapping.
Rafael Espindolaef237112014-12-08 18:45:16 +00001485 Function::arg_iterator DI = Dst.arg_begin();
Rafael Espindola3519da82014-12-08 14:25:26 +00001486 for (Argument &Arg : Src.args()) {
Rafael Espindolae3a933a2015-12-01 20:11:43 +00001487 DI->setName(Arg.getName()); // Copy the name over.
Reid Spencer361e5132004-11-12 20:37:43 +00001488
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001489 // Add a mapping to our mapping.
Duncan P. N. Exon Smith9934b262015-10-19 22:23:36 +00001490 ValueMap[&Arg] = &*DI;
Rafael Espindolaa314d1a2014-12-08 14:20:10 +00001491 ++DI;
Reid Spencer361e5132004-11-12 20:37:43 +00001492 }
1493
Duncan P. N. Exon Smithc8d987b2015-04-24 22:07:31 +00001494 // Copy over the metadata attachments.
1495 SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
1496 Src.getAllMetadata(MDs);
1497 for (const auto &I : MDs)
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +00001498 Dst.setMetadata(I.first, MapMetadata(I.second, ValueMap, RF_MoveDistinctMDs,
1499 &TypeMap, &ValMaterializer));
Duncan P. N. Exon Smithc8d987b2015-04-24 22:07:31 +00001500
Rafael Espindola9f8eff32014-10-28 00:24:16 +00001501 // Splice the body of the source function into the dest function.
Rafael Espindolaef237112014-12-08 18:45:16 +00001502 Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001503
Rafael Espindola9f8eff32014-10-28 00:24:16 +00001504 // At this point, all of the instructions and values of the function are now
1505 // copied over. The only problem is that they are still referencing values in
1506 // the Source function as operands. Loop through all of the operands of the
1507 // functions and patch them up to point to the local versions.
Rafael Espindolaef237112014-12-08 18:45:16 +00001508 for (BasicBlock &BB : Dst)
Rafael Espindolaa314d1a2014-12-08 14:20:10 +00001509 for (Instruction &I : BB)
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +00001510 RemapInstruction(&I, ValueMap,
1511 RF_IgnoreMissingEntries | RF_MoveDistinctMDs, &TypeMap,
Rafael Espindola9f8eff32014-10-28 00:24:16 +00001512 &ValMaterializer);
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001513
Chris Lattner7391dde2004-11-16 17:12:38 +00001514 // There is no need to map the arguments anymore.
Rafael Espindola3519da82014-12-08 14:25:26 +00001515 for (Argument &Arg : Src.args())
Rafael Espindolaa314d1a2014-12-08 14:20:10 +00001516 ValueMap.erase(&Arg);
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001517
Eric Christopher97cb5652015-05-15 18:20:14 +00001518 Src.dematerialize();
Rafael Espindola40d7ebe2014-12-08 13:29:33 +00001519 return false;
Reid Spencer361e5132004-11-12 20:37:43 +00001520}
1521
Rafael Espindolaef237112014-12-08 18:45:16 +00001522void ModuleLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) {
1523 Constant *Aliasee = Src.getAliasee();
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +00001524 Constant *Val = MapValue(Aliasee, ValueMap, RF_MoveDistinctMDs, &TypeMap,
1525 &ValMaterializer);
Rafael Espindolaef237112014-12-08 18:45:16 +00001526 Dst.setAliasee(Val);
1527}
1528
Rafael Espindolae39cd5b2015-12-01 22:40:40 +00001529bool ModuleLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) {
Teresa Johnson2d5fb8c2015-11-10 21:09:06 +00001530 if (const Comdat *SC = Src.getComdat()) {
1531 // To ensure that we don't generate an incomplete comdat group,
1532 // we must materialize and map in any other members that are not
1533 // yet materialized in Dst, which also ensures their definitions
1534 // are linked in. Otherwise, linkonce and other lazy linked GVs will
1535 // not be materialized if they aren't referenced.
1536 for (auto *SGV : ComdatMembers[SC]) {
Rafael Espindola19b52382015-11-27 20:28:19 +00001537 auto *DGV = cast_or_null<GlobalValue>(ValueMap[SGV]);
1538 if (DGV && !DGV->isDeclaration())
Teresa Johnson2d5fb8c2015-11-10 21:09:06 +00001539 continue;
Rafael Espindola19b52382015-11-27 20:28:19 +00001540 MapValue(SGV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
Teresa Johnson2d5fb8c2015-11-10 21:09:06 +00001541 }
1542 }
Artem Belevich020d4fb2015-09-01 17:55:55 +00001543 if (shouldInternalizeLinkedSymbols())
Rafael Espindolae39cd5b2015-12-01 22:40:40 +00001544 if (auto *DGV = dyn_cast<GlobalValue>(&Dst))
Artem Belevich020d4fb2015-09-01 17:55:55 +00001545 DGV->setLinkage(GlobalValue::InternalLinkage);
Rafael Espindolaef237112014-12-08 18:45:16 +00001546 if (auto *F = dyn_cast<Function>(&Src))
Rafael Espindolae39cd5b2015-12-01 22:40:40 +00001547 return linkFunctionBody(cast<Function>(Dst), *F);
Rafael Espindolaef237112014-12-08 18:45:16 +00001548 if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) {
Rafael Espindolae39cd5b2015-12-01 22:40:40 +00001549 linkGlobalInit(cast<GlobalVariable>(Dst), *GVar);
Rafael Espindolaef237112014-12-08 18:45:16 +00001550 return false;
Tanya Lattnercbb91402011-10-11 00:24:54 +00001551 }
Rafael Espindolae39cd5b2015-12-01 22:40:40 +00001552 linkAliasBody(cast<GlobalAlias>(Dst), cast<GlobalAlias>(Src));
Rafael Espindolaef237112014-12-08 18:45:16 +00001553 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001554}
Anton Korobeynikov26098882008-03-05 23:21:39 +00001555
Rafael Espindola18c89412014-10-27 02:35:46 +00001556/// Insert all of the named MDNodes in Src into the Dest module.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001557void ModuleLinker::linkNamedMDNodes() {
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001558 const NamedMDNode *SrcModFlags = SrcM.getModuleFlagsMetadata();
1559 for (const NamedMDNode &NMD : SrcM.named_metadata()) {
Bill Wendling66f02412012-02-11 11:38:06 +00001560 // Don't link module flags here. Do them separately.
Yaron Keren3bf3f1f2015-06-15 16:20:16 +00001561 if (&NMD == SrcModFlags)
1562 continue;
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001563 NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001564 // Add Src elements into Dest node.
Yaron Keren3bf3f1f2015-06-15 16:20:16 +00001565 for (const MDNode *op : NMD.operands())
Teresa Johnson83d03dd2015-11-15 14:50:14 +00001566 DestNMD->addOperand(MapMetadata(
1567 op, ValueMap, RF_MoveDistinctMDs | RF_NullMapMissingGlobalValues,
1568 &TypeMap, &ValMaterializer));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001569 }
1570}
Bill Wendling66f02412012-02-11 11:38:06 +00001571
Rafael Espindola18c89412014-10-27 02:35:46 +00001572/// Merge the linker flags in Src into the Dest module.
Bill Wendling66f02412012-02-11 11:38:06 +00001573bool ModuleLinker::linkModuleFlagsMetadata() {
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001574 // If the source module has no module flags, we are done.
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001575 const NamedMDNode *SrcModFlags = SrcM.getModuleFlagsMetadata();
Rafael Espindolae3a933a2015-12-01 20:11:43 +00001576 if (!SrcModFlags)
1577 return false;
Bill Wendling66f02412012-02-11 11:38:06 +00001578
Bill Wendling66f02412012-02-11 11:38:06 +00001579 // If the destination module doesn't have module flags yet, then just copy
1580 // over the source module's flags.
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001581 NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata();
Bill Wendling66f02412012-02-11 11:38:06 +00001582 if (DstModFlags->getNumOperands() == 0) {
1583 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1584 DstModFlags->addOperand(SrcModFlags->getOperand(I));
1585
1586 return false;
1587 }
1588
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001589 // First build a map of the existing module flags and requirements.
Duncan P. N. Exon Smithdf55d8b2015-01-07 21:32:27 +00001590 DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags;
Rafael Espindolae3a933a2015-12-01 20:11:43 +00001591 SmallSetVector<MDNode *, 16> Requirements;
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001592 for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001593 MDNode *Op = DstModFlags->getOperand(I);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001594 ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0));
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001595 MDString *ID = cast<MDString>(Op->getOperand(1));
Bill Wendling66f02412012-02-11 11:38:06 +00001596
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001597 if (Behavior->getZExtValue() == Module::Require) {
1598 Requirements.insert(cast<MDNode>(Op->getOperand(2)));
1599 } else {
Duncan P. N. Exon Smithdf55d8b2015-01-07 21:32:27 +00001600 Flags[ID] = std::make_pair(Op, I);
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001601 }
Bill Wendling66f02412012-02-11 11:38:06 +00001602 }
1603
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001604 // Merge in the flags from the source module, and also collect its set of
1605 // requirements.
1606 bool HasErr = false;
1607 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001608 MDNode *SrcOp = SrcModFlags->getOperand(I);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001609 ConstantInt *SrcBehavior =
1610 mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001611 MDString *ID = cast<MDString>(SrcOp->getOperand(1));
Duncan P. N. Exon Smithdf55d8b2015-01-07 21:32:27 +00001612 MDNode *DstOp;
1613 unsigned DstIndex;
1614 std::tie(DstOp, DstIndex) = Flags.lookup(ID);
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001615 unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
Bill Wendling66f02412012-02-11 11:38:06 +00001616
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001617 // If this is a requirement, add it and continue.
1618 if (SrcBehaviorValue == Module::Require) {
1619 // If the destination module does not already have this requirement, add
1620 // it.
1621 if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
1622 DstModFlags->addOperand(SrcOp);
1623 }
1624 continue;
Bill Wendling66f02412012-02-11 11:38:06 +00001625 }
1626
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001627 // If there is no existing flag with this ID, just add it.
1628 if (!DstOp) {
Duncan P. N. Exon Smithdf55d8b2015-01-07 21:32:27 +00001629 Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands());
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001630 DstModFlags->addOperand(SrcOp);
1631 continue;
1632 }
1633
1634 // Otherwise, perform a merge.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001635 ConstantInt *DstBehavior =
1636 mdconst::extract<ConstantInt>(DstOp->getOperand(0));
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001637 unsigned DstBehaviorValue = DstBehavior->getZExtValue();
1638
1639 // If either flag has override behavior, handle it first.
1640 if (DstBehaviorValue == Module::Override) {
1641 // Diagnose inconsistent flags which both have override behavior.
1642 if (SrcBehaviorValue == Module::Override &&
1643 SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1644 HasErr |= emitError("linking module flags '" + ID->getString() +
1645 "': IDs have conflicting override values");
1646 }
1647 continue;
1648 } else if (SrcBehaviorValue == Module::Override) {
1649 // Update the destination flag to that of the source.
Duncan P. N. Exon Smithdf55d8b2015-01-07 21:32:27 +00001650 DstModFlags->setOperand(DstIndex, SrcOp);
1651 Flags[ID].first = SrcOp;
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001652 continue;
1653 }
1654
1655 // Diagnose inconsistent merge behavior types.
1656 if (SrcBehaviorValue != DstBehaviorValue) {
1657 HasErr |= emitError("linking module flags '" + ID->getString() +
1658 "': IDs have conflicting behaviors");
1659 continue;
1660 }
1661
Duncan P. N. Exon Smithdf55d8b2015-01-07 21:32:27 +00001662 auto replaceDstValue = [&](MDNode *New) {
1663 Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001664 MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);
Duncan P. N. Exon Smithdf55d8b2015-01-07 21:32:27 +00001665 DstModFlags->setOperand(DstIndex, Flag);
1666 Flags[ID].first = Flag;
1667 };
1668
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001669 // Perform the merge for standard behavior types.
1670 switch (SrcBehaviorValue) {
1671 case Module::Require:
Rafael Espindolae3a933a2015-12-01 20:11:43 +00001672 case Module::Override:
1673 llvm_unreachable("not possible");
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001674 case Module::Error: {
1675 // Emit an error if the values differ.
1676 if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1677 HasErr |= emitError("linking module flags '" + ID->getString() +
1678 "': IDs have conflicting values");
1679 }
1680 continue;
1681 }
1682 case Module::Warning: {
1683 // Emit a warning if the values differ.
1684 if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001685 emitWarning("linking module flags '" + ID->getString() +
1686 "': IDs have conflicting values");
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001687 }
1688 continue;
1689 }
Daniel Dunbard77d9fb2013-01-16 21:38:56 +00001690 case Module::Append: {
1691 MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1692 MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001693 SmallVector<Metadata *, 8> MDs;
1694 MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands());
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001695 MDs.append(DstValue->op_begin(), DstValue->op_end());
1696 MDs.append(SrcValue->op_begin(), SrcValue->op_end());
Duncan P. N. Exon Smithdf55d8b2015-01-07 21:32:27 +00001697
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001698 replaceDstValue(MDNode::get(DstM.getContext(), MDs));
Daniel Dunbard77d9fb2013-01-16 21:38:56 +00001699 break;
1700 }
1701 case Module::AppendUnique: {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001702 SmallSetVector<Metadata *, 16> Elts;
Daniel Dunbard77d9fb2013-01-16 21:38:56 +00001703 MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1704 MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001705 Elts.insert(DstValue->op_begin(), DstValue->op_end());
1706 Elts.insert(SrcValue->op_begin(), SrcValue->op_end());
Duncan P. N. Exon Smithdf55d8b2015-01-07 21:32:27 +00001707
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001708 replaceDstValue(MDNode::get(DstM.getContext(),
Duncan P. N. Exon Smithdf55d8b2015-01-07 21:32:27 +00001709 makeArrayRef(Elts.begin(), Elts.end())));
Daniel Dunbard77d9fb2013-01-16 21:38:56 +00001710 break;
1711 }
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001712 }
Bill Wendling66f02412012-02-11 11:38:06 +00001713 }
1714
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001715 // Check all of the requirements.
1716 for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
1717 MDNode *Requirement = Requirements[I];
1718 MDString *Flag = cast<MDString>(Requirement->getOperand(0));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001719 Metadata *ReqValue = Requirement->getOperand(1);
Bill Wendling66f02412012-02-11 11:38:06 +00001720
Duncan P. N. Exon Smithdf55d8b2015-01-07 21:32:27 +00001721 MDNode *Op = Flags[Flag].first;
Daniel Dunbar0ec72bb2013-01-16 18:39:23 +00001722 if (!Op || Op->getOperand(2) != ReqValue) {
1723 HasErr |= emitError("linking module flags '" + Flag->getString() +
1724 "': does not have the required value");
1725 continue;
Bill Wendling66f02412012-02-11 11:38:06 +00001726 }
1727 }
1728
1729 return HasErr;
1730}
Rafael Espindolaed6dc372014-05-09 14:39:25 +00001731
Akira Hatanakac43df512015-02-13 00:40:41 +00001732// This function returns true if the triples match.
1733static bool triplesMatch(const Triple &T0, const Triple &T1) {
1734 // If vendor is apple, ignore the version number.
1735 if (T0.getVendor() == Triple::Apple)
Rafael Espindolae3a933a2015-12-01 20:11:43 +00001736 return T0.getArch() == T1.getArch() && T0.getSubArch() == T1.getSubArch() &&
1737 T0.getVendor() == T1.getVendor() && T0.getOS() == T1.getOS();
Akira Hatanakac43df512015-02-13 00:40:41 +00001738
1739 return T0 == T1;
1740}
1741
1742// This function returns the merged triple.
Rafael Espindolae3a933a2015-12-01 20:11:43 +00001743static std::string mergeTriples(const Triple &SrcTriple,
1744 const Triple &DstTriple) {
Akira Hatanakac43df512015-02-13 00:40:41 +00001745 // If vendor is apple, pick the triple with the larger version number.
1746 if (SrcTriple.getVendor() == Triple::Apple)
1747 if (DstTriple.isOSVersionLT(SrcTriple))
1748 return SrcTriple.str();
1749
1750 return DstTriple.str();
1751}
1752
Rafael Espindolabaa3bf82015-12-01 15:19:48 +00001753bool ModuleLinker::linkIfNeeded(GlobalValue &GV) {
1754 GlobalValue *DGV = getLinkedToGlobal(&GV);
1755
1756 if (shouldLinkOnlyNeeded() && !(DGV && DGV->isDeclaration()))
1757 return false;
1758
Rafael Espindola4b5ec262015-12-02 22:59:04 +00001759 if (DGV && !GV.hasLocalLinkage() && !GV.hasAppendingLinkage()) {
1760 auto *DGVar = dyn_cast<GlobalVariable>(DGV);
1761 auto *SGVar = dyn_cast<GlobalVariable>(&GV);
1762 if (DGVar && SGVar) {
1763 if (DGVar->isDeclaration() && SGVar->isDeclaration() &&
1764 (!DGVar->isConstant() || !SGVar->isConstant())) {
1765 DGVar->setConstant(false);
1766 SGVar->setConstant(false);
1767 }
1768 if (DGVar->hasCommonLinkage() && SGVar->hasCommonLinkage()) {
1769 unsigned Align = std::max(DGVar->getAlignment(), SGVar->getAlignment());
1770 SGVar->setAlignment(Align);
1771 DGVar->setAlignment(Align);
1772 }
1773 }
1774
Rafael Espindolabaa3bf82015-12-01 15:19:48 +00001775 GlobalValue::VisibilityTypes Visibility =
1776 getMinVisibility(DGV->getVisibility(), GV.getVisibility());
1777 DGV->setVisibility(Visibility);
1778 GV.setVisibility(Visibility);
Rafael Espindola4b5ec262015-12-02 22:59:04 +00001779
1780 bool HasUnnamedAddr = GV.hasUnnamedAddr() && DGV->hasUnnamedAddr();
1781 DGV->setUnnamedAddr(HasUnnamedAddr);
1782 GV.setUnnamedAddr(HasUnnamedAddr);
Rafael Espindolabaa3bf82015-12-01 15:19:48 +00001783 }
1784
Rafael Espindola4b5ec262015-12-02 22:59:04 +00001785 // Don't want to append to global_ctors list, for example, when we
1786 // are importing for ThinLTO, otherwise the global ctors and dtors
1787 // get executed multiple times for local variables (the latter causing
1788 // double frees).
1789 if (GV.hasAppendingLinkage() && isPerformingImport())
1790 return false;
1791
1792 if (isPerformingImport() && !doImportAsDefinition(&GV))
1793 return false;
1794
1795 if (!DGV && !shouldOverrideFromSrc() &&
1796 (GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() ||
1797 GV.hasAvailableExternallyLinkage()))
1798 return false;
1799
Rafael Espindolabaa3bf82015-12-01 15:19:48 +00001800 if (const Comdat *SC = GV.getComdat()) {
1801 bool LinkFromSrc;
1802 Comdat::SelectionKind SK;
1803 std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
Rafael Espindola4b5ec262015-12-02 22:59:04 +00001804 if (LinkFromSrc)
1805 ValuesToLink.insert(&GV);
Rafael Espindolabaa3bf82015-12-01 15:19:48 +00001806 return false;
1807 }
Rafael Espindola4b5ec262015-12-02 22:59:04 +00001808
1809 bool LinkFromSrc = true;
1810 if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, GV))
1811 return true;
1812 if (LinkFromSrc)
1813 ValuesToLink.insert(&GV);
1814 return false;
Rafael Espindolabaa3bf82015-12-01 15:19:48 +00001815}
1816
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001817bool ModuleLinker::run() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001818 // Inherit the target data from the source module if the destination module
1819 // doesn't have one already.
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001820 if (DstM.getDataLayout().isDefault())
1821 DstM.setDataLayout(SrcM.getDataLayout());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001822
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001823 if (SrcM.getDataLayout() != DstM.getDataLayout()) {
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001824 emitWarning("Linking two modules of different data layouts: '" +
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001825 SrcM.getModuleIdentifier() + "' is '" +
1826 SrcM.getDataLayoutStr() + "' whereas '" +
1827 DstM.getModuleIdentifier() + "' is '" +
1828 DstM.getDataLayoutStr() + "'\n");
Eli Benderskye17f3702014-02-06 18:01:56 +00001829 }
Akira Hatanakac43df512015-02-13 00:40:41 +00001830
1831 // Copy the target triple from the source to dest if the dest's is empty.
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001832 if (DstM.getTargetTriple().empty() && !SrcM.getTargetTriple().empty())
1833 DstM.setTargetTriple(SrcM.getTargetTriple());
Akira Hatanakac43df512015-02-13 00:40:41 +00001834
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001835 Triple SrcTriple(SrcM.getTargetTriple()), DstTriple(DstM.getTargetTriple());
Akira Hatanakac43df512015-02-13 00:40:41 +00001836
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001837 if (!SrcM.getTargetTriple().empty() && !triplesMatch(SrcTriple, DstTriple))
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001838 emitWarning("Linking two modules of different target triples: " +
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001839 SrcM.getModuleIdentifier() + "' is '" + SrcM.getTargetTriple() +
1840 "' whereas '" + DstM.getModuleIdentifier() + "' is '" +
1841 DstM.getTargetTriple() + "'\n");
Akira Hatanakac43df512015-02-13 00:40:41 +00001842
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001843 DstM.setTargetTriple(mergeTriples(SrcTriple, DstTriple));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001844
1845 // Append the module inline asm string.
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001846 if (!SrcM.getModuleInlineAsm().empty()) {
1847 if (DstM.getModuleInlineAsm().empty())
1848 DstM.setModuleInlineAsm(SrcM.getModuleInlineAsm());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001849 else
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001850 DstM.setModuleInlineAsm(DstM.getModuleInlineAsm() + "\n" +
1851 SrcM.getModuleInlineAsm());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001852 }
1853
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001854 // Loop over all of the linked values to compute type mappings.
1855 computeTypeMapping();
1856
David Majnemerdad0a642014-06-27 18:19:56 +00001857 ComdatsChosen.clear();
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001858 for (const auto &SMEC : SrcM.getComdatSymbolTable()) {
David Majnemerdad0a642014-06-27 18:19:56 +00001859 const Comdat &C = SMEC.getValue();
1860 if (ComdatsChosen.count(&C))
1861 continue;
1862 Comdat::SelectionKind SK;
1863 bool LinkFromSrc;
1864 if (getComdatResult(&C, SK, LinkFromSrc))
1865 return true;
1866 ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc);
1867 }
1868
Duncan P. N. Exon Smith09d84ad2014-08-12 16:46:37 +00001869 // Upgrade mismatched global arrays.
1870 upgradeMismatchedGlobals();
1871
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001872 for (GlobalVariable &GV : SrcM.globals())
Rafael Espindolabaa3bf82015-12-01 15:19:48 +00001873 if (const Comdat *SC = GV.getComdat())
1874 ComdatMembers[SC].push_back(&GV);
1875
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001876 for (Function &SF : SrcM)
Rafael Espindolabaa3bf82015-12-01 15:19:48 +00001877 if (const Comdat *SC = SF.getComdat())
1878 ComdatMembers[SC].push_back(&SF);
1879
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001880 for (GlobalAlias &GA : SrcM.aliases())
Rafael Espindolabaa3bf82015-12-01 15:19:48 +00001881 if (const Comdat *SC = GA.getComdat())
1882 ComdatMembers[SC].push_back(&GA);
1883
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001884 // Insert all of the globals in src into the DstM module... without linking
1885 // initializers (which could refer to functions not yet mapped over).
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001886 for (GlobalVariable &GV : SrcM.globals())
Rafael Espindolabaa3bf82015-12-01 15:19:48 +00001887 if (linkIfNeeded(GV))
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001888 return true;
1889
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001890 for (Function &SF : SrcM)
Rafael Espindolabaa3bf82015-12-01 15:19:48 +00001891 if (linkIfNeeded(SF))
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001892 return true;
1893
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001894 for (GlobalAlias &GA : SrcM.aliases())
Rafael Espindolabaa3bf82015-12-01 15:19:48 +00001895 if (linkIfNeeded(GA))
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001896 return true;
1897
Rafael Espindola4b5ec262015-12-02 22:59:04 +00001898 for (GlobalValue *GV : ValuesToLink) {
1899 MapValue(GV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
1900 if (HasError)
1901 return true;
Rafael Espindolabeadd562014-12-08 18:05:48 +00001902 }
1903
Teresa Johnson10632932015-11-06 17:50:53 +00001904 // Note that we are done linking global value bodies. This prevents
1905 // metadata linking from creating new references.
1906 DoneLinkingBodies = true;
1907
Teresa Johnson189b2522015-11-06 17:50:48 +00001908 // Remap all of the named MDNodes in Src into the DstM module. We do this
1909 // after linking GlobalValues so that MDNodes that reference GlobalValues
1910 // are properly remapped.
1911 linkNamedMDNodes();
1912
1913 // Merge the module flags into the DstM module.
1914 if (linkModuleFlagsMetadata())
1915 return true;
1916
Anton Korobeynikov26098882008-03-05 23:21:39 +00001917 return false;
1918}
Reid Spencer361e5132004-11-12 20:37:43 +00001919
Rafael Espindola31ad4682014-12-03 22:36:37 +00001920Linker::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P)
1921 : ETypes(E), IsPacked(P) {}
1922
1923Linker::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST)
1924 : ETypes(ST->elements()), IsPacked(ST->isPacked()) {}
1925
1926bool Linker::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const {
1927 if (IsPacked != That.IsPacked)
1928 return false;
1929 if (ETypes != That.ETypes)
1930 return false;
1931 return true;
1932}
1933
1934bool Linker::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const {
1935 return !this->operator==(That);
1936}
1937
1938StructType *Linker::StructTypeKeyInfo::getEmptyKey() {
1939 return DenseMapInfo<StructType *>::getEmptyKey();
1940}
1941
1942StructType *Linker::StructTypeKeyInfo::getTombstoneKey() {
1943 return DenseMapInfo<StructType *>::getTombstoneKey();
1944}
1945
1946unsigned Linker::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {
1947 return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()),
1948 Key.IsPacked);
1949}
1950
1951unsigned Linker::StructTypeKeyInfo::getHashValue(const StructType *ST) {
1952 return getHashValue(KeyTy(ST));
1953}
1954
1955bool Linker::StructTypeKeyInfo::isEqual(const KeyTy &LHS,
1956 const StructType *RHS) {
1957 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1958 return false;
1959 return LHS == KeyTy(RHS);
1960}
1961
1962bool Linker::StructTypeKeyInfo::isEqual(const StructType *LHS,
1963 const StructType *RHS) {
1964 if (RHS == getEmptyKey())
1965 return LHS == getEmptyKey();
1966
1967 if (RHS == getTombstoneKey())
1968 return LHS == getTombstoneKey();
1969
1970 return KeyTy(LHS) == KeyTy(RHS);
1971}
1972
1973void Linker::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) {
1974 assert(!Ty->isOpaque());
Benjamin Kramer3280a5d2014-12-06 19:22:54 +00001975 NonOpaqueStructTypes.insert(Ty);
Rafael Espindola31ad4682014-12-03 22:36:37 +00001976}
1977
Rafael Espindolaa5b9e1c2015-03-06 00:50:21 +00001978void Linker::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) {
1979 assert(!Ty->isOpaque());
1980 NonOpaqueStructTypes.insert(Ty);
1981 bool Removed = OpaqueStructTypes.erase(Ty);
1982 (void)Removed;
1983 assert(Removed);
1984}
1985
Rafael Espindola31ad4682014-12-03 22:36:37 +00001986void Linker::IdentifiedStructTypeSet::addOpaque(StructType *Ty) {
1987 assert(Ty->isOpaque());
1988 OpaqueStructTypes.insert(Ty);
1989}
1990
1991StructType *
1992Linker::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes,
1993 bool IsPacked) {
1994 Linker::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked);
1995 auto I = NonOpaqueStructTypes.find_as(Key);
1996 if (I == NonOpaqueStructTypes.end())
1997 return nullptr;
Benjamin Kramer3280a5d2014-12-06 19:22:54 +00001998 return *I;
Rafael Espindola31ad4682014-12-03 22:36:37 +00001999}
2000
2001bool Linker::IdentifiedStructTypeSet::hasType(StructType *Ty) {
2002 if (Ty->isOpaque())
2003 return OpaqueStructTypes.count(Ty);
2004 auto I = NonOpaqueStructTypes.find(Ty);
2005 if (I == NonOpaqueStructTypes.end())
2006 return false;
Benjamin Kramer3280a5d2014-12-06 19:22:54 +00002007 return *I == Ty;
Rafael Espindola31ad4682014-12-03 22:36:37 +00002008}
2009
Rafael Espindola0e309fe2015-12-01 19:50:54 +00002010Linker::Linker(Module &M, DiagnosticHandlerFunction DiagnosticHandler)
2011 : Composite(M), DiagnosticHandler(DiagnosticHandler) {
Rafael Espindolaa4e85e32014-12-01 16:32:20 +00002012 TypeFinder StructTypes;
Rafael Espindola0e309fe2015-12-01 19:50:54 +00002013 StructTypes.run(M, true);
Rafael Espindola31ad4682014-12-03 22:36:37 +00002014 for (StructType *Ty : StructTypes) {
2015 if (Ty->isOpaque())
2016 IdentifiedStructTypes.addOpaque(Ty);
2017 else
2018 IdentifiedStructTypes.addNonOpaque(Ty);
2019 }
Rafael Espindolaaa9918a2013-05-04 05:05:18 +00002020}
Rafael Espindola3df61b72013-05-04 03:48:37 +00002021
Rafael Espindola0e309fe2015-12-01 19:50:54 +00002022Linker::Linker(Module &M)
Rafael Espindola4dbdceb2015-12-01 18:46:19 +00002023 : Linker(M, [this](const DiagnosticInfo &DI) {
Rafael Espindola0e309fe2015-12-01 19:50:54 +00002024 Composite.getContext().diagnose(DI);
Rafael Espindola4dbdceb2015-12-01 18:46:19 +00002025 }) {}
Rafael Espindola5cb9c822014-11-17 20:51:01 +00002026
Rafael Espindola0e309fe2015-12-01 19:50:54 +00002027bool Linker::linkInModule(Module &Src, unsigned Flags,
Mehdi Amini8220e8a2015-11-23 01:59:16 +00002028 const FunctionInfoIndex *Index,
Mehdi Amini7471cf82015-12-03 02:37:30 +00002029 DenseSet<const GlobalValue *> *FunctionsToImport) {
Rafael Espindolaa4e85e32014-12-01 16:32:20 +00002030 ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src,
Mehdi Amini7471cf82015-12-03 02:37:30 +00002031 DiagnosticHandler, Flags, Index, FunctionsToImport);
Manman Rendab999d2015-01-20 19:24:59 +00002032 bool RetCode = TheLinker.run();
Rafael Espindola0e309fe2015-12-01 19:50:54 +00002033 Composite.dropTriviallyDeadConstantArrays();
Manman Rendab999d2015-01-20 19:24:59 +00002034 return RetCode;
Rafael Espindola3df61b72013-05-04 03:48:37 +00002035}
2036
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002037//===----------------------------------------------------------------------===//
2038// LinkModules entrypoint.
2039//===----------------------------------------------------------------------===//
2040
Rafael Espindola18c89412014-10-27 02:35:46 +00002041/// This function links two modules together, with the resulting Dest module
2042/// modified to be the composite of the two input modules. If an error occurs,
2043/// true is returned and ErrorMsg (if not null) is set to indicate the problem.
2044/// Upon failure, the Dest module could be in a modified state, and shouldn't be
2045/// relied on to be consistent.
Rafael Espindola0e309fe2015-12-01 19:50:54 +00002046bool Linker::linkModules(Module &Dest, Module &Src,
Artem Belevich020d4fb2015-09-01 17:55:55 +00002047 DiagnosticHandlerFunction DiagnosticHandler,
2048 unsigned Flags) {
Rafael Espindola4160f5d2014-10-27 23:02:10 +00002049 Linker L(Dest, DiagnosticHandler);
Artem Belevich020d4fb2015-09-01 17:55:55 +00002050 return L.linkInModule(Src, Flags);
Rafael Espindola4160f5d2014-10-27 23:02:10 +00002051}
2052
Rafael Espindola0e309fe2015-12-01 19:50:54 +00002053bool Linker::linkModules(Module &Dest, Module &Src, unsigned Flags) {
Rafael Espindola287f18b2013-05-04 04:08:02 +00002054 Linker L(Dest);
Artem Belevich020d4fb2015-09-01 17:55:55 +00002055 return L.linkInModule(Src, Flags);
Reid Spencer361e5132004-11-12 20:37:43 +00002056}
Bill Wendlinga3aeb982012-05-09 08:55:40 +00002057
2058//===----------------------------------------------------------------------===//
2059// C API.
2060//===----------------------------------------------------------------------===//
2061
2062LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
Juergen Ributzkaa57d5882015-03-02 18:59:38 +00002063 LLVMLinkerMode Unused, char **OutMessages) {
Rafael Espindola98ab63c2014-10-25 04:31:08 +00002064 Module *D = unwrap(Dest);
Rafael Espindola98ab63c2014-10-25 04:31:08 +00002065 std::string Message;
Rafael Espindola4160f5d2014-10-27 23:02:10 +00002066 raw_string_ostream Stream(Message);
2067 DiagnosticPrinterRawOStream DP(Stream);
2068
Rafael Espindola0e309fe2015-12-01 19:50:54 +00002069 LLVMBool Result = Linker::linkModules(
2070 *D, *unwrap(Src), [&](const DiagnosticInfo &DI) { DI.print(DP); });
Rafael Espindola98ab63c2014-10-25 04:31:08 +00002071
Eli Benderskyff715e22015-06-12 23:26:42 +00002072 if (OutMessages && Result) {
2073 Stream.flush();
Rafael Espindola98ab63c2014-10-25 04:31:08 +00002074 *OutMessages = strdup(Message.c_str());
Eli Benderskyff715e22015-06-12 23:26:42 +00002075 }
Bill Wendlinga3aeb982012-05-09 08:55:40 +00002076 return Result;
2077}