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 | |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 42 | /// Association between metadata value id and temporary metadata that |
| 43 | /// remains unmapped after function importing. Saved during function |
| 44 | /// importing and consumed during the metadata linking postpass. |
| 45 | DenseMap<unsigned, MDNode *> *ValIDToTempMDMap; |
| 46 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 47 | /// Used as the callback for lazy linking. |
| 48 | /// The mover has just hit GV and we have to decide if it, and other members |
| 49 | /// of the same comdat, should be linked. Every member to be linked is passed |
| 50 | /// to Add. |
| 51 | void addLazyFor(GlobalValue &GV, IRMover::ValueAdder Add); |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 52 | |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 53 | bool shouldOverrideFromSrc() { return Flags & Linker::OverrideFromSrc; } |
| 54 | bool shouldLinkOnlyNeeded() { return Flags & Linker::LinkOnlyNeeded; } |
| 55 | bool shouldInternalizeLinkedSymbols() { |
| 56 | return Flags & Linker::InternalizeLinkedSymbols; |
| 57 | } |
| 58 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 59 | bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest, |
| 60 | const GlobalValue &Src); |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 61 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 62 | /// Should we have mover and linker error diag info? |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 63 | bool emitError(const Twine &Message) { |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 64 | SrcM->getContext().diagnose(LinkDiagnosticInfo(DS_Error, Message)); |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 65 | return true; |
| 66 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 67 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 68 | bool getComdatLeader(Module &M, StringRef ComdatName, |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 69 | const GlobalVariable *&GVar); |
| 70 | bool computeResultingSelectionKind(StringRef ComdatName, |
| 71 | Comdat::SelectionKind Src, |
| 72 | Comdat::SelectionKind Dst, |
| 73 | Comdat::SelectionKind &Result, |
| 74 | bool &LinkFromSrc); |
| 75 | std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>> |
| 76 | ComdatsChosen; |
| 77 | bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK, |
| 78 | bool &LinkFromSrc); |
Rafael Espindola | 1ee9fbd | 2016-03-24 00:06:03 +0000 | [diff] [blame^] | 79 | // Keep track of the lazy linked global members of each comdat in source. |
| 80 | DenseMap<const Comdat *, std::vector<GlobalValue *>> LazyComdatMembers; |
Rafael Espindola | 4160f5d | 2014-10-27 23:02:10 +0000 | [diff] [blame] | 81 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 82 | /// Given a global in the source module, return the global in the |
| 83 | /// destination module that is being linked to, if any. |
| 84 | GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 85 | Module &DstM = Mover.getModule(); |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 86 | // If the source has no name it can't link. If it has local linkage, |
| 87 | // there is no name match-up going on. |
Teresa Johnson | 4504c1b | 2016-01-08 15:00:00 +0000 | [diff] [blame] | 88 | if (!SrcGV->hasName() || GlobalValue::isLocalLinkage(SrcGV->getLinkage())) |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 89 | return nullptr; |
Eli Bendersky | 7da92ed | 2014-02-20 22:19:24 +0000 | [diff] [blame] | 90 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 91 | // 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] | 92 | GlobalValue *DGV = DstM.getNamedValue(SrcGV->getName()); |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 93 | if (!DGV) |
| 94 | return nullptr; |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 95 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 96 | // If we found a global with the same name in the dest module, but it has |
| 97 | // internal linkage, we are really not doing any linkage here. |
| 98 | if (DGV->hasLocalLinkage()) |
| 99 | return nullptr; |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 100 | |
Rafael Espindola | c84f608 | 2014-11-25 06:11:24 +0000 | [diff] [blame] | 101 | // Otherwise, we do in fact link to the destination global. |
| 102 | return DGV; |
| 103 | } |
Rafael Espindola | ed6dc37 | 2014-05-09 14:39:25 +0000 | [diff] [blame] | 104 | |
Rafael Espindola | 370d528 | 2016-03-22 21:35:47 +0000 | [diff] [blame] | 105 | /// Drop GV if it is a member of a comdat that we are dropping. |
| 106 | /// This can happen with COFF's largest selection kind. |
| 107 | void dropReplacedComdat(GlobalValue &GV, |
| 108 | const DenseSet<const Comdat *> &ReplacedDstComdats); |
| 109 | |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 110 | bool linkIfNeeded(GlobalValue &GV); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 111 | |
Teresa Johnson | 4504c1b | 2016-01-08 15:00:00 +0000 | [diff] [blame] | 112 | /// Helper method to check if we are importing from the current source |
| 113 | /// module. |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 114 | bool isPerformingImport() const { return GlobalsToImport != nullptr; } |
Teresa Johnson | 4504c1b | 2016-01-08 15:00:00 +0000 | [diff] [blame] | 115 | |
| 116 | /// If we are importing from the source module, checks if we should |
| 117 | /// import SGV as a definition, otherwise import as a declaration. |
| 118 | bool doImportAsDefinition(const GlobalValue *SGV); |
| 119 | |
| 120 | public: |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 121 | ModuleLinker(IRMover &Mover, std::unique_ptr<Module> SrcM, unsigned Flags, |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 122 | DenseSet<const GlobalValue *> *GlobalsToImport = nullptr, |
Teresa Johnson | 4504c1b | 2016-01-08 15:00:00 +0000 | [diff] [blame] | 123 | DenseMap<unsigned, MDNode *> *ValIDToTempMDMap = nullptr) |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 124 | : Mover(Mover), SrcM(std::move(SrcM)), Flags(Flags), |
| 125 | GlobalsToImport(GlobalsToImport), ValIDToTempMDMap(ValIDToTempMDMap) {} |
Teresa Johnson | 4504c1b | 2016-01-08 15:00:00 +0000 | [diff] [blame] | 126 | |
| 127 | bool run(); |
| 128 | }; |
Teresa Johnson | 4504c1b | 2016-01-08 15:00:00 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) { |
| 132 | if (!isPerformingImport()) |
| 133 | return false; |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 134 | return FunctionImportGlobalProcessing::doImportAsDefinition(SGV, |
| 135 | GlobalsToImport); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Rafael Espindola | eb5e0a7 | 2015-11-29 14:33:06 +0000 | [diff] [blame] | 138 | static GlobalValue::VisibilityTypes |
| 139 | getMinVisibility(GlobalValue::VisibilityTypes A, |
| 140 | GlobalValue::VisibilityTypes B) { |
| 141 | if (A == GlobalValue::HiddenVisibility || B == GlobalValue::HiddenVisibility) |
| 142 | return GlobalValue::HiddenVisibility; |
| 143 | if (A == GlobalValue::ProtectedVisibility || |
| 144 | B == GlobalValue::ProtectedVisibility) |
| 145 | return GlobalValue::ProtectedVisibility; |
| 146 | return GlobalValue::DefaultVisibility; |
| 147 | } |
| 148 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 149 | bool ModuleLinker::getComdatLeader(Module &M, StringRef ComdatName, |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 150 | const GlobalVariable *&GVar) { |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 151 | const GlobalValue *GVal = M.getNamedValue(ComdatName); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 152 | if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) { |
| 153 | GVal = GA->getBaseObject(); |
| 154 | if (!GVal) |
| 155 | // We cannot resolve the size of the aliasee yet. |
| 156 | return emitError("Linking COMDATs named '" + ComdatName + |
| 157 | "': COMDAT key involves incomputable alias size."); |
| 158 | } |
| 159 | |
| 160 | GVar = dyn_cast_or_null<GlobalVariable>(GVal); |
| 161 | if (!GVar) |
| 162 | return emitError( |
| 163 | "Linking COMDATs named '" + ComdatName + |
| 164 | "': GlobalVariable required for data dependent selection!"); |
| 165 | |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName, |
| 170 | Comdat::SelectionKind Src, |
| 171 | Comdat::SelectionKind Dst, |
| 172 | Comdat::SelectionKind &Result, |
| 173 | bool &LinkFromSrc) { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 174 | Module &DstM = Mover.getModule(); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 175 | // The ability to mix Comdat::SelectionKind::Any with |
| 176 | // Comdat::SelectionKind::Largest is a behavior that comes from COFF. |
| 177 | bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any || |
| 178 | Dst == Comdat::SelectionKind::Largest; |
| 179 | bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any || |
| 180 | Src == Comdat::SelectionKind::Largest; |
| 181 | if (DstAnyOrLargest && SrcAnyOrLargest) { |
| 182 | if (Dst == Comdat::SelectionKind::Largest || |
| 183 | Src == Comdat::SelectionKind::Largest) |
| 184 | Result = Comdat::SelectionKind::Largest; |
| 185 | else |
| 186 | Result = Comdat::SelectionKind::Any; |
| 187 | } else if (Src == Dst) { |
| 188 | Result = Dst; |
| 189 | } else { |
| 190 | return emitError("Linking COMDATs named '" + ComdatName + |
| 191 | "': invalid selection kinds!"); |
| 192 | } |
| 193 | |
| 194 | switch (Result) { |
| 195 | case Comdat::SelectionKind::Any: |
| 196 | // Go with Dst. |
| 197 | LinkFromSrc = false; |
| 198 | break; |
| 199 | case Comdat::SelectionKind::NoDuplicates: |
| 200 | return emitError("Linking COMDATs named '" + ComdatName + |
| 201 | "': noduplicates has been violated!"); |
| 202 | case Comdat::SelectionKind::ExactMatch: |
| 203 | case Comdat::SelectionKind::Largest: |
| 204 | case Comdat::SelectionKind::SameSize: { |
| 205 | const GlobalVariable *DstGV; |
| 206 | const GlobalVariable *SrcGV; |
| 207 | if (getComdatLeader(DstM, ComdatName, DstGV) || |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 208 | getComdatLeader(*SrcM, ComdatName, SrcGV)) |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 209 | return true; |
| 210 | |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 211 | const DataLayout &DstDL = DstM.getDataLayout(); |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 212 | const DataLayout &SrcDL = SrcM->getDataLayout(); |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 213 | uint64_t DstSize = DstDL.getTypeAllocSize(DstGV->getValueType()); |
| 214 | uint64_t SrcSize = SrcDL.getTypeAllocSize(SrcGV->getValueType()); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 215 | if (Result == Comdat::SelectionKind::ExactMatch) { |
| 216 | if (SrcGV->getInitializer() != DstGV->getInitializer()) |
| 217 | return emitError("Linking COMDATs named '" + ComdatName + |
| 218 | "': ExactMatch violated!"); |
| 219 | LinkFromSrc = false; |
| 220 | } else if (Result == Comdat::SelectionKind::Largest) { |
| 221 | LinkFromSrc = SrcSize > DstSize; |
| 222 | } else if (Result == Comdat::SelectionKind::SameSize) { |
| 223 | if (SrcSize != DstSize) |
| 224 | return emitError("Linking COMDATs named '" + ComdatName + |
| 225 | "': SameSize violated!"); |
| 226 | LinkFromSrc = false; |
| 227 | } else { |
| 228 | llvm_unreachable("unknown selection kind"); |
| 229 | } |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | bool ModuleLinker::getComdatResult(const Comdat *SrcC, |
| 238 | Comdat::SelectionKind &Result, |
| 239 | bool &LinkFromSrc) { |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 240 | Module &DstM = Mover.getModule(); |
Rafael Espindola | b16196a | 2014-08-11 17:07:34 +0000 | [diff] [blame] | 241 | Comdat::SelectionKind SSK = SrcC->getSelectionKind(); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 242 | StringRef ComdatName = SrcC->getName(); |
Rafael Espindola | 0e309fe | 2015-12-01 19:50:54 +0000 | [diff] [blame] | 243 | Module::ComdatSymTabType &ComdatSymTab = DstM.getComdatSymbolTable(); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 244 | Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName); |
Rafael Espindola | 2ef3f29 | 2014-08-11 16:55:42 +0000 | [diff] [blame] | 245 | |
Rafael Espindola | b16196a | 2014-08-11 17:07:34 +0000 | [diff] [blame] | 246 | if (DstCI == ComdatSymTab.end()) { |
| 247 | // Use the comdat if it is only available in one of the modules. |
| 248 | LinkFromSrc = true; |
| 249 | Result = SSK; |
Rafael Espindola | 2ef3f29 | 2014-08-11 16:55:42 +0000 | [diff] [blame] | 250 | return false; |
Rafael Espindola | b16196a | 2014-08-11 17:07:34 +0000 | [diff] [blame] | 251 | } |
Rafael Espindola | 2ef3f29 | 2014-08-11 16:55:42 +0000 | [diff] [blame] | 252 | |
| 253 | const Comdat *DstC = &DstCI->second; |
Rafael Espindola | 2ef3f29 | 2014-08-11 16:55:42 +0000 | [diff] [blame] | 254 | Comdat::SelectionKind DSK = DstC->getSelectionKind(); |
| 255 | return computeResultingSelectionKind(ComdatName, SSK, DSK, Result, |
| 256 | LinkFromSrc); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 257 | } |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 258 | |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 259 | bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc, |
| 260 | const GlobalValue &Dest, |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 261 | const GlobalValue &Src) { |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 262 | |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 263 | // Should we unconditionally use the Src? |
Artem Belevich | 020d4fb | 2015-09-01 17:55:55 +0000 | [diff] [blame] | 264 | if (shouldOverrideFromSrc()) { |
Duncan P. N. Exon Smith | e868123 | 2015-04-22 04:11:00 +0000 | [diff] [blame] | 265 | LinkFromSrc = true; |
| 266 | return false; |
| 267 | } |
| 268 | |
Rafael Espindola | 778fcc7 | 2014-11-02 13:28:57 +0000 | [diff] [blame] | 269 | // We always have to add Src if it has appending linkage. |
| 270 | if (Src.hasAppendingLinkage()) { |
Teresa Johnson | 1e20a65 | 2015-12-03 18:20:05 +0000 | [diff] [blame] | 271 | // Should have prevented importing for appending linkage in linkIfNeeded. |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 272 | assert(!isPerformingImport()); |
Rafael Espindola | 778fcc7 | 2014-11-02 13:28:57 +0000 | [diff] [blame] | 273 | LinkFromSrc = true; |
| 274 | return false; |
| 275 | } |
| 276 | |
Rafael Espindola | d4bcefc | 2014-10-24 18:13:04 +0000 | [diff] [blame] | 277 | bool SrcIsDeclaration = Src.isDeclarationForLinker(); |
| 278 | bool DestIsDeclaration = Dest.isDeclarationForLinker(); |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 279 | |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 280 | if (isPerformingImport()) { |
| 281 | if (isa<Function>(&Src)) { |
Teresa Johnson | 4f04d85 | 2015-12-21 17:33:24 +0000 | [diff] [blame] | 282 | // For functions, LinkFromSrc iff this is a function requested |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 283 | // for importing. For variables, decide below normally. |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 284 | LinkFromSrc = GlobalsToImport->count(&Src); |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 285 | return false; |
| 286 | } |
| 287 | |
| 288 | // Check if this is an alias with an already existing definition |
| 289 | // in Dest, which must have come from a prior importing pass from |
| 290 | // the same Src module. Unlike imported function and variable |
| 291 | // definitions, which are imported as available_externally and are |
| 292 | // not definitions for the linker, that is not a valid linkage for |
| 293 | // imported aliases which must be definitions. Simply use the existing |
| 294 | // Dest copy. |
| 295 | if (isa<GlobalAlias>(&Src) && !DestIsDeclaration) { |
| 296 | assert(isa<GlobalAlias>(&Dest)); |
| 297 | LinkFromSrc = false; |
| 298 | return false; |
| 299 | } |
| 300 | } |
| 301 | |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 302 | if (SrcIsDeclaration) { |
| 303 | // If Src is external or if both Src & Dest are external.. Just link the |
| 304 | // external globals, we aren't adding anything. |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 305 | if (Src.hasDLLImportStorageClass()) { |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 306 | // 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] | 307 | LinkFromSrc = DestIsDeclaration; |
| 308 | return false; |
| 309 | } |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 310 | // If the Dest is weak, use the source linkage. |
Rafael Espindola | ed11bd2 | 2015-12-09 22:44:00 +0000 | [diff] [blame] | 311 | if (Dest.hasExternalWeakLinkage()) { |
| 312 | LinkFromSrc = true; |
| 313 | return false; |
| 314 | } |
| 315 | // Link an available_externally over a declaration. |
| 316 | LinkFromSrc = !Src.isDeclaration() && Dest.isDeclaration(); |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 317 | return false; |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 320 | if (DestIsDeclaration) { |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 321 | // If Dest is external but Src is not: |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 322 | LinkFromSrc = true; |
| 323 | return false; |
| 324 | } |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 325 | |
Rafael Espindola | 0910605 | 2014-09-09 15:59:12 +0000 | [diff] [blame] | 326 | if (Src.hasCommonLinkage()) { |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 327 | if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) { |
| 328 | LinkFromSrc = true; |
Rafael Espindola | 0910605 | 2014-09-09 15:59:12 +0000 | [diff] [blame] | 329 | return false; |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | if (!Dest.hasCommonLinkage()) { |
| 333 | LinkFromSrc = false; |
| 334 | return false; |
| 335 | } |
Rafael Espindola | 0910605 | 2014-09-09 15:59:12 +0000 | [diff] [blame] | 336 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 337 | const DataLayout &DL = Dest.getParent()->getDataLayout(); |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 338 | uint64_t DestSize = DL.getTypeAllocSize(Dest.getValueType()); |
| 339 | uint64_t SrcSize = DL.getTypeAllocSize(Src.getValueType()); |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 340 | LinkFromSrc = SrcSize > DestSize; |
| 341 | return false; |
Rafael Espindola | 0910605 | 2014-09-09 15:59:12 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 344 | if (Src.isWeakForLinker()) { |
| 345 | assert(!Dest.hasExternalWeakLinkage()); |
| 346 | assert(!Dest.hasAvailableExternallyLinkage()); |
Rafael Espindola | 0910605 | 2014-09-09 15:59:12 +0000 | [diff] [blame] | 347 | |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 348 | if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage()) { |
| 349 | LinkFromSrc = true; |
| 350 | return false; |
| 351 | } |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 352 | |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 353 | LinkFromSrc = false; |
Rafael Espindola | 0910605 | 2014-09-09 15:59:12 +0000 | [diff] [blame] | 354 | return false; |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | if (Dest.isWeakForLinker()) { |
| 358 | assert(Src.hasExternalLinkage()); |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 359 | LinkFromSrc = true; |
| 360 | return false; |
Rafael Espindola | dbb0bd1 | 2014-09-09 15:21:00 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | assert(!Src.hasExternalWeakLinkage()); |
| 364 | assert(!Dest.hasExternalWeakLinkage()); |
| 365 | assert(Dest.hasExternalLinkage() && Src.hasExternalLinkage() && |
| 366 | "Unexpected linkage type!"); |
| 367 | return emitError("Linking globals named '" + Src.getName() + |
| 368 | "': symbol multiply defined!"); |
| 369 | } |
| 370 | |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 371 | bool ModuleLinker::linkIfNeeded(GlobalValue &GV) { |
| 372 | GlobalValue *DGV = getLinkedToGlobal(&GV); |
| 373 | |
| 374 | if (shouldLinkOnlyNeeded() && !(DGV && DGV->isDeclaration())) |
| 375 | return false; |
| 376 | |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 377 | if (DGV && !GV.hasLocalLinkage() && !GV.hasAppendingLinkage()) { |
| 378 | auto *DGVar = dyn_cast<GlobalVariable>(DGV); |
| 379 | auto *SGVar = dyn_cast<GlobalVariable>(&GV); |
| 380 | if (DGVar && SGVar) { |
| 381 | if (DGVar->isDeclaration() && SGVar->isDeclaration() && |
| 382 | (!DGVar->isConstant() || !SGVar->isConstant())) { |
| 383 | DGVar->setConstant(false); |
| 384 | SGVar->setConstant(false); |
| 385 | } |
| 386 | if (DGVar->hasCommonLinkage() && SGVar->hasCommonLinkage()) { |
| 387 | unsigned Align = std::max(DGVar->getAlignment(), SGVar->getAlignment()); |
| 388 | SGVar->setAlignment(Align); |
| 389 | DGVar->setAlignment(Align); |
| 390 | } |
| 391 | } |
| 392 | |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 393 | GlobalValue::VisibilityTypes Visibility = |
| 394 | getMinVisibility(DGV->getVisibility(), GV.getVisibility()); |
| 395 | DGV->setVisibility(Visibility); |
| 396 | GV.setVisibility(Visibility); |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 397 | |
| 398 | bool HasUnnamedAddr = GV.hasUnnamedAddr() && DGV->hasUnnamedAddr(); |
| 399 | DGV->setUnnamedAddr(HasUnnamedAddr); |
| 400 | GV.setUnnamedAddr(HasUnnamedAddr); |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 403 | // Don't want to append to global_ctors list, for example, when we |
| 404 | // are importing for ThinLTO, otherwise the global ctors and dtors |
| 405 | // get executed multiple times for local variables (the latter causing |
| 406 | // double frees). |
| 407 | if (GV.hasAppendingLinkage() && isPerformingImport()) |
| 408 | return false; |
| 409 | |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 410 | if (isPerformingImport()) { |
| 411 | if (!doImportAsDefinition(&GV)) |
| 412 | return false; |
| 413 | } else if (!DGV && !shouldOverrideFromSrc() && |
| 414 | (GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() || |
| 415 | GV.hasAvailableExternallyLinkage())) |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 416 | return false; |
| 417 | |
Rafael Espindola | bd03c50 | 2015-12-07 16:31:41 +0000 | [diff] [blame] | 418 | if (GV.isDeclaration()) |
| 419 | return false; |
| 420 | |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 421 | if (const Comdat *SC = GV.getComdat()) { |
| 422 | bool LinkFromSrc; |
| 423 | Comdat::SelectionKind SK; |
| 424 | std::tie(SK, LinkFromSrc) = ComdatsChosen[SC]; |
Rafael Espindola | f2e7124 | 2016-03-23 21:16:33 +0000 | [diff] [blame] | 425 | if (!LinkFromSrc) |
| 426 | return false; |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 427 | } |
Rafael Espindola | 4b5ec26 | 2015-12-02 22:59:04 +0000 | [diff] [blame] | 428 | |
| 429 | bool LinkFromSrc = true; |
| 430 | if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, GV)) |
| 431 | return true; |
| 432 | if (LinkFromSrc) |
| 433 | ValuesToLink.insert(&GV); |
| 434 | return false; |
Rafael Espindola | baa3bf8 | 2015-12-01 15:19:48 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 437 | void ModuleLinker::addLazyFor(GlobalValue &GV, IRMover::ValueAdder Add) { |
| 438 | // Add these to the internalize list |
| 439 | if (!GV.hasLinkOnceLinkage()) |
| 440 | return; |
| 441 | |
| 442 | if (shouldInternalizeLinkedSymbols()) |
| 443 | Internalize.insert(GV.getName()); |
| 444 | Add(GV); |
| 445 | |
| 446 | const Comdat *SC = GV.getComdat(); |
| 447 | if (!SC) |
| 448 | return; |
Rafael Espindola | 1ee9fbd | 2016-03-24 00:06:03 +0000 | [diff] [blame^] | 449 | for (GlobalValue *GV2 : LazyComdatMembers[SC]) { |
| 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 | 1ee9fbd | 2016-03-24 00:06:03 +0000 | [diff] [blame^] | 569 | for (GlobalValue *GV2 : LazyComdatMembers[SC]) |
| 570 | ValuesToLink.insert(GV2); |
Rafael Espindola | beadd56 | 2014-12-08 18:05:48 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 573 | if (shouldInternalizeLinkedSymbols()) { |
| 574 | for (GlobalValue *GV : ValuesToLink) |
| 575 | Internalize.insert(GV->getName()); |
| 576 | } |
Teresa Johnson | 1063293 | 2015-11-06 17:50:53 +0000 | [diff] [blame] | 577 | |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 578 | if (Mover.move(std::move(SrcM), ValuesToLink.getArrayRef(), |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 579 | [this](GlobalValue &GV, IRMover::ValueAdder Add) { |
| 580 | addLazyFor(GV, Add); |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 581 | }, |
| 582 | ValIDToTempMDMap, false)) |
Teresa Johnson | 189b252 | 2015-11-06 17:50:48 +0000 | [diff] [blame] | 583 | return true; |
Rafael Espindola | caabe22 | 2015-12-10 14:19:35 +0000 | [diff] [blame] | 584 | for (auto &P : Internalize) { |
| 585 | GlobalValue *GV = DstM.getNamedValue(P.first()); |
| 586 | GV->setLinkage(GlobalValue::InternalLinkage); |
| 587 | } |
Teresa Johnson | 189b252 | 2015-11-06 17:50:48 +0000 | [diff] [blame] | 588 | |
Anton Korobeynikov | 2609888 | 2008-03-05 23:21:39 +0000 | [diff] [blame] | 589 | return false; |
| 590 | } |
Reid Spencer | 361e513 | 2004-11-12 20:37:43 +0000 | [diff] [blame] | 591 | |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 592 | Linker::Linker(Module &M) : Mover(M) {} |
Rafael Espindola | 3df61b7 | 2013-05-04 03:48:37 +0000 | [diff] [blame] | 593 | |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 594 | bool Linker::linkInModule(std::unique_ptr<Module> Src, unsigned Flags, |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 595 | DenseSet<const GlobalValue *> *GlobalsToImport, |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 596 | DenseMap<unsigned, MDNode *> *ValIDToTempMDMap) { |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 597 | ModuleLinker ModLinker(Mover, std::move(Src), Flags, GlobalsToImport, |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 598 | ValIDToTempMDMap); |
Teresa Johnson | bef5436 | 2015-12-18 19:28:59 +0000 | [diff] [blame] | 599 | return ModLinker.run(); |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 602 | bool Linker::linkInMetadata(std::unique_ptr<Module> Src, |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 603 | DenseMap<unsigned, MDNode *> *ValIDToTempMDMap) { |
| 604 | SetVector<GlobalValue *> ValuesToLink; |
| 605 | if (Mover.move( |
Rafael Espindola | 40358fb | 2016-02-16 18:50:12 +0000 | [diff] [blame] | 606 | std::move(Src), ValuesToLink.getArrayRef(), |
Teresa Johnson | e5a6191 | 2015-12-17 17:14:09 +0000 | [diff] [blame] | 607 | [this](GlobalValue &GV, IRMover::ValueAdder Add) { assert(false); }, |
| 608 | ValIDToTempMDMap, true)) |
| 609 | return true; |
| 610 | return false; |
| 611 | } |
| 612 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 613 | //===----------------------------------------------------------------------===// |
| 614 | // LinkModules entrypoint. |
| 615 | //===----------------------------------------------------------------------===// |
| 616 | |
Rafael Espindola | 18c8941 | 2014-10-27 02:35:46 +0000 | [diff] [blame] | 617 | /// This function links two modules together, with the resulting Dest module |
| 618 | /// modified to be the composite of the two input modules. If an error occurs, |
| 619 | /// true is returned and ErrorMsg (if not null) is set to indicate the problem. |
| 620 | /// Upon failure, the Dest module could be in a modified state, and shouldn't be |
| 621 | /// relied on to be consistent. |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 622 | bool Linker::linkModules(Module &Dest, std::unique_ptr<Module> Src, |
| 623 | unsigned Flags) { |
Rafael Espindola | 9d2bfc4 | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 624 | Linker L(Dest); |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 625 | return L.linkInModule(std::move(Src), Flags); |
Rafael Espindola | 4160f5d | 2014-10-27 23:02:10 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Bill Wendling | a3aeb98 | 2012-05-09 08:55:40 +0000 | [diff] [blame] | 628 | //===----------------------------------------------------------------------===// |
| 629 | // C API. |
| 630 | //===----------------------------------------------------------------------===// |
| 631 | |
Rafael Espindola | 434e956 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 632 | LLVMBool LLVMLinkModules2(LLVMModuleRef Dest, LLVMModuleRef Src) { |
| 633 | Module *D = unwrap(Dest); |
| 634 | std::unique_ptr<Module> M(unwrap(Src)); |
| 635 | return Linker::linkModules(*D, std::move(M)); |
| 636 | } |