/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/Linker/LinkModules.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===// |
2 | | // |
3 | | // The LLVM Compiler Infrastructure |
4 | | // |
5 | | // This file is distributed under the University of Illinois Open Source |
6 | | // License. See LICENSE.TXT for details. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | // |
10 | | // This file implements the LLVM module linker. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "LinkDiagnosticInfo.h" |
15 | | #include "llvm-c/Linker.h" |
16 | | #include "llvm/ADT/SetVector.h" |
17 | | #include "llvm/ADT/StringSet.h" |
18 | | #include "llvm/IR/DiagnosticPrinter.h" |
19 | | #include "llvm/IR/LLVMContext.h" |
20 | | #include "llvm/Linker/Linker.h" |
21 | | #include "llvm/Support/Error.h" |
22 | | #include "llvm/Transforms/Utils/FunctionImportUtils.h" |
23 | | using namespace llvm; |
24 | | |
25 | | namespace { |
26 | | |
27 | | /// This is an implementation class for the LinkModules function, which is the |
28 | | /// entrypoint for this file. |
29 | | class ModuleLinker { |
30 | | IRMover &Mover; |
31 | | std::unique_ptr<Module> SrcM; |
32 | | |
33 | | SetVector<GlobalValue *> ValuesToLink; |
34 | | StringSet<> Internalize; |
35 | | |
36 | | /// For symbol clashes, prefer those from Src. |
37 | | unsigned Flags; |
38 | | |
39 | | /// Functions to import from source module, all other functions are |
40 | | /// imported as declarations instead of definitions. |
41 | | DenseSet<const GlobalValue *> *GlobalsToImport; |
42 | | |
43 | | /// Used as the callback for lazy linking. |
44 | | /// The mover has just hit GV and we have to decide if it, and other members |
45 | | /// of the same comdat, should be linked. Every member to be linked is passed |
46 | | /// to Add. |
47 | | void addLazyFor(GlobalValue &GV, const IRMover::ValueAdder &Add); |
48 | | |
49 | 115 | bool shouldLinkReferencedLinkOnce() { |
50 | 115 | return !(Flags & Linker::DontForceLinkLinkonceODR); |
51 | 115 | } |
52 | 1.19k | bool shouldOverrideFromSrc() { return Flags & Linker::OverrideFromSrc; } |
53 | 1.74k | bool shouldLinkOnlyNeeded() { return Flags & Linker::LinkOnlyNeeded; } |
54 | 599 | bool shouldInternalizeLinkedSymbols() { |
55 | 599 | return Flags & Linker::InternalizeLinkedSymbols; |
56 | 599 | } |
57 | | |
58 | | bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest, |
59 | | const GlobalValue &Src); |
60 | | |
61 | | /// Should we have mover and linker error diag info? |
62 | 10 | bool emitError(const Twine &Message) { |
63 | 10 | SrcM->getContext().diagnose(LinkDiagnosticInfo(DS_Error, Message)); |
64 | 10 | return true; |
65 | 10 | } |
66 | | |
67 | | bool getComdatLeader(Module &M, StringRef ComdatName, |
68 | | const GlobalVariable *&GVar); |
69 | | bool computeResultingSelectionKind(StringRef ComdatName, |
70 | | Comdat::SelectionKind Src, |
71 | | Comdat::SelectionKind Dst, |
72 | | Comdat::SelectionKind &Result, |
73 | | bool &LinkFromSrc); |
74 | | std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>> |
75 | | ComdatsChosen; |
76 | | bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK, |
77 | | bool &LinkFromSrc); |
78 | | // Keep track of the lazy linked global members of each comdat in source. |
79 | | DenseMap<const Comdat *, std::vector<GlobalValue *>> LazyComdatMembers; |
80 | | |
81 | | /// Given a global in the source module, return the global in the |
82 | | /// destination module that is being linked to, if any. |
83 | 1.74k | GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) { |
84 | 1.74k | Module &DstM = Mover.getModule(); |
85 | 1.74k | // If the source has no name it can't link. If it has local linkage, |
86 | 1.74k | // there is no name match-up going on. |
87 | 1.74k | if (!SrcGV->hasName() || 1.74k GlobalValue::isLocalLinkage(SrcGV->getLinkage())1.74k ) |
88 | 67 | return nullptr; |
89 | 1.74k | |
90 | 1.74k | // Otherwise see if we have a match in the destination module's symtab. |
91 | 1.67k | GlobalValue *DGV = DstM.getNamedValue(SrcGV->getName()); |
92 | 1.67k | if (!DGV) |
93 | 1.19k | return nullptr; |
94 | 1.67k | |
95 | 1.67k | // If we found a global with the same name in the dest module, but it has |
96 | 1.67k | // internal linkage, we are really not doing any linkage here. |
97 | 482 | if (482 DGV->hasLocalLinkage()482 ) |
98 | 7 | return nullptr; |
99 | 482 | |
100 | 482 | // Otherwise, we do in fact link to the destination global. |
101 | 475 | return DGV; |
102 | 482 | } |
103 | | |
104 | | /// Drop GV if it is a member of a comdat that we are dropping. |
105 | | /// This can happen with COFF's largest selection kind. |
106 | | void dropReplacedComdat(GlobalValue &GV, |
107 | | const DenseSet<const Comdat *> &ReplacedDstComdats); |
108 | | |
109 | | bool linkIfNeeded(GlobalValue &GV); |
110 | | |
111 | | /// Helper method to check if we are importing from the current source |
112 | | /// module. |
113 | 3.62k | bool isPerformingImport() const { return GlobalsToImport != nullptr; } |
114 | | |
115 | | /// If we are importing from the source module, checks if we should |
116 | | /// import SGV as a definition, otherwise import as a declaration. |
117 | | bool doImportAsDefinition(const GlobalValue *SGV); |
118 | | |
119 | | public: |
120 | | ModuleLinker(IRMover &Mover, std::unique_ptr<Module> SrcM, unsigned Flags, |
121 | | DenseSet<const GlobalValue *> *GlobalsToImport = nullptr) |
122 | | : Mover(Mover), SrcM(std::move(SrcM)), Flags(Flags), |
123 | 565 | GlobalsToImport(GlobalsToImport) {} |
124 | | |
125 | | bool run(); |
126 | | }; |
127 | | } |
128 | | |
129 | 548 | bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) { |
130 | 548 | if (!isPerformingImport()) |
131 | 0 | return false; |
132 | 548 | return FunctionImportGlobalProcessing::doImportAsDefinition(SGV, |
133 | 548 | GlobalsToImport); |
134 | 548 | } |
135 | | |
136 | | static GlobalValue::VisibilityTypes |
137 | | getMinVisibility(GlobalValue::VisibilityTypes A, |
138 | 463 | GlobalValue::VisibilityTypes B) { |
139 | 463 | if (A == GlobalValue::HiddenVisibility || 463 B == GlobalValue::HiddenVisibility455 ) |
140 | 15 | return GlobalValue::HiddenVisibility; |
141 | 448 | if (448 A == GlobalValue::ProtectedVisibility ||448 |
142 | 445 | B == GlobalValue::ProtectedVisibility) |
143 | 7 | return GlobalValue::ProtectedVisibility; |
144 | 441 | return GlobalValue::DefaultVisibility; |
145 | 448 | } |
146 | | |
147 | | bool ModuleLinker::getComdatLeader(Module &M, StringRef ComdatName, |
148 | 12 | const GlobalVariable *&GVar) { |
149 | 12 | const GlobalValue *GVal = M.getNamedValue(ComdatName); |
150 | 12 | if (const auto *GA12 = dyn_cast_or_null<GlobalAlias>(GVal)) {2 |
151 | 2 | GVal = GA->getBaseObject(); |
152 | 2 | if (!GVal) |
153 | 2 | // We cannot resolve the size of the aliasee yet. |
154 | 1 | return emitError("Linking COMDATs named '" + ComdatName + |
155 | 1 | "': COMDAT key involves incomputable alias size."); |
156 | 2 | } |
157 | 12 | |
158 | 11 | GVar = dyn_cast_or_null<GlobalVariable>(GVal); |
159 | 11 | if (!GVar) |
160 | 1 | return emitError( |
161 | 1 | "Linking COMDATs named '" + ComdatName + |
162 | 1 | "': GlobalVariable required for data dependent selection!"); |
163 | 11 | |
164 | 10 | return false; |
165 | 11 | } |
166 | | |
167 | | bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName, |
168 | | Comdat::SelectionKind Src, |
169 | | Comdat::SelectionKind Dst, |
170 | | Comdat::SelectionKind &Result, |
171 | 19 | bool &LinkFromSrc) { |
172 | 19 | Module &DstM = Mover.getModule(); |
173 | 19 | // The ability to mix Comdat::SelectionKind::Any with |
174 | 19 | // Comdat::SelectionKind::Largest is a behavior that comes from COFF. |
175 | 19 | bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any || |
176 | 9 | Dst == Comdat::SelectionKind::Largest; |
177 | 19 | bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any || |
178 | 9 | Src == Comdat::SelectionKind::Largest; |
179 | 19 | if (DstAnyOrLargest && 19 SrcAnyOrLargest16 ) {16 |
180 | 16 | if (Dst == Comdat::SelectionKind::Largest || |
181 | 10 | Src == Comdat::SelectionKind::Largest) |
182 | 6 | Result = Comdat::SelectionKind::Largest; |
183 | 16 | else |
184 | 10 | Result = Comdat::SelectionKind::Any; |
185 | 3 | } else if (3 Src == Dst3 ) {2 |
186 | 2 | Result = Dst; |
187 | 1 | } else { |
188 | 1 | return emitError("Linking COMDATs named '" + ComdatName + |
189 | 1 | "': invalid selection kinds!"); |
190 | 1 | } |
191 | 19 | |
192 | 18 | switch (Result) { |
193 | 10 | case Comdat::SelectionKind::Any: |
194 | 10 | // Go with Dst. |
195 | 10 | LinkFromSrc = false; |
196 | 10 | break; |
197 | 1 | case Comdat::SelectionKind::NoDuplicates: |
198 | 1 | return emitError("Linking COMDATs named '" + ComdatName + |
199 | 1 | "': noduplicates has been violated!"); |
200 | 7 | case Comdat::SelectionKind::ExactMatch: |
201 | 7 | case Comdat::SelectionKind::Largest: |
202 | 7 | case Comdat::SelectionKind::SameSize: { |
203 | 7 | const GlobalVariable *DstGV; |
204 | 7 | const GlobalVariable *SrcGV; |
205 | 7 | if (getComdatLeader(DstM, ComdatName, DstGV) || |
206 | 5 | getComdatLeader(*SrcM, ComdatName, SrcGV)) |
207 | 2 | return true; |
208 | 7 | |
209 | 5 | const DataLayout &DstDL = DstM.getDataLayout(); |
210 | 5 | const DataLayout &SrcDL = SrcM->getDataLayout(); |
211 | 5 | uint64_t DstSize = DstDL.getTypeAllocSize(DstGV->getValueType()); |
212 | 5 | uint64_t SrcSize = SrcDL.getTypeAllocSize(SrcGV->getValueType()); |
213 | 5 | if (Result == Comdat::SelectionKind::ExactMatch5 ) {0 |
214 | 0 | if (SrcGV->getInitializer() != DstGV->getInitializer()) |
215 | 0 | return emitError("Linking COMDATs named '" + ComdatName + |
216 | 0 | "': ExactMatch violated!"); |
217 | 0 | LinkFromSrc = false; |
218 | 5 | } else if (5 Result == Comdat::SelectionKind::Largest5 ) {4 |
219 | 4 | LinkFromSrc = SrcSize > DstSize; |
220 | 1 | } else if (1 Result == Comdat::SelectionKind::SameSize1 ) {1 |
221 | 1 | if (SrcSize != DstSize) |
222 | 1 | return emitError("Linking COMDATs named '" + ComdatName + |
223 | 1 | "': SameSize violated!"); |
224 | 0 | LinkFromSrc = false; |
225 | 0 | } else { |
226 | 0 | llvm_unreachable("unknown selection kind"); |
227 | 0 | } |
228 | 4 | break; |
229 | 5 | } |
230 | 18 | } |
231 | 18 | |
232 | 14 | return false; |
233 | 18 | } |
234 | | |
235 | | bool ModuleLinker::getComdatResult(const Comdat *SrcC, |
236 | | Comdat::SelectionKind &Result, |
237 | 59 | bool &LinkFromSrc) { |
238 | 59 | Module &DstM = Mover.getModule(); |
239 | 59 | Comdat::SelectionKind SSK = SrcC->getSelectionKind(); |
240 | 59 | StringRef ComdatName = SrcC->getName(); |
241 | 59 | Module::ComdatSymTabType &ComdatSymTab = DstM.getComdatSymbolTable(); |
242 | 59 | Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName); |
243 | 59 | |
244 | 59 | if (DstCI == ComdatSymTab.end()59 ) {40 |
245 | 40 | // Use the comdat if it is only available in one of the modules. |
246 | 40 | LinkFromSrc = true; |
247 | 40 | Result = SSK; |
248 | 40 | return false; |
249 | 40 | } |
250 | 59 | |
251 | 19 | const Comdat *DstC = &DstCI->second; |
252 | 19 | Comdat::SelectionKind DSK = DstC->getSelectionKind(); |
253 | 19 | return computeResultingSelectionKind(ComdatName, SSK, DSK, Result, |
254 | 19 | LinkFromSrc); |
255 | 59 | } |
256 | | |
257 | | bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc, |
258 | | const GlobalValue &Dest, |
259 | 261 | const GlobalValue &Src) { |
260 | 261 | |
261 | 261 | // Should we unconditionally use the Src? |
262 | 261 | if (shouldOverrideFromSrc()261 ) {4 |
263 | 4 | LinkFromSrc = true; |
264 | 4 | return false; |
265 | 4 | } |
266 | 261 | |
267 | 261 | // We always have to add Src if it has appending linkage. |
268 | 257 | if (257 Src.hasAppendingLinkage()257 ) {9 |
269 | 9 | // Should have prevented importing for appending linkage in linkIfNeeded. |
270 | 9 | assert(!isPerformingImport()); |
271 | 9 | LinkFromSrc = true; |
272 | 9 | return false; |
273 | 9 | } |
274 | 257 | |
275 | 248 | if (248 isPerformingImport()248 ) {106 |
276 | 106 | // LinkFromSrc iff this is a global requested for importing. |
277 | 106 | LinkFromSrc = GlobalsToImport->count(&Src); |
278 | 106 | return false; |
279 | 106 | } |
280 | 248 | |
281 | 142 | bool SrcIsDeclaration = Src.isDeclarationForLinker(); |
282 | 142 | bool DestIsDeclaration = Dest.isDeclarationForLinker(); |
283 | 142 | |
284 | 142 | if (SrcIsDeclaration142 ) {3 |
285 | 3 | // If Src is external or if both Src & Dest are external.. Just link the |
286 | 3 | // external globals, we aren't adding anything. |
287 | 3 | if (Src.hasDLLImportStorageClass()3 ) {0 |
288 | 0 | // If one of GVs is marked as DLLImport, result should be dllimport'ed. |
289 | 0 | LinkFromSrc = DestIsDeclaration; |
290 | 0 | return false; |
291 | 0 | } |
292 | 3 | // If the Dest is weak, use the source linkage. |
293 | 3 | if (3 Dest.hasExternalWeakLinkage()3 ) {0 |
294 | 0 | LinkFromSrc = true; |
295 | 0 | return false; |
296 | 0 | } |
297 | 3 | // Link an available_externally over a declaration. |
298 | 3 | LinkFromSrc = !Src.isDeclaration() && 3 Dest.isDeclaration()3 ; |
299 | 3 | return false; |
300 | 3 | } |
301 | 142 | |
302 | 139 | if (139 DestIsDeclaration139 ) {51 |
303 | 51 | // If Dest is external but Src is not: |
304 | 51 | LinkFromSrc = true; |
305 | 51 | return false; |
306 | 51 | } |
307 | 139 | |
308 | 88 | if (88 Src.hasCommonLinkage()88 ) {9 |
309 | 9 | if (Dest.hasLinkOnceLinkage() || 9 Dest.hasWeakLinkage()9 ) {1 |
310 | 1 | LinkFromSrc = true; |
311 | 1 | return false; |
312 | 1 | } |
313 | 9 | |
314 | 8 | if (8 !Dest.hasCommonLinkage()8 ) {0 |
315 | 0 | LinkFromSrc = false; |
316 | 0 | return false; |
317 | 0 | } |
318 | 8 | |
319 | 8 | const DataLayout &DL = Dest.getParent()->getDataLayout(); |
320 | 8 | uint64_t DestSize = DL.getTypeAllocSize(Dest.getValueType()); |
321 | 8 | uint64_t SrcSize = DL.getTypeAllocSize(Src.getValueType()); |
322 | 8 | LinkFromSrc = SrcSize > DestSize; |
323 | 8 | return false; |
324 | 8 | } |
325 | 88 | |
326 | 79 | if (79 Src.isWeakForLinker()79 ) {49 |
327 | 49 | assert(!Dest.hasExternalWeakLinkage()); |
328 | 49 | assert(!Dest.hasAvailableExternallyLinkage()); |
329 | 49 | |
330 | 49 | if (Dest.hasLinkOnceLinkage() && 49 Src.hasWeakLinkage()3 ) {1 |
331 | 1 | LinkFromSrc = true; |
332 | 1 | return false; |
333 | 1 | } |
334 | 49 | |
335 | 48 | LinkFromSrc = false; |
336 | 48 | return false; |
337 | 49 | } |
338 | 79 | |
339 | 30 | if (30 Dest.isWeakForLinker()30 ) {25 |
340 | 25 | assert(Src.hasExternalLinkage()); |
341 | 25 | LinkFromSrc = true; |
342 | 25 | return false; |
343 | 25 | } |
344 | 30 | |
345 | 5 | assert(!Src.hasExternalWeakLinkage()); |
346 | 5 | assert(!Dest.hasExternalWeakLinkage()); |
347 | 5 | assert(Dest.hasExternalLinkage() && Src.hasExternalLinkage() && |
348 | 5 | "Unexpected linkage type!"); |
349 | 5 | return emitError("Linking globals named '" + Src.getName() + |
350 | 5 | "': symbol multiply defined!"); |
351 | 30 | } |
352 | | |
353 | 1.73k | bool ModuleLinker::linkIfNeeded(GlobalValue &GV) { |
354 | 1.73k | GlobalValue *DGV = getLinkedToGlobal(&GV); |
355 | 1.73k | |
356 | 1.73k | if (shouldLinkOnlyNeeded() && 1.73k !(DGV && 64 DGV->isDeclaration()19 )) |
357 | 45 | return false; |
358 | 1.73k | |
359 | 1.69k | if (1.69k DGV && 1.69k !GV.hasLocalLinkage()473 && !GV.hasAppendingLinkage()473 ) {463 |
360 | 463 | auto *DGVar = dyn_cast<GlobalVariable>(DGV); |
361 | 463 | auto *SGVar = dyn_cast<GlobalVariable>(&GV); |
362 | 463 | if (DGVar && 463 SGVar76 ) {68 |
363 | 68 | if (DGVar->isDeclaration() && 68 SGVar->isDeclaration()17 && |
364 | 2 | (!DGVar->isConstant() || 2 !SGVar->isConstant()0 )) {2 |
365 | 2 | DGVar->setConstant(false); |
366 | 2 | SGVar->setConstant(false); |
367 | 2 | } |
368 | 68 | if (DGVar->hasCommonLinkage() && 68 SGVar->hasCommonLinkage()9 ) {8 |
369 | 8 | unsigned Align = std::max(DGVar->getAlignment(), SGVar->getAlignment()); |
370 | 8 | SGVar->setAlignment(Align); |
371 | 8 | DGVar->setAlignment(Align); |
372 | 8 | } |
373 | 68 | } |
374 | 463 | |
375 | 463 | GlobalValue::VisibilityTypes Visibility = |
376 | 463 | getMinVisibility(DGV->getVisibility(), GV.getVisibility()); |
377 | 463 | DGV->setVisibility(Visibility); |
378 | 463 | GV.setVisibility(Visibility); |
379 | 463 | |
380 | 463 | GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::getMinUnnamedAddr( |
381 | 463 | DGV->getUnnamedAddr(), GV.getUnnamedAddr()); |
382 | 463 | DGV->setUnnamedAddr(UnnamedAddr); |
383 | 463 | GV.setUnnamedAddr(UnnamedAddr); |
384 | 463 | } |
385 | 1.69k | |
386 | 1.69k | // Don't want to append to global_ctors list, for example, when we |
387 | 1.69k | // are importing for ThinLTO, otherwise the global ctors and dtors |
388 | 1.69k | // get executed multiple times for local variables (the latter causing |
389 | 1.69k | // double frees). |
390 | 1.69k | if (GV.hasAppendingLinkage() && 1.69k isPerformingImport()30 ) |
391 | 6 | return false; |
392 | 1.69k | |
393 | 1.68k | if (1.68k isPerformingImport()1.68k ) {548 |
394 | 548 | if (!doImportAsDefinition(&GV)) |
395 | 404 | return false; |
396 | 1.13k | } else if (1.13k !DGV && 1.13k !shouldOverrideFromSrc()936 && |
397 | 932 | (GV.hasLocalLinkage() || 932 GV.hasLinkOnceLinkage()874 || |
398 | 818 | GV.hasAvailableExternallyLinkage())) |
399 | 119 | return false; |
400 | 1.68k | |
401 | 1.16k | if (1.16k GV.isDeclaration()1.16k ) |
402 | 284 | return false; |
403 | 1.16k | |
404 | 878 | if (const Comdat *878 SC878 = GV.getComdat()) {66 |
405 | 66 | bool LinkFromSrc; |
406 | 66 | Comdat::SelectionKind SK; |
407 | 66 | std::tie(SK, LinkFromSrc) = ComdatsChosen[SC]; |
408 | 66 | if (!LinkFromSrc) |
409 | 15 | return false; |
410 | 66 | } |
411 | 878 | |
412 | 863 | bool LinkFromSrc = true; |
413 | 863 | if (DGV && 863 shouldLinkFromSource(LinkFromSrc, *DGV, GV)259 ) |
414 | 5 | return true; |
415 | 858 | if (858 LinkFromSrc858 ) |
416 | 804 | ValuesToLink.insert(&GV); |
417 | 858 | return false; |
418 | 863 | } |
419 | | |
420 | 115 | void ModuleLinker::addLazyFor(GlobalValue &GV, const IRMover::ValueAdder &Add) { |
421 | 115 | if (!shouldLinkReferencedLinkOnce()) |
422 | 115 | // For ThinLTO we don't import more than what was required. |
423 | 115 | // The client has to guarantee that the linkonce will be availabe at link |
424 | 115 | // time (by promoting it to weak for instance). |
425 | 67 | return; |
426 | 115 | |
427 | 115 | // Add these to the internalize list |
428 | 48 | if (48 !GV.hasLinkOnceLinkage() && 48 !GV.hasAvailableExternallyLinkage()16 && |
429 | 12 | !shouldLinkOnlyNeeded()) |
430 | 8 | return; |
431 | 48 | |
432 | 40 | if (40 shouldInternalizeLinkedSymbols()40 ) |
433 | 4 | Internalize.insert(GV.getName()); |
434 | 40 | Add(GV); |
435 | 40 | |
436 | 40 | const Comdat *SC = GV.getComdat(); |
437 | 40 | if (!SC) |
438 | 38 | return; |
439 | 5 | for (GlobalValue *GV2 : LazyComdatMembers[SC]) 2 {5 |
440 | 5 | GlobalValue *DGV = getLinkedToGlobal(GV2); |
441 | 5 | bool LinkFromSrc = true; |
442 | 5 | if (DGV && 5 shouldLinkFromSource(LinkFromSrc, *DGV, *GV2)1 ) |
443 | 0 | return; |
444 | 5 | if (5 !LinkFromSrc5 ) |
445 | 1 | continue; |
446 | 4 | if (4 shouldInternalizeLinkedSymbols()4 ) |
447 | 0 | Internalize.insert(GV2->getName()); |
448 | 4 | Add(*GV2); |
449 | 4 | } |
450 | 2 | } |
451 | | |
452 | | void ModuleLinker::dropReplacedComdat( |
453 | 792 | GlobalValue &GV, const DenseSet<const Comdat *> &ReplacedDstComdats) { |
454 | 792 | Comdat *C = GV.getComdat(); |
455 | 792 | if (!C) |
456 | 761 | return; |
457 | 31 | if (31 !ReplacedDstComdats.count(C)31 ) |
458 | 23 | return; |
459 | 8 | if (8 GV.use_empty()8 ) {6 |
460 | 6 | GV.eraseFromParent(); |
461 | 6 | return; |
462 | 6 | } |
463 | 8 | |
464 | 2 | if (auto *2 F2 = dyn_cast<Function>(&GV)) {0 |
465 | 0 | F->deleteBody(); |
466 | 2 | } else if (auto *2 Var2 = dyn_cast<GlobalVariable>(&GV)) {0 |
467 | 0 | Var->setInitializer(nullptr); |
468 | 2 | } else { |
469 | 2 | auto &Alias = cast<GlobalAlias>(GV); |
470 | 2 | Module &M = *Alias.getParent(); |
471 | 2 | PointerType &Ty = *cast<PointerType>(Alias.getType()); |
472 | 2 | GlobalValue *Declaration; |
473 | 2 | if (auto *FTy2 = dyn_cast<FunctionType>(Alias.getValueType())) {1 |
474 | 1 | Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage, "", &M); |
475 | 1 | } else { |
476 | 1 | Declaration = |
477 | 1 | new GlobalVariable(M, Ty.getElementType(), /*isConstant*/ false, |
478 | 1 | GlobalValue::ExternalLinkage, |
479 | 1 | /*Initializer*/ nullptr); |
480 | 1 | } |
481 | 2 | Declaration->takeName(&Alias); |
482 | 2 | Alias.replaceAllUsesWith(Declaration); |
483 | 2 | Alias.eraseFromParent(); |
484 | 2 | } |
485 | 2 | } |
486 | | |
487 | 565 | bool ModuleLinker::run() { |
488 | 565 | Module &DstM = Mover.getModule(); |
489 | 565 | DenseSet<const Comdat *> ReplacedDstComdats; |
490 | 565 | |
491 | 59 | for (const auto &SMEC : SrcM->getComdatSymbolTable()) { |
492 | 59 | const Comdat &C = SMEC.getValue(); |
493 | 59 | if (ComdatsChosen.count(&C)) |
494 | 0 | continue; |
495 | 59 | Comdat::SelectionKind SK; |
496 | 59 | bool LinkFromSrc; |
497 | 59 | if (getComdatResult(&C, SK, LinkFromSrc)) |
498 | 5 | return true; |
499 | 54 | ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc); |
500 | 54 | |
501 | 54 | if (!LinkFromSrc) |
502 | 12 | continue; |
503 | 54 | |
504 | 42 | Module::ComdatSymTabType &ComdatSymTab = DstM.getComdatSymbolTable(); |
505 | 42 | Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(C.getName()); |
506 | 42 | if (DstCI == ComdatSymTab.end()) |
507 | 40 | continue; |
508 | 42 | |
509 | 42 | // The source comdat is replacing the dest one. |
510 | 2 | const Comdat *DstC = &DstCI->second; |
511 | 2 | ReplacedDstComdats.insert(DstC); |
512 | 2 | } |
513 | 565 | |
514 | 565 | // Alias have to go first, since we are not able to find their comdats |
515 | 565 | // otherwise. |
516 | 587 | for (auto I = DstM.alias_begin(), E = DstM.alias_end(); 560 I != E587 ;) {27 |
517 | 27 | GlobalAlias &GV = *I++; |
518 | 27 | dropReplacedComdat(GV, ReplacedDstComdats); |
519 | 27 | } |
520 | 560 | |
521 | 720 | for (auto I = DstM.global_begin(), E = DstM.global_end(); I != E720 ;) {160 |
522 | 160 | GlobalVariable &GV = *I++; |
523 | 160 | dropReplacedComdat(GV, ReplacedDstComdats); |
524 | 160 | } |
525 | 560 | |
526 | 1.16k | for (auto I = DstM.begin(), E = DstM.end(); I != E1.16k ;) {605 |
527 | 605 | Function &GV = *I++; |
528 | 605 | dropReplacedComdat(GV, ReplacedDstComdats); |
529 | 605 | } |
530 | 560 | |
531 | 560 | for (GlobalVariable &GV : SrcM->globals()) |
532 | 501 | if (501 GV.hasLinkOnceLinkage()501 ) |
533 | 35 | if (const Comdat *35 SC35 = GV.getComdat()) |
534 | 4 | LazyComdatMembers[SC].push_back(&GV); |
535 | 560 | |
536 | 560 | for (Function &SF : *SrcM) |
537 | 1.08k | if (1.08k SF.hasLinkOnceLinkage()1.08k ) |
538 | 56 | if (const Comdat *56 SC56 = SF.getComdat()) |
539 | 10 | LazyComdatMembers[SC].push_back(&SF); |
540 | 560 | |
541 | 560 | for (GlobalAlias &GA : SrcM->aliases()) |
542 | 147 | if (147 GA.hasLinkOnceLinkage()147 ) |
543 | 13 | if (const Comdat *13 SC13 = GA.getComdat()) |
544 | 0 | LazyComdatMembers[SC].push_back(&GA); |
545 | 560 | |
546 | 560 | // Insert all of the globals in src into the DstM module... without linking |
547 | 560 | // initializers (which could refer to functions not yet mapped over). |
548 | 560 | for (GlobalVariable &GV : SrcM->globals()) |
549 | 501 | if (501 linkIfNeeded(GV)501 ) |
550 | 1 | return true; |
551 | 560 | |
552 | 559 | for (Function &SF : *SrcM) |
553 | 1.08k | if (1.08k linkIfNeeded(SF)1.08k ) |
554 | 4 | return true; |
555 | 559 | |
556 | 555 | for (GlobalAlias &GA : SrcM->aliases()) |
557 | 147 | if (147 linkIfNeeded(GA)147 ) |
558 | 0 | return true; |
559 | 555 | |
560 | 1.36k | for (unsigned I = 0; 555 I < ValuesToLink.size()1.36k ; ++I806 ) {806 |
561 | 806 | GlobalValue *GV = ValuesToLink[I]; |
562 | 806 | const Comdat *SC = GV->getComdat(); |
563 | 806 | if (!SC) |
564 | 759 | continue; |
565 | 47 | for (GlobalValue *GV2 : LazyComdatMembers[SC]) 47 {5 |
566 | 5 | GlobalValue *DGV = getLinkedToGlobal(GV2); |
567 | 5 | bool LinkFromSrc = true; |
568 | 5 | if (DGV && 5 shouldLinkFromSource(LinkFromSrc, *DGV, *GV2)1 ) |
569 | 0 | return true; |
570 | 5 | if (5 LinkFromSrc5 ) |
571 | 4 | ValuesToLink.insert(GV2); |
572 | 5 | } |
573 | 47 | } |
574 | 555 | |
575 | 555 | if (555 shouldInternalizeLinkedSymbols()555 ) {11 |
576 | 11 | for (GlobalValue *GV : ValuesToLink) |
577 | 17 | Internalize.insert(GV->getName()); |
578 | 11 | } |
579 | 555 | |
580 | 555 | // FIXME: Propagate Errors through to the caller instead of emitting |
581 | 555 | // diagnostics. |
582 | 555 | bool HasErrors = false; |
583 | 555 | if (Error E = Mover.move(std::move(SrcM), ValuesToLink.getArrayRef(), |
584 | 115 | [this](GlobalValue &GV, IRMover::ValueAdder Add) { |
585 | 115 | addLazyFor(GV, Add); |
586 | 115 | }, |
587 | 555 | /* LinkModuleInlineAsm */ !isPerformingImport(), |
588 | 6 | /* IsPerformingImport */ isPerformingImport())) { |
589 | 6 | handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) { |
590 | 6 | DstM.getContext().diagnose(LinkDiagnosticInfo(DS_Error, EIB.message())); |
591 | 6 | HasErrors = true; |
592 | 6 | }); |
593 | 6 | } |
594 | 555 | if (HasErrors) |
595 | 6 | return true; |
596 | 555 | |
597 | 549 | for (auto &P : Internalize) 549 {21 |
598 | 21 | GlobalValue *GV = DstM.getNamedValue(P.first()); |
599 | 21 | GV->setLinkage(GlobalValue::InternalLinkage); |
600 | 21 | } |
601 | 549 | |
602 | 549 | return false; |
603 | 555 | } |
604 | | |
605 | 437 | Linker::Linker(Module &M) : Mover(M) {} |
606 | | |
607 | | bool Linker::linkInModule(std::unique_ptr<Module> Src, unsigned Flags, |
608 | 565 | DenseSet<const GlobalValue *> *GlobalsToImport) { |
609 | 565 | ModuleLinker ModLinker(Mover, std::move(Src), Flags, GlobalsToImport); |
610 | 565 | return ModLinker.run(); |
611 | 565 | } |
612 | | |
613 | | //===----------------------------------------------------------------------===// |
614 | | // LinkModules entrypoint. |
615 | | //===----------------------------------------------------------------------===// |
616 | | |
617 | | /// This function links two modules together, with the resulting Dest module |
618 | | /// modified to be the composite of the two input modules. If an error occurs, |
619 | | /// true is returned and ErrorMsg (if not null) is set to indicate the problem. |
620 | | /// Upon failure, the Dest module could be in a modified state, and shouldn't be |
621 | | /// relied on to be consistent. |
622 | | bool Linker::linkModules(Module &Dest, std::unique_ptr<Module> Src, |
623 | 20 | unsigned Flags) { |
624 | 20 | Linker L(Dest); |
625 | 20 | return L.linkInModule(std::move(Src), Flags); |
626 | 20 | } |
627 | | |
628 | | //===----------------------------------------------------------------------===// |
629 | | // C API. |
630 | | //===----------------------------------------------------------------------===// |
631 | | |
632 | 2 | LLVMBool LLVMLinkModules2(LLVMModuleRef Dest, LLVMModuleRef Src) { |
633 | 2 | Module *D = unwrap(Dest); |
634 | 2 | std::unique_ptr<Module> M(unwrap(Src)); |
635 | 2 | return Linker::linkModules(*D, std::move(M)); |
636 | 2 | } |