LLVM 22.0.0git
DebugInfoSupport.cpp
Go to the documentation of this file.
1//===--- DebugInfoSupport.cpp -- Utils for debug info support ---*- C++ -*-===//
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// Utilities to preserve and parse debug info from LinkGraphs.
10//
11//===----------------------------------------------------------------------===//
12
14
16
17#define DEBUG_TYPE "orc"
18
19using namespace llvm;
20using namespace llvm::orc;
21using namespace llvm::jitlink;
22
23namespace {
24static DenseSet<StringRef> DWARFSectionNames = {
25#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \
26 StringRef(ELF_NAME),
27#include "llvm/BinaryFormat/Dwarf.def"
28#undef HANDLE_DWARF_SECTION
29};
30
31// We might be able to drop relocations to symbols that do end up
32// being pruned by the linker, but for now we just preserve all
33static void preserveDWARFSection(LinkGraph &G, Section &Sec) {
35 for (auto Sym : Sec.symbols()) {
36 auto [It, Inserted] = Preserved.try_emplace(&Sym->getBlock());
37 if (Inserted || Sym->isLive())
38 It->second = Sym;
39 }
40 for (auto Block : Sec.blocks()) {
41 auto &PSym = Preserved[Block];
42 if (!PSym)
43 PSym = &G.addAnonymousSymbol(*Block, 0, 0, false, true);
44 else if (!PSym->isLive())
45 PSym->setLive(true);
46 }
47}
48
49static SmallVector<char, 0> getSectionData(Section &Sec) {
51 SmallVector<Block *, 8> SecBlocks(Sec.blocks().begin(), Sec.blocks().end());
52 std::sort(SecBlocks.begin(), SecBlocks.end(), [](Block *LHS, Block *RHS) {
53 return LHS->getAddress() < RHS->getAddress();
54 });
55 // Convert back to what object file would have, one blob of section content
56 // Assumes all zerofill
57 // TODO handle alignment?
58 // TODO handle alignment offset?
59 for (auto *Block : SecBlocks) {
60 if (Block->isZeroFill())
61 SecData.resize(SecData.size() + Block->getSize(), 0);
62 else
63 SecData.append(Block->getContent().begin(), Block->getContent().end());
64 }
65 return SecData;
66}
67
68static void dumpDWARFContext(DWARFContext &DC) {
69 auto options = llvm::DIDumpOptions();
70 options.DumpType &= ~DIDT_UUID;
71 options.DumpType &= ~(1 << DIDT_ID_DebugFrame);
72 LLVM_DEBUG(DC.dump(dbgs(), options));
73}
74
75} // namespace
76
78 if (!G.getTargetTriple().isOSBinFormatELF()) {
80 "preserveDebugSections only supports ELF LinkGraphs!",
82 }
83 for (auto &Sec : G.sections()) {
84 if (DWARFSectionNames.count(Sec.getName())) {
85 LLVM_DEBUG(dbgs() << "Preserving DWARF section " << Sec.getName()
86 << "\n");
87 preserveDWARFSection(G, Sec);
88 }
89 }
90 return Error::success();
91}
92
96 if (!G.getTargetTriple().isOSBinFormatELF()) {
98 "createDWARFContext only supports ELF LinkGraphs!",
100 }
102 for (auto &Sec : G.sections()) {
103 if (DWARFSectionNames.count(Sec.getName())) {
104 auto SecData = getSectionData(Sec);
105 auto Name = Sec.getName();
106 // DWARFContext expects the section name to not start with a dot
107 Name.consume_front(".");
108 LLVM_DEBUG(dbgs() << "Creating DWARFContext section " << Name
109 << " with size " << SecData.size() << "\n");
110 DWARFSectionData[Name] =
111 std::make_unique<SmallVectorMemoryBuffer>(std::move(SecData));
112 }
113 }
114 auto Ctx =
115 DWARFContext::create(DWARFSectionData, G.getPointerSize(),
116 G.getEndianness() == llvm::endianness::little);
117 dumpDWARFContext(*Ctx);
118 return std::make_pair(std::move(Ctx), std::move(DWARFSectionData));
119}
#define G(x, y, z)
Definition MD5.cpp:56
#define LLVM_DEBUG(...)
Definition Debug.h:114
Value * RHS
Value * LHS
iterator end() const
Definition ArrayRef.h:136
iterator begin() const
Definition ArrayRef.h:135
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
void dump(raw_ostream &OS, DIDumpOptions DumpOpts, std::array< std::optional< uint64_t >, DIDT_ID_Count > DumpOffsets)
Dump a textual representation to OS.
static std::unique_ptr< DWARFContext > create(const object::ObjectFile &Obj, ProcessDebugRelocations RelocAction=ProcessDebugRelocations::Process, const LoadedObjectInfo *L=nullptr, std::string DWPName="", std::function< void(Error)> RecoverableErrorHandler=WithColor::defaultErrorHandler, std::function< void(Error)> WarningHandler=WithColor::defaultWarningHandler, bool ThreadSafe=false)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:229
Implements a dense probed hash-table based set.
Definition DenseSet.h:269
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
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
IteratorT end() const
IteratorT begin() const
LLVM_ABI Error preserveDebugSections(jitlink::LinkGraph &G)
LLVM_ABI Expected< std::pair< std::unique_ptr< DWARFContext >, StringMap< std::unique_ptr< MemoryBuffer > > > > createDWARFContext(jitlink::LinkGraph &G)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:98
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
@ DIDT_UUID
Definition DIContext.h:191
Container for dump options that control which debug information will be dumped.
Definition DIContext.h:196