blob: 7dfbf32b1ffa551908c96c66765738d7ffdfc03e [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- RTDyldObjectLinkingLayer.h - RTDyld-based jit linking ---*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// Contains the definition for an RTDyld-based, in-process object linking layer.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H
14#define LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H
15
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020019#include "llvm/ExecutionEngine/JITEventListener.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010020#include "llvm/ExecutionEngine/JITSymbol.h"
21#include "llvm/ExecutionEngine/Orc/Core.h"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010022#include "llvm/ExecutionEngine/Orc/Layer.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010023#include "llvm/ExecutionEngine/RuntimeDyld.h"
24#include "llvm/Object/ObjectFile.h"
25#include "llvm/Support/Error.h"
26#include <algorithm>
27#include <cassert>
28#include <functional>
29#include <list>
30#include <memory>
31#include <string>
32#include <utility>
33#include <vector>
34
35namespace llvm {
36namespace orc {
37
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020038class RTDyldObjectLinkingLayer : public ObjectLayer, private ResourceManager {
Andrew Scullcdfcccc2018-10-05 20:58:37 +010039public:
40 /// Functor for receiving object-loaded notifications.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020041 using NotifyLoadedFunction = std::function<void(
42 MaterializationResponsibility &R, const object::ObjectFile &Obj,
43 const RuntimeDyld::LoadedObjectInfo &)>;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010044
45 /// Functor for receiving finalization notifications.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020046 using NotifyEmittedFunction = std::function<void(
47 MaterializationResponsibility &R, std::unique_ptr<MemoryBuffer>)>;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010048
49 using GetMemoryManagerFunction =
Andrew Walbran16937d02019-10-22 13:54:20 +010050 std::function<std::unique_ptr<RuntimeDyld::MemoryManager>()>;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010051
52 /// Construct an ObjectLinkingLayer with the given NotifyLoaded,
Andrew Scull0372a572018-11-16 15:47:06 +000053 /// and NotifyEmitted functors.
Andrew Walbran3d2c1972020-04-07 12:24:26 +010054 RTDyldObjectLinkingLayer(ExecutionSession &ES,
55 GetMemoryManagerFunction GetMemoryManager);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010056
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020057 ~RTDyldObjectLinkingLayer();
58
Andrew Scullcdfcccc2018-10-05 20:58:37 +010059 /// Emit the object.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020060 void emit(std::unique_ptr<MaterializationResponsibility> R,
Andrew Scullcdfcccc2018-10-05 20:58:37 +010061 std::unique_ptr<MemoryBuffer> O) override;
62
Andrew Walbran3d2c1972020-04-07 12:24:26 +010063 /// Set the NotifyLoaded callback.
64 RTDyldObjectLinkingLayer &setNotifyLoaded(NotifyLoadedFunction NotifyLoaded) {
65 this->NotifyLoaded = std::move(NotifyLoaded);
66 return *this;
67 }
68
69 /// Set the NotifyEmitted callback.
70 RTDyldObjectLinkingLayer &
71 setNotifyEmitted(NotifyEmittedFunction NotifyEmitted) {
72 this->NotifyEmitted = std::move(NotifyEmitted);
73 return *this;
74 }
75
Andrew Scullcdfcccc2018-10-05 20:58:37 +010076 /// Set the 'ProcessAllSections' flag.
77 ///
78 /// If set to true, all sections in each object file will be allocated using
79 /// the memory manager, rather than just the sections required for execution.
80 ///
81 /// This is kludgy, and may be removed in the future.
Andrew Walbran16937d02019-10-22 13:54:20 +010082 RTDyldObjectLinkingLayer &setProcessAllSections(bool ProcessAllSections) {
Andrew Scullcdfcccc2018-10-05 20:58:37 +010083 this->ProcessAllSections = ProcessAllSections;
Andrew Scull0372a572018-11-16 15:47:06 +000084 return *this;
85 }
86
87 /// Instructs this RTDyldLinkingLayer2 instance to override the symbol flags
88 /// returned by RuntimeDyld for any given object file with the flags supplied
89 /// by the MaterializationResponsibility instance. This is a workaround to
90 /// support symbol visibility in COFF, which does not use the libObject's
91 /// SF_Exported flag. Use only when generating / adding COFF object files.
92 ///
93 /// FIXME: We should be able to remove this if/when COFF properly tracks
94 /// exported symbols.
Andrew Walbran16937d02019-10-22 13:54:20 +010095 RTDyldObjectLinkingLayer &
Andrew Scull0372a572018-11-16 15:47:06 +000096 setOverrideObjectFlagsWithResponsibilityFlags(bool OverrideObjectFlags) {
97 this->OverrideObjectFlags = OverrideObjectFlags;
98 return *this;
99 }
100
Andrew Walbran16937d02019-10-22 13:54:20 +0100101 /// If set, this RTDyldObjectLinkingLayer instance will claim responsibility
Andrew Scull0372a572018-11-16 15:47:06 +0000102 /// for any symbols provided by a given object file that were not already in
103 /// the MaterializationResponsibility instance. Setting this flag allows
104 /// higher-level program representations (e.g. LLVM IR) to be added based on
105 /// only a subset of the symbols they provide, without having to write
106 /// intervening layers to scan and add the additional symbols. This trades
107 /// diagnostic quality for convenience however: If all symbols are enumerated
108 /// up-front then clashes can be detected and reported early (and usually
109 /// deterministically). If this option is set, clashes for the additional
110 /// symbols may not be detected until late, and detection may depend on
111 /// the flow of control through JIT'd code. Use with care.
Andrew Walbran16937d02019-10-22 13:54:20 +0100112 RTDyldObjectLinkingLayer &
Andrew Scull0372a572018-11-16 15:47:06 +0000113 setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols) {
114 this->AutoClaimObjectSymbols = AutoClaimObjectSymbols;
115 return *this;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100116 }
117
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200118 /// Register a JITEventListener.
119 void registerJITEventListener(JITEventListener &L);
120
121 /// Unregister a JITEventListener.
122 void unregisterJITEventListener(JITEventListener &L);
123
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100124private:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200125 using MemoryManagerUP = std::unique_ptr<RuntimeDyld::MemoryManager>;
126
127 Error onObjLoad(MaterializationResponsibility &R,
128 const object::ObjectFile &Obj,
129 RuntimeDyld::MemoryManager &MemMgr,
130 RuntimeDyld::LoadedObjectInfo &LoadedObjInfo,
Andrew Scull0372a572018-11-16 15:47:06 +0000131 std::map<StringRef, JITEvaluatedSymbol> Resolved,
132 std::set<StringRef> &InternalSymbols);
133
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200134 void onObjEmit(MaterializationResponsibility &R,
135 object::OwningBinary<object::ObjectFile> O,
136 std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,
137 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
138 Error Err);
139
140 Error handleRemoveResources(ResourceKey K) override;
141 void handleTransferResources(ResourceKey DstKey, ResourceKey SrcKey) override;
Andrew Scull0372a572018-11-16 15:47:06 +0000142
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100143 mutable std::mutex RTDyldLayerMutex;
144 GetMemoryManagerFunction GetMemoryManager;
145 NotifyLoadedFunction NotifyLoaded;
Andrew Scull0372a572018-11-16 15:47:06 +0000146 NotifyEmittedFunction NotifyEmitted;
147 bool ProcessAllSections = false;
148 bool OverrideObjectFlags = false;
149 bool AutoClaimObjectSymbols = false;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200150 DenseMap<ResourceKey, std::vector<MemoryManagerUP>> MemMgrs;
151 std::vector<JITEventListener *> EventListeners;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100152};
153
154} // end namespace orc
155} // end namespace llvm
156
157#endif // LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H