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