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