35#define DEBUG_TYPE "orc"
50 : Name(Name), WorkingMem(
std::
move(Alloc)),
51 MemMgr(Ctx.getMemoryManager()), ES(ES) {}
54 assert(!FinalizeFuture.valid());
56 std::vector<FinalizedAlloc> Allocs;
57 Allocs.push_back(std::move(Alloc));
58 if (
Error Err = MemMgr.deallocate(std::move(Allocs)))
59 ES.reportError(std::move(Err));
65 return SegInfo.WorkingMem;
69 FinalizeFuture = FinalizePromise.get_future();
70 return std::move(WorkingMem);
78 assert(FinalizeFuture.valid() &&
79 "FinalizeFuture is not valid. Perhaps there is no pending target "
80 "memory transaction?");
81 return FinalizeFuture.get();
85 FinalizePromise.set_value(TargetMem);
89 FinalizePromise.set_value(std::move(Err));
93 if (FinalizeFuture.valid()) {
101 [ES = &this->ES](
Error Err) { ES->reportError(std::move(Err)); });
108 template <
typename ELFT>
117 std::promise<MSVCPExpected<ExecutorAddrRange>> FinalizePromise;
118 std::future<MSVCPExpected<ExecutorAddrRange>> FinalizeFuture;
123template <
typename ELFT>
125 using SectionHeader =
typename ELFT::Shdr;
137 for (
const SectionHeader &Header : *Sections) {
145 const_cast<SectionHeader &
>(Header).sh_addr =
146 static_cast<typename ELFT::uint
>(LoadAddress.
getValue());
150 dbgs() <<
"Section load-addresses in debug object for \"" << Name
152 for (
const SectionHeader &Header : *Sections) {
154 if (uint64_t Addr = Header.sh_addr) {
166 unsigned char Class, Endian;
192 bool RequireDebugSections,
194 : ES(ES), RequireDebugSections(RequireDebugSections) {
197 Err = ES.getExecutorProcessControl().getBootstrapSymbols(
204#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \
206#include "llvm/BinaryFormat/Dwarf.def"
207#undef HANDLE_DWARF_SECTION
219 if (
G.getTargetTriple().getObjectFormat() !=
Triple::ELF)
222 unsigned char Class, Endian;
225 return ES.reportError(
227 "Skipping debug object registration: Invalid arch "
228 "0x%02x in ELF LinkGraph %s",
229 Class,
G.getName().c_str()));
231 return ES.reportError(
233 "Skipping debug object registration: Invalid endian "
234 "0x%02x in ELF LinkGraph %s",
235 Endian,
G.getName().c_str()));
244 Ctx.getMemoryManager(), ES.getSymbolStringPool(), ES.getTargetTriple(),
245 Ctx.getJITLinkDylib(), {{MemProt::Read, Segment}});
247 ES.reportError(
Alloc.takeError());
251 std::lock_guard<std::mutex> Lock(PendingObjsLock);
252 assert(PendingObjs.count(&MR) == 0 &&
"One debug object per materialization");
253 PendingObjs[&MR] = std::make_unique<DebugObject>(
254 InputObj.getBufferIdentifier(), std::move(*
Alloc), Ctx, ES);
257 memcpy(Buffer.
data(), InputObj.getBufferStart(),
Size);
261ELFDebugObjectPlugin::getPendingDebugObj(MaterializationResponsibility &MR) {
262 std::lock_guard<std::mutex> Lock(PendingObjsLock);
263 auto It = PendingObjs.find(&MR);
264 return It == PendingObjs.end() ? nullptr : It->second.get();
270 if (!getPendingDebugObj(MR))
274 size_t SectionsPatched = 0;
275 bool HasDebugSections =
false;
277 assert(DebugObj &&
"Don't inject passes if we have no debug object");
283 [&
G, &SectionsPatched, &HasDebugSections](
StringRef Name) {
284 Section *S =
G.findSectionByName(Name);
291 SectionsPatched += 1;
293 HasDebugSections =
true;
299 if (!SectionsPatched) {
300 LLVM_DEBUG(
dbgs() <<
"Skipping debug registration for LinkGraph '"
301 <<
G.getName() <<
"': no debug info\n");
305 if (RequireDebugSections && !HasDebugSections) {
306 LLVM_DEBUG(
dbgs() <<
"Skipping debug registration for LinkGraph '"
307 <<
G.getName() <<
"': no debug info\n");
321 std::lock_guard<std::mutex> Lock(PendingObjsLock);
322 auto It = PendingObjs.find(&MR);
323 if (It == PendingObjs.end()) {
348 assert(DebugObj &&
"Don't inject passes if we have no debug object");
356 return R.takeError();
361 std::lock_guard<std::mutex> LockPending(PendingObjsLock);
362 std::lock_guard<std::mutex> LockRegistered(RegisteredObjsLock);
363 auto It = PendingObjs.find(&MR);
364 RegisteredObjs[K].push_back(std::move(It->second));
365 PendingObjs.erase(It);
375 G.allocActions().push_back(
376 {
cantFail(WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddrRange>>(
377 RegistrationAction, *R)),
384 std::lock_guard<std::mutex> Lock(PendingObjsLock);
385 auto It = PendingObjs.find(&MR);
386 It->second->releasePendingResources();
387 PendingObjs.erase(It);
396 std::lock_guard<std::mutex> Lock(RegisteredObjsLock);
397 auto SrcIt = RegisteredObjs.find(SrcKey);
398 if (SrcIt != RegisteredObjs.end()) {
401 for (std::unique_ptr<DebugObject> &DebugObj : SrcIt->second)
402 RegisteredObjs[DstKey].push_back(std::move(DebugObj));
403 RegisteredObjs.erase(SrcIt);
411 std::lock_guard<std::mutex> Lock(RegisteredObjsLock);
412 RegisteredObjs.erase(
Key);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isDwarfSection(const MCObjectFileInfo *FI, const MCSection *Section)
Provides a library for accessing information about this process and other processes on the operating ...
size_t size() const
Get the array size.
Helper for Errors used as out-parameters.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
size_t getBufferSize() const
StringRef getBuffer() const
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Represent a constant reference to a string, i.e.
Holds context for a single jitLink invocation.
Represents a finalized allocation.
Manages allocations of JIT memory.
Represents a section address range via a pair of Block pointers to the first and last Blocks in the s...
orc::ExecutorAddr getStart() const
Represents an object file section.
A utility class for making simple allocations using JITLinkMemoryManager.
static LLVM_ABI void Create(JITLinkMemoryManager &MemMgr, std::shared_ptr< orc::SymbolStringPool > SSP, Triple TT, const JITLinkDylib *JD, SegmentMap Segments, OnCreatedFunction OnCreated)
static Expected< ELFFile > create(StringRef Object)
MutableArrayRef< char > getBuffer()
Error visitSectionLoadAddresses(GetLoadAddressFn Callback)
void releasePendingResources()
Expected< ExecutorAddrRange > awaitTargetMem()
void reportTargetMem(ExecutorAddrRange TargetMem)
bool hasPendingTargetMem() const
SimpleSegmentAlloc collectTargetAlloc()
void failMaterialization(Error Err)
DebugObject(StringRef Name, SimpleSegmentAlloc Alloc, JITLinkContext &Ctx, ExecutionSession &ES)
llvm::unique_function< ExecutorAddr(StringRef)> GetLoadAddressFn
Error visitSections(GetLoadAddressFn Callback)
void trackFinalizedAlloc(FinalizedAlloc FA)
JITLinkMemoryManager::FinalizedAlloc FinalizedAlloc
Error notifyFailed(MaterializationResponsibility &MR) override
void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey, ResourceKey SrcKey) override
~ELFDebugObjectPlugin() override
void notifyMaterializing(MaterializationResponsibility &MR, jitlink::LinkGraph &G, jitlink::JITLinkContext &Ctx, MemoryBufferRef InputObj) override
Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override
void modifyPassConfig(MaterializationResponsibility &MR, jitlink::LinkGraph &LG, jitlink::PassConfiguration &PassConfig) override
ELFDebugObjectPlugin(ExecutionSession &ES, bool RequireDebugSections, Error &Err)
Create the plugin for the given session and set additional options.
An ExecutionSession represents a running JIT program.
Represents an address in the executor process.
uint64_t getValue() const
Represents a JIT'd dynamic library.
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Error withResourceKeyDo(Func &&F) const
Runs the given callback under the session lock, passing in the associated ResourceKey.
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
unique_function is a type-erasing functor similar to std::function.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::pair< unsigned char, unsigned char > getElfArchType(StringRef Object)
LLVM_ABI const char * RegisterJITLoaderGDBAllocActionName
static const std::set< StringRef > DwarfSectionNames
static bool isDwarfSection(StringRef SectionName)
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.
This struct is a compact representation of a valid (non-zero power of two) alignment.
An LinkGraph pass configuration, consisting of a list of pre-prune, post-prune, and post-fixup passes...
LinkGraphPassList PostAllocationPasses
Post-allocation passes.
LinkGraphPassList PostFixupPasses
Post-fixup passes.
Describes a segment to be allocated.
Represents an address range in the exceutor process.