LLVM 20.0.0git
DebugObjectManagerPlugin.cpp
Go to the documentation of this file.
1//===------- DebugObjectManagerPlugin.cpp - JITLink debug objects ---------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// FIXME: Update Plugin to poke the debug object into a new JITLink section,
10// rather than creating a new allocation.
11//
12//===----------------------------------------------------------------------===//
13
15
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
25#include "llvm/Support/Errc.h"
30
31#include <set>
32
33#define DEBUG_TYPE "orc"
34
35using namespace llvm::jitlink;
36using namespace llvm::object;
37
38namespace llvm {
39namespace orc {
40
42public:
44 virtual void dump(raw_ostream &OS, StringRef Name) {}
45 virtual ~DebugObjectSection() = default;
46};
47
48template <typename ELFT>
50public:
51 // BinaryFormat ELF is not meant as a mutable format. We can only make changes
52 // that don't invalidate the file structure.
53 ELFDebugObjectSection(const typename ELFT::Shdr *Header)
54 : Header(const_cast<typename ELFT::Shdr *>(Header)) {}
55
57 void dump(raw_ostream &OS, StringRef Name) override;
58
59 Error validateInBounds(StringRef Buffer, const char *Name) const;
60
61private:
62 typename ELFT::Shdr *Header;
63};
64
65template <typename ELFT>
67 // All recorded sections are candidates for load-address patching.
68 Header->sh_addr =
69 static_cast<typename ELFT::uint>(Range.getStart().getValue());
70}
71
72template <typename ELFT>
74 const char *Name) const {
75 const uint8_t *Start = Buffer.bytes_begin();
76 const uint8_t *End = Buffer.bytes_end();
77 const uint8_t *HeaderPtr = reinterpret_cast<uint8_t *>(Header);
78 if (HeaderPtr < Start || HeaderPtr + sizeof(typename ELFT::Shdr) > End)
79 return make_error<StringError>(
80 formatv("{0} section header at {1:x16} not within bounds of the "
81 "given debug object buffer [{2:x16} - {3:x16}]",
82 Name, &Header->sh_addr, Start, End),
84 if (Header->sh_offset + Header->sh_size > Buffer.size())
85 return make_error<StringError>(
86 formatv("{0} section data [{1:x16} - {2:x16}] not within bounds of "
87 "the given debug object buffer [{3:x16} - {4:x16}]",
88 Name, Start + Header->sh_offset,
89 Start + Header->sh_offset + Header->sh_size, Start, End),
91 return Error::success();
92}
93
94template <typename ELFT>
96 if (uint64_t Addr = Header->sh_addr) {
97 OS << formatv(" {0:x16} {1}\n", Addr, Name);
98 } else {
99 OS << formatv(" {0}\n", Name);
100 }
101}
102
104 // Request final target memory load-addresses for all sections.
106
107 // We found sections with debug information when processing the input object.
109};
110
111/// The plugin creates a debug object from when JITLink starts processing the
112/// corresponding LinkGraph. It provides access to the pass configuration of
113/// the LinkGraph and calls the finalization function, once the resulting link
114/// artifact was emitted.
115///
117public:
120 : MemMgr(MemMgr), JD(JD), ES(ES), Flags(DebugObjectFlags{}) {}
121
122 bool hasFlags(DebugObjectFlags F) const { return Flags & F; }
124 Flags = static_cast<DebugObjectFlags>(Flags | F);
125 }
127 Flags = static_cast<DebugObjectFlags>(Flags & ~F);
128 }
129
131
132 void finalizeAsync(FinalizeContinuation OnFinalize);
133
134 virtual ~DebugObject() {
135 if (Alloc) {
136 std::vector<FinalizedAlloc> Allocs;
137 Allocs.push_back(std::move(Alloc));
138 if (Error Err = MemMgr.deallocate(std::move(Allocs)))
139 ES.reportError(std::move(Err));
140 }
141 }
142
144 SectionRange TargetMem) {}
145
146protected:
149
151
153 const JITLinkDylib *JD = nullptr;
154
155private:
157 DebugObjectFlags Flags;
158 FinalizedAlloc Alloc;
159};
160
161// Finalize working memory and take ownership of the resulting allocation. Start
162// copying memory over to the target and pass on the result once we're done.
163// Ownership of the allocation remains with us for the rest of our lifetime.
165 assert(!Alloc && "Cannot finalize more than once");
166
167 if (auto SimpleSegAlloc = finalizeWorkingMemory()) {
168 auto ROSeg = SimpleSegAlloc->getSegInfo(MemProt::Read);
169 ExecutorAddrRange DebugObjRange(ROSeg.Addr, ROSeg.WorkingMem.size());
170 SimpleSegAlloc->finalize(
171 [this, DebugObjRange,
172 OnFinalize = std::move(OnFinalize)](Expected<FinalizedAlloc> FA) {
173 if (FA) {
174 Alloc = std::move(*FA);
175 OnFinalize(DebugObjRange);
176 } else
177 OnFinalize(FA.takeError());
178 });
179 } else
180 OnFinalize(SimpleSegAlloc.takeError());
181}
182
183/// The current implementation of ELFDebugObject replicates the approach used in
184/// RuntimeDyld: It patches executable and data section headers in the given
185/// object buffer with load-addresses of their corresponding sections in target
186/// memory.
187///
189public:
192
194 SectionRange TargetMem) override;
195
196 StringRef getBuffer() const { return Buffer->getMemBufferRef().getBuffer(); }
197
198protected:
200
201 template <typename ELFT>
203 std::unique_ptr<ELFDebugObjectSection<ELFT>> Section);
205
206private:
207 template <typename ELFT>
209 CreateArchType(MemoryBufferRef Buffer, JITLinkMemoryManager &MemMgr,
210 const JITLinkDylib *JD, ExecutionSession &ES);
211
212 static std::unique_ptr<WritableMemoryBuffer>
213 CopyBuffer(MemoryBufferRef Buffer, Error &Err);
214
215 ELFDebugObject(std::unique_ptr<WritableMemoryBuffer> Buffer,
218 : DebugObject(MemMgr, JD, ES), Buffer(std::move(Buffer)) {
220 }
221
222 std::unique_ptr<WritableMemoryBuffer> Buffer;
224};
225
226static const std::set<StringRef> DwarfSectionNames = {
227#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \
228 ELF_NAME,
229#include "llvm/BinaryFormat/Dwarf.def"
230#undef HANDLE_DWARF_SECTION
231};
232
234 return DwarfSectionNames.count(SectionName) == 1;
235}
236
237std::unique_ptr<WritableMemoryBuffer>
238ELFDebugObject::CopyBuffer(MemoryBufferRef Buffer, Error &Err) {
240 size_t Size = Buffer.getBufferSize();
243 memcpy(Copy->getBufferStart(), Buffer.getBufferStart(), Size);
244 return Copy;
245 }
246
248 return nullptr;
249}
250
251template <typename ELFT>
253ELFDebugObject::CreateArchType(MemoryBufferRef Buffer,
254 JITLinkMemoryManager &MemMgr,
255 const JITLinkDylib *JD, ExecutionSession &ES) {
256 using SectionHeader = typename ELFT::Shdr;
257
258 Error Err = Error::success();
259 std::unique_ptr<ELFDebugObject> DebugObj(
260 new ELFDebugObject(CopyBuffer(Buffer, Err), MemMgr, JD, ES));
261 if (Err)
262 return std::move(Err);
263
264 Expected<ELFFile<ELFT>> ObjRef = ELFFile<ELFT>::create(DebugObj->getBuffer());
265 if (!ObjRef)
266 return ObjRef.takeError();
267
268 Expected<ArrayRef<SectionHeader>> Sections = ObjRef->sections();
269 if (!Sections)
270 return Sections.takeError();
271
272 for (const SectionHeader &Header : *Sections) {
273 Expected<StringRef> Name = ObjRef->getSectionName(Header);
274 if (!Name)
275 return Name.takeError();
276 if (Name->empty())
277 continue;
278 if (isDwarfSection(*Name))
279 DebugObj->setFlags(HasDebugSections);
280
281 // Only record text and data sections (i.e. no bss, comments, rel, etc.)
282 if (Header.sh_type != ELF::SHT_PROGBITS &&
283 Header.sh_type != ELF::SHT_X86_64_UNWIND)
284 continue;
285 if (!(Header.sh_flags & ELF::SHF_ALLOC))
286 continue;
287
288 auto Wrapped = std::make_unique<ELFDebugObjectSection<ELFT>>(&Header);
289 if (Error Err = DebugObj->recordSection(*Name, std::move(Wrapped)))
290 return std::move(Err);
291 }
292
293 return std::move(DebugObj);
294}
295
298 ExecutionSession &ES) {
299 unsigned char Class, Endian;
300 std::tie(Class, Endian) = getElfArchType(Buffer.getBuffer());
301
302 if (Class == ELF::ELFCLASS32) {
304 return CreateArchType<ELF32LE>(Buffer, Ctx.getMemoryManager(),
305 Ctx.getJITLinkDylib(), ES);
307 return CreateArchType<ELF32BE>(Buffer, Ctx.getMemoryManager(),
308 Ctx.getJITLinkDylib(), ES);
309 return nullptr;
310 }
311 if (Class == ELF::ELFCLASS64) {
313 return CreateArchType<ELF64LE>(Buffer, Ctx.getMemoryManager(),
314 Ctx.getJITLinkDylib(), ES);
316 return CreateArchType<ELF64BE>(Buffer, Ctx.getMemoryManager(),
317 Ctx.getJITLinkDylib(), ES);
318 return nullptr;
319 }
320 return nullptr;
321}
322
324 LLVM_DEBUG({
325 dbgs() << "Section load-addresses in debug object for \""
326 << Buffer->getBufferIdentifier() << "\":\n";
327 for (const auto &KV : Sections)
328 KV.second->dump(dbgs(), KV.first());
329 });
330
331 // TODO: This works, but what actual alignment requirements do we have?
333 size_t Size = Buffer->getBufferSize();
334
335 // Allocate working memory for debug object in read-only segment.
336 auto Alloc = SimpleSegmentAlloc::Create(
338 if (!Alloc)
339 return Alloc;
340
341 // Initialize working memory with a copy of our object buffer.
342 auto SegInfo = Alloc->getSegInfo(MemProt::Read);
343 memcpy(SegInfo.WorkingMem.data(), Buffer->getBufferStart(), Size);
344 Buffer.reset();
345
346 return Alloc;
347}
348
350 SectionRange TargetMem) {
351 if (auto *DebugObjSection = getSection(Name))
352 DebugObjSection->setTargetMemoryRange(TargetMem);
353}
354
355template <typename ELFT>
358 if (Error Err = Section->validateInBounds(this->getBuffer(), Name.data()))
359 return Err;
360 bool Inserted = Sections.try_emplace(Name, std::move(Section)).second;
361 if (!Inserted)
362 LLVM_DEBUG(dbgs() << "Skipping debug registration for section '" << Name
363 << "' in object " << Buffer->getBufferIdentifier()
364 << " (duplicate name)\n");
365 return Error::success();
366}
367
369 auto It = Sections.find(Name);
370 return It == Sections.end() ? nullptr : It->second.get();
371}
372
373/// Creates a debug object based on the input object file from
374/// ObjectLinkingLayerJITLinkContext.
375///
378 JITLinkContext &Ctx, MemoryBufferRef ObjBuffer) {
379 switch (G.getTargetTriple().getObjectFormat()) {
380 case Triple::ELF:
381 return ELFDebugObject::Create(ObjBuffer, Ctx, ES);
382
383 default:
384 // TODO: Once we add support for other formats, we might want to split this
385 // into multiple files.
386 return nullptr;
387 }
388}
389
391 ExecutionSession &ES, std::unique_ptr<DebugObjectRegistrar> Target,
392 bool RequireDebugSections, bool AutoRegisterCode)
393 : ES(ES), Target(std::move(Target)),
394 RequireDebugSections(RequireDebugSections),
395 AutoRegisterCode(AutoRegisterCode) {}
396
398 ExecutionSession &ES, std::unique_ptr<DebugObjectRegistrar> Target)
400
402
405 MemoryBufferRef ObjBuffer) {
406 std::lock_guard<std::mutex> Lock(PendingObjsLock);
407 assert(PendingObjs.count(&MR) == 0 &&
408 "Cannot have more than one pending debug object per "
409 "MaterializationResponsibility");
410
411 if (auto DebugObj = createDebugObjectFromBuffer(ES, G, Ctx, ObjBuffer)) {
412 // Not all link artifacts allow debugging.
413 if (*DebugObj == nullptr)
414 return;
415 if (RequireDebugSections && !(**DebugObj).hasFlags(HasDebugSections)) {
416 LLVM_DEBUG(dbgs() << "Skipping debug registration for LinkGraph '"
417 << G.getName() << "': no debug info\n");
418 return;
419 }
420 PendingObjs[&MR] = std::move(*DebugObj);
421 } else {
422 ES.reportError(DebugObj.takeError());
423 }
424}
425
428 PassConfiguration &PassConfig) {
429 // Not all link artifacts have associated debug objects.
430 std::lock_guard<std::mutex> Lock(PendingObjsLock);
431 auto It = PendingObjs.find(&MR);
432 if (It == PendingObjs.end())
433 return;
434
435 DebugObject &DebugObj = *It->second;
437 PassConfig.PostAllocationPasses.push_back(
438 [&DebugObj](LinkGraph &Graph) -> Error {
439 for (const Section &GraphSection : Graph.sections())
440 DebugObj.reportSectionTargetMemoryRange(GraphSection.getName(),
441 SectionRange(GraphSection));
442 return Error::success();
443 });
444 }
445}
446
449 std::lock_guard<std::mutex> Lock(PendingObjsLock);
450 auto It = PendingObjs.find(&MR);
451 if (It == PendingObjs.end())
452 return Error::success();
453
454 // During finalization the debug object is registered with the target.
455 // Materialization must wait for this process to finish. Otherwise we might
456 // start running code before the debugger processed the corresponding debug
457 // info.
458 std::promise<MSVCPError> FinalizePromise;
459 std::future<MSVCPError> FinalizeErr = FinalizePromise.get_future();
460
461 It->second->finalizeAsync(
462 [this, &FinalizePromise, &MR](Expected<ExecutorAddrRange> TargetMem) {
463 // Any failure here will fail materialization.
464 if (!TargetMem) {
465 FinalizePromise.set_value(TargetMem.takeError());
466 return;
467 }
468 if (Error Err =
469 Target->registerDebugObject(*TargetMem, AutoRegisterCode)) {
470 FinalizePromise.set_value(std::move(Err));
471 return;
472 }
473
474 // Once our tracking info is updated, notifyEmitted() can return and
475 // finish materialization.
476 FinalizePromise.set_value(MR.withResourceKeyDo([&](ResourceKey K) {
477 assert(PendingObjs.count(&MR) && "We still hold PendingObjsLock");
478 std::lock_guard<std::mutex> Lock(RegisteredObjsLock);
479 RegisteredObjs[K].push_back(std::move(PendingObjs[&MR]));
480 PendingObjs.erase(&MR);
481 }));
482 });
483
484 return FinalizeErr.get();
485}
486
489 std::lock_guard<std::mutex> Lock(PendingObjsLock);
490 PendingObjs.erase(&MR);
491 return Error::success();
492}
493
495 ResourceKey DstKey,
496 ResourceKey SrcKey) {
497 // Debug objects are stored by ResourceKey only after registration.
498 // Thus, pending objects don't need to be updated here.
499 std::lock_guard<std::mutex> Lock(RegisteredObjsLock);
500 auto SrcIt = RegisteredObjs.find(SrcKey);
501 if (SrcIt != RegisteredObjs.end()) {
502 // Resources from distinct MaterializationResponsibilitys can get merged
503 // after emission, so we can have multiple debug objects per resource key.
504 for (std::unique_ptr<DebugObject> &DebugObj : SrcIt->second)
505 RegisteredObjs[DstKey].push_back(std::move(DebugObj));
506 RegisteredObjs.erase(SrcIt);
507 }
508}
509
511 ResourceKey Key) {
512 // Removing the resource for a pending object fails materialization, so they
513 // get cleaned up in the notifyFailed() handler.
514 std::lock_guard<std::mutex> Lock(RegisteredObjsLock);
515 RegisteredObjs.erase(Key);
516
517 // TODO: Implement unregister notifications.
518 return Error::success();
519}
520
521} // namespace orc
522} // namespace llvm
This file defines the StringMap class.
basic Basic Alias true
#define LLVM_DEBUG(X)
Definition: Debug.h:101
Elf_Shdr Shdr
uint64_t Addr
std::string Name
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
#define _
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)
#define F(x, y, z)
Definition: MD5.cpp:55
#define G(x, y, z)
Definition: MD5.cpp:56
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
Provides a library for accessing information about this process and other processes on the operating ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
endianness Endian
raw_pwrite_stream & OS
Helper for Errors used as out-parameters.
Definition: Error.h:1130
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
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",...
Definition: StringMap.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
const unsigned char * bytes_end() const
Definition: StringRef.h:118
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
const unsigned char * bytes_begin() const
Definition: StringRef.h:115
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.
static Expected< ELFFile > create(StringRef Object)
Definition: ELF.h:887
Creates and manages DebugObjects for JITLink artifacts.
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
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.
bool hasFlags(DebugObjectFlags F) const
void finalizeAsync(FinalizeContinuation OnFinalize)
virtual Expected< SimpleSegmentAlloc > finalizeWorkingMemory()=0
std::function< void(Expected< ExecutorAddrRange >)> FinalizeContinuation
void clearFlags(DebugObjectFlags F)
virtual void reportSectionTargetMemoryRange(StringRef Name, SectionRange TargetMem)
void setFlags(DebugObjectFlags F)
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
static Expected< std::unique_ptr< DebugObject > > Create(MemoryBufferRef Buffer, JITLinkContext &Ctx, ExecutionSession &ES)
An ExecutionSession represents a running JIT program.
Definition: Core.h:1431
void reportError(Error Err)
Report a error for this execution session.
Definition: Core.h:1566
Represents a JIT'd dynamic library.
Definition: Core.h:989
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:555
Error withResourceKeyDo(Func &&F) const
Runs the given callback under the session lock, passing in the associated ResourceKey.
Definition: Core.h:571
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
Definition: Process.h:61
@ ELFDATA2MSB
Definition: ELF.h:337
@ ELFDATA2LSB
Definition: ELF.h:336
@ SHT_PROGBITS
Definition: ELF.h:1089
@ SHT_X86_64_UNWIND
Definition: ELF.h:1160
@ ELFCLASS64
Definition: ELF.h:330
@ ELFCLASS32
Definition: ELF.h:329
@ SHF_ALLOC
Definition: ELF.h:1186
std::pair< unsigned char, unsigned char > getElfArchType(StringRef Object)
Definition: ELF.h:79
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)
uintptr_t ResourceKey
Definition: Core.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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...
Definition: Error.cpp:98
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:111
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Represents an address range in the exceutor process.