LLVM  12.0.0git
RTDyldObjectLinkingLayer.cpp
Go to the documentation of this file.
1 //===-- RTDyldObjectLinkingLayer.cpp - RuntimeDyld backed ORC ObjectLayer -===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "llvm/Object/COFF.h"
11 
12 namespace {
13 
14 using namespace llvm;
15 using namespace llvm::orc;
16 
17 class JITDylibSearchOrderResolver : public JITSymbolResolver {
18 public:
19  JITDylibSearchOrderResolver(MaterializationResponsibility &MR) : MR(MR) {}
20 
21  void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) override {
22  auto &ES = MR.getTargetJITDylib().getExecutionSession();
23  SymbolLookupSet InternedSymbols;
24 
25  // Intern the requested symbols: lookup takes interned strings.
26  for (auto &S : Symbols)
27  InternedSymbols.add(ES.intern(S));
28 
29  // Build an OnResolve callback to unwrap the interned strings and pass them
30  // to the OnResolved callback.
31  auto OnResolvedWithUnwrap =
32  [OnResolved = std::move(OnResolved)](
33  Expected<SymbolMap> InternedResult) mutable {
34  if (!InternedResult) {
35  OnResolved(InternedResult.takeError());
36  return;
37  }
38 
40  for (auto &KV : *InternedResult)
41  Result[*KV.first] = std::move(KV.second);
42  OnResolved(Result);
43  };
44 
45  // Register dependencies for all symbols contained in this set.
46  auto RegisterDependencies = [&](const SymbolDependenceMap &Deps) {
47  MR.addDependenciesForAll(Deps);
48  };
49 
50  JITDylibSearchOrder LinkOrder;
51  MR.getTargetJITDylib().withLinkOrderDo(
52  [&](const JITDylibSearchOrder &LO) { LinkOrder = LO; });
53  ES.lookup(LookupKind::Static, LinkOrder, InternedSymbols,
54  SymbolState::Resolved, std::move(OnResolvedWithUnwrap),
55  RegisterDependencies);
56  }
57 
58  Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) override {
59  LookupSet Result;
60 
61  for (auto &KV : MR.getSymbols()) {
62  if (Symbols.count(*KV.first))
63  Result.insert(*KV.first);
64  }
65 
66  return Result;
67  }
68 
69 private:
71 };
72 
73 } // end anonymous namespace
74 
75 namespace llvm {
76 namespace orc {
77 
79  ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
80  : ObjectLayer(ES), GetMemoryManager(GetMemoryManager) {
81  ES.registerResourceManager(*this);
82 }
83 
85  assert(MemMgrs.empty() && "Layer destroyed with resources still attached");
86 }
87 
89  std::unique_ptr<MaterializationResponsibility> R,
90  std::unique_ptr<MemoryBuffer> O) {
91  assert(O && "Object must not be null");
92 
93  auto &ES = getExecutionSession();
94 
96 
97  if (!Obj) {
98  getExecutionSession().reportError(Obj.takeError());
99  R->failMaterialization();
100  return;
101  }
102 
103  // Collect the internal symbols from the object file: We will need to
104  // filter these later.
105  auto InternalSymbols = std::make_shared<std::set<StringRef>>();
106  {
107  for (auto &Sym : (*Obj)->symbols()) {
108 
109  // Skip file symbols.
110  if (auto SymType = Sym.getType()) {
111  if (*SymType == object::SymbolRef::ST_File)
112  continue;
113  } else {
114  ES.reportError(SymType.takeError());
115  R->failMaterialization();
116  return;
117  }
118 
119  Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
120  if (!SymFlagsOrErr) {
121  // TODO: Test this error.
122  ES.reportError(SymFlagsOrErr.takeError());
123  R->failMaterialization();
124  return;
125  }
126 
127  // Don't include symbols that aren't global.
128  if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global)) {
129  if (auto SymName = Sym.getName())
130  InternalSymbols->insert(*SymName);
131  else {
132  ES.reportError(SymName.takeError());
133  R->failMaterialization();
134  return;
135  }
136  }
137  }
138  }
139 
140  auto MemMgr = GetMemoryManager();
141  auto &MemMgrRef = *MemMgr;
142 
143  // Switch to shared ownership of MR so that it can be captured by both
144  // lambdas below.
145  std::shared_ptr<MaterializationResponsibility> SharedR(std::move(R));
146 
147  JITDylibSearchOrderResolver Resolver(*SharedR);
148 
151  MemMgrRef, Resolver, ProcessAllSections,
152  [this, SharedR, &MemMgrRef, InternalSymbols](
153  const object::ObjectFile &Obj,
154  RuntimeDyld::LoadedObjectInfo &LoadedObjInfo,
155  std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) {
156  return onObjLoad(*SharedR, Obj, MemMgrRef, LoadedObjInfo,
157  ResolvedSymbols, *InternalSymbols);
158  },
159  [this, SharedR, MemMgr = std::move(MemMgr)](
161  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
162  Error Err) mutable {
163  onObjEmit(*SharedR, std::move(Obj), std::move(MemMgr),
164  std::move(LoadedObjInfo), std::move(Err));
165  });
166 }
167 
169  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
170  assert(!llvm::is_contained(EventListeners, &L) &&
171  "Listener has already been registered");
172  EventListeners.push_back(&L);
173 }
174 
176  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
177  auto I = llvm::find(EventListeners, &L);
178  assert(I != EventListeners.end() && "Listener not registered");
179  EventListeners.erase(I);
180 }
181 
182 Error RTDyldObjectLinkingLayer::onObjLoad(
185  RuntimeDyld::LoadedObjectInfo &LoadedObjInfo,
186  std::map<StringRef, JITEvaluatedSymbol> Resolved,
187  std::set<StringRef> &InternalSymbols) {
188  SymbolFlagsMap ExtraSymbolsToClaim;
189  SymbolMap Symbols;
190 
191  // Hack to support COFF constant pool comdats introduced during compilation:
192  // (See http://llvm.org/PR40074)
193  if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(&Obj)) {
194  auto &ES = getExecutionSession();
195 
196  // For all resolved symbols that are not already in the responsibilty set:
197  // check whether the symbol is in a comdat section and if so mark it as
198  // weak.
199  for (auto &Sym : COFFObj->symbols()) {
200  // getFlags() on COFF symbols can't fail.
201  uint32_t SymFlags = cantFail(Sym.getFlags());
203  continue;
204  auto Name = Sym.getName();
205  if (!Name)
206  return Name.takeError();
207  auto I = Resolved.find(*Name);
208 
209  // Skip unresolved symbols, internal symbols, and symbols that are
210  // already in the responsibility set.
211  if (I == Resolved.end() || InternalSymbols.count(*Name) ||
212  R.getSymbols().count(ES.intern(*Name)))
213  continue;
214  auto Sec = Sym.getSection();
215  if (!Sec)
216  return Sec.takeError();
217  if (*Sec == COFFObj->section_end())
218  continue;
219  auto &COFFSec = *COFFObj->getCOFFSection(**Sec);
220  if (COFFSec.Characteristics & COFF::IMAGE_SCN_LNK_COMDAT)
221  I->second.setFlags(I->second.getFlags() | JITSymbolFlags::Weak);
222  }
223  }
224 
225  for (auto &KV : Resolved) {
226  // Scan the symbols and add them to the Symbols map for resolution.
227 
228  // We never claim internal symbols.
229  if (InternalSymbols.count(KV.first))
230  continue;
231 
232  auto InternedName = getExecutionSession().intern(KV.first);
233  auto Flags = KV.second.getFlags();
234 
235  // Override object flags and claim responsibility for symbols if
236  // requested.
237  if (OverrideObjectFlags || AutoClaimObjectSymbols) {
238  auto I = R.getSymbols().find(InternedName);
239 
240  if (OverrideObjectFlags && I != R.getSymbols().end())
241  Flags = I->second;
242  else if (AutoClaimObjectSymbols && I == R.getSymbols().end())
243  ExtraSymbolsToClaim[InternedName] = Flags;
244  }
245 
246  Symbols[InternedName] = JITEvaluatedSymbol(KV.second.getAddress(), Flags);
247  }
248 
249  if (!ExtraSymbolsToClaim.empty()) {
250  if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim))
251  return Err;
252 
253  // If we claimed responsibility for any weak symbols but were rejected then
254  // we need to remove them from the resolved set.
255  for (auto &KV : ExtraSymbolsToClaim)
256  if (KV.second.isWeak() && !R.getSymbols().count(KV.first))
257  Symbols.erase(KV.first);
258  }
259 
260  if (auto Err = R.notifyResolved(Symbols)) {
261  R.failMaterialization();
262  return Err;
263  }
264 
265  if (NotifyLoaded)
266  NotifyLoaded(R, Obj, LoadedObjInfo);
267 
268  return Error::success();
269 }
270 
271 void RTDyldObjectLinkingLayer::onObjEmit(
274  std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,
275  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo, Error Err) {
276  if (Err) {
278  R.failMaterialization();
279  return;
280  }
281 
282  if (auto Err = R.notifyEmitted()) {
284  R.failMaterialization();
285  return;
286  }
287 
288  std::unique_ptr<object::ObjectFile> Obj;
289  std::unique_ptr<MemoryBuffer> ObjBuffer;
290  std::tie(Obj, ObjBuffer) = O.takeBinary();
291 
292  // Run EventListener notifyLoaded callbacks.
293  {
294  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
295  for (auto *L : EventListeners)
296  L->notifyObjectLoaded(pointerToJITTargetAddress(MemMgr.get()), *Obj,
297  *LoadedObjInfo);
298  }
299 
300  if (NotifyEmitted)
301  NotifyEmitted(R, std::move(ObjBuffer));
302 
303  if (auto Err = R.withResourceKeyDo(
304  [&](ResourceKey K) { MemMgrs[K].push_back(std::move(MemMgr)); })) {
306  R.failMaterialization();
307  }
308 }
309 
310 Error RTDyldObjectLinkingLayer::handleRemoveResources(ResourceKey K) {
311 
312  std::vector<MemoryManagerUP> MemMgrsToRemove;
313 
315  auto I = MemMgrs.find(K);
316  if (I != MemMgrs.end()) {
317  std::swap(MemMgrsToRemove, I->second);
318  MemMgrs.erase(I);
319  }
320  });
321 
322  {
323  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
324  for (auto &MemMgr : MemMgrsToRemove) {
325  for (auto *L : EventListeners)
326  L->notifyFreeingObject(pointerToJITTargetAddress(MemMgr.get()));
327  MemMgr->deregisterEHFrames();
328  }
329  }
330 
331  return Error::success();
332 }
333 
334 void RTDyldObjectLinkingLayer::handleTransferResources(ResourceKey DstKey,
335  ResourceKey SrcKey) {
336  auto I = MemMgrs.find(SrcKey);
337  if (I != MemMgrs.end()) {
338  auto &SrcMemMgrs = I->second;
339  auto &DstMemMgrs = MemMgrs[DstKey];
340  DstMemMgrs.reserve(DstMemMgrs.size() + SrcMemMgrs.size());
341  for (auto &MemMgr : SrcMemMgrs)
342  DstMemMgrs.push_back(std::move(MemMgr));
343 
344  // Erase SrcKey entry using value rather than iterator I: I may have been
345  // invalidated when we looked up DstKey.
346  MemMgrs.erase(SrcKey);
347  }
348 }
349 
350 } // End namespace orc.
351 } // End namespace llvm.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:708
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:186
Queried, materialization begun.
Information about the loaded object.
Definition: RuntimeDyld.h:70
This class represents lattice values for constants.
Definition: AllocatorList.h:23
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
Definition: InlineInfo.cpp:108
static sys::Mutex Lock
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup.
Definition: Core.h:151
This class is the base class for all object file types.
Definition: ObjectFile.h:225
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O) override
Emit the object.
uintptr_t ResourceKey
Definition: Core.h:47
Error takeError()
Take ownership of the stored error.
Definition: Error.h:557
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition: Core.h:1247
RTDyldObjectLinkingLayer(ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
Construct an ObjectLinkingLayer with the given NotifyLoaded, and NotifyEmitted functors.
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:491
ExecutionSession & getExecutionSession()
Returns the execution session for this layer.
Definition: Layer.h:138
SymbolLookupSet & add(SymbolStringPtr Name, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Add an element to the set.
Definition: Core.h:221
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:150
std::vector< JITTargetAddress > LookupResult
bool erase(const KeyT &Val)
Definition: DenseMap.h:302
* if(!EatIfPresent(lltok::kw_thread_local)) return false
parseOptionalThreadLocal := /*empty
JITTargetAddress pointerToJITTargetAddress(T *Ptr)
Convert a pointer to a JITTargetAddress.
Definition: JITSymbol.h:69
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:1955
Symbol resolution interface.
Definition: JITSymbol.h:371
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:104
void registerJITEventListener(JITEventListener &L)
Register a JITEventListener.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:332
std::function< std::unique_ptr< RuntimeDyld::MemoryManager >()> GetMemoryManagerFunction
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1518
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition: Core.h:172
An ExecutionSession represents a running JIT program.
Definition: Core.h:1221
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:944
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:229
void reportError(Error Err)
Report a error for this execution session.
Definition: Core.h:1307
#define I(x, y, z)
Definition: MD5.cpp:59
iterator end()
Definition: DenseMap.h:83
void registerResourceManager(ResourceManager &RM)
Register the given ResourceManager with this ExecutionSession.
Definition: Core.cpp:1742
LLVM_NODISCARD bool empty() const
Definition: DenseMap.h:97
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void unregisterJITEventListener(JITEventListener &L)
Unregister a JITEventListener.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
void jitLinkForORC(object::OwningBinary< object::ObjectFile > O, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver, bool ProcessAllSections, unique_function< Error(const object::ObjectFile &Obj, RuntimeDyld::LoadedObjectInfo &, std::map< StringRef, JITEvaluatedSymbol >)> OnLoaded, unique_function< void(object::OwningBinary< object::ObjectFile >, std::unique_ptr< RuntimeDyld::LoadedObjectInfo >, Error)> OnEmitted)
Interface for Layers that accept object files.
Definition: Layer.h:132
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1556
decltype(auto) runSessionLocked(Func &&F)
Run the given lambda with the session mutex locked.
Definition: Core.h:1260
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1563