LLVM 24.0.0git
ELFDebugObjectPlugin.cpp
Go to the documentation of this file.
1//===--------- ELFDebugObjectPlugin.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/StringRef.h"
28#include "llvm/Object/Error.h"
29#include "llvm/Support/Error.h"
33
34#include <set>
35
36#define DEBUG_TYPE "orc"
37
38using namespace llvm::jitlink;
39using namespace llvm::object;
40
41namespace llvm {
42namespace orc {
43
44// Helper class to emit and fixup an individual debug object
46public:
48
51 : Name(Name), WorkingMem(std::move(Alloc)),
52 MemMgr(Ctx.getMemoryManager()), ES(ES) {}
53
55 assert(!FinalizeFuture.valid());
56 if (Alloc) {
57 std::vector<FinalizedAlloc> Allocs;
58 Allocs.push_back(std::move(Alloc));
59 if (Error Err = MemMgr.deallocate(std::move(Allocs)))
60 ES.reportError(std::move(Err));
61 }
62 }
63
65 auto SegInfo = WorkingMem.getSegInfo(MemProt::Read);
66 return SegInfo.WorkingMem;
67 }
68
70 FinalizeFuture = FinalizePromise.get_future();
71 return std::move(WorkingMem);
72 }
73
74 void trackFinalizedAlloc(FinalizedAlloc FA) { Alloc = std::move(FA); }
75
76 bool hasPendingTargetMem() const { return FinalizeFuture.valid(); }
77
79 assert(FinalizeFuture.valid() &&
80 "FinalizeFuture is not valid. Perhaps there is no pending target "
81 "memory transaction?");
82 return FinalizeFuture.get();
83 }
84
86 FinalizePromise.set_value(TargetMem);
87 }
88
90 FinalizePromise.set_value(std::move(Err));
91 }
92
94 if (FinalizeFuture.valid()) {
95 // Error before step 4: Finalization error was not reported
96 Expected<ExecutorAddrRange> TargetMem = FinalizeFuture.get();
97 if (!TargetMem)
98 ES.reportError(TargetMem.takeError());
99 } else {
100 // Error before step 3: WorkingMem was not collected
101 WorkingMem.abandon(
102 [ES = &this->ES](Error Err) { ES->reportError(std::move(Err)); });
103 }
104 }
105
108
109 template <typename ELFT>
111
112private:
113 std::string Name;
114 SimpleSegmentAlloc WorkingMem;
115 JITLinkMemoryManager &MemMgr;
117
118 std::promise<MSVCPExpected<ExecutorAddrRange>> FinalizePromise;
119 std::future<MSVCPExpected<ExecutorAddrRange>> FinalizeFuture;
120
121 FinalizedAlloc Alloc;
122};
123
124template <typename ELFT>
126 using SectionHeader = typename ELFT::Shdr;
127
129 StringRef BufferRef(Buffer.data(), Buffer.size());
131 if (!ObjRef)
132 return ObjRef.takeError();
133
134 Expected<ArrayRef<SectionHeader>> Sections = ObjRef->sections();
135 if (!Sections)
136 return Sections.takeError();
137
138 for (const SectionHeader &Header : *Sections) {
139 Expected<StringRef> Name = ObjRef->getSectionName(Header);
140 if (!Name)
141 return Name.takeError();
142 if (Name->empty())
143 continue;
144 ExecutorAddr LoadAddress = Callback(*Name);
145 if (LoadAddress)
146 const_cast<SectionHeader &>(Header).sh_addr =
147 static_cast<typename ELFT::uint>(LoadAddress.getValue());
148 }
149
150 LLVM_DEBUG({
151 dbgs() << "Section load-addresses in debug object for \"" << Name
152 << "\":\n";
153 for (const SectionHeader &Header : *Sections) {
154 StringRef Name = cantFail(ObjRef->getSectionName(Header));
155 if (uint64_t Addr = Header.sh_addr) {
156 dbgs() << formatv(" {0:x16} {1}\n", Addr, Name);
157 } else {
158 dbgs() << formatv(" {0}\n", Name);
159 }
160 }
161 });
162
163 return Error::success();
164}
165
167 unsigned char Class, Endian;
169 std::tie(Class, Endian) = getElfArchType(StringRef(Buf.data(), Buf.size()));
170
171 switch (Class) {
172 case ELF::ELFCLASS32:
173 if (Endian == ELF::ELFDATA2LSB)
174 return visitSectionLoadAddresses<ELF32LE>(std::move(Callback));
175 if (Endian == ELF::ELFDATA2MSB)
176 return visitSectionLoadAddresses<ELF32BE>(std::move(Callback));
177 break;
178
179 case ELF::ELFCLASS64:
180 if (Endian == ELF::ELFDATA2LSB)
181 return visitSectionLoadAddresses<ELF64LE>(std::move(Callback));
182 if (Endian == ELF::ELFDATA2MSB)
183 return visitSectionLoadAddresses<ELF64BE>(std::move(Callback));
184 break;
185
186 default:
187 break;
188 }
189 llvm_unreachable("Checked class and endian in notifyMaterializing()");
190}
191
193 bool RequireDebugSections,
194 Error &Err)
195 : ES(ES), RequireDebugSections(RequireDebugSections) {
196 // Pass bootstrap symbol for registration function to enable debugging
198 Err = lookupAndApply(ES.getBootstrapJITDylib(),
199 {recordAddr(rt::RegisterJITLoaderGDBAllocActionName,
200 &RegistrationAction)});
201}
202
204
205static const std::set<StringRef> DwarfSectionNames = {
206#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \
207 ELF_NAME,
208#include "llvm/BinaryFormat/Dwarf.def"
209#undef HANDLE_DWARF_SECTION
210};
211
213 return DwarfSectionNames.count(SectionName) == 1;
214}
215
218 MemoryBufferRef InputObj) {
219 if (InputObj.getBufferSize() == 0)
220 return;
221 if (G.getTargetTriple().getObjectFormat() != Triple::ELF)
222 return;
223
224 unsigned char Class, Endian;
225 std::tie(Class, Endian) = getElfArchType(InputObj.getBuffer());
226 if (Class != ELF::ELFCLASS64 && Class != ELF::ELFCLASS32)
227 return ES.reportError(
229 "Skipping debug object registration: Invalid arch "
230 "0x%02x in ELF LinkGraph %s",
231 Class, G.getName().c_str()));
232 if (Endian != ELF::ELFDATA2LSB && Endian != ELF::ELFDATA2MSB)
233 return ES.reportError(
235 "Skipping debug object registration: Invalid endian "
236 "0x%02x in ELF LinkGraph %s",
237 Endian, G.getName().c_str()));
238
239 // Step 1: We copy the raw input object into the working memory of a
240 // single-segment read-only allocation
241 size_t Size = InputObj.getBufferSize();
242 auto Alignment = sys::Process::getPageSizeEstimate();
243 SimpleSegmentAlloc::Segment Segment{Size, Align(Alignment)};
244
246 Ctx.getMemoryManager(), ES.getSymbolStringPool(), ES.getTargetTriple(),
247 Ctx.getJITLinkDylib(), {{MemProt::Read, Segment}});
248 if (!Alloc) {
249 ES.reportError(Alloc.takeError());
250 return;
251 }
252
253 std::lock_guard<std::mutex> Lock(PendingObjsLock);
254 assert(PendingObjs.count(&MR) == 0 && "One debug object per materialization");
255 PendingObjs[&MR] = std::make_unique<DebugObject>(
256 InputObj.getBufferIdentifier(), std::move(*Alloc), Ctx, ES);
257
258 MutableArrayRef<char> Buffer = PendingObjs[&MR]->getBuffer();
259 memcpy(Buffer.data(), InputObj.getBufferStart(), Size);
260}
261
262DebugObject *
263ELFDebugObjectPlugin::getPendingDebugObj(MaterializationResponsibility &MR) {
264 std::lock_guard<std::mutex> Lock(PendingObjsLock);
265 auto It = PendingObjs.find(&MR);
266 return It == PendingObjs.end() ? nullptr : It->second.get();
267}
268
270 LinkGraph &G,
271 PassConfiguration &PassConfig) {
272 if (!getPendingDebugObj(MR))
273 return;
274
275 PassConfig.PostAllocationPasses.push_back([this, &MR](LinkGraph &G) -> Error {
276 size_t SectionsPatched = 0;
277 bool HasDebugSections = false;
278 DebugObject *DebugObj = getPendingDebugObj(MR);
279 assert(DebugObj && "Don't inject passes if we have no debug object");
280
281 // Step 2: Once the target memory layout is ready, we write the
282 // addresses of the LinkGraph sections into the load-address fields of the
283 // section headers in our debug object allocation
284 Error Err = DebugObj->visitSections(
285 [&G, &SectionsPatched, &HasDebugSections](StringRef Name) {
286 Section *S = G.findSectionByName(Name);
287 if (!S) {
288 // The section may have been merged into a different one during
289 // linking, ignore it.
290 return ExecutorAddr();
291 }
292
293 SectionsPatched += 1;
294 if (isDwarfSection(Name))
295 HasDebugSections = true;
296 return SectionRange(*S).getStart();
297 });
298
299 if (Err)
300 return Err;
301 if (!SectionsPatched) {
302 LLVM_DEBUG(dbgs() << "Skipping debug registration for LinkGraph '"
303 << G.getName() << "': no debug info\n");
304 return Error::success();
305 }
306
307 if (RequireDebugSections && !HasDebugSections) {
308 LLVM_DEBUG(dbgs() << "Skipping debug registration for LinkGraph '"
309 << G.getName() << "': no debug info\n");
310 return Error::success();
311 }
312
313 // Step 3: We start copying the debug object into target memory
315
316 // FIXME: FA->getAddress() below is supposed to be the address of the memory
317 // range on the target, but InProcessMemoryManager returns the address of a
318 // FinalizedAllocInfo helper instead
319 auto ROSeg = Alloc.getSegInfo(MemProt::Read);
320 ExecutorAddrRange R(ROSeg.Addr, ROSeg.WorkingMem.size());
321 Alloc.finalize([this, R, &MR](Expected<DebugObject::FinalizedAlloc> FA) {
322 // Bail out if materialization failed in the meantime
323 std::lock_guard<std::mutex> Lock(PendingObjsLock);
324 auto It = PendingObjs.find(&MR);
325 if (It == PendingObjs.end()) {
326 if (!FA)
327 ES.reportError(FA.takeError());
328 return;
329 }
330
331 DebugObject *DebugObj = It->second.get();
332 if (!FA)
333 DebugObj->failMaterialization(FA.takeError());
334
335 // Keep allocation alive until the corresponding code is removed
336 DebugObj->trackFinalizedAlloc(std::move(*FA));
337
338 // Unblock post-fixup pass
339 DebugObj->reportTargetMem(R);
340 });
341
342 return Error::success();
343 });
344
345 PassConfig.PostFixupPasses.push_back([this, &MR](LinkGraph &G) -> Error {
346 // Step 4: We wait for the debug object copy to finish, so we can
347 // register the memory range with the GDB JIT Interface in an allocation
348 // action of the LinkGraph's own allocation
349 DebugObject *DebugObj = getPendingDebugObj(MR);
350 assert(DebugObj && "Don't inject passes if we have no debug object");
351 // Post-allocation phases would bail out if there is no debug section,
352 // in which case we wouldn't collect target memory and therefore shouldn't
353 // wait for the transaction to finish.
354 if (!DebugObj->hasPendingTargetMem())
355 return Error::success();
357 if (!R)
358 return R.takeError();
359
360 // Step 5: We have to keep the allocation alive until the corresponding
361 // code is removed
362 Error Err = MR.withResourceKeyDo([&](ResourceKey K) {
363 std::lock_guard<std::mutex> LockPending(PendingObjsLock);
364 std::lock_guard<std::mutex> LockRegistered(RegisteredObjsLock);
365 auto It = PendingObjs.find(&MR);
366 RegisteredObjs[K].push_back(std::move(It->second));
367 PendingObjs.erase(It);
368 });
369
370 if (Err)
371 return Err;
372
373 if (R->empty())
374 return Error::success();
375
376 using namespace shared;
377 G.allocActions().push_back(
378 {cantFail(WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddrRange>>(
379 RegistrationAction, *R)),
380 {/* no deregistration */}});
381 return Error::success();
382 });
383}
384
386 std::lock_guard<std::mutex> Lock(PendingObjsLock);
387 auto It = PendingObjs.find(&MR);
388 It->second->releasePendingResources();
389 PendingObjs.erase(It);
390 return Error::success();
391}
392
394 ResourceKey DstKey,
395 ResourceKey SrcKey) {
396 // Debug objects are stored by ResourceKey only after registration.
397 // Thus, pending objects don't need to be updated here.
398 std::lock_guard<std::mutex> Lock(RegisteredObjsLock);
399 auto SrcIt = RegisteredObjs.find(SrcKey);
400 if (SrcIt != RegisteredObjs.end()) {
401 // Resources from distinct MaterializationResponsibilitys can get merged
402 // after emission, so we can have multiple debug objects per resource key.
403 for (std::unique_ptr<DebugObject> &DebugObj : SrcIt->second)
404 RegisteredObjs[DstKey].push_back(std::move(DebugObj));
405 RegisteredObjs.erase(SrcIt);
406 }
407}
408
411 // Removing the resource for a pending object fails materialization, so they
412 // get cleaned up in the notifyFailed() handler.
413 std::lock_guard<std::mutex> Lock(RegisteredObjsLock);
414 RegisteredObjs.erase(Key);
415
416 // TODO: Implement unregister notifications.
417 return Error::success();
418}
419
420} // namespace orc
421} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define _
#define G(x, y, z)
Definition MD5.cpp:55
static bool isDwarfSection(const MCObjectFileInfo *FI, const MCSection *Section)
Provides a library for accessing information about this process and other processes on the operating ...
#define LLVM_DEBUG(...)
Definition Debug.h:119
size_t size() const
Get the array size.
Definition ArrayRef.h:141
Helper for Errors used as out-parameters.
Definition Error.h:1160
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
reference get()
Returns a reference to the stored T value.
Definition Error.h:582
size_t getBufferSize() const
StringRef getBuffer() const
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
static Expected< ELFFile > create(StringRef Object)
Definition ELF.h:1000
MutableArrayRef< char > getBuffer()
Error visitSectionLoadAddresses(GetLoadAddressFn Callback)
Expected< ExecutorAddrRange > awaitTargetMem()
void reportTargetMem(ExecutorAddrRange TargetMem)
SimpleSegmentAlloc collectTargetAlloc()
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
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.
Definition Core.h:1111
Represents an address in the executor process.
uint64_t getValue() const
Represents a JIT'd dynamic library.
Definition Core.h:675
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition Core.h:349
Error withResourceKeyDo(Func &&F) const
Runs the given callback under the session lock, passing in the associated ResourceKey.
Definition Core.h:368
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
Definition Process.h:62
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.
@ ELFDATA2MSB
Definition ELF.h:341
@ ELFDATA2LSB
Definition ELF.h:340
@ ELFCLASS64
Definition ELF.h:334
@ ELFCLASS32
Definition ELF.h:333
std::pair< unsigned char, unsigned char > getElfArchType(StringRef Object)
Definition ELF.h:82
static const std::set< StringRef > DwarfSectionNames
uintptr_t ResourceKey
Definition Core.h:60
static bool isDwarfSection(StringRef SectionName)
LLVM_ABI void lookupAndApply(unique_function< void(Error)> OnApplied, LookupKind K, const JITDylibSearchOrder &SearchOrder, ArrayRef< LookupPrepareFn > PrepareFns)
Resolve the symbols contributed by every prepare function with a single lookup, then let each of thei...
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.
Definition Error.h:1321
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.
Definition Debug.cpp:209
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.
Definition Error.h:769
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:1933
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
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.