Skip to content

Commit 6dc4a10

Browse files
[NFC] Extract Printing portions of DWARFCFIProgram to a new file
CFIPrograms' most common uses are within debug frames, but it is not their only use. For example, some assembly writers encode them by hand into .cfi_escape directives. This PR extracts printing code for them into its own files, which avoids the need for the main class to depend on DWARFUnit, sections, and similar. One in a series of NFC DebugInfo/DWARF refactoring changes to layer it more cleanly, so that binary CFI parsing can be used from low-level code, (such as byte strings created via .cfi_escape) without circular dependencies. The final goal is to make a more limited dwarf library usable from lower-level code.
1 parent 8957e64 commit 6dc4a10

File tree

7 files changed

+173
-107
lines changed

7 files changed

+173
-107
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===- DWARFCFIPrinter.h ----------------------------------------*- 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+
#ifndef LLVM_DEBUGINFO_DWARF_DWARFCFIPRINTER_H
10+
#define LLVM_DEBUGINFO_DWARF_DWARFCFIPRINTER_H
11+
12+
#include "llvm/DebugInfo/DWARF/DWARFCFIProgram.h"
13+
14+
namespace llvm {
15+
16+
struct DIDumpOptions;
17+
18+
namespace dwarf {
19+
20+
// This class is separate from CFIProgram to decouple CFIPrograms from the
21+
// enclosing DWARF dies and type units, which allows using them in lower-level
22+
// places without build dependencies.
23+
24+
class CFIPrinter {
25+
public:
26+
static void print(const CFIProgram &P, raw_ostream &OS, DIDumpOptions DumpOpts,
27+
unsigned IndentLevel, std::optional<uint64_t> Address);
28+
29+
static void printOperand(raw_ostream &OS, DIDumpOptions DumpOpts,
30+
const CFIProgram &P,
31+
const CFIProgram::Instruction &Instr,
32+
unsigned OperandIdx, uint64_t Operand,
33+
std::optional<uint64_t> &Address);
34+
};
35+
36+
} // end namespace dwarf
37+
38+
} // end namespace llvm
39+
40+
#endif // LLVM_DEBUGINFO_DWARF_DWARFCFIPRINTER_H

llvm/include/llvm/DebugInfo/DWARF/DWARFCFIProgram.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
namespace llvm {
2525

2626
namespace dwarf {
27+
28+
class CFIPrinter;
29+
2730
/// Represent a sequence of Call Frame Information instructions that, when read
2831
/// in order, construct a table mapping PC to frame state. This can also be
2932
/// referred to as "CFI rules" in DWARF literature to avoid confusion with
@@ -34,6 +37,7 @@ class CFIProgram {
3437
public:
3538
static constexpr size_t MaxOperands = 3;
3639
typedef SmallVector<uint64_t, MaxOperands> Operands;
40+
friend CFIPrinter;
3741

3842
/// An instruction consists of a DWARF CFI opcode and an optional sequence of
3943
/// operands. If it refers to an expression, then this expression has its own
@@ -80,10 +84,6 @@ class CFIProgram {
8084
LLVM_ABI Error parse(DWARFDataExtractor Data, uint64_t *Offset,
8185
uint64_t EndOffset);
8286

83-
LLVM_ABI void dump(raw_ostream &OS, DIDumpOptions DumpOpts,
84-
unsigned IndentLevel,
85-
std::optional<uint64_t> InitialLocation) const;
86-
8787
void addInstruction(const Instruction &I) { Instructions.push_back(I); }
8888

8989
/// Get a DWARF CFI call frame string for the given DW_CFA opcode.
@@ -147,11 +147,6 @@ class CFIProgram {
147147
/// Retrieve the array describing the types of operands according to the enum
148148
/// above. This is indexed by opcode.
149149
static ArrayRef<OperandType[MaxOperands]> getOperandTypes();
150-
151-
/// Print \p Opcode's operand number \p OperandIdx which has value \p Operand.
152-
void printOperand(raw_ostream &OS, DIDumpOptions DumpOpts,
153-
const Instruction &Instr, unsigned OperandIdx,
154-
uint64_t Operand, std::optional<uint64_t> &Address) const;
155150
};
156151

157152
} // end namespace dwarf

llvm/lib/DebugInfo/DWARF/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ add_llvm_component_library(LLVMDebugInfoDWARF
22
DWARFAbbreviationDeclaration.cpp
33
DWARFAddressRange.cpp
44
DWARFAcceleratorTable.cpp
5+
DWARFCFIPrinter.cpp
56
DWARFCFIProgram.cpp
67
DWARFCompileUnit.cpp
78
DWARFContext.cpp
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
//===- DWARFCFIPrinter.cpp - Print the cfi-portions of .debug_frame -------===//
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+
#include "llvm/DebugInfo/DWARF/DWARFCFIPrinter.h"
10+
#include "llvm/DebugInfo/DIContext.h"
11+
#include "llvm/DebugInfo/DWARF/DWARFCFIProgram.h"
12+
#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
13+
#include "llvm/Support/Compiler.h"
14+
#include "llvm/Support/DataExtractor.h"
15+
#include "llvm/Support/Errc.h"
16+
#include "llvm/Support/ErrorHandling.h"
17+
#include "llvm/Support/Format.h"
18+
#include "llvm/Support/raw_ostream.h"
19+
#include <cassert>
20+
#include <cinttypes>
21+
#include <cstdint>
22+
#include <optional>
23+
24+
using namespace llvm;
25+
using namespace dwarf;
26+
27+
28+
void CFIPrinter::print(const CFIProgram &P, raw_ostream &OS,
29+
DIDumpOptions DumpOpts, unsigned IndentLevel,
30+
std::optional<uint64_t> Address) {
31+
for (const auto &Instr : P) {
32+
uint8_t Opcode = Instr.Opcode;
33+
OS.indent(2 * IndentLevel);
34+
OS << P.callFrameString(Opcode) << ":";
35+
for (size_t i = 0; i < Instr.Ops.size(); ++i)
36+
printOperand(OS, DumpOpts, P, Instr, i, Instr.Ops[i], Address);
37+
OS << '\n';
38+
}
39+
}
40+
41+
static void printRegister(raw_ostream &OS, DIDumpOptions DumpOpts,
42+
unsigned RegNum) {
43+
if (DumpOpts.GetNameForDWARFReg) {
44+
auto RegName = DumpOpts.GetNameForDWARFReg(RegNum, DumpOpts.IsEH);
45+
if (!RegName.empty()) {
46+
OS << RegName;
47+
return;
48+
}
49+
}
50+
OS << "reg" << RegNum;
51+
}
52+
53+
/// Print \p Opcode's operand number \p OperandIdx which has value \p Operand.
54+
void CFIPrinter::printOperand(raw_ostream &OS, DIDumpOptions DumpOpts,
55+
const CFIProgram &P,
56+
const CFIProgram::Instruction &Instr,
57+
unsigned OperandIdx, uint64_t Operand,
58+
std::optional<uint64_t> &Address) {
59+
assert(OperandIdx < CFIProgram::MaxOperands);
60+
uint8_t Opcode = Instr.Opcode;
61+
CFIProgram::OperandType Type = P.getOperandTypes()[Opcode][OperandIdx];
62+
63+
switch (Type) {
64+
case CFIProgram::OT_Unset: {
65+
OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to";
66+
auto OpcodeName = P.callFrameString(Opcode);
67+
if (!OpcodeName.empty())
68+
OS << " " << OpcodeName;
69+
else
70+
OS << format(" Opcode %x", Opcode);
71+
break;
72+
}
73+
case CFIProgram::OT_None:
74+
break;
75+
case CFIProgram::OT_Address:
76+
OS << format(" %" PRIx64, Operand);
77+
Address = Operand;
78+
break;
79+
case CFIProgram::OT_Offset:
80+
// The offsets are all encoded in a unsigned form, but in practice
81+
// consumers use them signed. It's most certainly legacy due to
82+
// the lack of signed variants in the first Dwarf standards.
83+
OS << format(" %+" PRId64, int64_t(Operand));
84+
break;
85+
case CFIProgram::OT_FactoredCodeOffset: // Always Unsigned
86+
if (P.CodeAlignmentFactor)
87+
OS << format(" %" PRId64, Operand * P.CodeAlignmentFactor);
88+
else
89+
OS << format(" %" PRId64 "*code_alignment_factor", Operand);
90+
if (Address && P.CodeAlignmentFactor) {
91+
*Address += Operand * P.CodeAlignmentFactor;
92+
OS << format(" to 0x%" PRIx64, *Address);
93+
}
94+
break;
95+
case CFIProgram::OT_SignedFactDataOffset:
96+
if (P.DataAlignmentFactor)
97+
OS << format(" %" PRId64, int64_t(Operand) * P.DataAlignmentFactor);
98+
else
99+
OS << format(" %" PRId64 "*data_alignment_factor", int64_t(Operand));
100+
break;
101+
case CFIProgram::OT_UnsignedFactDataOffset:
102+
if (P.DataAlignmentFactor)
103+
OS << format(" %" PRId64, Operand * P.DataAlignmentFactor);
104+
else
105+
OS << format(" %" PRId64 "*data_alignment_factor", Operand);
106+
break;
107+
case CFIProgram::OT_Register:
108+
OS << ' ';
109+
printRegister(OS, DumpOpts, Operand);
110+
break;
111+
case CFIProgram::OT_AddressSpace:
112+
OS << format(" in addrspace%" PRId64, Operand);
113+
break;
114+
case CFIProgram::OT_Expression:
115+
assert(Instr.Expression && "missing DWARFExpression object");
116+
OS << " ";
117+
DWARFExpressionPrinter::print(&Instr.Expression.value(), OS, DumpOpts,
118+
nullptr);
119+
break;
120+
}
121+
}

llvm/lib/DebugInfo/DWARF/DWARFCFIProgram.cpp

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@
2323
using namespace llvm;
2424
using namespace dwarf;
2525

26-
static void printRegister(raw_ostream &OS, DIDumpOptions DumpOpts,
27-
unsigned RegNum) {
28-
if (DumpOpts.GetNameForDWARFReg) {
29-
auto RegName = DumpOpts.GetNameForDWARFReg(RegNum, DumpOpts.IsEH);
30-
if (!RegName.empty()) {
31-
OS << RegName;
32-
return;
33-
}
34-
}
35-
OS << "reg" << RegNum;
36-
}
37-
3826
// See DWARF standard v3, section 7.23
3927
const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0;
4028
const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f;
@@ -361,85 +349,3 @@ CFIProgram::getOperandTypes() {
361349

362350
return ArrayRef<OperandType[MaxOperands]>(&OpTypes[0], DW_CFA_restore + 1);
363351
}
364-
365-
/// Print \p Opcode's operand number \p OperandIdx which has value \p Operand.
366-
void CFIProgram::printOperand(raw_ostream &OS, DIDumpOptions DumpOpts,
367-
const Instruction &Instr, unsigned OperandIdx,
368-
uint64_t Operand,
369-
std::optional<uint64_t> &Address) const {
370-
assert(OperandIdx < MaxOperands);
371-
uint8_t Opcode = Instr.Opcode;
372-
OperandType Type = getOperandTypes()[Opcode][OperandIdx];
373-
374-
switch (Type) {
375-
case OT_Unset: {
376-
OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to";
377-
auto OpcodeName = callFrameString(Opcode);
378-
if (!OpcodeName.empty())
379-
OS << " " << OpcodeName;
380-
else
381-
OS << format(" Opcode %x", Opcode);
382-
break;
383-
}
384-
case OT_None:
385-
break;
386-
case OT_Address:
387-
OS << format(" %" PRIx64, Operand);
388-
Address = Operand;
389-
break;
390-
case OT_Offset:
391-
// The offsets are all encoded in a unsigned form, but in practice
392-
// consumers use them signed. It's most certainly legacy due to
393-
// the lack of signed variants in the first Dwarf standards.
394-
OS << format(" %+" PRId64, int64_t(Operand));
395-
break;
396-
case OT_FactoredCodeOffset: // Always Unsigned
397-
if (CodeAlignmentFactor)
398-
OS << format(" %" PRId64, Operand * CodeAlignmentFactor);
399-
else
400-
OS << format(" %" PRId64 "*code_alignment_factor", Operand);
401-
if (Address && CodeAlignmentFactor) {
402-
*Address += Operand * CodeAlignmentFactor;
403-
OS << format(" to 0x%" PRIx64, *Address);
404-
}
405-
break;
406-
case OT_SignedFactDataOffset:
407-
if (DataAlignmentFactor)
408-
OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor);
409-
else
410-
OS << format(" %" PRId64 "*data_alignment_factor", int64_t(Operand));
411-
break;
412-
case OT_UnsignedFactDataOffset:
413-
if (DataAlignmentFactor)
414-
OS << format(" %" PRId64, Operand * DataAlignmentFactor);
415-
else
416-
OS << format(" %" PRId64 "*data_alignment_factor", Operand);
417-
break;
418-
case OT_Register:
419-
OS << ' ';
420-
printRegister(OS, DumpOpts, Operand);
421-
break;
422-
case OT_AddressSpace:
423-
OS << format(" in addrspace%" PRId64, Operand);
424-
break;
425-
case OT_Expression:
426-
assert(Instr.Expression && "missing DWARFExpression object");
427-
OS << " ";
428-
DWARFExpressionPrinter::print(&Instr.Expression.value(), OS, DumpOpts,
429-
nullptr);
430-
break;
431-
}
432-
}
433-
434-
void CFIProgram::dump(raw_ostream &OS, DIDumpOptions DumpOpts,
435-
unsigned IndentLevel,
436-
std::optional<uint64_t> Address) const {
437-
for (const auto &Instr : Instructions) {
438-
uint8_t Opcode = Instr.Opcode;
439-
OS.indent(2 * IndentLevel);
440-
OS << callFrameString(Opcode) << ":";
441-
for (unsigned i = 0; i < Instr.Ops.size(); ++i)
442-
printOperand(OS, DumpOpts, Instr, i, Instr.Ops[i], Address);
443-
OS << '\n';
444-
}
445-
}

llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/ADT/StringRef.h"
1313
#include "llvm/BinaryFormat/Dwarf.h"
1414
#include "llvm/DebugInfo/DIContext.h"
15+
#include "llvm/DebugInfo/DWARF/DWARFCFIPrinter.h"
1516
#include "llvm/DebugInfo/DWARF/DWARFCFIProgram.h"
1617
#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
1718
#include "llvm/DebugInfo/DWARF/DWARFExpression.h"
@@ -602,7 +603,8 @@ void CIE::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
602603
OS << "\n";
603604
}
604605
OS << "\n";
605-
CFIs.dump(OS, DumpOpts, /*IndentLevel=*/1, /*InitialLocation=*/{});
606+
CFIPrinter::print(CFIs, OS, DumpOpts, /*IndentLevel=*/1,
607+
/*InitialLocation=*/{});
606608
OS << "\n";
607609

608610
if (Expected<UnwindTable> RowsOrErr = UnwindTable::create(this))
@@ -630,7 +632,7 @@ void FDE::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
630632
OS << " Format: " << FormatString(IsDWARF64) << "\n";
631633
if (LSDAAddress)
632634
OS << format(" LSDA Address: %016" PRIx64 "\n", *LSDAAddress);
633-
CFIs.dump(OS, DumpOpts, /*IndentLevel=*/1, InitialLocation);
635+
CFIPrinter::print(CFIs, OS, DumpOpts, /*IndentLevel=*/1, InitialLocation);
634636
OS << "\n";
635637

636638
if (Expected<UnwindTable> RowsOrErr = UnwindTable::create(this))

llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm-readobj.h"
1313
#include "llvm/ADT/STLExtras.h"
1414
#include "llvm/BinaryFormat/Dwarf.h"
15+
#include "llvm/DebugInfo/DWARF/DWARFCFIPrinter.h"
1516
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
1617
#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
1718
#include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
@@ -228,8 +229,8 @@ void PrinterContext<ELFT>::printEHFrame(const Elf_Shdr *EHFrameShdr) const {
228229
W.indent();
229230
auto DumpOpts = DIDumpOptions();
230231
DumpOpts.IsEH = true;
231-
Entry.cfis().dump(W.getOStream(), DumpOpts, W.getIndentLevel(),
232-
InitialLocation);
232+
dwarf::CFIPrinter::print(Entry.cfis(), W.getOStream(), DumpOpts,
233+
W.getIndentLevel(), InitialLocation);
233234
W.unindent();
234235
W.unindent();
235236
W.getOStream() << "\n";

0 commit comments

Comments
 (0)