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