annotate include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h @ 128:c347d3398279 default tip

fix
author mir3636
date Wed, 06 Dec 2017 14:37:17 +0900
parents 803732b1fca8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===- RTDyldObjectLinkingLayer.h - RTDyld-based jit linking ---*- C++ -*-===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 // Contains the definition for an RTDyld-based, in-process object linking layer.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14 #ifndef LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15 #define LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 #include "llvm/ADT/STLExtras.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 #include "llvm/ADT/StringMap.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 #include "llvm/ADT/StringRef.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 #include "llvm/ExecutionEngine/JITSymbol.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 #include "llvm/ExecutionEngine/RuntimeDyld.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22 #include "llvm/Object/ObjectFile.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 #include "llvm/Support/Error.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 #include <algorithm>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 #include <cassert>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 #include <functional>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27 #include <list>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 #include <memory>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 #include <string>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 #include <utility>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 #include <vector>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 namespace llvm {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 namespace orc {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 class RTDyldObjectLinkingLayerBase {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 using ObjectPtr =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 std::shared_ptr<object::OwningBinary<object::ObjectFile>>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 protected:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 /// @brief Holds an object to be allocated/linked as a unit in the JIT.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 /// An instance of this class will be created for each object added
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 /// via JITObjectLayer::addObject. Deleting the instance (via
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 /// removeObject) frees its memory, removing all symbol definitions that
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 /// had been provided by this instance. Higher level layers are responsible
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 /// for taking any action required to handle the missing symbols.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 class LinkedObject {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 LinkedObject() = default;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 LinkedObject(const LinkedObject&) = delete;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 void operator=(const LinkedObject&) = delete;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 virtual ~LinkedObject() = default;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58 virtual void finalize() = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 virtual JITSymbol::GetAddressFtor
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 getSymbolMaterializer(std::string Name) = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 virtual void mapSectionAddress(const void *LocalAddress,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64 JITTargetAddress TargetAddr) const = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 JITSymbol getSymbol(StringRef Name, bool ExportedSymbolsOnly) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 auto SymEntry = SymbolTable.find(Name);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 if (SymEntry == SymbolTable.end())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 return nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70 if (!SymEntry->second.getFlags().isExported() && ExportedSymbolsOnly)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 return nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72 if (!Finalized)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73 return JITSymbol(getSymbolMaterializer(Name),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 SymEntry->second.getFlags());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 return JITSymbol(SymEntry->second);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78 protected:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79 StringMap<JITEvaluatedSymbol> SymbolTable;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80 bool Finalized = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83 using LinkedObjectListT = std::list<std::unique_ptr<LinkedObject>>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86 /// @brief Handle to a loaded object.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87 using ObjHandleT = LinkedObjectListT::iterator;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90 /// @brief Bare bones object linking layer.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 /// This class is intended to be used as the base layer for a JIT. It allows
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93 /// object files to be loaded into memory, linked, and the addresses of their
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
94 /// symbols queried. All objects added to this layer can see each other's
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
95 /// symbols.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
96 class RTDyldObjectLinkingLayer : public RTDyldObjectLinkingLayerBase {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
97 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
98
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
99 using RTDyldObjectLinkingLayerBase::ObjectPtr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
100
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
101 /// @brief Functor for receiving object-loaded notifications.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
102 using NotifyLoadedFtor =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
103 std::function<void(ObjHandleT, const ObjectPtr &Obj,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
104 const RuntimeDyld::LoadedObjectInfo &)>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
105
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
106 /// @brief Functor for receiving finalization notifications.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
107 using NotifyFinalizedFtor = std::function<void(ObjHandleT)>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
108
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
109 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
110
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
111
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
112 template <typename MemoryManagerPtrT, typename SymbolResolverPtrT,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
113 typename FinalizerFtor>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
114 class ConcreteLinkedObject : public LinkedObject {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
115 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
116 ConcreteLinkedObject(ObjectPtr Obj, MemoryManagerPtrT MemMgr,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
117 SymbolResolverPtrT Resolver,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
118 FinalizerFtor Finalizer,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
119 bool ProcessAllSections)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
120 : MemMgr(std::move(MemMgr)),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
121 PFC(llvm::make_unique<PreFinalizeContents>(std::move(Obj),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
122 std::move(Resolver),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
123 std::move(Finalizer),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
124 ProcessAllSections)) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
125 buildInitialSymbolTable(PFC->Obj);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
126 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
127
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
128 ~ConcreteLinkedObject() override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
129 MemMgr->deregisterEHFrames();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
130 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
131
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
132 void setHandle(ObjHandleT H) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
133 PFC->Handle = H;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
134 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
135
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
136 void finalize() override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
137 assert(PFC && "mapSectionAddress called on finalized LinkedObject");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
138
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
139 RuntimeDyld RTDyld(*MemMgr, *PFC->Resolver);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
140 RTDyld.setProcessAllSections(PFC->ProcessAllSections);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
141 PFC->RTDyld = &RTDyld;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
142
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
143 this->Finalized = true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
144 PFC->Finalizer(PFC->Handle, RTDyld, std::move(PFC->Obj),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
145 [&]() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
146 this->updateSymbolTable(RTDyld);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
147 });
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
148
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
149 // Release resources.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
150 PFC = nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
151 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
152
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
153 JITSymbol::GetAddressFtor getSymbolMaterializer(std::string Name) override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
154 return
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
155 [this, Name]() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
156 // The symbol may be materialized between the creation of this lambda
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
157 // and its execution, so we need to double check.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
158 if (!this->Finalized)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
159 this->finalize();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
160 return this->getSymbol(Name, false).getAddress();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
161 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
162 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
163
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
164 void mapSectionAddress(const void *LocalAddress,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
165 JITTargetAddress TargetAddr) const override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
166 assert(PFC && "mapSectionAddress called on finalized LinkedObject");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
167 assert(PFC->RTDyld && "mapSectionAddress called on raw LinkedObject");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
168 PFC->RTDyld->mapSectionAddress(LocalAddress, TargetAddr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
169 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
170
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
171 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
172
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
173 void buildInitialSymbolTable(const ObjectPtr &Obj) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
174 for (auto &Symbol : Obj->getBinary()->symbols()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
175 if (Symbol.getFlags() & object::SymbolRef::SF_Undefined)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
176 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
177 Expected<StringRef> SymbolName = Symbol.getName();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
178 // FIXME: Raise an error for bad symbols.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
179 if (!SymbolName) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
180 consumeError(SymbolName.takeError());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
181 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
182 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
183 auto Flags = JITSymbolFlags::fromObjectSymbol(Symbol);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
184 SymbolTable.insert(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
185 std::make_pair(*SymbolName, JITEvaluatedSymbol(0, Flags)));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
186 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
187 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
188
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
189 void updateSymbolTable(const RuntimeDyld &RTDyld) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
190 for (auto &SymEntry : SymbolTable)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
191 SymEntry.second = RTDyld.getSymbol(SymEntry.first());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
192 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
193
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
194 // Contains the information needed prior to finalization: the object files,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
195 // memory manager, resolver, and flags needed for RuntimeDyld.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
196 struct PreFinalizeContents {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
197 PreFinalizeContents(ObjectPtr Obj, SymbolResolverPtrT Resolver,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
198 FinalizerFtor Finalizer, bool ProcessAllSections)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
199 : Obj(std::move(Obj)), Resolver(std::move(Resolver)),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
200 Finalizer(std::move(Finalizer)),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
201 ProcessAllSections(ProcessAllSections) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
202
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
203 ObjectPtr Obj;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
204 SymbolResolverPtrT Resolver;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
205 FinalizerFtor Finalizer;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
206 bool ProcessAllSections;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
207 ObjHandleT Handle;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
208 RuntimeDyld *RTDyld;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
209 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
210
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
211 MemoryManagerPtrT MemMgr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
212 std::unique_ptr<PreFinalizeContents> PFC;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
213 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
214
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
215 template <typename MemoryManagerPtrT, typename SymbolResolverPtrT,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
216 typename FinalizerFtor>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
217 std::unique_ptr<
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
218 ConcreteLinkedObject<MemoryManagerPtrT, SymbolResolverPtrT, FinalizerFtor>>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
219 createLinkedObject(ObjectPtr Obj, MemoryManagerPtrT MemMgr,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
220 SymbolResolverPtrT Resolver,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
221 FinalizerFtor Finalizer,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
222 bool ProcessAllSections) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
223 using LOS = ConcreteLinkedObject<MemoryManagerPtrT, SymbolResolverPtrT,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
224 FinalizerFtor>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
225 return llvm::make_unique<LOS>(std::move(Obj), std::move(MemMgr),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
226 std::move(Resolver), std::move(Finalizer),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
227 ProcessAllSections);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
228 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
229
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
230 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
231
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
232 /// @brief Functor for creating memory managers.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
233 using MemoryManagerGetter =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
234 std::function<std::shared_ptr<RuntimeDyld::MemoryManager>()>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
235
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
236 /// @brief Construct an ObjectLinkingLayer with the given NotifyLoaded,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
237 /// and NotifyFinalized functors.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
238 RTDyldObjectLinkingLayer(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
239 MemoryManagerGetter GetMemMgr,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
240 NotifyLoadedFtor NotifyLoaded = NotifyLoadedFtor(),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
241 NotifyFinalizedFtor NotifyFinalized = NotifyFinalizedFtor())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
242 : GetMemMgr(GetMemMgr),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
243 NotifyLoaded(std::move(NotifyLoaded)),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
244 NotifyFinalized(std::move(NotifyFinalized)),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
245 ProcessAllSections(false) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
246
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
247 /// @brief Set the 'ProcessAllSections' flag.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
248 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
249 /// If set to true, all sections in each object file will be allocated using
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
250 /// the memory manager, rather than just the sections required for execution.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
251 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
252 /// This is kludgy, and may be removed in the future.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
253 void setProcessAllSections(bool ProcessAllSections) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
254 this->ProcessAllSections = ProcessAllSections;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
255 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
256
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
257 /// @brief Add an object to the JIT.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
258 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
259 /// @return A handle that can be used to refer to the loaded object (for
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
260 /// symbol searching, finalization, freeing memory, etc.).
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
261 Expected<ObjHandleT> addObject(ObjectPtr Obj,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
262 std::shared_ptr<JITSymbolResolver> Resolver) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
263 auto Finalizer = [&](ObjHandleT H, RuntimeDyld &RTDyld,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
264 const ObjectPtr &ObjToLoad,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
265 std::function<void()> LOSHandleLoad) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
266 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> Info =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
267 RTDyld.loadObject(*ObjToLoad->getBinary());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
268
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
269 LOSHandleLoad();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
270
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
271 if (this->NotifyLoaded)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
272 this->NotifyLoaded(H, ObjToLoad, *Info);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
273
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
274 RTDyld.finalizeWithMemoryManagerLocking();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
275
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
276 if (this->NotifyFinalized)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
277 this->NotifyFinalized(H);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
278 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
279
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
280 auto LO =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
281 createLinkedObject(std::move(Obj), GetMemMgr(),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
282 std::move(Resolver), std::move(Finalizer),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
283 ProcessAllSections);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
284 // LOS is an owning-ptr. Keep a non-owning one so that we can set the handle
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
285 // below.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
286 auto *LOPtr = LO.get();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
287
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
288 ObjHandleT Handle = LinkedObjList.insert(LinkedObjList.end(), std::move(LO));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
289 LOPtr->setHandle(Handle);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
290
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
291 return Handle;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
292 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
293
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
294 /// @brief Remove the object associated with handle H.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
295 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
296 /// All memory allocated for the object will be freed, and the sections and
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
297 /// symbols it provided will no longer be available. No attempt is made to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
298 /// re-emit the missing symbols, and any use of these symbols (directly or
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
299 /// indirectly) will result in undefined behavior. If dependence tracking is
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
300 /// required to detect or resolve such issues it should be added at a higher
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
301 /// layer.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
302 Error removeObject(ObjHandleT H) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
303 // How do we invalidate the symbols in H?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
304 LinkedObjList.erase(H);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
305 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
306 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
307
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
308 /// @brief Search for the given named symbol.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
309 /// @param Name The name of the symbol to search for.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
310 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
311 /// @return A handle for the given named symbol, if it exists.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
312 JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
313 for (auto I = LinkedObjList.begin(), E = LinkedObjList.end(); I != E;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
314 ++I)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
315 if (auto Symbol = findSymbolIn(I, Name, ExportedSymbolsOnly))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
316 return Symbol;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
317
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
318 return nullptr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
319 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
320
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
321 /// @brief Search for the given named symbol in the context of the loaded
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
322 /// object represented by the handle H.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
323 /// @param H The handle for the object to search in.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
324 /// @param Name The name of the symbol to search for.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
325 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
326 /// @return A handle for the given named symbol, if it is found in the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
327 /// given object.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
328 JITSymbol findSymbolIn(ObjHandleT H, StringRef Name,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
329 bool ExportedSymbolsOnly) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
330 return (*H)->getSymbol(Name, ExportedSymbolsOnly);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
331 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
332
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
333 /// @brief Map section addresses for the object associated with the handle H.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
334 void mapSectionAddress(ObjHandleT H, const void *LocalAddress,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
335 JITTargetAddress TargetAddr) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
336 (*H)->mapSectionAddress(LocalAddress, TargetAddr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
337 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
338
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
339 /// @brief Immediately emit and finalize the object represented by the given
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
340 /// handle.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
341 /// @param H Handle for object to emit/finalize.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
342 Error emitAndFinalize(ObjHandleT H) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
343 (*H)->finalize();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
344 return Error::success();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
345 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
346
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
347 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
348
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
349 LinkedObjectListT LinkedObjList;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
350 MemoryManagerGetter GetMemMgr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
351 NotifyLoadedFtor NotifyLoaded;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
352 NotifyFinalizedFtor NotifyFinalized;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
353 bool ProcessAllSections = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
354 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
355
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
356 } // end namespace orc
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
357 } // end namespace llvm
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
358
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
359 #endif // LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H