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