summaryrefslogtreecommitdiffstats
path: root/llvm/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp
blob: 61e8ee8ff1a8efeefcd403fc0cac51c4cc1d7d95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//===-- RTDyldObjectLinkingLayer.cpp - RuntimeDyld backed ORC ObjectLayer -===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"

namespace llvm {
namespace orc {

RTDyldObjectLinkingLayer2::RTDyldObjectLinkingLayer2(
    ExecutionSession &ES, ResourcesGetterFunction GetResources,
    NotifyLoadedFunction NotifyLoaded, NotifyFinalizedFunction NotifyFinalized)
    : ObjectLayer(ES), GetResources(std::move(GetResources)),
      NotifyLoaded(std::move(NotifyLoaded)),
      NotifyFinalized(std::move(NotifyFinalized)), ProcessAllSections(false) {}

void RTDyldObjectLinkingLayer2::emit(MaterializationResponsibility R,
                                     VModuleKey K,
                                     std::unique_ptr<MemoryBuffer> O) {
  assert(O && "Object must not be null");

  auto &ES = getExecutionSession();

  auto ObjFile = object::ObjectFile::createObjectFile(*O);
  if (!ObjFile) {
    getExecutionSession().reportError(ObjFile.takeError());
    R.failMaterialization();
  }

  auto Resources = GetResources(K);

  JITSymbolResolverAdapter ResolverAdapter(ES, *Resources.Resolver, &R);
  auto RTDyld =
      llvm::make_unique<RuntimeDyld>(*Resources.MemMgr, ResolverAdapter);
  RTDyld->setProcessAllSections(ProcessAllSections);

  {
    std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);

    assert(!ActiveRTDylds.count(K) &&
           "An active RTDyld already exists for this key?");
    ActiveRTDylds[K] = RTDyld.get();

    assert(!MemMgrs.count(K) &&
           "A memory manager already exists for this key?");
    MemMgrs[K] = Resources.MemMgr;
  }

  auto Info = RTDyld->loadObject(**ObjFile);

  {
    std::set<StringRef> InternalSymbols;
    for (auto &Sym : (*ObjFile)->symbols()) {
      if (!(Sym.getFlags() & object::BasicSymbolRef::SF_Global)) {
        if (auto SymName = Sym.getName())
          InternalSymbols.insert(*SymName);
        else {
          ES.reportError(SymName.takeError());
          R.failMaterialization();
          return;
        }
      }
    }

    SymbolMap Symbols;
    for (auto &KV : RTDyld->getSymbolTable())
      if (!InternalSymbols.count(KV.first))
        Symbols[ES.getSymbolStringPool().intern(KV.first)] = KV.second;

    R.resolve(Symbols);
  }

  if (NotifyLoaded)
    NotifyLoaded(K, **ObjFile, *Info);

  RTDyld->finalizeWithMemoryManagerLocking();

  {
    std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
    ActiveRTDylds.erase(K);
  }

  if (RTDyld->hasError()) {
    ES.reportError(make_error<StringError>(RTDyld->getErrorString(),
                                           inconvertibleErrorCode()));
    R.failMaterialization();
    return;
  }

  R.finalize();

  if (NotifyFinalized)
    NotifyFinalized(K);
}

void RTDyldObjectLinkingLayer2::mapSectionAddress(
    VModuleKey K, const void *LocalAddress, JITTargetAddress TargetAddr) const {
  std::lock_guard<std::mutex> Lock(RTDyldLayerMutex);
  auto ActiveRTDyldItr = ActiveRTDylds.find(K);

  assert(ActiveRTDyldItr != ActiveRTDylds.end() &&
         "No active RTDyld instance found for key");
  ActiveRTDyldItr->second->mapSectionAddress(LocalAddress, TargetAddr);
}

} // End namespace orc.
} // End namespace llvm.
OpenPOWER on IntegriCloud