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