1 //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the LLVM module linker.
11 //===----------------------------------------------------------------------===//
13 #include "LinkDiagnosticInfo.h"
14 #include "llvm-c/Linker.h"
15 #include "llvm/ADT/SetVector.h"
16 #include "llvm/IR/Comdat.h"
17 #include "llvm/IR/GlobalValue.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Linker/Linker.h"
21 #include "llvm/Support/Error.h"
26 enum class LinkFrom
{ Dst
, Src
, Both
};
28 /// This is an implementation class for the LinkModules function, which is the
29 /// entrypoint for this file.
32 std::unique_ptr
<Module
> SrcM
;
34 SetVector
<GlobalValue
*> ValuesToLink
;
36 /// For symbol clashes, prefer those from Src.
39 /// List of global value names that should be internalized.
40 StringSet
<> Internalize
;
42 /// Function that will perform the actual internalization. The reason for a
43 /// callback is that the linker cannot call internalizeModule without
44 /// creating a circular dependency between IPO and the linker.
45 std::function
<void(Module
&, const StringSet
<> &)> InternalizeCallback
;
47 /// Used as the callback for lazy linking.
48 /// The mover has just hit GV and we have to decide if it, and other members
49 /// of the same comdat, should be linked. Every member to be linked is passed
51 void addLazyFor(GlobalValue
&GV
, const IRMover::ValueAdder
&Add
);
53 bool shouldOverrideFromSrc() { return Flags
& Linker::OverrideFromSrc
; }
54 bool shouldLinkOnlyNeeded() { return Flags
& Linker::LinkOnlyNeeded
; }
56 bool shouldLinkFromSource(bool &LinkFromSrc
, const GlobalValue
&Dest
,
57 const GlobalValue
&Src
);
59 /// Should we have mover and linker error diag info?
60 bool emitError(const Twine
&Message
) {
61 SrcM
->getContext().diagnose(LinkDiagnosticInfo(DS_Error
, Message
));
65 bool getComdatLeader(Module
&M
, StringRef ComdatName
,
66 const GlobalVariable
*&GVar
);
67 bool computeResultingSelectionKind(StringRef ComdatName
,
68 Comdat::SelectionKind Src
,
69 Comdat::SelectionKind Dst
,
70 Comdat::SelectionKind
&Result
,
72 DenseMap
<const Comdat
*, std::pair
<Comdat::SelectionKind
, LinkFrom
>>
74 bool getComdatResult(const Comdat
*SrcC
, Comdat::SelectionKind
&SK
,
76 // Keep track of the lazy linked global members of each comdat in source.
77 DenseMap
<const Comdat
*, std::vector
<GlobalValue
*>> LazyComdatMembers
;
79 /// Given a global in the source module, return the global in the
80 /// destination module that is being linked to, if any.
81 GlobalValue
*getLinkedToGlobal(const GlobalValue
*SrcGV
) {
82 Module
&DstM
= Mover
.getModule();
83 // If the source has no name it can't link. If it has local linkage,
84 // there is no name match-up going on.
85 if (!SrcGV
->hasName() || GlobalValue::isLocalLinkage(SrcGV
->getLinkage()))
88 // Otherwise see if we have a match in the destination module's symtab.
89 GlobalValue
*DGV
= DstM
.getNamedValue(SrcGV
->getName());
93 // If we found a global with the same name in the dest module, but it has
94 // internal linkage, we are really not doing any linkage here.
95 if (DGV
->hasLocalLinkage())
98 // Otherwise, we do in fact link to the destination global.
102 /// Drop GV if it is a member of a comdat that we are dropping.
103 /// This can happen with COFF's largest selection kind.
104 void dropReplacedComdat(GlobalValue
&GV
,
105 const DenseSet
<const Comdat
*> &ReplacedDstComdats
);
107 bool linkIfNeeded(GlobalValue
&GV
, SmallVectorImpl
<GlobalValue
*> &GVToClone
);
110 ModuleLinker(IRMover
&Mover
, std::unique_ptr
<Module
> SrcM
, unsigned Flags
,
111 std::function
<void(Module
&, const StringSet
<> &)>
112 InternalizeCallback
= {})
113 : Mover(Mover
), SrcM(std::move(SrcM
)), Flags(Flags
),
114 InternalizeCallback(std::move(InternalizeCallback
)) {}
120 static GlobalValue::VisibilityTypes
121 getMinVisibility(GlobalValue::VisibilityTypes A
,
122 GlobalValue::VisibilityTypes B
) {
123 if (A
== GlobalValue::HiddenVisibility
|| B
== GlobalValue::HiddenVisibility
)
124 return GlobalValue::HiddenVisibility
;
125 if (A
== GlobalValue::ProtectedVisibility
||
126 B
== GlobalValue::ProtectedVisibility
)
127 return GlobalValue::ProtectedVisibility
;
128 return GlobalValue::DefaultVisibility
;
131 bool ModuleLinker::getComdatLeader(Module
&M
, StringRef ComdatName
,
132 const GlobalVariable
*&GVar
) {
133 const GlobalValue
*GVal
= M
.getNamedValue(ComdatName
);
134 if (const auto *GA
= dyn_cast_or_null
<GlobalAlias
>(GVal
)) {
135 GVal
= GA
->getAliaseeObject();
137 // We cannot resolve the size of the aliasee yet.
138 return emitError("Linking COMDATs named '" + ComdatName
+
139 "': COMDAT key involves incomputable alias size.");
142 GVar
= dyn_cast_or_null
<GlobalVariable
>(GVal
);
145 "Linking COMDATs named '" + ComdatName
+
146 "': GlobalVariable required for data dependent selection!");
151 bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName
,
152 Comdat::SelectionKind Src
,
153 Comdat::SelectionKind Dst
,
154 Comdat::SelectionKind
&Result
,
156 Module
&DstM
= Mover
.getModule();
157 // The ability to mix Comdat::SelectionKind::Any with
158 // Comdat::SelectionKind::Largest is a behavior that comes from COFF.
159 bool DstAnyOrLargest
= Dst
== Comdat::SelectionKind::Any
||
160 Dst
== Comdat::SelectionKind::Largest
;
161 bool SrcAnyOrLargest
= Src
== Comdat::SelectionKind::Any
||
162 Src
== Comdat::SelectionKind::Largest
;
163 if (DstAnyOrLargest
&& SrcAnyOrLargest
) {
164 if (Dst
== Comdat::SelectionKind::Largest
||
165 Src
== Comdat::SelectionKind::Largest
)
166 Result
= Comdat::SelectionKind::Largest
;
168 Result
= Comdat::SelectionKind::Any
;
169 } else if (Src
== Dst
) {
172 return emitError("Linking COMDATs named '" + ComdatName
+
173 "': invalid selection kinds!");
177 case Comdat::SelectionKind::Any
:
179 From
= LinkFrom::Dst
;
181 case Comdat::SelectionKind::NoDeduplicate
:
182 From
= LinkFrom::Both
;
184 case Comdat::SelectionKind::ExactMatch
:
185 case Comdat::SelectionKind::Largest
:
186 case Comdat::SelectionKind::SameSize
: {
187 const GlobalVariable
*DstGV
;
188 const GlobalVariable
*SrcGV
;
189 if (getComdatLeader(DstM
, ComdatName
, DstGV
) ||
190 getComdatLeader(*SrcM
, ComdatName
, SrcGV
))
193 const DataLayout
&DstDL
= DstM
.getDataLayout();
194 const DataLayout
&SrcDL
= SrcM
->getDataLayout();
195 uint64_t DstSize
= DstDL
.getTypeAllocSize(DstGV
->getValueType());
196 uint64_t SrcSize
= SrcDL
.getTypeAllocSize(SrcGV
->getValueType());
197 if (Result
== Comdat::SelectionKind::ExactMatch
) {
198 if (SrcGV
->getInitializer() != DstGV
->getInitializer())
199 return emitError("Linking COMDATs named '" + ComdatName
+
200 "': ExactMatch violated!");
201 From
= LinkFrom::Dst
;
202 } else if (Result
== Comdat::SelectionKind::Largest
) {
203 From
= SrcSize
> DstSize
? LinkFrom::Src
: LinkFrom::Dst
;
204 } else if (Result
== Comdat::SelectionKind::SameSize
) {
205 if (SrcSize
!= DstSize
)
206 return emitError("Linking COMDATs named '" + ComdatName
+
207 "': SameSize violated!");
208 From
= LinkFrom::Dst
;
210 llvm_unreachable("unknown selection kind");
219 bool ModuleLinker::getComdatResult(const Comdat
*SrcC
,
220 Comdat::SelectionKind
&Result
,
222 Module
&DstM
= Mover
.getModule();
223 Comdat::SelectionKind SSK
= SrcC
->getSelectionKind();
224 StringRef ComdatName
= SrcC
->getName();
225 Module::ComdatSymTabType
&ComdatSymTab
= DstM
.getComdatSymbolTable();
226 Module::ComdatSymTabType::iterator DstCI
= ComdatSymTab
.find(ComdatName
);
228 if (DstCI
== ComdatSymTab
.end()) {
229 // Use the comdat if it is only available in one of the modules.
230 From
= LinkFrom::Src
;
235 const Comdat
*DstC
= &DstCI
->second
;
236 Comdat::SelectionKind DSK
= DstC
->getSelectionKind();
237 return computeResultingSelectionKind(ComdatName
, SSK
, DSK
, Result
, From
);
240 bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc
,
241 const GlobalValue
&Dest
,
242 const GlobalValue
&Src
) {
244 // Should we unconditionally use the Src?
245 if (shouldOverrideFromSrc()) {
250 // We always have to add Src if it has appending linkage.
251 if (Src
.hasAppendingLinkage() || Dest
.hasAppendingLinkage()) {
256 bool SrcIsDeclaration
= Src
.isDeclarationForLinker();
257 bool DestIsDeclaration
= Dest
.isDeclarationForLinker();
259 if (SrcIsDeclaration
) {
260 // If Src is external or if both Src & Dest are external.. Just link the
261 // external globals, we aren't adding anything.
262 if (Src
.hasDLLImportStorageClass()) {
263 // If one of GVs is marked as DLLImport, result should be dllimport'ed.
264 LinkFromSrc
= DestIsDeclaration
;
267 // If the Dest is weak, use the source linkage.
268 if (Dest
.hasExternalWeakLinkage()) {
272 // Link an available_externally over a declaration.
273 LinkFromSrc
= !Src
.isDeclaration() && Dest
.isDeclaration();
277 if (DestIsDeclaration
) {
278 // If Dest is external but Src is not:
283 if (Src
.hasCommonLinkage()) {
284 if (Dest
.hasLinkOnceLinkage() || Dest
.hasWeakLinkage()) {
289 if (!Dest
.hasCommonLinkage()) {
294 const DataLayout
&DL
= Dest
.getParent()->getDataLayout();
295 uint64_t DestSize
= DL
.getTypeAllocSize(Dest
.getValueType());
296 uint64_t SrcSize
= DL
.getTypeAllocSize(Src
.getValueType());
297 LinkFromSrc
= SrcSize
> DestSize
;
301 if (Src
.isWeakForLinker()) {
302 assert(!Dest
.hasExternalWeakLinkage());
303 assert(!Dest
.hasAvailableExternallyLinkage());
305 if (Dest
.hasLinkOnceLinkage() && Src
.hasWeakLinkage()) {
314 if (Dest
.isWeakForLinker()) {
315 assert(Src
.hasExternalLinkage());
320 assert(!Src
.hasExternalWeakLinkage());
321 assert(!Dest
.hasExternalWeakLinkage());
322 assert(Dest
.hasExternalLinkage() && Src
.hasExternalLinkage() &&
323 "Unexpected linkage type!");
324 return emitError("Linking globals named '" + Src
.getName() +
325 "': symbol multiply defined!");
328 bool ModuleLinker::linkIfNeeded(GlobalValue
&GV
,
329 SmallVectorImpl
<GlobalValue
*> &GVToClone
) {
330 GlobalValue
*DGV
= getLinkedToGlobal(&GV
);
332 if (shouldLinkOnlyNeeded()) {
333 // Always import variables with appending linkage.
334 if (!GV
.hasAppendingLinkage()) {
335 // Don't import globals unless they are referenced by the destination
339 // Don't import globals that are already defined in the destination module
340 if (!DGV
->isDeclaration())
345 if (DGV
&& !GV
.hasLocalLinkage() && !GV
.hasAppendingLinkage()) {
346 auto *DGVar
= dyn_cast
<GlobalVariable
>(DGV
);
347 auto *SGVar
= dyn_cast
<GlobalVariable
>(&GV
);
348 if (DGVar
&& SGVar
) {
349 if (DGVar
->isDeclaration() && SGVar
->isDeclaration() &&
350 (!DGVar
->isConstant() || !SGVar
->isConstant())) {
351 DGVar
->setConstant(false);
352 SGVar
->setConstant(false);
354 if (DGVar
->hasCommonLinkage() && SGVar
->hasCommonLinkage()) {
356 std::max(DGVar
->getAlignment(), SGVar
->getAlignment()));
357 SGVar
->setAlignment(Align
);
358 DGVar
->setAlignment(Align
);
362 GlobalValue::VisibilityTypes Visibility
=
363 getMinVisibility(DGV
->getVisibility(), GV
.getVisibility());
364 DGV
->setVisibility(Visibility
);
365 GV
.setVisibility(Visibility
);
367 GlobalValue::UnnamedAddr UnnamedAddr
= GlobalValue::getMinUnnamedAddr(
368 DGV
->getUnnamedAddr(), GV
.getUnnamedAddr());
369 DGV
->setUnnamedAddr(UnnamedAddr
);
370 GV
.setUnnamedAddr(UnnamedAddr
);
373 if (!DGV
&& !shouldOverrideFromSrc() &&
374 (GV
.hasLocalLinkage() || GV
.hasLinkOnceLinkage() ||
375 GV
.hasAvailableExternallyLinkage()))
378 if (GV
.isDeclaration())
381 LinkFrom ComdatFrom
= LinkFrom::Dst
;
382 if (const Comdat
*SC
= GV
.getComdat()) {
383 std::tie(std::ignore
, ComdatFrom
) = ComdatsChosen
[SC
];
384 if (ComdatFrom
== LinkFrom::Dst
)
388 bool LinkFromSrc
= true;
389 if (DGV
&& shouldLinkFromSource(LinkFromSrc
, *DGV
, GV
))
391 if (DGV
&& ComdatFrom
== LinkFrom::Both
)
392 GVToClone
.push_back(LinkFromSrc
? DGV
: &GV
);
394 ValuesToLink
.insert(&GV
);
398 void ModuleLinker::addLazyFor(GlobalValue
&GV
, const IRMover::ValueAdder
&Add
) {
399 // Add these to the internalize list
400 if (!GV
.hasLinkOnceLinkage() && !GV
.hasAvailableExternallyLinkage() &&
401 !shouldLinkOnlyNeeded())
404 if (InternalizeCallback
)
405 Internalize
.insert(GV
.getName());
408 const Comdat
*SC
= GV
.getComdat();
411 for (GlobalValue
*GV2
: LazyComdatMembers
[SC
]) {
412 GlobalValue
*DGV
= getLinkedToGlobal(GV2
);
413 bool LinkFromSrc
= true;
414 if (DGV
&& shouldLinkFromSource(LinkFromSrc
, *DGV
, *GV2
))
418 if (InternalizeCallback
)
419 Internalize
.insert(GV2
->getName());
424 void ModuleLinker::dropReplacedComdat(
425 GlobalValue
&GV
, const DenseSet
<const Comdat
*> &ReplacedDstComdats
) {
426 Comdat
*C
= GV
.getComdat();
429 if (!ReplacedDstComdats
.count(C
))
431 if (GV
.use_empty()) {
432 GV
.eraseFromParent();
436 if (auto *F
= dyn_cast
<Function
>(&GV
)) {
438 } else if (auto *Var
= dyn_cast
<GlobalVariable
>(&GV
)) {
439 Var
->setInitializer(nullptr);
441 auto &Alias
= cast
<GlobalAlias
>(GV
);
442 Module
&M
= *Alias
.getParent();
443 GlobalValue
*Declaration
;
444 if (auto *FTy
= dyn_cast
<FunctionType
>(Alias
.getValueType())) {
445 Declaration
= Function::Create(FTy
, GlobalValue::ExternalLinkage
, "", &M
);
448 new GlobalVariable(M
, Alias
.getValueType(), /*isConstant*/ false,
449 GlobalValue::ExternalLinkage
,
450 /*Initializer*/ nullptr);
452 Declaration
->takeName(&Alias
);
453 Alias
.replaceAllUsesWith(Declaration
);
454 Alias
.eraseFromParent();
458 bool ModuleLinker::run() {
459 Module
&DstM
= Mover
.getModule();
460 DenseSet
<const Comdat
*> ReplacedDstComdats
;
462 for (const auto &SMEC
: SrcM
->getComdatSymbolTable()) {
463 const Comdat
&C
= SMEC
.getValue();
464 if (ComdatsChosen
.count(&C
))
466 Comdat::SelectionKind SK
;
468 if (getComdatResult(&C
, SK
, From
))
470 ComdatsChosen
[&C
] = std::make_pair(SK
, From
);
472 if (From
!= LinkFrom::Src
)
475 Module::ComdatSymTabType
&ComdatSymTab
= DstM
.getComdatSymbolTable();
476 Module::ComdatSymTabType::iterator DstCI
= ComdatSymTab
.find(C
.getName());
477 if (DstCI
== ComdatSymTab
.end())
480 // The source comdat is replacing the dest one.
481 const Comdat
*DstC
= &DstCI
->second
;
482 ReplacedDstComdats
.insert(DstC
);
485 // Alias have to go first, since we are not able to find their comdats
487 for (GlobalAlias
&GV
: llvm::make_early_inc_range(DstM
.aliases()))
488 dropReplacedComdat(GV
, ReplacedDstComdats
);
490 for (GlobalVariable
&GV
: llvm::make_early_inc_range(DstM
.globals()))
491 dropReplacedComdat(GV
, ReplacedDstComdats
);
493 for (Function
&GV
: llvm::make_early_inc_range(DstM
))
494 dropReplacedComdat(GV
, ReplacedDstComdats
);
496 for (GlobalVariable
&GV
: SrcM
->globals())
497 if (GV
.hasLinkOnceLinkage())
498 if (const Comdat
*SC
= GV
.getComdat())
499 LazyComdatMembers
[SC
].push_back(&GV
);
501 for (Function
&SF
: *SrcM
)
502 if (SF
.hasLinkOnceLinkage())
503 if (const Comdat
*SC
= SF
.getComdat())
504 LazyComdatMembers
[SC
].push_back(&SF
);
506 for (GlobalAlias
&GA
: SrcM
->aliases())
507 if (GA
.hasLinkOnceLinkage())
508 if (const Comdat
*SC
= GA
.getComdat())
509 LazyComdatMembers
[SC
].push_back(&GA
);
511 // Insert all of the globals in src into the DstM module... without linking
512 // initializers (which could refer to functions not yet mapped over).
513 SmallVector
<GlobalValue
*, 0> GVToClone
;
514 for (GlobalVariable
&GV
: SrcM
->globals())
515 if (linkIfNeeded(GV
, GVToClone
))
518 for (Function
&SF
: *SrcM
)
519 if (linkIfNeeded(SF
, GVToClone
))
522 for (GlobalAlias
&GA
: SrcM
->aliases())
523 if (linkIfNeeded(GA
, GVToClone
))
526 for (GlobalIFunc
&GI
: SrcM
->ifuncs())
527 if (linkIfNeeded(GI
, GVToClone
))
530 // For a variable in a comdat nodeduplicate, its initializer should be
531 // preserved (its content may be implicitly used by other members) even if
532 // symbol resolution does not pick it. Clone it into an unnamed private
534 for (GlobalValue
*GV
: GVToClone
) {
535 if (auto *Var
= dyn_cast
<GlobalVariable
>(GV
)) {
536 auto *NewVar
= new GlobalVariable(*Var
->getParent(), Var
->getValueType(),
537 Var
->isConstant(), Var
->getLinkage(),
538 Var
->getInitializer());
539 NewVar
->copyAttributesFrom(Var
);
540 NewVar
->setVisibility(GlobalValue::DefaultVisibility
);
541 NewVar
->setLinkage(GlobalValue::PrivateLinkage
);
542 NewVar
->setDSOLocal(true);
543 NewVar
->setComdat(Var
->getComdat());
544 if (Var
->getParent() != &Mover
.getModule())
545 ValuesToLink
.insert(NewVar
);
547 emitError("linking '" + GV
->getName() +
548 "': non-variables in comdat nodeduplicate are not handled");
552 for (unsigned I
= 0; I
< ValuesToLink
.size(); ++I
) {
553 GlobalValue
*GV
= ValuesToLink
[I
];
554 const Comdat
*SC
= GV
->getComdat();
557 for (GlobalValue
*GV2
: LazyComdatMembers
[SC
]) {
558 GlobalValue
*DGV
= getLinkedToGlobal(GV2
);
559 bool LinkFromSrc
= true;
560 if (DGV
&& shouldLinkFromSource(LinkFromSrc
, *DGV
, *GV2
))
563 ValuesToLink
.insert(GV2
);
567 if (InternalizeCallback
) {
568 for (GlobalValue
*GV
: ValuesToLink
)
569 Internalize
.insert(GV
->getName());
572 // FIXME: Propagate Errors through to the caller instead of emitting
574 bool HasErrors
= false;
576 Mover
.move(std::move(SrcM
), ValuesToLink
.getArrayRef(),
577 IRMover::LazyCallback(
578 [this](GlobalValue
&GV
, IRMover::ValueAdder Add
) {
581 /* IsPerformingImport */ false)) {
582 handleAllErrors(std::move(E
), [&](ErrorInfoBase
&EIB
) {
583 DstM
.getContext().diagnose(LinkDiagnosticInfo(DS_Error
, EIB
.message()));
590 if (InternalizeCallback
)
591 InternalizeCallback(DstM
, Internalize
);
596 Linker::Linker(Module
&M
) : Mover(M
) {}
598 bool Linker::linkInModule(
599 std::unique_ptr
<Module
> Src
, unsigned Flags
,
600 std::function
<void(Module
&, const StringSet
<> &)> InternalizeCallback
) {
601 ModuleLinker
ModLinker(Mover
, std::move(Src
), Flags
,
602 std::move(InternalizeCallback
));
603 return ModLinker
.run();
606 //===----------------------------------------------------------------------===//
607 // LinkModules entrypoint.
608 //===----------------------------------------------------------------------===//
610 /// This function links two modules together, with the resulting Dest module
611 /// modified to be the composite of the two input modules. If an error occurs,
612 /// true is returned and ErrorMsg (if not null) is set to indicate the problem.
613 /// Upon failure, the Dest module could be in a modified state, and shouldn't be
614 /// relied on to be consistent.
615 bool Linker::linkModules(
616 Module
&Dest
, std::unique_ptr
<Module
> Src
, unsigned Flags
,
617 std::function
<void(Module
&, const StringSet
<> &)> InternalizeCallback
) {
619 return L
.linkInModule(std::move(Src
), Flags
, std::move(InternalizeCallback
));
622 //===----------------------------------------------------------------------===//
624 //===----------------------------------------------------------------------===//
626 LLVMBool
LLVMLinkModules2(LLVMModuleRef Dest
, LLVMModuleRef Src
) {
627 Module
*D
= unwrap(Dest
);
628 std::unique_ptr
<Module
> M(unwrap(Src
));
629 return Linker::linkModules(*D
, std::move(M
));