1 //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVM module linker.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Linker/Linker.h"
15 #include "LinkDiagnosticInfo.h"
16 #include "llvm-c/Linker.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/ADT/StringSet.h"
19 #include "llvm/IR/DiagnosticPrinter.h"
20 #include "llvm/IR/LLVMContext.h"
25 /// This is an implementation class for the LinkModules function, which is the
26 /// entrypoint for this file.
31 SetVector<GlobalValue *> ValuesToLink;
32 StringSet<> Internalize;
34 /// For symbol clashes, prefer those from Src.
37 /// Function index passed into ModuleLinker for using in function
38 /// importing/exporting handling.
39 const FunctionInfoIndex *ImportIndex;
41 /// Functions to import from source module, all other functions are
42 /// imported as declarations instead of definitions.
43 DenseSet<const GlobalValue *> *FunctionsToImport;
45 /// Set to true if the given FunctionInfoIndex contains any functions
46 /// from this source module, in which case we must conservatively assume
47 /// that any of its functions may be imported into another module
48 /// as part of a different backend compilation process.
49 bool HasExportedFunctions = false;
51 /// Association between metadata value id and temporary metadata that
52 /// remains unmapped after function importing. Saved during function
53 /// importing and consumed during the metadata linking postpass.
54 DenseMap<unsigned, MDNode *> *ValIDToTempMDMap;
56 /// Used as the callback for lazy linking.
57 /// The mover has just hit GV and we have to decide if it, and other members
58 /// of the same comdat, should be linked. Every member to be linked is passed
60 void addLazyFor(GlobalValue &GV, IRMover::ValueAdder Add);
62 bool shouldOverrideFromSrc() { return Flags & Linker::OverrideFromSrc; }
63 bool shouldLinkOnlyNeeded() { return Flags & Linker::LinkOnlyNeeded; }
64 bool shouldInternalizeLinkedSymbols() {
65 return Flags & Linker::InternalizeLinkedSymbols;
68 /// Check if we should promote the given local value to global scope.
69 bool doPromoteLocalToGlobal(const GlobalValue *SGV);
71 bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest,
72 const GlobalValue &Src);
74 /// Should we have mover and linker error diag info?
75 bool emitError(const Twine &Message) {
76 SrcM.getContext().diagnose(LinkDiagnosticInfo(DS_Error, Message));
80 bool getComdatLeader(Module &M, StringRef ComdatName,
81 const GlobalVariable *&GVar);
82 bool computeResultingSelectionKind(StringRef ComdatName,
83 Comdat::SelectionKind Src,
84 Comdat::SelectionKind Dst,
85 Comdat::SelectionKind &Result,
87 std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>>
89 bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK,
91 // Keep track of the global value members of each comdat in source.
92 DenseMap<const Comdat *, std::vector<GlobalValue *>> ComdatMembers;
94 /// Given a global in the source module, return the global in the
95 /// destination module that is being linked to, if any.
96 GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
97 Module &DstM = Mover.getModule();
98 // If the source has no name it can't link. If it has local linkage,
99 // there is no name match-up going on.
100 if (!SrcGV->hasName() || GlobalValue::isLocalLinkage(getLinkage(SrcGV)))
103 // Otherwise see if we have a match in the destination module's symtab.
104 GlobalValue *DGV = DstM.getNamedValue(getName(SrcGV));
108 // If we found a global with the same name in the dest module, but it has
109 // internal linkage, we are really not doing any linkage here.
110 if (DGV->hasLocalLinkage())
113 // Otherwise, we do in fact link to the destination global.
117 bool linkIfNeeded(GlobalValue &GV);
119 /// Helper methods to check if we are importing from or potentially
120 /// exporting from the current source module.
121 bool isPerformingImport() const { return FunctionsToImport != nullptr; }
122 bool isModuleExporting() const { return HasExportedFunctions; }
124 /// If we are importing from the source module, checks if we should
125 /// import SGV as a definition, otherwise import as a declaration.
126 bool doImportAsDefinition(const GlobalValue *SGV);
128 /// Get the name for SGV that should be used in the linked destination
129 /// module. Specifically, this handles the case where we need to rename
130 /// a local that is being promoted to global scope.
131 std::string getName(const GlobalValue *SGV);
133 /// Process globals so that they can be used in ThinLTO. This includes
134 /// promoting local variables so that they can be reference externally by
135 /// thin lto imported globals and converting strong external globals to
136 /// available_externally.
137 void processGlobalsForThinLTO();
138 void processGlobalForThinLTO(GlobalValue &GV);
140 /// Get the new linkage for SGV that should be used in the linked destination
141 /// module. Specifically, for ThinLTO importing or exporting it may need
143 GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV);
146 ModuleLinker(IRMover &Mover, Module &SrcM, unsigned Flags,
147 const FunctionInfoIndex *Index = nullptr,
148 DenseSet<const GlobalValue *> *FunctionsToImport = nullptr,
149 DenseMap<unsigned, MDNode *> *ValIDToTempMDMap = nullptr)
150 : Mover(Mover), SrcM(SrcM), Flags(Flags), ImportIndex(Index),
151 FunctionsToImport(FunctionsToImport),
152 ValIDToTempMDMap(ValIDToTempMDMap) {
153 assert((ImportIndex || !FunctionsToImport) &&
154 "Expect a FunctionInfoIndex when importing");
155 // If we have a FunctionInfoIndex but no function to import,
156 // then this is the primary module being compiled in a ThinLTO
157 // backend compilation, and we need to see if it has functions that
158 // may be exported to another backend compilation.
159 if (ImportIndex && !FunctionsToImport)
160 HasExportedFunctions = ImportIndex->hasExportedFunctions(SrcM);
161 assert((ValIDToTempMDMap || !FunctionsToImport) &&
162 "Function importing must provide a ValIDToTempMDMap");
169 bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) {
170 if (!isPerformingImport())
172 auto *GA = dyn_cast<GlobalAlias>(SGV);
174 if (GA->hasWeakAnyLinkage())
176 const GlobalObject *GO = GA->getBaseObject();
177 if (!GO->hasLinkOnceODRLinkage())
179 return doImportAsDefinition(GO);
181 // Always import GlobalVariable definitions, except for the special
182 // case of WeakAny which are imported as ExternalWeak declarations
183 // (see comments in ModuleLinker::getLinkage). The linkage changes
184 // described in ModuleLinker::getLinkage ensure the correct behavior (e.g.
185 // global variables with external linkage are transformed to
186 // available_externally definitions, which are ultimately turned into
187 // declarations after the EliminateAvailableExternally pass).
188 if (isa<GlobalVariable>(SGV) && !SGV->isDeclaration() &&
189 !SGV->hasWeakAnyLinkage())
191 // Only import the function requested for importing.
192 auto *SF = dyn_cast<Function>(SGV);
193 if (SF && FunctionsToImport->count(SF))
199 bool ModuleLinker::doPromoteLocalToGlobal(const GlobalValue *SGV) {
200 assert(SGV->hasLocalLinkage());
201 // Both the imported references and the original local variable must
203 if (!isPerformingImport() && !isModuleExporting())
206 // Local const variables never need to be promoted unless they are address
207 // taken. The imported uses can simply use the clone created in this module.
208 // For now we are conservative in determining which variables are not
209 // address taken by checking the unnamed addr flag. To be more aggressive,
210 // the address taken information must be checked earlier during parsing
211 // of the module and recorded in the function index for use when importing
213 auto *GVar = dyn_cast<GlobalVariable>(SGV);
214 if (GVar && GVar->isConstant() && GVar->hasUnnamedAddr())
217 // Eventually we only need to promote functions in the exporting module that
218 // are referenced by a potentially exported function (i.e. one that is in the
223 std::string ModuleLinker::getName(const GlobalValue *SGV) {
224 // For locals that must be promoted to global scope, ensure that
225 // the promoted name uniquely identifies the copy in the original module,
226 // using the ID assigned during combined index creation. When importing,
227 // we rename all locals (not just those that are promoted) in order to
228 // avoid naming conflicts between locals imported from different modules.
229 if (SGV->hasLocalLinkage() &&
230 (doPromoteLocalToGlobal(SGV) || isPerformingImport()))
231 return FunctionInfoIndex::getGlobalNameForLocal(
233 ImportIndex->getModuleId(SGV->getParent()->getModuleIdentifier()));
234 return SGV->getName();
237 GlobalValue::LinkageTypes ModuleLinker::getLinkage(const GlobalValue *SGV) {
238 // Any local variable that is referenced by an exported function needs
239 // to be promoted to global scope. Since we don't currently know which
240 // functions reference which local variables/functions, we must treat
241 // all as potentially exported if this module is exporting anything.
242 if (isModuleExporting()) {
243 if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV))
244 return GlobalValue::ExternalLinkage;
245 return SGV->getLinkage();
248 // Otherwise, if we aren't importing, no linkage change is needed.
249 if (!isPerformingImport())
250 return SGV->getLinkage();
252 switch (SGV->getLinkage()) {
253 case GlobalValue::ExternalLinkage:
254 // External defnitions are converted to available_externally
255 // definitions upon import, so that they are available for inlining
256 // and/or optimization, but are turned into declarations later
257 // during the EliminateAvailableExternally pass.
258 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
259 return GlobalValue::AvailableExternallyLinkage;
260 // An imported external declaration stays external.
261 return SGV->getLinkage();
263 case GlobalValue::AvailableExternallyLinkage:
264 // An imported available_externally definition converts
265 // to external if imported as a declaration.
266 if (!doImportAsDefinition(SGV))
267 return GlobalValue::ExternalLinkage;
268 // An imported available_externally declaration stays that way.
269 return SGV->getLinkage();
271 case GlobalValue::LinkOnceAnyLinkage:
272 case GlobalValue::LinkOnceODRLinkage:
273 // These both stay the same when importing the definition.
274 // The ThinLTO pass will eventually force-import their definitions.
275 return SGV->getLinkage();
277 case GlobalValue::WeakAnyLinkage:
278 // Can't import weak_any definitions correctly, or we might change the
279 // program semantics, since the linker will pick the first weak_any
280 // definition and importing would change the order they are seen by the
281 // linker. The module linking caller needs to enforce this.
282 assert(!doImportAsDefinition(SGV));
283 // If imported as a declaration, it becomes external_weak.
284 return GlobalValue::ExternalWeakLinkage;
286 case GlobalValue::WeakODRLinkage:
287 // For weak_odr linkage, there is a guarantee that all copies will be
288 // equivalent, so the issue described above for weak_any does not exist,
289 // and the definition can be imported. It can be treated similarly
290 // to an imported externally visible global value.
291 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
292 return GlobalValue::AvailableExternallyLinkage;
294 return GlobalValue::ExternalLinkage;
296 case GlobalValue::AppendingLinkage:
297 // It would be incorrect to import an appending linkage variable,
298 // since it would cause global constructors/destructors to be
299 // executed multiple times. This should have already been handled
300 // by linkIfNeeded, and we will assert in shouldLinkFromSource
301 // if we try to import, so we simply return AppendingLinkage here
302 // as this helper is called more widely in getLinkedToGlobal.
303 return GlobalValue::AppendingLinkage;
305 case GlobalValue::InternalLinkage:
306 case GlobalValue::PrivateLinkage:
307 // If we are promoting the local to global scope, it is handled
308 // similarly to a normal externally visible global.
309 if (doPromoteLocalToGlobal(SGV)) {
310 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
311 return GlobalValue::AvailableExternallyLinkage;
313 return GlobalValue::ExternalLinkage;
315 // A non-promoted imported local definition stays local.
316 // The ThinLTO pass will eventually force-import their definitions.
317 return SGV->getLinkage();
319 case GlobalValue::ExternalWeakLinkage:
320 // External weak doesn't apply to definitions, must be a declaration.
321 assert(!doImportAsDefinition(SGV));
322 // Linkage stays external_weak.
323 return SGV->getLinkage();
325 case GlobalValue::CommonLinkage:
326 // Linkage stays common on definitions.
327 // The ThinLTO pass will eventually force-import their definitions.
328 return SGV->getLinkage();
331 llvm_unreachable("unknown linkage type");
334 static GlobalValue::VisibilityTypes
335 getMinVisibility(GlobalValue::VisibilityTypes A,
336 GlobalValue::VisibilityTypes B) {
337 if (A == GlobalValue::HiddenVisibility || B == GlobalValue::HiddenVisibility)
338 return GlobalValue::HiddenVisibility;
339 if (A == GlobalValue::ProtectedVisibility ||
340 B == GlobalValue::ProtectedVisibility)
341 return GlobalValue::ProtectedVisibility;
342 return GlobalValue::DefaultVisibility;
345 bool ModuleLinker::getComdatLeader(Module &M, StringRef ComdatName,
346 const GlobalVariable *&GVar) {
347 const GlobalValue *GVal = M.getNamedValue(ComdatName);
348 if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) {
349 GVal = GA->getBaseObject();
351 // We cannot resolve the size of the aliasee yet.
352 return emitError("Linking COMDATs named '" + ComdatName +
353 "': COMDAT key involves incomputable alias size.");
356 GVar = dyn_cast_or_null<GlobalVariable>(GVal);
359 "Linking COMDATs named '" + ComdatName +
360 "': GlobalVariable required for data dependent selection!");
365 bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName,
366 Comdat::SelectionKind Src,
367 Comdat::SelectionKind Dst,
368 Comdat::SelectionKind &Result,
370 Module &DstM = Mover.getModule();
371 // The ability to mix Comdat::SelectionKind::Any with
372 // Comdat::SelectionKind::Largest is a behavior that comes from COFF.
373 bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any ||
374 Dst == Comdat::SelectionKind::Largest;
375 bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any ||
376 Src == Comdat::SelectionKind::Largest;
377 if (DstAnyOrLargest && SrcAnyOrLargest) {
378 if (Dst == Comdat::SelectionKind::Largest ||
379 Src == Comdat::SelectionKind::Largest)
380 Result = Comdat::SelectionKind::Largest;
382 Result = Comdat::SelectionKind::Any;
383 } else if (Src == Dst) {
386 return emitError("Linking COMDATs named '" + ComdatName +
387 "': invalid selection kinds!");
391 case Comdat::SelectionKind::Any:
395 case Comdat::SelectionKind::NoDuplicates:
396 return emitError("Linking COMDATs named '" + ComdatName +
397 "': noduplicates has been violated!");
398 case Comdat::SelectionKind::ExactMatch:
399 case Comdat::SelectionKind::Largest:
400 case Comdat::SelectionKind::SameSize: {
401 const GlobalVariable *DstGV;
402 const GlobalVariable *SrcGV;
403 if (getComdatLeader(DstM, ComdatName, DstGV) ||
404 getComdatLeader(SrcM, ComdatName, SrcGV))
407 const DataLayout &DstDL = DstM.getDataLayout();
408 const DataLayout &SrcDL = SrcM.getDataLayout();
410 DstDL.getTypeAllocSize(DstGV->getType()->getPointerElementType());
412 SrcDL.getTypeAllocSize(SrcGV->getType()->getPointerElementType());
413 if (Result == Comdat::SelectionKind::ExactMatch) {
414 if (SrcGV->getInitializer() != DstGV->getInitializer())
415 return emitError("Linking COMDATs named '" + ComdatName +
416 "': ExactMatch violated!");
418 } else if (Result == Comdat::SelectionKind::Largest) {
419 LinkFromSrc = SrcSize > DstSize;
420 } else if (Result == Comdat::SelectionKind::SameSize) {
421 if (SrcSize != DstSize)
422 return emitError("Linking COMDATs named '" + ComdatName +
423 "': SameSize violated!");
426 llvm_unreachable("unknown selection kind");
435 bool ModuleLinker::getComdatResult(const Comdat *SrcC,
436 Comdat::SelectionKind &Result,
438 Module &DstM = Mover.getModule();
439 Comdat::SelectionKind SSK = SrcC->getSelectionKind();
440 StringRef ComdatName = SrcC->getName();
441 Module::ComdatSymTabType &ComdatSymTab = DstM.getComdatSymbolTable();
442 Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName);
444 if (DstCI == ComdatSymTab.end()) {
445 // Use the comdat if it is only available in one of the modules.
451 const Comdat *DstC = &DstCI->second;
452 Comdat::SelectionKind DSK = DstC->getSelectionKind();
453 return computeResultingSelectionKind(ComdatName, SSK, DSK, Result,
457 bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
458 const GlobalValue &Dest,
459 const GlobalValue &Src) {
461 // Should we unconditionally use the Src?
462 if (shouldOverrideFromSrc()) {
467 // We always have to add Src if it has appending linkage.
468 if (Src.hasAppendingLinkage()) {
469 // Should have prevented importing for appending linkage in linkIfNeeded.
470 assert(!isPerformingImport());
475 bool SrcIsDeclaration = Src.isDeclarationForLinker();
476 bool DestIsDeclaration = Dest.isDeclarationForLinker();
478 if (isPerformingImport()) {
479 if (isa<Function>(&Src)) {
480 // For functions, LinkFromSrc iff this is a function requested
481 // for importing. For variables, decide below normally.
482 LinkFromSrc = FunctionsToImport->count(&Src);
486 // Check if this is an alias with an already existing definition
487 // in Dest, which must have come from a prior importing pass from
488 // the same Src module. Unlike imported function and variable
489 // definitions, which are imported as available_externally and are
490 // not definitions for the linker, that is not a valid linkage for
491 // imported aliases which must be definitions. Simply use the existing
493 if (isa<GlobalAlias>(&Src) && !DestIsDeclaration) {
494 assert(isa<GlobalAlias>(&Dest));
500 if (SrcIsDeclaration) {
501 // If Src is external or if both Src & Dest are external.. Just link the
502 // external globals, we aren't adding anything.
503 if (Src.hasDLLImportStorageClass()) {
504 // If one of GVs is marked as DLLImport, result should be dllimport'ed.
505 LinkFromSrc = DestIsDeclaration;
508 // If the Dest is weak, use the source linkage.
509 if (Dest.hasExternalWeakLinkage()) {
513 // Link an available_externally over a declaration.
514 LinkFromSrc = !Src.isDeclaration() && Dest.isDeclaration();
518 if (DestIsDeclaration) {
519 // If Dest is external but Src is not:
524 if (Src.hasCommonLinkage()) {
525 if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) {
530 if (!Dest.hasCommonLinkage()) {
535 const DataLayout &DL = Dest.getParent()->getDataLayout();
536 uint64_t DestSize = DL.getTypeAllocSize(Dest.getType()->getElementType());
537 uint64_t SrcSize = DL.getTypeAllocSize(Src.getType()->getElementType());
538 LinkFromSrc = SrcSize > DestSize;
542 if (Src.isWeakForLinker()) {
543 assert(!Dest.hasExternalWeakLinkage());
544 assert(!Dest.hasAvailableExternallyLinkage());
546 if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage()) {
555 if (Dest.isWeakForLinker()) {
556 assert(Src.hasExternalLinkage());
561 assert(!Src.hasExternalWeakLinkage());
562 assert(!Dest.hasExternalWeakLinkage());
563 assert(Dest.hasExternalLinkage() && Src.hasExternalLinkage() &&
564 "Unexpected linkage type!");
565 return emitError("Linking globals named '" + Src.getName() +
566 "': symbol multiply defined!");
569 bool ModuleLinker::linkIfNeeded(GlobalValue &GV) {
570 GlobalValue *DGV = getLinkedToGlobal(&GV);
572 if (shouldLinkOnlyNeeded() && !(DGV && DGV->isDeclaration()))
575 if (DGV && !GV.hasLocalLinkage() && !GV.hasAppendingLinkage()) {
576 auto *DGVar = dyn_cast<GlobalVariable>(DGV);
577 auto *SGVar = dyn_cast<GlobalVariable>(&GV);
578 if (DGVar && SGVar) {
579 if (DGVar->isDeclaration() && SGVar->isDeclaration() &&
580 (!DGVar->isConstant() || !SGVar->isConstant())) {
581 DGVar->setConstant(false);
582 SGVar->setConstant(false);
584 if (DGVar->hasCommonLinkage() && SGVar->hasCommonLinkage()) {
585 unsigned Align = std::max(DGVar->getAlignment(), SGVar->getAlignment());
586 SGVar->setAlignment(Align);
587 DGVar->setAlignment(Align);
591 GlobalValue::VisibilityTypes Visibility =
592 getMinVisibility(DGV->getVisibility(), GV.getVisibility());
593 DGV->setVisibility(Visibility);
594 GV.setVisibility(Visibility);
596 bool HasUnnamedAddr = GV.hasUnnamedAddr() && DGV->hasUnnamedAddr();
597 DGV->setUnnamedAddr(HasUnnamedAddr);
598 GV.setUnnamedAddr(HasUnnamedAddr);
601 // Don't want to append to global_ctors list, for example, when we
602 // are importing for ThinLTO, otherwise the global ctors and dtors
603 // get executed multiple times for local variables (the latter causing
605 if (GV.hasAppendingLinkage() && isPerformingImport())
608 if (isPerformingImport() && !doImportAsDefinition(&GV))
611 if (!DGV && !shouldOverrideFromSrc() &&
612 (GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() ||
613 GV.hasAvailableExternallyLinkage()))
616 if (GV.isDeclaration())
619 if (const Comdat *SC = GV.getComdat()) {
621 Comdat::SelectionKind SK;
622 std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
624 ValuesToLink.insert(&GV);
628 bool LinkFromSrc = true;
629 if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, GV))
632 ValuesToLink.insert(&GV);
636 void ModuleLinker::addLazyFor(GlobalValue &GV, IRMover::ValueAdder Add) {
637 // Add these to the internalize list
638 if (!GV.hasLinkOnceLinkage())
641 if (shouldInternalizeLinkedSymbols())
642 Internalize.insert(GV.getName());
645 const Comdat *SC = GV.getComdat();
648 for (GlobalValue *GV2 : ComdatMembers[SC]) {
649 if (!GV2->hasLocalLinkage() && shouldInternalizeLinkedSymbols())
650 Internalize.insert(GV2->getName());
655 void ModuleLinker::processGlobalForThinLTO(GlobalValue &GV) {
656 if (GV.hasLocalLinkage() &&
657 (doPromoteLocalToGlobal(&GV) || isPerformingImport())) {
658 GV.setName(getName(&GV));
659 GV.setLinkage(getLinkage(&GV));
660 if (!GV.hasLocalLinkage())
661 GV.setVisibility(GlobalValue::HiddenVisibility);
662 if (isModuleExporting())
663 ValuesToLink.insert(&GV);
666 GV.setLinkage(getLinkage(&GV));
669 void ModuleLinker::processGlobalsForThinLTO() {
670 for (GlobalVariable &GV : SrcM.globals())
671 processGlobalForThinLTO(GV);
672 for (Function &SF : SrcM)
673 processGlobalForThinLTO(SF);
674 for (GlobalAlias &GA : SrcM.aliases())
675 processGlobalForThinLTO(GA);
678 bool ModuleLinker::run() {
679 for (const auto &SMEC : SrcM.getComdatSymbolTable()) {
680 const Comdat &C = SMEC.getValue();
681 if (ComdatsChosen.count(&C))
683 Comdat::SelectionKind SK;
685 if (getComdatResult(&C, SK, LinkFromSrc))
687 ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc);
690 for (GlobalVariable &GV : SrcM.globals())
691 if (const Comdat *SC = GV.getComdat())
692 ComdatMembers[SC].push_back(&GV);
694 for (Function &SF : SrcM)
695 if (const Comdat *SC = SF.getComdat())
696 ComdatMembers[SC].push_back(&SF);
698 for (GlobalAlias &GA : SrcM.aliases())
699 if (const Comdat *SC = GA.getComdat())
700 ComdatMembers[SC].push_back(&GA);
702 // Insert all of the globals in src into the DstM module... without linking
703 // initializers (which could refer to functions not yet mapped over).
704 for (GlobalVariable &GV : SrcM.globals())
705 if (linkIfNeeded(GV))
708 for (Function &SF : SrcM)
709 if (linkIfNeeded(SF))
712 for (GlobalAlias &GA : SrcM.aliases())
713 if (linkIfNeeded(GA))
716 processGlobalsForThinLTO();
718 for (unsigned I = 0; I < ValuesToLink.size(); ++I) {
719 GlobalValue *GV = ValuesToLink[I];
720 const Comdat *SC = GV->getComdat();
723 for (GlobalValue *GV2 : ComdatMembers[SC])
724 ValuesToLink.insert(GV2);
727 if (shouldInternalizeLinkedSymbols()) {
728 for (GlobalValue *GV : ValuesToLink)
729 Internalize.insert(GV->getName());
732 if (Mover.move(SrcM, ValuesToLink.getArrayRef(),
733 [this](GlobalValue &GV, IRMover::ValueAdder Add) {
736 ValIDToTempMDMap, false))
738 Module &DstM = Mover.getModule();
739 for (auto &P : Internalize) {
740 GlobalValue *GV = DstM.getNamedValue(P.first());
741 GV->setLinkage(GlobalValue::InternalLinkage);
747 Linker::Linker(Module &M) : Mover(M) {}
749 bool Linker::linkInModule(std::unique_ptr<Module> Src, unsigned Flags,
750 const FunctionInfoIndex *Index,
751 DenseSet<const GlobalValue *> *FunctionsToImport,
752 DenseMap<unsigned, MDNode *> *ValIDToTempMDMap) {
753 ModuleLinker ModLinker(Mover, *Src, Flags, Index, FunctionsToImport,
755 return ModLinker.run();
758 bool Linker::linkInModuleForCAPI(Module &Src) {
759 ModuleLinker ModLinker(Mover, Src, 0, nullptr, nullptr);
760 return ModLinker.run();
763 bool Linker::linkInMetadata(Module &Src,
764 DenseMap<unsigned, MDNode *> *ValIDToTempMDMap) {
765 SetVector<GlobalValue *> ValuesToLink;
767 Src, ValuesToLink.getArrayRef(),
768 [this](GlobalValue &GV, IRMover::ValueAdder Add) { assert(false); },
769 ValIDToTempMDMap, true))
774 //===----------------------------------------------------------------------===//
775 // LinkModules entrypoint.
776 //===----------------------------------------------------------------------===//
778 /// This function links two modules together, with the resulting Dest module
779 /// modified to be the composite of the two input modules. If an error occurs,
780 /// true is returned and ErrorMsg (if not null) is set to indicate the problem.
781 /// Upon failure, the Dest module could be in a modified state, and shouldn't be
782 /// relied on to be consistent.
783 bool Linker::linkModules(Module &Dest, std::unique_ptr<Module> Src,
786 return L.linkInModule(std::move(Src), Flags);
789 std::unique_ptr<Module>
790 llvm::renameModuleForThinLTO(std::unique_ptr<Module> M,
791 const FunctionInfoIndex *Index) {
792 std::unique_ptr<llvm::Module> RenamedModule(
793 new llvm::Module(M->getModuleIdentifier(), M->getContext()));
794 Linker L(*RenamedModule.get());
795 if (L.linkInModule(std::move(M), llvm::Linker::Flags::None, Index))
797 return RenamedModule;
800 //===----------------------------------------------------------------------===//
802 //===----------------------------------------------------------------------===//
804 static void diagnosticHandler(const DiagnosticInfo &DI, void *C) {
805 auto *Message = reinterpret_cast<std::string *>(C);
806 raw_string_ostream Stream(*Message);
807 DiagnosticPrinterRawOStream DP(Stream);
811 LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
812 LLVMLinkerMode Unused, char **OutMessages) {
813 Module *D = unwrap(Dest);
814 LLVMContext &Ctx = D->getContext();
816 LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler =
817 Ctx.getDiagnosticHandler();
818 void *OldDiagnosticContext = Ctx.getDiagnosticContext();
820 Ctx.setDiagnosticHandler(diagnosticHandler, &Message, true);
823 Module *M = unwrap(Src);
824 LLVMBool Result = L.linkInModuleForCAPI(*M);
826 Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true);
828 if (OutMessages && Result)
829 *OutMessages = strdup(Message.c_str());
833 LLVMBool LLVMLinkModules2(LLVMModuleRef Dest, LLVMModuleRef Src) {
834 Module *D = unwrap(Dest);
835 std::unique_ptr<Module> M(unwrap(Src));
836 return Linker::linkModules(*D, std::move(M));