Skip to content

Commit 845b1c5

Browse files
committed
Added testcase for the DWARF64 and review comments
1 parent a85d648 commit 845b1c5

File tree

4 files changed

+115
-33
lines changed

4 files changed

+115
-33
lines changed

lldb/source/Plugins/SymbolFile/DWARF/DWARFFormValue.cpp

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ bool DWARFFormValue::ExtractValue(const DWARFDataExtractor &data,
7777
case DW_FORM_strp:
7878
case DW_FORM_line_strp:
7979
case DW_FORM_sec_offset:
80-
if (m_unit->GetFormat() == DwarfFormat::DWARF32)
81-
m_value.uval = data.GetMaxU64(offset_ptr, 4);
82-
else if (m_unit->GetFormat() == DwarfFormat::DWARF64)
83-
m_value.uval = data.GetMaxU64(offset_ptr, 8);
80+
assert(m_unit); // Unit must be valid
81+
ref_addr_size = m_unit->GetFormParams().getDwarfOffsetByteSize();
82+
m_value.uval = data.GetMaxU64(offset_ptr, ref_addr_size);
8483
break;
8584
case DW_FORM_addrx1:
8685
case DW_FORM_strx1:
@@ -122,14 +121,7 @@ bool DWARFFormValue::ExtractValue(const DWARFDataExtractor &data,
122121
break;
123122
case DW_FORM_ref_addr:
124123
assert(m_unit);
125-
if (m_unit->GetVersion() <= 2)
126-
ref_addr_size = m_unit->GetAddressByteSize();
127-
else {
128-
if (m_unit->GetFormat() == DwarfFormat::DWARF32)
129-
ref_addr_size = 4;
130-
else if (m_unit->GetFormat() == DwarfFormat::DWARF64)
131-
ref_addr_size = 8;
132-
}
124+
ref_addr_size = m_unit->GetFormParams().getRefAddrByteSize();
133125
m_value.uval = data.GetMaxU64(offset_ptr, ref_addr_size);
134126
break;
135127
case DW_FORM_indirect:
@@ -176,12 +168,12 @@ static FormSize g_form_sizes[] = {
176168
{0, 0}, // 0x0f DW_FORM_udata
177169
{0, 0}, // 0x10 DW_FORM_ref_addr (addr size for DWARF2 and earlier, 4 bytes
178170
// for DWARF32, 8 bytes for DWARF32 in DWARF 3 and later
179-
{1, 1}, // 0x11 DW_FORM_ref1
180-
{1, 2}, // 0x12 DW_FORM_ref2
181-
{1, 4}, // 0x13 DW_FORM_ref4
182-
{1, 8}, // 0x14 DW_FORM_ref8
183-
{0, 0}, // 0x15 DW_FORM_ref_udata
184-
{0, 0}, // 0x16 DW_FORM_indirect
171+
{1, 1}, // 0x11 DW_FORM_ref1
172+
{1, 2}, // 0x12 DW_FORM_ref2
173+
{1, 4}, // 0x13 DW_FORM_ref4
174+
{1, 8}, // 0x14 DW_FORM_ref8
175+
{0, 0}, // 0x15 DW_FORM_ref_udata
176+
{0, 0}, // 0x16 DW_FORM_indirect
185177
{0,
186178
0}, // 0x17 DW_FORM_sec_offset (4 bytes for DWARF32, 8 bytes for DWARF64)
187179
{0, 0}, // 0x18 DW_FORM_exprloc
@@ -192,7 +184,7 @@ static FormSize g_form_sizes[] = {
192184
{0, 0}, // 0x1d DW_FORM_strp_sup (4 bytes for DWARF32, 8 bytes for DWARF64)
193185
{1, 16}, // 0x1e DW_FORM_data16
194186
{0, 0}, // 0x1f DW_FORM_line_strp (4 bytes for DWARF32, 8 bytes for DWARF64)
195-
{1, 8}, // 0x20 DW_FORM_ref_sig8
187+
{1, 8}, // 0x20 DW_FORM_ref_sig8
196188
};
197189

198190
std::optional<uint8_t> DWARFFormValue::GetFixedSize(dw_form_t form,
@@ -254,17 +246,9 @@ bool DWARFFormValue::SkipValue(dw_form_t form,
254246
return true;
255247

256248
case DW_FORM_ref_addr:
257-
ref_addr_size = 4;
258249
assert(unit); // Unit must be valid for DW_FORM_ref_addr objects or we will
259250
// get this wrong
260-
if (unit->GetVersion() <= 2)
261-
ref_addr_size = unit->GetAddressByteSize();
262-
else {
263-
if (unit->GetFormat() == DwarfFormat::DWARF32)
264-
ref_addr_size = 4;
265-
else if (unit->GetFormat() == DwarfFormat::DWARF64)
266-
ref_addr_size = 8;
267-
}
251+
ref_addr_size = unit->GetFormParams().getRefAddrByteSize();
268252
*offset_ptr += ref_addr_size;
269253
return true;
270254

@@ -300,10 +284,9 @@ bool DWARFFormValue::SkipValue(dw_form_t form,
300284
case DW_FORM_sec_offset:
301285
case DW_FORM_strp:
302286
case DW_FORM_line_strp:
303-
if (unit->GetFormat() == DwarfFormat::DWARF32)
304-
*offset_ptr += 4;
305-
else if (unit->GetFormat() == DwarfFormat::DWARF64)
306-
*offset_ptr += 8;
287+
assert(unit); // Unit must be valid
288+
ref_addr_size = unit->GetFormParams().getDwarfOffsetByteSize();
289+
*offset_ptr += ref_addr_size;
307290
return true;
308291

309292
// 4 byte values

lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ class DWARFUnit : public DWARFExpression::Delegate, public UserID {
119119
// Size of the CU data incl. header but without initial length.
120120
dw_offset_t GetLength() const { return m_header.getLength(); }
121121
uint16_t GetVersion() const override { return m_header.getVersion(); }
122-
llvm::dwarf::DwarfFormat GetFormat() const { return m_header.getFormat(); }
122+
const llvm::dwarf::FormParams &GetFormParams() const {
123+
return m_header.getFormParams();
124+
}
123125
const llvm::DWARFAbbreviationDeclarationSet *GetAbbreviations() const;
124126
dw_offset_t GetAbbrevOffset() const;
125127
uint8_t GetAddressByteSize() const override {

lldb/unittests/SymbolFile/DWARF/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_lldb_unittest(SymbolFileDWARFTests
66
DWARFUnitTest.cpp
77
SymbolFileDWARFTests.cpp
88
XcodeSDKModuleTests.cpp
9+
DWARF64UnitTest.cpp
910

1011
LINK_COMPONENTS
1112
Support
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//===-- DWARF64UnitTest.cpp--------------------------------------------=---===//
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 "Plugins/SymbolFile/DWARF/DWARFDebugInfo.h"
10+
#include "Plugins/SymbolFile/DWARF/DWARFUnit.h"
11+
#include "TestingSupport/Symbol/YAMLModuleTester.h"
12+
13+
using namespace lldb_private;
14+
using namespace lldb_private::dwarf;
15+
using namespace lldb_private::plugin::dwarf;
16+
17+
TEST(DWARF64UnitTest, DWARF64DebugInfoAndCU) {
18+
const char *yamldata = R"(
19+
--- !ELF
20+
FileHeader:
21+
Class: ELFCLASS64
22+
Data: ELFDATA2LSB
23+
Type: ET_EXEC
24+
Machine: EM_PPC64
25+
DWARF:
26+
debug_str:
27+
- 'clang version 18.1.8 (clang-18.1.8-1)'
28+
- 'main'
29+
debug_abbrev:
30+
- Table:
31+
- Code: 0x1
32+
Tag: DW_TAG_compile_unit
33+
Children: DW_CHILDREN_yes
34+
Attributes:
35+
- Attribute: DW_AT_producer
36+
Form: DW_FORM_strp
37+
- Attribute: DW_AT_language
38+
Form: DW_FORM_data2
39+
- Attribute: DW_AT_stmt_list
40+
Form: DW_FORM_sec_offset
41+
- Attribute: DW_AT_low_pc
42+
Form: DW_FORM_addr
43+
- Attribute: DW_AT_high_pc
44+
Form: DW_FORM_data4
45+
- Code: 0x02
46+
Tag: DW_TAG_subprogram
47+
Children: DW_CHILDREN_no
48+
Attributes:
49+
- Attribute: DW_AT_name
50+
Form: DW_FORM_strp
51+
debug_info:
52+
- Format: DWARF64
53+
Version: 4
54+
AbbrOffset: 0x0
55+
AddrSize: 8
56+
Entries:
57+
- AbbrCode: 0x1
58+
Values:
59+
- Value: 0x0
60+
- Value: 0x04
61+
- Value: 0x0
62+
- Value: 0xdeadbeef
63+
- Value: 0xdeadbeef
64+
- AbbrCode: 0x2
65+
Values:
66+
- Value: 0x1
67+
- AbbrCode: 0x0
68+
)";
69+
70+
YAMLModuleTester t(yamldata);
71+
auto *symbol_file =
72+
llvm::cast<SymbolFileDWARF>(t.GetModule()->GetSymbolFile());
73+
DWARFUnit *unit = symbol_file->DebugInfo().GetUnitAtIndex(0);
74+
ASSERT_TRUE(unit);
75+
ASSERT_EQ(unit->GetFormParams().Format, DwarfFormat::DWARF64);
76+
ASSERT_EQ(unit->GetVersion(), 4);
77+
ASSERT_EQ(unit->GetAddressByteSize(), 8);
78+
79+
DWARFFormValue form_value;
80+
const DWARFDebugInfoEntry *cu_entry = unit->DIE().GetDIE();
81+
ASSERT_EQ(cu_entry->Tag(), DW_TAG_compile_unit);
82+
ASSERT_EQ(unit->GetProducer(), eProducerClang);
83+
ASSERT_EQ(unit->GetDWARFLanguageType(), DW_LANG_C_plus_plus);
84+
auto attrs = cu_entry->GetAttributes(unit, DWARFDebugInfoEntry::Recurse::yes);
85+
attrs.ExtractFormValueAtIndex(2, form_value); // Validate DW_AT_stmt_list
86+
ASSERT_EQ(form_value.Unsigned(), 0UL);
87+
attrs.ExtractFormValueAtIndex(3, form_value); // Validate DW_AT_low_pc
88+
ASSERT_EQ(form_value.Unsigned(), 0xdeadbeef);
89+
attrs.ExtractFormValueAtIndex(4, form_value); // Validate DW_AT_high_pc
90+
ASSERT_EQ(form_value.Unsigned(), 0xdeadbeef);
91+
92+
DWARFDIE cu_die(unit, cu_entry);
93+
auto declaration = cu_die.GetFirstChild();
94+
ASSERT_TRUE(declaration.IsValid());
95+
ASSERT_EQ(declaration.Tag(), DW_TAG_subprogram);
96+
}

0 commit comments

Comments
 (0)