33#define DEBUG_TYPE "orc"
48template <
typename ELFT>
54 : Header(const_cast<typename ELFT::
Shdr *>(Header)) {}
62 typename ELFT::Shdr *Header;
64 bool isTextOrDataSection()
const;
67template <
typename ELFT>
70 if (isTextOrDataSection())
72 static_cast<typename ELFT::uint
>(Range.getStart().getValue());
75template <
typename ELFT>
77 switch (Header->sh_type) {
85template <
typename ELFT>
87 const char *
Name)
const {
90 const uint8_t *HeaderPtr =
reinterpret_cast<uint8_t *
>(Header);
91 if (HeaderPtr < Start || HeaderPtr +
sizeof(
typename ELFT::Shdr) > End)
92 return make_error<StringError>(
93 formatv(
"{0} section header at {1:x16} not within bounds of the "
94 "given debug object buffer [{2:x16} - {3:x16}]",
95 Name, &Header->sh_addr, Start, End),
97 if (Header->sh_offset + Header->sh_size > Buffer.
size())
98 return make_error<StringError>(
99 formatv(
"{0} section data [{1:x16} - {2:x16}] not within bounds of "
100 "the given debug object buffer [{3:x16} - {4:x16}]",
101 Name, Start + Header->sh_offset,
102 Start + Header->sh_offset + Header->sh_size, Start, End),
107template <
typename ELFT>
141 std::vector<FinalizedAlloc> Allocs;
142 Allocs.push_back(std::move(Alloc));
162 std::set<Requirement> Reqs;
170 assert(!Alloc &&
"Cannot finalize more than once");
176 SimpleSegAlloc->finalize(
177 [
this, DebugObjRange,
180 Alloc = std::move(*FA);
181 OnFinalize(DebugObjRange);
186 OnFinalize(SimpleSegAlloc.takeError());
207 template <
typename ELFT>
213 template <
typename ELFT>
218 static std::unique_ptr<WritableMemoryBuffer>
228 std::unique_ptr<WritableMemoryBuffer> Buffer;
233#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \
235#include "llvm/BinaryFormat/Dwarf.def"
236#undef HANDLE_DWARF_SECTION
243std::unique_ptr<WritableMemoryBuffer>
257template <
typename ELFT>
265 std::unique_ptr<ELFDebugObject> DebugObj(
266 new ELFDebugObject(CopyBuffer(Buffer, Err),
MemMgr,
JD, ES));
268 return std::move(Err);
275 uint16_t TargetMachineArch = ObjRef->getHeader().e_machine;
283 bool HasDwarfSection =
false;
284 for (
const SectionHeader &Header : *Sections) {
287 return Name.takeError();
295 auto Wrapped = std::make_unique<ELFDebugObjectSection<ELFT>>(&Header);
296 if (
Error Err = DebugObj->recordSection(*
Name, std::move(Wrapped)))
297 return std::move(Err);
300 if (!HasDwarfSection) {
301 LLVM_DEBUG(
dbgs() <<
"Aborting debug registration for LinkGraph \""
302 << DebugObj->Buffer->getBufferIdentifier()
303 <<
"\": input object contains no debug info\n");
307 return std::move(DebugObj);
313 unsigned char Class,
Endian;
339 dbgs() <<
"Section load-addresses in debug object for \""
341 for (
const auto &KV : Sections)
342 KV.second->dump(
dbgs(), KV.first());
366 DebugObjSection->setTargetMemoryRange(TargetMem);
369template <
typename ELFT>
374 auto ItInserted = Sections.try_emplace(
Name, std::move(
Section));
375 if (!ItInserted.second)
377 ", encountered duplicate section \"" +
378 Name +
"\" while building debug object",
384 auto It = Sections.find(
Name);
385 return It == Sections.end() ? nullptr : It->second.get();
394 switch (
G.getTargetTriple().getObjectFormat()) {
414 std::lock_guard<std::mutex> Lock(PendingObjsLock);
415 assert(PendingObjs.count(&MR) == 0 &&
416 "Cannot have more than one pending debug object per "
417 "MaterializationResponsibility");
421 if (*DebugObj !=
nullptr)
422 PendingObjs[&MR] = std::move(*DebugObj);
424 ES.reportError(DebugObj.takeError());
432 std::lock_guard<std::mutex> Lock(PendingObjsLock);
433 auto It = PendingObjs.find(&MR);
434 if (It == PendingObjs.end())
451 std::lock_guard<std::mutex> Lock(PendingObjsLock);
452 auto It = PendingObjs.find(&MR);
453 if (It == PendingObjs.end())
460 std::promise<MSVCPError> FinalizePromise;
461 std::future<MSVCPError> FinalizeErr = FinalizePromise.get_future();
463 It->second->finalizeAsync(
467 FinalizePromise.set_value(TargetMem.
takeError());
470 if (
Error Err =
Target->registerDebugObject(*TargetMem)) {
471 FinalizePromise.set_value(std::move(Err));
478 assert(PendingObjs.count(&MR) &&
"We still hold PendingObjsLock");
479 std::lock_guard<std::mutex> Lock(RegisteredObjsLock);
480 RegisteredObjs[K].push_back(std::move(PendingObjs[&MR]));
481 PendingObjs.erase(&MR);
485 return FinalizeErr.get();
490 std::lock_guard<std::mutex> Lock(PendingObjsLock);
491 PendingObjs.erase(&MR);
500 std::lock_guard<std::mutex> Lock(RegisteredObjsLock);
501 auto SrcIt = RegisteredObjs.find(SrcKey);
502 if (SrcIt != RegisteredObjs.end()) {
505 for (std::unique_ptr<DebugObject> &DebugObj : SrcIt->second)
506 RegisteredObjs[DstKey].push_back(std::move(DebugObj));
507 RegisteredObjs.erase(SrcIt);
515 std::lock_guard<std::mutex> Lock(RegisteredObjsLock);
516 RegisteredObjs.erase(Key);
This file defines the StringMap class.
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096), cl::Hidden)
Provides a library for accessing information about this process and other processes on the operating ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
size_t getBufferSize() const
StringRef getBufferIdentifier() const
const char * getBufferStart() const
StringRef getBuffer() const
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
StringRef - Represent a constant reference to a string, i.e.
const unsigned char * bytes_end() const
constexpr size_t size() const
size - Get the string size.
const unsigned char * bytes_begin() const
Target - Wrapper for Target specific information.
static std::unique_ptr< WritableMemoryBuffer > getNewUninitMemBuffer(size_t Size, const Twine &BufferName="", std::optional< Align > Alignment=std::nullopt)
Allocate a new MemoryBuffer of the specified size that is not initialized.
Holds context for a single jitLink invocation.
const JITLinkDylib * getJITLinkDylib() const
Return the JITLinkDylib that this link is targeting, if any.
virtual JITLinkMemoryManager & getMemoryManager()=0
Return the MemoryManager to be used for this link.
Represents a finalized allocation.
Represents an allocation which has not been finalized yet.
Manages allocations of JIT memory.
virtual void deallocate(std::vector< FinalizedAlloc > Allocs, OnDeallocatedFunction OnDeallocated)=0
Deallocate a list of allocation objects.
iterator_range< section_iterator > sections()
Represents a section address range via a pair of Block pointers to the first and last Blocks in the s...
Represents an object file section.
static void Create(JITLinkMemoryManager &MemMgr, const JITLinkDylib *JD, SegmentMap Segments, OnCreatedFunction OnCreated)
static Expected< ELFFile > create(StringRef Object)
Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override
void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey, ResourceKey SrcKey) override
Error notifyFailed(MaterializationResponsibility &MR) override
Error notifyEmitted(MaterializationResponsibility &MR) override
~DebugObjectManagerPlugin()
void notifyMaterializing(MaterializationResponsibility &MR, jitlink::LinkGraph &G, jitlink::JITLinkContext &Ctx, MemoryBufferRef InputObject) override
void modifyPassConfig(MaterializationResponsibility &MR, jitlink::LinkGraph &LG, jitlink::PassConfiguration &PassConfig) override
DebugObjectManagerPlugin(ExecutionSession &ES, std::unique_ptr< DebugObjectRegistrar > Target)
virtual void setTargetMemoryRange(SectionRange Range)=0
virtual void dump(raw_ostream &OS, StringRef Name)
virtual ~DebugObjectSection()=default
The plugin creates a debug object from when JITLink starts processing the corresponding LinkGraph.
JITLinkMemoryManager & MemMgr
bool has(Requirement Req) const
void finalizeAsync(FinalizeContinuation OnFinalize)
virtual Expected< SimpleSegmentAlloc > finalizeWorkingMemory()=0
std::function< void(Expected< ExecutorAddrRange >)> FinalizeContinuation
virtual void reportSectionTargetMemoryRange(StringRef Name, SectionRange TargetMem)
void set(Requirement Req)
DebugObject(JITLinkMemoryManager &MemMgr, const JITLinkDylib *JD, ExecutionSession &ES)
Error validateInBounds(StringRef Buffer, const char *Name) const
void setTargetMemoryRange(SectionRange Range) override
void dump(raw_ostream &OS, StringRef Name) override
ELFDebugObjectSection(const typename ELFT::Shdr *Header)
The current implementation of ELFDebugObject replicates the approach used in RuntimeDyld: It patches ...
Error recordSection(StringRef Name, std::unique_ptr< ELFDebugObjectSection< ELFT > > Section)
DebugObjectSection * getSection(StringRef Name)
Expected< SimpleSegmentAlloc > finalizeWorkingMemory() override
void reportSectionTargetMemoryRange(StringRef Name, SectionRange TargetMem) override
StringRef getBuffer() const
static Expected< std::unique_ptr< DebugObject > > Create(MemoryBufferRef Buffer, JITLinkContext &Ctx, ExecutionSession &ES)
An ExecutionSession represents a running JIT program.
void reportError(Error Err)
Report a error for this execution session.
Represents an address in the executor process.
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.
This class implements an extremely fast bulk output stream that can only output to a stream.
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
std::pair< unsigned char, unsigned char > getElfArchType(StringRef Object)
static const std::set< StringRef > DwarfSectionNames
static Expected< std::unique_ptr< DebugObject > > createDebugObjectFromBuffer(ExecutionSession &ES, LinkGraph &G, JITLinkContext &Ctx, MemoryBufferRef ObjBuffer)
Creates a debug object based on the input object file from ObjectLinkingLayerJITLinkContext.
static bool isDwarfSection(StringRef SectionName)
@ ReportFinalSectionLoadAddresses
This is an optimization pass for GlobalISel generic memory operations.
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
std::error_code make_error_code(BitcodeError E)
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
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.
Represents an address range in the exceutor process.