1336809Sdim//===-- RTDyldObjectLinkingLayer.cpp - RuntimeDyld backed ORC ObjectLayer -===//
2336809Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6336809Sdim//
7336809Sdim//===----------------------------------------------------------------------===//
8336809Sdim
9336809Sdim#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
10360784Sdim#include "llvm/Object/COFF.h"
11336809Sdim
12336809Sdimnamespace {
13336809Sdim
14336809Sdimusing namespace llvm;
15336809Sdimusing namespace llvm::orc;
16336809Sdim
17344779Sdimclass JITDylibSearchOrderResolver : public JITSymbolResolver {
18336809Sdimpublic:
19344779Sdim  JITDylibSearchOrderResolver(MaterializationResponsibility &MR) : MR(MR) {}
20336809Sdim
21344779Sdim  void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) {
22344779Sdim    auto &ES = MR.getTargetJITDylib().getExecutionSession();
23360784Sdim    SymbolLookupSet InternedSymbols;
24336809Sdim
25344779Sdim    // Intern the requested symbols: lookup takes interned strings.
26336809Sdim    for (auto &S : Symbols)
27360784Sdim      InternedSymbols.add(ES.intern(S));
28336809Sdim
29344779Sdim    // Build an OnResolve callback to unwrap the interned strings and pass them
30344779Sdim    // to the OnResolved callback.
31344779Sdim    auto OnResolvedWithUnwrap =
32360784Sdim        [OnResolved = std::move(OnResolved)](
33360784Sdim            Expected<SymbolMap> InternedResult) mutable {
34344779Sdim          if (!InternedResult) {
35344779Sdim            OnResolved(InternedResult.takeError());
36344779Sdim            return;
37344779Sdim          }
38344779Sdim
39344779Sdim          LookupResult Result;
40344779Sdim          for (auto &KV : *InternedResult)
41344779Sdim            Result[*KV.first] = std::move(KV.second);
42344779Sdim          OnResolved(Result);
43344779Sdim        };
44344779Sdim
45344779Sdim    // Register dependencies for all symbols contained in this set.
46336809Sdim    auto RegisterDependencies = [&](const SymbolDependenceMap &Deps) {
47336809Sdim      MR.addDependenciesForAll(Deps);
48336809Sdim    };
49336809Sdim
50360784Sdim    JITDylibSearchOrder SearchOrder;
51344779Sdim    MR.getTargetJITDylib().withSearchOrderDo(
52360784Sdim        [&](const JITDylibSearchOrder &JDs) { SearchOrder = JDs; });
53360784Sdim    ES.lookup(LookupKind::Static, SearchOrder, InternedSymbols,
54360784Sdim              SymbolState::Resolved, std::move(OnResolvedWithUnwrap),
55360784Sdim              RegisterDependencies);
56336809Sdim  }
57336809Sdim
58344779Sdim  Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) {
59344779Sdim    LookupSet Result;
60336809Sdim
61344779Sdim    for (auto &KV : MR.getSymbols()) {
62344779Sdim      if (Symbols.count(*KV.first))
63344779Sdim        Result.insert(*KV.first);
64344779Sdim    }
65336809Sdim
66336809Sdim    return Result;
67336809Sdim  }
68336809Sdim
69336809Sdimprivate:
70336809Sdim  MaterializationResponsibility &MR;
71336809Sdim};
72336809Sdim
73336809Sdim} // end anonymous namespace
74336809Sdim
75336809Sdimnamespace llvm {
76336809Sdimnamespace orc {
77336809Sdim
78344779SdimRTDyldObjectLinkingLayer::RTDyldObjectLinkingLayer(
79353358Sdim    ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
80353358Sdim    : ObjectLayer(ES), GetMemoryManager(GetMemoryManager) {}
81336809Sdim
82360784SdimRTDyldObjectLinkingLayer::~RTDyldObjectLinkingLayer() {
83360784Sdim  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
84360784Sdim  for (auto &MemMgr : MemMgrs)
85360784Sdim    MemMgr->deregisterEHFrames();
86360784Sdim}
87360784Sdim
88344779Sdimvoid RTDyldObjectLinkingLayer::emit(MaterializationResponsibility R,
89344779Sdim                                    std::unique_ptr<MemoryBuffer> O) {
90336809Sdim  assert(O && "Object must not be null");
91336809Sdim
92344779Sdim  // This method launches an asynchronous link step that will fulfill our
93344779Sdim  // materialization responsibility. We need to switch R to be heap
94344779Sdim  // allocated before that happens so it can live as long as the asynchronous
95344779Sdim  // link needs it to (i.e. it must be able to outlive this method).
96344779Sdim  auto SharedR = std::make_shared<MaterializationResponsibility>(std::move(R));
97344779Sdim
98336809Sdim  auto &ES = getExecutionSession();
99336809Sdim
100353358Sdim  // Create a MemoryBufferRef backed MemoryBuffer (i.e. shallow) copy of the
101353358Sdim  // the underlying buffer to pass into RuntimeDyld. This allows us to hold
102353358Sdim  // ownership of the real underlying buffer and return it to the user once
103353358Sdim  // the object has been emitted.
104353358Sdim  auto ObjBuffer = MemoryBuffer::getMemBuffer(O->getMemBufferRef(), false);
105336809Sdim
106353358Sdim  auto Obj = object::ObjectFile::createObjectFile(*ObjBuffer);
107353358Sdim
108344779Sdim  if (!Obj) {
109344779Sdim    getExecutionSession().reportError(Obj.takeError());
110344779Sdim    SharedR->failMaterialization();
111344779Sdim    return;
112336809Sdim  }
113336809Sdim
114344779Sdim  // Collect the internal symbols from the object file: We will need to
115344779Sdim  // filter these later.
116344779Sdim  auto InternalSymbols = std::make_shared<std::set<StringRef>>();
117336809Sdim  {
118344779Sdim    for (auto &Sym : (*Obj)->symbols()) {
119336809Sdim      if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global)) {
120336809Sdim        if (auto SymName = Sym.getName())
121344779Sdim          InternalSymbols->insert(*SymName);
122336809Sdim        else {
123336809Sdim          ES.reportError(SymName.takeError());
124336809Sdim          R.failMaterialization();
125336809Sdim          return;
126336809Sdim        }
127336809Sdim      }
128336809Sdim    }
129344779Sdim  }
130336809Sdim
131344779Sdim  auto K = R.getVModuleKey();
132344779Sdim  RuntimeDyld::MemoryManager *MemMgr = nullptr;
133336809Sdim
134344779Sdim  // Create a record a memory manager for this object.
135344779Sdim  {
136344779Sdim    auto Tmp = GetMemoryManager();
137344779Sdim    std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
138344779Sdim    MemMgrs.push_back(std::move(Tmp));
139344779Sdim    MemMgr = MemMgrs.back().get();
140336809Sdim  }
141336809Sdim
142344779Sdim  JITDylibSearchOrderResolver Resolver(*SharedR);
143336809Sdim
144344779Sdim  jitLinkForORC(
145344779Sdim      **Obj, std::move(O), *MemMgr, Resolver, ProcessAllSections,
146344779Sdim      [this, K, SharedR, &Obj, InternalSymbols](
147344779Sdim          std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
148344779Sdim          std::map<StringRef, JITEvaluatedSymbol> ResolvedSymbols) {
149344779Sdim        return onObjLoad(K, *SharedR, **Obj, std::move(LoadedObjInfo),
150344779Sdim                         ResolvedSymbols, *InternalSymbols);
151344779Sdim      },
152360784Sdim      [this, K, SharedR, O = std::move(O)](Error Err) mutable {
153360784Sdim        onObjEmit(K, std::move(O), *SharedR, std::move(Err));
154344779Sdim      });
155344779Sdim}
156336809Sdim
157344779SdimError RTDyldObjectLinkingLayer::onObjLoad(
158344779Sdim    VModuleKey K, MaterializationResponsibility &R, object::ObjectFile &Obj,
159344779Sdim    std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
160344779Sdim    std::map<StringRef, JITEvaluatedSymbol> Resolved,
161344779Sdim    std::set<StringRef> &InternalSymbols) {
162344779Sdim  SymbolFlagsMap ExtraSymbolsToClaim;
163344779Sdim  SymbolMap Symbols;
164360784Sdim
165360784Sdim  // Hack to support COFF constant pool comdats introduced during compilation:
166360784Sdim  // (See http://llvm.org/PR40074)
167360784Sdim  if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(&Obj)) {
168360784Sdim    auto &ES = getExecutionSession();
169360784Sdim
170360784Sdim    // For all resolved symbols that are not already in the responsibilty set:
171360784Sdim    // check whether the symbol is in a comdat section and if so mark it as
172360784Sdim    // weak.
173360784Sdim    for (auto &Sym : COFFObj->symbols()) {
174360784Sdim      if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined)
175360784Sdim        continue;
176360784Sdim      auto Name = Sym.getName();
177360784Sdim      if (!Name)
178360784Sdim        return Name.takeError();
179360784Sdim      auto I = Resolved.find(*Name);
180360784Sdim
181360784Sdim      // Skip unresolved symbols, internal symbols, and symbols that are
182360784Sdim      // already in the responsibility set.
183360784Sdim      if (I == Resolved.end() || InternalSymbols.count(*Name) ||
184360784Sdim          R.getSymbols().count(ES.intern(*Name)))
185360784Sdim        continue;
186360784Sdim      auto Sec = Sym.getSection();
187360784Sdim      if (!Sec)
188360784Sdim        return Sec.takeError();
189360784Sdim      if (*Sec == COFFObj->section_end())
190360784Sdim        continue;
191360784Sdim      auto &COFFSec = *COFFObj->getCOFFSection(**Sec);
192360784Sdim      if (COFFSec.Characteristics & COFF::IMAGE_SCN_LNK_COMDAT)
193360784Sdim        I->second.setFlags(I->second.getFlags() | JITSymbolFlags::Weak);
194360784Sdim    }
195360784Sdim  }
196360784Sdim
197344779Sdim  for (auto &KV : Resolved) {
198344779Sdim    // Scan the symbols and add them to the Symbols map for resolution.
199344779Sdim
200344779Sdim    // We never claim internal symbols.
201344779Sdim    if (InternalSymbols.count(KV.first))
202344779Sdim      continue;
203344779Sdim
204344779Sdim    auto InternedName = getExecutionSession().intern(KV.first);
205344779Sdim    auto Flags = KV.second.getFlags();
206344779Sdim
207344779Sdim    // Override object flags and claim responsibility for symbols if
208344779Sdim    // requested.
209344779Sdim    if (OverrideObjectFlags || AutoClaimObjectSymbols) {
210344779Sdim      auto I = R.getSymbols().find(InternedName);
211344779Sdim
212344779Sdim      if (OverrideObjectFlags && I != R.getSymbols().end())
213353358Sdim        Flags = I->second;
214344779Sdim      else if (AutoClaimObjectSymbols && I == R.getSymbols().end())
215344779Sdim        ExtraSymbolsToClaim[InternedName] = Flags;
216344779Sdim    }
217344779Sdim
218344779Sdim    Symbols[InternedName] = JITEvaluatedSymbol(KV.second.getAddress(), Flags);
219336809Sdim  }
220336809Sdim
221360784Sdim  if (!ExtraSymbolsToClaim.empty()) {
222344779Sdim    if (auto Err = R.defineMaterializing(ExtraSymbolsToClaim))
223344779Sdim      return Err;
224344779Sdim
225360784Sdim    // If we claimed responsibility for any weak symbols but were rejected then
226360784Sdim    // we need to remove them from the resolved set.
227360784Sdim    for (auto &KV : ExtraSymbolsToClaim)
228360784Sdim      if (KV.second.isWeak() && !R.getSymbols().count(KV.first))
229360784Sdim        Symbols.erase(KV.first);
230360784Sdim  }
231344779Sdim
232360784Sdim  if (auto Err = R.notifyResolved(Symbols)) {
233360784Sdim    R.failMaterialization();
234360784Sdim    return Err;
235360784Sdim  }
236360784Sdim
237344779Sdim  if (NotifyLoaded)
238344779Sdim    NotifyLoaded(K, Obj, *LoadedObjInfo);
239344779Sdim
240344779Sdim  return Error::success();
241344779Sdim}
242344779Sdim
243353358Sdimvoid RTDyldObjectLinkingLayer::onObjEmit(
244353358Sdim    VModuleKey K, std::unique_ptr<MemoryBuffer> ObjBuffer,
245353358Sdim    MaterializationResponsibility &R, Error Err) {
246344779Sdim  if (Err) {
247344779Sdim    getExecutionSession().reportError(std::move(Err));
248336809Sdim    R.failMaterialization();
249336809Sdim    return;
250336809Sdim  }
251336809Sdim
252360784Sdim  if (auto Err = R.notifyEmitted()) {
253360784Sdim    getExecutionSession().reportError(std::move(Err));
254360784Sdim    R.failMaterialization();
255360784Sdim    return;
256360784Sdim  }
257336809Sdim
258344779Sdim  if (NotifyEmitted)
259353358Sdim    NotifyEmitted(K, std::move(ObjBuffer));
260336809Sdim}
261336809Sdim
262353358SdimLegacyRTDyldObjectLinkingLayer::LegacyRTDyldObjectLinkingLayer(
263353358Sdim    ExecutionSession &ES, ResourcesGetter GetResources,
264353358Sdim    NotifyLoadedFtor NotifyLoaded, NotifyFinalizedFtor NotifyFinalized,
265353358Sdim    NotifyFreedFtor NotifyFreed)
266353358Sdim    : ES(ES), GetResources(std::move(GetResources)),
267353358Sdim      NotifyLoaded(std::move(NotifyLoaded)),
268353358Sdim      NotifyFinalized(std::move(NotifyFinalized)),
269353358Sdim      NotifyFreed(std::move(NotifyFreed)), ProcessAllSections(false) {}
270353358Sdim
271336809Sdim} // End namespace orc.
272336809Sdim} // End namespace llvm.
273