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